diff --git a/pycaption/sami/reader.py b/pycaption/sami/reader.py index dc4aea62..a8ed2e68 100644 --- a/pycaption/sami/reader.py +++ b/pycaption/sami/reader.py @@ -142,7 +142,12 @@ 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 on the following line: {p.parent}." + ) start = milliseconds * 1000 self._backfill_end_times(captions, start) diff --git a/tests/test_sami.py b/tests/test_sami.py index 3ce482bb..4380d22f 100644 --- a/tests/test_sami.py +++ b/tests/test_sami.py @@ -50,6 +50,17 @@ def test_missing_start(self, sample_sami_missing_start): "Missing start time on the following line: " ) + def test_non_numeric_start(self): + # A non-numeric SYNC start attribute used to raise a bare ValueError + # from int(float(start_str)); it should be a CaptionReadTimingError. + content = "

Hi

" + with pytest.raises(CaptionReadTimingError) as exc_info: + self.reader.read(content) + + assert exc_info.value.args[0].startswith( + "Invalid start time on the following line: " + ) + 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")