-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
bpo-32311: Implement asyncio.create_task() shortcut #4848
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 all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
67c3520
Add helpers
asvetlov e693fac
Merge remote-tracking branch 'upstream/master' into asyncio-toplevel-…
asvetlov d940cc3
Drop run_in_executor shortcut
asvetlov 298d38e
Move create_task() into tasks.py
asvetlov 8ca254e
Extract asyncio.utils, add runtime checks to task ctors
asvetlov a25f1cb
Add tests
asvetlov ef9ccda
utils.py -> format_helpers.py
asvetlov d122823
Merge remote-tracking branch 'upstream/master' into asyncio-toplevel-…
asvetlov 7cbb758
Don't expose asyncio.extract_stack
asvetlov 14b2588
Add test for asyncio.create_task()
asvetlov e1c1ed1
Fix implementation
asvetlov 8533c74
Add NEWS entry
asvetlov 0f6ca60
Update docs
asvetlov a5d4a8b
Change exception message
asvetlov 0666276
Update doc
asvetlov 5dbae4e
Fix notes
asvetlov 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
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
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,75 @@ | ||
| import functools | ||
| import inspect | ||
| import reprlib | ||
| import traceback | ||
|
|
||
| from . import constants | ||
|
|
||
|
|
||
| def _get_function_source(func): | ||
| func = inspect.unwrap(func) | ||
| if inspect.isfunction(func): | ||
| code = func.__code__ | ||
| return (code.co_filename, code.co_firstlineno) | ||
| if isinstance(func, functools.partial): | ||
| return _get_function_source(func.func) | ||
| if isinstance(func, functools.partialmethod): | ||
| return _get_function_source(func.func) | ||
| return None | ||
|
|
||
|
|
||
| def _format_callback_source(func, args): | ||
| func_repr = _format_callback(func, args, None) | ||
| source = _get_function_source(func) | ||
| if source: | ||
| func_repr += f' at {source[0]}:{source[1]}' | ||
| return func_repr | ||
|
|
||
|
|
||
| def _format_args_and_kwargs(args, kwargs): | ||
| """Format function arguments and keyword arguments. | ||
|
|
||
| Special case for a single parameter: ('hello',) is formatted as ('hello'). | ||
| """ | ||
| # use reprlib to limit the length of the output | ||
| items = [] | ||
| if args: | ||
| items.extend(reprlib.repr(arg) for arg in args) | ||
| if kwargs: | ||
| items.extend(f'{k}={reprlib.repr(v)}' for k, v in kwargs.items()) | ||
| return '({})'.format(', '.join(items)) | ||
|
|
||
|
|
||
| def _format_callback(func, args, kwargs, suffix=''): | ||
| if isinstance(func, functools.partial): | ||
| suffix = _format_args_and_kwargs(args, kwargs) + suffix | ||
| return _format_callback(func.func, func.args, func.keywords, suffix) | ||
|
|
||
| if hasattr(func, '__qualname__'): | ||
| func_repr = getattr(func, '__qualname__') | ||
| elif hasattr(func, '__name__'): | ||
| func_repr = getattr(func, '__name__') | ||
| else: | ||
| func_repr = repr(func) | ||
|
|
||
| func_repr += _format_args_and_kwargs(args, kwargs) | ||
| if suffix: | ||
| func_repr += suffix | ||
| return func_repr | ||
|
|
||
|
|
||
| def extract_stack(f=None, limit=None): | ||
| """Replacement for traceback.extract_stack() that only does the | ||
| necessary work for asyncio debug mode. | ||
| """ | ||
| if f is None: | ||
| f = sys._getframe().f_back | ||
| if limit is None: | ||
| # Limit the amount of work to a reasonable amount, as extract_stack() | ||
| # can be called for each coroutine and future in debug mode. | ||
| limit = constants.DEBUG_STACK_DEPTH | ||
| stack = traceback.StackSummary.extract(traceback.walk_stack(f), | ||
| limit=limit, | ||
| lookup_lines=False) | ||
| stack.reverse() | ||
| return stack | ||
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
Oops, something went wrong.
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.
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.
Can we keep
extract_stackinevents.py?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.
No.
extract_stackis used incoroutines.pywhich in turn is used by_asynciomodule.c.