]> granicus.if.org Git - python/commitdiff
- Issue #18622: unittest.mock.mock_open().reset_mock would recurse infinitely.
authorRobert Collins <rbtcollins@hp.com>
Tue, 14 Jul 2015 23:42:28 +0000 (11:42 +1200)
committerRobert Collins <rbtcollins@hp.com>
Tue, 14 Jul 2015 23:42:28 +0000 (11:42 +1200)
Patch from Nicola Palumbo and Laurent De Buyst.

Lib/unittest/mock.py
Lib/unittest/test/testmock/testmock.py
Misc/ACKS
Misc/NEWS

index 1b1c9f4422a771ba6da5a0210584b8564daffd28..c805726f3fbe42151016aeb522b800579c7a5d7b 100644 (file)
@@ -519,8 +519,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
@@ -531,11 +537,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):
index 23675b9af6bee45f384993a59503a26378060415..4fe6314913661f229a984afde3b78698e47ce8ba 100644 (file)
@@ -237,6 +237,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()
index b377050ec7c7fa3905c28ab5f7c1adcb4df68520..63cf4da54619aad7db5f71c56d64de137beb369c 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1533,3 +1533,5 @@ Tarek Ziadé
 Gennadiy Zlobin
 Doug Zongker
 Peter Ã…strand
+Laurent De Buyst
+Nicola Palumbo
index bdbd74e6bfab7f6fff19ca376cc90f8e25ad749b..874bbd95edeed33144d35297c4a53811395474c1 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -66,6 +66,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 #24608: chunk.Chunk.read() now always returns bytes, not str.
 
 - Issue #18684: Fixed reading out of the buffer in the re module.