mirror of
https://github.com/python/cpython.git
synced 2025-10-17 04:08:28 +00:00
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.
This commit is contained in:
parent
fe0ca4a0f5
commit
9c14badc5f
4 changed files with 233 additions and 7 deletions
|
@ -3,7 +3,8 @@
|
||||||
Roger E. Masse
|
Roger E. Masse
|
||||||
"""
|
"""
|
||||||
import array
|
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():
|
def main():
|
||||||
testtype('c', 'c')
|
testtype('c', 'c')
|
||||||
|
@ -312,6 +313,46 @@ def testtype(type, example):
|
||||||
a.reverse()
|
a.reverse()
|
||||||
if a != array.array(type, [4, 3, 1]):
|
if a != array.array(type, [4, 3, 1]):
|
||||||
raise TestFailed, "array(%s) reverse-test" % `type`
|
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
|
# test that overflow exceptions are raised as expected for assignment
|
||||||
# to array of specific integral types
|
# to array of specific integral types
|
||||||
|
|
|
@ -410,6 +410,14 @@ vereq(a, [0, 10, 2, 3, 4, 10, 6, 7, 8 ,10])
|
||||||
a = range(4)
|
a = range(4)
|
||||||
a[::-1] = a
|
a[::-1] = a
|
||||||
vereq(a, [3, 2, 1, 0])
|
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'
|
print '6.6 Mappings == Dictionaries'
|
||||||
d = {}
|
d = {}
|
||||||
|
|
|
@ -1478,6 +1478,179 @@ array_repr(arrayobject *a)
|
||||||
return s;
|
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
|
static int
|
||||||
array_buffer_getreadbuf(arrayobject *self, int index, const void **ptr)
|
array_buffer_getreadbuf(arrayobject *self, int index, const void **ptr)
|
||||||
{
|
{
|
||||||
|
@ -1699,7 +1872,7 @@ statichere PyTypeObject Arraytype = {
|
||||||
(reprfunc)array_repr, /* tp_repr */
|
(reprfunc)array_repr, /* tp_repr */
|
||||||
0, /* tp_as _number*/
|
0, /* tp_as _number*/
|
||||||
&array_as_sequence, /* tp_as _sequence*/
|
&array_as_sequence, /* tp_as _sequence*/
|
||||||
0, /* tp_as _mapping*/
|
&array_as_mapping, /* tp_as _mapping*/
|
||||||
0, /* tp_hash */
|
0, /* tp_hash */
|
||||||
0, /* tp_call */
|
0, /* tp_call */
|
||||||
0, /* tp_str */
|
0, /* tp_str */
|
||||||
|
|
|
@ -1757,9 +1757,13 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
|
||||||
return -1;
|
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) {
|
if (value == NULL) {
|
||||||
/* delete slice */
|
/* delete slice */
|
||||||
PyObject **garbage, **item;
|
PyObject **garbage, **it;
|
||||||
int cur, i, j;
|
int cur, i, j;
|
||||||
|
|
||||||
if (slicelength <= 0)
|
if (slicelength <= 0)
|
||||||
|
@ -1788,15 +1792,15 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
|
||||||
cur + j + 1));
|
cur + j + 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (cur = start + slicelength*step + 1;
|
for (cur = start + slicelength*step + 1;
|
||||||
cur < self->ob_size; cur++) {
|
cur < self->ob_size; cur++) {
|
||||||
PyList_SET_ITEM(self, cur - slicelength,
|
PyList_SET_ITEM(self, cur - slicelength,
|
||||||
PyList_GET_ITEM(self, cur));
|
PyList_GET_ITEM(self, cur));
|
||||||
}
|
}
|
||||||
self->ob_size -= slicelength;
|
self->ob_size -= slicelength;
|
||||||
item = self->ob_item;
|
it = self->ob_item;
|
||||||
NRESIZE(item, PyObject*, self->ob_size);
|
NRESIZE(it, PyObject*, self->ob_size);
|
||||||
self->ob_item = item;
|
self->ob_item = it;
|
||||||
|
|
||||||
for (i = 0; i < slicelength; i++) {
|
for (i = 0; i < slicelength; i++) {
|
||||||
Py_DECREF(garbage[i]);
|
Py_DECREF(garbage[i]);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue