Mercurial > pytave
changeset 381:f777dca58361
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.
author | NVS Abhilash <nvs232@gmail.com> |
---|---|
date | Sun, 02 Apr 2017 05:58:35 +0530 |
parents | b054a6833022 |
children | d36f06f07082 |
files | exceptions.cc |
diffstat | 1 files changed, 18 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- 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 <config.h> #endif -#include <boost/python.hpp> #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<std::string> (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; }