gh-135110: Fix misleading generator.close() documentation#135152
Merged
ZeroIntensity merged 6 commits intopython:mainfrom Jun 26, 2025
cdenihan:fix-generator-close-docs
Merged
gh-135110: Fix misleading generator.close() documentation#135152ZeroIntensity merged 6 commits intopython:mainfrom cdenihan:fix-generator-close-docs
ZeroIntensity merged 6 commits intopython:mainfrom
cdenihan:fix-generator-close-docs
Conversation
The documentation incorrectly stated that generator.close() 'raises' a GeneratorExit exception. This was misleading because the method doesn't raise the exception to the caller - it sends the exception internally to the generator and returns None. Changed 'raises' to 'sends' in both Doc/reference/expressions.rst and Doc/howto/functional.rst for consistency and accuracy. Fixes #135110
Member
ZeroIntensity
left a comment
There was a problem hiding this comment.
I'm a little worried about the use of the word "send" here, especially considering generator.send() is a thing. I think "raises" is fine, we should just make it clearer that the actual yield expressions are what raise. Maybe we could say that close() is equivalent to throw(GeneratorExit)?
Replace 'sends' with 'raises' and clarify that close() is equivalent to throw(GeneratorExit). This makes the documentation clearer and avoids confusion with generator.send().
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Member
|
It looks like the CLA got screwed up. |
Contributor
Author
|
Just signed it again, should be all good now. |
|
Thanks @cdenihan for the PR, and @ZeroIntensity for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14. |
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this pull request
Jun 26, 2025
…thonGH-135152) The documentation incorrectly stated that generator.close() 'raises' a GeneratorExit exception. This was misleading because the method doesn't raise the exception to the caller - it sends the exception internally to the generator and returns None. (cherry picked from commit 0d76dcc) Co-authored-by: Connor Denihan <188690869+cdenihan@users.noreply.github.com>
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this pull request
Jun 26, 2025
…thonGH-135152) The documentation incorrectly stated that generator.close() 'raises' a GeneratorExit exception. This was misleading because the method doesn't raise the exception to the caller - it sends the exception internally to the generator and returns None. (cherry picked from commit 0d76dcc) Co-authored-by: Connor Denihan <188690869+cdenihan@users.noreply.github.com>
|
GH-135985 is a backport of this pull request to the 3.14 branch. |
|
GH-135986 is a backport of this pull request to the 3.13 branch. |
ZeroIntensity
pushed a commit
that referenced
this pull request
Jun 26, 2025
…H-135152) (GH-135986) gh-135110: Fix misleading `generator.close()` documentation (GH-135152) The documentation incorrectly stated that generator.close() 'raises' a GeneratorExit exception. This was misleading because the method doesn't raise the exception to the caller - it sends the exception internally to the generator and returns None. (cherry picked from commit 0d76dcc) Co-authored-by: Connor Denihan <188690869+cdenihan@users.noreply.github.com>
ZeroIntensity
pushed a commit
that referenced
this pull request
Jun 26, 2025
…H-135152) (GH-135985) gh-135110: Fix misleading `generator.close()` documentation (GH-135152) The documentation incorrectly stated that generator.close() 'raises' a GeneratorExit exception. This was misleading because the method doesn't raise the exception to the caller - it sends the exception internally to the generator and returns None. (cherry picked from commit 0d76dcc) Co-authored-by: Connor Denihan <188690869+cdenihan@users.noreply.github.com>
AndPuQing
pushed a commit
to AndPuQing/cpython
that referenced
this pull request
Jul 11, 2025
…thonGH-135152) The documentation incorrectly stated that generator.close() 'raises' a GeneratorExit exception. This was misleading because the method doesn't raise the exception to the caller - it sends the exception internally to the generator and returns None.
Pranjal095
pushed a commit
to Pranjal095/cpython
that referenced
this pull request
Jul 12, 2025
…thonGH-135152) The documentation incorrectly stated that generator.close() 'raises' a GeneratorExit exception. This was misleading because the method doesn't raise the exception to the caller - it sends the exception internally to the generator and returns None.
taegyunkim
pushed a commit
to taegyunkim/cpython
that referenced
this pull request
Aug 4, 2025
…thonGH-135152) The documentation incorrectly stated that generator.close() 'raises' a GeneratorExit exception. This was misleading because the method doesn't raise the exception to the caller - it sends the exception internally to the generator and returns None.
Agent-Hellboy
pushed a commit
to Agent-Hellboy/cpython
that referenced
this pull request
Aug 19, 2025
…thonGH-135152) The documentation incorrectly stated that generator.close() 'raises' a GeneratorExit exception. This was misleading because the method doesn't raise the exception to the caller - it sends the exception internally to the generator and returns None.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes the misleading documentation for generator.close() that incorrectly stated the method 'raises a GeneratorExit' to the caller.
Problem
The current documentation in both Doc/reference/expressions.rst and Doc/howto/functional.rst states that generator.close() 'raises a GeneratorExit' which is misleading because:
Solution
Changed 'raises' to 'sends' in both documentation files for accuracy and consistency:
Doc/reference/expressions.rst (line 628-629):
Doc/howto/functional.rst (line 605):
Testing
The behavior described in the fixed documentation can be verified by the examples in the original issue report, which show that generator.close() always returns None.
Fixes #135110
📚 Documentation preview 📚: https://cpython-previews--135152.org.readthedocs.build/
generator.close()never raisesGeneratorExit#135110