@@ -5034,5 +5034,89 @@ def test_traceback_exception_print_show_lines_false(self):
5034
5034
self .assertIn ('ZeroDivisionError' , result )
5035
5035
5036
5036
5037
+ class TestRecentFirst (unittest .TestCase ):
5038
+ """Tests for the recent_first parameter in traceback formatting functions."""
5039
+
5040
+ def setUp (self ):
5041
+ # Create a simple exception for testing
5042
+ def f1 ():
5043
+ return 1 / 0
5044
+
5045
+ def f2 ():
5046
+ return f1 ()
5047
+
5048
+ try :
5049
+ f2 ()
5050
+ except ZeroDivisionError as e :
5051
+ self .exc = e
5052
+
5053
+ def test_print_tb_recent_first (self ):
5054
+ """Test print_tb with recent_first=True"""
5055
+ output = StringIO ()
5056
+ traceback .print_tb (self .exc .__traceback__ , file = output , recent_first = True )
5057
+ result = output .getvalue ()
5058
+ f1pos = result .index (", in f1" )
5059
+ f2pos = result .index (", in f2" )
5060
+ self .assertLess (f1pos , f2pos , "f1 should be printed before f2" )
5061
+
5062
+ def test_format_tb_recent_first (self ):
5063
+ """Test format_tb with recent_first=True"""
5064
+ result = traceback .format_tb (self .exc .__traceback__ , recent_first = True )
5065
+ formatted = '' .join (result )
5066
+ f1pos = formatted .index (", in f1" )
5067
+ f2pos = formatted .index (", in f2" )
5068
+ self .assertLess (f1pos , f2pos , "f1 should be printed before f2" )
5069
+
5070
+ def check_recent_first_exception_order (self , result : str ):
5071
+ """Helper to check if the recent_first order is correct in the result."""
5072
+ lines = result .splitlines ()
5073
+ self .assertEqual (lines [0 ], "ZeroDivisionError: division by zero" )
5074
+ self .assertEqual (lines [1 ], "Traceback (most recent call first):" )
5075
+
5076
+ f1pos = result .index (", in f1" )
5077
+ f2pos = result .index (", in f2" )
5078
+ self .assertLess (f1pos , f2pos , "f1 should be printed before f2" )
5079
+
5080
+ def test_print_exception_recent_first (self ):
5081
+ """Test print_exception with recent_first=True"""
5082
+ output = StringIO ()
5083
+ traceback .print_exception (self .exc , file = output , recent_first = True )
5084
+ self .check_recent_first_exception_order (output .getvalue ())
5085
+
5086
+ def test_format_exception_recent_first (self ):
5087
+ """Test format_exception with recent_first=True"""
5088
+ result = traceback .format_exception (self .exc , recent_first = True )
5089
+ self .check_recent_first_exception_order ('' .join (result ))
5090
+
5091
+ def test_print_stack_recent_first (self ):
5092
+ """Test print_stack with recent_first=True"""
5093
+ output = StringIO ()
5094
+
5095
+ def f1 ():
5096
+ traceback .print_stack (file = output , recent_first = True )
5097
+
5098
+ def f2 ():
5099
+ f1 ()
5100
+
5101
+ f2 ()
5102
+ result = output .getvalue ()
5103
+ f1pos = result .index (", in f1" )
5104
+ f2pos = result .index (", in f2" )
5105
+ self .assertLess (f1pos , f2pos , "f1 should be printed before f2" )
5106
+
5107
+ def test_format_stack_recent_first (self ):
5108
+ """Test format_stack with recent_first=True"""
5109
+ def f1 ():
5110
+ return traceback .format_stack (recent_first = True )
5111
+
5112
+ def f2 ():
5113
+ return f1 ()
5114
+
5115
+ result = '' .join (f2 ())
5116
+ f1pos = result .index (", in f1" )
5117
+ f2pos = result .index (", in f2" )
5118
+ self .assertLess (f1pos , f2pos , "f1 should be printed before f2" )
5119
+
5120
+
5037
5121
if __name__ == "__main__" :
5038
5122
unittest .main ()
0 commit comments