feat: Pyodide improvements: version setting, standalone environments (#2002)

* Fix a typo: pyoodide ➡️ pyodide

* Add `pyodide_build_version` attribute

* Add version to xbuildenv log step

* Add version to Emscripten log step

* Use `pyodide-build`'s version for updating constraints

* Bump Pyodide constraints by updating `pyodide-build`

* Add a schema for `pyodide-version`

* Update Pyodide constraints

* Bump `pyodide-build` to new 0.29.0

* Test out another Pyodide identifier

* Update outdated Pyodide constraints

* Add Pyodide version to temp directory name

* Remove Pyodide 0.26.1 from build configurations

* Retrieve + validate + install specific xbuildenvs

* Test wheel builds with Pyodide 0.26.2

* Add correct Pyodide version to identifier temp dir

* Don't pre-call Pyodide xbuildenv search

* Fetch just the stable Pyodide versions

* Refactor search + validation + install into one step

* Move all of it under a lock

* Reorder xbuildenv installation

* Add env and cwd to xbuildenv search call

* Temporarily lower to 0.26.2 target

* Separate out search, validate, install; again

* Run xbuildenv search in `CIBW_CACHE_PATH`

* Remove prior `PYODIDE_ROOT` env vars, copy envs

* Validate doesn't need to depend on searching

* Add file lock when searching xbuildenvs

* Test the original version: 0.26.1

* Update Pyodide constraints

* Update constraints for `pyodide-build` 0.29.0 again

* Bump Pyodide from version 0.26.1 ➡️ version 0.26.4

* Add note on compatibility for macOS + other archs

* Note Pyodide version for Pyodide identifier

* Docs about `CIBW_PYODIDE_VERSION`

* Don't fetch just the stable versions

* Discard a variable that's not used later

* Rename `search_xbuildenv` ➡️ `get_xbuildenv_versions`

* `validate_xbuildenv` ➡️ `validate_xbuildenv_version`

* Replace ordered comment, add newline

* Replace sentence on macOS support

Co-Authored-By: Hood Chatham <roberthoodchatham@gmail.com>

* Capitalise: "pyodide" ➡️ "Pyodide"

Co-Authored-By: Hood Chatham <roberthoodchatham@gmail.com>

* "work" ➡️ "may succeed"

Co-Authored-By: Hood Chatham <roberthoodchatham@gmail.com>

* Add another job to test a custom Pyodide version

* Handle "v"-prefixed + non-prefixed versions

* Convert to a proper toml-able option, and remove some hardcoded versions

This removes the enscripten and pyodide-build version specs from
pyproject.toml - pyodide-build is spec'd in the constraints file, and
the emscripten version can be read from the pyodide-build output.

* Add a schema entry

* Add docs for CIBW_PYODIDE_VERSION

* Rephrase

* Add tests for pyodide-version

* Apply suggestions from code review

* Add python_build_standalone util

* Hook up to python-build-standalone, removing dependency on host python

* Remove python hard-code in action.yml

* Add log step

* Add workaround for https://github.com/pyodide/pyodide-build/issues/143

* Add emscripten pytest test

* Remove unneeded checks

* Fix a pytest invoke for emscripten

* Generate pyodide-build constraints from the pinned pyodide version

* Remove pyodide python-build-standalone workaround

* Fixup paths from newer version of pyodide-build

* Fix/skip some failing tests

* Use `python -m pytest` on pyodide, even on Linux

* Docs fixes

* Don't call the github API at runtime, cache the release assets instead

* Ignore pylint false positive

* Add version auto-updating for pyodide

* Add support for pyodide 3.13.

* Fix tests for multiple pyodide wheels

* Remove workaround for unreleased pyodide-build

* Rename to "test_pyodide"

* Fix pathname confusion

* Remove extra github actions job

* Fix expectation for test_abi_none

* Fix the custom_repair_wheel test to actually have clashing names

* Fix pinned version test

* Document test-command limitation

* Remove pyodide 0.28.0a1 for now

* Update constraints files

* Docs/test fixes post removing pyodide cp313

* Fix ABI test expectation

* Docs improvements

* Improve some comments

* Remove logic duplication

* remove pyodide special casing

* Refactor constraints code to use a utility script, circumventing import issues

* chore: nicer nox env

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* fix: typo in variable name found by copilot

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* Apply suggestions from code review

* Some more Pyodide version updates in the docs section

* We haven't released Pyodide v0.27.6 yet

* Back to the Github URL for cross-build-environments

* `pyodide-build`, not `emsdk` for Windows skips

Co-Authored-By: Hood Chatham <roberthoodchatham@gmail.com>

* Move to a separate `_json_request` function

Co-Authored-By: Hood Chatham <roberthoodchatham@gmail.com>

* Rename "retries" ➡️ "retry_count"

* Add some type hints

* Copy env vars before `UV_CUSTOM_COMPILE_COMMAND`

* Use `HTTPError.headers.get` instead

* Remove extra end quote

* Change download tests URL to `https://badssl.com/`

Co-Authored-By: Joe Rickerby <1244307+joerick@users.noreply.github.com>

* Download size changes, too

* Use jsdelivr for Github asset mirroring

* Bump to Pyodide v0.27.6

* Fix unit tests

* Move to pyodide v0.27.6 again

* Bump to pyodide-build 0.30.4

* Use new URL for cross-build environments metadata

Co-authored-by: Joe Rickerby <joerick@mac.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: Hood Chatham <roberthoodchatham@gmail.com>
Co-authored-by: Joe Rickerby <joerick@mac.com>
Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: Joe Rickerby <1244307+joerick@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Agriya Khetarpal
2025-05-20 16:59:58 -04:00
committed by GitHub
co-authored by Hood Chatham Joe Rickerby Joe Rickerby Henry Schreiner pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
parent c3c260dff7
commit 163864158d
30 changed files with 1298 additions and 115 deletions
+13 -6
View File
@@ -48,8 +48,13 @@ def test_abi3(tmp_path):
# check that the expected wheels are produced
if utils.get_platform() == "pyodide":
# there's only 1 possible configuration for pyodide, cp312
expected_wheels = utils.expected_wheels("spam", "0.1.0", python_abi_tags=["cp310-abi3"])
# there's only 1 possible configuration for pyodide, cp312. It builds
# a wheel that is tagged abi3, compatible back to 3.10
expected_wheels = utils.expected_wheels(
"spam",
"0.1.0",
python_abi_tags=["cp310-abi3"],
)
else:
expected_wheels = utils.expected_wheels(
"spam",
@@ -189,15 +194,17 @@ def test_abi_none(tmp_path, capfd):
},
)
# check that the expected wheels are produced
expected_wheels = utils.expected_wheels("ctypesexample", "1.0.0", python_abi_tags=["py3-none"])
# check that the expected wheels are produced
assert set(actual_wheels) == set(expected_wheels)
# check that each wheel was built once, and reused
captured = capfd.readouterr()
assert "Building wheel..." in captured.out
if utils.get_platform() == "pyodide":
# there's only 1 possible configuration for pyodide, we won't see the message expected on following builds
# pyodide builds a different platform tag for each python version, so
# wheels are not reused
assert "Found previously built wheel" not in captured.out
else:
# check that each wheel was built once, and reused
assert "Building wheel..." in captured.out
assert "Found previously built wheel" in captured.out
+6 -6
View File
@@ -16,6 +16,11 @@ from pathlib import Path
wheel = Path(sys.argv[1])
dest_dir = Path(sys.argv[2])
platform = wheel.stem.split("-")[-1]
if platform.startswith("pyodide"):
# for the sake of this test, munge the pyodide platforms into one, it's
# not valid, but it does activate the uniqueness check
platform = "pyodide"
name = f"spam-0.1.0-py2-none-{platform}.whl"
dest = dest_dir / name
dest_dir.mkdir(parents=True, exist_ok=True)
@@ -48,11 +53,6 @@ def test(tmp_path, capfd):
assert "Build failed because a wheel named" in captured.err
assert exc_info.value.returncode == 6
else:
# We only produced one wheel (currently Pyodide)
# We only produced one wheel (perhaps Pyodide)
# check that it has the right name
#
# As far as I can tell, this is the only full test coverage for
# CIBW_REPAIR_WHEEL_COMMAND so this is useful even in the case when no
# error is raised
assert "spam-0.1.0-py2-none-pyodide" in captured.out
assert result[0].startswith("spam-0.1.0-py2-none-")
+4 -1
View File
@@ -70,7 +70,10 @@ def test_pinned_versions(tmp_path, python_version, build_frontend_env_nouv):
version_no_dot = python_version.replace(".", "")
build_environment = {}
build_pattern = f"[cp]p{version_no_dot}-*"
constraint_filename = f"constraints-python{version_no_dot}.txt"
if utils.get_platform() == "pyodide":
constraint_filename = f"constraints-pyodide{version_no_dot}.txt"
else:
constraint_filename = f"constraints-python{version_no_dot}.txt"
constraint_file = resources.PATH / constraint_filename
constraint_versions = get_versions_from_constraint_file(constraint_file)
+6 -1
View File
@@ -100,7 +100,12 @@ def test_overridden_path(tmp_path, capfd):
"build_frontend",
[
pytest.param("pip", marks=utils.skip_if_pyodide("No pip for pyodide")),
"build",
pytest.param(
"build",
marks=utils.skip_if_pyodide(
"pyodide doesn't support multiple values for PIP_CONSTRAINT"
),
),
],
)
def test_overridden_pip_constraint(tmp_path, build_frontend):
+10 -3
View File
@@ -60,12 +60,19 @@ def test_pep518(tmp_path, build_frontend_env):
assert not (project_dir / "42").exists()
assert not (project_dir / "4.1.2").exists()
# pypa/build creates a "build" folder & a "*.egg-info" folder for the wheel being built,
# this should be harmless so remove them
# pypa/build creates a "build" folder & a "*.egg-info" folder for the
# wheel being built, this should be harmless so remove them. pyodide-build
# creates a ".pyodide_build" folder, but this is gitignored with a
# .gitignore file inside.
contents = [
item
for item in project_dir.iterdir()
if item.name != "build" and not item.name.endswith(".egg-info")
if item.name != "build"
and not item.name.endswith(".egg-info")
and item.name != ".pyodide_build"
]
print("Project contents after build:")
print("\n".join(f" {f}" for f in contents))
assert len(contents) == len(basic_project.files)
@@ -1,10 +1,10 @@
import shutil
import contextlib
import subprocess
import sys
import textwrap
import pytest
from cibuildwheel.ci import CIProvider, detect_ci_provider
from cibuildwheel.util.file import CIBW_CACHE_PATH
from . import test_projects, utils
@@ -43,13 +43,7 @@ if __name__ == "__main__":
@pytest.mark.parametrize("use_pyproject_toml", [True, False])
def test_pyodide_build(tmp_path, use_pyproject_toml):
if sys.platform == "win32":
pytest.skip("emsdk doesn't work correctly on Windows")
if not shutil.which("python3.12"):
pytest.skip("Python 3.12 not installed")
if detect_ci_provider() == CIProvider.travis_ci:
pytest.skip("Python 3.12 is just a non-working pyenv shim")
pytest.skip("pyodide-build doesn't work correctly on Windows")
if use_pyproject_toml:
basic_project.files["pyproject.toml"] = textwrap.dedent(
@@ -84,3 +78,64 @@ def test_pyodide_build(tmp_path, use_pyproject_toml):
print("expected_wheels", expected_wheels)
assert set(actual_wheels) == set(expected_wheels)
def test_pyodide_version_incompatible(tmp_path, capfd):
if sys.platform == "win32":
pytest.skip("pyodide-build doesn't work correctly on Windows")
basic_project.generate(tmp_path)
with pytest.raises(subprocess.CalledProcessError):
utils.cibuildwheel_run(
tmp_path,
add_args=["--platform", "pyodide"],
add_env={
"CIBW_DEPENDENCY_VERSIONS": "packages: pyodide-build==0.29.3",
"CIBW_PYODIDE_VERSION": "0.26.0a6",
},
)
out, err = capfd.readouterr()
assert "is not compatible with the pyodide-build version" in err
@pytest.mark.parametrize("expect_failure", [True, False])
def test_pyodide_build_and_test(tmp_path, expect_failure):
if sys.platform == "win32":
pytest.skip("pyodide-build doesn't work correctly on Windows")
if expect_failure:
basic_project.files["test/spam_test.py"] = textwrap.dedent(r"""
def test_filter():
assert 0 == 1
""")
else:
basic_project.files["test/spam_test.py"] = textwrap.dedent(r"""
import spam
def test_filter():
assert spam.filter("spam") == 0
""")
basic_project.generate(tmp_path)
context = (
pytest.raises(subprocess.CalledProcessError) if expect_failure else contextlib.nullcontext()
)
with context:
# build the wheels
actual_wheels = utils.cibuildwheel_run(
tmp_path,
add_args=["--platform", "pyodide"],
add_env={
"CIBW_TEST_REQUIRES": "pytest",
"CIBW_TEST_COMMAND": "python -m pytest",
},
)
# check that the expected wheels are produced
expected_wheels = [
"spam-0.1.0-cp312-cp312-pyodide_2024_0_wasm32.whl",
]
print("actual_wheels", actual_wheels)
print("expected_wheels", expected_wheels)
assert set(actual_wheels) == set(expected_wheels)
+1 -1
View File
@@ -224,10 +224,10 @@ def test_test_sources(tmp_path):
project_dir,
add_env={
"CIBW_TEST_REQUIRES": "pytest",
"CIBW_TEST_COMMAND": "pytest",
# pytest fails on GraalPy 24.2.0 on Windows so we skip it there
# until https://github.com/oracle/graalpython/issues/490 is fixed
"CIBW_TEST_COMMAND_WINDOWS": "where graalpy || pytest",
"CIBW_TEST_COMMAND": utils.invoke_pytest(),
"CIBW_TEST_SOURCES": "test",
},
)
+10 -2
View File
@@ -382,7 +382,15 @@ def _expected_wheels(
raise Exception(msg)
elif platform == "pyodide":
platform_tags = ["pyodide_2024_0_wasm32"]
platform_tags = {
"cp312-cp312": ["pyodide_2024_0_wasm32"],
"cp313-cp313": ["pyodide_2025_0_wasm32"],
}.get(python_abi_tag, [])
if not platform_tags:
# for example if the python tag is `none` or `abi3`, all
# platform tags are built with that python tag
platform_tags = ["pyodide_2024_0_wasm32"]
else:
msg = f"Unsupported platform {platform!r}"
@@ -425,7 +433,7 @@ def skip_if_pyodide(reason: str) -> Any:
def invoke_pytest() -> str:
# see https://github.com/pyodide/pyodide/issues/4802
if get_platform() == "pyodide" and sys.platform.startswith("darwin"):
if get_platform() == "pyodide":
return "python -m pytest"
return "pytest"