aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/src/Python/traceback.c
diff options
context:
space:
mode:
authorshadchin <shadchin@yandex-team.com>2023-12-13 02:43:57 +0300
committershadchin <shadchin@yandex-team.com>2023-12-13 03:08:48 +0300
commit5b48aabc614c6d407f885f3b228dc484ad4c5ba9 (patch)
tree602eb5cc5d85bf730c1de1fa50a13c2ee552830d /contrib/tools/python3/src/Python/traceback.c
parent35d7049b38602e8cbfcd3f96257329a1abce947e (diff)
downloadydb-5b48aabc614c6d407f885f3b228dc484ad4c5ba9.tar.gz
Update Python 3 to 3.11.7
Diffstat (limited to 'contrib/tools/python3/src/Python/traceback.c')
-rw-r--r--contrib/tools/python3/src/Python/traceback.c73
1 files changed, 65 insertions, 8 deletions
diff --git a/contrib/tools/python3/src/Python/traceback.c b/contrib/tools/python3/src/Python/traceback.c
index c4f5ec877b..722f007459 100644
--- a/contrib/tools/python3/src/Python/traceback.c
+++ b/contrib/tools/python3/src/Python/traceback.c
@@ -112,6 +112,29 @@ tb_next_get(PyTracebackObject *self, void *Py_UNUSED(_))
}
static int
+tb_get_lineno(PyTracebackObject* tb) {
+ PyFrameObject* frame = tb->tb_frame;
+ assert(frame != NULL);
+ PyCodeObject *code = PyFrame_GetCode(frame);
+ int lineno = PyCode_Addr2Line(code, tb->tb_lasti);
+ Py_DECREF(code);
+ return lineno;
+}
+
+static PyObject *
+tb_lineno_get(PyTracebackObject *self, void *Py_UNUSED(_))
+{
+ int lineno = self->tb_lineno;
+ if (lineno == -1) {
+ lineno = tb_get_lineno(self);
+ if (lineno < 0) {
+ Py_RETURN_NONE;
+ }
+ }
+ return PyLong_FromLong(lineno);
+}
+
+static int
tb_next_set(PyTracebackObject *self, PyObject *new_next, void *Py_UNUSED(_))
{
if (!new_next) {
@@ -157,12 +180,12 @@ static PyMethodDef tb_methods[] = {
static PyMemberDef tb_memberlist[] = {
{"tb_frame", T_OBJECT, OFF(tb_frame), READONLY|PY_AUDIT_READ},
{"tb_lasti", T_INT, OFF(tb_lasti), READONLY},
- {"tb_lineno", T_INT, OFF(tb_lineno), READONLY},
{NULL} /* Sentinel */
};
static PyGetSetDef tb_getsetters[] = {
{"tb_next", (getter)tb_next_get, (setter)tb_next_set, NULL, NULL},
+ {"tb_lineno", (getter)tb_lineno_get, NULL, NULL, NULL},
{NULL} /* Sentinel */
};
@@ -241,8 +264,7 @@ _PyTraceBack_FromFrame(PyObject *tb_next, PyFrameObject *frame)
assert(tb_next == NULL || PyTraceBack_Check(tb_next));
assert(frame != NULL);
int addr = _PyInterpreterFrame_LASTI(frame->f_frame) * sizeof(_Py_CODEUNIT);
- return tb_create_raw((PyTracebackObject *)tb_next, frame, addr,
- PyFrame_GetLineNumber(frame));
+ return tb_create_raw((PyTracebackObject *)tb_next, frame, addr, -1);
}
@@ -907,8 +929,39 @@ tb_displayline(PyTracebackObject* tb, PyObject *f, PyObject *filename, int linen
goto done;
}
- if (print_error_location_carets(f, truncation, start_offset, end_offset,
- right_start_offset, left_end_offset,
+ // Convert all offsets to display offsets (e.g. the space they would take up if printed
+ // on the screen).
+ Py_ssize_t dp_start = _PyPegen_calculate_display_width(source_line, start_offset);
+ if (dp_start < 0) {
+ err = ignore_source_errors() < 0;
+ goto done;
+ }
+
+ Py_ssize_t dp_end = _PyPegen_calculate_display_width(source_line, end_offset);
+ if (dp_end < 0) {
+ err = ignore_source_errors() < 0;
+ goto done;
+ }
+
+ Py_ssize_t dp_left_end = -1;
+ Py_ssize_t dp_right_start = -1;
+ if (has_secondary_ranges) {
+ dp_left_end = _PyPegen_calculate_display_width(source_line, left_end_offset);
+ if (dp_left_end < 0) {
+ err = ignore_source_errors() < 0;
+ goto done;
+ }
+
+ dp_right_start = _PyPegen_calculate_display_width(source_line, right_start_offset);
+ if (dp_right_start < 0) {
+ err = ignore_source_errors() < 0;
+ goto done;
+ }
+ }
+
+
+ if (print_error_location_carets(f, truncation, dp_start, dp_end,
+ dp_right_start, dp_left_end,
primary_error_char, secondary_error_char) < 0) {
err = -1;
goto done;
@@ -959,9 +1012,13 @@ tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit,
}
while (tb != NULL) {
code = PyFrame_GetCode(tb->tb_frame);
+ int tb_lineno = tb->tb_lineno;
+ if (tb_lineno == -1) {
+ tb_lineno = tb_get_lineno(tb);
+ }
if (last_file == NULL ||
code->co_filename != last_file ||
- last_line == -1 || tb->tb_lineno != last_line ||
+ last_line == -1 || tb_lineno != last_line ||
last_name == NULL || code->co_name != last_name) {
if (cnt > TB_RECURSIVE_CUTOFF) {
if (tb_print_line_repeated(f, cnt) < 0) {
@@ -969,13 +1026,13 @@ tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit,
}
}
last_file = code->co_filename;
- last_line = tb->tb_lineno;
+ last_line = tb_lineno;
last_name = code->co_name;
cnt = 0;
}
cnt++;
if (cnt <= TB_RECURSIVE_CUTOFF) {
- if (tb_displayline(tb, f, code->co_filename, tb->tb_lineno,
+ if (tb_displayline(tb, f, code->co_filename, tb_lineno,
tb->tb_frame, code->co_name, indent, margin) < 0) {
goto error;
}