From f58f88c4c7df1827a31457da8dbcacd127613ade Mon Sep 17 00:00:00 2001 From: Robert Collins Date: Tue, 14 Jul 2015 13:51:40 +1200 Subject: [PATCH] Issue #23661: unittest.mock side_effects can now be exceptions again. This was a regression vs Python 3.4. Patch from Ignacio Rossi --- Lib/unittest/mock.py | 3 ++- Lib/unittest/test/testmock/testmock.py | 9 +++++++++ Misc/ACKS | 1 + Misc/NEWS | 3 +++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index c3ab4e82e8..191a175a41 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -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 diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index 3a104cb2b9..f4a723d163 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -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): diff --git a/Misc/ACKS b/Misc/ACKS index 8a007ea9dc..9841059bec 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1581,3 +1581,4 @@ Tarek Ziadé Gennadiy Zlobin Doug Zongker Peter Åstrand +Ignacio Rossi diff --git a/Misc/NEWS b/Misc/NEWS index aaa7f910ad..2a5b4476a9 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -17,6 +17,9 @@ Core and Builtins Library ------- +- Issue #23661: unittest.mock side_effects can now be exceptions again. This + was a regression vs Python 3.4. Patch from Ignacio Rossi + - Issue #24608: chunk.Chunk.read() now always returns bytes, not str. - Issue #18684: Fixed reading out of the buffer in the re module. -- 2.40.0