Skip to content

Commit

Permalink
Include all LoadLibrary() failures in exception chain
Browse files Browse the repository at this point in the history
If libopenslide.so.1 fails to load, we want to report that exception too,
not just the fact that libopenslide.so.0 doesn't exist.  Make try_load()
recursive rather than iterative, allowing Python's exception chaining to
handle this automatically.

Reported-by: Govinda Kamath <[email protected]>
Signed-off-by: Benjamin Gilbert <[email protected]>
  • Loading branch information
bgilbert committed Oct 27, 2024
1 parent 343d435 commit d6fb5c0
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions openslide/lowlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,16 @@ 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:
Expand Down

0 comments on commit d6fb5c0

Please sign in to comment.