summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMario Frasca <mfrasca@zonnet.nl>2019-05-27 12:35:28 -0500
committerAarni Koskela <akx@iki.fi>2019-05-28 10:18:48 +0300
commit38dfc8dc87f869d4837cbe8338db53ae9e293f8f (patch)
tree07031f43973e27a9eccf1517b7666029d9571504
parent8b684d56e90d593d4f431263a6a3fea1aabc0d0c (diff)
downloadbabel-38dfc8dc87f869d4837cbe8338db53ae9e293f8f.tar.gz
attempt partial sorting at least
see issue #606. if one object has anything that doesn't compare to `int`, bring it to the top, and correctly sort the rest.
-rw-r--r--babel/messages/pofile.py10
1 files changed, 6 insertions, 4 deletions
diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py
index bbcf7f7..93b0697 100644
--- a/babel/messages/pofile.py
+++ b/babel/messages/pofile.py
@@ -582,11 +582,13 @@ def write_po(fileobj, catalog, width=76, no_location=False, omit_header=False,
if not no_location:
locs = []
- # Attempt to sort the locations. If we can't do that, for instance
- # because there are mixed integers and Nones or whatnot (see issue #606)
- # then give up, but also don't just crash.
+ # sort locations by filename and lineno.
+ # if there's no <int> as lineno, use `-1`.
+ # if no sorting possible, leave unsorted.
+ # (see issue #606)
try:
- locations = sorted(message.locations)
+ locations = sorted(message.locations,
+ key=lambda x: (x[0], isinstance(x[1], int) and x[1] or -1))
except TypeError: # e.g. "TypeError: unorderable types: NoneType() < int()"
locations = message.locations