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

fijal at codespeak.net fijal at codespeak.net
Mon Feb 18 21:53:23 CET 2008


Author: fijal
Date: Mon Feb 18 21:53:22 2008
New Revision: 51607

Modified:
   pypy/dist/pypy/lib/_ctypes/function.py
Log:
keepalive logic for functions.


Modified: pypy/dist/pypy/lib/_ctypes/function.py
==============================================================================
--- pypy/dist/pypy/lib/_ctypes/function.py	(original)
+++ pypy/dist/pypy/lib/_ctypes/function.py	Mon Feb 18 21:53:22 2008
@@ -19,6 +19,7 @@
     _restype_ = None
     _ffiletter = 'P'
     _ffishape = 'P'
+    _needs_free = False
 
     def _getargtypes(self):
         return self._argtypes_
@@ -49,6 +50,7 @@
             argtypes = [arg._ffiletter for arg in self._argtypes_]
             restype = self._restype_._ffiletter
             self._ptr = _rawffi.CallbackPtr(argument, argtypes, restype)
+            self._needs_free = True
             self._buffer = self._ptr.byptr()
         elif isinstance(argument, tuple) and len(argument) == 2:
             import ctypes
@@ -118,3 +120,11 @@
                     zip(argtypes, args)]
         except (UnicodeError, TypeError), e:
             raise ArgumentError(str(e))
+
+    def __del__(self):
+        if self._needs_free:
+            self._buffer.free()
+            self._buffer = None
+            self._ptr.free()
+            self._ptr = None
+            self._needs_free = False



More information about the Pypy-commit mailing list