Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-45963: Fix leak when an exception is raised during generator creation. #29960

Merged
merged 1 commit into from
Dec 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 7 additions & 20 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -5854,24 +5854,6 @@ initialize_locals(PyThreadState *tstate, PyFunctionObject *func,
return -1;
}

static int
initialize_coro_frame(InterpreterFrame *frame, PyThreadState *tstate,
PyFunctionObject *func, PyObject *locals,
PyObject *const *args, Py_ssize_t argcount,
PyObject *kwnames)
{
assert(is_tstate_valid(tstate));
assert(func->func_defaults == NULL || PyTuple_CheckExact(func->func_defaults));
PyCodeObject *code = (PyCodeObject *)func->func_code;
_PyFrame_InitializeSpecials(frame, func, locals, code->co_nlocalsplus);
for (int i = 0; i < code->co_nlocalsplus; i++) {
frame->localsplus[i] = NULL;
}
assert(frame->frame_obj == NULL);
return initialize_locals(tstate, func, frame->localsplus, args, argcount, kwnames);
}


/* Consumes all the references to the args */
static PyObject *
make_coro(PyThreadState *tstate, PyFunctionObject *func,
Expand All @@ -5885,12 +5867,17 @@ make_coro(PyThreadState *tstate, PyFunctionObject *func,
return NULL;
}
InterpreterFrame *frame = (InterpreterFrame *)((PyGenObject *)gen)->gi_iframe;
if (initialize_coro_frame(frame, tstate, func, locals, args, argcount, kwnames)) {
PyCodeObject *code = (PyCodeObject *)func->func_code;
_PyFrame_InitializeSpecials(frame, func, locals, code->co_nlocalsplus);
for (int i = 0; i < code->co_nlocalsplus; i++) {
frame->localsplus[i] = NULL;
}
((PyGenObject *)gen)->gi_frame_valid = 1;
if (initialize_locals(tstate, func, frame->localsplus, args, argcount, kwnames)) {
Py_DECREF(gen);
return NULL;
}
frame->generator = gen;
((PyGenObject *)gen)->gi_frame_valid = 1;
return gen;
}

Expand Down