Skip to content

Commit

Permalink
Merge #2937
Browse files Browse the repository at this point in the history
2937: link against pythonXY_d.dll for debug Python on Windows r=davidhewitt a=davidhewitt

Closes #2780 

Note that upstream Python issue python/cpython#101614 means linking against `python3_d.dll` is useless, so I've set this to always use the version-specific builds for now.

The heuristic for detecting a Windows debug build is... not great. I check if the `EXT_SUFFIX` starts with `_d.`, which is the only thing that I could see in the sysconfig which suggested a debug build. If this proves to be brittle, we may wish to ask upstream for something better to be added to `sysconfig`.

Co-authored-by: David Hewitt <[email protected]>
  • Loading branch information
bors[bot] and davidhewitt authored Feb 7, 2023
2 parents 1e4dc54 + d67a8dc commit 2db351b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
1 change: 1 addition & 0 deletions newsfragments/2937.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Link against `pythonXY_d.dll` for debug Python builds on Windows.
51 changes: 45 additions & 6 deletions pyo3-build-config/src/impl_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ print_if_set("base_prefix", base_prefix)
print("executable", sys.executable)
print("calcsize_pointer", struct.calcsize("P"))
print("mingw", get_platform().startswith("mingw"))
print("ext_suffix", get_config_var("EXT_SUFFIX"))
"#;
let output = run_python_script(interpreter.as_ref(), SCRIPT)?;
let map: HashMap<String, String> = parse_script_output(&output);
Expand Down Expand Up @@ -261,6 +262,10 @@ print("mingw", get_platform().startswith("mingw"))
implementation,
abi3,
map["mingw"].as_str() == "True",
// This is the best heuristic currently available to detect debug build
// on Windows from sysconfig - e.g. ext_suffix may be
// `_d.cp312-win_amd64.pyd` for 3.12 debug build
map["ext_suffix"].starts_with("_d."),
)
} else {
default_lib_name_unix(
Expand Down Expand Up @@ -1418,6 +1423,7 @@ fn default_abi3_config(host: &Triple, version: PythonVersion) -> InterpreterConf
implementation,
abi3,
false,
false,
))
} else {
None
Expand Down Expand Up @@ -1493,6 +1499,7 @@ fn default_lib_name_for_target(
implementation,
abi3,
false,
false,
))
} else if is_linking_libpython_for_target(target) {
Some(default_lib_name_unix(version, implementation, None))
Expand All @@ -1506,8 +1513,13 @@ fn default_lib_name_windows(
implementation: PythonImplementation,
abi3: bool,
mingw: bool,
debug: bool,
) -> String {
if abi3 && !implementation.is_pypy() {
if debug {
// CPython bug: linking against python3_d.dll raises error
// https://github.com/python/cpython/issues/101614
format!("python{}{}_d", version.major, version.minor)
} else if abi3 && !implementation.is_pypy() {
WINDOWS_ABI3_LIB_NAME.to_owned()
} else if mingw {
// https://packages.msys2.org/base/mingw-w64-python
Expand Down Expand Up @@ -2244,7 +2256,8 @@ mod tests {
PythonVersion { major: 3, minor: 7 },
CPython,
false,
false
false,
false,
),
"python37",
);
Expand All @@ -2253,7 +2266,8 @@ mod tests {
PythonVersion { major: 3, minor: 7 },
CPython,
true,
false
false,
false,
),
"python3",
);
Expand All @@ -2262,7 +2276,8 @@ mod tests {
PythonVersion { major: 3, minor: 7 },
CPython,
false,
true
true,
false,
),
"python3.7",
);
Expand All @@ -2271,7 +2286,8 @@ mod tests {
PythonVersion { major: 3, minor: 7 },
CPython,
true,
true
true,
false,
),
"python3",
);
Expand All @@ -2280,10 +2296,33 @@ mod tests {
PythonVersion { major: 3, minor: 7 },
PyPy,
true,
false
false,
false,
),
"python37",
);
assert_eq!(
super::default_lib_name_windows(
PythonVersion { major: 3, minor: 7 },
CPython,
false,
false,
true,
),
"python37_d",
);
// abi3 debug builds on windows use version-specific lib
// to workaround https://github.com/python/cpython/issues/101614
assert_eq!(
super::default_lib_name_windows(
PythonVersion { major: 3, minor: 7 },
CPython,
true,
false,
true,
),
"python37_d",
);
}

#[test]
Expand Down

0 comments on commit 2db351b

Please sign in to comment.