Make numexpr work under Pyodide/WebAssembly - #568
Open
ariostas wants to merge 3 commits into
Open
Conversation
`init_threads` called `exit(-1)` if `pthread_create` failed, which kills the host process rather than the library. On platforms without working threads (WebAssembly under Emscripten/Pyodide) every thread creation fails with EAGAIN, so `import numexpr` with `OMP_NUM_THREADS` or `NUMEXPR_MAX_THREADS` set, or any `set_num_threads(n)` with n > 1, tore down the whole interpreter. Fall back to serial evaluation instead: - `init_threads` returns -1 and leaves `gs.nthreads == 1` when the pool cannot be brought up, unwinding any threads that did start. - The pool shutdown sequence in `numexpr_set_nthreads` is factored out into `join_threads` so the partial-failure path can reuse it. - The signal-mask calls and `pthread_join` now warn instead of exiting, so no `exit()` calls remain in the extension. - `numexpr.nthreads` is read back from the VM after initialization so it reports the pool that actually exists rather than the one requested. Also fix `print_versions` on platforms with no dedicated `CPUInfo` subclass (Emscripten, FreeBSD, AIX): `cpu.info` resolved through `CPUInfoBase.__getattr__` to `lambda: None`, so `cpu.info[0]` raised `TypeError`, which the surrounding `except KeyError` did not catch. Assisted-by: claude-code:claude-opus-5[1m]
…neither
Eight tests need to spawn an interpreter or start OS threads to test what they
test, so they fail rather than skip on WebAssembly runtimes (Pyodide/Emscripten,
WASI), which provide neither:
- `test_locals_clears_globals` needs a top-level frame, so it has to run in a
separate interpreter.
- `test_threading_config.test_max_threads_{set,unset}` test import-time
configuration, which needs a fresh process to re-run the extension's init.
- `test_threading.*` needs real threads.
- `test_subprocess.test_multiprocess` needs the `_multiprocessing` extension.
Gate them on capability constants. `_thread` and `os.fork` are both present but
non-functional under Emscripten, so threads are detected by actually starting
one and processes by platform, matching what CPython's own test suite does.
The skips use `unittest.skipUnless` rather than pytest markers so they work
under `numexpr.test()` too, which runs the suite through `unittest`.
Assisted-by: claude-code:claude-opus-5[1m]
Add a Pyodide entry to the wheel matrix so wasm32 wheels are built alongside the others. cibuildwheel provides the Emscripten SDK and the Pyodide runtime and runs `test-command` inside it, so these wheels are tested under WebAssembly rather than only built. No changes to the cibuildwheel configuration in `pyproject.toml` were needed; `CIBW_ARCHS_LINUX` is ignored for this platform. This requires cibuildwheel 4.x, which does not support Pyodide in the 3.1.3 previously used, so bump the action for every platform. Two consequences: - `CIBW_ENABLE: cpython-freethreading` is removed. That enable group no longer exists in 4.x because free-threaded builds are on by default, and passing it is a hard error. The free-threaded targets are unaffected: cp314t is still built and cp313t still skipped via `pyproject.toml`. - CPython 3.15 is now in cibuildwheel's default target set, so cp315 and cp315t wheels are produced for every platform. Assisted-by: claude-code:claude-opus-5[1m]
ariostas
marked this pull request as ready for review
August 27, 2026 19:25
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.
I want to use numexpr for one of the projects that I work with, and I need it to still be able to run on Pyodide. However, there are no Pyodide wheels for numexpr. I looked into it a bit to see how much it would take to get this to work, and it turned out that it was fairly straightforward. Only some minor tweaks were needed to make sure that threading was not a problem in Pyodide, and a few tests needed to be skipped. I also added a CI job so that it starts building and testing the Pyodide wheels. These wheels can now also be uploaded to PyPI (see PEP 783).
I used assistance from Claude Code for this PR. I attached the more detailed description from Claude below.
🤖 AI text below 🤖
NumExpr does not currently work under Pyodide/WebAssembly. This makes it work, and adds CI coverage so it keeps working.
exit(-1)when the thread pool cannot be createdinit_threadscalledexit(-1)ifpthread_createfailed, which terminates the host process rather than the library. Emscripten fails every thread creation withEAGAIN, soimport numexprwithOMP_NUM_THREADSorNUMEXPR_MAX_THREADSset, or anyset_num_threads(n)withn > 1, tore down the whole interpreter.It now falls back to serial evaluation instead:
init_threadsreturns -1 and leavesgs.nthreads == 1when the pool cannot be brought up, unwinding any threads that did start.numexpr_set_nthreadsis factored out intojoin_threadsso the partial-failure path can reuse it.pthread_joinnow warn instead of exiting, so noexit()calls remain in the extension.numexpr.nthreadsis read back from the VM after initialization so it reports the pool that actually exists rather than the one that was requested.This part is not WebAssembly-specific: a library should not take down a CPython process because it could not get a thread.
print_versions()on platforms with no dedicatedCPUInfosubclasscpu.inforesolved throughCPUInfoBase.__getattr__tolambda: None, socpu.info[0]raisedTypeError, which the surroundingexcept KeyErrordid not catch. This affects Emscripten, but also FreeBSD and AIX.CPUInfoBasenow carries aninfo = []default and the guard catchesTypeError/IndexErroras well.Skipping tests that need processes or threads
Eight tests spawn an interpreter or start OS threads to test what they test, so they fail rather than skip on runtimes that have neither:
test_locals_clears_globalsneeds a top-level frame, so it has to run in a separate interpreter.test_threading_config.test_max_threads_{set,unset}test import-time configuration, which needs a fresh process to re-run the extension's init.test_threading.*needs real threads.test_subprocess.test_multiprocessneeds the_multiprocessingextension.They are now gated on capability constants. Note that
_threadandos.forkare both present but non-functional under Emscripten, hence the platform check for processes and an actual probe for threads, which is what CPython's own test suite does. The skips useunittest.skipUnlessrather than pytest markers so they also work undernumexpr.test(), which runs the suite throughunittest. Nothing is skipped on the existing platforms.CI
Adds a Pyodide entry to the wheel matrix. cibuildwheel provides the Emscripten SDK and the Pyodide runtime and runs
test-commandinside it, so these wheels are exercised under WebAssembly rather than only built. No changes to the cibuildwheel configuration inpyproject.tomlwere needed.This requires cibuildwheel 4.x, which does not support Pyodide in the 3.1.3 previously used, so the action is bumped for every platform. Two consequences worth flagging for review:
CIBW_ENABLE: cpython-freethreadingis removed. That enable group no longer exists in 4.x, because free-threaded builds are on by default, and passing it is a hard error. The free-threaded targets are unaffected: cp314t is still built and cp313t still skipped viapyproject.toml.cpython-prereleaseand cannot be disabled throughCIBW_ENABLE; it would need askipentry inpyproject.tomlif that is not wanted.