-
-
Notifications
You must be signed in to change notification settings - Fork 31.3k
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
TracebackException should not hold reference to the exception traceback #86648
Comments
TracebackException holds a reference to the exc_traceback, which is wrong because (1) it's supposed to capture the output without holding references to real things. (2) it makes comparison wrong for equivalent exceptions, as in this example: ------------------------------------------ import sys
import traceback
excs = []
for _ in range(2):
try:
1/0
except:
excs.append(traceback.TracebackException(*sys.exc_info())) print('formats equal: ', list(excs[0].format()) == list(excs[0].format())) ------------------------------------------ formats equal: True ------------------------------------------ The good news is that it's only used to check for non-None (added here https://bugs.python.org/issue24695) so should be easy to remove. |
This would be a change of behaviour, and 3.8 and 3.9 are in feature freeze, so we could only add it in 3.10. You say: "it's supposed to capture the output without holding references to real things" Is this requirement documented somewhere, or is it just your preference? "it makes comparison wrong for equivalent exceptions" If the tracebacks are different, they're not exactly equivalent. If we did accept this patch, would that mean there would no longer be any need to delete the exception variable at the end of an |
From the TracebackException docstring: "The traceback module captures enough attributes from the original exception to this intermediary form to ensure that no references are held, while still being able to fully print or format it."
The formatted form is identical, so from TracebackException's POV they are equivalent.
No, it's not related to that. |
For background, see iritkatriel#3 (comment) -- it seems the link to exc_traceback was added with little concern for the original design of TracebackExceptionGroup. The question is, can we get rid of it, even though it's been undocumented. |
I consider PR 23531 change as a bugfix and IMO it's fine to backport it. TracebackException docstring is quite explicit:
This issue shows that there was a bug in the implementation. -- The unit test should check the ref count of the exception and the exception traceback, to check functionally that no strong reference is hold. |
I've added the refcount check that Victor suggested. |
Re-adding older versions. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: