]> granicus.if.org Git - python/commitdiff
bpo-29935: Fixed error messages in the index() method of tuple, list and deque (...
authorSerhiy Storchaka <storchaka@gmail.com>
Thu, 30 Mar 2017 17:31:46 +0000 (20:31 +0300)
committerGitHub <noreply@github.com>
Thu, 30 Mar 2017 17:31:46 +0000 (20:31 +0300)
when pass indices of wrong type.
(cherry picked from commit d4edfc9abffca965e76ebc5957a92031a4d6c4d4)
(cherry picked from commit bf4bb2e43030661e568d5d4b046e8b9351cc164c)

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

index b5373a9cc474f8d23a0bed7b4b80e2b1bdc47b33..3f84b0658159ad6c0eb194f78f437967846ff45e 100644 (file)
@@ -203,6 +203,7 @@ PyAPI_FUNC(unsigned long) _PyEval_GetSwitchInterval(void);
 
 #ifndef Py_LIMITED_API
 PyAPI_FUNC(int) _PyEval_SliceIndex(PyObject *, Py_ssize_t *);
+PyAPI_FUNC(int) _PyEval_SliceIndexNotNone(PyObject *, Py_ssize_t *);
 PyAPI_FUNC(void) _PyEval_SignalAsyncExc(void);
 #endif
 
index 749f3e34997a2cdbd0101f01f866ed094b7b31f0..e640622f0d86a1a86e6c2b8282d22d4165064dbd 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ Release date: XXXX-XX-XX
 Core and Builtins
 -----------------
 
+- bpo-29935: Fixed error messages in the index() method of tuple, list and deque
+  when pass indices of wrong type.
+
 - bpo-28876: ``bool(range)`` works even if ``len(range)``
   raises :exc:`OverflowError`.
 
index ac81680e1bd7101945bbea82e3756a5832660b58..08fcecad9adc00f2154978ef51176b8a97407bff 100644 (file)
@@ -913,8 +913,8 @@ deque_index(dequeobject *deque, PyObject *args)
     size_t start_state = deque->state;
 
     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(deque);
index 815a1b9ea2d80b17f8c015aa05abcd145c25b6bb..e1e3cf018aaa7b8eeba49efacb62ec0d4ef989cb 100644 (file)
@@ -2154,8 +2154,8 @@ listindex(PyListObject *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 7920fec2bd86e8854f110afe8818e85737a66fc6..48719454fe65379c7b8f59bbdf13a8725e2959d9 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 9ae8653f7e4e84580b5b5200180485e2cebdd2c9..3070a90b43dbfbc361edff833c9d6a28d7187fd0 100644 (file)
@@ -5098,14 +5098,10 @@ ext_call_fail:
    and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
    Return 0 on error, 1 on success.
 */
-/* Note:  If v is NULL, return success without storing into *pi.  This
-   is because_PyEval_SliceIndex() is called by apply_slice(), which can be
-   called by the SLICE opcode with v and/or w equal to NULL.
-*/
 int
 _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
 {
-    if (v != NULL) {
+    if (v != Py_None) {
         Py_ssize_t x;
         if (PyIndex_Check(v)) {
             x = PyNumber_AsSsize_t(v, NULL);
@@ -5123,6 +5119,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;
+}
+
+
 #define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
                          "BaseException is not allowed"