[Python-checkins] cpython: Issue #23661: unittest.mock side_effects can now be exceptions again.

robert.collins python-checkins at python.org
Tue Jul 14 03:59:06 CEST 2015


https://hg.python.org/cpython/rev/db825807ab04
changeset:   96907:db825807ab04
user:        Robert Collins <rbtcollins at hp.com>
date:        Tue Jul 14 13:51:40 2015 +1200
summary:
  Issue #23661: unittest.mock side_effects can now be exceptions again.

This was a regression vs Python 3.4. Patch from Ignacio Rossi

files:
  Lib/unittest/mock.py                   |  3 ++-
  Lib/unittest/test/testmock/testmock.py |  9 +++++++++
  Misc/ACKS                              |  1 +
  Misc/NEWS                              |  3 +++
  4 files changed, 15 insertions(+), 1 deletions(-)


diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -506,7 +506,8 @@
         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
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -173,6 +173,15 @@
         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):
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1582,3 +1582,4 @@
 Gennadiy Zlobin
 Doug Zongker
 Peter Åstrand
+Ignacio Rossi
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -17,6 +17,9 @@
   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?
 ==================================

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list