summaryrefslogtreecommitdiffstats
path: root/Lib/inspect.py
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2010-11-21 03:44:04 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2010-11-21 03:44:04 (GMT)
commite0f04659cd717a031b42dcfd43ca07824b24f9b2 (patch)
tree63fbd71f11a93bb398b5d1f6c2d91b116bc7d78e /Lib/inspect.py
parentd3309df40b5335ebf225a359e5cb3b324801e221 (diff)
downloadcpython-e0f04659cd717a031b42dcfd43ca07824b24f9b2.zip
cpython-e0f04659cd717a031b42dcfd43ca07824b24f9b2.tar.gz
cpython-e0f04659cd717a031b42dcfd43ca07824b24f9b2.tar.bz2
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r--Lib/inspect.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 241cd08..e410dba 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -1128,3 +1128,23 @@ def getattr_static(obj, attr, default=_sentinel):
if default is not _sentinel:
return default
raise AttributeError(attr)
+
+
+GEN_CREATED, GEN_RUNNING, GEN_SUSPENDED, GEN_CLOSED = range(4)
+
+def getgeneratorstate(generator):
+ """Get current state of a generator-iterator.
+
+ Possible states are:
+ GEN_CREATED: Waiting to start execution.
+ GEN_RUNNING: Currently being executed by the interpreter.
+ GEN_SUSPENDED: Currently suspended at a yield expression.
+ GEN_CLOSED: Execution has completed.
+ """
+ if generator.gi_running:
+ return GEN_RUNNING
+ if generator.gi_frame is None:
+ return GEN_CLOSED
+ if generator.gi_frame.f_lasti == -1:
+ return GEN_CREATED
+ return GEN_SUSPENDED