Skip to content

Commit eb4774d

Browse files
[3.12] pythongh-100762: Fix optimization in gen_close (pythonGH-111069) (python#115818)
pythongh-100762: Fix optimization in gen_close (pythonGH-111069) (cherry picked from commit 0db2517) Co-authored-by: Irit Katriel <[email protected]>
1 parent db33060 commit eb4774d

File tree

3 files changed

+5
-8
lines changed

3 files changed

+5
-8
lines changed

Lib/test/test_cprofile.py

+2-2
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

-4
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

+3-2
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,6 @@ static PyObject *
374374
gen_close(PyGenObject *gen, PyObject *args)
375375
{
376376
PyObject *retval;
377-
PyObject *yf = _PyGen_yf(gen);
378377
int err = 0;
379378

380379
if (gen->gi_frame_state == FRAME_CREATED) {
@@ -384,6 +383,7 @@ gen_close(PyGenObject *gen, PyObject *args)
384383
if (gen->gi_frame_state >= FRAME_COMPLETED) {
385384
Py_RETURN_NONE;
386385
}
386+
PyObject *yf = _PyGen_yf(gen);
387387
if (yf) {
388388
PyFrameState state = gen->gi_frame_state;
389389
gen->gi_frame_state = FRAME_EXECUTING;
@@ -396,12 +396,13 @@ gen_close(PyGenObject *gen, PyObject *args)
396396
* YIELD_VALUE if the debugger has changed the lineno. */
397397
if (err == 0 && is_yield(frame->prev_instr)) {
398398
assert(is_resume(frame->prev_instr + 1));
399-
int exception_handler_depth = frame->prev_instr[0].op.code;
399+
int exception_handler_depth = frame->prev_instr[0].op.arg;
400400
assert(exception_handler_depth > 0);
401401
/* We can safely ignore the outermost try block
402402
* as it automatically generated to handle
403403
* StopIteration. */
404404
if (exception_handler_depth == 1) {
405+
gen->gi_frame_state = FRAME_COMPLETED;
405406
Py_RETURN_NONE;
406407
}
407408
}

0 commit comments

Comments
 (0)