This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ncoghlan
Recipients ezio.melotti, martin.panter, ncoghlan, pitrou, rhettinger, serhiy.storchaka
Date 2015-09-06.22:30:34
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1441578635.08.0.564733902144.issue25014@psf.upfronthosting.co.za>
In-reply-to
Content
__exit__ will still get invoked even if the generator is never exhausted:

>>> def itercm(cm):
...     with cm:
...         yield from cm
... 
>>> class TestCM:
...     def __iter__(self):
...         yield 6
...         yield 9
...         yield 42
...     def __enter__(self):
...         return self
...     def __exit__(self, *args):
...         print("Terminated CM")
... 
>>> itr = itercm(TestCM())
>>> next(itr)
6
>>> del itr
Terminated CM

We addressed the major problems with generators failing to clean up resources back when generator.close() was introduced in PEP 342, and then Antoine addressed the cyclic GC problem in PEP 442.

The key thing that itercm() adds over the status quo is that if the generator *is* exhausted, then the resource *will* be cleaned up immediately. If the generator *isn't* exhausted, then it falls back to non-deterministic GC based cleanup, which is what you'd get today by not using a context manager at all.

To be convinced that we need a third cleanup option beyond "always deterministic" and "always non-deterministic", I'd need some concrete use cases where the success case needs deterministic cleanup, but the error case is OK with non-deterministic cleanup.
History
Date User Action Args
2015-09-06 22:30:35ncoghlansetrecipients: + ncoghlan, rhettinger, pitrou, ezio.melotti, martin.panter, serhiy.storchaka
2015-09-06 22:30:35ncoghlansetmessageid: <1441578635.08.0.564733902144.issue25014@psf.upfronthosting.co.za>
2015-09-06 22:30:35ncoghlanlinkissue25014 messages
2015-09-06 22:30:34ncoghlancreate