diff options
author | John Belmonte <john@neggie.net> | 2022-07-11 06:40:53 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-11 06:40:53 (GMT) |
commit | da717519ecd17bf6c7ed334c12ff861f63b0f14f (patch) | |
tree | 582df2633e6f0e769a797bb496aea0944c58a111 /Lib/traceback.py | |
parent | c9118afd045a64ca22d4a8cc5d43532607083b2d (diff) | |
download | cpython-da717519ecd17bf6c7ed334c12ff861f63b0f14f.zip cpython-da717519ecd17bf6c7ed334c12ff861f63b0f14f.tar.gz cpython-da717519ecd17bf6c7ed334c12ff861f63b0f14f.tar.bz2 |
gh-93883: elide traceback indicators when possible (#93994)
* gh-93883: elide traceback indicators when possible
Elide traceback column indicators when the entire line of the
frame is implicated. This reduces traceback length and draws
even more attention to the remaining (very relevant) indicators.
Example:
```
Traceback (most recent call last):
File "query.py", line 99, in <module>
bar()
File "query.py", line 66, in bar
foo()
File "query.py", line 37, in foo
magic_arithmetic('foo')
File "query.py", line 18, in magic_arithmetic
return add_counts(x) / 25
^^^^^^^^^^^^^
File "query.py", line 24, in add_counts
return 25 + query_user(user1) + query_user(user2)
^^^^^^^^^^^^^^^^^
File "query.py", line 32, in query_user
return 1 + query_count(db, response['a']['b']['c']['user'], retry=True)
~~~~~~~~~~~~~~~~~~^^^^^
TypeError: 'NoneType' object is not subscriptable
```
Rather than going out of our way to provide indicator coverage
in every traceback test suite, the indicator test suite should
be responible for sufficient coverage (e.g. by adding a basic
exception group test to ensure that margin strings are covered).
Diffstat (limited to 'Lib/traceback.py')
-rw-r--r-- | Lib/traceback.py | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/Lib/traceback.py b/Lib/traceback.py index 3afe49d..55f8080 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -465,7 +465,8 @@ class StackSummary(list): row.append(' File "{}", line {}, in {}\n'.format( frame_summary.filename, frame_summary.lineno, frame_summary.name)) if frame_summary.line: - row.append(' {}\n'.format(frame_summary.line.strip())) + stripped_line = frame_summary.line.strip() + row.append(' {}\n'.format(stripped_line)) orig_line_len = len(frame_summary._original_line) frame_line_len = len(frame_summary.line.lstrip()) @@ -486,19 +487,22 @@ class StackSummary(list): frame_summary._original_line[colno - 1:end_colno - 1] ) else: - end_colno = stripped_characters + len(frame_summary.line.strip()) - - row.append(' ') - row.append(' ' * (colno - stripped_characters)) - - if anchors: - row.append(anchors.primary_char * (anchors.left_end_offset)) - row.append(anchors.secondary_char * (anchors.right_start_offset - anchors.left_end_offset)) - row.append(anchors.primary_char * (end_colno - colno - anchors.right_start_offset)) - else: - row.append('^' * (end_colno - colno)) + end_colno = stripped_characters + len(stripped_line) + + # show indicators if primary char doesn't span the frame line + if end_colno - colno < len(stripped_line) or ( + anchors and anchors.right_start_offset - anchors.left_end_offset > 0): + row.append(' ') + row.append(' ' * (colno - stripped_characters)) + + if anchors: + row.append(anchors.primary_char * (anchors.left_end_offset)) + row.append(anchors.secondary_char * (anchors.right_start_offset - anchors.left_end_offset)) + row.append(anchors.primary_char * (end_colno - colno - anchors.right_start_offset)) + else: + row.append('^' * (end_colno - colno)) - row.append('\n') + row.append('\n') if frame_summary.locals: for name, value in sorted(frame_summary.locals.items()): |