classification
Title: Document why generators don't support the context management protocol
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, ezio.melotti, martin.panter, meador.inge, miss-islington, ncoghlan, r.david.murray, terry.reedy, yak
Priority: normal Keywords: patch

Created on 2012-01-18 10:43 by yak, last changed 2021-06-26 14:49 by terry.reedy. This issue is now closed.

Files
File name Uploaded Description Edit
design.patch zektron42, 2015-04-13 20:26 This should add in some documentation to the design faqs review
Pull Requests
URL Status Linked Edit
PR 26835 merged terry.reedy, 2021-06-21 19:53
PR 26836 merged miss-islington, 2021-06-21 21:23
PR 26837 merged miss-islington, 2021-06-21 21:23
Messages (10)
msg151530 - (view) Author: Arkadiusz Wahlig (yak) Date: 2012-01-18 10:43
Generators should support the with statement with __exit__ calling self.close().

with genfunc() as g:
    for item in g:
        print(item)
msg151532 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2012-01-18 10:57
If you want to call .close() automatically on something you can use contextlib.closing(): http://docs.python.org/library/contextlib.html#contextlib.closing
msg151717 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-01-21 04:03
Calling g.close() is pointless for a generator used in normal pull mode and run to completion, as in the example. The generator is already 'closed', so g.close() does not do anything useful. See
http://docs.python.org/py3k/reference/expressions.html#yield-expressions

The method was added to be used with .send() so that generators used in push mode could be told to finish up when there is nothing more to send. For such rare uses, contextlib.closing should usually be sufficient, I think.

So I think this issue should be closed.
msg151763 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2012-01-22 06:58
Generators deliberately don't support the context management protocol. This is so that they raise an explicit TypeError or AttributeError (pointing out that __exit__ is missing) if you leave out the @contextmanager decorator when you're using a generator to write an actual context manager.

Generators supporting the context management protocol natively would turn that into a far more subtle (and confusing) error: your code would silently fail to invoke the generator body.

Ensuring this common error remains easy to detect is far more important than making it easier to invoke close() on a generator object (particularly when contextlib.closing() already makes that very easy).
msg240820 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-04-14 00:54
Looks like the right approach, I hadn't thought of the design FAQ, but it makes sense as a place to put it.  I made a couple of review comments.
msg396285 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-06-21 19:54
I added a simplified answer after a similar question about assignment and CMs.
msg396290 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-06-21 21:23
New changeset 51f45d085dad3b708f6fe166af517aba69e7e9f7 by Terry Jan Reedy in branch 'main':
bpo-13814: Explain why generators are not context managers (GH-26835)
https://github.com/python/cpython/commit/51f45d085dad3b708f6fe166af517aba69e7e9f7
msg396293 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-06-21 22:02
New changeset 1e16217204c0e8e595c4d1e869c81899bfe3376b by Miss Islington (bot) in branch '3.10':
bpo-13814: Explain why generators are not context managers (GH-26835)
https://github.com/python/cpython/commit/1e16217204c0e8e595c4d1e869c81899bfe3376b
msg396294 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-06-21 22:03
New changeset d881002fbdf12ddbd93db3e182dc5cdeb1f90386 by Miss Islington (bot) in branch '3.9':
bpo-13814: Explain why generators are not context managers (GH-26835)
https://github.com/python/cpython/commit/d881002fbdf12ddbd93db3e182dc5cdeb1f90386
msg396552 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-06-26 14:49
Note: Rietveld patch reviews are no longer accessible so I could not look at R. David's comments.
History
Date User Action Args
2021-06-26 14:49:27terry.reedysetmessages: + msg396552
2021-06-21 22:03:33terry.reedysetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-06-21 22:03:09terry.reedysetmessages: + msg396294
2021-06-21 22:02:50terry.reedysetmessages: + msg396293
2021-06-21 21:23:44miss-islingtonsetpull_requests: + pull_request25418
2021-06-21 21:23:38miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request25417
2021-06-21 21:23:36terry.reedysetmessages: + msg396290
2021-06-21 19:54:08terry.reedysetkeywords: - easy

messages: + msg396285
2021-06-21 19:53:30terry.reedysetkeywords: + patch
pull_requests: + pull_request25416
2021-06-21 17:33:50iritkatrielsetkeywords: + easy, - patch
versions: + Python 3.9, Python 3.10, Python 3.11, - Python 3.5, Python 3.6
2016-01-02 08:58:08ezio.melottisetstage: needs patch -> patch review
versions: + Python 3.5, Python 3.6, - Python 3.4
2015-04-22 04:05:41martin.pantersetnosy: + martin.panter
2015-04-14 00:54:29r.david.murraysetnosy: + r.david.murray
messages: + msg240820
2015-04-13 20:26:38zektron42setfiles: + design.patch
keywords: + patch
versions: + Python 3.4, - Python 3.3
2012-01-22 15:51:35meador.ingesetnosy: + meador.inge
2012-01-22 06:58:16ncoghlansetstatus: pending -> open

assignee: docs@python
components: + Documentation
title: Generators as context managers. -> Document why generators don't support the context management protocol
nosy: + docs@python

messages: + msg151763
stage: test needed -> needs patch
2012-01-21 04:03:30terry.reedysetstatus: open -> pending

nosy: + terry.reedy
messages: + msg151717

stage: test needed
2012-01-20 15:07:39pitrousetnosy: + ncoghlan

versions: + Python 3.3, - Python 2.7, Python 3.4
2012-01-18 10:57:59ezio.melottisetnosy: + ezio.melotti
messages: + msg151532
2012-01-18 10:43:15yakcreate