Skip to content

Add InterpreterLocal type as escape hatch from freeze system#82

Open
mjp41 wants to merge 1 commit intoimmutable-mainfrom
immutable-rollback
Open

Add InterpreterLocal type as escape hatch from freeze system#82
mjp41 wants to merge 1 commit intoimmutable-mainfrom
immutable-rollback

Conversation

@mjp41
Copy link
Collaborator

@mjp41 mjp41 commented Mar 12, 2026

InterpreterLocal provides per-sub-interpreter mutable state behind an
immutable indirection. When an object graph is frozen, InterpreterLocal
appears immutable to the freezer via tp_reachable (which visits only the
type and frozen default/factory fields), while each sub-interpreter
independently reads and writes its own value through get()/set() methods.

Two construction forms:
InterpreterLocal(42) - immutable default (frozen at construction)
InterpreterLocal(lambda: []) - factory callable (frozen at construction)

The type is a heap type created via PyType_FromModuleAndSpec, with
per-interpreter storage in the _immutable module's per-interpreter state.
This avoids changes to core interpreter structures.

InterpreterLocal provides per-sub-interpreter mutable state behind an
immutable indirection. When an object graph is frozen, InterpreterLocal
appears immutable to the freezer via tp_reachable (which visits only the
type and frozen default/factory fields), while each sub-interpreter
independently reads and writes its own value through get()/set() methods.

Two construction forms:
  InterpreterLocal(42)          - immutable default (frozen at construction)
  InterpreterLocal(lambda: [])  - factory callable (frozen at construction)

The type is a heap type created via PyType_FromModuleAndSpec, with
per-interpreter storage in the _immutable module's per-interpreter state.
This avoids changes to core interpreter structures.
@mjp41 mjp41 force-pushed the immutable-rollback branch from 800aed5 to 0b32955 Compare March 12, 2026 12:18
Copy link
Collaborator

@xFrednet xFrednet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Shall we merge it?

@xFrednet
Copy link
Collaborator

The CI error looks suspicious: Could you take a look at it before merging?

8
Re-running test_freeze in verbose mode (matching: test_object_without_dict_uses_ob_flags)
Warning -- Unraisable exception
Exception ignored while finalizing file <_io.FileIO name='/home/runner/work/cpython/cpython-ro-srcdir/Lib/test/test_freeze/test_io.py' mode='rb' closefd=True>:
Traceback (most recent call last):
  File "/home/runner/work/cpython/cpython-ro-srcdir/Lib/test/libregrtest/single.py", line 49, in _filter_suite
    _filter_suite(test, pred)
    ~~~~~~~~~~~~~^^^^^^^^^^^^
ResourceWarning: unclosed file <_io.BufferedReader name='/home/runner/work/cpython/cpython-ro-srcdir/Lib/test/test_freeze/test_io.py'>
Warning -- Unraisable exception
Exception ignored while finalizing file <_io.FileIO name='/home/runner/work/cpython/cpython-ro-srcdir/Lib/test/test_freeze/test_io.py' mode='rb' closefd=True>:
Traceback (most recent call last):
  File "/home/runner/work/cpython/cpython-ro-srcdir/Lib/test/libregrtest/single.py", line 49, in _filter_suite
    _filter_suite(test, pred)
    ~~~~~~~~~~~~~^^^^^^^^^^^^
ResourceWarning: unclosed file <_io.TextIOWrapper name='/home/runner/work/cpython/cpython-ro-srcdir/Lib/test/test_freeze/test_io.py' mode='r' encoding='utf-8'>
test_object_without_dict_uses_ob_flags (test.test_freeze.test_set_freezable.TestSetFreezableEdgeCases.test_object_without_dict_uses_ob_flags) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.001s

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.

2 participants