Skip to content

Commit 47c7a30

Browse files
committed
Revert "[3.12] pythongh-100762: Fix optimization in gen_close (pythonGH-111069) (python#115818)"
This reverts commit eb4774d.
1 parent 8883db0 commit 47c7a30

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
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, 1)
87-
self.assertEqual(nc, 1)
86+
self.assertEqual(cc, 2)
87+
self.assertEqual(nc, 2)
8888

8989

9090
class TestCommandLine(unittest.TestCase):

Lib/test/test_sys_setprofile.py

+4
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ 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
268272
(2, 'call', f_ident),
269273
(2, 'return', f_ident),
270274
(1, 'return', g_ident),

Objects/genobject.c

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

379380
if (gen->gi_frame_state == FRAME_CREATED) {
@@ -383,7 +384,6 @@ gen_close(PyGenObject *gen, PyObject *args)
383384
if (gen->gi_frame_state >= FRAME_COMPLETED) {
384385
Py_RETURN_NONE;
385386
}
386-
PyObject *yf = _PyGen_yf(gen);
387387
if (yf) {
388388
PyFrameState state = gen->gi_frame_state;
389389
gen->gi_frame_state = FRAME_EXECUTING;
@@ -396,13 +396,12 @@ 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.arg;
399+
int exception_handler_depth = frame->prev_instr[0].op.code;
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;
406405
Py_RETURN_NONE;
407406
}
408407
}

0 commit comments

Comments
 (0)