-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gh-79940: add introspection API for asynchronous generators to `inspe…
…ct` module (#11590)
- Loading branch information
Showing
6 changed files
with
199 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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