diff options
author | Thomas Krennwallner <tk@postsubmeta.net> | 2023-03-11 13:19:40 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-11 13:19:40 (GMT) |
commit | ced13c96a4eb9391a9d27e3e13218f70c579670f (patch) | |
tree | 2e48fbdb4e1539ae821c5b3ac0a7a70e6d1fbf93 /Lib/inspect.py | |
parent | aa0a73d1bc53dcb6348a869df1e775138991e561 (diff) | |
download | cpython-ced13c96a4eb9391a9d27e3e13218f70c579670f.zip cpython-ced13c96a4eb9391a9d27e3e13218f70c579670f.tar.gz cpython-ced13c96a4eb9391a9d27e3e13218f70c579670f.tar.bz2 |
gh-79940: add introspection API for asynchronous generators to `inspect` module (#11590)
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r-- | Lib/inspect.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py index edc23b0..0eceaaf 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -34,6 +34,10 @@ __author__ = ('Ka-Ping Yee <ping@lfw.org>', 'Yury Selivanov <yselivanov@sprymix.com>') __all__ = [ + "AGEN_CLOSED", + "AGEN_CREATED", + "AGEN_RUNNING", + "AGEN_SUSPENDED", "ArgInfo", "Arguments", "Attribute", @@ -77,6 +81,8 @@ __all__ = [ "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) ############################################################################### |