diff --git a/Changelog.md b/Changelog.md index 1e7158088..2c136fd97 100644 --- a/Changelog.md +++ b/Changelog.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Allow user to override default Emscripten settings in [#1059](https://github.com/PyO3/maturin/pull/1059) * Enable `--crate-type cdylib` on Rust 1.64.0 in [#1060](https://github.com/PyO3/maturin/pull/1060) * Update MSRV to 1.59.0 in [#1071](https://github.com/PyO3/maturin/pull/1071) +* Fix abi3 wheel build when no Python interpreters found in [#1072](https://github.com/PyO3/maturin/pull/1072) ## [0.13.2] - 2022-08-14 diff --git a/src/build_options.rs b/src/build_options.rs index 96153ca9d..10c9e1a86 100644 --- a/src/build_options.rs +++ b/src/build_options.rs @@ -403,18 +403,28 @@ impl BuildOptions { bail!("Failed to find a python interpreter"); } } else { - let interpreters = find_interpreter_in_host( + let found_interpreters = find_interpreter_in_host( bridge, interpreter, target, Some(*minor as usize), ) - .unwrap_or_else(|_| { - find_interpreter_in_sysconfig(interpreter, target, Some(*minor as usize)) - .unwrap_or_default() - }); + .or_else(|err| { + let interps = find_interpreter_in_sysconfig( + interpreter, + target, + Some(*minor as usize), + ) + .unwrap_or_default(); + if interps.is_empty() && !self.interpreter.is_empty() { + // Print error when user supplied `--interpreter` option + Err(err) + } else { + Ok(interps) + } + })?; println!("🐍 Not using a specific python interpreter"); - if interpreter.is_empty() { + if self.interpreter.is_empty() { // Fake one to make `BuildContext::build_wheels` happy for abi3 when no cpython/pypy found on host // The python interpreter config doesn't matter, as it's not used for anything Ok(vec![PythonInterpreter { @@ -432,9 +442,9 @@ impl BuildOptions { runnable: false, }]) } else if target.cross_compiling() { - let mut interps = Vec::with_capacity(interpreters.len()); + let mut interps = Vec::with_capacity(found_interpreters.len()); let mut pypys = Vec::new(); - for interp in interpreters { + for interp in found_interpreters { if interp.interpreter_kind.is_pypy() { pypys.push(PathBuf::from(format!( "pypy{}.{}", @@ -458,10 +468,10 @@ impl BuildOptions { } Ok(interps) } else { - if interpreters.is_empty() { + if found_interpreters.is_empty() { bail!("Failed to find any python interpreter"); } - Ok(interpreters) + Ok(found_interpreters) } } } diff --git a/tests/common/other.rs b/tests/common/other.rs index 839c7d361..d721259fe 100644 --- a/tests/common/other.rs +++ b/tests/common/other.rs @@ -150,3 +150,53 @@ pub fn test_source_distribution( ); Ok(()) } + +pub fn abi3_python_interpreter_args() -> Result<()> { + // Case 1: maturin build without `-i`, should work + let options = BuildOptions::try_parse_from(vec![ + "build", + "--manifest-path", + "test-crates/pyo3-pure/Cargo.toml", + "--quiet", + ])?; + let result = options.into_build_context(false, cfg!(feature = "faster-tests"), false); + assert!(result.is_ok()); + + // Case 2: maturin build -i python2.7, errors because python2.7 is supported + let options = BuildOptions::try_parse_from(vec![ + "build", + "--manifest-path", + "test-crates/pyo3-pure/Cargo.toml", + "--quiet", + "-i", + "python2.7", + ])?; + let result = options.into_build_context(false, cfg!(feature = "faster-tests"), false); + assert!(result.is_err()); + + // Case 3: maturin build -i python3.10, should work because python3.10 is in bundled sysconfigs + let options = BuildOptions::try_parse_from(vec![ + "build", + "--manifest-path", + "test-crates/pyo3-pure/Cargo.toml", + "--quiet", + "-i", + "python3.10", + ])?; + let result = options.into_build_context(false, cfg!(feature = "faster-tests"), false); + assert!(result.is_ok()); + + // Case 4: maturin build -i python-does-not-exists, errors because python executable is not found + let options = BuildOptions::try_parse_from(vec![ + "build", + "--manifest-path", + "test-crates/pyo3-pure/Cargo.toml", + "--quiet", + "-i", + "python-does-not-exists", + ])?; + let result = options.into_build_context(false, cfg!(feature = "faster-tests"), false); + assert!(result.is_err()); + + Ok(()) +} diff --git a/tests/run.rs b/tests/run.rs index 2588d38e1..221267448 100644 --- a/tests/run.rs +++ b/tests/run.rs @@ -292,3 +292,8 @@ fn workspace_with_path_dep_sdist() { "workspace_with_path_dep_sdist", )) } + +#[test] +fn abi3_python_interpreter_args() { + handle_result(other::abi3_python_interpreter_args()); +}