mirror of
https://github.com/python/cpython.git
synced 2025-08-28 04:35:02 +00:00
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:
parent
6cedf82ac1
commit
f5dd914196
1 changed files with 15 additions and 10 deletions
|
@ -1627,18 +1627,23 @@ builtin_isinstance(self, args)
|
|||
|
||||
if (!PyArg_ParseTuple(args, "OO", &inst, &cls))
|
||||
return NULL;
|
||||
if (!PyClass_Check(cls)) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"second argument must be a class");
|
||||
return NULL;
|
||||
if (PyType_Check(cls)) {
|
||||
retval = (inst->ob_type == cls);
|
||||
}
|
||||
|
||||
if (!PyInstance_Check(inst))
|
||||
retval = 0;
|
||||
else {
|
||||
PyObject *inclass =
|
||||
(PyObject*)((PyInstanceObject*)inst)->in_class;
|
||||
retval = PyClass_IsSubclass(inclass, cls);
|
||||
if (!PyClass_Check(cls)) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"second argument must be a class");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!PyInstance_Check(inst))
|
||||
retval = 0;
|
||||
else {
|
||||
PyObject *inclass =
|
||||
(PyObject*)((PyInstanceObject*)inst)->in_class;
|
||||
retval = PyClass_IsSubclass(inclass, cls);
|
||||
}
|
||||
}
|
||||
return PyInt_FromLong(retval);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue