derwiki-adroll / mock

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

reset_mock recurses infinitely on fluent mocks #222

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
class A(object):
    def __init__(self):
        self.value = 0

    def random(self):
        self.value = 4
        return self

m = mock.Mock(spec=A)
m.random.return_value = m
m.reset_mock() #<----- recurses infinitely

What is the expected output? What do you see instead?
A stack trace consisting of
  File "/usr/local/lib/python2.7/dist-packages/mock.py", line 628, in reset_mock
    ret.reset_mock()
  File "/usr/local/lib/python2.7/dist-packages/mock.py", line 624, in reset_mock
    child.reset_mock()
repeatedly until the stack depth is exceeded.

What version of the product are you using? On what operating system?
mock 1.0.1, Python 2.7.4, Ubuntu 13.04

Available Workarounds:
Set the return_value of the fluent methods to None, call reset_mock(), set the 
return_value of the fluent methods back.

Please provide any additional information below.
In the simplest case, including this one, the recursion code in reset_mock can 
just check if the child's child is the parent. A more robust fix would be to 
either just keep a set of previously visited objects or use something more 
complex like Tarjan's strongly connected components algorithm.

Original issue reported on code.google.com by qdchzruc...@gmail.com on 25 Feb 2014 at 7:15