diff options
author | Mario Corchero <mariocj89@gmail.com> | 2020-01-24 08:38:33 +0000 |
---|---|---|
committer | Chris Withers <chris@withers.org> | 2020-01-24 08:38:32 +0000 |
commit | e131c9720d087c0c4988bd2a5c62020feb9d1d77 (patch) | |
tree | 209230859dbb25990ed04ce139bf06abac490e04 /Lib/unittest/mock.py | |
parent | 1d0c5e16eab29d55773cc4196bb90d2bf12e09dd (diff) | |
download | cpython-git-e131c9720d087c0c4988bd2a5c62020feb9d1d77.tar.gz |
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.
Diffstat (limited to 'Lib/unittest/mock.py')
-rw-r--r-- | Lib/unittest/mock.py | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 5622917dc3..3fafe594c5 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -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): |