Repository navigation
bpo-36829: Add test.support.catch_unraisable_exception() #13490
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
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2342,12 +2342,24 @@ async def corofn(): | |
| orig_wuc = warnings._warn_unawaited_coroutine | ||
| try: | ||
| warnings._warn_unawaited_coroutine = lambda coro: 1/0 | ||
| with support.captured_stderr() as stream: | ||
| corofn() | ||
| support.gc_collect() | ||
| self.assertIn("Exception ignored in", stream.getvalue()) | ||
| self.assertIn("ZeroDivisionError", stream.getvalue()) | ||
| self.assertIn("was never awaited", stream.getvalue()) | ||
| try: | ||
| # Note: catch_unraisable_exception() resurect the coroutine | ||
| with support.catch_unraisable_exception() as cm, \ | ||
| support.captured_stderr() as stream: | ||
| # only store repr() to avoid keeping the coroutine alive | ||
| coro = corofn() | ||
| coro_repr = repr(coro) | ||
|
|
||
| # clear reference to the coroutine without awaiting for it | ||
| del coro | ||
| support.gc_collect() | ||
|
|
||
| self.assertEqual(repr(cm.unraisable.object), coro_repr) | ||
| self.assertEqual(cm.unraisable.exc_type, ZeroDivisionError) | ||
| self.assertIn("was never awaited", stream.getvalue()) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a little bit strange to have to catch half of the error message from stderr, and the other half from sys.unraisablehook. My PR #13488 will allow to inject the error message in the unraisable exception to catch both at the same time. |
||
| finally: | ||
| # Explicitly break reference cycle | ||
| cm = None | ||
|
|
||
| del warnings._warn_unawaited_coroutine | ||
| with support.captured_stderr() as stream: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Tests/2019-05-22-12-57-15.bpo-36829.e9mRWC.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Add :func:`test.support.catch_unraisable_exception`: context manager | ||
| catching unraisable exception using :func:`sys.unraisablehook`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
this try/with/finally is somewhat tricky, how about:
eg:
Uh oh!
There was an error while loading. Please reload this page.
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.
or recommend usage like this:
by updating
__exit__: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.
Good idea :-) I implemented your
__exit__idea to avoid the need for try/finally.throw_unraisable_exceptions() might be useful, but modified tests needs to access the 'obj' attribute of the unraisable hook. Later, they might also want to get access to the 'err_msg' attribute: #13488
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.
There's still a sharp edge here:
how about making
cm.unraisablethrow if accessed outside the context instead of being None, eg:Uh oh!
There was an error while loading. Please reload this page.
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.
and if you want to access additional unraisablehook info using throw_unraisable_exceptions:
then you can use it with:
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.
I wrote PR #13554 to implement your "del self.unraisable" idea.