summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-05-25 17:55:52 (GMT)
committerGuido van Rossum <guido@python.org>2007-05-25 17:55:52 (GMT)
commitadee45ed2c5c6602de6d9dd5f13c02fc262a23fe (patch)
tree3ff2bb904b653efa8303cac34e4a50130463eb98
parent573c08c1b73048876b62d99ff8d82337dc8ce0a2 (diff)
downloadcpython-adee45ed2c5c6602de6d9dd5f13c02fc262a23fe.zip
cpython-adee45ed2c5c6602de6d9dd5f13c02fc262a23fe.tar.gz
cpython-adee45ed2c5c6602de6d9dd5f13c02fc262a23fe.tar.bz2
Merged revisions 55545-55587 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk ........ r55587 | guido.van.rossum | 2007-05-25 10:37:01 -0700 (Fri, 25 May 2007) | 2 lines Implement isinstance and issubclass overriding, a la PEP 3119. ........
-rw-r--r--Lib/test/test_typechecks.py75
-rw-r--r--Objects/abstract.c40
2 files changed, 113 insertions, 2 deletions
diff --git a/Lib/test/test_typechecks.py b/Lib/test/test_typechecks.py
new file mode 100644
index 0000000..632598c
--- /dev/null
+++ b/Lib/test/test_typechecks.py
@@ -0,0 +1,75 @@
+"""Unit tests for __instancecheck__ and __subclasscheck__."""
+
+import unittest
+from test import test_support
+
+
+class ABC(type):
+
+ def __instancecheck__(cls, inst):
+ """Implement isinstance(inst, cls)."""
+ return any(cls.__subclasscheck__(c)
+ for c in {type(inst), inst.__class__})
+
+ def __subclasscheck__(cls, sub):
+ """Implement issubclass(sub, cls)."""
+ candidates = cls.__dict__.get("__subclass__", set()) | {cls}
+ return any(c in candidates for c in sub.mro())
+
+
+class Integer(metaclass=ABC):
+
+ __subclass__ = {int}
+
+
+class SubInt(Integer):
+
+ pass
+
+
+class Evil:
+ def __instancecheck__(self, inst): return False
+
+
+class TypeChecksTest(unittest.TestCase):
+
+ def testIsSubclassInternal(self):
+ self.assertEqual(Integer.__subclasscheck__(int), True)
+ self.assertEqual(Integer.__subclasscheck__(float), False)
+
+ def testIsSubclassBuiltin(self):
+ self.assertEqual(issubclass(int, Integer), True)
+ self.assertEqual(issubclass(float, Integer), False)
+
+ def testIsInstanceBuiltin(self):
+ self.assertEqual(isinstance(42, Integer), True)
+ self.assertEqual(isinstance(3.14, Integer), False)
+
+ def testIsInstanceActual(self):
+ self.assertEqual(isinstance(Integer(), Integer), True)
+
+ def testIsSubclassActual(self):
+ self.assertEqual(issubclass(Integer, Integer), True)
+
+ def testSubclassBehavior(self):
+ self.assertEqual(issubclass(SubInt, Integer), True)
+ self.assertEqual(issubclass(SubInt, SubInt), True)
+ self.assertEqual(issubclass(Integer, SubInt), False)
+ self.assertEqual(issubclass(int, SubInt), False)
+ self.assertEqual(isinstance(SubInt(), Integer), True)
+ self.assertEqual(isinstance(SubInt(), SubInt), True)
+ 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_main():
+ test_support.run_unittest(TypeChecksTest)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/Objects/abstract.c b/Objects/abstract.c
index 645fd95..dc66893 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -2130,7 +2130,25 @@ recursive_isinstance(PyObject *inst, PyObject *cls, int recursion_depth)
int
PyObject_IsInstance(PyObject *inst, PyObject *cls)
{
- return recursive_isinstance(inst, cls, Py_GetRecursionLimit());
+ PyObject *t, *v, *tb;
+ PyErr_Fetch(&t, &v, &tb);
+ PyObject *checker = PyObject_GetAttrString(cls, "__instancecheck__");
+ PyErr_Restore(t, v, tb);
+ if (checker != NULL) {
+ PyObject *res;
+ int ok = -1;
+ if (Py_EnterRecursiveCall(" in __instancecheck__"))
+ 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, Py_GetRecursionLimit());
}
static int
@@ -2180,7 +2198,25 @@ recursive_issubclass(PyObject *derived, PyObject *cls, int recursion_depth)
int
PyObject_IsSubclass(PyObject *derived, PyObject *cls)
{
- return recursive_issubclass(derived, cls, Py_GetRecursionLimit());
+ PyObject *t, *v, *tb;
+ PyErr_Fetch(&t, &v, &tb);
+ PyObject *checker = PyObject_GetAttrString(cls, "__subclasscheck__");
+ PyErr_Restore(t, v, tb);
+ if (checker != NULL) {
+ PyObject *res;
+ int ok = -1;
+ if (Py_EnterRecursiveCall(" in __subclasscheck__"))
+ 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, Py_GetRecursionLimit());
}