From 3f5aeee1c6836a127de37333829795439e14e4a7 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 17 Oct 2012 12:56:41 -0400 Subject: [PATCH] calibration: add Caldac.allocate() for stand-alone caldac instances. --- pycomedi/calibration.pxd | 1 + pycomedi/calibration.pyx | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/pycomedi/calibration.pxd b/pycomedi/calibration.pxd index 047bf0b..cbd827b 100644 --- a/pycomedi/calibration.pxd +++ b/pycomedi/calibration.pxd @@ -35,6 +35,7 @@ cdef class CalibratedConverter (object): cdef class Caldac (object): cdef _comedilib_h.comedi_caldac_t *caldac + cdef bint _local cdef class CalibrationSetting (object): diff --git a/pycomedi/calibration.pyx b/pycomedi/calibration.pyx index 6e688b3..49f0028 100644 --- a/pycomedi/calibration.pyx +++ b/pycomedi/calibration.pyx @@ -234,9 +234,32 @@ cdef class Caldac (object): >>> d.close() + + You can also allocate `Caldac` instances on your own. The + allocated memory will be automatically freed when the instance is + garbage collected. + + >>> caldac = Caldac() + >>> caldac.subdevice == None + True + >>> caldac.subdevice = 1 + Traceback (most recent call last): + ... + AssertionError: load caldac first + >>> caldac.allocate() + >>> caldac.subdevice + 0L + >>> caldac.subdevice = 1 """ def __cinit__(self): self.caldac = NULL + self._local = False + + def __dealloc__(self): + if self.caldac is not NULL and self._local: + _stdlib.free(self.caldac) + self.caldac = NULL + self._local = False def __str__(self): fields = ['{}:{}'.format(f, getattr(self, f)) @@ -246,6 +269,17 @@ cdef class Caldac (object): def __repr__(self): return self.__str__() + def allocate(self): + assert not self._local, 'already allocated' + self.caldac = <_comedilib_h.comedi_caldac_t *> _stdlib.malloc( + sizeof(_comedilib_h.comedi_caldac_t *)) + if self.caldac is NULL: + raise MemoryError() + self._local = True + self.subdevice = 0 + self.channel = 0 + self.value = 0 + def _subdevice_get(self): if self.caldac is not NULL: return self.caldac.subdevice -- 2.26.2