blob: 64303330012cddb43a8e4ce66050f3034c2e5cac [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
Roger E. Masse2919eaa1996-12-09 20:10:36 +00006#include "Python.h"
Raymond Hettingercb87bc82004-05-31 00:35:52 +00007#include "structmember.h"
Roger E. Masse5817f8f1996-12-09 22:24:19 +00008
Guido van Rossum0c709541994-08-19 12:01:32 +00009#ifdef STDC_HEADERS
10#include <stddef.h>
Guido van Rossum7f1de831999-08-27 20:33:52 +000011#else /* !STDC_HEADERS */
12#ifndef DONT_HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000013#include <sys/types.h> /* For size_t */
Guido van Rossum7f1de831999-08-27 20:33:52 +000014#endif /* DONT_HAVE_SYS_TYPES_H */
15#endif /* !STDC_HEADERS */
Guido van Rossum778983b1993-02-19 15:55:02 +000016
17struct arrayobject; /* Forward */
18
Tim Petersbb307342000-09-10 05:22:54 +000019/* All possible arraydescr values are defined in the vector "descriptors"
20 * below. That's defined later because the appropriate get and set
21 * functions aren't visible yet.
22 */
Guido van Rossum778983b1993-02-19 15:55:02 +000023struct arraydescr {
24 int typecode;
25 int itemsize;
Tim Petersdbd9ba62000-07-09 03:09:57 +000026 PyObject * (*getitem)(struct arrayobject *, int);
27 int (*setitem)(struct arrayobject *, int, PyObject *);
Guido van Rossum778983b1993-02-19 15:55:02 +000028};
29
30typedef struct arrayobject {
Martin v. Löwis99866332002-03-01 10:27:01 +000031 PyObject_HEAD
32 int ob_size;
Guido van Rossum778983b1993-02-19 15:55:02 +000033 char *ob_item;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000034 int allocated;
Guido van Rossum778983b1993-02-19 15:55:02 +000035 struct arraydescr *ob_descr;
Raymond Hettingercb87bc82004-05-31 00:35:52 +000036 PyObject *weakreflist; /* List of weak references */
Guido van Rossum778983b1993-02-19 15:55:02 +000037} arrayobject;
38
Jeremy Hylton938ace62002-07-17 16:30:39 +000039static PyTypeObject Arraytype;
Guido van Rossum778983b1993-02-19 15:55:02 +000040
Martin v. Löwis99866332002-03-01 10:27:01 +000041#define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
42#define array_CheckExact(op) ((op)->ob_type == &Arraytype)
Guido van Rossum778983b1993-02-19 15:55:02 +000043
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000044static int
45array_resize(arrayobject *self, int newsize)
46{
47 char *items;
48 size_t _new_size;
49
50 /* Bypass realloc() when a previous overallocation is large enough
51 to accommodate the newsize. If the newsize is 16 smaller than the
52 current size, then proceed with the realloc() to shrink the list.
53 */
54
55 if (self->allocated >= newsize &&
56 self->ob_size < newsize + 16 &&
57 self->ob_item != NULL) {
58 self->ob_size = newsize;
59 return 0;
60 }
61
62 /* This over-allocates proportional to the array size, making room
63 * for additional growth. The over-allocation is mild, but is
64 * enough to give linear-time amortized behavior over a long
65 * sequence of appends() in the presence of a poorly-performing
66 * system realloc().
67 * The growth pattern is: 0, 4, 8, 16, 25, 34, 46, 56, 67, 79, ...
68 * Note, the pattern starts out the same as for lists but then
69 * grows at a smaller rate so that larger arrays only overallocate
70 * by about 1/16th -- this is done because arrays are presumed to be more
71 * memory critical.
72 */
73
74 _new_size = (newsize >> 4) + (self->ob_size < 8 ? 3 : 7) + newsize;
75 items = self->ob_item;
76 /* XXX The following multiplication and division does not optimize away
77 like it does for lists since the size is not known at compile time */
78 if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize))
79 PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize));
80 else
81 items = NULL;
82 if (items == NULL) {
83 PyErr_NoMemory();
84 return -1;
85 }
86 self->ob_item = items;
87 self->ob_size = newsize;
88 self->allocated = _new_size;
89 return 0;
90}
91
Tim Petersbb307342000-09-10 05:22:54 +000092/****************************************************************************
93Get and Set functions for each type.
94A Get function takes an arrayobject* and an integer index, returning the
95array value at that index wrapped in an appropriate PyObject*.
96A Set function takes an arrayobject, integer index, and PyObject*; sets
97the array value at that index to the raw C data extracted from the PyObject*,
98and returns 0 if successful, else nonzero on failure (PyObject* not of an
99appropriate type or value).
100Note that the basic Get and Set functions do NOT check that the index is
101in bounds; that's the responsibility of the caller.
102****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +0000103
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000104static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000105c_getitem(arrayobject *ap, int i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000106{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000107 return PyString_FromStringAndSize(&((char *)ap->ob_item)[i], 1);
Guido van Rossum778983b1993-02-19 15:55:02 +0000108}
109
110static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000111c_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000112{
113 char x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000114 if (!PyArg_Parse(v, "c;array item must be char", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000115 return -1;
116 if (i >= 0)
Martin v. Löwis99866332002-03-01 10:27:01 +0000117 ((char *)ap->ob_item)[i] = x;
Guido van Rossum778983b1993-02-19 15:55:02 +0000118 return 0;
119}
120
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000121static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000122b_getitem(arrayobject *ap, int i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000123{
124 long x = ((char *)ap->ob_item)[i];
125 if (x >= 128)
126 x -= 256;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000127 return PyInt_FromLong(x);
Guido van Rossum778983b1993-02-19 15:55:02 +0000128}
129
130static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000131b_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000132{
Fred Drake541dc3b2000-06-28 17:49:30 +0000133 short x;
134 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
135 must use the next size up that is signed ('h') and manually do
136 the overflow checking */
137 if (!PyArg_Parse(v, "h;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000138 return -1;
Guido van Rossum9f754e02000-07-01 00:38:19 +0000139 else if (x < -128) {
Fred Drake541dc3b2000-06-28 17:49:30 +0000140 PyErr_SetString(PyExc_OverflowError,
141 "signed char is less than minimum");
142 return -1;
143 }
Guido van Rossum9f754e02000-07-01 00:38:19 +0000144 else if (x > 127) {
Fred Drake541dc3b2000-06-28 17:49:30 +0000145 PyErr_SetString(PyExc_OverflowError,
146 "signed char is greater than maximum");
147 return -1;
148 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000149 if (i >= 0)
Fred Drake541dc3b2000-06-28 17:49:30 +0000150 ((char *)ap->ob_item)[i] = (char)x;
Guido van Rossum778983b1993-02-19 15:55:02 +0000151 return 0;
152}
153
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000154static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000155BB_getitem(arrayobject *ap, int i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000156{
157 long x = ((unsigned char *)ap->ob_item)[i];
158 return PyInt_FromLong(x);
159}
160
Fred Drake541dc3b2000-06-28 17:49:30 +0000161static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000162BB_setitem(arrayobject *ap, int i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000163{
164 unsigned char x;
165 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
166 if (!PyArg_Parse(v, "b;array item must be integer", &x))
167 return -1;
168 if (i >= 0)
Martin v. Löwis99866332002-03-01 10:27:01 +0000169 ((char *)ap->ob_item)[i] = x;
Fred Drake541dc3b2000-06-28 17:49:30 +0000170 return 0;
171}
Guido van Rossum549ab711997-01-03 19:09:47 +0000172
Martin v. Löwis99866332002-03-01 10:27:01 +0000173#ifdef Py_USING_UNICODE
174static PyObject *
175u_getitem(arrayobject *ap, int i)
176{
177 return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1);
178}
179
180static int
181u_setitem(arrayobject *ap, int i, PyObject *v)
182{
183 Py_UNICODE *p;
184 int len;
185
186 if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
187 return -1;
188 if (len != 1) {
189 PyErr_SetString(PyExc_TypeError, "array item must be unicode character");
190 return -1;
191 }
192 if (i >= 0)
193 ((Py_UNICODE *)ap->ob_item)[i] = p[0];
194 return 0;
195}
196#endif
197
Guido van Rossum549ab711997-01-03 19:09:47 +0000198static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000199h_getitem(arrayobject *ap, int i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000200{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000201 return PyInt_FromLong((long) ((short *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000202}
203
204static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000205h_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000206{
207 short x;
Fred Drake541dc3b2000-06-28 17:49:30 +0000208 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000209 if (!PyArg_Parse(v, "h;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000210 return -1;
211 if (i >= 0)
212 ((short *)ap->ob_item)[i] = x;
213 return 0;
214}
215
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000216static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000217HH_getitem(arrayobject *ap, int i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000218{
219 return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
220}
221
Fred Drake541dc3b2000-06-28 17:49:30 +0000222static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000223HH_setitem(arrayobject *ap, int i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000224{
225 int x;
226 /* PyArg_Parse's 'h' formatter is for a signed short, therefore
227 must use the next size up and manually do the overflow checking */
228 if (!PyArg_Parse(v, "i;array item must be integer", &x))
229 return -1;
230 else if (x < 0) {
231 PyErr_SetString(PyExc_OverflowError,
232 "unsigned short is less than minimum");
233 return -1;
234 }
235 else if (x > USHRT_MAX) {
236 PyErr_SetString(PyExc_OverflowError,
237 "unsigned short is greater than maximum");
238 return -1;
239 }
240 if (i >= 0)
241 ((short *)ap->ob_item)[i] = (short)x;
242 return 0;
243}
Guido van Rossum549ab711997-01-03 19:09:47 +0000244
245static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000246i_getitem(arrayobject *ap, int i)
Guido van Rossume77a7571993-11-03 15:01:26 +0000247{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000248 return PyInt_FromLong((long) ((int *)ap->ob_item)[i]);
Guido van Rossume77a7571993-11-03 15:01:26 +0000249}
250
251static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000252i_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossume77a7571993-11-03 15:01:26 +0000253{
254 int x;
Fred Drake541dc3b2000-06-28 17:49:30 +0000255 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000256 if (!PyArg_Parse(v, "i;array item must be integer", &x))
Guido van Rossume77a7571993-11-03 15:01:26 +0000257 return -1;
258 if (i >= 0)
259 ((int *)ap->ob_item)[i] = x;
260 return 0;
261}
262
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000263static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000264II_getitem(arrayobject *ap, int i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000265{
266 return PyLong_FromUnsignedLong(
267 (unsigned long) ((unsigned int *)ap->ob_item)[i]);
268}
269
270static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000271II_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000272{
273 unsigned long x;
274 if (PyLong_Check(v)) {
275 x = PyLong_AsUnsignedLong(v);
276 if (x == (unsigned long) -1 && PyErr_Occurred())
277 return -1;
278 }
279 else {
Fred Drake541dc3b2000-06-28 17:49:30 +0000280 long y;
281 if (!PyArg_Parse(v, "l;array item must be integer", &y))
Guido van Rossum549ab711997-01-03 19:09:47 +0000282 return -1;
Fred Drake541dc3b2000-06-28 17:49:30 +0000283 if (y < 0) {
284 PyErr_SetString(PyExc_OverflowError,
285 "unsigned int is less than minimum");
286 return -1;
287 }
288 x = (unsigned long)y;
Tim Petersbb307342000-09-10 05:22:54 +0000289
Guido van Rossum549ab711997-01-03 19:09:47 +0000290 }
Fred Drake541dc3b2000-06-28 17:49:30 +0000291 if (x > UINT_MAX) {
292 PyErr_SetString(PyExc_OverflowError,
293 "unsigned int is greater than maximum");
294 return -1;
295 }
296
Guido van Rossum549ab711997-01-03 19:09:47 +0000297 if (i >= 0)
Fred Drake541dc3b2000-06-28 17:49:30 +0000298 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
Guido van Rossum549ab711997-01-03 19:09:47 +0000299 return 0;
300}
301
302static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000303l_getitem(arrayobject *ap, int i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000304{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000305 return PyInt_FromLong(((long *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000306}
307
308static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000309l_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000310{
311 long x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000312 if (!PyArg_Parse(v, "l;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000313 return -1;
314 if (i >= 0)
315 ((long *)ap->ob_item)[i] = x;
316 return 0;
317}
318
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000319static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000320LL_getitem(arrayobject *ap, int i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000321{
322 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
323}
324
325static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000326LL_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000327{
328 unsigned long x;
329 if (PyLong_Check(v)) {
330 x = PyLong_AsUnsignedLong(v);
331 if (x == (unsigned long) -1 && PyErr_Occurred())
332 return -1;
333 }
334 else {
Fred Drake541dc3b2000-06-28 17:49:30 +0000335 long y;
336 if (!PyArg_Parse(v, "l;array item must be integer", &y))
Guido van Rossum549ab711997-01-03 19:09:47 +0000337 return -1;
Fred Drake541dc3b2000-06-28 17:49:30 +0000338 if (y < 0) {
339 PyErr_SetString(PyExc_OverflowError,
340 "unsigned long is less than minimum");
341 return -1;
342 }
343 x = (unsigned long)y;
Tim Petersbb307342000-09-10 05:22:54 +0000344
Guido van Rossum549ab711997-01-03 19:09:47 +0000345 }
Fred Drake541dc3b2000-06-28 17:49:30 +0000346 if (x > ULONG_MAX) {
347 PyErr_SetString(PyExc_OverflowError,
348 "unsigned long is greater than maximum");
349 return -1;
350 }
Tim Petersbb307342000-09-10 05:22:54 +0000351
Guido van Rossum549ab711997-01-03 19:09:47 +0000352 if (i >= 0)
353 ((unsigned long *)ap->ob_item)[i] = x;
354 return 0;
355}
356
357static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000358f_getitem(arrayobject *ap, int i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000359{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000360 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000361}
362
363static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000364f_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000365{
366 float x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000367 if (!PyArg_Parse(v, "f;array item must be float", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000368 return -1;
369 if (i >= 0)
370 ((float *)ap->ob_item)[i] = x;
371 return 0;
372}
373
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000374static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000375d_getitem(arrayobject *ap, int i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000376{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000377 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000378}
379
380static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000381d_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000382{
383 double x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000384 if (!PyArg_Parse(v, "d;array item must be float", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000385 return -1;
386 if (i >= 0)
387 ((double *)ap->ob_item)[i] = x;
388 return 0;
389}
390
391/* Description of types */
Guido van Rossum234f9421993-06-17 12:35:49 +0000392static struct arraydescr descriptors[] = {
Guido van Rossum778983b1993-02-19 15:55:02 +0000393 {'c', sizeof(char), c_getitem, c_setitem},
394 {'b', sizeof(char), b_getitem, b_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000395 {'B', sizeof(char), BB_getitem, BB_setitem},
Martin v. Löwis99866332002-03-01 10:27:01 +0000396#ifdef Py_USING_UNICODE
397 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem},
398#endif
Guido van Rossum778983b1993-02-19 15:55:02 +0000399 {'h', sizeof(short), h_getitem, h_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000400 {'H', sizeof(short), HH_getitem, HH_setitem},
Guido van Rossume77a7571993-11-03 15:01:26 +0000401 {'i', sizeof(int), i_getitem, i_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000402 {'I', sizeof(int), II_getitem, II_setitem},
Guido van Rossum778983b1993-02-19 15:55:02 +0000403 {'l', sizeof(long), l_getitem, l_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000404 {'L', sizeof(long), LL_getitem, LL_setitem},
Guido van Rossum778983b1993-02-19 15:55:02 +0000405 {'f', sizeof(float), f_getitem, f_setitem},
406 {'d', sizeof(double), d_getitem, d_setitem},
407 {'\0', 0, 0, 0} /* Sentinel */
408};
Tim Petersbb307342000-09-10 05:22:54 +0000409
410/****************************************************************************
411Implementations of array object methods.
412****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +0000413
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000414static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +0000415newarrayobject(PyTypeObject *type, int size, struct arraydescr *descr)
Guido van Rossum778983b1993-02-19 15:55:02 +0000416{
Guido van Rossum778983b1993-02-19 15:55:02 +0000417 arrayobject *op;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000418 size_t nbytes;
Martin v. Löwis99866332002-03-01 10:27:01 +0000419
Guido van Rossum778983b1993-02-19 15:55:02 +0000420 if (size < 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000421 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000422 return NULL;
423 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000424
Guido van Rossum778983b1993-02-19 15:55:02 +0000425 nbytes = size * descr->itemsize;
426 /* Check for overflow */
Guido van Rossum7844e381997-04-11 20:44:04 +0000427 if (nbytes / descr->itemsize != (size_t)size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000428 return PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000429 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000430 op = (arrayobject *) type->tp_alloc(type, 0);
Guido van Rossum778983b1993-02-19 15:55:02 +0000431 if (op == NULL) {
Martin v. Löwis99866332002-03-01 10:27:01 +0000432 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +0000433 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000434 op->ob_size = size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000435 if (size <= 0) {
436 op->ob_item = NULL;
437 }
438 else {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000439 op->ob_item = PyMem_NEW(char, nbytes);
Guido van Rossum778983b1993-02-19 15:55:02 +0000440 if (op->ob_item == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000441 PyObject_Del(op);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000442 return PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000443 }
444 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000445 op->ob_descr = descr;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000446 op->allocated = size;
Raymond Hettingercb87bc82004-05-31 00:35:52 +0000447 op->weakreflist = NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000448 return (PyObject *) op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000449}
450
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000451static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000452getarrayitem(PyObject *op, int i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000453{
454 register arrayobject *ap;
Martin v. Löwis99866332002-03-01 10:27:01 +0000455 assert(array_Check(op));
Guido van Rossum778983b1993-02-19 15:55:02 +0000456 ap = (arrayobject *)op;
Walter Dörwald8bb1ae92003-05-23 10:01:07 +0000457 assert(i>=0 && i<ap->ob_size);
Guido van Rossum778983b1993-02-19 15:55:02 +0000458 return (*ap->ob_descr->getitem)(ap, i);
459}
460
461static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000462ins1(arrayobject *self, int where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000463{
Guido van Rossum778983b1993-02-19 15:55:02 +0000464 char *items;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000465 int n = self->ob_size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000466 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000467 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000468 return -1;
469 }
470 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
471 return -1;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000472
473 if (array_resize(self, n+1) == -1)
Guido van Rossum778983b1993-02-19 15:55:02 +0000474 return -1;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000475 items = self->ob_item;
Walter Dörwald9e46abe2003-05-18 03:15:10 +0000476 if (where < 0) {
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000477 where += n;
Walter Dörwald9e46abe2003-05-18 03:15:10 +0000478 if (where < 0)
479 where = 0;
480 }
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000481 if (where > n)
482 where = n;
483 /* appends don't need to call memmove() */
484 if (where != n)
485 memmove(items + (where+1)*self->ob_descr->itemsize,
486 items + where*self->ob_descr->itemsize,
487 (n-where)*self->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000488 return (*self->ob_descr->setitem)(self, where, v);
489}
490
Guido van Rossum778983b1993-02-19 15:55:02 +0000491/* Methods */
492
493static void
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000494array_dealloc(arrayobject *op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000495{
Raymond Hettingercb87bc82004-05-31 00:35:52 +0000496 if (op->weakreflist != NULL)
497 PyObject_ClearWeakRefs((PyObject *) op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000498 if (op->ob_item != NULL)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000499 PyMem_DEL(op->ob_item);
Martin v. Löwis99866332002-03-01 10:27:01 +0000500 op->ob_type->tp_free((PyObject *)op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000501}
502
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000503static PyObject *
504array_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000505{
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000506 arrayobject *va, *wa;
507 PyObject *vi = NULL;
508 PyObject *wi = NULL;
509 int i, k;
510 PyObject *res;
511
Martin v. Löwis99866332002-03-01 10:27:01 +0000512 if (!array_Check(v) || !array_Check(w)) {
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000513 Py_INCREF(Py_NotImplemented);
514 return Py_NotImplemented;
Guido van Rossum778983b1993-02-19 15:55:02 +0000515 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000516
517 va = (arrayobject *)v;
518 wa = (arrayobject *)w;
519
520 if (va->ob_size != wa->ob_size && (op == Py_EQ || op == Py_NE)) {
521 /* Shortcut: if the lengths differ, the arrays differ */
522 if (op == Py_EQ)
523 res = Py_False;
524 else
525 res = Py_True;
526 Py_INCREF(res);
527 return res;
528 }
529
530 /* Search for the first index where items are different */
531 k = 1;
532 for (i = 0; i < va->ob_size && i < wa->ob_size; i++) {
533 vi = getarrayitem(v, i);
534 wi = getarrayitem(w, i);
535 if (vi == NULL || wi == NULL) {
536 Py_XDECREF(vi);
537 Py_XDECREF(wi);
538 return NULL;
539 }
540 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
541 if (k == 0)
542 break; /* Keeping vi and wi alive! */
543 Py_DECREF(vi);
544 Py_DECREF(wi);
545 if (k < 0)
546 return NULL;
547 }
548
549 if (k) {
550 /* No more items to compare -- compare sizes */
551 int vs = va->ob_size;
552 int ws = wa->ob_size;
553 int cmp;
554 switch (op) {
555 case Py_LT: cmp = vs < ws; break;
Guido van Rossum2b597e42001-01-25 22:12:43 +0000556 case Py_LE: cmp = vs <= ws; break;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000557 case Py_EQ: cmp = vs == ws; break;
558 case Py_NE: cmp = vs != ws; break;
559 case Py_GT: cmp = vs > ws; break;
560 case Py_GE: cmp = vs >= ws; break;
561 default: return NULL; /* cannot happen */
562 }
563 if (cmp)
564 res = Py_True;
565 else
566 res = Py_False;
567 Py_INCREF(res);
568 return res;
569 }
570
571 /* We have an item that differs. First, shortcuts for EQ/NE */
572 if (op == Py_EQ) {
573 Py_INCREF(Py_False);
574 res = Py_False;
575 }
576 else if (op == Py_NE) {
577 Py_INCREF(Py_True);
578 res = Py_True;
579 }
580 else {
581 /* Compare the final item again using the proper operator */
582 res = PyObject_RichCompare(vi, wi, op);
583 }
584 Py_DECREF(vi);
585 Py_DECREF(wi);
586 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +0000587}
588
589static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000590array_length(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +0000591{
592 return a->ob_size;
593}
594
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000595static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000596array_item(arrayobject *a, int i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000597{
598 if (i < 0 || i >= a->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000599 PyErr_SetString(PyExc_IndexError, "array index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000600 return NULL;
601 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000602 return getarrayitem((PyObject *)a, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000603}
604
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000605static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000606array_slice(arrayobject *a, int ilow, int ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000607{
608 arrayobject *np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000609 if (ilow < 0)
610 ilow = 0;
611 else if (ilow > a->ob_size)
612 ilow = a->ob_size;
613 if (ihigh < 0)
614 ihigh = 0;
615 if (ihigh < ilow)
616 ihigh = ilow;
617 else if (ihigh > a->ob_size)
618 ihigh = a->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000619 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
Guido van Rossum778983b1993-02-19 15:55:02 +0000620 if (np == NULL)
621 return NULL;
622 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
623 (ihigh-ilow) * a->ob_descr->itemsize);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000624 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000625}
626
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000627static PyObject *
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000628array_copy(arrayobject *a, PyObject *unused)
629{
630 return array_slice(a, 0, a->ob_size);
631}
632
633PyDoc_STRVAR(copy_doc,
634"copy(array)\n\
635\n\
636 Return a copy of the array.");
637
638static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000639array_concat(arrayobject *a, PyObject *bb)
Guido van Rossum778983b1993-02-19 15:55:02 +0000640{
641 int size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000642 arrayobject *np;
Martin v. Löwis99866332002-03-01 10:27:01 +0000643 if (!array_Check(bb)) {
Fred Drake137507e2000-06-01 02:02:46 +0000644 PyErr_Format(PyExc_TypeError,
645 "can only append array (not \"%.200s\") to array",
646 bb->ob_type->tp_name);
Guido van Rossum778983b1993-02-19 15:55:02 +0000647 return NULL;
648 }
649#define b ((arrayobject *)bb)
650 if (a->ob_descr != b->ob_descr) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000651 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000652 return NULL;
653 }
654 size = a->ob_size + b->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000655 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
Guido van Rossum778983b1993-02-19 15:55:02 +0000656 if (np == NULL) {
657 return NULL;
658 }
659 memcpy(np->ob_item, a->ob_item, a->ob_size*a->ob_descr->itemsize);
660 memcpy(np->ob_item + a->ob_size*a->ob_descr->itemsize,
Guido van Rossum32be3a71993-11-05 10:16:27 +0000661 b->ob_item, b->ob_size*b->ob_descr->itemsize);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000662 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000663#undef b
664}
665
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000666static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000667array_repeat(arrayobject *a, int n)
Guido van Rossum778983b1993-02-19 15:55:02 +0000668{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000669 int i;
Guido van Rossum778983b1993-02-19 15:55:02 +0000670 int size;
671 arrayobject *np;
672 char *p;
673 int nbytes;
674 if (n < 0)
675 n = 0;
676 size = a->ob_size * n;
Martin v. Löwis99866332002-03-01 10:27:01 +0000677 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
Guido van Rossum778983b1993-02-19 15:55:02 +0000678 if (np == NULL)
679 return NULL;
680 p = np->ob_item;
681 nbytes = a->ob_size * a->ob_descr->itemsize;
682 for (i = 0; i < n; i++) {
683 memcpy(p, a->ob_item, nbytes);
684 p += nbytes;
685 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000686 return (PyObject *) np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000687}
688
689static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000690array_ass_slice(arrayobject *a, int ilow, int ihigh, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000691{
692 char *item;
693 int n; /* Size of replacement array */
694 int d; /* Change in size */
Guido van Rossum778983b1993-02-19 15:55:02 +0000695#define b ((arrayobject *)v)
696 if (v == NULL)
697 n = 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000698 else if (array_Check(v)) {
Guido van Rossum778983b1993-02-19 15:55:02 +0000699 n = b->ob_size;
700 if (a == b) {
701 /* Special case "a[i:j] = a" -- copy b first */
702 int ret;
703 v = array_slice(b, 0, n);
704 ret = array_ass_slice(a, ilow, ihigh, v);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000705 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000706 return ret;
707 }
708 if (b->ob_descr != a->ob_descr) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000709 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000710 return -1;
711 }
712 }
713 else {
Fred Drake137507e2000-06-01 02:02:46 +0000714 PyErr_Format(PyExc_TypeError,
715 "can only assign array (not \"%.200s\") to array slice",
716 v->ob_type->tp_name);
Guido van Rossum778983b1993-02-19 15:55:02 +0000717 return -1;
718 }
719 if (ilow < 0)
720 ilow = 0;
721 else if (ilow > a->ob_size)
722 ilow = a->ob_size;
723 if (ihigh < 0)
724 ihigh = 0;
725 if (ihigh < ilow)
726 ihigh = ilow;
727 else if (ihigh > a->ob_size)
728 ihigh = a->ob_size;
729 item = a->ob_item;
730 d = n - (ihigh-ilow);
731 if (d < 0) { /* Delete -d items */
732 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
733 item + ihigh*a->ob_descr->itemsize,
734 (a->ob_size-ihigh)*a->ob_descr->itemsize);
735 a->ob_size += d;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000736 PyMem_RESIZE(item, char, a->ob_size*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000737 /* Can't fail */
738 a->ob_item = item;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000739 a->allocated = a->ob_size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000740 }
741 else if (d > 0) { /* Insert d items */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000742 PyMem_RESIZE(item, char,
743 (a->ob_size + d)*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000744 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000745 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000746 return -1;
747 }
748 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
749 item + ihigh*a->ob_descr->itemsize,
750 (a->ob_size-ihigh)*a->ob_descr->itemsize);
751 a->ob_item = item;
752 a->ob_size += d;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000753 a->allocated = a->ob_size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000754 }
755 if (n > 0)
756 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
757 n*b->ob_descr->itemsize);
758 return 0;
759#undef b
760}
761
762static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000763array_ass_item(arrayobject *a, int i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000764{
765 if (i < 0 || i >= a->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000766 PyErr_SetString(PyExc_IndexError,
767 "array assignment index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000768 return -1;
769 }
770 if (v == NULL)
771 return array_ass_slice(a, i, i+1, v);
772 return (*a->ob_descr->setitem)(a, i, v);
773}
774
775static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000776setarrayitem(PyObject *a, int i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000777{
Martin v. Löwis99866332002-03-01 10:27:01 +0000778 assert(array_Check(a));
Guido van Rossum778983b1993-02-19 15:55:02 +0000779 return array_ass_item((arrayobject *)a, i, v);
780}
781
Martin v. Löwis99866332002-03-01 10:27:01 +0000782static int
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000783array_iter_extend(arrayobject *self, PyObject *bb)
784{
785 PyObject *it, *v;
786
787 it = PyObject_GetIter(bb);
788 if (it == NULL)
789 return -1;
790
791 while ((v = PyIter_Next(it)) != NULL) {
792 if (ins1(self, (int) self->ob_size, v) != 0) {
793 Py_DECREF(v);
794 Py_DECREF(it);
795 return -1;
796 }
797 Py_DECREF(v);
798 }
799 Py_DECREF(it);
800 if (PyErr_Occurred())
801 return -1;
802 return 0;
803}
804
805static int
Martin v. Löwis99866332002-03-01 10:27:01 +0000806array_do_extend(arrayobject *self, PyObject *bb)
807{
808 int size;
809
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000810 if (!array_Check(bb))
811 return array_iter_extend(self, bb);
Martin v. Löwis99866332002-03-01 10:27:01 +0000812#define b ((arrayobject *)bb)
813 if (self->ob_descr != b->ob_descr) {
814 PyErr_SetString(PyExc_TypeError,
815 "can only extend with array of same kind");
816 return -1;
817 }
818 size = self->ob_size + b->ob_size;
819 PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize);
820 if (self->ob_item == NULL) {
821 PyObject_Del(self);
822 PyErr_NoMemory();
823 return -1;
824 }
825 memcpy(self->ob_item + self->ob_size*self->ob_descr->itemsize,
826 b->ob_item, b->ob_size*b->ob_descr->itemsize);
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000827 self->ob_size = size;
828 self->allocated = size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000829
830 return 0;
831#undef b
832}
833
834static PyObject *
835array_inplace_concat(arrayobject *self, PyObject *bb)
836{
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000837 if (!array_Check(bb)) {
838 PyErr_Format(PyExc_TypeError,
839 "can only extend array with array (not \"%.200s\")",
840 bb->ob_type->tp_name);
841 return NULL;
842 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000843 if (array_do_extend(self, bb) == -1)
844 return NULL;
845 Py_INCREF(self);
846 return (PyObject *)self;
847}
848
849static PyObject *
850array_inplace_repeat(arrayobject *self, int n)
851{
852 char *items, *p;
853 int size, i;
854
855 if (self->ob_size > 0) {
856 if (n < 0)
857 n = 0;
858 items = self->ob_item;
859 size = self->ob_size * self->ob_descr->itemsize;
860 if (n == 0) {
861 PyMem_FREE(items);
862 self->ob_item = NULL;
863 self->ob_size = 0;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000864 self->allocated = 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000865 }
866 else {
867 PyMem_Resize(items, char, n * size);
868 if (items == NULL)
869 return PyErr_NoMemory();
870 p = items;
871 for (i = 1; i < n; i++) {
872 p += size;
873 memcpy(p, items, size);
874 }
875 self->ob_item = items;
876 self->ob_size *= n;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000877 self->allocated = self->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000878 }
879 }
880 Py_INCREF(self);
881 return (PyObject *)self;
882}
883
884
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000885static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000886ins(arrayobject *self, int where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000887{
888 if (ins1(self, where, v) != 0)
889 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000890 Py_INCREF(Py_None);
891 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000892}
893
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000894static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000895array_count(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000896{
897 int count = 0;
898 int i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000899
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000900 for (i = 0; i < self->ob_size; i++) {
901 PyObject *selfi = getarrayitem((PyObject *)self, i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000902 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000903 Py_DECREF(selfi);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000904 if (cmp > 0)
905 count++;
906 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000907 return NULL;
908 }
909 return PyInt_FromLong((long)count);
910}
911
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000912PyDoc_STRVAR(count_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000913"count(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000914\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000915Return number of occurences of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000916
917static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000918array_index(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000919{
920 int i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000921
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000922 for (i = 0; i < self->ob_size; i++) {
923 PyObject *selfi = getarrayitem((PyObject *)self, i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000924 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
925 Py_DECREF(selfi);
926 if (cmp > 0) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000927 return PyInt_FromLong((long)i);
928 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000929 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000930 return NULL;
931 }
932 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
933 return NULL;
934}
935
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000936PyDoc_STRVAR(index_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000937"index(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000938\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000939Return index of first occurence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000940
Raymond Hettinger625812f2003-01-07 01:58:52 +0000941static int
942array_contains(arrayobject *self, PyObject *v)
943{
944 int i, cmp;
945
946 for (i = 0, cmp = 0 ; cmp == 0 && i < self->ob_size; i++) {
947 PyObject *selfi = getarrayitem((PyObject *)self, i);
948 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
949 Py_DECREF(selfi);
950 }
951 return cmp;
952}
953
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000954static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000955array_remove(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000956{
957 int i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000958
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000959 for (i = 0; i < self->ob_size; i++) {
960 PyObject *selfi = getarrayitem((PyObject *)self,i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000961 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
962 Py_DECREF(selfi);
963 if (cmp > 0) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000964 if (array_ass_slice(self, i, i+1,
965 (PyObject *)NULL) != 0)
966 return NULL;
967 Py_INCREF(Py_None);
968 return Py_None;
969 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000970 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000971 return NULL;
972 }
973 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
974 return NULL;
975}
976
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000977PyDoc_STRVAR(remove_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000978"remove(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000979\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000980Remove the first occurence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000981
982static PyObject *
983array_pop(arrayobject *self, PyObject *args)
984{
985 int i = -1;
986 PyObject *v;
987 if (!PyArg_ParseTuple(args, "|i:pop", &i))
988 return NULL;
989 if (self->ob_size == 0) {
990 /* Special-case most common failure cause */
991 PyErr_SetString(PyExc_IndexError, "pop from empty array");
992 return NULL;
993 }
994 if (i < 0)
995 i += self->ob_size;
996 if (i < 0 || i >= self->ob_size) {
997 PyErr_SetString(PyExc_IndexError, "pop index out of range");
998 return NULL;
999 }
1000 v = getarrayitem((PyObject *)self,i);
1001 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
1002 Py_DECREF(v);
1003 return NULL;
1004 }
1005 return v;
1006}
1007
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001008PyDoc_STRVAR(pop_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001009"pop([i])\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001010\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001011Return the i-th element and delete it from the array. i defaults to -1.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001012
1013static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001014array_extend(arrayobject *self, PyObject *bb)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001015{
Martin v. Löwis99866332002-03-01 10:27:01 +00001016 if (array_do_extend(self, bb) == -1)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001017 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001018 Py_INCREF(Py_None);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001019 return Py_None;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001020}
1021
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001022PyDoc_STRVAR(extend_doc,
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001023"extend(array or iterable)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001024\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001025 Append items to the end of the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001026
1027static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001028array_insert(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001029{
1030 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001031 PyObject *v;
Peter Schneider-Kampdc71cac2000-07-31 21:57:30 +00001032 if (!PyArg_ParseTuple(args, "iO:insert", &i, &v))
Guido van Rossum778983b1993-02-19 15:55:02 +00001033 return NULL;
1034 return ins(self, i, v);
1035}
1036
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001037PyDoc_STRVAR(insert_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001038"insert(i,x)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001039\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001040Insert a new item x into the array before position i.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001041
1042
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001043static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001044array_buffer_info(arrayobject *self, PyObject *unused)
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001045{
Tim Peters077a11d2000-09-16 22:31:29 +00001046 PyObject* retval = NULL;
Tim Peters077a11d2000-09-16 22:31:29 +00001047 retval = PyTuple_New(2);
1048 if (!retval)
1049 return NULL;
Fred Drake541dc3b2000-06-28 17:49:30 +00001050
1051 PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item));
1052 PyTuple_SET_ITEM(retval, 1, PyInt_FromLong((long)(self->ob_size)));
1053
1054 return retval;
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001055}
1056
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001057PyDoc_STRVAR(buffer_info_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001058"buffer_info() -> (address, length)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001059\n\
1060Return a tuple (address, length) giving the current memory address and\n\
Guido van Rossum702d08e2001-07-27 16:05:32 +00001061the length in items of the buffer used to hold array's contents\n\
1062The length should be multiplied by the itemsize attribute to calculate\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001063the buffer length in bytes.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001064
1065
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001066static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001067array_append(arrayobject *self, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +00001068{
Guido van Rossum778983b1993-02-19 15:55:02 +00001069 return ins(self, (int) self->ob_size, v);
1070}
1071
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001072PyDoc_STRVAR(append_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001073"append(x)\n\
1074\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001075Append new value x to the end of the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001076
1077
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001078static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001079array_byteswap(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001080{
1081 char *p;
1082 int i;
Fred Drakebf272981999-12-03 17:15:30 +00001083
Guido van Rossum778983b1993-02-19 15:55:02 +00001084 switch (self->ob_descr->itemsize) {
1085 case 1:
1086 break;
1087 case 2:
1088 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 2) {
1089 char p0 = p[0];
1090 p[0] = p[1];
1091 p[1] = p0;
1092 }
1093 break;
1094 case 4:
1095 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 4) {
1096 char p0 = p[0];
1097 char p1 = p[1];
1098 p[0] = p[3];
1099 p[1] = p[2];
1100 p[2] = p1;
1101 p[3] = p0;
1102 }
1103 break;
Guido van Rossume77a7571993-11-03 15:01:26 +00001104 case 8:
1105 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 8) {
1106 char p0 = p[0];
1107 char p1 = p[1];
1108 char p2 = p[2];
1109 char p3 = p[3];
1110 p[0] = p[7];
1111 p[1] = p[6];
1112 p[2] = p[5];
1113 p[3] = p[4];
1114 p[4] = p3;
1115 p[5] = p2;
1116 p[6] = p1;
1117 p[7] = p0;
1118 }
1119 break;
Guido van Rossum778983b1993-02-19 15:55:02 +00001120 default:
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001121 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossum778983b1993-02-19 15:55:02 +00001122 "don't know how to byteswap this array type");
1123 return NULL;
1124 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001125 Py_INCREF(Py_None);
1126 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001127}
1128
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001129PyDoc_STRVAR(byteswap_doc,
Fred Drakebf272981999-12-03 17:15:30 +00001130"byteswap()\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001131\n\
Fred Drakebf272981999-12-03 17:15:30 +00001132Byteswap all items of the array. If the items in the array are not 1, 2,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011334, or 8 bytes in size, RuntimeError is raised.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001134
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001135static PyObject *
Raymond Hettingerb0900e62004-12-16 16:23:40 +00001136array_reduce(arrayobject *array)
1137{
1138 PyObject *dict, *result;
1139
1140 dict = PyObject_GetAttrString((PyObject *)array, "__dict__");
1141 if (dict == NULL) {
1142 PyErr_Clear();
1143 dict = Py_None;
1144 Py_INCREF(dict);
1145 }
1146 result = Py_BuildValue("O(cs#)O",
1147 array->ob_type,
1148 array->ob_descr->typecode,
1149 array->ob_item,
1150 array->ob_size * array->ob_descr->itemsize,
1151 dict);
1152 Py_DECREF(dict);
1153 return result;
1154}
1155
1156PyDoc_STRVAR(array_doc, "Return state information for pickling.");
1157
1158static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001159array_reverse(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001160{
Guido van Rossume77a7571993-11-03 15:01:26 +00001161 register int itemsize = self->ob_descr->itemsize;
1162 register char *p, *q;
Tim Peters077a11d2000-09-16 22:31:29 +00001163 /* little buffer to hold items while swapping */
1164 char tmp[256]; /* 8 is probably enough -- but why skimp */
1165 assert(itemsize <= sizeof(tmp));
Guido van Rossume77a7571993-11-03 15:01:26 +00001166
Guido van Rossum778983b1993-02-19 15:55:02 +00001167 if (self->ob_size > 1) {
Guido van Rossume77a7571993-11-03 15:01:26 +00001168 for (p = self->ob_item,
1169 q = self->ob_item + (self->ob_size - 1)*itemsize;
1170 p < q;
1171 p += itemsize, q -= itemsize) {
Tim Peters077a11d2000-09-16 22:31:29 +00001172 /* memory areas guaranteed disjoint, so memcpy
1173 * is safe (& memmove may be slower).
1174 */
1175 memcpy(tmp, p, itemsize);
1176 memcpy(p, q, itemsize);
1177 memcpy(q, tmp, itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001178 }
1179 }
Tim Petersbb307342000-09-10 05:22:54 +00001180
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001181 Py_INCREF(Py_None);
1182 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001183}
Guido van Rossume77a7571993-11-03 15:01:26 +00001184
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001185PyDoc_STRVAR(reverse_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001186"reverse()\n\
1187\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001188Reverse the order of the items in the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001189
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001190static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001191array_fromfile(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001192{
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001193 PyObject *f;
Guido van Rossum778983b1993-02-19 15:55:02 +00001194 int n;
1195 FILE *fp;
Peter Schneider-Kampdc71cac2000-07-31 21:57:30 +00001196 if (!PyArg_ParseTuple(args, "Oi:fromfile", &f, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +00001197 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001198 fp = PyFile_AsFile(f);
Guido van Rossum778983b1993-02-19 15:55:02 +00001199 if (fp == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001200 PyErr_SetString(PyExc_TypeError, "arg1 must be open file");
Guido van Rossum778983b1993-02-19 15:55:02 +00001201 return NULL;
1202 }
1203 if (n > 0) {
1204 char *item = self->ob_item;
1205 int itemsize = self->ob_descr->itemsize;
Guido van Rossum7d0ae5e2000-06-28 21:27:21 +00001206 size_t nread;
Guido van Rossum3791b0d1999-02-23 18:05:22 +00001207 int newlength;
1208 size_t newbytes;
1209 /* Be careful here about overflow */
1210 if ((newlength = self->ob_size + n) <= 0 ||
Guido van Rossum481ac881999-03-19 21:50:11 +00001211 (newbytes = newlength * itemsize) / itemsize !=
1212 (size_t)newlength)
Guido van Rossum3791b0d1999-02-23 18:05:22 +00001213 goto nomem;
1214 PyMem_RESIZE(item, char, newbytes);
Guido van Rossum778983b1993-02-19 15:55:02 +00001215 if (item == NULL) {
Guido van Rossum3791b0d1999-02-23 18:05:22 +00001216 nomem:
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001217 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001218 return NULL;
1219 }
1220 self->ob_item = item;
1221 self->ob_size += n;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +00001222 self->allocated = self->ob_size;
Guido van Rossum778983b1993-02-19 15:55:02 +00001223 nread = fread(item + (self->ob_size - n) * itemsize,
1224 itemsize, n, fp);
Guido van Rossum7d0ae5e2000-06-28 21:27:21 +00001225 if (nread < (size_t)n) {
Guido van Rossum778983b1993-02-19 15:55:02 +00001226 self->ob_size -= (n - nread);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001227 PyMem_RESIZE(item, char, self->ob_size*itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001228 self->ob_item = item;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +00001229 self->allocated = self->ob_size;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001230 PyErr_SetString(PyExc_EOFError,
1231 "not enough items in file");
Guido van Rossum778983b1993-02-19 15:55:02 +00001232 return NULL;
1233 }
1234 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001235 Py_INCREF(Py_None);
1236 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001237}
1238
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001239PyDoc_STRVAR(fromfile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001240"fromfile(f, n)\n\
1241\n\
1242Read 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 +00001243array. Also called as read.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001244
1245
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001246static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001247array_tofile(arrayobject *self, PyObject *f)
Guido van Rossum778983b1993-02-19 15:55:02 +00001248{
Guido van Rossum778983b1993-02-19 15:55:02 +00001249 FILE *fp;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001250
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001251 fp = PyFile_AsFile(f);
Guido van Rossum778983b1993-02-19 15:55:02 +00001252 if (fp == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001253 PyErr_SetString(PyExc_TypeError, "arg must be open file");
Guido van Rossum778983b1993-02-19 15:55:02 +00001254 return NULL;
1255 }
1256 if (self->ob_size > 0) {
Trent Mick6c116dd2000-08-12 20:58:11 +00001257 if (fwrite(self->ob_item, self->ob_descr->itemsize,
1258 self->ob_size, fp) != (size_t)self->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001259 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum778983b1993-02-19 15:55:02 +00001260 clearerr(fp);
1261 return NULL;
1262 }
1263 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001264 Py_INCREF(Py_None);
1265 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001266}
1267
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001268PyDoc_STRVAR(tofile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001269"tofile(f)\n\
1270\n\
1271Write all items (as machine values) to the file object f. Also called as\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001272write.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001273
1274
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001275static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001276array_fromlist(arrayobject *self, PyObject *list)
Guido van Rossum778983b1993-02-19 15:55:02 +00001277{
1278 int n;
Guido van Rossum778983b1993-02-19 15:55:02 +00001279 int itemsize = self->ob_descr->itemsize;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001280
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001281 if (!PyList_Check(list)) {
1282 PyErr_SetString(PyExc_TypeError, "arg must be list");
Guido van Rossum778983b1993-02-19 15:55:02 +00001283 return NULL;
1284 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001285 n = PyList_Size(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001286 if (n > 0) {
1287 char *item = self->ob_item;
1288 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001289 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001290 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001291 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001292 return NULL;
1293 }
1294 self->ob_item = item;
1295 self->ob_size += n;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +00001296 self->allocated = self->ob_size;
Guido van Rossum778983b1993-02-19 15:55:02 +00001297 for (i = 0; i < n; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001298 PyObject *v = PyList_GetItem(list, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001299 if ((*self->ob_descr->setitem)(self,
1300 self->ob_size - n + i, v) != 0) {
1301 self->ob_size -= n;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001302 PyMem_RESIZE(item, char,
1303 self->ob_size * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001304 self->ob_item = item;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +00001305 self->allocated = self->ob_size;
Guido van Rossum778983b1993-02-19 15:55:02 +00001306 return NULL;
1307 }
1308 }
1309 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001310 Py_INCREF(Py_None);
1311 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001312}
1313
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001314PyDoc_STRVAR(fromlist_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001315"fromlist(list)\n\
1316\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001317Append items to array from list.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001318
1319
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001320static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001321array_tolist(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001322{
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001323 PyObject *list = PyList_New(self->ob_size);
Guido van Rossum778983b1993-02-19 15:55:02 +00001324 int i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001325
Guido van Rossum778983b1993-02-19 15:55:02 +00001326 if (list == NULL)
1327 return NULL;
1328 for (i = 0; i < self->ob_size; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001329 PyObject *v = getarrayitem((PyObject *)self, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001330 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001331 Py_DECREF(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001332 return NULL;
1333 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001334 PyList_SetItem(list, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001335 }
1336 return list;
1337}
1338
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001339PyDoc_STRVAR(tolist_doc,
Guido van Rossumfc6aba51998-10-14 02:52:31 +00001340"tolist() -> list\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001341\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001342Convert array to an ordinary list with the same items.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001343
1344
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001345static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001346array_fromstring(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001347{
1348 char *str;
1349 int n;
1350 int itemsize = self->ob_descr->itemsize;
Peter Schneider-Kampdc71cac2000-07-31 21:57:30 +00001351 if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +00001352 return NULL;
1353 if (n % itemsize != 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001354 PyErr_SetString(PyExc_ValueError,
Guido van Rossum778983b1993-02-19 15:55:02 +00001355 "string length not a multiple of item size");
1356 return NULL;
1357 }
1358 n = n / itemsize;
1359 if (n > 0) {
1360 char *item = self->ob_item;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001361 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001362 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001363 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001364 return NULL;
1365 }
1366 self->ob_item = item;
1367 self->ob_size += n;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +00001368 self->allocated = self->ob_size;
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001369 memcpy(item + (self->ob_size - n) * itemsize,
1370 str, itemsize*n);
Guido van Rossum778983b1993-02-19 15:55:02 +00001371 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001372 Py_INCREF(Py_None);
1373 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001374}
1375
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001376PyDoc_STRVAR(fromstring_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001377"fromstring(string)\n\
1378\n\
1379Appends items from the string, interpreting it as an array of machine\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001380values,as if it had been read from a file using the fromfile() method).");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001381
1382
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001383static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001384array_tostring(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001385{
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001386 return PyString_FromStringAndSize(self->ob_item,
Guido van Rossum778983b1993-02-19 15:55:02 +00001387 self->ob_size * self->ob_descr->itemsize);
1388}
1389
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001390PyDoc_STRVAR(tostring_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001391"tostring() -> string\n\
1392\n\
1393Convert the array to an array of machine values and return the string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001394representation.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001395
Martin v. Löwis99866332002-03-01 10:27:01 +00001396
1397
1398#ifdef Py_USING_UNICODE
1399static PyObject *
1400array_fromunicode(arrayobject *self, PyObject *args)
1401{
1402 Py_UNICODE *ustr;
1403 int n;
1404
1405 if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
1406 return NULL;
1407 if (self->ob_descr->typecode != 'u') {
1408 PyErr_SetString(PyExc_ValueError,
1409 "fromunicode() may only be called on "
1410 "type 'u' arrays");
1411 return NULL;
1412 }
1413 if (n > 0) {
1414 Py_UNICODE *item = (Py_UNICODE *) self->ob_item;
1415 PyMem_RESIZE(item, Py_UNICODE, self->ob_size + n);
1416 if (item == NULL) {
1417 PyErr_NoMemory();
1418 return NULL;
1419 }
1420 self->ob_item = (char *) item;
1421 self->ob_size += n;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +00001422 self->allocated = self->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +00001423 memcpy(item + self->ob_size - n,
1424 ustr, n * sizeof(Py_UNICODE));
1425 }
1426
1427 Py_INCREF(Py_None);
1428 return Py_None;
1429}
1430
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001431PyDoc_STRVAR(fromunicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001432"fromunicode(ustr)\n\
1433\n\
1434Extends this array with data from the unicode string ustr.\n\
1435The array must be a type 'u' array; otherwise a ValueError\n\
1436is raised. Use array.fromstring(ustr.decode(...)) to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001437append Unicode data to an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001438
1439
1440static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001441array_tounicode(arrayobject *self, PyObject *unused)
Martin v. Löwis99866332002-03-01 10:27:01 +00001442{
Martin v. Löwis99866332002-03-01 10:27:01 +00001443 if (self->ob_descr->typecode != 'u') {
1444 PyErr_SetString(PyExc_ValueError,
1445 "tounicode() may only be called on type 'u' arrays");
1446 return NULL;
1447 }
1448 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, self->ob_size);
1449}
1450
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001451PyDoc_STRVAR(tounicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001452"tounicode() -> unicode\n\
1453\n\
1454Convert the array to a unicode string. The array must be\n\
1455a type 'u' array; otherwise a ValueError is raised. Use\n\
1456array.tostring().decode() to obtain a unicode string from\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001457an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001458
1459#endif /* Py_USING_UNICODE */
1460
1461
1462static PyObject *
1463array_get_typecode(arrayobject *a, void *closure)
1464{
1465 char tc = a->ob_descr->typecode;
1466 return PyString_FromStringAndSize(&tc, 1);
1467}
1468
1469static PyObject *
1470array_get_itemsize(arrayobject *a, void *closure)
1471{
1472 return PyInt_FromLong((long)a->ob_descr->itemsize);
1473}
1474
1475static PyGetSetDef array_getsets [] = {
1476 {"typecode", (getter) array_get_typecode, NULL,
1477 "the typecode character used to create the array"},
1478 {"itemsize", (getter) array_get_itemsize, NULL,
1479 "the size, in bytes, of one array item"},
1480 {NULL}
1481};
1482
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001483PyMethodDef array_methods[] = {
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001484 {"append", (PyCFunction)array_append, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001485 append_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001486 {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001487 buffer_info_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001488 {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001489 byteswap_doc},
Raymond Hettinger3aa82c02004-03-13 18:18:51 +00001490 {"__copy__", (PyCFunction)array_copy, METH_NOARGS,
1491 copy_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001492 {"count", (PyCFunction)array_count, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001493 count_doc},
Raymond Hettinger3aa82c02004-03-13 18:18:51 +00001494 {"__deepcopy__",(PyCFunction)array_copy, METH_NOARGS,
1495 copy_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001496 {"extend", (PyCFunction)array_extend, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001497 extend_doc},
1498 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
1499 fromfile_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001500 {"fromlist", (PyCFunction)array_fromlist, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001501 fromlist_doc},
1502 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
1503 fromstring_doc},
Martin v. Löwis99866332002-03-01 10:27:01 +00001504#ifdef Py_USING_UNICODE
1505 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
1506 fromunicode_doc},
1507#endif
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001508 {"index", (PyCFunction)array_index, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001509 index_doc},
1510 {"insert", (PyCFunction)array_insert, METH_VARARGS,
1511 insert_doc},
1512 {"pop", (PyCFunction)array_pop, METH_VARARGS,
1513 pop_doc},
1514 {"read", (PyCFunction)array_fromfile, METH_VARARGS,
1515 fromfile_doc},
Raymond Hettingerb0900e62004-12-16 16:23:40 +00001516 {"__reduce__", (PyCFunction)array_reduce, METH_NOARGS,
1517 array_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001518 {"remove", (PyCFunction)array_remove, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001519 remove_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001520 {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001521 reverse_doc},
1522/* {"sort", (PyCFunction)array_sort, METH_VARARGS,
1523 sort_doc},*/
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001524 {"tofile", (PyCFunction)array_tofile, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001525 tofile_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001526 {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001527 tolist_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001528 {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001529 tostring_doc},
Martin v. Löwis99866332002-03-01 10:27:01 +00001530#ifdef Py_USING_UNICODE
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001531 {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
Martin v. Löwis99866332002-03-01 10:27:01 +00001532 tounicode_doc},
1533#endif
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001534 {"write", (PyCFunction)array_tofile, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001535 tofile_doc},
Guido van Rossum778983b1993-02-19 15:55:02 +00001536 {NULL, NULL} /* sentinel */
1537};
1538
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001539static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001540array_repr(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +00001541{
Martin v. Löwis99866332002-03-01 10:27:01 +00001542 char buf[256], typecode;
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001543 PyObject *s, *t, *v = NULL;
1544 int len;
Martin v. Löwis99866332002-03-01 10:27:01 +00001545
Guido van Rossum778983b1993-02-19 15:55:02 +00001546 len = a->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +00001547 typecode = a->ob_descr->typecode;
Guido van Rossum778983b1993-02-19 15:55:02 +00001548 if (len == 0) {
Martin v. Löwis99866332002-03-01 10:27:01 +00001549 PyOS_snprintf(buf, sizeof(buf), "array('%c')", typecode);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001550 return PyString_FromString(buf);
Guido van Rossum778983b1993-02-19 15:55:02 +00001551 }
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001552
1553 if (typecode == 'c')
1554 v = array_tostring(a, NULL);
Michael W. Hudson1755ad92002-05-13 10:14:59 +00001555#ifdef Py_USING_UNICODE
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001556 else if (typecode == 'u')
1557 v = array_tounicode(a, NULL);
Michael W. Hudson1755ad92002-05-13 10:14:59 +00001558#endif
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001559 else
1560 v = array_tolist(a, NULL);
1561 t = PyObject_Repr(v);
1562 Py_XDECREF(v);
1563
1564 PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001565 s = PyString_FromString(buf);
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001566 PyString_ConcatAndDel(&s, t);
1567 PyString_ConcatAndDel(&s, PyString_FromString(")"));
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{
1574 if (PyInt_Check(item)) {
1575 long i = PyInt_AS_LONG(item);
1576 if (i < 0)
1577 i += self->ob_size;
1578 return array_item(self, i);
1579 }
1580 else if (PyLong_Check(item)) {
1581 long i = PyLong_AsLong(item);
1582 if (i == -1 && PyErr_Occurred())
1583 return NULL;
1584 if (i < 0)
1585 i += self->ob_size;
1586 return array_item(self, i);
1587 }
1588 else if (PySlice_Check(item)) {
1589 int start, stop, step, slicelength, cur, i;
1590 PyObject* result;
1591 arrayobject* ar;
1592 int itemsize = self->ob_descr->itemsize;
1593
1594 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
1595 &start, &stop, &step, &slicelength) < 0) {
1596 return NULL;
1597 }
1598
1599 if (slicelength <= 0) {
1600 return newarrayobject(&Arraytype, 0, self->ob_descr);
1601 }
1602 else {
1603 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
1604 if (!result) return NULL;
1605
1606 ar = (arrayobject*)result;
1607
1608 for (cur = start, i = 0; i < slicelength;
1609 cur += step, i++) {
1610 memcpy(ar->ob_item + i*itemsize,
1611 self->ob_item + cur*itemsize,
1612 itemsize);
1613 }
1614
1615 return result;
1616 }
1617 }
1618 else {
1619 PyErr_SetString(PyExc_TypeError,
1620 "list indices must be integers");
1621 return NULL;
1622 }
1623}
1624
1625static int
1626array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
1627{
1628 if (PyInt_Check(item)) {
1629 long i = PyInt_AS_LONG(item);
1630 if (i < 0)
1631 i += self->ob_size;
1632 return array_ass_item(self, i, value);
1633 }
1634 else if (PyLong_Check(item)) {
1635 long i = PyLong_AsLong(item);
1636 if (i == -1 && PyErr_Occurred())
1637 return -1;
1638 if (i < 0)
1639 i += self->ob_size;
1640 return array_ass_item(self, i, value);
1641 }
1642 else if (PySlice_Check(item)) {
1643 int start, stop, step, slicelength;
1644 int itemsize = self->ob_descr->itemsize;
1645
1646 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
1647 &start, &stop, &step, &slicelength) < 0) {
1648 return -1;
1649 }
1650
1651 /* treat A[slice(a,b)] = v _exactly_ like A[a:b] = v */
1652 if (step == 1 && ((PySliceObject*)item)->step == Py_None)
1653 return array_ass_slice(self, start, stop, value);
1654
1655 if (value == NULL) {
1656 /* delete slice */
Michael W. Hudson56796f62002-07-29 14:35:04 +00001657 int cur, i, extra;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001658
1659 if (slicelength <= 0)
1660 return 0;
1661
1662 if (step < 0) {
1663 stop = start + 1;
1664 start = stop + step*(slicelength - 1) - 1;
1665 step = -step;
1666 }
1667
Michael W. Hudson56796f62002-07-29 14:35:04 +00001668 for (cur = start, i = 0; i < slicelength - 1;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001669 cur += step, i++) {
1670 memmove(self->ob_item + (cur - i)*itemsize,
1671 self->ob_item + (cur + 1)*itemsize,
1672 (step - 1) * itemsize);
1673 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00001674 extra = self->ob_size - (cur + 1);
1675 if (extra > 0) {
1676 memmove(self->ob_item + (cur - i)*itemsize,
1677 self->ob_item + (cur + 1)*itemsize,
1678 extra*itemsize);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001679 }
1680
1681 self->ob_size -= slicelength;
1682 self->ob_item = PyMem_REALLOC(self->ob_item, itemsize*self->ob_size);
Raymond Hettinger6e2ee862004-03-14 04:37:50 +00001683 self->allocated = self->ob_size;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001684
1685 return 0;
1686 }
1687 else {
1688 /* assign slice */
1689 int cur, i;
1690 arrayobject* av;
1691
1692 if (!array_Check(value)) {
1693 PyErr_Format(PyExc_TypeError,
1694 "must assign array (not \"%.200s\") to slice",
1695 value->ob_type->tp_name);
1696 return -1;
1697 }
1698
1699 av = (arrayobject*)value;
1700
1701 if (av->ob_size != slicelength) {
1702 PyErr_Format(PyExc_ValueError,
1703 "attempt to assign array of size %d to extended slice of size %d",
1704 av->ob_size, slicelength);
1705 return -1;
1706 }
1707
1708 if (!slicelength)
1709 return 0;
1710
1711 /* protect against a[::-1] = a */
1712 if (self == av) {
1713 value = array_slice(av, 0, av->ob_size);
1714 av = (arrayobject*)value;
1715 }
1716 else {
1717 Py_INCREF(value);
1718 }
1719
1720 for (cur = start, i = 0; i < slicelength;
1721 cur += step, i++) {
1722 memcpy(self->ob_item + cur*itemsize,
1723 av->ob_item + i*itemsize,
1724 itemsize);
1725 }
1726
1727 Py_DECREF(value);
1728
1729 return 0;
1730 }
1731 }
1732 else {
1733 PyErr_SetString(PyExc_TypeError,
1734 "list indices must be integers");
1735 return -1;
1736 }
1737}
1738
1739static PyMappingMethods array_as_mapping = {
1740 (inquiry)array_length,
1741 (binaryfunc)array_subscr,
1742 (objobjargproc)array_ass_subscr
1743};
1744
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001745static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001746array_buffer_getreadbuf(arrayobject *self, int index, const void **ptr)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001747{
1748 if ( index != 0 ) {
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001749 PyErr_SetString(PyExc_SystemError,
1750 "Accessing non-existent array segment");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001751 return -1;
1752 }
1753 *ptr = (void *)self->ob_item;
1754 return self->ob_size*self->ob_descr->itemsize;
1755}
1756
1757static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001758array_buffer_getwritebuf(arrayobject *self, int index, const void **ptr)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001759{
1760 if ( index != 0 ) {
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001761 PyErr_SetString(PyExc_SystemError,
1762 "Accessing non-existent array segment");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001763 return -1;
1764 }
1765 *ptr = (void *)self->ob_item;
1766 return self->ob_size*self->ob_descr->itemsize;
1767}
1768
1769static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001770array_buffer_getsegcount(arrayobject *self, int *lenp)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001771{
1772 if ( lenp )
1773 *lenp = self->ob_size*self->ob_descr->itemsize;
1774 return 1;
1775}
1776
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001777static PySequenceMethods array_as_sequence = {
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001778 (inquiry)array_length, /*sq_length*/
1779 (binaryfunc)array_concat, /*sq_concat*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001780 (intargfunc)array_repeat, /*sq_repeat*/
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001781 (intargfunc)array_item, /*sq_item*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001782 (intintargfunc)array_slice, /*sq_slice*/
1783 (intobjargproc)array_ass_item, /*sq_ass_item*/
1784 (intintobjargproc)array_ass_slice, /*sq_ass_slice*/
Raymond Hettinger625812f2003-01-07 01:58:52 +00001785 (objobjproc)array_contains, /*sq_contains*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001786 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
1787 (intargfunc)array_inplace_repeat /*sq_inplace_repeat*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001788};
1789
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001790static PyBufferProcs array_as_buffer = {
1791 (getreadbufferproc)array_buffer_getreadbuf,
1792 (getwritebufferproc)array_buffer_getwritebuf,
1793 (getsegcountproc)array_buffer_getsegcount,
1794};
1795
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001796static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +00001797array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum778983b1993-02-19 15:55:02 +00001798{
1799 char c;
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001800 PyObject *initial = NULL, *it = NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001801 struct arraydescr *descr;
Martin v. Löwis99866332002-03-01 10:27:01 +00001802
1803 if (kwds != NULL) {
1804 int i = PyObject_Length(kwds);
1805 if (i < 0)
Guido van Rossum778983b1993-02-19 15:55:02 +00001806 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001807 else if (i > 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001808 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis99866332002-03-01 10:27:01 +00001809 "array.array constructor takes "
1810 "no keyword arguments");
Guido van Rossum778983b1993-02-19 15:55:02 +00001811 return NULL;
1812 }
1813 }
Martin v. Löwis99866332002-03-01 10:27:01 +00001814
1815 if (!PyArg_ParseTuple(args, "c|O:array", &c, &initial))
1816 return NULL;
1817
1818 if (!(initial == NULL || PyList_Check(initial)
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001819 || PyString_Check(initial) || PyTuple_Check(initial)
Martin v. Löwis99866332002-03-01 10:27:01 +00001820 || (c == 'u' && PyUnicode_Check(initial)))) {
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001821 it = PyObject_GetIter(initial);
1822 if (it == NULL)
1823 return NULL;
1824 /* We set initial to NULL so that the subsequent code
1825 will create an empty array of the appropriate type
1826 and afterwards we can use array_iter_extend to populate
1827 the array.
1828 */
1829 initial = NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001830 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001831 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1832 if (descr->typecode == c) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001833 PyObject *a;
Guido van Rossum778983b1993-02-19 15:55:02 +00001834 int len;
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001835
1836 if (initial == NULL || !(PyList_Check(initial)
1837 || PyTuple_Check(initial)))
Guido van Rossum778983b1993-02-19 15:55:02 +00001838 len = 0;
1839 else
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001840 len = PySequence_Size(initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001841
1842 a = newarrayobject(type, len, descr);
Guido van Rossum778983b1993-02-19 15:55:02 +00001843 if (a == NULL)
1844 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001845
Guido van Rossum778983b1993-02-19 15:55:02 +00001846 if (len > 0) {
1847 int i;
1848 for (i = 0; i < len; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001849 PyObject *v =
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001850 PySequence_GetItem(initial, i);
Raymond Hettinger85004cc2003-08-05 11:23:59 +00001851 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001852 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001853 return NULL;
1854 }
Raymond Hettinger85004cc2003-08-05 11:23:59 +00001855 if (setarrayitem(a, i, v) != 0) {
1856 Py_DECREF(v);
1857 Py_DECREF(a);
1858 return NULL;
1859 }
1860 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001861 }
Raymond Hettinger85004cc2003-08-05 11:23:59 +00001862 } else if (initial != NULL && PyString_Check(initial)) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001863 PyObject *t_initial = PyTuple_Pack(1,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001864 initial);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001865 PyObject *v =
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001866 array_fromstring((arrayobject *)a,
1867 t_initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001868 Py_DECREF(t_initial);
Guido van Rossum778983b1993-02-19 15:55:02 +00001869 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001870 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001871 return NULL;
1872 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001873 Py_DECREF(v);
Martin v. Löwis99866332002-03-01 10:27:01 +00001874#ifdef Py_USING_UNICODE
1875 } else if (initial != NULL && PyUnicode_Check(initial)) {
1876 int n = PyUnicode_GET_DATA_SIZE(initial);
1877 if (n > 0) {
1878 arrayobject *self = (arrayobject *)a;
1879 char *item = self->ob_item;
1880 item = PyMem_Realloc(item, n);
1881 if (item == NULL) {
1882 PyErr_NoMemory();
1883 Py_DECREF(a);
1884 return NULL;
1885 }
1886 self->ob_item = item;
1887 self->ob_size = n / sizeof(Py_UNICODE);
1888 memcpy(item, PyUnicode_AS_DATA(initial), n);
Raymond Hettinger6e2ee862004-03-14 04:37:50 +00001889 self->allocated = self->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +00001890 }
1891#endif
Guido van Rossum778983b1993-02-19 15:55:02 +00001892 }
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001893 if (it != NULL) {
1894 if (array_iter_extend((arrayobject *)a, it) == -1) {
1895 Py_DECREF(it);
1896 Py_DECREF(a);
1897 return NULL;
1898 }
1899 Py_DECREF(it);
1900 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001901 return a;
1902 }
1903 }
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001904 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis99866332002-03-01 10:27:01 +00001905 "bad typecode (must be c, b, B, u, h, H, i, I, l, L, f or d)");
Guido van Rossum778983b1993-02-19 15:55:02 +00001906 return NULL;
1907}
1908
Guido van Rossum778983b1993-02-19 15:55:02 +00001909
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001910PyDoc_STRVAR(module_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001911"This module defines an object type which can efficiently represent\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001912an array of basic values: characters, integers, floating point\n\
1913numbers. Arrays are sequence types and behave very much like lists,\n\
1914except that the type of objects stored in them is constrained. The\n\
1915type is specified at object creation time by using a type code, which\n\
1916is a single character. The following type codes are defined:\n\
1917\n\
1918 Type code C Type Minimum size in bytes \n\
1919 'c' character 1 \n\
1920 'b' signed integer 1 \n\
1921 'B' unsigned integer 1 \n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001922 'u' Unicode character 2 \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001923 'h' signed integer 2 \n\
1924 'H' unsigned integer 2 \n\
1925 'i' signed integer 2 \n\
1926 'I' unsigned integer 2 \n\
1927 'l' signed integer 4 \n\
1928 'L' unsigned integer 4 \n\
1929 'f' floating point 4 \n\
1930 'd' floating point 8 \n\
1931\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001932The constructor is:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001933\n\
1934array(typecode [, initializer]) -- create a new array\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001935");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001936
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001937PyDoc_STRVAR(arraytype_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001938"array(typecode [, initializer]) -> array\n\
1939\n\
1940Return a new array whose items are restricted by typecode, and\n\
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001941initialized from the optional initializer value, which must be a list,\n\
1942string. or iterable over elements of the appropriate type.\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001943\n\
1944Arrays represent basic values and behave very much like lists, except\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001945the type of objects stored in them is constrained.\n\
1946\n\
1947Methods:\n\
1948\n\
1949append() -- append a new item to the end of the array\n\
1950buffer_info() -- return information giving the current memory info\n\
1951byteswap() -- byteswap all the items of the array\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001952count() -- return number of occurences of an object\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001953extend() -- extend array by appending multiple elements from an iterable\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001954fromfile() -- read items from a file object\n\
1955fromlist() -- append items from the list\n\
1956fromstring() -- append items from the string\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001957index() -- return index of first occurence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001958insert() -- insert a new item into the array at a provided position\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001959pop() -- remove and return item (default last)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001960read() -- DEPRECATED, use fromfile()\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001961remove() -- remove first occurence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001962reverse() -- reverse the order of the items in the array\n\
1963tofile() -- write all items to a file object\n\
1964tolist() -- return the array converted to an ordinary list\n\
1965tostring() -- return the array converted to a string\n\
1966write() -- DEPRECATED, use tofile()\n\
1967\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001968Attributes:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001969\n\
1970typecode -- the typecode character used to create the array\n\
1971itemsize -- the length in bytes of one array item\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001972");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001973
Raymond Hettinger625812f2003-01-07 01:58:52 +00001974static PyObject *array_iter(arrayobject *ao);
1975
Tim Peters0c322792002-07-17 16:49:03 +00001976static PyTypeObject Arraytype = {
Fred Drake0d40ba42000-02-04 20:33:49 +00001977 PyObject_HEAD_INIT(NULL)
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001978 0,
Guido van Rossum14648392001-12-08 18:02:58 +00001979 "array.array",
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001980 sizeof(arrayobject),
1981 0,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001982 (destructor)array_dealloc, /* tp_dealloc */
Martin v. Löwisaa158be2002-03-04 09:38:52 +00001983 0, /* tp_print */
Martin v. Löwis99866332002-03-01 10:27:01 +00001984 0, /* tp_getattr */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001985 0, /* tp_setattr */
1986 0, /* tp_compare */
1987 (reprfunc)array_repr, /* tp_repr */
1988 0, /* tp_as _number*/
1989 &array_as_sequence, /* tp_as _sequence*/
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001990 &array_as_mapping, /* tp_as _mapping*/
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001991 0, /* tp_hash */
1992 0, /* tp_call */
1993 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00001994 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001995 0, /* tp_setattro */
Tim Petersb870c752001-06-05 04:43:26 +00001996 &array_as_buffer, /* tp_as_buffer*/
Raymond Hettingercb87bc82004-05-31 00:35:52 +00001997 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001998 arraytype_doc, /* tp_doc */
1999 0, /* tp_traverse */
2000 0, /* tp_clear */
2001 array_richcompare, /* tp_richcompare */
Raymond Hettingercb87bc82004-05-31 00:35:52 +00002002 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger625812f2003-01-07 01:58:52 +00002003 (getiterfunc)array_iter, /* tp_iter */
Martin v. Löwis99866332002-03-01 10:27:01 +00002004 0, /* tp_iternext */
2005 array_methods, /* tp_methods */
2006 0, /* tp_members */
2007 array_getsets, /* tp_getset */
2008 0, /* tp_base */
2009 0, /* tp_dict */
2010 0, /* tp_descr_get */
2011 0, /* tp_descr_set */
2012 0, /* tp_dictoffset */
2013 0, /* tp_init */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002014 PyType_GenericAlloc, /* tp_alloc */
Martin v. Löwis99866332002-03-01 10:27:01 +00002015 array_new, /* tp_new */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002016 PyObject_Del, /* tp_free */
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002017};
2018
Raymond Hettinger625812f2003-01-07 01:58:52 +00002019
2020/*********************** Array Iterator **************************/
2021
2022typedef struct {
2023 PyObject_HEAD
2024 long index;
2025 arrayobject *ao;
2026 PyObject * (*getitem)(struct arrayobject *, int);
2027} arrayiterobject;
2028
2029static PyTypeObject PyArrayIter_Type;
2030
2031#define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
2032
2033static PyObject *
2034array_iter(arrayobject *ao)
2035{
2036 arrayiterobject *it;
2037
2038 if (!array_Check(ao)) {
2039 PyErr_BadInternalCall();
2040 return NULL;
2041 }
2042
2043 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
2044 if (it == NULL)
2045 return NULL;
2046
2047 Py_INCREF(ao);
2048 it->ao = ao;
2049 it->index = 0;
2050 it->getitem = ao->ob_descr->getitem;
2051 PyObject_GC_Track(it);
2052 return (PyObject *)it;
2053}
2054
2055static PyObject *
Raymond Hettinger625812f2003-01-07 01:58:52 +00002056arrayiter_next(arrayiterobject *it)
2057{
2058 assert(PyArrayIter_Check(it));
2059 if (it->index < it->ao->ob_size)
2060 return (*it->getitem)(it->ao, it->index++);
2061 return NULL;
2062}
2063
2064static void
2065arrayiter_dealloc(arrayiterobject *it)
2066{
2067 PyObject_GC_UnTrack(it);
2068 Py_XDECREF(it->ao);
2069 PyObject_GC_Del(it);
2070}
2071
2072static int
2073arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2074{
2075 if (it->ao != NULL)
2076 return visit((PyObject *)(it->ao), arg);
2077 return 0;
2078}
2079
2080static PyTypeObject PyArrayIter_Type = {
Jason Tishler84375702003-02-10 20:45:47 +00002081 PyObject_HEAD_INIT(NULL)
Raymond Hettinger625812f2003-01-07 01:58:52 +00002082 0, /* ob_size */
2083 "arrayiterator", /* tp_name */
2084 sizeof(arrayiterobject), /* tp_basicsize */
2085 0, /* tp_itemsize */
2086 /* methods */
2087 (destructor)arrayiter_dealloc, /* tp_dealloc */
2088 0, /* tp_print */
2089 0, /* tp_getattr */
2090 0, /* tp_setattr */
2091 0, /* tp_compare */
2092 0, /* tp_repr */
2093 0, /* tp_as_number */
2094 0, /* tp_as_sequence */
2095 0, /* tp_as_mapping */
2096 0, /* tp_hash */
2097 0, /* tp_call */
2098 0, /* tp_str */
2099 PyObject_GenericGetAttr, /* tp_getattro */
2100 0, /* tp_setattro */
2101 0, /* tp_as_buffer */
2102 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2103 0, /* tp_doc */
2104 (traverseproc)arrayiter_traverse, /* tp_traverse */
2105 0, /* tp_clear */
2106 0, /* tp_richcompare */
2107 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002108 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger625812f2003-01-07 01:58:52 +00002109 (iternextfunc)arrayiter_next, /* tp_iternext */
2110 0, /* tp_methods */
2111};
2112
2113
2114/*********************** Install Module **************************/
2115
Martin v. Löwis99866332002-03-01 10:27:01 +00002116/* No functions in array module. */
2117static PyMethodDef a_methods[] = {
2118 {NULL, NULL, 0, NULL} /* Sentinel */
2119};
2120
2121
Mark Hammondfe51c6d2002-08-02 02:27:13 +00002122PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00002123initarray(void)
Guido van Rossum778983b1993-02-19 15:55:02 +00002124{
Fred Drakef4e34842002-04-01 03:45:06 +00002125 PyObject *m;
Fred Drake0d40ba42000-02-04 20:33:49 +00002126
Martin v. Löwis99866332002-03-01 10:27:01 +00002127 Arraytype.ob_type = &PyType_Type;
Jason Tishler84375702003-02-10 20:45:47 +00002128 PyArrayIter_Type.ob_type = &PyType_Type;
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002129 m = Py_InitModule3("array", a_methods, module_doc);
Fred Drakef4e34842002-04-01 03:45:06 +00002130
2131 Py_INCREF((PyObject *)&Arraytype);
2132 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
2133 Py_INCREF((PyObject *)&Arraytype);
2134 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
Guido van Rossuma0deb641998-10-14 13:45:06 +00002135 /* No need to check the error here, the caller will do that */
Guido van Rossum778983b1993-02-19 15:55:02 +00002136}