version: support importing under sub-interpreters - #343
Open
jinh-labs wants to merge 1 commit into
Open
Conversation
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.
jinh-labs
marked this pull request as ready for review
July 7, 2026 11:31
This was referenced Jul 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Make
import lz4work inside a sub-interpreter. Python 3.12+ won't import single-phase C extensions in a sub-interpreter, andlz4/__init__.pyimports_version— so todayimport lz4fails there withImportError: module _version does not support loading in subinterpreters. This converts_versionto multi-phase init and declares it safe under a per-interpreter GIL.Part of #345 - multi-phase init for the
_versionmodule (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 anyimport 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
_versionhas 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
import lz4succeeds in an isolated, own-GIL sub-interpreter.Refs: PEP 489, Isolating Extension Modules