diff Lib/contextlib.py @ 36926:fc10ff322df1 legacy-trunk

Um, I thought I'd already checked this in. Anyway, this is the changes to the with-statement so that __exit__ must return a true value in order for a pending exception to be ignored. The PEP (343) is already updated.
author Guido van Rossum <guido@python.org>
date Fri, 10 Mar 2006 02:28:35 +0000
parents 623763457a4e
children b39c3d7a5ad4 f731859e9b3b
line wrap: on
line diff
--- a/Lib/contextlib.py	Fri Mar 10 02:04:28 2006 +0000
+++ b/Lib/contextlib.py	Fri Mar 10 02:28:35 2006 +0000
@@ -30,8 +30,9 @@
         else:
             try:
                 self.gen.throw(type, value, traceback)
+                return True
             except StopIteration:
-                pass
+                return True
 
 
 def contextmanager(func):
@@ -91,6 +92,7 @@
     """
     exits = []
     vars = []
+    exc = (None, None, None)
     try:
         try:
             for context in contexts:
@@ -102,17 +104,14 @@
             yield vars
         except:
             exc = sys.exc_info()
-        else:
-            exc = (None, None, None)
     finally:
         while exits:
             exit = exits.pop()
             try:
-                exit(*exc)
+                if exit(*exc):
+                    exc = (None, None, None)
             except:
                 exc = sys.exc_info()
-            else:
-                exc = (None, None, None)
         if exc != (None, None, None):
             raise