contextlib.contextmanager and try/finally

johannh at gmail.com johannh at gmail.com
Wed Jan 11 12:30:58 EST 2012


On Wednesday, January 11, 2012 11:20:19 AM UTC-6, Ian wrote:
> 
> Second, I believe that passage is not referring to the contextmanager
> decorator specifically, but more generally to the changes that were
> made to allow generators to yield from within a try-finally construct
> (previously this would have been illegal syntax, since there was no
> way to guarantee the finally block would be performed).

Thanks.  You and Robert have shown me the light.  That paragraph was talking about the finalization that happens to a suspended generator when it is GC'd.  Meanwhile, the contextmanager decorator ensures that its generator is run all the way through, so it would never be left in a suspended state, so its GC/finalization semantics are irrelevant. (Please correct, if wrong.)

(Neil: I understood that it created a context manager object, but I had been reading the quoted paragraph to mean that by going through a generator the decorator was re-introducing a dependency on GC time.  But I was wrong.)

Regards,
Johann



More information about the Python-list mailing list