]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-135110: Fix misleading `generator.close()` documentation (GH-135152)
authorConnor Denihan <188690869+cdenihan@users.noreply.github.com>
Thu, 26 Jun 2025 13:27:25 +0000 (09:27 -0400)
committerGitHub <noreply@github.com>
Thu, 26 Jun 2025 13:27:25 +0000 (09:27 -0400)
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.

Doc/howto/functional.rst
Doc/reference/expressions.rst

index b4f3463afee81256eccbba44aca95166530a27e3..78e56e0c64fb1013b2799c01d17dd1663c54b8c2 100644 (file)
@@ -602,7 +602,7 @@ generators:
   raise an exception inside the generator; the exception is raised by the
   ``yield`` expression where the generator's execution is paused.
 
-* :meth:`~generator.close` raises a :exc:`GeneratorExit` exception inside the
+* :meth:`~generator.close` sends a :exc:`GeneratorExit` exception to the
   generator to terminate the iteration.  On receiving this exception, the
   generator's code must either raise :exc:`GeneratorExit` or
   :exc:`StopIteration`; catching the exception and doing anything else is
index 17f39aaf5f57cdb4f7af41caea858428e2394674..24544a055c3ed2f79177ff3ff19c37727ea038f1 100644 (file)
@@ -625,8 +625,10 @@ is already executing raises a :exc:`ValueError` exception.
 
 .. method:: generator.close()
 
-   Raises a :exc:`GeneratorExit` at the point where the generator function was
-   paused.  If the generator function catches the exception and returns a
+   Raises a :exc:`GeneratorExit` exception at the point where the generator
+   function was paused (equivalent to calling ``throw(GeneratorExit)``).
+   The exception is raised by the yield expression where the generator was paused.
+   If the generator function catches the exception and returns a
    value, this value is returned from :meth:`close`.  If the generator function
    is already closed, or raises :exc:`GeneratorExit` (by not catching the
    exception), :meth:`close` returns :const:`None`.  If the generator yields a