8000 windows: populate sys.winver in static builds · adiantek/python-build-standalone@876fd64 · GitHub
[go: up one dir, main page]

Skip to content

Commit 876fd64

Browse files
committed
windows: populate sys.winver in static builds
sys.winver population is protected behind a #ifdef that isn't hit in static builds. I discovered this when adding support for 3.10, as site.py uses sys.winver in 3.10 and bailed due to it not being set.
1 parent c53d127 commit 876fd64

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

cpython-windows/build.py

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,8 +1184,45 @@ def hack_project_files(
11841184
pythonapi = PyDLL(None)
11851185
"""
11861186

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+
11871224

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):
11891226
"""Apply source modifications to make things work."""
11901227

11911228
# 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):
12861323
b"pythonapi = PyDLL(_sys.executable)",
12871324
)
12881325

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+
12891344
# Producing statically linked binaries invalidates assumptions in the
12901345
# layout tool. Update the tool accordingly.
12911346
layout_main = source_path / "PC" / "layout" / "main.py"
@@ -1999,7 +2054,9 @@ def build_cpython(
19992054
static=static,
20002055
honor_allow_missing_preprocessor=python_entry_name == "cpython-3.8",
20012056
)
2002-
hack_source_files(cpython_source_path, static=static)
2057+
hack_source_files(
2058+
cpython_source_path, static=static, python_version=python_version
2059+
)
20032060

20042061
if pgo:
20052062
run_msbuild(

0 commit comments

Comments
 (0)
0