Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pyo3-build-config: fix location for conda interpreter on windows #1873

Merged
merged 1 commit into from Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Expand Up @@ -33,9 +33,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Mark FFI definitions removed in Python 3.10 `PyParser_ASTFromString`, `PyParser_ASTFromStringObject`, `PyParser_ASTFromFile`, `PyParser_ASTFromFileObject`, `PyParser_SimpleParseStringFlags`, `PyParser_SimpleParseStringFlagsFilename`, `PyParser_SimpleParseFileFlags`, `PyParser_SimpleParseString`, `PyParser_SimpleParseFile`, `Py_SymtableString`, and `Py_SymtableStringObject`. [#1830](https://github.com/PyO3/pyo3/pull/1830)
- `pyo3`'s `Cargo.toml` now advertises `links = "python"` to inform Cargo that it links against *libpython*. [#1819](https://github.com/PyO3/pyo3/pull/1819)

### Fixed

- Fix building with a conda environment on Windows. [#1873](https://github.com/PyO3/pyo3/pull/1873)

## [0.14.5] - 2021-09-05

## Added
### Added

- Make `pyo3_build_config::InterpreterConfig` and subfields public. [#1848](https://github.com/PyO3/pyo3/pull/1848)
- Add `resolve-config` feature to the `pyo3-build-config` to control whether its build script does anything. [#1856](https://github.com/PyO3/pyo3/pull/1856)
Expand Down
64 changes: 51 additions & 13 deletions pyo3-build-config/src/impl_.rs
Expand Up @@ -2,7 +2,7 @@ use std::{
collections::{HashMap, HashSet},
convert::AsRef,
env,
ffi::OsString,
ffi::{OsStr, OsString},
fmt::Display,
fs::{self, DirEntry},
io::{BufRead, BufReader, Read, Write},
Expand Down Expand Up @@ -1080,10 +1080,28 @@ fn run_python_script(interpreter: &Path, script: &str) -> Result<String> {
}
}

fn get_venv_path() -> Option<PathBuf> {
fn venv_interpreter(virtual_env: &OsStr, windows: bool) -> PathBuf {
if windows {
Path::new(virtual_env).join("Scripts").join("python.exe")
} else {
Path::new(virtual_env).join("bin").join("python")
}
}

fn conda_env_interpreter(conda_prefix: &OsStr, windows: bool) -> PathBuf {
if windows {
Path::new(conda_prefix).join("python.exe")
} else {
Path::new(conda_prefix).join("bin").join("python")
}
}

fn get_env_interpreter() -> Option<PathBuf> {
match (env_var("VIRTUAL_ENV"), env_var("CONDA_PREFIX")) {
(Some(dir), None) => Some(PathBuf::from(dir)),
(None, Some(dir)) => Some(PathBuf::from(dir)),
// Use cfg rather can CARGO_TARGET_OS because this affects where files are located on the
// build host
(Some(dir), None) => Some(venv_interpreter(&dir, cfg!(windows))),
(None, Some(dir)) => Some(conda_env_interpreter(&dir, cfg!(windows))),
(Some(_), Some(_)) => {
warn!(
"Both VIRTUAL_ENV and CONDA_PREFIX are set. PyO3 will ignore both of these for \
Expand All @@ -1105,14 +1123,8 @@ fn get_venv_path() -> Option<PathBuf> {
pub fn find_interpreter() -> Result<PathBuf> {
if let Some(exe) = env_var("PYO3_PYTHON") {
Ok(exe.into())
} else if let Some(venv_path) = get_venv_path() {
// Use cfg rather can CARGO_TARGET_OS because this affects how files are located on the
// host OS
if cfg!(windows) {
Ok(venv_path.join("Scripts\\python"))
} else {
Ok(venv_path.join("bin/python"))
}
} else if let Some(env_interpreter) = get_env_interpreter() {
Ok(env_interpreter)
} else {
println!("cargo:rerun-if-env-changed=PATH");
["python", "python3"]
Expand Down Expand Up @@ -1181,7 +1193,7 @@ pub fn make_interpreter_config() -> Result<InterpreterConfig> {

#[cfg(test)]
mod tests {
use std::io::Cursor;
use std::{io::Cursor, iter::FromIterator};

use super::*;

Expand Down Expand Up @@ -1516,4 +1528,30 @@ mod tests {
}
)
}

#[test]
fn test_venv_interpreter() {
let base = OsStr::new("base");
assert_eq!(
venv_interpreter(&base, true),
PathBuf::from_iter(&["base", "Scripts", "python.exe"])
);
assert_eq!(
venv_interpreter(&base, false),
PathBuf::from_iter(&["base", "bin", "python"])
);
}

#[test]
fn test_conda_env_interpreter() {
let base = OsStr::new("base");
assert_eq!(
conda_env_interpreter(&base, true),
PathBuf::from_iter(&["base", "python.exe"])
);
assert_eq!(
conda_env_interpreter(&base, false),
PathBuf::from_iter(&["base", "bin", "python"])
);
}
}