2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2011-02-22 11:32:29 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
* \ingroup pythonintern
|
2011-11-05 08:21:12 +00:00
|
|
|
*
|
|
|
|
* This file contains utility functions for getting data from a python stack
|
|
|
|
* trace.
|
2011-02-27 20:10:08 +00:00
|
|
|
*/
|
|
|
|
|
2011-02-22 11:32:29 +00:00
|
|
|
#include <Python.h>
|
|
|
|
#include <frameobject.h>
|
|
|
|
|
2012-03-08 02:19:41 +00:00
|
|
|
#include "BLI_path_util.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
2014-05-01 07:21:08 +10:00
|
|
|
#ifdef WIN32
|
2019-04-17 06:17:24 +02:00
|
|
|
# include "BLI_string.h" /* BLI_strcasecmp */
|
2014-05-01 07:21:08 +10:00
|
|
|
#endif
|
2012-03-08 02:19:41 +00:00
|
|
|
|
2011-02-22 11:32:29 +00:00
|
|
|
#include "bpy_traceback.h"
|
|
|
|
|
2011-08-26 17:55:03 +00:00
|
|
|
static const char *traceback_filepath(PyTracebackObject *tb, PyObject **coerce)
|
2011-02-22 11:32:29 +00:00
|
|
|
{
|
2020-07-03 17:35:31 +02:00
|
|
|
*coerce = PyUnicode_EncodeFSDefault(tb->tb_frame->f_code->co_filename);
|
|
|
|
return PyBytes_AS_STRING(*coerce);
|
2011-02-22 11:32:29 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 17:04:19 +11:00
|
|
|
/* copied from pythonrun.c, 3.10.0 */
|
2014-05-02 06:24:29 +10:00
|
|
|
_Py_static_string(PyId_string, "<string>");
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
static int parse_syntax_error(PyObject *err,
|
|
|
|
PyObject **message,
|
|
|
|
PyObject **filename,
|
|
|
|
int *lineno,
|
|
|
|
int *offset,
|
2022-03-28 17:04:19 +11:00
|
|
|
int *end_lineno,
|
|
|
|
int *end_offset,
|
2019-04-17 06:17:24 +02:00
|
|
|
PyObject **text)
|
2011-02-22 11:32:29 +00:00
|
|
|
{
|
2022-03-28 17:04:19 +11:00
|
|
|
Py_ssize_t hold;
|
2019-04-17 06:17:24 +02:00
|
|
|
PyObject *v;
|
|
|
|
_Py_IDENTIFIER(msg);
|
|
|
|
_Py_IDENTIFIER(filename);
|
|
|
|
_Py_IDENTIFIER(lineno);
|
|
|
|
_Py_IDENTIFIER(offset);
|
2022-03-28 17:04:19 +11:00
|
|
|
_Py_IDENTIFIER(end_lineno);
|
|
|
|
_Py_IDENTIFIER(end_offset);
|
2019-04-17 06:17:24 +02:00
|
|
|
_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;
|
|
|
|
}
|
2022-03-28 17:04:19 +11:00
|
|
|
hold = PyLong_AsSsize_t(v);
|
2019-04-17 06:17:24 +02:00
|
|
|
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 {
|
2022-03-28 17:04:19 +11:00
|
|
|
hold = PyLong_AsSsize_t(v);
|
2019-04-17 06:17:24 +02:00
|
|
|
Py_DECREF(v);
|
|
|
|
if (hold < 0 && PyErr_Occurred()) {
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
*offset = (int)hold;
|
|
|
|
}
|
|
|
|
|
2022-03-28 17:04:19 +11:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
v = _PyObject_GetAttrId(err, &PyId_text);
|
|
|
|
if (!v) {
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
if (v == Py_None) {
|
|
|
|
Py_DECREF(v);
|
|
|
|
*text = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*text = v;
|
|
|
|
}
|
|
|
|
return 1;
|
2011-02-22 11:32:29 +00:00
|
|
|
|
|
|
|
finally:
|
2019-04-17 06:17:24 +02:00
|
|
|
Py_XDECREF(*message);
|
|
|
|
Py_XDECREF(*filename);
|
|
|
|
return 0;
|
2011-02-22 11:32:29 +00:00
|
|
|
}
|
|
|
|
/* end copied function! */
|
|
|
|
|
2022-03-28 17:11:34 +11:00
|
|
|
bool python_script_error_jump(
|
2022-03-28 17:04:19 +11:00
|
|
|
const char *filepath, int *r_lineno, int *r_offset, int *r_lineno_end, int *r_offset_end)
|
2011-02-22 11:32:29 +00:00
|
|
|
{
|
2022-03-28 17:11:34 +11:00
|
|
|
bool success = false;
|
2019-04-17 06:17:24 +02:00
|
|
|
PyObject *exception, *value;
|
|
|
|
PyTracebackObject *tb;
|
|
|
|
|
2022-03-28 16:54:31 +11:00
|
|
|
*r_lineno = -1;
|
|
|
|
*r_offset = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 17:04:19 +11:00
|
|
|
*r_lineno_end = -1;
|
|
|
|
*r_offset_end = 0;
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
PyErr_Fetch(&exception, &value, (PyObject **)&tb);
|
2022-03-28 15:05:41 +11:00
|
|
|
if (exception == NULL) {
|
2022-03-28 17:11:34 +11:00
|
|
|
return false;
|
2022-03-28 15:05:41 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 15:05:41 +11:00
|
|
|
if (PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError)) {
|
2022-03-28 16:54:31 +11:00
|
|
|
/* No trace-back available when `SyntaxError`.
|
|
|
|
* Python has no API's to this. reference #parse_syntax_error() from `pythonrun.c`. */
|
2019-04-17 06:17:24 +02:00
|
|
|
PyErr_NormalizeException(&exception, &value, (PyObject **)&tb);
|
|
|
|
|
2022-03-28 16:54:31 +11:00
|
|
|
if (value) { /* Should always be true. */
|
2019-04-17 06:17:24 +02:00
|
|
|
PyObject *message;
|
2022-03-28 16:54:31 +11:00
|
|
|
PyObject *filepath_exc_py, *text_py;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 17:04:19 +11:00
|
|
|
if (parse_syntax_error(value,
|
|
|
|
&message,
|
|
|
|
&filepath_exc_py,
|
|
|
|
r_lineno,
|
|
|
|
r_offset,
|
|
|
|
r_lineno_end,
|
|
|
|
r_offset_end,
|
|
|
|
&text_py)) {
|
2022-03-28 16:54:31 +11:00
|
|
|
const char *filepath_exc = PyUnicode_AsUTF8(filepath_exc_py);
|
2019-04-17 06:17:24 +02:00
|
|
|
/* python adds a '/', prefix, so check for both */
|
2022-03-28 16:54:31 +11:00
|
|
|
if ((BLI_path_cmp(filepath_exc, filepath) == 0) ||
|
|
|
|
(ELEM(filepath_exc[0], '\\', '/') && BLI_path_cmp(filepath_exc + 1, filepath) == 0)) {
|
2022-03-28 17:11:34 +11:00
|
|
|
success = true;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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) ||
|
2020-03-07 00:58:48 +11:00
|
|
|
(ELEM(tb_filepath[0], '\\', '/') &&
|
2019-04-17 06:17:24 +02:00
|
|
|
BLI_path_cmp(tb_filepath + 1, filepath) == 0));
|
|
|
|
Py_DECREF(coerce);
|
|
|
|
|
|
|
|
if (match) {
|
2022-03-28 17:11:34 +11:00
|
|
|
success = true;
|
2022-03-28 17:04:19 +11:00
|
|
|
*r_lineno = *r_lineno_end = tb->tb_lineno;
|
2019-04-17 06:17:24 +02:00
|
|
|
/* used to break here, but better find the inner most line */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-28 17:11:34 +11:00
|
|
|
|
2022-04-06 16:15:11 +10:00
|
|
|
PyErr_Restore(exception, value, (PyObject *)tb); /* takes away reference! */
|
|
|
|
|
2022-03-28 17:11:34 +11:00
|
|
|
return success;
|
2011-02-22 11:32:29 +00:00
|
|
|
}
|