Skip to content

Commit

Permalink
[3.13] gh-122145: Handle an empty AST body when reporting tracebacks (G…
Browse files Browse the repository at this point in the history
…H-122161) (#124214)

gh-122145: Handle an empty AST body when reporting tracebacks (GH-122161)
(cherry picked from commit 5cd50cb)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
  • Loading branch information
miss-islington and picnixz authored Sep 30, 2024
1 parent 5dcce94 commit 99dc089
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -3304,6 +3304,41 @@ def format_frame_summary(self, frame_summary, colorize=False):
f' File "{__file__}", line {lno}, in f\n 1/0\n'
)

def test_summary_should_show_carets(self):
# See: https://github.com/python/cpython/issues/122353

# statement to execute and to get a ZeroDivisionError for a traceback
statement = "abcdef = 1 / 0 and 2.0"
colno = statement.index('1 / 0')
end_colno = colno + len('1 / 0')

# Actual line to use when rendering the traceback
# and whose AST will be extracted (it will be empty).
cached_line = '# this line will be used during rendering'
self.addCleanup(unlink, TESTFN)
with open(TESTFN, "w") as file:
file.write(cached_line)
linecache.updatecache(TESTFN, {})

try:
exec(compile(statement, TESTFN, "exec"))
except ZeroDivisionError as exc:
# This is the simplest way to create a StackSummary
# whose FrameSummary items have their column offsets.
s = traceback.TracebackException.from_exception(exc).stack
self.assertIsInstance(s, traceback.StackSummary)
with unittest.mock.patch.object(s, '_should_show_carets',
wraps=s._should_show_carets) as ff:
self.assertEqual(len(s), 2)
self.assertListEqual(
s.format_frame_summary(s[1]).splitlines(),
[
f' File "{TESTFN}", line 1, in <module>',
f' {cached_line}'
]
)
ff.assert_called_with(colno, end_colno, [cached_line], None)

class Unrepresentable:
def __repr__(self) -> str:
raise Exception("Unrepresentable")
Expand Down
2 changes: 2 additions & 0 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,8 @@ def _should_show_carets(self, start_offset, end_offset, all_lines, anchors):
with suppress(SyntaxError, ImportError):
import ast
tree = ast.parse('\n'.join(all_lines))
if not tree.body:
return False
statement = tree.body[0]
value = None
def _spawns_full_line(value):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix an issue when reporting tracebacks corresponding to Python code
emitting an empty AST body.
Patch by Nikita Sobolev and Bénédikt Tran.

0 comments on commit 99dc089

Please sign in to comment.