diff options
author | Guido van Rossum <guido@python.org> | 2001-03-21 18:40:58 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2001-03-21 18:40:58 (GMT) |
commit | 823649d544eec66a61176dbb76b51a9408c7f4ed (patch) | |
tree | 5421f4de7101fe676693edea46cc886661db96b7 /Python/bltinmodule.c | |
parent | 91751143ebae8db295a7e15b4aeb831ea43ca123 (diff) | |
download | cpython-823649d544eec66a61176dbb76b51a9408c7f4ed.zip cpython-823649d544eec66a61176dbb76b51a9408c7f4ed.tar.gz cpython-823649d544eec66a61176dbb76b51a9408c7f4ed.tar.bz2 |
Move the code implementing isinstance() and issubclass() to new C
APIs, PyObject_IsInstance() and PyObject_IsSubclass() -- both
returning an int, or -1 for errors.
Diffstat (limited to 'Python/bltinmodule.c')
-rw-r--r-- | Python/bltinmodule.c | 102 |
1 files changed, 6 insertions, 96 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index e5db11e..ec680d1 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -2005,101 +2005,19 @@ static char vars_doc[] = Without arguments, equivalent to locals().\n\ With an argument, equivalent to object.__dict__."; -static int -abstract_issubclass(PyObject *derived, PyObject *cls, int first) -{ - static PyObject *__bases__ = NULL; - PyObject *bases; - int i, n; - int r = 0; - - if (__bases__ == NULL) { - __bases__ = PyString_FromString("__bases__"); - if (__bases__ == NULL) - return -1; - } - - if (first) { - bases = PyObject_GetAttr(cls, __bases__); - if (bases == NULL || !PyTuple_Check(bases)) { - Py_XDECREF(bases); - PyErr_SetString(PyExc_TypeError, - "issubclass() arg 2 must be a class"); - return -1; - } - Py_DECREF(bases); - } - - if (derived == cls) - return 1; - - bases = PyObject_GetAttr(derived, __bases__); - if (bases == NULL || !PyTuple_Check(bases)) { - Py_XDECREF(bases); - PyErr_SetString(PyExc_TypeError, - "issubclass() arg 1 must be a class"); - return -1; - } - - n = PyTuple_GET_SIZE(bases); - for (i = 0; i < n; i++) { - r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls, 0); - if (r != 0) - break; - } - - Py_DECREF(bases); - - return r; -} - static PyObject * builtin_isinstance(PyObject *self, PyObject *args) { PyObject *inst; PyObject *cls; - PyObject *icls; - static PyObject *__class__ = NULL; - int retval = 0; + int retval; if (!PyArg_ParseTuple(args, "OO:isinstance", &inst, &cls)) return NULL; - if (PyClass_Check(cls)) { - if (PyInstance_Check(inst)) { - PyObject *inclass = - (PyObject*)((PyInstanceObject*)inst)->in_class; - retval = PyClass_IsSubclass(inclass, cls); - } - } - else if (PyType_Check(cls)) { - retval = ((PyObject *)(inst->ob_type) == cls); - } - else if (!PyInstance_Check(inst)) { - if (__class__ == NULL) { - __class__ = PyString_FromString("__class__"); - if (__class__ == NULL) - return NULL; - } - icls = PyObject_GetAttr(inst, __class__); - if (icls != NULL) { - retval = abstract_issubclass(icls, cls, 1); - Py_DECREF(icls); - if (retval < 0 && - !PyErr_ExceptionMatches(PyExc_TypeError)) - return NULL; - } - else - retval = -1; - } - else - retval = -1; - - if (retval < 0) { - PyErr_SetString(PyExc_TypeError, - "isinstance() arg 2 must be a class or type"); + retval = PyObject_IsInstance(inst, cls); + if (retval < 0) return NULL; - } return PyInt_FromLong(retval); } @@ -2120,17 +2038,9 @@ builtin_issubclass(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "OO:issubclass", &derived, &cls)) return NULL; - if (!PyClass_Check(derived) || !PyClass_Check(cls)) { - retval = abstract_issubclass(derived, cls, 1); - if (retval < 0) - return NULL; - } - else { - /* shortcut */ - if (!(retval = (derived == cls))) - retval = PyClass_IsSubclass(derived, cls); - } - + retval = PyObject_IsSubclass(derived, cls); + if (retval < 0) + return NULL; return PyInt_FromLong(retval); } |