[issue18622] reset_mock on mock created by mock_open causes infinite recursion

2014-01-14 Thread Laurent De Buyst

Laurent De Buyst added the comment:

Sorry Michael, I should have read your second comment more closely since you 
already pointed out that using a list as default argument is bad.

It is, however, easily fixed by changing to this:

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

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18622
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18622] reset_mock on mock created by mock_open causes infinite recursion

2014-01-14 Thread Laurent De Buyst

Laurent De Buyst added the comment:

And here's an actual patch with the corrected code

--
Added file: http://bugs.python.org/file33463/issue18622_2.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18622
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18622] reset_mock on mock created by mock_open causes infinite recursion

2014-01-13 Thread Laurent De Buyst

Laurent De Buyst added the comment:

The proposed patch does solve the infinite recursion bug, but a different 
problem appears when resetting the same mock multiple times: it only works the 
first time.

Using the patch as it stands:

 from unittest.mock import mock_open
 mo = mock_open()
 a = mo()
 mo.call_count
1
 mo.reset_mock()
 mo.call_count
0
 b = mo()
 mo.call_count
1
 mo.reset_mock()
 mo.call_count
1

And here from a version with an added print(visited) statement:

 from unittest.mock import mock_open
 mo = mock_open()
 a = mo()
 mo.call_count
1
 mo.reset_mock()
[]
[139803191795152]
[139803191795152, 139803181189008]
[139803191795152, 139803181189008, 139803213598416]
[139803191795152, 139803181189008, 139803213598416, 139803213652048]
[139803191795152, 139803181189008, 139803213598416, 139803213652048]
 mo.call_count
0
 b = mo()
 mo.call_count
1
 mo.reset_mock()
[139803191795152, 139803181189008, 139803213598416, 139803213652048, 
139803213598288]
 mo.call_count
1
 mo.reset_mock(visited=[])
[]
[139803191795152]
[139803191795152, 139803181189008]
[139803191795152, 139803181189008, 139803213598416]
[139803191795152, 139803181189008, 139803213598416, 139803213652048]
[139803191795152, 139803181189008, 139803213598416, 139803213652048]
 mo.call_count
0

As you can see, for some reason I don't quite grasp, the 'visited' parameter 
persists across calls to reset_mock(), meaning that the very first call does 
indeed reset it but subsequent calls do not.

As the last two calls show, one can force a reset by explicitly providing an 
empty list, but this is starting to become a change in API and not just a 
bugfix...

--
nosy: +Laurent.De.Buyst

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18622
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com