Skip to content

bpo-14911: Corrected generator.throw() documentation #32207

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

Merged
merged 3 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Doc/howto/functional.rst
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ generator function.
In addition to :meth:`~generator.send`, there are two other methods on
generators:

* :meth:`throw(type, value=None, traceback=None) <generator.throw>` is used to
* :meth:`throw(value) <generator.throw>` is used to
raise an exception inside the generator; the exception is raised by the
``yield`` expression where the generator's execution is paused.

Expand Down
3 changes: 2 additions & 1 deletion Doc/reference/datamodel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2984,7 +2984,8 @@ generators, coroutines do not directly support iteration.
:exc:`StopIteration`, or other exception) is the same as when
iterating over the :meth:`__await__` return value, described above.

.. method:: coroutine.throw(type[, value[, traceback]])
.. method:: coroutine.throw(value)
coroutine.throw(type[, value[, traceback]])

Raises the specified exception in the coroutine. This method delegates
to the :meth:`~generator.throw` method of the iterator that caused
Expand Down
17 changes: 15 additions & 2 deletions Doc/reference/expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -561,14 +561,27 @@ is already executing raises a :exc:`ValueError` exception.
could receive the value.


.. method:: generator.throw(type[, value[, traceback]])
.. method:: generator.throw(value)
generator.throw(type[, value[, traceback]])

Raises an exception of type ``type`` at the point where the generator was paused,
Raises an exception at the point where the generator was paused,
and returns the next value yielded by the generator function. If the generator
exits without yielding another value, a :exc:`StopIteration` exception is
raised. If the generator function does not catch the passed-in exception, or
raises a different exception, then that exception propagates to the caller.

In typical use, this is called with a single exception instance similar to the
way the :keyword:`raise` keyword is used.

For backwards compatability, however, the second signature is
supported, following a convention from older versions of Python.
The *type* argument should be an exception class, and *value*
should be an exception instance. If the *value* is not provided, the
*type* constructor is called to get an instance. If *traceback*
is provided, it is set on the exception, otherwise any existing
:attr:`~BaseException.__traceback__` attribute stored in *value* may
be cleared.

.. index:: exception: GeneratorExit


Expand Down
14 changes: 10 additions & 4 deletions Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,11 @@ gen_close(PyGenObject *gen, PyObject *args)


PyDoc_STRVAR(throw_doc,
"throw(typ[,val[,tb]]) -> raise exception in generator,\n\
return next yielded value or raise StopIteration.");
"throw(value)\n\
throw(type[,value[,tb]])\n\
\n\
Raise exception in generator, return next yielded value or raise\n\
StopIteration.");

static PyObject *
_gen_throw(PyGenObject *gen, int close_on_genexit,
Expand Down Expand Up @@ -1157,8 +1160,11 @@ PyDoc_STRVAR(coro_send_doc,
return next iterated value or raise StopIteration.");

PyDoc_STRVAR(coro_throw_doc,
"throw(typ[,val[,tb]]) -> raise exception in coroutine,\n\
return next iterated value or raise StopIteration.");
"throw(value)\n\
throw(type[,value[,traceback]])\n\
\n\
Raise exception in coroutine, return next iterated value or raise\n\
StopIteration.");

PyDoc_STRVAR(coro_close_doc,
"close() -> raise GeneratorExit inside coroutine.");
Expand Down