]> granicus.if.org Git - python/commitdiff
Fix the bug described in
authorMichael W. Hudson <mwh@python.net>
Wed, 19 Jun 2002 15:44:15 +0000 (15:44 +0000)
committerMichael W. Hudson <mwh@python.net>
Wed, 19 Jun 2002 15:44:15 +0000 (15:44 +0000)
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.

Lib/test/test_array.py
Lib/test/test_types.py
Modules/arraymodule.c
Objects/listobject.c

index 681a4d0d73f19a3df2ce5a4d3ae609cb608f5d61..9a298875a186775be58cdd3ef685dce3e44fc11c 100755 (executable)
@@ -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
index 7f3b9233638f2d93320cc7a1788b8725939c96c3..494a13a5185a9d4884aee46536ee84e9dbc4f3e2 100644 (file)
@@ -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 = {}
index dd93950f8dde0fb3d091297c367b88e09ffd1239..c0e59bc0d3f6c352db79a38682d755e86a8955bd 100644 (file)
@@ -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 */
index 03bcaee1007e6718deac39aabc9cb577c2bb0319..77349bb8f7df8c3c217b77dee35733e04d5ca903 100644 (file)
@@ -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]);