@@ -3304,6 +3304,41 @@ def format_frame_summary(self, frame_summary, colorize=False):
33043304 f' File "{ __file__ } ", line { lno } , in f\n 1/0\n '
33053305 )
33063306
3307+ def test_summary_should_show_carets (self ):
3308+ # See: https://github.com/python/cpython/issues/122353
3309+
3310+ # statement to execute and to get a ZeroDivisionError for a traceback
3311+ statement = "abcdef = 1 / 0 and 2.0"
3312+ colno = statement .index ('1 / 0' )
3313+ end_colno = colno + len ('1 / 0' )
3314+
3315+ # Actual line to use when rendering the traceback
3316+ # and whose AST will be extracted (it will be empty).
3317+ cached_line = '# this line will be used during rendering'
3318+ self .addCleanup (unlink , TESTFN )
3319+ with open (TESTFN , "w" ) as file :
3320+ file .write (cached_line )
3321+ linecache .updatecache (TESTFN , {})
3322+
3323+ try :
3324+ exec (compile (statement , TESTFN , "exec" ))
3325+ except ZeroDivisionError as exc :
3326+ # This is the simplest way to create a StackSummary
3327+ # whose FrameSummary items have their column offsets.
3328+ s = traceback .TracebackException .from_exception (exc ).stack
3329+ self .assertIsInstance (s , traceback .StackSummary )
3330+ with unittest .mock .patch .object (s , '_should_show_carets' ,
3331+ wraps = s ._should_show_carets ) as ff :
3332+ self .assertEqual (len (s ), 2 )
3333+ self .assertListEqual (
3334+ s .format_frame_summary (s [1 ]).splitlines (),
3335+ [
3336+ f' File "{ TESTFN } ", line 1, in <module>' ,
3337+ f' { cached_line } '
3338+ ]
3339+ )
3340+ ff .assert_called_with (colno , end_colno , [cached_line ], None )
3341+
33073342class Unrepresentable :
33083343 def __repr__ (self ) -> str :
33093344 raise Exception ("Unrepresentable" )
0 commit comments