13#include <boost/python/extract.hpp>
14#include <boost/python/object.hpp>
15#include <frameobject.h>
20using boost::python::extract;
32void tracebackToStream(std::ostream &msg, PyTracebackObject *traceback,
bool root =
true) {
33 if (traceback ==
nullptr)
41 msg <<
" line " << traceback->tb_lineno <<
" in \'"
42 << extract<const char *>(traceback->tb_frame->f_code->co_filename)() <<
"\'";
43 tracebackToStream(msg, traceback->tb_next,
false);
56std::string exceptionToString(
bool withTrace) {
57 GlobalInterpreterLock gil;
58 PyObject *exception(
nullptr), *
value(
nullptr), *traceback(
nullptr);
59 PyErr_Fetch(&exception, &
value, &traceback);
61 PyErr_NormalizeException(&exception, &
value, &traceback);
63 PyObject *strRepr = PyObject_Str(
value);
64 std::stringstream builder;
65 if (
value && strRepr) {
66 builder << extract<const char *>(strRepr)();
68 builder <<
"Unknown exception has occurred.";
71 tracebackToStream(builder,
reinterpret_cast<PyTracebackObject *
>(traceback));
79 Py_XDECREF(traceback);
80 Py_XDECREF(exception);
double value
The value of the point.
PythonException(bool withTrace=true)
Construct an exception object where the message is populated from the current Python exception state.