Skip to content

Commit 9a31711

Browse files
committed
Address feedback from @isidentical
1 parent 17fd03b commit 9a31711

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

Python/traceback.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -537,26 +537,31 @@ tb_displayline(PyTracebackObject* tb, PyObject *f, PyObject *filename, int linen
537537
/* ignore errors since we can't report them, can we? */
538538
if (!_Py_DisplaySourceLine(f, filename, lineno, _TRACEBACK_SOURCE_LINE_INDENT, &truncation, &source_line)) {
539539
int code_offset = tb->tb_lasti;
540-
if (PyCode_Addr2Line(_PyFrame_GetCode(frame), code_offset) != _PyCode_Addr2EndLine(_PyFrame_GetCode(frame), code_offset)) {
540+
PyCodeObject* code = _PyFrame_GetCode(frame);
541+
542+
int start_line;
543+
int end_line;
544+
int start_col_byte_offset;
545+
int end_col_byte_offset;
546+
if (!PyCode_Addr2Location(code, code_offset, &start_line, &start_col_byte_offset,
547+
&end_line, &end_col_byte_offset)) {
548+
goto done;
549+
}
550+
if (start_line != end_line) {
541551
goto done;
542552
}
543553

544-
Py_ssize_t start_offset = (Py_ssize_t) _PyCode_Addr2Offset(_PyFrame_GetCode(frame), code_offset);
545-
Py_ssize_t end_offset = (Py_ssize_t) _PyCode_Addr2EndOffset(_PyFrame_GetCode(frame), code_offset);
546-
547-
if (start_offset < 0 || end_offset < 0) {
554+
if (start_col_byte_offset < 0 || end_col_byte_offset < 0) {
548555
goto done;
549556
}
550-
if (end_offset == -1) {
557+
if (end_col_byte_offset == -1) {
551558
// TODO: highlight from start_offset to the end of line
552559
goto done;
553560
}
554561
// Convert the utf-8 byte offset to the actual character offset so we
555-
// print the right number of carets. We do -1 here because the column
556-
// offsets provided by _PyCode_Addr2Offset and _PyCode_Addr2EndOffset
557-
// are 1-indexed, not 0-indexed.
558-
start_offset = _byte_offset_to_character_offset(source_line, start_offset);
559-
end_offset = _byte_offset_to_character_offset(source_line, end_offset);
562+
// print the right number of carets.
563+
Py_ssize_t start_offset = _byte_offset_to_character_offset(source_line, start_col_byte_offset);
564+
Py_ssize_t end_offset = _byte_offset_to_character_offset(source_line, end_col_byte_offset);
560565

561566
char offset = truncation;
562567
while (++offset <= start_offset) {
@@ -986,3 +991,4 @@ _Py_DumpTracebackThreads(int fd, PyInterpreterState *interp,
986991

987992
return NULL;
988993
}
994+

0 commit comments

Comments
 (0)