From 23541418cb015686d9ab210c961978b76c86a3f7 Mon Sep 17 00:00:00 2001 From: junkmd Date: Thu, 20 Feb 2025 08:09:37 +0900 Subject: [PATCH] Replace `client._code_cache._get_module_filename` with `typeinfo.GetModuleFileName`. --- comtypes/client/_code_cache.py | 8 +------- comtypes/tools/tlbparser.py | 5 ++--- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/comtypes/client/_code_cache.py b/comtypes/client/_code_cache.py index 8c5ce324..16c9dc68 100644 --- a/comtypes/client/_code_cache.py +++ b/comtypes/client/_code_cache.py @@ -63,7 +63,7 @@ def _find_gen_dir(): elif ftype == "dll": # dll created with py2exe - path = _get_module_filename(sys.frozendllhandle) + path = typeinfo.GetModuleFileName(sys.frozendllhandle, MAX_PATH) base = os.path.splitext(os.path.basename(path))[0] subdir = rf"comtypes_cache\{base}-{pymaj:d}{pymin:d}" basedir = tempfile.gettempdir() @@ -135,12 +135,6 @@ def _is_writeable(path): return os.access(path[0], os.W_OK) -def _get_module_filename(hmodule): - """Call the Windows GetModuleFileName function which determines - the path from a module handle.""" - return typeinfo.GetModuleFileName(hmodule, MAX_PATH) - - def _get_appdata_dir(): """Return the 'file system directory that serves as a common repository for application-specific data' - CSIDL_APPDATA""" diff --git a/comtypes/tools/tlbparser.py b/comtypes/tools/tlbparser.py index a6f5dc87..3d80b12f 100644 --- a/comtypes/tools/tlbparser.py +++ b/comtypes/tools/tlbparser.py @@ -2,6 +2,7 @@ import sys from _ctypes import COMError from ctypes import alignment, c_void_p, sizeof, windll +from ctypes.wintypes import MAX_PATH from typing import Any, Dict, List, Optional, Tuple from comtypes import automation, typeinfo @@ -750,9 +751,7 @@ def get_tlib_filename(tlib: typeinfo.ITypeLib) -> Optional[str]: # workaround Windows 7 bug in QueryPathOfRegTypeLib returning relative path try: dll = windll.LoadLibrary(full_filename) - from comtypes.client._code_cache import _get_module_filename - - full_filename = _get_module_filename(dll._handle) + full_filename = typeinfo.GetModuleFileName(dll._handle, MAX_PATH) del dll except OSError: return None