]> granicus.if.org Git - python/commitdiff
bpo-35512: Resolve string target to patch.dict decorator during function call GH...
authorXtreak <tir.karthi@gmail.com>
Sun, 24 Feb 2019 18:54:49 +0000 (00:24 +0530)
committerChris Withers <chris@withers.org>
Sun, 24 Feb 2019 18:54:49 +0000 (18:54 +0000)
* Resolve string target to patch.dict during function call

* Add NEWS entry

* Remove unneeded call

* Restore original value for support.target and refactor assertions

* Add extra assertion to verify unpatched dict

Lib/unittest/mock.py
Lib/unittest/test/testmock/support.py
Lib/unittest/test/testmock/testpatch.py
Misc/NEWS.d/next/Library/2019-02-24-00-04-10.bpo-35512.eWDjCJ.rst [new file with mode: 0644]

index 8f46050462bd941dd1b1c4fde520ea3f7bb0587f..ccbcd355ce31c103a646cbe972cb3ecefdc80417 100644 (file)
@@ -1620,8 +1620,6 @@ class _patch_dict(object):
     """
 
     def __init__(self, in_dict, values=(), clear=False, **kwargs):
-        if isinstance(in_dict, str):
-            in_dict = _importer(in_dict)
         self.in_dict = in_dict
         # support any argument supported by dict(...) constructor
         self.values = dict(values)
@@ -1662,6 +1660,8 @@ class _patch_dict(object):
 
     def _patch_dict(self):
         values = self.values
+        if isinstance(self.in_dict, str):
+            self.in_dict = _importer(self.in_dict)
         in_dict = self.in_dict
         clear = self.clear
 
index c7ad20b80665f2f66737ac803903f0e572ebae09..f146be244e9c40c2328ce6168eb472487d4efd73 100644 (file)
@@ -1,3 +1,6 @@
+target = {'foo': 'FOO'}
+
+
 def is_instance(obj, klass):
     """Version of is_instance that doesn't access __class__"""
     return issubclass(type(obj), klass)
index f05225730dafb29e8b4264e3750cd40211b72647..c484adb605086a576b4bc84f802d0d9dddde540d 100644 (file)
@@ -664,6 +664,23 @@ class PatchTest(unittest.TestCase):
         test()
 
 
+    def test_patch_dict_decorator_resolution(self):
+        # bpo-35512: Ensure that patch with a string target resolves to
+        # the new dictionary during function call
+        original = support.target.copy()
+
+        @patch.dict('unittest.test.testmock.support.target', {'bar': 'BAR'})
+        def test():
+            self.assertEqual(support.target, {'foo': 'BAZ', 'bar': 'BAR'})
+
+        try:
+            support.target = {'foo': 'BAZ'}
+            test()
+            self.assertEqual(support.target, {'foo': 'BAZ'})
+        finally:
+            support.target = original
+
+
     def test_patch_descriptor(self):
         # would be some effort to fix this - we could special case the
         # builtin descriptors: classmethod, property, staticmethod
diff --git a/Misc/NEWS.d/next/Library/2019-02-24-00-04-10.bpo-35512.eWDjCJ.rst b/Misc/NEWS.d/next/Library/2019-02-24-00-04-10.bpo-35512.eWDjCJ.rst
new file mode 100644 (file)
index 0000000..8281b1b
--- /dev/null
@@ -0,0 +1,3 @@
+:func:`unittest.mock.patch.dict` used as a decorator with string target
+resolves the target during function call instead of during decorator
+construction. Patch by Karthikeyan Singaravelan.