mirror of
https://github.com/python/cpython.git
synced 2025-10-05 06:31:48 +00:00
bpo-31752: Fix possible crash in timedelta constructor called with custom integers. (GH-3947) (#4086)
Bad remainder in divmod() in intermediate calculations caused an assertion failure.
(cherry picked from commit 4ffd4653a7
)
This commit is contained in:
parent
aaf6a3dbbd
commit
6e45d7b90a
3 changed files with 28 additions and 2 deletions
|
@ -866,6 +866,26 @@ class TestTimeDelta(HarmlessMixedComparison, unittest.TestCase):
|
|||
with self.assertRaises(ValueError):
|
||||
timedelta() * get_bad_float(bad_ratio)
|
||||
|
||||
def test_issue31752(self):
|
||||
# The interpreter shouldn't crash because divmod() returns negative
|
||||
# remainder.
|
||||
class BadInt(int):
|
||||
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))
|
||||
|
||||
|
||||
#############################################################################
|
||||
# date tests
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue