2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02: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>
|
|
|
|
|
|
2024-09-26 21:13:39 +10:00
|
|
|
#include "BLI_path_utils.hh"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
2014-05-01 07:21:08 +10:00
|
|
|
#ifdef WIN32
|
|
|
|
|
# include "BLI_string.h" /* BLI_strcasecmp */
|
|
|
|
|
#endif
|
2012-03-08 02:19:41 +00:00
|
|
|
|
2024-09-24 12:23:25 +02:00
|
|
|
#include "bpy_traceback.hh"
|
2011-02-22 11:32:29 +00:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
#define MAKE_PY_IDENTIFIER_EX(varname, value) static _Py_Identifier varname{value, -1};
|
|
|
|
|
#define MAKE_PY_IDENTIFIER(varname) MAKE_PY_IDENTIFIER_EX(PyId_##varname, #varname)
|
|
|
|
|
|
|
|
|
|
MAKE_PY_IDENTIFIER_EX(PyId_string, "<string>")
|
|
|
|
|
MAKE_PY_IDENTIFIER(msg);
|
|
|
|
|
MAKE_PY_IDENTIFIER(filename);
|
|
|
|
|
MAKE_PY_IDENTIFIER(lineno);
|
|
|
|
|
MAKE_PY_IDENTIFIER(offset);
|
|
|
|
|
MAKE_PY_IDENTIFIER(end_lineno);
|
|
|
|
|
MAKE_PY_IDENTIFIER(end_offset);
|
2024-04-23 18:18:49 +10:00
|
|
|
MAKE_PY_IDENTIFIER(tb_lineno);
|
2023-07-21 02:18:59 +02:00
|
|
|
MAKE_PY_IDENTIFIER(text);
|
2014-05-02 06:24:29 +10:00
|
|
|
|
2024-04-23 18:18:49 +10:00
|
|
|
static const char *traceback_filepath(PyTracebackObject *tb, PyObject **r_coerce)
|
|
|
|
|
{
|
|
|
|
|
PyCodeObject *code = PyFrame_GetCode(tb->tb_frame);
|
|
|
|
|
*r_coerce = PyUnicode_EncodeFSDefault(code->co_filename);
|
|
|
|
|
return PyBytes_AS_STRING(*r_coerce);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Return the line number from the trace-back, -1 on failure. */
|
|
|
|
|
static int traceback_line_number(PyTracebackObject *tb)
|
|
|
|
|
{
|
|
|
|
|
int lineno = tb->tb_lineno;
|
|
|
|
|
if (lineno == -1) {
|
|
|
|
|
PyObject *lineno_py = _PyObject_GetAttrId((PyObject *)tb, &PyId_tb_lineno);
|
|
|
|
|
if (lineno_py) {
|
|
|
|
|
if (PyLong_Check(lineno_py)) {
|
|
|
|
|
const int lineno_test = PyLong_AsLongLong(lineno_py);
|
|
|
|
|
/* Theoretically could occur from overflow,
|
|
|
|
|
* internally these are `int` so it shouldn't happen. */
|
|
|
|
|
if (!((lineno_test == -1) && PyErr_Occurred())) {
|
|
|
|
|
lineno = lineno_test;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
PyErr_Clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Py_DECREF(lineno_py);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* This should never happen, print the error. */
|
|
|
|
|
PyErr_Print();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return lineno;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-02 06:24:29 +10: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,
|
2014-05-02 06:24:29 +10:00
|
|
|
PyObject **text)
|
2011-02-22 11:32:29 +00:00
|
|
|
{
|
2022-03-28 17:04:19 +11:00
|
|
|
Py_ssize_t hold;
|
2011-04-21 13:11:51 +00:00
|
|
|
PyObject *v;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
*message = nullptr;
|
|
|
|
|
*filename = nullptr;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2025-07-23 20:54:26 +10:00
|
|
|
/* New style errors. `err` is an instance. */
|
2012-12-04 20:09:07 +00:00
|
|
|
*message = _PyObject_GetAttrId(err, &PyId_msg);
|
2019-03-30 06:12:48 +11:00
|
|
|
if (!*message) {
|
2011-04-21 13:11:51 +00:00
|
|
|
goto finally;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-12-04 20:09:07 +00:00
|
|
|
v = _PyObject_GetAttrId(err, &PyId_filename);
|
2019-03-30 06:12:48 +11:00
|
|
|
if (!v) {
|
2011-04-21 13:11:51 +00:00
|
|
|
goto finally;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2012-12-04 20:09:07 +00:00
|
|
|
if (v == Py_None) {
|
|
|
|
|
Py_DECREF(v);
|
2014-05-02 06:24:29 +10:00
|
|
|
*filename = _PyUnicode_FromId(&PyId_string);
|
2023-07-21 02:18:59 +02:00
|
|
|
if (*filename == nullptr) {
|
2014-05-02 06:24:29 +10:00
|
|
|
goto finally;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2014-05-02 06:24:29 +10:00
|
|
|
Py_INCREF(*filename);
|
2012-12-04 20:09:07 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2014-05-02 06:24:29 +10:00
|
|
|
*filename = v;
|
2012-12-04 20:09:07 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-12-04 20:09:07 +00:00
|
|
|
v = _PyObject_GetAttrId(err, &PyId_lineno);
|
2019-03-30 06:12:48 +11:00
|
|
|
if (!v) {
|
2011-04-21 13:11:51 +00:00
|
|
|
goto finally;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2022-03-28 17:04:19 +11:00
|
|
|
hold = PyLong_AsSsize_t(v);
|
2011-04-21 13:11:51 +00:00
|
|
|
Py_DECREF(v);
|
2019-03-30 06:12:48 +11:00
|
|
|
if (hold < 0 && PyErr_Occurred()) {
|
2011-04-21 13:11:51 +00:00
|
|
|
goto finally;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2023-07-21 10:59:54 +10:00
|
|
|
*lineno = int(hold);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-12-04 20:09:07 +00:00
|
|
|
v = _PyObject_GetAttrId(err, &PyId_offset);
|
2019-03-30 06:12:48 +11:00
|
|
|
if (!v) {
|
2011-04-21 13:11:51 +00:00
|
|
|
goto finally;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2011-04-21 13:11:51 +00:00
|
|
|
if (v == Py_None) {
|
|
|
|
|
*offset = -1;
|
|
|
|
|
Py_DECREF(v);
|
2012-12-04 20:09:07 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2022-03-28 17:04:19 +11:00
|
|
|
hold = PyLong_AsSsize_t(v);
|
2011-04-21 13:11:51 +00:00
|
|
|
Py_DECREF(v);
|
2019-03-30 06:12:48 +11:00
|
|
|
if (hold < 0 && PyErr_Occurred()) {
|
2011-04-21 13:11:51 +00:00
|
|
|
goto finally;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2023-07-21 10:59:54 +10:00
|
|
|
*offset = int(hold);
|
2011-04-21 13:11:51 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-04 20:09:07 +00:00
|
|
|
v = _PyObject_GetAttrId(err, &PyId_text);
|
2019-03-30 06:12:48 +11:00
|
|
|
if (!v) {
|
2011-04-21 13:11:51 +00:00
|
|
|
goto finally;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2012-12-04 20:09:07 +00:00
|
|
|
if (v == Py_None) {
|
|
|
|
|
Py_DECREF(v);
|
2023-07-21 02:18:59 +02:00
|
|
|
*text = nullptr;
|
2012-12-04 20:09:07 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2014-05-02 06:24:29 +10:00
|
|
|
*text = v;
|
2012-12-04 20:09:07 +00:00
|
|
|
}
|
2011-04-21 13:11:51 +00:00
|
|
|
return 1;
|
2011-02-22 11:32:29 +00:00
|
|
|
|
|
|
|
|
finally:
|
2012-12-04 20:09:07 +00:00
|
|
|
Py_XDECREF(*message);
|
2014-05-02 06:24:29 +10:00
|
|
|
Py_XDECREF(*filename);
|
2011-04-21 13:11:51 +00:00
|
|
|
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;
|
2023-09-15 13:13:07 +10:00
|
|
|
PyObject *exception, *value, *tb;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
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;
|
|
|
|
|
|
2025-01-26 20:08:04 +01:00
|
|
|
PyErr_Fetch(&exception, &value, &tb);
|
2023-09-15 13:13:07 +10:00
|
|
|
if (exception == nullptr) { /* Equivalent of `!PyErr_Occurred()`. */
|
2022-03-28 17:11:34 +11:00
|
|
|
return false;
|
2022-03-28 15:05:41 +11:00
|
|
|
}
|
2023-09-15 13:13:07 +10:00
|
|
|
PyObject *base_exception_type = nullptr;
|
2022-03-28 15:05:41 +11:00
|
|
|
if (PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError)) {
|
2023-09-15 13:13:07 +10:00
|
|
|
base_exception_type = PyExc_SyntaxError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PyErr_NormalizeException(&exception, &value, &tb);
|
|
|
|
|
|
|
|
|
|
if (base_exception_type == PyExc_SyntaxError) {
|
2022-03-28 16:54:31 +11:00
|
|
|
/* No trace-back available when `SyntaxError`.
|
2023-09-15 13:13:07 +10:00
|
|
|
* Python has no API for this. reference #parse_syntax_error() from `pythonrun.c`. */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 16:54:31 +11:00
|
|
|
if (value) { /* Should always be true. */
|
2011-02-22 11:32:29 +00: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);
|
2023-09-15 13:13:07 +10: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;
|
2011-02-22 11:32:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2023-09-15 13:13:07 +10:00
|
|
|
for (PyTracebackObject *tb_iter = (PyTracebackObject *)tb;
|
2023-09-15 12:23:42 +10:00
|
|
|
tb_iter && (PyObject *)tb_iter != Py_None;
|
|
|
|
|
tb_iter = tb_iter->tb_next)
|
2011-12-26 12:26:11 +00:00
|
|
|
{
|
2011-08-26 17:55:03 +00:00
|
|
|
PyObject *coerce;
|
2023-09-15 12:23:42 +10:00
|
|
|
const char *tb_filepath = traceback_filepath(tb_iter, &coerce);
|
2012-03-30 05:26:08 +00:00
|
|
|
const int match = ((BLI_path_cmp(tb_filepath, filepath) == 0) ||
|
2020-03-07 00:58:48 +11:00
|
|
|
(ELEM(tb_filepath[0], '\\', '/') &&
|
2012-03-30 05:26:08 +00:00
|
|
|
BLI_path_cmp(tb_filepath + 1, filepath) == 0));
|
2011-08-26 17:55:03 +00:00
|
|
|
Py_DECREF(coerce);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-10-13 01:29:08 +00:00
|
|
|
if (match) {
|
2023-09-15 13:13:07 +10:00
|
|
|
/* Even though a match has been found, keep searching to find the inner most line. */
|
2022-03-28 17:11:34 +11:00
|
|
|
success = true;
|
2024-04-23 18:18:49 +10:00
|
|
|
*r_lineno = *r_lineno_end = traceback_line_number(tb_iter);
|
2011-02-22 11:32:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-28 17:11:34 +11:00
|
|
|
|
2023-09-15 13:13:07 +10:00
|
|
|
PyErr_Restore(exception, value, tb);
|
2022-04-06 16:15:11 +10:00
|
|
|
|
2022-03-28 17:11:34 +11:00
|
|
|
return success;
|
2011-02-22 11:32:29 +00:00
|
|
|
}
|