Mercurial > cpython-lru_cache-weakref
diff Lib/inspect.py @ 77619:dd82a910eb07
Close #15153: Added inspect.getgeneratorlocals to simplify whitebox testing of generator state updates
| author | Nick Coghlan <ncoghlan@gmail.com> |
|---|---|
| date | Sat, 23 Jun 2012 19:52:05 +1000 |
| parents | 487fe648de56 |
| children | af7961e1c362 |
line wrap: on
line diff
--- a/Lib/inspect.py Sat Jun 23 10:49:36 2012 +0100 +++ b/Lib/inspect.py Sat Jun 23 19:52:05 2012 +1000 @@ -1259,6 +1259,8 @@ raise AttributeError(attr) +# ------------------------------------------------ generator introspection + GEN_CREATED = 'GEN_CREATED' GEN_RUNNING = 'GEN_RUNNING' GEN_SUSPENDED = 'GEN_SUSPENDED' @@ -1282,6 +1284,22 @@ 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) ###############################################################################
