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")