summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2014-10-03 07:26:37 (GMT)
committerGeorg Brandl <georg@python.org>2014-10-03 07:26:37 (GMT)
commit72b8a80e5975a75398872a8406e4215937874165 (patch)
treeb0aaba5266e9bb3864227691a9dc2db5c092d0f0 /Objects
parentf355943002b0bf957d3481e7699dc626997ed901 (diff)
downloadcpython-72b8a80e5975a75398872a8406e4215937874165.zip
cpython-72b8a80e5975a75398872a8406e4215937874165.tar.gz
cpython-72b8a80e5975a75398872a8406e4215937874165.tar.bz2
Closes #22540: speed up PyObject_IsInstance and PyObject_IsSubclass in the common case that the second argument has metaclass "type".
Diffstat (limited to 'Objects')
-rw-r--r--Objects/abstract.c15
1 files changed, 15 insertions, 0 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c
index ec59972..177c34a 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -2538,6 +2538,11 @@ PyObject_IsInstance(PyObject *inst, PyObject *cls)
if (Py_TYPE(inst) == (PyTypeObject *)cls)
return 1;
+ /* We know what type's __instancecheck__ does. */
+ if (PyType_CheckExact(cls)) {
+ return recursive_isinstance(inst, cls);
+ }
+
if (PyTuple_Check(cls)) {
Py_ssize_t i;
Py_ssize_t n;
@@ -2576,6 +2581,7 @@ PyObject_IsInstance(PyObject *inst, PyObject *cls)
}
else if (PyErr_Occurred())
return -1;
+ /* Probably never reached anymore. */
return recursive_isinstance(inst, cls);
}
@@ -2603,6 +2609,14 @@ PyObject_IsSubclass(PyObject *derived, PyObject *cls)
_Py_IDENTIFIER(__subclasscheck__);
PyObject *checker;
+ /* We know what type's __subclasscheck__ does. */
+ if (PyType_CheckExact(cls)) {
+ /* Quick test for an exact match */
+ if (derived == cls)
+ return 1;
+ return recursive_issubclass(derived, cls);
+ }
+
if (PyTuple_Check(cls)) {
Py_ssize_t i;
Py_ssize_t n;
@@ -2641,6 +2655,7 @@ PyObject_IsSubclass(PyObject *derived, PyObject *cls)
}
else if (PyErr_Occurred())
return -1;
+ /* Probably never reached anymore. */
return recursive_issubclass(derived, cls);
}