GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||
GeezerVison is showing github.com — gh-141510: Add frozendict_check_mutable() by vstinner · Pull Request #144944 · python/cpython Conversation
|
|
@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? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
This should be a |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
I concur with Mark's opinion here. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| static int | ||
| frozendict_check_mutable(PyObject *self) | ||
| { | ||
| if (Py_REFCNT(self) > 1) { |
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
IIRC, Checking Py_REFCNT at runtime is not thread safe at free-threaded build.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
This change fix the following example: fd = None class SpecialDict(frozendict): def __new__(self): global fd fd = frozendict(x=1) return fd print(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 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
frozendict_check_mutable() was a bad idea, I close the PR. I wrote a 3rd PR to fix fromkeys(): #144952 copies the frozendict. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.