[Python-checkins] r72693 - in python/trunk: Lib/test/test_typechecks.py Objects/abstract.c

benjamin.peterson python-checkins at python.org
Sun May 17 00:40:56 CEST 2009


Author: benjamin.peterson
Date: Sun May 17 00:40:56 2009
New Revision: 72693
Log:
completely ignore old-style stuff for type checking overloading
Modified:
 python/trunk/Lib/test/test_typechecks.py
 python/trunk/Objects/abstract.c
Modified: python/trunk/Lib/test/test_typechecks.py
==============================================================================
--- python/trunk/Lib/test/test_typechecks.py	(original)
+++ python/trunk/Lib/test/test_typechecks.py	Sun May 17 00:40:56 2009
@@ -29,10 +29,6 @@
 pass
 
 
-class Evil:
- def __instancecheck__(self, inst): return False
-
-
 class TypeChecksTest(unittest.TestCase):
 
 def testIsSubclassInternal(self):
@@ -75,11 +71,18 @@
 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():
Modified: python/trunk/Objects/abstract.c
==============================================================================
--- python/trunk/Objects/abstract.c	(original)
+++ python/trunk/Objects/abstract.c	Sun May 17 00:40:56 2009
@@ -2902,7 +2902,6 @@
 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 @@
 		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 @@
 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 @@
 		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);
 }


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /