blob: 240a3720c52ac27d248979768366fa40a2c1bc97 [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
Martin v. Löwis99866332002-03-01 10:27:01 +000043#define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
Christian Heimes90aa7642007-12-19 02:45:37 +000044#define array_CheckExact(op) (Py_TYPE(op) == &Arraytype)
Guido van Rossum778983b1993-02-19 15:55:02 +000045
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000046static int
Martin v. Löwis18e16552006-02-15 17:27:45 +000047array_resize(arrayobject *self, Py_ssize_t newsize)
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000048{
49 char *items;
50 size_t _new_size;
51
52 /* Bypass realloc() when a previous overallocation is large enough
53 to accommodate the newsize. If the newsize is 16 smaller than the
54 current size, then proceed with the realloc() to shrink the list.
55 */
56
57 if (self->allocated >= newsize &&
Christian Heimes90aa7642007-12-19 02:45:37 +000058 Py_SIZE(self) < newsize + 16 &&
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000059 self->ob_item != NULL) {
Christian Heimes90aa7642007-12-19 02:45:37 +000060 Py_SIZE(self) = newsize;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000061 return 0;
62 }
63
Travis E. Oliphantb99f7622007-08-18 11:21:56 +000064 if (self->ob_exports > 0) {
65 PyErr_SetString(PyExc_BufferError,
66 "cannot resize an array that is exporting data");
67 return -1;
68 }
69
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000070 /* This over-allocates proportional to the array size, making room
71 * for additional growth. The over-allocation is mild, but is
72 * enough to give linear-time amortized behavior over a long
73 * sequence of appends() in the presence of a poorly-performing
74 * system realloc().
75 * The growth pattern is: 0, 4, 8, 16, 25, 34, 46, 56, 67, 79, ...
76 * Note, the pattern starts out the same as for lists but then
77 * grows at a smaller rate so that larger arrays only overallocate
78 * by about 1/16th -- this is done because arrays are presumed to be more
79 * memory critical.
80 */
81
Christian Heimes90aa7642007-12-19 02:45:37 +000082 _new_size = (newsize >> 4) + (Py_SIZE(self) < 8 ? 3 : 7) + newsize;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000083 items = self->ob_item;
84 /* XXX The following multiplication and division does not optimize away
85 like it does for lists since the size is not known at compile time */
86 if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize))
87 PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize));
88 else
89 items = NULL;
90 if (items == NULL) {
91 PyErr_NoMemory();
92 return -1;
93 }
94 self->ob_item = items;
Christian Heimes90aa7642007-12-19 02:45:37 +000095 Py_SIZE(self) = newsize;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000096 self->allocated = _new_size;
97 return 0;
98}
99
Tim Petersbb307342000-09-10 05:22:54 +0000100/****************************************************************************
101Get and Set functions for each type.
102A Get function takes an arrayobject* and an integer index, returning the
103array value at that index wrapped in an appropriate PyObject*.
104A Set function takes an arrayobject, integer index, and PyObject*; sets
105the array value at that index to the raw C data extracted from the PyObject*,
106and returns 0 if successful, else nonzero on failure (PyObject* not of an
107appropriate type or value).
108Note that the basic Get and Set functions do NOT check that the index is
109in bounds; that's the responsibility of the caller.
110****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +0000111
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000112static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000113b_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000114{
115 long x = ((char *)ap->ob_item)[i];
116 if (x >= 128)
117 x -= 256;
Christian Heimes217cfd12007-12-02 14:31:20 +0000118 return PyLong_FromLong(x);
Guido van Rossum778983b1993-02-19 15:55:02 +0000119}
120
121static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000122b_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000123{
Fred Drake541dc3b2000-06-28 17:49:30 +0000124 short x;
125 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
126 must use the next size up that is signed ('h') and manually do
127 the overflow checking */
128 if (!PyArg_Parse(v, "h;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000129 return -1;
Guido van Rossum9f754e02000-07-01 00:38:19 +0000130 else if (x < -128) {
Fred Drake541dc3b2000-06-28 17:49:30 +0000131 PyErr_SetString(PyExc_OverflowError,
132 "signed char is less than minimum");
133 return -1;
134 }
Guido van Rossum9f754e02000-07-01 00:38:19 +0000135 else if (x > 127) {
Fred Drake541dc3b2000-06-28 17:49:30 +0000136 PyErr_SetString(PyExc_OverflowError,
137 "signed char is greater than maximum");
138 return -1;
139 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000140 if (i >= 0)
Fred Drake541dc3b2000-06-28 17:49:30 +0000141 ((char *)ap->ob_item)[i] = (char)x;
Guido van Rossum778983b1993-02-19 15:55:02 +0000142 return 0;
143}
144
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000145static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000146BB_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000147{
148 long x = ((unsigned char *)ap->ob_item)[i];
Christian Heimes217cfd12007-12-02 14:31:20 +0000149 return PyLong_FromLong(x);
Guido van Rossum549ab711997-01-03 19:09:47 +0000150}
151
Fred Drake541dc3b2000-06-28 17:49:30 +0000152static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000153BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000154{
155 unsigned char x;
156 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
157 if (!PyArg_Parse(v, "b;array item must be integer", &x))
158 return -1;
159 if (i >= 0)
Martin v. Löwis99866332002-03-01 10:27:01 +0000160 ((char *)ap->ob_item)[i] = x;
Fred Drake541dc3b2000-06-28 17:49:30 +0000161 return 0;
162}
Guido van Rossum549ab711997-01-03 19:09:47 +0000163
Martin v. Löwis99866332002-03-01 10:27:01 +0000164static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000165u_getitem(arrayobject *ap, Py_ssize_t i)
Martin v. Löwis99866332002-03-01 10:27:01 +0000166{
167 return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1);
168}
169
170static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000171u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Martin v. Löwis99866332002-03-01 10:27:01 +0000172{
173 Py_UNICODE *p;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000174 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +0000175
176 if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
177 return -1;
178 if (len != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000179 PyErr_SetString(PyExc_TypeError,
180 "array item must be unicode character");
Martin v. Löwis99866332002-03-01 10:27:01 +0000181 return -1;
182 }
183 if (i >= 0)
184 ((Py_UNICODE *)ap->ob_item)[i] = p[0];
185 return 0;
186}
Martin v. Löwis99866332002-03-01 10:27:01 +0000187
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +0000188
Guido van Rossum549ab711997-01-03 19:09:47 +0000189static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000190h_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000191{
Christian Heimes217cfd12007-12-02 14:31:20 +0000192 return PyLong_FromLong((long) ((short *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000193}
194
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +0000195
Guido van Rossum778983b1993-02-19 15:55:02 +0000196static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000197h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000198{
199 short x;
Fred Drake541dc3b2000-06-28 17:49:30 +0000200 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000201 if (!PyArg_Parse(v, "h;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000202 return -1;
203 if (i >= 0)
204 ((short *)ap->ob_item)[i] = x;
205 return 0;
206}
207
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000208static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000209HH_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000210{
Christian Heimes217cfd12007-12-02 14:31:20 +0000211 return PyLong_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000212}
213
Fred Drake541dc3b2000-06-28 17:49:30 +0000214static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000215HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000216{
217 int x;
218 /* PyArg_Parse's 'h' formatter is for a signed short, therefore
219 must use the next size up and manually do the overflow checking */
220 if (!PyArg_Parse(v, "i;array item must be integer", &x))
221 return -1;
222 else if (x < 0) {
223 PyErr_SetString(PyExc_OverflowError,
224 "unsigned short is less than minimum");
225 return -1;
226 }
227 else if (x > USHRT_MAX) {
228 PyErr_SetString(PyExc_OverflowError,
229 "unsigned short is greater than maximum");
230 return -1;
231 }
232 if (i >= 0)
233 ((short *)ap->ob_item)[i] = (short)x;
234 return 0;
235}
Guido van Rossum549ab711997-01-03 19:09:47 +0000236
237static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000238i_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossume77a7571993-11-03 15:01:26 +0000239{
Christian Heimes217cfd12007-12-02 14:31:20 +0000240 return PyLong_FromLong((long) ((int *)ap->ob_item)[i]);
Guido van Rossume77a7571993-11-03 15:01:26 +0000241}
242
243static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000244i_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossume77a7571993-11-03 15:01:26 +0000245{
246 int x;
Fred Drake541dc3b2000-06-28 17:49:30 +0000247 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000248 if (!PyArg_Parse(v, "i;array item must be integer", &x))
Guido van Rossume77a7571993-11-03 15:01:26 +0000249 return -1;
250 if (i >= 0)
251 ((int *)ap->ob_item)[i] = x;
252 return 0;
253}
254
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000255static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000256II_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000257{
258 return PyLong_FromUnsignedLong(
259 (unsigned long) ((unsigned int *)ap->ob_item)[i]);
260}
261
262static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000263II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000264{
265 unsigned long x;
266 if (PyLong_Check(v)) {
267 x = PyLong_AsUnsignedLong(v);
268 if (x == (unsigned long) -1 && PyErr_Occurred())
269 return -1;
270 }
271 else {
Fred Drake541dc3b2000-06-28 17:49:30 +0000272 long y;
273 if (!PyArg_Parse(v, "l;array item must be integer", &y))
Guido van Rossum549ab711997-01-03 19:09:47 +0000274 return -1;
Fred Drake541dc3b2000-06-28 17:49:30 +0000275 if (y < 0) {
276 PyErr_SetString(PyExc_OverflowError,
277 "unsigned int is less than minimum");
278 return -1;
279 }
280 x = (unsigned long)y;
Tim Petersbb307342000-09-10 05:22:54 +0000281
Guido van Rossum549ab711997-01-03 19:09:47 +0000282 }
Fred Drake541dc3b2000-06-28 17:49:30 +0000283 if (x > UINT_MAX) {
284 PyErr_SetString(PyExc_OverflowError,
285 "unsigned int is greater than maximum");
286 return -1;
287 }
288
Guido van Rossum549ab711997-01-03 19:09:47 +0000289 if (i >= 0)
Fred Drake541dc3b2000-06-28 17:49:30 +0000290 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
Guido van Rossum549ab711997-01-03 19:09:47 +0000291 return 0;
292}
293
294static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000295l_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000296{
Christian Heimes217cfd12007-12-02 14:31:20 +0000297 return PyLong_FromLong(((long *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000298}
299
300static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000301l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000302{
303 long x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000304 if (!PyArg_Parse(v, "l;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000305 return -1;
306 if (i >= 0)
307 ((long *)ap->ob_item)[i] = x;
308 return 0;
309}
310
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000311static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000312LL_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000313{
314 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
315}
316
317static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000318LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000319{
320 unsigned long x;
321 if (PyLong_Check(v)) {
322 x = PyLong_AsUnsignedLong(v);
323 if (x == (unsigned long) -1 && PyErr_Occurred())
324 return -1;
325 }
326 else {
Fred Drake541dc3b2000-06-28 17:49:30 +0000327 long y;
328 if (!PyArg_Parse(v, "l;array item must be integer", &y))
Guido van Rossum549ab711997-01-03 19:09:47 +0000329 return -1;
Fred Drake541dc3b2000-06-28 17:49:30 +0000330 if (y < 0) {
331 PyErr_SetString(PyExc_OverflowError,
332 "unsigned long is less than minimum");
333 return -1;
334 }
335 x = (unsigned long)y;
Tim Petersbb307342000-09-10 05:22:54 +0000336
Guido van Rossum549ab711997-01-03 19:09:47 +0000337 }
Fred Drake541dc3b2000-06-28 17:49:30 +0000338 if (x > ULONG_MAX) {
339 PyErr_SetString(PyExc_OverflowError,
340 "unsigned long is greater than maximum");
341 return -1;
342 }
Tim Petersbb307342000-09-10 05:22:54 +0000343
Guido van Rossum549ab711997-01-03 19:09:47 +0000344 if (i >= 0)
345 ((unsigned long *)ap->ob_item)[i] = x;
346 return 0;
347}
348
349static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000350f_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000351{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000352 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000353}
354
355static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000356f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000357{
358 float x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000359 if (!PyArg_Parse(v, "f;array item must be float", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000360 return -1;
361 if (i >= 0)
362 ((float *)ap->ob_item)[i] = x;
363 return 0;
364}
365
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000366static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000367d_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000368{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000369 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000370}
371
372static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000373d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000374{
375 double x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000376 if (!PyArg_Parse(v, "d;array item must be float", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000377 return -1;
378 if (i >= 0)
379 ((double *)ap->ob_item)[i] = x;
380 return 0;
381}
382
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000383
Guido van Rossum778983b1993-02-19 15:55:02 +0000384/* Description of types */
Guido van Rossum234f9421993-06-17 12:35:49 +0000385static struct arraydescr descriptors[] = {
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +0000386 {'b', 1, b_getitem, b_setitem, "b"},
387 {'B', 1, BB_getitem, BB_setitem, "B"},
Travis E. Oliphantddacf962007-10-13 21:03:27 +0000388 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem, "u"},
Travis E. Oliphantb803c512007-08-20 07:16:33 +0000389 {'h', sizeof(short), h_getitem, h_setitem, "h"},
390 {'H', sizeof(short), HH_getitem, HH_setitem, "H"},
391 {'i', sizeof(int), i_getitem, i_setitem, "i"},
392 {'I', sizeof(int), II_getitem, II_setitem, "I"},
393 {'l', sizeof(long), l_getitem, l_setitem, "l"},
394 {'L', sizeof(long), LL_getitem, LL_setitem, "L"},
395 {'f', sizeof(float), f_getitem, f_setitem, "f"},
396 {'d', sizeof(double), d_getitem, d_setitem, "d"},
397 {'\0', 0, 0, 0, 0} /* Sentinel */
Guido van Rossum778983b1993-02-19 15:55:02 +0000398};
Tim Petersbb307342000-09-10 05:22:54 +0000399
400/****************************************************************************
401Implementations of array object methods.
402****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +0000403
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000404static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000405newarrayobject(PyTypeObject *type, Py_ssize_t size, struct arraydescr *descr)
Guido van Rossum778983b1993-02-19 15:55:02 +0000406{
Guido van Rossum778983b1993-02-19 15:55:02 +0000407 arrayobject *op;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000408 size_t nbytes;
Martin v. Löwis99866332002-03-01 10:27:01 +0000409
Guido van Rossum778983b1993-02-19 15:55:02 +0000410 if (size < 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000411 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000412 return NULL;
413 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000414
Guido van Rossum778983b1993-02-19 15:55:02 +0000415 nbytes = size * descr->itemsize;
416 /* Check for overflow */
Guido van Rossum7844e381997-04-11 20:44:04 +0000417 if (nbytes / descr->itemsize != (size_t)size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000418 return PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000419 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000420 op = (arrayobject *) type->tp_alloc(type, 0);
Guido van Rossum778983b1993-02-19 15:55:02 +0000421 if (op == NULL) {
Martin v. Löwis99866332002-03-01 10:27:01 +0000422 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +0000423 }
Christian Heimes90aa7642007-12-19 02:45:37 +0000424 Py_SIZE(op) = size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000425 if (size <= 0) {
426 op->ob_item = NULL;
427 }
428 else {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000429 op->ob_item = PyMem_NEW(char, nbytes);
Guido van Rossum778983b1993-02-19 15:55:02 +0000430 if (op->ob_item == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000431 PyObject_Del(op);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000432 return PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000433 }
434 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000435 op->ob_descr = descr;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000436 op->allocated = size;
Raymond Hettingercb87bc82004-05-31 00:35:52 +0000437 op->weakreflist = NULL;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000438 op->ob_exports = 0;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000439 return (PyObject *) op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000440}
441
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000442static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000443getarrayitem(PyObject *op, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000444{
445 register arrayobject *ap;
Martin v. Löwis99866332002-03-01 10:27:01 +0000446 assert(array_Check(op));
Guido van Rossum778983b1993-02-19 15:55:02 +0000447 ap = (arrayobject *)op;
Christian Heimes90aa7642007-12-19 02:45:37 +0000448 assert(i>=0 && i<Py_SIZE(ap));
Guido van Rossum778983b1993-02-19 15:55:02 +0000449 return (*ap->ob_descr->getitem)(ap, i);
450}
451
452static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000453ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000454{
Guido van Rossum778983b1993-02-19 15:55:02 +0000455 char *items;
Christian Heimes90aa7642007-12-19 02:45:37 +0000456 Py_ssize_t n = Py_SIZE(self);
Guido van Rossum778983b1993-02-19 15:55:02 +0000457 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000458 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000459 return -1;
460 }
461 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
462 return -1;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000463
464 if (array_resize(self, n+1) == -1)
Guido van Rossum778983b1993-02-19 15:55:02 +0000465 return -1;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000466 items = self->ob_item;
Walter Dörwald9e46abe2003-05-18 03:15:10 +0000467 if (where < 0) {
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000468 where += n;
Walter Dörwald9e46abe2003-05-18 03:15:10 +0000469 if (where < 0)
470 where = 0;
471 }
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000472 if (where > n)
473 where = n;
474 /* appends don't need to call memmove() */
475 if (where != n)
476 memmove(items + (where+1)*self->ob_descr->itemsize,
477 items + where*self->ob_descr->itemsize,
478 (n-where)*self->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000479 return (*self->ob_descr->setitem)(self, where, v);
480}
481
Guido van Rossum778983b1993-02-19 15:55:02 +0000482/* Methods */
483
484static void
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000485array_dealloc(arrayobject *op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000486{
Raymond Hettingercb87bc82004-05-31 00:35:52 +0000487 if (op->weakreflist != NULL)
488 PyObject_ClearWeakRefs((PyObject *) op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000489 if (op->ob_item != NULL)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000490 PyMem_DEL(op->ob_item);
Christian Heimes90aa7642007-12-19 02:45:37 +0000491 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000492}
493
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000494static PyObject *
495array_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000496{
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000497 arrayobject *va, *wa;
498 PyObject *vi = NULL;
499 PyObject *wi = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000500 Py_ssize_t i, k;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000501 PyObject *res;
502
Martin v. Löwis99866332002-03-01 10:27:01 +0000503 if (!array_Check(v) || !array_Check(w)) {
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000504 Py_INCREF(Py_NotImplemented);
505 return Py_NotImplemented;
Guido van Rossum778983b1993-02-19 15:55:02 +0000506 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000507
508 va = (arrayobject *)v;
509 wa = (arrayobject *)w;
510
Christian Heimes90aa7642007-12-19 02:45:37 +0000511 if (Py_SIZE(va) != Py_SIZE(wa) && (op == Py_EQ || op == Py_NE)) {
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000512 /* Shortcut: if the lengths differ, the arrays differ */
513 if (op == Py_EQ)
514 res = Py_False;
515 else
516 res = Py_True;
517 Py_INCREF(res);
518 return res;
519 }
520
521 /* Search for the first index where items are different */
522 k = 1;
Christian Heimes90aa7642007-12-19 02:45:37 +0000523 for (i = 0; i < Py_SIZE(va) && i < Py_SIZE(wa); i++) {
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000524 vi = getarrayitem(v, i);
525 wi = getarrayitem(w, i);
526 if (vi == NULL || wi == NULL) {
527 Py_XDECREF(vi);
528 Py_XDECREF(wi);
529 return NULL;
530 }
531 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
532 if (k == 0)
533 break; /* Keeping vi and wi alive! */
534 Py_DECREF(vi);
535 Py_DECREF(wi);
536 if (k < 0)
537 return NULL;
538 }
539
540 if (k) {
541 /* No more items to compare -- compare sizes */
Christian Heimes90aa7642007-12-19 02:45:37 +0000542 Py_ssize_t vs = Py_SIZE(va);
543 Py_ssize_t ws = Py_SIZE(wa);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000544 int cmp;
545 switch (op) {
546 case Py_LT: cmp = vs < ws; break;
Guido van Rossum2b597e42001-01-25 22:12:43 +0000547 case Py_LE: cmp = vs <= ws; break;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000548 case Py_EQ: cmp = vs == ws; break;
549 case Py_NE: cmp = vs != ws; break;
550 case Py_GT: cmp = vs > ws; break;
551 case Py_GE: cmp = vs >= ws; break;
552 default: return NULL; /* cannot happen */
553 }
554 if (cmp)
555 res = Py_True;
556 else
557 res = Py_False;
558 Py_INCREF(res);
559 return res;
560 }
561
562 /* We have an item that differs. First, shortcuts for EQ/NE */
563 if (op == Py_EQ) {
564 Py_INCREF(Py_False);
565 res = Py_False;
566 }
567 else if (op == Py_NE) {
568 Py_INCREF(Py_True);
569 res = Py_True;
570 }
571 else {
572 /* Compare the final item again using the proper operator */
573 res = PyObject_RichCompare(vi, wi, op);
574 }
575 Py_DECREF(vi);
576 Py_DECREF(wi);
577 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +0000578}
579
Martin v. Löwis18e16552006-02-15 17:27:45 +0000580static Py_ssize_t
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000581array_length(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +0000582{
Christian Heimes90aa7642007-12-19 02:45:37 +0000583 return Py_SIZE(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000584}
585
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000586static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000587array_item(arrayobject *a, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000588{
Christian Heimes90aa7642007-12-19 02:45:37 +0000589 if (i < 0 || i >= Py_SIZE(a)) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000590 PyErr_SetString(PyExc_IndexError, "array index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000591 return NULL;
592 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000593 return getarrayitem((PyObject *)a, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000594}
595
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000596static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000597array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000598{
599 arrayobject *np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000600 if (ilow < 0)
601 ilow = 0;
Christian Heimes90aa7642007-12-19 02:45:37 +0000602 else if (ilow > Py_SIZE(a))
603 ilow = Py_SIZE(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000604 if (ihigh < 0)
605 ihigh = 0;
606 if (ihigh < ilow)
607 ihigh = ilow;
Christian Heimes90aa7642007-12-19 02:45:37 +0000608 else if (ihigh > Py_SIZE(a))
609 ihigh = Py_SIZE(a);
Martin v. Löwis99866332002-03-01 10:27:01 +0000610 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
Guido van Rossum778983b1993-02-19 15:55:02 +0000611 if (np == NULL)
612 return NULL;
613 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
614 (ihigh-ilow) * a->ob_descr->itemsize);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000615 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000616}
617
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000618static PyObject *
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000619array_copy(arrayobject *a, PyObject *unused)
620{
Christian Heimes90aa7642007-12-19 02:45:37 +0000621 return array_slice(a, 0, Py_SIZE(a));
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000622}
623
624PyDoc_STRVAR(copy_doc,
625"copy(array)\n\
626\n\
627 Return a copy of the array.");
628
629static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000630array_concat(arrayobject *a, PyObject *bb)
Guido van Rossum778983b1993-02-19 15:55:02 +0000631{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000632 Py_ssize_t size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000633 arrayobject *np;
Martin v. Löwis99866332002-03-01 10:27:01 +0000634 if (!array_Check(bb)) {
Fred Drake137507e2000-06-01 02:02:46 +0000635 PyErr_Format(PyExc_TypeError,
636 "can only append array (not \"%.200s\") to array",
Christian Heimes90aa7642007-12-19 02:45:37 +0000637 Py_TYPE(bb)->tp_name);
Guido van Rossum778983b1993-02-19 15:55:02 +0000638 return NULL;
639 }
640#define b ((arrayobject *)bb)
641 if (a->ob_descr != b->ob_descr) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000642 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000643 return NULL;
644 }
Christian Heimes90aa7642007-12-19 02:45:37 +0000645 size = Py_SIZE(a) + Py_SIZE(b);
Martin v. Löwis99866332002-03-01 10:27:01 +0000646 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
Guido van Rossum778983b1993-02-19 15:55:02 +0000647 if (np == NULL) {
648 return NULL;
649 }
Christian Heimes90aa7642007-12-19 02:45:37 +0000650 memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize);
651 memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize,
652 b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000653 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000654#undef b
655}
656
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000657static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000658array_repeat(arrayobject *a, Py_ssize_t n)
Guido van Rossum778983b1993-02-19 15:55:02 +0000659{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000660 Py_ssize_t i;
661 Py_ssize_t size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000662 arrayobject *np;
663 char *p;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000664 Py_ssize_t nbytes;
Guido van Rossum778983b1993-02-19 15:55:02 +0000665 if (n < 0)
666 n = 0;
Christian Heimes90aa7642007-12-19 02:45:37 +0000667 size = Py_SIZE(a) * n;
Martin v. Löwis99866332002-03-01 10:27:01 +0000668 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
Guido van Rossum778983b1993-02-19 15:55:02 +0000669 if (np == NULL)
670 return NULL;
671 p = np->ob_item;
Christian Heimes90aa7642007-12-19 02:45:37 +0000672 nbytes = Py_SIZE(a) * a->ob_descr->itemsize;
Guido van Rossum778983b1993-02-19 15:55:02 +0000673 for (i = 0; i < n; i++) {
674 memcpy(p, a->ob_item, nbytes);
675 p += nbytes;
676 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000677 return (PyObject *) np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000678}
679
680static int
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000681array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000682{
683 char *item;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000684 Py_ssize_t n; /* Size of replacement array */
685 Py_ssize_t d; /* Change in size */
Guido van Rossum778983b1993-02-19 15:55:02 +0000686#define b ((arrayobject *)v)
687 if (v == NULL)
688 n = 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000689 else if (array_Check(v)) {
Christian Heimes90aa7642007-12-19 02:45:37 +0000690 n = Py_SIZE(b);
Guido van Rossum778983b1993-02-19 15:55:02 +0000691 if (a == b) {
692 /* Special case "a[i:j] = a" -- copy b first */
693 int ret;
694 v = array_slice(b, 0, n);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000695 if (!v)
696 return -1;
Guido van Rossum778983b1993-02-19 15:55:02 +0000697 ret = array_ass_slice(a, ilow, ihigh, v);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000698 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000699 return ret;
700 }
701 if (b->ob_descr != a->ob_descr) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000702 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000703 return -1;
704 }
705 }
706 else {
Fred Drake137507e2000-06-01 02:02:46 +0000707 PyErr_Format(PyExc_TypeError,
708 "can only assign array (not \"%.200s\") to array slice",
Christian Heimes90aa7642007-12-19 02:45:37 +0000709 Py_TYPE(v)->tp_name);
Guido van Rossum778983b1993-02-19 15:55:02 +0000710 return -1;
711 }
712 if (ilow < 0)
713 ilow = 0;
Christian Heimes90aa7642007-12-19 02:45:37 +0000714 else if (ilow > Py_SIZE(a))
715 ilow = Py_SIZE(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000716 if (ihigh < 0)
717 ihigh = 0;
718 if (ihigh < ilow)
719 ihigh = ilow;
Christian Heimes90aa7642007-12-19 02:45:37 +0000720 else if (ihigh > Py_SIZE(a))
721 ihigh = Py_SIZE(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000722 item = a->ob_item;
723 d = n - (ihigh-ilow);
724 if (d < 0) { /* Delete -d items */
725 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
726 item + ihigh*a->ob_descr->itemsize,
Christian Heimes90aa7642007-12-19 02:45:37 +0000727 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
728 Py_SIZE(a) += d;
729 PyMem_RESIZE(item, char, Py_SIZE(a)*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000730 /* Can't fail */
731 a->ob_item = item;
Christian Heimes90aa7642007-12-19 02:45:37 +0000732 a->allocated = Py_SIZE(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000733 }
734 else if (d > 0) { /* Insert d items */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000735 PyMem_RESIZE(item, char,
Christian Heimes90aa7642007-12-19 02:45:37 +0000736 (Py_SIZE(a) + d)*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000737 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000738 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000739 return -1;
740 }
741 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
742 item + ihigh*a->ob_descr->itemsize,
Christian Heimes90aa7642007-12-19 02:45:37 +0000743 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000744 a->ob_item = item;
Christian Heimes90aa7642007-12-19 02:45:37 +0000745 Py_SIZE(a) += d;
746 a->allocated = Py_SIZE(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000747 }
748 if (n > 0)
749 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
750 n*b->ob_descr->itemsize);
751 return 0;
752#undef b
753}
754
755static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000756array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000757{
Christian Heimes90aa7642007-12-19 02:45:37 +0000758 if (i < 0 || i >= Py_SIZE(a)) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000759 PyErr_SetString(PyExc_IndexError,
760 "array assignment index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000761 return -1;
762 }
763 if (v == NULL)
764 return array_ass_slice(a, i, i+1, v);
765 return (*a->ob_descr->setitem)(a, i, v);
766}
767
768static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000769setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000770{
Martin v. Löwis99866332002-03-01 10:27:01 +0000771 assert(array_Check(a));
Guido van Rossum778983b1993-02-19 15:55:02 +0000772 return array_ass_item((arrayobject *)a, i, v);
773}
774
Martin v. Löwis99866332002-03-01 10:27:01 +0000775static int
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000776array_iter_extend(arrayobject *self, PyObject *bb)
777{
778 PyObject *it, *v;
779
780 it = PyObject_GetIter(bb);
781 if (it == NULL)
782 return -1;
783
784 while ((v = PyIter_Next(it)) != NULL) {
Christian Heimes90aa7642007-12-19 02:45:37 +0000785 if (ins1(self, (int) Py_SIZE(self), v) != 0) {
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000786 Py_DECREF(v);
787 Py_DECREF(it);
788 return -1;
789 }
790 Py_DECREF(v);
791 }
792 Py_DECREF(it);
793 if (PyErr_Occurred())
794 return -1;
795 return 0;
796}
797
798static int
Martin v. Löwis99866332002-03-01 10:27:01 +0000799array_do_extend(arrayobject *self, PyObject *bb)
800{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000801 Py_ssize_t size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000802
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000803 if (!array_Check(bb))
804 return array_iter_extend(self, bb);
Martin v. Löwis99866332002-03-01 10:27:01 +0000805#define b ((arrayobject *)bb)
806 if (self->ob_descr != b->ob_descr) {
807 PyErr_SetString(PyExc_TypeError,
808 "can only extend with array of same kind");
809 return -1;
810 }
Christian Heimes90aa7642007-12-19 02:45:37 +0000811 size = Py_SIZE(self) + Py_SIZE(b);
Martin v. Löwis99866332002-03-01 10:27:01 +0000812 PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize);
813 if (self->ob_item == NULL) {
814 PyObject_Del(self);
815 PyErr_NoMemory();
816 return -1;
817 }
Christian Heimes90aa7642007-12-19 02:45:37 +0000818 memcpy(self->ob_item + Py_SIZE(self)*self->ob_descr->itemsize,
819 b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
820 Py_SIZE(self) = size;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000821 self->allocated = size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000822
823 return 0;
824#undef b
825}
826
827static PyObject *
828array_inplace_concat(arrayobject *self, PyObject *bb)
829{
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000830 if (!array_Check(bb)) {
831 PyErr_Format(PyExc_TypeError,
832 "can only extend array with array (not \"%.200s\")",
Christian Heimes90aa7642007-12-19 02:45:37 +0000833 Py_TYPE(bb)->tp_name);
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000834 return NULL;
835 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000836 if (array_do_extend(self, bb) == -1)
837 return NULL;
838 Py_INCREF(self);
839 return (PyObject *)self;
840}
841
842static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000843array_inplace_repeat(arrayobject *self, Py_ssize_t n)
Martin v. Löwis99866332002-03-01 10:27:01 +0000844{
845 char *items, *p;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000846 Py_ssize_t size, i;
Martin v. Löwis99866332002-03-01 10:27:01 +0000847
Christian Heimes90aa7642007-12-19 02:45:37 +0000848 if (Py_SIZE(self) > 0) {
Martin v. Löwis99866332002-03-01 10:27:01 +0000849 if (n < 0)
850 n = 0;
851 items = self->ob_item;
Christian Heimes90aa7642007-12-19 02:45:37 +0000852 size = Py_SIZE(self) * self->ob_descr->itemsize;
Martin v. Löwis99866332002-03-01 10:27:01 +0000853 if (n == 0) {
854 PyMem_FREE(items);
855 self->ob_item = NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +0000856 Py_SIZE(self) = 0;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000857 self->allocated = 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000858 }
859 else {
860 PyMem_Resize(items, char, n * size);
861 if (items == NULL)
862 return PyErr_NoMemory();
863 p = items;
864 for (i = 1; i < n; i++) {
865 p += size;
866 memcpy(p, items, size);
867 }
868 self->ob_item = items;
Christian Heimes90aa7642007-12-19 02:45:37 +0000869 Py_SIZE(self) *= n;
870 self->allocated = Py_SIZE(self);
Martin v. Löwis99866332002-03-01 10:27:01 +0000871 }
872 }
873 Py_INCREF(self);
874 return (PyObject *)self;
875}
876
877
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000878static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000879ins(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000880{
881 if (ins1(self, where, v) != 0)
882 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000883 Py_INCREF(Py_None);
884 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000885}
886
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000887static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000888array_count(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000889{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000890 Py_ssize_t count = 0;
891 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000892
Christian Heimes90aa7642007-12-19 02:45:37 +0000893 for (i = 0; i < Py_SIZE(self); i++) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000894 PyObject *selfi = getarrayitem((PyObject *)self, i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000895 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000896 Py_DECREF(selfi);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000897 if (cmp > 0)
898 count++;
899 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000900 return NULL;
901 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000902 return PyLong_FromSsize_t(count);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000903}
904
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000905PyDoc_STRVAR(count_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000906"count(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000907\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000908Return number of occurences of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000909
910static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000911array_index(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000912{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000913 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000914
Christian Heimes90aa7642007-12-19 02:45:37 +0000915 for (i = 0; i < Py_SIZE(self); i++) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000916 PyObject *selfi = getarrayitem((PyObject *)self, i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000917 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
918 Py_DECREF(selfi);
919 if (cmp > 0) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000920 return PyLong_FromLong((long)i);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000921 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000922 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000923 return NULL;
924 }
925 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
926 return NULL;
927}
928
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000929PyDoc_STRVAR(index_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000930"index(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000931\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000932Return index of first occurence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000933
Raymond Hettinger625812f2003-01-07 01:58:52 +0000934static int
935array_contains(arrayobject *self, PyObject *v)
936{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000937 Py_ssize_t i;
938 int cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +0000939
Christian Heimes90aa7642007-12-19 02:45:37 +0000940 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) {
Raymond Hettinger625812f2003-01-07 01:58:52 +0000941 PyObject *selfi = getarrayitem((PyObject *)self, i);
942 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
943 Py_DECREF(selfi);
944 }
945 return cmp;
946}
947
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000948static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000949array_remove(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000950{
951 int i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000952
Christian Heimes90aa7642007-12-19 02:45:37 +0000953 for (i = 0; i < Py_SIZE(self); i++) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000954 PyObject *selfi = getarrayitem((PyObject *)self,i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000955 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
956 Py_DECREF(selfi);
957 if (cmp > 0) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000958 if (array_ass_slice(self, i, i+1,
959 (PyObject *)NULL) != 0)
960 return NULL;
961 Py_INCREF(Py_None);
962 return Py_None;
963 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000964 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000965 return NULL;
966 }
967 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
968 return NULL;
969}
970
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000971PyDoc_STRVAR(remove_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000972"remove(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000973\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000974Remove the first occurence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000975
976static PyObject *
977array_pop(arrayobject *self, PyObject *args)
978{
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000979 Py_ssize_t i = -1;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000980 PyObject *v;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000981 if (!PyArg_ParseTuple(args, "|n:pop", &i))
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000982 return NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +0000983 if (Py_SIZE(self) == 0) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000984 /* Special-case most common failure cause */
985 PyErr_SetString(PyExc_IndexError, "pop from empty array");
986 return NULL;
987 }
988 if (i < 0)
Christian Heimes90aa7642007-12-19 02:45:37 +0000989 i += Py_SIZE(self);
990 if (i < 0 || i >= Py_SIZE(self)) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000991 PyErr_SetString(PyExc_IndexError, "pop index out of range");
992 return NULL;
993 }
994 v = getarrayitem((PyObject *)self,i);
995 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
996 Py_DECREF(v);
997 return NULL;
998 }
999 return v;
1000}
1001
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001002PyDoc_STRVAR(pop_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001003"pop([i])\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001004\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001005Return the i-th element and delete it from the array. i defaults to -1.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001006
1007static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001008array_extend(arrayobject *self, PyObject *bb)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001009{
Martin v. Löwis99866332002-03-01 10:27:01 +00001010 if (array_do_extend(self, bb) == -1)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001011 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001012 Py_INCREF(Py_None);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001013 return Py_None;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001014}
1015
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001016PyDoc_STRVAR(extend_doc,
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001017"extend(array or iterable)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001018\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001019 Append items to the end of the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001020
1021static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001022array_insert(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001023{
Thomas Woutersb9eb5512006-02-27 19:44:56 +00001024 Py_ssize_t i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001025 PyObject *v;
Thomas Woutersb9eb5512006-02-27 19:44:56 +00001026 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
Guido van Rossum778983b1993-02-19 15:55:02 +00001027 return NULL;
1028 return ins(self, i, v);
1029}
1030
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001031PyDoc_STRVAR(insert_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001032"insert(i,x)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001033\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001034Insert a new item x into the array before position i.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001035
1036
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001037static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001038array_buffer_info(arrayobject *self, PyObject *unused)
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001039{
Tim Peters077a11d2000-09-16 22:31:29 +00001040 PyObject* retval = NULL;
Tim Peters077a11d2000-09-16 22:31:29 +00001041 retval = PyTuple_New(2);
1042 if (!retval)
1043 return NULL;
Fred Drake541dc3b2000-06-28 17:49:30 +00001044
1045 PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item));
Christian Heimes90aa7642007-12-19 02:45:37 +00001046 PyTuple_SET_ITEM(retval, 1, PyLong_FromLong((long)(Py_SIZE(self))));
Fred Drake541dc3b2000-06-28 17:49:30 +00001047
1048 return retval;
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001049}
1050
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001051PyDoc_STRVAR(buffer_info_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001052"buffer_info() -> (address, length)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001053\n\
1054Return a tuple (address, length) giving the current memory address and\n\
Guido van Rossum702d08e2001-07-27 16:05:32 +00001055the length in items of the buffer used to hold array's contents\n\
1056The length should be multiplied by the itemsize attribute to calculate\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001057the buffer length in bytes.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001058
1059
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001060static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001061array_append(arrayobject *self, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +00001062{
Christian Heimes90aa7642007-12-19 02:45:37 +00001063 return ins(self, (int) Py_SIZE(self), v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001064}
1065
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001066PyDoc_STRVAR(append_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001067"append(x)\n\
1068\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001069Append new value x to the end of the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001070
1071
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001072static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001073array_byteswap(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001074{
1075 char *p;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001076 Py_ssize_t i;
Fred Drakebf272981999-12-03 17:15:30 +00001077
Guido van Rossum778983b1993-02-19 15:55:02 +00001078 switch (self->ob_descr->itemsize) {
1079 case 1:
1080 break;
1081 case 2:
Christian Heimes90aa7642007-12-19 02:45:37 +00001082 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) {
Guido van Rossum778983b1993-02-19 15:55:02 +00001083 char p0 = p[0];
1084 p[0] = p[1];
1085 p[1] = p0;
1086 }
1087 break;
1088 case 4:
Christian Heimes90aa7642007-12-19 02:45:37 +00001089 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 4) {
Guido van Rossum778983b1993-02-19 15:55:02 +00001090 char p0 = p[0];
1091 char p1 = p[1];
1092 p[0] = p[3];
1093 p[1] = p[2];
1094 p[2] = p1;
1095 p[3] = p0;
1096 }
1097 break;
Guido van Rossume77a7571993-11-03 15:01:26 +00001098 case 8:
Christian Heimes90aa7642007-12-19 02:45:37 +00001099 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) {
Guido van Rossume77a7571993-11-03 15:01:26 +00001100 char p0 = p[0];
1101 char p1 = p[1];
1102 char p2 = p[2];
1103 char p3 = p[3];
1104 p[0] = p[7];
1105 p[1] = p[6];
1106 p[2] = p[5];
1107 p[3] = p[4];
1108 p[4] = p3;
1109 p[5] = p2;
1110 p[6] = p1;
1111 p[7] = p0;
1112 }
1113 break;
Guido van Rossum778983b1993-02-19 15:55:02 +00001114 default:
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001115 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossum778983b1993-02-19 15:55:02 +00001116 "don't know how to byteswap this array type");
1117 return NULL;
1118 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001119 Py_INCREF(Py_None);
1120 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001121}
1122
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001123PyDoc_STRVAR(byteswap_doc,
Fred Drakebf272981999-12-03 17:15:30 +00001124"byteswap()\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001125\n\
Fred Drakebf272981999-12-03 17:15:30 +00001126Byteswap 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 +000011274, or 8 bytes in size, RuntimeError is raised.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001128
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001129static PyObject *
Raymond Hettingerb0900e62004-12-16 16:23:40 +00001130array_reduce(arrayobject *array)
1131{
1132 PyObject *dict, *result;
1133
1134 dict = PyObject_GetAttrString((PyObject *)array, "__dict__");
1135 if (dict == NULL) {
1136 PyErr_Clear();
1137 dict = Py_None;
1138 Py_INCREF(dict);
1139 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001140 if (Py_SIZE(array) > 0) {
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001141 result = Py_BuildValue("O(cy#)O",
Christian Heimes90aa7642007-12-19 02:45:37 +00001142 Py_TYPE(array),
Guido van Rossumd8faa362007-04-27 19:54:29 +00001143 array->ob_descr->typecode,
1144 array->ob_item,
Christian Heimes90aa7642007-12-19 02:45:37 +00001145 Py_SIZE(array) * array->ob_descr->itemsize,
Guido van Rossumd8faa362007-04-27 19:54:29 +00001146 dict);
1147 } else {
1148 result = Py_BuildValue("O(c)O",
Christian Heimes90aa7642007-12-19 02:45:37 +00001149 Py_TYPE(array),
Guido van Rossumd8faa362007-04-27 19:54:29 +00001150 array->ob_descr->typecode,
1151 dict);
1152 }
Raymond Hettingerb0900e62004-12-16 16:23:40 +00001153 Py_DECREF(dict);
1154 return result;
1155}
1156
1157PyDoc_STRVAR(array_doc, "Return state information for pickling.");
1158
1159static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001160array_reverse(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001161{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001162 register Py_ssize_t itemsize = self->ob_descr->itemsize;
Guido van Rossume77a7571993-11-03 15:01:26 +00001163 register char *p, *q;
Tim Peters077a11d2000-09-16 22:31:29 +00001164 /* little buffer to hold items while swapping */
1165 char tmp[256]; /* 8 is probably enough -- but why skimp */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001166 assert((size_t)itemsize <= sizeof(tmp));
Guido van Rossume77a7571993-11-03 15:01:26 +00001167
Christian Heimes90aa7642007-12-19 02:45:37 +00001168 if (Py_SIZE(self) > 1) {
Guido van Rossume77a7571993-11-03 15:01:26 +00001169 for (p = self->ob_item,
Christian Heimes90aa7642007-12-19 02:45:37 +00001170 q = self->ob_item + (Py_SIZE(self) - 1)*itemsize;
Guido van Rossume77a7571993-11-03 15:01:26 +00001171 p < q;
1172 p += itemsize, q -= itemsize) {
Tim Peters077a11d2000-09-16 22:31:29 +00001173 /* memory areas guaranteed disjoint, so memcpy
1174 * is safe (& memmove may be slower).
1175 */
1176 memcpy(tmp, p, itemsize);
1177 memcpy(p, q, itemsize);
1178 memcpy(q, tmp, itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001179 }
1180 }
Tim Petersbb307342000-09-10 05:22:54 +00001181
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001182 Py_INCREF(Py_None);
1183 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001184}
Guido van Rossume77a7571993-11-03 15:01:26 +00001185
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001186PyDoc_STRVAR(reverse_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001187"reverse()\n\
1188\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001189Reverse the order of the items in the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001190
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001191
1192/* Forward */
1193static PyObject *array_fromstring(arrayobject *self, PyObject *args);
1194
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001195static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001196array_fromfile(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001197{
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001198 PyObject *f, *b, *res;
1199 Py_ssize_t itemsize = self->ob_descr->itemsize;
1200 Py_ssize_t n, nbytes;
1201
Thomas Wouters7a2f83b2006-02-16 17:07:41 +00001202 if (!PyArg_ParseTuple(args, "On:fromfile", &f, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +00001203 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001204
1205 nbytes = n * itemsize;
1206 if (nbytes < 0 || nbytes/itemsize != n) {
1207 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001208 return NULL;
1209 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001210
1211 b = PyObject_CallMethod(f, "read", "n", nbytes);
1212 if (b == NULL)
1213 return NULL;
1214
Guido van Rossum98297ee2007-11-06 21:34:58 +00001215 if (!PyString_Check(b)) {
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001216 PyErr_SetString(PyExc_TypeError,
1217 "read() didn't return bytes");
1218 Py_DECREF(b);
1219 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001220 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001221
Guido van Rossum98297ee2007-11-06 21:34:58 +00001222 if (PyString_GET_SIZE(b) != nbytes) {
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001223 PyErr_SetString(PyExc_EOFError,
1224 "read() didn't return enough bytes");
1225 Py_DECREF(b);
1226 return NULL;
1227 }
1228
1229 args = Py_BuildValue("(O)", b);
1230 Py_DECREF(b);
1231 if (args == NULL)
1232 return NULL;
1233
1234 res = array_fromstring(self, args);
1235 Py_DECREF(args);
1236
1237 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +00001238}
1239
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001240PyDoc_STRVAR(fromfile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001241"fromfile(f, n)\n\
1242\n\
1243Read n objects from the file object f and append them to the end of the\n\
Georg Brandlf25ef502008-02-01 11:30:18 +00001244array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001245
1246
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001247static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001248array_tofile(arrayobject *self, PyObject *f)
Guido van Rossum778983b1993-02-19 15:55:02 +00001249{
Christian Heimes90aa7642007-12-19 02:45:37 +00001250 Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001251 /* Write 64K blocks at a time */
1252 /* XXX Make the block size settable */
1253 int BLOCKSIZE = 64*1024;
1254 Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE;
1255 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001256
Christian Heimes90aa7642007-12-19 02:45:37 +00001257 if (Py_SIZE(self) == 0)
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001258 goto done;
1259
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001260 for (i = 0; i < nblocks; i++) {
1261 char* ptr = self->ob_item + i*BLOCKSIZE;
1262 Py_ssize_t size = BLOCKSIZE;
1263 PyObject *bytes, *res;
1264 if (i*BLOCKSIZE + size > nbytes)
1265 size = nbytes - i*BLOCKSIZE;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001266 bytes = PyString_FromStringAndSize(ptr, size);
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001267 if (bytes == NULL)
Guido van Rossum778983b1993-02-19 15:55:02 +00001268 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001269 res = PyObject_CallMethod(f, "write", "O", bytes);
1270 Py_DECREF(bytes);
1271 if (res == NULL)
1272 return NULL;
Martin v. Löwisa291c8f2007-08-11 14:25:27 +00001273 Py_DECREF(res); /* drop write result */
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001274 }
1275
1276 done:
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001277 Py_INCREF(Py_None);
1278 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001279}
1280
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001281PyDoc_STRVAR(tofile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001282"tofile(f)\n\
1283\n\
Georg Brandlf25ef502008-02-01 11:30:18 +00001284Write all items (as machine values) to the file object f.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001285
1286
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001287static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001288array_fromlist(arrayobject *self, PyObject *list)
Guido van Rossum778983b1993-02-19 15:55:02 +00001289{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001290 Py_ssize_t n;
1291 Py_ssize_t itemsize = self->ob_descr->itemsize;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001292
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001293 if (!PyList_Check(list)) {
1294 PyErr_SetString(PyExc_TypeError, "arg must be list");
Guido van Rossum778983b1993-02-19 15:55:02 +00001295 return NULL;
1296 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001297 n = PyList_Size(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001298 if (n > 0) {
1299 char *item = self->ob_item;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001300 Py_ssize_t i;
Christian Heimes90aa7642007-12-19 02:45:37 +00001301 PyMem_RESIZE(item, char, (Py_SIZE(self) + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001302 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001303 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001304 return NULL;
1305 }
1306 self->ob_item = item;
Christian Heimes90aa7642007-12-19 02:45:37 +00001307 Py_SIZE(self) += n;
1308 self->allocated = Py_SIZE(self);
Guido van Rossum778983b1993-02-19 15:55:02 +00001309 for (i = 0; i < n; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001310 PyObject *v = PyList_GetItem(list, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001311 if ((*self->ob_descr->setitem)(self,
Christian Heimes90aa7642007-12-19 02:45:37 +00001312 Py_SIZE(self) - n + i, v) != 0) {
1313 Py_SIZE(self) -= n;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001314 PyMem_RESIZE(item, char,
Christian Heimes90aa7642007-12-19 02:45:37 +00001315 Py_SIZE(self) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001316 self->ob_item = item;
Christian Heimes90aa7642007-12-19 02:45:37 +00001317 self->allocated = Py_SIZE(self);
Guido van Rossum778983b1993-02-19 15:55:02 +00001318 return NULL;
1319 }
1320 }
1321 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001322 Py_INCREF(Py_None);
1323 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001324}
1325
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001326PyDoc_STRVAR(fromlist_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001327"fromlist(list)\n\
1328\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001329Append items to array from list.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001330
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001331static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001332array_tolist(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001333{
Christian Heimes90aa7642007-12-19 02:45:37 +00001334 PyObject *list = PyList_New(Py_SIZE(self));
Martin v. Löwis18e16552006-02-15 17:27:45 +00001335 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001336
Guido van Rossum778983b1993-02-19 15:55:02 +00001337 if (list == NULL)
1338 return NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +00001339 for (i = 0; i < Py_SIZE(self); i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001340 PyObject *v = getarrayitem((PyObject *)self, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001341 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001342 Py_DECREF(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001343 return NULL;
1344 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001345 PyList_SetItem(list, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001346 }
1347 return list;
1348}
1349
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001350PyDoc_STRVAR(tolist_doc,
Guido van Rossumfc6aba51998-10-14 02:52:31 +00001351"tolist() -> list\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001352\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001353Convert array to an ordinary list with the same items.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001354
1355
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001356static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001357array_fromstring(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001358{
1359 char *str;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001360 Py_ssize_t n;
Guido van Rossum778983b1993-02-19 15:55:02 +00001361 int itemsize = self->ob_descr->itemsize;
Peter Schneider-Kampdc71cac2000-07-31 21:57:30 +00001362 if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +00001363 return NULL;
1364 if (n % itemsize != 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001365 PyErr_SetString(PyExc_ValueError,
Guido van Rossum778983b1993-02-19 15:55:02 +00001366 "string length not a multiple of item size");
1367 return NULL;
1368 }
1369 n = n / itemsize;
1370 if (n > 0) {
1371 char *item = self->ob_item;
Christian Heimes90aa7642007-12-19 02:45:37 +00001372 PyMem_RESIZE(item, char, (Py_SIZE(self) + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001373 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001374 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001375 return NULL;
1376 }
1377 self->ob_item = item;
Christian Heimes90aa7642007-12-19 02:45:37 +00001378 Py_SIZE(self) += n;
1379 self->allocated = Py_SIZE(self);
1380 memcpy(item + (Py_SIZE(self) - n) * itemsize,
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001381 str, itemsize*n);
Guido van Rossum778983b1993-02-19 15:55:02 +00001382 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001383 Py_INCREF(Py_None);
1384 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001385}
1386
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001387PyDoc_STRVAR(fromstring_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001388"fromstring(string)\n\
1389\n\
1390Appends items from the string, interpreting it as an array of machine\n\
Walter Dörwald93b30b52007-06-22 12:21:53 +00001391values, as if it had been read from a file using the fromfile() method).");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001392
1393
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001394static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001395array_tostring(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001396{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001397 return PyString_FromStringAndSize(self->ob_item,
Christian Heimes90aa7642007-12-19 02:45:37 +00001398 Py_SIZE(self) * self->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001399}
1400
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001401PyDoc_STRVAR(tostring_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001402"tostring() -> string\n\
1403\n\
1404Convert the array to an array of machine values and return the string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001405representation.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001406
Martin v. Löwis99866332002-03-01 10:27:01 +00001407
1408
Martin v. Löwis99866332002-03-01 10:27:01 +00001409static PyObject *
1410array_fromunicode(arrayobject *self, PyObject *args)
1411{
1412 Py_UNICODE *ustr;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001413 Py_ssize_t n;
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00001414 char typecode;
Martin v. Löwis99866332002-03-01 10:27:01 +00001415
1416 if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
1417 return NULL;
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00001418 typecode = self->ob_descr->typecode;
1419 if ((typecode != 'u')) {
1420 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis99866332002-03-01 10:27:01 +00001421 "fromunicode() may only be called on "
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001422 "unicode type arrays");
Martin v. Löwis99866332002-03-01 10:27:01 +00001423 return NULL;
1424 }
1425 if (n > 0) {
1426 Py_UNICODE *item = (Py_UNICODE *) self->ob_item;
Christian Heimes90aa7642007-12-19 02:45:37 +00001427 PyMem_RESIZE(item, Py_UNICODE, Py_SIZE(self) + n);
Martin v. Löwis99866332002-03-01 10:27:01 +00001428 if (item == NULL) {
1429 PyErr_NoMemory();
1430 return NULL;
1431 }
1432 self->ob_item = (char *) item;
Christian Heimes90aa7642007-12-19 02:45:37 +00001433 Py_SIZE(self) += n;
1434 self->allocated = Py_SIZE(self);
1435 memcpy(item + Py_SIZE(self) - n,
Martin v. Löwis99866332002-03-01 10:27:01 +00001436 ustr, n * sizeof(Py_UNICODE));
1437 }
1438
1439 Py_INCREF(Py_None);
1440 return Py_None;
1441}
1442
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001443PyDoc_STRVAR(fromunicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001444"fromunicode(ustr)\n\
1445\n\
1446Extends this array with data from the unicode string ustr.\n\
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001447The array must be a unicode type array; otherwise a ValueError\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001448is raised. Use array.fromstring(ustr.decode(...)) to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001449append Unicode data to an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001450
1451
1452static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001453array_tounicode(arrayobject *self, PyObject *unused)
Martin v. Löwis99866332002-03-01 10:27:01 +00001454{
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00001455 char typecode;
1456 typecode = self->ob_descr->typecode;
1457 if ((typecode != 'u')) {
Martin v. Löwis99866332002-03-01 10:27:01 +00001458 PyErr_SetString(PyExc_ValueError,
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00001459 "tounicode() may only be called on unicode type arrays");
Martin v. Löwis99866332002-03-01 10:27:01 +00001460 return NULL;
1461 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001462 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_SIZE(self));
Martin v. Löwis99866332002-03-01 10:27:01 +00001463}
1464
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001465PyDoc_STRVAR(tounicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001466"tounicode() -> unicode\n\
1467\n\
1468Convert the array to a unicode string. The array must be\n\
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001469a unicode type array; otherwise a ValueError is raised. Use\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001470array.tostring().decode() to obtain a unicode string from\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001471an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001472
Martin v. Löwis99866332002-03-01 10:27:01 +00001473
1474
1475static PyObject *
1476array_get_typecode(arrayobject *a, void *closure)
1477{
1478 char tc = a->ob_descr->typecode;
Walter Dörwald93b30b52007-06-22 12:21:53 +00001479 return PyUnicode_FromStringAndSize(&tc, 1);
Martin v. Löwis99866332002-03-01 10:27:01 +00001480}
1481
1482static PyObject *
1483array_get_itemsize(arrayobject *a, void *closure)
1484{
Christian Heimes217cfd12007-12-02 14:31:20 +00001485 return PyLong_FromLong((long)a->ob_descr->itemsize);
Martin v. Löwis99866332002-03-01 10:27:01 +00001486}
1487
1488static PyGetSetDef array_getsets [] = {
1489 {"typecode", (getter) array_get_typecode, NULL,
1490 "the typecode character used to create the array"},
1491 {"itemsize", (getter) array_get_itemsize, NULL,
1492 "the size, in bytes, of one array item"},
1493 {NULL}
1494};
1495
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001496PyMethodDef array_methods[] = {
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001497 {"append", (PyCFunction)array_append, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001498 append_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001499 {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001500 buffer_info_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001501 {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001502 byteswap_doc},
Raymond Hettinger3aa82c02004-03-13 18:18:51 +00001503 {"__copy__", (PyCFunction)array_copy, METH_NOARGS,
1504 copy_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001505 {"count", (PyCFunction)array_count, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001506 count_doc},
Thomas Wouters89f507f2006-12-13 04:49:30 +00001507 {"__deepcopy__",(PyCFunction)array_copy, METH_O,
Raymond Hettinger3aa82c02004-03-13 18:18:51 +00001508 copy_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001509 {"extend", (PyCFunction)array_extend, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001510 extend_doc},
1511 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
1512 fromfile_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001513 {"fromlist", (PyCFunction)array_fromlist, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001514 fromlist_doc},
1515 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
1516 fromstring_doc},
Martin v. Löwis99866332002-03-01 10:27:01 +00001517 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
1518 fromunicode_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001519 {"index", (PyCFunction)array_index, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001520 index_doc},
1521 {"insert", (PyCFunction)array_insert, METH_VARARGS,
1522 insert_doc},
1523 {"pop", (PyCFunction)array_pop, METH_VARARGS,
1524 pop_doc},
Raymond Hettingerb0900e62004-12-16 16:23:40 +00001525 {"__reduce__", (PyCFunction)array_reduce, METH_NOARGS,
1526 array_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001527 {"remove", (PyCFunction)array_remove, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001528 remove_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001529 {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001530 reverse_doc},
1531/* {"sort", (PyCFunction)array_sort, METH_VARARGS,
1532 sort_doc},*/
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001533 {"tofile", (PyCFunction)array_tofile, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001534 tofile_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001535 {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001536 tolist_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001537 {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001538 tostring_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001539 {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
Martin v. Löwis99866332002-03-01 10:27:01 +00001540 tounicode_doc},
Guido van Rossum778983b1993-02-19 15:55:02 +00001541 {NULL, NULL} /* sentinel */
1542};
1543
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001544static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001545array_repr(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +00001546{
Walter Dörwaldc2f6a582007-05-20 12:49:47 +00001547 char typecode;
Walter Dörwald7569dfe2007-05-19 21:49:49 +00001548 PyObject *s, *v = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001549 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00001550
Christian Heimes90aa7642007-12-19 02:45:37 +00001551 len = Py_SIZE(a);
Martin v. Löwis99866332002-03-01 10:27:01 +00001552 typecode = a->ob_descr->typecode;
Guido van Rossum778983b1993-02-19 15:55:02 +00001553 if (len == 0) {
Walter Dörwald7569dfe2007-05-19 21:49:49 +00001554 return PyUnicode_FromFormat("array('%c')", typecode);
Guido van Rossum778983b1993-02-19 15:55:02 +00001555 }
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00001556 if ((typecode == 'u'))
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001557 v = array_tounicode(a, NULL);
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001558 else
1559 v = array_tolist(a, NULL);
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001560
Walter Dörwald7569dfe2007-05-19 21:49:49 +00001561 s = PyUnicode_FromFormat("array('%c', %R)", typecode, v);
1562 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001563 return s;
1564}
1565
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001566static PyObject*
1567array_subscr(arrayobject* self, PyObject* item)
1568{
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001569 if (PyIndex_Check(item)) {
1570 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001571 if (i==-1 && PyErr_Occurred()) {
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001572 return NULL;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001573 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001574 if (i < 0)
Christian Heimes90aa7642007-12-19 02:45:37 +00001575 i += Py_SIZE(self);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001576 return array_item(self, i);
1577 }
1578 else if (PySlice_Check(item)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001579 Py_ssize_t start, stop, step, slicelength, cur, i;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001580 PyObject* result;
1581 arrayobject* ar;
1582 int itemsize = self->ob_descr->itemsize;
1583
Christian Heimes90aa7642007-12-19 02:45:37 +00001584 if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self),
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001585 &start, &stop, &step, &slicelength) < 0) {
1586 return NULL;
1587 }
1588
1589 if (slicelength <= 0) {
1590 return newarrayobject(&Arraytype, 0, self->ob_descr);
1591 }
Thomas Woutersed03b412007-08-28 21:37:11 +00001592 else if (step == 1) {
1593 PyObject *result = newarrayobject(&Arraytype,
1594 slicelength, self->ob_descr);
1595 if (result == NULL)
1596 return NULL;
1597 memcpy(((arrayobject *)result)->ob_item,
1598 self->ob_item + start * itemsize,
1599 slicelength * itemsize);
1600 return result;
1601 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001602 else {
1603 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
1604 if (!result) return NULL;
1605
1606 ar = (arrayobject*)result;
1607
1608 for (cur = start, i = 0; i < slicelength;
1609 cur += step, i++) {
1610 memcpy(ar->ob_item + i*itemsize,
1611 self->ob_item + cur*itemsize,
1612 itemsize);
1613 }
1614
1615 return result;
1616 }
1617 }
1618 else {
1619 PyErr_SetString(PyExc_TypeError,
Thomas Woutersed03b412007-08-28 21:37:11 +00001620 "array indices must be integers");
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001621 return NULL;
1622 }
1623}
1624
1625static int
1626array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
1627{
Thomas Woutersed03b412007-08-28 21:37:11 +00001628 Py_ssize_t start, stop, step, slicelength, needed;
1629 arrayobject* other;
1630 int itemsize;
1631
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001632 if (PyIndex_Check(item)) {
1633 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Thomas Woutersed03b412007-08-28 21:37:11 +00001634
1635 if (i == -1 && PyErr_Occurred())
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001636 return -1;
1637 if (i < 0)
Christian Heimes90aa7642007-12-19 02:45:37 +00001638 i += Py_SIZE(self);
1639 if (i < 0 || i >= Py_SIZE(self)) {
Thomas Woutersed03b412007-08-28 21:37:11 +00001640 PyErr_SetString(PyExc_IndexError,
1641 "array assignment index out of range");
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001642 return -1;
1643 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001644 if (value == NULL) {
Thomas Woutersed03b412007-08-28 21:37:11 +00001645 /* Fall through to slice assignment */
1646 start = i;
1647 stop = i + 1;
1648 step = 1;
1649 slicelength = 1;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001650 }
Thomas Woutersed03b412007-08-28 21:37:11 +00001651 else
1652 return (*self->ob_descr->setitem)(self, i, value);
1653 }
1654 else if (PySlice_Check(item)) {
1655 if (PySlice_GetIndicesEx((PySliceObject *)item,
Christian Heimes90aa7642007-12-19 02:45:37 +00001656 Py_SIZE(self), &start, &stop,
Thomas Woutersed03b412007-08-28 21:37:11 +00001657 &step, &slicelength) < 0) {
1658 return -1;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001659 }
Thomas Woutersed03b412007-08-28 21:37:11 +00001660 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001661 else {
Thomas Woutersed03b412007-08-28 21:37:11 +00001662 PyErr_SetString(PyExc_TypeError,
1663 "array indices must be integer");
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001664 return -1;
1665 }
Thomas Woutersed03b412007-08-28 21:37:11 +00001666 if (value == NULL) {
1667 other = NULL;
1668 needed = 0;
1669 }
1670 else if (array_Check(value)) {
1671 other = (arrayobject *)value;
Christian Heimes90aa7642007-12-19 02:45:37 +00001672 needed = Py_SIZE(other);
Thomas Woutersed03b412007-08-28 21:37:11 +00001673 if (self == other) {
1674 /* Special case "self[i:j] = self" -- copy self first */
1675 int ret;
1676 value = array_slice(other, 0, needed);
1677 if (value == NULL)
1678 return -1;
1679 ret = array_ass_subscr(self, item, value);
1680 Py_DECREF(value);
1681 return ret;
1682 }
1683 if (other->ob_descr != self->ob_descr) {
1684 PyErr_BadArgument();
1685 return -1;
1686 }
1687 }
1688 else {
1689 PyErr_Format(PyExc_TypeError,
1690 "can only assign array (not \"%.200s\") to array slice",
Christian Heimes90aa7642007-12-19 02:45:37 +00001691 Py_TYPE(value)->tp_name);
Thomas Woutersed03b412007-08-28 21:37:11 +00001692 return -1;
1693 }
1694 itemsize = self->ob_descr->itemsize;
1695 /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
1696 if ((step > 0 && stop < start) ||
1697 (step < 0 && stop > start))
1698 stop = start;
1699 if (step == 1) {
1700 if (slicelength > needed) {
1701 memmove(self->ob_item + (start + needed) * itemsize,
1702 self->ob_item + stop * itemsize,
Christian Heimes90aa7642007-12-19 02:45:37 +00001703 (Py_SIZE(self) - stop) * itemsize);
1704 if (array_resize(self, Py_SIZE(self) +
Thomas Woutersed03b412007-08-28 21:37:11 +00001705 needed - slicelength) < 0)
1706 return -1;
1707 }
1708 else if (slicelength < needed) {
Christian Heimes90aa7642007-12-19 02:45:37 +00001709 if (array_resize(self, Py_SIZE(self) +
Thomas Woutersed03b412007-08-28 21:37:11 +00001710 needed - slicelength) < 0)
1711 return -1;
1712 memmove(self->ob_item + (start + needed) * itemsize,
1713 self->ob_item + stop * itemsize,
Christian Heimes90aa7642007-12-19 02:45:37 +00001714 (Py_SIZE(self) - start - needed) * itemsize);
Thomas Woutersed03b412007-08-28 21:37:11 +00001715 }
1716 if (needed > 0)
1717 memcpy(self->ob_item + start * itemsize,
1718 other->ob_item, needed * itemsize);
1719 return 0;
1720 }
1721 else if (needed == 0) {
1722 /* Delete slice */
1723 Py_ssize_t cur, i;
1724
1725 if (step < 0) {
1726 stop = start + 1;
1727 start = stop + step * (slicelength - 1) - 1;
1728 step = -step;
1729 }
1730 for (cur = start, i = 0; i < slicelength;
1731 cur += step, i++) {
1732 Py_ssize_t lim = step - 1;
1733
Christian Heimes90aa7642007-12-19 02:45:37 +00001734 if (cur + step >= Py_SIZE(self))
1735 lim = Py_SIZE(self) - cur - 1;
Thomas Woutersed03b412007-08-28 21:37:11 +00001736 memmove(self->ob_item + (cur - i) * itemsize,
1737 self->ob_item + (cur + 1) * itemsize,
1738 lim * itemsize);
1739 }
1740 cur = start + slicelength * step;
Christian Heimes90aa7642007-12-19 02:45:37 +00001741 if (cur < Py_SIZE(self)) {
Thomas Woutersed03b412007-08-28 21:37:11 +00001742 memmove(self->ob_item + (cur-slicelength) * itemsize,
1743 self->ob_item + cur * itemsize,
Christian Heimes90aa7642007-12-19 02:45:37 +00001744 (Py_SIZE(self) - cur) * itemsize);
Thomas Woutersed03b412007-08-28 21:37:11 +00001745 }
Christian Heimes90aa7642007-12-19 02:45:37 +00001746 if (array_resize(self, Py_SIZE(self) - slicelength) < 0)
Thomas Woutersed03b412007-08-28 21:37:11 +00001747 return -1;
1748 return 0;
1749 }
1750 else {
1751 Py_ssize_t cur, i;
1752
1753 if (needed != slicelength) {
1754 PyErr_Format(PyExc_ValueError,
1755 "attempt to assign array of size %zd "
1756 "to extended slice of size %zd",
1757 needed, slicelength);
1758 return -1;
1759 }
1760 for (cur = start, i = 0; i < slicelength;
1761 cur += step, i++) {
1762 memcpy(self->ob_item + cur * itemsize,
1763 other->ob_item + i * itemsize,
1764 itemsize);
1765 }
1766 return 0;
1767 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001768}
1769
1770static PyMappingMethods array_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001771 (lenfunc)array_length,
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001772 (binaryfunc)array_subscr,
1773 (objobjargproc)array_ass_subscr
1774};
1775
Guido van Rossumd8faa362007-04-27 19:54:29 +00001776static const void *emptybuf = "";
1777
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001778
1779static int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00001780array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001781{
Guido van Rossumd417a152007-10-13 21:01:08 +00001782 if ((flags & PyBUF_LOCK)) {
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001783 PyErr_SetString(PyExc_BufferError,
1784 "Cannot lock data");
1785 return -1;
1786 }
1787 if (view==NULL) goto finish;
1788
1789 view->buf = (void *)self->ob_item;
1790 if (view->buf == NULL)
1791 view->buf = (void *)emptybuf;
Christian Heimes90aa7642007-12-19 02:45:37 +00001792 view->len = (Py_SIZE(self)) * self->ob_descr->itemsize;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001793 view->readonly = 0;
1794 view->ndim = 1;
1795 view->itemsize = self->ob_descr->itemsize;
1796 view->suboffsets = NULL;
1797 view->shape = NULL;
1798 if ((flags & PyBUF_ND)==PyBUF_ND) {
Christian Heimes90aa7642007-12-19 02:45:37 +00001799 view->shape = &((Py_SIZE(self)));
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001800 }
1801 view->strides = NULL;
1802 if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES)
1803 view->strides = &(view->itemsize);
1804 view->format = NULL;
1805 view->internal = NULL;
1806 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
Travis E. Oliphantb803c512007-08-20 07:16:33 +00001807 view->format = self->ob_descr->formats;
Travis E. Oliphantddacf962007-10-13 21:03:27 +00001808#ifdef Py_UNICODE_WIDE
1809 if (self->ob_descr->typecode == 'u') {
Guido van Rossum7c4c6c52007-10-14 20:46:15 +00001810 view->format = "w";
Travis E. Oliphantddacf962007-10-13 21:03:27 +00001811 }
1812#endif
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001813 }
1814
1815 finish:
1816 self->ob_exports++;
1817 return 0;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001818}
1819
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001820static void
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00001821array_buffer_relbuf(arrayobject *self, Py_buffer *view)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001822{
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001823 self->ob_exports--;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001824}
1825
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001826static PySequenceMethods array_as_sequence = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001827 (lenfunc)array_length, /*sq_length*/
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001828 (binaryfunc)array_concat, /*sq_concat*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001829 (ssizeargfunc)array_repeat, /*sq_repeat*/
1830 (ssizeargfunc)array_item, /*sq_item*/
Thomas Woutersd2cf20e2007-08-30 22:57:53 +00001831 0, /*sq_slice*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001832 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
Thomas Woutersd2cf20e2007-08-30 22:57:53 +00001833 0, /*sq_ass_slice*/
Raymond Hettinger625812f2003-01-07 01:58:52 +00001834 (objobjproc)array_contains, /*sq_contains*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001835 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001836 (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001837};
1838
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001839static PyBufferProcs array_as_buffer = {
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001840 (getbufferproc)array_buffer_getbuf,
1841 (releasebufferproc)array_buffer_relbuf
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001842};
1843
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001844static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +00001845array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum778983b1993-02-19 15:55:02 +00001846{
Guido van Rossum8934fc22007-06-30 23:44:36 +00001847 int c;
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001848 PyObject *initial = NULL, *it = NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001849 struct arraydescr *descr;
Georg Brandl02c42872005-08-26 06:42:30 +00001850
Thomas Woutersb2137042007-02-01 18:02:27 +00001851 if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001852 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001853
Walter Dörwaldd0941302007-07-01 21:58:22 +00001854 if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
Martin v. Löwis99866332002-03-01 10:27:01 +00001855 return NULL;
1856
1857 if (!(initial == NULL || PyList_Check(initial)
Guido van Rossum6b826ab2007-07-03 16:22:09 +00001858 || PyBytes_Check(initial)
Guido van Rossum98297ee2007-11-06 21:34:58 +00001859 || PyString_Check(initial)
Neal Norwitz3fcbea52007-08-26 04:51:28 +00001860 || PyTuple_Check(initial)
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00001861 || ((c=='u') && PyUnicode_Check(initial)))) {
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001862 it = PyObject_GetIter(initial);
1863 if (it == NULL)
1864 return NULL;
1865 /* We set initial to NULL so that the subsequent code
1866 will create an empty array of the appropriate type
1867 and afterwards we can use array_iter_extend to populate
1868 the array.
1869 */
1870 initial = NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001871 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001872 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1873 if (descr->typecode == c) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001874 PyObject *a;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001875 Py_ssize_t len;
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001876
1877 if (initial == NULL || !(PyList_Check(initial)
1878 || PyTuple_Check(initial)))
Guido van Rossum778983b1993-02-19 15:55:02 +00001879 len = 0;
1880 else
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001881 len = PySequence_Size(initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001882
1883 a = newarrayobject(type, len, descr);
Guido van Rossum778983b1993-02-19 15:55:02 +00001884 if (a == NULL)
1885 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001886
Guido van Rossum778983b1993-02-19 15:55:02 +00001887 if (len > 0) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001888 Py_ssize_t i;
Guido van Rossum778983b1993-02-19 15:55:02 +00001889 for (i = 0; i < len; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001890 PyObject *v =
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001891 PySequence_GetItem(initial, i);
Raymond Hettinger85004cc2003-08-05 11:23:59 +00001892 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001893 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001894 return NULL;
1895 }
Raymond Hettinger85004cc2003-08-05 11:23:59 +00001896 if (setarrayitem(a, i, v) != 0) {
1897 Py_DECREF(v);
1898 Py_DECREF(a);
1899 return NULL;
1900 }
1901 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001902 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001903 }
1904 else if (initial != NULL && (PyBytes_Check(initial) ||
1905 PyString_Check(initial))) {
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001906 PyObject *t_initial, *v;
1907 t_initial = PyTuple_Pack(1, initial);
1908 if (t_initial == NULL) {
1909 Py_DECREF(a);
1910 return NULL;
1911 }
1912 v = array_fromstring((arrayobject *)a,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001913 t_initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001914 Py_DECREF(t_initial);
Guido van Rossum778983b1993-02-19 15:55:02 +00001915 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001916 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001917 return NULL;
1918 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001919 Py_DECREF(v);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001920 }
1921 else if (initial != NULL && PyUnicode_Check(initial)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001922 Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001923 if (n > 0) {
1924 arrayobject *self = (arrayobject *)a;
1925 char *item = self->ob_item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001926 item = (char *)PyMem_Realloc(item, n);
Martin v. Löwis99866332002-03-01 10:27:01 +00001927 if (item == NULL) {
1928 PyErr_NoMemory();
1929 Py_DECREF(a);
1930 return NULL;
1931 }
1932 self->ob_item = item;
Christian Heimes90aa7642007-12-19 02:45:37 +00001933 Py_SIZE(self) = n / sizeof(Py_UNICODE);
Martin v. Löwis99866332002-03-01 10:27:01 +00001934 memcpy(item, PyUnicode_AS_DATA(initial), n);
Christian Heimes90aa7642007-12-19 02:45:37 +00001935 self->allocated = Py_SIZE(self);
Martin v. Löwis99866332002-03-01 10:27:01 +00001936 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001937 }
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001938 if (it != NULL) {
1939 if (array_iter_extend((arrayobject *)a, it) == -1) {
1940 Py_DECREF(it);
1941 Py_DECREF(a);
1942 return NULL;
1943 }
1944 Py_DECREF(it);
1945 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001946 return a;
1947 }
1948 }
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001949 PyErr_SetString(PyExc_ValueError,
Guido van Rossum31f72d72007-06-18 18:44:28 +00001950 "bad typecode (must be b, B, u, h, H, i, I, l, L, f or d)");
Guido van Rossum778983b1993-02-19 15:55:02 +00001951 return NULL;
1952}
1953
Guido van Rossum778983b1993-02-19 15:55:02 +00001954
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001955PyDoc_STRVAR(module_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001956"This module defines an object type which can efficiently represent\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001957an array of basic values: characters, integers, floating point\n\
1958numbers. Arrays are sequence types and behave very much like lists,\n\
1959except that the type of objects stored in them is constrained. The\n\
1960type is specified at object creation time by using a type code, which\n\
1961is a single character. The following type codes are defined:\n\
1962\n\
1963 Type code C Type Minimum size in bytes \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001964 'b' signed integer 1 \n\
1965 'B' unsigned integer 1 \n\
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00001966 'u' Unicode character 2 (see note) \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001967 'h' signed integer 2 \n\
1968 'H' unsigned integer 2 \n\
1969 'i' signed integer 2 \n\
1970 'I' unsigned integer 2 \n\
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001971 'w' unicode character 4 \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001972 'l' signed integer 4 \n\
1973 'L' unsigned integer 4 \n\
1974 'f' floating point 4 \n\
1975 'd' floating point 8 \n\
1976\n\
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00001977NOTE: The 'u' typecode corresponds to Python's unicode character. On \n\
1978narrow builds this is 2-bytes on wide builds this is 4-bytes.\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\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00002008remove() -- remove first occurence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002009reverse() -- reverse the order of the items in the array\n\
2010tofile() -- write all items to a file object\n\
2011tolist() -- return the array converted to an ordinary list\n\
2012tostring() -- return the array converted to a string\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002013\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002014Attributes:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002015\n\
2016typecode -- the typecode character used to create the array\n\
2017itemsize -- the length in bytes of one array item\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002018");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002019
Raymond Hettinger625812f2003-01-07 01:58:52 +00002020static PyObject *array_iter(arrayobject *ao);
2021
Tim Peters0c322792002-07-17 16:49:03 +00002022static PyTypeObject Arraytype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002023 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00002024 "array.array",
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002025 sizeof(arrayobject),
2026 0,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00002027 (destructor)array_dealloc, /* tp_dealloc */
Martin v. Löwisaa158be2002-03-04 09:38:52 +00002028 0, /* tp_print */
Martin v. Löwis99866332002-03-01 10:27:01 +00002029 0, /* tp_getattr */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00002030 0, /* tp_setattr */
2031 0, /* tp_compare */
2032 (reprfunc)array_repr, /* tp_repr */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002033 0, /* tp_as_number*/
2034 &array_as_sequence, /* tp_as_sequence*/
2035 &array_as_mapping, /* tp_as_mapping*/
Guido van Rossum9d19cb82001-01-18 01:02:55 +00002036 0, /* tp_hash */
2037 0, /* tp_call */
2038 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002039 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00002040 0, /* tp_setattro */
Tim Petersb870c752001-06-05 04:43:26 +00002041 &array_as_buffer, /* tp_as_buffer*/
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002042 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00002043 arraytype_doc, /* tp_doc */
2044 0, /* tp_traverse */
2045 0, /* tp_clear */
2046 array_richcompare, /* tp_richcompare */
Raymond Hettingercb87bc82004-05-31 00:35:52 +00002047 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger625812f2003-01-07 01:58:52 +00002048 (getiterfunc)array_iter, /* tp_iter */
Martin v. Löwis99866332002-03-01 10:27:01 +00002049 0, /* tp_iternext */
2050 array_methods, /* tp_methods */
2051 0, /* tp_members */
2052 array_getsets, /* tp_getset */
2053 0, /* tp_base */
2054 0, /* tp_dict */
2055 0, /* tp_descr_get */
2056 0, /* tp_descr_set */
2057 0, /* tp_dictoffset */
2058 0, /* tp_init */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002059 PyType_GenericAlloc, /* tp_alloc */
Martin v. Löwis99866332002-03-01 10:27:01 +00002060 array_new, /* tp_new */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002061 PyObject_Del, /* tp_free */
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002062};
2063
Raymond Hettinger625812f2003-01-07 01:58:52 +00002064
2065/*********************** Array Iterator **************************/
2066
2067typedef struct {
2068 PyObject_HEAD
Martin v. Löwis18e16552006-02-15 17:27:45 +00002069 Py_ssize_t index;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002070 arrayobject *ao;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002071 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002072} arrayiterobject;
2073
2074static PyTypeObject PyArrayIter_Type;
2075
2076#define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
2077
2078static PyObject *
2079array_iter(arrayobject *ao)
2080{
2081 arrayiterobject *it;
2082
2083 if (!array_Check(ao)) {
2084 PyErr_BadInternalCall();
2085 return NULL;
2086 }
2087
2088 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
2089 if (it == NULL)
2090 return NULL;
2091
2092 Py_INCREF(ao);
2093 it->ao = ao;
2094 it->index = 0;
2095 it->getitem = ao->ob_descr->getitem;
2096 PyObject_GC_Track(it);
2097 return (PyObject *)it;
2098}
2099
2100static PyObject *
Raymond Hettinger625812f2003-01-07 01:58:52 +00002101arrayiter_next(arrayiterobject *it)
2102{
2103 assert(PyArrayIter_Check(it));
Christian Heimes90aa7642007-12-19 02:45:37 +00002104 if (it->index < Py_SIZE(it->ao))
Raymond Hettinger625812f2003-01-07 01:58:52 +00002105 return (*it->getitem)(it->ao, it->index++);
2106 return NULL;
2107}
2108
2109static void
2110arrayiter_dealloc(arrayiterobject *it)
2111{
2112 PyObject_GC_UnTrack(it);
2113 Py_XDECREF(it->ao);
2114 PyObject_GC_Del(it);
2115}
2116
2117static int
2118arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2119{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002120 Py_VISIT(it->ao);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002121 return 0;
2122}
2123
2124static PyTypeObject PyArrayIter_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002125 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger625812f2003-01-07 01:58:52 +00002126 "arrayiterator", /* tp_name */
2127 sizeof(arrayiterobject), /* tp_basicsize */
2128 0, /* tp_itemsize */
2129 /* methods */
2130 (destructor)arrayiter_dealloc, /* tp_dealloc */
2131 0, /* tp_print */
2132 0, /* tp_getattr */
2133 0, /* tp_setattr */
2134 0, /* tp_compare */
2135 0, /* tp_repr */
2136 0, /* tp_as_number */
2137 0, /* tp_as_sequence */
2138 0, /* tp_as_mapping */
2139 0, /* tp_hash */
2140 0, /* tp_call */
2141 0, /* tp_str */
2142 PyObject_GenericGetAttr, /* tp_getattro */
2143 0, /* tp_setattro */
2144 0, /* tp_as_buffer */
2145 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2146 0, /* tp_doc */
2147 (traverseproc)arrayiter_traverse, /* tp_traverse */
2148 0, /* tp_clear */
2149 0, /* tp_richcompare */
2150 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002151 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger625812f2003-01-07 01:58:52 +00002152 (iternextfunc)arrayiter_next, /* tp_iternext */
2153 0, /* tp_methods */
2154};
2155
2156
2157/*********************** Install Module **************************/
2158
Martin v. Löwis99866332002-03-01 10:27:01 +00002159/* No functions in array module. */
2160static PyMethodDef a_methods[] = {
2161 {NULL, NULL, 0, NULL} /* Sentinel */
2162};
2163
2164
Mark Hammondfe51c6d2002-08-02 02:27:13 +00002165PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00002166initarray(void)
Guido van Rossum778983b1993-02-19 15:55:02 +00002167{
Fred Drakef4e34842002-04-01 03:45:06 +00002168 PyObject *m;
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00002169 PyObject *typecodes;
2170 Py_ssize_t size = 0;
2171 register Py_UNICODE *p;
2172 struct arraydescr *descr;
Fred Drake0d40ba42000-02-04 20:33:49 +00002173
Guido van Rossum50e9fb92006-08-17 05:42:55 +00002174 if (PyType_Ready(&Arraytype) < 0)
2175 return;
Christian Heimes90aa7642007-12-19 02:45:37 +00002176 Py_TYPE(&PyArrayIter_Type) = &PyType_Type;
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002177 m = Py_InitModule3("array", a_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00002178 if (m == NULL)
2179 return;
Fred Drakef4e34842002-04-01 03:45:06 +00002180
2181 Py_INCREF((PyObject *)&Arraytype);
2182 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
2183 Py_INCREF((PyObject *)&Arraytype);
2184 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00002185
2186 for (descr=descriptors; descr->typecode != '\0'; descr++) {
2187 size++;
2188 }
2189
2190 typecodes = PyUnicode_FromStringAndSize(NULL, size);
2191 p = PyUnicode_AS_UNICODE(typecodes);
2192 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2193 *p++ = (char)descr->typecode;
2194 }
2195
2196 PyModule_AddObject(m, "typecodes", (PyObject *)typecodes);
2197
Guido van Rossuma0deb641998-10-14 13:45:06 +00002198 /* No need to check the error here, the caller will do that */
Guido van Rossum778983b1993-02-19 15:55:02 +00002199}