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

afa at codespeak.net afa at codespeak.net
Mon Apr 26 13:46:28 CEST 2010


Author: afa
Date: Mon Apr 26 13:46:26 2010
New Revision: 74067

Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/api.py
   pypy/branch/cpython-extension/pypy/module/cpyext/pycobject.py
   pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py
   pypy/branch/cpython-extension/pypy/module/cpyext/test/test_pycobject.py
Log:
PyCObject_Check, PyCObject_AsVoidPtr


Modified: pypy/branch/cpython-extension/pypy/module/cpyext/api.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/api.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/api.py	Mon Apr 26 13:46:26 2010
@@ -346,21 +346,29 @@
         if name in TYPES:
             TYPES[name].become(TYPE)
 
-def build_type_checkers(type_name, on_space=None):
-    if on_space is None:
-        on_space = "w_" + type_name.lower()
+def build_type_checkers(type_name, cls=None):
+    if cls is None:
+        attrname = "w_" + type_name.lower()
+        def get_w_type(space):
+            return getattr(space, attrname)
+    elif isinstance(cls, str):
+        def get_w_type(space):
+            return getattr(space, cls)
+    else:
+        def get_w_type(space):
+            return space.gettypeobject(cls.typedef)
     check_name = "Py" + type_name + "_Check"
     @cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL, name=check_name)
     def check(space, w_obj):
         w_obj_type = space.type(w_obj)
-        w_type = getattr(space, on_space)
+        w_type = get_w_type(space)
         return int(space.is_w(w_obj_type, w_type) or
                    space.is_true(space.issubtype(w_obj_type, w_type)))
     @cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL,
                  name=check_name + "Exact")
     def check_exact(space, w_obj):
         w_obj_type = space.type(w_obj)
-        w_type = getattr(space, on_space)
+        w_type = get_w_type(space)
         return int(space.is_w(w_obj_type, w_type))
     return check, check_exact
 

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	Mon Apr 26 13:46:26 2010
@@ -3,7 +3,7 @@
 from pypy.rpython.lltypesystem import rffi, lltype
 from pypy.module.cpyext.pyobject import make_ref, make_typedescr
 from pypy.module.cpyext.api import generic_cpy_call, cpython_api, bootstrap_function, \
-     PyObject, cpython_struct, PyObjectFields
+     PyObject, cpython_struct, PyObjectFields, build_type_checkers
 
 
 destructor_short = lltype.Ptr(lltype.FuncType([rffi.VOIDP_real], lltype.Void))
@@ -26,6 +26,8 @@
     )
 W_PyCObject.typedef.acceptable_as_base_class = False
 
+PyCObject_Check, _ = build_type_checkers("CObject", W_PyCObject)
+
 @bootstrap_function
 def init_pycobject(space):
     make_typedescr(W_PyCObject.typedef,
@@ -75,4 +77,9 @@
     pycobject.c_destructor = rffi.cast(destructor_short, destr)
     return pyo
 
-
+ at cpython_api([PyObject], rffi.VOIDP_real, error=lltype.nullptr(rffi.VOIDP_real.TO))
+def PyCObject_AsVoidPtr(space, w_obj):
+    """Return the object void * that the PyCObject self was
+    created with."""
+    assert isinstance(w_obj, W_PyCObjectFromVoidPtr)
+    return w_obj.pyo.c_cobject

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py	Mon Apr 26 13:46:26 2010
@@ -840,30 +840,6 @@
     instance."""
     raise NotImplementedError
 
- at cpython_api([PyObject], rffi.INT_real)
-def PyCObject_Check(space, p):
-    """Return true if its argument is a PyCObject."""
-    raise NotImplementedError
-
- at cpython_api([{void*}, {void (*destr)(void*}], PyObject)
-def PyCObject_FromVoidPtr(space, cobj, )):
-    """Create a PyCObject from the void * cobj.  The destr function
-    will be called when the object is reclaimed, unless it is NULL."""
-    raise NotImplementedError
-
- at cpython_api([{void*}, {void*}, {void (*destr)(void*}, {void*}], PyObject)
-def PyCObject_FromVoidPtrAndDesc(space, cobj, desc, , )):
-    """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."""
-    raise NotImplementedError
-
- at cpython_api([PyObject], {void*})
-def PyCObject_AsVoidPtr(space, self):
-    """Return the object void * that the PyCObject self was
-    created with."""
-    raise NotImplementedError
-
 @cpython_api([PyObject], {void*})
 def PyCObject_GetDesc(space, self):
     """Return the description void * that the PyCObject self was
@@ -3851,13 +3827,6 @@
     expression o.attr_name."""
     raise NotImplementedError
 
- at cpython_api([PyObject, rffi.CCHARP, PyObject], rffi.INT_real)
-def PyObject_SetAttrString(space, o, attr_name, v):
-    """Set the value of the attribute named attr_name, for object o, to the value
-    v. Returns -1 on failure.  This is the equivalent of the Python statement
-    o.attr_name = v."""
-    raise NotImplementedError
-
 @cpython_api([PyObject, PyObject], rffi.INT_real)
 def PyObject_DelAttr(space, o, attr_name):
     """Delete attribute named attr_name, for object o. Returns -1 on failure.

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	Mon Apr 26 13:46:26 2010
@@ -8,7 +8,7 @@
     def test_pycobject(self, space, api):
         ptr = rffi.cast(rffi.VOIDP_real, 1234)
         obj = api.PyCObject_FromVoidPtr(ptr, lltype.nullptr(destructor_short.TO))
-        try:
-            assert rffi.cast(PyCObject, obj).c_cobject == ptr
-        finally:
-            api.Py_DecRef(obj)
+        assert api.PyCObject_Check(obj)
+        assert api.PyCObject_AsVoidPtr(obj) == ptr
+        assert rffi.cast(PyCObject, obj).c_cobject == ptr
+        api.Py_DecRef(obj)



More information about the Pypy-commit mailing list