@@ -564,12 +564,12 @@ def test_signatures(self):
564
564
self .assertEqual (
565
565
str (inspect .signature (traceback .print_exception )),
566
566
('(exc, /, value=<implicit>, tb=<implicit>, '
567
- 'limit=None, file=None, chain=True, **kwargs)' ))
567
+ 'limit=None, file=None, chain=True, show_lines=True, **kwargs)' ))
568
568
569
569
self .assertEqual (
570
570
str (inspect .signature (traceback .format_exception )),
571
571
('(exc, /, value=<implicit>, tb=<implicit>, limit=None, '
572
- 'chain=True, **kwargs)' ))
572
+ 'chain=True, show_lines=True, **kwargs)' ))
573
573
574
574
self .assertEqual (
575
575
str (inspect .signature (traceback .format_exception_only )),
@@ -3340,7 +3340,7 @@ def some_inner(k, v):
3340
3340
3341
3341
def test_custom_format_frame (self ):
3342
3342
class CustomStackSummary (traceback .StackSummary ):
3343
- def format_frame_summary (self , frame_summary , colorize = False ):
3343
+ def format_frame_summary (self , frame_summary , ** kwargs ):
3344
3344
return f'{ frame_summary .filename } :{ frame_summary .lineno } '
3345
3345
3346
3346
def some_inner ():
@@ -3365,10 +3365,10 @@ def g():
3365
3365
tb = g ()
3366
3366
3367
3367
class Skip_G (traceback .StackSummary ):
3368
- def format_frame_summary (self , frame_summary , colorize = False ):
3368
+ def format_frame_summary (self , frame_summary , ** kwargs ):
3369
3369
if frame_summary .name == 'g' :
3370
3370
return None
3371
- return super ().format_frame_summary (frame_summary )
3371
+ return super ().format_frame_summary (frame_summary , ** kwargs )
3372
3372
3373
3373
stack = Skip_G .extract (
3374
3374
traceback .walk_tb (tb )).format ()
@@ -4877,5 +4877,161 @@ def expected(t, m, fn, l, f, E, e, z):
4877
4877
]
4878
4878
self .assertEqual (actual , expected (** colors ))
4879
4879
4880
+
4881
+ class TestShowLines (unittest .TestCase ):
4882
+ """Tests for the show_lines parameter in traceback formatting functions."""
4883
+
4884
+ def setUp (self ):
4885
+ # Create a simple exception for testing
4886
+ try :
4887
+ x = 1 / 0
4888
+ except ZeroDivisionError as e :
4889
+ self .exc = e
4890
+
4891
+ def test_print_tb_show_lines_true (self ):
4892
+ """Test print_tb with show_lines=True (default)"""
4893
+ output = StringIO ()
4894
+ traceback .print_tb (self .exc .__traceback__ , file = output , show_lines = True )
4895
+ result = output .getvalue ()
4896
+ self .assertIn ('x = 1 / 0' , result )
4897
+ self .assertIn ('File ' , result )
4898
+
4899
+ def test_print_tb_show_lines_false (self ):
4900
+ """Test print_tb with show_lines=False"""
4901
+ output = StringIO ()
4902
+ traceback .print_tb (self .exc .__traceback__ , file = output , show_lines = False )
4903
+ result = output .getvalue ()
4904
+ self .assertNotIn ('x = 1 / 0' , result )
4905
+ self .assertIn ('File ' , result ) # File info should still be present
4906
+
4907
+ def test_format_tb_show_lines_true (self ):
4908
+ """Test format_tb with show_lines=True (default)"""
4909
+ result = traceback .format_tb (self .exc .__traceback__ , show_lines = True )
4910
+ formatted = '' .join (result )
4911
+ self .assertIn ('x = 1 / 0' , formatted )
4912
+ self .assertIn ('File ' , formatted )
4913
+
4914
+ def test_format_tb_show_lines_false (self ):
4915
+ """Test format_tb with show_lines=False"""
4916
+ result = traceback .format_tb (self .exc .__traceback__ , show_lines = False )
4917
+ formatted = '' .join (result )
4918
+ self .assertNotIn ('x = 1 / 0' , formatted )
4919
+ self .assertIn ('File ' , formatted ) # File info should still be present
4920
+
4921
+ def test_print_exception_show_lines_true (self ):
4922
+ """Test print_exception with show_lines=True (default)"""
4923
+ output = StringIO ()
4924
+ traceback .print_exception (self .exc , file = output , show_lines = True )
4925
+ result = output .getvalue ()
4926
+ self .assertIn ('x = 1 / 0' , result )
4927
+ self .assertIn ('ZeroDivisionError' , result )
4928
+
4929
+ def test_print_exception_show_lines_false (self ):
4930
+ """Test print_exception with show_lines=False"""
4931
+ output = StringIO ()
4932
+ traceback .print_exception (self .exc , file = output , show_lines = False )
4933
+ result = output .getvalue ()
4934
+ self .assertNotIn ('x = 1 / 0' , result )
4935
+ self .assertIn ('ZeroDivisionError' , result ) # Exception type should still be present
4936
+
4937
+ def test_format_exception_show_lines_true (self ):
4938
+ """Test format_exception with show_lines=True (default)"""
4939
+ result = traceback .format_exception (self .exc , show_lines = True )
4940
+ formatted = '' .join (result )
4941
+ self .assertIn ('x = 1 / 0' , formatted )
4942
+ self .assertIn ('ZeroDivisionError' , formatted )
4943
+
4944
+ def test_format_exception_show_lines_false (self ):
4945
+ """Test format_exception with show_lines=False"""
4946
+ result = traceback .format_exception (self .exc , show_lines = False )
4947
+ formatted = '' .join (result )
4948
+ self .assertNotIn ('x = 1 / 0' , formatted )
4949
+ self .assertIn ('ZeroDivisionError' , formatted ) # Exception type should still be present
4950
+
4951
+ def test_print_exc_show_lines_false (self ):
4952
+ """Test print_exc with show_lines=False"""
4953
+ # Override sys.exception() to return our test exception
4954
+ original_exception = sys .exception
4955
+ sys .exception = lambda : self .exc
4956
+ try :
4957
+ output = StringIO ()
4958
+ traceback .print_exc (file = output , show_lines = False )
4959
+ result = output .getvalue ()
4960
+ self .assertNotIn ('x = 1 / 0' , result )
4961
+ self .assertIn ('ZeroDivisionError' , result )
4962
+ finally :
4963
+ sys .exception = original_exception
4964
+
4965
+ def test_format_exc_show_lines_false (self ):
4966
+ """Test format_exc with show_lines=False"""
4967
+ # Override sys.exception() to return our test exception
4968
+ original_exception = sys .exception
4969
+ sys .exception = lambda : self .exc
4970
+ try :
4971
+ result = traceback .format_exc (show_lines = False )
4972
+ self .assertNotIn ('x = 1 / 0' , result )
4973
+ self .assertIn ('ZeroDivisionError' , result )
4974
+ finally :
4975
+ sys .exception = original_exception
4976
+
4977
+ def test_print_stack_show_lines_false (self ):
4978
+ """Test print_stack with show_lines=False"""
4979
+ output = StringIO ()
4980
+ traceback .print_stack (file = output , show_lines = False )
4981
+ result = output .getvalue ()
4982
+ # Should not contain source code lines
4983
+ lines = result .split ('\n ' )
4984
+ # Filter out empty lines and check that remaining lines are just file/line info
4985
+ non_empty_lines = [line for line in lines if line .strip ()]
4986
+ for line in non_empty_lines :
4987
+ if line .strip ():
4988
+ self .assertTrue (line .strip ().startswith ('File ' ) or
4989
+ 'in ' in line or
4990
+ line .strip () == 'traceback.print_stack(file=output, show_lines=False)' )
4991
+
4992
+ def test_format_stack_show_lines_false (self ):
4993
+ """Test format_stack with show_lines=False"""
4994
+ result = traceback .format_stack (show_lines = False )
4995
+ formatted = '' .join (result )
4996
+ # Should contain file information but not source code
4997
+ self .assertIn ('File ' , formatted )
4998
+ # Check that the source code of this test is not included
4999
+ self .assertNotIn ('traceback.format_stack(show_lines=False)' , formatted )
5000
+
5001
+ def test_format_list_show_lines_false (self ):
5002
+ """Test format_list with show_lines=False"""
5003
+ tb_list = traceback .extract_tb (self .exc .__traceback__ )
5004
+ result = traceback .format_list (tb_list , show_lines = False )
5005
+ formatted = '' .join (result )
5006
+ self .assertNotIn ('x = 1 / 0' , formatted )
5007
+ self .assertIn ('File ' , formatted ) # File info should still be present
5008
+
5009
+ def test_print_list_show_lines_false (self ):
5010
+ """Test print_list with show_lines=False"""
5011
+ tb_list = traceback .extract_tb (self .exc .__traceback__ )
5012
+ output = StringIO ()
5013
+ traceback .print_list (tb_list , file = output , show_lines = False )
5014
+ result = output .getvalue ()
5015
+ self .assertNotIn ('x = 1 / 0' , result )
5016
+ self .assertIn ('File ' , result ) # File info should still be present
5017
+
5018
+ def test_traceback_exception_show_lines_false (self ):
5019
+ """Test TracebackException with show_lines=False"""
5020
+ te = traceback .TracebackException .from_exception (self .exc )
5021
+ result = list (te .format (show_lines = False ))
5022
+ formatted = '' .join (result )
5023
+ self .assertNotIn ('x = 1 / 0' , formatted )
5024
+ self .assertIn ('ZeroDivisionError' , formatted )
5025
+
5026
+ def test_traceback_exception_print_show_lines_false (self ):
5027
+ """Test TracebackException.print with show_lines=False"""
5028
+ te = traceback .TracebackException .from_exception (self .exc )
5029
+ output = StringIO ()
5030
+ te .print (file = output , show_lines = False )
5031
+ result = output .getvalue ()
5032
+ self .assertNotIn ('x = 1 / 0' , result )
5033
+ self .assertIn ('ZeroDivisionError' , result )
5034
+
5035
+
4880
5036
if __name__ == "__main__" :
4881
5037
unittest .main ()
0 commit comments