Commit b879fe82 authored by Serhiy Storchaka's avatar Serhiy Storchaka Committed by GitHub

Expand the PySlice_GetIndicesEx macro. (#1023)

parent 205e00c5
...@@ -4277,11 +4277,10 @@ Array_subscript(PyObject *myself, PyObject *item) ...@@ -4277,11 +4277,10 @@ Array_subscript(PyObject *myself, PyObject *item)
PyObject *np; PyObject *np;
Py_ssize_t start, stop, step, slicelen, cur, i; Py_ssize_t start, stop, step, slicelen, cur, i;
if (PySlice_GetIndicesEx(item, if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
self->b_length, &start, &stop,
&step, &slicelen) < 0) {
return NULL; return NULL;
} }
slicelen = PySlice_AdjustIndices(self->b_length, &start, &stop, step);
stgdict = PyObject_stgdict((PyObject *)self); stgdict = PyObject_stgdict((PyObject *)self);
assert(stgdict); /* Cannot be NULL for array object instances */ assert(stgdict); /* Cannot be NULL for array object instances */
...@@ -4418,11 +4417,10 @@ Array_ass_subscript(PyObject *myself, PyObject *item, PyObject *value) ...@@ -4418,11 +4417,10 @@ Array_ass_subscript(PyObject *myself, PyObject *item, PyObject *value)
else if (PySlice_Check(item)) { else if (PySlice_Check(item)) {
Py_ssize_t start, stop, step, slicelen, otherlen, i, cur; Py_ssize_t start, stop, step, slicelen, otherlen, i, cur;
if (PySlice_GetIndicesEx(item, if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
self->b_length, &start, &stop,
&step, &slicelen) < 0) {
return -1; return -1;
} }
slicelen = PySlice_AdjustIndices(self->b_length, &start, &stop, step);
if ((step < 0 && start < stop) || if ((step < 0 && start < stop) ||
(step > 0 && start > stop)) (step > 0 && start > stop))
stop = start; stop = start;
......
...@@ -1740,11 +1740,11 @@ element_subscr(PyObject* self_, PyObject* item) ...@@ -1740,11 +1740,11 @@ element_subscr(PyObject* self_, PyObject* item)
if (!self->extra) if (!self->extra)
return PyList_New(0); return PyList_New(0);
if (PySlice_GetIndicesEx(item, if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
self->extra->length,
&start, &stop, &step, &slicelen) < 0) {
return NULL; return NULL;
} }
slicelen = PySlice_AdjustIndices(self->extra->length, &start, &stop,
step);
if (slicelen <= 0) if (slicelen <= 0)
return PyList_New(0); return PyList_New(0);
...@@ -1796,11 +1796,11 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value) ...@@ -1796,11 +1796,11 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value)
return -1; return -1;
} }
if (PySlice_GetIndicesEx(item, if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
self->extra->length,
&start, &stop, &step, &slicelen) < 0) {
return -1; return -1;
} }
slicelen = PySlice_AdjustIndices(self->extra->length, &start, &stop,
step);
if (value == NULL) { if (value == NULL) {
/* Delete slice */ /* Delete slice */
......
...@@ -1715,10 +1715,10 @@ init_slice(Py_buffer *base, PyObject *key, int dim) ...@@ -1715,10 +1715,10 @@ init_slice(Py_buffer *base, PyObject *key, int dim)
{ {
Py_ssize_t start, stop, step, slicelength; Py_ssize_t start, stop, step, slicelength;
if (PySlice_GetIndicesEx(key, base->shape[dim], if (PySlice_Unpack(key, &start, &stop, &step) < 0) {
&start, &stop, &step, &slicelength) < 0) {
return -1; return -1;
} }
slicelength = PySlice_AdjustIndices(base->shape[dim], &start, &stop, step);
if (base->suboffsets == NULL || dim == 0) { if (base->suboffsets == NULL || dim == 0) {
...@@ -1935,9 +1935,10 @@ slice_indices(PyObject *self, PyObject *args) ...@@ -1935,9 +1935,10 @@ slice_indices(PyObject *self, PyObject *args)
"first argument must be a slice object"); "first argument must be a slice object");
return NULL; return NULL;
} }
if (PySlice_GetIndicesEx(key, len, &s[0], &s[1], &s[2], &s[3]) < 0) { if (PySlice_Unpack(key, &s[0], &s[1], &s[2]) < 0) {
return NULL; return NULL;
} }
s[3] = PySlice_AdjustIndices(len, &s[0], &s[1], s[2]);
ret = PyTuple_New(4); ret = PyTuple_New(4);
if (ret == NULL) if (ret == NULL)
......
...@@ -2289,10 +2289,11 @@ array_subscr(arrayobject* self, PyObject* item) ...@@ -2289,10 +2289,11 @@ array_subscr(arrayobject* self, PyObject* item)
arrayobject* ar; arrayobject* ar;
int itemsize = self->ob_descr->itemsize; int itemsize = self->ob_descr->itemsize;
if (PySlice_GetIndicesEx(item, Py_SIZE(self), if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
&start, &stop, &step, &slicelength) < 0) {
return NULL; return NULL;
} }
slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
step);
if (slicelength <= 0) { if (slicelength <= 0) {
return newarrayobject(&Arraytype, 0, self->ob_descr); return newarrayobject(&Arraytype, 0, self->ob_descr);
...@@ -2360,11 +2361,11 @@ array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value) ...@@ -2360,11 +2361,11 @@ array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
return (*self->ob_descr->setitem)(self, i, value); return (*self->ob_descr->setitem)(self, i, value);
} }
else if (PySlice_Check(item)) { else if (PySlice_Check(item)) {
if (PySlice_GetIndicesEx(item, if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Py_SIZE(self), &start, &stop,
&step, &slicelength) < 0) {
return -1; return -1;
} }
slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
step);
} }
else { else {
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_TypeError,
......
...@@ -778,10 +778,10 @@ mmap_subscript(mmap_object *self, PyObject *item) ...@@ -778,10 +778,10 @@ mmap_subscript(mmap_object *self, PyObject *item)
else if (PySlice_Check(item)) { else if (PySlice_Check(item)) {
Py_ssize_t start, stop, step, slicelen; Py_ssize_t start, stop, step, slicelen;
if (PySlice_GetIndicesEx(item, self->size, if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
&start, &stop, &step, &slicelen) < 0) {
return NULL; return NULL;
} }
slicelen = PySlice_AdjustIndices(self->size, &start, &stop, step);
if (slicelen <= 0) if (slicelen <= 0)
return PyBytes_FromStringAndSize("", 0); return PyBytes_FromStringAndSize("", 0);
...@@ -904,11 +904,10 @@ mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value) ...@@ -904,11 +904,10 @@ mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value)
Py_ssize_t start, stop, step, slicelen; Py_ssize_t start, stop, step, slicelen;
Py_buffer vbuf; Py_buffer vbuf;
if (PySlice_GetIndicesEx(item, if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
self->size, &start, &stop,
&step, &slicelen) < 0) {
return -1; return -1;
} }
slicelen = PySlice_AdjustIndices(self->size, &start, &stop, step);
if (value == NULL) { if (value == NULL) {
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_TypeError,
"mmap object doesn't support slice deletion"); "mmap object doesn't support slice deletion");
......
...@@ -400,11 +400,11 @@ bytearray_subscript(PyByteArrayObject *self, PyObject *index) ...@@ -400,11 +400,11 @@ bytearray_subscript(PyByteArrayObject *self, PyObject *index)
} }
else if (PySlice_Check(index)) { else if (PySlice_Check(index)) {
Py_ssize_t start, stop, step, slicelength, cur, i; Py_ssize_t start, stop, step, slicelength, cur, i;
if (PySlice_GetIndicesEx(index, if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
PyByteArray_GET_SIZE(self),
&start, &stop, &step, &slicelength) < 0) {
return NULL; return NULL;
} }
slicelength = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self),
&start, &stop, step);
if (slicelength <= 0) if (slicelength <= 0)
return PyByteArray_FromStringAndSize("", 0); return PyByteArray_FromStringAndSize("", 0);
...@@ -630,11 +630,11 @@ bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *valu ...@@ -630,11 +630,11 @@ bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *valu
} }
} }
else if (PySlice_Check(index)) { else if (PySlice_Check(index)) {
if (PySlice_GetIndicesEx(index, if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
PyByteArray_GET_SIZE(self),
&start, &stop, &step, &slicelen) < 0) {
return -1; return -1;
} }
slicelen = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self), &start,
&stop, step);
} }
else { else {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
......
...@@ -1683,11 +1683,11 @@ bytes_subscript(PyBytesObject* self, PyObject* item) ...@@ -1683,11 +1683,11 @@ bytes_subscript(PyBytesObject* self, PyObject* item)
char* result_buf; char* result_buf;
PyObject* result; PyObject* result;
if (PySlice_GetIndicesEx(item, if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
PyBytes_GET_SIZE(self),
&start, &stop, &step, &slicelength) < 0) {
return NULL; return NULL;
} }
slicelength = PySlice_AdjustIndices(PyBytes_GET_SIZE(self), &start,
&stop, step);
if (slicelength <= 0) { if (slicelength <= 0) {
return PyBytes_FromStringAndSize("", 0); return PyBytes_FromStringAndSize("", 0);
......
...@@ -2486,10 +2486,11 @@ list_subscript(PyListObject* self, PyObject* item) ...@@ -2486,10 +2486,11 @@ list_subscript(PyListObject* self, PyObject* item)
PyObject* it; PyObject* it;
PyObject **src, **dest; PyObject **src, **dest;
if (PySlice_GetIndicesEx(item, Py_SIZE(self), if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
&start, &stop, &step, &slicelength) < 0) {
return NULL; return NULL;
} }
slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
step);
if (slicelength <= 0) { if (slicelength <= 0) {
return PyList_New(0); return PyList_New(0);
...@@ -2535,10 +2536,11 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value) ...@@ -2535,10 +2536,11 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
else if (PySlice_Check(item)) { else if (PySlice_Check(item)) {
Py_ssize_t start, stop, step, slicelength; Py_ssize_t start, stop, step, slicelength;
if (PySlice_GetIndicesEx(item, Py_SIZE(self), if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
&start, &stop, &step, &slicelength) < 0) {
return -1; return -1;
} }
slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
step);
if (step == 1) if (step == 1)
return list_ass_slice(self, start, stop, value); return list_ass_slice(self, start, stop, value);
......
...@@ -2285,10 +2285,10 @@ init_slice(Py_buffer *base, PyObject *key, int dim) ...@@ -2285,10 +2285,10 @@ init_slice(Py_buffer *base, PyObject *key, int dim)
{ {
Py_ssize_t start, stop, step, slicelength; Py_ssize_t start, stop, step, slicelength;
if (PySlice_GetIndicesEx(key, base->shape[dim], if (PySlice_Unpack(key, &start, &stop, &step) < 0) {
&start, &stop, &step, &slicelength) < 0) {
return -1; return -1;
} }
slicelength = PySlice_AdjustIndices(base->shape[dim], &start, &stop, step);
if (base->suboffsets == NULL || dim == 0) { if (base->suboffsets == NULL || dim == 0) {
......
...@@ -758,11 +758,11 @@ tuplesubscript(PyTupleObject* self, PyObject* item) ...@@ -758,11 +758,11 @@ tuplesubscript(PyTupleObject* self, PyObject* item)
PyObject* it; PyObject* it;
PyObject **src, **dest; PyObject **src, **dest;
if (PySlice_GetIndicesEx(item, if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
PyTuple_GET_SIZE(self),
&start, &stop, &step, &slicelength) < 0) {
return NULL; return NULL;
} }
slicelength = PySlice_AdjustIndices(PyTuple_GET_SIZE(self), &start,
&stop, step);
if (slicelength <= 0) { if (slicelength <= 0) {
return PyTuple_New(0); return PyTuple_New(0);
......
...@@ -13996,10 +13996,11 @@ unicode_subscript(PyObject* self, PyObject* item) ...@@ -13996,10 +13996,11 @@ unicode_subscript(PyObject* self, PyObject* item)
int src_kind, dest_kind; int src_kind, dest_kind;
Py_UCS4 ch, max_char, kind_limit; Py_UCS4 ch, max_char, kind_limit;
if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
&start, &stop, &step, &slicelength) < 0) {
return NULL; return NULL;
} }
slicelength = PySlice_AdjustIndices(PyUnicode_GET_LENGTH(self),
&start, &stop, step);
if (slicelength <= 0) { if (slicelength <= 0) {
_Py_RETURN_UNICODE_EMPTY(); _Py_RETURN_UNICODE_EMPTY();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment