blob: e79847f6fab6ae08aaaa12afb42a92a0729d1c74 [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 *);
Travis E. Oliphantb803c512007-08-20 07:16:33 +000029 char *formats;
Guido van Rossum778983b1993-02-19 15:55:02 +000030};
31
32typedef struct arrayobject {
Georg Brandl1b672672006-02-17 08:56:33 +000033 PyObject_VAR_HEAD
Guido van Rossum778983b1993-02-19 15:55:02 +000034 char *ob_item;
Martin v. Löwis18e16552006-02-15 17:27:45 +000035 Py_ssize_t allocated;
Guido van Rossum778983b1993-02-19 15:55:02 +000036 struct arraydescr *ob_descr;
Raymond Hettingercb87bc82004-05-31 00:35:52 +000037 PyObject *weakreflist; /* List of weak references */
Travis E. Oliphantb99f7622007-08-18 11:21:56 +000038 int ob_exports; /* Number of exported buffers */
Guido van Rossum778983b1993-02-19 15:55:02 +000039} arrayobject;
40
Jeremy Hylton938ace62002-07-17 16:30:39 +000041static PyTypeObject Arraytype;
Guido van Rossum778983b1993-02-19 15:55:02 +000042
Travis E. Oliphantb99f7622007-08-18 11:21:56 +000043#ifdef Py_UNICODE_WIDE
44#define PyArr_UNI 'w'
Travis E. Oliphantb803c512007-08-20 07:16:33 +000045#define PyArr_UNISTR "w"
Travis E. Oliphantb99f7622007-08-18 11:21:56 +000046#else
47#define PyArr_UNI 'u'
Travis E. Oliphantb803c512007-08-20 07:16:33 +000048#define PyArr_UNISTR "u"
Travis E. Oliphantb99f7622007-08-18 11:21:56 +000049#endif
50
Martin v. Löwis99866332002-03-01 10:27:01 +000051#define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
Martin v. Löwis9f2e3462007-07-21 17:22:18 +000052#define array_CheckExact(op) (Py_Type(op) == &Arraytype)
Guido van Rossum778983b1993-02-19 15:55:02 +000053
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000054static int
Martin v. Löwis18e16552006-02-15 17:27:45 +000055array_resize(arrayobject *self, Py_ssize_t newsize)
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000056{
57 char *items;
58 size_t _new_size;
59
60 /* Bypass realloc() when a previous overallocation is large enough
61 to accommodate the newsize. If the newsize is 16 smaller than the
62 current size, then proceed with the realloc() to shrink the list.
63 */
64
65 if (self->allocated >= newsize &&
Martin v. Löwis9f2e3462007-07-21 17:22:18 +000066 Py_Size(self) < newsize + 16 &&
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000067 self->ob_item != NULL) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +000068 Py_Size(self) = newsize;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000069 return 0;
70 }
71
Travis E. Oliphantb99f7622007-08-18 11:21:56 +000072 if (self->ob_exports > 0) {
73 PyErr_SetString(PyExc_BufferError,
74 "cannot resize an array that is exporting data");
75 return -1;
76 }
77
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000078 /* This over-allocates proportional to the array size, making room
79 * for additional growth. The over-allocation is mild, but is
80 * enough to give linear-time amortized behavior over a long
81 * sequence of appends() in the presence of a poorly-performing
82 * system realloc().
83 * The growth pattern is: 0, 4, 8, 16, 25, 34, 46, 56, 67, 79, ...
84 * Note, the pattern starts out the same as for lists but then
85 * grows at a smaller rate so that larger arrays only overallocate
86 * by about 1/16th -- this is done because arrays are presumed to be more
87 * memory critical.
88 */
89
Martin v. Löwis9f2e3462007-07-21 17:22:18 +000090 _new_size = (newsize >> 4) + (Py_Size(self) < 8 ? 3 : 7) + newsize;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000091 items = self->ob_item;
92 /* XXX The following multiplication and division does not optimize away
93 like it does for lists since the size is not known at compile time */
94 if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize))
95 PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize));
96 else
97 items = NULL;
98 if (items == NULL) {
99 PyErr_NoMemory();
100 return -1;
101 }
102 self->ob_item = items;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000103 Py_Size(self) = newsize;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000104 self->allocated = _new_size;
105 return 0;
106}
107
Tim Petersbb307342000-09-10 05:22:54 +0000108/****************************************************************************
109Get and Set functions for each type.
110A Get function takes an arrayobject* and an integer index, returning the
111array value at that index wrapped in an appropriate PyObject*.
112A Set function takes an arrayobject, integer index, and PyObject*; sets
113the array value at that index to the raw C data extracted from the PyObject*,
114and returns 0 if successful, else nonzero on failure (PyObject* not of an
115appropriate type or value).
116Note that the basic Get and Set functions do NOT check that the index is
117in bounds; that's the responsibility of the caller.
118****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +0000119
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000120static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000121b_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000122{
123 long x = ((char *)ap->ob_item)[i];
124 if (x >= 128)
125 x -= 256;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000126 return PyInt_FromLong(x);
Guido van Rossum778983b1993-02-19 15:55:02 +0000127}
128
129static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000130b_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000131{
Fred Drake541dc3b2000-06-28 17:49:30 +0000132 short x;
133 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
134 must use the next size up that is signed ('h') and manually do
135 the overflow checking */
136 if (!PyArg_Parse(v, "h;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000137 return -1;
Guido van Rossum9f754e02000-07-01 00:38:19 +0000138 else if (x < -128) {
Fred Drake541dc3b2000-06-28 17:49:30 +0000139 PyErr_SetString(PyExc_OverflowError,
140 "signed char is less than minimum");
141 return -1;
142 }
Guido van Rossum9f754e02000-07-01 00:38:19 +0000143 else if (x > 127) {
Fred Drake541dc3b2000-06-28 17:49:30 +0000144 PyErr_SetString(PyExc_OverflowError,
145 "signed char is greater than maximum");
146 return -1;
147 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000148 if (i >= 0)
Fred Drake541dc3b2000-06-28 17:49:30 +0000149 ((char *)ap->ob_item)[i] = (char)x;
Guido van Rossum778983b1993-02-19 15:55:02 +0000150 return 0;
151}
152
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000153static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000154BB_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000155{
156 long x = ((unsigned char *)ap->ob_item)[i];
157 return PyInt_FromLong(x);
158}
159
Fred Drake541dc3b2000-06-28 17:49:30 +0000160static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000161BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000162{
163 unsigned char x;
164 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
165 if (!PyArg_Parse(v, "b;array item must be integer", &x))
166 return -1;
167 if (i >= 0)
Martin v. Löwis99866332002-03-01 10:27:01 +0000168 ((char *)ap->ob_item)[i] = x;
Fred Drake541dc3b2000-06-28 17:49:30 +0000169 return 0;
170}
Guido van Rossum549ab711997-01-03 19:09:47 +0000171
Martin v. Löwis99866332002-03-01 10:27:01 +0000172static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000173u_getitem(arrayobject *ap, Py_ssize_t i)
Martin v. Löwis99866332002-03-01 10:27:01 +0000174{
175 return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1);
176}
177
178static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000179u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Martin v. Löwis99866332002-03-01 10:27:01 +0000180{
181 Py_UNICODE *p;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000182 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +0000183
184 if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
185 return -1;
186 if (len != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000187 PyErr_SetString(PyExc_TypeError,
188 "array item must be unicode character");
Martin v. Löwis99866332002-03-01 10:27:01 +0000189 return -1;
190 }
191 if (i >= 0)
192 ((Py_UNICODE *)ap->ob_item)[i] = p[0];
193 return 0;
194}
Martin v. Löwis99866332002-03-01 10:27:01 +0000195
Guido van Rossum549ab711997-01-03 19:09:47 +0000196static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000197h_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000198{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000199 return PyInt_FromLong((long) ((short *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000200}
201
202static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000203h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000204{
205 short x;
Fred Drake541dc3b2000-06-28 17:49:30 +0000206 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000207 if (!PyArg_Parse(v, "h;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000208 return -1;
209 if (i >= 0)
210 ((short *)ap->ob_item)[i] = x;
211 return 0;
212}
213
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000214static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000215HH_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000216{
217 return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
218}
219
Fred Drake541dc3b2000-06-28 17:49:30 +0000220static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000221HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000222{
223 int x;
224 /* PyArg_Parse's 'h' formatter is for a signed short, therefore
225 must use the next size up and manually do the overflow checking */
226 if (!PyArg_Parse(v, "i;array item must be integer", &x))
227 return -1;
228 else if (x < 0) {
229 PyErr_SetString(PyExc_OverflowError,
230 "unsigned short is less than minimum");
231 return -1;
232 }
233 else if (x > USHRT_MAX) {
234 PyErr_SetString(PyExc_OverflowError,
235 "unsigned short is greater than maximum");
236 return -1;
237 }
238 if (i >= 0)
239 ((short *)ap->ob_item)[i] = (short)x;
240 return 0;
241}
Guido van Rossum549ab711997-01-03 19:09:47 +0000242
243static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000244i_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossume77a7571993-11-03 15:01:26 +0000245{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000246 return PyInt_FromLong((long) ((int *)ap->ob_item)[i]);
Guido van Rossume77a7571993-11-03 15:01:26 +0000247}
248
249static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000250i_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossume77a7571993-11-03 15:01:26 +0000251{
252 int x;
Fred Drake541dc3b2000-06-28 17:49:30 +0000253 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000254 if (!PyArg_Parse(v, "i;array item must be integer", &x))
Guido van Rossume77a7571993-11-03 15:01:26 +0000255 return -1;
256 if (i >= 0)
257 ((int *)ap->ob_item)[i] = x;
258 return 0;
259}
260
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000261static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000262II_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000263{
264 return PyLong_FromUnsignedLong(
265 (unsigned long) ((unsigned int *)ap->ob_item)[i]);
266}
267
268static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000269II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000270{
271 unsigned long x;
272 if (PyLong_Check(v)) {
273 x = PyLong_AsUnsignedLong(v);
274 if (x == (unsigned long) -1 && PyErr_Occurred())
275 return -1;
276 }
277 else {
Fred Drake541dc3b2000-06-28 17:49:30 +0000278 long y;
279 if (!PyArg_Parse(v, "l;array item must be integer", &y))
Guido van Rossum549ab711997-01-03 19:09:47 +0000280 return -1;
Fred Drake541dc3b2000-06-28 17:49:30 +0000281 if (y < 0) {
282 PyErr_SetString(PyExc_OverflowError,
283 "unsigned int is less than minimum");
284 return -1;
285 }
286 x = (unsigned long)y;
Tim Petersbb307342000-09-10 05:22:54 +0000287
Guido van Rossum549ab711997-01-03 19:09:47 +0000288 }
Fred Drake541dc3b2000-06-28 17:49:30 +0000289 if (x > UINT_MAX) {
290 PyErr_SetString(PyExc_OverflowError,
291 "unsigned int is greater than maximum");
292 return -1;
293 }
294
Guido van Rossum549ab711997-01-03 19:09:47 +0000295 if (i >= 0)
Fred Drake541dc3b2000-06-28 17:49:30 +0000296 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
Guido van Rossum549ab711997-01-03 19:09:47 +0000297 return 0;
298}
299
300static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000301l_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000302{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000303 return PyInt_FromLong(((long *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000304}
305
306static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000307l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000308{
309 long x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000310 if (!PyArg_Parse(v, "l;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000311 return -1;
312 if (i >= 0)
313 ((long *)ap->ob_item)[i] = x;
314 return 0;
315}
316
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000317static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000318LL_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000319{
320 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
321}
322
323static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000324LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000325{
326 unsigned long x;
327 if (PyLong_Check(v)) {
328 x = PyLong_AsUnsignedLong(v);
329 if (x == (unsigned long) -1 && PyErr_Occurred())
330 return -1;
331 }
332 else {
Fred Drake541dc3b2000-06-28 17:49:30 +0000333 long y;
334 if (!PyArg_Parse(v, "l;array item must be integer", &y))
Guido van Rossum549ab711997-01-03 19:09:47 +0000335 return -1;
Fred Drake541dc3b2000-06-28 17:49:30 +0000336 if (y < 0) {
337 PyErr_SetString(PyExc_OverflowError,
338 "unsigned long is less than minimum");
339 return -1;
340 }
341 x = (unsigned long)y;
Tim Petersbb307342000-09-10 05:22:54 +0000342
Guido van Rossum549ab711997-01-03 19:09:47 +0000343 }
Fred Drake541dc3b2000-06-28 17:49:30 +0000344 if (x > ULONG_MAX) {
345 PyErr_SetString(PyExc_OverflowError,
346 "unsigned long is greater than maximum");
347 return -1;
348 }
Tim Petersbb307342000-09-10 05:22:54 +0000349
Guido van Rossum549ab711997-01-03 19:09:47 +0000350 if (i >= 0)
351 ((unsigned long *)ap->ob_item)[i] = x;
352 return 0;
353}
354
355static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000356f_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000357{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000358 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000359}
360
361static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000362f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000363{
364 float x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000365 if (!PyArg_Parse(v, "f;array item must be float", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000366 return -1;
367 if (i >= 0)
368 ((float *)ap->ob_item)[i] = x;
369 return 0;
370}
371
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000372static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000373d_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000374{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000375 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000376}
377
378static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000379d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000380{
381 double x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000382 if (!PyArg_Parse(v, "d;array item must be float", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000383 return -1;
384 if (i >= 0)
385 ((double *)ap->ob_item)[i] = x;
386 return 0;
387}
388
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000389
Guido van Rossum778983b1993-02-19 15:55:02 +0000390/* Description of types */
Guido van Rossum234f9421993-06-17 12:35:49 +0000391static struct arraydescr descriptors[] = {
Travis E. Oliphantb803c512007-08-20 07:16:33 +0000392 {'b', sizeof(char), b_getitem, b_setitem, "b"},
393 {'B', sizeof(char), BB_getitem, BB_setitem, "B"},
394 {PyArr_UNI, sizeof(Py_UNICODE), u_getitem, u_setitem, PyArr_UNISTR},
395 {'h', sizeof(short), h_getitem, h_setitem, "h"},
396 {'H', sizeof(short), HH_getitem, HH_setitem, "H"},
397 {'i', sizeof(int), i_getitem, i_setitem, "i"},
398 {'I', sizeof(int), II_getitem, II_setitem, "I"},
399 {'l', sizeof(long), l_getitem, l_setitem, "l"},
400 {'L', sizeof(long), LL_getitem, LL_setitem, "L"},
401 {'f', sizeof(float), f_getitem, f_setitem, "f"},
402 {'d', sizeof(double), d_getitem, d_setitem, "d"},
403 {'\0', 0, 0, 0, 0} /* Sentinel */
Guido van Rossum778983b1993-02-19 15:55:02 +0000404};
Tim Petersbb307342000-09-10 05:22:54 +0000405
406/****************************************************************************
407Implementations of array object methods.
408****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +0000409
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000410static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000411newarrayobject(PyTypeObject *type, Py_ssize_t size, struct arraydescr *descr)
Guido van Rossum778983b1993-02-19 15:55:02 +0000412{
Guido van Rossum778983b1993-02-19 15:55:02 +0000413 arrayobject *op;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000414 size_t nbytes;
Martin v. Löwis99866332002-03-01 10:27:01 +0000415
Guido van Rossum778983b1993-02-19 15:55:02 +0000416 if (size < 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000417 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000418 return NULL;
419 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000420
Guido van Rossum778983b1993-02-19 15:55:02 +0000421 nbytes = size * descr->itemsize;
422 /* Check for overflow */
Guido van Rossum7844e381997-04-11 20:44:04 +0000423 if (nbytes / descr->itemsize != (size_t)size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000424 return PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000425 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000426 op = (arrayobject *) type->tp_alloc(type, 0);
Guido van Rossum778983b1993-02-19 15:55:02 +0000427 if (op == NULL) {
Martin v. Löwis99866332002-03-01 10:27:01 +0000428 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +0000429 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000430 Py_Size(op) = size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000431 if (size <= 0) {
432 op->ob_item = NULL;
433 }
434 else {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000435 op->ob_item = PyMem_NEW(char, nbytes);
Guido van Rossum778983b1993-02-19 15:55:02 +0000436 if (op->ob_item == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000437 PyObject_Del(op);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000438 return PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000439 }
440 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000441 op->ob_descr = descr;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000442 op->allocated = size;
Raymond Hettingercb87bc82004-05-31 00:35:52 +0000443 op->weakreflist = NULL;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000444 op->ob_exports = 0;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000445 return (PyObject *) op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000446}
447
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000448static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000449getarrayitem(PyObject *op, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000450{
451 register arrayobject *ap;
Martin v. Löwis99866332002-03-01 10:27:01 +0000452 assert(array_Check(op));
Guido van Rossum778983b1993-02-19 15:55:02 +0000453 ap = (arrayobject *)op;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000454 assert(i>=0 && i<Py_Size(ap));
Guido van Rossum778983b1993-02-19 15:55:02 +0000455 return (*ap->ob_descr->getitem)(ap, i);
456}
457
458static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000459ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000460{
Guido van Rossum778983b1993-02-19 15:55:02 +0000461 char *items;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000462 Py_ssize_t n = Py_Size(self);
Guido van Rossum778983b1993-02-19 15:55:02 +0000463 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000464 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000465 return -1;
466 }
467 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
468 return -1;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000469
470 if (array_resize(self, n+1) == -1)
Guido van Rossum778983b1993-02-19 15:55:02 +0000471 return -1;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000472 items = self->ob_item;
Walter Dörwald9e46abe2003-05-18 03:15:10 +0000473 if (where < 0) {
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000474 where += n;
Walter Dörwald9e46abe2003-05-18 03:15:10 +0000475 if (where < 0)
476 where = 0;
477 }
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000478 if (where > n)
479 where = n;
480 /* appends don't need to call memmove() */
481 if (where != n)
482 memmove(items + (where+1)*self->ob_descr->itemsize,
483 items + where*self->ob_descr->itemsize,
484 (n-where)*self->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000485 return (*self->ob_descr->setitem)(self, where, v);
486}
487
Guido van Rossum778983b1993-02-19 15:55:02 +0000488/* Methods */
489
490static void
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000491array_dealloc(arrayobject *op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000492{
Raymond Hettingercb87bc82004-05-31 00:35:52 +0000493 if (op->weakreflist != NULL)
494 PyObject_ClearWeakRefs((PyObject *) op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000495 if (op->ob_item != NULL)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000496 PyMem_DEL(op->ob_item);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000497 Py_Type(op)->tp_free((PyObject *)op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000498}
499
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000500static PyObject *
501array_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000502{
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000503 arrayobject *va, *wa;
504 PyObject *vi = NULL;
505 PyObject *wi = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000506 Py_ssize_t i, k;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000507 PyObject *res;
508
Martin v. Löwis99866332002-03-01 10:27:01 +0000509 if (!array_Check(v) || !array_Check(w)) {
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000510 Py_INCREF(Py_NotImplemented);
511 return Py_NotImplemented;
Guido van Rossum778983b1993-02-19 15:55:02 +0000512 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000513
514 va = (arrayobject *)v;
515 wa = (arrayobject *)w;
516
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000517 if (Py_Size(va) != Py_Size(wa) && (op == Py_EQ || op == Py_NE)) {
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000518 /* Shortcut: if the lengths differ, the arrays differ */
519 if (op == Py_EQ)
520 res = Py_False;
521 else
522 res = Py_True;
523 Py_INCREF(res);
524 return res;
525 }
526
527 /* Search for the first index where items are different */
528 k = 1;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000529 for (i = 0; i < Py_Size(va) && i < Py_Size(wa); i++) {
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000530 vi = getarrayitem(v, i);
531 wi = getarrayitem(w, i);
532 if (vi == NULL || wi == NULL) {
533 Py_XDECREF(vi);
534 Py_XDECREF(wi);
535 return NULL;
536 }
537 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
538 if (k == 0)
539 break; /* Keeping vi and wi alive! */
540 Py_DECREF(vi);
541 Py_DECREF(wi);
542 if (k < 0)
543 return NULL;
544 }
545
546 if (k) {
547 /* No more items to compare -- compare sizes */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000548 Py_ssize_t vs = Py_Size(va);
549 Py_ssize_t ws = Py_Size(wa);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000550 int cmp;
551 switch (op) {
552 case Py_LT: cmp = vs < ws; break;
Guido van Rossum2b597e42001-01-25 22:12:43 +0000553 case Py_LE: cmp = vs <= ws; break;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000554 case Py_EQ: cmp = vs == ws; break;
555 case Py_NE: cmp = vs != ws; break;
556 case Py_GT: cmp = vs > ws; break;
557 case Py_GE: cmp = vs >= ws; break;
558 default: return NULL; /* cannot happen */
559 }
560 if (cmp)
561 res = Py_True;
562 else
563 res = Py_False;
564 Py_INCREF(res);
565 return res;
566 }
567
568 /* We have an item that differs. First, shortcuts for EQ/NE */
569 if (op == Py_EQ) {
570 Py_INCREF(Py_False);
571 res = Py_False;
572 }
573 else if (op == Py_NE) {
574 Py_INCREF(Py_True);
575 res = Py_True;
576 }
577 else {
578 /* Compare the final item again using the proper operator */
579 res = PyObject_RichCompare(vi, wi, op);
580 }
581 Py_DECREF(vi);
582 Py_DECREF(wi);
583 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +0000584}
585
Martin v. Löwis18e16552006-02-15 17:27:45 +0000586static Py_ssize_t
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000587array_length(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +0000588{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000589 return Py_Size(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000590}
591
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000592static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000593array_item(arrayobject *a, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000594{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000595 if (i < 0 || i >= Py_Size(a)) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000596 PyErr_SetString(PyExc_IndexError, "array index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000597 return NULL;
598 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000599 return getarrayitem((PyObject *)a, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000600}
601
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000602static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000603array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000604{
605 arrayobject *np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000606 if (ilow < 0)
607 ilow = 0;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000608 else if (ilow > Py_Size(a))
609 ilow = Py_Size(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000610 if (ihigh < 0)
611 ihigh = 0;
612 if (ihigh < ilow)
613 ihigh = ilow;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000614 else if (ihigh > Py_Size(a))
615 ihigh = Py_Size(a);
Martin v. Löwis99866332002-03-01 10:27:01 +0000616 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
Guido van Rossum778983b1993-02-19 15:55:02 +0000617 if (np == NULL)
618 return NULL;
619 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
620 (ihigh-ilow) * a->ob_descr->itemsize);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000621 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000622}
623
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000624static PyObject *
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000625array_copy(arrayobject *a, PyObject *unused)
626{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000627 return array_slice(a, 0, Py_Size(a));
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000628}
629
630PyDoc_STRVAR(copy_doc,
631"copy(array)\n\
632\n\
633 Return a copy of the array.");
634
635static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000636array_concat(arrayobject *a, PyObject *bb)
Guido van Rossum778983b1993-02-19 15:55:02 +0000637{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000638 Py_ssize_t size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000639 arrayobject *np;
Martin v. Löwis99866332002-03-01 10:27:01 +0000640 if (!array_Check(bb)) {
Fred Drake137507e2000-06-01 02:02:46 +0000641 PyErr_Format(PyExc_TypeError,
642 "can only append array (not \"%.200s\") to array",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000643 Py_Type(bb)->tp_name);
Guido van Rossum778983b1993-02-19 15:55:02 +0000644 return NULL;
645 }
646#define b ((arrayobject *)bb)
647 if (a->ob_descr != b->ob_descr) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000648 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000649 return NULL;
650 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000651 size = Py_Size(a) + Py_Size(b);
Martin v. Löwis99866332002-03-01 10:27:01 +0000652 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
Guido van Rossum778983b1993-02-19 15:55:02 +0000653 if (np == NULL) {
654 return NULL;
655 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000656 memcpy(np->ob_item, a->ob_item, Py_Size(a)*a->ob_descr->itemsize);
657 memcpy(np->ob_item + Py_Size(a)*a->ob_descr->itemsize,
658 b->ob_item, Py_Size(b)*b->ob_descr->itemsize);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000659 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000660#undef b
661}
662
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000663static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000664array_repeat(arrayobject *a, Py_ssize_t n)
Guido van Rossum778983b1993-02-19 15:55:02 +0000665{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000666 Py_ssize_t i;
667 Py_ssize_t size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000668 arrayobject *np;
669 char *p;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000670 Py_ssize_t nbytes;
Guido van Rossum778983b1993-02-19 15:55:02 +0000671 if (n < 0)
672 n = 0;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000673 size = Py_Size(a) * n;
Martin v. Löwis99866332002-03-01 10:27:01 +0000674 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
Guido van Rossum778983b1993-02-19 15:55:02 +0000675 if (np == NULL)
676 return NULL;
677 p = np->ob_item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000678 nbytes = Py_Size(a) * a->ob_descr->itemsize;
Guido van Rossum778983b1993-02-19 15:55:02 +0000679 for (i = 0; i < n; i++) {
680 memcpy(p, a->ob_item, nbytes);
681 p += nbytes;
682 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000683 return (PyObject *) np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000684}
685
686static int
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000687array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000688{
689 char *item;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000690 Py_ssize_t n; /* Size of replacement array */
691 Py_ssize_t d; /* Change in size */
Guido van Rossum778983b1993-02-19 15:55:02 +0000692#define b ((arrayobject *)v)
693 if (v == NULL)
694 n = 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000695 else if (array_Check(v)) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000696 n = Py_Size(b);
Guido van Rossum778983b1993-02-19 15:55:02 +0000697 if (a == b) {
698 /* Special case "a[i:j] = a" -- copy b first */
699 int ret;
700 v = array_slice(b, 0, n);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000701 if (!v)
702 return -1;
Guido van Rossum778983b1993-02-19 15:55:02 +0000703 ret = array_ass_slice(a, ilow, ihigh, v);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000704 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000705 return ret;
706 }
707 if (b->ob_descr != a->ob_descr) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000708 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000709 return -1;
710 }
711 }
712 else {
Fred Drake137507e2000-06-01 02:02:46 +0000713 PyErr_Format(PyExc_TypeError,
714 "can only assign array (not \"%.200s\") to array slice",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000715 Py_Type(v)->tp_name);
Guido van Rossum778983b1993-02-19 15:55:02 +0000716 return -1;
717 }
718 if (ilow < 0)
719 ilow = 0;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000720 else if (ilow > Py_Size(a))
721 ilow = Py_Size(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000722 if (ihigh < 0)
723 ihigh = 0;
724 if (ihigh < ilow)
725 ihigh = ilow;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000726 else if (ihigh > Py_Size(a))
727 ihigh = Py_Size(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000728 item = a->ob_item;
729 d = n - (ihigh-ilow);
730 if (d < 0) { /* Delete -d items */
731 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
732 item + ihigh*a->ob_descr->itemsize,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000733 (Py_Size(a)-ihigh)*a->ob_descr->itemsize);
734 Py_Size(a) += d;
735 PyMem_RESIZE(item, char, Py_Size(a)*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000736 /* Can't fail */
737 a->ob_item = item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000738 a->allocated = Py_Size(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000739 }
740 else if (d > 0) { /* Insert d items */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000741 PyMem_RESIZE(item, char,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000742 (Py_Size(a) + d)*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000743 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000744 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000745 return -1;
746 }
747 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
748 item + ihigh*a->ob_descr->itemsize,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000749 (Py_Size(a)-ihigh)*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000750 a->ob_item = item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000751 Py_Size(a) += d;
752 a->allocated = Py_Size(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000753 }
754 if (n > 0)
755 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
756 n*b->ob_descr->itemsize);
757 return 0;
758#undef b
759}
760
761static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000762array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000763{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000764 if (i < 0 || i >= Py_Size(a)) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000765 PyErr_SetString(PyExc_IndexError,
766 "array assignment index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000767 return -1;
768 }
769 if (v == NULL)
770 return array_ass_slice(a, i, i+1, v);
771 return (*a->ob_descr->setitem)(a, i, v);
772}
773
774static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000775setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000776{
Martin v. Löwis99866332002-03-01 10:27:01 +0000777 assert(array_Check(a));
Guido van Rossum778983b1993-02-19 15:55:02 +0000778 return array_ass_item((arrayobject *)a, i, v);
779}
780
Martin v. Löwis99866332002-03-01 10:27:01 +0000781static int
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000782array_iter_extend(arrayobject *self, PyObject *bb)
783{
784 PyObject *it, *v;
785
786 it = PyObject_GetIter(bb);
787 if (it == NULL)
788 return -1;
789
790 while ((v = PyIter_Next(it)) != NULL) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000791 if (ins1(self, (int) Py_Size(self), v) != 0) {
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000792 Py_DECREF(v);
793 Py_DECREF(it);
794 return -1;
795 }
796 Py_DECREF(v);
797 }
798 Py_DECREF(it);
799 if (PyErr_Occurred())
800 return -1;
801 return 0;
802}
803
804static int
Martin v. Löwis99866332002-03-01 10:27:01 +0000805array_do_extend(arrayobject *self, PyObject *bb)
806{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000807 Py_ssize_t size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000808
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000809 if (!array_Check(bb))
810 return array_iter_extend(self, bb);
Martin v. Löwis99866332002-03-01 10:27:01 +0000811#define b ((arrayobject *)bb)
812 if (self->ob_descr != b->ob_descr) {
813 PyErr_SetString(PyExc_TypeError,
814 "can only extend with array of same kind");
815 return -1;
816 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000817 size = Py_Size(self) + Py_Size(b);
Martin v. Löwis99866332002-03-01 10:27:01 +0000818 PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize);
819 if (self->ob_item == NULL) {
820 PyObject_Del(self);
821 PyErr_NoMemory();
822 return -1;
823 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000824 memcpy(self->ob_item + Py_Size(self)*self->ob_descr->itemsize,
825 b->ob_item, Py_Size(b)*b->ob_descr->itemsize);
826 Py_Size(self) = size;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000827 self->allocated = size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000828
829 return 0;
830#undef b
831}
832
833static PyObject *
834array_inplace_concat(arrayobject *self, PyObject *bb)
835{
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000836 if (!array_Check(bb)) {
837 PyErr_Format(PyExc_TypeError,
838 "can only extend array with array (not \"%.200s\")",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000839 Py_Type(bb)->tp_name);
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000840 return NULL;
841 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000842 if (array_do_extend(self, bb) == -1)
843 return NULL;
844 Py_INCREF(self);
845 return (PyObject *)self;
846}
847
848static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000849array_inplace_repeat(arrayobject *self, Py_ssize_t n)
Martin v. Löwis99866332002-03-01 10:27:01 +0000850{
851 char *items, *p;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000852 Py_ssize_t size, i;
Martin v. Löwis99866332002-03-01 10:27:01 +0000853
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000854 if (Py_Size(self) > 0) {
Martin v. Löwis99866332002-03-01 10:27:01 +0000855 if (n < 0)
856 n = 0;
857 items = self->ob_item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000858 size = Py_Size(self) * self->ob_descr->itemsize;
Martin v. Löwis99866332002-03-01 10:27:01 +0000859 if (n == 0) {
860 PyMem_FREE(items);
861 self->ob_item = NULL;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000862 Py_Size(self) = 0;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000863 self->allocated = 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000864 }
865 else {
866 PyMem_Resize(items, char, n * size);
867 if (items == NULL)
868 return PyErr_NoMemory();
869 p = items;
870 for (i = 1; i < n; i++) {
871 p += size;
872 memcpy(p, items, size);
873 }
874 self->ob_item = items;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000875 Py_Size(self) *= n;
876 self->allocated = Py_Size(self);
Martin v. Löwis99866332002-03-01 10:27:01 +0000877 }
878 }
879 Py_INCREF(self);
880 return (PyObject *)self;
881}
882
883
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000884static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000885ins(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000886{
887 if (ins1(self, where, v) != 0)
888 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000889 Py_INCREF(Py_None);
890 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000891}
892
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000893static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000894array_count(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000895{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000896 Py_ssize_t count = 0;
897 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000898
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000899 for (i = 0; i < Py_Size(self); i++) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000900 PyObject *selfi = getarrayitem((PyObject *)self, i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000901 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000902 Py_DECREF(selfi);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000903 if (cmp > 0)
904 count++;
905 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000906 return NULL;
907 }
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000908 return PyInt_FromSsize_t(count);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000909}
910
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000911PyDoc_STRVAR(count_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000912"count(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000913\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000914Return number of occurences of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000915
916static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000917array_index(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000918{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000919 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000920
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000921 for (i = 0; i < Py_Size(self); i++) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000922 PyObject *selfi = getarrayitem((PyObject *)self, i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000923 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
924 Py_DECREF(selfi);
925 if (cmp > 0) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000926 return PyInt_FromLong((long)i);
927 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000928 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000929 return NULL;
930 }
931 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
932 return NULL;
933}
934
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000935PyDoc_STRVAR(index_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000936"index(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000937\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000938Return index of first occurence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000939
Raymond Hettinger625812f2003-01-07 01:58:52 +0000940static int
941array_contains(arrayobject *self, PyObject *v)
942{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000943 Py_ssize_t i;
944 int cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +0000945
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000946 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_Size(self); i++) {
Raymond Hettinger625812f2003-01-07 01:58:52 +0000947 PyObject *selfi = getarrayitem((PyObject *)self, i);
948 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
949 Py_DECREF(selfi);
950 }
951 return cmp;
952}
953
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000954static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000955array_remove(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000956{
957 int i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000958
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000959 for (i = 0; i < Py_Size(self); i++) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000960 PyObject *selfi = getarrayitem((PyObject *)self,i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000961 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
962 Py_DECREF(selfi);
963 if (cmp > 0) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000964 if (array_ass_slice(self, i, i+1,
965 (PyObject *)NULL) != 0)
966 return NULL;
967 Py_INCREF(Py_None);
968 return Py_None;
969 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000970 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000971 return NULL;
972 }
973 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
974 return NULL;
975}
976
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000977PyDoc_STRVAR(remove_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000978"remove(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000979\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000980Remove the first occurence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000981
982static PyObject *
983array_pop(arrayobject *self, PyObject *args)
984{
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000985 Py_ssize_t i = -1;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000986 PyObject *v;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000987 if (!PyArg_ParseTuple(args, "|n:pop", &i))
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000988 return NULL;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000989 if (Py_Size(self) == 0) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000990 /* Special-case most common failure cause */
991 PyErr_SetString(PyExc_IndexError, "pop from empty array");
992 return NULL;
993 }
994 if (i < 0)
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000995 i += Py_Size(self);
996 if (i < 0 || i >= Py_Size(self)) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000997 PyErr_SetString(PyExc_IndexError, "pop index out of range");
998 return NULL;
999 }
1000 v = getarrayitem((PyObject *)self,i);
1001 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
1002 Py_DECREF(v);
1003 return NULL;
1004 }
1005 return v;
1006}
1007
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001008PyDoc_STRVAR(pop_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001009"pop([i])\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001010\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001011Return the i-th element and delete it from the array. i defaults to -1.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001012
1013static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001014array_extend(arrayobject *self, PyObject *bb)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001015{
Martin v. Löwis99866332002-03-01 10:27:01 +00001016 if (array_do_extend(self, bb) == -1)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001017 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001018 Py_INCREF(Py_None);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001019 return Py_None;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001020}
1021
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001022PyDoc_STRVAR(extend_doc,
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001023"extend(array or iterable)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001024\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001025 Append items to the end of the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001026
1027static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001028array_insert(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001029{
Thomas Woutersb9eb5512006-02-27 19:44:56 +00001030 Py_ssize_t i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001031 PyObject *v;
Thomas Woutersb9eb5512006-02-27 19:44:56 +00001032 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
Guido van Rossum778983b1993-02-19 15:55:02 +00001033 return NULL;
1034 return ins(self, i, v);
1035}
1036
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001037PyDoc_STRVAR(insert_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001038"insert(i,x)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001039\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001040Insert a new item x into the array before position i.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001041
1042
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001043static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001044array_buffer_info(arrayobject *self, PyObject *unused)
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001045{
Tim Peters077a11d2000-09-16 22:31:29 +00001046 PyObject* retval = NULL;
Tim Peters077a11d2000-09-16 22:31:29 +00001047 retval = PyTuple_New(2);
1048 if (!retval)
1049 return NULL;
Fred Drake541dc3b2000-06-28 17:49:30 +00001050
1051 PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item));
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001052 PyTuple_SET_ITEM(retval, 1, PyInt_FromLong((long)(Py_Size(self))));
Fred Drake541dc3b2000-06-28 17:49:30 +00001053
1054 return retval;
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001055}
1056
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001057PyDoc_STRVAR(buffer_info_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001058"buffer_info() -> (address, length)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001059\n\
1060Return a tuple (address, length) giving the current memory address and\n\
Guido van Rossum702d08e2001-07-27 16:05:32 +00001061the length in items of the buffer used to hold array's contents\n\
1062The length should be multiplied by the itemsize attribute to calculate\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001063the buffer length in bytes.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001064
1065
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001066static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001067array_append(arrayobject *self, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +00001068{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001069 return ins(self, (int) Py_Size(self), v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001070}
1071
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001072PyDoc_STRVAR(append_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001073"append(x)\n\
1074\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001075Append new value x to the end of the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001076
1077
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001078static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001079array_byteswap(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001080{
1081 char *p;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001082 Py_ssize_t i;
Fred Drakebf272981999-12-03 17:15:30 +00001083
Guido van Rossum778983b1993-02-19 15:55:02 +00001084 switch (self->ob_descr->itemsize) {
1085 case 1:
1086 break;
1087 case 2:
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001088 for (p = self->ob_item, i = Py_Size(self); --i >= 0; p += 2) {
Guido van Rossum778983b1993-02-19 15:55:02 +00001089 char p0 = p[0];
1090 p[0] = p[1];
1091 p[1] = p0;
1092 }
1093 break;
1094 case 4:
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001095 for (p = self->ob_item, i = Py_Size(self); --i >= 0; p += 4) {
Guido van Rossum778983b1993-02-19 15:55:02 +00001096 char p0 = p[0];
1097 char p1 = p[1];
1098 p[0] = p[3];
1099 p[1] = p[2];
1100 p[2] = p1;
1101 p[3] = p0;
1102 }
1103 break;
Guido van Rossume77a7571993-11-03 15:01:26 +00001104 case 8:
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001105 for (p = self->ob_item, i = Py_Size(self); --i >= 0; p += 8) {
Guido van Rossume77a7571993-11-03 15:01:26 +00001106 char p0 = p[0];
1107 char p1 = p[1];
1108 char p2 = p[2];
1109 char p3 = p[3];
1110 p[0] = p[7];
1111 p[1] = p[6];
1112 p[2] = p[5];
1113 p[3] = p[4];
1114 p[4] = p3;
1115 p[5] = p2;
1116 p[6] = p1;
1117 p[7] = p0;
1118 }
1119 break;
Guido van Rossum778983b1993-02-19 15:55:02 +00001120 default:
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001121 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossum778983b1993-02-19 15:55:02 +00001122 "don't know how to byteswap this array type");
1123 return NULL;
1124 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001125 Py_INCREF(Py_None);
1126 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001127}
1128
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001129PyDoc_STRVAR(byteswap_doc,
Fred Drakebf272981999-12-03 17:15:30 +00001130"byteswap()\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001131\n\
Fred Drakebf272981999-12-03 17:15:30 +00001132Byteswap 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 +000011334, or 8 bytes in size, RuntimeError is raised.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001134
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001135static PyObject *
Raymond Hettingerb0900e62004-12-16 16:23:40 +00001136array_reduce(arrayobject *array)
1137{
1138 PyObject *dict, *result;
1139
1140 dict = PyObject_GetAttrString((PyObject *)array, "__dict__");
1141 if (dict == NULL) {
1142 PyErr_Clear();
1143 dict = Py_None;
1144 Py_INCREF(dict);
1145 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001146 if (Py_Size(array) > 0) {
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001147 result = Py_BuildValue("O(cy#)O",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001148 Py_Type(array),
Guido van Rossumd8faa362007-04-27 19:54:29 +00001149 array->ob_descr->typecode,
1150 array->ob_item,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001151 Py_Size(array) * array->ob_descr->itemsize,
Guido van Rossumd8faa362007-04-27 19:54:29 +00001152 dict);
1153 } else {
1154 result = Py_BuildValue("O(c)O",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001155 Py_Type(array),
Guido van Rossumd8faa362007-04-27 19:54:29 +00001156 array->ob_descr->typecode,
1157 dict);
1158 }
Raymond Hettingerb0900e62004-12-16 16:23:40 +00001159 Py_DECREF(dict);
1160 return result;
1161}
1162
1163PyDoc_STRVAR(array_doc, "Return state information for pickling.");
1164
1165static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001166array_reverse(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001167{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001168 register Py_ssize_t itemsize = self->ob_descr->itemsize;
Guido van Rossume77a7571993-11-03 15:01:26 +00001169 register char *p, *q;
Tim Peters077a11d2000-09-16 22:31:29 +00001170 /* little buffer to hold items while swapping */
1171 char tmp[256]; /* 8 is probably enough -- but why skimp */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001172 assert((size_t)itemsize <= sizeof(tmp));
Guido van Rossume77a7571993-11-03 15:01:26 +00001173
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001174 if (Py_Size(self) > 1) {
Guido van Rossume77a7571993-11-03 15:01:26 +00001175 for (p = self->ob_item,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001176 q = self->ob_item + (Py_Size(self) - 1)*itemsize;
Guido van Rossume77a7571993-11-03 15:01:26 +00001177 p < q;
1178 p += itemsize, q -= itemsize) {
Tim Peters077a11d2000-09-16 22:31:29 +00001179 /* memory areas guaranteed disjoint, so memcpy
1180 * is safe (& memmove may be slower).
1181 */
1182 memcpy(tmp, p, itemsize);
1183 memcpy(p, q, itemsize);
1184 memcpy(q, tmp, itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001185 }
1186 }
Tim Petersbb307342000-09-10 05:22:54 +00001187
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001188 Py_INCREF(Py_None);
1189 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001190}
Guido van Rossume77a7571993-11-03 15:01:26 +00001191
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001192PyDoc_STRVAR(reverse_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001193"reverse()\n\
1194\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001195Reverse the order of the items in the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001196
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001197
1198/* Forward */
1199static PyObject *array_fromstring(arrayobject *self, PyObject *args);
1200
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001201static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001202array_fromfile(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001203{
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001204 PyObject *f, *b, *res;
1205 Py_ssize_t itemsize = self->ob_descr->itemsize;
1206 Py_ssize_t n, nbytes;
1207
Thomas Wouters7a2f83b2006-02-16 17:07:41 +00001208 if (!PyArg_ParseTuple(args, "On:fromfile", &f, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +00001209 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001210
1211 nbytes = n * itemsize;
1212 if (nbytes < 0 || nbytes/itemsize != n) {
1213 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001214 return NULL;
1215 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001216
1217 b = PyObject_CallMethod(f, "read", "n", nbytes);
1218 if (b == NULL)
1219 return NULL;
1220
1221 if (!PyBytes_Check(b)) {
1222 PyErr_SetString(PyExc_TypeError,
1223 "read() didn't return bytes");
1224 Py_DECREF(b);
1225 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001226 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001227
1228 if (PyBytes_GET_SIZE(b) != nbytes) {
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001229 PyErr_SetString(PyExc_EOFError,
1230 "read() didn't return enough bytes");
1231 Py_DECREF(b);
1232 return NULL;
1233 }
1234
1235 args = Py_BuildValue("(O)", b);
1236 Py_DECREF(b);
1237 if (args == NULL)
1238 return NULL;
1239
1240 res = array_fromstring(self, args);
1241 Py_DECREF(args);
1242
1243 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +00001244}
1245
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001246PyDoc_STRVAR(fromfile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001247"fromfile(f, n)\n\
1248\n\
1249Read 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 +00001250array. Also called as read.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001251
1252
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001253static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001254array_tofile(arrayobject *self, PyObject *f)
Guido van Rossum778983b1993-02-19 15:55:02 +00001255{
Martin v. Löwis5d7428b2007-07-21 18:47:48 +00001256 Py_ssize_t nbytes = Py_Size(self) * self->ob_descr->itemsize;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001257 /* Write 64K blocks at a time */
1258 /* XXX Make the block size settable */
1259 int BLOCKSIZE = 64*1024;
1260 Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE;
1261 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001262
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001263 if (Py_Size(self) == 0)
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001264 goto done;
1265
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001266 for (i = 0; i < nblocks; i++) {
1267 char* ptr = self->ob_item + i*BLOCKSIZE;
1268 Py_ssize_t size = BLOCKSIZE;
1269 PyObject *bytes, *res;
1270 if (i*BLOCKSIZE + size > nbytes)
1271 size = nbytes - i*BLOCKSIZE;
1272 bytes = PyBytes_FromStringAndSize(ptr, size);
1273 if (bytes == NULL)
Guido van Rossum778983b1993-02-19 15:55:02 +00001274 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001275 res = PyObject_CallMethod(f, "write", "O", bytes);
1276 Py_DECREF(bytes);
1277 if (res == NULL)
1278 return NULL;
Martin v. Löwisa291c8f2007-08-11 14:25:27 +00001279 Py_DECREF(res); /* drop write result */
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001280 }
1281
1282 done:
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001283 Py_INCREF(Py_None);
1284 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001285}
1286
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001287PyDoc_STRVAR(tofile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001288"tofile(f)\n\
1289\n\
1290Write all items (as machine values) to the file object f. Also called as\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001291write.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001292
1293
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001294static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001295array_fromlist(arrayobject *self, PyObject *list)
Guido van Rossum778983b1993-02-19 15:55:02 +00001296{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001297 Py_ssize_t n;
1298 Py_ssize_t itemsize = self->ob_descr->itemsize;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001299
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001300 if (!PyList_Check(list)) {
1301 PyErr_SetString(PyExc_TypeError, "arg must be list");
Guido van Rossum778983b1993-02-19 15:55:02 +00001302 return NULL;
1303 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001304 n = PyList_Size(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001305 if (n > 0) {
1306 char *item = self->ob_item;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001307 Py_ssize_t i;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001308 PyMem_RESIZE(item, char, (Py_Size(self) + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001309 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001310 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001311 return NULL;
1312 }
1313 self->ob_item = item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001314 Py_Size(self) += n;
1315 self->allocated = Py_Size(self);
Guido van Rossum778983b1993-02-19 15:55:02 +00001316 for (i = 0; i < n; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001317 PyObject *v = PyList_GetItem(list, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001318 if ((*self->ob_descr->setitem)(self,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001319 Py_Size(self) - n + i, v) != 0) {
1320 Py_Size(self) -= n;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001321 PyMem_RESIZE(item, char,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001322 Py_Size(self) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001323 self->ob_item = item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001324 self->allocated = Py_Size(self);
Guido van Rossum778983b1993-02-19 15:55:02 +00001325 return NULL;
1326 }
1327 }
1328 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001329 Py_INCREF(Py_None);
1330 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001331}
1332
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001333PyDoc_STRVAR(fromlist_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001334"fromlist(list)\n\
1335\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001336Append items to array from list.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001337
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001338static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001339array_tolist(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001340{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001341 PyObject *list = PyList_New(Py_Size(self));
Martin v. Löwis18e16552006-02-15 17:27:45 +00001342 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001343
Guido van Rossum778983b1993-02-19 15:55:02 +00001344 if (list == NULL)
1345 return NULL;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001346 for (i = 0; i < Py_Size(self); i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001347 PyObject *v = getarrayitem((PyObject *)self, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001348 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001349 Py_DECREF(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001350 return NULL;
1351 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001352 PyList_SetItem(list, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001353 }
1354 return list;
1355}
1356
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001357PyDoc_STRVAR(tolist_doc,
Guido van Rossumfc6aba51998-10-14 02:52:31 +00001358"tolist() -> list\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001359\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001360Convert array to an ordinary list with the same items.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001361
1362
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001363static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001364array_fromstring(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001365{
1366 char *str;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001367 Py_ssize_t n;
Guido van Rossum778983b1993-02-19 15:55:02 +00001368 int itemsize = self->ob_descr->itemsize;
Peter Schneider-Kampdc71cac2000-07-31 21:57:30 +00001369 if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +00001370 return NULL;
1371 if (n % itemsize != 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001372 PyErr_SetString(PyExc_ValueError,
Guido van Rossum778983b1993-02-19 15:55:02 +00001373 "string length not a multiple of item size");
1374 return NULL;
1375 }
1376 n = n / itemsize;
1377 if (n > 0) {
1378 char *item = self->ob_item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001379 PyMem_RESIZE(item, char, (Py_Size(self) + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001380 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001381 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001382 return NULL;
1383 }
1384 self->ob_item = item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001385 Py_Size(self) += n;
1386 self->allocated = Py_Size(self);
1387 memcpy(item + (Py_Size(self) - n) * itemsize,
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001388 str, itemsize*n);
Guido van Rossum778983b1993-02-19 15:55:02 +00001389 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001390 Py_INCREF(Py_None);
1391 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001392}
1393
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001394PyDoc_STRVAR(fromstring_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001395"fromstring(string)\n\
1396\n\
1397Appends items from the string, interpreting it as an array of machine\n\
Walter Dörwald93b30b52007-06-22 12:21:53 +00001398values, as if it had been read from a file using the fromfile() method).");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001399
1400
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001401static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001402array_tostring(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001403{
Guido van Rossum31f72d72007-06-18 18:44:28 +00001404 return PyBytes_FromStringAndSize(self->ob_item,
Martin v. Löwis5d7428b2007-07-21 18:47:48 +00001405 Py_Size(self) * self->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001406}
1407
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001408PyDoc_STRVAR(tostring_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001409"tostring() -> string\n\
1410\n\
1411Convert the array to an array of machine values and return the string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001412representation.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001413
Martin v. Löwis99866332002-03-01 10:27:01 +00001414
1415
Martin v. Löwis99866332002-03-01 10:27:01 +00001416static PyObject *
1417array_fromunicode(arrayobject *self, PyObject *args)
1418{
1419 Py_UNICODE *ustr;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001420 Py_ssize_t n;
Martin v. Löwis99866332002-03-01 10:27:01 +00001421
1422 if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
1423 return NULL;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001424 if (self->ob_descr->typecode != PyArr_UNI) {
Martin v. Löwis99866332002-03-01 10:27:01 +00001425 PyErr_SetString(PyExc_ValueError,
1426 "fromunicode() may only be called on "
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001427 "unicode type arrays");
Martin v. Löwis99866332002-03-01 10:27:01 +00001428 return NULL;
1429 }
1430 if (n > 0) {
1431 Py_UNICODE *item = (Py_UNICODE *) self->ob_item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001432 PyMem_RESIZE(item, Py_UNICODE, Py_Size(self) + n);
Martin v. Löwis99866332002-03-01 10:27:01 +00001433 if (item == NULL) {
1434 PyErr_NoMemory();
1435 return NULL;
1436 }
1437 self->ob_item = (char *) item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001438 Py_Size(self) += n;
1439 self->allocated = Py_Size(self);
1440 memcpy(item + Py_Size(self) - n,
Martin v. Löwis99866332002-03-01 10:27:01 +00001441 ustr, n * sizeof(Py_UNICODE));
1442 }
1443
1444 Py_INCREF(Py_None);
1445 return Py_None;
1446}
1447
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001448PyDoc_STRVAR(fromunicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001449"fromunicode(ustr)\n\
1450\n\
1451Extends this array with data from the unicode string ustr.\n\
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001452The array must be a unicode type array; otherwise a ValueError\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001453is raised. Use array.fromstring(ustr.decode(...)) to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001454append Unicode data to an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001455
1456
1457static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001458array_tounicode(arrayobject *self, PyObject *unused)
Martin v. Löwis99866332002-03-01 10:27:01 +00001459{
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001460 if (self->ob_descr->typecode != PyArr_UNI) {
Martin v. Löwis99866332002-03-01 10:27:01 +00001461 PyErr_SetString(PyExc_ValueError,
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001462 "tounicode() may only be called on unicode type arrays");
Martin v. Löwis99866332002-03-01 10:27:01 +00001463 return NULL;
1464 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001465 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_Size(self));
Martin v. Löwis99866332002-03-01 10:27:01 +00001466}
1467
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001468PyDoc_STRVAR(tounicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001469"tounicode() -> unicode\n\
1470\n\
1471Convert the array to a unicode string. The array must be\n\
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001472a unicode type array; otherwise a ValueError is raised. Use\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001473array.tostring().decode() to obtain a unicode string from\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001474an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001475
Martin v. Löwis99866332002-03-01 10:27:01 +00001476
1477
1478static PyObject *
1479array_get_typecode(arrayobject *a, void *closure)
1480{
1481 char tc = a->ob_descr->typecode;
Walter Dörwald93b30b52007-06-22 12:21:53 +00001482 return PyUnicode_FromStringAndSize(&tc, 1);
Martin v. Löwis99866332002-03-01 10:27:01 +00001483}
1484
1485static PyObject *
1486array_get_itemsize(arrayobject *a, void *closure)
1487{
1488 return PyInt_FromLong((long)a->ob_descr->itemsize);
1489}
1490
1491static PyGetSetDef array_getsets [] = {
1492 {"typecode", (getter) array_get_typecode, NULL,
1493 "the typecode character used to create the array"},
1494 {"itemsize", (getter) array_get_itemsize, NULL,
1495 "the size, in bytes, of one array item"},
1496 {NULL}
1497};
1498
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001499PyMethodDef array_methods[] = {
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001500 {"append", (PyCFunction)array_append, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001501 append_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001502 {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001503 buffer_info_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001504 {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001505 byteswap_doc},
Raymond Hettinger3aa82c02004-03-13 18:18:51 +00001506 {"__copy__", (PyCFunction)array_copy, METH_NOARGS,
1507 copy_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001508 {"count", (PyCFunction)array_count, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001509 count_doc},
Thomas Wouters89f507f2006-12-13 04:49:30 +00001510 {"__deepcopy__",(PyCFunction)array_copy, METH_O,
Raymond Hettinger3aa82c02004-03-13 18:18:51 +00001511 copy_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001512 {"extend", (PyCFunction)array_extend, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001513 extend_doc},
1514 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
1515 fromfile_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001516 {"fromlist", (PyCFunction)array_fromlist, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001517 fromlist_doc},
1518 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
1519 fromstring_doc},
Martin v. Löwis99866332002-03-01 10:27:01 +00001520 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
1521 fromunicode_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001522 {"index", (PyCFunction)array_index, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001523 index_doc},
1524 {"insert", (PyCFunction)array_insert, METH_VARARGS,
1525 insert_doc},
1526 {"pop", (PyCFunction)array_pop, METH_VARARGS,
1527 pop_doc},
1528 {"read", (PyCFunction)array_fromfile, METH_VARARGS,
1529 fromfile_doc},
Raymond Hettingerb0900e62004-12-16 16:23:40 +00001530 {"__reduce__", (PyCFunction)array_reduce, METH_NOARGS,
1531 array_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001532 {"remove", (PyCFunction)array_remove, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001533 remove_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001534 {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001535 reverse_doc},
1536/* {"sort", (PyCFunction)array_sort, METH_VARARGS,
1537 sort_doc},*/
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001538 {"tofile", (PyCFunction)array_tofile, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001539 tofile_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001540 {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001541 tolist_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001542 {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001543 tostring_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001544 {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
Martin v. Löwis99866332002-03-01 10:27:01 +00001545 tounicode_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001546 {"write", (PyCFunction)array_tofile, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001547 tofile_doc},
Guido van Rossum778983b1993-02-19 15:55:02 +00001548 {NULL, NULL} /* sentinel */
1549};
1550
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001551static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001552array_repr(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +00001553{
Walter Dörwaldc2f6a582007-05-20 12:49:47 +00001554 char typecode;
Walter Dörwald7569dfe2007-05-19 21:49:49 +00001555 PyObject *s, *v = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001556 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00001557
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001558 len = Py_Size(a);
Martin v. Löwis99866332002-03-01 10:27:01 +00001559 typecode = a->ob_descr->typecode;
Guido van Rossum778983b1993-02-19 15:55:02 +00001560 if (len == 0) {
Walter Dörwald7569dfe2007-05-19 21:49:49 +00001561 return PyUnicode_FromFormat("array('%c')", typecode);
Guido van Rossum778983b1993-02-19 15:55:02 +00001562 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001563 if (typecode == PyArr_UNI)
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001564 v = array_tounicode(a, NULL);
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001565 else
1566 v = array_tolist(a, NULL);
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001567
Walter Dörwald7569dfe2007-05-19 21:49:49 +00001568 s = PyUnicode_FromFormat("array('%c', %R)", typecode, v);
1569 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001570 return s;
1571}
1572
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001573static PyObject*
1574array_subscr(arrayobject* self, PyObject* item)
1575{
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001576 if (PyIndex_Check(item)) {
1577 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001578 if (i==-1 && PyErr_Occurred()) {
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001579 return NULL;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001580 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001581 if (i < 0)
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001582 i += Py_Size(self);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001583 return array_item(self, i);
1584 }
1585 else if (PySlice_Check(item)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001586 Py_ssize_t start, stop, step, slicelength, cur, i;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001587 PyObject* result;
1588 arrayobject* ar;
1589 int itemsize = self->ob_descr->itemsize;
1590
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001591 if (PySlice_GetIndicesEx((PySliceObject*)item, Py_Size(self),
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001592 &start, &stop, &step, &slicelength) < 0) {
1593 return NULL;
1594 }
1595
1596 if (slicelength <= 0) {
1597 return newarrayobject(&Arraytype, 0, self->ob_descr);
1598 }
Thomas Woutersed03b412007-08-28 21:37:11 +00001599 else if (step == 1) {
1600 PyObject *result = newarrayobject(&Arraytype,
1601 slicelength, self->ob_descr);
1602 if (result == NULL)
1603 return NULL;
1604 memcpy(((arrayobject *)result)->ob_item,
1605 self->ob_item + start * itemsize,
1606 slicelength * itemsize);
1607 return result;
1608 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001609 else {
1610 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
1611 if (!result) return NULL;
1612
1613 ar = (arrayobject*)result;
1614
1615 for (cur = start, i = 0; i < slicelength;
1616 cur += step, i++) {
1617 memcpy(ar->ob_item + i*itemsize,
1618 self->ob_item + cur*itemsize,
1619 itemsize);
1620 }
1621
1622 return result;
1623 }
1624 }
1625 else {
1626 PyErr_SetString(PyExc_TypeError,
Thomas Woutersed03b412007-08-28 21:37:11 +00001627 "array indices must be integers");
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001628 return NULL;
1629 }
1630}
1631
1632static int
1633array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
1634{
Thomas Woutersed03b412007-08-28 21:37:11 +00001635 Py_ssize_t start, stop, step, slicelength, needed;
1636 arrayobject* other;
1637 int itemsize;
1638
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001639 if (PyIndex_Check(item)) {
1640 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Thomas Woutersed03b412007-08-28 21:37:11 +00001641
1642 if (i == -1 && PyErr_Occurred())
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001643 return -1;
1644 if (i < 0)
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001645 i += Py_Size(self);
Thomas Woutersed03b412007-08-28 21:37:11 +00001646 if (i < 0 || i >= Py_Size(self)) {
1647 PyErr_SetString(PyExc_IndexError,
1648 "array assignment index out of range");
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001649 return -1;
1650 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001651 if (value == NULL) {
Thomas Woutersed03b412007-08-28 21:37:11 +00001652 /* Fall through to slice assignment */
1653 start = i;
1654 stop = i + 1;
1655 step = 1;
1656 slicelength = 1;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001657 }
Thomas Woutersed03b412007-08-28 21:37:11 +00001658 else
1659 return (*self->ob_descr->setitem)(self, i, value);
1660 }
1661 else if (PySlice_Check(item)) {
1662 if (PySlice_GetIndicesEx((PySliceObject *)item,
1663 Py_Size(self), &start, &stop,
1664 &step, &slicelength) < 0) {
1665 return -1;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001666 }
Thomas Woutersed03b412007-08-28 21:37:11 +00001667 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001668 else {
Thomas Woutersed03b412007-08-28 21:37:11 +00001669 PyErr_SetString(PyExc_TypeError,
1670 "array indices must be integer");
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001671 return -1;
1672 }
Thomas Woutersed03b412007-08-28 21:37:11 +00001673 if (value == NULL) {
1674 other = NULL;
1675 needed = 0;
1676 }
1677 else if (array_Check(value)) {
1678 other = (arrayobject *)value;
1679 needed = Py_Size(other);
1680 if (self == other) {
1681 /* Special case "self[i:j] = self" -- copy self first */
1682 int ret;
1683 value = array_slice(other, 0, needed);
1684 if (value == NULL)
1685 return -1;
1686 ret = array_ass_subscr(self, item, value);
1687 Py_DECREF(value);
1688 return ret;
1689 }
1690 if (other->ob_descr != self->ob_descr) {
1691 PyErr_BadArgument();
1692 return -1;
1693 }
1694 }
1695 else {
1696 PyErr_Format(PyExc_TypeError,
1697 "can only assign array (not \"%.200s\") to array slice",
1698 Py_Type(value)->tp_name);
1699 return -1;
1700 }
1701 itemsize = self->ob_descr->itemsize;
1702 /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
1703 if ((step > 0 && stop < start) ||
1704 (step < 0 && stop > start))
1705 stop = start;
1706 if (step == 1) {
1707 if (slicelength > needed) {
1708 memmove(self->ob_item + (start + needed) * itemsize,
1709 self->ob_item + stop * itemsize,
1710 (Py_Size(self) - stop) * itemsize);
1711 if (array_resize(self, Py_Size(self) +
1712 needed - slicelength) < 0)
1713 return -1;
1714 }
1715 else if (slicelength < needed) {
1716 if (array_resize(self, Py_Size(self) +
1717 needed - slicelength) < 0)
1718 return -1;
1719 memmove(self->ob_item + (start + needed) * itemsize,
1720 self->ob_item + stop * itemsize,
1721 (Py_Size(self) - start - needed) * itemsize);
1722 }
1723 if (needed > 0)
1724 memcpy(self->ob_item + start * itemsize,
1725 other->ob_item, needed * itemsize);
1726 return 0;
1727 }
1728 else if (needed == 0) {
1729 /* Delete slice */
1730 Py_ssize_t cur, i;
1731
1732 if (step < 0) {
1733 stop = start + 1;
1734 start = stop + step * (slicelength - 1) - 1;
1735 step = -step;
1736 }
1737 for (cur = start, i = 0; i < slicelength;
1738 cur += step, i++) {
1739 Py_ssize_t lim = step - 1;
1740
1741 if (cur + step >= Py_Size(self))
1742 lim = Py_Size(self) - cur - 1;
1743 memmove(self->ob_item + (cur - i) * itemsize,
1744 self->ob_item + (cur + 1) * itemsize,
1745 lim * itemsize);
1746 }
1747 cur = start + slicelength * step;
1748 if (cur < Py_Size(self)) {
1749 memmove(self->ob_item + (cur-slicelength) * itemsize,
1750 self->ob_item + cur * itemsize,
1751 (Py_Size(self) - cur) * itemsize);
1752 }
1753 if (array_resize(self, Py_Size(self) - slicelength) < 0)
1754 return -1;
1755 return 0;
1756 }
1757 else {
1758 Py_ssize_t cur, i;
1759
1760 if (needed != slicelength) {
1761 PyErr_Format(PyExc_ValueError,
1762 "attempt to assign array of size %zd "
1763 "to extended slice of size %zd",
1764 needed, slicelength);
1765 return -1;
1766 }
1767 for (cur = start, i = 0; i < slicelength;
1768 cur += step, i++) {
1769 memcpy(self->ob_item + cur * itemsize,
1770 other->ob_item + i * itemsize,
1771 itemsize);
1772 }
1773 return 0;
1774 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001775}
1776
1777static PyMappingMethods array_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001778 (lenfunc)array_length,
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001779 (binaryfunc)array_subscr,
1780 (objobjargproc)array_ass_subscr
1781};
1782
Guido van Rossumd8faa362007-04-27 19:54:29 +00001783static const void *emptybuf = "";
1784
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001785
1786static int
1787array_buffer_getbuf(arrayobject *self, PyBuffer *view, int flags)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001788{
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001789 if ((flags & PyBUF_CHARACTER)) {
1790 PyErr_SetString(PyExc_TypeError,
1791 "Cannot be a character buffer");
1792 return -1;
1793 }
1794 if ((flags & PyBUF_LOCKDATA)) {
1795 PyErr_SetString(PyExc_BufferError,
1796 "Cannot lock data");
1797 return -1;
1798 }
1799 if (view==NULL) goto finish;
1800
1801 view->buf = (void *)self->ob_item;
1802 if (view->buf == NULL)
1803 view->buf = (void *)emptybuf;
1804 view->len = (Py_Size(self)) * self->ob_descr->itemsize;
1805 view->readonly = 0;
1806 view->ndim = 1;
1807 view->itemsize = self->ob_descr->itemsize;
1808 view->suboffsets = NULL;
1809 view->shape = NULL;
1810 if ((flags & PyBUF_ND)==PyBUF_ND) {
1811 view->shape = &((Py_Size(self)));
1812 }
1813 view->strides = NULL;
1814 if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES)
1815 view->strides = &(view->itemsize);
1816 view->format = NULL;
1817 view->internal = NULL;
1818 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
Travis E. Oliphantb803c512007-08-20 07:16:33 +00001819 view->format = self->ob_descr->formats;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001820 }
1821
1822 finish:
1823 self->ob_exports++;
1824 return 0;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001825}
1826
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001827static void
1828array_buffer_relbuf(arrayobject *self, PyBuffer *view)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001829{
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001830 self->ob_exports--;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001831}
1832
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001833static PySequenceMethods array_as_sequence = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001834 (lenfunc)array_length, /*sq_length*/
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001835 (binaryfunc)array_concat, /*sq_concat*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001836 (ssizeargfunc)array_repeat, /*sq_repeat*/
1837 (ssizeargfunc)array_item, /*sq_item*/
1838 (ssizessizeargfunc)array_slice, /*sq_slice*/
1839 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
1840 (ssizessizeobjargproc)array_ass_slice, /*sq_ass_slice*/
Raymond Hettinger625812f2003-01-07 01:58:52 +00001841 (objobjproc)array_contains, /*sq_contains*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001842 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001843 (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001844};
1845
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001846static PyBufferProcs array_as_buffer = {
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001847 (getbufferproc)array_buffer_getbuf,
1848 (releasebufferproc)array_buffer_relbuf
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001849};
1850
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001851static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +00001852array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum778983b1993-02-19 15:55:02 +00001853{
Guido van Rossum8934fc22007-06-30 23:44:36 +00001854 int c;
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001855 PyObject *initial = NULL, *it = NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001856 struct arraydescr *descr;
Georg Brandl02c42872005-08-26 06:42:30 +00001857
Thomas Woutersb2137042007-02-01 18:02:27 +00001858 if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001859 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001860
Walter Dörwaldd0941302007-07-01 21:58:22 +00001861 if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
Martin v. Löwis99866332002-03-01 10:27:01 +00001862 return NULL;
1863
1864 if (!(initial == NULL || PyList_Check(initial)
Guido van Rossum6b826ab2007-07-03 16:22:09 +00001865 || PyBytes_Check(initial)
Neal Norwitz3fcbea52007-08-26 04:51:28 +00001866 || PyTuple_Check(initial)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001867 || (c == PyArr_UNI && PyUnicode_Check(initial)))) {
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001868 it = PyObject_GetIter(initial);
1869 if (it == NULL)
1870 return NULL;
1871 /* We set initial to NULL so that the subsequent code
1872 will create an empty array of the appropriate type
1873 and afterwards we can use array_iter_extend to populate
1874 the array.
1875 */
1876 initial = NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001877 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001878 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1879 if (descr->typecode == c) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001880 PyObject *a;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001881 Py_ssize_t len;
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001882
1883 if (initial == NULL || !(PyList_Check(initial)
1884 || PyTuple_Check(initial)))
Guido van Rossum778983b1993-02-19 15:55:02 +00001885 len = 0;
1886 else
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001887 len = PySequence_Size(initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001888
1889 a = newarrayobject(type, len, descr);
Guido van Rossum778983b1993-02-19 15:55:02 +00001890 if (a == NULL)
1891 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001892
Guido van Rossum778983b1993-02-19 15:55:02 +00001893 if (len > 0) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001894 Py_ssize_t i;
Guido van Rossum778983b1993-02-19 15:55:02 +00001895 for (i = 0; i < len; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001896 PyObject *v =
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001897 PySequence_GetItem(initial, i);
Raymond Hettinger85004cc2003-08-05 11:23:59 +00001898 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001899 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001900 return NULL;
1901 }
Raymond Hettinger85004cc2003-08-05 11:23:59 +00001902 if (setarrayitem(a, i, v) != 0) {
1903 Py_DECREF(v);
1904 Py_DECREF(a);
1905 return NULL;
1906 }
1907 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001908 }
Neal Norwitz3fcbea52007-08-26 04:51:28 +00001909 } else if (initial != NULL && PyBytes_Check(initial)) {
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001910 PyObject *t_initial, *v;
1911 t_initial = PyTuple_Pack(1, initial);
1912 if (t_initial == NULL) {
1913 Py_DECREF(a);
1914 return NULL;
1915 }
1916 v = array_fromstring((arrayobject *)a,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001917 t_initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001918 Py_DECREF(t_initial);
Guido van Rossum778983b1993-02-19 15:55:02 +00001919 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001920 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001921 return NULL;
1922 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001923 Py_DECREF(v);
Martin v. Löwis99866332002-03-01 10:27:01 +00001924 } else if (initial != NULL && PyUnicode_Check(initial)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001925 Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001926 if (n > 0) {
1927 arrayobject *self = (arrayobject *)a;
1928 char *item = self->ob_item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001929 item = (char *)PyMem_Realloc(item, n);
Martin v. Löwis99866332002-03-01 10:27:01 +00001930 if (item == NULL) {
1931 PyErr_NoMemory();
1932 Py_DECREF(a);
1933 return NULL;
1934 }
1935 self->ob_item = item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001936 Py_Size(self) = n / sizeof(Py_UNICODE);
Martin v. Löwis99866332002-03-01 10:27:01 +00001937 memcpy(item, PyUnicode_AS_DATA(initial), n);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001938 self->allocated = Py_Size(self);
Martin v. Löwis99866332002-03-01 10:27:01 +00001939 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001940 }
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001941 if (it != NULL) {
1942 if (array_iter_extend((arrayobject *)a, it) == -1) {
1943 Py_DECREF(it);
1944 Py_DECREF(a);
1945 return NULL;
1946 }
1947 Py_DECREF(it);
1948 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001949 return a;
1950 }
1951 }
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001952 PyErr_SetString(PyExc_ValueError,
Guido van Rossum31f72d72007-06-18 18:44:28 +00001953 "bad typecode (must be b, B, u, h, H, i, I, l, L, f or d)");
Guido van Rossum778983b1993-02-19 15:55:02 +00001954 return NULL;
1955}
1956
Guido van Rossum778983b1993-02-19 15:55:02 +00001957
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001958PyDoc_STRVAR(module_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001959"This module defines an object type which can efficiently represent\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001960an array of basic values: characters, integers, floating point\n\
1961numbers. Arrays are sequence types and behave very much like lists,\n\
1962except that the type of objects stored in them is constrained. The\n\
1963type is specified at object creation time by using a type code, which\n\
1964is a single character. The following type codes are defined:\n\
1965\n\
1966 Type code C Type Minimum size in bytes \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001967 'b' signed integer 1 \n\
1968 'B' unsigned integer 1 \n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001969 'u' Unicode character 2 \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001970 'h' signed integer 2 \n\
1971 'H' unsigned integer 2 \n\
1972 'i' signed integer 2 \n\
1973 'I' unsigned integer 2 \n\
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001974 'w' unicode character 4 \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001975 'l' signed integer 4 \n\
1976 'L' unsigned integer 4 \n\
1977 'f' floating point 4 \n\
1978 'd' floating point 8 \n\
1979\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001980The constructor is:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001981\n\
1982array(typecode [, initializer]) -- create a new array\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001983");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001984
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001985PyDoc_STRVAR(arraytype_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001986"array(typecode [, initializer]) -> array\n\
1987\n\
1988Return a new array whose items are restricted by typecode, and\n\
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001989initialized from the optional initializer value, which must be a list,\n\
1990string. or iterable over elements of the appropriate type.\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001991\n\
1992Arrays represent basic values and behave very much like lists, except\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001993the type of objects stored in them is constrained.\n\
1994\n\
1995Methods:\n\
1996\n\
1997append() -- append a new item to the end of the array\n\
1998buffer_info() -- return information giving the current memory info\n\
1999byteswap() -- byteswap all the items of the array\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00002000count() -- return number of occurences of an object\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00002001extend() -- extend array by appending multiple elements from an iterable\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002002fromfile() -- read items from a file object\n\
2003fromlist() -- append items from the list\n\
2004fromstring() -- append items from the string\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00002005index() -- return index of first occurence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002006insert() -- insert a new item into the array at a provided position\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00002007pop() -- remove and return item (default last)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002008read() -- DEPRECATED, use fromfile()\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00002009remove() -- remove first occurence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002010reverse() -- reverse the order of the items in the array\n\
2011tofile() -- write all items to a file object\n\
2012tolist() -- return the array converted to an ordinary list\n\
2013tostring() -- return the array converted to a string\n\
2014write() -- DEPRECATED, use tofile()\n\
2015\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002016Attributes:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002017\n\
2018typecode -- the typecode character used to create the array\n\
2019itemsize -- the length in bytes of one array item\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002020");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002021
Raymond Hettinger625812f2003-01-07 01:58:52 +00002022static PyObject *array_iter(arrayobject *ao);
2023
Tim Peters0c322792002-07-17 16:49:03 +00002024static PyTypeObject Arraytype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002025 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00002026 "array.array",
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002027 sizeof(arrayobject),
2028 0,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00002029 (destructor)array_dealloc, /* tp_dealloc */
Martin v. Löwisaa158be2002-03-04 09:38:52 +00002030 0, /* tp_print */
Martin v. Löwis99866332002-03-01 10:27:01 +00002031 0, /* tp_getattr */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00002032 0, /* tp_setattr */
2033 0, /* tp_compare */
2034 (reprfunc)array_repr, /* tp_repr */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002035 0, /* tp_as_number*/
2036 &array_as_sequence, /* tp_as_sequence*/
2037 &array_as_mapping, /* tp_as_mapping*/
Guido van Rossum9d19cb82001-01-18 01:02:55 +00002038 0, /* tp_hash */
2039 0, /* tp_call */
2040 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002041 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00002042 0, /* tp_setattro */
Tim Petersb870c752001-06-05 04:43:26 +00002043 &array_as_buffer, /* tp_as_buffer*/
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002044 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00002045 arraytype_doc, /* tp_doc */
2046 0, /* tp_traverse */
2047 0, /* tp_clear */
2048 array_richcompare, /* tp_richcompare */
Raymond Hettingercb87bc82004-05-31 00:35:52 +00002049 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger625812f2003-01-07 01:58:52 +00002050 (getiterfunc)array_iter, /* tp_iter */
Martin v. Löwis99866332002-03-01 10:27:01 +00002051 0, /* tp_iternext */
2052 array_methods, /* tp_methods */
2053 0, /* tp_members */
2054 array_getsets, /* tp_getset */
2055 0, /* tp_base */
2056 0, /* tp_dict */
2057 0, /* tp_descr_get */
2058 0, /* tp_descr_set */
2059 0, /* tp_dictoffset */
2060 0, /* tp_init */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002061 PyType_GenericAlloc, /* tp_alloc */
Martin v. Löwis99866332002-03-01 10:27:01 +00002062 array_new, /* tp_new */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002063 PyObject_Del, /* tp_free */
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002064};
2065
Raymond Hettinger625812f2003-01-07 01:58:52 +00002066
2067/*********************** Array Iterator **************************/
2068
2069typedef struct {
2070 PyObject_HEAD
Martin v. Löwis18e16552006-02-15 17:27:45 +00002071 Py_ssize_t index;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002072 arrayobject *ao;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002073 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002074} arrayiterobject;
2075
2076static PyTypeObject PyArrayIter_Type;
2077
2078#define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
2079
2080static PyObject *
2081array_iter(arrayobject *ao)
2082{
2083 arrayiterobject *it;
2084
2085 if (!array_Check(ao)) {
2086 PyErr_BadInternalCall();
2087 return NULL;
2088 }
2089
2090 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
2091 if (it == NULL)
2092 return NULL;
2093
2094 Py_INCREF(ao);
2095 it->ao = ao;
2096 it->index = 0;
2097 it->getitem = ao->ob_descr->getitem;
2098 PyObject_GC_Track(it);
2099 return (PyObject *)it;
2100}
2101
2102static PyObject *
Raymond Hettinger625812f2003-01-07 01:58:52 +00002103arrayiter_next(arrayiterobject *it)
2104{
2105 assert(PyArrayIter_Check(it));
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002106 if (it->index < Py_Size(it->ao))
Raymond Hettinger625812f2003-01-07 01:58:52 +00002107 return (*it->getitem)(it->ao, it->index++);
2108 return NULL;
2109}
2110
2111static void
2112arrayiter_dealloc(arrayiterobject *it)
2113{
2114 PyObject_GC_UnTrack(it);
2115 Py_XDECREF(it->ao);
2116 PyObject_GC_Del(it);
2117}
2118
2119static int
2120arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2121{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002122 Py_VISIT(it->ao);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002123 return 0;
2124}
2125
2126static PyTypeObject PyArrayIter_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002127 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger625812f2003-01-07 01:58:52 +00002128 "arrayiterator", /* tp_name */
2129 sizeof(arrayiterobject), /* tp_basicsize */
2130 0, /* tp_itemsize */
2131 /* methods */
2132 (destructor)arrayiter_dealloc, /* tp_dealloc */
2133 0, /* tp_print */
2134 0, /* tp_getattr */
2135 0, /* tp_setattr */
2136 0, /* tp_compare */
2137 0, /* tp_repr */
2138 0, /* tp_as_number */
2139 0, /* tp_as_sequence */
2140 0, /* tp_as_mapping */
2141 0, /* tp_hash */
2142 0, /* tp_call */
2143 0, /* tp_str */
2144 PyObject_GenericGetAttr, /* tp_getattro */
2145 0, /* tp_setattro */
2146 0, /* tp_as_buffer */
2147 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2148 0, /* tp_doc */
2149 (traverseproc)arrayiter_traverse, /* tp_traverse */
2150 0, /* tp_clear */
2151 0, /* tp_richcompare */
2152 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002153 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger625812f2003-01-07 01:58:52 +00002154 (iternextfunc)arrayiter_next, /* tp_iternext */
2155 0, /* tp_methods */
2156};
2157
2158
2159/*********************** Install Module **************************/
2160
Martin v. Löwis99866332002-03-01 10:27:01 +00002161/* No functions in array module. */
2162static PyMethodDef a_methods[] = {
2163 {NULL, NULL, 0, NULL} /* Sentinel */
2164};
2165
2166
Mark Hammondfe51c6d2002-08-02 02:27:13 +00002167PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00002168initarray(void)
Guido van Rossum778983b1993-02-19 15:55:02 +00002169{
Fred Drakef4e34842002-04-01 03:45:06 +00002170 PyObject *m;
Fred Drake0d40ba42000-02-04 20:33:49 +00002171
Guido van Rossum50e9fb92006-08-17 05:42:55 +00002172 if (PyType_Ready(&Arraytype) < 0)
2173 return;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002174 Py_Type(&PyArrayIter_Type) = &PyType_Type;
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002175 m = Py_InitModule3("array", a_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00002176 if (m == NULL)
2177 return;
Fred Drakef4e34842002-04-01 03:45:06 +00002178
2179 Py_INCREF((PyObject *)&Arraytype);
2180 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
2181 Py_INCREF((PyObject *)&Arraytype);
2182 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
Guido van Rossuma0deb641998-10-14 13:45:06 +00002183 /* No need to check the error here, the caller will do that */
Guido van Rossum778983b1993-02-19 15:55:02 +00002184}