Uh oh!
There was an error while loading. Please reload this page.
gh-141510: Add frozendict_check_mutable() - #144944
Conversation
frozendict.fromkeys() now checks if it can mutate the newly created frozendict.
vstinner
commented
Feb 18, 2026
@methane@corona10: I wrote this change to raise an exception if a frozendict subclass constructor keeps a reference to the newly created dictionary. I don't know which error message should be used, so I wrote: RuntimeError("cannot mutate frozendict already exposed in Python"). Do you have a better error message to propose? |
markshannon
commented
Feb 18, 2026
Frozen dicts are immutable, that's the whole point of them, so why are we allowing them to be mutated? Mutation of tuples has been an endless source of bugs, please don't do the same with frozen dicts. |
markshannon
commented
Feb 18, 2026
This should be a |
corona10
commented
Feb 18, 2026
I concur with Mark's opinion here. |
| static int | ||
| frozendict_check_mutable(PyObject *self) | ||
| { | ||
| if (Py_REFCNT(self) > 1) { |
There was a problem hiding this comment.
IIRC, Checking Py_REFCNT at runtime is not thread safe at free-threaded build.
vstinner
commented
Feb 18, 2026
This change fix the following example: fd=NoneclassSpecialDict(frozendict):
def__new__(self):
globalfdfd=frozendict(x=1)
returnfdprint(SpecialDict.fromkeys("y"))Currently, This change detects the special constructor and raises an error.
An alternative is to modify A more radical fix would be to disallow subclassing |
vstinner
commented
Feb 18, 2026
frozendict_check_mutable() was a bad idea, I close the PR. I wrote a 3rd PR to fix fromkeys(): #144952 copies the frozendict. |
frozendict.fromkeys() now checks if it can mutate the newly created frozendict.