From 042dc1ba223d9d22ff34897cb21876868713c9ee Mon Sep 17 00:00:00 2001 From: eeshsaxena Date: Sat, 15 Aug 2026 07:42:45 +0530 Subject: [PATCH] Raise CaptionReadTimingError for a non-numeric SAMI start time The SAMI reader converted a SYNC element's start attribute with int(float(start_str)), raising a bare ValueError when the value was not numeric. A missing start already raises CaptionReadTimingError, so raise the same error for an unparsable one. --- pycaption/sami/reader.py | 7 ++++++- tests/test_sami.py | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) 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")