[pypy-svn] r73654 - in pypy/branch/cpython-extension/pypy/module/cpyext: . test

xoraxax at codespeak.net xoraxax at codespeak.net
Sun Apr 11 22:14:06 CEST 2010


Author: xoraxax
Date: Sun Apr 11 22:14:04 2010
New Revision: 73654

Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/pycobject.py
   pypy/branch/cpython-extension/pypy/module/cpyext/pystate.py
   pypy/branch/cpython-extension/pypy/module/cpyext/test/test_pycobject.py
Log:
Add PyCObject_FromVoidPtrAndDesc (untested).

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/pycobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/pycobject.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/pycobject.py	Sun Apr 11 22:14:04 2010
@@ -6,8 +6,9 @@
         cpython_struct, PyObjectFields
 
 
-destructor = lltype.Ptr(lltype.FuncType([rffi.VOIDP_real], lltype.Void))
-PyCObjectStruct = cpython_struct('PyCObject', PyObjectFields + (("destructor", destructor), ))
+destructor_short = lltype.Ptr(lltype.FuncType([rffi.VOIDP_real], lltype.Void))
+destructor_long = lltype.Ptr(lltype.FuncType([rffi.VOIDP_real, rffi.VOIDP_real], lltype.Void))
+PyCObjectStruct = cpython_struct('PyCObject', PyObjectFields + (("destructor", destructor_short), ))
 PyCObject = lltype.Ptr(PyCObjectStruct)
 
 
@@ -16,23 +17,29 @@
         self.space = space
 
 class W_PyCObjectFromVoidPtr(W_PyCObject):
-    def __init__(self, space, voidp):
+    def __init__(self, space, voidp, desc):
         W_PyCObject.__init__(self, space)
         self.voidp = voidp
         self.pyo = lltype.nullptr(PyCObject.TO)
+        self.desc = desc
 
     def set_pycobject(self, pyo):
         self.pyo = pyo
 
     def __del__(self):
         if self.pyo and self.pyo.c_destructor:
-            self.pyo.c_destructor(self.voidp)
+            if self.desc:
+                rffi.cast(self.pyo.c_destructor, destructor_long)(self.pyo, self.desc)
+            else:
+                self.pyo.c_destructor(self.voidp)
 
- at cpython_api([rffi.VOIDP_real, destructor], PyObject)
+
+ at cpython_api([rffi.VOIDP_real, destructor_short], PyObject)
 def PyCObject_FromVoidPtr(space, cobj, destr):
     """Create a PyCObject from the void * cobj.  The destr function
     will be called when the object is reclaimed, unless it is NULL."""
-    w_pycobject = space.wrap(W_PyCObjectFromVoidPtr(space, cobj))
+    w_pycobject = space.wrap(W_PyCObjectFromVoidPtr(space, cobj,
+        lltype.nullptr(rffi.VOIDP_real.TO)))
     assert isinstance(w_pycobject, W_PyCObjectFromVoidPtr)
     pyo = make_ref(space, w_pycobject)
     pycobject = rffi.cast(PyCObject, pyo)
@@ -40,6 +47,20 @@
     pycobject.c_destructor = destr
     return pyo
 
+ at cpython_api([rffi.VOIDP_real, rffi.VOIDP_real, destructor_long], PyObject)
+def PyCObject_FromVoidPtrAndDesc(space, cobj, desc, destr):
+    """Create a PyCObject from the void * cobj.  The destr
+    function will be called when the object is reclaimed. The desc argument can
+    be used to pass extra callback data for the destructor function."""
+    w_pycobject = space.wrap(W_PyCObjectFromVoidPtr(space, cobj,
+        desc))
+    assert isinstance(w_pycobject, W_PyCObjectFromVoidPtr)
+    pyo = make_ref(space, w_pycobject)
+    pycobject = rffi.cast(PyCObject, pyo)
+    w_pycobject.set_pycobject(pycobject)
+    pycobject.c_destructor = rffi.cast(destructor_short, destr)
+    return pyo
+
 
 W_PyCObject.typedef = TypeDef(
     'PyCObject',

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/pystate.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/pystate.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/pystate.py	Sun Apr 11 22:14:04 2010
@@ -28,4 +28,3 @@
         from pypy.module.thread.gil import after_external_call
         after_external_call()
 
-

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_pycobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/test/test_pycobject.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_pycobject.py	Sun Apr 11 22:14:04 2010
@@ -2,9 +2,9 @@
 
 from pypy.rpython.lltypesystem import rffi, lltype
 from pypy.module.cpyext.test.test_api import BaseApiTest
-from pypy.module.cpyext.pycobject import destructor
+from pypy.module.cpyext.pycobject import destructor_short
 
 class TestPyCObject(BaseApiTest):
     def test_pycobject(self, space, api):
-        obj = api.PyCObject_FromVoidPtr(rffi.cast(rffi.VOIDP_real, 0), lltype.nullptr(destructor.TO))
+        obj = api.PyCObject_FromVoidPtr(rffi.cast(rffi.VOIDP_real, 0), lltype.nullptr(destructor_short.TO))
         api.Py_DecRef(obj)



More information about the Pypy-commit mailing list