summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-02-04 12:42:13 (GMT)
committerGitHub <noreply@github.com>2020-02-04 12:42:13 (GMT)
commit850a4bd839ca11b59439e21dda2a3ebe917a9a16 (patch)
tree263a0e0494eefce7448ea60c42aa86c30e1b644d /Objects
parent4590f72259ecbcea66e12a28af14d867255d2e66 (diff)
downloadcpython-850a4bd839ca11b59439e21dda2a3ebe917a9a16.zip
cpython-850a4bd839ca11b59439e21dda2a3ebe917a9a16.tar.gz
cpython-850a4bd839ca11b59439e21dda2a3ebe917a9a16.tar.bz2
Restore PyObject_IsInstance() comment (GH-18345)
Restore PyObject_IsInstance() comment explaining why only tuples of types are accepted, but not general sequence. Comment written by Guido van Rossum in commit 03290ecbf1661c0192e6abdbe00ae163af461d77 which implements isinstance(x, (A, B, ...)). The comment was lost in a PyObject_IsInstance() optimization: commit ec569b794737be248671d0dfac11b664fc930eef. Cleanup also the code. recursive_isinstance() is no longer recursive, so rename it to object_isinstance(), whereas object_isinstance() is recursive and so rename it to object_recursive_isinstance().
Diffstat (limited to 'Objects')
-rw-r--r--Objects/abstract.c40
1 files changed, 23 insertions, 17 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c
index dc8ba10..aca1a4e 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -2423,7 +2423,7 @@ check_class(PyObject *cls, const char *error)
}
static int
-recursive_isinstance(PyObject *inst, PyObject *cls)
+object_isinstance(PyObject *inst, PyObject *cls)
{
PyObject *icls;
int retval;
@@ -2461,21 +2461,23 @@ recursive_isinstance(PyObject *inst, PyObject *cls)
}
static int
-object_isinstance(PyThreadState *tstate, PyObject *inst, PyObject *cls)
+object_recursive_isinstance(PyThreadState *tstate, PyObject *inst, PyObject *cls)
{
_Py_IDENTIFIER(__instancecheck__);
- PyObject *checker;
/* Quick test for an exact match */
- if (Py_TYPE(inst) == (PyTypeObject *)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);
+ return object_isinstance(inst, cls);
}
if (PyTuple_Check(cls)) {
+ /* Not a general sequence -- that opens up the road to
+ recursion and stack overflow. */
if (_Py_EnterRecursiveCall(tstate, " in __instancecheck__")) {
return -1;
}
@@ -2483,37 +2485,41 @@ object_isinstance(PyThreadState *tstate, PyObject *inst, PyObject *cls)
int r = 0;
for (Py_ssize_t i = 0; i < n; ++i) {
PyObject *item = PyTuple_GET_ITEM(cls, i);
- r = object_isinstance(tstate, inst, item);
- if (r != 0)
+ r = object_recursive_isinstance(tstate, inst, item);
+ if (r != 0) {
/* either found it, or got an error */
break;
+ }
}
_Py_LeaveRecursiveCall(tstate);
return r;
}
- checker = _PyObject_LookupSpecial(cls, &PyId___instancecheck__);
+ PyObject *checker = _PyObject_LookupSpecial(cls, &PyId___instancecheck__);
if (checker != NULL) {
- int ok = -1;
if (_Py_EnterRecursiveCall(tstate, " in __instancecheck__")) {
Py_DECREF(checker);
- return ok;
+ return -1;
}
+
PyObject *res = _PyObject_CallOneArg(checker, inst);
_Py_LeaveRecursiveCall(tstate);
Py_DECREF(checker);
- if (res != NULL) {
- ok = PyObject_IsTrue(res);
- Py_DECREF(res);
+
+ if (res == NULL) {
+ return -1;
}
+ int ok = PyObject_IsTrue(res);
+ Py_DECREF(res);
+
return ok;
}
else if (_PyErr_Occurred(tstate)) {
return -1;
}
- /* Probably never reached anymore. */
- return recursive_isinstance(inst, cls);
+ /* cls has no __instancecheck__() method */
+ return object_isinstance(inst, cls);
}
@@ -2521,7 +2527,7 @@ int
PyObject_IsInstance(PyObject *inst, PyObject *cls)
{
PyThreadState *tstate = _PyThreadState_GET();
- return object_isinstance(tstate, inst, cls);
+ return object_recursive_isinstance(tstate, inst, cls);
}
@@ -2611,7 +2617,7 @@ PyObject_IsSubclass(PyObject *derived, PyObject *cls)
int
_PyObject_RealIsInstance(PyObject *inst, PyObject *cls)
{
- return recursive_isinstance(inst, cls);
+ return object_isinstance(inst, cls);
}
int