[pypy-svn] r51437 - pypy/dist/pypy/lib/_ctypes

fijal at codespeak.net fijal at codespeak.net
Wed Feb 13 14:21:34 CET 2008


Author: fijal
Date: Wed Feb 13 14:21:34 2008
New Revision: 51437

Modified:
   pypy/dist/pypy/lib/_ctypes/array.py
   pypy/dist/pypy/lib/_ctypes/basics.py
   pypy/dist/pypy/lib/_ctypes/pointer.py
   pypy/dist/pypy/lib/_ctypes/structure.py
Log:
Introduce some more dels


Modified: pypy/dist/pypy/lib/_ctypes/array.py
==============================================================================
--- pypy/dist/pypy/lib/_ctypes/array.py	(original)
+++ pypy/dist/pypy/lib/_ctypes/array.py	Wed Feb 13 14:21:34 2008
@@ -105,9 +105,12 @@
 class Array(_CData):
     __metaclass__ = ArrayMeta
     _ffiletter = 'P'
+    _needs_free = False
 
     def __init__(self, *args):
         self._buffer = self._ffiarray(self._length_)
+        self._needs_free = True
+        self._objects = []
         for i, arg in enumerate(args):
             self[i] = arg
 
@@ -133,6 +136,7 @@
         if isinstance(index, slice):
             self._slice_setitem(index, value)
             return
+        self._objects.append(value) # keepalive value
         value = self._type_._CData_input(value)
         index = self._fix_index(index)
         if not isinstance(self._type_._ffishape, tuple):
@@ -156,6 +160,12 @@
     def _get_buffer_for_param(self):
         return self._buffer.byptr()
 
+    def __del__(self):
+        if self._needs_free:
+            self._buffer.free()
+            self._buffer = None
+            self._needs_free = False
+
 ARRAY_CACHE = {}
 
 def create_array_type(base, length):

Modified: pypy/dist/pypy/lib/_ctypes/basics.py
==============================================================================
--- pypy/dist/pypy/lib/_ctypes/basics.py	(original)
+++ pypy/dist/pypy/lib/_ctypes/basics.py	Wed Feb 13 14:21:34 2008
@@ -99,6 +99,7 @@
     # fix the address, in case it's unsigned
     address = address & (sys.maxint * 2 + 1)
     instance = self.__new__(self)
+    instance._objects = []
     lgt = getattr(self, '_length_', 1)
     instance._buffer = self._ffiarray.fromaddress(address, lgt)
     return instance

Modified: pypy/dist/pypy/lib/_ctypes/pointer.py
==============================================================================
--- pypy/dist/pypy/lib/_ctypes/pointer.py	(original)
+++ pypy/dist/pypy/lib/_ctypes/pointer.py	Wed Feb 13 14:21:34 2008
@@ -55,6 +55,7 @@
         ffiarray = _rawffi.Array('P')
         def __init__(self, value=None):
             self._buffer = ffiarray(1)
+            self._needs_free = True
             self._objects = [value] # keepalive value
             if value is not None:
                 self.contents = value
@@ -66,6 +67,7 @@
 
 class _Pointer(_CData):
     __metaclass__ = PointerType
+    _needs_free = False
 
     def getcontents(self):
         addr = self._buffer[0]
@@ -78,6 +80,7 @@
             raise TypeError("expected %s instead of %s" % (
                 self._type_.__name__, type(value).__name__))
         value = value._buffer
+        self._objects = [value]
         self._buffer[0] = value
 
     _get_slice_params = array_get_slice_params
@@ -96,11 +99,18 @@
         return self._type_._CData_output(self._subarray(index))
 
     def __setitem__(self, index, value):
+        self._objects = [value]
         self._subarray(index)[0] = self._type_._CData_input(value)[0]
 
     def __nonzero__(self):
         return self._buffer[0] != 0
 
+    def __del__(self):
+        if self._needs_free:
+            self._buffer.free()
+            self._needs_free = False
+            self._buffer = None
+
     contents = property(getcontents, setcontents)
 
 

Modified: pypy/dist/pypy/lib/_ctypes/structure.py
==============================================================================
--- pypy/dist/pypy/lib/_ctypes/structure.py	(original)
+++ pypy/dist/pypy/lib/_ctypes/structure.py	Wed Feb 13 14:21:34 2008
@@ -107,6 +107,7 @@
             if not hasattr(self, '_ffistruct'):
                 raise TypeError("Cannot instantiate structure, has no _fields_")
             self.__dict__['_buffer'] = self._ffistruct()
+            self.__dict__['_needs_free'] = True
             if len(args) > len(self._names):
                 raise TypeError("too many arguments")
             for name, arg in zip(self._names, args):
@@ -146,6 +147,7 @@
 class Structure(_CData):
     __metaclass__ = StructureMeta
     _ffiletter = 'P'
+    _needs_free = False
 
     def _subarray(self, fieldtype, name):
         """Return a _rawffi array of length 1 whose address is the same as
@@ -173,3 +175,9 @@
 
     def _get_buffer_for_param(self):
         return self._buffer.byptr()
+
+    def __del__(self):
+        if self._needs_free:
+            self._buffer.free()
+            self.__dict__['_buffer'] = None
+            self.__dict__['_needs_free'] = False



More information about the Pypy-commit mailing list