]> granicus.if.org Git - python/commitdiff
Closes issue 21239. unittest.mock.patch.stopall() did not work deterministically...
authorMichael Foord <michael@voidspace.org.uk>
Tue, 15 Apr 2014 21:21:08 +0000 (17:21 -0400)
committerMichael Foord <michael@voidspace.org.uk>
Tue, 15 Apr 2014 21:21:08 +0000 (17:21 -0400)
Lib/unittest/mock.py
Lib/unittest/test/testmock/testpatch.py
Misc/NEWS

index 13fd7b1af04ea92b25bbe4221fd6c15db52983f0..5555774467d4cad9b90904ea703dcbf81295c1f8 100644 (file)
@@ -1050,7 +1050,7 @@ def _is_started(patcher):
 class _patch(object):
 
     attribute_name = None
-    _active_patches = set()
+    _active_patches = []
 
     def __init__(
             self, getter, attribute, new, spec, create,
@@ -1323,13 +1323,18 @@ class _patch(object):
     def start(self):
         """Activate a patch, returning any created mock."""
         result = self.__enter__()
-        self._active_patches.add(self)
+        self._active_patches.append(self)
         return result
 
 
     def stop(self):
         """Stop an active patch."""
-        self._active_patches.discard(self)
+        try:
+            self._active_patches.remove(self)
+        except ValueError:
+            # If the patch hasn't been started this will fail
+            pass
+
         return self.__exit__()
 
 
@@ -1622,8 +1627,8 @@ def _clear_dict(in_dict):
 
 
 def _patch_stopall():
-    """Stop all active patches."""
-    for patch in list(_patch._active_patches):
+    """Stop all active patches. LIFO to unroll nested patches."""
+    for patch in reversed(_patch._active_patches):
         patch.stop()
 
 
index c1bc34fa8c92641a9ef4addf592feb87197509b2..b516f42af0b712e0d62f74ebd7f5dd834d896966 100644 (file)
@@ -12,7 +12,7 @@ from unittest.test.testmock.support import SomeClass, is_instance
 from unittest.mock import (
     NonCallableMock, CallableMixin, patch, sentinel,
     MagicMock, Mock, NonCallableMagicMock, patch, _patch,
-    DEFAULT, call, _get_target
+    DEFAULT, call, _get_target, _patch
 )
 
 
@@ -1779,6 +1779,23 @@ class PatchTest(unittest.TestCase):
         patched()
         self.assertIs(os.path, path)
 
+    def test_stopall_lifo(self):
+        stopped = []
+        class thing(object):
+            one = two = three = None
+
+        def get_patch(attribute):
+            class mypatch(_patch):
+                def stop(self):
+                    stopped.append(attribute)
+                    return super(mypatch, self).stop()
+            return mypatch(lambda: thing, attribute, None, None,
+                           False, None, None, None, {})
+        [get_patch(val).start() for val in ("one", "two", "three")]
+        patch.stopall()
+
+        self.assertEqual(stopped, ["three", "two", "one"])
+
 
 if __name__ == '__main__':
     unittest.main()
index 04410743653aca6073461a2705dce99f69998b1e..1c4a80339e92a2bb4e51e55d040a1a626c636a03 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -37,6 +37,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #21239: patch.stopall() didn't work deterministically when the same
+  name was patched more than once.
+
 - Issue #21222: Passing name keyword argument to mock.create_autospec now
   works.