From a5e037cd4c3511c2bfdd7ee6fce069e20e2c0b0a Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Wed, 27 Apr 2005 21:12:49 +0000 Subject: [PATCH] Add an alternative __next__() API to pass arbitrary exceptions in. I like this better. --- pep-0340.txt | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/pep-0340.txt b/pep-0340.txt index 964b6f506..16d03bacd 100644 --- a/pep-0340.txt +++ b/pep-0340.txt @@ -282,6 +282,57 @@ Specification: Generator Exception Handling cases work differently; in Python, you cannot save the block for later use, and you cannot test whether there is a block or not. +Specification: Alternative __next__() and Generator Exception Handling + + The above specification doesn't let the generator handle general + exceptions. If we want that, we could modify the __next__() API + to take either a value or an exception argument. When it is an + Exception instance, it is raised at the point of the resuming + yield; otherwise it is returned from the yield-expression (or + ignored by a yield-statement). Wrapping a regular value in a + ContinueIteration is then no longer necessary. The translation of + a block-statement would become: + + itr = EXPR1 + arg = val = None + ret = False + while True: + try: + VAR1 = next(itr, arg) + except StopIteration: + if ret: + return val + break + try: + arg = val = None + ret = False + BLOCK1 + except Exception, arg: + pass + + The translation of "continue EXPR2" would become: + + arg = EXPR2 + continue + + The translation of "break" inside a block-statement would become: + + arg = StopIteration() + continue + + The translation of "return EXPR3" inside a block-statement would + become: + + val = EXPR3 + arg = StopIteration() + ret = True + continue + + The translation of a for-loop would be the same as indicated + earlier (inside a for-loop only the translation of "continue + EXPR2" is changed; break and return translate to themselves in + that case). + Alternatives Considered TBD.