-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better handling of errors during dynamic catalogue loading (#1702)
* Add platform abstraction for loading DLL/SOs. * Throw more informative exceptions when handling DLL/SOs. * More informative errors when dealing with dynamically loaded catalogues. * Translate `arb::file_not_found_error` to `FileNotFoundError` in Python.
- Loading branch information
1 parent
76b78e4
commit 71b9515
Showing
9 changed files
with
92 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#pragma once | ||
|
||
#ifdef ARB_HAVE_DL | ||
#include "util/dl_platform_posix.hpp" | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#pragma once | ||
|
||
#include <fstream> | ||
#include <string> | ||
|
||
#include <dlfcn.h> | ||
|
||
#include <arbor/arbexcept.hpp> | ||
|
||
#include "util/strprintf.hpp" | ||
|
||
namespace arb { | ||
|
||
struct dl_error: arbor_exception { | ||
dl_error(const std::string& msg): arbor_exception{msg} {} | ||
}; | ||
|
||
struct dl_handle { | ||
void* dl = nullptr; | ||
}; | ||
|
||
inline | ||
dl_handle dl_open(const std::string& fn) { | ||
// Test if we can open file | ||
if (!std::ifstream{fn.c_str()}.good()) throw file_not_found_error{fn}; | ||
// Call once to clear errors not caused by us | ||
dlerror(); | ||
// Try to open shared object | ||
auto result = dlopen(fn.c_str(), RTLD_LAZY); | ||
// dlopen fails by returning NULL | ||
if (!result) throw dl_error{util::pprintf("[POSIX] dl_open failed with: {}", dlerror())}; | ||
return {result}; | ||
} | ||
|
||
template<typename T> inline | ||
T dl_get_symbol(const dl_handle& handle, const std::string& symbol) { | ||
// Call once to clear errors not caused by us | ||
dlerror(); | ||
// Get symbol from shared object, may return NULL if that is what symbol refers to | ||
auto result = dlsym(handle.dl, symbol.c_str()); | ||
// ... so we can only ask for dlerror here | ||
if (auto error = dlerror()) throw dl_error{util::pprintf("[POSIX] dl_get_symbol failed with: {}", error)}; | ||
return reinterpret_cast<T>(result); | ||
} | ||
|
||
inline | ||
void dl_close(dl_handle& handle) { | ||
dlclose(handle.dl); | ||
handle.dl = nullptr; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters