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