Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1398,7 +1398,7 @@ def __enter__(self):
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)
Expand Down
12 changes: 10 additions & 2 deletions Lib/unittest/test/testmock/testpatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,10 +772,18 @@ def test_patch_start_stop(self):


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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Calling ``stop()`` on an unstarted or stopped :func:`unittest.mock.patch`
object will now return `None` instead of raising :exc:`RuntimeError`,
making the method idempotent.
Patch byKarthikeyan Singaravelan.