Skip to content

Commit bf4bb2e

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

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
@@ -216,6 +216,7 @@ PyAPI_FUNC(Py_ssize_t) _PyEval_RequestCodeExtraIndex(freefunc);
216216

217217
#ifndef Py_LIMITED_API
218218
PyAPI_FUNC(int) _PyEval_SliceIndex(PyObject *, Py_ssize_t *);
219+
PyAPI_FUNC(int) _PyEval_SliceIndexNotNone(PyObject *, Py_ssize_t *);
219220
PyAPI_FUNC(void) _PyEval_SignalAsyncExc(void);
220221
#endif
221222

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.6.2 release candidate 1?
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-29859: Show correct error messages when any of the pthread_* calls in
1417
thread_pthread.h fails.
1518

Modules/_collectionsmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,8 +1051,8 @@ deque_index(dequeobject *deque, PyObject *args)
10511051
int cmp;
10521052

10531053
if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
1054-
_PyEval_SliceIndex, &start,
1055-
_PyEval_SliceIndex, &stop))
1054+
_PyEval_SliceIndexNotNone, &start,
1055+
_PyEval_SliceIndexNotNone, &stop))
10561056
return NULL;
10571057
if (start < 0) {
10581058
start += Py_SIZE(deque);

Objects/listobject.c

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

21552155
if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
2156-
_PyEval_SliceIndex, &start,
2157-
_PyEval_SliceIndex, &stop))
2156+
_PyEval_SliceIndexNotNone, &start,
2157+
_PyEval_SliceIndexNotNone, &stop))
21582158
return NULL;
21592159
if (start < 0) {
21602160
start += Py_SIZE(self);

Objects/tupleobject.c

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

524524
if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
525-
_PyEval_SliceIndex, &start,
526-
_PyEval_SliceIndex, &stop))
525+
_PyEval_SliceIndexNotNone, &start,
526+
_PyEval_SliceIndexNotNone, &stop))
527527
return NULL;
528528
if (start < 0) {
529529
start += Py_SIZE(self);

Python/ceval.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5074,14 +5074,10 @@ do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
50745074
and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
50755075
Return 0 on error, 1 on success.
50765076
*/
5077-
/* Note: If v is NULL, return success without storing into *pi. This
5078-
is because_PyEval_SliceIndex() is called by apply_slice(), which can be
5079-
called by the SLICE opcode with v and/or w equal to NULL.
5080-
*/
50815077
int
50825078
_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
50835079
{
5084-
if (v != NULL) {
5080+
if (v != Py_None) {
50855081
Py_ssize_t x;
50865082
if (PyIndex_Check(v)) {
50875083
x = PyNumber_AsSsize_t(v, NULL);
@@ -5099,6 +5095,26 @@ _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
50995095
return 1;
51005096
}
51015097

5098+
int
5099+
_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
5100+
{
5101+
Py_ssize_t x;
5102+
if (PyIndex_Check(v)) {
5103+
x = PyNumber_AsSsize_t(v, NULL);
5104+
if (x == -1 && PyErr_Occurred())
5105+
return 0;
5106+
}
5107+
else {
5108+
PyErr_SetString(PyExc_TypeError,
5109+
"slice indices must be integers or "
5110+
"have an __index__ method");
5111+
return 0;
5112+
}
5113+
*pi = x;
5114+
return 1;
5115+
}
5116+
5117+
51025118
#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
51035119
"BaseException is not allowed"
51045120

0 commit comments

Comments
 (0)