diff options
author | Red4Ru <39802734+Red4Ru@users.noreply.github.com> | 2024-11-13 08:20:38 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-13 08:20:38 (GMT) |
commit | 1e40c5ba47780ddd91868abb3aa064f5ba3015e4 (patch) | |
tree | 1ee9d4458d80c7a853a342f4676ead7de1085994 /Lib/unittest | |
parent | 2e39d77ddeb51505d65fd54ccfcd72615c6b1927 (diff) | |
download | cpython-1e40c5ba47780ddd91868abb3aa064f5ba3015e4.zip cpython-1e40c5ba47780ddd91868abb3aa064f5ba3015e4.tar.gz cpython-1e40c5ba47780ddd91868abb3aa064f5ba3015e4.tar.bz2 |
gh-104745: Limit starting a patcher more than once without stopping it (#126649)
Previously, this would cause an `AttributeError` if the patch stopped more than once after this, and would also disrupt the original patched object.
---------
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Diffstat (limited to 'Lib/unittest')
-rw-r--r-- | Lib/unittest/mock.py | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 21ca061..55cb4b1 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -1360,6 +1360,7 @@ class _patch(object): self.autospec = autospec self.kwargs = kwargs self.additional_patchers = [] + self.is_started = False def copy(self): @@ -1472,6 +1473,9 @@ class _patch(object): def __enter__(self): """Perform the patch.""" + if self.is_started: + raise RuntimeError("Patch is already started") + new, spec, spec_set = self.new, self.spec, self.spec_set autospec, kwargs = self.autospec, self.kwargs new_callable = self.new_callable @@ -1603,6 +1607,7 @@ class _patch(object): self.temp_original = original self.is_local = local self._exit_stack = contextlib.ExitStack() + self.is_started = True try: setattr(self.target, self.attribute, new_attr) if self.attribute_name is not None: @@ -1622,6 +1627,9 @@ class _patch(object): def __exit__(self, *exc_info): """Undo the patch.""" + if not self.is_started: + return + if self.is_local and self.temp_original is not DEFAULT: setattr(self.target, self.attribute, self.temp_original) else: @@ -1638,6 +1646,7 @@ class _patch(object): del self.target exit_stack = self._exit_stack del self._exit_stack + self.is_started = False return exit_stack.__exit__(*exc_info) |