Skip to content

Commit 3baff26

Browse files
iritkatrielaisk
authored andcommitted
pythongh-100762: Fix optimization in gen_close (python#111069)
1 parent 6134250 commit 3baff26

File tree

3 files changed

+5
-8
lines changed

3 files changed

+5
-8
lines changed

Lib/test/test_cprofile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ def test_throw(self):
8383

8484
for func, (cc, nc, _, _, _) in pr.stats.items():
8585
if func[2] == "<genexpr>":
86-
self.assertEqual(cc, 2)
87-
self.assertEqual(nc, 2)
86+
self.assertEqual(cc, 1)
87+
self.assertEqual(nc, 1)
8888

8989

9090
class TestCommandLine(unittest.TestCase):

Lib/test/test_sys_setprofile.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,6 @@ def g(p):
265265
f_ident = ident(f)
266266
g_ident = ident(g)
267267
self.check_events(g, [(1, 'call', g_ident),
268-
(2, 'call', f_ident),
269-
(2, 'return', f_ident),
270-
# once more; the generator is being garbage collected
271-
# and it will do a PY_THROW
272268
(2, 'call', f_ident),
273269
(2, 'return', f_ident),
274270
(1, 'return', g_ident),

Objects/genobject.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,6 @@ static PyObject *
378378
gen_close(PyGenObject *gen, PyObject *args)
379379
{
380380
PyObject *retval;
381-
PyObject *yf = _PyGen_yf(gen);
382381
int err = 0;
383382

384383
if (gen->gi_frame_state == FRAME_CREATED) {
@@ -388,6 +387,7 @@ gen_close(PyGenObject *gen, PyObject *args)
388387
if (gen->gi_frame_state >= FRAME_COMPLETED) {
389388
Py_RETURN_NONE;
390389
}
390+
PyObject *yf = _PyGen_yf(gen);
391391
if (yf) {
392392
PyFrameState state = gen->gi_frame_state;
393393
gen->gi_frame_state = FRAME_EXECUTING;
@@ -400,12 +400,13 @@ gen_close(PyGenObject *gen, PyObject *args)
400400
* YIELD_VALUE if the debugger has changed the lineno. */
401401
if (err == 0 && is_yield(frame->prev_instr)) {
402402
assert(is_resume(frame->prev_instr + 1));
403-
int exception_handler_depth = frame->prev_instr[0].op.code;
403+
int exception_handler_depth = frame->prev_instr[0].op.arg;
404404
assert(exception_handler_depth > 0);
405405
/* We can safely ignore the outermost try block
406406
* as it automatically generated to handle
407407
* StopIteration. */
408408
if (exception_handler_depth == 1) {
409+
gen->gi_frame_state = FRAME_COMPLETED;
409410
Py_RETURN_NONE;
410411
}
411412
}

0 commit comments

Comments
 (0)