diff options
author | Albert Zeyer <albzey@gmail.com> | 2024-01-31 19:14:44 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-31 19:14:44 (GMT) |
commit | 78c254582b1757c15098ae65e97a2589ae663cd7 (patch) | |
tree | 62424661fd61ba9f10f52155801ff24f1ab110ce | |
parent | b905fad83819ec9102ecfb97e3d8ab0aaddd9784 (diff) | |
download | cpython-78c254582b1757c15098ae65e97a2589ae663cd7.zip cpython-78c254582b1757c15098ae65e97a2589ae663cd7.tar.gz cpython-78c254582b1757c15098ae65e97a2589ae663cd7.tar.bz2 |
gh-113939: Frame clear, clear locals (#113940)
-rw-r--r-- | Lib/test/test_frame.py | 22 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Core and Builtins/2024-01-12-16-40-07.gh-issue-113939.Yi3L-e.rst | 4 | ||||
-rw-r--r-- | Objects/frameobject.c | 1 |
3 files changed, 27 insertions, 0 deletions
diff --git a/Lib/test/test_frame.py b/Lib/test/test_frame.py index 7f17666..244ce8a 100644 --- a/Lib/test/test_frame.py +++ b/Lib/test/test_frame.py @@ -55,6 +55,28 @@ class ClearTest(unittest.TestCase): # The reference was released by .clear() self.assertIs(None, wr()) + def test_clear_locals_after_f_locals_access(self): + # see gh-113939 + class C: + pass + + wr = None + def inner(): + nonlocal wr + c = C() + wr = weakref.ref(c) + 1/0 + + try: + inner() + except ZeroDivisionError as exc: + support.gc_collect() + self.assertIsNotNone(wr()) + print(exc.__traceback__.tb_next.tb_frame.f_locals) + exc.__traceback__.tb_next.tb_frame.clear() + support.gc_collect() + self.assertIsNone(wr()) + def test_clear_does_not_clear_specials(self): class C: pass diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-01-12-16-40-07.gh-issue-113939.Yi3L-e.rst b/Misc/NEWS.d/next/Core and Builtins/2024-01-12-16-40-07.gh-issue-113939.Yi3L-e.rst new file mode 100644 index 0000000..28b8e4b --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-01-12-16-40-07.gh-issue-113939.Yi3L-e.rst @@ -0,0 +1,4 @@ +frame.clear(): +Clear frame.f_locals as well, and not only the fast locals. +This is relevant once frame.f_locals was accessed, +which would contain also references to all the locals. diff --git a/Objects/frameobject.c b/Objects/frameobject.c index cafe4ef..a914c61 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -926,6 +926,7 @@ frame_tp_clear(PyFrameObject *f) Py_CLEAR(locals[i]); } f->f_frame->stacktop = 0; + Py_CLEAR(f->f_frame->f_locals); return 0; } |