From 5dab92ea839c7e1810a41a249873056773893363 Mon Sep 17 00:00:00 2001
From: thexai <58434170+thexai@users.noreply.github.com>
Date: Mon, 29 Jun 2026 17:15:21 +0200
Subject: [PATCH 1/4] gh-152433: Windows: fix errors in sys.getwindowsversion()
for UWP build
---
.../2026-06-29-17-17-10.gh-issue-152433.AhW--j.rst | 2 ++
Python/sysmodule.c | 13 +++++++------
2 files changed, 9 insertions(+), 6 deletions(-)
create mode 100644 Misc/NEWS.d/next/Windows/2026-06-29-17-17-10.gh-issue-152433.AhW--j.rst
diff --git a/Misc/NEWS.d/next/Windows/2026-06-29-17-17-10.gh-issue-152433.AhW--j.rst b/Misc/NEWS.d/next/Windows/2026-06-29-17-17-10.gh-issue-152433.AhW--j.rst
new file mode 100644
index 000000000000000..47f4ff689bd60aa
--- /dev/null
+++ b/Misc/NEWS.d/next/Windows/2026-06-29-17-17-10.gh-issue-152433.AhW--j.rst
@@ -0,0 +1,2 @@
+Fix errors in :func:`sys.getwindowsversion` for Universal Windows Platform
+build.
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index d9f7b9c449cfb94..83e75b58af92f9e 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -1645,13 +1645,10 @@ static PyStructSequence_Desc windows_version_desc = {
via indexing, the rest are name only */
};
+#ifdef MS_WINDOWS_DESKTOP
static PyObject *
_sys_getwindowsversion_from_kernel32(void)
{
-#ifndef MS_WINDOWS_DESKTOP
- PyErr_SetString(PyExc_OSError, "cannot read version info on this platform");
- return NULL;
-#else
HANDLE hKernel32;
wchar_t kernel32_path[MAX_PATH];
LPVOID verblock;
@@ -1688,8 +1685,8 @@ _sys_getwindowsversion_from_kernel32(void)
realBuild = HIWORD(ffi->dwProductVersionLS);
PyMem_RawFree(verblock);
return Py_BuildValue("(kkk)", realMajor, realMinor, realBuild);
-#endif /* !MS_WINDOWS_DESKTOP */
}
+#endif /* MS_WINDOWS_DESKTOP */
/* Disable deprecation warnings about GetVersionEx as the result is
being passed straight through to the caller, who is responsible for
@@ -1719,7 +1716,6 @@ sys_getwindowsversion_impl(PyObject *module)
{
PyObject *version;
int pos = 0;
- OSVERSIONINFOEXW ver;
if (PyObject_GetOptionalAttrString(module, "_cached_windows_version", &version) < 0) {
return NULL;
@@ -1729,6 +1725,8 @@ sys_getwindowsversion_impl(PyObject *module)
}
Py_XDECREF(version);
+ OSVERSIONINFOEXW ver;
+ ZeroMemory(&ver, sizeof(ver));
ver.dwOSVersionInfoSize = sizeof(ver);
if (!GetVersionExW((OSVERSIONINFOW*) &ver))
return PyErr_SetFromWindowsErr(0);
@@ -1756,10 +1754,12 @@ sys_getwindowsversion_impl(PyObject *module)
SET_VERSION_INFO(PyLong_FromLong(ver.wSuiteMask));
SET_VERSION_INFO(PyLong_FromLong(ver.wProductType));
+#ifdef MS_WINDOWS_DESKTOP
// GetVersion will lie if we are running in a compatibility mode.
// We need to read the version info from a system file resource
// to accurately identify the OS version. If we fail for any reason,
// just return whatever GetVersion said.
+ // UWP return correct version from GetVersionExW, this is not necessary.
PyObject *realVersion = _sys_getwindowsversion_from_kernel32();
if (!realVersion) {
if (!PyErr_ExceptionMatches(PyExc_WindowsError)) {
@@ -1775,6 +1775,7 @@ sys_getwindowsversion_impl(PyObject *module)
}
SET_VERSION_INFO(realVersion);
+#endif
#undef SET_VERSION_INFO
From ce0d374dff27e1d3cbaba472688dfe8d2972e66b Mon Sep 17 00:00:00 2001
From: thexai <58434170+thexai@users.noreply.github.com>
Date: Sat, 25 Jul 2026 12:43:59 +0200
Subject: [PATCH 2/4] Add a new field ``device_family`` in version and not call
``_syscmd_ver()`` in UWP
---
Lib/platform.py | 15 ++++++++++-----
Python/sysmodule.c | 5 +++++
2 files changed, 15 insertions(+), 5 deletions(-)
diff --git a/Lib/platform.py b/Lib/platform.py
index 36489d4fdd98ae2..5a63ce9504e264e 100644
--- a/Lib/platform.py
+++ b/Lib/platform.py
@@ -427,11 +427,16 @@ def _win32_ver(version, csd, ptype):
winver = getwindowsversion()
is_client = (getattr(winver, 'product_type', 1) == 1)
- try:
- version = _syscmd_ver()[2]
- major, minor, build = map(int, version.split('.'))
- except ValueError:
- major, minor, build = winver.platform_version or winver[:3]
+
+ if winver.device_family == "Desktop":
+ try:
+ version = _syscmd_ver()[2]
+ major, minor, build = map(int, version.split('.'))
+ except ValueError:
+ major, minor, build = winver.platform_version or winver[:3]
+ version = '{0}.{1}.{2}'.format(major, minor, build)
+ else:
+ major, minor, build = winver[:3]
version = '{0}.{1}.{2}'.format(major, minor, build)
# getwindowsversion() reflect the compatibility mode Python is
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 83e75b58af92f9e..7f14531999d9284 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -1633,6 +1633,7 @@ static PyStructSequence_Field windows_version_fields[] = {
{"suite_mask", "Bit mask identifying available product suites"},
{"product_type", "System product type"},
{"platform_version", "Diagnostic version number"},
+ {"device_family", "Desktop or UWP"},
{0}
};
@@ -1775,6 +1776,10 @@ sys_getwindowsversion_impl(PyObject *module)
}
SET_VERSION_INFO(realVersion);
+ SET_VERSION_INFO(PyUnicode_FromString("Desktop"));
+#else
+ SET_VERSION_INFO(Py_BuildValue("(kkk)", ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber));
+ SET_VERSION_INFO(PyUnicode_FromString("UWP"));
#endif
#undef SET_VERSION_INFO
From 0428b7dc348e66838dd2b7cd21b95dbb9fd1cb5b Mon Sep 17 00:00:00 2001
From: thexai <58434170+thexai@users.noreply.github.com>
Date: Tue, 28 Jul 2026 19:42:14 +0200
Subject: [PATCH 3/4] Handle also ``MS_WINDOWS_GAMES`` use case
---
Python/sysmodule.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 7f14531999d9284..85cf3ac765a322c 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -1633,7 +1633,7 @@ static PyStructSequence_Field windows_version_fields[] = {
{"suite_mask", "Bit mask identifying available product suites"},
{"product_type", "System product type"},
{"platform_version", "Diagnostic version number"},
- {"device_family", "Desktop or UWP"},
+ {"device_family", "'Desktop', 'Xbox' or 'UWP'"},
{0}
};
@@ -1755,12 +1755,11 @@ sys_getwindowsversion_impl(PyObject *module)
SET_VERSION_INFO(PyLong_FromLong(ver.wSuiteMask));
SET_VERSION_INFO(PyLong_FromLong(ver.wProductType));
-#ifdef MS_WINDOWS_DESKTOP
+#if defined(MS_WINDOWS_DESKTOP)
// GetVersion will lie if we are running in a compatibility mode.
// We need to read the version info from a system file resource
// to accurately identify the OS version. If we fail for any reason,
// just return whatever GetVersion said.
- // UWP return correct version from GetVersionExW, this is not necessary.
PyObject *realVersion = _sys_getwindowsversion_from_kernel32();
if (!realVersion) {
if (!PyErr_ExceptionMatches(PyExc_WindowsError)) {
@@ -1777,6 +1776,9 @@ sys_getwindowsversion_impl(PyObject *module)
SET_VERSION_INFO(realVersion);
SET_VERSION_INFO(PyUnicode_FromString("Desktop"));
+#elif defined (MS_WINDOWS_GAMES)
+ SET_VERSION_INFO(Py_BuildValue("(kkk)", ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber));
+ SET_VERSION_INFO(PyUnicode_FromString("Xbox"));
#else
SET_VERSION_INFO(Py_BuildValue("(kkk)", ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber));
SET_VERSION_INFO(PyUnicode_FromString("UWP"));
From 0f69920257a1d71e16f45c41c45815f36efdcb50 Mon Sep 17 00:00:00 2001
From: thexai <58434170+thexai@users.noreply.github.com>
Date: Wed, 29 Jul 2026 17:47:59 +0200
Subject: [PATCH 4/4] Code cosmetics
---
Python/sysmodule.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 85cf3ac765a322c..60669e462f3e02a 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -1776,7 +1776,7 @@ sys_getwindowsversion_impl(PyObject *module)
SET_VERSION_INFO(realVersion);
SET_VERSION_INFO(PyUnicode_FromString("Desktop"));
-#elif defined (MS_WINDOWS_GAMES)
+#elif defined(MS_WINDOWS_GAMES)
SET_VERSION_INFO(Py_BuildValue("(kkk)", ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber));
SET_VERSION_INFO(PyUnicode_FromString("Xbox"));
#else