gh-100762: Fix optimization in gen_close (#111069)

This commit is contained in:
Irit Katriel 2023-10-25 16:22:34 +01:00 committed by GitHub
parent a4981921aa
commit 0db2517687
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 5 additions and 8 deletions

View File

@ -83,8 +83,8 @@ class CProfileTest(ProfileTest):
for func, (cc, nc, _, _, _) in pr.stats.items(): for func, (cc, nc, _, _, _) in pr.stats.items():
if func[2] == "<genexpr>": if func[2] == "<genexpr>":
self.assertEqual(cc, 2) self.assertEqual(cc, 1)
self.assertEqual(nc, 2) self.assertEqual(nc, 1)
class TestCommandLine(unittest.TestCase): class TestCommandLine(unittest.TestCase):

View File

@ -265,10 +265,6 @@ class ProfileHookTestCase(TestCaseBase):
f_ident = ident(f) f_ident = ident(f)
g_ident = ident(g) g_ident = ident(g)
self.check_events(g, [(1, 'call', g_ident), self.check_events(g, [(1, 'call', g_ident),
(2, 'call', f_ident),
(2, 'return', f_ident),
# once more; the generator is being garbage collected
# and it will do a PY_THROW
(2, 'call', f_ident), (2, 'call', f_ident),
(2, 'return', f_ident), (2, 'return', f_ident),
(1, 'return', g_ident), (1, 'return', g_ident),

View File

@ -378,7 +378,6 @@ static PyObject *
gen_close(PyGenObject *gen, PyObject *args) gen_close(PyGenObject *gen, PyObject *args)
{ {
PyObject *retval; PyObject *retval;
PyObject *yf = _PyGen_yf(gen);
int err = 0; int err = 0;
if (gen->gi_frame_state == FRAME_CREATED) { if (gen->gi_frame_state == FRAME_CREATED) {
@ -388,6 +387,7 @@ gen_close(PyGenObject *gen, PyObject *args)
if (gen->gi_frame_state >= FRAME_COMPLETED) { if (gen->gi_frame_state >= FRAME_COMPLETED) {
Py_RETURN_NONE; Py_RETURN_NONE;
} }
PyObject *yf = _PyGen_yf(gen);
if (yf) { if (yf) {
PyFrameState state = gen->gi_frame_state; PyFrameState state = gen->gi_frame_state;
gen->gi_frame_state = FRAME_EXECUTING; gen->gi_frame_state = FRAME_EXECUTING;
@ -400,12 +400,13 @@ gen_close(PyGenObject *gen, PyObject *args)
* YIELD_VALUE if the debugger has changed the lineno. */ * YIELD_VALUE if the debugger has changed the lineno. */
if (err == 0 && is_yield(frame->prev_instr)) { if (err == 0 && is_yield(frame->prev_instr)) {
assert(is_resume(frame->prev_instr + 1)); assert(is_resume(frame->prev_instr + 1));
int exception_handler_depth = frame->prev_instr[0].op.code; int exception_handler_depth = frame->prev_instr[0].op.arg;
assert(exception_handler_depth > 0); assert(exception_handler_depth > 0);
/* We can safely ignore the outermost try block /* We can safely ignore the outermost try block
* as it automatically generated to handle * as it automatically generated to handle
* StopIteration. */ * StopIteration. */
if (exception_handler_depth == 1) { if (exception_handler_depth == 1) {
gen->gi_frame_state = FRAME_COMPLETED;
Py_RETURN_NONE; Py_RETURN_NONE;
} }
} }