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 dangyogi
Recipients amaury.forgeotdarc, dangyogi, rhettinger
Date 2008-09-12.18:52:19
SpamBayes Score 4.3390536e-08
Marked as misclassified No
Message-id <48CAB978.2010204@gmail.com>
In-reply-to <1221209047.08.0.842271376394.issue3842@psf.upfronthosting.co.za>
Content
What you propose is not portable code.  It won't work on jython or 
ironpython or pypy because they don't have reference counting garbage 
collectors that immediately reclaim objects.

Also I'm not asking that all c-based iterators grow a 'close' method 
(though this would be nice! :-) ).

For all other c-based iterators, you can do:

with closing(gen()) as argument:
    for x in map(fn, argument): ...

which is portable.

So I am only asking that 'close' be added to itertools.chain because 
there is no portable solution there.

-bruce

Amaury Forgeot d'Arc wrote:
> Amaury Forgeot d'Arc <amauryfa@gmail.com> added the comment:
>
> You somehow must tell the iterator that you are done with it.
> This is best done by a "del c" in your first snippet, since c is the
> last reference to the iterator. 
> To do this in a function (or a context manager), the trick is to wrap
> the map() iterator inside a generator function.
>
> def wrapiterator(it):
>     for x in it:
>         yield x
>
> Then your sample becomes:
>   
>>>> with closing(wrapiterator(chain.from_iterable(
>>>>         
> ...                  map(gen, (1,2,3))))) as c:
> ...    next(c)
>
>
> Which of course can be rewritten as:
>
> def closingiterator(it):
>     def wrapper():
>         for x in it:
>             yield x
>     return closing(wrapper())
>
>   
>>>> with closingiterator(chain.from_iterable(map(gen, (1,2,3))))) as c:
>>>>         
> ...    next(c)
>
>
> This works because the only reference to the map() iterator is held by
> the wrapper generator function. Calling its close() method will
> terminate the function, delete its locals, ultimately call the
> deallocator of the gen() iterator, which will fire the "finally" block.
>
> Your proposal implies that all c-based iterators need to grow a "close"
> method. This is not practical, and is best simulated with this wrapper
> generator: close()ing the wrapper will (recursively) destroy the iterators.
> If this also works for you, I suggest to close this issue.
>
History
Date User Action Args
2008-09-12 18:52:21dangyogisetrecipients: + dangyogi, rhettinger, amaury.forgeotdarc
2008-09-12 18:52:20dangyogilinkissue3842 messages
2008-09-12 18:52:19dangyogicreate