Py-Driver: add 'self' option
[blender.git] / source / blender / python / intern / bpy_traceback.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file blender/python/intern/bpy_traceback.c
22  *  \ingroup pythonintern
23  *
24  * This file contains utility functions for getting data from a python stack
25  * trace.
26  */
27
28
29 #include <Python.h>
30 #include <frameobject.h>
31
32 #include "BLI_utildefines.h"
33 #include "BLI_path_util.h"
34 #ifdef WIN32
35 #  include "BLI_string.h"  /* BLI_strcasecmp */
36 #endif
37
38 #include "bpy_traceback.h"
39
40 static const char *traceback_filepath(PyTracebackObject *tb, PyObject **coerce)
41 {
42         return PyBytes_AS_STRING((*coerce = PyUnicode_EncodeFSDefault(tb->tb_frame->f_code->co_filename)));
43 }
44
45 /* copied from pythonrun.c, 3.4.0 */
46 _Py_static_string(PyId_string, "<string>");
47
48 static int
49 parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
50                    int *lineno, int *offset, PyObject **text)
51 {
52         long hold;
53         PyObject *v;
54         _Py_IDENTIFIER(msg);
55         _Py_IDENTIFIER(filename);
56         _Py_IDENTIFIER(lineno);
57         _Py_IDENTIFIER(offset);
58         _Py_IDENTIFIER(text);
59
60         *message = NULL;
61         *filename = NULL;
62
63         /* new style errors.  `err' is an instance */
64         *message = _PyObject_GetAttrId(err, &PyId_msg);
65         if (!*message)
66                 goto finally;
67
68         v = _PyObject_GetAttrId(err, &PyId_filename);
69         if (!v)
70                 goto finally;
71         if (v == Py_None) {
72                 Py_DECREF(v);
73                 *filename = _PyUnicode_FromId(&PyId_string);
74                 if (*filename == NULL)
75                         goto finally;
76                 Py_INCREF(*filename);
77         }
78         else {
79                 *filename = v;
80         }
81
82         v = _PyObject_GetAttrId(err, &PyId_lineno);
83         if (!v)
84                 goto finally;
85         hold = PyLong_AsLong(v);
86         Py_DECREF(v);
87         if (hold < 0 && PyErr_Occurred())
88                 goto finally;
89         *lineno = (int)hold;
90
91         v = _PyObject_GetAttrId(err, &PyId_offset);
92         if (!v)
93                 goto finally;
94         if (v == Py_None) {
95                 *offset = -1;
96                 Py_DECREF(v);
97         } else {
98                 hold = PyLong_AsLong(v);
99                 Py_DECREF(v);
100                 if (hold < 0 && PyErr_Occurred())
101                         goto finally;
102                 *offset = (int)hold;
103         }
104
105         v = _PyObject_GetAttrId(err, &PyId_text);
106         if (!v)
107                 goto finally;
108         if (v == Py_None) {
109                 Py_DECREF(v);
110                 *text = NULL;
111         }
112         else {
113                 *text = v;
114         }
115         return 1;
116
117 finally:
118         Py_XDECREF(*message);
119         Py_XDECREF(*filename);
120         return 0;
121 }
122 /* end copied function! */
123
124
125 void python_script_error_jump(const char *filepath, int *lineno, int *offset)
126 {
127         PyObject *exception, *value;
128         PyTracebackObject *tb;
129
130         *lineno = -1;
131         *offset = 0;
132
133         PyErr_Fetch(&exception, &value, (PyObject **)&tb);
134
135         if (exception && PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError)) {
136                 /* no traceback available when SyntaxError.
137                  * python has no api's to this. reference parse_syntax_error() from pythonrun.c */
138                 PyErr_NormalizeException(&exception, &value, (PyObject **)&tb);
139                 PyErr_Restore(exception, value, (PyObject *)tb);  /* takes away reference! */
140
141                 if (value) { /* should always be true */
142                         PyObject *message;
143                         PyObject *filename_py, *text_py;
144
145                         if (parse_syntax_error(value, &message, &filename_py, lineno, offset, &text_py)) {
146                                 const char *filename = _PyUnicode_AsString(filename_py);
147                                 /* python adds a '/', prefix, so check for both */
148                                 if ((BLI_path_cmp(filename, filepath) == 0) ||
149                                     ((filename[0] == '\\' || filename[0] == '/') && BLI_path_cmp(filename + 1, filepath) == 0))
150                                 {
151                                         /* good */
152                                 }
153                                 else {
154                                         *lineno = -1;
155                                 }
156                         }
157                         else {
158                                 *lineno = -1;
159                         }
160                 }
161         }
162         else {
163                 PyErr_NormalizeException(&exception, &value, (PyObject **)&tb);
164                 PyErr_Restore(exception, value, (PyObject *)tb);  /* takes away reference! */
165                 PyErr_Print();
166
167                 for (tb = (PyTracebackObject *)PySys_GetObject("last_traceback");
168                      tb && (PyObject *)tb != Py_None;
169                      tb = tb->tb_next)
170                 {
171                         PyObject *coerce;
172                         const char *tb_filepath = traceback_filepath(tb, &coerce);
173                         const int match = ((BLI_path_cmp(tb_filepath, filepath) == 0) ||
174                                            ((tb_filepath[0] == '\\' || tb_filepath[0] == '/') && BLI_path_cmp(tb_filepath + 1, filepath) == 0));
175                         Py_DECREF(coerce);
176
177                         if (match) {
178                                 *lineno = tb->tb_lineno;
179                                 /* used to break here, but better find the inner most line */
180                         }
181                 }
182         }
183 }