Skip to content

Commit 5f09192

Browse files
committed
Add further msgstr plural checks
1 parent 663f97e commit 5f09192

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

babel/messages/pofile.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,21 @@ def _process_keyword_line(self, lineno, line, obsolete=False) -> None:
292292
self.in_msgid = False
293293
self.in_msgstr = True
294294
if arg.startswith('['):
295-
idx, msg = arg[1:].split(']', 1)
296-
self.translations.append([int(idx), _NormalizedString(msg)])
295+
arg = arg[1:]
296+
if ']' not in arg:
297+
self._invalid_pofile(line, lineno, "msgstr plural doesn't include a closing bracket")
298+
# if it doesn't include ] it probablyd doesn't have a number either
299+
arg = '0]' + arg
300+
idx, msg = arg.split(']', 1)
301+
try:
302+
idx = int(idx)
303+
except ValueError:
304+
self._invalid_pofile(line, lineno, "msgstr plural's index is not a number")
305+
idx = 0
306+
if len(msg) < 2:
307+
self._invalid_pofile(line, lineno, "msgstr plural doesn't have a message")
308+
msg = '""'
309+
self.translations.append([idx, _NormalizedString(msg)])
297310
else:
298311
self.translations.append([0, _NormalizedString(arg)])
299312

tests/messages/test_pofile.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,34 @@ def test_abort_invalid_po_file(self):
544544
output = pofile.read_po(buf, locale='fr', abort_invalid=False)
545545
assert isinstance(output, Catalog)
546546

547+
def test_invalid_msgstr_plural(self):
548+
# msgstr plural broken
549+
invalid_1 = '''
550+
msgid "A"
551+
msgstr[
552+
'''
553+
invalid_2 = '''
554+
msgid "A"
555+
msgstr[] "no index"
556+
'''
557+
invalid_3 = '''
558+
msgid "A"
559+
msgstr[bah] "index is not a number"
560+
'''
561+
# no content
562+
invalid_4 = '''
563+
msgid "A"
564+
msgstr[0]
565+
'''
566+
for incorrectly_plural in (invalid_1, invalid_2, invalid_3, invalid_4):
567+
buf = StringIO(incorrectly_plural)
568+
with pytest.raises(pofile.PoFileError):
569+
pofile.read_po(buf, locale='fr', abort_invalid=True)
570+
buf.seek(0)
571+
output = pofile.read_po(buf, locale='fr', abort_invalid=False)
572+
assert isinstance(output, Catalog)
573+
574+
547575
def test_invalid_pofile_with_abort_flag(self):
548576
parser = pofile.PoFileParser(None, abort_invalid=True)
549577
lineno = 10

0 commit comments

Comments
 (0)