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 sjdrake
Recipients docs@python, sjdrake
Date 2015-01-13.07:35:44
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
If a generator has its close() method called before any items are requested from it, a finally block in the generator function will not be executed.

I encountered this when wrapping an open file to alter the result of iterating over it.  Using a generator function with a try/finally block seemed like a simple way of acheiving this.  Here's an example that logs each line as it's read:

def logged_lines(f):
        for line in f:
            yield line

If the generator is created and closed immediately, the underlying file-like object is left open:
>>> f = urlopen('')
>>> lines = logged_lines(f)
>>> lines.close()
>>> f.closed

But once the first item is requested from the generator, close() will trigger cleanup:
>>> lines = logged_lines(f)
>>> next(lines)
WARNING:root:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n'
>>> lines.close()
>>> f.closed

Having read the documentation for yield expressions, I don't believe this behaviour to be non-conformant - but it still seems like a bit of a gotcha to me.  Should this usage be warned against?
Date User Action Args
2015-01-13 07:35:46sjdrakesetrecipients: + sjdrake, docs@python
2015-01-13 07:35:46sjdrakesetmessageid: <>
2015-01-13 07:35:46sjdrakelinkissue23227 messages
2015-01-13 07:35:44sjdrakecreate