Fix mock.patch.dict to be stopped with mock.patch.stopall (#17606)

As the function was not registering in the active patches, the mocks
started by `mock.patch.dict` were not being stopped when
`mock.patch.stopall` was being called.
This commit is contained in:
Mario Corchero 2020-01-24 08:38:33 +00:00 committed by Chris Withers
parent 1d0c5e16ea
commit e131c9720d
3 changed files with 69 additions and 2 deletions

View file

@ -1851,8 +1851,23 @@ class _patch_dict(object):
self._unpatch_dict()
return False
start = __enter__
stop = __exit__
def start(self):
"""Activate a patch, returning any created mock."""
result = self.__enter__()
_patch._active_patches.append(self)
return result
def stop(self):
"""Stop an active patch."""
try:
_patch._active_patches.remove(self)
except ValueError:
# If the patch hasn't been started this will fail
pass
return self.__exit__()
def _clear_dict(in_dict):