[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:

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)

----------
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>
<http://bugs.python.org/issue10220>
_______________________________________


More information about the New-bugs-announce mailing list