From 438550412ea07996315772882a68811fb9be190f Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Sun, 2 Feb 2025 23:16:07 +0900 Subject: [PATCH] Remove workarounds for Windows CE in `find_memleak`. (#791) * Remove workarounds for Windows CE in `test/find_memleak.py`. * Format the import block in `test/find_memleak.py`. * Remove unused imports from `test/find_memleak.py`. * Replace `from ctypes import *` with specific imports in `test/find_memleak.py`. * Replace `from ctypes.wintypes import *` with specific imports in `test/find_memleak.py`. --- comtypes/test/find_memleak.py | 82 +++++++++++++++-------------------- 1 file changed, 36 insertions(+), 46 deletions(-) diff --git a/comtypes/test/find_memleak.py b/comtypes/test/find_memleak.py index 88cf7f07..3908a017 100644 --- a/comtypes/test/find_memleak.py +++ b/comtypes/test/find_memleak.py @@ -1,6 +1,6 @@ -import unittest, gc -from ctypes import * -from ctypes.wintypes import * +import gc +from ctypes import POINTER, Structure, WinDLL, WinError, byref, c_size_t, sizeof +from ctypes.wintypes import BOOL, DWORD, HANDLE ################################################################ @@ -33,50 +33,40 @@ def dump(self): _GetProcessMemoryInfo.argtypes = [HANDLE, POINTER(PROCESS_MEMORY_COUNTERS), DWORD] _GetProcessMemoryInfo.restype = BOOL -try: - _GetProcessMemoryInfo.argtypes = ( - HANDLE, - POINTER(PROCESS_MEMORY_COUNTERS), - DWORD, - ) -except WindowsError: - # cannot search for memory leaks on Windows CE - def find_memleak(func, loops=None): - return 0 -else: +def wss(): + # Return the working set size (memory used by process) + pmi = PROCESS_MEMORY_COUNTERS() + if not _GetProcessMemoryInfo(-1, byref(pmi), sizeof(pmi)): + raise WinError() + return pmi.WorkingSetSize - def wss(): - # Return the working set size (memory used by process) - pmi = PROCESS_MEMORY_COUNTERS() - if not _GetProcessMemoryInfo(-1, byref(pmi), sizeof(pmi)): - raise WinError() - return pmi.WorkingSetSize - LOOPS = 10, 1000 +LOOPS = 10, 1000 - def find_memleak(func, loops=LOOPS): - # call 'func' several times, so that memory consumption - # stabilizes: - for j in range(loops[0]): - for k in range(loops[1]): - func() - gc.collect() - gc.collect() - gc.collect() - bytes = wss() - # call 'func' several times, recording the difference in - # memory consumption before and after the call. Repeat this a - # few times, and return a list containing the memory - # consumption differences. - for j in range(loops[0]): - for k in range(loops[1]): - func() - gc.collect() - gc.collect() - gc.collect() - # return the increased in process size - result = wss() - bytes - # Sometimes the process size did decrease, we do not report leaks - # in this case: - return max(result, 0) + +def find_memleak(func, loops=LOOPS): + # call 'func' several times, so that memory consumption + # stabilizes: + for j in range(loops[0]): + for k in range(loops[1]): + func() + gc.collect() + gc.collect() + gc.collect() + bytes = wss() + # call 'func' several times, recording the difference in + # memory consumption before and after the call. Repeat this a + # few times, and return a list containing the memory + # consumption differences. + for j in range(loops[0]): + for k in range(loops[1]): + func() + gc.collect() + gc.collect() + gc.collect() + # return the increased in process size + result = wss() - bytes + # Sometimes the process size did decrease, we do not report leaks + # in this case: + return max(result, 0)