Skip to content

Commit ef63328

Browse files
bpo-43284: Update platform.win32_ver to use _syscmd_ver instead of sys.getwindowsversion() (GH-25500)
The sys module uses the kernel32.dll version number, which can vary from the "actual" Windows version. Since the best option for getting the version is WMI (which is expensive), we switch back to launching cmd.exe (which is also expensive, but a lot less code on our part). sys.getwindowsversion() is not updated to avoid launching executables from that module. (cherry picked from commit 2a3f489) Co-authored-by: Shreyan Avigyan <[email protected]>
1 parent f7bc441 commit ef63328

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

Doc/library/sys.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,11 +778,16 @@ always available.
778778
Microsoft documentation on :c:func:`OSVERSIONINFOEX` for more information
779779
about these fields.
780780

781-
*platform_version* returns the accurate major version, minor version and
781+
*platform_version* returns the major version, minor version and
782782
build number of the current operating system, rather than the version that
783783
is being emulated for the process. It is intended for use in logging rather
784784
than for feature detection.
785785

786+
.. note::
787+
*platform_version* derives the version from kernel32.dll which can be of a different
788+
version than the OS version. Please use :mod:`platform` module for achieving accurate
789+
OS version.
790+
786791
.. availability:: Windows.
787792

788793
.. versionchanged:: 3.2

Lib/platform.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,9 @@ def _norm_version(version, build=''):
236236
if build:
237237
l.append(build)
238238
try:
239-
ints = map(int, l)
239+
strings = list(map(str, map(int, l)))
240240
except ValueError:
241241
strings = l
242-
else:
243-
strings = list(map(str, ints))
244242
version = '.'.join(strings[:3])
245243
return version
246244

@@ -362,17 +360,20 @@ def win32_ver(release='', version='', csd='', ptype=''):
362360
return release, version, csd, ptype
363361

364362
winver = getwindowsversion()
365-
maj, min, build = winver.platform_version or winver[:3]
366-
version = '{0}.{1}.{2}'.format(maj, min, build)
363+
try:
364+
major, minor, build = map(int, _syscmd_ver()[2].split('.'))
365+
except ValueError:
366+
major, minor, build = winver.platform_version or winver[:3]
367+
version = '{0}.{1}.{2}'.format(major, minor, build)
367368

368-
release = (_WIN32_CLIENT_RELEASES.get((maj, min)) or
369-
_WIN32_CLIENT_RELEASES.get((maj, None)) or
369+
release = (_WIN32_CLIENT_RELEASES.get((major, minor)) or
370+
_WIN32_CLIENT_RELEASES.get((major, None)) or
370371
release)
371372

372373
# getwindowsversion() reflect the compatibility mode Python is
373374
# running under, and so the service pack value is only going to be
374375
# valid if the versions match.
375-
if winver[:2] == (maj, min):
376+
if winver[:2] == (major, minor):
376377
try:
377378
csd = 'SP{}'.format(winver.service_pack_major)
378379
except AttributeError:
@@ -381,8 +382,8 @@ def win32_ver(release='', version='', csd='', ptype=''):
381382

382383
# VER_NT_SERVER = 3
383384
if getattr(winver, 'product_type', None) == 3:
384-
release = (_WIN32_SERVER_RELEASES.get((maj, min)) or
385-
_WIN32_SERVER_RELEASES.get((maj, None)) or
385+
release = (_WIN32_SERVER_RELEASES.get((major, minor)) or
386+
_WIN32_SERVER_RELEASES.get((major, None)) or
386387
release)
387388

388389
try:
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
platform.win32_ver derives the windows version from
2+
sys.getwindowsversion().platform_version which in turn derives the version
3+
from kernel32.dll (which can be of a different version than Windows itself).
4+
Therefore change the platform.win32_ver to determine the version using the
5+
platform module's _syscmd_ver private function to return an accurate
6+
version.

0 commit comments

Comments
 (0)