|
36 | 36 | log_level = logging.root.level |
37 | 37 |
|
38 | 38 | 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 |
40 | 40 | path = pathlib.Path(__file__).parent.resolve() |
41 | 41 | library_names = ["libcufinufft", "cufinufft"] |
| 42 | + |
| 43 | +if reset_log_level: |
| 44 | + logging.root.setLevel(log_level) |
| 45 | + |
| 46 | +# First attempt: try from package directory |
42 | 47 | for lib_name in library_names: |
43 | 48 | try: |
44 | 49 | lib = np.ctypeslib.load_library(lib_name, path) |
45 | 50 | 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 |
52 | 53 |
|
| 54 | +# Second attempt: try from system path |
53 | 55 | if lib is None: |
54 | | - # If that fails, try to load the library from the system path. |
55 | 56 | 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: |
57 | 60 | 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}") |
62 | 63 |
|
| 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") |
63 | 67 |
|
64 | 68 | def _get_NufftOpts(): |
65 | 69 | fields = [ |
|
0 commit comments