mirror of
https://github.com/python/cpython.git
synced 2025-10-09 08:31:26 +00:00
Close #15153: Added inspect.getgeneratorlocals to simplify whitebox testing of generator state updates
This commit is contained in:
parent
766e62266e
commit
04e2e3f231
5 changed files with 98 additions and 0 deletions
|
@ -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)
|
||||
###############################################################################
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue