|
1 | 1 | # Copyright 2025 NVIDIA Corporation. All rights reserved.
|
2 | 2 | # SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
|
3 | 3 |
|
4 |
| -import ctypes |
5 |
| -import ctypes.wintypes |
6 | 4 | from typing import Optional
|
7 | 5 |
|
8 | 6 | import pywintypes
|
@@ -36,46 +34,11 @@ def add_dll_directory(dll_abs_path: str) -> None:
|
36 | 34 |
|
37 | 35 |
|
38 | 36 | def abs_path_for_dynamic_library(libname: str, handle: pywintypes.HANDLE) -> str:
|
39 |
| - """Get the absolute path of a loaded dynamic library on Windows. |
40 |
| -
|
41 |
| - Args: |
42 |
| - handle: The library handle |
43 |
| -
|
44 |
| - Returns: |
45 |
| - The absolute path to the DLL file |
46 |
| -
|
47 |
| - Raises: |
48 |
| - OSError: If GetModuleFileNameW fails |
49 |
| - RuntimeError: If the required path length is unreasonably long |
50 |
| - """ |
51 |
| - MAX_ITERATIONS = 10 # Allows for extremely long paths (up to ~266,000 chars) |
52 |
| - buf_size = 260 # Start with traditional MAX_PATH |
53 |
| - |
54 |
| - for _ in range(MAX_ITERATIONS): |
55 |
| - buf = ctypes.create_unicode_buffer(buf_size) |
56 |
| - n_chars = ctypes.windll.kernel32.GetModuleFileNameW(ctypes.wintypes.HMODULE(handle), buf, buf_size) |
57 |
| - |
58 |
| - if n_chars == 0: |
59 |
| - raise OSError( |
60 |
| - f"GetModuleFileNameW failed ({libname=!r}, {buf_size=}). " |
61 |
| - "Long paths may require enabling the " |
62 |
| - "Windows 10+ long path registry setting. See: " |
63 |
| - "https://docs.python.org/3/using/windows.html#removing-the-max-path-limitation" |
64 |
| - ) |
65 |
| - if n_chars < buf_size - 1: |
66 |
| - return buf.value |
67 |
| - |
68 |
| - buf_size *= 2 # Double the buffer size and try again |
69 |
| - |
70 |
| - raise RuntimeError( |
71 |
| - f"Failed to retrieve the full path after {MAX_ITERATIONS} attempts " |
72 |
| - f"(final buffer size: {buf_size} characters). " |
73 |
| - "This may indicate:\n" |
74 |
| - " 1. An extremely long path requiring Windows long path support, or\n" |
75 |
| - " 2. An invalid or corrupt library handle, or\n" |
76 |
| - " 3. An unexpected system error.\n" |
77 |
| - "See: https://docs.python.org/3/using/windows.html#removing-the-max-path-limitation" |
78 |
| - ) |
| 37 | + """Get the absolute path of a loaded dynamic library on Windows.""" |
| 38 | + try: |
| 39 | + return win32api.GetModuleFileName(handle) |
| 40 | + except Exception as e: |
| 41 | + raise RuntimeError(f"GetModuleFileName failed for {libname!r} (exception type: {type(e)})") from e |
79 | 42 |
|
80 | 43 |
|
81 | 44 | def check_if_already_loaded_from_elsewhere(libname: str) -> Optional[LoadedDL]:
|
|
0 commit comments