Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Lib/gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,10 @@ def _parse(self, fp):
self._charset = v.split('charset=')[1]
elif k == 'plural-forms':
v = v.split(';')
plural = v[1].split('plural=')[1]
try:
plural = v[1].split('plural=')[1]
except IndexError as e:
raise ValueError('invalid plural forms syntax') from e
self.plural = c2py(plural)
# Note: we unconditionally convert both msgids and msgstrs to
# Unicode using the character encoding specified in the charset
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,22 @@ def test_ignore_comments_in_headers_issue36239(self):
t = gettext.GNUTranslations(fp)
self.assertEqual(t.info()["plural-forms"], "nplurals=2; plural=(n != 1);")

def test_raise_descriptive_error_for_incorrect_plural_forms(self):
with open(MOFILE, 'wb') as fp:
# below is msgfmt run on such a PO file:
# msgid ""
# msgstr ""
# "Content-Type: text/plain; charset=UTF-8\n"
# "Plural-Forms: \n"
fp.write(
b'\xde\x12\x04\x95\x00\x00\x00\x00\x01\x00\x00\x00\x1c\x00\x00\x00$\x00\x00\x00\x03\x00\x00\x00,\x00'
b'\x00\x00\x00\x00\x00\x008\x00\x00\x007\x00\x00\x009\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00Content-Type: text/plain; charset=UTF-8\nPlural-Forms: \n\x00'
)
with self.assertRaisesRegex(ValueError, "invalid plural forms syntax"):
with open(MOFILE, 'rb') as fp:
gettext.GNUTranslations(fp)


class UnicodeTranslationsTest(GettextBaseTest):
def setUp(self):
Expand Down
Loading