]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
PO files: Consider a message without a translation to have an empty translation ...
authorGabe Sherman <56273933+gabe-sherman@users.noreply.github.com>
Sat, 19 Oct 2024 12:33:39 +0000 (06:33 -0600)
committerGitHub <noreply@github.com>
Sat, 19 Oct 2024 12:33:39 +0000 (12:33 +0000)
Fixes an index out of bounds error in add_message.

Co-authored-by: Tomas R <tomas.roun8@gmail.com>
Co-authored-by: Aarni Koskela <akx@iki.fi>
babel/messages/pofile.py
tests/messages/test_pofile.py

index 5cd65d86737dda2bee71fb7f071e4dd16b5fc43a..fa5c859cdd49bf476d94a1d30efede5f5483ae03 100644 (file)
@@ -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:
index c0ded12967c2d252e62146bcb5e0d7d0ee30f6d7..3609b5c2b7fcc20a378ec7aa6dfcc9e29ddd1d63 100644 (file)
@@ -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 ((''), ('', ''))