Skip to content

Commit 5dab92e

Browse files
committed
gh-152433: Windows: fix errors in sys.getwindowsversion() for UWP build
1 parent bc08413 commit 5dab92e

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix errors in :func:`sys.getwindowsversion` for Universal Windows Platform
2+
build.

Python/sysmodule.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,13 +1645,10 @@ static PyStructSequence_Desc windows_version_desc = {
16451645
via indexing, the rest are name only */
16461646
};
16471647

1648+
#ifdef MS_WINDOWS_DESKTOP
16481649
static PyObject *
16491650
_sys_getwindowsversion_from_kernel32(void)
16501651
{
1651-
#ifndef MS_WINDOWS_DESKTOP
1652-
PyErr_SetString(PyExc_OSError, "cannot read version info on this platform");
1653-
return NULL;
1654-
#else
16551652
HANDLE hKernel32;
16561653
wchar_t kernel32_path[MAX_PATH];
16571654
LPVOID verblock;
@@ -1688,8 +1685,8 @@ _sys_getwindowsversion_from_kernel32(void)
16881685
realBuild = HIWORD(ffi->dwProductVersionLS);
16891686
PyMem_RawFree(verblock);
16901687
return Py_BuildValue("(kkk)", realMajor, realMinor, realBuild);
1691-
#endif /* !MS_WINDOWS_DESKTOP */
16921688
}
1689+
#endif /* MS_WINDOWS_DESKTOP */
16931690

16941691
/* Disable deprecation warnings about GetVersionEx as the result is
16951692
being passed straight through to the caller, who is responsible for
@@ -1719,7 +1716,6 @@ sys_getwindowsversion_impl(PyObject *module)
17191716
{
17201717
PyObject *version;
17211718
int pos = 0;
1722-
OSVERSIONINFOEXW ver;
17231719

17241720
if (PyObject_GetOptionalAttrString(module, "_cached_windows_version", &version) < 0) {
17251721
return NULL;
@@ -1729,6 +1725,8 @@ sys_getwindowsversion_impl(PyObject *module)
17291725
}
17301726
Py_XDECREF(version);
17311727

1728+
OSVERSIONINFOEXW ver;
1729+
ZeroMemory(&ver, sizeof(ver));
17321730
ver.dwOSVersionInfoSize = sizeof(ver);
17331731
if (!GetVersionExW((OSVERSIONINFOW*) &ver))
17341732
return PyErr_SetFromWindowsErr(0);
@@ -1756,10 +1754,12 @@ sys_getwindowsversion_impl(PyObject *module)
17561754
SET_VERSION_INFO(PyLong_FromLong(ver.wSuiteMask));
17571755
SET_VERSION_INFO(PyLong_FromLong(ver.wProductType));
17581756

1757+
#ifdef MS_WINDOWS_DESKTOP
17591758
// GetVersion will lie if we are running in a compatibility mode.
17601759
// We need to read the version info from a system file resource
17611760
// to accurately identify the OS version. If we fail for any reason,
17621761
// just return whatever GetVersion said.
1762+
// UWP return correct version from GetVersionExW, this is not necessary.
17631763
PyObject *realVersion = _sys_getwindowsversion_from_kernel32();
17641764
if (!realVersion) {
17651765
if (!PyErr_ExceptionMatches(PyExc_WindowsError)) {
@@ -1775,6 +1775,7 @@ sys_getwindowsversion_impl(PyObject *module)
17751775
}
17761776

17771777
SET_VERSION_INFO(realVersion);
1778+
#endif
17781779

17791780
#undef SET_VERSION_INFO
17801781

0 commit comments

Comments
 (0)