python-babel / babel

The official repository for Babel, the Python Internationalization Library
http://babel.pocoo.org/
BSD 3-Clause "New" or "Revised" License
1.34k stars 448 forks source link

Can't parse POT files with plurals generated by xgettext #1154

Open lheckemann opened 2 weeks ago

lheckemann commented 2 weeks ago

Overview Description

xgettext generates a plural-forms header with all-caps strings as placeholders: Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n

pofile.read_po chokes on these.

Steps to Reproduce

xgettext --output=- - --language=C <<<'ngettext("Hello World!", "Hello Worlds!", 3);' | python3 -c 'import sys; from babel.messages import pofile; pofile.read_po(sys.stdin)'

Actual Results

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/nix/store/wpv3wmn4j889lm2sabq8294vap8gwmri-python3-3.11.10-env/lib/python3.11/site-packages/babel/messages/pofile.py", line 387, in read_po
    parser.parse(fileobj)
  File "/nix/store/wpv3wmn4j889lm2sabq8294vap8gwmri-python3-3.11.10-env/lib/python3.11/site-packages/babel/messages/pofile.py", line 310, in parse
    self._process_comment(line)
  File "/nix/store/wpv3wmn4j889lm2sabq8294vap8gwmri-python3-3.11.10-env/lib/python3.11/site-packages/babel/messages/pofile.py", line 269, in _process_comment
    self._finish_current_message()
  File "/nix/store/wpv3wmn4j889lm2sabq8294vap8gwmri-python3-3.11.10-env/lib/python3.11/site-packages/babel/messages/pofile.py", line 206, in _finish_current_message
    self._add_message()
  File "/nix/store/wpv3wmn4j889lm2sabq8294vap8gwmri-python3-3.11.10-env/lib/python3.11/site-packages/babel/messages/pofile.py", line 200, in _add_message
    self.catalog[msgid] = message
    ~~~~~~~~~~~~^^^^^^^
  File "/nix/store/wpv3wmn4j889lm2sabq8294vap8gwmri-python3-3.11.10-env/lib/python3.11/site-packages/babel/messages/catalog.py", line 686, in __setitem__
    self.mime_headers = message_from_string(message.string).items()
    ^^^^^^^^^^^^^^^^^
  File "/nix/store/wpv3wmn4j889lm2sabq8294vap8gwmri-python3-3.11.10-env/lib/python3.11/site-packages/babel/messages/catalog.py", line 495, in _set_mime_headers
    self._num_plurals = int(params.get('nplurals', 2))
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: 'INTEGER'

Expected Results

Parsing the PO template successfully, even if the plural specification is invalid.

Reproducibility

Seems to be entirely deterministic.

Additional Information