blob: 89e3f25f4ee074c210bef2d151b807f7db923b95 [file] [log] [blame]
Guido van Rossum778983b1993-02-19 15:55:02 +00001/* Array object implementation */
2
3/* An array is a uniform list -- all items have the same type.
4 The item type is restricted to simple C types like int or float */
5
Martin v. Löwis18e16552006-02-15 17:27:45 +00006#define PY_SSIZE_T_CLEAN
Roger E. Masse2919eaa1996-12-09 20:10:36 +00007#include "Python.h"
Raymond Hettingercb87bc82004-05-31 00:35:52 +00008#include "structmember.h"
Roger E. Masse5817f8f1996-12-09 22:24:19 +00009
Guido van Rossum0c709541994-08-19 12:01:32 +000010#ifdef STDC_HEADERS
11#include <stddef.h>
Guido van Rossum7f1de831999-08-27 20:33:52 +000012#else /* !STDC_HEADERS */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000013#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000014#include <sys/types.h> /* For size_t */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000015#endif /* HAVE_SYS_TYPES_H */
Guido van Rossum7f1de831999-08-27 20:33:52 +000016#endif /* !STDC_HEADERS */
Guido van Rossum778983b1993-02-19 15:55:02 +000017
18struct arrayobject; /* Forward */
19
Tim Petersbb307342000-09-10 05:22:54 +000020/* All possible arraydescr values are defined in the vector "descriptors"
21 * below. That's defined later because the appropriate get and set
22 * functions aren't visible yet.
23 */
Guido van Rossum778983b1993-02-19 15:55:02 +000024struct arraydescr {
25 int typecode;
26 int itemsize;
Martin v. Löwis18e16552006-02-15 17:27:45 +000027 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
28 int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *);
Guido van Rossum778983b1993-02-19 15:55:02 +000029};
30
31typedef struct arrayobject {
Georg Brandl1b672672006-02-17 08:56:33 +000032 PyObject_VAR_HEAD
Guido van Rossum778983b1993-02-19 15:55:02 +000033 char *ob_item;
Martin v. Löwis18e16552006-02-15 17:27:45 +000034 Py_ssize_t allocated;
Guido van Rossum778983b1993-02-19 15:55:02 +000035 struct arraydescr *ob_descr;
Raymond Hettingercb87bc82004-05-31 00:35:52 +000036 PyObject *weakreflist; /* List of weak references */
Guido van Rossum778983b1993-02-19 15:55:02 +000037} arrayobject;
38
Jeremy Hylton938ace62002-07-17 16:30:39 +000039static PyTypeObject Arraytype;
Guido van Rossum778983b1993-02-19 15:55:02 +000040
Martin v. Löwis99866332002-03-01 10:27:01 +000041#define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
Martin v. Löwis9f2e3462007-07-21 17:22:18 +000042#define array_CheckExact(op) (Py_Type(op) == &Arraytype)
Guido van Rossum778983b1993-02-19 15:55:02 +000043
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000044static int
Martin v. Löwis18e16552006-02-15 17:27:45 +000045array_resize(arrayobject *self, Py_ssize_t newsize)
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000046{
47 char *items;
48 size_t _new_size;
49
50 /* Bypass realloc() when a previous overallocation is large enough
51 to accommodate the newsize. If the newsize is 16 smaller than the
52 current size, then proceed with the realloc() to shrink the list.
53 */
54
55 if (self->allocated >= newsize &&
Martin v. Löwis9f2e3462007-07-21 17:22:18 +000056 Py_Size(self) < newsize + 16 &&
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000057 self->ob_item != NULL) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +000058 Py_Size(self) = newsize;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000059 return 0;
60 }
61
62 /* This over-allocates proportional to the array size, making room
63 * for additional growth. The over-allocation is mild, but is
64 * enough to give linear-time amortized behavior over a long
65 * sequence of appends() in the presence of a poorly-performing
66 * system realloc().
67 * The growth pattern is: 0, 4, 8, 16, 25, 34, 46, 56, 67, 79, ...
68 * Note, the pattern starts out the same as for lists but then
69 * grows at a smaller rate so that larger arrays only overallocate
70 * by about 1/16th -- this is done because arrays are presumed to be more
71 * memory critical.
72 */
73
Martin v. Löwis9f2e3462007-07-21 17:22:18 +000074 _new_size = (newsize >> 4) + (Py_Size(self) < 8 ? 3 : 7) + newsize;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000075 items = self->ob_item;
76 /* XXX The following multiplication and division does not optimize away
77 like it does for lists since the size is not known at compile time */
78 if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize))
79 PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize));
80 else
81 items = NULL;
82 if (items == NULL) {
83 PyErr_NoMemory();
84 return -1;
85 }
86 self->ob_item = items;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +000087 Py_Size(self) = newsize;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000088 self->allocated = _new_size;
89 return 0;
90}
91
Tim Petersbb307342000-09-10 05:22:54 +000092/****************************************************************************
93Get and Set functions for each type.
94A Get function takes an arrayobject* and an integer index, returning the
95array value at that index wrapped in an appropriate PyObject*.
96A Set function takes an arrayobject, integer index, and PyObject*; sets
97the array value at that index to the raw C data extracted from the PyObject*,
98and returns 0 if successful, else nonzero on failure (PyObject* not of an
99appropriate type or value).
100Note that the basic Get and Set functions do NOT check that the index is
101in bounds; that's the responsibility of the caller.
102****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +0000103
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000104static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000105b_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000106{
107 long x = ((char *)ap->ob_item)[i];
108 if (x >= 128)
109 x -= 256;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000110 return PyInt_FromLong(x);
Guido van Rossum778983b1993-02-19 15:55:02 +0000111}
112
113static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000114b_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000115{
Fred Drake541dc3b2000-06-28 17:49:30 +0000116 short x;
117 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
118 must use the next size up that is signed ('h') and manually do
119 the overflow checking */
120 if (!PyArg_Parse(v, "h;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000121 return -1;
Guido van Rossum9f754e02000-07-01 00:38:19 +0000122 else if (x < -128) {
Fred Drake541dc3b2000-06-28 17:49:30 +0000123 PyErr_SetString(PyExc_OverflowError,
124 "signed char is less than minimum");
125 return -1;
126 }
Guido van Rossum9f754e02000-07-01 00:38:19 +0000127 else if (x > 127) {
Fred Drake541dc3b2000-06-28 17:49:30 +0000128 PyErr_SetString(PyExc_OverflowError,
129 "signed char is greater than maximum");
130 return -1;
131 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000132 if (i >= 0)
Fred Drake541dc3b2000-06-28 17:49:30 +0000133 ((char *)ap->ob_item)[i] = (char)x;
Guido van Rossum778983b1993-02-19 15:55:02 +0000134 return 0;
135}
136
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000137static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000138BB_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000139{
140 long x = ((unsigned char *)ap->ob_item)[i];
141 return PyInt_FromLong(x);
142}
143
Fred Drake541dc3b2000-06-28 17:49:30 +0000144static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000145BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000146{
147 unsigned char x;
148 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
149 if (!PyArg_Parse(v, "b;array item must be integer", &x))
150 return -1;
151 if (i >= 0)
Martin v. Löwis99866332002-03-01 10:27:01 +0000152 ((char *)ap->ob_item)[i] = x;
Fred Drake541dc3b2000-06-28 17:49:30 +0000153 return 0;
154}
Guido van Rossum549ab711997-01-03 19:09:47 +0000155
Martin v. Löwis99866332002-03-01 10:27:01 +0000156static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000157u_getitem(arrayobject *ap, Py_ssize_t i)
Martin v. Löwis99866332002-03-01 10:27:01 +0000158{
159 return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1);
160}
161
162static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000163u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Martin v. Löwis99866332002-03-01 10:27:01 +0000164{
165 Py_UNICODE *p;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000166 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +0000167
168 if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
169 return -1;
170 if (len != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000171 PyErr_SetString(PyExc_TypeError,
172 "array item must be unicode character");
Martin v. Löwis99866332002-03-01 10:27:01 +0000173 return -1;
174 }
175 if (i >= 0)
176 ((Py_UNICODE *)ap->ob_item)[i] = p[0];
177 return 0;
178}
Martin v. Löwis99866332002-03-01 10:27:01 +0000179
Guido van Rossum549ab711997-01-03 19:09:47 +0000180static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000181h_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000182{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000183 return PyInt_FromLong((long) ((short *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000184}
185
186static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000187h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000188{
189 short x;
Fred Drake541dc3b2000-06-28 17:49:30 +0000190 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000191 if (!PyArg_Parse(v, "h;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000192 return -1;
193 if (i >= 0)
194 ((short *)ap->ob_item)[i] = x;
195 return 0;
196}
197
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000198static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000199HH_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000200{
201 return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
202}
203
Fred Drake541dc3b2000-06-28 17:49:30 +0000204static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000205HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000206{
207 int x;
208 /* PyArg_Parse's 'h' formatter is for a signed short, therefore
209 must use the next size up and manually do the overflow checking */
210 if (!PyArg_Parse(v, "i;array item must be integer", &x))
211 return -1;
212 else if (x < 0) {
213 PyErr_SetString(PyExc_OverflowError,
214 "unsigned short is less than minimum");
215 return -1;
216 }
217 else if (x > USHRT_MAX) {
218 PyErr_SetString(PyExc_OverflowError,
219 "unsigned short is greater than maximum");
220 return -1;
221 }
222 if (i >= 0)
223 ((short *)ap->ob_item)[i] = (short)x;
224 return 0;
225}
Guido van Rossum549ab711997-01-03 19:09:47 +0000226
227static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000228i_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossume77a7571993-11-03 15:01:26 +0000229{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000230 return PyInt_FromLong((long) ((int *)ap->ob_item)[i]);
Guido van Rossume77a7571993-11-03 15:01:26 +0000231}
232
233static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000234i_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossume77a7571993-11-03 15:01:26 +0000235{
236 int x;
Fred Drake541dc3b2000-06-28 17:49:30 +0000237 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000238 if (!PyArg_Parse(v, "i;array item must be integer", &x))
Guido van Rossume77a7571993-11-03 15:01:26 +0000239 return -1;
240 if (i >= 0)
241 ((int *)ap->ob_item)[i] = x;
242 return 0;
243}
244
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000245static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000246II_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000247{
248 return PyLong_FromUnsignedLong(
249 (unsigned long) ((unsigned int *)ap->ob_item)[i]);
250}
251
252static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000253II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000254{
255 unsigned long x;
256 if (PyLong_Check(v)) {
257 x = PyLong_AsUnsignedLong(v);
258 if (x == (unsigned long) -1 && PyErr_Occurred())
259 return -1;
260 }
261 else {
Fred Drake541dc3b2000-06-28 17:49:30 +0000262 long y;
263 if (!PyArg_Parse(v, "l;array item must be integer", &y))
Guido van Rossum549ab711997-01-03 19:09:47 +0000264 return -1;
Fred Drake541dc3b2000-06-28 17:49:30 +0000265 if (y < 0) {
266 PyErr_SetString(PyExc_OverflowError,
267 "unsigned int is less than minimum");
268 return -1;
269 }
270 x = (unsigned long)y;
Tim Petersbb307342000-09-10 05:22:54 +0000271
Guido van Rossum549ab711997-01-03 19:09:47 +0000272 }
Fred Drake541dc3b2000-06-28 17:49:30 +0000273 if (x > UINT_MAX) {
274 PyErr_SetString(PyExc_OverflowError,
275 "unsigned int is greater than maximum");
276 return -1;
277 }
278
Guido van Rossum549ab711997-01-03 19:09:47 +0000279 if (i >= 0)
Fred Drake541dc3b2000-06-28 17:49:30 +0000280 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
Guido van Rossum549ab711997-01-03 19:09:47 +0000281 return 0;
282}
283
284static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000285l_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000286{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000287 return PyInt_FromLong(((long *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000288}
289
290static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000291l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000292{
293 long x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000294 if (!PyArg_Parse(v, "l;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000295 return -1;
296 if (i >= 0)
297 ((long *)ap->ob_item)[i] = x;
298 return 0;
299}
300
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000301static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000302LL_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000303{
304 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
305}
306
307static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000308LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000309{
310 unsigned long x;
311 if (PyLong_Check(v)) {
312 x = PyLong_AsUnsignedLong(v);
313 if (x == (unsigned long) -1 && PyErr_Occurred())
314 return -1;
315 }
316 else {
Fred Drake541dc3b2000-06-28 17:49:30 +0000317 long y;
318 if (!PyArg_Parse(v, "l;array item must be integer", &y))
Guido van Rossum549ab711997-01-03 19:09:47 +0000319 return -1;
Fred Drake541dc3b2000-06-28 17:49:30 +0000320 if (y < 0) {
321 PyErr_SetString(PyExc_OverflowError,
322 "unsigned long is less than minimum");
323 return -1;
324 }
325 x = (unsigned long)y;
Tim Petersbb307342000-09-10 05:22:54 +0000326
Guido van Rossum549ab711997-01-03 19:09:47 +0000327 }
Fred Drake541dc3b2000-06-28 17:49:30 +0000328 if (x > ULONG_MAX) {
329 PyErr_SetString(PyExc_OverflowError,
330 "unsigned long is greater than maximum");
331 return -1;
332 }
Tim Petersbb307342000-09-10 05:22:54 +0000333
Guido van Rossum549ab711997-01-03 19:09:47 +0000334 if (i >= 0)
335 ((unsigned long *)ap->ob_item)[i] = x;
336 return 0;
337}
338
339static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000340f_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000341{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000342 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000343}
344
345static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000346f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000347{
348 float x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000349 if (!PyArg_Parse(v, "f;array item must be float", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000350 return -1;
351 if (i >= 0)
352 ((float *)ap->ob_item)[i] = x;
353 return 0;
354}
355
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000356static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000357d_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000358{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000359 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000360}
361
362static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000363d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000364{
365 double x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000366 if (!PyArg_Parse(v, "d;array item must be float", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000367 return -1;
368 if (i >= 0)
369 ((double *)ap->ob_item)[i] = x;
370 return 0;
371}
372
373/* Description of types */
Guido van Rossum234f9421993-06-17 12:35:49 +0000374static struct arraydescr descriptors[] = {
Guido van Rossum778983b1993-02-19 15:55:02 +0000375 {'b', sizeof(char), b_getitem, b_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000376 {'B', sizeof(char), BB_getitem, BB_setitem},
Martin v. Löwis99866332002-03-01 10:27:01 +0000377 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem},
Guido van Rossum778983b1993-02-19 15:55:02 +0000378 {'h', sizeof(short), h_getitem, h_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000379 {'H', sizeof(short), HH_getitem, HH_setitem},
Guido van Rossume77a7571993-11-03 15:01:26 +0000380 {'i', sizeof(int), i_getitem, i_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000381 {'I', sizeof(int), II_getitem, II_setitem},
Guido van Rossum778983b1993-02-19 15:55:02 +0000382 {'l', sizeof(long), l_getitem, l_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000383 {'L', sizeof(long), LL_getitem, LL_setitem},
Guido van Rossum778983b1993-02-19 15:55:02 +0000384 {'f', sizeof(float), f_getitem, f_setitem},
385 {'d', sizeof(double), d_getitem, d_setitem},
386 {'\0', 0, 0, 0} /* Sentinel */
387};
Tim Petersbb307342000-09-10 05:22:54 +0000388
389/****************************************************************************
390Implementations of array object methods.
391****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +0000392
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000393static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000394newarrayobject(PyTypeObject *type, Py_ssize_t size, struct arraydescr *descr)
Guido van Rossum778983b1993-02-19 15:55:02 +0000395{
Guido van Rossum778983b1993-02-19 15:55:02 +0000396 arrayobject *op;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000397 size_t nbytes;
Martin v. Löwis99866332002-03-01 10:27:01 +0000398
Guido van Rossum778983b1993-02-19 15:55:02 +0000399 if (size < 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000400 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000401 return NULL;
402 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000403
Guido van Rossum778983b1993-02-19 15:55:02 +0000404 nbytes = size * descr->itemsize;
405 /* Check for overflow */
Guido van Rossum7844e381997-04-11 20:44:04 +0000406 if (nbytes / descr->itemsize != (size_t)size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000407 return PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000408 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000409 op = (arrayobject *) type->tp_alloc(type, 0);
Guido van Rossum778983b1993-02-19 15:55:02 +0000410 if (op == NULL) {
Martin v. Löwis99866332002-03-01 10:27:01 +0000411 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +0000412 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000413 Py_Size(op) = size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000414 if (size <= 0) {
415 op->ob_item = NULL;
416 }
417 else {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000418 op->ob_item = PyMem_NEW(char, nbytes);
Guido van Rossum778983b1993-02-19 15:55:02 +0000419 if (op->ob_item == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000420 PyObject_Del(op);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000421 return PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000422 }
423 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000424 op->ob_descr = descr;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000425 op->allocated = size;
Raymond Hettingercb87bc82004-05-31 00:35:52 +0000426 op->weakreflist = NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000427 return (PyObject *) op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000428}
429
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000430static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000431getarrayitem(PyObject *op, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000432{
433 register arrayobject *ap;
Martin v. Löwis99866332002-03-01 10:27:01 +0000434 assert(array_Check(op));
Guido van Rossum778983b1993-02-19 15:55:02 +0000435 ap = (arrayobject *)op;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000436 assert(i>=0 && i<Py_Size(ap));
Guido van Rossum778983b1993-02-19 15:55:02 +0000437 return (*ap->ob_descr->getitem)(ap, i);
438}
439
440static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000441ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000442{
Guido van Rossum778983b1993-02-19 15:55:02 +0000443 char *items;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000444 Py_ssize_t n = Py_Size(self);
Guido van Rossum778983b1993-02-19 15:55:02 +0000445 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000446 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000447 return -1;
448 }
449 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
450 return -1;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000451
452 if (array_resize(self, n+1) == -1)
Guido van Rossum778983b1993-02-19 15:55:02 +0000453 return -1;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000454 items = self->ob_item;
Walter Dörwald9e46abe2003-05-18 03:15:10 +0000455 if (where < 0) {
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000456 where += n;
Walter Dörwald9e46abe2003-05-18 03:15:10 +0000457 if (where < 0)
458 where = 0;
459 }
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000460 if (where > n)
461 where = n;
462 /* appends don't need to call memmove() */
463 if (where != n)
464 memmove(items + (where+1)*self->ob_descr->itemsize,
465 items + where*self->ob_descr->itemsize,
466 (n-where)*self->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000467 return (*self->ob_descr->setitem)(self, where, v);
468}
469
Guido van Rossum778983b1993-02-19 15:55:02 +0000470/* Methods */
471
472static void
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000473array_dealloc(arrayobject *op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000474{
Raymond Hettingercb87bc82004-05-31 00:35:52 +0000475 if (op->weakreflist != NULL)
476 PyObject_ClearWeakRefs((PyObject *) op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000477 if (op->ob_item != NULL)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000478 PyMem_DEL(op->ob_item);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000479 Py_Type(op)->tp_free((PyObject *)op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000480}
481
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000482static PyObject *
483array_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000484{
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000485 arrayobject *va, *wa;
486 PyObject *vi = NULL;
487 PyObject *wi = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000488 Py_ssize_t i, k;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000489 PyObject *res;
490
Martin v. Löwis99866332002-03-01 10:27:01 +0000491 if (!array_Check(v) || !array_Check(w)) {
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000492 Py_INCREF(Py_NotImplemented);
493 return Py_NotImplemented;
Guido van Rossum778983b1993-02-19 15:55:02 +0000494 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000495
496 va = (arrayobject *)v;
497 wa = (arrayobject *)w;
498
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000499 if (Py_Size(va) != Py_Size(wa) && (op == Py_EQ || op == Py_NE)) {
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000500 /* Shortcut: if the lengths differ, the arrays differ */
501 if (op == Py_EQ)
502 res = Py_False;
503 else
504 res = Py_True;
505 Py_INCREF(res);
506 return res;
507 }
508
509 /* Search for the first index where items are different */
510 k = 1;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000511 for (i = 0; i < Py_Size(va) && i < Py_Size(wa); i++) {
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000512 vi = getarrayitem(v, i);
513 wi = getarrayitem(w, i);
514 if (vi == NULL || wi == NULL) {
515 Py_XDECREF(vi);
516 Py_XDECREF(wi);
517 return NULL;
518 }
519 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
520 if (k == 0)
521 break; /* Keeping vi and wi alive! */
522 Py_DECREF(vi);
523 Py_DECREF(wi);
524 if (k < 0)
525 return NULL;
526 }
527
528 if (k) {
529 /* No more items to compare -- compare sizes */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000530 Py_ssize_t vs = Py_Size(va);
531 Py_ssize_t ws = Py_Size(wa);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000532 int cmp;
533 switch (op) {
534 case Py_LT: cmp = vs < ws; break;
Guido van Rossum2b597e42001-01-25 22:12:43 +0000535 case Py_LE: cmp = vs <= ws; break;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000536 case Py_EQ: cmp = vs == ws; break;
537 case Py_NE: cmp = vs != ws; break;
538 case Py_GT: cmp = vs > ws; break;
539 case Py_GE: cmp = vs >= ws; break;
540 default: return NULL; /* cannot happen */
541 }
542 if (cmp)
543 res = Py_True;
544 else
545 res = Py_False;
546 Py_INCREF(res);
547 return res;
548 }
549
550 /* We have an item that differs. First, shortcuts for EQ/NE */
551 if (op == Py_EQ) {
552 Py_INCREF(Py_False);
553 res = Py_False;
554 }
555 else if (op == Py_NE) {
556 Py_INCREF(Py_True);
557 res = Py_True;
558 }
559 else {
560 /* Compare the final item again using the proper operator */
561 res = PyObject_RichCompare(vi, wi, op);
562 }
563 Py_DECREF(vi);
564 Py_DECREF(wi);
565 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +0000566}
567
Martin v. Löwis18e16552006-02-15 17:27:45 +0000568static Py_ssize_t
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000569array_length(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +0000570{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000571 return Py_Size(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000572}
573
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000574static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000575array_item(arrayobject *a, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000576{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000577 if (i < 0 || i >= Py_Size(a)) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000578 PyErr_SetString(PyExc_IndexError, "array index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000579 return NULL;
580 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000581 return getarrayitem((PyObject *)a, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000582}
583
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000584static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000585array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000586{
587 arrayobject *np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000588 if (ilow < 0)
589 ilow = 0;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000590 else if (ilow > Py_Size(a))
591 ilow = Py_Size(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000592 if (ihigh < 0)
593 ihigh = 0;
594 if (ihigh < ilow)
595 ihigh = ilow;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000596 else if (ihigh > Py_Size(a))
597 ihigh = Py_Size(a);
Martin v. Löwis99866332002-03-01 10:27:01 +0000598 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
Guido van Rossum778983b1993-02-19 15:55:02 +0000599 if (np == NULL)
600 return NULL;
601 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
602 (ihigh-ilow) * a->ob_descr->itemsize);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000603 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000604}
605
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000606static PyObject *
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000607array_copy(arrayobject *a, PyObject *unused)
608{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000609 return array_slice(a, 0, Py_Size(a));
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000610}
611
612PyDoc_STRVAR(copy_doc,
613"copy(array)\n\
614\n\
615 Return a copy of the array.");
616
617static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000618array_concat(arrayobject *a, PyObject *bb)
Guido van Rossum778983b1993-02-19 15:55:02 +0000619{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000620 Py_ssize_t size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000621 arrayobject *np;
Martin v. Löwis99866332002-03-01 10:27:01 +0000622 if (!array_Check(bb)) {
Fred Drake137507e2000-06-01 02:02:46 +0000623 PyErr_Format(PyExc_TypeError,
624 "can only append array (not \"%.200s\") to array",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000625 Py_Type(bb)->tp_name);
Guido van Rossum778983b1993-02-19 15:55:02 +0000626 return NULL;
627 }
628#define b ((arrayobject *)bb)
629 if (a->ob_descr != b->ob_descr) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000630 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000631 return NULL;
632 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000633 size = Py_Size(a) + Py_Size(b);
Martin v. Löwis99866332002-03-01 10:27:01 +0000634 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
Guido van Rossum778983b1993-02-19 15:55:02 +0000635 if (np == NULL) {
636 return NULL;
637 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000638 memcpy(np->ob_item, a->ob_item, Py_Size(a)*a->ob_descr->itemsize);
639 memcpy(np->ob_item + Py_Size(a)*a->ob_descr->itemsize,
640 b->ob_item, Py_Size(b)*b->ob_descr->itemsize);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000641 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000642#undef b
643}
644
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000645static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000646array_repeat(arrayobject *a, Py_ssize_t n)
Guido van Rossum778983b1993-02-19 15:55:02 +0000647{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000648 Py_ssize_t i;
649 Py_ssize_t size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000650 arrayobject *np;
651 char *p;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000652 Py_ssize_t nbytes;
Guido van Rossum778983b1993-02-19 15:55:02 +0000653 if (n < 0)
654 n = 0;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000655 size = Py_Size(a) * n;
Martin v. Löwis99866332002-03-01 10:27:01 +0000656 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
Guido van Rossum778983b1993-02-19 15:55:02 +0000657 if (np == NULL)
658 return NULL;
659 p = np->ob_item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000660 nbytes = Py_Size(a) * a->ob_descr->itemsize;
Guido van Rossum778983b1993-02-19 15:55:02 +0000661 for (i = 0; i < n; i++) {
662 memcpy(p, a->ob_item, nbytes);
663 p += nbytes;
664 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000665 return (PyObject *) np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000666}
667
668static int
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000669array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000670{
671 char *item;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000672 Py_ssize_t n; /* Size of replacement array */
673 Py_ssize_t d; /* Change in size */
Guido van Rossum778983b1993-02-19 15:55:02 +0000674#define b ((arrayobject *)v)
675 if (v == NULL)
676 n = 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000677 else if (array_Check(v)) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000678 n = Py_Size(b);
Guido van Rossum778983b1993-02-19 15:55:02 +0000679 if (a == b) {
680 /* Special case "a[i:j] = a" -- copy b first */
681 int ret;
682 v = array_slice(b, 0, n);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000683 if (!v)
684 return -1;
Guido van Rossum778983b1993-02-19 15:55:02 +0000685 ret = array_ass_slice(a, ilow, ihigh, v);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000686 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000687 return ret;
688 }
689 if (b->ob_descr != a->ob_descr) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000690 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000691 return -1;
692 }
693 }
694 else {
Fred Drake137507e2000-06-01 02:02:46 +0000695 PyErr_Format(PyExc_TypeError,
696 "can only assign array (not \"%.200s\") to array slice",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000697 Py_Type(v)->tp_name);
Guido van Rossum778983b1993-02-19 15:55:02 +0000698 return -1;
699 }
700 if (ilow < 0)
701 ilow = 0;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000702 else if (ilow > Py_Size(a))
703 ilow = Py_Size(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000704 if (ihigh < 0)
705 ihigh = 0;
706 if (ihigh < ilow)
707 ihigh = ilow;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000708 else if (ihigh > Py_Size(a))
709 ihigh = Py_Size(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000710 item = a->ob_item;
711 d = n - (ihigh-ilow);
712 if (d < 0) { /* Delete -d items */
713 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
714 item + ihigh*a->ob_descr->itemsize,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000715 (Py_Size(a)-ihigh)*a->ob_descr->itemsize);
716 Py_Size(a) += d;
717 PyMem_RESIZE(item, char, Py_Size(a)*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000718 /* Can't fail */
719 a->ob_item = item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000720 a->allocated = Py_Size(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000721 }
722 else if (d > 0) { /* Insert d items */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000723 PyMem_RESIZE(item, char,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000724 (Py_Size(a) + d)*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000725 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000726 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000727 return -1;
728 }
729 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
730 item + ihigh*a->ob_descr->itemsize,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000731 (Py_Size(a)-ihigh)*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000732 a->ob_item = item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000733 Py_Size(a) += d;
734 a->allocated = Py_Size(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000735 }
736 if (n > 0)
737 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
738 n*b->ob_descr->itemsize);
739 return 0;
740#undef b
741}
742
743static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000744array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000745{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000746 if (i < 0 || i >= Py_Size(a)) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000747 PyErr_SetString(PyExc_IndexError,
748 "array assignment index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000749 return -1;
750 }
751 if (v == NULL)
752 return array_ass_slice(a, i, i+1, v);
753 return (*a->ob_descr->setitem)(a, i, v);
754}
755
756static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000757setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000758{
Martin v. Löwis99866332002-03-01 10:27:01 +0000759 assert(array_Check(a));
Guido van Rossum778983b1993-02-19 15:55:02 +0000760 return array_ass_item((arrayobject *)a, i, v);
761}
762
Martin v. Löwis99866332002-03-01 10:27:01 +0000763static int
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000764array_iter_extend(arrayobject *self, PyObject *bb)
765{
766 PyObject *it, *v;
767
768 it = PyObject_GetIter(bb);
769 if (it == NULL)
770 return -1;
771
772 while ((v = PyIter_Next(it)) != NULL) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000773 if (ins1(self, (int) Py_Size(self), v) != 0) {
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000774 Py_DECREF(v);
775 Py_DECREF(it);
776 return -1;
777 }
778 Py_DECREF(v);
779 }
780 Py_DECREF(it);
781 if (PyErr_Occurred())
782 return -1;
783 return 0;
784}
785
786static int
Martin v. Löwis99866332002-03-01 10:27:01 +0000787array_do_extend(arrayobject *self, PyObject *bb)
788{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000789 Py_ssize_t size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000790
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000791 if (!array_Check(bb))
792 return array_iter_extend(self, bb);
Martin v. Löwis99866332002-03-01 10:27:01 +0000793#define b ((arrayobject *)bb)
794 if (self->ob_descr != b->ob_descr) {
795 PyErr_SetString(PyExc_TypeError,
796 "can only extend with array of same kind");
797 return -1;
798 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000799 size = Py_Size(self) + Py_Size(b);
Martin v. Löwis99866332002-03-01 10:27:01 +0000800 PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize);
801 if (self->ob_item == NULL) {
802 PyObject_Del(self);
803 PyErr_NoMemory();
804 return -1;
805 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000806 memcpy(self->ob_item + Py_Size(self)*self->ob_descr->itemsize,
807 b->ob_item, Py_Size(b)*b->ob_descr->itemsize);
808 Py_Size(self) = size;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000809 self->allocated = size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000810
811 return 0;
812#undef b
813}
814
815static PyObject *
816array_inplace_concat(arrayobject *self, PyObject *bb)
817{
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000818 if (!array_Check(bb)) {
819 PyErr_Format(PyExc_TypeError,
820 "can only extend array with array (not \"%.200s\")",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000821 Py_Type(bb)->tp_name);
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000822 return NULL;
823 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000824 if (array_do_extend(self, bb) == -1)
825 return NULL;
826 Py_INCREF(self);
827 return (PyObject *)self;
828}
829
830static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000831array_inplace_repeat(arrayobject *self, Py_ssize_t n)
Martin v. Löwis99866332002-03-01 10:27:01 +0000832{
833 char *items, *p;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000834 Py_ssize_t size, i;
Martin v. Löwis99866332002-03-01 10:27:01 +0000835
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000836 if (Py_Size(self) > 0) {
Martin v. Löwis99866332002-03-01 10:27:01 +0000837 if (n < 0)
838 n = 0;
839 items = self->ob_item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000840 size = Py_Size(self) * self->ob_descr->itemsize;
Martin v. Löwis99866332002-03-01 10:27:01 +0000841 if (n == 0) {
842 PyMem_FREE(items);
843 self->ob_item = NULL;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000844 Py_Size(self) = 0;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000845 self->allocated = 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000846 }
847 else {
848 PyMem_Resize(items, char, n * size);
849 if (items == NULL)
850 return PyErr_NoMemory();
851 p = items;
852 for (i = 1; i < n; i++) {
853 p += size;
854 memcpy(p, items, size);
855 }
856 self->ob_item = items;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000857 Py_Size(self) *= n;
858 self->allocated = Py_Size(self);
Martin v. Löwis99866332002-03-01 10:27:01 +0000859 }
860 }
861 Py_INCREF(self);
862 return (PyObject *)self;
863}
864
865
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000866static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000867ins(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000868{
869 if (ins1(self, where, v) != 0)
870 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000871 Py_INCREF(Py_None);
872 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000873}
874
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000875static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000876array_count(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000877{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000878 Py_ssize_t count = 0;
879 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000880
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000881 for (i = 0; i < Py_Size(self); i++) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000882 PyObject *selfi = getarrayitem((PyObject *)self, i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000883 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000884 Py_DECREF(selfi);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000885 if (cmp > 0)
886 count++;
887 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000888 return NULL;
889 }
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000890 return PyInt_FromSsize_t(count);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000891}
892
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000893PyDoc_STRVAR(count_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000894"count(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000895\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000896Return number of occurences of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000897
898static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000899array_index(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000900{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000901 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000902
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000903 for (i = 0; i < Py_Size(self); i++) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000904 PyObject *selfi = getarrayitem((PyObject *)self, i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000905 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
906 Py_DECREF(selfi);
907 if (cmp > 0) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000908 return PyInt_FromLong((long)i);
909 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000910 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000911 return NULL;
912 }
913 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
914 return NULL;
915}
916
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000917PyDoc_STRVAR(index_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000918"index(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000919\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000920Return index of first occurence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000921
Raymond Hettinger625812f2003-01-07 01:58:52 +0000922static int
923array_contains(arrayobject *self, PyObject *v)
924{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000925 Py_ssize_t i;
926 int cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +0000927
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000928 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_Size(self); i++) {
Raymond Hettinger625812f2003-01-07 01:58:52 +0000929 PyObject *selfi = getarrayitem((PyObject *)self, i);
930 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
931 Py_DECREF(selfi);
932 }
933 return cmp;
934}
935
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000936static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000937array_remove(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000938{
939 int i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000940
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000941 for (i = 0; i < Py_Size(self); i++) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000942 PyObject *selfi = getarrayitem((PyObject *)self,i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000943 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
944 Py_DECREF(selfi);
945 if (cmp > 0) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000946 if (array_ass_slice(self, i, i+1,
947 (PyObject *)NULL) != 0)
948 return NULL;
949 Py_INCREF(Py_None);
950 return Py_None;
951 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000952 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000953 return NULL;
954 }
955 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
956 return NULL;
957}
958
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000959PyDoc_STRVAR(remove_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000960"remove(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000961\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000962Remove the first occurence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000963
964static PyObject *
965array_pop(arrayobject *self, PyObject *args)
966{
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000967 Py_ssize_t i = -1;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000968 PyObject *v;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000969 if (!PyArg_ParseTuple(args, "|n:pop", &i))
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000970 return NULL;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000971 if (Py_Size(self) == 0) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000972 /* Special-case most common failure cause */
973 PyErr_SetString(PyExc_IndexError, "pop from empty array");
974 return NULL;
975 }
976 if (i < 0)
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000977 i += Py_Size(self);
978 if (i < 0 || i >= Py_Size(self)) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000979 PyErr_SetString(PyExc_IndexError, "pop index out of range");
980 return NULL;
981 }
982 v = getarrayitem((PyObject *)self,i);
983 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
984 Py_DECREF(v);
985 return NULL;
986 }
987 return v;
988}
989
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000990PyDoc_STRVAR(pop_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000991"pop([i])\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000992\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000993Return the i-th element and delete it from the array. i defaults to -1.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000994
995static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000996array_extend(arrayobject *self, PyObject *bb)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000997{
Martin v. Löwis99866332002-03-01 10:27:01 +0000998 if (array_do_extend(self, bb) == -1)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000999 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001000 Py_INCREF(Py_None);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001001 return Py_None;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001002}
1003
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001004PyDoc_STRVAR(extend_doc,
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001005"extend(array or iterable)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001006\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001007 Append items to the end of the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001008
1009static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001010array_insert(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001011{
Thomas Woutersb9eb5512006-02-27 19:44:56 +00001012 Py_ssize_t i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001013 PyObject *v;
Thomas Woutersb9eb5512006-02-27 19:44:56 +00001014 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
Guido van Rossum778983b1993-02-19 15:55:02 +00001015 return NULL;
1016 return ins(self, i, v);
1017}
1018
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001019PyDoc_STRVAR(insert_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001020"insert(i,x)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001021\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001022Insert a new item x into the array before position i.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001023
1024
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001025static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001026array_buffer_info(arrayobject *self, PyObject *unused)
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001027{
Tim Peters077a11d2000-09-16 22:31:29 +00001028 PyObject* retval = NULL;
Tim Peters077a11d2000-09-16 22:31:29 +00001029 retval = PyTuple_New(2);
1030 if (!retval)
1031 return NULL;
Fred Drake541dc3b2000-06-28 17:49:30 +00001032
1033 PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item));
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001034 PyTuple_SET_ITEM(retval, 1, PyInt_FromLong((long)(Py_Size(self))));
Fred Drake541dc3b2000-06-28 17:49:30 +00001035
1036 return retval;
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001037}
1038
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001039PyDoc_STRVAR(buffer_info_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001040"buffer_info() -> (address, length)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001041\n\
1042Return a tuple (address, length) giving the current memory address and\n\
Guido van Rossum702d08e2001-07-27 16:05:32 +00001043the length in items of the buffer used to hold array's contents\n\
1044The length should be multiplied by the itemsize attribute to calculate\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001045the buffer length in bytes.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001046
1047
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001048static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001049array_append(arrayobject *self, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +00001050{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001051 return ins(self, (int) Py_Size(self), v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001052}
1053
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001054PyDoc_STRVAR(append_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001055"append(x)\n\
1056\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001057Append new value x to the end of the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001058
1059
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001060static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001061array_byteswap(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001062{
1063 char *p;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001064 Py_ssize_t i;
Fred Drakebf272981999-12-03 17:15:30 +00001065
Guido van Rossum778983b1993-02-19 15:55:02 +00001066 switch (self->ob_descr->itemsize) {
1067 case 1:
1068 break;
1069 case 2:
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001070 for (p = self->ob_item, i = Py_Size(self); --i >= 0; p += 2) {
Guido van Rossum778983b1993-02-19 15:55:02 +00001071 char p0 = p[0];
1072 p[0] = p[1];
1073 p[1] = p0;
1074 }
1075 break;
1076 case 4:
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001077 for (p = self->ob_item, i = Py_Size(self); --i >= 0; p += 4) {
Guido van Rossum778983b1993-02-19 15:55:02 +00001078 char p0 = p[0];
1079 char p1 = p[1];
1080 p[0] = p[3];
1081 p[1] = p[2];
1082 p[2] = p1;
1083 p[3] = p0;
1084 }
1085 break;
Guido van Rossume77a7571993-11-03 15:01:26 +00001086 case 8:
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001087 for (p = self->ob_item, i = Py_Size(self); --i >= 0; p += 8) {
Guido van Rossume77a7571993-11-03 15:01:26 +00001088 char p0 = p[0];
1089 char p1 = p[1];
1090 char p2 = p[2];
1091 char p3 = p[3];
1092 p[0] = p[7];
1093 p[1] = p[6];
1094 p[2] = p[5];
1095 p[3] = p[4];
1096 p[4] = p3;
1097 p[5] = p2;
1098 p[6] = p1;
1099 p[7] = p0;
1100 }
1101 break;
Guido van Rossum778983b1993-02-19 15:55:02 +00001102 default:
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001103 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossum778983b1993-02-19 15:55:02 +00001104 "don't know how to byteswap this array type");
1105 return NULL;
1106 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001107 Py_INCREF(Py_None);
1108 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001109}
1110
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001111PyDoc_STRVAR(byteswap_doc,
Fred Drakebf272981999-12-03 17:15:30 +00001112"byteswap()\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001113\n\
Fred Drakebf272981999-12-03 17:15:30 +00001114Byteswap all items of the array. If the items in the array are not 1, 2,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011154, or 8 bytes in size, RuntimeError is raised.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001116
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001117static PyObject *
Raymond Hettingerb0900e62004-12-16 16:23:40 +00001118array_reduce(arrayobject *array)
1119{
1120 PyObject *dict, *result;
1121
1122 dict = PyObject_GetAttrString((PyObject *)array, "__dict__");
1123 if (dict == NULL) {
1124 PyErr_Clear();
1125 dict = Py_None;
1126 Py_INCREF(dict);
1127 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001128 if (Py_Size(array) > 0) {
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001129 result = Py_BuildValue("O(cy#)O",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001130 Py_Type(array),
Guido van Rossumd8faa362007-04-27 19:54:29 +00001131 array->ob_descr->typecode,
1132 array->ob_item,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001133 Py_Size(array) * array->ob_descr->itemsize,
Guido van Rossumd8faa362007-04-27 19:54:29 +00001134 dict);
1135 } else {
1136 result = Py_BuildValue("O(c)O",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001137 Py_Type(array),
Guido van Rossumd8faa362007-04-27 19:54:29 +00001138 array->ob_descr->typecode,
1139 dict);
1140 }
Raymond Hettingerb0900e62004-12-16 16:23:40 +00001141 Py_DECREF(dict);
1142 return result;
1143}
1144
1145PyDoc_STRVAR(array_doc, "Return state information for pickling.");
1146
1147static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001148array_reverse(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001149{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001150 register Py_ssize_t itemsize = self->ob_descr->itemsize;
Guido van Rossume77a7571993-11-03 15:01:26 +00001151 register char *p, *q;
Tim Peters077a11d2000-09-16 22:31:29 +00001152 /* little buffer to hold items while swapping */
1153 char tmp[256]; /* 8 is probably enough -- but why skimp */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001154 assert((size_t)itemsize <= sizeof(tmp));
Guido van Rossume77a7571993-11-03 15:01:26 +00001155
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001156 if (Py_Size(self) > 1) {
Guido van Rossume77a7571993-11-03 15:01:26 +00001157 for (p = self->ob_item,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001158 q = self->ob_item + (Py_Size(self) - 1)*itemsize;
Guido van Rossume77a7571993-11-03 15:01:26 +00001159 p < q;
1160 p += itemsize, q -= itemsize) {
Tim Peters077a11d2000-09-16 22:31:29 +00001161 /* memory areas guaranteed disjoint, so memcpy
1162 * is safe (& memmove may be slower).
1163 */
1164 memcpy(tmp, p, itemsize);
1165 memcpy(p, q, itemsize);
1166 memcpy(q, tmp, itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001167 }
1168 }
Tim Petersbb307342000-09-10 05:22:54 +00001169
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001170 Py_INCREF(Py_None);
1171 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001172}
Guido van Rossume77a7571993-11-03 15:01:26 +00001173
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001174PyDoc_STRVAR(reverse_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001175"reverse()\n\
1176\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001177Reverse the order of the items in the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001178
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001179
1180/* Forward */
1181static PyObject *array_fromstring(arrayobject *self, PyObject *args);
1182
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001183static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001184array_fromfile(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001185{
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001186 PyObject *f, *b, *res;
1187 Py_ssize_t itemsize = self->ob_descr->itemsize;
1188 Py_ssize_t n, nbytes;
1189
Thomas Wouters7a2f83b2006-02-16 17:07:41 +00001190 if (!PyArg_ParseTuple(args, "On:fromfile", &f, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +00001191 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001192
1193 nbytes = n * itemsize;
1194 if (nbytes < 0 || nbytes/itemsize != n) {
1195 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001196 return NULL;
1197 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001198
1199 b = PyObject_CallMethod(f, "read", "n", nbytes);
1200 if (b == NULL)
1201 return NULL;
1202
1203 if (!PyBytes_Check(b)) {
1204 PyErr_SetString(PyExc_TypeError,
1205 "read() didn't return bytes");
1206 Py_DECREF(b);
1207 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001208 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001209
1210 if (PyBytes_GET_SIZE(b) != nbytes) {
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001211 PyErr_SetString(PyExc_EOFError,
1212 "read() didn't return enough bytes");
1213 Py_DECREF(b);
1214 return NULL;
1215 }
1216
1217 args = Py_BuildValue("(O)", b);
1218 Py_DECREF(b);
1219 if (args == NULL)
1220 return NULL;
1221
1222 res = array_fromstring(self, args);
1223 Py_DECREF(args);
1224
1225 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +00001226}
1227
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001228PyDoc_STRVAR(fromfile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001229"fromfile(f, n)\n\
1230\n\
1231Read n objects from the file object f and append them to the end of the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001232array. Also called as read.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001233
1234
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001235static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001236array_tofile(arrayobject *self, PyObject *f)
Guido van Rossum778983b1993-02-19 15:55:02 +00001237{
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001238 Py_ssize_t nbytes = self->ob_size * self->ob_descr->itemsize;
1239 /* Write 64K blocks at a time */
1240 /* XXX Make the block size settable */
1241 int BLOCKSIZE = 64*1024;
1242 Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE;
1243 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001244
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001245 if (Py_Size(self) == 0)
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001246 goto done;
1247
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001248 for (i = 0; i < nblocks; i++) {
1249 char* ptr = self->ob_item + i*BLOCKSIZE;
1250 Py_ssize_t size = BLOCKSIZE;
1251 PyObject *bytes, *res;
1252 if (i*BLOCKSIZE + size > nbytes)
1253 size = nbytes - i*BLOCKSIZE;
1254 bytes = PyBytes_FromStringAndSize(ptr, size);
1255 if (bytes == NULL)
Guido van Rossum778983b1993-02-19 15:55:02 +00001256 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001257 res = PyObject_CallMethod(f, "write", "O", bytes);
1258 Py_DECREF(bytes);
1259 if (res == NULL)
1260 return NULL;
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001261 }
1262
1263 done:
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001264 Py_INCREF(Py_None);
1265 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001266}
1267
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001268PyDoc_STRVAR(tofile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001269"tofile(f)\n\
1270\n\
1271Write all items (as machine values) to the file object f. Also called as\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001272write.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001273
1274
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001275static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001276array_fromlist(arrayobject *self, PyObject *list)
Guido van Rossum778983b1993-02-19 15:55:02 +00001277{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001278 Py_ssize_t n;
1279 Py_ssize_t itemsize = self->ob_descr->itemsize;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001280
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001281 if (!PyList_Check(list)) {
1282 PyErr_SetString(PyExc_TypeError, "arg must be list");
Guido van Rossum778983b1993-02-19 15:55:02 +00001283 return NULL;
1284 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001285 n = PyList_Size(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001286 if (n > 0) {
1287 char *item = self->ob_item;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001288 Py_ssize_t i;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001289 PyMem_RESIZE(item, char, (Py_Size(self) + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001290 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001291 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001292 return NULL;
1293 }
1294 self->ob_item = item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001295 Py_Size(self) += n;
1296 self->allocated = Py_Size(self);
Guido van Rossum778983b1993-02-19 15:55:02 +00001297 for (i = 0; i < n; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001298 PyObject *v = PyList_GetItem(list, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001299 if ((*self->ob_descr->setitem)(self,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001300 Py_Size(self) - n + i, v) != 0) {
1301 Py_Size(self) -= n;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001302 PyMem_RESIZE(item, char,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001303 Py_Size(self) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001304 self->ob_item = item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001305 self->allocated = Py_Size(self);
Guido van Rossum778983b1993-02-19 15:55:02 +00001306 return NULL;
1307 }
1308 }
1309 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001310 Py_INCREF(Py_None);
1311 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001312}
1313
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001314PyDoc_STRVAR(fromlist_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001315"fromlist(list)\n\
1316\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001317Append items to array from list.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001318
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001319static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001320array_tolist(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001321{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001322 PyObject *list = PyList_New(Py_Size(self));
Martin v. Löwis18e16552006-02-15 17:27:45 +00001323 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001324
Guido van Rossum778983b1993-02-19 15:55:02 +00001325 if (list == NULL)
1326 return NULL;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001327 for (i = 0; i < Py_Size(self); i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001328 PyObject *v = getarrayitem((PyObject *)self, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001329 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001330 Py_DECREF(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001331 return NULL;
1332 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001333 PyList_SetItem(list, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001334 }
1335 return list;
1336}
1337
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001338PyDoc_STRVAR(tolist_doc,
Guido van Rossumfc6aba51998-10-14 02:52:31 +00001339"tolist() -> list\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001340\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001341Convert array to an ordinary list with the same items.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001342
1343
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001344static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001345array_fromstring(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001346{
1347 char *str;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001348 Py_ssize_t n;
Guido van Rossum778983b1993-02-19 15:55:02 +00001349 int itemsize = self->ob_descr->itemsize;
Peter Schneider-Kampdc71cac2000-07-31 21:57:30 +00001350 if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +00001351 return NULL;
1352 if (n % itemsize != 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001353 PyErr_SetString(PyExc_ValueError,
Guido van Rossum778983b1993-02-19 15:55:02 +00001354 "string length not a multiple of item size");
1355 return NULL;
1356 }
1357 n = n / itemsize;
1358 if (n > 0) {
1359 char *item = self->ob_item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001360 PyMem_RESIZE(item, char, (Py_Size(self) + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001361 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001362 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001363 return NULL;
1364 }
1365 self->ob_item = item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001366 Py_Size(self) += n;
1367 self->allocated = Py_Size(self);
1368 memcpy(item + (Py_Size(self) - n) * itemsize,
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001369 str, itemsize*n);
Guido van Rossum778983b1993-02-19 15:55:02 +00001370 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001371 Py_INCREF(Py_None);
1372 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001373}
1374
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001375PyDoc_STRVAR(fromstring_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001376"fromstring(string)\n\
1377\n\
1378Appends items from the string, interpreting it as an array of machine\n\
Walter Dörwald93b30b52007-06-22 12:21:53 +00001379values, as if it had been read from a file using the fromfile() method).");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001380
1381
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001382static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001383array_tostring(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001384{
Guido van Rossum31f72d72007-06-18 18:44:28 +00001385 return PyBytes_FromStringAndSize(self->ob_item,
1386 self->ob_size * self->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001387}
1388
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001389PyDoc_STRVAR(tostring_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001390"tostring() -> string\n\
1391\n\
1392Convert the array to an array of machine values and return the string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001393representation.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001394
Martin v. Löwis99866332002-03-01 10:27:01 +00001395
1396
Martin v. Löwis99866332002-03-01 10:27:01 +00001397static PyObject *
1398array_fromunicode(arrayobject *self, PyObject *args)
1399{
1400 Py_UNICODE *ustr;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001401 Py_ssize_t n;
Martin v. Löwis99866332002-03-01 10:27:01 +00001402
1403 if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
1404 return NULL;
1405 if (self->ob_descr->typecode != 'u') {
1406 PyErr_SetString(PyExc_ValueError,
1407 "fromunicode() may only be called on "
1408 "type 'u' arrays");
1409 return NULL;
1410 }
1411 if (n > 0) {
1412 Py_UNICODE *item = (Py_UNICODE *) self->ob_item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001413 PyMem_RESIZE(item, Py_UNICODE, Py_Size(self) + n);
Martin v. Löwis99866332002-03-01 10:27:01 +00001414 if (item == NULL) {
1415 PyErr_NoMemory();
1416 return NULL;
1417 }
1418 self->ob_item = (char *) item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001419 Py_Size(self) += n;
1420 self->allocated = Py_Size(self);
1421 memcpy(item + Py_Size(self) - n,
Martin v. Löwis99866332002-03-01 10:27:01 +00001422 ustr, n * sizeof(Py_UNICODE));
1423 }
1424
1425 Py_INCREF(Py_None);
1426 return Py_None;
1427}
1428
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001429PyDoc_STRVAR(fromunicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001430"fromunicode(ustr)\n\
1431\n\
1432Extends this array with data from the unicode string ustr.\n\
1433The array must be a type 'u' array; otherwise a ValueError\n\
1434is raised. Use array.fromstring(ustr.decode(...)) to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001435append Unicode data to an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001436
1437
1438static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001439array_tounicode(arrayobject *self, PyObject *unused)
Martin v. Löwis99866332002-03-01 10:27:01 +00001440{
Martin v. Löwis99866332002-03-01 10:27:01 +00001441 if (self->ob_descr->typecode != 'u') {
1442 PyErr_SetString(PyExc_ValueError,
1443 "tounicode() may only be called on type 'u' arrays");
1444 return NULL;
1445 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001446 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_Size(self));
Martin v. Löwis99866332002-03-01 10:27:01 +00001447}
1448
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001449PyDoc_STRVAR(tounicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001450"tounicode() -> unicode\n\
1451\n\
1452Convert the array to a unicode string. The array must be\n\
1453a type 'u' array; otherwise a ValueError is raised. Use\n\
1454array.tostring().decode() to obtain a unicode string from\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001455an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001456
Martin v. Löwis99866332002-03-01 10:27:01 +00001457
1458
1459static PyObject *
1460array_get_typecode(arrayobject *a, void *closure)
1461{
1462 char tc = a->ob_descr->typecode;
Walter Dörwald93b30b52007-06-22 12:21:53 +00001463 return PyUnicode_FromStringAndSize(&tc, 1);
Martin v. Löwis99866332002-03-01 10:27:01 +00001464}
1465
1466static PyObject *
1467array_get_itemsize(arrayobject *a, void *closure)
1468{
1469 return PyInt_FromLong((long)a->ob_descr->itemsize);
1470}
1471
1472static PyGetSetDef array_getsets [] = {
1473 {"typecode", (getter) array_get_typecode, NULL,
1474 "the typecode character used to create the array"},
1475 {"itemsize", (getter) array_get_itemsize, NULL,
1476 "the size, in bytes, of one array item"},
1477 {NULL}
1478};
1479
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001480PyMethodDef array_methods[] = {
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001481 {"append", (PyCFunction)array_append, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001482 append_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001483 {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001484 buffer_info_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001485 {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001486 byteswap_doc},
Raymond Hettinger3aa82c02004-03-13 18:18:51 +00001487 {"__copy__", (PyCFunction)array_copy, METH_NOARGS,
1488 copy_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001489 {"count", (PyCFunction)array_count, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001490 count_doc},
Thomas Wouters89f507f2006-12-13 04:49:30 +00001491 {"__deepcopy__",(PyCFunction)array_copy, METH_O,
Raymond Hettinger3aa82c02004-03-13 18:18:51 +00001492 copy_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001493 {"extend", (PyCFunction)array_extend, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001494 extend_doc},
1495 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
1496 fromfile_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001497 {"fromlist", (PyCFunction)array_fromlist, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001498 fromlist_doc},
1499 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
1500 fromstring_doc},
Martin v. Löwis99866332002-03-01 10:27:01 +00001501 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
1502 fromunicode_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001503 {"index", (PyCFunction)array_index, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001504 index_doc},
1505 {"insert", (PyCFunction)array_insert, METH_VARARGS,
1506 insert_doc},
1507 {"pop", (PyCFunction)array_pop, METH_VARARGS,
1508 pop_doc},
1509 {"read", (PyCFunction)array_fromfile, METH_VARARGS,
1510 fromfile_doc},
Raymond Hettingerb0900e62004-12-16 16:23:40 +00001511 {"__reduce__", (PyCFunction)array_reduce, METH_NOARGS,
1512 array_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001513 {"remove", (PyCFunction)array_remove, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001514 remove_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001515 {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001516 reverse_doc},
1517/* {"sort", (PyCFunction)array_sort, METH_VARARGS,
1518 sort_doc},*/
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001519 {"tofile", (PyCFunction)array_tofile, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001520 tofile_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001521 {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001522 tolist_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001523 {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001524 tostring_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001525 {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
Martin v. Löwis99866332002-03-01 10:27:01 +00001526 tounicode_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001527 {"write", (PyCFunction)array_tofile, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001528 tofile_doc},
Guido van Rossum778983b1993-02-19 15:55:02 +00001529 {NULL, NULL} /* sentinel */
1530};
1531
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001532static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001533array_repr(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +00001534{
Walter Dörwaldc2f6a582007-05-20 12:49:47 +00001535 char typecode;
Walter Dörwald7569dfe2007-05-19 21:49:49 +00001536 PyObject *s, *v = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001537 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00001538
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001539 len = Py_Size(a);
Martin v. Löwis99866332002-03-01 10:27:01 +00001540 typecode = a->ob_descr->typecode;
Guido van Rossum778983b1993-02-19 15:55:02 +00001541 if (len == 0) {
Walter Dörwald7569dfe2007-05-19 21:49:49 +00001542 return PyUnicode_FromFormat("array('%c')", typecode);
Guido van Rossum778983b1993-02-19 15:55:02 +00001543 }
Guido van Rossum31f72d72007-06-18 18:44:28 +00001544 if (typecode == 'u')
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001545 v = array_tounicode(a, NULL);
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001546 else
1547 v = array_tolist(a, NULL);
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001548
Walter Dörwald7569dfe2007-05-19 21:49:49 +00001549 s = PyUnicode_FromFormat("array('%c', %R)", typecode, v);
1550 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001551 return s;
1552}
1553
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001554static PyObject*
1555array_subscr(arrayobject* self, PyObject* item)
1556{
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001557 if (PyIndex_Check(item)) {
1558 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001559 if (i==-1 && PyErr_Occurred()) {
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001560 return NULL;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001561 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001562 if (i < 0)
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001563 i += Py_Size(self);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001564 return array_item(self, i);
1565 }
1566 else if (PySlice_Check(item)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001567 Py_ssize_t start, stop, step, slicelength, cur, i;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001568 PyObject* result;
1569 arrayobject* ar;
1570 int itemsize = self->ob_descr->itemsize;
1571
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001572 if (PySlice_GetIndicesEx((PySliceObject*)item, Py_Size(self),
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001573 &start, &stop, &step, &slicelength) < 0) {
1574 return NULL;
1575 }
1576
1577 if (slicelength <= 0) {
1578 return newarrayobject(&Arraytype, 0, self->ob_descr);
1579 }
1580 else {
1581 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
1582 if (!result) return NULL;
1583
1584 ar = (arrayobject*)result;
1585
1586 for (cur = start, i = 0; i < slicelength;
1587 cur += step, i++) {
1588 memcpy(ar->ob_item + i*itemsize,
1589 self->ob_item + cur*itemsize,
1590 itemsize);
1591 }
1592
1593 return result;
1594 }
1595 }
1596 else {
1597 PyErr_SetString(PyExc_TypeError,
1598 "list indices must be integers");
1599 return NULL;
1600 }
1601}
1602
1603static int
1604array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
1605{
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001606 if (PyIndex_Check(item)) {
1607 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001608 if (i==-1 && PyErr_Occurred())
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001609 return -1;
1610 if (i < 0)
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001611 i += Py_Size(self);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001612 return array_ass_item(self, i, value);
1613 }
1614 else if (PySlice_Check(item)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001615 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001616 int itemsize = self->ob_descr->itemsize;
1617
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001618 if (PySlice_GetIndicesEx((PySliceObject*)item, Py_Size(self),
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001619 &start, &stop, &step, &slicelength) < 0) {
1620 return -1;
1621 }
1622
1623 /* treat A[slice(a,b)] = v _exactly_ like A[a:b] = v */
1624 if (step == 1 && ((PySliceObject*)item)->step == Py_None)
1625 return array_ass_slice(self, start, stop, value);
1626
1627 if (value == NULL) {
1628 /* delete slice */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001629 Py_ssize_t cur, i, extra;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001630
1631 if (slicelength <= 0)
1632 return 0;
1633
1634 if (step < 0) {
1635 stop = start + 1;
1636 start = stop + step*(slicelength - 1) - 1;
1637 step = -step;
1638 }
1639
Michael W. Hudson56796f62002-07-29 14:35:04 +00001640 for (cur = start, i = 0; i < slicelength - 1;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001641 cur += step, i++) {
1642 memmove(self->ob_item + (cur - i)*itemsize,
1643 self->ob_item + (cur + 1)*itemsize,
1644 (step - 1) * itemsize);
1645 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001646 extra = Py_Size(self) - (cur + 1);
Michael W. Hudson56796f62002-07-29 14:35:04 +00001647 if (extra > 0) {
1648 memmove(self->ob_item + (cur - i)*itemsize,
1649 self->ob_item + (cur + 1)*itemsize,
1650 extra*itemsize);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001651 }
1652
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001653 Py_Size(self) -= slicelength;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001654 self->ob_item = (char *)PyMem_REALLOC(self->ob_item,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001655 itemsize*Py_Size(self));
1656 self->allocated = Py_Size(self);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001657
1658 return 0;
1659 }
1660 else {
1661 /* assign slice */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001662 Py_ssize_t cur, i;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001663 arrayobject* av;
1664
1665 if (!array_Check(value)) {
1666 PyErr_Format(PyExc_TypeError,
1667 "must assign array (not \"%.200s\") to slice",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001668 Py_Type(value)->tp_name);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001669 return -1;
1670 }
1671
1672 av = (arrayobject*)value;
1673
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001674 if (Py_Size(av) != slicelength) {
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001675 PyErr_Format(PyExc_ValueError,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001676 "attempt to assign array of size %ld to extended slice of size %ld",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001677 /*XXX*/(long)Py_Size(av), /*XXX*/(long)slicelength);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001678 return -1;
1679 }
1680
1681 if (!slicelength)
1682 return 0;
1683
1684 /* protect against a[::-1] = a */
1685 if (self == av) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001686 value = array_slice(av, 0, Py_Size(av));
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001687 av = (arrayobject*)value;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001688 if (!av)
1689 return -1;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001690 }
1691 else {
1692 Py_INCREF(value);
1693 }
1694
1695 for (cur = start, i = 0; i < slicelength;
1696 cur += step, i++) {
1697 memcpy(self->ob_item + cur*itemsize,
1698 av->ob_item + i*itemsize,
1699 itemsize);
1700 }
1701
1702 Py_DECREF(value);
1703
1704 return 0;
1705 }
1706 }
1707 else {
1708 PyErr_SetString(PyExc_TypeError,
1709 "list indices must be integers");
1710 return -1;
1711 }
1712}
1713
1714static PyMappingMethods array_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001715 (lenfunc)array_length,
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001716 (binaryfunc)array_subscr,
1717 (objobjargproc)array_ass_subscr
1718};
1719
Guido van Rossumd8faa362007-04-27 19:54:29 +00001720static const void *emptybuf = "";
1721
Martin v. Löwis18e16552006-02-15 17:27:45 +00001722static Py_ssize_t
1723array_buffer_getreadbuf(arrayobject *self, Py_ssize_t index, const void **ptr)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001724{
1725 if ( index != 0 ) {
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001726 PyErr_SetString(PyExc_SystemError,
1727 "Accessing non-existent array segment");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001728 return -1;
1729 }
1730 *ptr = (void *)self->ob_item;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001731 if (*ptr == NULL)
1732 *ptr = emptybuf;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001733 return Py_Size(self)*self->ob_descr->itemsize;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001734}
1735
Martin v. Löwis18e16552006-02-15 17:27:45 +00001736static Py_ssize_t
1737array_buffer_getwritebuf(arrayobject *self, Py_ssize_t index, const void **ptr)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001738{
1739 if ( index != 0 ) {
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001740 PyErr_SetString(PyExc_SystemError,
1741 "Accessing non-existent array segment");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001742 return -1;
1743 }
1744 *ptr = (void *)self->ob_item;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001745 if (*ptr == NULL)
1746 *ptr = emptybuf;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001747 return Py_Size(self)*self->ob_descr->itemsize;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001748}
1749
Martin v. Löwis18e16552006-02-15 17:27:45 +00001750static Py_ssize_t
1751array_buffer_getsegcount(arrayobject *self, Py_ssize_t *lenp)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001752{
1753 if ( lenp )
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001754 *lenp = Py_Size(self)*self->ob_descr->itemsize;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001755 return 1;
1756}
1757
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001758static PySequenceMethods array_as_sequence = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001759 (lenfunc)array_length, /*sq_length*/
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001760 (binaryfunc)array_concat, /*sq_concat*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001761 (ssizeargfunc)array_repeat, /*sq_repeat*/
1762 (ssizeargfunc)array_item, /*sq_item*/
1763 (ssizessizeargfunc)array_slice, /*sq_slice*/
1764 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
1765 (ssizessizeobjargproc)array_ass_slice, /*sq_ass_slice*/
Raymond Hettinger625812f2003-01-07 01:58:52 +00001766 (objobjproc)array_contains, /*sq_contains*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001767 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001768 (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001769};
1770
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001771static PyBufferProcs array_as_buffer = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001772 (readbufferproc)array_buffer_getreadbuf,
1773 (writebufferproc)array_buffer_getwritebuf,
1774 (segcountproc)array_buffer_getsegcount,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001775 NULL,
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001776};
1777
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001778static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +00001779array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum778983b1993-02-19 15:55:02 +00001780{
Guido van Rossum8934fc22007-06-30 23:44:36 +00001781 int c;
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001782 PyObject *initial = NULL, *it = NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001783 struct arraydescr *descr;
Georg Brandl02c42872005-08-26 06:42:30 +00001784
Thomas Woutersb2137042007-02-01 18:02:27 +00001785 if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001786 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001787
Walter Dörwaldd0941302007-07-01 21:58:22 +00001788 if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
Martin v. Löwis99866332002-03-01 10:27:01 +00001789 return NULL;
1790
1791 if (!(initial == NULL || PyList_Check(initial)
Guido van Rossum6b826ab2007-07-03 16:22:09 +00001792 || PyBytes_Check(initial)
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001793 || PyString_Check(initial) || PyTuple_Check(initial)
Martin v. Löwis99866332002-03-01 10:27:01 +00001794 || (c == 'u' && PyUnicode_Check(initial)))) {
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001795 it = PyObject_GetIter(initial);
1796 if (it == NULL)
1797 return NULL;
1798 /* We set initial to NULL so that the subsequent code
1799 will create an empty array of the appropriate type
1800 and afterwards we can use array_iter_extend to populate
1801 the array.
1802 */
1803 initial = NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001804 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001805 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1806 if (descr->typecode == c) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001807 PyObject *a;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001808 Py_ssize_t len;
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001809
1810 if (initial == NULL || !(PyList_Check(initial)
1811 || PyTuple_Check(initial)))
Guido van Rossum778983b1993-02-19 15:55:02 +00001812 len = 0;
1813 else
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001814 len = PySequence_Size(initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001815
1816 a = newarrayobject(type, len, descr);
Guido van Rossum778983b1993-02-19 15:55:02 +00001817 if (a == NULL)
1818 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001819
Guido van Rossum778983b1993-02-19 15:55:02 +00001820 if (len > 0) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001821 Py_ssize_t i;
Guido van Rossum778983b1993-02-19 15:55:02 +00001822 for (i = 0; i < len; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001823 PyObject *v =
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001824 PySequence_GetItem(initial, i);
Raymond Hettinger85004cc2003-08-05 11:23:59 +00001825 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001826 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001827 return NULL;
1828 }
Raymond Hettinger85004cc2003-08-05 11:23:59 +00001829 if (setarrayitem(a, i, v) != 0) {
1830 Py_DECREF(v);
1831 Py_DECREF(a);
1832 return NULL;
1833 }
1834 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001835 }
Guido van Rossum6b826ab2007-07-03 16:22:09 +00001836 } else if (initial != NULL &&
1837 (PyString_Check(initial) || PyBytes_Check(initial))) {
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001838 PyObject *t_initial, *v;
1839 t_initial = PyTuple_Pack(1, initial);
1840 if (t_initial == NULL) {
1841 Py_DECREF(a);
1842 return NULL;
1843 }
1844 v = array_fromstring((arrayobject *)a,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001845 t_initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001846 Py_DECREF(t_initial);
Guido van Rossum778983b1993-02-19 15:55:02 +00001847 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001848 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001849 return NULL;
1850 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001851 Py_DECREF(v);
Martin v. Löwis99866332002-03-01 10:27:01 +00001852 } else if (initial != NULL && PyUnicode_Check(initial)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001853 Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001854 if (n > 0) {
1855 arrayobject *self = (arrayobject *)a;
1856 char *item = self->ob_item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001857 item = (char *)PyMem_Realloc(item, n);
Martin v. Löwis99866332002-03-01 10:27:01 +00001858 if (item == NULL) {
1859 PyErr_NoMemory();
1860 Py_DECREF(a);
1861 return NULL;
1862 }
1863 self->ob_item = item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001864 Py_Size(self) = n / sizeof(Py_UNICODE);
Martin v. Löwis99866332002-03-01 10:27:01 +00001865 memcpy(item, PyUnicode_AS_DATA(initial), n);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001866 self->allocated = Py_Size(self);
Martin v. Löwis99866332002-03-01 10:27:01 +00001867 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001868 }
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001869 if (it != NULL) {
1870 if (array_iter_extend((arrayobject *)a, it) == -1) {
1871 Py_DECREF(it);
1872 Py_DECREF(a);
1873 return NULL;
1874 }
1875 Py_DECREF(it);
1876 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001877 return a;
1878 }
1879 }
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001880 PyErr_SetString(PyExc_ValueError,
Guido van Rossum31f72d72007-06-18 18:44:28 +00001881 "bad typecode (must be b, B, u, h, H, i, I, l, L, f or d)");
Guido van Rossum778983b1993-02-19 15:55:02 +00001882 return NULL;
1883}
1884
Guido van Rossum778983b1993-02-19 15:55:02 +00001885
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001886PyDoc_STRVAR(module_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001887"This module defines an object type which can efficiently represent\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001888an array of basic values: characters, integers, floating point\n\
1889numbers. Arrays are sequence types and behave very much like lists,\n\
1890except that the type of objects stored in them is constrained. The\n\
1891type is specified at object creation time by using a type code, which\n\
1892is a single character. The following type codes are defined:\n\
1893\n\
1894 Type code C Type Minimum size in bytes \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001895 'b' signed integer 1 \n\
1896 'B' unsigned integer 1 \n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001897 'u' Unicode character 2 \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001898 'h' signed integer 2 \n\
1899 'H' unsigned integer 2 \n\
1900 'i' signed integer 2 \n\
1901 'I' unsigned integer 2 \n\
1902 'l' signed integer 4 \n\
1903 'L' unsigned integer 4 \n\
1904 'f' floating point 4 \n\
1905 'd' floating point 8 \n\
1906\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001907The constructor is:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001908\n\
1909array(typecode [, initializer]) -- create a new array\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001910");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001911
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001912PyDoc_STRVAR(arraytype_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001913"array(typecode [, initializer]) -> array\n\
1914\n\
1915Return a new array whose items are restricted by typecode, and\n\
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001916initialized from the optional initializer value, which must be a list,\n\
1917string. or iterable over elements of the appropriate type.\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001918\n\
1919Arrays represent basic values and behave very much like lists, except\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001920the type of objects stored in them is constrained.\n\
1921\n\
1922Methods:\n\
1923\n\
1924append() -- append a new item to the end of the array\n\
1925buffer_info() -- return information giving the current memory info\n\
1926byteswap() -- byteswap all the items of the array\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001927count() -- return number of occurences of an object\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001928extend() -- extend array by appending multiple elements from an iterable\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001929fromfile() -- read items from a file object\n\
1930fromlist() -- append items from the list\n\
1931fromstring() -- append items from the string\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001932index() -- return index of first occurence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001933insert() -- insert a new item into the array at a provided position\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001934pop() -- remove and return item (default last)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001935read() -- DEPRECATED, use fromfile()\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001936remove() -- remove first occurence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001937reverse() -- reverse the order of the items in the array\n\
1938tofile() -- write all items to a file object\n\
1939tolist() -- return the array converted to an ordinary list\n\
1940tostring() -- return the array converted to a string\n\
1941write() -- DEPRECATED, use tofile()\n\
1942\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001943Attributes:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001944\n\
1945typecode -- the typecode character used to create the array\n\
1946itemsize -- the length in bytes of one array item\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001947");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001948
Raymond Hettinger625812f2003-01-07 01:58:52 +00001949static PyObject *array_iter(arrayobject *ao);
1950
Tim Peters0c322792002-07-17 16:49:03 +00001951static PyTypeObject Arraytype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001952 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001953 "array.array",
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001954 sizeof(arrayobject),
1955 0,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001956 (destructor)array_dealloc, /* tp_dealloc */
Martin v. Löwisaa158be2002-03-04 09:38:52 +00001957 0, /* tp_print */
Martin v. Löwis99866332002-03-01 10:27:01 +00001958 0, /* tp_getattr */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001959 0, /* tp_setattr */
1960 0, /* tp_compare */
1961 (reprfunc)array_repr, /* tp_repr */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001962 0, /* tp_as_number*/
1963 &array_as_sequence, /* tp_as_sequence*/
1964 &array_as_mapping, /* tp_as_mapping*/
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001965 0, /* tp_hash */
1966 0, /* tp_call */
1967 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00001968 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001969 0, /* tp_setattro */
Tim Petersb870c752001-06-05 04:43:26 +00001970 &array_as_buffer, /* tp_as_buffer*/
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001971 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001972 arraytype_doc, /* tp_doc */
1973 0, /* tp_traverse */
1974 0, /* tp_clear */
1975 array_richcompare, /* tp_richcompare */
Raymond Hettingercb87bc82004-05-31 00:35:52 +00001976 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger625812f2003-01-07 01:58:52 +00001977 (getiterfunc)array_iter, /* tp_iter */
Martin v. Löwis99866332002-03-01 10:27:01 +00001978 0, /* tp_iternext */
1979 array_methods, /* tp_methods */
1980 0, /* tp_members */
1981 array_getsets, /* tp_getset */
1982 0, /* tp_base */
1983 0, /* tp_dict */
1984 0, /* tp_descr_get */
1985 0, /* tp_descr_set */
1986 0, /* tp_dictoffset */
1987 0, /* tp_init */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00001988 PyType_GenericAlloc, /* tp_alloc */
Martin v. Löwis99866332002-03-01 10:27:01 +00001989 array_new, /* tp_new */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00001990 PyObject_Del, /* tp_free */
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001991};
1992
Raymond Hettinger625812f2003-01-07 01:58:52 +00001993
1994/*********************** Array Iterator **************************/
1995
1996typedef struct {
1997 PyObject_HEAD
Martin v. Löwis18e16552006-02-15 17:27:45 +00001998 Py_ssize_t index;
Raymond Hettinger625812f2003-01-07 01:58:52 +00001999 arrayobject *ao;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002000 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002001} arrayiterobject;
2002
2003static PyTypeObject PyArrayIter_Type;
2004
2005#define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
2006
2007static PyObject *
2008array_iter(arrayobject *ao)
2009{
2010 arrayiterobject *it;
2011
2012 if (!array_Check(ao)) {
2013 PyErr_BadInternalCall();
2014 return NULL;
2015 }
2016
2017 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
2018 if (it == NULL)
2019 return NULL;
2020
2021 Py_INCREF(ao);
2022 it->ao = ao;
2023 it->index = 0;
2024 it->getitem = ao->ob_descr->getitem;
2025 PyObject_GC_Track(it);
2026 return (PyObject *)it;
2027}
2028
2029static PyObject *
Raymond Hettinger625812f2003-01-07 01:58:52 +00002030arrayiter_next(arrayiterobject *it)
2031{
2032 assert(PyArrayIter_Check(it));
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002033 if (it->index < Py_Size(it->ao))
Raymond Hettinger625812f2003-01-07 01:58:52 +00002034 return (*it->getitem)(it->ao, it->index++);
2035 return NULL;
2036}
2037
2038static void
2039arrayiter_dealloc(arrayiterobject *it)
2040{
2041 PyObject_GC_UnTrack(it);
2042 Py_XDECREF(it->ao);
2043 PyObject_GC_Del(it);
2044}
2045
2046static int
2047arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2048{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002049 Py_VISIT(it->ao);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002050 return 0;
2051}
2052
2053static PyTypeObject PyArrayIter_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002054 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger625812f2003-01-07 01:58:52 +00002055 "arrayiterator", /* tp_name */
2056 sizeof(arrayiterobject), /* tp_basicsize */
2057 0, /* tp_itemsize */
2058 /* methods */
2059 (destructor)arrayiter_dealloc, /* tp_dealloc */
2060 0, /* tp_print */
2061 0, /* tp_getattr */
2062 0, /* tp_setattr */
2063 0, /* tp_compare */
2064 0, /* tp_repr */
2065 0, /* tp_as_number */
2066 0, /* tp_as_sequence */
2067 0, /* tp_as_mapping */
2068 0, /* tp_hash */
2069 0, /* tp_call */
2070 0, /* tp_str */
2071 PyObject_GenericGetAttr, /* tp_getattro */
2072 0, /* tp_setattro */
2073 0, /* tp_as_buffer */
2074 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2075 0, /* tp_doc */
2076 (traverseproc)arrayiter_traverse, /* tp_traverse */
2077 0, /* tp_clear */
2078 0, /* tp_richcompare */
2079 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002080 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger625812f2003-01-07 01:58:52 +00002081 (iternextfunc)arrayiter_next, /* tp_iternext */
2082 0, /* tp_methods */
2083};
2084
2085
2086/*********************** Install Module **************************/
2087
Martin v. Löwis99866332002-03-01 10:27:01 +00002088/* No functions in array module. */
2089static PyMethodDef a_methods[] = {
2090 {NULL, NULL, 0, NULL} /* Sentinel */
2091};
2092
2093
Mark Hammondfe51c6d2002-08-02 02:27:13 +00002094PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00002095initarray(void)
Guido van Rossum778983b1993-02-19 15:55:02 +00002096{
Fred Drakef4e34842002-04-01 03:45:06 +00002097 PyObject *m;
Fred Drake0d40ba42000-02-04 20:33:49 +00002098
Guido van Rossum50e9fb92006-08-17 05:42:55 +00002099 if (PyType_Ready(&Arraytype) < 0)
2100 return;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002101 Py_Type(&PyArrayIter_Type) = &PyType_Type;
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002102 m = Py_InitModule3("array", a_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00002103 if (m == NULL)
2104 return;
Fred Drakef4e34842002-04-01 03:45:06 +00002105
2106 Py_INCREF((PyObject *)&Arraytype);
2107 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
2108 Py_INCREF((PyObject *)&Arraytype);
2109 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
Guido van Rossuma0deb641998-10-14 13:45:06 +00002110 /* No need to check the error here, the caller will do that */
Guido van Rossum778983b1993-02-19 15:55:02 +00002111}