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.
This commit is contained in:
Guido van Rossum 2007-09-10 22:36:02 +00:00
parent 1ff91d95a2
commit b55911378f
4 changed files with 465 additions and 0 deletions

View file

@ -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());
}