Blender V4.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
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 PyErr_Clear();
67 }
68 }
69 return lineno;
70}
71
72static int parse_syntax_error(PyObject *err,
73 PyObject **message,
74 PyObject **filename,
75 int *lineno,
76 int *offset,
77 int *end_lineno,
78 int *end_offset,
79 PyObject **text)
80{
81 Py_ssize_t hold;
82 PyObject *v;
83
84 *message = nullptr;
85 *filename = nullptr;
86
87 /* new style errors. `err' is an instance */
88 *message = _PyObject_GetAttrId(err, &PyId_msg);
89 if (!*message) {
90 goto finally;
91 }
92
93 v = _PyObject_GetAttrId(err, &PyId_filename);
94 if (!v) {
95 goto finally;
96 }
97 if (v == Py_None) {
98 Py_DECREF(v);
99 *filename = _PyUnicode_FromId(&PyId_string);
100 if (*filename == nullptr) {
101 goto finally;
102 }
103 Py_INCREF(*filename);
104 }
105 else {
106 *filename = v;
107 }
108
109 v = _PyObject_GetAttrId(err, &PyId_lineno);
110 if (!v) {
111 goto finally;
112 }
113 hold = PyLong_AsSsize_t(v);
114 Py_DECREF(v);
115 if (hold < 0 && PyErr_Occurred()) {
116 goto finally;
117 }
118 *lineno = int(hold);
119
120 v = _PyObject_GetAttrId(err, &PyId_offset);
121 if (!v) {
122 goto finally;
123 }
124 if (v == Py_None) {
125 *offset = -1;
126 Py_DECREF(v);
127 }
128 else {
129 hold = PyLong_AsSsize_t(v);
130 Py_DECREF(v);
131 if (hold < 0 && PyErr_Occurred()) {
132 goto finally;
133 }
134 *offset = int(hold);
135 }
136
137 if (Py_TYPE(err) == (PyTypeObject *)PyExc_SyntaxError) {
138 v = _PyObject_GetAttrId(err, &PyId_end_lineno);
139 if (!v) {
140 PyErr_Clear();
141 *end_lineno = *lineno;
142 }
143 else if (v == Py_None) {
144 *end_lineno = *lineno;
145 Py_DECREF(v);
146 }
147 else {
148 hold = PyLong_AsSsize_t(v);
149 Py_DECREF(v);
150 if (hold < 0 && PyErr_Occurred()) {
151 goto finally;
152 }
153 *end_lineno = hold;
154 }
155
156 v = _PyObject_GetAttrId(err, &PyId_end_offset);
157 if (!v) {
158 PyErr_Clear();
159 *end_offset = -1;
160 }
161 else if (v == Py_None) {
162 *end_offset = -1;
163 Py_DECREF(v);
164 }
165 else {
166 hold = PyLong_AsSsize_t(v);
167 Py_DECREF(v);
168 if (hold < 0 && PyErr_Occurred()) {
169 goto finally;
170 }
171 *end_offset = hold;
172 }
173 }
174 else {
175 /* `SyntaxError` subclasses. */
176 *end_lineno = *lineno;
177 *end_offset = -1;
178 }
179
180 v = _PyObject_GetAttrId(err, &PyId_text);
181 if (!v) {
182 goto finally;
183 }
184 if (v == Py_None) {
185 Py_DECREF(v);
186 *text = nullptr;
187 }
188 else {
189 *text = v;
190 }
191 return 1;
192
193finally:
194 Py_XDECREF(*message);
195 Py_XDECREF(*filename);
196 return 0;
197}
198/* end copied function! */
199
201 const char *filepath, int *r_lineno, int *r_offset, int *r_lineno_end, int *r_offset_end)
202{
203 bool success = false;
204 PyObject *exception, *value, *tb;
205
206 *r_lineno = -1;
207 *r_offset = 0;
208
209 *r_lineno_end = -1;
210 *r_offset_end = 0;
211
212 PyErr_Fetch(&exception, &value, (PyObject **)&tb);
213 if (exception == nullptr) { /* Equivalent of `!PyErr_Occurred()`. */
214 return false;
215 }
216 PyObject *base_exception_type = nullptr;
217 if (PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError)) {
218 base_exception_type = PyExc_SyntaxError;
219 }
220
221 PyErr_NormalizeException(&exception, &value, &tb);
222
223 if (base_exception_type == PyExc_SyntaxError) {
224 /* No trace-back available when `SyntaxError`.
225 * Python has no API for this. reference #parse_syntax_error() from `pythonrun.c`. */
226
227 if (value) { /* Should always be true. */
228 PyObject *message;
229 PyObject *filepath_exc_py, *text_py;
230
231 if (parse_syntax_error(value,
232 &message,
233 &filepath_exc_py,
234 r_lineno,
235 r_offset,
236 r_lineno_end,
237 r_offset_end,
238 &text_py))
239 {
240 const char *filepath_exc = PyUnicode_AsUTF8(filepath_exc_py);
241 /* Python adds a '/', prefix, so check for both. */
242 if ((BLI_path_cmp(filepath_exc, filepath) == 0) ||
243 (ELEM(filepath_exc[0], '\\', '/') && BLI_path_cmp(filepath_exc + 1, filepath) == 0))
244 {
245 success = true;
246 }
247 }
248 }
249 }
250 else {
251 for (PyTracebackObject *tb_iter = (PyTracebackObject *)tb;
252 tb_iter && (PyObject *)tb_iter != Py_None;
253 tb_iter = tb_iter->tb_next)
254 {
255 PyObject *coerce;
256 const char *tb_filepath = traceback_filepath(tb_iter, &coerce);
257 const int match = ((BLI_path_cmp(tb_filepath, filepath) == 0) ||
258 (ELEM(tb_filepath[0], '\\', '/') &&
259 BLI_path_cmp(tb_filepath + 1, filepath) == 0));
260 Py_DECREF(coerce);
261
262 if (match) {
263 /* Even though a match has been found, keep searching to find the inner most line. */
264 success = true;
265 *r_lineno = *r_lineno_end = traceback_line_number(tb_iter);
266 }
267 }
268 }
269
270 PyErr_Restore(exception, value, tb);
271
272 return success;
273}
#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)
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int