Skip to content

Commit 1588a3b

Browse files
committed
Issue #27932: Fixes memory leak in platform.win32_ver()
2 parents b44aceb + 6a294a5 commit 1588a3b

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

Lib/platform.py

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -497,57 +497,61 @@ def _syscmd_ver(system='', release='', version='',
497497
(6, None): "post2012ServerR2",
498498
}
499499

500-
def _get_real_winver(maj, min, build):
501-
if maj < 6 or (maj == 6 and min < 2):
502-
return maj, min, build
503-
504-
from ctypes import (c_buffer, POINTER, byref, create_unicode_buffer,
505-
Structure, WinDLL)
506-
from ctypes.wintypes import DWORD, HANDLE
500+
if sys.platform == 'win32':
501+
import ctypes
502+
import ctypes.wintypes
507503

508-
class VS_FIXEDFILEINFO(Structure):
504+
class VS_FIXEDFILEINFO(ctypes.Structure):
509505
_fields_ = [
510-
("dwSignature", DWORD),
511-
("dwStrucVersion", DWORD),
512-
("dwFileVersionMS", DWORD),
513-
("dwFileVersionLS", DWORD),
514-
("dwProductVersionMS", DWORD),
515-
("dwProductVersionLS", DWORD),
516-
("dwFileFlagsMask", DWORD),
517-
("dwFileFlags", DWORD),
518-
("dwFileOS", DWORD),
519-
("dwFileType", DWORD),
520-
("dwFileSubtype", DWORD),
521-
("dwFileDateMS", DWORD),
522-
("dwFileDateLS", DWORD),
506+
("dwSignature", ctypes.wintypes.DWORD),
507+
("dwStrucVersion", ctypes.wintypes.DWORD),
508+
("dwFileVersionMS", ctypes.wintypes.DWORD),
509+
("dwFileVersionLS", ctypes.wintypes.DWORD),
510+
("dwProductVersionMS", ctypes.wintypes.DWORD),
511+
("dwProductVersionLS", ctypes.wintypes.DWORD),
512+
("dwFileFlagsMask", ctypes.wintypes.DWORD),
513+
("dwFileFlags", ctypes.wintypes.DWORD),
514+
("dwFileOS", ctypes.wintypes.DWORD),
515+
("dwFileType", ctypes.wintypes.DWORD),
516+
("dwFileSubtype", ctypes.wintypes.DWORD),
517+
("dwFileDateMS", ctypes.wintypes.DWORD),
518+
("dwFileDateLS", ctypes.wintypes.DWORD),
523519
]
524520

525-
kernel32 = WinDLL('kernel32')
526-
version = WinDLL('version')
521+
P_VS_FIXEDFILEINFO = ctypes.POINTER(VS_FIXEDFILEINFO)
522+
523+
def _get_real_winver(maj, min, build):
524+
if maj < 6 or (maj == 6 and min < 2):
525+
return maj, min, build
527526

527+
kernel32 = ctypes.WinDLL('kernel32')
528528
# We will immediately double the length up to MAX_PATH, but the
529529
# path may be longer, so we retry until the returned string is
530530
# shorter than our buffer.
531531
name_len = actual_len = 130
532532
while actual_len == name_len:
533533
name_len *= 2
534-
name = create_unicode_buffer(name_len)
535-
actual_len = kernel32.GetModuleFileNameW(HANDLE(kernel32._handle),
536-
name, len(name))
534+
name = ctypes.create_unicode_buffer(name_len)
535+
actual_len = kernel32.GetModuleFileNameW(
536+
ctypes.wintypes.HANDLE(kernel32._handle),
537+
name, len(name)
538+
)
537539
if not actual_len:
538540
return maj, min, build
539541

542+
version = ctypes.WinDLL('version')
540543
size = version.GetFileVersionInfoSizeW(name, None)
541544
if not size:
542545
return maj, min, build
543546

544-
ver_block = c_buffer(size)
547+
ver_block = ctypes.c_buffer(size)
545548
if (not version.GetFileVersionInfoW(name, None, size, ver_block) or
546549
not ver_block):
547550
return maj, min, build
548551

549-
pvi = POINTER(VS_FIXEDFILEINFO)()
550-
if not version.VerQueryValueW(ver_block, "", byref(pvi), byref(DWORD())):
552+
pvi = P_VS_FIXEDFILEINFO()
553+
if not version.VerQueryValueW(ver_block, "",
554+
ctypes.byref(pvi), ctypes.byref(ctypes.wintypes.DWORD())):
551555
return maj, min, build
552556

553557
maj = pvi.contents.dwProductVersionMS >> 16

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ Core and Builtins
125125
Library
126126
-------
127127

128+
- Issue #27932: Fixes memory leak in platform.win32_ver()
129+
128130
- Issue #14977: mailcap now respects the order of the lines in the mailcap
129131
files ("first match"), as required by RFC 1542. Patch by Michael Lazar.
130132

0 commit comments

Comments
 (0)