Skip to content

Commit

Permalink
attempt partial sorting at least
Browse files Browse the repository at this point in the history
see issue #606.  if one object has anything that doesn't compare to `int`, bring it to the top, and correctly sort the rest.
  • Loading branch information
Mario Frasca authored and akx committed May 28, 2019
1 parent 8b684d5 commit 38dfc8d
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions babel/messages/pofile.py
Expand Up @@ -582,11 +582,13 @@ def _write_message(message, prefix=''):
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

Expand Down

0 comments on commit 38dfc8d

Please sign in to comment.