Skip to content

Commit

Permalink
fix PYO3_CONFIG_FILE env var not requesting rebuilds (#4758)
Browse files Browse the repository at this point in the history
* fix `PYO3_CONFIG_FILE` env var not requesting rebuilds

* test and newsfragment

* fix clippy

* relax assertion
  • Loading branch information
davidhewitt committed Dec 3, 2024
1 parent 9c862e5 commit a423c79
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions newsfragments/4758.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix compile-time regression in PyO3 0.23.0 where changing `PYO3_CONFIG_FILE` would not reconfigure PyO3 for the new interpreter.
26 changes: 23 additions & 3 deletions pyo3-build-config/src/impl_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#[path = "import_lib.rs"]
mod import_lib;

#[cfg(test)]
use std::cell::RefCell;
use std::{
collections::{HashMap, HashSet},
env,
Expand All @@ -15,8 +17,7 @@ use std::{
io::{BufRead, BufReader, Read, Write},
path::{Path, PathBuf},
process::{Command, Stdio},
str,
str::FromStr,
str::{self, FromStr},
};

pub use target_lexicon::Triple;
Expand All @@ -41,6 +42,11 @@ const MINIMUM_SUPPORTED_VERSION_GRAALPY: PythonVersion = PythonVersion {
/// Maximum Python version that can be used as minimum required Python version with abi3.
pub(crate) const ABI3_MAX_MINOR: u8 = 12;

#[cfg(test)]
thread_local! {
static READ_ENV_VARS: RefCell<Vec<String>> = const { RefCell::new(Vec::new()) };
}

/// Gets an environment variable owned by cargo.
///
/// Environment variables set by cargo are expected to be valid UTF8.
Expand All @@ -54,6 +60,12 @@ pub fn env_var(var: &str) -> Option<OsString> {
if cfg!(feature = "resolve-config") {
println!("cargo:rerun-if-env-changed={}", var);
}
#[cfg(test)]
{
READ_ENV_VARS.with(|env_vars| {
env_vars.borrow_mut().push(var.to_owned());
});
}
env::var_os(var)
}

Expand Down Expand Up @@ -420,7 +432,7 @@ print("gil_disabled", get_config_var("Py_GIL_DISABLED"))
/// The `abi3` features, if set, may apply an `abi3` constraint to the Python version.
#[allow(dead_code)] // only used in build.rs
pub(super) fn from_pyo3_config_file_env() -> Option<Result<Self>> {
cargo_env_var("PYO3_CONFIG_FILE").map(|path| {
env_var("PYO3_CONFIG_FILE").map(|path| {
let path = Path::new(&path);
println!("cargo:rerun-if-changed={}", path.display());
// Absolute path is necessary because this build script is run with a cwd different to the
Expand Down Expand Up @@ -3070,4 +3082,12 @@ mod tests {
"
));
}

#[test]
fn test_from_pyo3_config_file_env_rebuild() {
READ_ENV_VARS.with(|vars| vars.borrow_mut().clear());
let _ = InterpreterConfig::from_pyo3_config_file_env();
// it's possible that other env vars were also read, hence just checking for contains
READ_ENV_VARS.with(|vars| assert!(vars.borrow().contains(&"PYO3_CONFIG_FILE".to_string())));
}
}

0 comments on commit a423c79

Please sign in to comment.