diff options
author | Nicolas Tessore <n.tessore@ucl.ac.uk> | 2023-05-23 20:51:56 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-23 20:51:56 (GMT) |
commit | d56c933992c86986bd58eb3880aed0ed1b0cadc9 (patch) | |
tree | d5c4630de7661b61442891cc296584e24ae6dbab /Doc | |
parent | 50fce89d123b25e53fa8a0303a169e8887154a0e (diff) | |
download | cpython-d56c933992c86986bd58eb3880aed0ed1b0cadc9.zip cpython-d56c933992c86986bd58eb3880aed0ed1b0cadc9.tar.gz cpython-d56c933992c86986bd58eb3880aed0ed1b0cadc9.tar.bz2 |
gh-104770: Let generator.close() return value (#104771)
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/reference/expressions.rst | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index b97a08f..0c700f9 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -595,12 +595,19 @@ 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 then exits gracefully, is already closed, - or raises :exc:`GeneratorExit` (by not catching the exception), close - returns to its caller. If the generator yields a value, a - :exc:`RuntimeError` is raised. If the generator raises any other exception, - it is propagated to the caller. :meth:`close` does nothing if the generator - has already exited due to an exception or normal exit. + 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 + value, a :exc:`RuntimeError` is raised. If the generator raises any other + exception, it is propagated to the caller. If the generator has already + exited due to an exception or normal exit, :meth:`close` returns + :const:`None` and has no other effect. + + .. versionchanged:: 3.13 + + If a generator returns a value upon being closed, the value is returned + by :meth:`close`. .. index:: single: yield; examples |