gh-145307: Defer loading psapi.dll until ctypes.util.dllist() is called. (GH-145308)

This commit is contained in:
Steve Dower 2026-03-02 16:10:15 +00:00 committed by GitHub
parent 201d251567
commit 1cf5abedeb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 10 deletions

View file

@ -85,15 +85,10 @@ def find_library(name):
wintypes.DWORD,
)
_psapi = ctypes.WinDLL('psapi', use_last_error=True)
_enum_process_modules = _psapi["EnumProcessModules"]
_enum_process_modules.restype = wintypes.BOOL
_enum_process_modules.argtypes = (
wintypes.HANDLE,
ctypes.POINTER(wintypes.HMODULE),
wintypes.DWORD,
wintypes.LPDWORD,
)
# gh-145307: We defer loading psapi.dll until _get_module_handles is called.
# Loading additional DLLs at startup for functionality that may never be
# used is wasteful.
_enum_process_modules = None
def _get_module_filename(module: wintypes.HMODULE):
name = (wintypes.WCHAR * 32767)() # UNICODE_STRING_MAX_CHARS
@ -101,8 +96,19 @@ def _get_module_filename(module: wintypes.HMODULE):
return name.value
return None
def _get_module_handles():
global _enum_process_modules
if _enum_process_modules is None:
_psapi = ctypes.WinDLL('psapi', use_last_error=True)
_enum_process_modules = _psapi["EnumProcessModules"]
_enum_process_modules.restype = wintypes.BOOL
_enum_process_modules.argtypes = (
wintypes.HANDLE,
ctypes.POINTER(wintypes.HMODULE),
wintypes.DWORD,
wintypes.LPDWORD,
)
process = _get_current_process()
space_needed = wintypes.DWORD()
n = 1024

View file

@ -0,0 +1,2 @@
Defers loading of the ``psapi.dll`` module until it is used by
:func:`ctypes.util.dllist`.