blob: 8a24a7e06eeddf7b8a2ffae235395f7eee2f9b30 [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)
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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 &&
Martin v. Löwis9f2e3462007-07-21 17:22:18 +000058 Py_Size(self) < newsize + 16 &&
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000059 self->ob_item != NULL) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000118 return PyInt_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];
149 return PyInt_FromLong(x);
150}
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{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000192 return PyInt_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{
211 return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
212}
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{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000240 return PyInt_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{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000297 return PyInt_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 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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 */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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)) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000743 (Py_Size(a)-ihigh)*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000744 a->ob_item = item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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) {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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\")",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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 }
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000902 return PyInt_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
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000920 return PyInt_FromLong((long)i);
921 }
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
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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)
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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));
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001046 PyTuple_SET_ITEM(retval, 1, PyInt_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{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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:
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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:
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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:
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001140 if (Py_Size(array) > 0) {
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001141 result = Py_BuildValue("O(cy#)O",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001142 Py_Type(array),
Guido van Rossumd8faa362007-04-27 19:54:29 +00001143 array->ob_descr->typecode,
1144 array->ob_item,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001168 if (Py_Size(self) > 1) {
Guido van Rossume77a7571993-11-03 15:01:26 +00001169 for (p = self->ob_item,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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
1215 if (!PyBytes_Check(b)) {
1216 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
1222 if (PyBytes_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\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001244array. Also called as read.");
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{
Martin v. Löwis5d7428b2007-07-21 18:47:48 +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
Martin v. Löwis9f2e3462007-07-21 17:22:18 +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;
1266 bytes = PyBytes_FromStringAndSize(ptr, size);
1267 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\
1284Write all items (as machine values) to the file object f. Also called as\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001285write.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001286
1287
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001288static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001289array_fromlist(arrayobject *self, PyObject *list)
Guido van Rossum778983b1993-02-19 15:55:02 +00001290{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001291 Py_ssize_t n;
1292 Py_ssize_t itemsize = self->ob_descr->itemsize;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001293
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001294 if (!PyList_Check(list)) {
1295 PyErr_SetString(PyExc_TypeError, "arg must be list");
Guido van Rossum778983b1993-02-19 15:55:02 +00001296 return NULL;
1297 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001298 n = PyList_Size(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001299 if (n > 0) {
1300 char *item = self->ob_item;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001301 Py_ssize_t i;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001302 PyMem_RESIZE(item, char, (Py_Size(self) + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001303 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001304 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001305 return NULL;
1306 }
1307 self->ob_item = item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001308 Py_Size(self) += n;
1309 self->allocated = Py_Size(self);
Guido van Rossum778983b1993-02-19 15:55:02 +00001310 for (i = 0; i < n; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001311 PyObject *v = PyList_GetItem(list, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001312 if ((*self->ob_descr->setitem)(self,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001313 Py_Size(self) - n + i, v) != 0) {
1314 Py_Size(self) -= n;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001315 PyMem_RESIZE(item, char,
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001316 Py_Size(self) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001317 self->ob_item = item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001318 self->allocated = Py_Size(self);
Guido van Rossum778983b1993-02-19 15:55:02 +00001319 return NULL;
1320 }
1321 }
1322 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001323 Py_INCREF(Py_None);
1324 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001325}
1326
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001327PyDoc_STRVAR(fromlist_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001328"fromlist(list)\n\
1329\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001330Append items to array from list.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001331
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001332static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001333array_tolist(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001334{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001335 PyObject *list = PyList_New(Py_Size(self));
Martin v. Löwis18e16552006-02-15 17:27:45 +00001336 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001337
Guido van Rossum778983b1993-02-19 15:55:02 +00001338 if (list == NULL)
1339 return NULL;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001340 for (i = 0; i < Py_Size(self); i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001341 PyObject *v = getarrayitem((PyObject *)self, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001342 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001343 Py_DECREF(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001344 return NULL;
1345 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001346 PyList_SetItem(list, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001347 }
1348 return list;
1349}
1350
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001351PyDoc_STRVAR(tolist_doc,
Guido van Rossumfc6aba51998-10-14 02:52:31 +00001352"tolist() -> list\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001353\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001354Convert array to an ordinary list with the same items.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001355
1356
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001357static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001358array_fromstring(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001359{
1360 char *str;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001361 Py_ssize_t n;
Guido van Rossum778983b1993-02-19 15:55:02 +00001362 int itemsize = self->ob_descr->itemsize;
Peter Schneider-Kampdc71cac2000-07-31 21:57:30 +00001363 if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +00001364 return NULL;
1365 if (n % itemsize != 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001366 PyErr_SetString(PyExc_ValueError,
Guido van Rossum778983b1993-02-19 15:55:02 +00001367 "string length not a multiple of item size");
1368 return NULL;
1369 }
1370 n = n / itemsize;
1371 if (n > 0) {
1372 char *item = self->ob_item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001373 PyMem_RESIZE(item, char, (Py_Size(self) + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001374 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001375 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001376 return NULL;
1377 }
1378 self->ob_item = item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001379 Py_Size(self) += n;
1380 self->allocated = Py_Size(self);
1381 memcpy(item + (Py_Size(self) - n) * itemsize,
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001382 str, itemsize*n);
Guido van Rossum778983b1993-02-19 15:55:02 +00001383 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001384 Py_INCREF(Py_None);
1385 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001386}
1387
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001388PyDoc_STRVAR(fromstring_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001389"fromstring(string)\n\
1390\n\
1391Appends items from the string, interpreting it as an array of machine\n\
Walter Dörwald93b30b52007-06-22 12:21:53 +00001392values, as if it had been read from a file using the fromfile() method).");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001393
1394
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001395static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001396array_tostring(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001397{
Guido van Rossum31f72d72007-06-18 18:44:28 +00001398 return PyBytes_FromStringAndSize(self->ob_item,
Martin v. Löwis5d7428b2007-07-21 18:47:48 +00001399 Py_Size(self) * self->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001400}
1401
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001402PyDoc_STRVAR(tostring_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001403"tostring() -> string\n\
1404\n\
1405Convert the array to an array of machine values and return the string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001406representation.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001407
Martin v. Löwis99866332002-03-01 10:27:01 +00001408
1409
Martin v. Löwis99866332002-03-01 10:27:01 +00001410static PyObject *
1411array_fromunicode(arrayobject *self, PyObject *args)
1412{
1413 Py_UNICODE *ustr;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001414 Py_ssize_t n;
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00001415 char typecode;
Martin v. Löwis99866332002-03-01 10:27:01 +00001416
1417 if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
1418 return NULL;
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00001419 typecode = self->ob_descr->typecode;
1420 if ((typecode != 'u')) {
1421 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis99866332002-03-01 10:27:01 +00001422 "fromunicode() may only be called on "
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001423 "unicode type arrays");
Martin v. Löwis99866332002-03-01 10:27:01 +00001424 return NULL;
1425 }
1426 if (n > 0) {
1427 Py_UNICODE *item = (Py_UNICODE *) self->ob_item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001428 PyMem_RESIZE(item, Py_UNICODE, Py_Size(self) + n);
Martin v. Löwis99866332002-03-01 10:27:01 +00001429 if (item == NULL) {
1430 PyErr_NoMemory();
1431 return NULL;
1432 }
1433 self->ob_item = (char *) item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001434 Py_Size(self) += n;
1435 self->allocated = Py_Size(self);
1436 memcpy(item + Py_Size(self) - n,
Martin v. Löwis99866332002-03-01 10:27:01 +00001437 ustr, n * sizeof(Py_UNICODE));
1438 }
1439
1440 Py_INCREF(Py_None);
1441 return Py_None;
1442}
1443
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001444PyDoc_STRVAR(fromunicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001445"fromunicode(ustr)\n\
1446\n\
1447Extends this array with data from the unicode string ustr.\n\
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001448The array must be a unicode type array; otherwise a ValueError\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001449is raised. Use array.fromstring(ustr.decode(...)) to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001450append Unicode data to an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001451
1452
1453static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001454array_tounicode(arrayobject *self, PyObject *unused)
Martin v. Löwis99866332002-03-01 10:27:01 +00001455{
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00001456 char typecode;
1457 typecode = self->ob_descr->typecode;
1458 if ((typecode != 'u')) {
Martin v. Löwis99866332002-03-01 10:27:01 +00001459 PyErr_SetString(PyExc_ValueError,
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00001460 "tounicode() may only be called on unicode type arrays");
Martin v. Löwis99866332002-03-01 10:27:01 +00001461 return NULL;
1462 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001463 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_Size(self));
Martin v. Löwis99866332002-03-01 10:27:01 +00001464}
1465
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001466PyDoc_STRVAR(tounicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001467"tounicode() -> unicode\n\
1468\n\
1469Convert the array to a unicode string. The array must be\n\
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001470a unicode type array; otherwise a ValueError is raised. Use\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001471array.tostring().decode() to obtain a unicode string from\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001472an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001473
Martin v. Löwis99866332002-03-01 10:27:01 +00001474
1475
1476static PyObject *
1477array_get_typecode(arrayobject *a, void *closure)
1478{
1479 char tc = a->ob_descr->typecode;
Walter Dörwald93b30b52007-06-22 12:21:53 +00001480 return PyUnicode_FromStringAndSize(&tc, 1);
Martin v. Löwis99866332002-03-01 10:27:01 +00001481}
1482
1483static PyObject *
1484array_get_itemsize(arrayobject *a, void *closure)
1485{
1486 return PyInt_FromLong((long)a->ob_descr->itemsize);
1487}
1488
1489static PyGetSetDef array_getsets [] = {
1490 {"typecode", (getter) array_get_typecode, NULL,
1491 "the typecode character used to create the array"},
1492 {"itemsize", (getter) array_get_itemsize, NULL,
1493 "the size, in bytes, of one array item"},
1494 {NULL}
1495};
1496
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001497PyMethodDef array_methods[] = {
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001498 {"append", (PyCFunction)array_append, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001499 append_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001500 {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001501 buffer_info_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001502 {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001503 byteswap_doc},
Raymond Hettinger3aa82c02004-03-13 18:18:51 +00001504 {"__copy__", (PyCFunction)array_copy, METH_NOARGS,
1505 copy_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001506 {"count", (PyCFunction)array_count, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001507 count_doc},
Thomas Wouters89f507f2006-12-13 04:49:30 +00001508 {"__deepcopy__",(PyCFunction)array_copy, METH_O,
Raymond Hettinger3aa82c02004-03-13 18:18:51 +00001509 copy_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001510 {"extend", (PyCFunction)array_extend, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001511 extend_doc},
1512 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
1513 fromfile_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001514 {"fromlist", (PyCFunction)array_fromlist, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001515 fromlist_doc},
1516 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
1517 fromstring_doc},
Martin v. Löwis99866332002-03-01 10:27:01 +00001518 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
1519 fromunicode_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001520 {"index", (PyCFunction)array_index, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001521 index_doc},
1522 {"insert", (PyCFunction)array_insert, METH_VARARGS,
1523 insert_doc},
1524 {"pop", (PyCFunction)array_pop, METH_VARARGS,
1525 pop_doc},
1526 {"read", (PyCFunction)array_fromfile, METH_VARARGS,
1527 fromfile_doc},
Raymond Hettingerb0900e62004-12-16 16:23:40 +00001528 {"__reduce__", (PyCFunction)array_reduce, METH_NOARGS,
1529 array_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001530 {"remove", (PyCFunction)array_remove, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001531 remove_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001532 {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001533 reverse_doc},
1534/* {"sort", (PyCFunction)array_sort, METH_VARARGS,
1535 sort_doc},*/
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001536 {"tofile", (PyCFunction)array_tofile, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001537 tofile_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001538 {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001539 tolist_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001540 {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001541 tostring_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001542 {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
Martin v. Löwis99866332002-03-01 10:27:01 +00001543 tounicode_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001544 {"write", (PyCFunction)array_tofile, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001545 tofile_doc},
Guido van Rossum778983b1993-02-19 15:55:02 +00001546 {NULL, NULL} /* sentinel */
1547};
1548
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001549static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001550array_repr(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +00001551{
Walter Dörwaldc2f6a582007-05-20 12:49:47 +00001552 char typecode;
Walter Dörwald7569dfe2007-05-19 21:49:49 +00001553 PyObject *s, *v = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001554 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00001555
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001556 len = Py_Size(a);
Martin v. Löwis99866332002-03-01 10:27:01 +00001557 typecode = a->ob_descr->typecode;
Guido van Rossum778983b1993-02-19 15:55:02 +00001558 if (len == 0) {
Walter Dörwald7569dfe2007-05-19 21:49:49 +00001559 return PyUnicode_FromFormat("array('%c')", typecode);
Guido van Rossum778983b1993-02-19 15:55:02 +00001560 }
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00001561 if ((typecode == 'u'))
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001562 v = array_tounicode(a, NULL);
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001563 else
1564 v = array_tolist(a, NULL);
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001565
Walter Dörwald7569dfe2007-05-19 21:49:49 +00001566 s = PyUnicode_FromFormat("array('%c', %R)", typecode, v);
1567 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001568 return s;
1569}
1570
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001571static PyObject*
1572array_subscr(arrayobject* self, PyObject* item)
1573{
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001574 if (PyIndex_Check(item)) {
1575 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001576 if (i==-1 && PyErr_Occurred()) {
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001577 return NULL;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001578 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001579 if (i < 0)
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001580 i += Py_Size(self);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001581 return array_item(self, i);
1582 }
1583 else if (PySlice_Check(item)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001584 Py_ssize_t start, stop, step, slicelength, cur, i;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001585 PyObject* result;
1586 arrayobject* ar;
1587 int itemsize = self->ob_descr->itemsize;
1588
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001589 if (PySlice_GetIndicesEx((PySliceObject*)item, Py_Size(self),
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001590 &start, &stop, &step, &slicelength) < 0) {
1591 return NULL;
1592 }
1593
1594 if (slicelength <= 0) {
1595 return newarrayobject(&Arraytype, 0, self->ob_descr);
1596 }
Thomas Woutersed03b412007-08-28 21:37:11 +00001597 else if (step == 1) {
1598 PyObject *result = newarrayobject(&Arraytype,
1599 slicelength, self->ob_descr);
1600 if (result == NULL)
1601 return NULL;
1602 memcpy(((arrayobject *)result)->ob_item,
1603 self->ob_item + start * itemsize,
1604 slicelength * itemsize);
1605 return result;
1606 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001607 else {
1608 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
1609 if (!result) return NULL;
1610
1611 ar = (arrayobject*)result;
1612
1613 for (cur = start, i = 0; i < slicelength;
1614 cur += step, i++) {
1615 memcpy(ar->ob_item + i*itemsize,
1616 self->ob_item + cur*itemsize,
1617 itemsize);
1618 }
1619
1620 return result;
1621 }
1622 }
1623 else {
1624 PyErr_SetString(PyExc_TypeError,
Thomas Woutersed03b412007-08-28 21:37:11 +00001625 "array indices must be integers");
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001626 return NULL;
1627 }
1628}
1629
1630static int
1631array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
1632{
Thomas Woutersed03b412007-08-28 21:37:11 +00001633 Py_ssize_t start, stop, step, slicelength, needed;
1634 arrayobject* other;
1635 int itemsize;
1636
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001637 if (PyIndex_Check(item)) {
1638 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Thomas Woutersed03b412007-08-28 21:37:11 +00001639
1640 if (i == -1 && PyErr_Occurred())
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001641 return -1;
1642 if (i < 0)
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001643 i += Py_Size(self);
Thomas Woutersed03b412007-08-28 21:37:11 +00001644 if (i < 0 || i >= Py_Size(self)) {
1645 PyErr_SetString(PyExc_IndexError,
1646 "array assignment index out of range");
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001647 return -1;
1648 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001649 if (value == NULL) {
Thomas Woutersed03b412007-08-28 21:37:11 +00001650 /* Fall through to slice assignment */
1651 start = i;
1652 stop = i + 1;
1653 step = 1;
1654 slicelength = 1;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001655 }
Thomas Woutersed03b412007-08-28 21:37:11 +00001656 else
1657 return (*self->ob_descr->setitem)(self, i, value);
1658 }
1659 else if (PySlice_Check(item)) {
1660 if (PySlice_GetIndicesEx((PySliceObject *)item,
1661 Py_Size(self), &start, &stop,
1662 &step, &slicelength) < 0) {
1663 return -1;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001664 }
Thomas Woutersed03b412007-08-28 21:37:11 +00001665 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001666 else {
Thomas Woutersed03b412007-08-28 21:37:11 +00001667 PyErr_SetString(PyExc_TypeError,
1668 "array indices must be integer");
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001669 return -1;
1670 }
Thomas Woutersed03b412007-08-28 21:37:11 +00001671 if (value == NULL) {
1672 other = NULL;
1673 needed = 0;
1674 }
1675 else if (array_Check(value)) {
1676 other = (arrayobject *)value;
1677 needed = Py_Size(other);
1678 if (self == other) {
1679 /* Special case "self[i:j] = self" -- copy self first */
1680 int ret;
1681 value = array_slice(other, 0, needed);
1682 if (value == NULL)
1683 return -1;
1684 ret = array_ass_subscr(self, item, value);
1685 Py_DECREF(value);
1686 return ret;
1687 }
1688 if (other->ob_descr != self->ob_descr) {
1689 PyErr_BadArgument();
1690 return -1;
1691 }
1692 }
1693 else {
1694 PyErr_Format(PyExc_TypeError,
1695 "can only assign array (not \"%.200s\") to array slice",
1696 Py_Type(value)->tp_name);
1697 return -1;
1698 }
1699 itemsize = self->ob_descr->itemsize;
1700 /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
1701 if ((step > 0 && stop < start) ||
1702 (step < 0 && stop > start))
1703 stop = start;
1704 if (step == 1) {
1705 if (slicelength > needed) {
1706 memmove(self->ob_item + (start + needed) * itemsize,
1707 self->ob_item + stop * itemsize,
1708 (Py_Size(self) - stop) * itemsize);
1709 if (array_resize(self, Py_Size(self) +
1710 needed - slicelength) < 0)
1711 return -1;
1712 }
1713 else if (slicelength < needed) {
1714 if (array_resize(self, Py_Size(self) +
1715 needed - slicelength) < 0)
1716 return -1;
1717 memmove(self->ob_item + (start + needed) * itemsize,
1718 self->ob_item + stop * itemsize,
1719 (Py_Size(self) - start - needed) * itemsize);
1720 }
1721 if (needed > 0)
1722 memcpy(self->ob_item + start * itemsize,
1723 other->ob_item, needed * itemsize);
1724 return 0;
1725 }
1726 else if (needed == 0) {
1727 /* Delete slice */
1728 Py_ssize_t cur, i;
1729
1730 if (step < 0) {
1731 stop = start + 1;
1732 start = stop + step * (slicelength - 1) - 1;
1733 step = -step;
1734 }
1735 for (cur = start, i = 0; i < slicelength;
1736 cur += step, i++) {
1737 Py_ssize_t lim = step - 1;
1738
1739 if (cur + step >= Py_Size(self))
1740 lim = Py_Size(self) - cur - 1;
1741 memmove(self->ob_item + (cur - i) * itemsize,
1742 self->ob_item + (cur + 1) * itemsize,
1743 lim * itemsize);
1744 }
1745 cur = start + slicelength * step;
1746 if (cur < Py_Size(self)) {
1747 memmove(self->ob_item + (cur-slicelength) * itemsize,
1748 self->ob_item + cur * itemsize,
1749 (Py_Size(self) - cur) * itemsize);
1750 }
1751 if (array_resize(self, Py_Size(self) - slicelength) < 0)
1752 return -1;
1753 return 0;
1754 }
1755 else {
1756 Py_ssize_t cur, i;
1757
1758 if (needed != slicelength) {
1759 PyErr_Format(PyExc_ValueError,
1760 "attempt to assign array of size %zd "
1761 "to extended slice of size %zd",
1762 needed, slicelength);
1763 return -1;
1764 }
1765 for (cur = start, i = 0; i < slicelength;
1766 cur += step, i++) {
1767 memcpy(self->ob_item + cur * itemsize,
1768 other->ob_item + i * itemsize,
1769 itemsize);
1770 }
1771 return 0;
1772 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001773}
1774
1775static PyMappingMethods array_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001776 (lenfunc)array_length,
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001777 (binaryfunc)array_subscr,
1778 (objobjargproc)array_ass_subscr
1779};
1780
Guido van Rossumd8faa362007-04-27 19:54:29 +00001781static const void *emptybuf = "";
1782
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001783
1784static int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00001785array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001786{
Guido van Rossumd417a152007-10-13 21:01:08 +00001787 if ((flags & PyBUF_LOCK)) {
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001788 PyErr_SetString(PyExc_BufferError,
1789 "Cannot lock data");
1790 return -1;
1791 }
1792 if (view==NULL) goto finish;
1793
1794 view->buf = (void *)self->ob_item;
1795 if (view->buf == NULL)
1796 view->buf = (void *)emptybuf;
1797 view->len = (Py_Size(self)) * self->ob_descr->itemsize;
1798 view->readonly = 0;
1799 view->ndim = 1;
1800 view->itemsize = self->ob_descr->itemsize;
1801 view->suboffsets = NULL;
1802 view->shape = NULL;
1803 if ((flags & PyBUF_ND)==PyBUF_ND) {
1804 view->shape = &((Py_Size(self)));
1805 }
1806 view->strides = NULL;
1807 if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES)
1808 view->strides = &(view->itemsize);
1809 view->format = NULL;
1810 view->internal = NULL;
1811 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
Travis E. Oliphantb803c512007-08-20 07:16:33 +00001812 view->format = self->ob_descr->formats;
Travis E. Oliphantddacf962007-10-13 21:03:27 +00001813#ifdef Py_UNICODE_WIDE
1814 if (self->ob_descr->typecode == 'u') {
Guido van Rossum7c4c6c52007-10-14 20:46:15 +00001815 view->format = "w";
Travis E. Oliphantddacf962007-10-13 21:03:27 +00001816 }
1817#endif
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001818 }
1819
1820 finish:
1821 self->ob_exports++;
1822 return 0;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001823}
1824
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001825static void
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00001826array_buffer_relbuf(arrayobject *self, Py_buffer *view)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001827{
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001828 self->ob_exports--;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001829}
1830
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001831static PySequenceMethods array_as_sequence = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001832 (lenfunc)array_length, /*sq_length*/
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001833 (binaryfunc)array_concat, /*sq_concat*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001834 (ssizeargfunc)array_repeat, /*sq_repeat*/
1835 (ssizeargfunc)array_item, /*sq_item*/
Thomas Woutersd2cf20e2007-08-30 22:57:53 +00001836 0, /*sq_slice*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001837 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
Thomas Woutersd2cf20e2007-08-30 22:57:53 +00001838 0, /*sq_ass_slice*/
Raymond Hettinger625812f2003-01-07 01:58:52 +00001839 (objobjproc)array_contains, /*sq_contains*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001840 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001841 (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001842};
1843
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001844static PyBufferProcs array_as_buffer = {
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001845 (getbufferproc)array_buffer_getbuf,
1846 (releasebufferproc)array_buffer_relbuf
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001847};
1848
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001849static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +00001850array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum778983b1993-02-19 15:55:02 +00001851{
Guido van Rossum8934fc22007-06-30 23:44:36 +00001852 int c;
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001853 PyObject *initial = NULL, *it = NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001854 struct arraydescr *descr;
Georg Brandl02c42872005-08-26 06:42:30 +00001855
Thomas Woutersb2137042007-02-01 18:02:27 +00001856 if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001857 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001858
Walter Dörwaldd0941302007-07-01 21:58:22 +00001859 if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
Martin v. Löwis99866332002-03-01 10:27:01 +00001860 return NULL;
1861
1862 if (!(initial == NULL || PyList_Check(initial)
Guido van Rossum6b826ab2007-07-03 16:22:09 +00001863 || PyBytes_Check(initial)
Neal Norwitz3fcbea52007-08-26 04:51:28 +00001864 || PyTuple_Check(initial)
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00001865 || ((c=='u') && PyUnicode_Check(initial)))) {
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001866 it = PyObject_GetIter(initial);
1867 if (it == NULL)
1868 return NULL;
1869 /* We set initial to NULL so that the subsequent code
1870 will create an empty array of the appropriate type
1871 and afterwards we can use array_iter_extend to populate
1872 the array.
1873 */
1874 initial = NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001875 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001876 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1877 if (descr->typecode == c) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001878 PyObject *a;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001879 Py_ssize_t len;
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001880
1881 if (initial == NULL || !(PyList_Check(initial)
1882 || PyTuple_Check(initial)))
Guido van Rossum778983b1993-02-19 15:55:02 +00001883 len = 0;
1884 else
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001885 len = PySequence_Size(initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001886
1887 a = newarrayobject(type, len, descr);
Guido van Rossum778983b1993-02-19 15:55:02 +00001888 if (a == NULL)
1889 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001890
Guido van Rossum778983b1993-02-19 15:55:02 +00001891 if (len > 0) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001892 Py_ssize_t i;
Guido van Rossum778983b1993-02-19 15:55:02 +00001893 for (i = 0; i < len; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001894 PyObject *v =
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001895 PySequence_GetItem(initial, i);
Raymond Hettinger85004cc2003-08-05 11:23:59 +00001896 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001897 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001898 return NULL;
1899 }
Raymond Hettinger85004cc2003-08-05 11:23:59 +00001900 if (setarrayitem(a, i, v) != 0) {
1901 Py_DECREF(v);
1902 Py_DECREF(a);
1903 return NULL;
1904 }
1905 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001906 }
Neal Norwitz3fcbea52007-08-26 04:51:28 +00001907 } else if (initial != NULL && PyBytes_Check(initial)) {
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001908 PyObject *t_initial, *v;
1909 t_initial = PyTuple_Pack(1, initial);
1910 if (t_initial == NULL) {
1911 Py_DECREF(a);
1912 return NULL;
1913 }
1914 v = array_fromstring((arrayobject *)a,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001915 t_initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001916 Py_DECREF(t_initial);
Guido van Rossum778983b1993-02-19 15:55:02 +00001917 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001918 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001919 return NULL;
1920 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001921 Py_DECREF(v);
Martin v. Löwis99866332002-03-01 10:27:01 +00001922 } else if (initial != NULL && PyUnicode_Check(initial)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001923 Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001924 if (n > 0) {
1925 arrayobject *self = (arrayobject *)a;
1926 char *item = self->ob_item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001927 item = (char *)PyMem_Realloc(item, n);
Martin v. Löwis99866332002-03-01 10:27:01 +00001928 if (item == NULL) {
1929 PyErr_NoMemory();
1930 Py_DECREF(a);
1931 return NULL;
1932 }
1933 self->ob_item = item;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001934 Py_Size(self) = n / sizeof(Py_UNICODE);
Martin v. Löwis99866332002-03-01 10:27:01 +00001935 memcpy(item, PyUnicode_AS_DATA(initial), n);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001936 self->allocated = Py_Size(self);
Martin v. Löwis99866332002-03-01 10:27:01 +00001937 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001938 }
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001939 if (it != NULL) {
1940 if (array_iter_extend((arrayobject *)a, it) == -1) {
1941 Py_DECREF(it);
1942 Py_DECREF(a);
1943 return NULL;
1944 }
1945 Py_DECREF(it);
1946 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001947 return a;
1948 }
1949 }
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001950 PyErr_SetString(PyExc_ValueError,
Guido van Rossum31f72d72007-06-18 18:44:28 +00001951 "bad typecode (must be b, B, u, h, H, i, I, l, L, f or d)");
Guido van Rossum778983b1993-02-19 15:55:02 +00001952 return NULL;
1953}
1954
Guido van Rossum778983b1993-02-19 15:55:02 +00001955
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001956PyDoc_STRVAR(module_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001957"This module defines an object type which can efficiently represent\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001958an array of basic values: characters, integers, floating point\n\
1959numbers. Arrays are sequence types and behave very much like lists,\n\
1960except that the type of objects stored in them is constrained. The\n\
1961type is specified at object creation time by using a type code, which\n\
1962is a single character. The following type codes are defined:\n\
1963\n\
1964 Type code C Type Minimum size in bytes \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001965 'b' signed integer 1 \n\
1966 'B' unsigned integer 1 \n\
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00001967 'u' Unicode character 2 (see note) \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001968 'h' signed integer 2 \n\
1969 'H' unsigned integer 2 \n\
1970 'i' signed integer 2 \n\
1971 'I' unsigned integer 2 \n\
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001972 'w' unicode character 4 \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001973 'l' signed integer 4 \n\
1974 'L' unsigned integer 4 \n\
1975 'f' floating point 4 \n\
1976 'd' floating point 8 \n\
1977\n\
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00001978NOTE: The 'u' typecode corresponds to Python's unicode character. On \n\
1979narrow builds this is 2-bytes on wide builds this is 4-bytes.\n\
1980\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001981The constructor is:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001982\n\
1983array(typecode [, initializer]) -- create a new array\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001984");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001985
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001986PyDoc_STRVAR(arraytype_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001987"array(typecode [, initializer]) -> array\n\
1988\n\
1989Return a new array whose items are restricted by typecode, and\n\
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001990initialized from the optional initializer value, which must be a list,\n\
1991string. or iterable over elements of the appropriate type.\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001992\n\
1993Arrays represent basic values and behave very much like lists, except\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001994the type of objects stored in them is constrained.\n\
1995\n\
1996Methods:\n\
1997\n\
1998append() -- append a new item to the end of the array\n\
1999buffer_info() -- return information giving the current memory info\n\
2000byteswap() -- byteswap all the items of the array\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00002001count() -- return number of occurences of an object\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00002002extend() -- extend array by appending multiple elements from an iterable\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002003fromfile() -- read items from a file object\n\
2004fromlist() -- append items from the list\n\
2005fromstring() -- append items from the string\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00002006index() -- return index of first occurence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002007insert() -- insert a new item into the array at a provided position\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00002008pop() -- remove and return item (default last)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002009read() -- DEPRECATED, use fromfile()\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00002010remove() -- remove first occurence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002011reverse() -- reverse the order of the items in the array\n\
2012tofile() -- write all items to a file object\n\
2013tolist() -- return the array converted to an ordinary list\n\
2014tostring() -- return the array converted to a string\n\
2015write() -- DEPRECATED, use tofile()\n\
2016\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002017Attributes:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002018\n\
2019typecode -- the typecode character used to create the array\n\
2020itemsize -- the length in bytes of one array item\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002021");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002022
Raymond Hettinger625812f2003-01-07 01:58:52 +00002023static PyObject *array_iter(arrayobject *ao);
2024
Tim Peters0c322792002-07-17 16:49:03 +00002025static PyTypeObject Arraytype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002026 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00002027 "array.array",
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002028 sizeof(arrayobject),
2029 0,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00002030 (destructor)array_dealloc, /* tp_dealloc */
Martin v. Löwisaa158be2002-03-04 09:38:52 +00002031 0, /* tp_print */
Martin v. Löwis99866332002-03-01 10:27:01 +00002032 0, /* tp_getattr */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00002033 0, /* tp_setattr */
2034 0, /* tp_compare */
2035 (reprfunc)array_repr, /* tp_repr */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002036 0, /* tp_as_number*/
2037 &array_as_sequence, /* tp_as_sequence*/
2038 &array_as_mapping, /* tp_as_mapping*/
Guido van Rossum9d19cb82001-01-18 01:02:55 +00002039 0, /* tp_hash */
2040 0, /* tp_call */
2041 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002042 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00002043 0, /* tp_setattro */
Tim Petersb870c752001-06-05 04:43:26 +00002044 &array_as_buffer, /* tp_as_buffer*/
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002045 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00002046 arraytype_doc, /* tp_doc */
2047 0, /* tp_traverse */
2048 0, /* tp_clear */
2049 array_richcompare, /* tp_richcompare */
Raymond Hettingercb87bc82004-05-31 00:35:52 +00002050 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger625812f2003-01-07 01:58:52 +00002051 (getiterfunc)array_iter, /* tp_iter */
Martin v. Löwis99866332002-03-01 10:27:01 +00002052 0, /* tp_iternext */
2053 array_methods, /* tp_methods */
2054 0, /* tp_members */
2055 array_getsets, /* tp_getset */
2056 0, /* tp_base */
2057 0, /* tp_dict */
2058 0, /* tp_descr_get */
2059 0, /* tp_descr_set */
2060 0, /* tp_dictoffset */
2061 0, /* tp_init */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002062 PyType_GenericAlloc, /* tp_alloc */
Martin v. Löwis99866332002-03-01 10:27:01 +00002063 array_new, /* tp_new */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002064 PyObject_Del, /* tp_free */
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002065};
2066
Raymond Hettinger625812f2003-01-07 01:58:52 +00002067
2068/*********************** Array Iterator **************************/
2069
2070typedef struct {
2071 PyObject_HEAD
Martin v. Löwis18e16552006-02-15 17:27:45 +00002072 Py_ssize_t index;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002073 arrayobject *ao;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002074 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002075} arrayiterobject;
2076
2077static PyTypeObject PyArrayIter_Type;
2078
2079#define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
2080
2081static PyObject *
2082array_iter(arrayobject *ao)
2083{
2084 arrayiterobject *it;
2085
2086 if (!array_Check(ao)) {
2087 PyErr_BadInternalCall();
2088 return NULL;
2089 }
2090
2091 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
2092 if (it == NULL)
2093 return NULL;
2094
2095 Py_INCREF(ao);
2096 it->ao = ao;
2097 it->index = 0;
2098 it->getitem = ao->ob_descr->getitem;
2099 PyObject_GC_Track(it);
2100 return (PyObject *)it;
2101}
2102
2103static PyObject *
Raymond Hettinger625812f2003-01-07 01:58:52 +00002104arrayiter_next(arrayiterobject *it)
2105{
2106 assert(PyArrayIter_Check(it));
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002107 if (it->index < Py_Size(it->ao))
Raymond Hettinger625812f2003-01-07 01:58:52 +00002108 return (*it->getitem)(it->ao, it->index++);
2109 return NULL;
2110}
2111
2112static void
2113arrayiter_dealloc(arrayiterobject *it)
2114{
2115 PyObject_GC_UnTrack(it);
2116 Py_XDECREF(it->ao);
2117 PyObject_GC_Del(it);
2118}
2119
2120static int
2121arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2122{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002123 Py_VISIT(it->ao);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002124 return 0;
2125}
2126
2127static PyTypeObject PyArrayIter_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002128 PyVarObject_HEAD_INIT(NULL, 0)
Raymond Hettinger625812f2003-01-07 01:58:52 +00002129 "arrayiterator", /* tp_name */
2130 sizeof(arrayiterobject), /* tp_basicsize */
2131 0, /* tp_itemsize */
2132 /* methods */
2133 (destructor)arrayiter_dealloc, /* tp_dealloc */
2134 0, /* tp_print */
2135 0, /* tp_getattr */
2136 0, /* tp_setattr */
2137 0, /* tp_compare */
2138 0, /* tp_repr */
2139 0, /* tp_as_number */
2140 0, /* tp_as_sequence */
2141 0, /* tp_as_mapping */
2142 0, /* tp_hash */
2143 0, /* tp_call */
2144 0, /* tp_str */
2145 PyObject_GenericGetAttr, /* tp_getattro */
2146 0, /* tp_setattro */
2147 0, /* tp_as_buffer */
2148 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2149 0, /* tp_doc */
2150 (traverseproc)arrayiter_traverse, /* tp_traverse */
2151 0, /* tp_clear */
2152 0, /* tp_richcompare */
2153 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002154 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger625812f2003-01-07 01:58:52 +00002155 (iternextfunc)arrayiter_next, /* tp_iternext */
2156 0, /* tp_methods */
2157};
2158
2159
2160/*********************** Install Module **************************/
2161
Martin v. Löwis99866332002-03-01 10:27:01 +00002162/* No functions in array module. */
2163static PyMethodDef a_methods[] = {
2164 {NULL, NULL, 0, NULL} /* Sentinel */
2165};
2166
2167
Mark Hammondfe51c6d2002-08-02 02:27:13 +00002168PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00002169initarray(void)
Guido van Rossum778983b1993-02-19 15:55:02 +00002170{
Fred Drakef4e34842002-04-01 03:45:06 +00002171 PyObject *m;
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00002172 PyObject *typecodes;
2173 Py_ssize_t size = 0;
2174 register Py_UNICODE *p;
2175 struct arraydescr *descr;
Fred Drake0d40ba42000-02-04 20:33:49 +00002176
Guido van Rossum50e9fb92006-08-17 05:42:55 +00002177 if (PyType_Ready(&Arraytype) < 0)
2178 return;
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002179 Py_Type(&PyArrayIter_Type) = &PyType_Type;
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002180 m = Py_InitModule3("array", a_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00002181 if (m == NULL)
2182 return;
Fred Drakef4e34842002-04-01 03:45:06 +00002183
2184 Py_INCREF((PyObject *)&Arraytype);
2185 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
2186 Py_INCREF((PyObject *)&Arraytype);
2187 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00002188
2189 for (descr=descriptors; descr->typecode != '\0'; descr++) {
2190 size++;
2191 }
2192
2193 typecodes = PyUnicode_FromStringAndSize(NULL, size);
2194 p = PyUnicode_AS_UNICODE(typecodes);
2195 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2196 *p++ = (char)descr->typecode;
2197 }
2198
2199 PyModule_AddObject(m, "typecodes", (PyObject *)typecodes);
2200
Guido van Rossuma0deb641998-10-14 13:45:06 +00002201 /* No need to check the error here, the caller will do that */
Guido van Rossum778983b1993-02-19 15:55:02 +00002202}