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

jandem at codespeak.net jandem at codespeak.net
Sat Apr 10 15:43:10 CEST 2010


Author: jandem
Date: Sat Apr 10 15:43:09 2010
New Revision: 73620

Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/object.py
   pypy/branch/cpython-extension/pypy/module/cpyext/test/test_object.py
Log:
Add PyObject_TypeCheck, with tests


Modified: pypy/branch/cpython-extension/pypy/module/cpyext/object.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/object.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/object.py	Sat Apr 10 15:43:09 2010
@@ -141,3 +141,13 @@
     opid."""
     w_res = PyObject_RichCompare(space, ref1, ref2, opid)
     return int(space.is_true(w_res))
+
+ at cpython_api([PyObject, PyObject], rffi.INT_real, error=CANNOT_FAIL)
+def PyObject_TypeCheck(space, w_obj, w_type):
+    """Return true if the object o is of type type or a subtype of type.  Both
+    parameters must be non-NULL.
+    """
+    w_obj_type = space.type(w_obj)
+    assert isinstance(w_type, W_TypeObject)
+    return int(space.is_w(w_obj_type, w_type) or
+                   space.is_true(space.issubtype(w_obj_type, w_type)))

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_object.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/test/test_object.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_object.py	Sat Apr 10 15:43:09 2010
@@ -4,6 +4,7 @@
 from pypy.rpython.lltypesystem import rffi, lltype
 from pypy.module.cpyext.api import Py_LT, Py_LE, Py_NE, Py_EQ,\
     Py_GE, Py_GT
+from pypy.module.cpyext.pyobject import make_ref
 
 class TestObject(BaseApiTest):
     def test_IsTrue(self, space, api):
@@ -94,3 +95,8 @@
         assert api.PyObject_RichCompareBool(w_i, w_i, 123456) == -1
         assert api.PyErr_Occurred() is space.w_SystemError
         api.PyErr_Clear()
+        
+    def test_TypeCheck(self, space, api):
+        assert api.PyObject_TypeCheck(space.wrap(1), space.w_int)
+        assert api.PyObject_TypeCheck(space.wrap('foo'), space.w_str)
+        assert api.PyObject_TypeCheck(space.wrap('foo'), space.w_object)



More information about the Pypy-commit mailing list