Mercurial > pytave
comparison exceptions.cc @ 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 | ef057c4e6aa2 |
children | d36f06f07082 |
comparison
equal
deleted
inserted
replaced
380:b054a6833022 | 381:f777dca58361 |
---|---|
23 | 23 |
24 #if defined (HAVE_CONFIG_H) | 24 #if defined (HAVE_CONFIG_H) |
25 # include <config.h> | 25 # include <config.h> |
26 #endif | 26 #endif |
27 | 27 |
28 #include <boost/python.hpp> | |
29 #include "exceptions.h" | 28 #include "exceptions.h" |
29 #include "oct-py-eval.h" | |
30 #include "oct-py-types.h" | |
30 | 31 |
31 namespace pytave | 32 namespace pytave |
32 { | 33 { |
33 | 34 |
34 PyObject *octave_error_exception::excclass = 0; | 35 PyObject *octave_error_exception::excclass = 0; |
46 && variable_name_exception::init ()); | 47 && variable_name_exception::init ()); |
47 } | 48 } |
48 | 49 |
49 std::string fetch_exception_message (void) | 50 std::string fetch_exception_message (void) |
50 { | 51 { |
51 using namespace boost::python; | 52 PyObject *ptype, *pvalue, *ptraceback; |
53 PyObject *formatted_list, *pargs; | |
52 | 54 |
53 PyObject *ptype, *pvalue, *ptraceback; | |
54 PyErr_Fetch (&ptype, &pvalue, &ptraceback); | 55 PyErr_Fetch (&ptype, &pvalue, &ptraceback); |
55 PyErr_NormalizeException (&ptype, &pvalue, &ptraceback); | 56 PyErr_NormalizeException (&ptype, &pvalue, &ptraceback); |
56 std::string message; | 57 std::string message; |
57 | 58 |
58 try | 59 pargs = PyTuple_New (2); |
60 PyTuple_SetItem (pargs, 0, ptype); | |
61 PyTuple_SetItem (pargs, 1, pvalue); | |
62 formatted_list = py_call_function \ | |
63 ("traceback.format_exception_only", pargs); | |
64 Py_DECREF (pargs); | |
65 | |
66 if (formatted_list && PyList_Check (formatted_list)) | |
59 { | 67 { |
60 handle<> htype (ptype); | 68 int len = PyList_Size (formatted_list); |
61 handle<> hval (allow_null (pvalue)); | |
62 | 69 |
63 object traceback = import ("traceback"); | 70 for (int i = 0; i < len; i++) |
64 object format_exception_only = traceback.attr ("format_exception_only"); | 71 message.append (extract_py_str (PyList_GetItem (formatted_list, i))); |
65 | 72 Py_DECREF (formatted_list); |
66 object formatted_list = format_exception_only (htype, hval); | |
67 object formatted = str ("").join (formatted_list).strip (); | |
68 message = extract<std::string> (formatted); | |
69 } | 73 } |
70 catch (error_already_set const &) | 74 else |
71 { | 75 { |
72 PyErr_Restore (ptype, pvalue, ptraceback); | 76 PyErr_Restore (ptype, pvalue, ptraceback); |
73 PyErr_Print (); | 77 PyErr_Print (); |
74 message = std::string ("Something weird happened. See traceback above ^"); | 78 message = std::string ("exceptions.cc (pytave::fetch_exception_message): Cannot call 'format_exceptions_only' for the traceback"); |
75 } | 79 } |
76 return message; | 80 return message; |
77 } | 81 } |
78 } | 82 } |