Add an alternative __next__() API to pass arbitrary exceptions in. I
like this better.
This commit is contained in:
parent
999cd28594
commit
a5e037cd4c
51
pep-0340.txt
51
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.
|
||||
|
|
Loading…
Reference in New Issue