[issue18622] reset_mock on mock created by mock_open causes infinite recursion
Changes by Robert Collins robe...@robertcollins.net: -- nosy: +rbcollins versions: +Python 3.6 ___ 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
Robert Collins added the comment: Applying this to 3.4 and up with a test. Laurent, it would be good to sign the CLA - since your change here is minimal and Nicola has, I'm just going ahead. -- ___ 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
Roundup Robot added the comment: New changeset 4c8cb603ab42 by Robert Collins in branch '3.4': - Issue #18622: unittest.mock.mock_open().reset_mock would recurse infinitely. https://hg.python.org/cpython/rev/4c8cb603ab42 -- nosy: +python-dev ___ 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
Changes by Robert Collins robe...@robertcollins.net: -- assignee: michael.foord - rbcollins resolution: - fixed status: open - closed ___ 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
Roundup Robot added the comment: New changeset c0ec61cf5a7d by Robert Collins in branch '3.5': - Issue #18622: unittest.mock.mock_open().reset_mock would recurse infinitely. https://hg.python.org/cpython/rev/c0ec61cf5a7d New changeset a4fe32477df6 by Robert Collins in branch 'default': - Issue #18622: unittest.mock.mock_open().reset_mock would recurse infinitely. https://hg.python.org/cpython/rev/a4fe32477df6 -- ___ 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
Florent Xicluna added the comment: I've been bitten by this issue with a custom psycopg2 mock. cur = mock.Mock() cur.connection.cursor.return_value = cur cur.reset_mock() RuntimeError: maximum recursion depth exceeded the patch looks ok, except the mix of tab and spaces :-) -- nosy: +flox ___ 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
Changes by Michael Foord mich...@voidspace.org.uk: -- nosy: +kushal.das versions: +Python 3.5 -Python 3.3 ___ 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
Changes by Berker Peksag berker.pek...@gmail.com: -- nosy: +berker.peksag ___ 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
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
Nicola Palumbo added the comment: I should have read more carefully, too! Thanks to both. -- ___ 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
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
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
[issue18622] reset_mock on mock created by mock_open causes infinite recursion
Nicola Palumbo added the comment: Hi all, I've fixed the infinite recursion in `reset_mock()`. It has been solved by tracking a set of visited ids as suggested. from unittest import mock a = mock.mock_open() a.reset_mock() a MagicMock name='open' spec='builtin_function_or_method' id='4449688192' -- keywords: +patch nosy: +npalumbo Added file: http://bugs.python.org/file31694/issue18622.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
Michael Foord added the comment: The best way to solve this seems to be to track a set of visited ids (mocks we've reset) and not recurse into mocks we've already done. This is similar to the patch proposed on the google code issue - but not identical as that uses a list and has it as the default argument to reset_mock. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue18622 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue18622] reset_mock on mock created by mock_open causes infinite recursion
New submission from Michael Foord: As reported at: http://code.google.com/p/mock/issues/detail?id=209 from unittest import mock [107971 refs] mock.mock_open function mock_open at 0x10cff9d20 [107974 refs] a = mock.mock_open() [109965 refs] a.reset_mock() ... -- assignee: michael.foord components: Library (Lib) keywords: easy messages: 194119 nosy: michael.foord priority: normal severity: normal stage: needs patch status: open title: reset_mock on mock created by mock_open causes infinite recursion type: behavior versions: Python 3.3, Python 3.4 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue18622 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com