changeset 5466:09966de4d3f6

Python 3 built-in types can't be compared against None
author Christof Meerwald <cmeerw@cmeerw.org>
date Sat, 28 Jul 2018 20:34:31 +0100
parents c4f9a152258d
children 630a00b20394
files roundup/hyperdb.py
diffstat 1 files changed, 48 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/roundup/hyperdb.py	Sat Jul 28 20:21:59 2018 +0100
+++ b/roundup/hyperdb.py	Sat Jul 28 20:34:31 2018 +0100
@@ -33,6 +33,51 @@
 
 logger = logging.getLogger('roundup.hyperdb')
 
+try:
+    None < 0
+    def _NoneComparable(v):
+        return v
+except TypeError:
+    class _NoneComparable(object):
+        def __init__(self, value):
+            self.value = value
+
+        def __cmp__(self, other):
+            if not isinstance(other, self.__class__):
+                raise TypeError('not comparable')
+
+            if self.value is None and other.value is None:
+                return 0
+            elif self.value is None:
+                return -1
+            elif other.value is None:
+                return 1
+            elif type(self.value) == type(()) and type(other.value) == type(()):
+                for lhs, rhs in zip(self.value, other.value):
+                    result = _NoneComparable(lhs).__cmp__(_NoneComparable(rhs))
+                    if result != 0:
+                        return result
+                return len(self.value) - len(other.value)
+            elif self.value < other.value:
+                return -1
+            elif self.value > other.value:
+                return 1
+            else:
+                return 0
+
+        def __eq__(self, other):
+            return self.__cmp__(other) == 0
+        def __ne__(self, other):
+            return self.__cmp__(other) != 0
+        def __lt__(self, other):
+            return self.__cmp__(other) < 0
+        def __le__(self, other):
+            return self.__cmp__(other) <= 0
+        def __ge__(self, other):
+            return self.__cmp__(other) >= 0
+        def __gt__(self, other):
+            return self.__cmp__(other) > 0
+
 #
 # Types
 #
@@ -606,7 +651,9 @@
         sortattr = zip (*sortattr)
         for dir, i in reversed(list(zip(directions, dir_idx))):
             rev = dir == '-'
-            sortattr = sorted (sortattr, key = lambda x:x[i:idx], reverse = rev)
+            sortattr = sorted (sortattr,
+                               key = lambda x: _NoneComparable(x[i:idx]),
+                               reverse = rev)
             idx = i
         return [x[-1] for x in sortattr]
 

Roundup Issue Tracker: http://roundup-tracker.org/