Skip to content

Commit 996d72b

Browse files
committed
Issue python#7063: Remove dead code from array slice handling
Patch by Chuck.
1 parent 3b055b5 commit 996d72b

2 files changed

Lines changed: 11 additions & 47 deletions

File tree

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ Core and Builtins
2626
Library
2727
-------
2828

29+
- Issue #7063: Remove dead code from the "array" module's slice handling.
30+
Patch by Chuck.
31+
2932
- Issue #27130: In the "zlib" module, fix handling of large buffers
3033
(typically 4 GiB) when compressing and decompressing. Previously, inputs
3134
were limited to 4 GiB, and compression and decompression operations did not

Modules/arraymodule.c

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -846,37 +846,10 @@ array_repeat(arrayobject *a, Py_ssize_t n)
846846
}
847847

848848
static int
849-
array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
849+
array_del_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
850850
{
851851
char *item;
852-
Py_ssize_t n; /* Size of replacement array */
853852
Py_ssize_t d; /* Change in size */
854-
#define b ((arrayobject *)v)
855-
if (v == NULL)
856-
n = 0;
857-
else if (array_Check(v)) {
858-
n = Py_SIZE(b);
859-
if (a == b) {
860-
/* Special case "a[i:j] = a" -- copy b first */
861-
int ret;
862-
v = array_slice(b, 0, n);
863-
if (!v)
864-
return -1;
865-
ret = array_ass_slice(a, ilow, ihigh, v);
866-
Py_DECREF(v);
867-
return ret;
868-
}
869-
if (b->ob_descr != a->ob_descr) {
870-
PyErr_BadArgument();
871-
return -1;
872-
}
873-
}
874-
else {
875-
PyErr_Format(PyExc_TypeError,
876-
"can only assign array (not \"%.200s\") to array slice",
877-
Py_TYPE(v)->tp_name);
878-
return -1;
879-
}
880853
if (ilow < 0)
881854
ilow = 0;
882855
else if (ilow > Py_SIZE(a))
@@ -888,7 +861,7 @@ array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
888861
else if (ihigh > Py_SIZE(a))
889862
ihigh = Py_SIZE(a);
890863
item = a->ob_item;
891-
d = n - (ihigh-ilow);
864+
d = ihigh-ilow;
892865
/* Issue #4509: If the array has exported buffers and the slice
893866
assignment would change the size of the array, fail early to make
894867
sure we don't modify it. */
@@ -897,25 +870,14 @@ array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
897870
"cannot resize an array that is exporting buffers");
898871
return -1;
899872
}
900-
if (d < 0) { /* Delete -d items */
901-
memmove(item + (ihigh+d)*a->ob_descr->itemsize,
873+
if (d > 0) { /* Delete d items */
874+
memmove(item + (ihigh-d)*a->ob_descr->itemsize,
902875
item + ihigh*a->ob_descr->itemsize,
903876
(Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
904-
if (array_resize(a, Py_SIZE(a) + d) == -1)
877+
if (array_resize(a, Py_SIZE(a) - d) == -1)
905878
return -1;
906879
}
907-
else if (d > 0) { /* Insert d items */
908-
if (array_resize(a, Py_SIZE(a) + d))
909-
return -1;
910-
memmove(item + (ihigh+d)*a->ob_descr->itemsize,
911-
item + ihigh*a->ob_descr->itemsize,
912-
(Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
913-
}
914-
if (n > 0)
915-
memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
916-
n*b->ob_descr->itemsize);
917880
return 0;
918-
#undef b
919881
}
920882

921883
static int
@@ -927,7 +889,7 @@ array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v)
927889
return -1;
928890
}
929891
if (v == NULL)
930-
return array_ass_slice(a, i, i+1, v);
892+
return array_del_slice(a, i, i+1);
931893
return (*a->ob_descr->setitem)(a, i, v);
932894
}
933895

@@ -1155,8 +1117,7 @@ array_array_remove(arrayobject *self, PyObject *v)
11551117
cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
11561118
Py_DECREF(selfi);
11571119
if (cmp > 0) {
1158-
if (array_ass_slice(self, i, i+1,
1159-
(PyObject *)NULL) != 0)
1120+
if (array_del_slice(self, i, i+1) != 0)
11601121
return NULL;
11611122
Py_INCREF(Py_None);
11621123
return Py_None;
@@ -1199,7 +1160,7 @@ array_array_pop_impl(arrayobject *self, Py_ssize_t i)
11991160
v = getarrayitem((PyObject *)self, i);
12001161
if (v == NULL)
12011162
return NULL;
1202-
if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
1163+
if (array_del_slice(self, i, i+1) != 0) {
12031164
Py_DECREF(v);
12041165
return NULL;
12051166
}

0 commit comments

Comments
 (0)