Skip to content

Commit d78095a

Browse files
committed
Fix last comments
1 parent 1c03794 commit d78095a

File tree

2 files changed

+31
-43
lines changed

2 files changed

+31
-43
lines changed

Lib/test/test_traceback.py

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3416,26 +3416,16 @@ def __repr__(self) -> str:
34163416

34173417
# Used in test_dont_swallow_cause_or_context_of_falsey_exception and
34183418
# test_dont_swallow_subexceptions_of_falsey_exceptiongroup.
3419-
class FalseyBoolException(Exception):
3419+
class FalseyException(Exception):
34203420
def __bool__(self):
34213421
return False
34223422

34233423

3424-
class FalseyLenException(Exception):
3425-
def __len__(self):
3426-
return 0
3427-
3428-
3429-
class FalseyBoolExceptionGroup(ExceptionGroup):
3424+
class FalseyExceptionGroup(ExceptionGroup):
34303425
def __bool__(self):
34313426
return False
34323427

34333428

3434-
class FalseyLenExceptionGroup(ExceptionGroup):
3435-
def __len__(self):
3436-
return 0
3437-
3438-
34393429
class TestTracebackException(unittest.TestCase):
34403430
def do_test_smoke(self, exc, expected_type_str):
34413431
try:
@@ -3784,23 +3774,21 @@ def f():
37843774

37853775
def test_dont_swallow_cause_or_context_of_falsey_exception(self):
37863776
# see gh-132308: Ensure that __cause__ or __context__ attributes of exceptions
3787-
# that evaluate as falsey are included in the output.
3788-
# Recall: `x` is falsey if `len(x)` returns 0 or `bool(x)` returns False.
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.
37893779

3790-
for exc in (FalseyBoolException, FalseyLenException):
3791-
try:
3792-
raise exc from KeyError
3793-
except exc as e:
3794-
self.assertIn(cause_message, traceback.format_exception(e))
3780+
try:
3781+
raise FalseyException from KeyError
3782+
except FalseyException as e:
3783+
self.assertIn(cause_message, traceback.format_exception(e))
37953784

3796-
for exc in (FalseyBoolException, FalseyLenException):
3785+
try:
37973786
try:
3798-
try:
3799-
1/0
3800-
except ZeroDivisionError:
3801-
raise exc
3802-
except exc as e:
3803-
self.assertIn(context_message, traceback.format_exception(e))
3787+
1/0
3788+
except ZeroDivisionError:
3789+
raise FalseyException
3790+
except FalseyException as e:
3791+
self.assertIn(context_message, traceback.format_exception(e))
38043792

38053793

38063794
class TestTracebackException_ExceptionGroups(unittest.TestCase):
@@ -4005,24 +3993,23 @@ def test_comparison(self):
40053993

40063994
def test_dont_swallow_subexceptions_of_falsey_exceptiongroup(self):
40073995
# see gh-132308: Ensure that subexceptions of exception groups
4008-
# that evaluate as falsey are displayed in the output.
4009-
# Recall: `x` is falsey if `len(x)` returns 0 or `bool(x)` returns False.
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.
40103998

4011-
for falsey_exception in (FalseyBoolExceptionGroup, FalseyLenExceptionGroup):
4012-
try:
4013-
raise falsey_exception("Gih", (KeyError(), NameError()))
4014-
except Exception as ee:
4015-
str_exc = ''.join(traceback.format_exception(ee))
4016-
self.assertIn('+---------------- 1 ----------------', str_exc)
4017-
self.assertIn('+---------------- 2 ----------------', str_exc)
4018-
4019-
# Test with a falsey exception, in last position, as sub-exceptions.
4020-
msg = 'bool'
4021-
try:
4022-
raise falsey_exception("Gah", (KeyError(), FalseyBoolException(msg)))
4023-
except Exception as ee:
4024-
str_exc = traceback.format_exception(ee)
4025-
self.assertIn(f'{FalseyBoolException.__name__}: {msg}', str_exc[-2])
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])
40264013

40274014

40284015
global_for_suggestions = None
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
The :class:`traceback.TracebackException` must include the ``__context__``, ``__cause__`` attributes
22
from falsey Exception, and the ``exceptions`` attribute from falsey ``ExceptionGroup`` in its render.
3+
For *falsey* term, see https://docs.python.org/3/library/stdtypes.html#truth-value-testing.

0 commit comments

Comments
 (0)