https://github.com/python/cpython/commit/5cd50cb6eb28e525f0c838e049e900ea982a5a23
commit: 5cd50cb6eb28e525f0c838e049e900ea982a5a23
branch: main
author: Bénédikt Tran <[email protected]>
committer: vstinner <[email protected]>
date: 2024-09-18T18:42:33+02:00
summary:
gh-122145: Handle an empty AST body when reporting tracebacks (#122161)
files:
A Misc/NEWS.d/next/Library/2024-07-23-12-38-14.gh-issue-122145.sTO8nX.rst
M Lib/test/test_traceback.py
M Lib/traceback.py
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index b568221212d71f..455fea034198a6 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -3307,6 +3307,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")
diff --git a/Lib/traceback.py b/Lib/traceback.py
index 3e708c6f86a4c5..0fe7187a0c6193 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -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):
diff --git
a/Misc/NEWS.d/next/Library/2024-07-23-12-38-14.gh-issue-122145.sTO8nX.rst
b/Misc/NEWS.d/next/Library/2024-07-23-12-38-14.gh-issue-122145.sTO8nX.rst
new file mode 100644
index 00000000000000..a4282f12d9742a
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-07-23-12-38-14.gh-issue-122145.sTO8nX.rst
@@ -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.
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]