Mercurial > pytave
annotate exceptions.cc @ 419:2011e0d2f926
maint: update hg ignore list
* .hgignore: Rewrite and reorganize ignore pattern list. Drop patterns not
needed for this project, group and sort related patterns.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Thu, 04 May 2017 09:41:40 -0700 |
parents | aef165ff92b0 |
children |
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 "exceptions.h" |
381
f777dca58361
Rewrite exception handling using Python/C API
NVS Abhilash <nvs232@gmail.com>
parents:
253
diff
changeset
|
29 #include "oct-py-eval.h" |
404
aef165ff92b0
Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents:
392
diff
changeset
|
30 #include "oct-py-object.h" |
381
f777dca58361
Rewrite exception handling using Python/C API
NVS Abhilash <nvs232@gmail.com>
parents:
253
diff
changeset
|
31 #include "oct-py-types.h" |
0
4da14cce0890
First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff
changeset
|
32 |
146
8e3d06f2f5cf
Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents:
140
diff
changeset
|
33 namespace pytave |
8e3d06f2f5cf
Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents:
140
diff
changeset
|
34 { |
231
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
35 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
|
36 { |
381
f777dca58361
Rewrite exception handling using Python/C API
NVS Abhilash <nvs232@gmail.com>
parents:
253
diff
changeset
|
37 PyObject *ptype, *pvalue, *ptraceback; |
231
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
38 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
|
39 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
|
40 |
404
aef165ff92b0
Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents:
392
diff
changeset
|
41 python_object args = PyTuple_Pack (2, ptype, pvalue); |
aef165ff92b0
Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents:
392
diff
changeset
|
42 python_object formatted_list = py_call_function |
aef165ff92b0
Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents:
392
diff
changeset
|
43 ("traceback.format_exception_only", args); |
382
d36f06f07082
Minor code reorganization to previous commit
Mike Miller <mtmiller@octave.org>
parents:
381
diff
changeset
|
44 |
d36f06f07082
Minor code reorganization to previous commit
Mike Miller <mtmiller@octave.org>
parents:
381
diff
changeset
|
45 std::string message; |
233
0deddd1b0526
Eliminate extra newlines in Python error formatting
Mike Miller <mtmiller@octave.org>
parents:
231
diff
changeset
|
46 |
381
f777dca58361
Rewrite exception handling using Python/C API
NVS Abhilash <nvs232@gmail.com>
parents:
253
diff
changeset
|
47 if (formatted_list && PyList_Check (formatted_list)) |
f777dca58361
Rewrite exception handling using Python/C API
NVS Abhilash <nvs232@gmail.com>
parents:
253
diff
changeset
|
48 { |
f777dca58361
Rewrite exception handling using Python/C API
NVS Abhilash <nvs232@gmail.com>
parents:
253
diff
changeset
|
49 int len = PyList_Size (formatted_list); |
233
0deddd1b0526
Eliminate extra newlines in Python error formatting
Mike Miller <mtmiller@octave.org>
parents:
231
diff
changeset
|
50 |
381
f777dca58361
Rewrite exception handling using Python/C API
NVS Abhilash <nvs232@gmail.com>
parents:
253
diff
changeset
|
51 for (int i = 0; i < len; i++) |
f777dca58361
Rewrite exception handling using Python/C API
NVS Abhilash <nvs232@gmail.com>
parents:
253
diff
changeset
|
52 message.append (extract_py_str (PyList_GetItem (formatted_list, i))); |
231
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
53 } |
381
f777dca58361
Rewrite exception handling using Python/C API
NVS Abhilash <nvs232@gmail.com>
parents:
253
diff
changeset
|
54 else |
231
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
55 { |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
56 PyErr_Restore (ptype, pvalue, ptraceback); |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
57 PyErr_Print (); |
382
d36f06f07082
Minor code reorganization to previous commit
Mike Miller <mtmiller@octave.org>
parents:
381
diff
changeset
|
58 message = "exceptions.cc (pytave::fetch_exception_message): cannot call 'format_exceptions_only' for the traceback"; |
231
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
59 } |
382
d36f06f07082
Minor code reorganization to previous commit
Mike Miller <mtmiller@octave.org>
parents:
381
diff
changeset
|
60 |
231
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
61 return message; |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
171
diff
changeset
|
62 } |
0
4da14cce0890
First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff
changeset
|
63 } |