changeset: 96911:c0ec61cf5a7d branch: 3.5 parent: 96908:7021d46c490e parent: 96910:4c8cb603ab42 user: Robert Collins date: Wed Jul 15 11:49:43 2015 +1200 files: Lib/unittest/mock.py Lib/unittest/test/testmock/testmock.py Misc/ACKS Misc/NEWS description: - Issue #18622: unittest.mock.mock_open().reset_mock would recurse infinitely. Patch from Nicola Palumbo and Laurent De Buyst. diff -r 7021d46c490e -r c0ec61cf5a7d Lib/unittest/mock.py --- a/Lib/unittest/mock.py Tue Jul 14 13:51:40 2015 +1200 +++ b/Lib/unittest/mock.py Wed Jul 15 11:49:43 2015 +1200 @@ -523,8 +523,14 @@ side_effect = property(__get_side_effect, __set_side_effect) - def reset_mock(self): + def reset_mock(self, visited=None): "Restore the mock object to its initial state." + if visited is None: + visited = [] + if id(self) in visited: + return + visited.append(id(self)) + self.called = False self.call_args = None self.call_count = 0 @@ -535,11 +541,11 @@ for child in self._mock_children.values(): if isinstance(child, _SpecState): continue - child.reset_mock() + child.reset_mock(visited) ret = self._mock_return_value if _is_instance_mock(ret) and ret is not self: - ret.reset_mock() + ret.reset_mock(visited) def configure_mock(self, **kwargs): diff -r 7021d46c490e -r c0ec61cf5a7d Lib/unittest/test/testmock/testmock.py --- a/Lib/unittest/test/testmock/testmock.py Tue Jul 14 13:51:40 2015 +1200 +++ b/Lib/unittest/test/testmock/testmock.py Wed Jul 15 11:49:43 2015 +1200 @@ -246,6 +246,9 @@ # used to cause recursion mock.reset_mock() + def test_reset_mock_on_mock_open_issue_18622(self): + a = mock.mock_open() + a.reset_mock() def test_call(self): mock = Mock() diff -r 7021d46c490e -r c0ec61cf5a7d Misc/ACKS --- a/Misc/ACKS Tue Jul 14 13:51:40 2015 +1200 +++ b/Misc/ACKS Wed Jul 15 11:49:43 2015 +1200 @@ -1582,3 +1582,5 @@ Doug Zongker Peter Åstrand Ignacio Rossi +Laurent De Buyst +Nicola Palumbo diff -r 7021d46c490e -r c0ec61cf5a7d Misc/NEWS --- a/Misc/NEWS Tue Jul 14 13:51:40 2015 +1200 +++ b/Misc/NEWS Wed Jul 15 11:49:43 2015 +1200 @@ -17,6 +17,9 @@ Library ------- +- Issue #18622: unittest.mock.mock_open().reset_mock would recurse infinitely. + Patch from Nicola Palumbo and Laurent De Buyst. + - Issue #23661: unittest.mock side_effects can now be exceptions again. This was a regression vs Python 3.4. Patch from Ignacio Rossi