Message282533
(B7) frame.clear() does not clear f_locals, unlike what a test says
(Lib/test/test_frame.py)::
def test_locals_clear_locals(self):
# Test f_locals before and after clear() (to exercise caching)
f, outer, inner = self.make_frames()
outer.f_locals
inner.f_locals
outer.clear()
inner.clear()
self.assertEqual(outer.f_locals, {})
self.assertEqual(inner.f_locals, {})
This test passes, but the C-level PyFrameObject has got a strong
reference to f_locals, which is only updated (to be empty) if the
Python code tries to read this attribute. In the normal case,
code that calls clear() but doesn't read f_locals afterwards will
still leak everything contained in the C-level f_locals field. This
can be shown by this failing test::
import sys
def g():
x = 42
return sys._getframe()
frame = g()
d = frame.f_locals
frame.clear()
print(d)
assert d == {} # fails! but 'assert d is frame.f_locals' passes,
# which shows that this dict is kept alive by
# 'frame'; and we've seen that it is non-empty
# as long as we don't read frame.f_locals. |
|
Date |
User |
Action |
Args |
2016-12-06 11:57:39 | arigo | set | recipients:
+ arigo |
2016-12-06 11:57:39 | arigo | set | messageid: <1481025459.28.0.556916407815.issue28884@psf.upfronthosting.co.za> |
2016-12-06 11:57:39 | arigo | link | issue28884 messages |
2016-12-06 11:57:38 | arigo | create | |
|