bpo-31293: Fix crashes in truediv and mul of a timedelta by a float with a bad as_integer_ratio() method. (#3227)

This commit is contained in:
Oren Milman 2017-09-19 15:58:11 +03:00 committed by Serhiy Storchaka
parent 9974e1bcf3
commit 865e4b4f63
3 changed files with 55 additions and 4 deletions

View file

@ -866,6 +866,26 @@ class TestTimeDelta(HarmlessMixedComparison, unittest.TestCase):
self.assertRaises(TypeError, divmod, t, 10)
def test_issue31293(self):
# The interpreter shouldn't crash in case a timedelta is divided or
# multiplied by a float with a bad as_integer_ratio() method.
def get_bad_float(bad_ratio):
class BadFloat(float):
def as_integer_ratio(self):
return bad_ratio
return BadFloat()
with self.assertRaises(TypeError):
timedelta() / get_bad_float(1 << 1000)
with self.assertRaises(TypeError):
timedelta() * get_bad_float(1 << 1000)
for bad_ratio in [(), (42, ), (1, 2, 3)]:
with self.assertRaises(ValueError):
timedelta() / get_bad_float(bad_ratio)
with self.assertRaises(ValueError):
timedelta() * get_bad_float(bad_ratio)
#############################################################################
# date tests