Skip to content

Commit c62d6da

Browse files
authored
Merge pull request #199 from bgilbert/soversion
lowlevel: support `libopenslide.so.1` soname
2 parents d986427 + e7308c1 commit c62d6da

File tree

1 file changed

+47
-34
lines changed

1 file changed

+47
-34
lines changed

openslide/lowlevel.py

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# openslide-python - Python bindings for the OpenSlide library
33
#
44
# Copyright (c) 2010-2013 Carnegie Mellon University
5-
# Copyright (c) 2016-2021 Benjamin Gilbert
5+
# Copyright (c) 2016-2023 Benjamin Gilbert
66
#
77
# This library is free software; you can redistribute it and/or modify it
88
# under the terms of version 2.1 of the GNU Lesser General Public License
@@ -49,39 +49,52 @@
4949

5050
from . import _convert
5151

52-
if platform.system() == 'Windows':
53-
try:
54-
_lib = cdll.LoadLibrary('libopenslide-0.dll')
55-
except FileNotFoundError:
56-
import os
5752

58-
if hasattr(os, 'add_dll_directory'):
59-
# Python >= 3.8
60-
_admonition = 'Did you call os.add_dll_directory()?'
61-
else:
62-
_admonition = 'Did you add OpenSlide to PATH?'
63-
raise ModuleNotFoundError(
64-
f"Couldn't locate OpenSlide DLL. {_admonition} "
65-
"https://openslide.org/api/python/#installing"
66-
)
67-
elif platform.system() == 'Darwin':
68-
try:
69-
_lib = cdll.LoadLibrary('libopenslide.0.dylib')
70-
except OSError:
71-
# MacPorts doesn't add itself to the dyld search path, but
72-
# does add itself to the find_library() search path
73-
# (DEFAULT_LIBRARY_FALLBACK in ctypes.macholib.dyld).
74-
import ctypes.util
75-
76-
_lib = ctypes.util.find_library('openslide')
77-
if _lib is None:
53+
def _load_library():
54+
def try_load(names):
55+
for name in names:
56+
try:
57+
return cdll.LoadLibrary(name)
58+
except OSError:
59+
if name == names[-1]:
60+
raise
61+
62+
if platform.system() == 'Windows':
63+
try:
64+
return try_load(['libopenslide-1.dll', 'libopenslide-0.dll'])
65+
except FileNotFoundError:
66+
import os
67+
68+
if hasattr(os, 'add_dll_directory'):
69+
# Python >= 3.8
70+
admonition = 'Did you call os.add_dll_directory()?'
71+
else:
72+
admonition = 'Did you add OpenSlide to PATH?'
7873
raise ModuleNotFoundError(
79-
"Couldn't locate OpenSlide dylib. Is OpenSlide installed "
80-
"correctly? https://openslide.org/api/python/#installing"
74+
f"Couldn't locate OpenSlide DLL. {admonition} "
75+
"https://openslide.org/api/python/#installing"
8176
)
82-
_lib = cdll.LoadLibrary(_lib)
83-
else:
84-
_lib = cdll.LoadLibrary('libopenslide.so.0')
77+
elif platform.system() == 'Darwin':
78+
try:
79+
return try_load(['libopenslide.1.dylib', 'libopenslide.0.dylib'])
80+
except OSError:
81+
# MacPorts doesn't add itself to the dyld search path, but
82+
# does add itself to the find_library() search path
83+
# (DEFAULT_LIBRARY_FALLBACK in ctypes.macholib.dyld).
84+
import ctypes.util
85+
86+
lib = ctypes.util.find_library('openslide')
87+
if lib is None:
88+
raise ModuleNotFoundError(
89+
"Couldn't locate OpenSlide dylib. Is OpenSlide installed "
90+
"correctly? https://openslide.org/api/python/#installing"
91+
)
92+
return cdll.LoadLibrary(lib)
93+
else:
94+
return try_load(['libopenslide.so.1', 'libopenslide.so.0'])
95+
96+
97+
_lib = _load_library()
8598

8699

87100
class OpenSlideError(Exception):
@@ -361,17 +374,17 @@ def read_associated_image(slide, name):
361374
c_void_p,
362375
[_size_t],
363376
_check_cache_create,
364-
minimum_version='3.5.0',
377+
minimum_version='4.0.0',
365378
)
366379

367380
set_cache = _func(
368381
'openslide_set_cache',
369382
None,
370383
[_OpenSlide, _OpenSlideCache],
371384
None,
372-
minimum_version='3.5.0',
385+
minimum_version='4.0.0',
373386
)
374387

375388
cache_release = _func(
376-
'openslide_cache_release', None, [_OpenSlideCache], None, minimum_version='3.5.0'
389+
'openslide_cache_release', None, [_OpenSlideCache], None, minimum_version='4.0.0'
377390
)

0 commit comments

Comments
 (0)