Fix contextlib.nested to cope with exit methods raising and handling exceptions
diff --git a/Lib/contextlib.py b/Lib/contextlib.py
index aa5335d..157b4cc 100644
--- a/Lib/contextlib.py
+++ b/Lib/contextlib.py
@@ -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
diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py
index 97470c7..c23e428 100644
--- a/Lib/test/test_contextlib.py
+++ b/Lib/test/test_contextlib.py
@@ -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():