From f1c8633a7d765f07b94e0a6097b3f0c7912b955f Mon Sep 17 00:00:00 2001 From: Gabe Sherman <56273933+gabe-sherman@users.noreply.github.com> Date: Sat, 19 Oct 2024 06:33:39 -0600 Subject: [PATCH] PO files: Consider a message without a translation to have an empty translation (#1135) Fixes an index out of bounds error in add_message. Co-authored-by: Tomas R Co-authored-by: Aarni Koskela --- babel/messages/pofile.py | 3 +++ tests/messages/test_pofile.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py index 5cd65d86..fa5c859c 100644 --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -247,6 +247,9 @@ class PoFileParser: def _finish_current_message(self) -> None: if self.messages: + if not self.translations: + self._invalid_pofile("", self.offset, f"missing msgstr for msgid '{self.messages[0].denormalize()}'") + self.translations.append([0, _NormalizedString("")]) self._add_message() def _process_message_line(self, lineno, line, obsolete=False) -> None: diff --git a/tests/messages/test_pofile.py b/tests/messages/test_pofile.py index c0ded129..3609b5c2 100644 --- a/tests/messages/test_pofile.py +++ b/tests/messages/test_pofile.py @@ -1014,3 +1014,20 @@ msgstr "" "Language: \n" ''') assert pofile.read_po(buf).locale is None + + +@pytest.mark.parametrize("case", ['msgid "foo"', 'msgid "foo"\nmsgid_plural "foos"']) +@pytest.mark.parametrize("abort_invalid", [False, True]) +def test_issue_1134(case: str, abort_invalid: bool): + buf = StringIO(case) + + if abort_invalid: + # Catalog not created, aborted with PoFileError + with pytest.raises(pofile.PoFileError) as excinfo: + pofile.read_po(buf, abort_invalid=True) + assert str(excinfo.value) == "missing msgstr for msgid 'foo' on 0" + else: + # Catalog is created with warning, no abort + output = pofile.read_po(buf) + assert len(output) == 1 + assert output["foo"].string in ((''), ('', '')) -- 2.47.2