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/Objects/sliceobject.c b/Objects/sliceobject.c
index d8a2465..0075a4e 100644
--- a/Objects/sliceobject.c
+++ b/Objects/sliceobject.c
@@ -280,25 +280,55 @@
{NULL, NULL}
};
-static int
-slice_compare(PySliceObject *v, PySliceObject *w)
+static PyObject *
+slice_richcompare(PyObject *v, PyObject *w, int op)
{
- int result = 0;
+ PyObject *t1;
+ PyObject *t2;
+ PyObject *res;
- if (v == w)
- return 0;
+ if (v == w) {
+ /* XXX Do we really need this shortcut?
+ There's a unit test for it, but is that fair? */
+ switch (op) {
+ case Py_EQ:
+ case Py_LE:
+ case Py_GE:
+ res = Py_True;
+ break;
+ default:
+ res = Py_False;
+ break;
+ }
+ Py_INCREF(res);
+ return res;
+ }
- if (PyObject_Cmp(v->start, w->start, &result) < 0)
- return -2;
- if (result != 0)
- return result;
- if (PyObject_Cmp(v->stop, w->stop, &result) < 0)
- return -2;
- if (result != 0)
- return result;
- if (PyObject_Cmp(v->step, w->step, &result) < 0)
- return -2;
- return result;
+ t1 = PyTuple_New(3);
+ t2 = PyTuple_New(3);
+ if (t1 == NULL || t2 == NULL)
+ return NULL;
+
+ PyTuple_SET_ITEM(t1, 0, ((PySliceObject *)v)->start);
+ PyTuple_SET_ITEM(t1, 1, ((PySliceObject *)v)->stop);
+ PyTuple_SET_ITEM(t1, 2, ((PySliceObject *)v)->step);
+ PyTuple_SET_ITEM(t2, 0, ((PySliceObject *)w)->start);
+ PyTuple_SET_ITEM(t2, 1, ((PySliceObject *)w)->stop);
+ PyTuple_SET_ITEM(t2, 2, ((PySliceObject *)w)->step);
+
+ res = PyObject_RichCompare(t1, t2, op);
+
+ PyTuple_SET_ITEM(t1, 0, NULL);
+ PyTuple_SET_ITEM(t1, 1, NULL);
+ PyTuple_SET_ITEM(t1, 2, NULL);
+ PyTuple_SET_ITEM(t2, 0, NULL);
+ PyTuple_SET_ITEM(t2, 1, NULL);
+ PyTuple_SET_ITEM(t2, 2, NULL);
+
+ Py_DECREF(t1);
+ Py_DECREF(t2);
+
+ return res;
}
static long
@@ -318,7 +348,7 @@
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
- (cmpfunc)slice_compare, /* tp_compare */
+ 0, /* tp_compare */
(reprfunc)slice_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
@@ -333,7 +363,7 @@
slice_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
- 0, /* tp_richcompare */
+ slice_richcompare, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */