[Python-checkins] CVS: python/dist/src/Objects stringobject.c,2.64,2.65

Guido van Rossum python-dev@python.org
Mon, 8 May 2000 10:08:08 -0400 (EDT)


Update of /projects/cvsroot/python/dist/src/Objects
In directory eric:/projects/python/develop/guido/src/Objects

Modified Files:
	stringobject.c 
Log Message:
Trent Mick:

Fix the string methods that implement slice-like semantics with
optional args (count, find, endswith, etc.) to properly handle
indeces outside [INT_MIN, INT_MAX]. Previously the "i" formatter
for PyArg_ParseTuple was used to get the indices. These could overflow.

This patch changes the string methods to use the "O&" formatter with
the slice_index() function from ceval.c which is used to do the same
job for Python code slices (e.g. 'abcabcabc'[0:1000000000L]). slice_index()
is renamed _PyEval_SliceIndex() and is now exported. As well, the return
values for success/fail were changed to make slice_index directly
usable as required by the "O&" formatter.

[GvR: shouldn't a similar patch be applied to unicodeobject.c?]


Index: stringobject.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.64
retrieving revision 2.65
diff -C2 -r2.64 -r2.65
*** stringobject.c	2000/05/05 20:44:24	2.64
--- stringobject.c	2000/05/08 14:08:05	2.65
***************
*** 823,828 ****
  	PyObject *subobj;
  
! 	if (!PyArg_ParseTuple(args, "O|ii:find/rfind/index/rindex", 
! 			      &subobj, &i, &last))
  		return -2;
  	if (PyString_Check(subobj)) {
--- 823,828 ----
  	PyObject *subobj;
  
! 	if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex", 
! 		&subobj, _PyEval_SliceIndex, &i, _PyEval_SliceIndex, &last))
  		return -2;
  	if (PyString_Check(subobj)) {
***************
*** 1195,1200 ****
  	PyObject *subobj;
  
! 	if (!PyArg_ParseTuple(args, "O|ii:count", &subobj, &i, &last))
  		return NULL;
  	if (PyString_Check(subobj)) {
  		sub = PyString_AS_STRING(subobj);
--- 1195,1202 ----
  	PyObject *subobj;
  
! 	if (!PyArg_ParseTuple(args, "O|O&O&:count", &subobj,
! 		_PyEval_SliceIndex, &i, _PyEval_SliceIndex, &last))
  		return NULL;
+ 
  	if (PyString_Check(subobj)) {
  		sub = PyString_AS_STRING(subobj);
***************
*** 1618,1622 ****
  	PyObject *subobj;
  
! 	if (!PyArg_ParseTuple(args, "O|ii:startswith", &subobj, &start, &end))
  		return NULL;
  	if (PyString_Check(subobj)) {
--- 1620,1625 ----
  	PyObject *subobj;
  
! 	if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj,
! 		_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
  		return NULL;
  	if (PyString_Check(subobj)) {
***************
*** 1672,1676 ****
  	PyObject *subobj;
  
! 	if (!PyArg_ParseTuple(args, "O|ii:endswith", &subobj, &start, &end))
  		return NULL;
  	if (PyString_Check(subobj)) {
--- 1675,1680 ----
  	PyObject *subobj;
  
! 	if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj,
! 		_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
  		return NULL;
  	if (PyString_Check(subobj)) {