Skip to content

stream: support importing under sub-interpreters#342

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

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

Conversation

@jinh-labs

Copy link
Copy Markdown

What

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

Why it's safe

LZ4StreamError 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.

One raise site lived in a state-less helper (store_block_length); its only
caller 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.stream is experimental — built only with PYLZ4_EXPERIMENTAL=1 and
excluded from the default package. This change keeps it consistent with block
and frame for when it graduates.

Testing

  • tests/stream passes (5,441 passed, 2,700 skipped).
  • Verified _stream imports and works in isolated, own-GIL sub-interpreters,
    and that LZ4StreamError 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 _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.
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