[pypy-svn] r72509 - in pypy/trunk/pypy/module/cpyext: . include test

xoraxax at codespeak.net xoraxax at codespeak.net
Sun Mar 21 21:14:20 CET 2010


Author: xoraxax
Date: Sun Mar 21 21:14:11 2010
New Revision: 72509

Modified:
   pypy/trunk/pypy/module/cpyext/api.py
   pypy/trunk/pypy/module/cpyext/boolobject.py
   pypy/trunk/pypy/module/cpyext/include/tupleobject.h
   pypy/trunk/pypy/module/cpyext/macros.py
   pypy/trunk/pypy/module/cpyext/modsupport.py
   pypy/trunk/pypy/module/cpyext/test/test_cpyext.py
Log:
Simplify code, fix translation error, add refcounting test.

Modified: pypy/trunk/pypy/module/cpyext/api.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/api.py	(original)
+++ pypy/trunk/pypy/module/cpyext/api.py	Sun Mar 21 21:14:11 2010
@@ -160,6 +160,10 @@
         raise InvalidPointerException("Got invalid reference to a PyObject")
     return obj
 
+def general_check(space, w_obj, w_type):
+    w_obj_type = space.type(w_obj)
+    return int(space.is_w(w_obj_type, w_type) or space.is_true(space.issubtype(w_obj_type, w_type)))
+
 #_____________________________________________________
 # Build the bridge DLL, Allow extension DLLs to call
 # back into Pypy space functions

Modified: pypy/trunk/pypy/module/cpyext/boolobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/boolobject.py	(original)
+++ pypy/trunk/pypy/module/cpyext/boolobject.py	Sun Mar 21 21:14:11 2010
@@ -1,11 +1,10 @@
 from pypy.rpython.lltypesystem import rffi, lltype
-from pypy.module.cpyext.api import cpython_api, PyObject
+from pypy.module.cpyext.api import cpython_api, PyObject, general_check
 
 @cpython_api([PyObject], rffi.INT)
 def PyBool_Check(space, w_obj):
-    if space.eq_w(space.type(w_obj), space.w_bool):
-        return 1
-    return 0
+    w_type = space.w_bool
+    return general_check(space, w_obj, w_type)
 
 @cpython_api([rffi.LONG], PyObject)
 def PyBool_FromLong(space, value):

Modified: pypy/trunk/pypy/module/cpyext/include/tupleobject.h
==============================================================================
--- pypy/trunk/pypy/module/cpyext/include/tupleobject.h	(original)
+++ pypy/trunk/pypy/module/cpyext/include/tupleobject.h	Sun Mar 21 21:14:11 2010
@@ -9,7 +9,7 @@
 
 PyObject * PyTuple_New(Py_ssize_t size);
 PyObject * PyTuple_Pack(Py_ssize_t, ...);
-PyTuple_SetItem(PyObject *, Py_ssize_t, PyObject *);
+int PyTuple_SetItem(PyObject *, Py_ssize_t, PyObject *);
 
 #ifdef __cplusplus
 }

Modified: pypy/trunk/pypy/module/cpyext/macros.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/macros.py	(original)
+++ pypy/trunk/pypy/module/cpyext/macros.py	Sun Mar 21 21:14:11 2010
@@ -5,7 +5,7 @@
 from pypy.module.cpyext.state import State
 
 # XXX Optimize these functions and put them into macro definitions
- at cpython_api([PyObject], lltype.Void)
+ at cpython_api([PyObject], lltype.Void, borrowed=True)
 def Py_DECREF(space, w_obj):
     state = space.fromcache(State)
     obj = state.py_objects_w2r.get(w_obj)
@@ -20,8 +20,9 @@
         assert obj.c_obj_refcnt > 0
     return
 
- at cpython_api([PyObject], lltype.Void)
+ at cpython_api([PyObject], lltype.Void, borrowed=True)
 def Py_INCREF(space, w_obj):
     state = space.fromcache(State)
     obj = state.py_objects_w2r.get(w_obj)
     obj.c_obj_refcnt += 1
+

Modified: pypy/trunk/pypy/module/cpyext/modsupport.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/modsupport.py	(original)
+++ pypy/trunk/pypy/module/cpyext/modsupport.py	Sun Mar 21 21:14:11 2010
@@ -1,6 +1,6 @@
 from pypy.rpython.lltypesystem import rffi, lltype
 from pypy.module.cpyext.api import cpython_api, cpython_struct, PyObject, \
-        METH_STATIC, METH_CLASS, METH_COEXIST
+        METH_STATIC, METH_CLASS, METH_COEXIST, general_check
 from pypy.interpreter.module import Module
 from pypy.module.cpyext.methodobject import PyCFunction_NewEx, PyDescr_NewMethod
 from pypy.module.cpyext.pyerrors import PyErr_BadInternalCall
@@ -69,8 +69,7 @@
 @cpython_api([PyObject], rffi.INT)
 def PyModule_Check(space, w_obj):
     w_type = space.gettypeobject(Module.typedef)
-    w_obj_type = space.type(w_obj)
-    return space.is_w(w_obj_type, w_type) or space.is_true(space.issubtype(w_obj_type, w_type))
+    return general_check(space, w_obj, w_type)
 
 @cpython_api([PyObject], PyObject)
 def PyModule_GetDict(space, w_mod):

Modified: pypy/trunk/pypy/module/cpyext/test/test_cpyext.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_cpyext.py	(original)
+++ pypy/trunk/pypy/module/cpyext/test/test_cpyext.py	Sun Mar 21 21:14:11 2010
@@ -185,6 +185,34 @@
 
         assert exc.value.message == "moo!"
 
+    def test_refcount(self):
+        import sys
+        init = """
+        if (Py_IsInitialized())
+            Py_InitModule("foo", methods);
+        """
+        body = """
+        static PyObject* foo_pi(PyObject* self, PyObject *args)
+        {
+            PyObject *true = Py_True;
+            int refcnt = Py_REFCNT(true);
+            int refcnt_after;
+            Py_INCREF(true);
+            PyBool_Check(true);
+            Py_DECREF(true);
+            refcnt_after = Py_REFCNT(true);
+            printf("REFCNT %i %i\\n", refcnt, refcnt_after);
+            return PyBool_FromLong(refcnt_after == refcnt);
+        }
+        static PyMethodDef methods[] = {
+            { "test_refcount", foo_pi, METH_NOARGS },
+            { NULL }
+        };
+        """
+        module = self.import_module(name='foo', init=init, body=body)
+        assert module.test_refcount()
+
+
     def test_init_exception(self):
         import sys
         init = """



More information about the Pypy-commit mailing list