jsatt / mock

Automatically exported from code.google.com/p/mock
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

reset_mock call to open() mock object causes infinite loop #209

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. mockOpen = patch('builtins.open', mock_open()).start()
2. mockOpen.reset_mock()

What is the expected output? What do you see instead?

You see recursive calls to child.reset_mock() and ret.reset_mock() in an 
infinite loop until the stack is overloaded. It looks like open().__enter__'s 
return_value is the original open() mock.

What version of the product are you using? On what operating system?

Using python 3.3.1's unittest.mock library.

Please provide any additional information below.

Can be fixed with the following change to the reset_mock function:

def reset_mock(self, visited=[]):
    if id(self) in visited:
        return
    visited.append(id(self))

    "Restore the mock object to its initial state."
    self.called = False
    self.call_args = None
    self.call_count = 0
    self.mock_calls = _CallList()
    self.call_args_list = _CallList()
    self.method_calls = _CallList()
    for child in self._mock_children.values():
       if isinstance(child, _SpecState):
           continue
       child.reset_mock()

    ret = self._mock_return_value
    if _is_instance_mock(ret) and ret is not self:
        ret.reset_mock()

Original issue reported on code.google.com by mattz...@gmail.com on 30 Jul 2013 at 12:29

GoogleCodeExporter commented 8 years ago
Correction: the suggested fix should replace the following lines:

child.reset_mock() -> child.reset_mock(visited)
ret.reset_mock() -> ret.reset_mock(visited)

Original comment by mattz...@gmail.com on 30 Jul 2013 at 12:30

GoogleCodeExporter commented 8 years ago
Thanks for catching this.

Original comment by fuzzyman on 1 Aug 2013 at 9:04

GoogleCodeExporter commented 8 years ago
Reported against unittest.mock as: http://bugs.python.org/issue18622

Original comment by fuzzyman on 2 Aug 2013 at 7:56