diff Objects/stringobject.c @ 43886:25a5f6a30292 legacy-trunk

Now in find, rfind, index, and rindex, you can use None as defaults, as usual with slicing (both with str and unicode strings). This fixes issue 1259. For str only the stringobject.c file was modified. But for unicode, I needed to repeat in the four functions a lot of code, so created a new function that does part of the job for them (and placed it in find.h, following a suggestion of Barry). Also added tests for this behaviour.
author Facundo Batista <facundobatista@gmail.com>
date Fri, 16 Nov 2007 18:04:14 +0000
parents 7347f75b9c34
children ad8b80460644
line wrap: on
line diff
--- a/Objects/stringobject.c	Fri Nov 16 00:24:44 2007 +0000
+++ b/Objects/stringobject.c	Fri Nov 16 18:04:14 2007 +0000
@@ -1880,10 +1880,21 @@
 	const char *sub;
 	Py_ssize_t sub_len;
 	Py_ssize_t start=0, end=PY_SSIZE_T_MAX;
-
-	if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex", &subobj,
-		_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
+	PyObject *obj_start=Py_None, *obj_end=Py_None;
+
+	if (!PyArg_ParseTuple(args, "O|OO:find/rfind/index/rindex", &subobj,
+		&obj_start, &obj_end))
 		return -2;
+	/* To support None in "start" and "end" arguments, meaning
+	   the same as if they were not passed.
+	*/
+	if (obj_start != Py_None)
+		if (!_PyEval_SliceIndex(obj_start, &start))
+	        return -2;
+	if (obj_end != Py_None)
+		if (!_PyEval_SliceIndex(obj_end, &end))
+	        return -2;
+
 	if (PyString_Check(subobj)) {
 		sub = PyString_AS_STRING(subobj);
 		sub_len = PyString_GET_SIZE(subobj);