@@ -1184,8 +1184,45 @@ def hack_project_files(
1184
1184
pythonapi = PyDLL(None)
1185
1185
"""
1186
1186
1187
+ SYSMODULE_WINVER_SEARCH = b"""
1188
+ #ifdef MS_COREDLL
1189
+ SET_SYS("dllhandle", PyLong_FromVoidPtr(PyWin_DLLhModule));
1190
+ SET_SYS_FROM_STRING("winver", PyWin_DLLVersionString);
1191
+ #endif
1192
+ """
1193
+
1194
+ SYSMODULE_WINVER_REPLACE = b"""
1195
+ #ifdef MS_COREDLL
1196
+ SET_SYS("dllhandle", PyLong_FromVoidPtr(PyWin_DLLhModule));
1197
+ SET_SYS_FROM_STRING("winver", PyWin_DLLVersionString);
1198
+ #else
1199
+ SET_SYS_FROM_STRING("winver", "%s");
1200
+ #endif
1201
+ """
1202
+
1203
+ SYSMODULE_WINVER_SEARCH_38 = b"""
1204
+ #ifdef MS_COREDLL
1205
+ SET_SYS_FROM_STRING("dllhandle",
1206
+ PyLong_FromVoidPtr(PyWin_DLLhModule));
1207
+ SET_SYS_FROM_STRING("winver",
1208
+ PyUnicode_FromString(PyWin_DLLVersionString));
1209
+ #endif
1210
+ """
1211
+
1212
+
1213
+ SYSMODULE_WINVER_REPLACE_38 = b"""
1214
+ #ifdef MS_COREDLL
1215
+ SET_SYS_FROM_STRING("dllhandle",
1216
+ PyLong_FromVoidPtr(PyWin_DLLhModule));
1217
+ SET_SYS_FROM_STRING("winver",
1218
+ PyUnicode_FromString(PyWin_DLLVersionString));
1219
+ #else
1220
+ SET_SYS_FROM_STRING("winver", PyUnicode_FromString("%s"));
1221
+ #endif
1222
+ """
1223
+
1187
1224
1188
- def hack_source_files (source_path : pathlib .Path , static : bool ):
1225
+ def hack_source_files (source_path : pathlib .Path , static : bool , python_version : str ):
1189
1226
"""Apply source modifications to make things work."""
1190
1227
1191
1228
# The PyAPI_FUNC, PyAPI_DATA, and PyMODINIT_FUNC macros define symbol
@@ -1286,6 +1323,24 @@ def hack_source_files(source_path: pathlib.Path, static: bool):
1286
1323
b"pythonapi = PyDLL(_sys.executable)" ,
1287
1324
)
1288
1325
1326
+ # The `sys` module only populates `sys.winver` if MS_COREDLL is defined,
1327
+ # which it isn't in static builds. We know what the version should be, so
1328
+ # we go ahead and set it.
1329
+ if static :
1330
+ # Source changed in 3.10.
1331
+ try :
1332
+ static_replace_in_file (
1333
+ source_path / "Python" / "sysmodule.c" ,
1334
+ SYSMODULE_WINVER_SEARCH ,
1335
+ SYSMODULE_WINVER_REPLACE % python_version [0 :3 ].encode ("ascii" ),
1336
+ )
1337
+ except NoSearchStringError :
1338
+ static_replace_in_file (
1339
+ source_path / "Python" / "sysmodule.c" ,
1340
+ SYSMODULE_WINVER_SEARCH_38 ,
1341
+ SYSMODULE_WINVER_REPLACE_38 % python_version [0 :3 ].encode ("ascii" ),
1342
+ )
1343
+
1289
1344
# Producing statically linked binaries invalidates assumptions in the
1290
1345
# layout tool. Update the tool accordingly.
1291
1346
layout_main = source_path / "PC" / "layout" / "main.py"
@@ -1999,7 +2054,9 @@ def build_cpython(
1999
2054
static = static ,
2000
2055
honor_allow_missing_preprocessor = python_entry_name == "cpython-3.8" ,
2001
2056
)
2002
- hack_source_files (cpython_source_path , static = static )
2057
+ hack_source_files (
2058
+ cpython_source_path , static = static , python_version = python_version
2059
+ )
2003
2060
2004
2061
if pgo :
2005
2062
run_msbuild (
0 commit comments