From 819d8d447da7e2a8530574af2a7574fdc011b0c2 Mon Sep 17 00:00:00 2001 From: Petri Lehtinen Date: Sat, 5 Nov 2011 23:18:06 +0200 Subject: [PATCH] Accept None as start and stop parameters for list.index() and tuple.index() Closes #13340. --- Lib/test/list_tests.py | 7 +++++++ Lib/test/seq_tests.py | 7 +++++++ Misc/NEWS | 3 +++ Objects/listobject.c | 14 +++++++++++--- Objects/tupleobject.c | 15 +++++++++++---- 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/Lib/test/list_tests.py b/Lib/test/list_tests.py index b8d6d6bda6..9528052e31 100644 --- a/Lib/test/list_tests.py +++ b/Lib/test/list_tests.py @@ -365,6 +365,13 @@ class CommonTest(seq_tests.CommonTest): self.assertEqual(u.index(0, 3), 3) self.assertEqual(u.index(0, 3, 4), 3) self.assertRaises(ValueError, u.index, 2, 0, -10) + self.assertEqual(u.index(1, None), 4) + self.assertEqual(u.index(1, None, None), 4) + self.assertEqual(u.index(1, 0, None), 4) + self.assertEqual(u.index(1, None, 6), 4) + self.assertRaises(ValueError, u.index, -1, 3) + self.assertRaises(ValueError, u.index, -1, 3, None) + self.assertRaises(ValueError, u.index, 1, None, 4) self.assertRaises(TypeError, u.index) diff --git a/Lib/test/seq_tests.py b/Lib/test/seq_tests.py index f5e4e0ef55..0c8cf903a8 100644 --- a/Lib/test/seq_tests.py +++ b/Lib/test/seq_tests.py @@ -363,6 +363,13 @@ class CommonTest(unittest.TestCase): self.assertEqual(u.index(0, 3), 3) self.assertEqual(u.index(0, 3, 4), 3) self.assertRaises(ValueError, u.index, 2, 0, -10) + self.assertEqual(u.index(1, None), 4) + self.assertEqual(u.index(1, None, None), 4) + self.assertEqual(u.index(1, 0, None), 4) + self.assertEqual(u.index(1, None, 6), 4) + self.assertRaises(ValueError, u.index, -1, 3) + self.assertRaises(ValueError, u.index, -1, 3, None) + self.assertRaises(ValueError, u.index, 1, None, 4) self.assertRaises(TypeError, u.index) diff --git a/Misc/NEWS b/Misc/NEWS index d053a2b090..2f7b54e4e5 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -9,6 +9,9 @@ What's New in Python 2.7.3? Core and Builtins ----------------- +- Issue #13340: Accept None as start and stop parameters for + list.index() and tuple.index(). + - Issue #10519: Avoid unnecessary recursive function calls in setobject.c. diff --git a/Objects/listobject.c b/Objects/listobject.c index f753643bd3..10d9c009c1 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -2277,12 +2277,20 @@ listindex(PyListObject *self, PyObject *args) { Py_ssize_t i, start=0, stop=Py_SIZE(self); PyObject *v, *format_tuple, *err_string; + PyObject *start_obj = NULL, *stop_obj = NULL; static PyObject *err_format = NULL; - if (!PyArg_ParseTuple(args, "O|O&O&:index", &v, - _PyEval_SliceIndex, &start, - _PyEval_SliceIndex, &stop)) + if (!PyArg_ParseTuple(args, "O|OO:index", &v, &start_obj, &stop_obj)) return NULL; + + if (start_obj != Py_None) + if (!_PyEval_SliceIndex(start_obj, &start)) + return NULL; + + if (stop_obj != Py_None) + if (!_PyEval_SliceIndex(stop_obj, &stop)) + return NULL; + if (start < 0) { start += Py_SIZE(self); if (start < 0) diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index 3249cccdb7..1f2ab552ae 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -510,12 +510,19 @@ static PyObject * tupleindex(PyTupleObject *self, PyObject *args) { Py_ssize_t i, start=0, stop=Py_SIZE(self); - PyObject *v; + PyObject *v, *start_obj = NULL, *stop_obj = NULL; - if (!PyArg_ParseTuple(args, "O|O&O&:index", &v, - _PyEval_SliceIndex, &start, - _PyEval_SliceIndex, &stop)) + if (!PyArg_ParseTuple(args, "O|OO:index", &v, &start_obj, &stop_obj)) return NULL; + + if (start_obj != Py_None) + if (!_PyEval_SliceIndex(start_obj, &start)) + return NULL; + + if (stop_obj != Py_None) + if (!_PyEval_SliceIndex(stop_obj, &stop)) + return NULL; + if (start < 0) { start += Py_SIZE(self); if (start < 0) -- 2.40.0