summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Collins <rbtcollins@hp.com>2015-07-15 11:42:28 +1200
committerRobert Collins <rbtcollins@hp.com>2015-07-17 21:09:19 +1200
commit5c57dd2b9ef71366f302f91693a2e5c290aa473b (patch)
treeaf7a53db7520e0c38bf732a064b90be5087f0d8e
parent8747bf419e820091e05bbff71121a79e6d6d485e (diff)
downloadmock-5c57dd2b9ef71366f302f91693a2e5c290aa473b.tar.gz
Issue #18622: unittest.mock.mock_open().reset_mock would recurse infinitely.
Patch from Nicola Palumbo and Laurent De Buyst.
-rw-r--r--NEWS3
-rw-r--r--mock/mock.py12
-rw-r--r--mock/tests/testmock.py3
3 files changed, 15 insertions, 3 deletions
diff --git a/NEWS b/NEWS
index 90fdb0e..90897a2 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,6 @@
+- 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
diff --git a/mock/mock.py b/mock/mock.py
index eee0b7a..cd90dea 100644
--- a/mock/mock.py
+++ b/mock/mock.py
@@ -661,8 +661,14 @@ class NonCallableMock(Base):
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
@@ -673,11 +679,11 @@ class NonCallableMock(Base):
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 --git a/mock/tests/testmock.py b/mock/tests/testmock.py
index 4ddef33..4be8779 100644
--- a/mock/tests/testmock.py
+++ b/mock/tests/testmock.py
@@ -271,6 +271,9 @@ class MockTest(unittest.TestCase):
# 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()