This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author xtreak
Recipients Antoni Szych, cjw296, eric.snow, kakuma, kushal.das, lisroach, lwcolton, mariocj89, michael.foord, xtreak
Date 2019-12-13.09:05:58
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1576227959.09.0.197730913223.issue21600@roundup.psfhosted.org>
In-reply-to
Content
This sounds like a good idea given the docs and the general agreement in the thread. I would like to target this for 3.9 . Differentiation between the normal patches and the ones from patch.dict is nice but given that docstring indicates LIFO for all active patches storing patch.dict items also in _patch.active_patches ensures we can do LIFO if there is a mixture of normal and dictionary patches started with start method. Adding other maintainers for thoughts.

diff --git Lib/unittest/mock.py Lib/unittest/mock.py
index cd5a2aeb60..96115e06ba 100644
--- Lib/unittest/mock.py
+++ Lib/unittest/mock.py
@@ -1853,8 +1853,21 @@ 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__()
History
Date User Action Args
2019-12-13 09:05:59xtreaksetrecipients: + xtreak, cjw296, michael.foord, eric.snow, kushal.das, kakuma, lwcolton, lisroach, mariocj89, Antoni Szych
2019-12-13 09:05:59xtreaksetmessageid: <1576227959.09.0.197730913223.issue21600@roundup.psfhosted.org>
2019-12-13 09:05:59xtreaklinkissue21600 messages
2019-12-13 09:05:58xtreakcreate