Skip to content

[6x] python: split the stdlib extension modules into side-module packages - #8716

Open
DerThorsten wants to merge 1 commit into
emscripten-forge:emscripten-6xfrom
DerThorsten:python-split
Open

DerThorsten wants to merge 1 commit into
emscripten-forge:emscripten-6xfrom
DerThorsten:python-split

Conversation

@DerThorsten

@DerThorsten DerThorsten commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

python: stdlib extension modules as side-module packages

libpython is split at the builtin-module line, so that embedders linking libpythonX.Y.a as emscripten MAIN_MODULE (pyjs, xeus-python, ...) only link what the core needs. A MAIN_MODULE keeps every symbol of every linked archive, so openssl / sqlite / lzma used to end up in every runtime.

package content
libpython-static libpythonX.Y.a + headers: the slim core
python-core the stdlib (as python before the split)
python-ssl _ssl (+ libssl / libcrypto, linked statically)
python-sqlite3 _sqlite3 (+ libsqlite3)
python-lzma _lzma (+ liblzma)
python-cjkcodecs _multibytecodec, _codecs_*
python-test-modules _testcapi, _testinternalcapi, ... (CPython's own tests)
python meta package: python-core + all of the above except python-test-modules
python-dev meta package: python + libpython-static + pkg-config

The extension modules are side modules in lib/python3.14/lib-dynload, which python dlopens on import. Envs with python behave as before. A slim env has python-core plus the extension packages it needs.

  • libpython-static: openssl / lzma / uuid / sqlite are no longer run dependencies (their modules are side modules now, _uuid is disabled); embedders that still list them on their link line need a rebuild.
  • python-core and python have run_constraints: pyjs >=6: pyjs < 6 (and xeus-python built on it) only unpacks the package named python before starting the interpreter, so those envs keep getting the unsplit python (build 8).
  • New test: the core alone, linked without openssl / sqlite / lzma, runs the python tests in node. The full test loads every extension module from lib-dynload, in node and in a browser. Its playwright / playwright-python are pinned to the same version (1.62.*, as in pytester): a driver / API mismatch failed the browser launch.
  • The license files now also cover the bundled libraries (Doc/license.rst, expat, HACL*).

sqlite: amalgamation, -fvisibility=hidden

Without hidden visibility, a side module linking sqlite (python's _sqlite3, apsw, ...) exports the whole sqlite3_* API. The dynamic linker then binds it to another loaded copy of sqlite, whose state the module never initialized, and the result is a "null function" trap in sqlite3_close. It is now built from the amalgamation (no tcl / configure), with the same options as before.

generic-testing 0.4.0

python needs this version: its tests no longer pass --allow-pthread. The thread check now fails only on code compiled with -pthread (atomics / shared memory). That is what actually forces users to link with -pthread. Calling pthread_* functions is fine without it: emscripten's libc has them, and CPython calls them. The check also accepts LLVM bitcode (-flto) static libraries and checks their target architecture.

regex

Rebuilt against the split python, to test it with pytester (pyjs 6.0.1 + pytester 1.7.0 from the channel) in CI.

Tested locally (rattler-build)

  • python, wasm32: all outputs incl. generic-testing, 22/22 python tests in node (core-only env and full env), browser run in chromium
  • existing recipes keep working without a rebuild: the published regex (py314hff31f85_1, built against the unsplit python) passes its pytester test (9/9: worker / main thread, with and without preloaded shared libs) in an env with the split python, pyjs-dev 6.0.1 and pytester 1.7.0 from the channel
  • earlier: pyjs 6's full browser test suite with these packages, wasm32 in chromium / firefox / webkit and wasm64 in chromium, main thread + worker, including optional python-ssl / -sqlite3 / -lzma / -cjkcodecs / -test-modules

(This replaces the earlier version of the branch: its temporary python pin in pytester-run is no longer needed since pyjs 6.0.1 and pytester 1.7.0 are on the channel.)

🤖 Generated with Claude Code

SIDE_MODULES_CJKCODECS="_multibytecodec _codecs_cn _codecs_hk _codecs_iso2022 _codecs_jp _codecs_kr _codecs_tw"
SIDE_MODULES_TEST="xxsubtype _xxtestfuzz _testbuffer _testinternalcapi _testcapi _testlimitedcapi _testclinic _testclinic_limited"
SIDE_MODULES="${SIDE_MODULES_SSL} ${SIDE_MODULES_SQLITE3} ${SIDE_MODULES_LZMA} ${SIDE_MODULES_CJKCODECS} ${SIDE_MODULES_TEST}"
DYNLOAD_DIR="${PREFIX}/lib/python${PYMAJOR}.${PYMINOR}/lib-dynload"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explain that dir

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's CPython's standard directory for stdlib extension modules built as shared libraries (on Linux, _ssl.cpython-314-x86_64-linux-gnu.so lives there too). It is on sys.path by default, so import _ssl finds the side module there without any configuration. Explained in a comment now (5f4ddf3).

case "$1" in
# _ssl.c refuses an openssl without thread support (libopenssl-static is
# built without threads, there are none in the browser)
_ssl) echo "-DOPENSSL_THREADS" ;;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this drag in any actual libthread stuf?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. In _ssl.c, OPENSSL_THREADS is only used in #ifndef OPENSSL_THREADS / #error "Python requires thread-safe OpenSSL". The define silences that check, and that's all: no thread code is compiled, no thread library is linked. The locks _ssl.c takes are CPython's own (PyMutex, PyThread_allocate_lock) and openssl's (X509_STORE_lock), and all of them are no-ops in these non-threaded builds. generic-testing 0.4.0 also checks the side module for code compiled with -pthread (atomics / shared memory). Comment extended (5f4ddf3).

# Append a *shared* section with the rules of the side modules to Setup.local.
# The rules (sources, MODULE_<NAME>_CFLAGS / _LDFLAGS from configure) are
# taken from upstream's Modules/Setup.stdlib.in, so they follow CPython.
append_side_module_rules() {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rules in the sense of compiling rules?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. A rule is a line of CPython's Setup file format, <module> <sources> [flags] (e.g. _lzma _lzmamodule.c). CPython's makesetup turns each line into Makefile targets that compile and link that module, and modules below *shared* become shared libraries (here: side modules) instead of builtins of libpython. Explained in the comment now, with an example (5f4ddf3).

# the slim core alone: link it without openssl / sqlite / lzma and run
# the python tests in a core-only env (node)
- script:
- PY_VER=${{ ver2 }} PYTHON_SKIP_BROWSER_TEST=1 bash build_tests.sh core

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we skip the browser here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The core-only env has no side module to load. The browser-specific part, loading side modules from lib-dynload (compiled asynchronously while the page loads), is covered by the browser run of the full test (python-dev). A browser run here would test the same core code again, at the cost of installing playwright + a browser and a second link. If we want it anyway, it's one line: drop PYTHON_SKIP_BROWSER_TEST=1 and add playwright / playwright-python / the browser_test files to that test. Reason written in the recipe now (5f4ddf3).

Comment thread recipes/recipes_wasm/python/recipe.yaml Outdated
run:
- python_abi ${{ ver2 }}.* *_${{ abi_tag }}
# The core needs libffi, zstd, libbzip2 and libzlib. openssl, lzma,
# uuid and sqlite are NOT needed to link libpython anymore; they stay

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

embedders will be rebuild / changed anyhow..no need to include non running things.

do run deps for staging even make sense?
I would expect these pin_compatible things only to appear in python-core (we may have to dublicated the host-env to get the pin_compatible things working in run of python-core

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed on the first part: openssl / lzma / uuid / sqlite are removed (_uuid is disabled in Setup.local anyway). Embedders that still link them get rebuilt.

On the second part: this block isn't the staging output. It's the libpython-static package. It inherits the build from staging and repeats the host env, which is exactly what makes pin_compatible work here. I think the pins belong in this package rather than python-core. The run deps are the archives that libpythonX.Y.a itself needs at link time (libffi, zstd, bzip2, zlib): whoever links libpython needs them too, and that's always someone who installs libpython-static. python-core contains nothing to link (stdlib .py files), and the side-module packages contain their libraries statically. So pins in python-core would pull static libs into every runtime env for no reason. Comment rewritten to say this.

@DerThorsten

Copy link
Copy Markdown
Contributor Author

The first CI run showed that published embedders break with the split: the peewee / regex tests ran pyjs 4.4.1 (from 6x) with this PR's python build 9, and pyjs failed with error while bootstrapping python. pyjs < 6 (bootstrap_from_empack_packed_environment) unpacks only the package named python before starting the interpreter. That package is now an empty meta package and the stdlib is in python-core, so Python starts without its stdlib. xeus-python uses the same bootstrap.

Fix: run_constraints: pyjs >=6 on python-core and on the python meta package. Envs with an older pyjs (and xeus-python) now get the unsplit python build 8; the split is used once pyjs 6 is on the channel. Consequence for this PR: the regex / peewee tests run with python build 8 again, so they no longer exercise the split.

@DerThorsten

Copy link
Copy Markdown
Contributor Author

Follow-up: regex / peewee are out of this PR again (reverted). With pyjs >=6 on the new python, their pytester tests cannot be solved in this PR's CI. Strict channel priority makes the PR's output channel (python build 9 only) hide the python builds of 6x, so the fallback to build 8 doesn't exist here. After the merge both builds are in 6x, and envs with an older pyjs (pytester-run, xeus-python) get build 8. The first CI run with them already showed what they were meant to show: pyjs < 6 cannot bootstrap the split python.

…ages

The interpreter is split at the builtin-module line (see the outputs comment
in recipes/recipes_wasm/python/recipe.yaml): libpython-static is the slim
core, openssl / sqlite / lzma / the CJK codecs / CPython's test modules are
emscripten side modules in their own packages (python-ssl, python-sqlite3,
python-lzma, python-cjkcodecs, python-test-modules); python-core is the
stdlib, python a meta package with everything as before (but the test
modules), python-dev adds libpython-static.

- run_constraints pyjs >= 6 on python-core and python: pyjs < 6 (and
  xeus-python built on it) only unpacks the package named python and so
  gets the unsplit python (build 8)
- libpython-static: openssl / lzma / uuid / sqlite no longer run deps
- generic-testing 0.4.0: archives with LLVM bitcode members, ...
- sqlite: built for the side module
- python tests: core-only env (node) and full env (node + browser);
  playwright + playwright-python pinned to the same version (1.62.*): a
  driver / API mismatch failed the browser launch
- regex: rebuilt against the split python, tested with pytester

(Replaces the earlier branch: its temporary python pin in pytester-run is
not needed since pyjs 6.0.1 + pytester 1.7.0 are on the channel.)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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