Skip to content

Raise CaptionReadSyntaxError for malformed SRT instead of IndexError - #419

Open
eeshsaxena wants to merge 1 commit into
pbs:mainfrom
eeshsaxena:srt-malformed-read-error
Open

Raise CaptionReadSyntaxError for malformed SRT instead of IndexError#419
eeshsaxena wants to merge 1 commit into
pbs:mainfrom
eeshsaxena:srt-malformed-read-error

Conversation

@eeshsaxena

Copy link
Copy Markdown

SRTReader().read() leaks a bare IndexError (or ValueError) on a few kinds of malformed SRT, rather than one of the CaptionRead* errors callers expect to catch:

from pycaption import SRTReader
SRTReader().read("1\n")               # IndexError: list index out of range
SRTReader().read("1\n00:x:01,000 --> 00:00:04,000\ntext\n")  # ValueError

The first is a caption number with no timing line after it, the second a non-numeric timestamp. A timing line missing the --> arrow, or a stamp with too few fields, hit the same paths. I guarded the timing-line lookup and wrapped the timestamp parsing so they raise CaptionReadSyntaxError like the other readers do. Added a parametrized test for the malformed cases; full suite still passes.

SRTReader.read leaked a raw IndexError or ValueError on a few kinds of
malformed input: a caption number with no timing line after it, a timing
line with no --> arrow, or a timestamp with missing or non-numeric
fields. read() is meant to raise the CaptionRead* errors, so callers
catching those got an unexpected crash instead.

Guard the timing line and wrap the timestamp parsing so these surface as
CaptionReadSyntaxError, matching the other readers.
@eeshsaxena
eeshsaxena requested a review from a team as a code owner August 16, 2026 03:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant