a.pop()
a.pop()
a.pop()
- a.pop()
+ x = a.pop()
+ if x != 'e':
+ raise TestFailed, "array(%s) pop-test" % `type`
if a != array.array(type, "acd"):
raise TestFailed, "array(%s) pop-test" % `type`
+ a.reverse()
+ if a != array.array(type, "dca"):
+ raise TestFailed, "array(%s) reverse-test" % `type`
else:
a = array.array(type, [1, 2, 3, 4, 5])
a[:-1] = a
a.pop()
a.pop()
a.pop()
- a.pop()
+ x = a.pop()
+ if x != 5:
+ raise TestFailed, "array(%s) pop-test" % `type`
if a != array.array(type, [1, 3, 4]):
raise TestFailed, "array(%s) pop-test" % `type`
+ a.reverse()
+ if a != array.array(type, [4, 3, 1]):
+ raise TestFailed, "array(%s) reverse-test" % `type`
# test that overflow exceptions are raised as expected for assignment
# to array of specific integral types
{'d', sizeof(double), d_getitem, d_setitem},
{'\0', 0, 0, 0} /* Sentinel */
};
-/* If we ever allow items larger than double, we must change reverse()! */
/****************************************************************************
Implementations of array object methods.
}
static char count_doc [] =
-"count (x)\n\
+"count(x)\n\
\n\
Return number of occurences of x in the array.";
}
static char index_doc [] =
-"index (x)\n\
+"index(x)\n\
\n\
Return index of first occurence of x in the array.";
}
static char remove_doc [] =
-"remove (x)\n\
+"remove(x)\n\
\n\
Remove the first occurence of x in the array.";
}
static char pop_doc [] =
-"pop ([i])\n\
+"pop([i])\n\
\n\
Return the i-th element and delete it from the array. i defaults to -1.";
}
static char insert_doc [] =
-"insert (i,x)\n\
+"insert(i,x)\n\
\n\
Insert a new item x into the array before position i.";
static PyObject *
array_buffer_info(arrayobject *self, PyObject *args)
{
- PyObject* retval = PyTuple_New(2);
- if (!retval) return NULL;
+ PyObject* retval = NULL;
+ if (!PyArg_ParseTuple(args, ":buffer_info"))
+ return NULL;
+ retval = PyTuple_New(2);
+ if (!retval)
+ return NULL;
PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item));
PyTuple_SET_ITEM(retval, 1, PyInt_FromLong((long)(self->ob_size)));
}
static char buffer_info_doc [] =
-"buffer_info -> (address, length)\n\
+"buffer_info() -> (address, length)\n\
\n\
Return a tuple (address, length) giving the current memory address and\n\
the length in bytes of the buffer used to hold array's contents.";
{
register int itemsize = self->ob_descr->itemsize;
register char *p, *q;
- char tmp[sizeof(double)]; /* Assume that's the max item size */
+ /* little buffer to hold items while swapping */
+ char tmp[256]; /* 8 is probably enough -- but why skimp */
+ assert(itemsize <= sizeof(tmp));
- if (args != NULL) {
- PyErr_SetString(PyExc_TypeError,
- "<array>.reverse requires exactly 0 arguments");
- return NULL;
- }
+ if (!PyArg_ParseTuple(args, ":reverse"))
+ return NULL;
if (self->ob_size > 1) {
for (p = self->ob_item,
q = self->ob_item + (self->ob_size - 1)*itemsize;
p < q;
p += itemsize, q -= itemsize) {
- memmove(tmp, p, itemsize);
- memmove(p, q, itemsize);
- memmove(q, tmp, itemsize);
+ /* memory areas guaranteed disjoint, so memcpy
+ * is safe (& memmove may be slower).
+ */
+ memcpy(tmp, p, itemsize);
+ memcpy(p, q, itemsize);
+ memcpy(q, tmp, itemsize);
}
}