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

View File

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