[issue25014] Add contextlib.itercm()

Martin Panter report at bugs.python.org
Mon Sep 7 00:13:10 CEST 2015


Martin Panter added the comment:

I had also thought of this kind of function, but I dismissed it because it would either have to rely on garbage collection or an explicit close() call to close the generator in case the iteration is aborted. I think it might need some kind of “with-for” combined statement added to the langauge to be bulletproof.

Considering the second example in your script, “exit is called in case of errors”: What is stopping the interpreter from storing the iterator of the current “for” loop in the top-level frame object? Then the iterator would be referenced by the exception traceback, and prevent garbage collection of its itercm() instance. Hypothetically:

__traceback__ → tb_frame → “for” iterator → itercm() instance

Also, I would tend to put this sort of function in “itertools”, since generators are not context managers by design.

----------
nosy: +martin.panter

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


More information about the Python-bugs-list mailing list