Skip to content

Commit 802ed89

Browse files
committed
Accept None as start and stop parameters for list.index() and tuple.index()
Closes #13340.
1 parent 165c178 commit 802ed89

5 files changed

Lines changed: 39 additions & 7 deletions

File tree

Lib/test/list_tests.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,13 @@ def test_index(self):
365365
self.assertEqual(u.index(0, 3), 3)
366366
self.assertEqual(u.index(0, 3, 4), 3)
367367
self.assertRaises(ValueError, u.index, 2, 0, -10)
368+
self.assertEqual(u.index(1, None), 4)
369+
self.assertEqual(u.index(1, None, None), 4)
370+
self.assertEqual(u.index(1, 0, None), 4)
371+
self.assertEqual(u.index(1, None, 6), 4)
372+
self.assertRaises(ValueError, u.index, -1, 3)
373+
self.assertRaises(ValueError, u.index, -1, 3, None)
374+
self.assertRaises(ValueError, u.index, 1, None, 4)
368375

369376
self.assertRaises(TypeError, u.index)
370377

Lib/test/seq_tests.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,13 @@ def test_index(self):
361361
self.assertEqual(u.index(0, 3), 3)
362362
self.assertEqual(u.index(0, 3, 4), 3)
363363
self.assertRaises(ValueError, u.index, 2, 0, -10)
364+
self.assertEqual(u.index(1, None), 4)
365+
self.assertEqual(u.index(1, None, None), 4)
366+
self.assertEqual(u.index(1, 0, None), 4)
367+
self.assertEqual(u.index(1, None, 6), 4)
368+
self.assertRaises(ValueError, u.index, -1, 3)
369+
self.assertRaises(ValueError, u.index, -1, 3, None)
370+
self.assertRaises(ValueError, u.index, 1, None, 4)
364371

365372
self.assertRaises(TypeError, u.index)
366373

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.2.3?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #13340: Accept None as start and stop parameters for
14+
list.index() and tuple.index().
15+
1316
- Issue #13343: Fix a SystemError when a lambda expression uses a global
1417
variable in the default value of a keyword-only argument:
1518
(lambda *, arg=GLOBAL_NAME: None)

Objects/listobject.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,12 +2109,20 @@ listindex(PyListObject *self, PyObject *args)
21092109
{
21102110
Py_ssize_t i, start=0, stop=Py_SIZE(self);
21112111
PyObject *v, *format_tuple, *err_string;
2112+
PyObject *start_obj = NULL, *stop_obj = NULL;
21122113
static PyObject *err_format = NULL;
21132114

2114-
if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
2115-
_PyEval_SliceIndex, &start,
2116-
_PyEval_SliceIndex, &stop))
2115+
if (!PyArg_ParseTuple(args, "O|OO:index", &v, &start_obj, &stop_obj))
21172116
return NULL;
2117+
2118+
if (start_obj != Py_None)
2119+
if (!_PyEval_SliceIndex(start_obj, &start))
2120+
return NULL;
2121+
2122+
if (stop_obj != Py_None)
2123+
if (!_PyEval_SliceIndex(stop_obj, &stop))
2124+
return NULL;
2125+
21182126
if (start < 0) {
21192127
start += Py_SIZE(self);
21202128
if (start < 0)

Objects/tupleobject.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -483,12 +483,19 @@ static PyObject *
483483
tupleindex(PyTupleObject *self, PyObject *args)
484484
{
485485
Py_ssize_t i, start=0, stop=Py_SIZE(self);
486-
PyObject *v;
486+
PyObject *v, *start_obj = NULL, *stop_obj = NULL;
487487

488-
if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
489-
_PyEval_SliceIndex, &start,
490-
_PyEval_SliceIndex, &stop))
488+
if (!PyArg_ParseTuple(args, "O|OO:index", &v, &start_obj, &stop_obj))
491489
return NULL;
490+
491+
if (start_obj != Py_None)
492+
if (!_PyEval_SliceIndex(start_obj, &start))
493+
return NULL;
494+
495+
if (stop_obj != Py_None)
496+
if (!_PyEval_SliceIndex(stop_obj, &stop))
497+
return NULL;
498+
492499
if (start < 0) {
493500
start += Py_SIZE(self);
494501
if (start < 0)

0 commit comments

Comments
 (0)