Skip to content

Commit

Permalink
pythongh-93883: elide traceback indicators when possible (python#93994)
Browse files Browse the repository at this point in the history
* pythongh-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).
  • Loading branch information
belm0 authored Jul 11, 2022
1 parent c9118af commit da71751
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 137 deletions.
13 changes: 4 additions & 9 deletions Doc/library/traceback.rst
Original file line number Diff line number Diff line change
Expand Up @@ -464,41 +464,36 @@ The output for the example would look similar to this:
*** print_tb:
File "<doctest...>", line 10, in <module>
lumberjack()
^^^^^^^^^^^^
*** print_exception:
Traceback (most recent call last):
File "<doctest...>", line 10, in <module>
lumberjack()
^^^^^^^^^^^^
File "<doctest...>", line 4, in lumberjack
bright_side_of_death()
^^^^^^^^^^^^^^^^^^^^^^
IndexError: tuple index out of range
*** print_exc:
Traceback (most recent call last):
File "<doctest...>", line 10, in <module>
lumberjack()
^^^^^^^^^^^^
File "<doctest...>", line 4, in lumberjack
bright_side_of_death()
^^^^^^^^^^^^^^^^^^^^^^
IndexError: tuple index out of range
*** format_exc, first and last line:
Traceback (most recent call last):
IndexError: tuple index out of range
*** format_exception:
['Traceback (most recent call last):\n',
' File "<doctest default[0]>", line 10, in <module>\n lumberjack()\n ^^^^^^^^^^^^\n',
' File "<doctest default[0]>", line 4, in lumberjack\n bright_side_of_death()\n ^^^^^^^^^^^^^^^^^^^^^^\n',
' File "<doctest default[0]>", line 10, in <module>\n lumberjack()\n',
' File "<doctest default[0]>", line 4, in lumberjack\n bright_side_of_death()\n',
' File "<doctest default[0]>", line 7, in bright_side_of_death\n return tuple()[0]\n ~~~~~~~^^^\n',
'IndexError: tuple index out of range\n']
*** extract_tb:
[<FrameSummary file <doctest...>, line 10 in <module>>,
<FrameSummary file <doctest...>, line 4 in lumberjack>,
<FrameSummary file <doctest...>, line 7 in bright_side_of_death>]
*** format_tb:
[' File "<doctest default[0]>", line 10, in <module>\n lumberjack()\n ^^^^^^^^^^^^\n',
' File "<doctest default[0]>", line 4, in lumberjack\n bright_side_of_death()\n ^^^^^^^^^^^^^^^^^^^^^^\n',
[' File "<doctest default[0]>", line 10, in <module>\n lumberjack()\n',
' File "<doctest default[0]>", line 4, in lumberjack\n bright_side_of_death()\n',
' File "<doctest default[0]>", line 7, in bright_side_of_death\n return tuple()[0]\n ~~~~~~~^^^\n']
*** tb_lineno: 10

Expand Down
1 change: 0 additions & 1 deletion Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ when dealing with deeply nested dictionary objects and multiple function calls,
Traceback (most recent call last):
File "query.py", line 37, in <module>
magic_arithmetic('foo')
^^^^^^^^^^^^^^^^^^^^^^^
File "query.py", line 18, in magic_arithmetic
return add_counts(x) / 25
^^^^^^^^^^^^^
Expand Down
13 changes: 4 additions & 9 deletions Lib/idlelib/idle_test/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from idlelib import run
import io
import sys
from test.support import captured_output, captured_stderr, has_no_debug_ranges
from test.support import captured_output, captured_stderr
import unittest
from unittest import mock
import idlelib
Expand Down Expand Up @@ -33,14 +33,9 @@ def __eq__(self, other):
run.print_exception()

tb = output.getvalue().strip().splitlines()
if has_no_debug_ranges():
self.assertEqual(11, len(tb))
self.assertIn('UnhashableException: ex2', tb[3])
self.assertIn('UnhashableException: ex1', tb[10])
else:
self.assertEqual(13, len(tb))
self.assertIn('UnhashableException: ex2', tb[4])
self.assertIn('UnhashableException: ex1', tb[12])
self.assertEqual(11, len(tb))
self.assertIn('UnhashableException: ex2', tb[3])
self.assertIn('UnhashableException: ex1', tb[10])

data = (('1/0', ZeroDivisionError, "division by zero\n"),
('abc', NameError, "name 'abc' is not defined. "
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_cmd_line_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,10 +549,10 @@ def test_pep_409_verbiage(self):
script_name = _make_test_script(script_dir, 'script', script)
exitcode, stdout, stderr = assert_python_failure(script_name)
text = stderr.decode('ascii').split('\n')
self.assertEqual(len(text), 6)
self.assertEqual(len(text), 5)
self.assertTrue(text[0].startswith('Traceback'))
self.assertTrue(text[1].startswith(' File '))
self.assertTrue(text[4].startswith('NameError'))
self.assertTrue(text[3].startswith('NameError'))

def test_non_ascii(self):
# Mac OS X denies the creation of a file with an invalid UTF-8 name.
Expand Down
4 changes: 1 addition & 3 deletions Lib/test/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2854,7 +2854,7 @@ def test_testmod(): r"""
# Skip the test: the filesystem encoding is unable to encode the filename
supports_unicode = False

if supports_unicode and not support.has_no_debug_ranges():
if supports_unicode:
def test_unicode(): """
Check doctest with a non-ascii filename:
Expand All @@ -2876,10 +2876,8 @@ def test_unicode(): """
Traceback (most recent call last):
File ...
exec(compile(example.source, filename, "single",
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<doctest foo-bär@baz[0]>", line 1, in <module>
raise Exception('clé')
^^^^^^^^^^^^^^^^^^^^^^
Exception: clé
TestResults(failed=1, attempted=1)
"""
Expand Down
Loading

0 comments on commit da71751

Please sign in to comment.