|
2 | 2 | # openslide-python - Python bindings for the OpenSlide library
|
3 | 3 | #
|
4 | 4 | # Copyright (c) 2010-2013 Carnegie Mellon University
|
5 |
| -# Copyright (c) 2016-2021 Benjamin Gilbert |
| 5 | +# Copyright (c) 2016-2023 Benjamin Gilbert |
6 | 6 | #
|
7 | 7 | # This library is free software; you can redistribute it and/or modify it
|
8 | 8 | # under the terms of version 2.1 of the GNU Lesser General Public License
|
|
49 | 49 |
|
50 | 50 | from . import _convert
|
51 | 51 |
|
52 |
| -if platform.system() == 'Windows': |
53 |
| - try: |
54 |
| - _lib = cdll.LoadLibrary('libopenslide-0.dll') |
55 |
| - except FileNotFoundError: |
56 |
| - import os |
57 | 52 |
|
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?' |
78 | 73 | 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" |
81 | 76 | )
|
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() |
85 | 98 |
|
86 | 99 |
|
87 | 100 | class OpenSlideError(Exception):
|
@@ -361,17 +374,17 @@ def read_associated_image(slide, name):
|
361 | 374 | c_void_p,
|
362 | 375 | [_size_t],
|
363 | 376 | _check_cache_create,
|
364 |
| - minimum_version='3.5.0', |
| 377 | + minimum_version='4.0.0', |
365 | 378 | )
|
366 | 379 |
|
367 | 380 | set_cache = _func(
|
368 | 381 | 'openslide_set_cache',
|
369 | 382 | None,
|
370 | 383 | [_OpenSlide, _OpenSlideCache],
|
371 | 384 | None,
|
372 |
| - minimum_version='3.5.0', |
| 385 | + minimum_version='4.0.0', |
373 | 386 | )
|
374 | 387 |
|
375 | 388 | 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' |
377 | 390 | )
|
0 commit comments