From ae95b7f90ffb0d5a8c3f2dc7ae370c78dd7e41f5 Mon Sep 17 00:00:00 2001 From: Eesh Saxena Date: Thu, 13 Aug 2026 04:18:19 +0530 Subject: [PATCH] Return None instead of raising OverflowError on an out-of-range value timeparse already skips a malformed number field (like '1.2.3') and moves on to return None, but only ValueError was caught. A value large enough that float() overflows to inf makes int(sum(...)) raise OverflowError, which escaped and crashed the call. Catch OverflowError alongside ValueError so these behave like any other unparseable input. --- pytimeparse/tests/testtimeparse.py | 12 ++++++++++++ pytimeparse/timeparse.py | 10 +++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/pytimeparse/tests/testtimeparse.py b/pytimeparse/tests/testtimeparse.py index c305bb5..9022e3b 100644 --- a/pytimeparse/tests/testtimeparse.py +++ b/pytimeparse/tests/testtimeparse.py @@ -358,6 +358,18 @@ def test_timeparse_33(self): self.assertEqual(timeparse.timeparse('+5.6 weeks'), 3386880) self.assertEqual(timeparse.timeparse('-5.6 weeks'), -3386880) + def test_out_of_range(self): + '''An absurdly large value returns None instead of raising.''' + # The number is syntactically valid but so large that float() + # overflows to inf, which int() cannot convert. That used to leak + # an OverflowError; it should be treated as unparseable (None), the + # same as any other malformed number. + for value in ('9' * 400 + '.5 minutes', + '1' * 350 + '.1 hours', + '5' * 500 + '.5 days', + '9' * 400 + '.9 wk'): + self.assertIsNone(timeparse.timeparse(value)) + def test_doctest(self): '''Run timeparse doctests.''' self.assertTrue(doctest.testmod(timeparse, raise_on_error=True)) diff --git a/pytimeparse/timeparse.py b/pytimeparse/timeparse.py index ace5cc3..8d83725 100644 --- a/pytimeparse/timeparse.py +++ b/pytimeparse/timeparse.py @@ -181,7 +181,11 @@ def timeparse(sval, granularity='seconds'): # SECS is a float, we will return a float return sign * sum([MULTIPLIERS[k] * float(v) for (k, v) in list(mdict.items()) if v is not None]) - except ValueError: - # Malformed number string (e.g. '1.2.3', '.') — skip to - # the next time format pattern per documented behavior. + except (ValueError, OverflowError): + # A malformed number string (e.g. '1.2.3', '.') raises + # ValueError, and an absurdly large value whose float() + # overflows to inf (so int() cannot convert it) raises + # OverflowError. In either case the field is not a usable + # number, so skip to the next time format pattern, per the + # documented behavior of returning None for unparseable input. pass