Issue 13814: Document why generators don't support the context management protocol (original) (raw)

Issue13814

Created on 2012-01-18 10:43 by yak, last changed 2022-04-11 14:57 by admin. 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: Alyssa 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
2022-04-11 14:57:25 admin set github: 58022
2021-06-26 14:49:27 terry.reedy set messages: +
2021-06-21 22:03:33 terry.reedy set status: open -> closedresolution: fixedstage: patch review -> resolved
2021-06-21 22:03:09 terry.reedy set messages: +
2021-06-21 22:02:50 terry.reedy set messages: +
2021-06-21 21:23:44 miss-islington set pull_requests: + <pull%5Frequest25418>
2021-06-21 21:23:38 miss-islington set nosy: + miss-islingtonpull_requests: + <pull%5Frequest25417>
2021-06-21 21:23:36 terry.reedy set messages: +
2021-06-21 19:54:08 terry.reedy set keywords: - easymessages: +
2021-06-21 19:53:30 terry.reedy set keywords: + patchpull_requests: + <pull%5Frequest25416>
2021-06-21 17:33:50 iritkatriel set keywords: + easy, - patchversions: + Python 3.9, Python 3.10, Python 3.11, - Python 3.5, Python 3.6
2016-01-02 08:58:08 ezio.melotti set stage: needs patch -> patch reviewversions: + Python 3.5, Python 3.6, - Python 3.4
2015-04-22 04:05:41 martin.panter set nosy: + martin.panter
2015-04-14 00:54:29 r.david.murray set nosy: + r.david.murraymessages: +
2015-04-13 20:26:38 zektron42 set files: + design.patchkeywords: + patchversions: + Python 3.4, - Python 3.3
2012-01-22 15:51:35 meador.inge set nosy: + meador.inge
2012-01-22 06:58:16 ncoghlan set status: pending -> openassignee: docs@pythoncomponents: + Documentationtitle: Generators as context managers. -> Document why generators don't support the context management protocolnosy: + docs@pythonmessages: + stage: test needed -> needs patch
2012-01-21 04:03:30 terry.reedy set status: open -> pendingnosy: + terry.reedymessages: + stage: test needed
2012-01-20 15:07:39 pitrou set nosy: + ncoghlanversions: + Python 3.3, - Python 2.7, Python 3.4
2012-01-18 10:57:59 ezio.melotti set nosy: + ezio.melottimessages: +
2012-01-18 10:43:15 yak create