Skip to content

Commit 8be7c2b

Browse files
ringofasvetlov
andauthored
bpo-14911: Corrected generator.throw() documentation (GH-32207)
Co-authored-by: Andrew Svetlov <[email protected]>
1 parent a00518d commit 8be7c2b

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
@@ -2984,7 +2984,8 @@ generators, coroutines do not directly support iteration.
29842984
:exc:`StopIteration`, or other exception) is the same as when
29852985
iterating over the :meth:`__await__` return value, described above.
29862986

2987-
.. method:: coroutine.throw(type[, value[, traceback]])
2987+
.. method:: coroutine.throw(value)
2988+
coroutine.throw(type[, value[, traceback]])
29882989

29892990
Raises the specified exception in the coroutine. This method delegates
29902991
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
@@ -561,14 +561,27 @@ is already executing raises a :exc:`ValueError` exception.
561561
could receive the value.
562562

563563

564-
.. method:: generator.throw(type[, value[, traceback]])
564+
.. method:: generator.throw(value)
565+
generator.throw(type[, value[, traceback]])
565566

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

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

574587

Objects/genobject.c

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

411411

412412
PyDoc_STRVAR(throw_doc,
413-
"throw(typ[,val[,tb]]) -> raise exception in generator,\n\
414-
return next yielded value or raise StopIteration.");
413+
"throw(value)\n\
414+
throw(type[,value[,tb]])\n\
415+
\n\
416+
Raise exception in generator, return next yielded value or raise\n\
417+
StopIteration.");
415418

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

11591162
PyDoc_STRVAR(coro_throw_doc,
1160-
"throw(typ[,val[,tb]]) -> raise exception in coroutine,\n\
1161-
return next iterated value or raise StopIteration.");
1163+
"throw(value)\n\
1164+
throw(type[,value[,traceback]])\n\
1165+
\n\
1166+
Raise exception in coroutine, return next iterated value or raise\n\
1167+
StopIteration.");
11621168

11631169
PyDoc_STRVAR(coro_close_doc,
11641170
"close() -> raise GeneratorExit inside coroutine.");

0 commit comments

Comments
 (0)