Skip to content

Commit 064e53d

Browse files
authored
Fix leak when an exception is raised during generator creation. (GH-29960)
1 parent d596acb commit 064e53d

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

Python/ceval.c

+7-20
Original file line numberDiff line numberDiff line change
@@ -5852,24 +5852,6 @@ initialize_locals(PyThreadState *tstate, PyFunctionObject *func,
58525852
return -1;
58535853
}
58545854

5855-
static int
5856-
initialize_coro_frame(InterpreterFrame *frame, PyThreadState *tstate,
5857-
PyFunctionObject *func, PyObject *locals,
5858-
PyObject *const *args, Py_ssize_t argcount,
5859-
PyObject *kwnames)
5860-
{
5861-
assert(is_tstate_valid(tstate));
5862-
assert(func->func_defaults == NULL || PyTuple_CheckExact(func->func_defaults));
5863-
PyCodeObject *code = (PyCodeObject *)func->func_code;
5864-
_PyFrame_InitializeSpecials(frame, func, locals, code->co_nlocalsplus);
5865-
for (int i = 0; i < code->co_nlocalsplus; i++) {
5866-
frame->localsplus[i] = NULL;
5867-
}
5868-
assert(frame->frame_obj == NULL);
5869-
return initialize_locals(tstate, func, frame->localsplus, args, argcount, kwnames);
5870-
}
5871-
5872-
58735855
/* Consumes all the references to the args */
58745856
static PyObject *
58755857
make_coro(PyThreadState *tstate, PyFunctionObject *func,
@@ -5883,12 +5865,17 @@ make_coro(PyThreadState *tstate, PyFunctionObject *func,
58835865
return NULL;
58845866
}
58855867
InterpreterFrame *frame = (InterpreterFrame *)((PyGenObject *)gen)->gi_iframe;
5886-
if (initialize_coro_frame(frame, tstate, func, locals, args, argcount, kwnames)) {
5868+
PyCodeObject *code = (PyCodeObject *)func->func_code;
5869+
_PyFrame_InitializeSpecials(frame, func, locals, code->co_nlocalsplus);
5870+
for (int i = 0; i < code->co_nlocalsplus; i++) {
5871+
frame->localsplus[i] = NULL;
5872+
}
5873+
((PyGenObject *)gen)->gi_frame_valid = 1;
5874+
if (initialize_locals(tstate, func, frame->localsplus, args, argcount, kwnames)) {
58875875
Py_DECREF(gen);
58885876
return NULL;
58895877
}
58905878
frame->generator = gen;
5891-
((PyGenObject *)gen)->gi_frame_valid = 1;
58925879
return gen;
58935880
}
58945881

0 commit comments

Comments
 (0)