calibration: add Caldac.allocate() for stand-alone caldac instances.
authorW. Trevor King <wking@tremily.us>
Wed, 17 Oct 2012 16:56:41 +0000 (12:56 -0400)
committerW. Trevor King <wking@tremily.us>
Wed, 17 Oct 2012 17:05:13 +0000 (13:05 -0400)
pycomedi/calibration.pxd
pycomedi/calibration.pyx

index 047bf0bd8d6ee8d583b71890041621589379d142..cbd827b27e07595e3ecb05f5a727eafb00cbb0c0 100644 (file)
@@ -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):
index 6e688b375b3fd86449ded838d0eb813aa002665d..49f0028526b4a592d5ff2e2c9f04670e19cdd818 100644 (file)
@@ -234,9 +234,32 @@ cdef class Caldac (object):
     <Caldac subdevice:5 channel:4 value:255>
 
     >>> 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