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 /mock/tests | |
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.
Diffstat (limited to 'mock/tests')
-rw-r--r-- | mock/tests/support.py | 3 | ||||
-rw-r--r-- | mock/tests/testpatch.py | 15 |
2 files changed, 18 insertions, 0 deletions
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 |