Skip to content

gh-135110: Fix misleading generator.close() documentation #135152

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cdenihan
Copy link

@cdenihan cdenihan commented Jun 4, 2025

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:

  • The method sends the GeneratorExit exception to the generator internally
  • The method returns None to the caller
  • The method does not raise the exception to the caller

Solution

Changed 'raises' to 'sends' in both documentation files for accuracy and consistency:

  1. Doc/reference/expressions.rst (line 628-629):

    • Before: 'Raises a GeneratorExit at the point where the generator function was paused'
    • After: 'Sends a GeneratorExit exception to the generator at the point where the generator function was paused'
  2. Doc/howto/functional.rst (line 605):

    • Before: 'raises a GeneratorExit exception inside the'
    • After: 'sends a GeneratorExit exception inside the'

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/

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 python#135110
@python-cla-bot
Copy link

python-cla-bot bot commented Jun 4, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app bedevere-app bot added awaiting review docs Documentation in the Doc dir skip news labels Jun 4, 2025
@github-project-automation github-project-automation bot moved this to Todo in Docs PRs Jun 4, 2025
@cdenihan cdenihan changed the title Fix misleading generator.close() documentation gh-135110: Fix misleading generator.close() documentation Jun 4, 2025
Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review docs Documentation in the Doc dir skip news
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

generator.close() never raises GeneratorExit
2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy