merge from trunk 38379
[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 /** \file blender/python/intern/bpy_traceback.c
24  *  \ingroup pythonintern
25  */
26
27
28 #include <Python.h>
29 #include <frameobject.h>
30
31 #include "bpy_traceback.h"
32
33 static const char *traceback_filepath(PyTracebackObject *tb)
34 {
35         return _PyUnicode_AsString(tb->tb_frame->f_code->co_filename);
36 }
37
38 /* copied from pythonrun.c, 3.2.0 */
39 static int
40 parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
41                                    int *lineno, int *offset, const char **text)
42 {
43         long hold;
44         PyObject *v;
45
46         /* old style errors */
47         if (PyTuple_Check(err))
48                 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
49                                                                 lineno, offset, text);
50
51         /* new style errors.  `err' is an instance */
52
53         if (! (v = PyObject_GetAttrString(err, "msg")))
54                 goto finally;
55         *message = v;
56
57         if (!(v = PyObject_GetAttrString(err, "filename")))
58                 goto finally;
59         if (v == Py_None)
60                 *filename = NULL;
61         else if (! (*filename = _PyUnicode_AsString(v)))
62                 goto finally;
63
64         Py_DECREF(v);
65         if (!(v = PyObject_GetAttrString(err, "lineno")))
66                 goto finally;
67         hold = PyLong_AsLong(v);
68         Py_DECREF(v);
69         v = NULL;
70         if (hold < 0 && PyErr_Occurred())
71                 goto finally;
72         *lineno = (int)hold;
73
74         if (!(v = PyObject_GetAttrString(err, "offset")))
75                 goto finally;
76         if (v == Py_None) {
77                 *offset = -1;
78                 Py_DECREF(v);
79                 v = NULL;
80         }
81         else {
82                 hold = PyLong_AsLong(v);
83                 Py_DECREF(v);
84                 v = NULL;
85                 if (hold < 0 && PyErr_Occurred())
86                         goto finally;
87                 *offset = (int)hold;
88         }
89
90         if (!(v = PyObject_GetAttrString(err, "text")))
91                 goto finally;
92         if (v == Py_None)
93                 *text = NULL;
94         else if (!PyUnicode_Check(v) ||
95                          !(*text = _PyUnicode_AsString(v)))
96                 goto finally;
97         Py_DECREF(v);
98         return 1;
99
100 finally:
101         Py_XDECREF(v);
102         return 0;
103 }
104 /* end copied function! */
105
106
107 void python_script_error_jump(const char *filepath, int *lineno, int *offset)
108 {
109         PyObject *exception, *value;
110         PyTracebackObject *tb;
111
112         *lineno= -1;
113         *offset= 0;
114
115         PyErr_Fetch(&exception, &value, (PyObject **)&tb);
116
117         if(exception && PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError)) {
118                 /* no traceback available when SyntaxError.
119                  * python has no api's to this. reference parse_syntax_error() from pythonrun.c */
120                 PyErr_NormalizeException(&exception, &value, (PyObject **)&tb);
121                 PyErr_Restore(exception, value, (PyObject *)tb);        /* takes away reference! */
122
123                 if(value) { /* should always be true */
124                         PyObject *message;
125                         const char *filename, *text;
126
127                         if(parse_syntax_error(value, &message, &filename, lineno, offset, &text)) {
128                                 /* python adds a '/', prefix, so check for both */
129                                 if(     (strcmp(filename, filepath) == 0) || 
130                                         ((filename[0] == '\\' || filename[0] == '/') && strcmp(filename + 1, filepath) == 0)
131                                 ) {
132                                         /* good */
133                                 }
134                                 else {
135                                         *lineno= -1;
136                                 }
137                         }
138                         else {
139                                 *lineno= -1;
140                         }
141                 }
142         }
143         else {
144                 PyErr_NormalizeException(&exception, &value, (PyObject **)&tb);
145                 PyErr_Restore(exception, value, (PyObject *)tb);        /* takes away reference! */
146                 PyErr_Print();
147
148                 for(tb= (PyTracebackObject *)PySys_GetObject("last_traceback"); tb && (PyObject *)tb != Py_None; tb= tb->tb_next) {
149                         if(strcmp(traceback_filepath(tb), filepath) != 0) {
150                                 *lineno= tb->tb_lineno;
151                                 break;
152                         }
153                 }
154         }
155 }