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

Conversation

bgilbert
Copy link
Member

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; thanks!

Also use explicit exception chaining during any conversion of exception types. This changes Python's exception chain reporting to make it clear that the new exception was not an unrelated failure while handling the original one, but rather the direct cause of it.

This changes Python's exception chain reporting to make it clear that the
new exception was not an unrelated failure while handling the original
one, but rather the direct cause of it.

Signed-off-by: Benjamin Gilbert <[email protected]>
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]>
@openslide-bot
Copy link

DCO signed off ✔️

All commits have been signed off. You have certified to the terms of the Developer Certificate of Origin, version 1.1. In particular, you certify that this contribution has not been developed using information obtained under a non-disclosure agreement or other license terms that forbid you from contributing it under the GNU Lesser General Public License, version 2.1.

@bgilbert
Copy link
Member Author

This now produces exception chains like this:

Traceback (most recent call last):
  File "/home/bgilbert/.local/lib/python3.12/site-packages/openslide/lowlevel.py", line 76, in try_load
    return cdll.LoadLibrary(names[0])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.12/ctypes/__init__.py", line 460, in LoadLibrary
    return self._dlltype(name)
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.12/ctypes/__init__.py", line 379, in __init__
    self._handle = _dlopen(self._name, mode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: libopenslide.so.1: cannot open shared object file: No such file or directory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/bgilbert/.local/lib/python3.12/site-packages/openslide/lowlevel.py", line 116, in _load_library
    return try_load(['libopenslide.so.1', 'libopenslide.so.0'])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bgilbert/.local/lib/python3.12/site-packages/openslide/lowlevel.py", line 82, in try_load
    return try_load(remaining)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/bgilbert/.local/lib/python3.12/site-packages/openslide/lowlevel.py", line 76, in try_load
    return cdll.LoadLibrary(names[0])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.12/ctypes/__init__.py", line 460, in LoadLibrary
    return self._dlltype(name)
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.12/ctypes/__init__.py", line 379, in __init__
    self._handle = _dlopen(self._name, mode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: libopenslide.so.0: cannot open shared object file: No such file or directory

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/bgilbert/.local/lib/python3.12/site-packages/openslide/__init__.py", line 35, in <module>
    from openslide import lowlevel
  File "/home/bgilbert/.local/lib/python3.12/site-packages/openslide/lowlevel.py", line 125, in <module>
    _lib = _load_library()
           ^^^^^^^^^^^^^^^
  File "/home/bgilbert/.local/lib/python3.12/site-packages/openslide/lowlevel.py", line 118, in _load_library
    raise ModuleNotFoundError(
ModuleNotFoundError: Couldn't locate OpenSlide shared library. Try `pip install openslide-bin`. https://openslide.org/api/python/#installing

@bgilbert bgilbert merged commit 9ded242 into openslide:main Oct 28, 2024
52 checks passed
@bgilbert bgilbert deleted the chaining branch October 28, 2024 16:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants