Commits

Benjamin Peterson committed de5fa92

add a test for #7853; the exception must be normalized for with

Comments (0)

Files changed (1)

Lib/test/test_with.py

     def raiseTestException(self):
         raise self.TEST_EXCEPTION
 
-    def assertAfterWithManagerInvariantsWithError(self, mock_manager):
+    def assertAfterWithManagerInvariantsWithError(self, mock_manager,
+                                                  exc_type=None):
         self.assertTrue(mock_manager.enter_called)
         self.assertTrue(mock_manager.exit_called)
-        self.assertEqual(mock_manager.exit_args[0], RuntimeError)
-        self.assertEqual(mock_manager.exit_args[1], self.TEST_EXCEPTION)
+        if exc_type is None:
+            self.assertEqual(mock_manager.exit_args[1], self.TEST_EXCEPTION)
+            exc_type = type(self.TEST_EXCEPTION)
+        self.assertEqual(mock_manager.exit_args[0], exc_type)
+        # Test the __exit__ arguments. Issue #7853
+        self.assertIsInstance(mock_manager.exit_args[1], exc_type)
+        self.assertIsNot(mock_manager.exit_args[2], None)
 
     def assertAfterWithGeneratorInvariantsWithError(self, mock_generator):
         self.assertTrue(mock_generator.yielded)
         self.assertAfterWithManagerInvariantsWithError(cm)
         self.assertAfterWithGeneratorInvariantsWithError(self.resource)
 
+    @unittest.expectedFailure
+    def testExceptionNormalized(self):
+        cm = mock_contextmanager_generator()
+        def shouldThrow():
+            with cm as self.resource:
+                # Note this relies on the fact that 1 // 0 produces an exception
+                # that is not normalized immediately.
+                1 // 0
+        self.assertRaises(ZeroDivisionError, shouldThrow)
+        self.assertAfterWithManagerInvariantsWithError(cm, ZeroDivisionError)
+
     def testNestedSingleStatements(self):
         mock_a = mock_contextmanager_generator()
         mock_b = mock_contextmanager_generator()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.