File tree Expand file tree Collapse file tree 2 files changed +21
-2
lines changed
Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Original file line number Diff line number Diff line change @@ -410,7 +410,10 @@ def _parse(self, fp):
410410 self ._charset = v .split ('charset=' )[1 ]
411411 elif k == 'plural-forms' :
412412 v = v .split (';' )
413- plural = v [1 ].split ('plural=' )[1 ]
413+ try :
414+ plural = v [1 ].split ('plural=' )[1 ]
415+ except IndexError as e :
416+ raise ValueError ('invalid plural forms syntax' ) from e
414417 self .plural = c2py (plural )
415418 # Note: we unconditionally convert both msgids and msgstrs to
416419 # Unicode using the character encoding specified in the charset
Original file line number Diff line number Diff line change 11import os
22import base64
3- import gettext
3+ from Lib import gettext
44import unittest
55from functools import partial
66
@@ -624,6 +624,22 @@ def test_ignore_comments_in_headers_issue36239(self):
624624 t = gettext .GNUTranslations (fp )
625625 self .assertEqual (t .info ()["plural-forms" ], "nplurals=2; plural=(n != 1);" )
626626
627+ def test_gracefully_handle_incorrect_plural_forms (self ):
628+ with open (MOFILE , 'wb' ) as fp :
629+ # below is msgfmt run on such a PO file:
630+ # msgid ""
631+ # msgstr ""
632+ # "Content-Type: text/plain; charset=UTF-8\n"
633+ # "Plural-Forms: \n"
634+ fp .write (
635+ 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 '
636+ b'\x00 \x00 \x00 \x00 \x00 \x00 8\x00 \x00 \x00 7\x00 \x00 \x00 9\x00 \x00 \x00 \x01 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 '
637+ b'\x00 \x00 \x00 \x00 Content-Type: text/plain; charset=UTF-8\n Plural-Forms: \n \x00 '
638+ )
639+ with self .assertRaisesRegex (ValueError , "invalid plural forms syntax" ):
640+ with open (MOFILE , 'rb' ) as fp :
641+ gettext .GNUTranslations (fp )
642+
627643
628644class UnicodeTranslationsTest (GettextBaseTest ):
629645 def setUp (self ):
You can’t perform that action at this time.
0 commit comments