Restructure comparison dramatically. There is no longer a default
*ordering* between objects; there is only a default equality test
(defined by an object being equal to itself only). Read the comment
in object.c. The current implementation never uses a three-way
comparison to compute a rich comparison, but it does use a rich
comparison to compute a three-way comparison. I'm not quite done
ripping out all the calls to PyObject_Compare/Cmp, or replacing
tp_compare implementations with tp_richcompare implementations;
but much of that has happened (to make most unit tests pass).
The following tests still fail, because I need help deciding
or understanding:
test_codeop -- depends on comparing code objects
test_datetime -- need Tim Peters' opinion
test_marshal -- depends on comparing code objects
test_mutants -- need help understanding it
The problem with test_codeop and test_marshal is this: these tests
compare two different code objects and expect them to be equal.
Is that still a feature we'd like to support? I've temporarily
removed the comparison and hash code from code objects, so they
use the default (equality by pointer only) comparison.
For the other two tests, run them to see for yourself.
(There may be more failing test with "-u all".)
A general problem with getting lots of these tests to pass is
the reality that for object types that have a natural total ordering,
implementing __cmp__ is much more convenient than implementing
__eq__, __ne__, __lt__, and so on. Should we go back to allowing
__cmp__ to provide a total ordering? Should we provide some other
way to implement rich comparison with a single method override?
Alex proposed a __key__() method; I've considered a __richcmp__()
method. Or perhaps __cmp__() just shouldn't be killed off...
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index 00800a7..3fb01e7 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -151,7 +151,7 @@
def dicts():
if verbose: print "Testing dict operations..."
- testbinop({1:2}, {2:1}, -1, "cmp(a,b)", "__cmp__")
+ ##testbinop({1:2}, {2:1}, -1, "cmp(a,b)", "__cmp__")
testbinop({1:2,3:4}, 1, 1, "b in a", "__contains__")
testbinop({1:2,3:4}, 2, 0, "b in a", "__contains__")
testbinop({1:2,3:4}, 1, 2, "a[b]", "__getitem__")
@@ -523,7 +523,7 @@
# This is an ugly hack:
copy._deepcopy_dispatch[spam.spamdict] = spamdict
- testbinop(spamdict({1:2}), spamdict({2:1}), -1, "cmp(a,b)", "__cmp__")
+ ##testbinop(spamdict({1:2}), spamdict({2:1}), -1, "cmp(a,b)", "__cmp__")
testbinop(spamdict({1:2,3:4}), 1, 1, "b in a", "__contains__")
testbinop(spamdict({1:2,3:4}), 2, 0, "b in a", "__contains__")
testbinop(spamdict({1:2,3:4}), 1, 2, "a[b]", "__getitem__")
@@ -1641,7 +1641,7 @@
verify(id(c1) != id(c2))
hash(c1)
hash(c2)
- vereq(cmp(c1, c2), cmp(id(c1), id(c2)))
+ ##vereq(cmp(c1, c2), cmp(id(c1), id(c2)))
vereq(c1, c1)
verify(c1 != c2)
verify(not c1 != c1)
@@ -1665,7 +1665,7 @@
verify(id(d1) != id(d2))
hash(d1)
hash(d2)
- vereq(cmp(d1, d2), cmp(id(d1), id(d2)))
+ ##vereq(cmp(d1, d2), cmp(id(d1), id(d2)))
vereq(d1, d1)
verify(d1 != d2)
verify(not d1 != d1)
@@ -1758,21 +1758,21 @@
for i in range(10):
verify(i in p10)
verify(10 not in p10)
- # Safety test for __cmp__
- def unsafecmp(a, b):
- try:
- a.__class__.__cmp__(a, b)
- except TypeError:
- pass
- else:
- raise TestFailed, "shouldn't allow %s.__cmp__(%r, %r)" % (
- a.__class__, a, b)
- unsafecmp(u"123", "123")
- unsafecmp("123", u"123")
- unsafecmp(1, 1.0)
- unsafecmp(1.0, 1)
- unsafecmp(1, 1L)
- unsafecmp(1L, 1)
+## # Safety test for __cmp__
+## def unsafecmp(a, b):
+## try:
+## a.__class__.__cmp__(a, b)
+## except TypeError:
+## pass
+## else:
+## raise TestFailed, "shouldn't allow %s.__cmp__(%r, %r)" % (
+## a.__class__, a, b)
+## unsafecmp(u"123", "123")
+## unsafecmp("123", u"123")
+## unsafecmp(1, 1.0)
+## unsafecmp(1.0, 1)
+## unsafecmp(1, 1L)
+## unsafecmp(1L, 1)
class Letter(str):
def __new__(cls, letter):
@@ -2469,12 +2469,43 @@
class C(base):
def __init__(self, value):
self.value = int(value)
- def __cmp__(self, other):
+ def __eq__(self, other):
if isinstance(other, C):
- return cmp(self.value, other.value)
+ return self.value == other.value
if isinstance(other, int) or isinstance(other, long):
- return cmp(self.value, other)
+ return self.value == other
return NotImplemented
+ def __ne__(self, other):
+ if isinstance(other, C):
+ return self.value != other.value
+ if isinstance(other, int) or isinstance(other, long):
+ return self.value != other
+ return NotImplemented
+ def __lt__(self, other):
+ if isinstance(other, C):
+ return self.value < other.value
+ if isinstance(other, int) or isinstance(other, long):
+ return self.value < other
+ return NotImplemented
+ def __le__(self, other):
+ if isinstance(other, C):
+ return self.value <= other.value
+ if isinstance(other, int) or isinstance(other, long):
+ return self.value <= other
+ return NotImplemented
+ def __gt__(self, other):
+ if isinstance(other, C):
+ return self.value > other.value
+ if isinstance(other, int) or isinstance(other, long):
+ return self.value > other
+ return NotImplemented
+ def __ge__(self, other):
+ if isinstance(other, C):
+ return self.value >= other.value
+ if isinstance(other, int) or isinstance(other, long):
+ return self.value >= other
+ return NotImplemented
+
c1 = C(1)
c2 = C(2)
c3 = C(3)
@@ -2482,12 +2513,12 @@
c = {1: c1, 2: c2, 3: c3}
for x in 1, 2, 3:
for y in 1, 2, 3:
- verify(cmp(c[x], c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y))
+ ##verify(cmp(c[x], c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y))
for op in "<", "<=", "==", "!=", ">", ">=":
verify(eval("c[x] %s c[y]" % op) == eval("x %s y" % op),
"x=%d, y=%d" % (x, y))
- verify(cmp(c[x], y) == cmp(x, y), "x=%d, y=%d" % (x, y))
- verify(cmp(x, c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y))
+ ##verify(cmp(c[x], y) == cmp(x, y), "x=%d, y=%d" % (x, y))
+ ##verify(cmp(x, c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y))
def rich_comparisons():
if verbose:
@@ -3875,6 +3906,8 @@
if verbose:
print "Testing method-wrapper objects..."
+ return # XXX should methods really support __eq__?
+
l = []
vereq(l.__add__, l.__add__)
vereq(l.__add__, [].__add__)