comparison exceptions.cc @ 382:d36f06f07082

Minor code reorganization to previous commit * exceptions.cc (pytave::fetch_exception_message): Reorganize code to keep declaration with use, other cleanups.
author Mike Miller <mtmiller@octave.org>
date Sun, 02 Apr 2017 10:06:42 -0700
parents f777dca58361
children 09a1acb81d8b
comparison
equal deleted inserted replaced
381:f777dca58361 382:d36f06f07082
48 } 48 }
49 49
50 std::string fetch_exception_message (void) 50 std::string fetch_exception_message (void)
51 { 51 {
52 PyObject *ptype, *pvalue, *ptraceback; 52 PyObject *ptype, *pvalue, *ptraceback;
53 PyObject *formatted_list, *pargs;
54
55 PyErr_Fetch (&ptype, &pvalue, &ptraceback); 53 PyErr_Fetch (&ptype, &pvalue, &ptraceback);
56 PyErr_NormalizeException (&ptype, &pvalue, &ptraceback); 54 PyErr_NormalizeException (&ptype, &pvalue, &ptraceback);
55
56 PyObject *args = PyTuple_New (2);
57 PyTuple_SetItem (args, 0, ptype);
58 PyTuple_SetItem (args, 1, pvalue);
59 PyObject *formatted_list = py_call_function
60 ("traceback.format_exception_only", args);
61 Py_DECREF (args);
62
57 std::string message; 63 std::string message;
58
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 64
66 if (formatted_list && PyList_Check (formatted_list)) 65 if (formatted_list && PyList_Check (formatted_list))
67 { 66 {
68 int len = PyList_Size (formatted_list); 67 int len = PyList_Size (formatted_list);
69 68
73 } 72 }
74 else 73 else
75 { 74 {
76 PyErr_Restore (ptype, pvalue, ptraceback); 75 PyErr_Restore (ptype, pvalue, ptraceback);
77 PyErr_Print (); 76 PyErr_Print ();
78 message = std::string ("exceptions.cc (pytave::fetch_exception_message): Cannot call 'format_exceptions_only' for the traceback"); 77 message = "exceptions.cc (pytave::fetch_exception_message): cannot call 'format_exceptions_only' for the traceback";
79 } 78 }
79
80 return message; 80 return message;
81 } 81 }
82 } 82 }