[Python-checkins] CVS: python/dist/src/Objects sliceobject.c,2.6,2.7

Guido van Rossum gvanrossum@users.sourceforge.net
Tue, 20 Mar 2001 04:41:36 -0800


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv12132

Modified Files:
	sliceobject.c 
Log Message:
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]


Index: sliceobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/sliceobject.c,v
retrieving revision 2.6
retrieving revision 2.7
diff -C2 -r2.6 -r2.7
*** sliceobject.c	2000/12/14 15:09:46	2.6
--- sliceobject.c	2001/03/20 12:41:34	2.7
***************
*** 156,160 ****
--- 156,180 ----
  }
  
+ 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)
***************
*** 167,174 ****
  	(getattrfunc)slice_getattr, /*tp_getattr*/
  	0,			/*tp_setattr*/
! 	0,		    /*tp_compare*/
! 	(reprfunc)slice_repr, /*tp_repr*/
  	0,			/*tp_as_number*/
! 	0,	    	/*tp_as_sequence*/
  	0,			/*tp_as_mapping*/
  };
--- 187,194 ----
  	(getattrfunc)slice_getattr, /*tp_getattr*/
  	0,			/*tp_setattr*/
! 	(cmpfunc)slice_compare, /*tp_compare*/
! 	(reprfunc)slice_repr,   /*tp_repr*/
  	0,			/*tp_as_number*/
! 	0,	    		/*tp_as_sequence*/
  	0,			/*tp_as_mapping*/
  };