diff options
author | Guido van Rossum <guido@python.org> | 2007-05-25 17:55:52 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2007-05-25 17:55:52 (GMT) |
commit | adee45ed2c5c6602de6d9dd5f13c02fc262a23fe (patch) | |
tree | 3ff2bb904b653efa8303cac34e4a50130463eb98 /Objects/abstract.c | |
parent | 573c08c1b73048876b62d99ff8d82337dc8ce0a2 (diff) | |
download | cpython-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.
........
Diffstat (limited to 'Objects/abstract.c')
-rw-r--r-- | Objects/abstract.c | 40 |
1 files changed, 38 insertions, 2 deletions
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()); } |