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

afa at codespeak.net afa at codespeak.net
Tue Apr 20 08:55:00 CEST 2010


Author: afa
Date: Tue Apr 20 08:54:58 2010
New Revision: 73894

Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/object.py
   pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py
   pypy/branch/cpython-extension/pypy/module/cpyext/test/test_object.py
Log:
add PyObject_IsInstance and PyObject_IsSubclass


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	Tue Apr 20 08:54:58 2010
@@ -196,3 +196,29 @@
     from pypy.objspace.descroperation import object_getattribute
     w_descr = object_getattribute(space)
     return space.get_and_call_function(w_descr, w_obj, w_name)
+
+ at cpython_api([PyObject, PyObject], rffi.INT_real, error=-1)
+def PyObject_IsInstance(space, w_inst, w_cls):
+    """Returns 1 if inst is an instance of the class cls or a subclass of
+    cls, or 0 if not.  On error, returns -1 and sets an exception.  If
+    cls is a type object rather than a class object, PyObject_IsInstance()
+    returns 1 if inst is of type cls.  If cls is a tuple, the check will
+    be done against every entry in cls. The result will be 1 when at least one
+    of the checks returns 1, otherwise it will be 0. If inst is not a class
+    instance and cls is neither a type object, nor a class object, nor a
+    tuple, inst must have a __class__ attribute --- the class relationship
+    of the value of that attribute with cls will be used to determine the result
+    of this function."""
+    from pypy.module.__builtin__.abstractinst import abstract_isinstance_w
+    return abstract_isinstance_w(space, w_inst, w_cls)
+
+ at cpython_api([PyObject, PyObject], rffi.INT_real, error=-1)
+def PyObject_IsSubclass(space, w_derived, w_cls):
+    """Returns 1 if the class derived is identical to or derived from the class
+    cls, otherwise returns 0.  In case of an error, returns -1. If cls
+    is a tuple, the check will be done against every entry in cls. The result will
+    be 1 when at least one of the checks returns 1, otherwise it will be
+    0. If either derived or cls is not an actual class object (or tuple),
+    this function uses the generic algorithm described above."""
+    from pypy.module.__builtin__.abstractinst import abstract_issubclass_w
+    return abstract_issubclass_w(space, w_derived, w_cls)

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	Tue Apr 20 08:54:58 2010
@@ -4429,38 +4429,6 @@
     function."""
     raise NotImplementedError
 
- at cpython_api([PyObject, PyObject], rffi.INT_real)
-def PyObject_IsInstance(space, inst, cls):
-    """Returns 1 if inst is an instance of the class cls or a subclass of
-    cls, or 0 if not.  On error, returns -1 and sets an exception.  If
-    cls is a type object rather than a class object, PyObject_IsInstance()
-    returns 1 if inst is of type cls.  If cls is a tuple, the check will
-    be done against every entry in cls. The result will be 1 when at least one
-    of the checks returns 1, otherwise it will be 0. If inst is not a
-    class instance and cls is neither a type object, nor a class object, nor a
-    tuple, inst must have a __class__ attribute --- the class relationship
-    of the value of that attribute with cls will be used to determine the result
-    of this function.
-    
-    
-    
-    Support for a tuple as the second argument added."""
-    raise NotImplementedError
-
- at cpython_api([PyObject, PyObject], rffi.INT_real)
-def PyObject_IsSubclass(space, derived, cls):
-    """Returns 1 if the class derived is identical to or derived from the class
-    cls, otherwise returns 0.  In case of an error, returns -1. If cls
-    is a tuple, the check will be done against every entry in cls. The result will
-    be 1 when at least one of the checks returns 1, otherwise it will be
-    0. If either derived or cls is not an actual class object (or tuple),
-    this function uses the generic algorithm described above.
-    
-    
-    
-    Older versions of Python did not support a tuple as the second argument."""
-    raise NotImplementedError
-
 @cpython_api([PyObject], rffi.INT_real)
 def PyCallable_Check(space, o):
     """Determine if the object o is callable.  Return 1 if the object is callable

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	Tue Apr 20 08:54:58 2010
@@ -105,3 +105,22 @@
         assert api.PyObject_TypeCheck(space.wrap(True), api.PyBool_Type)
         assert api.PyObject_TypeCheck(space.wrap(1.2), api.PyFloat_Type)
         assert api.PyObject_TypeCheck(space.w_int, api.PyType_Type)
+
+    def test_IsInstance(self, space, api):
+        assert api.PyObject_IsInstance(space.wrap(1), space.w_int) == 1
+        assert api.PyObject_IsInstance(space.wrap(1), space.w_float) == 0
+        assert api.PyObject_IsInstance(space.w_True, space.w_int) == 1
+        assert api.PyObject_IsInstance(
+            space.wrap(1), space.newtuple([space.w_int, space.w_float])) == 1
+        assert api.PyObject_IsInstance(space.w_type, space.w_type) == 1
+        assert api.PyObject_IsInstance(space.wrap(1), space.w_None) == -1
+        api.PyErr_Clear()
+
+    def test_IsSubclass(self, space, api):
+        assert api.PyObject_IsSubclass(space.w_type, space.w_type) == 1
+        assert api.PyObject_IsSubclass(space.w_type, space.w_object) == 1
+        assert api.PyObject_IsSubclass(space.w_object, space.w_type) == 0
+        assert api.PyObject_IsSubclass(
+            space.w_type, space.newtuple([space.w_int, space.w_type])) == 1
+        assert api.PyObject_IsSubclass(space.wrap(1), space.w_type) == -1
+        api.PyErr_Clear()



More information about the Pypy-commit mailing list