This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ncoghlan
Recipients gvanrossum, ncoghlan, pitrou
Date 2010-10-29.00:26:01
SpamBayes Score 1.220255e-05
Marked as misclassified No
Message-id <1288311964.34.0.620278625513.issue10220@psf.upfronthosting.co.za>
In-reply-to
Content
So something like:

GEN_CREATED, GEN_ACTIVE, GEN_CLOSED = range(3)

def getgeneratorstate(g):
  """Get current state of a generator-iterator.
  
  Possible states are:
    GEN_CREATED: Created, waiting to start execution
    GEN_ACTIVE: Currently being executed (or suspended at yield)
    GEN_CLOSED: Execution has completed

  Use g.gi_running to determine if an active generator is running or
  is suspended at a yield expression.
  """
  if g.gi_frame is None:
    return GEN_CLOSED
  if g.gi_frame.f_lasti == -1:
    return GEN_CREATED
  return GEN_ACTIVE

Having 4 separate states actually makes the documentation a little easier to write:

def getgeneratorstate(g):
  """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
  """

Checking if the generator is active is then just a matter of checking "gen_state in (GEN_RUNNING, GEN_SUSPENDED)".
History
Date User Action Args
2010-10-29 00:26:04ncoghlansetrecipients: + ncoghlan, gvanrossum, pitrou
2010-10-29 00:26:04ncoghlansetmessageid: <1288311964.34.0.620278625513.issue10220@psf.upfronthosting.co.za>
2010-10-29 00:26:03ncoghlanlinkissue10220 messages
2010-10-29 00:26:01ncoghlancreate