From: Aarni Koskela Date: Thu, 24 Dec 2015 18:06:28 +0000 (+0200) Subject: pofile: Use `.sort(key=...)`, not `cmp` X-Git-Tag: 2.3.1~23^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=edce8eeaa594e277d3abb29fb419cf6e4fefb52a;p=thirdparty%2Fbabel.git pofile: Use `.sort(key=...)`, not `cmp` Fixes #79 (https://github.com/python-babel/babel/issues/79) --- diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py index dfc78fec..3c18226d 100644 --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -431,7 +431,7 @@ def write_po(fileobj, catalog, width=76, no_location=False, omit_header=False, 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 4e991c63..f375a1a4 100644 --- a/tests/messages/test_pofile.py +++ b/tests/messages/test_pofile.py @@ -513,6 +513,15 @@ msgstr[0] "Voh" 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)],