]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
attempt partial sorting at least
authorMario Frasca <mfrasca@zonnet.nl>
Mon, 27 May 2019 17:35:28 +0000 (12:35 -0500)
committerAarni Koskela <akx@iki.fi>
Tue, 28 May 2019 07:18:48 +0000 (10:18 +0300)
see issue #606.  if one object has anything that doesn't compare to `int`, bring it to the top, and correctly sort the rest.

babel/messages/pofile.py

index bbcf7f76c41d6db8f01470763a7200af54f5d6d7..93b0697c6886f3910c7abf532ad374e14236c56d 100644 (file)
@@ -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