]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
pofile: don't crash when message.locations can't be sorted
authorAarni Koskela <akx@iki.fi>
Mon, 27 May 2019 08:24:33 +0000 (11:24 +0300)
committerAarni Koskela <akx@iki.fi>
Mon, 27 May 2019 08:37:12 +0000 (11:37 +0300)
Fixes #606

babel/messages/pofile.py

index 2943fa2ece6fe134e3019f2c3c16c5a7220c66e5..9eb3a5cb311e9e13b1052f63de3cc8471c1bd5bb 100644 (file)
@@ -581,7 +581,16 @@ def write_po(fileobj, catalog, width=76, no_location=False, omit_header=False,
 
         if not no_location:
             locs = []
-            for filename, lineno in sorted(message.locations):
+
+            # 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.
+            try:
+                locations = sorted(message.locations)
+            except TypeError:  # e.g. "TypeError: unorderable types: NoneType() < int()"
+                locations = message.locations
+
+            for filename, lineno in locations:
                 if lineno and include_lineno:
                     locs.append(u'%s:%d' % (filename.replace(os.sep, '/'), lineno))
                 else: