Message365395
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() |
|
Date |
User |
Action |
Args |
2020-03-31 16:07:24 | bmclarnon | set | recipients:
+ bmclarnon |
2020-03-31 16:07:24 | bmclarnon | set | messageid: <1585670844.45.0.631660598637.issue40126@roundup.psfhosted.org> |
2020-03-31 16:07:24 | bmclarnon | link | issue40126 messages |
2020-03-31 16:07:24 | bmclarnon | create | |
|