Skip to content

Commit 5e3a8ed

Browse files
committed
Hook up to python-build-standalone, removing dependency on host python
1 parent d47b080 commit 5e3a8ed

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

cibuildwheel/platforms/pyodide.py

+20-15
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
)
3333
from ..util.helpers import prepare_command, unwrap, unwrap_preserving_paragraphs
3434
from ..util.packaging import combine_constraints, find_compatible_wheel, get_pip_version
35+
from ..util.python_build_standalone import (
36+
PythonBuildStandaloneError,
37+
create_python_build_standalone_environment,
38+
)
3539
from ..venv import constraint_flags, virtualenv
3640

3741
IS_WIN: Final[bool] = sys.platform.startswith("win")
@@ -43,6 +47,7 @@ class PythonConfiguration:
4347
identifier: str
4448
default_pyodide_version: str
4549
node_version: str
50+
python_build_standalone_tag: str
4651

4752

4853
class PyodideXBuildEnvInfoVersionRange(TypedDict):
@@ -206,20 +211,20 @@ def install_xbuildenv(env: dict[str, str], pyodide_build_version: str, pyodide_v
206211
return str(pyodide_root)
207212

208213

209-
def get_base_python(identifier: str) -> Path:
210-
implementation_id = identifier.split("-")[0]
211-
majorminor = implementation_id[len("cp") :]
212-
version_info = (int(majorminor[0]), int(majorminor[1:]))
213-
if version_info == sys.version_info[:2]:
214-
return Path(sys.executable)
215-
216-
major_minor = ".".join(str(v) for v in version_info)
217-
python_name = f"python{major_minor}"
218-
which_python = shutil.which(python_name)
219-
if which_python is None:
220-
msg = f"CPython {major_minor} is not installed."
221-
raise errors.FatalError(msg)
222-
return Path(which_python)
214+
def get_base_python(tmp: Path, python_configuration: PythonConfiguration) -> Path:
215+
try:
216+
return create_python_build_standalone_environment(
217+
release_tag=python_configuration.python_build_standalone_tag,
218+
python_version=python_configuration.version,
219+
temp_dir=tmp,
220+
cache_dir=CIBW_CACHE_PATH,
221+
)
222+
except PythonBuildStandaloneError as e:
223+
msg = unwrap(f"""
224+
Failed to create a Python build environment:
225+
{e}
226+
""")
227+
raise errors.FatalError(msg) from e
223228

224229

225230
def setup_python(
@@ -229,7 +234,7 @@ def setup_python(
229234
environment: ParsedEnvironment,
230235
user_pyodide_version: str | None,
231236
) -> dict[str, str]:
232-
base_python = get_base_python(python_configuration.identifier)
237+
base_python = get_base_python(tmp / "base", python_configuration)
233238
pyodide_version = user_pyodide_version or python_configuration.default_pyodide_version
234239

235240
log.step("Setting up build environment...")

cibuildwheel/resources/build-platforms.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ python_configurations = [
161161

162162
[pyodide]
163163
python_configurations = [
164-
{ identifier = "cp312-pyodide_wasm32", version = "3.12", default_pyodide_version = "0.27.0", node_version = "v22" },
164+
{ identifier = "cp312-pyodide_wasm32", version = "3.12", default_pyodide_version = "0.27.0", node_version = "v22", python_build_standalone_tag = "20250317" },
165165
]
166166

167167
[ios]

cibuildwheel/util/python_build_standalone.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def _find_python_executable(extracted_dir: Path) -> Path:
155155

156156
def create_python_build_standalone_environment(
157157
release_tag: str, python_version: str, temp_dir: Path, cache_dir: Path
158-
) -> str:
158+
) -> Path:
159159
"""
160160
Returns a Python environment from python-build-standalone,
161161
downloading it if necessary using a cache, and expanding it into a fresh base path.
@@ -200,5 +200,4 @@ def create_python_build_standalone_environment(
200200
assert not python_base_dir.exists()
201201
extract_tar(archive_path, python_base_dir)
202202

203-
executable_path = _find_python_executable(python_base_dir)
204-
return str(executable_path)
203+
return _find_python_executable(python_base_dir)

0 commit comments

Comments
 (0)