Blender V5.3
bpy_traceback.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include <Python.h>
13#include <frameobject.h>
14
15#include "BLI_path_utils.hh"
16#include "BLI_utildefines.hh"
17#ifdef WIN32
18# include "BLI_string.hh" /* BLI_strcasecmp */
19#endif
20
21#include "bpy_traceback.hh"
22
23namespace blender {
24
25#define MAKE_PY_IDENTIFIER_EX(varname, value) static _Py_Identifier varname{value, -1};
26#define MAKE_PY_IDENTIFIER(varname) MAKE_PY_IDENTIFIER_EX(PyId_##varname, #varname)
27
28MAKE_PY_IDENTIFIER_EX(PyId_string, "<string>")
37
38static const char *traceback_filepath(PyTracebackObject *tb, PyObject **r_coerce)
39{
40 PyCodeObject *code = PyFrame_GetCode(tb->tb_frame);
41 *r_coerce = PyUnicode_EncodeFSDefault(code->co_filename);
42 Py_DECREF(code);
43 return PyBytes_AS_STRING(*r_coerce);
44}
45
47static int traceback_line_number(PyTracebackObject *tb)
48{
49 int lineno = tb->tb_lineno;
50 if (lineno == -1) {
51 PyObject *lineno_py = _PyObject_GetAttrId(reinterpret_cast<PyObject *>(tb), &PyId_tb_lineno);
52 if (lineno_py) {
53 if (PyLong_Check(lineno_py)) {
54 const int lineno_test = PyLong_AsLongLong(lineno_py);
55 /* Theoretically could occur from overflow,
56 * internally these are `int` so it shouldn't happen. */
57 if (!((lineno_test == -1) && PyErr_Occurred())) {
58 lineno = lineno_test;
59 }
60 else {
61 PyErr_Clear();
62 }
63 }
64 Py_DECREF(lineno_py);
65 }
66 else {
67 /* This should never happen, print the error. */
68 PyErr_Print();
69 }
70 }
71 return lineno;
72}
73
74static int parse_syntax_error(PyObject *err,
75 PyObject **message,
76 PyObject **filename,
77 int *lineno,
78 int *offset,
79 int *end_lineno,
80 int *end_offset,
81 PyObject **text)
82{
83 Py_ssize_t hold;
84 PyObject *v;
85
86 *message = nullptr;
87 *filename = nullptr;
88
89 /* New style errors. `err` is an instance. */
90 *message = _PyObject_GetAttrId(err, &PyId_msg);
91 if (!*message) {
92 goto finally;
93 }
94
95 v = _PyObject_GetAttrId(err, &PyId_filename);
96 if (!v) {
97 goto finally;
98 }
99 if (v == Py_None) {
100 Py_DECREF(v);
101 *filename = _PyUnicode_FromId(&PyId_string);
102 if (*filename == nullptr) {
103 goto finally;
104 }
105 Py_INCREF(*filename);
106 }
107 else {
108 *filename = v;
109 }
110
111 v = _PyObject_GetAttrId(err, &PyId_lineno);
112 if (!v) {
113 goto finally;
114 }
115 hold = PyLong_AsSsize_t(v);
116 Py_DECREF(v);
117 if (hold < 0 && PyErr_Occurred()) {
118 goto finally;
119 }
120 *lineno = int(hold);
121
122 v = _PyObject_GetAttrId(err, &PyId_offset);
123 if (!v) {
124 goto finally;
125 }
126 if (v == Py_None) {
127 *offset = -1;
128 Py_DECREF(v);
129 }
130 else {
131 hold = PyLong_AsSsize_t(v);
132 Py_DECREF(v);
133 if (hold < 0 && PyErr_Occurred()) {
134 goto finally;
135 }
136 *offset = int(hold);
137 }
138
139 if (Py_TYPE(err) == reinterpret_cast<PyTypeObject *>(PyExc_SyntaxError)) {
140 v = _PyObject_GetAttrId(err, &PyId_end_lineno);
141 if (!v) {
142 PyErr_Clear();
143 *end_lineno = *lineno;
144 }
145 else if (v == Py_None) {
146 *end_lineno = *lineno;
147 Py_DECREF(v);
148 }
149 else {
150 hold = PyLong_AsSsize_t(v);
151 Py_DECREF(v);
152 if (hold < 0 && PyErr_Occurred()) {
153 goto finally;
154 }
155 *end_lineno = hold;
156 }
157
158 v = _PyObject_GetAttrId(err, &PyId_end_offset);
159 if (!v) {
160 PyErr_Clear();
161 *end_offset = -1;
162 }
163 else if (v == Py_None) {
164 *end_offset = -1;
165 Py_DECREF(v);
166 }
167 else {
168 hold = PyLong_AsSsize_t(v);
169 Py_DECREF(v);
170 if (hold < 0 && PyErr_Occurred()) {
171 goto finally;
172 }
173 *end_offset = hold;
174 }
175 }
176 else {
177 /* `SyntaxError` subclasses. */
178 *end_lineno = *lineno;
179 *end_offset = -1;
180 }
181
182 v = _PyObject_GetAttrId(err, &PyId_text);
183 if (!v) {
184 goto finally;
185 }
186 if (v == Py_None) {
187 Py_DECREF(v);
188 *text = nullptr;
189 }
190 else {
191 *text = v;
192 }
193 return 1;
194
195finally:
196 Py_XDECREF(*message);
197 Py_XDECREF(*filename);
198 return 0;
199}
200/* end copied function! */
201
203 const char *filepath, int *r_lineno, int *r_offset, int *r_lineno_end, int *r_offset_end)
204{
205 bool success = false;
206 PyObject *exception, *value, *tb;
207
208 *r_lineno = -1;
209 *r_offset = 0;
210
211 *r_lineno_end = -1;
212 *r_offset_end = 0;
213
214 PyErr_Fetch(&exception, &value, &tb);
215 if (exception == nullptr) { /* Equivalent of `!PyErr_Occurred()`. */
216 return false;
217 }
218 PyObject *base_exception_type = nullptr;
219 if (PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError)) {
220 base_exception_type = PyExc_SyntaxError;
221 }
222
223 PyErr_NormalizeException(&exception, &value, &tb);
224
225 if (base_exception_type == PyExc_SyntaxError) {
226 /* No trace-back available when `SyntaxError`.
227 * Python has no API for this. reference #parse_syntax_error() from `pythonrun.c`. */
228
229 if (value) { /* Should always be true. */
230 PyObject *message;
231 PyObject *filepath_exc_py, *text_py;
232
233 if (parse_syntax_error(value,
234 &message,
235 &filepath_exc_py,
236 r_lineno,
237 r_offset,
238 r_lineno_end,
239 r_offset_end,
240 &text_py))
241 {
242 const char *filepath_exc = PyUnicode_AsUTF8(filepath_exc_py);
243 /* Python adds a '/', prefix, so check for both. */
244 if ((BLI_path_cmp(filepath_exc, filepath) == 0) ||
245 (ELEM(filepath_exc[0], '\\', '/') && BLI_path_cmp(filepath_exc + 1, filepath) == 0))
246 {
247 success = true;
248 }
249 Py_DECREF(message);
250 Py_DECREF(filepath_exc_py);
251 Py_XDECREF(text_py);
252 }
253 }
254 }
255 else {
256 for (PyTracebackObject *tb_iter = reinterpret_cast<PyTracebackObject *>(tb);
257 tb_iter && reinterpret_cast<PyObject *>(tb_iter) != Py_None;
258 tb_iter = tb_iter->tb_next)
259 {
260 PyObject *coerce;
261 const char *tb_filepath = traceback_filepath(tb_iter, &coerce);
262 const int match = ((BLI_path_cmp(tb_filepath, filepath) == 0) ||
263 (ELEM(tb_filepath[0], '\\', '/') &&
264 BLI_path_cmp(tb_filepath + 1, filepath) == 0));
265 Py_DECREF(coerce);
266
267 if (match) {
268 /* Even though a match has been found, keep searching to find the inner most line. */
269 success = true;
270 *r_lineno = *r_lineno_end = traceback_line_number(tb_iter);
271 }
272 }
273 }
274
275 PyErr_Restore(exception, value, tb);
276
277 return success;
278}
279
280} // namespace blender
#define BLI_path_cmp
#define ELEM(...)
#define MAKE_PY_IDENTIFIER_EX(varname, value)
std::string filename(const std::string_view &str, size_t pos)
BMesh Iterators.
static int traceback_line_number(PyTracebackObject *tb)
ATTR_WARN_UNUSED_RESULT const BMVert * v
static int parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename, int *lineno, int *offset, int *end_lineno, int *end_offset, PyObject **text)
bool python_script_error_jump(const char *filepath, int *r_lineno, int *r_offset, int *r_lineno_end, int *r_offset_end)
MAKE_PY_IDENTIFIER(msg)
static const char * traceback_filepath(PyTracebackObject *tb, PyObject **r_coerce)