New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect error handling in unittest.mock #84307
Comments
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 This happened as a result of a 3rd-party library that attempts to clear the 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() |
After further investigation, it seems this was fixed in 436c2b0 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. |
I think that the current code is not correct. __exit__ should not be called if __enter__ is failed. If some __enter__ implementation calls other __enter__s it should manually call corresponding __exit__s. |
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. |
3.5 and 3.6 are in security fixes only mode, and this issue is not a security issue. After merging PR 19484 I cannot reproduce the original issue anymore. I left this issue open for the case if I find a way to write tests for the merged changes. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: