diff options
author | Nick Coghlan <ncoghlan@gmail.com> | 2012-06-23 09:52:05 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2012-06-23 09:52:05 (GMT) |
commit | 04e2e3f2310964bb353cc85e4ebedf2391109118 (patch) | |
tree | 9fa80195784e0db02237898ebc272c5d23971a69 /Lib/inspect.py | |
parent | 766e62266e72d7586e8cbf74213a3935a974ef14 (diff) | |
download | cpython-04e2e3f2310964bb353cc85e4ebedf2391109118.zip cpython-04e2e3f2310964bb353cc85e4ebedf2391109118.tar.gz cpython-04e2e3f2310964bb353cc85e4ebedf2391109118.tar.bz2 |
Close #15153: Added inspect.getgeneratorlocals to simplify whitebox testing of generator state updates
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r-- | Lib/inspect.py | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py index dd2de64..074e1b4 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1259,6 +1259,8 @@ def getattr_static(obj, attr, default=_sentinel): raise AttributeError(attr) +# ------------------------------------------------ generator introspection + GEN_CREATED = 'GEN_CREATED' GEN_RUNNING = 'GEN_RUNNING' GEN_SUSPENDED = 'GEN_SUSPENDED' @@ -1282,6 +1284,22 @@ def getgeneratorstate(generator): return GEN_SUSPENDED +def getgeneratorlocals(generator): + """ + Get the mapping of 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 isgenerator(generator): + raise TypeError("'{!r}' is not a Python generator".format(generator)) + + frame = getattr(generator, "gi_frame", None) + if frame is not None: + return generator.gi_frame.f_locals + else: + return {} + ############################################################################### ### Function Signature Object (PEP 362) ############################################################################### |