comparison oct-py-error.cc @ 423:6b9de18b4bdd

Eliminate remaining custom exception handling in favor of Octave errors * oct-py-error.cc, oct-py-error.h (pytave::error_python_exception): New function to generate an Octave error from an active Python error condition, based on pytave::fetch_exception_message. * oct-py-eval.cc (pytave::py_call_function, pytave::py_run_string_safe): Use it. * oct-py-types.cc (pytave::make_py_dict): Likewise. * oct-py-util.cc (pytave::py_objstore): Likewise. * pycall.cc (Fpycall): Remove exception handling logic. * pyeval.cc (Fpyeval): Likewise. * pyexec.cc (Fpyexec): Likewise. * exceptions.cc, exceptions.h: Delete. * Makefile.am (COMMON_SOURCE_FILES, PYTAVE_HEADER_FILES): Remove them.
author Mike Miller <mtmiller@octave.org>
date Thu, 04 May 2017 21:15:07 -0700
parents 8247f298fd16
children
comparison
equal deleted inserted replaced
422:8247f298fd16 423:6b9de18b4bdd
22 22
23 #if defined (HAVE_CONFIG_H) 23 #if defined (HAVE_CONFIG_H)
24 # include <config.h> 24 # include <config.h>
25 #endif 25 #endif
26 26
27 #include <Python.h>
27 #include <octave/error.h> 28 #include <octave/error.h>
28 29
29 #include "oct-py-error.h" 30 #include "oct-py-error.h"
31 #include "oct-py-eval.h"
32 #include "oct-py-object.h"
33 #include "oct-py-types.h"
30 34
31 namespace pytave 35 namespace pytave
32 { 36 {
33 37
34 void 38 void
43 { 47 {
44 error ("unable to convert to %s, must be a Python %s", to.c_str (), 48 error ("unable to convert to %s, must be a Python %s", to.c_str (),
45 must.c_str ()); 49 must.c_str ());
46 } 50 }
47 51
52 void
53 error_python_exception ()
54 {
55 const char *format_exception_only = "traceback.format_exception_only";
56
57 PyObject *ptype, *pvalue, *ptraceback;
58 PyErr_Fetch (&ptype, &pvalue, &ptraceback);
59 PyErr_NormalizeException (&ptype, &pvalue, &ptraceback);
60
61 python_object args = PyTuple_Pack (2, ptype, pvalue);
62 python_object lines = py_call_function (format_exception_only, args);
63
64 if (lines && PySequence_Check (lines))
65 {
66 Py_ssize_t len = PySequence_Size (lines);
67 python_object last_line = PySequence_GetItem (lines, len - 1);
68
69 std::string msg = extract_py_str (last_line);
70 if (msg.back () == '\n')
71 msg.resize (msg.size () - 1);
72
73 error ("%s", msg.c_str ());
74 }
75 else
76 {
77 PyErr_Restore (ptype, pvalue, ptraceback);
78 PyErr_Print ();
79 error ("runtime failed to get exception information from %s",
80 format_exception_only);
81 }
82 }
83
48 } 84 }