Skip to content

Commit

Permalink
Remove workarounds for Windows CE in find_memleak. (enthought#791)
Browse files Browse the repository at this point in the history
* 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`.
  • Loading branch information
junkmd authored Feb 2, 2025
1 parent 1a66805 commit 4385504
Showing 1 changed file with 36 additions and 46 deletions.
82 changes: 36 additions & 46 deletions comtypes/test/find_memleak.py
Original file line number Diff line number Diff line change
@@ -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

################################################################

Expand Down Expand Up @@ -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)

0 comments on commit 4385504

Please sign in to comment.