[2.7] bpo-31752: Fix possible crash in timedelta constructor called with custom integers. (GH-3947) (#4088)
Bad remainder in divmod() in intermediate calculations caused an assertion failure..
(cherry picked from commit 4ffd4653a7ec9c97775472276cf5e159e2366bb2)
diff --git a/Lib/test/test_datetime.py b/Lib/test/test_datetime.py
index 20abe74..f56c3f5 100644
--- a/Lib/test/test_datetime.py
+++ b/Lib/test/test_datetime.py
@@ -492,6 +492,46 @@
self.assertEqual(str(t3), str(t4))
self.assertEqual(t4.as_hours(), -1)
+ def test_issue31752(self):
+ # The interpreter shouldn't crash because divmod() returns negative
+ # remainder.
+ class BadInt(int):
+ def __mul__(self, other):
+ return Prod()
+
+ class BadLong(long):
+ def __mul__(self, other):
+ return Prod()
+
+ class Prod:
+ def __radd__(self, other):
+ return Sum()
+
+ class Sum(int):
+ def __divmod__(self, other):
+ # negative remainder
+ return (0, -1)
+
+ timedelta(microseconds=BadInt(1))
+ timedelta(hours=BadInt(1))
+ timedelta(weeks=BadInt(1))
+ timedelta(microseconds=BadLong(1))
+ timedelta(hours=BadLong(1))
+ timedelta(weeks=BadLong(1))
+
+ class Sum(long):
+ def __divmod__(self, other):
+ # negative remainder
+ return (0, -1)
+
+ timedelta(microseconds=BadInt(1))
+ timedelta(hours=BadInt(1))
+ timedelta(weeks=BadInt(1))
+ timedelta(microseconds=BadLong(1))
+ timedelta(hours=BadLong(1))
+ timedelta(weeks=BadLong(1))
+
+
#############################################################################
# date tests