Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Avoid expensive ``gen.throw()`` call when closing generators (and
coroutines) that can be closed trivially.
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