diff options
author | Batuhan Taskaya <batuhan@python.org> | 2021-07-26 01:01:44 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-25 15:01:44 -0700 |
commit | 3e235e0447e373d81f195f4292959c7be9c013dc (patch) | |
tree | 3fe06bef120b56ff47d75abd70b32db050b7c99b /Lib/traceback.py | |
parent | 96cf5a63d2dbadaebf236362b4c7c09c51fda55c (diff) | |
download | cpython-3e235e0447e373d81f195f4292959c7be9c013dc.tar.gz cpython-3e235e0447e373d81f195f4292959c7be9c013dc.zip |
bpo-43950: support some multi-line expressions for PEP 657 (GH-27339)
This is basically something that I noticed up while fixing test runs for another issue. It is really common to have multiline calls, and when they fail the display is kind of weird since we omit the annotations. E.g;
```
$ ./python t.py
Traceback (most recent call last):
File "/home/isidentical/cpython/cpython/t.py", line 11, in <module>
frame_1()
^^^^^^^^^
File "/home/isidentical/cpython/cpython/t.py", line 5, in frame_1
frame_2(
File "/home/isidentical/cpython/cpython/t.py", line 2, in frame_2
return a / 0 / b / c
~~^~~
ZeroDivisionError: division by zero
```
This patch basically adds support for annotating the rest of the line, if the instruction covers multiple lines (start_line != end_line).
Automerge-Triggered-By: GH:isidentical
Diffstat (limited to 'Lib/traceback.py')
-rw-r--r-- | Lib/traceback.py | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/Lib/traceback.py b/Lib/traceback.py index 15bdb3c8c2e..4ad8c9a17b3 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -4,6 +4,7 @@ import collections import itertools import linecache import sys +from contextlib import suppress __all__ = ['extract_stack', 'extract_tb', 'format_exception', 'format_exception_only', 'format_list', 'format_stack', @@ -463,19 +464,20 @@ class StackSummary(list): stripped_characters = len(frame._original_line) - len(frame.line.lstrip()) if ( - frame.end_lineno == frame.lineno - and frame.colno is not None + frame.colno is not None and frame.end_colno is not None ): colno = _byte_offset_to_character_offset(frame._original_line, frame.colno) end_colno = _byte_offset_to_character_offset(frame._original_line, frame.end_colno) - try: - anchors = _extract_caret_anchors_from_line_segment( - frame._original_line[colno - 1:end_colno - 1] - ) - except Exception: - anchors = None + anchors = None + if frame.lineno == frame.end_lineno: + with suppress(Exception): + anchors = _extract_caret_anchors_from_line_segment( + frame._original_line[colno - 1:end_colno - 1] + ) + else: + end_colno = stripped_characters + len(frame.line.strip()) row.append(' ') row.append(' ' * (colno - stripped_characters)) |