From d6fb5c09f0fc887ef1172e6cdefd9c7b9e58bf57 Mon Sep 17 00:00:00 2001 From: Benjamin Gilbert Date: Sun, 27 Oct 2024 14:39:02 -0700 Subject: [PATCH] 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 Signed-off-by: Benjamin Gilbert --- openslide/lowlevel.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/openslide/lowlevel.py b/openslide/lowlevel.py index 7adb0dbe..11ccc575 100644 --- a/openslide/lowlevel.py +++ b/openslide/lowlevel.py @@ -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: