[Python-checkins] r45683 - in python/trunk: Lib/contextlib.py Lib/test/test_contextlib.py Misc/NEWS

nick.coghlan python-checkins at python.org
Mon Apr 24 06:37:16 CEST 2006


Author: nick.coghlan
Date: Mon Apr 24 06:37:15 2006
New Revision: 45683

Modified:
   python/trunk/Lib/contextlib.py
   python/trunk/Lib/test/test_contextlib.py
   python/trunk/Misc/NEWS
Log:
Fix contextlib.nested to cope with exit methods raising and handling exceptions

Modified: python/trunk/Lib/contextlib.py
==============================================================================
--- python/trunk/Lib/contextlib.py	(original)
+++ python/trunk/Lib/contextlib.py	Mon Apr 24 06:37:15 2006
@@ -127,7 +127,10 @@
             except:
                 exc = sys.exc_info()
         if exc != (None, None, None):
-            raise
+            # Don't rely on sys.exc_info() still containing
+            # the right information. Another exception may
+            # have been raised and caught by an exit method
+            raise exc[0], exc[1], exc[2]
 
 
 @contextmanager

Modified: python/trunk/Lib/test/test_contextlib.py
==============================================================================
--- python/trunk/Lib/test/test_contextlib.py	(original)
+++ python/trunk/Lib/test/test_contextlib.py	Mon Apr 24 06:37:15 2006
@@ -146,6 +146,29 @@
         else:
             self.fail("Didn't raise ZeroDivisionError")
 
+    def test_nested_right_exception(self):
+        state = []
+        @contextmanager
+        def a():
+            yield 1
+        class b(object):
+            def __enter__(self):
+                return 2
+            def __exit__(self, *exc_info):
+                try:
+                    raise Exception()
+                except:
+                    pass
+        try:
+            with nested(a(), b()) as (x, y):
+                1/0
+        except ZeroDivisionError:
+            self.assertEqual((x, y), (1, 2))
+        except Exception:
+            self.fail("Reraised wrong exception")
+        else:
+            self.fail("Didn't raise ZeroDivisionError")
+
     def test_nested_b_swallows(self):
         @contextmanager
         def a():

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Mon Apr 24 06:37:15 2006
@@ -86,6 +86,9 @@
 Library
 -------
 
+- Fixed contextlib.nested to cope with exceptions being raised and
+  caught inside exit handlers.
+
 - Updated optparse module to Optik 1.5.1 (allow numeric constants in
   hex, octal, or binary; add ``append_const`` action; keep going if
   gettext cannot be imported; added ``OptionParser.destroy()`` method;
@@ -158,6 +161,9 @@
 Tests
 -----
 
+- test_contextlib now checks contextlib.nested can cope with exceptions
+  being raised and caught inside exit handlers.
+
 - test_cmd_line now checks operation of the -m and -c command switches
 
 - The test_contextlib test in 2.5a1 wasn't actually run unless you ran


More information about the Python-checkins mailing list