[ python-Bugs-1448060 ] gettext.py breaks on plural-forms header (PATCH)

SourceForge.net noreply at sourceforge.net
Mon Mar 20 05:07:54 CET 2006

Bugs item #1448060, was opened at 2006-03-12 00:20
Message generated for change (Comment added) made by dsegan
You can respond by visiting: 

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: Python 2.4
Status: Open
Resolution: None
Priority: 5
Submitted By: Danilo Segan (dsegan)
Assigned to: Nobody/Anonymous (nobody)
Summary: gettext.py breaks on plural-forms header (PATCH)

Initial Comment:
See http://bugzilla.gnome.org/show_bug.cgi?id=334256

The problem is a PO file like the following:

msgid ""
msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"
"Plural-Forms:  nplurals=2; plural=(n != 1);\n"
"#-#-#-#-#  plo.po (PACKAGE VERSION)  #-#-#-#-#\n"

(these kinds of entries are sometimes inserted by
msgmerge, so they're somewhat common)

Any Python program trying to access this breaks:

$ python test.py
Traceback (most recent call last):
  File "test.py", line 7, in ?
    gt = gettext.GNUTranslations(file)
  File "/usr/lib/python2.4/gettext.py", line 177, in
  File "/usr/lib/python2.4/gettext.py", line 300, in _parse
    v = v.split(';')
AttributeError: 'list' object has no attribute 'split'

test.py is simple:
#!/usr/bin/env python
import gettext
file = open("test.mo", "rb")
if file:
    gt = gettext.GNUTranslations(file)

The problem is the corner-case: plural-forms precedes
this kind of comment, so "v" is split (v=v.split(";")).
In the next instance, lastk is "plural-forms", yet the
entry doesn't contain ":", so it tries to set plural
forms to v.split(";") again, which fails since v is
already a list.

The attached simple patch fixes this.


>Comment By: Danilo Segan (dsegan)
Date: 2006-03-20 05:07

Logged In: YES 

Agreed on all points, except the "summary": multi-line
plural forms are actually supported and widely used.

Anyway, gettext.py should fail gracefully in case of any
problem in the header, instead of running into exception.


Comment By: Martin v. Löwis (loewis)
Date: 2006-03-19 12:51

Logged In: YES 

Several things seem to be going on here:

1. gettext.py is clearly wrong; it shouldn't break on that file.

2. it is trying to process multi-line fields here. So the
patch is also wrong, as it just sets k and v to None.

3. I believe that the PO file presented is also wrong. I
believe the intention of the header is that it should have
the RFC822 style syntax, which doesn't allow for # comment
lines. The tool should use a syntax like

X-Filename: plo.po; package=PACKAGE; version=VERSION;

To summarize, I think the attempt to process multi-line
fields in the header is misguided, and gettext.py should
just fetch the first line of content-type and plural-forms.


You can respond by visiting: 

More information about the Python-bugs-list mailing list