Skip to content

Commit af49df9

Browse files
authored
gh-152433: Fix errors in sys.getwindowsversion() for UWP build (GH-152604)
1 parent c5168ea commit af49df9

3 files changed

Lines changed: 26 additions & 11 deletions

File tree

Lib/platform.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,11 +427,16 @@ def _win32_ver(version, csd, ptype):
427427

428428
winver = getwindowsversion()
429429
is_client = (getattr(winver, 'product_type', 1) == 1)
430-
try:
431-
version = _syscmd_ver()[2]
432-
major, minor, build = map(int, version.split('.'))
433-
except ValueError:
434-
major, minor, build = winver.platform_version or winver[:3]
430+
431+
if winver.device_family == "Desktop":
432+
try:
433+
version = _syscmd_ver()[2]
434+
major, minor, build = map(int, version.split('.'))
435+
except ValueError:
436+
major, minor, build = winver.platform_version or winver[:3]
437+
version = '{0}.{1}.{2}'.format(major, minor, build)
438+
else:
439+
major, minor, build = winver[:3]
435440
version = '{0}.{1}.{2}'.format(major, minor, build)
436441

437442
# getwindowsversion() reflect the compatibility mode Python is
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: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,6 +1633,7 @@ static PyStructSequence_Field windows_version_fields[] = {
16331633
{"suite_mask", "Bit mask identifying available product suites"},
16341634
{"product_type", "System product type"},
16351635
{"platform_version", "Diagnostic version number"},
1636+
{"device_family", "'Desktop', 'Xbox' or 'UWP'"},
16361637
{0}
16371638
};
16381639

@@ -1645,13 +1646,10 @@ static PyStructSequence_Desc windows_version_desc = {
16451646
via indexing, the rest are name only */
16461647
};
16471648

1649+
#ifdef MS_WINDOWS_DESKTOP
16481650
static PyObject *
16491651
_sys_getwindowsversion_from_kernel32(void)
16501652
{
1651-
#ifndef MS_WINDOWS_DESKTOP
1652-
PyErr_SetString(PyExc_OSError, "cannot read version info on this platform");
1653-
return NULL;
1654-
#else
16551653
HANDLE hKernel32;
16561654
wchar_t kernel32_path[MAX_PATH];
16571655
LPVOID verblock;
@@ -1688,8 +1686,8 @@ _sys_getwindowsversion_from_kernel32(void)
16881686
realBuild = HIWORD(ffi->dwProductVersionLS);
16891687
PyMem_RawFree(verblock);
16901688
return Py_BuildValue("(kkk)", realMajor, realMinor, realBuild);
1691-
#endif /* !MS_WINDOWS_DESKTOP */
16921689
}
1690+
#endif /* MS_WINDOWS_DESKTOP */
16931691

16941692
/* Disable deprecation warnings about GetVersionEx as the result is
16951693
being passed straight through to the caller, who is responsible for
@@ -1719,7 +1717,6 @@ sys_getwindowsversion_impl(PyObject *module)
17191717
{
17201718
PyObject *version;
17211719
int pos = 0;
1722-
OSVERSIONINFOEXW ver;
17231720

17241721
if (PyObject_GetOptionalAttrString(module, "_cached_windows_version", &version) < 0) {
17251722
return NULL;
@@ -1729,6 +1726,8 @@ sys_getwindowsversion_impl(PyObject *module)
17291726
}
17301727
Py_XDECREF(version);
17311728

1729+
OSVERSIONINFOEXW ver;
1730+
ZeroMemory(&ver, sizeof(ver));
17321731
ver.dwOSVersionInfoSize = sizeof(ver);
17331732
if (!GetVersionExW((OSVERSIONINFOW*) &ver))
17341733
return PyErr_SetFromWindowsErr(0);
@@ -1756,6 +1755,7 @@ sys_getwindowsversion_impl(PyObject *module)
17561755
SET_VERSION_INFO(PyLong_FromLong(ver.wSuiteMask));
17571756
SET_VERSION_INFO(PyLong_FromLong(ver.wProductType));
17581757

1758+
#if defined(MS_WINDOWS_DESKTOP)
17591759
// GetVersion will lie if we are running in a compatibility mode.
17601760
// We need to read the version info from a system file resource
17611761
// to accurately identify the OS version. If we fail for any reason,
@@ -1775,6 +1775,14 @@ sys_getwindowsversion_impl(PyObject *module)
17751775
}
17761776

17771777
SET_VERSION_INFO(realVersion);
1778+
SET_VERSION_INFO(PyUnicode_FromString("Desktop"));
1779+
#elif defined(MS_WINDOWS_GAMES)
1780+
SET_VERSION_INFO(Py_BuildValue("(kkk)", ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber));
1781+
SET_VERSION_INFO(PyUnicode_FromString("Xbox"));
1782+
#else
1783+
SET_VERSION_INFO(Py_BuildValue("(kkk)", ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber));
1784+
SET_VERSION_INFO(PyUnicode_FromString("UWP"));
1785+
#endif
17781786

17791787
#undef SET_VERSION_INFO
17801788

0 commit comments

Comments
 (0)