Skip to content
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

Include all LoadLibrary() failures in exception chain #296

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions openslide/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ def set_cache(self, cache: OpenSlideCache) -> None:
cache: an OpenSlideCache object."""
try:
llcache = cache._openslide_cache
except AttributeError:
raise TypeError('Not a cache object')
except AttributeError as exc:
raise TypeError('Not a cache object') from exc
lowlevel.set_cache(self._osr, llcache)


Expand Down
30 changes: 16 additions & 14 deletions openslide/lowlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,30 +72,32 @@ def _load_library() -> CDLL:
pass

def try_load(names: list[str]) -> CDLL:
for name in names:
try:
return cdll.LoadLibrary(name)
except OSError:
if name == names[-1]:
raise
else:
raise ValueError('No library names specified')
try:
return cdll.LoadLibrary(names[0])
except OSError:
remaining = names[1:]
if remaining:
# handle recursively so implicit exception chaining captures
# all the failures
return try_load(remaining)
else:
raise

if platform.system() == 'Windows':
try:
return try_load(['libopenslide-1.dll', 'libopenslide-0.dll'])
except FileNotFoundError:
except FileNotFoundError as exc:
raise ModuleNotFoundError(
"Couldn't locate OpenSlide DLL. "
"Try `pip install openslide-bin`, "
"or if you're using an OpenSlide binary package, "
"ensure you've called os.add_dll_directory(). "
"https://openslide.org/api/python/#installing"
)
) from exc
elif platform.system() == 'Darwin':
try:
return try_load(['libopenslide.1.dylib', 'libopenslide.0.dylib'])
except OSError:
except OSError as exc:
# MacPorts doesn't add itself to the dyld search path, but
# does add itself to the find_library() search path
# (DEFAULT_LIBRARY_FALLBACK in ctypes.macholib.dyld).
Expand All @@ -107,17 +109,17 @@ def try_load(names: list[str]) -> CDLL:
"Couldn't locate OpenSlide dylib. "
"Try `pip install openslide-bin`. "
"https://openslide.org/api/python/#installing"
)
) from exc
return cdll.LoadLibrary(lib)
else:
try:
return try_load(['libopenslide.so.1', 'libopenslide.so.0'])
except OSError:
except OSError as exc:
raise ModuleNotFoundError(
"Couldn't locate OpenSlide shared library. "
"Try `pip install openslide-bin`. "
"https://openslide.org/api/python/#installing"
)
) from exc


_lib = _load_library()
Expand Down
Loading