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>
This commit is contained in:
Red4Ru 2024-11-13 11:20:38 +03:00 committed by GitHub
parent 2e39d77dde
commit 1e40c5ba47
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 62 additions and 2 deletions

View File

@ -745,6 +745,54 @@ class PatchTest(unittest.TestCase):
self.assertIsNone(patcher.stop()) self.assertIsNone(patcher.stop())
def test_exit_idempotent(self):
patcher = patch(foo_name, 'bar', 3)
with patcher:
patcher.stop()
def test_second_start_failure(self):
patcher = patch(foo_name, 'bar', 3)
patcher.start()
try:
self.assertRaises(RuntimeError, patcher.start)
finally:
patcher.stop()
def test_second_enter_failure(self):
patcher = patch(foo_name, 'bar', 3)
with patcher:
self.assertRaises(RuntimeError, patcher.start)
def test_second_start_after_stop(self):
patcher = patch(foo_name, 'bar', 3)
patcher.start()
patcher.stop()
patcher.start()
patcher.stop()
def test_property_setters(self):
mock_object = Mock()
mock_bar = mock_object.bar
patcher = patch.object(mock_object, 'bar', 'x')
with patcher:
self.assertEqual(patcher.is_local, False)
self.assertIs(patcher.target, mock_object)
self.assertEqual(patcher.temp_original, mock_bar)
patcher.is_local = True
patcher.target = mock_bar
patcher.temp_original = mock_object
self.assertEqual(patcher.is_local, True)
self.assertIs(patcher.target, mock_bar)
self.assertEqual(patcher.temp_original, mock_object)
# if changes are left intact, they may lead to disruption as shown below (it might be what someone needs though)
self.assertEqual(mock_bar.bar, mock_object)
self.assertEqual(mock_object.bar, 'x')
def test_patchobject_start_stop(self): def test_patchobject_start_stop(self):
original = something original = something
patcher = patch.object(PTModule, 'something', 'foo') patcher = patch.object(PTModule, 'something', 'foo')
@ -1098,7 +1146,7 @@ class PatchTest(unittest.TestCase):
self.assertIsNot(m1, m2) self.assertIsNot(m1, m2)
for mock in m1, m2: for mock in m1, m2:
self.assertNotCallable(m1) self.assertNotCallable(mock)
def test_new_callable_patch_object(self): def test_new_callable_patch_object(self):
@ -1111,7 +1159,7 @@ class PatchTest(unittest.TestCase):
self.assertIsNot(m1, m2) self.assertIsNot(m1, m2)
for mock in m1, m2: for mock in m1, m2:
self.assertNotCallable(m1) self.assertNotCallable(mock)
def test_new_callable_keyword_arguments(self): def test_new_callable_keyword_arguments(self):

View File

@ -1360,6 +1360,7 @@ class _patch(object):
self.autospec = autospec self.autospec = autospec
self.kwargs = kwargs self.kwargs = kwargs
self.additional_patchers = [] self.additional_patchers = []
self.is_started = False
def copy(self): def copy(self):
@ -1472,6 +1473,9 @@ class _patch(object):
def __enter__(self): def __enter__(self):
"""Perform the patch.""" """Perform the patch."""
if self.is_started:
raise RuntimeError("Patch is already started")
new, spec, spec_set = self.new, self.spec, self.spec_set new, spec, spec_set = self.new, self.spec, self.spec_set
autospec, kwargs = self.autospec, self.kwargs autospec, kwargs = self.autospec, self.kwargs
new_callable = self.new_callable new_callable = self.new_callable
@ -1603,6 +1607,7 @@ class _patch(object):
self.temp_original = original self.temp_original = original
self.is_local = local self.is_local = local
self._exit_stack = contextlib.ExitStack() self._exit_stack = contextlib.ExitStack()
self.is_started = True
try: try:
setattr(self.target, self.attribute, new_attr) setattr(self.target, self.attribute, new_attr)
if self.attribute_name is not None: if self.attribute_name is not None:
@ -1622,6 +1627,9 @@ class _patch(object):
def __exit__(self, *exc_info): def __exit__(self, *exc_info):
"""Undo the patch.""" """Undo the patch."""
if not self.is_started:
return
if self.is_local and self.temp_original is not DEFAULT: if self.is_local and self.temp_original is not DEFAULT:
setattr(self.target, self.attribute, self.temp_original) setattr(self.target, self.attribute, self.temp_original)
else: else:
@ -1638,6 +1646,7 @@ class _patch(object):
del self.target del self.target
exit_stack = self._exit_stack exit_stack = self._exit_stack
del self._exit_stack del self._exit_stack
self.is_started = False
return exit_stack.__exit__(*exc_info) return exit_stack.__exit__(*exc_info)

View File

@ -0,0 +1,3 @@
Limit starting a patcher (from :func:`unittest.mock.patch` or
:func:`unittest.mock.patch.object`) more than
once without stopping it