From 1c2c1c0a7ab4ba701b929e7ef733bb4619336169 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Wed, 26 Nov 2014 09:34:12 -0800 Subject: [PATCH] Chris A: Mention Mark Shannon's counterproposal of changing next(). One small copy-edit. --- pep-0479.txt | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/pep-0479.txt b/pep-0479.txt index 442b7f88a..93a2e08a6 100644 --- a/pep-0479.txt +++ b/pep-0479.txt @@ -143,8 +143,8 @@ under older Python versions as under the new semantics. This is done by enclosing those places in the generator body where a ``StopIteration`` is expected (e.g. bare ``next()`` calls or in some cases helper functions that are expected to raise ``StopIteration``) -in a ``try/except`` construct that returns when ``StopIteration`` -returns. The ``try/except`` construct should appear directly in the +in a ``try/except`` construct that returns when ``StopIteration`` is +raised. The ``try/except`` construct should appear directly in the generator function; doing this in a helper function that is not itself a generator does not work. If ``raise StopIteration`` occurs directly in a generator, simply replace it with ``return``. @@ -364,6 +364,21 @@ another generator and relies on the latter's ``StopIteration`` to bubble out would still be potentially wrong, depending on the use made of the distinction between the two exception types. +Converting the exception inside next() +-------------------------------------- + +Mark Shannon suggested [11]_ that the problem could be solved in +``next()`` rather than at the boundary of generator functions. By +having ``next()`` catch ``StopIteration`` and raise instead +``ValueError``, all unexpected ``StopIteration`` bubbling would be +prevented; however, the backward-incompatibility concerns are far +more serious than for the current proposal, as every ``next()`` call +now needs to be rewritten to guard against ``ValueError`` instead of +``StopIteration`` - not to mention that there is no way to write one +block of code which reliably works on multiple versions of Python. +(Using a dedicated exception type, perhaps subclassing ``ValueError``, +would help this; however, all code would still need to be rewritten.) + Criticism ========= @@ -426,6 +441,9 @@ References .. [10] Tracker issue with Proof-of-Concept patch (http://bugs.python.org/issue22906) +.. [11] Post from Mark Shannon with alternate proposal + (https://mail.python.org/pipermail/python-dev/2014-November/137129.html) + Copyright =========