summaryrefslogtreecommitdiff
path: root/Objects/sliceobject.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2006-08-24 00:41:19 +0000
committerGuido van Rossum <guido@python.org>2006-08-24 00:41:19 +0000
commit47b9ff6ba11fab4c90556357c437cb4feec1e853 (patch)
treeb5319f74e08caf3276275462b14372b4da9f7dee /Objects/sliceobject.c
parent9a6e62b947ebb5547ca9a164f6145a461b98d86a (diff)
downloadcpython-git-47b9ff6ba11fab4c90556357c437cb4feec1e853.tar.gz
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...
Diffstat (limited to 'Objects/sliceobject.c')
-rw-r--r--Objects/sliceobject.c70
1 files changed, 50 insertions, 20 deletions
diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c
index d8a24653a7..0075a4e3d0 100644
--- a/Objects/sliceobject.c
+++ b/Objects/sliceobject.c
@@ -280,25 +280,55 @@ static PyMethodDef slice_methods[] = {
{NULL, NULL}
};
-static int
-slice_compare(PySliceObject *v, PySliceObject *w)
+static PyObject *
+slice_richcompare(PyObject *v, PyObject *w, int op)
{
- int result = 0;
-
- if (v == w)
- return 0;
-
- 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;
+ PyObject *t1;
+ PyObject *t2;
+ PyObject *res;
+
+ 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;
+ }
+
+ 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 @@ PyTypeObject PySlice_Type = {
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 @@ PyTypeObject PySlice_Type = {
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 */