Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pymoos: fix exceptions so the trace is returned to python #10

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
platforms: all

- name: Install cibuildwheel
run: python -m pip install cibuildwheel==1.8.0
run: python -m pip install cibuildwheel==1.10.0

- name: Checkout MOOS source
uses: actions/checkout@v2
Expand Down Expand Up @@ -108,6 +108,7 @@ jobs:
CIBW_MANYLINUX_I686_IMAGE: manylinux2014
CIBW_MANYLINUX_AARCH64_IMAGE: manylinux2014
CIBW_ARCHS_LINUX: "auto aarch64"
CIBW_BUILD: cp39-manylinux_x86_64
CIBW_SKIP: cp27-*
CIBW_BEFORE_ALL: |
mkdir moosbuild;
Expand Down Expand Up @@ -153,7 +154,7 @@ jobs:
# - uses: actions/setup-python@v2

# - name: Install cibuildwheel
# run: python -m pip install cibuildwheel==1.6.3
# run: python -m pip install cibuildwheel==1.10.0

# - uses: ilammy/msvc-dev-cmd@v1

Expand Down
56 changes: 16 additions & 40 deletions src/pyMOOS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ PYBIND11_MAKE_OPAQUE(CommsStatusVector);

namespace py = pybind11;

class pyMOOSException : public std::exception {
public:
explicit pyMOOSException(const char * m) : message{m} {}
virtual const char * what() const noexcept override {return message.c_str();}
private:
std::string message = "";
};

namespace MOOS {

/** this is a class which wraps MOOS::MOOSAsyncCommClient to provide
Expand Down Expand Up @@ -79,11 +71,11 @@ class AsyncCommsWrapper : public MOOS::MOOSAsyncCommClient {
bool bResult = false;

Py_BEGIN_ALLOW_THREADS
// PyGILState_STATE gstate = PyGILState_Ensure();
// py::gil_scoped_acquire acquire;
closing_ = true;
bResult = BASE::Close(true);

// PyGILState_Release(gstate);
// py::gil_scoped_release release;
Py_END_ALLOW_THREADS

return bResult;
Expand All @@ -94,17 +86,16 @@ class AsyncCommsWrapper : public MOOS::MOOSAsyncCommClient {

bool bResult = false;

PyGILState_STATE gstate = PyGILState_Ensure();
py::gil_scoped_acquire acquire;
try {
py::object result = on_connect_object_();
bResult = py::bool_(result);
} catch (const py::error_already_set& e) {
PyGILState_Release(gstate);
throw pyMOOSException(
"OnConnect:: caught an exception thrown in python callback");
py::gil_scoped_release release;
throw;
}

PyGILState_Release(gstate);
py::gil_scoped_release release;

return bResult;

Expand All @@ -125,19 +116,18 @@ class AsyncCommsWrapper : public MOOS::MOOSAsyncCommClient {
bool on_mail() {
bool bResult = false;

PyGILState_STATE gstate = PyGILState_Ensure();
py::gil_scoped_acquire acquire;
try {
if(!closing_){
py::object result = on_mail_object_();
bResult = py::bool_(result);
}
} catch (const py::error_already_set& e) {
PyGILState_Release(gstate);
throw pyMOOSException(
"OnMail:: caught an exception thrown in python callback");
py::gil_scoped_release release;
throw;
}

PyGILState_Release(gstate);
py::gil_scoped_release release;

return bResult;
}
Expand All @@ -158,19 +148,17 @@ class AsyncCommsWrapper : public MOOS::MOOSAsyncCommClient {
}

bool bResult = false;
py::gil_scoped_acquire acquire;

PyGILState_STATE gstate = PyGILState_Ensure();
try {
py::object result = q->second->func_(M);
bResult = py::bool_(result);
} catch (const py::error_already_set& e) {
PyGILState_Release(gstate);
throw pyMOOSException(
"ActiveQueue:: caught an exception thrown in python callback");
py::print("ActiveQueue:: caught an exception thrown in python callback");
py::gil_scoped_release release;
throw;
}

PyGILState_Release(gstate);

return bResult;

}
Expand All @@ -184,7 +172,7 @@ class AsyncCommsWrapper : public MOOS::MOOSAsyncCommClient {
maq->queue_name_ = sQueueName;
maq->func_ = func;

std::cerr << "adding active queue OK\n";
// std::cerr << "adding active queue OK\n";

active_queue_details_[sQueueName] = maq;
return BASE::AddActiveQueue(sQueueName, active_queue_delegate, maq);
Expand Down Expand Up @@ -518,24 +506,12 @@ PYBIND11_MODULE(pymoos, m) {
"unified time. Of course, if your process isn't using MOOSComms"
"at all, this function works just fine and returns the "
"unadulterated time as you would expect.",
py::arg("apply_timewartping") = true);
py::arg("apply_timewarping") = true);
m.def("is_little_end_in", &IsLittleEndian,
"Return True if current machine is little end in.");
m.def("set_moos_timewarp", &SetMOOSTimeWarp,
"Set the rate at which time is accelerated.",
py::arg("warp"));
m.def("get_moos_timewarp", &GetMOOSTimeWarp,
"Return the current time warp factor.");

// TODO: double check that it's still needed
static py::exception<pyMOOSException> ex(m, "pyMOOSException");
py::register_exception_translator([](std::exception_ptr p) {
try {
if (p) std::rethrow_exception(p);
} catch (const pyMOOSException &e) {
// Set pyMOOSException as the active python error
// ex(e.what());
PyErr_SetString(PyExc_RuntimeError, e.what());
}
});
}