diff options
author | Xtreak <tir.karthi@gmail.com> | 2019-02-25 00:24:49 +0530 |
---|---|---|
committer | Chris Withers <chris@withers.org> | 2019-04-30 08:39:55 +0100 |
commit | 5268edae65d7f840e91f3a689e4269ed5fcd6459 (patch) | |
tree | e859588bb8e4953570cdef91380ded858cd9990e | |
parent | 3830cd5a0d3b10d95c7b717505f266556202a518 (diff) | |
download | mock-5268edae65d7f840e91f3a689e4269ed5fcd6459.tar.gz |
bpo-35512: Resolve string target to patch.dict decorator during function call GH#12000
* 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
Backports: a875ea58b29fbf510f9790ae1653eeaa47dc0de8
Signed-off-by: Chris Withers <chris@withers.org>
Needed some munging due to package layout and code differences.
-rw-r--r-- | NEWS.d/2019-02-24-00-04-10.bpo-35512.eWDjCJ.rst | 3 | ||||
-rw-r--r-- | mock/mock.py | 4 | ||||
-rw-r--r-- | mock/tests/support.py | 3 | ||||
-rw-r--r-- | mock/tests/testpatch.py | 15 |
4 files changed, 23 insertions, 2 deletions
diff --git a/NEWS.d/2019-02-24-00-04-10.bpo-35512.eWDjCJ.rst b/NEWS.d/2019-02-24-00-04-10.bpo-35512.eWDjCJ.rst new file mode 100644 index 0000000..8281b1b --- /dev/null +++ b/NEWS.d/2019-02-24-00-04-10.bpo-35512.eWDjCJ.rst @@ -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. diff --git a/mock/mock.py b/mock/mock.py index 8d18e9b..acd7c28 100644 --- a/mock/mock.py +++ b/mock/mock.py @@ -1756,8 +1756,6 @@ class _patch_dict(object): """ def __init__(self, in_dict, values=(), clear=False, **kwargs): - if isinstance(in_dict, basestring): - in_dict = _importer(in_dict) self.in_dict = in_dict # support any argument supported by dict(...) constructor self.values = dict(values) @@ -1798,6 +1796,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 diff --git a/mock/tests/support.py b/mock/tests/support.py index 2bbc8a2..933be92 100644 --- a/mock/tests/support.py +++ b/mock/tests/support.py @@ -2,6 +2,9 @@ import contextlib import sys +target = {'foo': 'FOO'} + + def is_instance(obj, klass): """Version of is_instance that doesn't access __class__""" return issubclass(type(obj), klass) diff --git a/mock/tests/testpatch.py b/mock/tests/testpatch.py index 434e780..d8857b9 100644 --- a/mock/tests/testpatch.py +++ b/mock/tests/testpatch.py @@ -668,6 +668,21 @@ 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('mock.tests.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 + + @unittest.expectedFailure def test_patch_descriptor(self): # would be some effort to fix this - we could special case the |