blob: 585ed2f2d34789b6719140b59703809697bddce3 [file] [log] [blame]
Guido van Rossum778983b1993-02-19 15:55:02 +00001/* Array object implementation */
2
3/* An array is a uniform list -- all items have the same type.
4 The item type is restricted to simple C types like int or float */
5
Martin v. Löwis18e16552006-02-15 17:27:45 +00006#define PY_SSIZE_T_CLEAN
Roger E. Masse2919eaa1996-12-09 20:10:36 +00007#include "Python.h"
Raymond Hettingercb87bc82004-05-31 00:35:52 +00008#include "structmember.h"
Roger E. Masse5817f8f1996-12-09 22:24:19 +00009
Guido van Rossum0c709541994-08-19 12:01:32 +000010#ifdef STDC_HEADERS
11#include <stddef.h>
Guido van Rossum7f1de831999-08-27 20:33:52 +000012#else /* !STDC_HEADERS */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000013#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000014#include <sys/types.h> /* For size_t */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000015#endif /* HAVE_SYS_TYPES_H */
Guido van Rossum7f1de831999-08-27 20:33:52 +000016#endif /* !STDC_HEADERS */
Guido van Rossum778983b1993-02-19 15:55:02 +000017
18struct arrayobject; /* Forward */
19
Tim Petersbb307342000-09-10 05:22:54 +000020/* All possible arraydescr values are defined in the vector "descriptors"
21 * below. That's defined later because the appropriate get and set
22 * functions aren't visible yet.
23 */
Guido van Rossum778983b1993-02-19 15:55:02 +000024struct arraydescr {
25 int typecode;
26 int itemsize;
Martin v. Löwis18e16552006-02-15 17:27:45 +000027 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
28 int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *);
Guido van Rossum778983b1993-02-19 15:55:02 +000029};
30
31typedef struct arrayobject {
Georg Brandl1b672672006-02-17 08:56:33 +000032 PyObject_VAR_HEAD
Guido van Rossum778983b1993-02-19 15:55:02 +000033 char *ob_item;
Martin v. Löwis18e16552006-02-15 17:27:45 +000034 Py_ssize_t allocated;
Guido van Rossum778983b1993-02-19 15:55:02 +000035 struct arraydescr *ob_descr;
Raymond Hettingercb87bc82004-05-31 00:35:52 +000036 PyObject *weakreflist; /* List of weak references */
Guido van Rossum778983b1993-02-19 15:55:02 +000037} arrayobject;
38
Jeremy Hylton938ace62002-07-17 16:30:39 +000039static PyTypeObject Arraytype;
Guido van Rossum778983b1993-02-19 15:55:02 +000040
Martin v. Löwis99866332002-03-01 10:27:01 +000041#define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
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
Martin v. Löwis18e16552006-02-15 17:27:45 +000045array_resize(arrayobject *self, Py_ssize_t newsize)
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000046{
47 char *items;
48 size_t _new_size;
49
50 /* Bypass realloc() when a previous overallocation is large enough
51 to accommodate the newsize. If the newsize is 16 smaller than the
52 current size, then proceed with the realloc() to shrink the list.
53 */
54
55 if (self->allocated >= newsize &&
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 *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000105b_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000106{
107 long x = ((char *)ap->ob_item)[i];
108 if (x >= 128)
109 x -= 256;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000110 return PyInt_FromLong(x);
Guido van Rossum778983b1993-02-19 15:55:02 +0000111}
112
113static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000114b_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000115{
Fred Drake541dc3b2000-06-28 17:49:30 +0000116 short x;
117 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
118 must use the next size up that is signed ('h') and manually do
119 the overflow checking */
120 if (!PyArg_Parse(v, "h;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000121 return -1;
Guido van Rossum9f754e02000-07-01 00:38:19 +0000122 else if (x < -128) {
Fred Drake541dc3b2000-06-28 17:49:30 +0000123 PyErr_SetString(PyExc_OverflowError,
124 "signed char is less than minimum");
125 return -1;
126 }
Guido van Rossum9f754e02000-07-01 00:38:19 +0000127 else if (x > 127) {
Fred Drake541dc3b2000-06-28 17:49:30 +0000128 PyErr_SetString(PyExc_OverflowError,
129 "signed char is greater than maximum");
130 return -1;
131 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000132 if (i >= 0)
Fred Drake541dc3b2000-06-28 17:49:30 +0000133 ((char *)ap->ob_item)[i] = (char)x;
Guido van Rossum778983b1993-02-19 15:55:02 +0000134 return 0;
135}
136
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000137static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000138BB_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000139{
140 long x = ((unsigned char *)ap->ob_item)[i];
141 return PyInt_FromLong(x);
142}
143
Fred Drake541dc3b2000-06-28 17:49:30 +0000144static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000145BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000146{
147 unsigned char x;
148 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
149 if (!PyArg_Parse(v, "b;array item must be integer", &x))
150 return -1;
151 if (i >= 0)
Martin v. Löwis99866332002-03-01 10:27:01 +0000152 ((char *)ap->ob_item)[i] = x;
Fred Drake541dc3b2000-06-28 17:49:30 +0000153 return 0;
154}
Guido van Rossum549ab711997-01-03 19:09:47 +0000155
Martin v. Löwis99866332002-03-01 10:27:01 +0000156static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000157u_getitem(arrayobject *ap, Py_ssize_t i)
Martin v. Löwis99866332002-03-01 10:27:01 +0000158{
159 return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1);
160}
161
162static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000163u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Martin v. Löwis99866332002-03-01 10:27:01 +0000164{
165 Py_UNICODE *p;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000166 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +0000167
168 if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
169 return -1;
170 if (len != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000171 PyErr_SetString(PyExc_TypeError,
172 "array item must be unicode character");
Martin v. Löwis99866332002-03-01 10:27:01 +0000173 return -1;
174 }
175 if (i >= 0)
176 ((Py_UNICODE *)ap->ob_item)[i] = p[0];
177 return 0;
178}
Martin v. Löwis99866332002-03-01 10:27:01 +0000179
Guido van Rossum549ab711997-01-03 19:09:47 +0000180static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000181h_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000182{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000183 return PyInt_FromLong((long) ((short *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000184}
185
186static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000187h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000188{
189 short x;
Fred Drake541dc3b2000-06-28 17:49:30 +0000190 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000191 if (!PyArg_Parse(v, "h;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000192 return -1;
193 if (i >= 0)
194 ((short *)ap->ob_item)[i] = x;
195 return 0;
196}
197
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000198static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000199HH_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000200{
201 return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
202}
203
Fred Drake541dc3b2000-06-28 17:49:30 +0000204static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000205HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000206{
207 int x;
208 /* PyArg_Parse's 'h' formatter is for a signed short, therefore
209 must use the next size up and manually do the overflow checking */
210 if (!PyArg_Parse(v, "i;array item must be integer", &x))
211 return -1;
212 else if (x < 0) {
213 PyErr_SetString(PyExc_OverflowError,
214 "unsigned short is less than minimum");
215 return -1;
216 }
217 else if (x > USHRT_MAX) {
218 PyErr_SetString(PyExc_OverflowError,
219 "unsigned short is greater than maximum");
220 return -1;
221 }
222 if (i >= 0)
223 ((short *)ap->ob_item)[i] = (short)x;
224 return 0;
225}
Guido van Rossum549ab711997-01-03 19:09:47 +0000226
227static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000228i_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossume77a7571993-11-03 15:01:26 +0000229{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000230 return PyInt_FromLong((long) ((int *)ap->ob_item)[i]);
Guido van Rossume77a7571993-11-03 15:01:26 +0000231}
232
233static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000234i_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossume77a7571993-11-03 15:01:26 +0000235{
236 int x;
Fred Drake541dc3b2000-06-28 17:49:30 +0000237 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000238 if (!PyArg_Parse(v, "i;array item must be integer", &x))
Guido van Rossume77a7571993-11-03 15:01:26 +0000239 return -1;
240 if (i >= 0)
241 ((int *)ap->ob_item)[i] = x;
242 return 0;
243}
244
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000245static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000246II_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000247{
248 return PyLong_FromUnsignedLong(
249 (unsigned long) ((unsigned int *)ap->ob_item)[i]);
250}
251
252static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000253II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000254{
255 unsigned long x;
256 if (PyLong_Check(v)) {
257 x = PyLong_AsUnsignedLong(v);
258 if (x == (unsigned long) -1 && PyErr_Occurred())
259 return -1;
260 }
261 else {
Fred Drake541dc3b2000-06-28 17:49:30 +0000262 long y;
263 if (!PyArg_Parse(v, "l;array item must be integer", &y))
Guido van Rossum549ab711997-01-03 19:09:47 +0000264 return -1;
Fred Drake541dc3b2000-06-28 17:49:30 +0000265 if (y < 0) {
266 PyErr_SetString(PyExc_OverflowError,
267 "unsigned int is less than minimum");
268 return -1;
269 }
270 x = (unsigned long)y;
Tim Petersbb307342000-09-10 05:22:54 +0000271
Guido van Rossum549ab711997-01-03 19:09:47 +0000272 }
Fred Drake541dc3b2000-06-28 17:49:30 +0000273 if (x > UINT_MAX) {
274 PyErr_SetString(PyExc_OverflowError,
275 "unsigned int is greater than maximum");
276 return -1;
277 }
278
Guido van Rossum549ab711997-01-03 19:09:47 +0000279 if (i >= 0)
Fred Drake541dc3b2000-06-28 17:49:30 +0000280 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
Guido van Rossum549ab711997-01-03 19:09:47 +0000281 return 0;
282}
283
284static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000285l_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000286{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000287 return PyInt_FromLong(((long *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000288}
289
290static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000291l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000292{
293 long x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000294 if (!PyArg_Parse(v, "l;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000295 return -1;
296 if (i >= 0)
297 ((long *)ap->ob_item)[i] = x;
298 return 0;
299}
300
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000301static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000302LL_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000303{
304 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
305}
306
307static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000308LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000309{
310 unsigned long x;
311 if (PyLong_Check(v)) {
312 x = PyLong_AsUnsignedLong(v);
313 if (x == (unsigned long) -1 && PyErr_Occurred())
314 return -1;
315 }
316 else {
Fred Drake541dc3b2000-06-28 17:49:30 +0000317 long y;
318 if (!PyArg_Parse(v, "l;array item must be integer", &y))
Guido van Rossum549ab711997-01-03 19:09:47 +0000319 return -1;
Fred Drake541dc3b2000-06-28 17:49:30 +0000320 if (y < 0) {
321 PyErr_SetString(PyExc_OverflowError,
322 "unsigned long is less than minimum");
323 return -1;
324 }
325 x = (unsigned long)y;
Tim Petersbb307342000-09-10 05:22:54 +0000326
Guido van Rossum549ab711997-01-03 19:09:47 +0000327 }
Fred Drake541dc3b2000-06-28 17:49:30 +0000328 if (x > ULONG_MAX) {
329 PyErr_SetString(PyExc_OverflowError,
330 "unsigned long is greater than maximum");
331 return -1;
332 }
Tim Petersbb307342000-09-10 05:22:54 +0000333
Guido van Rossum549ab711997-01-03 19:09:47 +0000334 if (i >= 0)
335 ((unsigned long *)ap->ob_item)[i] = x;
336 return 0;
337}
338
339static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000340f_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000341{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000342 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000343}
344
345static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000346f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000347{
348 float x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000349 if (!PyArg_Parse(v, "f;array item must be float", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000350 return -1;
351 if (i >= 0)
352 ((float *)ap->ob_item)[i] = x;
353 return 0;
354}
355
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000356static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000357d_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000358{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000359 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000360}
361
362static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000363d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000364{
365 double x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000366 if (!PyArg_Parse(v, "d;array item must be float", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000367 return -1;
368 if (i >= 0)
369 ((double *)ap->ob_item)[i] = x;
370 return 0;
371}
372
373/* Description of types */
Guido van Rossum234f9421993-06-17 12:35:49 +0000374static struct arraydescr descriptors[] = {
Guido van Rossum778983b1993-02-19 15:55:02 +0000375 {'b', sizeof(char), b_getitem, b_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000376 {'B', sizeof(char), BB_getitem, BB_setitem},
Martin v. Löwis99866332002-03-01 10:27:01 +0000377 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem},
Guido van Rossum778983b1993-02-19 15:55:02 +0000378 {'h', sizeof(short), h_getitem, h_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000379 {'H', sizeof(short), HH_getitem, HH_setitem},
Guido van Rossume77a7571993-11-03 15:01:26 +0000380 {'i', sizeof(int), i_getitem, i_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000381 {'I', sizeof(int), II_getitem, II_setitem},
Guido van Rossum778983b1993-02-19 15:55:02 +0000382 {'l', sizeof(long), l_getitem, l_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000383 {'L', sizeof(long), LL_getitem, LL_setitem},
Guido van Rossum778983b1993-02-19 15:55:02 +0000384 {'f', sizeof(float), f_getitem, f_setitem},
385 {'d', sizeof(double), d_getitem, d_setitem},
386 {'\0', 0, 0, 0} /* Sentinel */
387};
Tim Petersbb307342000-09-10 05:22:54 +0000388
389/****************************************************************************
390Implementations of array object methods.
391****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +0000392
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000393static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000394newarrayobject(PyTypeObject *type, Py_ssize_t size, struct arraydescr *descr)
Guido van Rossum778983b1993-02-19 15:55:02 +0000395{
Guido van Rossum778983b1993-02-19 15:55:02 +0000396 arrayobject *op;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000397 size_t nbytes;
Martin v. Löwis99866332002-03-01 10:27:01 +0000398
Guido van Rossum778983b1993-02-19 15:55:02 +0000399 if (size < 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000400 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000401 return NULL;
402 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000403
Guido van Rossum778983b1993-02-19 15:55:02 +0000404 nbytes = size * descr->itemsize;
405 /* Check for overflow */
Guido van Rossum7844e381997-04-11 20:44:04 +0000406 if (nbytes / descr->itemsize != (size_t)size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000407 return PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000408 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000409 op = (arrayobject *) type->tp_alloc(type, 0);
Guido van Rossum778983b1993-02-19 15:55:02 +0000410 if (op == NULL) {
Martin v. Löwis99866332002-03-01 10:27:01 +0000411 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +0000412 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000413 op->ob_size = size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000414 if (size <= 0) {
415 op->ob_item = NULL;
416 }
417 else {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000418 op->ob_item = PyMem_NEW(char, nbytes);
Guido van Rossum778983b1993-02-19 15:55:02 +0000419 if (op->ob_item == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000420 PyObject_Del(op);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000421 return PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000422 }
423 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000424 op->ob_descr = descr;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000425 op->allocated = size;
Raymond Hettingercb87bc82004-05-31 00:35:52 +0000426 op->weakreflist = NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000427 return (PyObject *) op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000428}
429
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000430static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000431getarrayitem(PyObject *op, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000432{
433 register arrayobject *ap;
Martin v. Löwis99866332002-03-01 10:27:01 +0000434 assert(array_Check(op));
Guido van Rossum778983b1993-02-19 15:55:02 +0000435 ap = (arrayobject *)op;
Walter Dörwald8bb1ae92003-05-23 10:01:07 +0000436 assert(i>=0 && i<ap->ob_size);
Guido van Rossum778983b1993-02-19 15:55:02 +0000437 return (*ap->ob_descr->getitem)(ap, i);
438}
439
440static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000441ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000442{
Guido van Rossum778983b1993-02-19 15:55:02 +0000443 char *items;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000444 Py_ssize_t n = self->ob_size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000445 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000446 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000447 return -1;
448 }
449 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
450 return -1;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000451
452 if (array_resize(self, n+1) == -1)
Guido van Rossum778983b1993-02-19 15:55:02 +0000453 return -1;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000454 items = self->ob_item;
Walter Dörwald9e46abe2003-05-18 03:15:10 +0000455 if (where < 0) {
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000456 where += n;
Walter Dörwald9e46abe2003-05-18 03:15:10 +0000457 if (where < 0)
458 where = 0;
459 }
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000460 if (where > n)
461 where = n;
462 /* appends don't need to call memmove() */
463 if (where != n)
464 memmove(items + (where+1)*self->ob_descr->itemsize,
465 items + where*self->ob_descr->itemsize,
466 (n-where)*self->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000467 return (*self->ob_descr->setitem)(self, where, v);
468}
469
Guido van Rossum778983b1993-02-19 15:55:02 +0000470/* Methods */
471
472static void
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000473array_dealloc(arrayobject *op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000474{
Raymond Hettingercb87bc82004-05-31 00:35:52 +0000475 if (op->weakreflist != NULL)
476 PyObject_ClearWeakRefs((PyObject *) op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000477 if (op->ob_item != NULL)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000478 PyMem_DEL(op->ob_item);
Martin v. Löwis99866332002-03-01 10:27:01 +0000479 op->ob_type->tp_free((PyObject *)op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000480}
481
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000482static PyObject *
483array_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000484{
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000485 arrayobject *va, *wa;
486 PyObject *vi = NULL;
487 PyObject *wi = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000488 Py_ssize_t i, k;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000489 PyObject *res;
490
Martin v. Löwis99866332002-03-01 10:27:01 +0000491 if (!array_Check(v) || !array_Check(w)) {
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000492 Py_INCREF(Py_NotImplemented);
493 return Py_NotImplemented;
Guido van Rossum778983b1993-02-19 15:55:02 +0000494 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000495
496 va = (arrayobject *)v;
497 wa = (arrayobject *)w;
498
499 if (va->ob_size != wa->ob_size && (op == Py_EQ || op == Py_NE)) {
500 /* Shortcut: if the lengths differ, the arrays differ */
501 if (op == Py_EQ)
502 res = Py_False;
503 else
504 res = Py_True;
505 Py_INCREF(res);
506 return res;
507 }
508
509 /* Search for the first index where items are different */
510 k = 1;
511 for (i = 0; i < va->ob_size && i < wa->ob_size; i++) {
512 vi = getarrayitem(v, i);
513 wi = getarrayitem(w, i);
514 if (vi == NULL || wi == NULL) {
515 Py_XDECREF(vi);
516 Py_XDECREF(wi);
517 return NULL;
518 }
519 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
520 if (k == 0)
521 break; /* Keeping vi and wi alive! */
522 Py_DECREF(vi);
523 Py_DECREF(wi);
524 if (k < 0)
525 return NULL;
526 }
527
528 if (k) {
529 /* No more items to compare -- compare sizes */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000530 Py_ssize_t vs = va->ob_size;
531 Py_ssize_t ws = wa->ob_size;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000532 int cmp;
533 switch (op) {
534 case Py_LT: cmp = vs < ws; break;
Guido van Rossum2b597e42001-01-25 22:12:43 +0000535 case Py_LE: cmp = vs <= ws; break;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000536 case Py_EQ: cmp = vs == ws; break;
537 case Py_NE: cmp = vs != ws; break;
538 case Py_GT: cmp = vs > ws; break;
539 case Py_GE: cmp = vs >= ws; break;
540 default: return NULL; /* cannot happen */
541 }
542 if (cmp)
543 res = Py_True;
544 else
545 res = Py_False;
546 Py_INCREF(res);
547 return res;
548 }
549
550 /* We have an item that differs. First, shortcuts for EQ/NE */
551 if (op == Py_EQ) {
552 Py_INCREF(Py_False);
553 res = Py_False;
554 }
555 else if (op == Py_NE) {
556 Py_INCREF(Py_True);
557 res = Py_True;
558 }
559 else {
560 /* Compare the final item again using the proper operator */
561 res = PyObject_RichCompare(vi, wi, op);
562 }
563 Py_DECREF(vi);
564 Py_DECREF(wi);
565 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +0000566}
567
Martin v. Löwis18e16552006-02-15 17:27:45 +0000568static Py_ssize_t
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000569array_length(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +0000570{
571 return a->ob_size;
572}
573
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000574static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000575array_item(arrayobject *a, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000576{
577 if (i < 0 || i >= a->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000578 PyErr_SetString(PyExc_IndexError, "array index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000579 return NULL;
580 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000581 return getarrayitem((PyObject *)a, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000582}
583
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000584static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000585array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000586{
587 arrayobject *np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000588 if (ilow < 0)
589 ilow = 0;
590 else if (ilow > a->ob_size)
591 ilow = a->ob_size;
592 if (ihigh < 0)
593 ihigh = 0;
594 if (ihigh < ilow)
595 ihigh = ilow;
596 else if (ihigh > a->ob_size)
597 ihigh = a->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000598 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
Guido van Rossum778983b1993-02-19 15:55:02 +0000599 if (np == NULL)
600 return NULL;
601 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
602 (ihigh-ilow) * a->ob_descr->itemsize);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000603 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000604}
605
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000606static PyObject *
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000607array_copy(arrayobject *a, PyObject *unused)
608{
609 return array_slice(a, 0, a->ob_size);
610}
611
612PyDoc_STRVAR(copy_doc,
613"copy(array)\n\
614\n\
615 Return a copy of the array.");
616
617static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000618array_concat(arrayobject *a, PyObject *bb)
Guido van Rossum778983b1993-02-19 15:55:02 +0000619{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000620 Py_ssize_t size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000621 arrayobject *np;
Martin v. Löwis99866332002-03-01 10:27:01 +0000622 if (!array_Check(bb)) {
Fred Drake137507e2000-06-01 02:02:46 +0000623 PyErr_Format(PyExc_TypeError,
624 "can only append array (not \"%.200s\") to array",
625 bb->ob_type->tp_name);
Guido van Rossum778983b1993-02-19 15:55:02 +0000626 return NULL;
627 }
628#define b ((arrayobject *)bb)
629 if (a->ob_descr != b->ob_descr) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000630 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000631 return NULL;
632 }
633 size = a->ob_size + b->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000634 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
Guido van Rossum778983b1993-02-19 15:55:02 +0000635 if (np == NULL) {
636 return NULL;
637 }
638 memcpy(np->ob_item, a->ob_item, a->ob_size*a->ob_descr->itemsize);
639 memcpy(np->ob_item + a->ob_size*a->ob_descr->itemsize,
Guido van Rossum32be3a71993-11-05 10:16:27 +0000640 b->ob_item, b->ob_size*b->ob_descr->itemsize);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000641 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000642#undef b
643}
644
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000645static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000646array_repeat(arrayobject *a, Py_ssize_t n)
Guido van Rossum778983b1993-02-19 15:55:02 +0000647{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000648 Py_ssize_t i;
649 Py_ssize_t size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000650 arrayobject *np;
651 char *p;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000652 Py_ssize_t nbytes;
Guido van Rossum778983b1993-02-19 15:55:02 +0000653 if (n < 0)
654 n = 0;
655 size = a->ob_size * n;
Martin v. Löwis99866332002-03-01 10:27:01 +0000656 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
Guido van Rossum778983b1993-02-19 15:55:02 +0000657 if (np == NULL)
658 return NULL;
659 p = np->ob_item;
660 nbytes = a->ob_size * a->ob_descr->itemsize;
661 for (i = 0; i < n; i++) {
662 memcpy(p, a->ob_item, nbytes);
663 p += nbytes;
664 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000665 return (PyObject *) np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000666}
667
668static int
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000669array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000670{
671 char *item;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000672 Py_ssize_t n; /* Size of replacement array */
673 Py_ssize_t d; /* Change in size */
Guido van Rossum778983b1993-02-19 15:55:02 +0000674#define b ((arrayobject *)v)
675 if (v == NULL)
676 n = 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000677 else if (array_Check(v)) {
Guido van Rossum778983b1993-02-19 15:55:02 +0000678 n = b->ob_size;
679 if (a == b) {
680 /* Special case "a[i:j] = a" -- copy b first */
681 int ret;
682 v = array_slice(b, 0, n);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000683 if (!v)
684 return -1;
Guido van Rossum778983b1993-02-19 15:55:02 +0000685 ret = array_ass_slice(a, ilow, ihigh, v);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000686 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000687 return ret;
688 }
689 if (b->ob_descr != a->ob_descr) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000690 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000691 return -1;
692 }
693 }
694 else {
Fred Drake137507e2000-06-01 02:02:46 +0000695 PyErr_Format(PyExc_TypeError,
696 "can only assign array (not \"%.200s\") to array slice",
697 v->ob_type->tp_name);
Guido van Rossum778983b1993-02-19 15:55:02 +0000698 return -1;
699 }
700 if (ilow < 0)
701 ilow = 0;
702 else if (ilow > a->ob_size)
703 ilow = a->ob_size;
704 if (ihigh < 0)
705 ihigh = 0;
706 if (ihigh < ilow)
707 ihigh = ilow;
708 else if (ihigh > a->ob_size)
709 ihigh = a->ob_size;
710 item = a->ob_item;
711 d = n - (ihigh-ilow);
712 if (d < 0) { /* Delete -d items */
713 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
714 item + ihigh*a->ob_descr->itemsize,
715 (a->ob_size-ihigh)*a->ob_descr->itemsize);
716 a->ob_size += d;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000717 PyMem_RESIZE(item, char, a->ob_size*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000718 /* Can't fail */
719 a->ob_item = item;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000720 a->allocated = a->ob_size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000721 }
722 else if (d > 0) { /* Insert d items */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000723 PyMem_RESIZE(item, char,
724 (a->ob_size + d)*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000725 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000726 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000727 return -1;
728 }
729 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
730 item + ihigh*a->ob_descr->itemsize,
731 (a->ob_size-ihigh)*a->ob_descr->itemsize);
732 a->ob_item = item;
733 a->ob_size += d;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000734 a->allocated = a->ob_size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000735 }
736 if (n > 0)
737 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
738 n*b->ob_descr->itemsize);
739 return 0;
740#undef b
741}
742
743static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000744array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000745{
746 if (i < 0 || i >= a->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000747 PyErr_SetString(PyExc_IndexError,
748 "array assignment index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000749 return -1;
750 }
751 if (v == NULL)
752 return array_ass_slice(a, i, i+1, v);
753 return (*a->ob_descr->setitem)(a, i, v);
754}
755
756static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000757setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000758{
Martin v. Löwis99866332002-03-01 10:27:01 +0000759 assert(array_Check(a));
Guido van Rossum778983b1993-02-19 15:55:02 +0000760 return array_ass_item((arrayobject *)a, i, v);
761}
762
Martin v. Löwis99866332002-03-01 10:27:01 +0000763static int
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000764array_iter_extend(arrayobject *self, PyObject *bb)
765{
766 PyObject *it, *v;
767
768 it = PyObject_GetIter(bb);
769 if (it == NULL)
770 return -1;
771
772 while ((v = PyIter_Next(it)) != NULL) {
773 if (ins1(self, (int) self->ob_size, v) != 0) {
774 Py_DECREF(v);
775 Py_DECREF(it);
776 return -1;
777 }
778 Py_DECREF(v);
779 }
780 Py_DECREF(it);
781 if (PyErr_Occurred())
782 return -1;
783 return 0;
784}
785
786static int
Martin v. Löwis99866332002-03-01 10:27:01 +0000787array_do_extend(arrayobject *self, PyObject *bb)
788{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000789 Py_ssize_t size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000790
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000791 if (!array_Check(bb))
792 return array_iter_extend(self, bb);
Martin v. Löwis99866332002-03-01 10:27:01 +0000793#define b ((arrayobject *)bb)
794 if (self->ob_descr != b->ob_descr) {
795 PyErr_SetString(PyExc_TypeError,
796 "can only extend with array of same kind");
797 return -1;
798 }
799 size = self->ob_size + b->ob_size;
800 PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize);
801 if (self->ob_item == NULL) {
802 PyObject_Del(self);
803 PyErr_NoMemory();
804 return -1;
805 }
806 memcpy(self->ob_item + self->ob_size*self->ob_descr->itemsize,
807 b->ob_item, b->ob_size*b->ob_descr->itemsize);
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000808 self->ob_size = size;
809 self->allocated = size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000810
811 return 0;
812#undef b
813}
814
815static PyObject *
816array_inplace_concat(arrayobject *self, PyObject *bb)
817{
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000818 if (!array_Check(bb)) {
819 PyErr_Format(PyExc_TypeError,
820 "can only extend array with array (not \"%.200s\")",
821 bb->ob_type->tp_name);
822 return NULL;
823 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000824 if (array_do_extend(self, bb) == -1)
825 return NULL;
826 Py_INCREF(self);
827 return (PyObject *)self;
828}
829
830static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000831array_inplace_repeat(arrayobject *self, Py_ssize_t n)
Martin v. Löwis99866332002-03-01 10:27:01 +0000832{
833 char *items, *p;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000834 Py_ssize_t size, i;
Martin v. Löwis99866332002-03-01 10:27:01 +0000835
836 if (self->ob_size > 0) {
837 if (n < 0)
838 n = 0;
839 items = self->ob_item;
840 size = self->ob_size * self->ob_descr->itemsize;
841 if (n == 0) {
842 PyMem_FREE(items);
843 self->ob_item = NULL;
844 self->ob_size = 0;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000845 self->allocated = 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000846 }
847 else {
848 PyMem_Resize(items, char, n * size);
849 if (items == NULL)
850 return PyErr_NoMemory();
851 p = items;
852 for (i = 1; i < n; i++) {
853 p += size;
854 memcpy(p, items, size);
855 }
856 self->ob_item = items;
857 self->ob_size *= n;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000858 self->allocated = self->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000859 }
860 }
861 Py_INCREF(self);
862 return (PyObject *)self;
863}
864
865
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000866static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000867ins(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000868{
869 if (ins1(self, where, v) != 0)
870 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000871 Py_INCREF(Py_None);
872 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000873}
874
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000875static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000876array_count(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000877{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000878 Py_ssize_t count = 0;
879 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000880
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000881 for (i = 0; i < self->ob_size; i++) {
882 PyObject *selfi = getarrayitem((PyObject *)self, i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000883 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000884 Py_DECREF(selfi);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000885 if (cmp > 0)
886 count++;
887 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000888 return NULL;
889 }
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000890 return PyInt_FromSsize_t(count);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000891}
892
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000893PyDoc_STRVAR(count_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000894"count(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000895\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000896Return number of occurences of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000897
898static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000899array_index(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000900{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000901 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000902
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000903 for (i = 0; i < self->ob_size; i++) {
904 PyObject *selfi = getarrayitem((PyObject *)self, i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000905 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
906 Py_DECREF(selfi);
907 if (cmp > 0) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000908 return PyInt_FromLong((long)i);
909 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000910 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000911 return NULL;
912 }
913 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
914 return NULL;
915}
916
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000917PyDoc_STRVAR(index_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000918"index(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000919\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000920Return index of first occurence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000921
Raymond Hettinger625812f2003-01-07 01:58:52 +0000922static int
923array_contains(arrayobject *self, PyObject *v)
924{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000925 Py_ssize_t i;
926 int cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +0000927
928 for (i = 0, cmp = 0 ; cmp == 0 && i < self->ob_size; i++) {
929 PyObject *selfi = getarrayitem((PyObject *)self, i);
930 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
931 Py_DECREF(selfi);
932 }
933 return cmp;
934}
935
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000936static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000937array_remove(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000938{
939 int i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000940
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000941 for (i = 0; i < self->ob_size; i++) {
942 PyObject *selfi = getarrayitem((PyObject *)self,i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000943 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
944 Py_DECREF(selfi);
945 if (cmp > 0) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000946 if (array_ass_slice(self, i, i+1,
947 (PyObject *)NULL) != 0)
948 return NULL;
949 Py_INCREF(Py_None);
950 return Py_None;
951 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000952 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000953 return NULL;
954 }
955 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
956 return NULL;
957}
958
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000959PyDoc_STRVAR(remove_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000960"remove(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000961\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000962Remove the first occurence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000963
964static PyObject *
965array_pop(arrayobject *self, PyObject *args)
966{
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000967 Py_ssize_t i = -1;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000968 PyObject *v;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000969 if (!PyArg_ParseTuple(args, "|n:pop", &i))
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000970 return NULL;
971 if (self->ob_size == 0) {
972 /* Special-case most common failure cause */
973 PyErr_SetString(PyExc_IndexError, "pop from empty array");
974 return NULL;
975 }
976 if (i < 0)
977 i += self->ob_size;
978 if (i < 0 || i >= self->ob_size) {
979 PyErr_SetString(PyExc_IndexError, "pop index out of range");
980 return NULL;
981 }
982 v = getarrayitem((PyObject *)self,i);
983 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
984 Py_DECREF(v);
985 return NULL;
986 }
987 return v;
988}
989
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000990PyDoc_STRVAR(pop_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000991"pop([i])\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000992\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000993Return the i-th element and delete it from the array. i defaults to -1.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000994
995static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000996array_extend(arrayobject *self, PyObject *bb)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000997{
Martin v. Löwis99866332002-03-01 10:27:01 +0000998 if (array_do_extend(self, bb) == -1)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000999 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001000 Py_INCREF(Py_None);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001001 return Py_None;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001002}
1003
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001004PyDoc_STRVAR(extend_doc,
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001005"extend(array or iterable)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001006\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001007 Append items to the end of the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001008
1009static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001010array_insert(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001011{
Thomas Woutersb9eb5512006-02-27 19:44:56 +00001012 Py_ssize_t i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001013 PyObject *v;
Thomas Woutersb9eb5512006-02-27 19:44:56 +00001014 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
Guido van Rossum778983b1993-02-19 15:55:02 +00001015 return NULL;
1016 return ins(self, i, v);
1017}
1018
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001019PyDoc_STRVAR(insert_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001020"insert(i,x)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001021\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001022Insert a new item x into the array before position i.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001023
1024
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001025static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001026array_buffer_info(arrayobject *self, PyObject *unused)
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001027{
Tim Peters077a11d2000-09-16 22:31:29 +00001028 PyObject* retval = NULL;
Tim Peters077a11d2000-09-16 22:31:29 +00001029 retval = PyTuple_New(2);
1030 if (!retval)
1031 return NULL;
Fred Drake541dc3b2000-06-28 17:49:30 +00001032
1033 PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item));
1034 PyTuple_SET_ITEM(retval, 1, PyInt_FromLong((long)(self->ob_size)));
1035
1036 return retval;
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001037}
1038
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001039PyDoc_STRVAR(buffer_info_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001040"buffer_info() -> (address, length)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001041\n\
1042Return a tuple (address, length) giving the current memory address and\n\
Guido van Rossum702d08e2001-07-27 16:05:32 +00001043the length in items of the buffer used to hold array's contents\n\
1044The length should be multiplied by the itemsize attribute to calculate\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001045the buffer length in bytes.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001046
1047
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001048static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001049array_append(arrayobject *self, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +00001050{
Guido van Rossum778983b1993-02-19 15:55:02 +00001051 return ins(self, (int) self->ob_size, v);
1052}
1053
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001054PyDoc_STRVAR(append_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001055"append(x)\n\
1056\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001057Append new value x to the end of the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001058
1059
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001060static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001061array_byteswap(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001062{
1063 char *p;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001064 Py_ssize_t i;
Fred Drakebf272981999-12-03 17:15:30 +00001065
Guido van Rossum778983b1993-02-19 15:55:02 +00001066 switch (self->ob_descr->itemsize) {
1067 case 1:
1068 break;
1069 case 2:
1070 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 2) {
1071 char p0 = p[0];
1072 p[0] = p[1];
1073 p[1] = p0;
1074 }
1075 break;
1076 case 4:
1077 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 4) {
1078 char p0 = p[0];
1079 char p1 = p[1];
1080 p[0] = p[3];
1081 p[1] = p[2];
1082 p[2] = p1;
1083 p[3] = p0;
1084 }
1085 break;
Guido van Rossume77a7571993-11-03 15:01:26 +00001086 case 8:
1087 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 8) {
1088 char p0 = p[0];
1089 char p1 = p[1];
1090 char p2 = p[2];
1091 char p3 = p[3];
1092 p[0] = p[7];
1093 p[1] = p[6];
1094 p[2] = p[5];
1095 p[3] = p[4];
1096 p[4] = p3;
1097 p[5] = p2;
1098 p[6] = p1;
1099 p[7] = p0;
1100 }
1101 break;
Guido van Rossum778983b1993-02-19 15:55:02 +00001102 default:
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001103 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossum778983b1993-02-19 15:55:02 +00001104 "don't know how to byteswap this array type");
1105 return NULL;
1106 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001107 Py_INCREF(Py_None);
1108 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001109}
1110
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001111PyDoc_STRVAR(byteswap_doc,
Fred Drakebf272981999-12-03 17:15:30 +00001112"byteswap()\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001113\n\
Fred Drakebf272981999-12-03 17:15:30 +00001114Byteswap all items of the array. If the items in the array are not 1, 2,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011154, or 8 bytes in size, RuntimeError is raised.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001116
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001117static PyObject *
Raymond Hettingerb0900e62004-12-16 16:23:40 +00001118array_reduce(arrayobject *array)
1119{
1120 PyObject *dict, *result;
1121
1122 dict = PyObject_GetAttrString((PyObject *)array, "__dict__");
1123 if (dict == NULL) {
1124 PyErr_Clear();
1125 dict = Py_None;
1126 Py_INCREF(dict);
1127 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001128 if (array->ob_size > 0) {
1129 result = Py_BuildValue("O(cs#)O",
1130 array->ob_type,
1131 array->ob_descr->typecode,
1132 array->ob_item,
1133 array->ob_size * array->ob_descr->itemsize,
1134 dict);
1135 } else {
1136 result = Py_BuildValue("O(c)O",
1137 array->ob_type,
1138 array->ob_descr->typecode,
1139 dict);
1140 }
Raymond Hettingerb0900e62004-12-16 16:23:40 +00001141 Py_DECREF(dict);
1142 return result;
1143}
1144
1145PyDoc_STRVAR(array_doc, "Return state information for pickling.");
1146
1147static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001148array_reverse(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001149{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001150 register Py_ssize_t itemsize = self->ob_descr->itemsize;
Guido van Rossume77a7571993-11-03 15:01:26 +00001151 register char *p, *q;
Tim Peters077a11d2000-09-16 22:31:29 +00001152 /* little buffer to hold items while swapping */
1153 char tmp[256]; /* 8 is probably enough -- but why skimp */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001154 assert((size_t)itemsize <= sizeof(tmp));
Guido van Rossume77a7571993-11-03 15:01:26 +00001155
Guido van Rossum778983b1993-02-19 15:55:02 +00001156 if (self->ob_size > 1) {
Guido van Rossume77a7571993-11-03 15:01:26 +00001157 for (p = self->ob_item,
1158 q = self->ob_item + (self->ob_size - 1)*itemsize;
1159 p < q;
1160 p += itemsize, q -= itemsize) {
Tim Peters077a11d2000-09-16 22:31:29 +00001161 /* memory areas guaranteed disjoint, so memcpy
1162 * is safe (& memmove may be slower).
1163 */
1164 memcpy(tmp, p, itemsize);
1165 memcpy(p, q, itemsize);
1166 memcpy(q, tmp, itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001167 }
1168 }
Tim Petersbb307342000-09-10 05:22:54 +00001169
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001170 Py_INCREF(Py_None);
1171 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001172}
Guido van Rossume77a7571993-11-03 15:01:26 +00001173
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001174PyDoc_STRVAR(reverse_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001175"reverse()\n\
1176\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001177Reverse the order of the items in the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001178
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001179
1180/* Forward */
1181static PyObject *array_fromstring(arrayobject *self, PyObject *args);
1182
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001183static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001184array_fromfile(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001185{
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001186 PyObject *f, *b, *res;
1187 Py_ssize_t itemsize = self->ob_descr->itemsize;
1188 Py_ssize_t n, nbytes;
1189
Thomas Wouters7a2f83b2006-02-16 17:07:41 +00001190 if (!PyArg_ParseTuple(args, "On:fromfile", &f, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +00001191 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001192
1193 nbytes = n * itemsize;
1194 if (nbytes < 0 || nbytes/itemsize != n) {
1195 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001196 return NULL;
1197 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001198
1199 b = PyObject_CallMethod(f, "read", "n", nbytes);
1200 if (b == NULL)
1201 return NULL;
1202
1203 if (!PyBytes_Check(b)) {
1204 PyErr_SetString(PyExc_TypeError,
1205 "read() didn't return bytes");
1206 Py_DECREF(b);
1207 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001208 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001209
1210 if (PyBytes_GET_SIZE(b) != nbytes) {
1211 printf("nbytes = %d, len(b) == %d\n", nbytes, PyBytes_GET_SIZE(b));
1212 PyErr_SetString(PyExc_EOFError,
1213 "read() didn't return enough bytes");
1214 Py_DECREF(b);
1215 return NULL;
1216 }
1217
1218 args = Py_BuildValue("(O)", b);
1219 Py_DECREF(b);
1220 if (args == NULL)
1221 return NULL;
1222
1223 res = array_fromstring(self, args);
1224 Py_DECREF(args);
1225
1226 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +00001227}
1228
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001229PyDoc_STRVAR(fromfile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001230"fromfile(f, n)\n\
1231\n\
1232Read 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 +00001233array. Also called as read.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001234
1235
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001236static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001237array_tofile(arrayobject *self, PyObject *f)
Guido van Rossum778983b1993-02-19 15:55:02 +00001238{
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001239 Py_ssize_t nbytes = self->ob_size * self->ob_descr->itemsize;
1240 /* Write 64K blocks at a time */
1241 /* XXX Make the block size settable */
1242 int BLOCKSIZE = 64*1024;
1243 Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE;
1244 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001245
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001246 if (self->ob_size == 0)
1247 goto done;
1248
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001249 for (i = 0; i < nblocks; i++) {
1250 char* ptr = self->ob_item + i*BLOCKSIZE;
1251 Py_ssize_t size = BLOCKSIZE;
1252 PyObject *bytes, *res;
1253 if (i*BLOCKSIZE + size > nbytes)
1254 size = nbytes - i*BLOCKSIZE;
1255 bytes = PyBytes_FromStringAndSize(ptr, size);
1256 if (bytes == NULL)
Guido van Rossum778983b1993-02-19 15:55:02 +00001257 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001258 res = PyObject_CallMethod(f, "write", "O", bytes);
1259 Py_DECREF(bytes);
1260 if (res == NULL)
1261 return NULL;
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001262 }
1263
1264 done:
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001265 Py_INCREF(Py_None);
1266 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001267}
1268
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001269PyDoc_STRVAR(tofile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001270"tofile(f)\n\
1271\n\
1272Write all items (as machine values) to the file object f. Also called as\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001273write.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001274
1275
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001276static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001277array_fromlist(arrayobject *self, PyObject *list)
Guido van Rossum778983b1993-02-19 15:55:02 +00001278{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001279 Py_ssize_t n;
1280 Py_ssize_t itemsize = self->ob_descr->itemsize;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001281
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001282 if (!PyList_Check(list)) {
1283 PyErr_SetString(PyExc_TypeError, "arg must be list");
Guido van Rossum778983b1993-02-19 15:55:02 +00001284 return NULL;
1285 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001286 n = PyList_Size(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001287 if (n > 0) {
1288 char *item = self->ob_item;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001289 Py_ssize_t i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001290 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001291 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001292 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001293 return NULL;
1294 }
1295 self->ob_item = item;
1296 self->ob_size += n;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +00001297 self->allocated = self->ob_size;
Guido van Rossum778983b1993-02-19 15:55:02 +00001298 for (i = 0; i < n; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001299 PyObject *v = PyList_GetItem(list, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001300 if ((*self->ob_descr->setitem)(self,
1301 self->ob_size - n + i, v) != 0) {
1302 self->ob_size -= n;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001303 PyMem_RESIZE(item, char,
1304 self->ob_size * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001305 self->ob_item = item;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +00001306 self->allocated = self->ob_size;
Guido van Rossum778983b1993-02-19 15:55:02 +00001307 return NULL;
1308 }
1309 }
1310 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001311 Py_INCREF(Py_None);
1312 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001313}
1314
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001315PyDoc_STRVAR(fromlist_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001316"fromlist(list)\n\
1317\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001318Append items to array from list.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001319
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);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001324 Py_ssize_t 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;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001349 Py_ssize_t n;
Guido van Rossum778983b1993-02-19 15:55:02 +00001350 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\
Walter Dörwald93b30b52007-06-22 12:21:53 +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{
Guido van Rossum31f72d72007-06-18 18:44:28 +00001386 return PyBytes_FromStringAndSize(self->ob_item,
1387 self->ob_size * self->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001388}
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
Martin v. Löwis99866332002-03-01 10:27:01 +00001398static PyObject *
1399array_fromunicode(arrayobject *self, PyObject *args)
1400{
1401 Py_UNICODE *ustr;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001402 Py_ssize_t n;
Martin v. Löwis99866332002-03-01 10:27:01 +00001403
1404 if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
1405 return NULL;
1406 if (self->ob_descr->typecode != 'u') {
1407 PyErr_SetString(PyExc_ValueError,
1408 "fromunicode() may only be called on "
1409 "type 'u' arrays");
1410 return NULL;
1411 }
1412 if (n > 0) {
1413 Py_UNICODE *item = (Py_UNICODE *) self->ob_item;
1414 PyMem_RESIZE(item, Py_UNICODE, self->ob_size + n);
1415 if (item == NULL) {
1416 PyErr_NoMemory();
1417 return NULL;
1418 }
1419 self->ob_item = (char *) item;
1420 self->ob_size += n;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +00001421 self->allocated = self->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +00001422 memcpy(item + self->ob_size - n,
1423 ustr, n * sizeof(Py_UNICODE));
1424 }
1425
1426 Py_INCREF(Py_None);
1427 return Py_None;
1428}
1429
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001430PyDoc_STRVAR(fromunicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001431"fromunicode(ustr)\n\
1432\n\
1433Extends this array with data from the unicode string ustr.\n\
1434The array must be a type 'u' array; otherwise a ValueError\n\
1435is raised. Use array.fromstring(ustr.decode(...)) to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001436append Unicode data to an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001437
1438
1439static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001440array_tounicode(arrayobject *self, PyObject *unused)
Martin v. Löwis99866332002-03-01 10:27:01 +00001441{
Martin v. Löwis99866332002-03-01 10:27:01 +00001442 if (self->ob_descr->typecode != 'u') {
1443 PyErr_SetString(PyExc_ValueError,
1444 "tounicode() may only be called on type 'u' arrays");
1445 return NULL;
1446 }
1447 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, self->ob_size);
1448}
1449
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001450PyDoc_STRVAR(tounicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001451"tounicode() -> unicode\n\
1452\n\
1453Convert the array to a unicode string. The array must be\n\
1454a type 'u' array; otherwise a ValueError is raised. Use\n\
1455array.tostring().decode() to obtain a unicode string from\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001456an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001457
Martin v. Löwis99866332002-03-01 10:27:01 +00001458
1459
1460static PyObject *
1461array_get_typecode(arrayobject *a, void *closure)
1462{
1463 char tc = a->ob_descr->typecode;
Walter Dörwald93b30b52007-06-22 12:21:53 +00001464 return PyUnicode_FromStringAndSize(&tc, 1);
Martin v. Löwis99866332002-03-01 10:27:01 +00001465}
1466
1467static PyObject *
1468array_get_itemsize(arrayobject *a, void *closure)
1469{
1470 return PyInt_FromLong((long)a->ob_descr->itemsize);
1471}
1472
1473static PyGetSetDef array_getsets [] = {
1474 {"typecode", (getter) array_get_typecode, NULL,
1475 "the typecode character used to create the array"},
1476 {"itemsize", (getter) array_get_itemsize, NULL,
1477 "the size, in bytes, of one array item"},
1478 {NULL}
1479};
1480
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001481PyMethodDef array_methods[] = {
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001482 {"append", (PyCFunction)array_append, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001483 append_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001484 {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001485 buffer_info_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001486 {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001487 byteswap_doc},
Raymond Hettinger3aa82c02004-03-13 18:18:51 +00001488 {"__copy__", (PyCFunction)array_copy, METH_NOARGS,
1489 copy_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001490 {"count", (PyCFunction)array_count, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001491 count_doc},
Thomas Wouters89f507f2006-12-13 04:49:30 +00001492 {"__deepcopy__",(PyCFunction)array_copy, METH_O,
Raymond Hettinger3aa82c02004-03-13 18:18:51 +00001493 copy_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001494 {"extend", (PyCFunction)array_extend, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001495 extend_doc},
1496 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
1497 fromfile_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001498 {"fromlist", (PyCFunction)array_fromlist, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001499 fromlist_doc},
1500 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
1501 fromstring_doc},
Martin v. Löwis99866332002-03-01 10:27:01 +00001502 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
1503 fromunicode_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001504 {"index", (PyCFunction)array_index, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001505 index_doc},
1506 {"insert", (PyCFunction)array_insert, METH_VARARGS,
1507 insert_doc},
1508 {"pop", (PyCFunction)array_pop, METH_VARARGS,
1509 pop_doc},
1510 {"read", (PyCFunction)array_fromfile, METH_VARARGS,
1511 fromfile_doc},
Raymond Hettingerb0900e62004-12-16 16:23:40 +00001512 {"__reduce__", (PyCFunction)array_reduce, METH_NOARGS,
1513 array_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001514 {"remove", (PyCFunction)array_remove, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001515 remove_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001516 {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001517 reverse_doc},
1518/* {"sort", (PyCFunction)array_sort, METH_VARARGS,
1519 sort_doc},*/
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001520 {"tofile", (PyCFunction)array_tofile, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001521 tofile_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001522 {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001523 tolist_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001524 {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001525 tostring_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001526 {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
Martin v. Löwis99866332002-03-01 10:27:01 +00001527 tounicode_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001528 {"write", (PyCFunction)array_tofile, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001529 tofile_doc},
Guido van Rossum778983b1993-02-19 15:55:02 +00001530 {NULL, NULL} /* sentinel */
1531};
1532
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001533static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001534array_repr(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +00001535{
Walter Dörwaldc2f6a582007-05-20 12:49:47 +00001536 char typecode;
Walter Dörwald7569dfe2007-05-19 21:49:49 +00001537 PyObject *s, *v = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001538 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00001539
Guido van Rossum778983b1993-02-19 15:55:02 +00001540 len = a->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +00001541 typecode = a->ob_descr->typecode;
Guido van Rossum778983b1993-02-19 15:55:02 +00001542 if (len == 0) {
Walter Dörwald7569dfe2007-05-19 21:49:49 +00001543 return PyUnicode_FromFormat("array('%c')", typecode);
Guido van Rossum778983b1993-02-19 15:55:02 +00001544 }
Guido van Rossum31f72d72007-06-18 18:44:28 +00001545 if (typecode == 'u')
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001546 v = array_tounicode(a, NULL);
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001547 else
1548 v = array_tolist(a, NULL);
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001549
Walter Dörwald7569dfe2007-05-19 21:49:49 +00001550 s = PyUnicode_FromFormat("array('%c', %R)", typecode, v);
1551 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001552 return s;
1553}
1554
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001555static PyObject*
1556array_subscr(arrayobject* self, PyObject* item)
1557{
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001558 if (PyIndex_Check(item)) {
1559 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001560 if (i==-1 && PyErr_Occurred()) {
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001561 return NULL;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001562 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001563 if (i < 0)
1564 i += self->ob_size;
1565 return array_item(self, i);
1566 }
1567 else if (PySlice_Check(item)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001568 Py_ssize_t start, stop, step, slicelength, cur, i;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001569 PyObject* result;
1570 arrayobject* ar;
1571 int itemsize = self->ob_descr->itemsize;
1572
1573 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
1574 &start, &stop, &step, &slicelength) < 0) {
1575 return NULL;
1576 }
1577
1578 if (slicelength <= 0) {
1579 return newarrayobject(&Arraytype, 0, self->ob_descr);
1580 }
1581 else {
1582 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
1583 if (!result) return NULL;
1584
1585 ar = (arrayobject*)result;
1586
1587 for (cur = start, i = 0; i < slicelength;
1588 cur += step, i++) {
1589 memcpy(ar->ob_item + i*itemsize,
1590 self->ob_item + cur*itemsize,
1591 itemsize);
1592 }
1593
1594 return result;
1595 }
1596 }
1597 else {
1598 PyErr_SetString(PyExc_TypeError,
1599 "list indices must be integers");
1600 return NULL;
1601 }
1602}
1603
1604static int
1605array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
1606{
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001607 if (PyIndex_Check(item)) {
1608 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001609 if (i==-1 && PyErr_Occurred())
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001610 return -1;
1611 if (i < 0)
1612 i += self->ob_size;
1613 return array_ass_item(self, i, value);
1614 }
1615 else if (PySlice_Check(item)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001616 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001617 int itemsize = self->ob_descr->itemsize;
1618
1619 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
1620 &start, &stop, &step, &slicelength) < 0) {
1621 return -1;
1622 }
1623
1624 /* treat A[slice(a,b)] = v _exactly_ like A[a:b] = v */
1625 if (step == 1 && ((PySliceObject*)item)->step == Py_None)
1626 return array_ass_slice(self, start, stop, value);
1627
1628 if (value == NULL) {
1629 /* delete slice */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001630 Py_ssize_t cur, i, extra;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001631
1632 if (slicelength <= 0)
1633 return 0;
1634
1635 if (step < 0) {
1636 stop = start + 1;
1637 start = stop + step*(slicelength - 1) - 1;
1638 step = -step;
1639 }
1640
Michael W. Hudson56796f62002-07-29 14:35:04 +00001641 for (cur = start, i = 0; i < slicelength - 1;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001642 cur += step, i++) {
1643 memmove(self->ob_item + (cur - i)*itemsize,
1644 self->ob_item + (cur + 1)*itemsize,
1645 (step - 1) * itemsize);
1646 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00001647 extra = self->ob_size - (cur + 1);
1648 if (extra > 0) {
1649 memmove(self->ob_item + (cur - i)*itemsize,
1650 self->ob_item + (cur + 1)*itemsize,
1651 extra*itemsize);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001652 }
1653
1654 self->ob_size -= slicelength;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001655 self->ob_item = (char *)PyMem_REALLOC(self->ob_item,
1656 itemsize*self->ob_size);
Raymond Hettinger6e2ee862004-03-14 04:37:50 +00001657 self->allocated = self->ob_size;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001658
1659 return 0;
1660 }
1661 else {
1662 /* assign slice */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001663 Py_ssize_t cur, i;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001664 arrayobject* av;
1665
1666 if (!array_Check(value)) {
1667 PyErr_Format(PyExc_TypeError,
1668 "must assign array (not \"%.200s\") to slice",
1669 value->ob_type->tp_name);
1670 return -1;
1671 }
1672
1673 av = (arrayobject*)value;
1674
1675 if (av->ob_size != slicelength) {
1676 PyErr_Format(PyExc_ValueError,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001677 "attempt to assign array of size %ld to extended slice of size %ld",
1678 /*XXX*/(long)av->ob_size, /*XXX*/(long)slicelength);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001679 return -1;
1680 }
1681
1682 if (!slicelength)
1683 return 0;
1684
1685 /* protect against a[::-1] = a */
1686 if (self == av) {
1687 value = array_slice(av, 0, av->ob_size);
1688 av = (arrayobject*)value;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001689 if (!av)
1690 return -1;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001691 }
1692 else {
1693 Py_INCREF(value);
1694 }
1695
1696 for (cur = start, i = 0; i < slicelength;
1697 cur += step, i++) {
1698 memcpy(self->ob_item + cur*itemsize,
1699 av->ob_item + i*itemsize,
1700 itemsize);
1701 }
1702
1703 Py_DECREF(value);
1704
1705 return 0;
1706 }
1707 }
1708 else {
1709 PyErr_SetString(PyExc_TypeError,
1710 "list indices must be integers");
1711 return -1;
1712 }
1713}
1714
1715static PyMappingMethods array_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001716 (lenfunc)array_length,
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001717 (binaryfunc)array_subscr,
1718 (objobjargproc)array_ass_subscr
1719};
1720
Guido van Rossumd8faa362007-04-27 19:54:29 +00001721static const void *emptybuf = "";
1722
Martin v. Löwis18e16552006-02-15 17:27:45 +00001723static Py_ssize_t
1724array_buffer_getreadbuf(arrayobject *self, Py_ssize_t index, const void **ptr)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001725{
1726 if ( index != 0 ) {
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001727 PyErr_SetString(PyExc_SystemError,
1728 "Accessing non-existent array segment");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001729 return -1;
1730 }
1731 *ptr = (void *)self->ob_item;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001732 if (*ptr == NULL)
1733 *ptr = emptybuf;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001734 return self->ob_size*self->ob_descr->itemsize;
1735}
1736
Martin v. Löwis18e16552006-02-15 17:27:45 +00001737static Py_ssize_t
1738array_buffer_getwritebuf(arrayobject *self, Py_ssize_t index, const void **ptr)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001739{
1740 if ( index != 0 ) {
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001741 PyErr_SetString(PyExc_SystemError,
1742 "Accessing non-existent array segment");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001743 return -1;
1744 }
1745 *ptr = (void *)self->ob_item;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001746 if (*ptr == NULL)
1747 *ptr = emptybuf;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001748 return self->ob_size*self->ob_descr->itemsize;
1749}
1750
Martin v. Löwis18e16552006-02-15 17:27:45 +00001751static Py_ssize_t
1752array_buffer_getsegcount(arrayobject *self, Py_ssize_t *lenp)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001753{
1754 if ( lenp )
1755 *lenp = self->ob_size*self->ob_descr->itemsize;
1756 return 1;
1757}
1758
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001759static PySequenceMethods array_as_sequence = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001760 (lenfunc)array_length, /*sq_length*/
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001761 (binaryfunc)array_concat, /*sq_concat*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001762 (ssizeargfunc)array_repeat, /*sq_repeat*/
1763 (ssizeargfunc)array_item, /*sq_item*/
1764 (ssizessizeargfunc)array_slice, /*sq_slice*/
1765 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
1766 (ssizessizeobjargproc)array_ass_slice, /*sq_ass_slice*/
Raymond Hettinger625812f2003-01-07 01:58:52 +00001767 (objobjproc)array_contains, /*sq_contains*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001768 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001769 (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001770};
1771
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001772static PyBufferProcs array_as_buffer = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001773 (readbufferproc)array_buffer_getreadbuf,
1774 (writebufferproc)array_buffer_getwritebuf,
1775 (segcountproc)array_buffer_getsegcount,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001776 NULL,
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001777};
1778
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001779static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +00001780array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum778983b1993-02-19 15:55:02 +00001781{
1782 char c;
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001783 PyObject *initial = NULL, *it = NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001784 struct arraydescr *descr;
Georg Brandl02c42872005-08-26 06:42:30 +00001785
Thomas Woutersb2137042007-02-01 18:02:27 +00001786 if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001787 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001788
1789 if (!PyArg_ParseTuple(args, "c|O:array", &c, &initial))
1790 return NULL;
1791
1792 if (!(initial == NULL || PyList_Check(initial)
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001793 || PyString_Check(initial) || PyTuple_Check(initial)
Martin v. Löwis99866332002-03-01 10:27:01 +00001794 || (c == 'u' && PyUnicode_Check(initial)))) {
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001795 it = PyObject_GetIter(initial);
1796 if (it == NULL)
1797 return NULL;
1798 /* We set initial to NULL so that the subsequent code
1799 will create an empty array of the appropriate type
1800 and afterwards we can use array_iter_extend to populate
1801 the array.
1802 */
1803 initial = NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001804 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001805 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1806 if (descr->typecode == c) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001807 PyObject *a;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001808 Py_ssize_t len;
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001809
1810 if (initial == NULL || !(PyList_Check(initial)
1811 || PyTuple_Check(initial)))
Guido van Rossum778983b1993-02-19 15:55:02 +00001812 len = 0;
1813 else
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001814 len = PySequence_Size(initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001815
1816 a = newarrayobject(type, len, descr);
Guido van Rossum778983b1993-02-19 15:55:02 +00001817 if (a == NULL)
1818 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001819
Guido van Rossum778983b1993-02-19 15:55:02 +00001820 if (len > 0) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001821 Py_ssize_t i;
Guido van Rossum778983b1993-02-19 15:55:02 +00001822 for (i = 0; i < len; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001823 PyObject *v =
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001824 PySequence_GetItem(initial, i);
Raymond Hettinger85004cc2003-08-05 11:23:59 +00001825 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001826 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001827 return NULL;
1828 }
Raymond Hettinger85004cc2003-08-05 11:23:59 +00001829 if (setarrayitem(a, i, v) != 0) {
1830 Py_DECREF(v);
1831 Py_DECREF(a);
1832 return NULL;
1833 }
1834 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001835 }
Raymond Hettinger85004cc2003-08-05 11:23:59 +00001836 } else if (initial != NULL && PyString_Check(initial)) {
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001837 PyObject *t_initial, *v;
1838 t_initial = PyTuple_Pack(1, initial);
1839 if (t_initial == NULL) {
1840 Py_DECREF(a);
1841 return NULL;
1842 }
1843 v = array_fromstring((arrayobject *)a,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001844 t_initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001845 Py_DECREF(t_initial);
Guido van Rossum778983b1993-02-19 15:55:02 +00001846 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001847 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001848 return NULL;
1849 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001850 Py_DECREF(v);
Martin v. Löwis99866332002-03-01 10:27:01 +00001851 } else if (initial != NULL && PyUnicode_Check(initial)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001852 Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001853 if (n > 0) {
1854 arrayobject *self = (arrayobject *)a;
1855 char *item = self->ob_item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001856 item = (char *)PyMem_Realloc(item, n);
Martin v. Löwis99866332002-03-01 10:27:01 +00001857 if (item == NULL) {
1858 PyErr_NoMemory();
1859 Py_DECREF(a);
1860 return NULL;
1861 }
1862 self->ob_item = item;
1863 self->ob_size = n / sizeof(Py_UNICODE);
1864 memcpy(item, PyUnicode_AS_DATA(initial), n);
Raymond Hettinger6e2ee862004-03-14 04:37:50 +00001865 self->allocated = self->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +00001866 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001867 }
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001868 if (it != NULL) {
1869 if (array_iter_extend((arrayobject *)a, it) == -1) {
1870 Py_DECREF(it);
1871 Py_DECREF(a);
1872 return NULL;
1873 }
1874 Py_DECREF(it);
1875 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001876 return a;
1877 }
1878 }
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001879 PyErr_SetString(PyExc_ValueError,
Guido van Rossum31f72d72007-06-18 18:44:28 +00001880 "bad typecode (must be b, B, u, h, H, i, I, l, L, f or d)");
Guido van Rossum778983b1993-02-19 15:55:02 +00001881 return NULL;
1882}
1883
Guido van Rossum778983b1993-02-19 15:55:02 +00001884
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001885PyDoc_STRVAR(module_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001886"This module defines an object type which can efficiently represent\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001887an array of basic values: characters, integers, floating point\n\
1888numbers. Arrays are sequence types and behave very much like lists,\n\
1889except that the type of objects stored in them is constrained. The\n\
1890type is specified at object creation time by using a type code, which\n\
1891is a single character. The following type codes are defined:\n\
1892\n\
1893 Type code C Type Minimum size in bytes \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001894 'b' signed integer 1 \n\
1895 'B' unsigned integer 1 \n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001896 'u' Unicode character 2 \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001897 'h' signed integer 2 \n\
1898 'H' unsigned integer 2 \n\
1899 'i' signed integer 2 \n\
1900 'I' unsigned integer 2 \n\
1901 'l' signed integer 4 \n\
1902 'L' unsigned integer 4 \n\
1903 'f' floating point 4 \n\
1904 'd' floating point 8 \n\
1905\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001906The constructor is:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001907\n\
1908array(typecode [, initializer]) -- create a new array\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001909");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001910
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001911PyDoc_STRVAR(arraytype_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001912"array(typecode [, initializer]) -> array\n\
1913\n\
1914Return a new array whose items are restricted by typecode, and\n\
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001915initialized from the optional initializer value, which must be a list,\n\
1916string. or iterable over elements of the appropriate type.\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001917\n\
1918Arrays represent basic values and behave very much like lists, except\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001919the type of objects stored in them is constrained.\n\
1920\n\
1921Methods:\n\
1922\n\
1923append() -- append a new item to the end of the array\n\
1924buffer_info() -- return information giving the current memory info\n\
1925byteswap() -- byteswap all the items of the array\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001926count() -- return number of occurences of an object\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001927extend() -- extend array by appending multiple elements from an iterable\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001928fromfile() -- read items from a file object\n\
1929fromlist() -- append items from the list\n\
1930fromstring() -- append items from the string\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001931index() -- return index of first occurence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001932insert() -- insert a new item into the array at a provided position\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001933pop() -- remove and return item (default last)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001934read() -- DEPRECATED, use fromfile()\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001935remove() -- remove first occurence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001936reverse() -- reverse the order of the items in the array\n\
1937tofile() -- write all items to a file object\n\
1938tolist() -- return the array converted to an ordinary list\n\
1939tostring() -- return the array converted to a string\n\
1940write() -- DEPRECATED, use tofile()\n\
1941\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001942Attributes:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001943\n\
1944typecode -- the typecode character used to create the array\n\
1945itemsize -- the length in bytes of one array item\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001946");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001947
Raymond Hettinger625812f2003-01-07 01:58:52 +00001948static PyObject *array_iter(arrayobject *ao);
1949
Tim Peters0c322792002-07-17 16:49:03 +00001950static PyTypeObject Arraytype = {
Fred Drake0d40ba42000-02-04 20:33:49 +00001951 PyObject_HEAD_INIT(NULL)
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001952 0,
Guido van Rossum14648392001-12-08 18:02:58 +00001953 "array.array",
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001954 sizeof(arrayobject),
1955 0,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001956 (destructor)array_dealloc, /* tp_dealloc */
Martin v. Löwisaa158be2002-03-04 09:38:52 +00001957 0, /* tp_print */
Martin v. Löwis99866332002-03-01 10:27:01 +00001958 0, /* tp_getattr */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001959 0, /* tp_setattr */
1960 0, /* tp_compare */
1961 (reprfunc)array_repr, /* tp_repr */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001962 0, /* tp_as_number*/
1963 &array_as_sequence, /* tp_as_sequence*/
1964 &array_as_mapping, /* tp_as_mapping*/
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001965 0, /* tp_hash */
1966 0, /* tp_call */
1967 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00001968 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001969 0, /* tp_setattro */
Tim Petersb870c752001-06-05 04:43:26 +00001970 &array_as_buffer, /* tp_as_buffer*/
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001971 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001972 arraytype_doc, /* tp_doc */
1973 0, /* tp_traverse */
1974 0, /* tp_clear */
1975 array_richcompare, /* tp_richcompare */
Raymond Hettingercb87bc82004-05-31 00:35:52 +00001976 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger625812f2003-01-07 01:58:52 +00001977 (getiterfunc)array_iter, /* tp_iter */
Martin v. Löwis99866332002-03-01 10:27:01 +00001978 0, /* tp_iternext */
1979 array_methods, /* tp_methods */
1980 0, /* tp_members */
1981 array_getsets, /* tp_getset */
1982 0, /* tp_base */
1983 0, /* tp_dict */
1984 0, /* tp_descr_get */
1985 0, /* tp_descr_set */
1986 0, /* tp_dictoffset */
1987 0, /* tp_init */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00001988 PyType_GenericAlloc, /* tp_alloc */
Martin v. Löwis99866332002-03-01 10:27:01 +00001989 array_new, /* tp_new */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00001990 PyObject_Del, /* tp_free */
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001991};
1992
Raymond Hettinger625812f2003-01-07 01:58:52 +00001993
1994/*********************** Array Iterator **************************/
1995
1996typedef struct {
1997 PyObject_HEAD
Martin v. Löwis18e16552006-02-15 17:27:45 +00001998 Py_ssize_t index;
Raymond Hettinger625812f2003-01-07 01:58:52 +00001999 arrayobject *ao;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002000 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002001} arrayiterobject;
2002
2003static PyTypeObject PyArrayIter_Type;
2004
2005#define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
2006
2007static PyObject *
2008array_iter(arrayobject *ao)
2009{
2010 arrayiterobject *it;
2011
2012 if (!array_Check(ao)) {
2013 PyErr_BadInternalCall();
2014 return NULL;
2015 }
2016
2017 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
2018 if (it == NULL)
2019 return NULL;
2020
2021 Py_INCREF(ao);
2022 it->ao = ao;
2023 it->index = 0;
2024 it->getitem = ao->ob_descr->getitem;
2025 PyObject_GC_Track(it);
2026 return (PyObject *)it;
2027}
2028
2029static PyObject *
Raymond Hettinger625812f2003-01-07 01:58:52 +00002030arrayiter_next(arrayiterobject *it)
2031{
2032 assert(PyArrayIter_Check(it));
2033 if (it->index < it->ao->ob_size)
2034 return (*it->getitem)(it->ao, it->index++);
2035 return NULL;
2036}
2037
2038static void
2039arrayiter_dealloc(arrayiterobject *it)
2040{
2041 PyObject_GC_UnTrack(it);
2042 Py_XDECREF(it->ao);
2043 PyObject_GC_Del(it);
2044}
2045
2046static int
2047arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2048{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002049 Py_VISIT(it->ao);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002050 return 0;
2051}
2052
2053static PyTypeObject PyArrayIter_Type = {
Jason Tishler84375702003-02-10 20:45:47 +00002054 PyObject_HEAD_INIT(NULL)
Raymond Hettinger625812f2003-01-07 01:58:52 +00002055 0, /* ob_size */
2056 "arrayiterator", /* tp_name */
2057 sizeof(arrayiterobject), /* tp_basicsize */
2058 0, /* tp_itemsize */
2059 /* methods */
2060 (destructor)arrayiter_dealloc, /* tp_dealloc */
2061 0, /* tp_print */
2062 0, /* tp_getattr */
2063 0, /* tp_setattr */
2064 0, /* tp_compare */
2065 0, /* tp_repr */
2066 0, /* tp_as_number */
2067 0, /* tp_as_sequence */
2068 0, /* tp_as_mapping */
2069 0, /* tp_hash */
2070 0, /* tp_call */
2071 0, /* tp_str */
2072 PyObject_GenericGetAttr, /* tp_getattro */
2073 0, /* tp_setattro */
2074 0, /* tp_as_buffer */
2075 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2076 0, /* tp_doc */
2077 (traverseproc)arrayiter_traverse, /* tp_traverse */
2078 0, /* tp_clear */
2079 0, /* tp_richcompare */
2080 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002081 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger625812f2003-01-07 01:58:52 +00002082 (iternextfunc)arrayiter_next, /* tp_iternext */
2083 0, /* tp_methods */
2084};
2085
2086
2087/*********************** Install Module **************************/
2088
Martin v. Löwis99866332002-03-01 10:27:01 +00002089/* No functions in array module. */
2090static PyMethodDef a_methods[] = {
2091 {NULL, NULL, 0, NULL} /* Sentinel */
2092};
2093
2094
Mark Hammondfe51c6d2002-08-02 02:27:13 +00002095PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00002096initarray(void)
Guido van Rossum778983b1993-02-19 15:55:02 +00002097{
Fred Drakef4e34842002-04-01 03:45:06 +00002098 PyObject *m;
Fred Drake0d40ba42000-02-04 20:33:49 +00002099
Guido van Rossum50e9fb92006-08-17 05:42:55 +00002100 if (PyType_Ready(&Arraytype) < 0)
2101 return;
Jason Tishler84375702003-02-10 20:45:47 +00002102 PyArrayIter_Type.ob_type = &PyType_Type;
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002103 m = Py_InitModule3("array", a_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00002104 if (m == NULL)
2105 return;
Fred Drakef4e34842002-04-01 03:45:06 +00002106
2107 Py_INCREF((PyObject *)&Arraytype);
2108 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
2109 Py_INCREF((PyObject *)&Arraytype);
2110 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
Guido van Rossuma0deb641998-10-14 13:45:06 +00002111 /* No need to check the error here, the caller will do that */
Guido van Rossum778983b1993-02-19 15:55:02 +00002112}