How does GC affect generator context managers?

Jason jason.heeris at gmail.com
Tue Nov 30 09:16:24 EST 2010


I've been reading through the docs for contextlib and PEP 343, and
came across this:

    Note that we're not guaranteeing that the finally-clause is
    executed immediately after the generator object becomes unused,
    even though this is how it will work in CPython.

...referring to context managers created via the
contextlib.contextmanager decorator containing cleanup code in a
"finally" clause. While I understand that Python-the-language does not
specify GC semantics, and different implementations can do different
things with that, what I don't get is how GC even relates to a context
manager created from a generator.

As I understood it, when the "with" block exits, the __exit__() method
is called immediately. This calls the next() method on the underlying
generator, which forces it to run to completion (and raise a
StopIteration), which includes the finally clause... right?

— Jason



More information about the Python-list mailing list