bpo-38644: Pass tstate to Py_EnterRecursiveCall() (GH-16997) · python/cpython@be434dc

11

/* Abstract Object Interface (many thanks to Jim Fulton) */

2233

#include "Python.h"

4+

#include "pycore_pyerrors.h"

45

#include "pycore_pystate.h"

56

#include <ctype.h>

67

#include "structmember.h" /* we need the offsetof() macro from there */

@@ -2459,8 +2460,8 @@ recursive_isinstance(PyObject *inst, PyObject *cls)

24592460

return retval;

24602461

}

246124622462-

int

2463-

PyObject_IsInstance(PyObject *inst, PyObject *cls)

2463+

static int

2464+

object_isinstance(PyThreadState *tstate, PyObject *inst, PyObject *cls)

24642465

{

24652466

_Py_IDENTIFIER(__instancecheck__);

24662467

PyObject *checker;

@@ -2475,47 +2476,55 @@ PyObject_IsInstance(PyObject *inst, PyObject *cls)

24752476

}

2476247724772478

if (PyTuple_Check(cls)) {

2478-

Py_ssize_t i;

2479-

Py_ssize_t n;

2480-

int r = 0;

2481-2482-

if (Py_EnterRecursiveCall(" in __instancecheck__"))

2479+

if (_Py_EnterRecursiveCall(tstate, " in __instancecheck__")) {

24832480

return -1;

2484-

n = PyTuple_GET_SIZE(cls);

2485-

for (i = 0; i < n; ++i) {

2481+

}

2482+

Py_ssize_t n = PyTuple_GET_SIZE(cls);

2483+

int r = 0;

2484+

for (Py_ssize_t i = 0; i < n; ++i) {

24862485

PyObject *item = PyTuple_GET_ITEM(cls, i);

2487-

r = PyObject_IsInstance(inst, item);

2486+

r = object_isinstance(tstate, inst, item);

24882487

if (r != 0)

24892488

/* either found it, or got an error */

24902489

break;

24912490

}

2492-

Py_LeaveRecursiveCall();

2491+

_Py_LeaveRecursiveCall(tstate);

24932492

return r;

24942493

}

2495249424962495

checker = _PyObject_LookupSpecial(cls, &PyId___instancecheck__);

24972496

if (checker != NULL) {

2498-

PyObject *res;

24992497

int ok = -1;

2500-

if (Py_EnterRecursiveCall(" in __instancecheck__")) {

2498+

if (_Py_EnterRecursiveCall(tstate, " in __instancecheck__")) {

25012499

Py_DECREF(checker);

25022500

return ok;

25032501

}

2504-

res = _PyObject_CallOneArg(checker, inst);

2505-

Py_LeaveRecursiveCall();

2502+

PyObject *res = _PyObject_CallOneArg(checker, inst);

2503+

_Py_LeaveRecursiveCall(tstate);

25062504

Py_DECREF(checker);

25072505

if (res != NULL) {

25082506

ok = PyObject_IsTrue(res);

25092507

Py_DECREF(res);

25102508

}

25112509

return ok;

25122510

}

2513-

else if (PyErr_Occurred())

2511+

else if (_PyErr_Occurred(tstate)) {

25142512

return -1;

2513+

}

2514+25152515

/* Probably never reached anymore. */

25162516

return recursive_isinstance(inst, cls);

25172517

}

251825182519+2520+

int

2521+

PyObject_IsInstance(PyObject *inst, PyObject *cls)

2522+

{

2523+

PyThreadState *tstate = _PyThreadState_GET();

2524+

return object_isinstance(tstate, inst, cls);

2525+

}

2526+2527+25192528

static int

25202529

recursive_issubclass(PyObject *derived, PyObject *cls)

25212530

{

@@ -2534,8 +2543,8 @@ recursive_issubclass(PyObject *derived, PyObject *cls)

25342543

return abstract_issubclass(derived, cls);

25352544

}

253625452537-

int

2538-

PyObject_IsSubclass(PyObject *derived, PyObject *cls)

2546+

static int

2547+

object_issubclass(PyThreadState *tstate, PyObject *derived, PyObject *cls)

25392548

{

25402549

_Py_IDENTIFIER(__subclasscheck__);

25412550

PyObject *checker;

@@ -2549,47 +2558,56 @@ PyObject_IsSubclass(PyObject *derived, PyObject *cls)

25492558

}

2550255925512560

if (PyTuple_Check(cls)) {

2552-

Py_ssize_t i;

2553-

Py_ssize_t n;

2554-

int r = 0;

255525612556-

if (Py_EnterRecursiveCall(" in __subclasscheck__"))

2562+

if (_Py_EnterRecursiveCall(tstate, " in __subclasscheck__")) {

25572563

return -1;

2558-

n = PyTuple_GET_SIZE(cls);

2559-

for (i = 0; i < n; ++i) {

2564+

}

2565+

Py_ssize_t n = PyTuple_GET_SIZE(cls);

2566+

int r = 0;

2567+

for (Py_ssize_t i = 0; i < n; ++i) {

25602568

PyObject *item = PyTuple_GET_ITEM(cls, i);

2561-

r = PyObject_IsSubclass(derived, item);

2569+

r = object_issubclass(tstate, derived, item);

25622570

if (r != 0)

25632571

/* either found it, or got an error */

25642572

break;

25652573

}

2566-

Py_LeaveRecursiveCall();

2574+

_Py_LeaveRecursiveCall(tstate);

25672575

return r;

25682576

}

2569257725702578

checker = _PyObject_LookupSpecial(cls, &PyId___subclasscheck__);

25712579

if (checker != NULL) {

2572-

PyObject *res;

25732580

int ok = -1;

2574-

if (Py_EnterRecursiveCall(" in __subclasscheck__")) {

2581+

if (_Py_EnterRecursiveCall(tstate, " in __subclasscheck__")) {

25752582

Py_DECREF(checker);

25762583

return ok;

25772584

}

2578-

res = _PyObject_CallOneArg(checker, derived);

2579-

Py_LeaveRecursiveCall();

2585+

PyObject *res = _PyObject_CallOneArg(checker, derived);

2586+

_Py_LeaveRecursiveCall(tstate);

25802587

Py_DECREF(checker);

25812588

if (res != NULL) {

25822589

ok = PyObject_IsTrue(res);

25832590

Py_DECREF(res);

25842591

}

25852592

return ok;

25862593

}

2587-

else if (PyErr_Occurred())

2594+

else if (_PyErr_Occurred(tstate)) {

25882595

return -1;

2596+

}

2597+25892598

/* Probably never reached anymore. */

25902599

return recursive_issubclass(derived, cls);

25912600

}

259226012602+2603+

int

2604+

PyObject_IsSubclass(PyObject *derived, PyObject *cls)

2605+

{

2606+

PyThreadState *tstate = _PyThreadState_GET();

2607+

return object_issubclass(tstate, derived, cls);

2608+

}

2609+2610+25932611

int

25942612

_PyObject_RealIsInstance(PyObject *inst, PyObject *cls)

25952613

{