From d67a8dc0a16acd3a52af81aed97a13280906c7c8 Mon Sep 17 00:00:00 2001 From: David Hewitt <1939362+davidhewitt@users.noreply.github.com> Date: Mon, 6 Feb 2023 20:33:16 +0000 Subject: [PATCH] link against pythonXY_d.dll for debug Python on Windows --- newsfragments/2937.fixed.md | 1 + pyo3-build-config/src/impl_.rs | 51 ++++++++++++++++++++++++++++++---- 2 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 newsfragments/2937.fixed.md diff --git a/newsfragments/2937.fixed.md b/newsfragments/2937.fixed.md new file mode 100644 index 00000000000..9956d7b9978 --- /dev/null +++ b/newsfragments/2937.fixed.md @@ -0,0 +1 @@ +Link against `pythonXY_d.dll` for debug Python builds on Windows. diff --git a/pyo3-build-config/src/impl_.rs b/pyo3-build-config/src/impl_.rs index a46936cf7e7..159f999a6ec 100644 --- a/pyo3-build-config/src/impl_.rs +++ b/pyo3-build-config/src/impl_.rs @@ -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 = parse_script_output(&output); @@ -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( @@ -1418,6 +1423,7 @@ fn default_abi3_config(host: &Triple, version: PythonVersion) -> InterpreterConf implementation, abi3, false, + false, )) } else { None @@ -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)) @@ -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 @@ -2244,7 +2256,8 @@ mod tests { PythonVersion { major: 3, minor: 7 }, CPython, false, - false + false, + false, ), "python37", ); @@ -2253,7 +2266,8 @@ mod tests { PythonVersion { major: 3, minor: 7 }, CPython, true, - false + false, + false, ), "python3", ); @@ -2262,7 +2276,8 @@ mod tests { PythonVersion { major: 3, minor: 7 }, CPython, false, - true + true, + false, ), "python3.7", ); @@ -2271,7 +2286,8 @@ mod tests { PythonVersion { major: 3, minor: 7 }, CPython, true, - true + true, + false, ), "python3", ); @@ -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]