From 868a224cbb355686301178b1d446d7bce9da75f2 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Fri, 27 Feb 2026 10:59:16 +0000 Subject: [PATCH 1/3] gh-145307: Defer loading psapi.dll until ctypes.util.dllist() is called. --- Lib/ctypes/util.py | 24 +++++++++++-------- ...-02-27-10-57-20.gh-issue-145307.ueoT7j.rst | 2 ++ 2 files changed, 16 insertions(+), 10 deletions(-) create mode 100644 Misc/NEWS.d/next/Windows/2026-02-27-10-57-20.gh-issue-145307.ueoT7j.rst diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py index 378f12167c6842..2a01256b7cf2b9 100644 --- a/Lib/ctypes/util.py +++ b/Lib/ctypes/util.py @@ -85,16 +85,6 @@ 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, - ) - def _get_module_filename(module: wintypes.HMODULE): name = (wintypes.WCHAR * 32767)() # UNICODE_STRING_MAX_CHARS if _k32_get_module_file_name(module, name, len(name)): @@ -102,7 +92,21 @@ def _get_module_filename(module: wintypes.HMODULE): return None + _enum_process_modules = 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 diff --git a/Misc/NEWS.d/next/Windows/2026-02-27-10-57-20.gh-issue-145307.ueoT7j.rst b/Misc/NEWS.d/next/Windows/2026-02-27-10-57-20.gh-issue-145307.ueoT7j.rst new file mode 100644 index 00000000000000..6f039197962e10 --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2026-02-27-10-57-20.gh-issue-145307.ueoT7j.rst @@ -0,0 +1,2 @@ +Defers loading of the ``psapi.dll`` module until it is used by +:func:`ctypes.util.dllist`. From 36bc0c35a00f554f923f57bd3c596415e5a21ee2 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Fri, 27 Feb 2026 11:00:48 +0000 Subject: [PATCH 2/3] Better comment and code change --- Lib/ctypes/util.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py index 2a01256b7cf2b9..8e859e1ed82bfd 100644 --- a/Lib/ctypes/util.py +++ b/Lib/ctypes/util.py @@ -85,15 +85,17 @@ def find_library(name): wintypes.DWORD, ) + # gh-145307: We defer loading psapi.dll until _get_module_handles is called. + # Loading additional DLLs at startup for functionality that may never be + # uses is wasteful. + _enum_process_modules = None + def _get_module_filename(module: wintypes.HMODULE): name = (wintypes.WCHAR * 32767)() # UNICODE_STRING_MAX_CHARS if _k32_get_module_file_name(module, name, len(name)): return name.value return None - - _enum_process_modules = None - def _get_module_handles(): global _enum_process_modules if _enum_process_modules is None: From 96a71c9a55b823d3608f4778f7bf2b6bc2376d5f Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Fri, 27 Feb 2026 12:31:16 +0000 Subject: [PATCH 3/3] Comment typo --- Lib/ctypes/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py index 8e859e1ed82bfd..3b21658433b2ed 100644 --- a/Lib/ctypes/util.py +++ b/Lib/ctypes/util.py @@ -87,7 +87,7 @@ def find_library(name): # gh-145307: We defer loading psapi.dll until _get_module_handles is called. # Loading additional DLLs at startup for functionality that may never be - # uses is wasteful. + # used is wasteful. _enum_process_modules = None def _get_module_filename(module: wintypes.HMODULE):