This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author arigo
Recipients arigo
Date 2016-12-06.11:57:38
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1481025459.28.0.556916407815.issue28884@psf.upfronthosting.co.za>
In-reply-to
Content
(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.
History
Date User Action Args
2016-12-06 11:57:39arigosetrecipients: + arigo
2016-12-06 11:57:39arigosetmessageid: <1481025459.28.0.556916407815.issue28884@psf.upfronthosting.co.za>
2016-12-06 11:57:39arigolinkissue28884 messages
2016-12-06 11:57:38arigocreate