Use == rather than cmp(). The return value of cmp() is not well defined when

comparing different types.
This commit is contained in:
Neil Schemenauer 2001-01-03 02:13:26 +00:00
parent 38796d07a5
commit e7e694fd22
2 changed files with 130 additions and 128 deletions

View File

@ -1,122 +1,122 @@
test_compare test_compare
cmp(2, 2) = 0 2 == 2
cmp(2, 2.2) = -1 2 == 2.0
cmp(2, 2) = 0 2 == 2
cmp(2, (2+4j)) = -1 2 == (2+0j)
cmp(2, [1]) = -108 2 != [1]
cmp(2, (2,)) = -116 2 != (3,)
cmp(2, None) = -78 2 != None
cmp(2, <Empty>) = -1 2 != <Empty>
cmp(2, <Coerce 3>) = -1 2 == <Coerce 2>
cmp(2, <Cmp 4>) = -1 2 == <Cmp 2.0>
cmp(2, <RCmp 5>) = -1 2 != <RCmp 2>
cmp(2.2, 2) = 1 2.0 == 2
cmp(2.2, 2.2) = 0 2.0 == 2.0
cmp(2.2, 2) = 1 2.0 == 2
cmp(2.2, (2+4j)) = 1 2.0 == (2+0j)
cmp(2.2, [1]) = -108 2.0 != [1]
cmp(2.2, (2,)) = -116 2.0 != (3,)
cmp(2.2, None) = -78 2.0 != None
cmp(2.2, <Empty>) = -1 2.0 != <Empty>
cmp(2.2, <Coerce 3>) = -1 2.0 == <Coerce 2>
cmp(2.2, <Cmp 4>) = -1 2.0 == <Cmp 2.0>
cmp(2.2, <RCmp 5>) = -1 2.0 != <RCmp 2>
cmp(2, 2) = 0 2 == 2
cmp(2, 2.2) = -1 2 == 2.0
cmp(2, 2) = 0 2 == 2
cmp(2, (2+4j)) = -1 2 == (2+0j)
cmp(2, [1]) = -108 2 != [1]
cmp(2, (2,)) = -116 2 != (3,)
cmp(2, None) = -78 2 != None
cmp(2, <Empty>) = -1 2 != <Empty>
cmp(2, <Coerce 3>) = -1 2 == <Coerce 2>
cmp(2, <Cmp 4>) = -1 2 == <Cmp 2.0>
cmp(2, <RCmp 5>) = -1 2 != <RCmp 2>
cmp((2+4j), 2) = 1 (2+0j) == 2
cmp((2+4j), 2.2) = -1 (2+0j) == 2.0
cmp((2+4j), 2) = 1 (2+0j) == 2
cmp((2+4j), (2+4j)) = 0 (2+0j) == (2+0j)
cmp((2+4j), [1]) = -108 (2+0j) != [1]
cmp((2+4j), (2,)) = -116 (2+0j) != (3,)
cmp((2+4j), None) = -78 (2+0j) != None
cmp((2+4j), <Empty>) = -1 (2+0j) != <Empty>
cmp((2+4j), <Coerce 3>) = -1 (2+0j) == <Coerce 2>
cmp((2+4j), <Cmp 4>) = -1 (2+0j) == <Cmp 2.0>
cmp((2+4j), <RCmp 5>) = -1 (2+0j) != <RCmp 2>
cmp([1], 2) = 108 [1] != 2
cmp([1], 2.2) = 108 [1] != 2.0
cmp([1], 2) = 108 [1] != 2
cmp([1], (2+4j)) = 108 [1] != (2+0j)
cmp([1], [1]) = 0 [1] == [1]
cmp([1], (2,)) = -8 [1] != (3,)
cmp([1], None) = 30 [1] != None
cmp([1], <Empty>) = -1 [1] != <Empty>
cmp([1], <Coerce 3>) = 1 [1] != <Coerce 2>
cmp([1], <Cmp 4>) = 1 [1] != <Cmp 2.0>
cmp([1], <RCmp 5>) = -1 [1] != <RCmp 2>
cmp((2,), 2) = 116 (3,) != 2
cmp((2,), 2.2) = 116 (3,) != 2.0
cmp((2,), 2) = 116 (3,) != 2
cmp((2,), (2+4j)) = 116 (3,) != (2+0j)
cmp((2,), [1]) = 8 (3,) != [1]
cmp((2,), (2,)) = 0 (3,) == (3,)
cmp((2,), None) = 38 (3,) != None
cmp((2,), <Empty>) = -1 (3,) != <Empty>
cmp((2,), <Coerce 3>) = 1 (3,) != <Coerce 2>
cmp((2,), <Cmp 4>) = 1 (3,) != <Cmp 2.0>
cmp((2,), <RCmp 5>) = -1 (3,) != <RCmp 2>
cmp(None, 2) = 78 None != 2
cmp(None, 2.2) = 78 None != 2.0
cmp(None, 2) = 78 None != 2
cmp(None, (2+4j)) = 78 None != (2+0j)
cmp(None, [1]) = -30 None != [1]
cmp(None, (2,)) = -38 None != (3,)
cmp(None, None) = 0 None == None
cmp(None, <Empty>) = -1 None != <Empty>
cmp(None, <Coerce 3>) = 1 None != <Coerce 2>
cmp(None, <Cmp 4>) = 1 None != <Cmp 2.0>
cmp(None, <RCmp 5>) = -1 None != <RCmp 2>
cmp(<Empty>, 2) = 1 <Empty> != 2
cmp(<Empty>, 2.2) = 1 <Empty> != 2.0
cmp(<Empty>, 2) = 1 <Empty> != 2
cmp(<Empty>, (2+4j)) = 1 <Empty> != (2+0j)
cmp(<Empty>, [1]) = 1 <Empty> != [1]
cmp(<Empty>, (2,)) = 1 <Empty> != (3,)
cmp(<Empty>, None) = 1 <Empty> != None
cmp(<Empty>, <Empty>) = 0 <Empty> == <Empty>
cmp(<Empty>, <Coerce 3>) = -1 <Empty> != <Coerce 2>
cmp(<Empty>, <Cmp 4>) = -1 <Empty> != <Cmp 2.0>
cmp(<Empty>, <RCmp 5>) = 1 <Empty> != <RCmp 2>
cmp(<Coerce 3>, 2) = 1 <Coerce 2> == 2
cmp(<Coerce 3>, 2.2) = 1 <Coerce 2> == 2.0
cmp(<Coerce 3>, 2) = 1 <Coerce 2> == 2
cmp(<Coerce 3>, (2+4j)) = 1 <Coerce 2> == (2+0j)
cmp(<Coerce 3>, [1]) = -1 <Coerce 2> != [1]
cmp(<Coerce 3>, (2,)) = -1 <Coerce 2> != (3,)
cmp(<Coerce 3>, None) = -1 <Coerce 2> != None
cmp(<Coerce 3>, <Empty>) = 1 <Coerce 2> != <Empty>
cmp(<Coerce 3>, <Coerce 3>) = 0 <Coerce 2> == <Coerce 2>
cmp(<Coerce 3>, <Cmp 4>) = -1 <Coerce 2> != <Cmp 2.0>
cmp(<Coerce 3>, <RCmp 5>) = -1 <Coerce 2> == <RCmp 2>
cmp(<Cmp 4>, 2) = 1 <Cmp 2.0> == 2
cmp(<Cmp 4>, 2.2) = 1 <Cmp 2.0> == 2.0
cmp(<Cmp 4>, 2) = 1 <Cmp 2.0> == 2
cmp(<Cmp 4>, (2+4j)) = 1 <Cmp 2.0> == (2+0j)
cmp(<Cmp 4>, [1]) = -1 <Cmp 2.0> != [1]
cmp(<Cmp 4>, (2,)) = -1 <Cmp 2.0> != (3,)
cmp(<Cmp 4>, None) = -1 <Cmp 2.0> != None
cmp(<Cmp 4>, <Empty>) = -1 <Cmp 2.0> != <Empty>
cmp(<Cmp 4>, <Coerce 3>) = 1 <Cmp 2.0> == <Coerce 2>
cmp(<Cmp 4>, <Cmp 4>) = 0 <Cmp 2.0> == <Cmp 2.0>
cmp(<Cmp 4>, <RCmp 5>) = -1 <Cmp 2.0> != <RCmp 2>
cmp(<RCmp 5>, 2) = 1 <RCmp 2> != 2
cmp(<RCmp 5>, 2.2) = 1 <RCmp 2> != 2.0
cmp(<RCmp 5>, 2) = 1 <RCmp 2> != 2
cmp(<RCmp 5>, (2+4j)) = 1 <RCmp 2> != (2+0j)
cmp(<RCmp 5>, [1]) = 1 <RCmp 2> != [1]
cmp(<RCmp 5>, (2,)) = 1 <RCmp 2> != (3,)
cmp(<RCmp 5>, None) = 1 <RCmp 2> != None
cmp(<RCmp 5>, <Empty>) = 1 <RCmp 2> != <Empty>
cmp(<RCmp 5>, <Coerce 3>) = 1 <RCmp 2> != <Coerce 2>
cmp(<RCmp 5>, <Cmp 4>) = 1 <RCmp 2> != <Cmp 2.0>
cmp(<RCmp 5>, <RCmp 5>) = 0 <RCmp 2> == <RCmp 2>

View File

@ -17,7 +17,7 @@ class Coerce:
if isinstance(other, Coerce): if isinstance(other, Coerce):
return self.arg, other.arg return self.arg, other.arg
else: else:
return (self.arg, other) return self.arg, other
class Cmp: class Cmp:
def __init__(self,arg): def __init__(self,arg):
@ -40,18 +40,20 @@ class RCmp:
return cmp(other, self.arg) return cmp(other, self.arg)
candidates = [2, 2.2, 2L, 2+4j, [1], (2,), None, Empty(), Coerce(3), candidates = [2, 2.0, 2L, 2+0j, [1], (3,), None, Empty(), Coerce(2),
Cmp(4), RCmp(5)] Cmp(2.0), RCmp(2L)]
def test(): def test():
for a in candidates: for a in candidates:
for b in candidates: for b in candidates:
print "cmp(%s, %s)" % (a, b),
try: try:
x = cmp(a, b) x = a == b
except: except:
print '... %s' % sys.exc_info(0) print 'cmp(%s, %s) => %s' % (a, b, sys.exc_info()[0])
else: else:
print '=', x if x:
print "%s == %s" % (a, b)
else:
print "%s != %s" % (a, b)
test() test()