From a785dece572e7f2bcc8a10aab3a661208e94583e Mon Sep 17 00:00:00 2001
From: Berker Peksag <berker.peksag@gmail.com>
Date: Sun, 15 Mar 2015 01:51:56 +0200
Subject: Issue #23568: Add rdivmod support to MagicMock() objects.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Patch by Håkan Lövdahl.
---
 Lib/unittest/mock.py                           |  4 +++-
 Lib/unittest/test/testmock/testmagicmethods.py | 15 +++++++++++++++
 Misc/NEWS                                      |  3 +++
 3 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 5555774..dcb2d8f 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -1644,7 +1644,9 @@ magic_methods = (
     "len contains iter "
     "hash str sizeof "
     "enter exit "
-    "divmod neg pos abs invert "
+    # we added divmod and rdivmod here instead of numerics
+    # because there is no idivmod
+    "divmod rdivmod neg pos abs invert "
     "complex int float index "
     "trunc floor ceil "
     "bool next "
diff --git a/Lib/unittest/test/testmock/testmagicmethods.py b/Lib/unittest/test/testmock/testmagicmethods.py
index cc0e707..73b717d 100644
--- a/Lib/unittest/test/testmock/testmagicmethods.py
+++ b/Lib/unittest/test/testmock/testmagicmethods.py
@@ -424,5 +424,20 @@ class TestMockingMagicMethods(unittest.TestCase):
         self.assertEqual(list(m), [])
 
 
+    def test_divmod_and_rdivmod(self):
+        m = MagicMock()
+        self.assertIsInstance(divmod(5, m), MagicMock)
+        m.__divmod__.return_value = (2, 1)
+        self.assertEqual(divmod(m, 2), (2, 1))
+        m = MagicMock()
+        foo = divmod(2, m)
+        self.assertIsInstance(foo, MagicMock)
+        foo_direct = m.__divmod__(2)
+        self.assertIsInstance(foo_direct, MagicMock)
+        bar = divmod(m, 2)
+        self.assertIsInstance(bar, MagicMock)
+        bar_direct = m.__rdivmod__(2)
+        self.assertIsInstance(bar_direct, MagicMock)
+
 if __name__ == '__main__':
     unittest.main()
diff --git a/Misc/NEWS b/Misc/NEWS
index 170cd4e..90edc96 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -18,6 +18,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #23568: Add rdivmod support to MagicMock() objects.
+  Patch by Håkan Lövdahl.
+
 - Issue #23138: Fixed parsing cookies with absent keys or values in cookiejar.
   Patch by Demian Brecht.
 
-- 
cgit v0.12

