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