Skip to content

version: support importing under sub-interpreters - #343

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

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

Conversation

@jinh-labs

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

Copy link
Copy Markdown

What

Make import lz4 work inside a sub-interpreter. Python 3.12+ won't import single-phase C extensions in a sub-interpreter, and lz4/__init__.py imports _version — so today import lz4 fails there with ImportError: module _version does not support loading in subinterpreters. This converts _version to multi-phase init and declares it safe under a per-interpreter GIL.

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

Relationship to the other PRs

Part of a series making the whole package sub-interpreter-compatible: #340 (frame), #341 (block), #342 (stream), and this one (_version).

These are independent and can merge in any order. They touch different files and don't conflict. But this PR is the gate: because import lz4 (and therefore any import lz4.frame / lz4.block / lz4.stream, which import the parent package first) goes through _version, the normal import paths stay blocked in a sub-interpreter until this one lands. With all four in, the whole package loads and round-trips in an isolated, own-GIL sub-interpreter.

Why it's safe

_version has no module state and just wraps the reentrant LZ4 version calls, so it is safe even in fully isolated interpreters. This is a mechanical conversion.

Compatibility

No API change. The new declaration is guarded for Python 3.12+, so builds on older Python are unaffected.

Testing

  • Version tests pass.
  • Verified import lz4 succeeds in an isolated, own-GIL sub-interpreter.

Refs: PEP 489, Isolating Extension Modules

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

_version has no state, so this is a mechanical conversion. Since lz4/__init__
imports it, this is also what lets `import lz4` work in a sub-interpreter.
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