Skip to content

Commit

Permalink
Throw meep errors as Python exceptions (NanoComp#953)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherHogan authored and stevengj committed Jul 8, 2019
1 parent 3d145b3 commit 69ca241
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
9 changes: 9 additions & 0 deletions python/meep.i
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,15 @@ meep::volume_list *make_volume_list(const meep::volume &v, int c,
double *total
};

%exception {
try {
$action
} catch (std::runtime_error &e) {
PyErr_SetString(PyExc_RuntimeError, e.what());
SWIG_fail;
}
}

// Tells Python to take ownership of the h5file* this function returns so that
// it gets garbage collected and the file gets closed.
%newobject meep::fields::open_h5file;
Expand Down
14 changes: 10 additions & 4 deletions src/mympi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,20 @@ double wall_time(void) {
void abort(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "meep: ");
vfprintf(stderr, fmt, ap);
char *s;
vasprintf(&s, fmt, ap);
va_end(ap);
if (fmt[strlen(fmt) - 1] != '\n') fputc('\n', stderr); // force newline
// Make a std::string to support older compilers (std::runtime_error(char *) was added in C++11)
std::string error_msg(s);
free(s);
if (count_processors() == 1) {
throw runtime_error("meep: " + error_msg);
}
#ifdef HAVE_MPI
fprintf(stderr, "meep: %s", error_msg.c_str());
if (fmt[strlen(fmt) - 1] != '\n') fputc('\n', stderr); // force newline
MPI_Abort(MPI_COMM_WORLD, 1);
#endif
exit(1);
}

void send(int from, int to, double *data, int size) {
Expand Down

0 comments on commit 69ca241

Please sign in to comment.