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
Next Next commit
Initial commit
  • Loading branch information
YvesDup committed Apr 10, 2025
commit 3e79eaefcfd653af59d82fff23642b5c1fa4ff05
57 changes: 57 additions & 0 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -3413,6 +3413,24 @@ class Unrepresentable:
def __repr__(self) -> str:
raise Exception("Unrepresentable")


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


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


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


class TestTracebackException(unittest.TestCase):
def do_test_smoke(self, exc, expected_type_str):
try:
Expand Down Expand Up @@ -3759,6 +3777,26 @@ def f():
'ZeroDivisionError: division by zero',
''])

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.

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

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


class TestTracebackException_ExceptionGroups(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -3960,6 +3998,25 @@ def test_comparison(self):
self.assertNotEqual(exc, object())
self.assertEqual(exc, ALWAYS_EQ)

def test_dont_swallow_subexceptions_of_falsey_exceptiongroup(self):
# see gh-132308: Ensure that subexceptions of exception group
# that evaluate as falsey are displayed in the output.
# Recall: `x` is falsey if `len(x)` returns 0 or `bool(x)` returns False.

try:
raise FalseyExceptionGroup("Gih", (KeyError(2), NameError('Guh')))
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("Gih", (KeyError(2), FalseyBoolException(msg)))
except Exception as ee:
str_exc = traceback.format_exception(ee)
self.assertIn(f'{FalseyBoolException.__name__}: {msg}', str_exc[-2])

global_for_suggestions = None

Expand Down
6 changes: 3 additions & 3 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,7 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
queue = [(self, exc_value)]
while queue:
te, e = queue.pop()
if (e and e.__cause__ is not None
if (e is not None and e.__cause__ is not None
and id(e.__cause__) not in _seen):
cause = TracebackException(
type(e.__cause__),
Expand All @@ -1141,7 +1141,7 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
not e.__suppress_context__)
else:
need_context = True
if (e and e.__context__ is not None
if (e is not None and e.__context__ is not None
and need_context and id(e.__context__) not in _seen):
context = TracebackException(
type(e.__context__),
Expand All @@ -1156,7 +1156,7 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
else:
context = None

if e and isinstance(e, BaseExceptionGroup):
if e is not None and isinstance(e, BaseExceptionGroup):
exceptions = []
for exc in e.exceptions:
texc = TracebackException(
Expand Down
Loading
0