Return the loaded shared library object from the ``dll_loc`` location.
(dll_loc, *args)
| 213 | |
| 214 | |
| 215 | def load_shared_library(dll_loc, *args): |
| 216 | """ |
| 217 | Return the loaded shared library object from the ``dll_loc`` location. |
| 218 | """ |
| 219 | if not dll_loc or not path.exists(dll_loc): |
| 220 | raise ImportError(f"Shared library does not exists: dll_loc: {dll_loc}") |
| 221 | |
| 222 | if not isinstance(dll_loc, str): |
| 223 | dll_loc = os.fsdecode(dll_loc) |
| 224 | |
| 225 | lib = None |
| 226 | |
| 227 | dll_dir = os.path.dirname(dll_loc) |
| 228 | try: |
| 229 | with pushd(dll_dir): |
| 230 | lib = ctypes.CDLL(dll_loc) |
| 231 | except OSError as e: |
| 232 | import traceback |
| 233 | from pprint import pformat |
| 234 | |
| 235 | msgs = tuple( |
| 236 | [ |
| 237 | f'ctypes.CDLL("{dll_loc}")', |
| 238 | "os.environ:\n{}".format(pformat(dict(os.environ))), |
| 239 | traceback.format_exc(), |
| 240 | ] |
| 241 | ) |
| 242 | raise Exception(msgs) from e |
| 243 | |
| 244 | if lib and lib._name: |
| 245 | return lib |
| 246 | |
| 247 | raise Exception(f"Failed to load shared library with ctypes: {dll_loc}") |
| 248 | |
| 249 | |
| 250 | @contextlib.contextmanager |