Skip to content

bpo-27867: Expand the PySlice_GetIndicesEx macro. (#1023) #1044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -4273,11 +4273,10 @@ Array_subscript(PyObject *myself, PyObject *item)
PyObject *np;
Py_ssize_t start, stop, step, slicelen, cur, i;

if (PySlice_GetIndicesEx(item,
self->b_length, &start, &stop,
&step, &slicelen) < 0) {
if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
return NULL;
}
slicelen = PySlice_AdjustIndices(self->b_length, &start, &stop, step);

stgdict = PyObject_stgdict((PyObject *)self);
assert(stgdict); /* Cannot be NULL for array object instances */
Expand Down Expand Up @@ -4414,11 +4413,10 @@ Array_ass_subscript(PyObject *myself, PyObject *item, PyObject *value)
else if (PySlice_Check(item)) {
Py_ssize_t start, stop, step, slicelen, otherlen, i, cur;

if (PySlice_GetIndicesEx(item,
self->b_length, &start, &stop,
&step, &slicelen) < 0) {
if (PySlice_Unpack(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;
Expand Down
12 changes: 6 additions & 6 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -1712,11 +1712,11 @@ element_subscr(PyObject* self_, PyObject* item)
if (!self->extra)
return PyList_New(0);

if (PySlice_GetIndicesEx(item,
self->extra->length,
&start, &stop, &step, &slicelen) < 0) {
if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
return NULL;
}
slicelen = PySlice_AdjustIndices(self->extra->length, &start, &stop,
step);

if (slicelen <= 0)
return PyList_New(0);
Expand Down Expand Up @@ -1768,11 +1768,11 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value)
return -1;
}

if (PySlice_GetIndicesEx(item,
self->extra->length,
&start, &stop, &step, &slicelen) < 0) {
if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
return -1;
}
slicelen = PySlice_AdjustIndices(self->extra->length, &start, &stop,
step);

if (value == NULL) {
/* Delete slice */
Expand Down
7 changes: 4 additions & 3 deletions Modules/_testbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1715,10 +1715,10 @@ init_slice(Py_buffer *base, PyObject *key, int dim)
{
Py_ssize_t start, stop, step, slicelength;

if (PySlice_GetIndicesEx(key, base->shape[dim],
&start, &stop, &step, &slicelength) < 0) {
if (PySlice_Unpack(key, &start, &stop, &step) < 0) {
return -1;
}
slicelength = PySlice_AdjustIndices(base->shape[dim], &start, &stop, step);


if (base->suboffsets == NULL || dim == 0) {
Expand Down Expand Up @@ -1935,9 +1935,10 @@ slice_indices(PyObject *self, PyObject *args)
"first argument must be a slice object");
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;
}
s[3] = PySlice_AdjustIndices(len, &s[0], &s[1], s[2]);

ret = PyTuple_New(4);
if (ret == NULL)
Expand Down
11 changes: 6 additions & 5 deletions Modules/arraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2297,10 +2297,11 @@ array_subscr(arrayobject* self, PyObject* item)
arrayobject* ar;
int itemsize = self->ob_descr->itemsize;

if (PySlice_GetIndicesEx(item, Py_SIZE(self),
&start, &stop, &step, &slicelength) < 0) {
if (PySlice_Unpack(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);
Expand Down Expand Up @@ -2368,11 +2369,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(item,
Py_SIZE(self), &start, &stop,
&step, &slicelength) < 0) {
if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
return -1;
}
slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
step);
}
else {
PyErr_SetString(PyExc_TypeError,
Expand Down
9 changes: 4 additions & 5 deletions Modules/mmapmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -806,10 +806,10 @@ mmap_subscript(mmap_object *self, PyObject *item)
else if (PySlice_Check(item)) {
Py_ssize_t start, stop, step, slicelen;

if (PySlice_GetIndicesEx(item, self->size,
&start, &stop, &step, &slicelen) < 0) {
if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
return NULL;
}
slicelen = PySlice_AdjustIndices(self->size, &start, &stop, step);

if (slicelen <= 0)
return PyBytes_FromStringAndSize("", 0);
Expand Down Expand Up @@ -932,11 +932,10 @@ mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value)
Py_ssize_t start, stop, step, slicelen;
Py_buffer vbuf;

if (PySlice_GetIndicesEx(item,
self->size, &start, &stop,
&step, &slicelen) < 0) {
if (PySlice_Unpack(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");
Expand Down
12 changes: 6 additions & 6 deletions Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,11 @@ bytearray_subscript(PyByteArrayObject *self, PyObject *index)
}
else if (PySlice_Check(index)) {
Py_ssize_t start, stop, step, slicelength, cur, i;
if (PySlice_GetIndicesEx(index,
PyByteArray_GET_SIZE(self),
&start, &stop, &step, &slicelength) < 0) {
if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
return NULL;
}
slicelength = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self),
&start, &stop, step);

if (slicelength <= 0)
return PyByteArray_FromStringAndSize("", 0);
Expand Down Expand Up @@ -630,11 +630,11 @@ bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *valu
}
}
else if (PySlice_Check(index)) {
if (PySlice_GetIndicesEx(index,
PyByteArray_GET_SIZE(self),
&start, &stop, &step, &slicelen) < 0) {
if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
return -1;
}
slicelen = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self), &start,
&stop, step);
}
else {
PyErr_Format(PyExc_TypeError,
Expand Down
6 changes: 3 additions & 3 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1683,11 +1683,11 @@ bytes_subscript(PyBytesObject* self, PyObject* item)
char* result_buf;
PyObject* result;

if (PySlice_GetIndicesEx(item,
PyBytes_GET_SIZE(self),
&start, &stop, &step, &slicelength) < 0) {
if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
return NULL;
}
slicelength = PySlice_AdjustIndices(PyBytes_GET_SIZE(self), &start,
&stop, step);

if (slicelength <= 0) {
return PyBytes_FromStringAndSize("", 0);
Expand Down
10 changes: 6 additions & 4 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2420,10 +2420,11 @@ list_subscript(PyListObject* self, PyObject* item)
PyObject* it;
PyObject **src, **dest;

if (PySlice_GetIndicesEx(item, Py_SIZE(self),
&start, &stop, &step, &slicelength) < 0) {
if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
return NULL;
}
slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
step);

if (slicelength <= 0) {
return PyList_New(0);
Expand Down Expand Up @@ -2469,10 +2470,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(item, Py_SIZE(self),
&start, &stop, &step, &slicelength) < 0) {
if (PySlice_Unpack(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);
Expand Down
4 changes: 2 additions & 2 deletions Objects/memoryobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2285,10 +2285,10 @@ init_slice(Py_buffer *base, PyObject *key, int dim)
{
Py_ssize_t start, stop, step, slicelength;

if (PySlice_GetIndicesEx(key, base->shape[dim],
&start, &stop, &step, &slicelength) < 0) {
if (PySlice_Unpack(key, &start, &stop, &step) < 0) {
return -1;
}
slicelength = PySlice_AdjustIndices(base->shape[dim], &start, &stop, step);


if (base->suboffsets == NULL || dim == 0) {
Expand Down
6 changes: 3 additions & 3 deletions Objects/tupleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -720,11 +720,11 @@ tuplesubscript(PyTupleObject* self, PyObject* item)
PyObject* it;
PyObject **src, **dest;

if (PySlice_GetIndicesEx(item,
PyTuple_GET_SIZE(self),
&start, &stop, &step, &slicelength) < 0) {
if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
return NULL;
}
slicelength = PySlice_AdjustIndices(PyTuple_GET_SIZE(self), &start,
&stop, step);

if (slicelength <= 0) {
return PyTuple_New(0);
Expand Down
5 changes: 3 additions & 2 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -13915,10 +13915,11 @@ unicode_subscript(PyObject* self, PyObject* item)
int src_kind, dest_kind;
Py_UCS4 ch, max_char, kind_limit;

if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self),
&start, &stop, &step, &slicelength) < 0) {
if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
return NULL;
}
slicelength = PySlice_AdjustIndices(PyUnicode_GET_LENGTH(self),
&start, &stop, step);

if (slicelength <= 0) {
_Py_RETURN_UNICODE_EMPTY();
Expand Down