-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
bpo-24959: fix unittest.assertRaises bug where traceback entries are dropped from chained exceptions #23688
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
Conversation
10ace29
to
3a01233
Compare
This PR is stale because it has been open for 30 days with no activity. |
See also issue24959 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM in general.
I think that tracebacks in __context__
(and maybe __cause__
) chains should be cleaned recursively too. For the case like:
try:
self.assertFoo()
finally:
self.assertBar()
Is it possible to write tests which do not rely on private methods? |
Thanks. I've marked this as draft because I wanted consider a different approach to this. We recently had some changes in the traceback module that allow you to customize how frames are rendered, and also to exclude frames from the output. I think it might be better to use that approach here. I'll make another PR soon. |
… dropped from the chained exception
Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Serhiy Storchaka <[email protected]>
9c9acf5
to
7274a04
Compare
I rewrote the test to not rely on private methods. The traceback utility we added recently doesn't help because it allows to filter FrameSummary objects (not frames) so we no longer have access to the globals. Regarding filtering chained tracebacks - I think this is another problem. The current filtering assumes that the traceback starts in user code. With chained tracebacks I'm not sure it is (in the contrived test I tried this wasn't the case). I think we should define the use case and desired behaviour before implementing. |
Ah I've rebased against main as well. |
The use case is:
We want to acquire some resource or make some environment changes for the part of the test and release the resource or restore environment after testing, so we use the I do not know the use case for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but I'll try to write more tests. Surprisingly, there are no tests for removing unittest frames at all.
See testStackFrameTrimming in test_result. |
I added the chained exceptions. I avoided recursion because it can be just a bit too deep for certain constructs involving recursion. |
Hello, Any updates on the PR? Is it possible to get this in soon? :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Thanks @iritkatriel for the PR 🌮🎉.. I'm working now to backport this PR to: 3.9, 3.10. |
Sorry, @iritkatriel, I could not cleanly backport this to |
GH-31775 is a backport of this pull request to the 3.10 branch. |
Thank you @serhiy-storchaka ! |
…dropped from chained exceptions (pythonGH-23688) (cherry picked from commit 88b7d86) Co-authored-by: Irit Katriel <[email protected]>
GH-31776 is a backport of this pull request to the 3.9 branch. |
…dropped from chained exceptions (GH-23688) (cherry picked from commit 88b7d86) Co-authored-by: Irit Katriel <[email protected]>
Just to clarify: Will this work with python 3.7 or it would need to be backported? |
It will not be backport Ed to 3.7. |
…dropped from chained exceptions (pythonGH-23688) (pythonGH-31776) (cherry picked from commit 88b7d86)
Unittest applies a hack that uses the limit parameter of TracebackException to hide traceback frames from the unittest code itself. The problem with this is that the limit applies to the chained (context) exception too, so we lose frames we want to keep.
This patch replaces the hack by code that modifies the traceback to remove the frames we don't want.
https://bugs.python.org/issue24959