[Python-checkins] python/nondist/peps pep-0340.txt,1.3,1.4
gvanrossum at users.sourceforge.net
gvanrossum at users.sourceforge.net
Wed Apr 27 23:12:52 CEST 2005
Update of /cvsroot/python/python/nondist/peps
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29289
Modified Files:
pep-0340.txt
Log Message:
Add an alternative __next__() API to pass arbitrary exceptions in. I
like this better.
Index: pep-0340.txt
===================================================================
RCS file: /cvsroot/python/python/nondist/peps/pep-0340.txt,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- pep-0340.txt 27 Apr 2005 17:41:44 -0000 1.3
+++ pep-0340.txt 27 Apr 2005 21:12:49 -0000 1.4
@@ -282,6 +282,57 @@
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.
More information about the Python-checkins
mailing list