]> granicus.if.org Git - python/commitdiff
completely ignore old-style stuff for type checking overloading
authorBenjamin Peterson <benjamin@python.org>
Sat, 16 May 2009 22:40:56 +0000 (22:40 +0000)
committerBenjamin Peterson <benjamin@python.org>
Sat, 16 May 2009 22:40:56 +0000 (22:40 +0000)
Lib/test/test_typechecks.py
Objects/abstract.c

index 32aa660346f3360a5badada8ee988dd620ca3636..64c417474d9b8e122245d556410db161aa06262f 100644 (file)
@@ -29,10 +29,6 @@ class SubInt(Integer):
     pass
 
 
-class Evil:
-    def __instancecheck__(self, inst): return False
-
-
 class TypeChecksTest(unittest.TestCase):
 
     def testIsSubclassInternal(self):
@@ -75,11 +71,18 @@ class TypeChecksTest(unittest.TestCase):
         self.assertEqual(isinstance(42, SubInt), False)
         self.assertEqual(isinstance(42, (SubInt,)), False)
 
-    def testInfiniteRecursionCaughtProperly(self):
-        e = Evil()
-        # This invokes isinstance() recursively, until the stack is exhausted.
-        self.assertRaises(RuntimeError, isinstance, e, Evil)
-        # XXX How to check the same situation for issubclass()?
+    def test_oldstyle(self):
+        # These should just be ignored.
+        class X:
+            def __instancecheck__(self, inst):
+                return True
+            def __subclasscheck__(self, cls):
+                return True
+        class Sub(X): pass
+        self.assertFalse(isinstance(3, X))
+        self.assertTrue(isinstance(X(), X))
+        self.assertFalse(issubclass(int, X))
+        self.assertTrue(issubclass(Sub, X))
 
 
 def test_main():
index 2266fa16279399665e13277d7ec390f7a419ea5b..63b20416faf84fc5b0f439a75b86d63d7bb22609 100644 (file)
@@ -2902,7 +2902,6 @@ int
 PyObject_IsInstance(PyObject *inst, PyObject *cls)
 {
        static PyObject *name = NULL;
-       PyObject *checker;
 
        /* Quick test for an exact match */
        if (Py_TYPE(inst) == (PyTypeObject *)cls)
@@ -2927,33 +2926,25 @@ PyObject_IsInstance(PyObject *inst, PyObject *cls)
                return r;
        }
 
-       if (PyClass_Check(cls) || PyInstance_Check(cls)) {
-               checker = PyObject_GetAttrString(cls, "__instancecheck__");
-               if (checker == NULL) {
-                       if (PyErr_ExceptionMatches(PyExc_AttributeError))
-                               PyErr_Clear();
-                       else
-                               return -1;
-               }
-       }
-       else {
+       if (!(PyClass_Check(cls) || PyInstance_Check(cls))) {
+               PyObject *checker;
                checker = _PyObject_LookupSpecial(cls, "__instancecheck__", &name);
-       }
-       if (checker != NULL) {
-               PyObject *res;
-               int ok = -1;
-               if (Py_EnterRecursiveCall(" in __instancecheck__")) {
+               if (checker != NULL) {
+                       PyObject *res;
+                       int ok = -1;
+                       if (Py_EnterRecursiveCall(" in __instancecheck__")) {
+                               Py_DECREF(checker);
+                               return ok;
+                       }
+                       res = PyObject_CallFunctionObjArgs(checker, inst, NULL);
+                       Py_LeaveRecursiveCall();
                        Py_DECREF(checker);
+                       if (res != NULL) {
+                               ok = PyObject_IsTrue(res);
+                               Py_DECREF(res);
+                       }
                        return ok;
                }
-               res = PyObject_CallFunctionObjArgs(checker, inst, NULL);
-               Py_LeaveRecursiveCall();
-               Py_DECREF(checker);
-               if (res != NULL) {
-                       ok = PyObject_IsTrue(res);
-                       Py_DECREF(res);
-               }
-               return ok;
        }
        return recursive_isinstance(inst, cls);
 }
@@ -2992,8 +2983,6 @@ int
 PyObject_IsSubclass(PyObject *derived, PyObject *cls)
 {
        static PyObject *name = NULL;
-       PyObject *t, *v, *tb;
-       PyObject *checker;
        
        if (PyTuple_Check(cls)) {
                Py_ssize_t i;
@@ -3013,36 +3002,25 @@ PyObject_IsSubclass(PyObject *derived, PyObject *cls)
                Py_LeaveRecursiveCall();
                return r;
        }
-       if (PyClass_Check(cls) || PyInstance_Check(cls)) {
-               PyErr_Fetch(&t, &v, &tb);
-               checker = PyObject_GetAttr(cls, name);
-               if (checker == NULL &&
-                   !PyErr_ExceptionMatches(PyExc_AttributeError)) {
-                       Py_XDECREF(t);
-                       Py_XDECREF(v);
-                       Py_XDECREF(tb);
-                       return -1;
-               }
-               PyErr_Restore(t, v, tb);
-       }
-       else {
+       if (!(PyClass_Check(cls) || PyInstance_Check(cls))) {
+               PyObject *checker;
                checker = _PyObject_LookupSpecial(cls, "__subclasscheck__", &name);
-       }
-       if (checker != NULL) {
-               PyObject *res;
-               int ok = -1;
-               if (Py_EnterRecursiveCall(" in __subclasscheck__")) {
+               if (checker != NULL) {
+                       PyObject *res;
+                       int ok = -1;
+                       if (Py_EnterRecursiveCall(" in __subclasscheck__")) {
+                               Py_DECREF(checker);
+                               return ok;
+                       }
+                       res = PyObject_CallFunctionObjArgs(checker, derived, NULL);
+                       Py_LeaveRecursiveCall();
                        Py_DECREF(checker);
+                       if (res != NULL) {
+                               ok = PyObject_IsTrue(res);
+                               Py_DECREF(res);
+                       }
                        return ok;
                }
-               res = PyObject_CallFunctionObjArgs(checker, derived, NULL);
-               Py_LeaveRecursiveCall();
-               Py_DECREF(checker);
-               if (res != NULL) {
-                       ok = PyObject_IsTrue(res);
-                       Py_DECREF(res);
-               }
-               return ok;
        }
        return recursive_issubclass(derived, cls);
 }