Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pytimeparse/tests/testtimeparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
10 changes: 7 additions & 3 deletions pytimeparse/timeparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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