From 53cea3ecc3513132b9061fbb4e0571109524f75f Mon Sep 17 00:00:00 2001 From: David Hewitt Date: Tue, 3 Dec 2024 14:57:46 +0000 Subject: [PATCH 1/4] fix `PYO3_CONFIG_FILE` env var not requesting rebuilds --- pyo3-build-config/src/impl_.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyo3-build-config/src/impl_.rs b/pyo3-build-config/src/impl_.rs index bc97460a795..dc626a7f56e 100644 --- a/pyo3-build-config/src/impl_.rs +++ b/pyo3-build-config/src/impl_.rs @@ -420,7 +420,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 From e394f2033c7cda172393b36ad1e77a3c538d49b2 Mon Sep 17 00:00:00 2001 From: David Hewitt Date: Tue, 3 Dec 2024 16:16:01 +0000 Subject: [PATCH 2/4] test and newsfragment --- newsfragments/4758.fixed.md | 1 + pyo3-build-config/src/impl_.rs | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 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 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()])); + } } From bdfed62c21b4ec27a2e5133d62418178e11a905d Mon Sep 17 00:00:00 2001 From: David Hewitt Date: Tue, 3 Dec 2024 16:37:04 +0000 Subject: [PATCH 3/4] fix clippy --- pyo3-build-config/src/impl_.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyo3-build-config/src/impl_.rs b/pyo3-build-config/src/impl_.rs index 474916a73f2..19c299556ab 100644 --- a/pyo3-build-config/src/impl_.rs +++ b/pyo3-build-config/src/impl_.rs @@ -44,7 +44,7 @@ pub(crate) const ABI3_MAX_MINOR: u8 = 12; #[cfg(test)] thread_local! { - static READ_ENV_VARS: RefCell> = RefCell::new(Vec::new()); + static READ_ENV_VARS: RefCell> = const { RefCell::new(Vec::new()) }; } /// Gets an environment variable owned by cargo. From 457b4d3bf69028926ea13afa306b30e96338d7c0 Mon Sep 17 00:00:00 2001 From: David Hewitt Date: Tue, 3 Dec 2024 20:21:51 +0000 Subject: [PATCH 4/4] relax assertion --- pyo3-build-config/src/impl_.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyo3-build-config/src/impl_.rs b/pyo3-build-config/src/impl_.rs index 19c299556ab..f36c5e50226 100644 --- a/pyo3-build-config/src/impl_.rs +++ b/pyo3-build-config/src/impl_.rs @@ -3087,7 +3087,7 @@ mod tests { 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()])); + // 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()))); } }