From: Michael Foord Date: Mon, 14 Apr 2014 15:23:48 +0000 (-0400) Subject: Issue 20968. unittest.mock.MagicMock now supports division X-Git-Tag: v3.4.1rc1~95^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d2623d778663c1fee90a8835c00d0caa5690d2bd;p=python Issue 20968. unittest.mock.MagicMock now supports division --- diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 8b76503429..a23df5a73d 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -1634,7 +1634,9 @@ magic_methods = ( "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 index 5ff158dd12..cc0e707ece 100644 --- a/Lib/unittest/test/testmock/testmagicmethods.py +++ b/Lib/unittest/test/testmock/testmagicmethods.py @@ -126,6 +126,31 @@ class TestMockingMagicMethods(unittest.TestCase): 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 index 6e14968629..5bab4932af 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -30,6 +30,9 @@ Core and Builtins 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.