Skip to content

WIP: Add __frozendict__ to type and modules - #15

Draft
vstinner wants to merge 3 commits into
mainfrom
frozen_type_mod
Draft

WIP: Add __frozendict__ to type and modules#15
vstinner wants to merge 3 commits into
mainfrom
frozen_type_mod

Conversation

@vstinner

@vstinner vstinner commented Jul 5, 2026

Copy link
Copy Markdown
Owner

Changes:

  • Add _imp._module_set_frozendict() function.
  • importlib calls _module_set_frozendict() if a module namespace contains frozendict (and is true).

Modify functions to handle/support frozendict:

  • _PyDict_LookupIndexAndValue()
  • _PyModule_Clear()
  • module_dir()

Use __frozendict__ on stdlib modules:

  • _abc
  • _ast
  • _operator
  • _stat
  • _struct
  • _thread
  • _types
  • _warnings
  • _weakref
  • _weakrefset
  • errno
  • fcntl
  • marshal
  • math
  • posix
  • select
  • signal
  • stat
  • textwrap
  • token
  • weakref

Stdlib modules which cannot use __frozendict__:

  • linecache: test_linecache overrides a module attribute.
  • keyword: test_keyword modifies module attributes.
  • os: test_os overrides os atttributes.
  • pwd: test_getpass modifies pwd atttributes.
  • struct: test_struct modifies a struct attribute.
  • time: time.tzset() modifies attributes such as time.timezone and time.daylight.
  • site: test_site modify site attributes.
  • sys: sys.ps1 can be modified by design.
  • termios: test_pyrepl overrides termios attributes.
  • traceback: test_traceback overrides multiple traceback attributes.
  • _thread: test_idle overrides _thread.interrupt_main().
  • warnings: warnings.showwarning() can be overriden by design.

Using __frozendict__ on Mapping and MutableMapping of collections.abc doesn't work because ABCMeta.__new__() calls _abc_init(cls) which sets attributes.

vstinner added 2 commits July 5, 2026 22:26
Changes:

* Add _imp._module_set_frozendict() function.
* importlib calls _module_set_frozendict() if a module namespace
  contains __frozendict__ (and is true).

Modify functions to handle/support frozendict:

* _PyDict_LookupIndexAndValue()
* _PyModule_Clear()
* module_dir()

Use __frozendict__ on stdlib modules:

* _abc
* _ast
* _operator
* _stat
* _struct
* _thread
* _types
* _warnings
* _weakref
* _weakrefset
* errno
* fcntl
* marshal
* math
* posix
* select
* signal
* stat
* textwrap
* token
* weakref

Stdlib modules which cannot use __frozendict__:

* linecache: test_linecache overrides a module attribute.
* keyword: test_keyword modifies module attributes.
* os: test_os overrides os atttributes.
* pwd: test_getpass modifies pwd atttributes.
* struct: test_struct modifies a struct attribute.
* time: time.tzset() modifies attributes such as time.timezone and
  time.daylight.
* site: test_site modify site attributes.
* sys: sys.ps1 can be modified by design.
* termios: test_pyrepl overrides termios attributes.
* traceback: test_traceback overrides multiple traceback attributes.
* _thread: test_idle overrides _thread.interrupt_main().
* warnings: warnings.showwarning() can be overriden by design.

Using __frozendict__ on Mapping and MutableMapping of collections.abc
doesn't work because ABCMeta.__new__() calls _abc_init(cls) which
sets attributes.
fcntl cannot use frozendict because test_pathlib overrides fcntl
attributes.

weakref cannot use frozendict, test_functools overrides weakref
attribute.
@vstinner

vstinner commented Jul 5, 2026

Copy link
Copy Markdown
Owner Author

TODO: 3 tests of test_typing are still failing.

@vstinner

vstinner commented Jul 5, 2026

Copy link
Copy Markdown
Owner Author

Reading __annotate__ or __annotations__ attribute of a type may have to modify the type dictionary. For now, I added a hack to not modify the dict if it's a frozendict. But maybe the type dict should be replaced with a new updated frozendict.

@vstinner
vstinner force-pushed the main branch 2 times, most recently from e24eb61 to fbce45b Compare July 10, 2026 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant