-
Notifications
You must be signed in to change notification settings - Fork 214
Support LoadedDL.from_distribution
#1049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
599ef39
b5d53c0
8746a22
bf44cdf
95d913c
9c5a059
7557d50
711179f
96574e5
936cd20
4f1abfd
a969731
8df1369
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,7 +138,9 @@ def check_if_already_loaded_from_elsewhere(libname: str, _have_abs_path: bool) - | |
except OSError: | ||
continue | ||
else: | ||
return LoadedDL(abs_path_for_dynamic_library(libname, handle), True, handle._handle) | ||
return LoadedDL( | ||
abs_path_for_dynamic_library(libname, handle), True, handle._handle, "already-loaded-from-elsewhere" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please add But I think it's great to use minus signs instead of underscores, as you do. |
||
) | ||
return None | ||
|
||
|
||
|
@@ -170,7 +172,7 @@ def load_with_system_search(libname: str) -> Optional[LoadedDL]: | |
abs_path = abs_path_for_dynamic_library(libname, handle) | ||
if abs_path is None: | ||
raise RuntimeError(f"No expected symbol for {libname=!r}") | ||
return LoadedDL(abs_path, False, handle._handle) | ||
return LoadedDL(abs_path, False, handle._handle, "system-search") | ||
return None | ||
|
||
|
||
|
@@ -193,7 +195,7 @@ def _work_around_known_bugs(libname: str, found_path: str) -> None: | |
ctypes.CDLL(dep_path, CDLL_MODE) | ||
|
||
|
||
def load_with_abs_path(libname: str, found_path: str) -> LoadedDL: | ||
def load_with_abs_path(libname: str, found_path: str, found_via: Optional[str] = None) -> LoadedDL: | ||
"""Load a dynamic library from the given path. | ||
|
||
Args: | ||
|
@@ -211,4 +213,4 @@ def load_with_abs_path(libname: str, found_path: str) -> LoadedDL: | |
handle = _load_lib(libname, found_path) | ||
except OSError as e: | ||
raise RuntimeError(f"Failed to dlopen {found_path}: {e}") from e | ||
return LoadedDL(found_path, False, handle._handle) | ||
return LoadedDL(found_path, False, handle._handle, found_via) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,7 +100,10 @@ def abs_path_for_dynamic_library(libname: str, handle: ctypes.wintypes.HMODULE) | |
return buffer.value | ||
|
||
|
||
def check_if_already_loaded_from_elsewhere(libname: str, have_abs_path: bool) -> Optional[LoadedDL]: | ||
def check_if_already_loaded_from_elsewhere( | ||
libname: str, | ||
have_abs_path: bool, | ||
) -> Optional[LoadedDL]: | ||
for dll_name in SUPPORTED_WINDOWS_DLLS.get(libname, ()): | ||
handle = kernel32.GetModuleHandleW(dll_name) | ||
if handle: | ||
|
@@ -110,7 +113,7 @@ def check_if_already_loaded_from_elsewhere(libname: str, have_abs_path: bool) -> | |
# load_with_abs_path(). To make the side-effect more deterministic, | ||
# activate it even if the library was already loaded from elsewhere. | ||
add_dll_directory(abs_path) | ||
return LoadedDL(abs_path, True, ctypes_handle_to_unsigned_int(handle)) | ||
return LoadedDL(abs_path, True, ctypes_handle_to_unsigned_int(handle), "already-loaded-from-elsewhere") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return None | ||
|
||
|
||
|
@@ -128,12 +131,12 @@ def load_with_system_search(libname: str) -> Optional[LoadedDL]: | |
handle = kernel32.LoadLibraryExW(dll_name, None, 0) | ||
if handle: | ||
abs_path = abs_path_for_dynamic_library(libname, handle) | ||
return LoadedDL(abs_path, False, ctypes_handle_to_unsigned_int(handle)) | ||
return LoadedDL(abs_path, False, ctypes_handle_to_unsigned_int(handle), "system-search") | ||
|
||
return None | ||
|
||
|
||
def load_with_abs_path(libname: str, found_path: str) -> LoadedDL: | ||
def load_with_abs_path(libname: str, found_path: str, found_via: Optional[str] = None) -> LoadedDL: | ||
"""Load a dynamic library from the given path. | ||
|
||
Args: | ||
|
@@ -156,4 +159,4 @@ def load_with_abs_path(libname: str, found_path: str) -> LoadedDL: | |
error_code = ctypes.GetLastError() # type: ignore[attr-defined] | ||
raise RuntimeError(f"Failed to load DLL at {found_path}: Windows error {error_code}") | ||
|
||
return LoadedDL(found_path, False, ctypes_handle_to_unsigned_int(handle)) | ||
return LoadedDL(found_path, False, ctypes_handle_to_unsigned_int(handle), found_via) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,8 +26,13 @@ | |
def _load_lib_no_cache(libname: str) -> LoadedDL: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice to move this below the Also, this could be a one-liner:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suspect it'll be simpler (less code) to wrap this function in a helper that adds
Alternatively, we could pass |
||
finder = _FindNvidiaDynamicLib(libname) | ||
abs_path = finder.try_site_packages() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor but would be nice: keep the
|
||
|
||
if abs_path is None: | ||
abs_path = finder.try_with_conda_prefix() | ||
if abs_path is not None: | ||
found_via = "conda" | ||
else: | ||
found_via = "site-packages" | ||
|
||
# If the library was already loaded by someone else, reproduce any OS-specific | ||
# side-effects we would have applied on a direct absolute-path load (e.g., | ||
|
@@ -49,8 +54,10 @@ def _load_lib_no_cache(libname: str) -> LoadedDL: | |
abs_path = finder.try_with_cuda_home() | ||
if abs_path is None: | ||
finder.raise_not_found_error() | ||
else: | ||
found_via = "CUDA_HOME" | ||
|
||
return load_with_abs_path(libname, abs_path) | ||
return load_with_abs_path(libname, abs_path, found_via) | ||
|
||
|
||
@functools.cache | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two trivial independent change here would be great:
found_via
with underscore. I think you only have to change this and the one referencechild_load_nvidia_dynamic_lib_helper.py
pre-commit run --all-files
still passes after removingOptional[]
here? That'd be a promise that we're always populating the field.