[Python-checkins] cpython (3.4): - Issue #18622: unittest.mock.mock_open().reset_mock would recurse infinitely.

robert.collins python-checkins at python.org
Wed Jul 15 01:42:38 CEST 2015


https://hg.python.org/cpython/rev/4c8cb603ab42
changeset:   96910:4c8cb603ab42
branch:      3.4
parent:      96901:1cae77f873af
user:        Robert Collins <rbtcollins at hp.com>
date:        Wed Jul 15 11:42:28 2015 +1200
summary:
  - Issue #18622: unittest.mock.mock_open().reset_mock would recurse infinitely.

Patch from Nicola Palumbo and Laurent De Buyst.

files:
  Lib/unittest/mock.py                   |  12 +++++++++---
  Lib/unittest/test/testmock/testmock.py |   3 +++
  Misc/ACKS                              |   2 ++
  Misc/NEWS                              |   3 +++
  4 files changed, 17 insertions(+), 3 deletions(-)


diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -519,8 +519,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
@@ -531,11 +537,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 --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -237,6 +237,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 --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1533,3 +1533,5 @@
 Gennadiy Zlobin
 Doug Zongker
 Peter Åstrand
+Laurent De Buyst
+Nicola Palumbo
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -66,6 +66,9 @@
 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.

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list