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