Skip to content

Commit

Permalink
pythongh-113939: Frame clear, clear locals (python#113940)
Browse files Browse the repository at this point in the history
  • Loading branch information
albertz committed Jan 31, 2024
1 parent b905fad commit 78c2545
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Lib/test/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ class C:
# 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 78c2545

Please sign in to comment.