Skip to content

Commit a16fc6f

Browse files
DiamonDinoiajanden
authored andcommitted
fix-707
1 parent ba68088 commit a16fc6f

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

python/cufinufft/cufinufft/_cufinufft.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,34 @@
3636
log_level = logging.root.level
3737

3838
lib = None
39-
# Try to load the library as installed in the Python package.
39+
# Try to load the library as installed in the Python package
4040
path = pathlib.Path(__file__).parent.resolve()
4141
library_names = ["libcufinufft", "cufinufft"]
42+
43+
if reset_log_level:
44+
logging.root.setLevel(log_level)
45+
46+
# First attempt: try from package directory
4247
for lib_name in library_names:
4348
try:
4449
lib = np.ctypeslib.load_library(lib_name, path)
4550
break
46-
except OSError:
47-
# Paranoid, in case lib is set to something and then an exception is thrown
48-
lib = None
49-
50-
if reset_log_level:
51-
logging.root.setLevel(log_level)
51+
except (OSError, AttributeError):
52+
pass
5253

54+
# Second attempt: try from system path
5355
if lib is None:
54-
# If that fails, try to load the library from the system path.
5556
libname = find_library('cufinufft')
56-
if libname is not None:
57+
if libname is None:
58+
raise ImportError("Could not find cufinufft library in system path")
59+
try:
5760
lib = ctypes.cdll.LoadLibrary(libname)
58-
# we probably should add a version check and trow a warning if the version is different
59-
else:
60-
# if that does not work, cufinufft is not installed correctly.
61-
raise ImportError("Failed to find a suitable cufinufft library.")
61+
except (OSError, AttributeError) as e:
62+
raise ImportError(f"Found cufinufft library at {libname}, but failed to load it: {e}")
6263

64+
# Safety check - if somehow we still don't have a library
65+
if lib is None:
66+
raise ImportError("Failed to load cufinufft library")
6367

6468
def _get_NufftOpts():
6569
fields = [

0 commit comments

Comments
 (0)