diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py index dfc78fecb..3c18226da 100644 --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -431,7 +431,7 @@ def _write_message(message, prefix=''): if sort_output: messages.sort() elif sort_by_file: - messages.sort(lambda x,y: cmp(x.locations, y.locations)) + messages.sort(key=lambda m: m.locations) for message in messages: if not message.id: # This is the header "message" diff --git a/tests/messages/test_pofile.py b/tests/messages/test_pofile.py index 4e991c63c..f375a1a42 100644 --- a/tests/messages/test_pofile.py +++ b/tests/messages/test_pofile.py @@ -513,6 +513,15 @@ def test_sorted_po(self): msgstr[1] "Voeh"''' in value assert value.find(b'msgid ""') < value.find(b'msgid "bar"') < value.find(b'msgid "foo"') + def test_file_sorted_po(self): + catalog = Catalog() + catalog.add(u'bar', locations=[('utils.py', 3)]) + catalog.add((u'foo', u'foos'), (u'Voh', u'Voeh'), locations=[('main.py', 1)]) + buf = BytesIO() + pofile.write_po(buf, catalog, sort_by_file=True) + value = buf.getvalue().strip() + assert value.find(b'main.py') < value.find(b'utils.py') + def test_file_with_no_lineno(self): catalog = Catalog() catalog.add(u'bar', locations=[('utils.py', None)],