Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'finally' suite not invoked in generator function #736

Closed
gitlab-importer opened this issue Jun 3, 2011 · 3 comments
Closed

'finally' suite not invoked in generator function #736

gitlab-importer opened this issue Jun 3, 2011 · 3 comments
Labels
bug Something isn't working

Comments

@gitlab-importer
Copy link

In Heptapod by bitbucket_importer on Jun 3, 2011, 12:07

Created originally on Bitbucket by JonasH (Jonas Hunkemoeller)

From (bugs.pypy.org)

@gitlab-importer
Copy link
Author

In Heptapod by bitbucket_importer on Jun 3, 2011, 12:07

Created originally on Bitbucket by JonasH (Jonas Hunkemoeller)

Consider this code sample:


def g():
    try:
        yield 1
    finally:
        print 'finally'

g().next()

CPython behaves differently from PyPy:

$ python2.7 asd.py 
finally
---
$ pypy asd.py
---

The finally suite is executed only if the generator is exhausted in such way 
that StopIteration is raised.

@gitlab-importer
Copy link
Author

In Heptapod by bitbucket_importer on Jun 3, 2011, 12:11

Created originally on Bitbucket by antocuni (Antonio Cuni)

The finally is printed when the generator object is garbage collected.
Since CPython has reference counting, the generator is collected immediately,
while on PyPy is not.

If you add "import gc; gc.collect()" at the end of the script, the "finally" is
printed also on Pypy

@gitlab-importer
Copy link
Author

In Heptapod by bitbucket_importer on May 14, 2015, 11:08

Created originally on Bitbucket by antondries (Anton Dries)

Issue #2047 was marked as a duplicate of this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant