import traceback as tb class FalseyLenException(Exception): def __len__(self): return 0 class FalseyBoolException(Exception): def __bool__(self): return False def tracebackexception_swallows_cause_and_context(): try: raise FalseyBoolException("Oh") from KeyError except Exception as e: print(f'{e = }, {e.__cause__ = }') tb.print_exception(e) print("---"*20) try: try: 1/0 except: raise FalseyLenException("Ah") except Exception as e: print(f'{e = }, {e.__context__ = }') tb.print_exception(e) print("---"*20) if __name__ == "__main__": tracebackexception_swallows_cause_and_context()
Ouptput is:
e = FalseyBoolException('Oh'), e.__cause__ = KeyError() Traceback (most recent call last): File "/Users/yves/Desktop/Bugs/gh-132XXX.py", line 13, in tracebackexception_swallows_cause_and_context raise FalseyBoolException("Oh") from KeyError FalseyBoolException: Oh ------------------------------------------------------------ e = FalseyLenException('Ah'), e.__context__ = ZeroDivisionError('division by zero') Traceback (most recent call last): File "/Users/yves/Desktop/Bugs/gh-132XXX.py", line 23, in tracebackexception_swallows_cause_and_context raise FalseyLenException("Ah") FalseyLenException: Ah
The Cause or Context descriptions are not printed as expected.
These following tests are wrong.
cpython/Lib/traceback.py
Line 1123 in 255eb37
Line 1144 in 255eb37
See: #132129
CPython main branch
macOS
TracebackException class also swallows sub-exceptions of falsey exception group.
def tracebackexception_swallows_exceptiongroup(): # Works fine. try: raise ExceptionGroup("Gah", (KeyError('1'), NameError('Goh'))) except Exception as e: print(f'{e = }, {e.__cause__ = }') tb.print_exception(e) print("---"*20) # Swallows details. try: raise FalseyExceptionGroup("Gih", (KeyError(2), NameError('Guh'))) except Exception as ee: print(f'{ee = }, {ee.__cause__ = }') tb.print_exception(ee) print("---"*20) if __name__ == "__main__": tracebackexception_swallows_exceptiongroup()
Output is:
e = ExceptionGroup('Gah', (KeyError('1'), NameError('Goh'))), e.__cause__ = None + Exception Group Traceback (most recent call last): | File "/Users/yves/Desktop/Bugs/gh-132308.py", line 37, in tracebackexception_swallows_exceptiongroup | raise ExceptionGroup("Gah", (KeyError('1'), NameError('Goh'))) | ExceptionGroup: Gah (2 sub-exceptions) +-+---------------- 1 ---------------- | KeyError: '1' +---------------- 2 ---------------- | NameError: Goh +------------------------------------ ------------------------------------------------------------ ee = FalseyExceptionGroup('Gih', (KeyError(2), NameError('Guh'))), ee.__cause__ = None Traceback (most recent call last): File "/Users/yves/Desktop/Bugs/gh-132308.py", line 45, in tracebackexception_swallows_exceptiongroup raise FalseyExceptionGroup("Gih", (KeyError(2), NameError('Guh'))) FalseyExceptionGroup: Gih (2 sub-exceptions) ------------------------------------------------------------
As below, a wrong test must be change.
Line 1159 in 255eb37
Thank you. Would you like to make a PR?
gh-132308: prevent `TracebackException` swallowing attributes of a fa…
pythongh-132308: prevent `TracebackException` swallowing attributes o…
[3.13] gh-132308: prevent `TracebackException` swallowing attributes …
Activity
YvesDup commented on Apr 9, 2025
TracebackException class also swallows sub-exceptions of falsey exception group.
def tracebackexception_swallows_exceptiongroup(): # Works fine. try: raise ExceptionGroup("Gah", (KeyError('1'), NameError('Goh'))) except Exception as e: print(f'{e = }, {e.__cause__ = }') tb.print_exception(e) print("---"*20) # Swallows details. try: raise FalseyExceptionGroup("Gih", (KeyError(2), NameError('Guh'))) except Exception as ee: print(f'{ee = }, {ee.__cause__ = }') tb.print_exception(ee) print("---"*20) if __name__ == "__main__": tracebackexception_swallows_exceptiongroup()Output is:
e = ExceptionGroup('Gah', (KeyError('1'), NameError('Goh'))), e.__cause__ = None + Exception Group Traceback (most recent call last): | File "/Users/yves/Desktop/Bugs/gh-132308.py", line 37, in tracebackexception_swallows_exceptiongroup | raise ExceptionGroup("Gah", (KeyError('1'), NameError('Goh'))) | ExceptionGroup: Gah (2 sub-exceptions) +-+---------------- 1 ---------------- | KeyError: '1' +---------------- 2 ---------------- | NameError: Goh +------------------------------------ ------------------------------------------------------------ ee = FalseyExceptionGroup('Gih', (KeyError(2), NameError('Guh'))), ee.__cause__ = None Traceback (most recent call last): File "/Users/yves/Desktop/Bugs/gh-132308.py", line 45, in tracebackexception_swallows_exceptiongroup raise FalseyExceptionGroup("Gih", (KeyError(2), NameError('Guh'))) FalseyExceptionGroup: Gih (2 sub-exceptions) ------------------------------------------------------------As below, a wrong test must be change.
cpython/Lib/traceback.py
Line 1159 in 255eb37
iritkatriel commented on Apr 9, 2025
Thank you. Would you like to make a PR?
gh-132308: prevent `TracebackException` swallowing attributes of a fa…
pythongh-132308: prevent `TracebackException` swallowing attributes o…
[3.13] gh-132308: prevent `TracebackException` swallowing attributes …