[Python-checkins] cpython: Issue #23310: Fix MagicMock's initializer to work with __methods__.

lukasz.langa python-checkins at python.org
Tue Apr 14 08:12:54 CEST 2015


https://hg.python.org/cpython/rev/4d1236b180be
changeset:   95627:4d1236b180be
user:        Łukasz Langa <lukasz at langa.pl>
date:        Mon Apr 13 23:12:42 2015 -0700
summary:
  Issue #23310: Fix MagicMock's initializer to work with __methods__.
Behavior equivalent to m.configure_mock().  Patch by Kasia Jachim.

files:
  Lib/unittest/mock.py                           |   5 ++-
  Lib/unittest/test/testmock/testmagicmethods.py |  15 ++++++++++
  Misc/ACKS                                      |   1 +
  Misc/NEWS                                      |   3 ++
  4 files changed, 22 insertions(+), 2 deletions(-)


diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -1789,14 +1789,15 @@
 
 class MagicMixin(object):
     def __init__(self, *args, **kw):
+        self._mock_set_magics()  # make magic work for kwargs in init
         _safe_super(MagicMixin, self).__init__(*args, **kw)
-        self._mock_set_magics()
+        self._mock_set_magics()  # fix magic broken by upper level init
 
 
     def _mock_set_magics(self):
         these_magics = _magics
 
-        if self._mock_methods is not None:
+        if getattr(self, "_mock_methods", None) is not None:
             these_magics = _magics.intersection(self._mock_methods)
 
             remove_magics = set()
diff --git a/Lib/unittest/test/testmock/testmagicmethods.py b/Lib/unittest/test/testmock/testmagicmethods.py
--- a/Lib/unittest/test/testmock/testmagicmethods.py
+++ b/Lib/unittest/test/testmock/testmagicmethods.py
@@ -450,5 +450,20 @@
         bar_direct = m.__rdivmod__(2)
         self.assertIsInstance(bar_direct, MagicMock)
 
+    # http://bugs.python.org/issue23310
+    # Check if you can change behaviour of magic methds in MagicMock init
+    def test_magic_in_initialization(self):
+        m = MagicMock(**{'__str__.return_value': "12"})
+        self.assertEqual(str(m), "12")
+
+    def test_changing_magic_set_in_initialization(self):
+        m = MagicMock(**{'__str__.return_value': "12"})
+        m.__str__.return_value = "13"
+        self.assertEqual(str(m), "13")
+        m = MagicMock(**{'__str__.return_value': "12"})
+        m.configure_mock(**{'__str__.return_value': "14"})
+        self.assertEqual(str(m), "14")
+
+
 if __name__ == '__main__':
     unittest.main()
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -849,6 +849,7 @@
 Anne Lord
 Tom Loredo
 Justin Love
+Kasia Jachim
 Ned Jackson Lovely
 Chalmer Lowe
 Jason Lowe
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -136,6 +136,9 @@
 - Issue #22117: Fix os.utime(), it now rounds the timestamp towards minus
   infinity (-inf) instead of rounding towards zero.
 
+- Issue #23310: Fix MagicMock's initializer to work with __methods__, just
+  like configure_mock().  Patch by Kasia Jachim.
+
 Build
 -----
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list