Skip to content

frame: support importing under sub-interpreters - #340

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

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

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 _frame 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 _frame module (see also #341 _block, #342 _stream, #343 _version).

Why it's safe

_frame keeps no shared global state: each compression/decompression context is
allocated per call and freed with the object it's wrapped in, and there are no
custom module globals or static types. 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.

Scope

This covers the frame submodule only. block, stream, and _version are
still single-phase, so import lz4 as a whole doesn't work under a
sub-interpreter yet — those are follow-ups. This PR makes the _frame extension
itself compatible, which is the foundation for the rest.

Testing

  • tests/frame/ passes (12,587 tests).
  • Verified _frame imports and round-trips inside an isolated, own-GIL
    sub-interpreter (checked with both the _xxsubinterpreters API and a C harness
    that enables the multi-interpreter check).

Refs: PEP 489, Isolating Extension Modules

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

_frame keeps no shared global state — each compression/decompression context
is allocated per call — so this is safe. 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