[Python-checkins] CVS: python/dist/src/Python ceval.c,2.268,2.269

Guido van Rossum gvanrossum@users.sourceforge.net
Sat, 18 Aug 2001 10:43:38 -0700


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

Modified Files:
	ceval.c 
Log Message:
Fix SF bug #443600:

Change to get/set/del slice operations so that if the object doesn't
support slicing, *or* if either of the slice arguments is not an int
or long, we construct a slice object and call the get/set/del item
operation instead.  This makes it possible to design classes that
support slice arguments of non-integral types.


Index: ceval.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/ceval.c,v
retrieving revision 2.268
retrieving revision 2.269
diff -C2 -d -r2.268 -r2.269
*** ceval.c	2001/08/17 20:47:47	2.268
--- ceval.c	2001/08/18 17:43:36	2.269
***************
*** 3348,3360 ****
  }
  
  static PyObject *
  apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
  {
! 	int ilow = 0, ihigh = INT_MAX;
! 	if (!_PyEval_SliceIndex(v, &ilow))
! 		return NULL;
! 	if (!_PyEval_SliceIndex(w, &ihigh))
! 		return NULL;
! 	return PySequence_GetSlice(u, ilow, ihigh);
  }
  
--- 3348,3375 ----
  }
  
+ #undef ISINT
+ #define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
+ 
  static PyObject *
  apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
  {
! 	PyTypeObject *tp = u->ob_type;
! 	PySequenceMethods *sq = tp->tp_as_sequence;
! 
! 	if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
! 		int ilow = 0, ihigh = INT_MAX;
! 		if (!_PyEval_SliceIndex(v, &ilow))
! 			return NULL;
! 		if (!_PyEval_SliceIndex(w, &ihigh))
! 			return NULL;
! 		return PySequence_GetSlice(u, ilow, ihigh);
! 	}
! 	else {
! 		PyObject *slice = PySlice_New(v, w, NULL);
! 		if (slice != NULL)
! 			return PyObject_GetItem(u, slice);
! 		else
! 			return NULL;
! 	}
  }
  
***************
*** 3363,3375 ****
  	/* u[v:w] = x */
  {
! 	int ilow = 0, ihigh = INT_MAX;
! 	if (!_PyEval_SliceIndex(v, &ilow))
! 		return -1;
! 	if (!_PyEval_SliceIndex(w, &ihigh))
! 		return -1;
! 	if (x == NULL)
! 		return PySequence_DelSlice(u, ilow, ihigh);
! 	else
! 		return PySequence_SetSlice(u, ilow, ihigh, x);
  }
  
--- 3378,3406 ----
  	/* u[v:w] = x */
  {
! 	PyTypeObject *tp = u->ob_type;
! 	PySequenceMethods *sq = tp->tp_as_sequence;
! 
! 	if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
! 		int ilow = 0, ihigh = INT_MAX;
! 		if (!_PyEval_SliceIndex(v, &ilow))
! 			return -1;
! 		if (!_PyEval_SliceIndex(w, &ihigh))
! 			return -1;
! 		if (x == NULL)
! 			return PySequence_DelSlice(u, ilow, ihigh);
! 		else
! 			return PySequence_SetSlice(u, ilow, ihigh, x);
! 	}
! 	else {
! 		PyObject *slice = PySlice_New(v, w, NULL);
! 		if (slice != NULL) {
! 			if (x != NULL)
! 				return PyObject_SetItem(u, slice, x);
! 			else
! 				return PyObject_DelItem(u, slice);
! 		}
! 		else
! 			return -1;
! 	}
  }