From 02b84cb1b4f5407309c81c8b1ae0397355d6e568 Mon Sep 17 00:00:00 2001 From: Xtreak Date: Fri, 29 Mar 2019 02:38:43 +0530 Subject: bpo-36366: Return None on stopping unstarted patch object (GH-12472) Return None after calling unittest.mock.patch.object.stop() regardless of whether the object was started. This makes the method idempotent. https://bugs.python.org/issue36366 --- Lib/unittest/test/testmock/testpatch.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'Lib/unittest/test') diff --git a/Lib/unittest/test/testmock/testpatch.py b/Lib/unittest/test/testmock/testpatch.py index c484adb605..2c14360b2d 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): -- cgit v1.2.1