diff --git a/Lib/gettext.py b/Lib/gettext.py --- a/Lib/gettext.py +++ b/Lib/gettext.py @@ -48,7 +48,7 @@ import locale, copy, io, os, re, struct, sys from errno import ENOENT - +import warnings __all__ = ['NullTranslations', 'GNUTranslations', 'Catalog', 'find', 'translation', 'install', 'textdomain', 'bindtextdomain', @@ -293,7 +293,14 @@ self._charset = v.split('charset=')[1] elif k == 'plural-forms': v = v.split(';') - plural = v[1].split('plural=')[1] + if len(v) < 2: + warnings.warn('plural-forms header is empty') + continue + v = v[1].split('plural=') + if len(v) < 2: + warnings.warn('"plural=" not found') + continue + plural = v[1] self.plural = c2py(plural) # Note: we unconditionally convert both msgids and msgstrs to # Unicode using the character encoding specified in the charset diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py --- a/Lib/test/test_gettext.py +++ b/Lib/test/test_gettext.py @@ -81,6 +81,12 @@ ciBUQUgKdHJnZ3JrZyB6cmZmbnRyIHBuZ255YnQgeXZvZW5lbC4AYmFjb24Ad2luayB3aW5rAA== ''' +GNU_MO_DATA_ISSUE_12425 = b'''\ +3hIElQAAAAABAAAAHAAAACQAAAAAAAAAAAAAAAAAAAAsAAAAeQAAAC0AAAAAUGx1cmFsLUZvcm1z +OiBucGx1cmFscz0yOyBwbHVyYWw7CiMtIy0jLSMtIyAgbWVzc2FnZXMucG8gKEVkWCBTdHVkaW8p +ICAjLSMtIy0jLSMKQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PVVURi04CgA= +''' + GNU_MO_DATA_ISSUE_17898 = b'''\ 3hIElQAAAAABAAAAHAAAACQAAAAAAAAAAAAAAAAAAAAsAAAAggAAAC0AAAAAUGx1cmFsLUZvcm1z OiBucGx1cmFscz0yOyBwbHVyYWw9KG4gIT0gMSk7CiMtIy0jLSMtIyAgbWVzc2FnZXMucG8gKEVk @@ -367,6 +373,14 @@ raises(ValueError, gettext.c2py, "os.chmod('/etc/passwd',0777)") class GNUTranslationParsingTest(GettextBaseTest): + def test_plural_form_warning_issue12425(self): + with open(MOFILE, 'wb') as fp: + fp.write(base64.decodebytes(GNU_MO_DATA_ISSUE_12425)) + with open(MOFILE, 'rb') as fp: + # If this runs cleanly and issues a warning, the bug is fixed. + with self.assertWarns(Warning): + t = gettext.GNUTranslations(fp) + def test_plural_form_error_issue17898(self): with open(MOFILE, 'wb') as fp: fp.write(base64.decodebytes(GNU_MO_DATA_ISSUE_17898)) @@ -562,3 +576,16 @@ "#-#-#-#-# messages.po (EdX Studio) #-#-#-#-#\n" "Content-Type: text/plain; charset=UTF-8\n" ''' + +# +# messages.po, used for bug 12425 +# + +''' +# test file for http://bugs.python.org/issue12425 +msgid "" +msgstr "" +"Plural-Forms: nplurals=2; plural;\n" +"#-#-#-#-# messages.po (EdX Studio) #-#-#-#-#\n" +"Content-Type: text/plain; charset=UTF-8\n" +'''