Welcome to mirror list, hosted at ThFree Co, Russian Federation.

bpy_traceback.c « intern « python « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cb93843a6de6828d7a68e4326c92a7f598bee7a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* SPDX-License-Identifier: GPL-2.0-or-later */

/** \file
 * \ingroup pythonintern
 *
 * This file contains utility functions for getting data from a python stack
 * trace.
 */

#include <Python.h>
#include <frameobject.h>

#include "BLI_path_util.h"
#include "BLI_utildefines.h"
#ifdef WIN32
#  include "BLI_string.h" /* BLI_strcasecmp */
#endif

#include "bpy_traceback.h"

static const char *traceback_filepath(PyTracebackObject *tb, PyObject **coerce)
{
  *coerce = PyUnicode_EncodeFSDefault(tb->tb_frame->f_code->co_filename);
  return PyBytes_AS_STRING(*coerce);
}

/* copied from pythonrun.c, 3.10.0 */
_Py_static_string(PyId_string, "<string>");

static int parse_syntax_error(PyObject *err,
                              PyObject **message,
                              PyObject **filename,
                              int *lineno,
                              int *offset,
                              int *end_lineno,
                              int *end_offset,
                              PyObject **text)
{
  Py_ssize_t hold;
  PyObject *v;
  _Py_IDENTIFIER(msg);
  _Py_IDENTIFIER(filename);
  _Py_IDENTIFIER(lineno);
  _Py_IDENTIFIER(offset);
  _Py_IDENTIFIER(end_lineno);
  _Py_IDENTIFIER(end_offset);
  _Py_IDENTIFIER(text);

  *message = NULL;
  *filename = NULL;

  /* new style errors.  `err' is an instance */
  *message = _PyObject_GetAttrId(err, &PyId_msg);
  if (!*message) {
    goto finally;
  }

  v = _PyObject_GetAttrId(err, &PyId_filename);
  if (!v) {
    goto finally;
  }
  if (v == Py_None) {
    Py_DECREF(v);
    *filename = _PyUnicode_FromId(&PyId_string);
    if (*filename == NULL) {
      goto finally;
    }
    Py_INCREF(*filename);
  }
  else {
    *filename = v;
  }

  v = _PyObject_GetAttrId(err, &PyId_lineno);
  if (!v) {
    goto finally;
  }
  hold = PyLong_AsSsize_t(v);
  Py_DECREF(v);
  if (hold < 0 && PyErr_Occurred()) {
    goto finally;
  }
  *lineno = (int)hold;

  v = _PyObject_GetAttrId(err, &PyId_offset);
  if (!v) {
    goto finally;
  }
  if (v == Py_None) {
    *offset = -1;
    Py_DECREF(v);
  }
  else {
    hold = PyLong_AsSsize_t(v);
    Py_DECREF(v);
    if (hold < 0 && PyErr_Occurred()) {
      goto finally;
    }
    *offset = (int)hold;
  }

  if (Py_TYPE(err) == (PyTypeObject *)PyExc_SyntaxError) {
    v = _PyObject_GetAttrId(err, &PyId_end_lineno);
    if (!v) {
      PyErr_Clear();
      *end_lineno = *lineno;
    }
    else if (v == Py_None) {
      *end_lineno = *lineno;
      Py_DECREF(v);
    }
    else {
      hold = PyLong_AsSsize_t(v);
      Py_DECREF(v);
      if (hold < 0 && PyErr_Occurred()) {
        goto finally;
      }
      *end_lineno = hold;
    }

    v = _PyObject_GetAttrId(err, &PyId_end_offset);
    if (!v) {
      PyErr_Clear();
      *end_offset = -1;
    }
    else if (v == Py_None) {
      *end_offset = -1;
      Py_DECREF(v);
    }
    else {
      hold = PyLong_AsSsize_t(v);
      Py_DECREF(v);
      if (hold < 0 && PyErr_Occurred()) {
        goto finally;
      }
      *end_offset = hold;
    }
  }
  else {
    /* `SyntaxError` subclasses. */
    *end_lineno = *lineno;
    *end_offset = -1;
  }

  v = _PyObject_GetAttrId(err, &PyId_text);
  if (!v) {
    goto finally;
  }
  if (v == Py_None) {
    Py_DECREF(v);
    *text = NULL;
  }
  else {
    *text = v;
  }
  return 1;

finally:
  Py_XDECREF(*message);
  Py_XDECREF(*filename);
  return 0;
}
/* end copied function! */

bool python_script_error_jump(
    const char *filepath, int *r_lineno, int *r_offset, int *r_lineno_end, int *r_offset_end)
{
  /* WARNING(@campbellbarton): The normalized exception is restored (losing line number info).
   * Ideally this would leave the exception state as it found it, but that needs to be done
   * carefully with regards to reference counting, see: T97731. */

  bool success = false;
  PyObject *exception, *value;
  PyTracebackObject *tb;

  *r_lineno = -1;
  *r_offset = 0;

  *r_lineno_end = -1;
  *r_offset_end = 0;

  PyErr_Fetch(&exception, &value, (PyObject **)&tb);
  if (exception == NULL) {
    return false;
  }

  if (PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError)) {
    /* No trace-back available when `SyntaxError`.
     * Python has no API's to this. reference #parse_syntax_error() from `pythonrun.c`. */
    PyErr_NormalizeException(&exception, &value, (PyObject **)&tb);

    if (value) { /* Should always be true. */
      PyObject *message;
      PyObject *filepath_exc_py, *text_py;

      if (parse_syntax_error(value,
                             &message,
                             &filepath_exc_py,
                             r_lineno,
                             r_offset,
                             r_lineno_end,
                             r_offset_end,
                             &text_py)) {
        const char *filepath_exc = PyUnicode_AsUTF8(filepath_exc_py);
        /* python adds a '/', prefix, so check for both */
        if ((BLI_path_cmp(filepath_exc, filepath) == 0) ||
            (ELEM(filepath_exc[0], '\\', '/') && BLI_path_cmp(filepath_exc + 1, filepath) == 0)) {
          success = true;
        }
      }
    }
  }
  else {
    PyErr_NormalizeException(&exception, &value, (PyObject **)&tb);

    for (tb = (PyTracebackObject *)PySys_GetObject("last_traceback");
         tb && (PyObject *)tb != Py_None;
         tb = tb->tb_next) {
      PyObject *coerce;
      const char *tb_filepath = traceback_filepath(tb, &coerce);
      const int match = ((BLI_path_cmp(tb_filepath, filepath) == 0) ||
                         (ELEM(tb_filepath[0], '\\', '/') &&
                          BLI_path_cmp(tb_filepath + 1, filepath) == 0));
      Py_DECREF(coerce);

      if (match) {
        success = true;
        *r_lineno = *r_lineno_end = tb->tb_lineno;
        /* used to break here, but better find the inner most line */
      }
    }
  }

  PyErr_Restore(exception, value, (PyObject *)tb); /* takes away reference! */

  return success;
}