# HG changeset patch # User NVS Abhilash # Date 1491092915 -19800 # Node ID f777dca5836119484c1c1a23e6614d6897a28ecc # Parent b054a683302282887d3e6678e257a61dc0a91de0 Rewrite exception handling using Python/C API * exceptions.cc (pytave::fetch_exception_message): Rewrite to use native Python/C API, replaces use of Boost.Python. diff -r b054a6833022 -r f777dca58361 exceptions.cc --- a/exceptions.cc Thu Mar 23 17:35:43 2017 +0000 +++ b/exceptions.cc Sun Apr 02 05:58:35 2017 +0530 @@ -25,8 +25,9 @@ # include #endif -#include #include "exceptions.h" +#include "oct-py-eval.h" +#include "oct-py-types.h" namespace pytave { @@ -48,30 +49,33 @@ std::string fetch_exception_message (void) { - using namespace boost::python; + PyObject *ptype, *pvalue, *ptraceback; + PyObject *formatted_list, *pargs; - PyObject *ptype, *pvalue, *ptraceback; PyErr_Fetch (&ptype, &pvalue, &ptraceback); PyErr_NormalizeException (&ptype, &pvalue, &ptraceback); std::string message; - try - { - handle<> htype (ptype); - handle<> hval (allow_null (pvalue)); + pargs = PyTuple_New (2); + PyTuple_SetItem (pargs, 0, ptype); + PyTuple_SetItem (pargs, 1, pvalue); + formatted_list = py_call_function \ + ("traceback.format_exception_only", pargs); + Py_DECREF (pargs); - object traceback = import ("traceback"); - object format_exception_only = traceback.attr ("format_exception_only"); + if (formatted_list && PyList_Check (formatted_list)) + { + int len = PyList_Size (formatted_list); - object formatted_list = format_exception_only (htype, hval); - object formatted = str ("").join (formatted_list).strip (); - message = extract (formatted); + for (int i = 0; i < len; i++) + message.append (extract_py_str (PyList_GetItem (formatted_list, i))); + Py_DECREF (formatted_list); } - catch (error_already_set const &) + else { PyErr_Restore (ptype, pvalue, ptraceback); PyErr_Print (); - message = std::string ("Something weird happened. See traceback above ^"); + message = std::string ("exceptions.cc (pytave::fetch_exception_message): Cannot call 'format_exceptions_only' for the traceback"); } return message; }