summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael W. Hudson <mwh@python.net>2002-06-19 15:44:15 (GMT)
committerMichael W. Hudson <mwh@python.net>2002-06-19 15:44:15 (GMT)
commit9c14badc5fed3c28504470ba4bd64bf7acad599f (patch)
tree2a474dbc252c8f5aa1f70d032feaeedc49d04231
parentfe0ca4a0f50ca90359d3c619fad98bbe018ca7d2 (diff)
downloadcpython-9c14badc5fed3c28504470ba4bd64bf7acad599f.zip
cpython-9c14badc5fed3c28504470ba4bd64bf7acad599f.tar.gz
cpython-9c14badc5fed3c28504470ba4bd64bf7acad599f.tar.bz2
Fix the bug described in
http://mail.python.org/pipermail/python-dev/2002-June/025461.html with test cases. Also includes extended slice support for arrays, which I thought I'd already checked in but obviously not.
-rwxr-xr-xLib/test/test_array.py43
-rw-r--r--Lib/test/test_types.py8
-rw-r--r--Modules/arraymodule.c175
-rw-r--r--Objects/listobject.c14
4 files changed, 233 insertions, 7 deletions
diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py
index 681a4d0..9a29887 100755
--- a/Lib/test/test_array.py
+++ b/Lib/test/test_array.py
@@ -3,7 +3,8 @@
Roger E. Masse
"""
import array
-from test_support import verbose, TESTFN, unlink, TestFailed, have_unicode
+from test_support import verbose, TESTFN, unlink, TestFailed,\
+ have_unicode, vereq
def main():
testtype('c', 'c')
@@ -312,6 +313,46 @@ def testtype(type, example):
a.reverse()
if a != array.array(type, [4, 3, 1]):
raise TestFailed, "array(%s) reverse-test" % `type`
+ # extended slicing
+ # subscription
+ a = array.array(type, [0,1,2,3,4])
+ vereq(a[::], a)
+ vereq(a[::2], array.array(type, [0,2,4]))
+ vereq(a[1::2], array.array(type, [1,3]))
+ vereq(a[::-1], array.array(type, [4,3,2,1,0]))
+ vereq(a[::-2], array.array(type, [4,2,0]))
+ vereq(a[3::-2], array.array(type, [3,1]))
+ vereq(a[-100:100:], a)
+ vereq(a[100:-100:-1], a[::-1])
+ vereq(a[-100L:100L:2L], array.array(type, [0,2,4]))
+ vereq(a[1000:2000:2], array.array(type, []))
+ vereq(a[-1000:-2000:-2], array.array(type, []))
+ # deletion
+ del a[::2]
+ vereq(a, array.array(type, [1,3]))
+ a = array.array(type, range(5))
+ del a[1::2]
+ vereq(a, array.array(type, [0,2,4]))
+ a = array.array(type, range(5))
+ del a[1::-2]
+ vereq(a, array.array(type, [0,2,3,4]))
+ # assignment
+ a = array.array(type, range(10))
+ a[::2] = array.array(type, [-1]*5)
+ vereq(a, array.array(type, [-1, 1, -1, 3, -1, 5, -1, 7, -1, 9]))
+ a = array.array(type, range(10))
+ a[::-4] = array.array(type, [10]*3)
+ vereq(a, array.array(type, [0, 10, 2, 3, 4, 10, 6, 7, 8 ,10]))
+ a = array.array(type, range(4))
+ a[::-1] = a
+ vereq(a, array.array(type, [3, 2, 1, 0]))
+ a = array.array(type, range(10))
+ b = a[:]
+ c = a[:]
+ ins = array.array(type, range(2))
+ a[2:3] = ins
+ b[slice(2,3)] = ins
+ c[2:3:] = ins
# test that overflow exceptions are raised as expected for assignment
# to array of specific integral types
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
index 7f3b923..494a13a 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -410,6 +410,14 @@ vereq(a, [0, 10, 2, 3, 4, 10, 6, 7, 8 ,10])
a = range(4)
a[::-1] = a
vereq(a, [3, 2, 1, 0])
+a = range(10)
+b = a[:]
+c = a[:]
+a[2:3] = ["two", "elements"]
+b[slice(2,3)] = ["two", "elements"]
+c[2:3:] = ["two", "elements"]
+vereq(a, b)
+vereq(a, c)
print '6.6 Mappings == Dictionaries'
d = {}
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index dd93950..c0e59bc 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -1478,6 +1478,179 @@ array_repr(arrayobject *a)
return s;
}
+static PyObject*
+array_subscr(arrayobject* self, PyObject* item)
+{
+ if (PyInt_Check(item)) {
+ long i = PyInt_AS_LONG(item);
+ if (i < 0)
+ i += self->ob_size;
+ return array_item(self, i);
+ }
+ else if (PyLong_Check(item)) {
+ long i = PyLong_AsLong(item);
+ if (i == -1 && PyErr_Occurred())
+ return NULL;
+ if (i < 0)
+ i += self->ob_size;
+ return array_item(self, i);
+ }
+ else if (PySlice_Check(item)) {
+ int start, stop, step, slicelength, cur, i;
+ PyObject* result;
+ arrayobject* ar;
+ int itemsize = self->ob_descr->itemsize;
+
+ if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
+ &start, &stop, &step, &slicelength) < 0) {
+ return NULL;
+ }
+
+ if (slicelength <= 0) {
+ return newarrayobject(&Arraytype, 0, self->ob_descr);
+ }
+ else {
+ result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
+ if (!result) return NULL;
+
+ ar = (arrayobject*)result;
+
+ for (cur = start, i = 0; i < slicelength;
+ cur += step, i++) {
+ memcpy(ar->ob_item + i*itemsize,
+ self->ob_item + cur*itemsize,
+ itemsize);
+ }
+
+ return result;
+ }
+ }
+ else {
+ PyErr_SetString(PyExc_TypeError,
+ "list indices must be integers");
+ return NULL;
+ }
+}
+
+static int
+array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
+{
+ if (PyInt_Check(item)) {
+ long i = PyInt_AS_LONG(item);
+ if (i < 0)
+ i += self->ob_size;
+ return array_ass_item(self, i, value);
+ }
+ else if (PyLong_Check(item)) {
+ long i = PyLong_AsLong(item);
+ if (i == -1 && PyErr_Occurred())
+ return -1;
+ if (i < 0)
+ i += self->ob_size;
+ return array_ass_item(self, i, value);
+ }
+ else if (PySlice_Check(item)) {
+ int start, stop, step, slicelength;
+ int itemsize = self->ob_descr->itemsize;
+
+ if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
+ &start, &stop, &step, &slicelength) < 0) {
+ return -1;
+ }
+
+ /* treat A[slice(a,b)] = v _exactly_ like A[a:b] = v */
+ if (step == 1 && ((PySliceObject*)item)->step == Py_None)
+ return array_ass_slice(self, start, stop, value);
+
+ if (value == NULL) {
+ /* delete slice */
+ int cur, i;
+
+ if (slicelength <= 0)
+ return 0;
+
+ if (step < 0) {
+ stop = start + 1;
+ start = stop + step*(slicelength - 1) - 1;
+ step = -step;
+ }
+
+ for (cur = start, i = 0; cur < stop;
+ cur += step, i++) {
+ memmove(self->ob_item + (cur - i)*itemsize,
+ self->ob_item + (cur + 1)*itemsize,
+ (step - 1) * itemsize);
+ }
+ if (self->ob_size > (start + slicelength*step)) {
+ memmove(self->ob_item + (start + slicelength*(step - 1))*itemsize,
+ self->ob_item + (start + slicelength*step)*itemsize,
+ (self->ob_size - (start + slicelength*step))*itemsize);
+ }
+
+ self->ob_size -= slicelength;
+ self->ob_item = PyMem_REALLOC(self->ob_item, itemsize*self->ob_size);
+
+
+ return 0;
+ }
+ else {
+ /* assign slice */
+ int cur, i;
+ arrayobject* av;
+
+ if (!array_Check(value)) {
+ PyErr_Format(PyExc_TypeError,
+ "must assign array (not \"%.200s\") to slice",
+ value->ob_type->tp_name);
+ return -1;
+ }
+
+ av = (arrayobject*)value;
+
+ if (av->ob_size != slicelength) {
+ PyErr_Format(PyExc_ValueError,
+ "attempt to assign array of size %d to extended slice of size %d",
+ av->ob_size, slicelength);
+ return -1;
+ }
+
+ if (!slicelength)
+ return 0;
+
+ /* protect against a[::-1] = a */
+ if (self == av) {
+ value = array_slice(av, 0, av->ob_size);
+ av = (arrayobject*)value;
+ }
+ else {
+ Py_INCREF(value);
+ }
+
+ for (cur = start, i = 0; i < slicelength;
+ cur += step, i++) {
+ memcpy(self->ob_item + cur*itemsize,
+ av->ob_item + i*itemsize,
+ itemsize);
+ }
+
+ Py_DECREF(value);
+
+ return 0;
+ }
+ }
+ else {
+ PyErr_SetString(PyExc_TypeError,
+ "list indices must be integers");
+ return -1;
+ }
+}
+
+static PyMappingMethods array_as_mapping = {
+ (inquiry)array_length,
+ (binaryfunc)array_subscr,
+ (objobjargproc)array_ass_subscr
+};
+
static int
array_buffer_getreadbuf(arrayobject *self, int index, const void **ptr)
{
@@ -1699,7 +1872,7 @@ statichere PyTypeObject Arraytype = {
(reprfunc)array_repr, /* tp_repr */
0, /* tp_as _number*/
&array_as_sequence, /* tp_as _sequence*/
- 0, /* tp_as _mapping*/
+ &array_as_mapping, /* tp_as _mapping*/
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
diff --git a/Objects/listobject.c b/Objects/listobject.c
index 03bcaee..77349bb 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -1757,9 +1757,13 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
return -1;
}
+ /* treat L[slice(a,b)] = v _exactly_ like L[a:b] = v */
+ if (step == 1 && ((PySliceObject*)item)->step == Py_None)
+ return list_ass_slice(self, start, stop, value);
+
if (value == NULL) {
/* delete slice */
- PyObject **garbage, **item;
+ PyObject **garbage, **it;
int cur, i, j;
if (slicelength <= 0)
@@ -1788,15 +1792,15 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
cur + j + 1));
}
}
- for (cur = start + slicelength*step + 1;
+ for (cur = start + slicelength*step + 1;
cur < self->ob_size; cur++) {
PyList_SET_ITEM(self, cur - slicelength,
PyList_GET_ITEM(self, cur));
}
self->ob_size -= slicelength;
- item = self->ob_item;
- NRESIZE(item, PyObject*, self->ob_size);
- self->ob_item = item;
+ it = self->ob_item;
+ NRESIZE(it, PyObject*, self->ob_size);
+ self->ob_item = it;
for (i = 0; i < slicelength; i++) {
Py_DECREF(garbage[i]);