summaryrefslogtreecommitdiff
path: root/Objects/sliceobject.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2001-03-20 12:41:34 +0000
committerGuido van Rossum <guido@python.org>2001-03-20 12:41:34 +0000
commit8933d057484f27ccac49184948061e9679d9190c (patch)
treeaff3620d03314a3a89ad7b454287efefb1650f2d /Objects/sliceobject.c
parent0a666a6245ed657778f31f5a9c272fa5532de138 (diff)
downloadcpython-8933d057484f27ccac49184948061e9679d9190c.tar.gz
SF patch #408326 by Robin Thomas: slice objects comparable, not
hashable This patch changes the behavior of slice objects in the following manner: - Slice objects are now comparable with other slice objects as though they were logically tuples of (start,stop,step). The tuple is not created in the comparison function, but the comparison behavior is logically equivalent. - Slice objects are not hashable. With the above change to being comparable, slice objects now cannot be used as keys in dictionaries. [I've edited the patch for style. Note that this fixes the problem that dict[i:j] seemed to work but was meaningless. --GvR]
Diffstat (limited to 'Objects/sliceobject.c')
-rw-r--r--Objects/sliceobject.c26
1 files changed, 23 insertions, 3 deletions
diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c
index bb94e42311..6f7a6d818b 100644
--- a/Objects/sliceobject.c
+++ b/Objects/sliceobject.c
@@ -155,6 +155,26 @@ static PyObject *slice_getattr(PySliceObject *self, char *name)
return ret;
}
+static int
+slice_compare(PySliceObject *v, PySliceObject *w)
+{
+ 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;
+}
PyTypeObject PySlice_Type = {
PyObject_HEAD_INIT(&PyType_Type)
@@ -166,9 +186,9 @@ PyTypeObject PySlice_Type = {
0, /*tp_print*/
(getattrfunc)slice_getattr, /*tp_getattr*/
0, /*tp_setattr*/
- 0, /*tp_compare*/
- (reprfunc)slice_repr, /*tp_repr*/
+ (cmpfunc)slice_compare, /*tp_compare*/
+ (reprfunc)slice_repr, /*tp_repr*/
0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
+ 0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
};