Skip to content

Commit 4d5c0f4

Browse files
committed
Python 3 built-in types can't be compared against None
1 parent bfb35db commit 4d5c0f4

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

roundup/hyperdb.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,51 @@
3333

3434
logger = logging.getLogger('roundup.hyperdb')
3535

36+
try:
37+
None < 0
38+
def _NoneComparable(v):
39+
return v
40+
except TypeError:
41+
class _NoneComparable(object):
42+
def __init__(self, value):
43+
self.value = value
44+
45+
def __cmp__(self, other):
46+
if not isinstance(other, self.__class__):
47+
raise TypeError('not comparable')
48+
49+
if self.value is None and other.value is None:
50+
return 0
51+
elif self.value is None:
52+
return -1
53+
elif other.value is None:
54+
return 1
55+
elif type(self.value) == type(()) and type(other.value) == type(()):
56+
for lhs, rhs in zip(self.value, other.value):
57+
result = _NoneComparable(lhs).__cmp__(_NoneComparable(rhs))
58+
if result != 0:
59+
return result
60+
return len(self.value) - len(other.value)
61+
elif self.value < other.value:
62+
return -1
63+
elif self.value > other.value:
64+
return 1
65+
else:
66+
return 0
67+
68+
def __eq__(self, other):
69+
return self.__cmp__(other) == 0
70+
def __ne__(self, other):
71+
return self.__cmp__(other) != 0
72+
def __lt__(self, other):
73+
return self.__cmp__(other) < 0
74+
def __le__(self, other):
75+
return self.__cmp__(other) <= 0
76+
def __ge__(self, other):
77+
return self.__cmp__(other) >= 0
78+
def __gt__(self, other):
79+
return self.__cmp__(other) > 0
80+
3681
#
3782
# Types
3883
#
@@ -606,7 +651,9 @@ def _sort(self, val):
606651
sortattr = zip (*sortattr)
607652
for dir, i in reversed(list(zip(directions, dir_idx))):
608653
rev = dir == '-'
609-
sortattr = sorted (sortattr, key = lambda x:x[i:idx], reverse = rev)
654+
sortattr = sorted (sortattr,
655+
key = lambda x: _NoneComparable(x[i:idx]),
656+
reverse = rev)
610657
idx = i
611658
return [x[-1] for x in sortattr]
612659

0 commit comments

Comments
 (0)