https://github.com/python/cpython/commit/d307d05350e26a7a5f8f74db9af632a1521... commit: d307d05350e26a7a5f8f74db9af632a15215b50f branch: master author: Steve Dower <steve.dower@microsoft.com> committer: GitHub <noreply@github.com> date: 2019-04-22T11:40:12-07:00 summary: Fixes platform.win32_ver on non-Windows platforms (GH-12912) files: M Lib/platform.py diff --git a/Lib/platform.py b/Lib/platform.py index 2ab68aed7861..21defd1095d2 100755 --- a/Lib/platform.py +++ b/Lib/platform.py @@ -339,10 +339,6 @@ def win32_ver(release='', version='', csd='', ptype=''): from sys import getwindowsversion except ImportError: return release, version, csd, ptype - try: - from winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE - except ImportError: - from _winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE winver = getwindowsversion() maj, min, build = winver.platform_version or winver[:3] @@ -368,16 +364,20 @@ def win32_ver(release='', version='', csd='', ptype=''): _WIN32_SERVER_RELEASES.get((maj, None)) or release) - key = None try: - key = OpenKeyEx(HKEY_LOCAL_MACHINE, - r'SOFTWARE\Microsoft\Windows NT\CurrentVersion') - ptype = QueryValueEx(key, 'CurrentType')[0] - except: + try: + import winreg + except ImportError: + import _winreg as winreg + except ImportError: pass - finally: - if key: - CloseKey(key) + else: + try: + cvkey = r'SOFTWARE\Microsoft\Windows NT\CurrentVersion' + with winreg.OpenKeyEx(HKEY_LOCAL_MACHINE, cvkey) as key: + ptype = QueryValueEx(key, 'CurrentType')[0] + except: + pass return release, version, csd, ptype