[New-bugs-announce] [issue3842] can't run close through itertools.chain on inner generator

Bruce Frederiksen report at bugs.python.org
Thu Sep 11 21:04:14 CEST 2008


New submission from Bruce Frederiksen <dangyogi at gmail.com>:

There is no way to get generators to clean up (run their 'finally'
clause) when used as an inner iterable to chain:

>>> def gen(n):
...     try:
...         # do stuff yielding values
...     finally:
...         # clean up
>>> c = chain.from_iterable(map(gen, (1,2,3)))
>>> next(c)
0
>>> # done with c, but can't clean up inner gen!

Could you add a 'close' method to itertools.chain that would call close
(if present) on both the inner and other iterable?  Then clean up could
be done as:

>>> with closing(chain.from_iterable(map(gen, (1,2,3)))) as c:
...    next(c)
>>> # generator finalized by "with closing"!

----------
components: Extension Modules
messages: 73052
nosy: dangyogi
severity: normal
status: open
title: can't run close through itertools.chain on inner generator
type: feature request
versions: Python 3.0

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


More information about the New-bugs-announce mailing list