Skip to content

Commit d6fb5c0

Browse files
committed
Include all LoadLibrary() failures in exception chain
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]>
1 parent 343d435 commit d6fb5c0

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

openslide/lowlevel.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,16 @@ def _load_library() -> CDLL:
7272
pass
7373

7474
def try_load(names: list[str]) -> CDLL:
75-
for name in names:
76-
try:
77-
return cdll.LoadLibrary(name)
78-
except OSError:
79-
if name == names[-1]:
80-
raise
81-
else:
82-
raise ValueError('No library names specified')
75+
try:
76+
return cdll.LoadLibrary(names[0])
77+
except OSError:
78+
remaining = names[1:]
79+
if remaining:
80+
# handle recursively so implicit exception chaining captures
81+
# all the failures
82+
return try_load(remaining)
83+
else:
84+
raise
8385

8486
if platform.system() == 'Windows':
8587
try:

0 commit comments

Comments
 (0)