# HG changeset patch # User aronacher # Date 1213735236 0 # Node ID c82ad0f5ff6581dccc4495f733449674c36d8bf5 # Parent 9acf6b5baa220e4a3d296c2d8bd3eb8f7bf6daf1 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))