Skip to content

Commit fcb3d2f

Browse files
authored
Restore PEP 523 functionality. (GH-28871)
1 parent 9883ca4 commit fcb3d2f

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

Include/internal/pycore_ceval.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ extern PyObject *_PyEval_BuiltinsFromGlobals(
4343
static inline PyObject*
4444
_PyEval_EvalFrame(PyThreadState *tstate, struct _interpreter_frame *frame, int throwflag)
4545
{
46+
if (tstate->interp->eval_frame == NULL) {
47+
return _PyEval_EvalFrameDefault(tstate, frame, throwflag);
48+
}
4649
return tstate->interp->eval_frame(tstate, frame, throwflag);
4750
}
4851

Python/ceval.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4612,7 +4612,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
46124612

46134613
// Check if the call can be inlined or not
46144614
PyObject *function = PEEK(oparg + 1);
4615-
if (Py_TYPE(function) == &PyFunction_Type) {
4615+
if (Py_TYPE(function) == &PyFunction_Type && tstate->interp->eval_frame == NULL) {
46164616
int code_flags = ((PyCodeObject*)PyFunction_GET_CODE(function))->co_flags;
46174617
PyObject *locals = code_flags & CO_OPTIMIZED ? NULL : PyFunction_GET_GLOBALS(function);
46184618
int is_generator = code_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR);
@@ -4630,7 +4630,6 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
46304630
}
46314631

46324632
STACK_SHRINK(oparg + 1);
4633-
assert(tstate->interp->eval_frame != NULL);
46344633
// The frame has stolen all the arguments from the stack,
46354634
// so there is no need to clean them up.
46364635
Py_DECREF(function);
@@ -5687,7 +5686,7 @@ _PyEvalFramePushAndInit(PyThreadState *tstate, PyFrameConstructor *con,
56875686
if (steal_args) {
56885687
// If we failed to initialize locals, make sure the caller still own all the
56895688
// arguments. Notice that we only need to increase the reference count of the
5690-
// *valid* arguments (i.e. the ones that fit into the frame).
5689+
// *valid* arguments (i.e. the ones that fit into the frame).
56915690
PyCodeObject *co = (PyCodeObject*)con->fc_code;
56925691
const size_t total_args = co->co_argcount + co->co_kwonlyargcount;
56935692
for (size_t i = 0; i < Py_MIN(argcount, total_args); i++) {
@@ -5734,7 +5733,6 @@ _PyEval_Vector(PyThreadState *tstate, PyFrameConstructor *con,
57345733
if (frame == NULL) {
57355734
return NULL;
57365735
}
5737-
assert (tstate->interp->eval_frame != NULL);
57385736
PyObject *retval = _PyEval_EvalFrame(tstate, frame, 0);
57395737
assert(_PyFrame_GetStackPointer(frame) == _PyFrame_Stackbase(frame));
57405738
if (_PyEvalFrameClearAndPop(tstate, frame)) {

Python/pystate.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ PyInterpreterState_New(void)
230230
PyConfig_InitPythonConfig(&interp->config);
231231
_PyType_InitCache(interp);
232232

233-
interp->eval_frame = _PyEval_EvalFrameDefault;
233+
interp->eval_frame = NULL;
234234
#ifdef HAVE_DLOPEN
235235
#if HAVE_DECL_RTLD_NOW
236236
interp->dlopenflags = RTLD_NOW;
@@ -1973,6 +1973,9 @@ _register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry)
19731973
_PyFrameEvalFunction
19741974
_PyInterpreterState_GetEvalFrameFunc(PyInterpreterState *interp)
19751975
{
1976+
if (interp->eval_frame == NULL) {
1977+
return _PyEval_EvalFrameDefault;
1978+
}
19761979
return interp->eval_frame;
19771980
}
19781981

@@ -1981,7 +1984,12 @@ void
19811984
_PyInterpreterState_SetEvalFrameFunc(PyInterpreterState *interp,
19821985
_PyFrameEvalFunction eval_frame)
19831986
{
1984-
interp->eval_frame = eval_frame;
1987+
if (eval_frame == _PyEval_EvalFrameDefault) {
1988+
interp->eval_frame = NULL;
1989+
}
1990+
else {
1991+
interp->eval_frame = eval_frame;
1992+
}
19851993
}
19861994

19871995

0 commit comments

Comments
 (0)