-
-
Notifications
You must be signed in to change notification settings - Fork 35.1k
gh-155176: Don't track frozendicts whose contents can never be tracked by the GC #155178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1869,6 +1869,26 @@ def test_copy(self): | |
| self.assertEqual(d2, frozendict(x=1, y=2)) | ||
| self.assertEqual(type(d2), frozendict) | ||
|
|
||
| def test_gc_tracking(self): | ||
| self.assertFalse(gc.is_tracked(frozendict())) | ||
| self.assertFalse(gc.is_tracked(frozendict({1: 2}))) | ||
| self.assertFalse(gc.is_tracked(frozendict.fromkeys('ab', 1))) | ||
| self.assertFalse(gc.is_tracked(frozendict({1: 2}) | {3: 4})) | ||
|
|
||
| self.assertTrue(gc.is_tracked(frozendict({1: [2]}))) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think is it worth to add a test with {1: (2,)}? |
||
| # subclasses can create reference cycles, they are always tracked | ||
| self.assertTrue(gc.is_tracked(FrozenDict({1: 2}))) | ||
|
|
||
| # a reference cycle through a tracked frozendict is collectable | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would make this a separate test. |
||
| class Obj: | ||
| pass | ||
| obj = Obj() | ||
| obj.fd = frozendict({1: obj}) | ||
| ref = weakref.ref(obj) | ||
| del obj | ||
| gc.collect() | ||
| self.assertIsNone(ref()) | ||
|
|
||
| def test_merge(self): | ||
| # test "a | b" operator | ||
| self.assertEqual(frozendict(x=1) | frozendict(y=2), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| :class:`frozendict` objects whose keys and values can never be tracked by | ||
| the garbage collector are no longer tracked, like :class:`frozenset` | ||
| objects. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please, test nested frozendicts :)