Alsvinn  0.5.3
The fast FVM simulator with UQ support
handle_pyerror.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <boost/python.hpp>
4 namespace alsfvm {
5 namespace python {
6 
11 #define HANDLE_PYTHON_EXCEPTION \
12  std::string pythonErrorMessage = "none"; \
13  if (PyErr_Occurred()) { \
14  pythonErrorMessage = alsfvm::python::handle_pyerror(); \
15  } \
16  boost::python::handle_exception(); \
17  PyErr_Clear(); \
18  THROW("Error running python script:\n" << pythonErrorMessage)
19 
20 
21 
25 inline std::string handle_pyerror() {
26  using namespace boost::python;
27  using namespace boost;
28 
29  PyObject* exc, *val, *tb;
30  object formatted_list, formatted;
31  PyErr_Fetch(&exc, &val, &tb);
32  handle<> hexc(exc), hval(allow_null(val)), htb(allow_null(tb));
33  object traceback(import("traceback"));
34 
35  if (!tb) {
36  object format_exception_only(traceback.attr("format_exception_only"));
37  formatted_list = format_exception_only(hexc, hval);
38  } else {
39  object format_exception(traceback.attr("format_exception"));
40  formatted_list = format_exception(hexc, hval, htb);
41  }
42 
43  formatted = str("\n").join(formatted_list);
44  return extract<std::string>(formatted);
45 }
46 }
47 }
std::string handle_pyerror()
Definition: handle_pyerror.hpp:25
Various utility functions to implement the tecno flux.
Definition: types.hpp:30