Title: calling close on an iterator generator
Type: behaviour Severity: major
Components: Core Versions: 2.5alpha1
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: zyasoft Nosy List: egonw_, zyasoft
Priority: normal Keywords:

Created on 2008-09-01.06:55:20 by egonw_, last changed 2008-09-05.03:04:24 by zyasoft.

msg3466 (view) Author: (egonw_) Date: 2008-09-01.06:55:19
There is an incompatibility with CPython 2.5 w.r.t. to caling close on
iterator generators. Please consider the following code:

def gen3(n):
        for x in range(n):
            print "gen3 for x, before for y, x:", x
            for y in range(x+1):
              print "gen3 for y, y:", y
              yield y
            print "gen3 for x, after for y, x:", x
        print "gen3 finally"

def test3():
        generated = gen3(3)
        for x in generated:
            print "test3 for x, x:", x

        print "test3 finally"

The output of CPython is:

gen3 for x, before for y, x: 0
gen3 for y, y: 0
test3 for x, x: 0
gen3 finally
test3 finally

The output of Jython is:

gen3 for x, before for y, x: 0
gen3 for y, y: 0
test3 for x, x: 0
test3 finally

It seems that closing the iterator generator does not run the finally
clause of gen3.
msg3469 (view) Author: Jim Baker (zyasoft) Date: 2008-09-05.03:04:23
This will not occur until the generator is garbage collected. We have a
specific test for this in where we do this:

>>> del g; gc.collect(); sleep(1); gc.collect()
Date User Action Args
2008-09-05 03:04:24zyasoftsetstatus: open -> closed
nosy: + zyasoft
messages: + msg3469
priority: normal
assignee: zyasoft
resolution: works for me
2008-09-01 06:55:20egonw_create