Fix msvc 2013 compiler errors after the ingenious cleanup in 4ca67869cc7a.
[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 #include "BLI_string.h"
35
36 #include "bpy_traceback.h"
37
38 static const char *traceback_filepath(PyTracebackObject *tb, PyObject **coerce)
39 {
40         return PyBytes_AS_STRING((*coerce = PyUnicode_EncodeFSDefault(tb->tb_frame->f_code->co_filename)));
41 }
42
43 /* copied from pythonrun.c, 3.3.0 */
44 static int
45 parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
46                    int *lineno, int *offset, const char **text)
47 {
48         long hold;
49         PyObject *v;
50         _Py_IDENTIFIER(msg);
51         _Py_IDENTIFIER(filename);
52         _Py_IDENTIFIER(lineno);
53         _Py_IDENTIFIER(offset);
54         _Py_IDENTIFIER(text);
55
56         *message = NULL;
57
58         /* new style errors.  `err' is an instance */
59         *message = _PyObject_GetAttrId(err, &PyId_msg);
60         if (!*message)
61                 goto finally;
62
63         v = _PyObject_GetAttrId(err, &PyId_filename);
64         if (!v)
65                 goto finally;
66         if (v == Py_None) {
67                 Py_DECREF(v);
68                 *filename = NULL;
69         }
70         else {
71                 *filename = _PyUnicode_AsString(v);
72                 Py_DECREF(v);
73                 if (!*filename)
74                         goto finally;
75         }
76
77         v = _PyObject_GetAttrId(err, &PyId_lineno);
78         if (!v)
79                 goto finally;
80         hold = PyLong_AsLong(v);
81         Py_DECREF(v);
82         if (hold < 0 && PyErr_Occurred())
83                 goto finally;
84         *lineno = (int)hold;
85
86         v = _PyObject_GetAttrId(err, &PyId_offset);
87         if (!v)
88                 goto finally;
89         if (v == Py_None) {
90                 *offset = -1;
91                 Py_DECREF(v);
92         } else {
93                 hold = PyLong_AsLong(v);
94                 Py_DECREF(v);
95                 if (hold < 0 && PyErr_Occurred())
96                         goto finally;
97                 *offset = (int)hold;
98         }
99
100         v = _PyObject_GetAttrId(err, &PyId_text);
101         if (!v)
102                 goto finally;
103         if (v == Py_None) {
104                 Py_DECREF(v);
105                 *text = NULL;
106         }
107         else {
108                 *text = _PyUnicode_AsString(v);
109                 Py_DECREF(v);
110                 if (!*text)
111                         goto finally;
112         }
113         return 1;
114
115 finally:
116         Py_XDECREF(*message);
117         return 0;
118 }
119 /* end copied function! */
120
121
122 void python_script_error_jump(const char *filepath, int *lineno, int *offset)
123 {
124         PyObject *exception, *value;
125         PyTracebackObject *tb;
126
127         *lineno = -1;
128         *offset = 0;
129
130         PyErr_Fetch(&exception, &value, (PyObject **)&tb);
131
132         if (exception && PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError)) {
133                 /* no traceback available when SyntaxError.
134                  * python has no api's to this. reference parse_syntax_error() from pythonrun.c */
135                 PyErr_NormalizeException(&exception, &value, (PyObject **)&tb);
136                 PyErr_Restore(exception, value, (PyObject *)tb);  /* takes away reference! */
137
138                 if (value) { /* should always be true */
139                         PyObject *message;
140                         const char *filename, *text;
141
142                         if (parse_syntax_error(value, &message, &filename, lineno, offset, &text)) {
143                                 /* python adds a '/', prefix, so check for both */
144                                 if ((BLI_path_cmp(filename, filepath) == 0) ||
145                                     ((filename[0] == '\\' || filename[0] == '/') && BLI_path_cmp(filename + 1, filepath) == 0))
146                                 {
147                                         /* good */
148                                 }
149                                 else {
150                                         *lineno = -1;
151                                 }
152                         }
153                         else {
154                                 *lineno = -1;
155                         }
156                 }
157         }
158         else {
159                 PyErr_NormalizeException(&exception, &value, (PyObject **)&tb);
160                 PyErr_Restore(exception, value, (PyObject *)tb);  /* takes away reference! */
161                 PyErr_Print();
162
163                 for (tb = (PyTracebackObject *)PySys_GetObject("last_traceback");
164                      tb && (PyObject *)tb != Py_None;
165                      tb = tb->tb_next)
166                 {
167                         PyObject *coerce;
168                         const char *tb_filepath = traceback_filepath(tb, &coerce);
169                         const int match = ((BLI_path_cmp(tb_filepath, filepath) == 0) ||
170                                            ((tb_filepath[0] == '\\' || tb_filepath[0] == '/') && BLI_path_cmp(tb_filepath + 1, filepath) == 0));
171                         Py_DECREF(coerce);
172
173                         if (match) {
174                                 *lineno = tb->tb_lineno;
175                                 /* used to break here, but better find the inner most line */
176                         }
177                 }
178         }
179 }