Skip to content

Commit a423c79

Browse files
committed
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
1 parent 9c862e5 commit a423c79

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

newsfragments/4758.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix compile-time regression in PyO3 0.23.0 where changing `PYO3_CONFIG_FILE` would not reconfigure PyO3 for the new interpreter.

pyo3-build-config/src/impl_.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#[path = "import_lib.rs"]
77
mod import_lib;
88

9+
#[cfg(test)]
10+
use std::cell::RefCell;
911
use std::{
1012
collections::{HashMap, HashSet},
1113
env,
@@ -15,8 +17,7 @@ use std::{
1517
io::{BufRead, BufReader, Read, Write},
1618
path::{Path, PathBuf},
1719
process::{Command, Stdio},
18-
str,
19-
str::FromStr,
20+
str::{self, FromStr},
2021
};
2122

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

45+
#[cfg(test)]
46+
thread_local! {
47+
static READ_ENV_VARS: RefCell<Vec<String>> = const { RefCell::new(Vec::new()) };
48+
}
49+
4450
/// Gets an environment variable owned by cargo.
4551
///
4652
/// Environment variables set by cargo are expected to be valid UTF8.
@@ -54,6 +60,12 @@ pub fn env_var(var: &str) -> Option<OsString> {
5460
if cfg!(feature = "resolve-config") {
5561
println!("cargo:rerun-if-env-changed={}", var);
5662
}
63+
#[cfg(test)]
64+
{
65+
READ_ENV_VARS.with(|env_vars| {
66+
env_vars.borrow_mut().push(var.to_owned());
67+
});
68+
}
5769
env::var_os(var)
5870
}
5971

@@ -420,7 +432,7 @@ print("gil_disabled", get_config_var("Py_GIL_DISABLED"))
420432
/// The `abi3` features, if set, may apply an `abi3` constraint to the Python version.
421433
#[allow(dead_code)] // only used in build.rs
422434
pub(super) fn from_pyo3_config_file_env() -> Option<Result<Self>> {
423-
cargo_env_var("PYO3_CONFIG_FILE").map(|path| {
435+
env_var("PYO3_CONFIG_FILE").map(|path| {
424436
let path = Path::new(&path);
425437
println!("cargo:rerun-if-changed={}", path.display());
426438
// Absolute path is necessary because this build script is run with a cwd different to the
@@ -3070,4 +3082,12 @@ mod tests {
30703082
"
30713083
));
30723084
}
3085+
3086+
#[test]
3087+
fn test_from_pyo3_config_file_env_rebuild() {
3088+
READ_ENV_VARS.with(|vars| vars.borrow_mut().clear());
3089+
let _ = InterpreterConfig::from_pyo3_config_file_env();
3090+
// it's possible that other env vars were also read, hence just checking for contains
3091+
READ_ENV_VARS.with(|vars| assert!(vars.borrow().contains(&"PYO3_CONFIG_FILE".to_string())));
3092+
}
30733093
}

0 commit comments

Comments
 (0)