10000 update test · python/cpython@c944dae · GitHub
[go: up one dir, main page]

Skip to content

Commit c944dae

Browse files
committed
update test
1 parent a18d746 commit c944dae

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

Lib/test/test_traceback.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3277,13 +3277,38 @@ def format_frame_summary(self, frame_summary, colorize=False):
32773277

32783278
def test_summary_should_show_carets(self):
32793279
# See: https://github.com/python/cpython/issues/122353
3280-
should_show = traceback.StackSummary()._should_show_carets
3281-
# a line that may or may not have carrets shown
3282-
self.assertTrue(should_show(0, 1, ['a = 123456789'], None))
3283-
self.assertFalse(should_show(0, 999, ['return'], None))
3284-
# commented lines have an empty AST body, hence never shown
3285-
self.assertFalse(should_show(0, 1, ['# abc = 123456789'], None))
3286-
self.assertFalse(should_show(0, 999, ['# return'], None))
3280+
3281+
# statement to execute and to get a ZeroDivisionError for a traceback
3282+
statement = "abcdef = 1 / 0 and 2.0"
3283+
colno = statement.index('1 / 0')
3284+
end_colno = colno + len('1 / 0')
3285+
3286+
# Actual line to use when rendering the traceback
3287+
# and whose AST will be extracted (it will be empty).
3288+
cached_line = '# this line will be used during rendering'
3289+
self.addCleanup(unlink, TESTFN)
3290+
with open(TESTFN, "w") as file:
3291+
file.write(cached_line)
3292+
linecache.updatecache(TESTFN, {})
3293+
3294+
try:
3295+
exec(compile(statement, TESTFN, "exec"))
3296+
except ZeroDivisionError as exc:
3297+
# This is the simplest way to create a StackSummary
3298+
# whose FrameSummary items have their column offsets.
3299+
s = traceback.TracebackException.from_exception(exc).stack
3300+
self.assertIsInstance(s, traceback.StackSummary)
3301+
with unittest.mock.patch.object(s, '_should_show_carets',
3302+
wraps=s._should_show_carets) as ff:
3303+
self.assertEqual(len(s), 2)
3304+
self.assertListEqual(
3305+
s.format_frame_summary(s[1]).splitlines(),
3306+
[
3307+
f' File "{TESTFN}", line 1, in <module>',
3308+
f' {cached_line}'
3309+
]
3310+
)
3311+
ff.assert_called_with(colno, end_colno, [cached_line], None)
32873312

32883313
class Unrepresentable:
32893314
def __repr__(self) -> str:

0 commit comments

Comments
 (0)
0