Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1 Include/ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ PyAPI_FUNC(void) PyEval_ReInitThreads(void);
#endif /* !WITH_THREAD */

PyAPI_FUNC(int) _PyEval_SliceIndex(PyObject *, Py_ssize_t *);
PyAPI_FUNC(int) _PyEval_SliceIndexNotNone(PyObject *, Py_ssize_t *);


#ifdef __cplusplus
Expand Down
3 changes: 3 additions & 0 deletions 3 Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ What's New in Python 2.7.14?
Core and Builtins
-----------------

- bpo-29935: Fixed error messages in the index() method of tuple and list
when pass indices of wrong type.

- bpo-28598: Support __rmod__ for subclasses of str being called before
str.__mod__. Patch by Martijn Pieters.

Expand Down
4 changes: 2 additions & 2 deletions 4 Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2284,8 +2284,8 @@ listindex(PyListObject *self, PyObject *args)
static PyObject *err_format = NULL;

if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
_PyEval_SliceIndex, &start,
_PyEval_SliceIndex, &stop))
_PyEval_SliceIndexNotNone, &start,
_PyEval_SliceIndexNotNone, &stop))
return NULL;
if (start < 0) {
start += Py_SIZE(self);
Expand Down
4 changes: 2 additions & 2 deletions 4 Objects/tupleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,8 @@ tupleindex(PyTupleObject *self, PyObject *args)
PyObject *v;

if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
_PyEval_SliceIndex, &start,
_PyEval_SliceIndex, &stop))
_PyEval_SliceIndexNotNone, &start,
_PyEval_SliceIndexNotNone, &stop))
return NULL;
if (start < 0) {
start += Py_SIZE(self);
Expand Down
22 changes: 21 additions & 1 deletion 22 Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -4689,7 +4689,7 @@ ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
int
_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
{
if (v != NULL) {
if (v != NULL && v != Py_None) {
Py_ssize_t x;
if (PyInt_Check(v)) {
/* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
Expand All @@ -4714,6 +4714,26 @@ _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
return 1;
}

int
_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
{
Py_ssize_t x;
if (PyIndex_Check(v)) {
x = PyNumber_AsSsize_t(v, NULL);
if (x == -1 && PyErr_Occurred())
return 0;
}
else {
PyErr_SetString(PyExc_TypeError,
"slice indices must be integers or "
"have an __index__ method");
return 0;
}
*pi = x;
return 1;
}


#undef ISINDEX
#define ISINDEX(x) ((x) == NULL || \
PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.