Skip to content

Commit 079f21f

Browse files
bpo-29935: Fixed error messages in the index() method of tuple and list (#887) (#907) (#910)
when pass indices of wrong type. (cherry picked from commit d4edfc9) (cherry picked from commit bf4bb2e)
1 parent 3ceca68 commit 079f21f

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

Include/ceval.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ PyAPI_FUNC(void) PyEval_ReInitThreads(void);
145145
#endif /* !WITH_THREAD */
146146

147147
PyAPI_FUNC(int) _PyEval_SliceIndex(PyObject *, Py_ssize_t *);
148+
PyAPI_FUNC(int) _PyEval_SliceIndexNotNone(PyObject *, Py_ssize_t *);
148149

149150

150151
#ifdef __cplusplus

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 2.7.14?
1010
Core and Builtins
1111
-----------------
1212

13+
- bpo-29935: Fixed error messages in the index() method of tuple and list
14+
when pass indices of wrong type.
15+
1316
- bpo-28598: Support __rmod__ for subclasses of str being called before
1417
str.__mod__. Patch by Martijn Pieters.
1518

Objects/listobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2284,8 +2284,8 @@ listindex(PyListObject *self, PyObject *args)
22842284
static PyObject *err_format = NULL;
22852285

22862286
if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
2287-
_PyEval_SliceIndex, &start,
2288-
_PyEval_SliceIndex, &stop))
2287+
_PyEval_SliceIndexNotNone, &start,
2288+
_PyEval_SliceIndexNotNone, &stop))
22892289
return NULL;
22902290
if (start < 0) {
22912291
start += Py_SIZE(self);

Objects/tupleobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,8 @@ tupleindex(PyTupleObject *self, PyObject *args)
515515
PyObject *v;
516516

517517
if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
518-
_PyEval_SliceIndex, &start,
519-
_PyEval_SliceIndex, &stop))
518+
_PyEval_SliceIndexNotNone, &start,
519+
_PyEval_SliceIndexNotNone, &stop))
520520
return NULL;
521521
if (start < 0) {
522522
start += Py_SIZE(self);

Python/ceval.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4689,7 +4689,7 @@ ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
46894689
int
46904690
_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
46914691
{
4692-
if (v != NULL) {
4692+
if (v != NULL && v != Py_None) {
46934693
Py_ssize_t x;
46944694
if (PyInt_Check(v)) {
46954695
/* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
@@ -4714,6 +4714,26 @@ _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
47144714
return 1;
47154715
}
47164716

4717+
int
4718+
_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
4719+
{
4720+
Py_ssize_t x;
4721+
if (PyIndex_Check(v)) {
4722+
x = PyNumber_AsSsize_t(v, NULL);
4723+
if (x == -1 && PyErr_Occurred())
4724+
return 0;
4725+
}
4726+
else {
4727+
PyErr_SetString(PyExc_TypeError,
4728+
"slice indices must be integers or "
4729+
"have an __index__ method");
4730+
return 0;
4731+
}
4732+
*pi = x;
4733+
return 1;
4734+
}
4735+
4736+
47174737
#undef ISINDEX
47184738
#define ISINDEX(x) ((x) == NULL || \
47194739
PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))

0 commit comments

Comments
 (0)