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: 10 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
Changelog
---------
2.3.8
^^^^^^
- Fix ``SAMIReader`` raising a bare ``ValueError`` when a ``<sync>`` tag's
``start`` attribute is present but not numeric. It now raises
``CaptionReadTimingError`` like the missing-start case.

- Fix ``SAMIReader`` raising ``AttributeError`` when a tag has a valueless
``class`` (or ``lang``) attribute. Such attributes carry no language
information and are now ignored.

2.3.7
^^^^^^
- Fix ``SCCWriter`` merging multi-line captions into a single line when
Expand Down
4 changes: 4 additions & 0 deletions pycaption/sami/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ class name in the parsed stylesheet for a lang property.
:rtype: str | None
"""
for attr, value in attrs:
if value is None:
# A valueless attribute (e.g. a bare ``class``) carries no
# language information; skip it rather than crashing.
continue
if attr.lower() == "lang":
return value[:2]
if attr.lower() == "class":
Expand Down
8 changes: 7 additions & 1 deletion pycaption/sami/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,13 @@ def _translate_lang(self, language, sami_soup, parent_layout):
raise CaptionReadTimingError(
f"Missing start time on the following line: {p.parent}."
)
milliseconds = int(float(start_str))
try:
milliseconds = int(float(start_str))
except ValueError:
raise CaptionReadTimingError(
f"Invalid start time {start_str!r} on the following "
f"line: {p.parent}."
)
start = milliseconds * 1000

self._backfill_end_times(captions, start)
Expand Down
20 changes: 20 additions & 0 deletions tests/test_sami.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ def test_missing_start(self, sample_sami_missing_start):
"Missing start time on the following line: "
)

@pytest.mark.parametrize("start", ["abc", "1rt=1000", ""])
def test_non_numeric_start_raises_timing_error(self, start):
content = (
f"<SAMI><BODY><SYNC Start={start}>"
"<P>hi</P></SYNC></BODY></SAMI>"
)
with pytest.raises(CaptionReadTimingError):
self.reader.read(content)

def test_valueless_class_attribute_is_ignored(self):
# A bare ``class`` attribute (no value) used to crash _find_lang with
# AttributeError; it should just be treated as carrying no language.
content = (
"<SAMI><BODY><SYNC Start=1000>"
"<P Class>hi</P></SYNC></BODY></SAMI>"
)
caption_set = self.reader.read(content)
langs = caption_set.get_languages()
assert caption_set.get_captions(langs[0])[0].get_text() == "hi"

def test_6digit_color_code_from_6digit_input(self, sample_sami):
caption_set = self.reader.read(sample_sami)
p_style = caption_set.get_style("p")
Expand Down