stream: support importing under sub-interpreters#342
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 _stream to multi-phase init so it imports there, and declare it safe to run with a per-interpreter GIL. LZ4StreamError moves from a global into per-module state, so every interpreter gets its own. Also drop a redundant error raise in a stream helper; its caller already raises the same error. No API change, and older Python versions are unaffected.
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
Python 3.12+ won't import single-phase C extensions inside a sub-interpreter.
This converts
_streamto multi-phase init so it imports there, and declares itsafe to run with a per-interpreter GIL.
Why it's safe
LZ4StreamErrormoves from a module global into per-module state, so eachinterpreter gets its own copy. There is no other shared mutable state, and the
bundled liblz4 is reentrant.
One raise site lived in a state-less helper (
store_block_length); its onlycaller already raises the same error, so the redundant raise was removed.
Compatibility
No API change. The new declaration is guarded for Python 3.12+, so builds on
older Python are unaffected.
Note
lz4.streamis experimental — built only withPYLZ4_EXPERIMENTAL=1andexcluded from the default package. This change keeps it consistent with
blockand
framefor when it graduates.Testing
tests/streampasses (5,441 passed, 2,700 skipped)._streamimports and works in isolated, own-GIL sub-interpreters,and that
LZ4StreamErroris a distinct object per interpreter.Refs: PEP 489, Isolating Extension Modules