Skip to content

Commit 58f23f3

Browse files
authored
Merge pull request #102 from pelson/clean_cleanup
Added a reference to the unit system inside the Unit class
2 parents a505913 + 89340bb commit 58f23f3

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

cf_units/_udunits2.pyx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,11 @@ cdef class Unit:
7272
7373
"""
7474
cdef ut_unit *cunit
75+
cdef System system
7576

7677
def __cinit__(self):
7778
self.cunit = NULL
79+
self.system = None
7880

7981
def __dealloc__(self):
8082
ut_free(self.cunit)
@@ -116,11 +118,12 @@ cdef System wrap_system(ut_system* csystem):
116118
sytem.csystem = csystem
117119
return sytem
118120

119-
cdef Unit wrap_unit(ut_unit* cunit):
121+
cdef Unit wrap_unit(System system, ut_unit* cunit):
120122
if cunit is NULL:
121123
_raise_error()
122124
cdef Unit unit = Unit()
123125
unit.cunit = cunit
126+
unit.system = system
124127
return unit
125128

126129
cdef Converter wrap_converter(cv_converter* cconverter):
@@ -204,11 +207,11 @@ def read_xml(char* path=NULL):
204207

205208
def get_unit_by_name(System system, char* name):
206209
cdef ut_unit* cunit = ut_get_unit_by_name(system.csystem, name)
207-
return wrap_unit(cunit)
210+
return wrap_unit(system, cunit)
208211

209212
def clone(Unit unit):
210213
cdef ut_unit* cunit = ut_clone(unit.cunit)
211-
return wrap_unit(cunit)
214+
return wrap_unit(unit.system, cunit)
212215

213216
def is_dimensionless(Unit unit):
214217
return <bint>ut_is_dimensionless(unit.cunit)
@@ -225,43 +228,44 @@ def get_converter(Unit fr, Unit to):
225228

226229
def scale(double factor, Unit unit):
227230
cdef ut_unit* cunit = ut_scale(factor, unit.cunit)
228-
return wrap_unit(cunit)
231+
return wrap_unit(unit.system, cunit)
229232

230233
def offset(Unit unit, double offset):
231234
cdef ut_unit* cunit = ut_offset(unit.cunit, offset)
232-
return wrap_unit(cunit)
235+
return wrap_unit(unit.system, cunit)
233236

234237
def offset_by_time(Unit unit, double origin):
235238
cdef ut_unit* cunit = ut_offset_by_time(unit.cunit, origin)
236-
return wrap_unit(cunit)
239+
return wrap_unit(unit.system, cunit)
237240

238241
def multiply(Unit unit1, Unit unit2):
239242
cdef ut_unit* cunit = ut_multiply(unit1.cunit, unit2.cunit)
240-
return wrap_unit(cunit)
243+
return wrap_unit(unit1.system, cunit)
241244

242245
def invert(Unit unit):
243246
cdef ut_unit* cunit = ut_invert(unit.cunit)
244-
return wrap_unit(cunit)
247+
return wrap_unit(unit.system, cunit)
245248

246249
def divide(Unit numer, Unit denom):
247250
cdef ut_unit* cunit = ut_divide(numer.cunit, denom.cunit)
248-
return wrap_unit(cunit)
251+
return wrap_unit(numer.system, cunit)
249252

250253
def raise_(Unit unit, int power):
251254
cdef ut_unit* cunit = ut_raise(unit.cunit, power)
252-
return wrap_unit(cunit)
255+
return wrap_unit(unit.system, cunit)
253256

254257
def root(Unit unit, int root):
255258
cdef ut_unit* cunit = ut_root(unit.cunit, root)
256-
return wrap_unit(cunit)
259+
return wrap_unit(unit.system, cunit)
257260

258261
def log(double base, Unit reference):
259262
cdef ut_unit* cunit = ut_log(base, reference.cunit)
260-
return wrap_unit(cunit)
263+
return wrap_unit(reference.system, cunit)
264+
261265

262266
def parse(System system, char* string, ut_encoding encoding):
263267
cdef ut_unit* cunit = ut_parse(system.csystem, string, encoding)
264-
return wrap_unit(cunit)
268+
return wrap_unit(system, cunit)
265269

266270
def format(Unit unit, unsigned opts=0):
267271
cdef bytearray buf = bytearray(_STRING_BUFFER_DEPTH)

0 commit comments

Comments
 (0)