diff options
Diffstat (limited to 'source/blender/python/intern/bpy_traceback.c')
-rw-r--r-- | source/blender/python/intern/bpy_traceback.c | 109 |
1 files changed, 84 insertions, 25 deletions
diff --git a/source/blender/python/intern/bpy_traceback.c b/source/blender/python/intern/bpy_traceback.c index 13af254c286..40478f3613c 100644 --- a/source/blender/python/intern/bpy_traceback.c +++ b/source/blender/python/intern/bpy_traceback.c @@ -24,7 +24,7 @@ static const char *traceback_filepath(PyTracebackObject *tb, PyObject **coerce) return PyBytes_AS_STRING(*coerce); } -/* copied from pythonrun.c, 3.4.0 */ +/* copied from pythonrun.c, 3.10.0 */ _Py_static_string(PyId_string, "<string>"); static int parse_syntax_error(PyObject *err, @@ -32,14 +32,18 @@ static int parse_syntax_error(PyObject *err, PyObject **filename, int *lineno, int *offset, + int *end_lineno, + int *end_offset, PyObject **text) { - long hold; + 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; @@ -71,7 +75,7 @@ static int parse_syntax_error(PyObject *err, if (!v) { goto finally; } - hold = PyLong_AsLong(v); + hold = PyLong_AsSsize_t(v); Py_DECREF(v); if (hold < 0 && PyErr_Occurred()) { goto finally; @@ -87,7 +91,7 @@ static int parse_syntax_error(PyObject *err, Py_DECREF(v); } else { - hold = PyLong_AsLong(v); + hold = PyLong_AsSsize_t(v); Py_DECREF(v); if (hold < 0 && PyErr_Occurred()) { goto finally; @@ -95,6 +99,49 @@ static int parse_syntax_error(PyObject *err, *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; @@ -115,39 +162,48 @@ finally: } /* end copied function! */ -void python_script_error_jump(const char *filepath, int *lineno, int *offset) +bool python_script_error_jump( + const char *filepath, int *r_lineno, int *r_offset, int *r_lineno_end, int *r_offset_end) { + bool success = false; PyObject *exception, *value; PyTracebackObject *tb; - *lineno = -1; - *offset = 0; + *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 (exception && 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 */ + 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 */ + if (value) { /* Should always be true. */ PyObject *message; - PyObject *filename_py, *text_py; - - if (parse_syntax_error(value, &message, &filename_py, lineno, offset, &text_py)) { - const char *filename = PyUnicode_AsUTF8(filename_py); + 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(filename, filepath) == 0) || - (ELEM(filename[0], '\\', '/') && BLI_path_cmp(filename + 1, filepath) == 0)) { - /* good */ - } - else { - *lineno = -1; + if ((BLI_path_cmp(filepath_exc, filepath) == 0) || + (ELEM(filepath_exc[0], '\\', '/') && BLI_path_cmp(filepath_exc + 1, filepath) == 0)) { + success = true; } } - else { - *lineno = -1; - } } PyErr_Restore(exception, value, (PyObject *)tb); /* takes away reference! */ } @@ -167,9 +223,12 @@ void python_script_error_jump(const char *filepath, int *lineno, int *offset) Py_DECREF(coerce); if (match) { - *lineno = tb->tb_lineno; + success = true; + *r_lineno = *r_lineno_end = tb->tb_lineno; /* used to break here, but better find the inner most line */ } } } + + return success; } |