From 8883db01f53d45386912f7570f051998f4e1a8b0 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Thu, 13 Jun 2024 19:22:15 +0100 Subject: [PATCH 1/3] Revert "[3.12] gh-118272: Clear generator frame's locals when the generator is closed (#118451)" This reverts commit 238efbecab24204f822b1d1611914f5bcb2ae2de. --- Include/internal/pycore_frame.h | 3 --- Lib/test/test_generators.py | 20 -------------------- Objects/genobject.c | 1 - Python/frame.c | 18 +++++------------- 4 files changed, 5 insertions(+), 37 deletions(-) diff --git a/Include/internal/pycore_frame.h b/Include/internal/pycore_frame.h index ad7d74c5dd2b9f..bfe4a759bac078 100644 --- a/Include/internal/pycore_frame.h +++ b/Include/internal/pycore_frame.h @@ -213,9 +213,6 @@ _PyFrame_GetFrameObject(_PyInterpreterFrame *frame) return _PyFrame_MakeAndSetFrameObject(frame); } -void -_PyFrame_ClearLocals(_PyInterpreterFrame *frame); - /* Clears all references in the frame. * If take is non-zero, then the _PyInterpreterFrame frame * may be transferred to the frame object it references diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py index e0da9152c33954..1ee9958445bf18 100644 --- a/Lib/test/test_generators.py +++ b/Lib/test/test_generators.py @@ -450,26 +450,6 @@ def g(): self.assertIsInstance(cm.exception.value, StopIteration) self.assertEqual(cm.exception.value.value, 2) - def test_close_releases_frame_locals(self): - # See gh-118272 - - class Foo: - pass - - f = Foo() - f_wr = weakref.ref(f) - - def genfn(): - a = f - yield - - g = genfn() - next(g) - del f - g.close() - support.gc_collect() - self.assertIsNone(f_wr()) - class GeneratorThrowTest(unittest.TestCase): diff --git a/Objects/genobject.c b/Objects/genobject.c index dc034a4b723472..119a71fcabf04a 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -403,7 +403,6 @@ gen_close(PyGenObject *gen, PyObject *args) * StopIteration. */ if (exception_handler_depth == 1) { gen->gi_frame_state = FRAME_COMPLETED; - _PyFrame_ClearLocals((_PyInterpreterFrame *)gen->gi_iframe); Py_RETURN_NONE; } } diff --git a/Python/frame.c b/Python/frame.c index a49215fa44a796..b84fd9b6a9380a 100644 --- a/Python/frame.c +++ b/Python/frame.c @@ -115,18 +115,6 @@ take_ownership(PyFrameObject *f, _PyInterpreterFrame *frame) } } -void -_PyFrame_ClearLocals(_PyInterpreterFrame *frame) -{ - assert(frame->stacktop >= 0); - int stacktop = frame->stacktop; - frame->stacktop = 0; - for (int i = 0; i < stacktop; i++) { - Py_XDECREF(frame->localsplus[i]); - } - Py_CLEAR(frame->f_locals); -} - void _PyFrame_ClearExceptCode(_PyInterpreterFrame *frame) { @@ -147,8 +135,12 @@ _PyFrame_ClearExceptCode(_PyInterpreterFrame *frame) } Py_DECREF(f); } - _PyFrame_ClearLocals(frame); + assert(frame->stacktop >= 0); + for (int i = 0; i < frame->stacktop; i++) { + Py_XDECREF(frame->localsplus[i]); + } Py_XDECREF(frame->frame_obj); + Py_XDECREF(frame->f_locals); Py_DECREF(frame->f_funcobj); } From 47c7a304d73b0b13ac5d3a9d35302a959acee6ee Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Thu, 13 Jun 2024 19:22:27 +0100 Subject: [PATCH 2/3] Revert "[3.12] gh-100762: Fix optimization in gen_close (GH-111069) (#115818)" This reverts commit eb4774d2b7f5d50cd3cb8dc5abce9f94ce54197e. --- Lib/test/test_cprofile.py | 4 ++-- Lib/test/test_sys_setprofile.py | 4 ++++ Objects/genobject.c | 5 ++--- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_cprofile.py b/Lib/test/test_cprofile.py index 27e8a767903777..3056fe84dac5dd 100644 --- a/Lib/test/test_cprofile.py +++ b/Lib/test/test_cprofile.py @@ -83,8 +83,8 @@ def test_throw(self): for func, (cc, nc, _, _, _) in pr.stats.items(): if func[2] == "": - self.assertEqual(cc, 1) - self.assertEqual(nc, 1) + self.assertEqual(cc, 2) + self.assertEqual(nc, 2) class TestCommandLine(unittest.TestCase): diff --git a/Lib/test/test_sys_setprofile.py b/Lib/test/test_sys_setprofile.py index bb8adc8b555cb4..9e8936630de920 100644 --- a/Lib/test/test_sys_setprofile.py +++ b/Lib/test/test_sys_setprofile.py @@ -265,6 +265,10 @@ def g(p): f_ident = ident(f) g_ident = ident(g) 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, 'return', f_ident), (1, 'return', g_ident), diff --git a/Objects/genobject.c b/Objects/genobject.c index 119a71fcabf04a..474abe1094be72 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -374,6 +374,7 @@ static PyObject * gen_close(PyGenObject *gen, PyObject *args) { PyObject *retval; + PyObject *yf = _PyGen_yf(gen); int err = 0; if (gen->gi_frame_state == FRAME_CREATED) { @@ -383,7 +384,6 @@ gen_close(PyGenObject *gen, PyObject *args) if (gen->gi_frame_state >= FRAME_COMPLETED) { Py_RETURN_NONE; } - PyObject *yf = _PyGen_yf(gen); if (yf) { PyFrameState state = gen->gi_frame_state; gen->gi_frame_state = FRAME_EXECUTING; @@ -396,13 +396,12 @@ gen_close(PyGenObject *gen, PyObject *args) * YIELD_VALUE if the debugger has changed the lineno. */ if (err == 0 && is_yield(frame->prev_instr)) { assert(is_resume(frame->prev_instr + 1)); - int exception_handler_depth = frame->prev_instr[0].op.arg; + int exception_handler_depth = frame->prev_instr[0].op.code; assert(exception_handler_depth > 0); /* We can safely ignore the outermost try block * as it automatically generated to handle * StopIteration. */ if (exception_handler_depth == 1) { - gen->gi_frame_state = FRAME_COMPLETED; Py_RETURN_NONE; } } From dc42b2a33436da3178f6118f0ce5d297abd5d3d9 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Mon, 17 Jun 2024 18:17:00 +0100 Subject: [PATCH 3/3] add test --- Lib/test/test_generators.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py index 1ee9958445bf18..83ca8a6096c646 100644 --- a/Lib/test/test_generators.py +++ b/Lib/test/test_generators.py @@ -6,6 +6,7 @@ import unittest import weakref import inspect +import types from test import support @@ -89,9 +90,12 @@ def gen(): self.assertEqual(gc.garbage, old_garbage) def test_lambda_generator(self): - # Issue #23192: Test that a lambda returning a generator behaves + # bpo-23192, gh-119897: Test that a lambda returning a generator behaves # like the equivalent function f = lambda: (yield 1) + self.assertIsInstance(f(), types.GeneratorType) + self.assertEqual(next(f()), 1) + def g(): return (yield 1) # test 'yield from'