Author ncoghlan
Recipients ncoghlan
Date 2010-10-28.12:42:51
SpamBayes Score 0.000684052
Marked as misclassified No
Message-id <1288269773.83.0.431465195808.issue10220@psf.upfronthosting.co.za>
In-reply-to
Content
Generators can be in four different states that may be relevant to framework code making use of them (especially as coroutines). This state is all currently available from Python code, but is rather obscure and could be made readable.

The four states are:

Created:
  "g.gi_frame is not None and g.gi_frame.f_lasti == -1"
  (Frame exists, but no instructions have been executed yet)

Currently executing:
  "g.gi_running"
  (This being true implies other things about the state as well, but this is all you need to check)

Suspended at a yield point:
  "g.gi_frame is not None and g.gi_frame.f_lasti != -1 and not g.gi_running"

Exhausted/closed:
  "g.gi_frame is None"

My API proposal is to add the following to the inspect module:

GEN_CREATED, GEN_RUNNING, GEN_SUSPENDED, GEN_CLOSED = range(4)

def getgeneratorstate(g):
  if g.gi_running:
    return GEN_RUNNING
  if g.gi_frame is None:
    return GEN_CLOSED
  if g.gi_frame.f_lasti == -1:
    return GEN_CREATED
  return GEN_SUSPENDED

(the lack of underscores in the function name follows the general style of the inspect module)
History
Date User Action Args
2010-10-28 12:42:53ncoghlansetrecipients: + ncoghlan
2010-10-28 12:42:53ncoghlansetmessageid: <1288269773.83.0.431465195808.issue10220@psf.upfronthosting.co.za>
2010-10-28 12:42:52ncoghlanlinkissue10220 messages
2010-10-28 12:42:51ncoghlancreate