How does GC affect generator context managers?

J

Jason

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top