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 dc626a7f56e..474916a73f2 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> = 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) } @@ -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(); + READ_ENV_VARS + .with(|vars| assert_eq!(vars.borrow().as_slice(), &["PYO3_CONFIG_FILE".to_string()])); + } }