[issue3842] can't run close through itertools.chain on inner generator

2008-09-12 Thread Amaury Forgeot d'Arc

Amaury Forgeot d'Arc [EMAIL PROTECTED] 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.

--
nosy: +amaury.forgeotdarc
resolution:  - works for me
status: open - pending

___
Python tracker [EMAIL PROTECTED]
http://bugs.python.org/issue3842
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue3842] can't run close through itertools.chain on inner generator

2008-09-12 Thread Raymond Hettinger

Raymond Hettinger [EMAIL PROTECTED] added the comment:

Sorry, am going to reject this.   The use cases are somewhat uncommon
and I don't want to clutter-up tools that need to remain as simple as
possible.  The pure python code for chain() is so simple that it's not
hard to roll your own version as needed.

--
resolution: works for me - rejected
status: pending - closed

___
Python tracker [EMAIL PROTECTED]
http://bugs.python.org/issue3842
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue3842] can't run close through itertools.chain on inner generator

2008-09-11 Thread Bruce Frederiksen

New submission from Bruce Frederiksen [EMAIL PROTECTED]:

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 [EMAIL PROTECTED]
http://bugs.python.org/issue3842
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue3842] can't run close through itertools.chain on inner generator

2008-09-11 Thread Benjamin Peterson

Changes by Benjamin Peterson [EMAIL PROTECTED]:


--
assignee:  - rhettinger
nosy: +rhettinger
versions: +Python 2.7, Python 3.1 -Python 3.0

___
Python tracker [EMAIL PROTECTED]
http://bugs.python.org/issue3842
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com