Skip to content

Commit 2db351b

Browse files
Merge #2937
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]>
2 parents 1e4dc54 + d67a8dc commit 2db351b

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

newsfragments/2937.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Link against `pythonXY_d.dll` for debug Python builds on Windows.

pyo3-build-config/src/impl_.rs

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ print_if_set("base_prefix", base_prefix)
230230
print("executable", sys.executable)
231231
print("calcsize_pointer", struct.calcsize("P"))
232232
print("mingw", get_platform().startswith("mingw"))
233+
print("ext_suffix", get_config_var("EXT_SUFFIX"))
233234
"#;
234235
let output = run_python_script(interpreter.as_ref(), SCRIPT)?;
235236
let map: HashMap<String, String> = parse_script_output(&output);
@@ -261,6 +262,10 @@ print("mingw", get_platform().startswith("mingw"))
261262
implementation,
262263
abi3,
263264
map["mingw"].as_str() == "True",
265+
// This is the best heuristic currently available to detect debug build
266+
// on Windows from sysconfig - e.g. ext_suffix may be
267+
// `_d.cp312-win_amd64.pyd` for 3.12 debug build
268+
map["ext_suffix"].starts_with("_d."),
264269
)
265270
} else {
266271
default_lib_name_unix(
@@ -1418,6 +1423,7 @@ fn default_abi3_config(host: &Triple, version: PythonVersion) -> InterpreterConf
14181423
implementation,
14191424
abi3,
14201425
false,
1426+
false,
14211427
))
14221428
} else {
14231429
None
@@ -1493,6 +1499,7 @@ fn default_lib_name_for_target(
14931499
implementation,
14941500
abi3,
14951501
false,
1502+
false,
14961503
))
14971504
} else if is_linking_libpython_for_target(target) {
14981505
Some(default_lib_name_unix(version, implementation, None))
@@ -1506,8 +1513,13 @@ fn default_lib_name_windows(
15061513
implementation: PythonImplementation,
15071514
abi3: bool,
15081515
mingw: bool,
1516+
debug: bool,
15091517
) -> String {
1510-
if abi3 && !implementation.is_pypy() {
1518+
if debug {
1519+
// CPython bug: linking against python3_d.dll raises error
1520+
// https://github.com/python/cpython/issues/101614
1521+
format!("python{}{}_d", version.major, version.minor)
1522+
} else if abi3 && !implementation.is_pypy() {
15111523
WINDOWS_ABI3_LIB_NAME.to_owned()
15121524
} else if mingw {
15131525
// https://packages.msys2.org/base/mingw-w64-python
@@ -2244,7 +2256,8 @@ mod tests {
22442256
PythonVersion { major: 3, minor: 7 },
22452257
CPython,
22462258
false,
2247-
false
2259+
false,
2260+
false,
22482261
),
22492262
"python37",
22502263
);
@@ -2253,7 +2266,8 @@ mod tests {
22532266
PythonVersion { major: 3, minor: 7 },
22542267
CPython,
22552268
true,
2256-
false
2269+
false,
2270+
false,
22572271
),
22582272
"python3",
22592273
);
@@ -2262,7 +2276,8 @@ mod tests {
22622276
PythonVersion { major: 3, minor: 7 },
22632277
CPython,
22642278
false,
2265-
true
2279+
true,
2280+
false,
22662281
),
22672282
"python3.7",
22682283
);
@@ -2271,7 +2286,8 @@ mod tests {
22712286
PythonVersion { major: 3, minor: 7 },
22722287
CPython,
22732288
true,
2274-
true
2289+
true,
2290+
false,
22752291
),
22762292
"python3",
22772293
);
@@ -2280,10 +2296,33 @@ mod tests {
22802296
PythonVersion { major: 3, minor: 7 },
22812297
PyPy,
22822298
true,
2283-
false
2299+
false,
2300+
false,
22842301
),
22852302
"python37",
22862303
);
2304+
assert_eq!(
2305+
super::default_lib_name_windows(
2306+
PythonVersion { major: 3, minor: 7 },
2307+
CPython,
2308+
false,
2309+
false,
2310+
true,
2311+
),
2312+
"python37_d",
2313+
);
2314+
// abi3 debug builds on windows use version-specific lib
2315+
// to workaround https://github.com/python/cpython/issues/101614
2316+
assert_eq!(
2317+
super::default_lib_name_windows(
2318+
PythonVersion { major: 3, minor: 7 },
2319+
CPython,
2320+
true,
2321+
false,
2322+
true,
2323+
),
2324+
"python37_d",
2325+
);
22872326
}
22882327

22892328
#[test]

0 commit comments

Comments
 (0)