]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-29935: Fixed error messages in the index() method of tuple and list (#887) (...
authorSerhiy Storchaka <storchaka@gmail.com>
Thu, 30 Mar 2017 17:32:18 +0000 (20:32 +0300)
committerGitHub <noreply@github.com>
Thu, 30 Mar 2017 17:32:18 +0000 (20:32 +0300)
when pass indices of wrong type.
(cherry picked from commit d4edfc9abffca965e76ebc5957a92031a4d6c4d4)
(cherry picked from commit bf4bb2e43030661e568d5d4b046e8b9351cc164c)

Include/ceval.h
Misc/NEWS
Objects/listobject.c
Objects/tupleobject.c
Python/ceval.c

index 3735f009f93522606239e8a5ef0676b37cf703d1..f46160158e52af7d9d84330ed32221c81eb3a563 100644 (file)
@@ -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
index 8a071901a5f0513f53c501b5fd46cced7e54c258..0ca08d6befbfbb709c33cee920761bb181506884 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -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.
 
index 8ee86c64132c2fc3296bb78e1fc710ec69855691..a71df7bd2a5bf3a1c8755ebaef80529b58a45499 100644 (file)
@@ -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);
index 550719f327c4a57583e5beb8347a7fed6c0f54c8..2495e95e37946b94e5dfc23d8e06a2b3db03697b 100644 (file)
@@ -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);
index 09836519988ae61c3b7f4e77aada8f0b42f9d113..cea503e56faadc108a8e91b7bde76117a6dba00a 100644 (file)
@@ -4689,7 +4689,7 @@ ext_call_fail:
 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,
@@ -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))