[Python-checkins] cpython (merge 3.4 -> default): Merge

michael.foord python-checkins at python.org
Mon Apr 14 17:27:06 CEST 2014


http://hg.python.org/cpython/rev/34687e01543f
changeset:   90252:34687e01543f
parent:      90247:fe532dccf8f6
parent:      90251:445ef3b58109
user:        Michael Foord <michael at voidspace.org.uk>
date:        Mon Apr 14 11:24:38 2014 -0400
summary:
  Merge

files:
  Lib/unittest/mock.py                           |   4 +-
  Lib/unittest/test/testmock/testmagicmethods.py |  25 ++++++++++
  Misc/NEWS                                      |   3 +
  3 files changed, 31 insertions(+), 1 deletions(-)


diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -1634,7 +1634,9 @@
     "bool next "
 )
 
-numerics = "add sub mul div floordiv mod lshift rshift and xor or pow "
+numerics = (
+    "add sub mul div floordiv mod lshift rshift and xor or pow truediv"
+)
 inplace = ' '.join('i%s' % n for n in numerics.split())
 right = ' '.join('r%s' % n for n in numerics.split())
 
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
@@ -126,6 +126,31 @@
         self.assertEqual(7 + mock, mock)
         self.assertEqual(mock.value, 16)
 
+    def test_division(self):
+        original = mock = Mock()
+        mock.value = 32
+        self.assertRaises(TypeError, lambda: mock / 2)
+
+        def truediv(self, other):
+            mock.value /= other
+            return self
+        mock.__truediv__ = truediv
+        self.assertEqual(mock / 2, mock)
+        self.assertEqual(mock.value, 16)
+
+        del mock.__truediv__
+        def itruediv(mock):
+            mock /= 4
+        self.assertRaises(TypeError, itruediv, mock)
+        mock.__itruediv__ = truediv
+        mock /= 8
+        self.assertEqual(mock, original)
+        self.assertEqual(mock.value, 2)
+
+        self.assertRaises(TypeError, lambda: 8 / mock)
+        mock.__rtruediv__ = truediv
+        self.assertEqual(0.5 / mock, mock)
+        self.assertEqual(mock.value, 4)
 
     def test_hash(self):
         mock = Mock()
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -37,6 +37,9 @@
 Library
 -------
 
+- Issue #20968: unittest.mock.MagicMock now supports division.
+  Patch by Johannes Baiter.
+
 - Fix arbitrary memory access in JSONDecoder.raw_decode with a negative second
   parameter. Bug reported by Guido Vranken.
 

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


More information about the Python-checkins mailing list