[Python-checkins] peps: Add more words to GeneratorReturn alternative.
python-checkins at python.org
Mon Nov 17 21:16:35 CET 2014
user: Guido van Rossum <guido at python.org>
date: Mon Nov 17 12:15:42 2014 -0800
Add more words to GeneratorReturn alternative.
pep-0479.txt | 27 +++++++++++++++++++++------
1 files changed, 21 insertions(+), 6 deletions(-)
diff --git a/pep-0479.txt b/pep-0479.txt
@@ -129,8 +129,18 @@
For certain situations, a simpler and fully backward-compatible
solution may be sufficient: when a generator returns, instead of
raising ``StopIteration``, it raises a specific subclass of
-``StopIteration`` which can then be detected. If it is not that
-subclass, it is an escaping exception rather than a return statement.
+``StopIteration`` (``GeneratorReturn``) which can then be detected.
+If it is not that subclass, it is an escaping exception rather than a
+The inspiration for this alternative proposal was Nick's observation
+_ that if an ``asyncio`` coroutine _ accidentally raises
+``StopIteration``, it currently terminates silently, which may present
+a hard-to-debug mystery to the developer. The main proposal turns
+such accidents in clearly distinguishable ``RuntimeError`` exceptions,
+but if that is rejected, this alternate proposal would enable
+``asyncio`` to distinguish between a ``return`` statement and an
+accidentally-raised ``StopIteration`` exception.
Of the three outcomes listed above:
@@ -144,10 +154,10 @@
exception in its ``__cause__``. If uncaught, this would clearly show
the chaining of exceptions.
-This alternative does *not* affect the discrepancy between generator expressions
-and list comprehensions, but allows generator-aware code (such as the
-contextlib and asyncio modules) to reliably differentiate between the
-second and third outcomes listed above.
+This alternative does *not* affect the discrepancy between generator
+expressions and list comprehensions, but allows generator-aware code
+(such as the ``contextlib`` and ``asyncio`` modules) to reliably
+differentiate between the second and third outcomes listed above.
However, once code exists that depends on this distinction between
``GeneratorReturn`` and ``StopIteration``, a generator that invokes
@@ -186,6 +196,11 @@
..  wrap unbounded generator to restrict its output
+..  Post from Nick Coghlan mentioning asyncio
+..  Coroutines in asyncio
Repository URL: https://hg.python.org/peps
More information about the Python-checkins