From 3ef3ee7fdcdfb468441bfbe243c513345c70ce64 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Sat, 9 Nov 2013 08:45:04 +0100 Subject: [PATCH] add another straight exception case to macros --- Cython/Utility/ObjectHandling.c | 9 ++++++--- tests/run/index.pyx | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/Cython/Utility/ObjectHandling.c b/Cython/Utility/ObjectHandling.c index 6296721..5e72ea5 100644 --- a/Cython/Utility/ObjectHandling.c +++ b/Cython/Utility/ObjectHandling.c @@ -250,7 +250,8 @@ static PyObject *__Pyx_PyDict_GetItem(PyObject *d, PyObject* key) { #define __Pyx_GetItemInt(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck) \ (__Pyx_fits_Py_ssize_t(i, type, is_signed) ? \ __Pyx_GetItemInt_Fast(o, (Py_ssize_t)i, is_list, wraparound, boundscheck) : \ - __Pyx_GetItemInt_Generic(o, to_py_func(i))) + (is_list ? (PyErr_SetString(PyExc_IndexError, "list index out of range"), NULL) : \ + __Pyx_GetItemInt_Generic(o, to_py_func(i)))) {{for type in ['List', 'Tuple']}} #define __Pyx_GetItemInt_{{type}}(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck) \ @@ -343,7 +344,8 @@ static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i, #define __Pyx_SetItemInt(o, i, v, type, is_signed, to_py_func, is_list, wraparound, boundscheck) \ (__Pyx_fits_Py_ssize_t(i, type, is_signed) ? \ __Pyx_SetItemInt_Fast(o, (Py_ssize_t)i, v, is_list, wraparound, boundscheck) : \ - __Pyx_SetItemInt_Generic(o, to_py_func(i), v)) + (is_list ? (PyErr_SetString(PyExc_IndexError, "list assignment index out of range"), -1) : \ + __Pyx_SetItemInt_Generic(o, to_py_func(i), v))) static CYTHON_INLINE int __Pyx_SetItemInt_Generic(PyObject *o, PyObject *j, PyObject *v); static CYTHON_INLINE int __Pyx_SetItemInt_Fast(PyObject *o, Py_ssize_t i, PyObject *v, @@ -408,7 +410,8 @@ static CYTHON_INLINE int __Pyx_SetItemInt_Fast(PyObject *o, Py_ssize_t i, PyObje #define __Pyx_DelItemInt(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck) \ (__Pyx_fits_Py_ssize_t(i, type, is_signed) ? \ __Pyx_DelItemInt_Fast(o, (Py_ssize_t)i, is_list, wraparound) : \ - __Pyx_DelItem_Generic(o, to_py_func(i))) + (is_list ? (PyErr_SetString(PyExc_IndexError, "list assignment index out of range"), -1) : \ + __Pyx_DelItem_Generic(o, to_py_func(i)))) static CYTHON_INLINE int __Pyx_DelItem_Generic(PyObject *o, PyObject *j); static CYTHON_INLINE int __Pyx_DelItemInt_Fast(PyObject *o, Py_ssize_t i, diff --git a/tests/run/index.pyx b/tests/run/index.pyx index 6f068da..9a1d12f 100644 --- a/tests/run/index.pyx +++ b/tests/run/index.pyx @@ -75,6 +75,44 @@ def index_object(object o, int i): return o[i] +def del_index_list(list L, Py_ssize_t index): + """ + >>> del_index_list(list(range(4)), 0) + [1, 2, 3] + >>> del_index_list(list(range(4)), 1) + [0, 2, 3] + >>> del_index_list(list(range(4)), -1) + [0, 1, 2] + >>> del_index_list(list(range(4)), py_maxsize) + Traceback (most recent call last): + IndexError: list assignment index out of range + >>> del_index_list(list(range(4)), -py_maxsize) + Traceback (most recent call last): + IndexError: list assignment index out of range + """ + del L[index] + return L + + +def set_index_list(list L, Py_ssize_t index): + """ + >>> set_index_list(list(range(4)), 0) + [5, 1, 2, 3] + >>> set_index_list(list(range(4)), 1) + [0, 5, 2, 3] + >>> set_index_list(list(range(4)), -1) + [0, 1, 2, 5] + >>> set_index_list(list(range(4)), py_maxsize) + Traceback (most recent call last): + IndexError: list assignment index out of range + >>> set_index_list(list(range(4)), -py_maxsize) + Traceback (most recent call last): + IndexError: list assignment index out of range + """ + L[index] = 5 + return L + + # These make sure that our fast indexing works with large and unsigned types. def test_unsigned_long(): -- 2.7.4