pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/python/cpython/pull/155178

ull-requests-12898a7be3dbcd40.css" /> gh-155176: Don't track frozendicts whose contents can never be tracked by the GC by aisk · Pull Request #155178 · python/cpython · GitHub
Skip to content

gh-155176: Don't track frozendicts whose contents can never be tracked by the GC - #155178

Open
aisk wants to merge 2 commits into
python:mainfrom
aisk:frozendict-untrack
Open

gh-155176: Don't track frozendicts whose contents can never be tracked by the GC#155178
aisk wants to merge 2 commits into
python:mainfrom
aisk:frozendict-untrack

Conversation

@aisk

@aisk aisk commented Aug 4, 2026

Copy link
Copy Markdown
Member

Comment thread Lib/test/test_dict.py
Comment thread Lib/test/test_dict.py
Comment thread Lib/test/test_dict.py
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}))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please, test nested frozendicts :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that the current implementation doesn't support nested frozendicts / frozensets. The outer frozendict is conservatively tracked even if the nested one is untracked. This is because the current implementation of _PyObject_GC_MAY_BE_TRACKED only has special support for tuples:

static inline int _PyObject_GC_MAY_BE_TRACKED(PyObject *obj) {
if (!PyObject_IS_GC(obj)) {
return 0;
}
if (PyTuple_CheckExact(obj)) {
return _PyObject_GC_IS_TRACKED(obj);
}
return 1;
}

frozenset has the same limitation. We can update _PyObject_GC_MAY_BE_TRACKED to support frozenset and frozendict too. But I think this could be out of the current PR's scope, and adding a test to check that nested frozendicts are not supported and then updating it in another PR seems meaningless. If you think it's OK, I'll create another issue to track the process.

@methane

methane commented Aug 5, 2026

Copy link
Copy Markdown
Member

I find _PyDict_FromKeys() too complex. Shouldn't the implementations of frozendict.fromkeys() and dict.fromkeys() be separated?

@aisk

aisk commented Aug 5, 2026

Copy link
Copy Markdown
Member Author

I find _PyDict_FromKeys() too complex. Shouldn't the implementations of frozendict.fromkeys() and dict.fromkeys() be separated?

I agree. But since most of the complexity comes from the current implementation and this change only adds a few branches to it, I guess this could be done in another issue, to reduce the review work for this change? If so, I'm happy to create a new issue to track it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy