summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/unittest/mock.py12
-rw-r--r--Lib/unittest/test/testmock/testmock.py3
-rw-r--r--Misc/ACKS2
-rw-r--r--Misc/NEWS3
4 files changed, 17 insertions, 3 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 191a175..944029b 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -523,8 +523,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
@@ -535,11 +541,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/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
index f4a723d..f4fb228 100644
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -246,6 +246,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()
diff --git a/Misc/ACKS b/Misc/ACKS
index 9841059..2dc6411 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1582,3 +1582,5 @@ Gennadiy Zlobin
Doug Zongker
Peter Åstrand
Ignacio Rossi
+Laurent De Buyst
+Nicola Palumbo
diff --git a/Misc/NEWS b/Misc/NEWS
index 2a5b447..77c898c 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -17,6 +17,9 @@ Core and Builtins
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