Skip to content

Commit

Permalink
Python 3.7: gc.get_referrers(a_local_var) is []
Browse files Browse the repository at this point in the history
The important thing for these tests is that the internal data structures
in objgraph's functions do not retain extra references, so we want to
know that the number of referrers does not increase.  It's less
important to make specific assertions about the actual number of
referrers, which apparently differs across Python versions.

I'd like somebody to explain to me why Python 3.7 no longer includes the
currently executing stack frame in the list of referrers for a local
variable.  Some kind of an optimization?
  • Loading branch information
mgedmin committed Feb 1, 2018
1 parent b52a992 commit 5510e32
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,10 @@ def test_no_new_reference_cycles(self):
# count()
gc.disable()
x = type('MyClass', (), {})()
self.assertEqual(len(gc.get_referrers(x)), 1)
before = len(gc.get_referrers(x))
objgraph.count('MyClass')
self.assertEqual(len(gc.get_referrers(x)), 1)
after = len(gc.get_referrers(x))
self.assertEqual(before, after)


class TypestatsTest(GarbageCollectedMixin, unittest.TestCase):
Expand All @@ -276,9 +277,10 @@ def test_no_new_reference_cycles(self):
# typestats()
gc.disable()
x = type('MyClass', (), {})()
self.assertEqual(len(gc.get_referrers(x)), 1)
before = len(gc.get_referrers(x))
objgraph.typestats()
self.assertEqual(len(gc.get_referrers(x)), 1)
after = len(gc.get_referrers(x))
self.assertEqual(before, after)


class TypestatsFilterArguTest(GarbageCollectedMixin, unittest.TestCase):
Expand Down Expand Up @@ -335,11 +337,11 @@ def test_new_garbage(self):
res = objgraph.by_type('MyClass')
self.assertEqual(res, [x])
# referrers we expect:
# 1. this stack frame
# 1. this stack frame (except on Python 3.7 where it's somehow missing)
# 2. the `res` list
# referrers we don't want:
# the ``objects`` list in the now-dead stack frame of objgraph.by_type
self.assertEqual(len(gc.get_referrers(res[0])), 2)
self.assertLessEqual(len(gc.get_referrers(res[0])), 2)


class StringRepresentationTest(GarbageCollectedMixin,
Expand Down

0 comments on commit 5510e32

Please sign in to comment.