Skip to content

gh-151722: Defer GC tracking in frozendict.copy()#152230

Open
vstinner wants to merge 2 commits into
python:mainfrom
vstinner:frozendict_copy
Open

gh-151722: Defer GC tracking in frozendict.copy()#152230
vstinner wants to merge 2 commits into
python:mainfrom
vstinner:frozendict_copy

Conversation

@vstinner

@vstinner vstinner commented Jun 25, 2026

Copy link
Copy Markdown
Member

Fix _PyDict_Or() and frozendict.copy(): only track the frozendict by the GC once the dictionary is fully initialized.

Functions modifying frozendict now ensures that the object is not tracked by the GC (in debug mode).

  • can_modify_dict() checks that _PyObject_GC_IS_TRACKED() is false for frozendicts.
  • dict_merge_api() makes sure that the dictionary is tracked by the GC.

Fix _PyDict_Or() and frozendict.copy(): only track the frozendict by
the GC once the dictionary is fully initialized.

Functions modifying frozendict now ensures that the object is not
tracked by the GC (in debug mode).

* can_modify_dict() checks that _PyObject_GC_IS_TRACKED() is false
  for frozendicts.
* dict_merge_api() makes sure that the dictionary is tracked by the
  GC.
@vstinner

Copy link
Copy Markdown
Member Author

@corona10 @methane: More frozendict fixes.

With this change, there are now many checks in can_modify_dict() to make sure that we don't modify a frozendict exposed in Python. It should catch future bugs.

@corona10

Copy link
Copy Markdown
Member

Let me take a look till this weekend.

@corona10 corona10 self-requested a review June 25, 2026 20:03

@corona10 corona10 left a comment

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.

hmm lgtm

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

Labels

awaiting merge needs backport to 3.15 pre-release feature fixes, bugs and security fixes skip news

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants