@@ -3413,6 +3413,19 @@ class Unrepresentable:
34133413 def __repr__ (self ) -> str :
34143414 raise Exception ("Unrepresentable" )
34153415
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+
34163429class TestTracebackException (unittest .TestCase ):
34173430 def do_test_smoke (self , exc , expected_type_str ):
34183431 try :
@@ -3759,6 +3772,24 @@ def f():
37593772 'ZeroDivisionError: division by zero' ,
37603773 '' ])
37613774
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+
37623793
37633794class TestTracebackException_ExceptionGroups (unittest .TestCase ):
37643795 def setUp (self ):
@@ -3960,6 +3991,26 @@ def test_comparison(self):
39603991 self .assertNotEqual (exc , object ())
39613992 self .assertEqual (exc , ALWAYS_EQ )
39623993
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+
39634014
39644015global_for_suggestions = None
39654016
0 commit comments