]> granicus.if.org Git - python/commitdiff
Issue #23661: unittest.mock side_effects can now be exceptions again.
authorRobert Collins <rbtcollins@hp.com>
Tue, 14 Jul 2015 01:51:40 +0000 (13:51 +1200)
committerRobert Collins <rbtcollins@hp.com>
Tue, 14 Jul 2015 01:51:40 +0000 (13:51 +1200)
This was a regression vs Python 3.4. Patch from Ignacio Rossi

Lib/unittest/mock.py
Lib/unittest/test/testmock/testmock.py
Misc/ACKS
Misc/NEWS

index c3ab4e82e897c68a16af830107892e8c06f526a7..191a175a418c4d23fc816b1766ea3c4da01aab98 100644 (file)
@@ -506,7 +506,8 @@ class NonCallableMock(Base):
         if delegated is None:
             return self._mock_side_effect
         sf = delegated.side_effect
-        if sf is not None and not callable(sf) and not isinstance(sf, _MockIter):
+        if (sf is not None and not callable(sf)
+                and not isinstance(sf, _MockIter) and not _is_exception(sf)):
             sf = _MockIter(sf)
             delegated.side_effect = sf
         return sf
index 3a104cb2b9d6455a1495b3dca4b4a7dece94113c..f4a723d163abd0da2e02a7e20f5a85a68b084dec 100644 (file)
@@ -173,6 +173,15 @@ class MockTest(unittest.TestCase):
         self.assertEqual([mock(), mock(), mock()], [3, 2, 1],
                           "callable side effect not used correctly")
 
+    def test_autospec_side_effect_exception(self):
+        # Test for issue 23661
+        def f():
+            pass
+
+        mock = create_autospec(f)
+        mock.side_effect = ValueError('Bazinga!')
+        self.assertRaisesRegex(ValueError, 'Bazinga!', mock)
+
     @unittest.skipUnless('java' in sys.platform,
                           'This test only applies to Jython')
     def test_java_exception_side_effect(self):
index e4ba783953fb492ee9348cffcbb7458b51fd9e49..36a747257d35cdc619835d9407ff59ce64795922 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1582,3 +1582,4 @@ Tarek Ziadé
 Gennadiy Zlobin
 Doug Zongker
 Peter Ã…strand
+Ignacio Rossi
index 554d9aa27a7b643a39fd289803fae99f1e1e7685..e737c46c5ee567e5775affd89ae5ed17c6992706 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -17,6 +17,9 @@ Library
   for patterns that starts with capturing groups.  Fast searching optimization
   now can't be disabled at compile time.
 
+- Issue #23661: unittest.mock side_effects can now be exceptions again. This
+  was a regression vs Python 3.4. Patch from Ignacio Rossi
+
 
 What's New in Python 3.5.0 beta 4?
 ==================================