Skip to content

Commit 8b8bde4

Browse files
bpo-29935: Fixed error messages in the index() method of tuple, list and deque (#887) (#907) (#909)
when pass indices of wrong type. (cherry picked from commit d4edfc9) (cherry picked from commit bf4bb2e)
1 parent c90ff1b commit 8b8bde4

File tree

6 files changed

+31
-11
lines changed

6 files changed

+31
-11
lines changed

Include/ceval.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ PyAPI_FUNC(unsigned long) _PyEval_GetSwitchInterval(void);
203203

204204
#ifndef Py_LIMITED_API
205205
PyAPI_FUNC(int) _PyEval_SliceIndex(PyObject *, Py_ssize_t *);
206+
PyAPI_FUNC(int) _PyEval_SliceIndexNotNone(PyObject *, Py_ssize_t *);
206207
PyAPI_FUNC(void) _PyEval_SignalAsyncExc(void);
207208
#endif
208209

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: XXXX-XX-XX
1010
Core and Builtins
1111
-----------------
1212

13+
- bpo-29935: Fixed error messages in the index() method of tuple, list and deque
14+
when pass indices of wrong type.
15+
1316
- bpo-28876: ``bool(range)`` works even if ``len(range)``
1417
raises :exc:`OverflowError`.
1518

Modules/_collectionsmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,8 +913,8 @@ deque_index(dequeobject *deque, PyObject *args)
913913
size_t start_state = deque->state;
914914

915915
if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
916-
_PyEval_SliceIndex, &start,
917-
_PyEval_SliceIndex, &stop))
916+
_PyEval_SliceIndexNotNone, &start,
917+
_PyEval_SliceIndexNotNone, &stop))
918918
return NULL;
919919
if (start < 0) {
920920
start += Py_SIZE(deque);

Objects/listobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,8 +2154,8 @@ listindex(PyListObject *self, PyObject *args)
21542154
PyObject *v;
21552155

21562156
if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
2157-
_PyEval_SliceIndex, &start,
2158-
_PyEval_SliceIndex, &stop))
2157+
_PyEval_SliceIndexNotNone, &start,
2158+
_PyEval_SliceIndexNotNone, &stop))
21592159
return NULL;
21602160
if (start < 0) {
21612161
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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5098,14 +5098,10 @@ ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
50985098
and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
50995099
Return 0 on error, 1 on success.
51005100
*/
5101-
/* Note: If v is NULL, return success without storing into *pi. This
5102-
is because_PyEval_SliceIndex() is called by apply_slice(), which can be
5103-
called by the SLICE opcode with v and/or w equal to NULL.
5104-
*/
51055101
int
51065102
_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
51075103
{
5108-
if (v != NULL) {
5104+
if (v != Py_None) {
51095105
Py_ssize_t x;
51105106
if (PyIndex_Check(v)) {
51115107
x = PyNumber_AsSsize_t(v, NULL);
@@ -5123,6 +5119,26 @@ _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
51235119
return 1;
51245120
}
51255121

5122+
int
5123+
_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
5124+
{
5125+
Py_ssize_t x;
5126+
if (PyIndex_Check(v)) {
5127+
x = PyNumber_AsSsize_t(v, NULL);
5128+
if (x == -1 && PyErr_Occurred())
5129+
return 0;
5130+
}
5131+
else {
5132+
PyErr_SetString(PyExc_TypeError,
5133+
"slice indices must be integers or "
5134+
"have an __index__ method");
5135+
return 0;
5136+
}
5137+
*pi = x;
5138+
return 1;
5139+
}
5140+
5141+
51265142
#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
51275143
"BaseException is not allowed"
51285144

0 commit comments

Comments
 (0)