doxygen: prevent GPL license block from being parsed as doxygen comment.
[blender.git] / source / blender / python / intern / bpy_traceback.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 #include <Python.h>
24 #include <frameobject.h>
25
26 #include "bpy_traceback.h"
27
28 static const char *traceback_filepath(PyTracebackObject *tb)
29 {
30         return _PyUnicode_AsString(tb->tb_frame->f_code->co_filename);
31 }
32
33 /* copied from pythonrun.c, 3.2.0 */
34 static int
35 parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
36                    int *lineno, int *offset, const char **text)
37 {
38     long hold;
39     PyObject *v;
40
41     /* old style errors */
42     if (PyTuple_Check(err))
43         return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
44                                 lineno, offset, text);
45
46     /* new style errors.  `err' is an instance */
47
48     if (! (v = PyObject_GetAttrString(err, "msg")))
49         goto finally;
50     *message = v;
51
52     if (!(v = PyObject_GetAttrString(err, "filename")))
53         goto finally;
54     if (v == Py_None)
55         *filename = NULL;
56     else if (! (*filename = _PyUnicode_AsString(v)))
57         goto finally;
58
59     Py_DECREF(v);
60     if (!(v = PyObject_GetAttrString(err, "lineno")))
61         goto finally;
62     hold = PyLong_AsLong(v);
63     Py_DECREF(v);
64     v = NULL;
65     if (hold < 0 && PyErr_Occurred())
66         goto finally;
67     *lineno = (int)hold;
68
69     if (!(v = PyObject_GetAttrString(err, "offset")))
70         goto finally;
71     if (v == Py_None) {
72         *offset = -1;
73         Py_DECREF(v);
74         v = NULL;
75     } else {
76         hold = PyLong_AsLong(v);
77         Py_DECREF(v);
78         v = NULL;
79         if (hold < 0 && PyErr_Occurred())
80             goto finally;
81         *offset = (int)hold;
82     }
83
84     if (!(v = PyObject_GetAttrString(err, "text")))
85         goto finally;
86     if (v == Py_None)
87         *text = NULL;
88     else if (!PyUnicode_Check(v) ||
89              !(*text = _PyUnicode_AsString(v)))
90         goto finally;
91     Py_DECREF(v);
92     return 1;
93
94 finally:
95     Py_XDECREF(v);
96     return 0;
97 }
98 /* end copied function! */
99
100
101 void python_script_error_jump(const char *filepath, int *lineno, int *offset)
102 {
103         PyObject *exception, *value;
104         PyTracebackObject *tb;
105
106         *lineno= -1;
107         *offset= 0;
108
109         PyErr_Fetch(&exception, &value, (PyObject **)&tb);
110
111         if(exception && PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError)) {
112                 /* no traceback available when SyntaxError.
113                  * python has no api's to this. reference parse_syntax_error() from pythonrun.c */
114                 PyErr_NormalizeException(&exception, &value, (PyObject **)&tb);
115                 PyErr_Restore(exception, value, (PyObject *)tb);        /* takes away reference! */
116
117                 if(value) { /* should always be true */
118                         PyObject *message;
119                 const char *filename, *text;
120
121                         if(parse_syntax_error(value, &message, &filename, lineno, offset, &text)) {
122                                 /* python adds a '/', prefix, so check for both */
123                                 if(     (strcmp(filename, filepath) == 0) || 
124                                         ((filename[0] == '\\' || filename[0] == '/') && strcmp(filename + 1, filepath) == 0)
125                                 ) {
126                                         /* good */
127                                 }
128                                 else {
129                                         *lineno= -1;
130                                 }
131                         }
132                         else {
133                                 *lineno= -1;
134                         }
135                 }
136
137                 /* this avoids an abort in Python 2.3's garbage collecting */
138         }
139         else {
140                 PyErr_NormalizeException(&exception, &value, (PyObject **)&tb);
141                 PyErr_Restore(exception, value, (PyObject *)tb);        /* takes away reference! */
142                 PyErr_Print();
143
144                 for(tb= (PyTracebackObject *)PySys_GetObject("last_traceback"); tb && (PyObject *)tb != Py_None; tb= tb->tb_next) {
145                         if(strcmp(traceback_filepath(tb), filepath) != 0) {
146                                 *lineno= tb->tb_lineno;
147                                 break;
148                         }
149                 }
150         }
151 }