diff options
Diffstat (limited to 'Lib/unittest')
-rw-r--r-- | Lib/unittest/mock.py | 2 | ||||
-rw-r--r-- | Lib/unittest/test/testmock/testpatch.py | 12 |
2 files changed, 11 insertions, 3 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index fdde16b..8684f1d 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -1398,7 +1398,7 @@ class _patch(object): def __exit__(self, *exc_info): """Undo the patch.""" if not _is_started(self): - raise RuntimeError('stop called on unstarted patcher') + return if self.is_local and self.temp_original is not DEFAULT: setattr(self.target, self.attribute, self.temp_original) diff --git a/Lib/unittest/test/testmock/testpatch.py b/Lib/unittest/test/testmock/testpatch.py index c484adb..2c14360 100644 --- a/Lib/unittest/test/testmock/testpatch.py +++ b/Lib/unittest/test/testmock/testpatch.py @@ -772,10 +772,18 @@ class PatchTest(unittest.TestCase): def test_stop_without_start(self): + # bpo-36366: calling stop without start will return None. patcher = patch(foo_name, 'bar', 3) + self.assertIsNone(patcher.stop()) - # calling stop without start used to produce a very obscure error - self.assertRaises(RuntimeError, patcher.stop) + + def test_stop_idempotent(self): + # bpo-36366: calling stop on an already stopped patch will return None. + patcher = patch(foo_name, 'bar', 3) + + patcher.start() + patcher.stop() + self.assertIsNone(patcher.stop()) def test_patchobject_start_stop(self): |