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):
410
410
self ._charset = v .split ('charset=' )[1 ]
411
411
elif k == 'plural-forms' :
412
412
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
414
417
self .plural = c2py (plural )
415
418
# Note: we unconditionally convert both msgids and msgstrs to
416
419
# Unicode using the character encoding specified in the charset
Original file line number Diff line number Diff line change 1
1
import os
2
2
import base64
3
- import gettext
3
+ from Lib import gettext
4
4
import unittest
5
5
from functools import partial
6
6
@@ -624,6 +624,22 @@ def test_ignore_comments_in_headers_issue36239(self):
624
624
t = gettext .GNUTranslations (fp )
625
625
self .assertEqual (t .info ()["plural-forms" ], "nplurals=2; plural=(n != 1);" )
626
626
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
+
627
643
628
644
class UnicodeTranslationsTest (GettextBaseTest ):
629
645
def setUp (self ):
You can’t perform that action at this time.
0 commit comments