From 48436da870d379df01f7c7745bbc21149d6062b6 Mon Sep 17 00:00:00 2001 From: David Hewitt Date: Tue, 3 Dec 2024 20:44:51 +0000 Subject: [PATCH] fix `PYO3_CONFIG_FILE` env var not requesting rebuilds (#4758) * fix `PYO3_CONFIG_FILE` env var not requesting rebuilds * test and newsfragment * fix clippy * relax assertion --- newsfragments/4758.fixed.md | 1 + pyo3-build-config/src/impl_.rs | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 newsfragments/4758.fixed.md diff --git a/newsfragments/4758.fixed.md b/newsfragments/4758.fixed.md new file mode 100644 index 00000000000..ef73abd966d --- /dev/null +++ b/newsfragments/4758.fixed.md @@ -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. diff --git a/pyo3-build-config/src/impl_.rs b/pyo3-build-config/src/impl_.rs index bc97460a795..f36c5e50226 100644 --- a/pyo3-build-config/src/impl_.rs +++ b/pyo3-build-config/src/impl_.rs @@ -6,6 +6,8 @@ #[path = "import_lib.rs"] mod import_lib; +#[cfg(test)] +use std::cell::RefCell; use std::{ collections::{HashMap, HashSet}, env, @@ -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; @@ -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> = const { RefCell::new(Vec::new()) }; +} + /// Gets an environment variable owned by cargo. /// /// Environment variables set by cargo are expected to be valid UTF8. @@ -54,6 +60,12 @@ pub fn env_var(var: &str) -> Option { 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) } @@ -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> { - 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 @@ -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()))); + } }