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
10 changes: 9 additions & 1 deletion srt.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,15 @@ def srt_timestamp_to_timedelta(timestamp):
if match is None:
raise TimestampParseError("Unparseable timestamp: {}".format(timestamp))
hrs, mins, secs, msecs = [int(m) if m else 0 for m in match.groups()]
return timedelta(hours=hrs, minutes=mins, seconds=secs, milliseconds=msecs)
try:
return timedelta(hours=hrs, minutes=mins, seconds=secs, milliseconds=msecs)
except OverflowError:
# timedelta only holds up to ~2.7 million years, so a field with enough
# digits blows past that. This is still a bad timestamp as far as we're
# concerned, so surface it as one rather than leaking OverflowError.
raise TimestampParseError(
"Timestamp is too large to represent: {}".format(timestamp)
)


def sort_and_reindex(subtitles, start_index=1, in_place=False, skip=True):
Expand Down
12 changes: 12 additions & 0 deletions tests/test_srt.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,18 @@ def test_bad_timestamp_format_raises(ts):
srt.srt_timestamp_to_timedelta(ts)


def test_out_of_range_timestamp_raises():
# A field big enough to overflow timedelta used to leak OverflowError
with pytest.raises(srt.TimestampParseError):
srt.srt_timestamp_to_timedelta("9999999999999999:00:00,000")


def test_parse_out_of_range_timestamp_raises():
over_range = "1\n9999999999999999:00:00,000 --> 00:00:01,000\nhi\n\n"
with pytest.raises(srt.TimestampParseError):
list(srt.parse(over_range))


@given(st.lists(subtitles()), st.lists(st.sampled_from(string.whitespace)))
def test_can_parse_index_trailing_ws(input_subs, whitespace):
out = ""
Expand Down