[Python-Dev] Combining the best of PEP 288 and PEP 325: generator exceptions and cleanup

Guido van Rossum gvanrossum at gmail.com
Thu May 19 19:09:00 CEST 2005


[Phillip J. Eby]the only way a generator-iterator can become
> part of a cycle is via  an action taken outside the generator.  (E.g.
> passing it into itself via 'continue', creating a link from one of its
> arguments to it, etc.)  So, it's probably not a terrible limitation in
> practice.

It's enough to store a reference to the generator in a global (or in
anything that's reachable from a global). The generator's frame's
f_globals pointer then ensures the cycle.

Throwing an exception also provides ample opportunity for creating
cycles, since the frame hold a reference to the most recent traceback.
Ironically, throwing an exception with a traceback into a generator is
likely to cause a cycle because the traceback likely references the
throwing frame, which certainly has a reference to the generator...

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list