-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-79940: add introspection API for asynchronous generators #11590
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 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bca9939
inspect: add introspection API for asynchronous generators
tkren 093595a
inspect: use f-string in getasyncgenlocals()
tkren 1931c0b
inspect: use anext() built-in in TestGetAsyncGenState
tkren 771f95e
whatsnew: add an entry for inspect
tkren ac46ef8
inspect: use IsolatedAsyncioTestCase for TestGetAsyncGenState
tkren 5302f23
whatsnew: use bpo issue instead of github id
tkren 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 |
---|---|---|
|
@@ -34,6 +34,10 @@ | |
'Yury Selivanov <[email protected]>') | ||
|
||
__all__ = [ | ||
"AGEN_CLOSED", | ||
"AGEN_CREATED", | ||
"AGEN_RUNNING", | ||
"AGEN_SUSPENDED", | ||
"ArgInfo", | ||
"Arguments", | ||
"Attribute", | ||
|
@@ -77,6 +81,8 @@ | |
"getabsfile", | ||
"getargs", | ||
"getargvalues", | ||
"getasyncgenlocals", | ||
"getasyncgenstate", | ||
"getattr_static", | ||
"getblock", | ||
"getcallargs", | ||
|
@@ -1935,6 +1941,50 @@ def getcoroutinelocals(coroutine): | |
return {} | ||
|
||
|
||
# ----------------------------------- asynchronous generator introspection | ||
|
||
AGEN_CREATED = 'AGEN_CREATED' | ||
AGEN_RUNNING = 'AGEN_RUNNING' | ||
AGEN_SUSPENDED = 'AGEN_SUSPENDED' | ||
AGEN_CLOSED = 'AGEN_CLOSED' | ||
|
||
|
||
def getasyncgenstate(agen): | ||
"""Get current state of an asynchronous generator object. | ||
|
||
Possible states are: | ||
AGEN_CREATED: Waiting to start execution. | ||
AGEN_RUNNING: Currently being executed by the interpreter. | ||
AGEN_SUSPENDED: Currently suspended at a yield expression. | ||
AGEN_CLOSED: Execution has completed. | ||
""" | ||
if agen.ag_running: | ||
return AGEN_RUNNING | ||
if agen.ag_suspended: | ||
return AGEN_SUSPENDED | ||
if agen.ag_frame is None: | ||
return AGEN_CLOSED | ||
return AGEN_CREATED | ||
|
||
|
||
def getasyncgenlocals(agen): | ||
""" | ||
Get the mapping of asynchronous generator local variables to their current | ||
values. | ||
|
||
A dict is returned, with the keys the local variable names and values the | ||
bound values.""" | ||
|
||
if not isasyncgen(agen): | ||
raise TypeError(f"{agen!r} is not a Python async generator") | ||
|
||
frame = getattr(agen, "ag_frame", None) | ||
if frame is not None: | ||
return agen.ag_frame.f_locals | ||
else: | ||
return {} | ||
|
||
|
||
############################################################################### | ||
### Function Signature Object (PEP 362) | ||
############################################################################### | ||
|
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/Library/2023-02-26-17-29-57.gh-issue-79940.SAfmAy.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:`inspect.getasyncgenstate` and :func:`inspect.getasyncgenlocals`. | ||
Patch by Thomas Krennwallner. |
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
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.