From e41390aca51e4e3eb455cf3b70f5d656a2814db9 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 8 Apr 2017 11:48:57 +0300 Subject: bpo-27867: Expand the PySlice_GetIndicesEx macro. (#1023) (#1046) (cherry picked from commit b879fe8) --- Modules/_ctypes/_ctypes.c | 5 ++--- Modules/_elementtree.c | 12 ++++++------ Modules/arraymodule.c | 11 ++++++----- Modules/mmapmodule.c | 9 ++++----- Objects/bytearrayobject.c | 12 ++++++------ Objects/listobject.c | 10 ++++++---- Objects/memoryobject.c | 9 +++++---- Objects/sliceobject.c | 4 ++-- Objects/stringobject.c | 6 +++--- Objects/structseq.c | 6 +++--- Objects/tupleobject.c | 6 +++--- Objects/unicodeobject.c | 5 +++-- 12 files changed, 49 insertions(+), 46 deletions(-) diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index f301b13..7edd94c 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -4613,11 +4613,10 @@ Array_ass_subscript(PyObject *_self, PyObject *item, PyObject *value) else if (PySlice_Check(item)) { Py_ssize_t start, stop, step, slicelen, otherlen, i, cur; - if (PySlice_GetIndicesEx((PySliceObject *)item, - self->b_length, &start, &stop, - &step, &slicelen) < 0) { + if (_PySlice_Unpack((PySliceObject *)item, &start, &stop, &step) < 0) { return -1; } + slicelen = _PySlice_AdjustIndices(self->b_length, &start, &stop, step); if ((step < 0 && start < stop) || (step > 0 && start > stop)) stop = start; diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index d7f75c9..71ffc58 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -1334,11 +1334,11 @@ element_subscr(PyObject* self_, PyObject* item) if (!self->extra) return PyList_New(0); - if (PySlice_GetIndicesEx((PySliceObject *)item, - self->extra->length, - &start, &stop, &step, &slicelen) < 0) { + if (_PySlice_Unpack((PySliceObject *)item, &start, &stop, &step) < 0) { return NULL; } + slicelen = _PySlice_AdjustIndices(self->extra->length, &start, &stop, + step); if (slicelen <= 0) return PyList_New(0); @@ -1393,11 +1393,11 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value) if (!self->extra) element_new_extra(self, NULL); - if (PySlice_GetIndicesEx((PySliceObject *)item, - self->extra->length, - &start, &stop, &step, &slicelen) < 0) { + if (_PySlice_Unpack((PySliceObject *)item, &start, &stop, &step) < 0) { return -1; } + slicelen = _PySlice_AdjustIndices(self->extra->length, &start, &stop, + step); assert(slicelen <= self->extra->length); if (value == NULL) diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 9819141..310ce7a 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -1701,10 +1701,11 @@ array_subscr(arrayobject* self, PyObject* item) arrayobject* ar; int itemsize = self->ob_descr->itemsize; - if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self), - &start, &stop, &step, &slicelength) < 0) { + if (_PySlice_Unpack((PySliceObject *)item, &start, &stop, &step) < 0) { return NULL; } + slicelength = _PySlice_AdjustIndices(Py_SIZE(self), &start, &stop, + step); if (slicelength <= 0) { return newarrayobject(&Arraytype, 0, self->ob_descr); @@ -1772,11 +1773,11 @@ array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value) return (*self->ob_descr->setitem)(self, i, value); } else if (PySlice_Check(item)) { - if (PySlice_GetIndicesEx((PySliceObject *)item, - Py_SIZE(self), &start, &stop, - &step, &slicelength) < 0) { + if (_PySlice_Unpack((PySliceObject *)item, &start, &stop, &step) < 0) { return -1; } + slicelength = _PySlice_AdjustIndices(Py_SIZE(self), &start, &stop, + step); } else { PyErr_SetString(PyExc_TypeError, diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index 0dc4805..4f4c959 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -784,10 +784,10 @@ mmap_subscript(mmap_object *self, PyObject *item) else if (PySlice_Check(item)) { Py_ssize_t start, stop, step, slicelen; - if (PySlice_GetIndicesEx((PySliceObject *)item, self->size, - &start, &stop, &step, &slicelen) < 0) { + if (_PySlice_Unpack((PySliceObject *)item, &start, &stop, &step) < 0) { return NULL; } + slicelen = _PySlice_AdjustIndices(self->size, &start, &stop, step); if (slicelen <= 0) return PyString_FromStringAndSize("", 0); @@ -939,11 +939,10 @@ mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value) else if (PySlice_Check(item)) { Py_ssize_t start, stop, step, slicelen; - if (PySlice_GetIndicesEx((PySliceObject *)item, - self->size, &start, &stop, - &step, &slicelen) < 0) { + if (_PySlice_Unpack((PySliceObject *)item, &start, &stop, &step) < 0) { return -1; } + slicelen = _PySlice_AdjustIndices(self->size, &start, &stop, step); if (value == NULL) { PyErr_SetString(PyExc_TypeError, "mmap object doesn't support slice deletion"); diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index c74ff06..8c17245 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -435,11 +435,11 @@ bytearray_subscript(PyByteArrayObject *self, PyObject *index) } else if (PySlice_Check(index)) { Py_ssize_t start, stop, step, slicelength, cur, i; - if (PySlice_GetIndicesEx((PySliceObject *)index, - PyByteArray_GET_SIZE(self), - &start, &stop, &step, &slicelength) < 0) { + if (_PySlice_Unpack((PySliceObject *)index, &start, &stop, &step) < 0) { return NULL; } + slicelength = _PySlice_AdjustIndices(PyByteArray_GET_SIZE(self), + &start, &stop, step); if (slicelength <= 0) return PyByteArray_FromStringAndSize("", 0); @@ -619,11 +619,11 @@ bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *valu } } else if (PySlice_Check(index)) { - if (PySlice_GetIndicesEx((PySliceObject *)index, - PyByteArray_GET_SIZE(self), - &start, &stop, &step, &slicelen) < 0) { + if (_PySlice_Unpack((PySliceObject *)index, &start, &stop, &step) < 0) { return -1; } + slicelen = _PySlice_AdjustIndices(PyByteArray_GET_SIZE(self), &start, + &stop, step); } else { PyErr_SetString(PyExc_TypeError, "bytearray indices must be integer"); diff --git a/Objects/listobject.c b/Objects/listobject.c index a71df7b..24bb5bb 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -2562,10 +2562,11 @@ list_subscript(PyListObject* self, PyObject* item) PyObject* it; PyObject **src, **dest; - if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self), - &start, &stop, &step, &slicelength) < 0) { + if (_PySlice_Unpack((PySliceObject *)item, &start, &stop, &step) < 0) { return NULL; } + slicelength = _PySlice_AdjustIndices(Py_SIZE(self), &start, &stop, + step); if (slicelength <= 0) { return PyList_New(0); @@ -2611,10 +2612,11 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value) else if (PySlice_Check(item)) { Py_ssize_t start, stop, step, slicelength; - if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self), - &start, &stop, &step, &slicelength) < 0) { + if (_PySlice_Unpack((PySliceObject *)item, &start, &stop, &step) < 0) { return -1; } + slicelength = _PySlice_AdjustIndices(Py_SIZE(self), &start, &stop, + step); if (step == 1) return list_ass_slice(self, start, stop, value); diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c index 2bac266..12be12b 100644 --- a/Objects/memoryobject.c +++ b/Objects/memoryobject.c @@ -584,10 +584,11 @@ memory_subscript(PyMemoryViewObject *self, PyObject *key) else if (PySlice_Check(key)) { Py_ssize_t start, stop, step, slicelength; - if (PySlice_GetIndicesEx((PySliceObject*)key, get_shape0(view), - &start, &stop, &step, &slicelength) < 0) { + if (_PySlice_Unpack((PySliceObject *)key, &start, &stop, &step) < 0) { return NULL; } + slicelength = _PySlice_AdjustIndices(get_shape0(view), &start, &stop, + step); if (step == 1 && view->ndim == 1) { Py_buffer newview; @@ -662,10 +663,10 @@ memory_ass_sub(PyMemoryViewObject *self, PyObject *key, PyObject *value) else if (PySlice_Check(key)) { Py_ssize_t stop, step; - if (PySlice_GetIndicesEx((PySliceObject*)key, get_shape0(view), - &start, &stop, &step, &len) < 0) { + if (_PySlice_Unpack((PySliceObject *)key, &start, &stop, &step) < 0) { return -1; } + len = _PySlice_AdjustIndices(get_shape0(view), &start, &stop, step); if (step != 1) { PyErr_SetNone(PyExc_NotImplementedError); return -1; diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index ff9479e..3ac3811 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -294,10 +294,10 @@ slice_indices(PySliceObject* self, PyObject* len) return NULL; } - if (PySlice_GetIndicesEx(self, ilen, &start, &stop, - &step, &slicelength) < 0) { + if (_PySlice_Unpack(self, &start, &stop, &step) < 0) { return NULL; } + slicelength = _PySlice_AdjustIndices(ilen, &start, &stop, step); return Py_BuildValue("(nnn)", start, stop, step); } diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 9b93898..ef4f8c1 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -1310,11 +1310,11 @@ string_subscript(PyStringObject* self, PyObject* item) char* result_buf; PyObject* result; - if (PySlice_GetIndicesEx((PySliceObject*)item, - PyString_GET_SIZE(self), - &start, &stop, &step, &slicelength) < 0) { + if (_PySlice_Unpack((PySliceObject *)item, &start, &stop, &step) < 0) { return NULL; } + slicelength = _PySlice_AdjustIndices(PyString_GET_SIZE(self), &start, + &stop, step); if (slicelength <= 0) { return PyString_FromStringAndSize("", 0); diff --git a/Objects/structseq.c b/Objects/structseq.c index 75c1ffb..9b3ba48 100644 --- a/Objects/structseq.c +++ b/Objects/structseq.c @@ -114,11 +114,11 @@ structseq_subscript(PyStructSequence *self, PyObject *item) Py_ssize_t start, stop, step, slicelen, cur, i; PyObject *result; - if (PySlice_GetIndicesEx((PySliceObject *)item, - VISIBLE_SIZE(self), &start, &stop, - &step, &slicelen) < 0) { + if (_PySlice_Unpack((PySliceObject *)item, &start, &stop, &step) < 0) { return NULL; } + slicelen = _PySlice_AdjustIndices(VISIBLE_SIZE(self), &start, &stop, + step); if (slicelen <= 0) return PyTuple_New(0); result = PyTuple_New(slicelen); diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index 2495e95..808156f 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -715,11 +715,11 @@ tuplesubscript(PyTupleObject* self, PyObject* item) PyObject* it; PyObject **src, **dest; - if (PySlice_GetIndicesEx((PySliceObject*)item, - PyTuple_GET_SIZE(self), - &start, &stop, &step, &slicelength) < 0) { + if (_PySlice_Unpack((PySliceObject *)item, &start, &stop, &step) < 0) { return NULL; } + slicelength = _PySlice_AdjustIndices(PyTuple_GET_SIZE(self), &start, + &stop, step); if (slicelength <= 0) { return PyTuple_New(0); diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 7986d70..689529a 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -8008,10 +8008,11 @@ unicode_subscript(PyUnicodeObject* self, PyObject* item) Py_UNICODE* result_buf; PyObject* result; - if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), - &start, &stop, &step, &slicelength) < 0) { + if (_PySlice_Unpack((PySliceObject *)item, &start, &stop, &step) < 0) { return NULL; } + slicelength = _PySlice_AdjustIndices(PyUnicode_GET_SIZE(self), &start, + &stop, step); if (slicelength <= 0) { return PyUnicode_FromUnicode(NULL, 0); -- cgit v0.12