Skip to content

Commit f512131

Browse files
committed
Improve plural forms error message, add a test
1 parent f69c9ab commit f512131

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

Lib/gettext.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,8 @@ def _parse(self, fp):
419419
plural = v[1].split('plural=')[1]
420420
except IndexError:
421421
raise ValueError(
422-
f"expected ';' and 'plural=' in Plural-Forms metadata in {filename}, got {v!r}"
422+
f"expected ';' and 'plural=' in Plural-Forms metadata in {filename}, "
423+
f"got {';'.join(v)!r}"
423424
) from None
424425
self.plural = c2py(plural)
425426
# Note: we unconditionally convert both msgids and msgstrs to

Lib/test/test_gettext.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,8 @@ def test_raise_descriptive_error_for_incorrect_content_type(self):
677677
)
678678
with self.assertRaisesRegex(
679679
ValueError,
680-
"expected 'charset=' in Content-Type metadata in gettext.mo, got 'text/plain; charste=UTF-8'"
680+
"expected 'charset=' in Content-Type metadata in xx/LC_MESSAGES/gettext.mo, "
681+
"got 'text/plain; charste=UTF-8'"
681682
):
682683
with open(MOFILE, 'rb') as fp:
683684
gettext.GNUTranslations(fp)
@@ -687,15 +688,36 @@ def test_raise_descriptive_error_for_incorrect_plural_forms(self):
687688
# below is msgfmt run on such a PO file:
688689
# msgid ""
689690
# msgstr ""
690-
# "Content-Type: text/plain; charset=UTF-8\n"
691691
# "Plural-Forms: \n"
692692
fp.write(
693693
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'
694-
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'
695-
b'\x00\x00\x00\x00Content-Type: text/plain; charset=UTF-8\nPlural-Forms: \n\x00'
694+
b'\x00\x00\x00\x00\x00\x008\x00\x00\x00\x0f\x00\x00\x009\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00'
695+
b'\x00\x00\x00\x00\x00Plural-Forms: \n\x00'
696696
)
697697
with self.assertRaisesRegex(
698-
ValueError, "expected ';' and 'plural=' in Plural-Forms metadata in gettext.mo, got ''"
698+
ValueError,
699+
"expected ';' and 'plural=' in Plural-Forms metadata in xx/LC_MESSAGES/gettext.mo, got ''"
700+
):
701+
with open(MOFILE, 'rb') as fp:
702+
gettext.GNUTranslations(fp)
703+
704+
705+
def test_raise_descriptive_error_for_incorrect_plural_forms_with_semicolon(self):
706+
with open(MOFILE, 'wb') as fp:
707+
# below is msgfmt run on such a PO file:
708+
# msgid ""
709+
# msgstr ""
710+
# "Plural-Forms: nplurals=1; prulal=0;\n"
711+
fp.write(
712+
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'
713+
b'\x00\x00\x00\x00\x00\x008\x00\x00\x00$\x00\x00\x009\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00'
714+
b'\x00\x00\x00\x00Plural-Forms: nplurals=1; prulal=0;\n\x00'
715+
716+
)
717+
with self.assertRaisesRegex(
718+
ValueError,
719+
"expected ';' and 'plural=' in Plural-Forms metadata in xx/LC_MESSAGES/gettext.mo, "
720+
"got 'nplurals=1; prulal=0;'"
699721
):
700722
with open(MOFILE, 'rb') as fp:
701723
gettext.GNUTranslations(fp)

0 commit comments

Comments
 (0)