Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a785dec

Browse files
committed
Issue #23568: Add rdivmod support to MagicMock() objects.
Patch by Håkan Lövdahl.
1 parent b19542d commit a785dec

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

Lib/unittest/mock.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1644,7 +1644,9 @@ def _patch_stopall():
16441644
"len contains iter "
16451645
"hash str sizeof "
16461646
"enter exit "
1647-
"divmod neg pos abs invert "
1647+
# we added divmod and rdivmod here instead of numerics
1648+
# because there is no idivmod
1649+
"divmod rdivmod neg pos abs invert "
16481650
"complex int float index "
16491651
"trunc floor ceil "
16501652
"bool next "

Lib/unittest/test/testmock/testmagicmethods.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,5 +424,20 @@ def test_iterable_as_iter_return_value(self):
424424
self.assertEqual(list(m), [])
425425

426426

427+
def test_divmod_and_rdivmod(self):
428+
m = MagicMock()
429+
self.assertIsInstance(divmod(5, m), MagicMock)
430+
m.__divmod__.return_value = (2, 1)
431+
self.assertEqual(divmod(m, 2), (2, 1))
432+
m = MagicMock()
433+
foo = divmod(2, m)
434+
self.assertIsInstance(foo, MagicMock)
435+
foo_direct = m.__divmod__(2)
436+
self.assertIsInstance(foo_direct, MagicMock)
437+
bar = divmod(m, 2)
438+
self.assertIsInstance(bar, MagicMock)
439+
bar_direct = m.__rdivmod__(2)
440+
self.assertIsInstance(bar_direct, MagicMock)
441+
427442
if __name__ == '__main__':
428443
unittest.main()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Core and Builtins
1818
Library
1919
-------
2020

21+
- Issue #23568: Add rdivmod support to MagicMock() objects.
22+
Patch by Håkan Lövdahl.
23+
2124
- Issue #23138: Fixed parsing cookies with absent keys or values in cookiejar.
2225
Patch by Demian Brecht.
2326

0 commit comments

Comments
 (0)