Repository navigation
[6x] python: split the stdlib extension modules into side-module packages - #8716
DerThorsten wants to merge 1 commit into
Conversation
| 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" |
There was a problem hiding this comment.
explain that dir
There was a problem hiding this comment.
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" ;; |
There was a problem hiding this comment.
will this drag in any actual libthread stuf?
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
rules in the sense of compiling rules?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
why do we skip the browser here?
There was a problem hiding this comment.
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).
| 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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
|
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 Fix: |
|
Follow-up: regex / peewee are out of this PR again (reverted). With |
…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>
5fd2fac to
8112006
Compare
python: stdlib extension modules as side-module packages
libpython is split at the builtin-module line, so that embedders linking
libpythonX.Y.aas emscriptenMAIN_MODULE(pyjs, xeus-python, ...) only link what the core needs. AMAIN_MODULEkeeps every symbol of every linked archive, so openssl / sqlite / lzma used to end up in every runtime.libpython-staticlibpythonX.Y.a+ headers: the slim corepython-corepythonbefore 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)pythonpython-core+ all of the above exceptpython-test-modulespython-devpython+libpython-static+ pkg-configThe extension modules are side modules in
lib/python3.14/lib-dynload, which python dlopens on import. Envs withpythonbehave as before. A slim env haspython-coreplus the extension packages it needs.libpython-static: openssl / lzma / uuid / sqlite are no longer run dependencies (their modules are side modules now,_uuidis disabled); embedders that still list them on their link line need a rebuild.python-coreandpythonhaverun_constraints: pyjs >=6: pyjs < 6 (and xeus-python built on it) only unpacks the package namedpythonbefore starting the interpreter, so those envs keep getting the unsplit python (build 8).lib-dynload, in node and in a browser. Itsplaywright/playwright-pythonare pinned to the same version (1.62.*, as in pytester): a driver / API mismatch failed the browser launch.Doc/license.rst, expat, HACL*).sqlite: amalgamation,
-fvisibility=hiddenWithout hidden visibility, a side module linking sqlite (python's
_sqlite3, apsw, ...) exports the wholesqlite3_*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 insqlite3_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. Callingpthread_*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)
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 channelpython-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