[Python-checkins] r43307 - peps/trunk/pep-0343.txt

phillip.eby python-checkins at python.org
Sat Mar 25 05:31:37 CET 2006


Author: phillip.eby
Date: Sat Mar 25 05:31:37 2006
New Revision: 43307

Modified:
   peps/trunk/pep-0343.txt
Log:
More info on the __exit__ return issue, per Guido's request


Modified: peps/trunk/pep-0343.txt
==============================================================================
--- peps/trunk/pep-0343.txt	(original)
+++ peps/trunk/pep-0343.txt	Sat Mar 25 05:31:37 2006
@@ -312,9 +312,10 @@
     non-local goto should be considered unexceptional for the purposes
     of a database transaction roll-back decision.
 
-    To facilitate chaining of contexts, __exit__() methods should
-    *not* reraise the error that is passed in, because it is always the
-    responsibility of the *caller* to do any reraising.
+    To facilitate chaining of contexts in Python code that directly
+    manipulates context objects, __exit__() methods should *not*
+    re-raise the error that is passed in to them, because it is always
+    the responsibility of the *caller* to do any reraising in that case.
 
     That way, if the caller needs to tell whether the __exit__() 
     invocation *failed* (as opposed to successfully cleaning up before
@@ -379,8 +380,16 @@
                    except StopIteration:
                        return True
                    except:
+                       # only re-raise if it's *not* the exception that was
+                       # passed to throw(), because __exit__() must not raise
+                       # an exception unless __exit__() itself failed.  But
+                       # throw() has to raise the exception to signal
+                       # propagation, so this fixes the impedance mismatch 
+                       # between the throw() protocol and the __exit__()
+                       # protocol.
+                       #
                        if sys.exc_info()[1] is not value:
-                           raise    # only reraise if it's a new exception
+                           raise
 
         def contextmanager(func):
            def helper(*args, **kwds):


More information about the Python-checkins mailing list