From 084c91a29f6a439257e2add2717040b54a7a5823 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Thu, 19 Oct 2023 14:32:00 +0100 Subject: [PATCH 1/6] gh-100762: fix refleak in gen_close --- Objects/genobject.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Objects/genobject.c b/Objects/genobject.c index 491e631f2242e9..f91cf2c42a7961 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -378,7 +378,6 @@ 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) { @@ -388,6 +387,8 @@ 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; From eafd6b15f7a38e3a8ea3773b4cb93a219ab827e9 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Thu, 19 Oct 2023 16:44:14 +0100 Subject: [PATCH 2/6] assert instead --- Objects/genobject.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Objects/genobject.c b/Objects/genobject.c index f91cf2c42a7961..9b196064b5be3d 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -378,17 +378,19 @@ 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) { + assert(yf == NULL); gen->gi_frame_state = FRAME_COMPLETED; Py_RETURN_NONE; } if (gen->gi_frame_state >= FRAME_COMPLETED) { + assert(yf == NULL); Py_RETURN_NONE; } - PyObject *yf = _PyGen_yf(gen); if (yf) { PyFrameState state = gen->gi_frame_state; gen->gi_frame_state = FRAME_EXECUTING; From d3e4f64d4387046fbef0a8b5befae45b3d40cbd7 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Thu, 19 Oct 2023 16:44:51 +0100 Subject: [PATCH 3/6] op.code --> op.arg --- Objects/genobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/genobject.c b/Objects/genobject.c index 9b196064b5be3d..fa04997f79d715 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -403,7 +403,7 @@ 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.code; + int exception_handler_depth = frame->prev_instr[0].op.arg; assert(exception_handler_depth > 0); /* We can safely ignore the outermost try block * as it automatically generated to handle From 9a83cf873aa53ea684e7611a62dcf829e7b01a80 Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Thu, 19 Oct 2023 16:50:03 +0100 Subject: [PATCH 4/6] whitespace --- Objects/genobject.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Objects/genobject.c b/Objects/genobject.c index fa04997f79d715..25a459d414760d 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -390,7 +390,6 @@ gen_close(PyGenObject *gen, PyObject *args) assert(yf == NULL); Py_RETURN_NONE; } - if (yf) { PyFrameState state = gen->gi_frame_state; gen->gi_frame_state = FRAME_EXECUTING; From 90d64094d140b08ac04a476bc2bf26bf2bef081f Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 20 Oct 2023 13:53:54 +0100 Subject: [PATCH 5/6] set frame state when returning early --- Objects/genobject.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Objects/genobject.c b/Objects/genobject.c index 25a459d414760d..c3e3ccea069b98 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -408,6 +408,7 @@ gen_close(PyGenObject *gen, PyObject *args) * as it automatically generated to handle * StopIteration. */ if (exception_handler_depth == 1) { + gen->gi_frame_state = FRAME_COMPLETED; Py_RETURN_NONE; } } From 86b325dfe96e0dce9464c976ab43d60c3d8d18b6 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Tue, 24 Oct 2023 12:10:53 +0100 Subject: [PATCH 6/6] apply review comments --- Lib/test/test_cprofile.py | 4 ++-- Lib/test/test_sys_setprofile.py | 4 ---- Objects/genobject.c | 4 +--- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/Lib/test/test_cprofile.py b/Lib/test/test_cprofile.py index 3056fe84dac5dd..27e8a767903777 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, 2) - self.assertEqual(nc, 2) + self.assertEqual(cc, 1) + self.assertEqual(nc, 1) class TestCommandLine(unittest.TestCase): diff --git a/Lib/test/test_sys_setprofile.py b/Lib/test/test_sys_setprofile.py index 9e8936630de920..bb8adc8b555cb4 100644 --- a/Lib/test/test_sys_setprofile.py +++ b/Lib/test/test_sys_setprofile.py @@ -265,10 +265,6 @@ 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 c3e3ccea069b98..6794cc693970c5 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -378,18 +378,16 @@ 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) { - assert(yf == NULL); gen->gi_frame_state = FRAME_COMPLETED; Py_RETURN_NONE; } if (gen->gi_frame_state >= FRAME_COMPLETED) { - assert(yf == NULL); Py_RETURN_NONE; } + PyObject *yf = _PyGen_yf(gen); if (yf) { PyFrameState state = gen->gi_frame_state; gen->gi_frame_state = FRAME_EXECUTING;