Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Don't call gen.throw() from gen.close() unless necessary.
  • Loading branch information
markshannon committed Jan 25, 2023
commit f67b210bdf5bd386326922bc1cff6c5c83daeb72
12 changes: 12 additions & 0 deletions Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,13 @@ static PyObject *
gen_close(PyGenObject *gen, PyObject *args)
{
PyObject *retval;
if (gen->gi_frame_state == FRAME_CREATED) {
gen->gi_frame_state = FRAME_COMPLETED;
Py_RETURN_NONE;
}
if (gen->gi_frame_state >= FRAME_COMPLETED) {
Py_RETURN_NONE;
}
PyObject *yf = _PyGen_yf(gen);
int err = 0;

Expand All @@ -386,6 +393,11 @@ gen_close(PyGenObject *gen, PyObject *args)
gen->gi_frame_state = state;
Py_DECREF(yf);
}
_PyInterpreterFrame *frame = (_PyInterpreterFrame *)gen->gi_iframe;
assert(PyBytes_CheckExact(frame->f_code->co_exceptiontable));
if (err == 0 && PyBytes_GET_SIZE(frame->f_code->co_exceptiontable) == 0) {
Py_RETURN_NONE;
}
if (err == 0)
PyErr_SetNone(PyExc_GeneratorExit);
retval = gen_send_ex(gen, Py_None, 1, 1);
Expand Down