|
From: Ben W. <be...@sa...> - 2010-04-23 21:44:10
|
On 04/23/2010 01:48 PM, Bob Hood wrote:
> I'm causing a number of Swig::DirectorExceptions to be thrown while
> developing my SWIG wrapper.
...
> I can't seem to find a good "hook" point to trap these.
What we do is use the director:except feature to convert Python
exceptions in director methods to C++ exceptions. These will then
terminate the C++ method being called by SWIG; we catch these with
%exception, and then fail the SWIG wrapper so the original Python
exception is raised in the Python script. This works reasonably well for us:
%{
static void handle_exception(void) {
try {
throw;
} catch (std::out_of_range &e) {
PyErr_SetString(our_index_exception, e.what());
} etc.
}
%}
%exception {
try {
$action
} catch (...) {
# Note that if a director method failed, the Python error indicator
# already contains full details of the exception, and it will be
# reraised when we go to SWIG_fail; so no need to convert the C++
# exception back to a Python one
if (!PyErr_Occurred()) {
handle_exception();
}
SWIG_fail;
}
}
%feature("director:except") {
if ($error != NULL) {
throw Swig::DirectorMethodException();
}
}
Ben
--
be...@sa... http://salilab.org/~ben/
"It is a capital mistake to theorize before one has data."
- Sir Arthur Conan Doyle
|