Skip to content

Commit 9a7ba58

Browse files
committed
[exceptions] getting closer, but not there yet.
1 parent 3a5dd49 commit 9a7ba58

1 file changed

Lines changed: 20 additions & 25 deletions

File tree

src/pyMOOS.cpp

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,14 @@ PYBIND11_MAKE_OPAQUE(CommsStatusVector);
1818

1919
namespace py = pybind11;
2020

21-
struct pyMOOSException : std::exception {
22-
pyMOOSException() {
23-
}
24-
;
25-
virtual ~pyMOOSException() throw () {
26-
}
27-
;
28-
29-
pyMOOSException(const std::string & s) :
30-
s_(s) {
31-
}
32-
char const* what() const throw () {
33-
return s_.c_str();
34-
}
35-
std::string s_;
21+
class pyMOOSException : public std::exception {
22+
public:
23+
explicit pyMOOSException(const char * m) : message{m} {}
24+
virtual const char * what() const noexcept override {return message.c_str();}
25+
private:
26+
std::string message = "";
3627
};
3728

38-
void MOOSExceptionTranslator(const pyMOOSException & e) {
39-
// Use the Python 'C' API to set up an exception object
40-
PyErr_SetString(PyExc_RuntimeError, e.what());
41-
}
42-
4329
namespace MOOS {
4430

4531
/** this is a class which wraps MOOS::MOOSAsyncCommClient to provide
@@ -115,7 +101,7 @@ class AsyncCommsWrapper : public MOOS::MOOSAsyncCommClient {
115101
} catch (const py::error_already_set& e) {
116102
PyGILState_Release(gstate);
117103
throw pyMOOSException(
118-
"OnConnect:: caught an exception thrown in python callback");
104+
"OnConnect:: caught an exception thrown in python callback");
119105
}
120106

121107
PyGILState_Release(gstate);
@@ -148,7 +134,7 @@ class AsyncCommsWrapper : public MOOS::MOOSAsyncCommClient {
148134
} catch (const py::error_already_set& e) {
149135
PyGILState_Release(gstate);
150136
throw pyMOOSException(
151-
"OnMail:: caught an exception thrown in python callback");
137+
"OnMail:: caught an exception thrown in python callback");
152138
}
153139

154140
PyGILState_Release(gstate);
@@ -180,7 +166,7 @@ class AsyncCommsWrapper : public MOOS::MOOSAsyncCommClient {
180166
} catch (const py::error_already_set& e) {
181167
PyGILState_Release(gstate);
182168
throw pyMOOSException(
183-
"ActiveQueue:: caught an exception thrown in python callback");
169+
"ActiveQueue:: caught an exception thrown in python callback");
184170
}
185171

186172
PyGILState_Release(gstate);
@@ -544,8 +530,17 @@ PYBIND11_PLUGIN(pymoos)
544530
"Return the current time warp factor.");
545531

546532
// TODO: double check that it's still needed
547-
// py::register_exception_translator<pyMOOSException>(&MOOSExceptionTranslator);
548-
// py::register_exception<pyMOOSException>(m, "MOOSExcep");
533+
static py::exception<pyMOOSException> ex(m, "pyMOOSException");
534+
py::register_exception_translator([](std::exception_ptr p) {
535+
try {
536+
if (p) std::rethrow_exception(p);
537+
} catch (const pyMOOSException &e) {
538+
// Set pyMOOSException as the active python error
539+
// ex(e.what());
540+
PyErr_SetString(PyExc_RuntimeError, e.what());
541+
}
542+
});
543+
549544

550545
return m.ptr();
551546
}

0 commit comments

Comments
 (0)