Skip to content

Commit 625f670

Browse files
bpo-14911: Corrected generator.throw() documentation (GH-32207)
Co-authored-by: Andrew Svetlov <[email protected]> (cherry picked from commit 8be7c2b) Co-authored-by: Dave Goncalves <[email protected]>
1 parent 1f2ec4c commit 625f670

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

Doc/howto/functional.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ generator function.
589589
In addition to :meth:`~generator.send`, there are two other methods on
590590
generators:
591591

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

Doc/reference/datamodel.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2913,7 +2913,8 @@ generators, coroutines do not directly support iteration.
29132913
:exc:`StopIteration`, or other exception) is the same as when
29142914
iterating over the :meth:`__await__` return value, described above.
29152915

2916-
.. method:: coroutine.throw(type[, value[, traceback]])
2916+
.. method:: coroutine.throw(value)
2917+
coroutine.throw(type[, value[, traceback]])
29172918

29182919
Raises the specified exception in the coroutine. This method delegates
29192920
to the :meth:`~generator.throw` method of the iterator that caused

Doc/reference/expressions.rst

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,14 +556,27 @@ is already executing raises a :exc:`ValueError` exception.
556556
could receive the value.
557557

558558

559-
.. method:: generator.throw(type[, value[, traceback]])
559+
.. method:: generator.throw(value)
560+
generator.throw(type[, value[, traceback]])
560561

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

568+
In typical use, this is called with a single exception instance similar to the
569+
way the :keyword:`raise` keyword is used.
570+
571+
For backwards compatability, however, the second signature is
572+
supported, following a convention from older versions of Python.
573+
The *type* argument should be an exception class, and *value*
574+
should be an exception instance. If the *value* is not provided, the
575+
*type* constructor is called to get an instance. If *traceback*
576+
is provided, it is set on the exception, otherwise any existing
577+
:attr:`~BaseException.__traceback__` attribute stored in *value* may
578+
be cleared.
579+
567580
.. index:: exception: GeneratorExit
568581

569582

Objects/genobject.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,11 @@ gen_close(PyGenObject *gen, PyObject *args)
403403

404404

405405
PyDoc_STRVAR(throw_doc,
406-
"throw(typ[,val[,tb]]) -> raise exception in generator,\n\
407-
return next yielded value or raise StopIteration.");
406+
"throw(value)\n\
407+
throw(type[,value[,tb]])\n\
408+
\n\
409+
Raise exception in generator, return next yielded value or raise\n\
410+
StopIteration.");
408411

409412
static PyObject *
410413
_gen_throw(PyGenObject *gen, int close_on_genexit,
@@ -1001,8 +1004,11 @@ PyDoc_STRVAR(coro_send_doc,
10011004
return next iterated value or raise StopIteration.");
10021005

10031006
PyDoc_STRVAR(coro_throw_doc,
1004-
"throw(typ[,val[,tb]]) -> raise exception in coroutine,\n\
1005-
return next iterated value or raise StopIteration.");
1007+
"throw(value)\n\
1008+
throw(type[,value[,traceback]])\n\
1009+
\n\
1010+
Raise exception in coroutine, return next iterated value or raise\n\
1011+
StopIteration.");
10061012

10071013
PyDoc_STRVAR(coro_close_doc,
10081014
"close() -> raise GeneratorExit inside coroutine.");

0 commit comments

Comments
 (0)