Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6a294a5

Browse files
committed
Issue #27932: Fixes memory leak in platform.win32_ver()
1 parent 8dcc48e commit 6a294a5

2 files changed

Lines changed: 35 additions & 29 deletions

File tree

Lib/platform.py

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

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

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

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

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

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

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

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

554558
maj = pvi.contents.dwProductVersionMS >> 16

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ Core and Builtins
6565
Library
6666
-------
6767

68+
- Issue #27932: Fixes memory leak in platform.win32_ver()
69+
6870
- Issue #14977: mailcap now respects the order of the lines in the mailcap
6971
files ("first match"), as required by RFC 1542. Patch by Michael Lazar.
7072

0 commit comments

Comments
 (0)