Add an alternative __next__() API to pass arbitrary exceptions in. I

like this better.
This commit is contained in:
Guido van Rossum 2005-04-27 21:12:49 +00:00
parent 999cd28594
commit a5e037cd4c
1 changed files with 51 additions and 0 deletions

View File

@ -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.