Skip to content

Commit 9ce9e63

Browse files
committed
Fix reference counting when creating a new interpreter frame fails.
1 parent 9210541 commit 9ce9e63

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

Python/ceval.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4645,14 +4645,14 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
46454645
PyObject *function = PEEK(oparg + 1);
46464646
if (Py_TYPE(function) == &PyFunction_Type) {
46474647
PyCodeObject *code = (PyCodeObject*)PyFunction_GET_CODE(function);
4648-
STACK_SHRINK(oparg + 1);
46494648
if ((code->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) == 0) {
4650-
InterpreterFrame *new_frame = _PyEval_FrameFromPyFunctionAndArgs(tstate, stack_pointer+1, oparg, function);
4649+
InterpreterFrame *new_frame = _PyEval_FrameFromPyFunctionAndArgs(tstate, stack_pointer-oparg, oparg, function);
46514650
if (new_frame == NULL) {
46524651
// When we exit here, we own all variables in the stack (the frame creation has not stolen
46534652
// any variable) so we need to clean the whole stack (done in the "error" label).
46544653
goto error;
46554654
}
4655+
STACK_SHRINK(oparg + 1);
46564656
assert(tstate->interp->eval_frame != NULL);
46574657
// The frame has stolen all the arguments from the stack, so there is no need to clean them up.```
46584658
Py_DECREF(function);
@@ -4664,8 +4664,9 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
46644664
else {
46654665
/* Callable is a generator or coroutine function: create coroutine or generator. */
46664666
PyObject *locals = code->co_flags & CO_OPTIMIZED ? NULL : PyFunction_GET_GLOBALS(function);
4667-
res = make_coro(tstate, PyFunction_AS_FRAME_CONSTRUCTOR(function), locals, stack_pointer+1, oparg, NULL);
4668-
for (int i = 0; i < oparg+1; i++) {
4667+
res = make_coro(tstate, PyFunction_AS_FRAME_CONSTRUCTOR(function), locals, stack_pointer-oparg, oparg, NULL);
4668+
STACK_SHRINK(oparg + 1);
4669+
for (int i = 0; i < oparg + 1; i++) {
46694670
Py_DECREF(stack_pointer[i]);
46704671
}
46714672
}

0 commit comments

Comments
 (0)