[issue25014] Add contextlib.itercm()

Nick Coghlan report at bugs.python.org
Mon Sep 7 00:30:35 CEST 2015


Nick Coghlan added the comment:

__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.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25014>
_______________________________________


More information about the Python-bugs-list mailing list