]> granicus.if.org Git - python/commitdiff
assertRaises as context manager now allows you to access exception as documented
authorMichael Foord <fuzzyman@voidspace.org.uk>
Sun, 7 Feb 2010 18:44:12 +0000 (18:44 +0000)
committerMichael Foord <fuzzyman@voidspace.org.uk>
Sun, 7 Feb 2010 18:44:12 +0000 (18:44 +0000)
Lib/test/test_unittest.py
Lib/unittest/case.py

index ab043822daf01a4e97151ed606ff699017ee3016..04a7322b236e179353410b49d5ed78785aa12967 100644 (file)
@@ -3059,8 +3059,13 @@ class Test_Assertions(TestCase):
             pass
         else:
             self.fail("assertRaises() didn't let exception pass through")
-        with self.assertRaises(KeyError):
-            raise KeyError
+        with self.assertRaises(KeyError) as cm:
+            try:
+                raise KeyError
+            except Exception, e:
+                raise
+        self.assertIs(cm.exception, e)
+
         with self.assertRaises(KeyError):
             raise KeyError("key")
         try:
index 63408e35ace31a78840821379cd679701d272093..4acfa6539aff70494fa4fcf812237c820b6f9d13 100644 (file)
@@ -91,7 +91,7 @@ class _AssertRaisesContext(object):
         self.expected_regexp = expected_regexp
 
     def __enter__(self):
-        pass
+        return self
 
     def __exit__(self, exc_type, exc_value, tb):
         if exc_type is None: