From e7e694fd22fd68f39d359b3d1d5a1f0ebb4ec265 Mon Sep 17 00:00:00 2001 From: Neil Schemenauer Date: Wed, 3 Jan 2001 02:13:26 +0000 Subject: Use == rather than cmp(). The return value of cmp() is not well defined when comparing different types. --- Lib/test/output/test_compare | 242 +++++++++++++++++++++---------------------- Lib/test/test_compare.py | 16 +-- 2 files changed, 130 insertions(+), 128 deletions(-) diff --git a/Lib/test/output/test_compare b/Lib/test/output/test_compare index 4df9e49..8301233 100644 --- a/Lib/test/output/test_compare +++ b/Lib/test/output/test_compare @@ -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, ) = -1 -cmp(2, ) = -1 -cmp(2, ) = -1 -cmp(2, ) = -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, ) = -1 -cmp(2.2, ) = -1 -cmp(2.2, ) = -1 -cmp(2.2, ) = -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, ) = -1 -cmp(2, ) = -1 -cmp(2, ) = -1 -cmp(2, ) = -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), ) = -1 -cmp((2+4j), ) = -1 -cmp((2+4j), ) = -1 -cmp((2+4j), ) = -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], ) = -1 -cmp([1], ) = 1 -cmp([1], ) = 1 -cmp([1], ) = -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,), ) = -1 -cmp((2,), ) = 1 -cmp((2,), ) = 1 -cmp((2,), ) = -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, ) = -1 -cmp(None, ) = 1 -cmp(None, ) = 1 -cmp(None, ) = -1 -cmp(, 2) = 1 -cmp(, 2.2) = 1 -cmp(, 2) = 1 -cmp(, (2+4j)) = 1 -cmp(, [1]) = 1 -cmp(, (2,)) = 1 -cmp(, None) = 1 -cmp(, ) = 0 -cmp(, ) = -1 -cmp(, ) = -1 -cmp(, ) = 1 -cmp(, 2) = 1 -cmp(, 2.2) = 1 -cmp(, 2) = 1 -cmp(, (2+4j)) = 1 -cmp(, [1]) = -1 -cmp(, (2,)) = -1 -cmp(, None) = -1 -cmp(, ) = 1 -cmp(, ) = 0 -cmp(, ) = -1 -cmp(, ) = -1 -cmp(, 2) = 1 -cmp(, 2.2) = 1 -cmp(, 2) = 1 -cmp(, (2+4j)) = 1 -cmp(, [1]) = -1 -cmp(, (2,)) = -1 -cmp(, None) = -1 -cmp(, ) = -1 -cmp(, ) = 1 -cmp(, ) = 0 -cmp(, ) = -1 -cmp(, 2) = 1 -cmp(, 2.2) = 1 -cmp(, 2) = 1 -cmp(, (2+4j)) = 1 -cmp(, [1]) = 1 -cmp(, (2,)) = 1 -cmp(, None) = 1 -cmp(, ) = 1 -cmp(, ) = 1 -cmp(, ) = 1 -cmp(, ) = 0 +2 == 2 +2 == 2.0 +2 == 2 +2 == (2+0j) +2 != [1] +2 != (3,) +2 != None +2 != +2 == +2 == +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 != +2.0 == +2.0 == +2.0 != +2 == 2 +2 == 2.0 +2 == 2 +2 == (2+0j) +2 != [1] +2 != (3,) +2 != None +2 != +2 == +2 == +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) != +(2+0j) == +(2+0j) == +(2+0j) != +[1] != 2 +[1] != 2.0 +[1] != 2 +[1] != (2+0j) +[1] == [1] +[1] != (3,) +[1] != None +[1] != +[1] != +[1] != +[1] != +(3,) != 2 +(3,) != 2.0 +(3,) != 2 +(3,) != (2+0j) +(3,) != [1] +(3,) == (3,) +(3,) != None +(3,) != +(3,) != +(3,) != +(3,) != +None != 2 +None != 2.0 +None != 2 +None != (2+0j) +None != [1] +None != (3,) +None == None +None != +None != +None != +None != + != 2 + != 2.0 + != 2 + != (2+0j) + != [1] + != (3,) + != None + == + != + != + != + == 2 + == 2.0 + == 2 + == (2+0j) + != [1] + != (3,) + != None + != + == + != + == + == 2 + == 2.0 + == 2 + == (2+0j) + != [1] + != (3,) + != None + != + == + == + != + != 2 + != 2.0 + != 2 + != (2+0j) + != [1] + != (3,) + != None + != + != + != + == diff --git a/Lib/test/test_compare.py b/Lib/test/test_compare.py index d74f300..e8698a9 100644 --- a/Lib/test/test_compare.py +++ b/Lib/test/test_compare.py @@ -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() -- cgit v0.12