[New-bugs-announce] [issue10220] Make generator state easier to introspect

Nick Coghlan report at bugs.python.org
Thu Oct 28 14:42:53 CEST 2010

New submission from Nick Coghlan <ncoghlan at gmail.com>:

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:

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

Currently executing:
  (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"

  "g.gi_frame is None"

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


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

(the lack of underscores in the function name follows the general style of the inspect module)

components: Library (Lib)
keywords: easy
messages: 119774
nosy: ncoghlan
priority: normal
severity: normal
stage: needs patch
status: open
title: Make generator state easier to introspect
type: feature request
versions: Python 3.2

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list