bpo-38644: Pass tstate to Py_EnterRecursiveCall() by vstinner · Pull Request #16997 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions Include/cpython/ceval.h
80 changes: 49 additions & 31 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* Abstract Object Interface (many thanks to Jim Fulton) */

#include "Python.h"
#include "pycore_pyerrors.h"
#include "pycore_pystate.h"
#include <ctype.h>
#include "structmember.h" /* we need the offsetof() macro from there */
Expand Down Expand Up @@ -2459,8 +2460,8 @@ recursive_isinstance(PyObject *inst, PyObject *cls)
return retval;
}

int
PyObject_IsInstance(PyObject *inst, PyObject *cls)
static int
object_isinstance(PyThreadState *tstate, PyObject *inst, PyObject *cls)
{
_Py_IDENTIFIER(__instancecheck__);
PyObject *checker;
Expand All @@ -2475,47 +2476,55 @@ PyObject_IsInstance(PyObject *inst, PyObject *cls)
}

if (PyTuple_Check(cls)) {
Py_ssize_t i;
Py_ssize_t n;
int r = 0;

if (Py_EnterRecursiveCall(" in __instancecheck__"))
if (_Py_EnterRecursiveCall(tstate, " in __instancecheck__")) {
return -1;
n = PyTuple_GET_SIZE(cls);
for (i = 0; i < n; ++i) {
}
Py_ssize_t n = PyTuple_GET_SIZE(cls);
int r = 0;
for (Py_ssize_t i = 0; i < n; ++i) {
PyObject *item = PyTuple_GET_ITEM(cls, i);
r = PyObject_IsInstance(inst, item);
r = object_isinstance(tstate, inst, item);
if (r != 0)
/* either found it, or got an error */
break;
}
Py_LeaveRecursiveCall();
_Py_LeaveRecursiveCall(tstate);
return r;
}

checker = _PyObject_LookupSpecial(cls, &PyId___instancecheck__);
if (checker != NULL) {
PyObject *res;
int ok = -1;
if (Py_EnterRecursiveCall(" in __instancecheck__")) {
if (_Py_EnterRecursiveCall(tstate, " in __instancecheck__")) {
Py_DECREF(checker);
return ok;
}
res = _PyObject_CallOneArg(checker, inst);
Py_LeaveRecursiveCall();
PyObject *res = _PyObject_CallOneArg(checker, inst);
_Py_LeaveRecursiveCall(tstate);
Py_DECREF(checker);
if (res != NULL) {
ok = PyObject_IsTrue(res);
Py_DECREF(res);
}
return ok;
}
else if (PyErr_Occurred())
else if (_PyErr_Occurred(tstate)) {
return -1;
}

/* Probably never reached anymore. */
return recursive_isinstance(inst, cls);
}


int
PyObject_IsInstance(PyObject *inst, PyObject *cls)
{
PyThreadState *tstate = _PyThreadState_GET();
return object_isinstance(tstate, inst, cls);
}


static int
recursive_issubclass(PyObject *derived, PyObject *cls)
{
Expand All @@ -2534,8 +2543,8 @@ recursive_issubclass(PyObject *derived, PyObject *cls)
return abstract_issubclass(derived, cls);
}

int
PyObject_IsSubclass(PyObject *derived, PyObject *cls)
static int
object_issubclass(PyThreadState *tstate, PyObject *derived, PyObject *cls)
{
_Py_IDENTIFIER(__subclasscheck__);
PyObject *checker;
Expand All @@ -2549,47 +2558,56 @@ PyObject_IsSubclass(PyObject *derived, PyObject *cls)
}

if (PyTuple_Check(cls)) {
Py_ssize_t i;
Py_ssize_t n;
int r = 0;

if (Py_EnterRecursiveCall(" in __subclasscheck__"))
if (_Py_EnterRecursiveCall(tstate, " in __subclasscheck__")) {
return -1;
n = PyTuple_GET_SIZE(cls);
for (i = 0; i < n; ++i) {
}
Py_ssize_t n = PyTuple_GET_SIZE(cls);
int r = 0;
for (Py_ssize_t i = 0; i < n; ++i) {
PyObject *item = PyTuple_GET_ITEM(cls, i);
r = PyObject_IsSubclass(derived, item);
r = object_issubclass(tstate, derived, item);
if (r != 0)
/* either found it, or got an error */
break;
}
Py_LeaveRecursiveCall();
_Py_LeaveRecursiveCall(tstate);
return r;
}

checker = _PyObject_LookupSpecial(cls, &PyId___subclasscheck__);
if (checker != NULL) {
PyObject *res;
int ok = -1;
if (Py_EnterRecursiveCall(" in __subclasscheck__")) {
if (_Py_EnterRecursiveCall(tstate, " in __subclasscheck__")) {
Py_DECREF(checker);
return ok;
}
res = _PyObject_CallOneArg(checker, derived);
Py_LeaveRecursiveCall();
PyObject *res = _PyObject_CallOneArg(checker, derived);
_Py_LeaveRecursiveCall(tstate);
Py_DECREF(checker);
if (res != NULL) {
ok = PyObject_IsTrue(res);
Py_DECREF(res);
}
return ok;
}
else if (PyErr_Occurred())
else if (_PyErr_Occurred(tstate)) {
return -1;
}

/* Probably never reached anymore. */
return recursive_issubclass(derived, cls);
}


int
PyObject_IsSubclass(PyObject *derived, PyObject *cls)
{
PyThreadState *tstate = _PyThreadState_GET();
return object_issubclass(tstate, derived, cls);
}


int
_PyObject_RealIsInstance(PyObject *inst, PyObject *cls)
{
Expand Down
42 changes: 23 additions & 19 deletions Objects/call.c
Loading