Skip to content

generator.close() never raises GeneratorExit #135110

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
hyperkai opened this issue Jun 3, 2025 · 2 comments · May be fixed by #135152
Open

generator.close() never raises GeneratorExit #135110

hyperkai opened this issue Jun 3, 2025 · 2 comments · May be fixed by #135152
Labels
docs Documentation in the Doc dir easy pending The issue will be closed if no feedback is provided

Comments

@hyperkai
Copy link

hyperkai commented Jun 3, 2025

Bug report

Bug description:

The doc of generator.close() says as shown below:

Raises a GeneratorExit at the point where the generator function was paused.

But generator.close() never raises GeneratorExit as shown below:

def func():
    yield 'Hello'
    yield 'World'

v = func()

print(v.close()) # None
print(v.close()) # None
def func():
    yield 'Hello'
    yield 'World'

v = func()

print(next(v)) # Hello

print(v.close()) # None
print(v.close()) # None
def func():
    yield 'Hello'
    yield 'World'

v = func()

print(next(v)) # Hello
print(next(v)) # World

print(v.close()) # None
print(v.close()) # None
def func():
    yield 'Hello'
    yield 'World'

v = func()

print(v.close()) # None

print(next(v)) # StopIteration
def func():
    yield 'Hello'
    yield 'World'

v = func()

print(next(v)) # Hello

print(v.close()) # None

print(next(v)) # StopIteration

CPython versions tested on:

3.13

Operating systems tested on:

Windows

Linked PRs

@hyperkai hyperkai added the type-bug An unexpected behavior, bug, or error label Jun 3, 2025
@ZeroIntensity ZeroIntensity added interpreter-core (Objects, Python, Grammar, and Parser dirs) docs Documentation in the Doc dir and removed type-bug An unexpected behavior, bug, or error labels Jun 4, 2025
@ZeroIntensity
Copy link
Member

generator.close() sends the GeneratorExit to the yield inside the generator, not to caller of close. For example:

def a():
    try:
        yield "123"
    except GeneratorExit:
        print("gotcha!")

gen = a()
next(gen)
gen.close()  # gotcha!

Do you have any suggestions to make the documentation clearer?

@ryan-duve
Copy link
Contributor

I think it's also helpful to emphasize this sentence later in the paragraph:

If the generator function is already closed, or raises GeneratorExit (by not catching the exception), close() returns None.

@ZeroIntensity ZeroIntensity added easy and removed interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Jun 4, 2025
@hugovk hugovk added the pending The issue will be closed if no feedback is provided label Jun 4, 2025
cdenihan added a commit to cdenihan/cpython that referenced this issue Jun 4, 2025
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir easy pending The issue will be closed if no feedback is provided
Projects
Status: Todo
Development

Successfully merging a pull request may close this issue.

4 participants