This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author bmclarnon
Recipients bmclarnon
Date 2020-03-31.16:07:24
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1585670844.45.0.631660598637.issue40126@roundup.psfhosted.org>
In-reply-to
Content
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()
History
Date User Action Args
2020-03-31 16:07:24bmclarnonsetrecipients: + bmclarnon
2020-03-31 16:07:24bmclarnonsetmessageid: <1585670844.45.0.631660598637.issue40126@roundup.psfhosted.org>
2020-03-31 16:07:24bmclarnonlinkissue40126 messages
2020-03-31 16:07:24bmclarnoncreate