diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-10-23 16:57:04 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-23 16:57:04 (GMT) |
commit | 5ef883b096895a84123760859f0f34ad37bf2277 (patch) | |
tree | 4c6b14625df7fe8699ae86a64b7e30e74515d765 /Lib/test/test_datetime.py | |
parent | f7d19b0464cb89ec696affbad9fd7e7c94456eaf (diff) | |
download | cpython-5ef883b096895a84123760859f0f34ad37bf2277.zip cpython-5ef883b096895a84123760859f0f34ad37bf2277.tar.gz cpython-5ef883b096895a84123760859f0f34ad37bf2277.tar.bz2 |
[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)
Diffstat (limited to 'Lib/test/test_datetime.py')
-rw-r--r-- | Lib/test/test_datetime.py | 40 |
1 files changed, 40 insertions, 0 deletions
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 @@ class TestTimeDelta(HarmlessMixedComparison, unittest.TestCase): 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 |