Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
update test
  • Loading branch information
picnixz committed Jul 29, 2024
commit c944dae5a26c4bade7df4e2e09fba2067b71d29d
39 changes: 32 additions & 7 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -3277,13 +3277,38 @@ def format_frame_summary(self, frame_summary, colorize=False):

def test_summary_should_show_carets(self):
# See: https://github.com/python/cpython/issues/122353
should_show = traceback.StackSummary()._should_show_carets
# a line that may or may not have carrets shown
self.assertTrue(should_show(0, 1, ['a = 123456789'], None))
self.assertFalse(should_show(0, 999, ['return'], None))
# commented lines have an empty AST body, hence never shown
self.assertFalse(should_show(0, 1, ['# abc = 123456789'], None))
self.assertFalse(should_show(0, 999, ['# return'], None))

# 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:
Comment thread
vstinner marked this conversation as resolved.
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:
Expand Down