Skip to content

Commit da71751

Browse files
authored
pythongh-93883: elide traceback indicators when possible (python#93994)
* 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).
1 parent c9118af commit da71751

File tree

9 files changed

+113
-137
lines changed

9 files changed

+113
-137
lines changed

Doc/library/traceback.rst

+4-9
Original file line numberDiff line numberDiff line change
@@ -464,41 +464,36 @@ The output for the example would look similar to this:
464464
*** print_tb:
465465
File "<doctest...>", line 10, in <module>
466466
lumberjack()
467-
^^^^^^^^^^^^
468467
*** print_exception:
469468
Traceback (most recent call last):
470469
File "<doctest...>", line 10, in <module>
471470
lumberjack()
472-
^^^^^^^^^^^^
473471
File "<doctest...>", line 4, in lumberjack
474472
bright_side_of_death()
475-
^^^^^^^^^^^^^^^^^^^^^^
476473
IndexError: tuple index out of range
477474
*** print_exc:
478475
Traceback (most recent call last):
479476
File "<doctest...>", line 10, in <module>
480477
lumberjack()
481-
^^^^^^^^^^^^
482478
File "<doctest...>", line 4, in lumberjack
483479
bright_side_of_death()
484-
^^^^^^^^^^^^^^^^^^^^^^
485480
IndexError: tuple index out of range
486481
*** format_exc, first and last line:
487482
Traceback (most recent call last):
488483
IndexError: tuple index out of range
489484
*** format_exception:
490485
['Traceback (most recent call last):\n',
491-
' File "<doctest default[0]>", line 10, in <module>\n lumberjack()\n ^^^^^^^^^^^^\n',
492-
' File "<doctest default[0]>", line 4, in lumberjack\n bright_side_of_death()\n ^^^^^^^^^^^^^^^^^^^^^^\n',
486+
' File "<doctest default[0]>", line 10, in <module>\n lumberjack()\n',
487+
' File "<doctest default[0]>", line 4, in lumberjack\n bright_side_of_death()\n',
493488
' File "<doctest default[0]>", line 7, in bright_side_of_death\n return tuple()[0]\n ~~~~~~~^^^\n',
494489
'IndexError: tuple index out of range\n']
495490
*** extract_tb:
496491
[<FrameSummary file <doctest...>, line 10 in <module>>,
497492
<FrameSummary file <doctest...>, line 4 in lumberjack>,
498493
<FrameSummary file <doctest...>, line 7 in bright_side_of_death>]
499494
*** format_tb:
500-
[' File "<doctest default[0]>", line 10, in <module>\n lumberjack()\n ^^^^^^^^^^^^\n',
501-
' File "<doctest default[0]>", line 4, in lumberjack\n bright_side_of_death()\n ^^^^^^^^^^^^^^^^^^^^^^\n',
495+
[' File "<doctest default[0]>", line 10, in <module>\n lumberjack()\n',
496+
' File "<doctest default[0]>", line 4, in lumberjack\n bright_side_of_death()\n',
502497
' File "<doctest default[0]>", line 7, in bright_side_of_death\n return tuple()[0]\n ~~~~~~~^^^\n']
503498
*** tb_lineno: 10
504499

Doc/whatsnew/3.11.rst

-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ when dealing with deeply nested dictionary objects and multiple function calls,
117117
Traceback (most recent call last):
118118
File "query.py", line 37, in <module>
119119
magic_arithmetic('foo')
120-
^^^^^^^^^^^^^^^^^^^^^^^
121120
File "query.py", line 18, in magic_arithmetic
122121
return add_counts(x) / 25
123122
^^^^^^^^^^^^^

Lib/idlelib/idle_test/test_run.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from idlelib import run
44
import io
55
import sys
6-
from test.support import captured_output, captured_stderr, has_no_debug_ranges
6+
from test.support import captured_output, captured_stderr
77
import unittest
88
from unittest import mock
99
import idlelib
@@ -33,14 +33,9 @@ def __eq__(self, other):
3333
run.print_exception()
3434

3535
tb = output.getvalue().strip().splitlines()
36-
if has_no_debug_ranges():
37-
self.assertEqual(11, len(tb))
38-
self.assertIn('UnhashableException: ex2', tb[3])
39-
self.assertIn('UnhashableException: ex1', tb[10])
40-
else:
41-
self.assertEqual(13, len(tb))
42-
self.assertIn('UnhashableException: ex2', tb[4])
43-
self.assertIn('UnhashableException: ex1', tb[12])
36+
self.assertEqual(11, len(tb))
37+
self.assertIn('UnhashableException: ex2', tb[3])
38+
self.assertIn('UnhashableException: ex1', tb[10])
4439

4540
data = (('1/0', ZeroDivisionError, "division by zero\n"),
4641
('abc', NameError, "name 'abc' is not defined. "

Lib/test/test_cmd_line_script.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -549,10 +549,10 @@ def test_pep_409_verbiage(self):
549549
script_name = _make_test_script(script_dir, 'script', script)
550550
exitcode, stdout, stderr = assert_python_failure(script_name)
551551
text = stderr.decode('ascii').split('\n')
552-
self.assertEqual(len(text), 6)
552+
self.assertEqual(len(text), 5)
553553
self.assertTrue(text[0].startswith('Traceback'))
554554
self.assertTrue(text[1].startswith(' File '))
555-
self.assertTrue(text[4].startswith('NameError'))
555+
self.assertTrue(text[3].startswith('NameError'))
556556

557557
def test_non_ascii(self):
558558
# Mac OS X denies the creation of a file with an invalid UTF-8 name.

Lib/test/test_doctest.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -2854,7 +2854,7 @@ def test_testmod(): r"""
28542854
# Skip the test: the filesystem encoding is unable to encode the filename
28552855
supports_unicode = False
28562856

2857-
if supports_unicode and not support.has_no_debug_ranges():
2857+
if supports_unicode:
28582858
def test_unicode(): """
28592859
Check doctest with a non-ascii filename:
28602860
@@ -2876,10 +2876,8 @@ def test_unicode(): """
28762876
Traceback (most recent call last):
28772877
File ...
28782878
exec(compile(example.source, filename, "single",
2879-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
28802879
File "<doctest foo-bär@baz[0]>", line 1, in <module>
28812880
raise Exception('clé')
2882-
^^^^^^^^^^^^^^^^^^^^^^
28832881
Exception: clé
28842882
TestResults(failed=1, attempted=1)
28852883
"""

0 commit comments

Comments
 (0)