Support type objects in isinstance().

E.g. isinstance('',types.StringType) will return true now instead of
raising a TypeError exception.  This is for JPython compatibility.
This commit is contained in:
Guido van Rossum 1997-12-02 19:11:45 +00:00
parent 6cedf82ac1
commit f5dd914196

View file

@ -1627,6 +1627,10 @@ builtin_isinstance(self, args)
if (!PyArg_ParseTuple(args, "OO", &inst, &cls)) if (!PyArg_ParseTuple(args, "OO", &inst, &cls))
return NULL; return NULL;
if (PyType_Check(cls)) {
retval = (inst->ob_type == cls);
}
else {
if (!PyClass_Check(cls)) { if (!PyClass_Check(cls)) {
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_TypeError,
"second argument must be a class"); "second argument must be a class");
@ -1640,6 +1644,7 @@ builtin_isinstance(self, args)
(PyObject*)((PyInstanceObject*)inst)->in_class; (PyObject*)((PyInstanceObject*)inst)->in_class;
retval = PyClass_IsSubclass(inclass, cls); retval = PyClass_IsSubclass(inclass, cls);
} }
}
return PyInt_FromLong(retval); return PyInt_FromLong(retval);
} }