@@ -72,30 +72,32 @@ def _load_library() -> CDLL:
72
72
pass
73
73
74
74
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
83
85
84
86
if platform .system () == 'Windows' :
85
87
try :
86
88
return try_load (['libopenslide-1.dll' , 'libopenslide-0.dll' ])
87
- except FileNotFoundError :
89
+ except FileNotFoundError as exc :
88
90
raise ModuleNotFoundError (
89
91
"Couldn't locate OpenSlide DLL. "
90
92
"Try `pip install openslide-bin`, "
91
93
"or if you're using an OpenSlide binary package, "
92
94
"ensure you've called os.add_dll_directory(). "
93
95
"https://openslide.org/api/python/#installing"
94
- )
96
+ ) from exc
95
97
elif platform .system () == 'Darwin' :
96
98
try :
97
99
return try_load (['libopenslide.1.dylib' , 'libopenslide.0.dylib' ])
98
- except OSError :
100
+ except OSError as exc :
99
101
# MacPorts doesn't add itself to the dyld search path, but
100
102
# does add itself to the find_library() search path
101
103
# (DEFAULT_LIBRARY_FALLBACK in ctypes.macholib.dyld).
@@ -107,17 +109,17 @@ def try_load(names: list[str]) -> CDLL:
107
109
"Couldn't locate OpenSlide dylib. "
108
110
"Try `pip install openslide-bin`. "
109
111
"https://openslide.org/api/python/#installing"
110
- )
112
+ ) from exc
111
113
return cdll .LoadLibrary (lib )
112
114
else :
113
115
try :
114
116
return try_load (['libopenslide.so.1' , 'libopenslide.so.0' ])
115
- except OSError :
117
+ except OSError as exc :
116
118
raise ModuleNotFoundError (
117
119
"Couldn't locate OpenSlide shared library. "
118
120
"Try `pip install openslide-bin`. "
119
121
"https://openslide.org/api/python/#installing"
120
- )
122
+ ) from exc
121
123
122
124
123
125
_lib = _load_library ()
0 commit comments