Skip to content

Commit b9f66be

Browse files
committed
Strict wheel, conda, system rule: try using the platform-specific dynamic loader search mechanisms only last
1 parent 97a95c4 commit b9f66be

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

cuda_bindings/cuda/bindings/_path_finder/load_nvidia_dynamic_library.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
_LINUX_CDLL_MODE = os.RTLD_NOW | os.RTLD_GLOBAL
2323

24-
from .find_nvidia_dynamic_library import find_nvidia_dynamic_library
24+
from .find_nvidia_dynamic_library import _find_nvidia_dynamic_library
2525
from .supported_libs import DIRECT_DEPENDENCIES, SUPPORTED_WINDOWS_DLLS
2626

2727

@@ -63,38 +63,39 @@ def _windows_load_with_dll_basename(name: str) -> int:
6363

6464

6565
@functools.cache
66-
def load_nvidia_dynamic_library(name: str) -> int:
67-
for dep in DIRECT_DEPENDENCIES.get(name, ()):
66+
def load_nvidia_dynamic_library(libname: str) -> int:
67+
for dep in DIRECT_DEPENDENCIES.get(libname, ()):
6868
load_nvidia_dynamic_library(dep)
6969

70-
# First try using the platform-specific dynamic loader search mechanisms
71-
if sys.platform == "win32":
72-
handle = _windows_load_with_dll_basename(name)
73-
if handle:
74-
return handle
75-
else:
76-
dl_path = f"lib{name}.so" # Version intentionally no specified.
77-
try:
78-
handle = ctypes.CDLL(dl_path, _LINUX_CDLL_MODE)
79-
except OSError:
80-
pass
70+
found = _find_nvidia_dynamic_library(libname)
71+
if found.abs_path is None:
72+
if sys.platform == "win32":
73+
handle = _windows_load_with_dll_basename(libname)
74+
if handle:
75+
# Use `cdef void* ptr = <void*><intptr_t>` in cython to convert back to void*
76+
return handle
8177
else:
82-
# Use `cdef void* ptr = <void*><uintptr_t>` in cython to convert back to void*
83-
return handle._handle # C unsigned int
78+
try:
79+
handle = ctypes.CDLL(found.lib_searched_for, _LINUX_CDLL_MODE)
80+
except OSError:
81+
pass
82+
else:
83+
# Use `cdef void* ptr = <void*><uintptr_t>` in cython to convert back to void*
84+
return handle._handle # C unsigned int
85+
found.raise_if_abs_path_is_None()
8486

85-
dl_path = find_nvidia_dynamic_library(name)
8687
if sys.platform == "win32":
8788
flags = _WINBASE_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | _WINBASE_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR
8889
try:
89-
handle = win32api.LoadLibraryEx(dl_path, 0, flags)
90+
handle = win32api.LoadLibraryEx(found.abs_path, 0, flags)
9091
except pywintypes.error as e:
91-
raise RuntimeError(f"Failed to load DLL at {dl_path}: {e}") from e
92+
raise RuntimeError(f"Failed to load DLL at {found.abs_path}: {e}") from e
9293
# Use `cdef void* ptr = <void*><intptr_t>` in cython to convert back to void*
9394
return handle # C signed int, matches win32api.GetProcAddress
9495
else:
9596
try:
96-
handle = ctypes.CDLL(dl_path, _LINUX_CDLL_MODE)
97+
handle = ctypes.CDLL(found.abs_path, _LINUX_CDLL_MODE)
9798
except OSError as e:
98-
raise RuntimeError(f"Failed to dlopen {dl_path}: {e}") from e
99+
raise RuntimeError(f"Failed to dlopen {found.abs_path}: {e}") from e
99100
# Use `cdef void* ptr = <void*><uintptr_t>` in cython to convert back to void*
100101
return handle._handle # C unsigned int

0 commit comments

Comments
 (0)