Skip to content
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

Include stacktrace of exception cause in stacktrace #446

Merged
merged 3 commits into from
Jan 14, 2025
Merged

Conversation

ewjoachim
Copy link
Contributor

@ewjoachim ewjoachim commented Jan 10, 2025

>>> import traceback
>>> try:
...     try:
...         raise ValueError
...     except ValueError as exc:
...         raise IndexError from exc
... except IndexError:
...     import sys
...     exc_type, exc_value, exc_traceback = sys.exc_info()
...     print(traceback.format_tb(exc_traceback))
...
['  File "<python-input-3>", line 5, in <module>\n    raise IndexError from exc\n']
>>> try:
...     try:
...         raise ValueError
...     except ValueError as exc:
...         raise IndexError from exc
... except IndexError:
...     import sys
...     exc_type, exc_value, exc_traceback = sys.exc_info()
...     print(traceback.format_exception(exc_value))
...
['Traceback (most recent call last):\n', '  File "<python-input-5>", line 3, in <module>\n    raise ValueError\n', 'ValueError\n', '\nThe above exception was the direct cause of the following exception:\n\n', 'Traceback (most recent call last):\n', '  File "<python-input-5>", line 5, in <module>\n    raise IndexError from exc\n', 'IndexError\n']

As you can see, if we use traceback.format_exception, we end up having the full exception, including its causes, whereas with format_tb we only have the last exception which can be far less informative.

This PR makes the exception tracebacks more informative.

Copy link

@SuperDormito SuperDormito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ewjoachim
Copy link
Contributor Author

@ybastide I don't understand your last commit ?

@ewjoachim
Copy link
Contributor Author

HAHA it depends on the python version. They don't all print tracebacks the same way :D

@ybastide ybastide merged commit d69d270 into main Jan 14, 2025
9 checks passed
@ybastide ybastide deleted the stacktrace_cause branch January 14, 2025 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants