@@ -3413,6 +3413,19 @@ class Unrepresentable:
3413
3413
def __repr__ (self ) -> str :
3414
3414
raise Exception ("Unrepresentable" )
3415
3415
3416
+
3417
+ # Used in test_dont_swallow_cause_or_context_of_falsey_exception and
3418
+ # test_dont_swallow_subexceptions_of_falsey_exceptiongroup.
3419
+ class FalseyException (Exception ):
3420
+ def __bool__ (self ):
3421
+ return False
3422
+
3423
+
3424
+ class FalseyExceptionGroup (ExceptionGroup ):
3425
+ def __bool__ (self ):
3426
+ return False
3427
+
3428
+
3416
3429
class TestTracebackException (unittest .TestCase ):
3417
3430
def do_test_smoke (self , exc , expected_type_str ):
3418
3431
try :
@@ -3759,6 +3772,24 @@ def f():
3759
3772
'ZeroDivisionError: division by zero' ,
3760
3773
'' ])
3761
3774
3775
+ def test_dont_swallow_cause_or_context_of_falsey_exception (self ):
3776
+ # see gh-132308: Ensure that __cause__ or __context__ attributes of exceptions
3777
+ # that evaluate as falsey are included in the output. For falsey term,
3778
+ # see https://docs.python.org/3/library/stdtypes.html#truth-value-testing.
3779
+
3780
+ try :
3781
+ raise FalseyException from KeyError
3782
+ except FalseyException as e :
3783
+ self .assertIn (cause_message , traceback .format_exception (e ))
3784
+
3785
+ try :
3786
+ try :
3787
+ 1 / 0
3788
+ except ZeroDivisionError :
3789
+ raise FalseyException
3790
+ except FalseyException as e :
3791
+ self .assertIn (context_message , traceback .format_exception (e ))
3792
+
3762
3793
3763
3794
class TestTracebackException_ExceptionGroups (unittest .TestCase ):
3764
3795
def setUp (self ):
@@ -3960,6 +3991,26 @@ def test_comparison(self):
3960
3991
self .assertNotEqual (exc , object ())
3961
3992
self .assertEqual (exc , ALWAYS_EQ )
3962
3993
3994
+ def test_dont_swallow_subexceptions_of_falsey_exceptiongroup (self ):
3995
+ # see gh-132308: Ensure that subexceptions of exception groups
3996
+ # that evaluate as falsey are displayed in the output. For falsey term,
3997
+ # see https://docs.python.org/3/library/stdtypes.html#truth-value-testing.
3998
+
3999
+ try :
4000
+ raise FalseyExceptionGroup ("Gih" , (KeyError (), NameError ()))
4001
+ except Exception as ee :
4002
+ str_exc = '' .join (traceback .format_exception (ee ))
4003
+ self .assertIn ('+---------------- 1 ----------------' , str_exc )
4004
+ self .assertIn ('+---------------- 2 ----------------' , str_exc )
4005
+
4006
+ # Test with a falsey exception, in last position, as sub-exceptions.
4007
+ msg = 'bool'
4008
+ try :
4009
+ raise FalseyExceptionGroup ("Gah" , (KeyError (), FalseyException (msg )))
4010
+ except Exception as ee :
4011
+ str_exc = traceback .format_exception (ee )
4012
+ self .assertIn (f'{ FalseyException .__name__ } : { msg } ' , str_exc [- 2 ])
4013
+
3963
4014
3964
4015
global_for_suggestions = None
3965
4016
0 commit comments