[Python-checkins] python/nondist/peps pep-0343.txt,1.4,1.5
gvanrossum@users.sourceforge.net
gvanrossum at users.sourceforge.net
Sat May 14 07:08:44 CEST 2005
Update of /cvsroot/python/python/nondist/peps
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17313
Modified Files:
pep-0343.txt
Log Message:
Slight elaboration for readers who don't recall PEP 310.
Index: pep-0343.txt
===================================================================
RCS file: /cvsroot/python/python/nondist/peps/pep-0343.txt,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- pep-0343.txt 14 May 2005 05:02:28 -0000 1.4
+++ pep-0343.txt 14 May 2005 05:08:23 -0000 1.5
@@ -37,18 +37,34 @@
However, the with-statement of PEP 310 does *not* hide control
flow, in my view: while a finally-suite temporarily suspends the
control flow, in the end, the control flow resumes as if the
- finally-suite wasn't there at all. Consider this:
+ finally-suite wasn't there at all.
+
+ Remember, PEP 310 proposes rougly this syntax (the "VAR =" part is
+ optional):
with VAR = EXPR:
+ BLOCK
+
+ which roughly translates into
+
+ VAR = EXPR
+ VAR.__enter__()
+ try:
+ BLOCK
+ finally:
+ VAR.__exit__()
+
+ Now consider this example:
+
+ with f = opening("/etc/passwd"):
BLOCK1
BLOCK2
Here, just as if the first line was "if True" instead, we know
that if BLOCK1 completes without an exception, BLOCK2 will be
- reached; and if BLOCK1 raises an exception or executes a
- non-local goto (a break, continue or return), BLOCK2 is *not*
- reached. The magic added by the with-statement at the end doesn't
- affect this.
+ reached; and if BLOCK1 raises an exception or executes a non-local
+ goto (a break, continue or return), BLOCK2 is *not* reached. The
+ magic added by the with-statement at the end doesn't affect this.
(You may ask, what if a bug in the __exit__ method causes an
exception? Then all is lost -- but this is no worse than with
More information about the Python-checkins
mailing list