@@ -3277,13 +3277,38 @@ def format_frame_summary(self, frame_summary, colorize=False):
3277
3277
3278
3278
def test_summary_should_show_carets (self ):
3279
3279
# 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 )
3287
3312
3288
3313
class Unrepresentable :
3289
3314
def __repr__ (self ) -> str :
0 commit comments