# HG changeset patch # User aronacher # Date 1213735236 0 # Node ID 4cdca48fc83227cd199d30e2db3ea8286fc9858a # Parent 9a2618ab9bbc07523e078ccd0396b57228424915 Fixed #59 by falling back silently on invalid location comments. diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -208,8 +208,13 @@ _add_message() if line[1:].startswith(':'): for location in line[2:].lstrip().split(): - filename, lineno = location.split(':', 1) - locations.append((filename, int(lineno))) + pos = location.rfind(':') + if pos >= 0: + try: + lineno = int(location[pos + 1:]) + except ValueError: + continue + locations.append((location[:pos], lineno)) elif line[1:].startswith(','): for flag in line[2:].lstrip().split(','): flags.append(flag.strip()) diff --git a/babel/messages/tests/pofile.py b/babel/messages/tests/pofile.py --- a/babel/messages/tests/pofile.py +++ b/babel/messages/tests/pofile.py @@ -380,6 +380,19 @@ msgstr[1] "Voeh"''' in value assert value.find('msgid ""') < value.find('msgid "bar"') < value.find('msgid "foo"') + def test_silent_location_fallback(self): + buf = StringIO('''\ +#: broken_file.py +msgid "missing line number" +msgstr "" + +#: broken_file.py:broken_line_number +msgid "broken line number" +msgstr ""''') + catalog = pofile.read_po(buf) + self.assertEqual(catalog.get('missing line number').locations, []) + self.assertEqual(catalog.get('broken line number').locations, []) + def suite(): suite = unittest.TestSuite() suite.addTest(doctest.DocTestSuite(pofile))