8000 gh-132308: `TracebackException` swallows attributes of falsey `Exception` and falsey `ExceptionGroup` by YvesDup · Pull Request #132363 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-132308: TracebackException swallows attributes of falsey Exception and falsey ExceptionGroup #132363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix last comments
  • Loading branch information
YvesDup committed Apr 18, 2025
commit d78095a7fbbb768a8842188ab1a6aa47f8af0bf8
73 changes: 30 additions & 43 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -3416,26 +3416,16 @@ def __repr__(self) -> str:

# Used in test_dont_swallow_cause_or_context_of_falsey_exception and
# test_dont_swallow_subexceptions_of_falsey_exceptiongroup.
class FalseyBoolException(Exception):
class FalseyException(Exception):
def __bool__(self):
return False


class FalseyLenException(Exception):
def __len__(self):
return 0


class FalseyBoolExceptionGroup(ExceptionGroup):
class FalseyExceptionGroup(ExceptionGroup):
def __bool__(self):
return False


class FalseyLenExceptionGroup(ExceptionGroup):
def __len__(self):
return 0


class TestTracebackException(unittest.TestCase):
def do_test_smoke(self, exc, expected_type_str):
try:
Expand Down Expand Up @@ -3784,23 +3774,21 @@ def f():

def test_dont_swallow_cause_or_context_of_falsey_exception(self):
# see gh-132308: Ensure that __cause__ or __context__ attributes of exceptions
# that evaluate as falsey are included in the output.
# Recall: `x` is falsey if `len(x)` returns 0 or `bool(x)` returns False.
# that evaluate as falsey are included in the output. For falsey term,
# see https://docs.python.org/3/library/stdtypes.html#truth-value-testing.

for exc in (FalseyBoolException, FalseyLenException):
try:
raise exc from KeyError
except exc as e:
self.assertIn(cause_message, traceback.format_exception(e))
try:
raise FalseyException from KeyError
except FalseyException as e:
self.assertIn(cause_message, traceback.format_exception(e))

for exc in (FalseyBoolException, FalseyLenException):
try:
try:
try:
1/0
except ZeroDivisionError:
raise exc
except exc as e:
self.assertIn(context_message, traceback.format_exception(e))
1/0
except ZeroDivisionError:
raise FalseyException
except FalseyException as e:
self.assertIn(context_message, traceback.format_exception(e))


class TestTracebackException_ExceptionGroups(unittest.TestCase):
Expand Down Expand Up @@ -4005,24 +3993,23 @@ def test_comparison(self):

def test_dont_swallow_subexceptions_of_falsey_exceptiongroup(self):
# see gh-132308: Ensure that subexceptions of exception groups
# that evaluate as falsey are displayed in the output.
# Recall: `x` is falsey if `len(x)` returns 0 or `bool(x)` returns False.
# that evaluate as falsey are displayed in the output. For falsey term,
# see https://docs.python.org/3/library/stdtypes.html#truth-value-testing.

for falsey_exception in (FalseyBoolExceptionGroup, FalseyLenExceptionGroup):
try:
raise falsey_exception("Gih", (KeyError(), NameError()))
except Exception as ee:
str_exc = ''.join(traceback.format_exception(ee))
self.assertIn('+---------------- 1 ----------------', str_exc)
self.assertIn('+---------------- 2 ----------------', str_exc)

# Test with a falsey exception, in last position, as sub-exceptions.
msg = 'bool'
try:
raise falsey_exception("Gah", (KeyError(), FalseyBoolException(msg)))
except Exception as ee:
str_exc = traceback.format_exception(ee)
self.assertIn(f'{FalseyBoolException.__name__}: {msg}', str_exc[-2])
try:
raise FalseyExceptionGroup("Gih", (KeyError(), NameError()))
except Exception as ee:
str_exc = ''.join(traceback.format_exception(ee))
self.assertIn('+---------------- 1 ----------------', str_exc)
self.assertIn('+---------------- 2 ----------------', str_exc)

# Test with a falsey exception, in last position, as sub-exceptions.
msg = 'bool'
try:
raise FalseyExceptionGroup("Gah", (KeyError(), FalseyException(msg)))
except Exception as ee:
str_exc = traceback.format_exception(ee)
self.assertIn(f'{FalseyException.__name__}: {msg}', str_exc[-2])


global_for_suggestions = None
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
The :class:`traceback.TracebackException` must include the ``__context__``, ``__cause__`` attributes
from falsey Exception, and the ``exceptions`` attribute from falsey ``ExceptionGroup`` in its render.
For *falsey* term, see https://docs.python.org/3/library/stdtypes.html#truth-value-testing.
Loading
0