Mercurial > pytave
annotate exceptions.cc @ 253:ef057c4e6aa2
Normalize exception info before extracting text (fixes issue #32)
* exceptions.cc (pytave::fetch_exception_message): Ensure that ptype and pvalue
are of same class before trying to extract the text. Fixes crash with Python 3.
author | Abhinav Tripathi <genuinelucifer@gmail.com> |
---|---|
date | Thu, 28 Jul 2016 14:21:14 -0700 |
parents | 0deddd1b0526 |
children | f777dca58361 |
rev | line source |
---|---|
0
4da14cce0890
First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff
changeset
|
1 /* |
139
b12908ffa6df
maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents:
40
diff
changeset
|
2 |
150
a3dc9d24ae38
maint: Add myself to copyright notices
Mike Miller <mtmiller@octave.org>
parents:
146
diff
changeset
|
3 Copyright (C) 2015-2016 Mike Miller |
139
b12908ffa6df
maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents:
40
diff
changeset
|
4 Copyright (C) 2008 David Grundberg, HÃ¥kan Fors Nilsson |
b12908ffa6df
maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents:
40
diff
changeset
|
5 |
b12908ffa6df
maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents:
40
diff
changeset
|
6 This file is part of Pytave. |
b12908ffa6df
maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents:
40
diff
changeset
|
7 |
b12908ffa6df
maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents:
40
diff
changeset
|
8 Pytave is free software: you can redistribute it and/or modify it |
b12908ffa6df
maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents:
40
diff
changeset
|
9 under the terms of the GNU General Public License as published by the |
b12908ffa6df
maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents:
40
diff
changeset
|
10 Free Software Foundation, either version 3 of the License, or (at your |
b12908ffa6df
maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents:
40
diff
changeset
|
11 option) any later version. |
b12908ffa6df
maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents:
40
diff
changeset
|
12 |
b12908ffa6df
maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents:
40
diff
changeset
|
13 Pytave is distributed in the hope that it will be useful, but WITHOUT |
b12908ffa6df
maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents:
40
diff
changeset
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
b12908ffa6df
maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents:
40
diff
changeset
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
b12908ffa6df
maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents:
40
diff
changeset
|
16 for more details. |
b12908ffa6df
maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents:
40
diff
changeset
|
17 |
b12908ffa6df
maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents:
40
diff
changeset
|
18 You should have received a copy of the GNU General Public License |
b12908ffa6df
maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents:
40
diff
changeset
|
19 along with Pytave; see the file COPYING. If not, see |
b12908ffa6df
maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents:
40
diff
changeset
|
20 <http://www.gnu.org/licenses/>. |
b12908ffa6df
maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents:
40
diff
changeset
|
21 |
b12908ffa6df
maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents:
40
diff
changeset
|
22 */ |
0
4da14cce0890
First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff
changeset
|
23 |
140
d9fa0b60e889
* exceptions.cc: Include config.h before all other header files.
Mike Miller <mtmiller@octave.org>
parents:
139
diff
changeset
|
24 #if defined (HAVE_CONFIG_H) |
d9fa0b60e889
* exceptions.cc: Include config.h before all other header files.
Mike Miller <mtmiller@octave.org>
parents:
139
diff
changeset
|
25 # include <config.h> |
d9fa0b60e889
* exceptions.cc: Include config.h before all other header files.
Mike Miller <mtmiller@octave.org>
parents:
139
diff
changeset
|
26 #endif |
d9fa0b60e889
* exceptions.cc: Include config.h before all other header files.
Mike Miller <mtmiller@octave.org>
parents:
139
diff
changeset
|
27 |
0
4da14cce0890
First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff
changeset
|
28 #include <boost/python.hpp> |
4da14cce0890
First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff
changeset
|
29 #include "exceptions.h" |
4da14cce0890
First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff
changeset
|
30 |
146
8e3d06f2f5cf
Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents:
140
diff
changeset
|
31 namespace pytave |
8e3d06f2f5cf
Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents:
140
diff
changeset
|
32 { |
0
4da14cce0890
First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff
changeset
|
33 |
146
8e3d06f2f5cf
Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents:
140
diff
changeset
|
34 PyObject *octave_error_exception::excclass = 0; |
8e3d06f2f5cf
Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents:
140
diff
changeset
|
35 PyObject *value_convert_exception::excclass = 0; |
8e3d06f2f5cf
Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents:
140
diff
changeset
|
36 PyObject *object_convert_exception::excclass = 0; |
8e3d06f2f5cf
Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents:
140
diff
changeset
|
37 PyObject *octave_parse_exception::excclass = 0; |
8e3d06f2f5cf
Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents:
140
diff
changeset
|
38 PyObject *variable_name_exception::excclass = 0; |
0
4da14cce0890
First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff
changeset
|
39 |
171
b8560f97e9bf
Wrap custom exception initialization in a function
Mike Miller <mtmiller@octave.org>
parents:
150
diff
changeset
|
40 bool init_exceptions (void) |
b8560f97e9bf
Wrap custom exception initialization in a function
Mike Miller <mtmiller@octave.org>
parents:
150
diff
changeset
|
41 { |
b8560f97e9bf
Wrap custom exception initialization in a function
Mike Miller <mtmiller@octave.org>
parents:
150
diff
changeset
|
42 return (octave_error_exception::init () |
b8560f97e9bf
Wrap custom exception initialization in a function
Mike Miller <mtmiller@octave.org>
parents:
150
diff
changeset
|
43 && value_convert_exception::init () |
b8560f97e9bf
Wrap custom exception initialization in a function
Mike Miller <mtmiller@octave.org>
parents:
150
diff
changeset
|
44 && object_convert_exception::init () |
b8560f97e9bf
Wrap custom exception initialization in a function
Mike Miller <mtmiller@octave.org>
parents:
150
diff
changeset
|
45 && octave_parse_exception::init () |
b8560f97e9bf
Wrap custom exception initialization in a function
Mike Miller <mtmiller@octave.org>
parents:
150
diff
changeset
|
46 && variable_name_exception::init ()); |
b8560f97e9bf
Wrap custom exception initialization in a function
Mike Miller <mtmiller@octave.org>
parents:
150
diff
changeset
|
47 } |
231
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
48 |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
49 std::string fetch_exception_message (void) |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
50 { |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
51 using namespace boost::python; |
233
0deddd1b0526
Eliminate extra newlines in Python error formatting
Mike Miller <mtmiller@octave.org>
parents:
231
diff
changeset
|
52 |
231
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
53 PyObject *ptype, *pvalue, *ptraceback; |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
54 PyErr_Fetch (&ptype, &pvalue, &ptraceback); |
253
ef057c4e6aa2
Normalize exception info before extracting text (fixes issue #32)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
233
diff
changeset
|
55 PyErr_NormalizeException (&ptype, &pvalue, &ptraceback); |
231
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
56 std::string message; |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
57 |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
58 try |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
59 { |
233
0deddd1b0526
Eliminate extra newlines in Python error formatting
Mike Miller <mtmiller@octave.org>
parents:
231
diff
changeset
|
60 handle<> htype (ptype); |
0deddd1b0526
Eliminate extra newlines in Python error formatting
Mike Miller <mtmiller@octave.org>
parents:
231
diff
changeset
|
61 handle<> hval (allow_null (pvalue)); |
0deddd1b0526
Eliminate extra newlines in Python error formatting
Mike Miller <mtmiller@octave.org>
parents:
231
diff
changeset
|
62 |
0deddd1b0526
Eliminate extra newlines in Python error formatting
Mike Miller <mtmiller@octave.org>
parents:
231
diff
changeset
|
63 object traceback = import ("traceback"); |
0deddd1b0526
Eliminate extra newlines in Python error formatting
Mike Miller <mtmiller@octave.org>
parents:
231
diff
changeset
|
64 object format_exception_only = traceback.attr ("format_exception_only"); |
0deddd1b0526
Eliminate extra newlines in Python error formatting
Mike Miller <mtmiller@octave.org>
parents:
231
diff
changeset
|
65 |
0deddd1b0526
Eliminate extra newlines in Python error formatting
Mike Miller <mtmiller@octave.org>
parents:
231
diff
changeset
|
66 object formatted_list = format_exception_only (htype, hval); |
0deddd1b0526
Eliminate extra newlines in Python error formatting
Mike Miller <mtmiller@octave.org>
parents:
231
diff
changeset
|
67 object formatted = str ("").join (formatted_list).strip (); |
231
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
68 message = extract<std::string> (formatted); |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
69 } |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
70 catch (error_already_set const &) |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
71 { |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
72 PyErr_Restore (ptype, pvalue, ptraceback); |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
73 PyErr_Print (); |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
74 message = std::string ("Something weird happened. See traceback above ^"); |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
75 } |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
76 return message; |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
77 } |
0
4da14cce0890
First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff
changeset
|
78 } |