diff options
author | Guido van Rossum <guido@python.org> | 2007-09-10 22:36:02 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2007-09-10 22:36:02 (GMT) |
commit | b55911378fb34df08cd303895ef212e2a86d6606 (patch) | |
tree | 929f2bde84c0571279dce9e01c9659f07df4546a /Objects | |
parent | 1ff91d95a280449cfd9c723a081cb7b19a52e758 (diff) | |
download | cpython-b55911378fb34df08cd303895ef212e2a86d6606.zip cpython-b55911378fb34df08cd303895ef212e2a86d6606.tar.gz cpython-b55911378fb34df08cd303895ef212e2a86d6606.tar.bz2 |
Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Backport abc.py and isinstance/issubclass overloading to 2.6.
I had to backport test_typechecks.py myself, and make one small change
to abc.py to avoid duplicate work when x.__class__ and type(x) are the
same.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/abstract.c | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c index 7378e0d..16e0270 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -2279,6 +2279,27 @@ recursive_isinstance(PyObject *inst, PyObject *cls, int recursion_depth) int PyObject_IsInstance(PyObject *inst, PyObject *cls) { + PyObject *t, *v, *tb; + PyObject *checker; + PyErr_Fetch(&t, &v, &tb); + checker = PyObject_GetAttrString(cls, "__instancecheck__"); + PyErr_Restore(t, v, tb); + 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; + } return recursive_isinstance(inst, cls, Py_GetRecursionLimit()); } @@ -2334,6 +2355,25 @@ recursive_issubclass(PyObject *derived, PyObject *cls, int recursion_depth) int PyObject_IsSubclass(PyObject *derived, PyObject *cls) { + PyObject *t, *v, *tb; + PyObject *checker; + PyErr_Fetch(&t, &v, &tb); + 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()); } |