Mercurial > pytave
annotate 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 |
rev | line source |
---|---|
422
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
1 /* |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
2 |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
3 Copyright (C) 2017 Mike Miller |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
4 |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
5 This file is part of Pytave. |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
6 |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
7 Pytave is free software; you can redistribute it and/or modify it |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
8 under the terms of the GNU General Public License as published by the |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
9 Free Software Foundation; either version 3 of the License, or (at your |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
10 option) any later version. |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
11 |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
12 Pytave is distributed in the hope that it will be useful, but WITHOUT |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
15 for more details. |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
16 |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
17 You should have received a copy of the GNU General Public License |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
18 along with Pytave; see the file COPYING. If not, see |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
19 <http://www.gnu.org/licenses/>. |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
20 |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
21 */ |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
22 |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
23 #if defined (HAVE_CONFIG_H) |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
24 # include <config.h> |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
25 #endif |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
26 |
423
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
27 #include <Python.h> |
422
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
28 #include <octave/error.h> |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
29 |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
30 #include "oct-py-error.h" |
423
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
31 #include "oct-py-eval.h" |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
32 #include "oct-py-object.h" |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
33 #include "oct-py-types.h" |
422
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
34 |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
35 namespace pytave |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
36 { |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
37 |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
38 void |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
39 error_conversion_invalid_python_object (const std::string& to) |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
40 { |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
41 error ("unable to convert to %s, invalid Python object", to.c_str ()); |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
42 } |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
43 |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
44 void |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
45 error_conversion_mismatch_python_type (const std::string& to, |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
46 const std::string& must) |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
47 { |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
48 error ("unable to convert to %s, must be a Python %s", to.c_str (), |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
49 must.c_str ()); |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
50 } |
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
51 |
423
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
52 void |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
53 error_python_exception () |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
54 { |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
55 const char *format_exception_only = "traceback.format_exception_only"; |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
56 |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
57 PyObject *ptype, *pvalue, *ptraceback; |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
58 PyErr_Fetch (&ptype, &pvalue, &ptraceback); |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
59 PyErr_NormalizeException (&ptype, &pvalue, &ptraceback); |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
60 |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
61 python_object args = PyTuple_Pack (2, ptype, pvalue); |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
62 python_object lines = py_call_function (format_exception_only, args); |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
63 |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
64 if (lines && PySequence_Check (lines)) |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
65 { |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
66 Py_ssize_t len = PySequence_Size (lines); |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
67 python_object last_line = PySequence_GetItem (lines, len - 1); |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
68 |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
69 std::string msg = extract_py_str (last_line); |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
70 if (msg.back () == '\n') |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
71 msg.resize (msg.size () - 1); |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
72 |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
73 error ("%s", msg.c_str ()); |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
74 } |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
75 else |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
76 { |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
77 PyErr_Restore (ptype, pvalue, ptraceback); |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
78 PyErr_Print (); |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
79 error ("runtime failed to get exception information from %s", |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
80 format_exception_only); |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
81 } |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
82 } |
6b9de18b4bdd
Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents:
422
diff
changeset
|
83 |
422
8247f298fd16
Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents:
diff
changeset
|
84 } |