annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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"
f777dca58361 Rewrite exception handling using Python/C API
NVS Abhilash <nvs232@gmail.com>
parents: 253
diff changeset
30 #include "oct-py-types.h"
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
31
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 140
diff changeset
32 namespace pytave
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 140
diff changeset
33 {
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
34
146
8e3d06f2f5cf Use Octave coding style for C++ sources, PEP8 style for Python sources
Mike Miller <mtmiller@octave.org>
parents: 140
diff changeset
35 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
36 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
37 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
38 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
39 PyObject *variable_name_exception::excclass = 0;
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
40
171
b8560f97e9bf Wrap custom exception initialization in a function
Mike Miller <mtmiller@octave.org>
parents: 150
diff changeset
41 bool init_exceptions (void)
b8560f97e9bf Wrap custom exception initialization in a function
Mike Miller <mtmiller@octave.org>
parents: 150
diff changeset
42 {
b8560f97e9bf Wrap custom exception initialization in a function
Mike Miller <mtmiller@octave.org>
parents: 150
diff changeset
43 return (octave_error_exception::init ()
b8560f97e9bf Wrap custom exception initialization in a function
Mike Miller <mtmiller@octave.org>
parents: 150
diff changeset
44 && value_convert_exception::init ()
b8560f97e9bf Wrap custom exception initialization in a function
Mike Miller <mtmiller@octave.org>
parents: 150
diff changeset
45 && object_convert_exception::init ()
b8560f97e9bf Wrap custom exception initialization in a function
Mike Miller <mtmiller@octave.org>
parents: 150
diff changeset
46 && octave_parse_exception::init ()
b8560f97e9bf Wrap custom exception initialization in a function
Mike Miller <mtmiller@octave.org>
parents: 150
diff changeset
47 && variable_name_exception::init ());
b8560f97e9bf Wrap custom exception initialization in a function
Mike Miller <mtmiller@octave.org>
parents: 150
diff changeset
48 }
231
6fffa6219b2c Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 171
diff changeset
49
6fffa6219b2c Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 171
diff changeset
50 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
51 {
381
f777dca58361 Rewrite exception handling using Python/C API
NVS Abhilash <nvs232@gmail.com>
parents: 253
diff changeset
52 PyObject *ptype, *pvalue, *ptraceback;
231
6fffa6219b2c Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 171
diff changeset
53 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
54 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
55
382
d36f06f07082 Minor code reorganization to previous commit
Mike Miller <mtmiller@octave.org>
parents: 381
diff changeset
56 PyObject *args = PyTuple_New (2);
d36f06f07082 Minor code reorganization to previous commit
Mike Miller <mtmiller@octave.org>
parents: 381
diff changeset
57 PyTuple_SetItem (args, 0, ptype);
d36f06f07082 Minor code reorganization to previous commit
Mike Miller <mtmiller@octave.org>
parents: 381
diff changeset
58 PyTuple_SetItem (args, 1, pvalue);
d36f06f07082 Minor code reorganization to previous commit
Mike Miller <mtmiller@octave.org>
parents: 381
diff changeset
59 PyObject *formatted_list = py_call_function
d36f06f07082 Minor code reorganization to previous commit
Mike Miller <mtmiller@octave.org>
parents: 381
diff changeset
60 ("traceback.format_exception_only", args);
d36f06f07082 Minor code reorganization to previous commit
Mike Miller <mtmiller@octave.org>
parents: 381
diff changeset
61 Py_DECREF (args);
d36f06f07082 Minor code reorganization to previous commit
Mike Miller <mtmiller@octave.org>
parents: 381
diff changeset
62
d36f06f07082 Minor code reorganization to previous commit
Mike Miller <mtmiller@octave.org>
parents: 381
diff changeset
63 std::string message;
233
0deddd1b0526 Eliminate extra newlines in Python error formatting
Mike Miller <mtmiller@octave.org>
parents: 231
diff changeset
64
381
f777dca58361 Rewrite exception handling using Python/C API
NVS Abhilash <nvs232@gmail.com>
parents: 253
diff changeset
65 if (formatted_list && PyList_Check (formatted_list))
f777dca58361 Rewrite exception handling using Python/C API
NVS Abhilash <nvs232@gmail.com>
parents: 253
diff changeset
66 {
f777dca58361 Rewrite exception handling using Python/C API
NVS Abhilash <nvs232@gmail.com>
parents: 253
diff changeset
67 int len = PyList_Size (formatted_list);
233
0deddd1b0526 Eliminate extra newlines in Python error formatting
Mike Miller <mtmiller@octave.org>
parents: 231
diff changeset
68
381
f777dca58361 Rewrite exception handling using Python/C API
NVS Abhilash <nvs232@gmail.com>
parents: 253
diff changeset
69 for (int i = 0; i < len; i++)
f777dca58361 Rewrite exception handling using Python/C API
NVS Abhilash <nvs232@gmail.com>
parents: 253
diff changeset
70 message.append (extract_py_str (PyList_GetItem (formatted_list, i)));
f777dca58361 Rewrite exception handling using Python/C API
NVS Abhilash <nvs232@gmail.com>
parents: 253
diff changeset
71 Py_DECREF (formatted_list);
231
6fffa6219b2c Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 171
diff changeset
72 }
381
f777dca58361 Rewrite exception handling using Python/C API
NVS Abhilash <nvs232@gmail.com>
parents: 253
diff changeset
73 else
231
6fffa6219b2c Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 171
diff changeset
74 {
6fffa6219b2c Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 171
diff changeset
75 PyErr_Restore (ptype, pvalue, ptraceback);
6fffa6219b2c Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 171
diff changeset
76 PyErr_Print ();
382
d36f06f07082 Minor code reorganization to previous commit
Mike Miller <mtmiller@octave.org>
parents: 381
diff changeset
77 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
78 }
382
d36f06f07082 Minor code reorganization to previous commit
Mike Miller <mtmiller@octave.org>
parents: 381
diff changeset
79
231
6fffa6219b2c Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 171
diff changeset
80 return message;
6fffa6219b2c Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 171
diff changeset
81 }
0
4da14cce0890 First launchpad.net check in.
David Grundberg <c04dgg@cs.umu.se>
parents:
diff changeset
82 }