Skip to content

block: support importing under sub-interpreters - #341

Open
jinh-labs wants to merge 1 commit into
python-lz4:masterfrom
jinh-labs:subinterp-compat-block
Open

block: support importing under sub-interpreters#341
jinh-labs wants to merge 1 commit into
python-lz4:masterfrom
jinh-labs:subinterp-compat-block

Conversation

@jinh-labs

@jinh-labs jinh-labs commented Jul 2, 2026

Copy link
Copy Markdown

What

Python 3.12+ won't import single-phase C extensions inside a sub-interpreter.
This converts _block to multi-phase init so it imports there, and declares it
safe to run with a per-interpreter GIL.

Part of #345 - multi-phase init for the _block module (see also #340 _frame, #342 _stream, #343 _version).

Why it's safe

LZ4BlockError moves from a module global into per-module state, so each
interpreter gets its own copy. There is no other shared mutable state, and the
bundled liblz4 is reentrant. So the module is safe even in fully isolated
interpreters.

Compatibility

No API change. The new declaration is guarded for Python 3.12+, so builds on
older Python are unaffected. (Uses PyModule_AddObject rather than
PyModule_AddObjectRef to keep the 3.9 minimum.)

Testing

  • tests/block passes (7,217 tests).
  • Verified _block imports and round-trips in isolated, own-GIL
    sub-interpreters, and that LZ4BlockError is a distinct object per
    interpreter.

Refs: PEP 489, Isolating Extension Modules

Python 3.12+ won't load single-phase C extensions inside a sub-interpreter.
Convert _block to multi-phase init so it imports there, and declare it safe
to run with a per-interpreter GIL.

LZ4BlockError moves from a global into per-module state, so every interpreter
gets its own. No API change, and older Python versions are unaffected.
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