summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteve Dower <steve.dower@microsoft.com>2016-09-10 01:01:25 (GMT)
committerSteve Dower <steve.dower@microsoft.com>2016-09-10 01:01:25 (GMT)
commit6a294a54dea96c3ee4b1b78e2b27de5e2324bb49 (patch)
tree8e45b483a435caa0bf88f95714defcaef1201a84
parent8dcc48ee3b6447cd63038e63ee11de36f6d85a68 (diff)
downloadcpython-6a294a54dea96c3ee4b1b78e2b27de5e2324bb49.zip
cpython-6a294a54dea96c3ee4b1b78e2b27de5e2324bb49.tar.gz
cpython-6a294a54dea96c3ee4b1b78e2b27de5e2324bb49.tar.bz2
Issue #27932: Fixes memory leak in platform.win32_ver()
-rwxr-xr-xLib/platform.py62
-rw-r--r--Misc/NEWS2
2 files changed, 35 insertions, 29 deletions
diff --git a/Lib/platform.py b/Lib/platform.py
index e7eaa32..e219326 100755
--- a/Lib/platform.py
+++ b/Lib/platform.py
@@ -498,57 +498,61 @@ _WIN32_SERVER_RELEASES = {
(6, None): "post2012ServerR2",
}
-def _get_real_winver(maj, min, build):
- if maj < 6 or (maj == 6 and min < 2):
- return maj, min, build
-
- from ctypes import (c_buffer, POINTER, byref, create_unicode_buffer,
- Structure, WinDLL)
- from ctypes.wintypes import DWORD, HANDLE
+if sys.platform == 'win32':
+ import ctypes
+ import ctypes.wintypes
- class VS_FIXEDFILEINFO(Structure):
+ class VS_FIXEDFILEINFO(ctypes.Structure):
_fields_ = [
- ("dwSignature", DWORD),
- ("dwStrucVersion", DWORD),
- ("dwFileVersionMS", DWORD),
- ("dwFileVersionLS", DWORD),
- ("dwProductVersionMS", DWORD),
- ("dwProductVersionLS", DWORD),
- ("dwFileFlagsMask", DWORD),
- ("dwFileFlags", DWORD),
- ("dwFileOS", DWORD),
- ("dwFileType", DWORD),
- ("dwFileSubtype", DWORD),
- ("dwFileDateMS", DWORD),
- ("dwFileDateLS", DWORD),
+ ("dwSignature", ctypes.wintypes.DWORD),
+ ("dwStrucVersion", ctypes.wintypes.DWORD),
+ ("dwFileVersionMS", ctypes.wintypes.DWORD),
+ ("dwFileVersionLS", ctypes.wintypes.DWORD),
+ ("dwProductVersionMS", ctypes.wintypes.DWORD),
+ ("dwProductVersionLS", ctypes.wintypes.DWORD),
+ ("dwFileFlagsMask", ctypes.wintypes.DWORD),
+ ("dwFileFlags", ctypes.wintypes.DWORD),
+ ("dwFileOS", ctypes.wintypes.DWORD),
+ ("dwFileType", ctypes.wintypes.DWORD),
+ ("dwFileSubtype", ctypes.wintypes.DWORD),
+ ("dwFileDateMS", ctypes.wintypes.DWORD),
+ ("dwFileDateLS", ctypes.wintypes.DWORD),
]
- kernel32 = WinDLL('kernel32')
- version = WinDLL('version')
+ P_VS_FIXEDFILEINFO = ctypes.POINTER(VS_FIXEDFILEINFO)
+
+def _get_real_winver(maj, min, build):
+ if maj < 6 or (maj == 6 and min < 2):
+ return maj, min, build
+ kernel32 = ctypes.WinDLL('kernel32')
# We will immediately double the length up to MAX_PATH, but the
# path may be longer, so we retry until the returned string is
# shorter than our buffer.
name_len = actual_len = 130
while actual_len == name_len:
name_len *= 2
- name = create_unicode_buffer(name_len)
- actual_len = kernel32.GetModuleFileNameW(HANDLE(kernel32._handle),
- name, len(name))
+ name = ctypes.create_unicode_buffer(name_len)
+ actual_len = kernel32.GetModuleFileNameW(
+ ctypes.wintypes.HANDLE(kernel32._handle),
+ name, len(name)
+ )
if not actual_len:
return maj, min, build
+ version = ctypes.WinDLL('version')
size = version.GetFileVersionInfoSizeW(name, None)
if not size:
return maj, min, build
- ver_block = c_buffer(size)
+ ver_block = ctypes.c_buffer(size)
if (not version.GetFileVersionInfoW(name, None, size, ver_block) or
not ver_block):
return maj, min, build
- pvi = POINTER(VS_FIXEDFILEINFO)()
- if not version.VerQueryValueW(ver_block, "", byref(pvi), byref(DWORD())):
+ pvi = P_VS_FIXEDFILEINFO()
+ if not version.VerQueryValueW(ver_block, "",
+ ctypes.byref(pvi), ctypes.byref(ctypes.wintypes.DWORD())):
return maj, min, build
maj = pvi.contents.dwProductVersionMS >> 16
diff --git a/Misc/NEWS b/Misc/NEWS
index c1420e1..fcc27ba 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -65,6 +65,8 @@ Core and Builtins
Library
-------
+- Issue #27932: Fixes memory leak in platform.win32_ver()
+
- Issue #14977: mailcap now respects the order of the lines in the mailcap
files ("first match"), as required by RFC 1542. Patch by Michael Lazar.