[issue40126] Incorrect error handling in unittest.mock
Barry McLarnon added the comment: Issue still exists in 3.7 and below, as it was part of a different function before. Current PR doesn't resolve the original issue that was raised. -- versions: +Python 3.5, Python 3.6 ___ Python tracker <https://bugs.python.org/issue40126> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue40126] Incorrect error handling in unittest.mock
Change by Barry McLarnon : -- components: +Library (Lib) -Tests ___ Python tracker <https://bugs.python.org/issue40126> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue40126] Incorrect error handling in unittest.mock
Barry McLarnon added the comment: After further investigation, it seems this was fixed in https://github.com/python/cpython/commit/436c2b0d67da68465e709a96daac7340af3a5238 However, this fix was as part of an unrelated changeset and in a different function in 3.8+, and was never rolled back to 3.7 and below. PR opened to add the missing attribute instantiation to 3.7. -- nosy: -python-dev versions: -Python 3.8, Python 3.9 ___ Python tracker <https://bugs.python.org/issue40126> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue40126] Incorrect error handling in unittest.mock
New submission from Barry McLarnon : The error handling in mock.decorate_callable (3.5-3.7) and mock.decoration_helper (3.8-3.9) is incorrectly implemented. If the error handler is triggered in the loop, the `patching` variable is out of scope and raises an unhandled `UnboundLocalError` instead. This happened as a result of a 3rd-party library that attempts to clear the `patchings` list of a decorated function. The below code shows a recreation of the incorrect error handling: import functools from unittest import mock def is_valid(): return True def mock_is_valid(): return False def decorate(f): @functools.wraps(f) def decorate_wrapper(*args, **kwargs): # This happens in a 3rd-party library f.patchings = [] return f(*args, **kwargs) return decorate_wrapper @decorate @mock.patch('test.is_valid', new=mock_is_valid) def test_patch(): raise Exception() -- components: Tests messages: 365395 nosy: bmclarnon priority: normal severity: normal status: open title: Incorrect error handling in unittest.mock type: behavior versions: Python 3.5, Python 3.6, Python 3.7, Python 3.8, Python 3.9 ___ Python tracker <https://bugs.python.org/issue40126> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com