gh-141510: Add can_modify_dict() in dictobject.c#144955
Merged
vstinner merged 1 commit intopython:mainfrom Feb 18, 2026
Merged
Conversation
can_modify_dict() is stricter than ASSERT_DICT_LOCKED() for frozendict. It uses PyUnstable_Object_IsUniquelyReferenced() which matters for free-threaded builds. Replace anydict_setitem_take2() with setitem_take2_lock_held(). It's no longer useful to have two functions.
corona10
reviewed
Feb 18, 2026
Member
corona10
left a comment
There was a problem hiding this comment.
Just to share my thoughts:
- When we implement an xxx_lock_held function, I believe the goal is to ensure that developers declare critical sections properly through assertions. This change could cause confusion if someone starts calling xxx_lock_held, since it may require ASSERT_DICT_LOCKED for mutable collections but not for immutable ones.
- My personal view is that assertions should report consistent results whenever possible. As I understand it, PyUnstable_Object_IsUniquelyReferenced does not guarantee this. Its result depends on the runtime situation, which means we could miss buggy scenarios until they are triggered in CI.
It does not mean -1 on this change but prefer to listen other core devs opinion.
corona10
approved these changes
Feb 18, 2026
Member
There was a problem hiding this comment.
I changed my mind after DM with Victor.
Since the current assertion is only meaningful when the refcount is not 1.
cpython/Include/internal/pycore_critical_section.h
Lines 55 to 58 in c6a142f
Member
Author
|
This change does multiple things:
|
Member
Author
|
Merged. Thanks for your review @corona10. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
can_modify_dict() is stricter than ASSERT_DICT_LOCKED() for frozendict. It uses PyUnstable_Object_IsUniquelyReferenced() which matters for free-threaded builds.
Replace anydict_setitem_take2() with setitem_take2_lock_held(). It's no longer useful to have two functions.