blob: edd1530e2c540841f69ed1348b123ccac188100e [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 +0000105c_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000106{
Guido van Rossum1f05a3b2007-05-10 18:41:30 +0000107 Py_UNICODE buf[1];
108 buf[0] = ((unsigned char *)ap->ob_item)[i];
109 return PyUnicode_FromUnicode(buf, 1);
Guido van Rossum778983b1993-02-19 15:55:02 +0000110}
111
112static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000113c_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000114{
115 char x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000116 if (!PyArg_Parse(v, "c;array item must be char", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000117 return -1;
118 if (i >= 0)
Martin v. Löwis99866332002-03-01 10:27:01 +0000119 ((char *)ap->ob_item)[i] = x;
Guido van Rossum778983b1993-02-19 15:55:02 +0000120 return 0;
121}
122
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000123static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000124b_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000125{
126 long x = ((char *)ap->ob_item)[i];
127 if (x >= 128)
128 x -= 256;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000129 return PyInt_FromLong(x);
Guido van Rossum778983b1993-02-19 15:55:02 +0000130}
131
132static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000133b_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000134{
Fred Drake541dc3b2000-06-28 17:49:30 +0000135 short x;
136 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
137 must use the next size up that is signed ('h') and manually do
138 the overflow checking */
139 if (!PyArg_Parse(v, "h;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000140 return -1;
Guido van Rossum9f754e02000-07-01 00:38:19 +0000141 else if (x < -128) {
Fred Drake541dc3b2000-06-28 17:49:30 +0000142 PyErr_SetString(PyExc_OverflowError,
143 "signed char is less than minimum");
144 return -1;
145 }
Guido van Rossum9f754e02000-07-01 00:38:19 +0000146 else if (x > 127) {
Fred Drake541dc3b2000-06-28 17:49:30 +0000147 PyErr_SetString(PyExc_OverflowError,
148 "signed char is greater than maximum");
149 return -1;
150 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000151 if (i >= 0)
Fred Drake541dc3b2000-06-28 17:49:30 +0000152 ((char *)ap->ob_item)[i] = (char)x;
Guido van Rossum778983b1993-02-19 15:55:02 +0000153 return 0;
154}
155
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000156static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000157BB_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000158{
159 long x = ((unsigned char *)ap->ob_item)[i];
160 return PyInt_FromLong(x);
161}
162
Fred Drake541dc3b2000-06-28 17:49:30 +0000163static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000164BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000165{
166 unsigned char x;
167 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
168 if (!PyArg_Parse(v, "b;array item must be integer", &x))
169 return -1;
170 if (i >= 0)
Martin v. Löwis99866332002-03-01 10:27:01 +0000171 ((char *)ap->ob_item)[i] = x;
Fred Drake541dc3b2000-06-28 17:49:30 +0000172 return 0;
173}
Guido van Rossum549ab711997-01-03 19:09:47 +0000174
Martin v. Löwis99866332002-03-01 10:27:01 +0000175static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000176u_getitem(arrayobject *ap, Py_ssize_t i)
Martin v. Löwis99866332002-03-01 10:27:01 +0000177{
178 return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1);
179}
180
181static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000182u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Martin v. Löwis99866332002-03-01 10:27:01 +0000183{
184 Py_UNICODE *p;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000185 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +0000186
187 if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
188 return -1;
189 if (len != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000190 PyErr_SetString(PyExc_TypeError,
191 "array item must be unicode character");
Martin v. Löwis99866332002-03-01 10:27:01 +0000192 return -1;
193 }
194 if (i >= 0)
195 ((Py_UNICODE *)ap->ob_item)[i] = p[0];
196 return 0;
197}
Martin v. Löwis99866332002-03-01 10:27:01 +0000198
Guido van Rossum549ab711997-01-03 19:09:47 +0000199static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000200h_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000201{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000202 return PyInt_FromLong((long) ((short *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000203}
204
205static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000206h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000207{
208 short x;
Fred Drake541dc3b2000-06-28 17:49:30 +0000209 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000210 if (!PyArg_Parse(v, "h;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000211 return -1;
212 if (i >= 0)
213 ((short *)ap->ob_item)[i] = x;
214 return 0;
215}
216
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000217static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000218HH_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000219{
220 return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
221}
222
Fred Drake541dc3b2000-06-28 17:49:30 +0000223static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000224HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000225{
226 int x;
227 /* PyArg_Parse's 'h' formatter is for a signed short, therefore
228 must use the next size up and manually do the overflow checking */
229 if (!PyArg_Parse(v, "i;array item must be integer", &x))
230 return -1;
231 else if (x < 0) {
232 PyErr_SetString(PyExc_OverflowError,
233 "unsigned short is less than minimum");
234 return -1;
235 }
236 else if (x > USHRT_MAX) {
237 PyErr_SetString(PyExc_OverflowError,
238 "unsigned short is greater than maximum");
239 return -1;
240 }
241 if (i >= 0)
242 ((short *)ap->ob_item)[i] = (short)x;
243 return 0;
244}
Guido van Rossum549ab711997-01-03 19:09:47 +0000245
246static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000247i_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossume77a7571993-11-03 15:01:26 +0000248{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000249 return PyInt_FromLong((long) ((int *)ap->ob_item)[i]);
Guido van Rossume77a7571993-11-03 15:01:26 +0000250}
251
252static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000253i_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossume77a7571993-11-03 15:01:26 +0000254{
255 int x;
Fred Drake541dc3b2000-06-28 17:49:30 +0000256 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000257 if (!PyArg_Parse(v, "i;array item must be integer", &x))
Guido van Rossume77a7571993-11-03 15:01:26 +0000258 return -1;
259 if (i >= 0)
260 ((int *)ap->ob_item)[i] = x;
261 return 0;
262}
263
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000264static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000265II_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000266{
267 return PyLong_FromUnsignedLong(
268 (unsigned long) ((unsigned int *)ap->ob_item)[i]);
269}
270
271static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000272II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000273{
274 unsigned long x;
275 if (PyLong_Check(v)) {
276 x = PyLong_AsUnsignedLong(v);
277 if (x == (unsigned long) -1 && PyErr_Occurred())
278 return -1;
279 }
280 else {
Fred Drake541dc3b2000-06-28 17:49:30 +0000281 long y;
282 if (!PyArg_Parse(v, "l;array item must be integer", &y))
Guido van Rossum549ab711997-01-03 19:09:47 +0000283 return -1;
Fred Drake541dc3b2000-06-28 17:49:30 +0000284 if (y < 0) {
285 PyErr_SetString(PyExc_OverflowError,
286 "unsigned int is less than minimum");
287 return -1;
288 }
289 x = (unsigned long)y;
Tim Petersbb307342000-09-10 05:22:54 +0000290
Guido van Rossum549ab711997-01-03 19:09:47 +0000291 }
Fred Drake541dc3b2000-06-28 17:49:30 +0000292 if (x > UINT_MAX) {
293 PyErr_SetString(PyExc_OverflowError,
294 "unsigned int is greater than maximum");
295 return -1;
296 }
297
Guido van Rossum549ab711997-01-03 19:09:47 +0000298 if (i >= 0)
Fred Drake541dc3b2000-06-28 17:49:30 +0000299 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
Guido van Rossum549ab711997-01-03 19:09:47 +0000300 return 0;
301}
302
303static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000304l_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000305{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000306 return PyInt_FromLong(((long *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000307}
308
309static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000310l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000311{
312 long x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000313 if (!PyArg_Parse(v, "l;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000314 return -1;
315 if (i >= 0)
316 ((long *)ap->ob_item)[i] = x;
317 return 0;
318}
319
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000320static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000321LL_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000322{
323 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
324}
325
326static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000327LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000328{
329 unsigned long x;
330 if (PyLong_Check(v)) {
331 x = PyLong_AsUnsignedLong(v);
332 if (x == (unsigned long) -1 && PyErr_Occurred())
333 return -1;
334 }
335 else {
Fred Drake541dc3b2000-06-28 17:49:30 +0000336 long y;
337 if (!PyArg_Parse(v, "l;array item must be integer", &y))
Guido van Rossum549ab711997-01-03 19:09:47 +0000338 return -1;
Fred Drake541dc3b2000-06-28 17:49:30 +0000339 if (y < 0) {
340 PyErr_SetString(PyExc_OverflowError,
341 "unsigned long is less than minimum");
342 return -1;
343 }
344 x = (unsigned long)y;
Tim Petersbb307342000-09-10 05:22:54 +0000345
Guido van Rossum549ab711997-01-03 19:09:47 +0000346 }
Fred Drake541dc3b2000-06-28 17:49:30 +0000347 if (x > ULONG_MAX) {
348 PyErr_SetString(PyExc_OverflowError,
349 "unsigned long is greater than maximum");
350 return -1;
351 }
Tim Petersbb307342000-09-10 05:22:54 +0000352
Guido van Rossum549ab711997-01-03 19:09:47 +0000353 if (i >= 0)
354 ((unsigned long *)ap->ob_item)[i] = x;
355 return 0;
356}
357
358static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000359f_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000360{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000361 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000362}
363
364static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000365f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000366{
367 float x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000368 if (!PyArg_Parse(v, "f;array item must be float", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000369 return -1;
370 if (i >= 0)
371 ((float *)ap->ob_item)[i] = x;
372 return 0;
373}
374
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000375static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000376d_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000377{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000378 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000379}
380
381static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000382d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000383{
384 double x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000385 if (!PyArg_Parse(v, "d;array item must be float", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000386 return -1;
387 if (i >= 0)
388 ((double *)ap->ob_item)[i] = x;
389 return 0;
390}
391
392/* Description of types */
Guido van Rossum234f9421993-06-17 12:35:49 +0000393static struct arraydescr descriptors[] = {
Guido van Rossum778983b1993-02-19 15:55:02 +0000394 {'c', sizeof(char), c_getitem, c_setitem},
395 {'b', sizeof(char), b_getitem, b_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000396 {'B', sizeof(char), BB_getitem, BB_setitem},
Martin v. Löwis99866332002-03-01 10:27:01 +0000397 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem},
Guido van Rossum778983b1993-02-19 15:55:02 +0000398 {'h', sizeof(short), h_getitem, h_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000399 {'H', sizeof(short), HH_getitem, HH_setitem},
Guido van Rossume77a7571993-11-03 15:01:26 +0000400 {'i', sizeof(int), i_getitem, i_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000401 {'I', sizeof(int), II_getitem, II_setitem},
Guido van Rossum778983b1993-02-19 15:55:02 +0000402 {'l', sizeof(long), l_getitem, l_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000403 {'L', sizeof(long), LL_getitem, LL_setitem},
Guido van Rossum778983b1993-02-19 15:55:02 +0000404 {'f', sizeof(float), f_getitem, f_setitem},
405 {'d', sizeof(double), d_getitem, d_setitem},
406 {'\0', 0, 0, 0} /* Sentinel */
407};
Tim Petersbb307342000-09-10 05:22:54 +0000408
409/****************************************************************************
410Implementations of array object methods.
411****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +0000412
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000413static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000414newarrayobject(PyTypeObject *type, Py_ssize_t size, struct arraydescr *descr)
Guido van Rossum778983b1993-02-19 15:55:02 +0000415{
Guido van Rossum778983b1993-02-19 15:55:02 +0000416 arrayobject *op;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000417 size_t nbytes;
Martin v. Löwis99866332002-03-01 10:27:01 +0000418
Guido van Rossum778983b1993-02-19 15:55:02 +0000419 if (size < 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000420 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000421 return NULL;
422 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000423
Guido van Rossum778983b1993-02-19 15:55:02 +0000424 nbytes = size * descr->itemsize;
425 /* Check for overflow */
Guido van Rossum7844e381997-04-11 20:44:04 +0000426 if (nbytes / descr->itemsize != (size_t)size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000427 return PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000428 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000429 op = (arrayobject *) type->tp_alloc(type, 0);
Guido van Rossum778983b1993-02-19 15:55:02 +0000430 if (op == NULL) {
Martin v. Löwis99866332002-03-01 10:27:01 +0000431 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +0000432 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000433 op->ob_size = size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000434 if (size <= 0) {
435 op->ob_item = NULL;
436 }
437 else {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000438 op->ob_item = PyMem_NEW(char, nbytes);
Guido van Rossum778983b1993-02-19 15:55:02 +0000439 if (op->ob_item == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000440 PyObject_Del(op);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000441 return PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000442 }
443 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000444 op->ob_descr = descr;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000445 op->allocated = size;
Raymond Hettingercb87bc82004-05-31 00:35:52 +0000446 op->weakreflist = NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000447 return (PyObject *) op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000448}
449
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000450static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000451getarrayitem(PyObject *op, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000452{
453 register arrayobject *ap;
Martin v. Löwis99866332002-03-01 10:27:01 +0000454 assert(array_Check(op));
Guido van Rossum778983b1993-02-19 15:55:02 +0000455 ap = (arrayobject *)op;
Walter Dörwald8bb1ae92003-05-23 10:01:07 +0000456 assert(i>=0 && i<ap->ob_size);
Guido van Rossum778983b1993-02-19 15:55:02 +0000457 return (*ap->ob_descr->getitem)(ap, i);
458}
459
460static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000461ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000462{
Guido van Rossum778983b1993-02-19 15:55:02 +0000463 char *items;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000464 Py_ssize_t n = self->ob_size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000465 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000466 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000467 return -1;
468 }
469 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
470 return -1;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000471
472 if (array_resize(self, n+1) == -1)
Guido van Rossum778983b1993-02-19 15:55:02 +0000473 return -1;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000474 items = self->ob_item;
Walter Dörwald9e46abe2003-05-18 03:15:10 +0000475 if (where < 0) {
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000476 where += n;
Walter Dörwald9e46abe2003-05-18 03:15:10 +0000477 if (where < 0)
478 where = 0;
479 }
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000480 if (where > n)
481 where = n;
482 /* appends don't need to call memmove() */
483 if (where != n)
484 memmove(items + (where+1)*self->ob_descr->itemsize,
485 items + where*self->ob_descr->itemsize,
486 (n-where)*self->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000487 return (*self->ob_descr->setitem)(self, where, v);
488}
489
Guido van Rossum778983b1993-02-19 15:55:02 +0000490/* Methods */
491
492static void
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000493array_dealloc(arrayobject *op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000494{
Raymond Hettingercb87bc82004-05-31 00:35:52 +0000495 if (op->weakreflist != NULL)
496 PyObject_ClearWeakRefs((PyObject *) op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000497 if (op->ob_item != NULL)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000498 PyMem_DEL(op->ob_item);
Martin v. Löwis99866332002-03-01 10:27:01 +0000499 op->ob_type->tp_free((PyObject *)op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000500}
501
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000502static PyObject *
503array_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000504{
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000505 arrayobject *va, *wa;
506 PyObject *vi = NULL;
507 PyObject *wi = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000508 Py_ssize_t i, k;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000509 PyObject *res;
510
Martin v. Löwis99866332002-03-01 10:27:01 +0000511 if (!array_Check(v) || !array_Check(w)) {
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000512 Py_INCREF(Py_NotImplemented);
513 return Py_NotImplemented;
Guido van Rossum778983b1993-02-19 15:55:02 +0000514 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000515
516 va = (arrayobject *)v;
517 wa = (arrayobject *)w;
518
519 if (va->ob_size != wa->ob_size && (op == Py_EQ || op == Py_NE)) {
520 /* Shortcut: if the lengths differ, the arrays differ */
521 if (op == Py_EQ)
522 res = Py_False;
523 else
524 res = Py_True;
525 Py_INCREF(res);
526 return res;
527 }
528
529 /* Search for the first index where items are different */
530 k = 1;
531 for (i = 0; i < va->ob_size && i < wa->ob_size; i++) {
532 vi = getarrayitem(v, i);
533 wi = getarrayitem(w, i);
534 if (vi == NULL || wi == NULL) {
535 Py_XDECREF(vi);
536 Py_XDECREF(wi);
537 return NULL;
538 }
539 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
540 if (k == 0)
541 break; /* Keeping vi and wi alive! */
542 Py_DECREF(vi);
543 Py_DECREF(wi);
544 if (k < 0)
545 return NULL;
546 }
547
548 if (k) {
549 /* No more items to compare -- compare sizes */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000550 Py_ssize_t vs = va->ob_size;
551 Py_ssize_t ws = wa->ob_size;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000552 int cmp;
553 switch (op) {
554 case Py_LT: cmp = vs < ws; break;
Guido van Rossum2b597e42001-01-25 22:12:43 +0000555 case Py_LE: cmp = vs <= ws; break;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000556 case Py_EQ: cmp = vs == ws; break;
557 case Py_NE: cmp = vs != ws; break;
558 case Py_GT: cmp = vs > ws; break;
559 case Py_GE: cmp = vs >= ws; break;
560 default: return NULL; /* cannot happen */
561 }
562 if (cmp)
563 res = Py_True;
564 else
565 res = Py_False;
566 Py_INCREF(res);
567 return res;
568 }
569
570 /* We have an item that differs. First, shortcuts for EQ/NE */
571 if (op == Py_EQ) {
572 Py_INCREF(Py_False);
573 res = Py_False;
574 }
575 else if (op == Py_NE) {
576 Py_INCREF(Py_True);
577 res = Py_True;
578 }
579 else {
580 /* Compare the final item again using the proper operator */
581 res = PyObject_RichCompare(vi, wi, op);
582 }
583 Py_DECREF(vi);
584 Py_DECREF(wi);
585 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +0000586}
587
Martin v. Löwis18e16552006-02-15 17:27:45 +0000588static Py_ssize_t
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000589array_length(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +0000590{
591 return a->ob_size;
592}
593
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000594static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000595array_item(arrayobject *a, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000596{
597 if (i < 0 || i >= a->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000598 PyErr_SetString(PyExc_IndexError, "array index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000599 return NULL;
600 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000601 return getarrayitem((PyObject *)a, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000602}
603
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000604static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000605array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000606{
607 arrayobject *np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000608 if (ilow < 0)
609 ilow = 0;
610 else if (ilow > a->ob_size)
611 ilow = a->ob_size;
612 if (ihigh < 0)
613 ihigh = 0;
614 if (ihigh < ilow)
615 ihigh = ilow;
616 else if (ihigh > a->ob_size)
617 ihigh = a->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000618 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
Guido van Rossum778983b1993-02-19 15:55:02 +0000619 if (np == NULL)
620 return NULL;
621 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
622 (ihigh-ilow) * a->ob_descr->itemsize);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000623 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000624}
625
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000626static PyObject *
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000627array_copy(arrayobject *a, PyObject *unused)
628{
629 return array_slice(a, 0, a->ob_size);
630}
631
632PyDoc_STRVAR(copy_doc,
633"copy(array)\n\
634\n\
635 Return a copy of the array.");
636
637static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000638array_concat(arrayobject *a, PyObject *bb)
Guido van Rossum778983b1993-02-19 15:55:02 +0000639{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000640 Py_ssize_t size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000641 arrayobject *np;
Martin v. Löwis99866332002-03-01 10:27:01 +0000642 if (!array_Check(bb)) {
Fred Drake137507e2000-06-01 02:02:46 +0000643 PyErr_Format(PyExc_TypeError,
644 "can only append array (not \"%.200s\") to array",
645 bb->ob_type->tp_name);
Guido van Rossum778983b1993-02-19 15:55:02 +0000646 return NULL;
647 }
648#define b ((arrayobject *)bb)
649 if (a->ob_descr != b->ob_descr) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000650 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000651 return NULL;
652 }
653 size = a->ob_size + b->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000654 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
Guido van Rossum778983b1993-02-19 15:55:02 +0000655 if (np == NULL) {
656 return NULL;
657 }
658 memcpy(np->ob_item, a->ob_item, a->ob_size*a->ob_descr->itemsize);
659 memcpy(np->ob_item + a->ob_size*a->ob_descr->itemsize,
Guido van Rossum32be3a71993-11-05 10:16:27 +0000660 b->ob_item, b->ob_size*b->ob_descr->itemsize);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000661 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000662#undef b
663}
664
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000665static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000666array_repeat(arrayobject *a, Py_ssize_t n)
Guido van Rossum778983b1993-02-19 15:55:02 +0000667{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000668 Py_ssize_t i;
669 Py_ssize_t size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000670 arrayobject *np;
671 char *p;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000672 Py_ssize_t nbytes;
Guido van Rossum778983b1993-02-19 15:55:02 +0000673 if (n < 0)
674 n = 0;
675 size = a->ob_size * n;
Martin v. Löwis99866332002-03-01 10:27:01 +0000676 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
Guido van Rossum778983b1993-02-19 15:55:02 +0000677 if (np == NULL)
678 return NULL;
679 p = np->ob_item;
680 nbytes = a->ob_size * a->ob_descr->itemsize;
681 for (i = 0; i < n; i++) {
682 memcpy(p, a->ob_item, nbytes);
683 p += nbytes;
684 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000685 return (PyObject *) np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000686}
687
688static int
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000689array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000690{
691 char *item;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000692 Py_ssize_t n; /* Size of replacement array */
693 Py_ssize_t d; /* Change in size */
Guido van Rossum778983b1993-02-19 15:55:02 +0000694#define b ((arrayobject *)v)
695 if (v == NULL)
696 n = 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000697 else if (array_Check(v)) {
Guido van Rossum778983b1993-02-19 15:55:02 +0000698 n = b->ob_size;
699 if (a == b) {
700 /* Special case "a[i:j] = a" -- copy b first */
701 int ret;
702 v = array_slice(b, 0, n);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000703 if (!v)
704 return -1;
Guido van Rossum778983b1993-02-19 15:55:02 +0000705 ret = array_ass_slice(a, ilow, ihigh, v);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000706 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000707 return ret;
708 }
709 if (b->ob_descr != a->ob_descr) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000710 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000711 return -1;
712 }
713 }
714 else {
Fred Drake137507e2000-06-01 02:02:46 +0000715 PyErr_Format(PyExc_TypeError,
716 "can only assign array (not \"%.200s\") to array slice",
717 v->ob_type->tp_name);
Guido van Rossum778983b1993-02-19 15:55:02 +0000718 return -1;
719 }
720 if (ilow < 0)
721 ilow = 0;
722 else if (ilow > a->ob_size)
723 ilow = a->ob_size;
724 if (ihigh < 0)
725 ihigh = 0;
726 if (ihigh < ilow)
727 ihigh = ilow;
728 else if (ihigh > a->ob_size)
729 ihigh = a->ob_size;
730 item = a->ob_item;
731 d = n - (ihigh-ilow);
732 if (d < 0) { /* Delete -d items */
733 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
734 item + ihigh*a->ob_descr->itemsize,
735 (a->ob_size-ihigh)*a->ob_descr->itemsize);
736 a->ob_size += d;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000737 PyMem_RESIZE(item, char, a->ob_size*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000738 /* Can't fail */
739 a->ob_item = item;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000740 a->allocated = a->ob_size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000741 }
742 else if (d > 0) { /* Insert d items */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000743 PyMem_RESIZE(item, char,
744 (a->ob_size + d)*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000745 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000746 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000747 return -1;
748 }
749 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
750 item + ihigh*a->ob_descr->itemsize,
751 (a->ob_size-ihigh)*a->ob_descr->itemsize);
752 a->ob_item = item;
753 a->ob_size += d;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000754 a->allocated = a->ob_size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000755 }
756 if (n > 0)
757 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
758 n*b->ob_descr->itemsize);
759 return 0;
760#undef b
761}
762
763static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000764array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000765{
766 if (i < 0 || i >= a->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000767 PyErr_SetString(PyExc_IndexError,
768 "array assignment index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000769 return -1;
770 }
771 if (v == NULL)
772 return array_ass_slice(a, i, i+1, v);
773 return (*a->ob_descr->setitem)(a, i, v);
774}
775
776static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000777setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000778{
Martin v. Löwis99866332002-03-01 10:27:01 +0000779 assert(array_Check(a));
Guido van Rossum778983b1993-02-19 15:55:02 +0000780 return array_ass_item((arrayobject *)a, i, v);
781}
782
Martin v. Löwis99866332002-03-01 10:27:01 +0000783static int
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000784array_iter_extend(arrayobject *self, PyObject *bb)
785{
786 PyObject *it, *v;
787
788 it = PyObject_GetIter(bb);
789 if (it == NULL)
790 return -1;
791
792 while ((v = PyIter_Next(it)) != NULL) {
793 if (ins1(self, (int) self->ob_size, v) != 0) {
794 Py_DECREF(v);
795 Py_DECREF(it);
796 return -1;
797 }
798 Py_DECREF(v);
799 }
800 Py_DECREF(it);
801 if (PyErr_Occurred())
802 return -1;
803 return 0;
804}
805
806static int
Martin v. Löwis99866332002-03-01 10:27:01 +0000807array_do_extend(arrayobject *self, PyObject *bb)
808{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000809 Py_ssize_t size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000810
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000811 if (!array_Check(bb))
812 return array_iter_extend(self, bb);
Martin v. Löwis99866332002-03-01 10:27:01 +0000813#define b ((arrayobject *)bb)
814 if (self->ob_descr != b->ob_descr) {
815 PyErr_SetString(PyExc_TypeError,
816 "can only extend with array of same kind");
817 return -1;
818 }
819 size = self->ob_size + b->ob_size;
820 PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize);
821 if (self->ob_item == NULL) {
822 PyObject_Del(self);
823 PyErr_NoMemory();
824 return -1;
825 }
826 memcpy(self->ob_item + self->ob_size*self->ob_descr->itemsize,
827 b->ob_item, b->ob_size*b->ob_descr->itemsize);
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000828 self->ob_size = size;
829 self->allocated = size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000830
831 return 0;
832#undef b
833}
834
835static PyObject *
836array_inplace_concat(arrayobject *self, PyObject *bb)
837{
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000838 if (!array_Check(bb)) {
839 PyErr_Format(PyExc_TypeError,
840 "can only extend array with array (not \"%.200s\")",
841 bb->ob_type->tp_name);
842 return NULL;
843 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000844 if (array_do_extend(self, bb) == -1)
845 return NULL;
846 Py_INCREF(self);
847 return (PyObject *)self;
848}
849
850static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000851array_inplace_repeat(arrayobject *self, Py_ssize_t n)
Martin v. Löwis99866332002-03-01 10:27:01 +0000852{
853 char *items, *p;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000854 Py_ssize_t size, i;
Martin v. Löwis99866332002-03-01 10:27:01 +0000855
856 if (self->ob_size > 0) {
857 if (n < 0)
858 n = 0;
859 items = self->ob_item;
860 size = self->ob_size * self->ob_descr->itemsize;
861 if (n == 0) {
862 PyMem_FREE(items);
863 self->ob_item = NULL;
864 self->ob_size = 0;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000865 self->allocated = 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000866 }
867 else {
868 PyMem_Resize(items, char, n * size);
869 if (items == NULL)
870 return PyErr_NoMemory();
871 p = items;
872 for (i = 1; i < n; i++) {
873 p += size;
874 memcpy(p, items, size);
875 }
876 self->ob_item = items;
877 self->ob_size *= n;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000878 self->allocated = self->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000879 }
880 }
881 Py_INCREF(self);
882 return (PyObject *)self;
883}
884
885
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000886static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000887ins(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000888{
889 if (ins1(self, where, v) != 0)
890 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000891 Py_INCREF(Py_None);
892 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000893}
894
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000895static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000896array_count(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000897{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000898 Py_ssize_t count = 0;
899 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000900
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000901 for (i = 0; i < self->ob_size; i++) {
902 PyObject *selfi = getarrayitem((PyObject *)self, i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000903 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000904 Py_DECREF(selfi);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000905 if (cmp > 0)
906 count++;
907 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000908 return NULL;
909 }
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000910 return PyInt_FromSsize_t(count);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000911}
912
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000913PyDoc_STRVAR(count_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000914"count(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000915\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000916Return number of occurences of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000917
918static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000919array_index(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000920{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000921 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000922
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000923 for (i = 0; i < self->ob_size; i++) {
924 PyObject *selfi = getarrayitem((PyObject *)self, i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000925 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
926 Py_DECREF(selfi);
927 if (cmp > 0) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000928 return PyInt_FromLong((long)i);
929 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000930 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000931 return NULL;
932 }
933 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
934 return NULL;
935}
936
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000937PyDoc_STRVAR(index_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000938"index(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000939\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000940Return index of first occurence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000941
Raymond Hettinger625812f2003-01-07 01:58:52 +0000942static int
943array_contains(arrayobject *self, PyObject *v)
944{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000945 Py_ssize_t i;
946 int cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +0000947
948 for (i = 0, cmp = 0 ; cmp == 0 && i < self->ob_size; i++) {
949 PyObject *selfi = getarrayitem((PyObject *)self, i);
950 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
951 Py_DECREF(selfi);
952 }
953 return cmp;
954}
955
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000956static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000957array_remove(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000958{
959 int i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000960
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000961 for (i = 0; i < self->ob_size; i++) {
962 PyObject *selfi = getarrayitem((PyObject *)self,i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000963 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
964 Py_DECREF(selfi);
965 if (cmp > 0) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000966 if (array_ass_slice(self, i, i+1,
967 (PyObject *)NULL) != 0)
968 return NULL;
969 Py_INCREF(Py_None);
970 return Py_None;
971 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000972 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000973 return NULL;
974 }
975 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
976 return NULL;
977}
978
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000979PyDoc_STRVAR(remove_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000980"remove(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000981\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000982Remove the first occurence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000983
984static PyObject *
985array_pop(arrayobject *self, PyObject *args)
986{
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000987 Py_ssize_t i = -1;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000988 PyObject *v;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000989 if (!PyArg_ParseTuple(args, "|n:pop", &i))
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000990 return NULL;
991 if (self->ob_size == 0) {
992 /* Special-case most common failure cause */
993 PyErr_SetString(PyExc_IndexError, "pop from empty array");
994 return NULL;
995 }
996 if (i < 0)
997 i += self->ob_size;
998 if (i < 0 || i >= self->ob_size) {
999 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1000 return NULL;
1001 }
1002 v = getarrayitem((PyObject *)self,i);
1003 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
1004 Py_DECREF(v);
1005 return NULL;
1006 }
1007 return v;
1008}
1009
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001010PyDoc_STRVAR(pop_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001011"pop([i])\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001012\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001013Return the i-th element and delete it from the array. i defaults to -1.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001014
1015static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001016array_extend(arrayobject *self, PyObject *bb)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001017{
Martin v. Löwis99866332002-03-01 10:27:01 +00001018 if (array_do_extend(self, bb) == -1)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001019 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001020 Py_INCREF(Py_None);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001021 return Py_None;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001022}
1023
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001024PyDoc_STRVAR(extend_doc,
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001025"extend(array or iterable)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001026\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001027 Append items to the end of the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001028
1029static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001030array_insert(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001031{
Thomas Woutersb9eb5512006-02-27 19:44:56 +00001032 Py_ssize_t i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001033 PyObject *v;
Thomas Woutersb9eb5512006-02-27 19:44:56 +00001034 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
Guido van Rossum778983b1993-02-19 15:55:02 +00001035 return NULL;
1036 return ins(self, i, v);
1037}
1038
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001039PyDoc_STRVAR(insert_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001040"insert(i,x)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001041\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001042Insert a new item x into the array before position i.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001043
1044
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001045static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001046array_buffer_info(arrayobject *self, PyObject *unused)
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001047{
Tim Peters077a11d2000-09-16 22:31:29 +00001048 PyObject* retval = NULL;
Tim Peters077a11d2000-09-16 22:31:29 +00001049 retval = PyTuple_New(2);
1050 if (!retval)
1051 return NULL;
Fred Drake541dc3b2000-06-28 17:49:30 +00001052
1053 PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item));
1054 PyTuple_SET_ITEM(retval, 1, PyInt_FromLong((long)(self->ob_size)));
1055
1056 return retval;
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001057}
1058
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001059PyDoc_STRVAR(buffer_info_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001060"buffer_info() -> (address, length)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001061\n\
1062Return a tuple (address, length) giving the current memory address and\n\
Guido van Rossum702d08e2001-07-27 16:05:32 +00001063the length in items of the buffer used to hold array's contents\n\
1064The length should be multiplied by the itemsize attribute to calculate\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001065the buffer length in bytes.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001066
1067
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001068static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001069array_append(arrayobject *self, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +00001070{
Guido van Rossum778983b1993-02-19 15:55:02 +00001071 return ins(self, (int) self->ob_size, v);
1072}
1073
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001074PyDoc_STRVAR(append_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001075"append(x)\n\
1076\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001077Append new value x to the end of the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001078
1079
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001080static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001081array_byteswap(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001082{
1083 char *p;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001084 Py_ssize_t i;
Fred Drakebf272981999-12-03 17:15:30 +00001085
Guido van Rossum778983b1993-02-19 15:55:02 +00001086 switch (self->ob_descr->itemsize) {
1087 case 1:
1088 break;
1089 case 2:
1090 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 2) {
1091 char p0 = p[0];
1092 p[0] = p[1];
1093 p[1] = p0;
1094 }
1095 break;
1096 case 4:
1097 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 4) {
1098 char p0 = p[0];
1099 char p1 = p[1];
1100 p[0] = p[3];
1101 p[1] = p[2];
1102 p[2] = p1;
1103 p[3] = p0;
1104 }
1105 break;
Guido van Rossume77a7571993-11-03 15:01:26 +00001106 case 8:
1107 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 8) {
1108 char p0 = p[0];
1109 char p1 = p[1];
1110 char p2 = p[2];
1111 char p3 = p[3];
1112 p[0] = p[7];
1113 p[1] = p[6];
1114 p[2] = p[5];
1115 p[3] = p[4];
1116 p[4] = p3;
1117 p[5] = p2;
1118 p[6] = p1;
1119 p[7] = p0;
1120 }
1121 break;
Guido van Rossum778983b1993-02-19 15:55:02 +00001122 default:
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001123 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossum778983b1993-02-19 15:55:02 +00001124 "don't know how to byteswap this array type");
1125 return NULL;
1126 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001127 Py_INCREF(Py_None);
1128 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001129}
1130
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001131PyDoc_STRVAR(byteswap_doc,
Fred Drakebf272981999-12-03 17:15:30 +00001132"byteswap()\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001133\n\
Fred Drakebf272981999-12-03 17:15:30 +00001134Byteswap 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 +000011354, or 8 bytes in size, RuntimeError is raised.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001136
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001137static PyObject *
Raymond Hettingerb0900e62004-12-16 16:23:40 +00001138array_reduce(arrayobject *array)
1139{
1140 PyObject *dict, *result;
1141
1142 dict = PyObject_GetAttrString((PyObject *)array, "__dict__");
1143 if (dict == NULL) {
1144 PyErr_Clear();
1145 dict = Py_None;
1146 Py_INCREF(dict);
1147 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001148 if (array->ob_size > 0) {
1149 result = Py_BuildValue("O(cs#)O",
1150 array->ob_type,
1151 array->ob_descr->typecode,
1152 array->ob_item,
1153 array->ob_size * array->ob_descr->itemsize,
1154 dict);
1155 } else {
1156 result = Py_BuildValue("O(c)O",
1157 array->ob_type,
1158 array->ob_descr->typecode,
1159 dict);
1160 }
Raymond Hettingerb0900e62004-12-16 16:23:40 +00001161 Py_DECREF(dict);
1162 return result;
1163}
1164
1165PyDoc_STRVAR(array_doc, "Return state information for pickling.");
1166
1167static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001168array_reverse(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001169{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001170 register Py_ssize_t itemsize = self->ob_descr->itemsize;
Guido van Rossume77a7571993-11-03 15:01:26 +00001171 register char *p, *q;
Tim Peters077a11d2000-09-16 22:31:29 +00001172 /* little buffer to hold items while swapping */
1173 char tmp[256]; /* 8 is probably enough -- but why skimp */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001174 assert((size_t)itemsize <= sizeof(tmp));
Guido van Rossume77a7571993-11-03 15:01:26 +00001175
Guido van Rossum778983b1993-02-19 15:55:02 +00001176 if (self->ob_size > 1) {
Guido van Rossume77a7571993-11-03 15:01:26 +00001177 for (p = self->ob_item,
1178 q = self->ob_item + (self->ob_size - 1)*itemsize;
1179 p < q;
1180 p += itemsize, q -= itemsize) {
Tim Peters077a11d2000-09-16 22:31:29 +00001181 /* memory areas guaranteed disjoint, so memcpy
1182 * is safe (& memmove may be slower).
1183 */
1184 memcpy(tmp, p, itemsize);
1185 memcpy(p, q, itemsize);
1186 memcpy(q, tmp, itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001187 }
1188 }
Tim Petersbb307342000-09-10 05:22:54 +00001189
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001190 Py_INCREF(Py_None);
1191 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001192}
Guido van Rossume77a7571993-11-03 15:01:26 +00001193
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001194PyDoc_STRVAR(reverse_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001195"reverse()\n\
1196\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001197Reverse the order of the items in the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001198
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001199static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001200array_fromfile(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001201{
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001202 PyObject *f;
Martin v. Löwisad0a4622006-02-16 14:30:23 +00001203 Py_ssize_t n;
Guido van Rossum778983b1993-02-19 15:55:02 +00001204 FILE *fp;
Thomas Wouters7a2f83b2006-02-16 17:07:41 +00001205 if (!PyArg_ParseTuple(args, "On:fromfile", &f, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +00001206 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001207 fp = PyFile_AsFile(f);
Guido van Rossum778983b1993-02-19 15:55:02 +00001208 if (fp == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001209 PyErr_SetString(PyExc_TypeError, "arg1 must be open file");
Guido van Rossum778983b1993-02-19 15:55:02 +00001210 return NULL;
1211 }
1212 if (n > 0) {
1213 char *item = self->ob_item;
Martin v. Löwisad0a4622006-02-16 14:30:23 +00001214 Py_ssize_t itemsize = self->ob_descr->itemsize;
Guido van Rossum7d0ae5e2000-06-28 21:27:21 +00001215 size_t nread;
Martin v. Löwisad0a4622006-02-16 14:30:23 +00001216 Py_ssize_t newlength;
Guido van Rossum3791b0d1999-02-23 18:05:22 +00001217 size_t newbytes;
1218 /* Be careful here about overflow */
1219 if ((newlength = self->ob_size + n) <= 0 ||
Guido van Rossum481ac881999-03-19 21:50:11 +00001220 (newbytes = newlength * itemsize) / itemsize !=
1221 (size_t)newlength)
Guido van Rossum3791b0d1999-02-23 18:05:22 +00001222 goto nomem;
1223 PyMem_RESIZE(item, char, newbytes);
Guido van Rossum778983b1993-02-19 15:55:02 +00001224 if (item == NULL) {
Guido van Rossum3791b0d1999-02-23 18:05:22 +00001225 nomem:
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001226 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001227 return NULL;
1228 }
1229 self->ob_item = item;
1230 self->ob_size += n;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +00001231 self->allocated = self->ob_size;
Guido van Rossum778983b1993-02-19 15:55:02 +00001232 nread = fread(item + (self->ob_size - n) * itemsize,
1233 itemsize, n, fp);
Guido van Rossum7d0ae5e2000-06-28 21:27:21 +00001234 if (nread < (size_t)n) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001235 self->ob_size -= (n - nread);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001236 PyMem_RESIZE(item, char, self->ob_size*itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001237 self->ob_item = item;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +00001238 self->allocated = self->ob_size;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001239 PyErr_SetString(PyExc_EOFError,
1240 "not enough items in file");
Guido van Rossum778983b1993-02-19 15:55:02 +00001241 return NULL;
1242 }
1243 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001244 Py_INCREF(Py_None);
1245 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001246}
1247
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001248PyDoc_STRVAR(fromfile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001249"fromfile(f, n)\n\
1250\n\
1251Read 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 +00001252array. Also called as read.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001253
1254
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001255static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001256array_tofile(arrayobject *self, PyObject *f)
Guido van Rossum778983b1993-02-19 15:55:02 +00001257{
Guido van Rossum778983b1993-02-19 15:55:02 +00001258 FILE *fp;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001259
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001260 if (self->ob_size == 0)
1261 goto done;
1262
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001263 fp = PyFile_AsFile(f);
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001264 if (fp != NULL) {
Trent Mick6c116dd2000-08-12 20:58:11 +00001265 if (fwrite(self->ob_item, self->ob_descr->itemsize,
1266 self->ob_size, fp) != (size_t)self->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001267 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum778983b1993-02-19 15:55:02 +00001268 clearerr(fp);
1269 return NULL;
1270 }
1271 }
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001272 else {
1273 Py_ssize_t nbytes = self->ob_size * self->ob_descr->itemsize;
1274 /* Write 64K blocks at a time */
1275 /* XXX Make the block size settable */
1276 int BLOCKSIZE = 64*1024;
1277 Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE;
1278 Py_ssize_t i;
1279 for (i = 0; i < nblocks; i++) {
1280 char* ptr = self->ob_item + i*BLOCKSIZE;
1281 Py_ssize_t size = BLOCKSIZE;
1282 PyObject *bytes, *res;
1283 if (i*BLOCKSIZE + size > nbytes)
1284 size = nbytes - i*BLOCKSIZE;
1285 bytes = PyBytes_FromStringAndSize(ptr, size);
1286 if (bytes == NULL)
1287 return NULL;
1288 res = PyObject_CallMethod(f, "write", "O",
1289 bytes);
1290 Py_DECREF(bytes);
1291 if (res == NULL)
1292 return NULL;
1293 }
1294 }
1295
1296 done:
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001297 Py_INCREF(Py_None);
1298 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001299}
1300
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001301PyDoc_STRVAR(tofile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001302"tofile(f)\n\
1303\n\
1304Write all items (as machine values) to the file object f. Also called as\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001305write.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001306
1307
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001308static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001309array_fromlist(arrayobject *self, PyObject *list)
Guido van Rossum778983b1993-02-19 15:55:02 +00001310{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001311 Py_ssize_t n;
1312 Py_ssize_t itemsize = self->ob_descr->itemsize;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001313
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001314 if (!PyList_Check(list)) {
1315 PyErr_SetString(PyExc_TypeError, "arg must be list");
Guido van Rossum778983b1993-02-19 15:55:02 +00001316 return NULL;
1317 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001318 n = PyList_Size(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001319 if (n > 0) {
1320 char *item = self->ob_item;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001321 Py_ssize_t i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001322 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001323 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001324 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001325 return NULL;
1326 }
1327 self->ob_item = item;
1328 self->ob_size += n;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +00001329 self->allocated = self->ob_size;
Guido van Rossum778983b1993-02-19 15:55:02 +00001330 for (i = 0; i < n; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001331 PyObject *v = PyList_GetItem(list, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001332 if ((*self->ob_descr->setitem)(self,
1333 self->ob_size - n + i, v) != 0) {
1334 self->ob_size -= n;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001335 PyMem_RESIZE(item, char,
1336 self->ob_size * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001337 self->ob_item = item;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +00001338 self->allocated = self->ob_size;
Guido van Rossum778983b1993-02-19 15:55:02 +00001339 return NULL;
1340 }
1341 }
1342 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001343 Py_INCREF(Py_None);
1344 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001345}
1346
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001347PyDoc_STRVAR(fromlist_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001348"fromlist(list)\n\
1349\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001350Append items to array from list.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001351
1352
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001353static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001354array_tolist(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001355{
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001356 PyObject *list = PyList_New(self->ob_size);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001357 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001358
Guido van Rossum778983b1993-02-19 15:55:02 +00001359 if (list == NULL)
1360 return NULL;
1361 for (i = 0; i < self->ob_size; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001362 PyObject *v = getarrayitem((PyObject *)self, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001363 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001364 Py_DECREF(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001365 return NULL;
1366 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001367 PyList_SetItem(list, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001368 }
1369 return list;
1370}
1371
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001372PyDoc_STRVAR(tolist_doc,
Guido van Rossumfc6aba51998-10-14 02:52:31 +00001373"tolist() -> list\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001374\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001375Convert array to an ordinary list with the same items.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001376
1377
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001378static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001379array_fromstring(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001380{
1381 char *str;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001382 Py_ssize_t n;
Guido van Rossum778983b1993-02-19 15:55:02 +00001383 int itemsize = self->ob_descr->itemsize;
Peter Schneider-Kampdc71cac2000-07-31 21:57:30 +00001384 if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +00001385 return NULL;
1386 if (n % itemsize != 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001387 PyErr_SetString(PyExc_ValueError,
Guido van Rossum778983b1993-02-19 15:55:02 +00001388 "string length not a multiple of item size");
1389 return NULL;
1390 }
1391 n = n / itemsize;
1392 if (n > 0) {
1393 char *item = self->ob_item;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001394 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001395 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001396 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001397 return NULL;
1398 }
1399 self->ob_item = item;
1400 self->ob_size += n;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +00001401 self->allocated = self->ob_size;
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001402 memcpy(item + (self->ob_size - n) * itemsize,
1403 str, itemsize*n);
Guido van Rossum778983b1993-02-19 15:55:02 +00001404 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001405 Py_INCREF(Py_None);
1406 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001407}
1408
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001409PyDoc_STRVAR(fromstring_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001410"fromstring(string)\n\
1411\n\
1412Appends items from the string, interpreting it as an array of machine\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001413values,as if it had been read from a file using the fromfile() method).");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001414
1415
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001416static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001417array_tostring(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001418{
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001419 return PyString_FromStringAndSize(self->ob_item,
Guido van Rossum778983b1993-02-19 15:55:02 +00001420 self->ob_size * self->ob_descr->itemsize);
1421}
1422
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001423PyDoc_STRVAR(tostring_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001424"tostring() -> string\n\
1425\n\
1426Convert the array to an array of machine values and return the string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001427representation.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001428
Martin v. Löwis99866332002-03-01 10:27:01 +00001429
1430
Martin v. Löwis99866332002-03-01 10:27:01 +00001431static PyObject *
1432array_fromunicode(arrayobject *self, PyObject *args)
1433{
1434 Py_UNICODE *ustr;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001435 Py_ssize_t n;
Martin v. Löwis99866332002-03-01 10:27:01 +00001436
1437 if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
1438 return NULL;
1439 if (self->ob_descr->typecode != 'u') {
1440 PyErr_SetString(PyExc_ValueError,
1441 "fromunicode() may only be called on "
1442 "type 'u' arrays");
1443 return NULL;
1444 }
1445 if (n > 0) {
1446 Py_UNICODE *item = (Py_UNICODE *) self->ob_item;
1447 PyMem_RESIZE(item, Py_UNICODE, self->ob_size + n);
1448 if (item == NULL) {
1449 PyErr_NoMemory();
1450 return NULL;
1451 }
1452 self->ob_item = (char *) item;
1453 self->ob_size += n;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +00001454 self->allocated = self->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +00001455 memcpy(item + self->ob_size - n,
1456 ustr, n * sizeof(Py_UNICODE));
1457 }
1458
1459 Py_INCREF(Py_None);
1460 return Py_None;
1461}
1462
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001463PyDoc_STRVAR(fromunicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001464"fromunicode(ustr)\n\
1465\n\
1466Extends this array with data from the unicode string ustr.\n\
1467The array must be a type 'u' array; otherwise a ValueError\n\
1468is raised. Use array.fromstring(ustr.decode(...)) to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001469append Unicode data to an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001470
1471
1472static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001473array_tounicode(arrayobject *self, PyObject *unused)
Martin v. Löwis99866332002-03-01 10:27:01 +00001474{
Martin v. Löwis99866332002-03-01 10:27:01 +00001475 if (self->ob_descr->typecode != 'u') {
1476 PyErr_SetString(PyExc_ValueError,
1477 "tounicode() may only be called on type 'u' arrays");
1478 return NULL;
1479 }
1480 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, self->ob_size);
1481}
1482
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001483PyDoc_STRVAR(tounicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001484"tounicode() -> unicode\n\
1485\n\
1486Convert the array to a unicode string. The array must be\n\
1487a type 'u' array; otherwise a ValueError is raised. Use\n\
1488array.tostring().decode() to obtain a unicode string from\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001489an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001490
Martin v. Löwis99866332002-03-01 10:27:01 +00001491
1492
1493static PyObject *
1494array_get_typecode(arrayobject *a, void *closure)
1495{
1496 char tc = a->ob_descr->typecode;
1497 return PyString_FromStringAndSize(&tc, 1);
1498}
1499
1500static PyObject *
1501array_get_itemsize(arrayobject *a, void *closure)
1502{
1503 return PyInt_FromLong((long)a->ob_descr->itemsize);
1504}
1505
1506static PyGetSetDef array_getsets [] = {
1507 {"typecode", (getter) array_get_typecode, NULL,
1508 "the typecode character used to create the array"},
1509 {"itemsize", (getter) array_get_itemsize, NULL,
1510 "the size, in bytes, of one array item"},
1511 {NULL}
1512};
1513
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001514PyMethodDef array_methods[] = {
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001515 {"append", (PyCFunction)array_append, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001516 append_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001517 {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001518 buffer_info_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001519 {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001520 byteswap_doc},
Raymond Hettinger3aa82c02004-03-13 18:18:51 +00001521 {"__copy__", (PyCFunction)array_copy, METH_NOARGS,
1522 copy_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001523 {"count", (PyCFunction)array_count, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001524 count_doc},
Thomas Wouters89f507f2006-12-13 04:49:30 +00001525 {"__deepcopy__",(PyCFunction)array_copy, METH_O,
Raymond Hettinger3aa82c02004-03-13 18:18:51 +00001526 copy_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001527 {"extend", (PyCFunction)array_extend, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001528 extend_doc},
1529 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
1530 fromfile_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001531 {"fromlist", (PyCFunction)array_fromlist, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001532 fromlist_doc},
1533 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
1534 fromstring_doc},
Martin v. Löwis99866332002-03-01 10:27:01 +00001535 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
1536 fromunicode_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001537 {"index", (PyCFunction)array_index, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001538 index_doc},
1539 {"insert", (PyCFunction)array_insert, METH_VARARGS,
1540 insert_doc},
1541 {"pop", (PyCFunction)array_pop, METH_VARARGS,
1542 pop_doc},
1543 {"read", (PyCFunction)array_fromfile, METH_VARARGS,
1544 fromfile_doc},
Raymond Hettingerb0900e62004-12-16 16:23:40 +00001545 {"__reduce__", (PyCFunction)array_reduce, METH_NOARGS,
1546 array_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001547 {"remove", (PyCFunction)array_remove, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001548 remove_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001549 {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001550 reverse_doc},
1551/* {"sort", (PyCFunction)array_sort, METH_VARARGS,
1552 sort_doc},*/
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001553 {"tofile", (PyCFunction)array_tofile, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001554 tofile_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001555 {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001556 tolist_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001557 {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001558 tostring_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001559 {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
Martin v. Löwis99866332002-03-01 10:27:01 +00001560 tounicode_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001561 {"write", (PyCFunction)array_tofile, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001562 tofile_doc},
Guido van Rossum778983b1993-02-19 15:55:02 +00001563 {NULL, NULL} /* sentinel */
1564};
1565
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001566static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001567array_repr(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +00001568{
Walter Dörwaldc2f6a582007-05-20 12:49:47 +00001569 char typecode;
Walter Dörwald7569dfe2007-05-19 21:49:49 +00001570 PyObject *s, *v = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001571 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00001572
Guido van Rossum778983b1993-02-19 15:55:02 +00001573 len = a->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +00001574 typecode = a->ob_descr->typecode;
Guido van Rossum778983b1993-02-19 15:55:02 +00001575 if (len == 0) {
Walter Dörwald7569dfe2007-05-19 21:49:49 +00001576 return PyUnicode_FromFormat("array('%c')", typecode);
Guido van Rossum778983b1993-02-19 15:55:02 +00001577 }
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001578 if (typecode == 'c')
1579 v = array_tostring(a, NULL);
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001580 else if (typecode == 'u')
1581 v = array_tounicode(a, NULL);
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001582 else
1583 v = array_tolist(a, NULL);
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001584
Walter Dörwald7569dfe2007-05-19 21:49:49 +00001585 s = PyUnicode_FromFormat("array('%c', %R)", typecode, v);
1586 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001587 return s;
1588}
1589
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001590static PyObject*
1591array_subscr(arrayobject* self, PyObject* item)
1592{
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001593 if (PyIndex_Check(item)) {
1594 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001595 if (i==-1 && PyErr_Occurred()) {
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001596 return NULL;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001597 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001598 if (i < 0)
1599 i += self->ob_size;
1600 return array_item(self, i);
1601 }
1602 else if (PySlice_Check(item)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001603 Py_ssize_t start, stop, step, slicelength, cur, i;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001604 PyObject* result;
1605 arrayobject* ar;
1606 int itemsize = self->ob_descr->itemsize;
1607
1608 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
1609 &start, &stop, &step, &slicelength) < 0) {
1610 return NULL;
1611 }
1612
1613 if (slicelength <= 0) {
1614 return newarrayobject(&Arraytype, 0, self->ob_descr);
1615 }
1616 else {
1617 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
1618 if (!result) return NULL;
1619
1620 ar = (arrayobject*)result;
1621
1622 for (cur = start, i = 0; i < slicelength;
1623 cur += step, i++) {
1624 memcpy(ar->ob_item + i*itemsize,
1625 self->ob_item + cur*itemsize,
1626 itemsize);
1627 }
1628
1629 return result;
1630 }
1631 }
1632 else {
1633 PyErr_SetString(PyExc_TypeError,
1634 "list indices must be integers");
1635 return NULL;
1636 }
1637}
1638
1639static int
1640array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
1641{
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001642 if (PyIndex_Check(item)) {
1643 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001644 if (i==-1 && PyErr_Occurred())
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001645 return -1;
1646 if (i < 0)
1647 i += self->ob_size;
1648 return array_ass_item(self, i, value);
1649 }
1650 else if (PySlice_Check(item)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001651 Py_ssize_t start, stop, step, slicelength;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001652 int itemsize = self->ob_descr->itemsize;
1653
1654 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
1655 &start, &stop, &step, &slicelength) < 0) {
1656 return -1;
1657 }
1658
1659 /* treat A[slice(a,b)] = v _exactly_ like A[a:b] = v */
1660 if (step == 1 && ((PySliceObject*)item)->step == Py_None)
1661 return array_ass_slice(self, start, stop, value);
1662
1663 if (value == NULL) {
1664 /* delete slice */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001665 Py_ssize_t cur, i, extra;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001666
1667 if (slicelength <= 0)
1668 return 0;
1669
1670 if (step < 0) {
1671 stop = start + 1;
1672 start = stop + step*(slicelength - 1) - 1;
1673 step = -step;
1674 }
1675
Michael W. Hudson56796f62002-07-29 14:35:04 +00001676 for (cur = start, i = 0; i < slicelength - 1;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001677 cur += step, i++) {
1678 memmove(self->ob_item + (cur - i)*itemsize,
1679 self->ob_item + (cur + 1)*itemsize,
1680 (step - 1) * itemsize);
1681 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00001682 extra = self->ob_size - (cur + 1);
1683 if (extra > 0) {
1684 memmove(self->ob_item + (cur - i)*itemsize,
1685 self->ob_item + (cur + 1)*itemsize,
1686 extra*itemsize);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001687 }
1688
1689 self->ob_size -= slicelength;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001690 self->ob_item = (char *)PyMem_REALLOC(self->ob_item,
1691 itemsize*self->ob_size);
Raymond Hettinger6e2ee862004-03-14 04:37:50 +00001692 self->allocated = self->ob_size;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001693
1694 return 0;
1695 }
1696 else {
1697 /* assign slice */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001698 Py_ssize_t cur, i;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001699 arrayobject* av;
1700
1701 if (!array_Check(value)) {
1702 PyErr_Format(PyExc_TypeError,
1703 "must assign array (not \"%.200s\") to slice",
1704 value->ob_type->tp_name);
1705 return -1;
1706 }
1707
1708 av = (arrayobject*)value;
1709
1710 if (av->ob_size != slicelength) {
1711 PyErr_Format(PyExc_ValueError,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001712 "attempt to assign array of size %ld to extended slice of size %ld",
1713 /*XXX*/(long)av->ob_size, /*XXX*/(long)slicelength);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001714 return -1;
1715 }
1716
1717 if (!slicelength)
1718 return 0;
1719
1720 /* protect against a[::-1] = a */
1721 if (self == av) {
1722 value = array_slice(av, 0, av->ob_size);
1723 av = (arrayobject*)value;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001724 if (!av)
1725 return -1;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001726 }
1727 else {
1728 Py_INCREF(value);
1729 }
1730
1731 for (cur = start, i = 0; i < slicelength;
1732 cur += step, i++) {
1733 memcpy(self->ob_item + cur*itemsize,
1734 av->ob_item + i*itemsize,
1735 itemsize);
1736 }
1737
1738 Py_DECREF(value);
1739
1740 return 0;
1741 }
1742 }
1743 else {
1744 PyErr_SetString(PyExc_TypeError,
1745 "list indices must be integers");
1746 return -1;
1747 }
1748}
1749
1750static PyMappingMethods array_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001751 (lenfunc)array_length,
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001752 (binaryfunc)array_subscr,
1753 (objobjargproc)array_ass_subscr
1754};
1755
Guido van Rossumd8faa362007-04-27 19:54:29 +00001756static const void *emptybuf = "";
1757
Martin v. Löwis18e16552006-02-15 17:27:45 +00001758static Py_ssize_t
1759array_buffer_getreadbuf(arrayobject *self, Py_ssize_t index, const void **ptr)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001760{
1761 if ( index != 0 ) {
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001762 PyErr_SetString(PyExc_SystemError,
1763 "Accessing non-existent array segment");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001764 return -1;
1765 }
1766 *ptr = (void *)self->ob_item;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001767 if (*ptr == NULL)
1768 *ptr = emptybuf;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001769 return self->ob_size*self->ob_descr->itemsize;
1770}
1771
Martin v. Löwis18e16552006-02-15 17:27:45 +00001772static Py_ssize_t
1773array_buffer_getwritebuf(arrayobject *self, Py_ssize_t index, const void **ptr)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001774{
1775 if ( index != 0 ) {
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001776 PyErr_SetString(PyExc_SystemError,
1777 "Accessing non-existent array segment");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001778 return -1;
1779 }
1780 *ptr = (void *)self->ob_item;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001781 if (*ptr == NULL)
1782 *ptr = emptybuf;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001783 return self->ob_size*self->ob_descr->itemsize;
1784}
1785
Martin v. Löwis18e16552006-02-15 17:27:45 +00001786static Py_ssize_t
1787array_buffer_getsegcount(arrayobject *self, Py_ssize_t *lenp)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001788{
1789 if ( lenp )
1790 *lenp = self->ob_size*self->ob_descr->itemsize;
1791 return 1;
1792}
1793
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001794static PySequenceMethods array_as_sequence = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001795 (lenfunc)array_length, /*sq_length*/
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001796 (binaryfunc)array_concat, /*sq_concat*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001797 (ssizeargfunc)array_repeat, /*sq_repeat*/
1798 (ssizeargfunc)array_item, /*sq_item*/
1799 (ssizessizeargfunc)array_slice, /*sq_slice*/
1800 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
1801 (ssizessizeobjargproc)array_ass_slice, /*sq_ass_slice*/
Raymond Hettinger625812f2003-01-07 01:58:52 +00001802 (objobjproc)array_contains, /*sq_contains*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001803 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
Martin v. Löwis18e16552006-02-15 17:27:45 +00001804 (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001805};
1806
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001807static PyBufferProcs array_as_buffer = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001808 (readbufferproc)array_buffer_getreadbuf,
1809 (writebufferproc)array_buffer_getwritebuf,
1810 (segcountproc)array_buffer_getsegcount,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001811 NULL,
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001812};
1813
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001814static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +00001815array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum778983b1993-02-19 15:55:02 +00001816{
1817 char c;
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001818 PyObject *initial = NULL, *it = NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001819 struct arraydescr *descr;
Georg Brandl02c42872005-08-26 06:42:30 +00001820
Thomas Woutersb2137042007-02-01 18:02:27 +00001821 if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds))
Georg Brandl02c42872005-08-26 06:42:30 +00001822 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001823
1824 if (!PyArg_ParseTuple(args, "c|O:array", &c, &initial))
1825 return NULL;
1826
1827 if (!(initial == NULL || PyList_Check(initial)
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001828 || PyString_Check(initial) || PyTuple_Check(initial)
Martin v. Löwis99866332002-03-01 10:27:01 +00001829 || (c == 'u' && PyUnicode_Check(initial)))) {
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001830 it = PyObject_GetIter(initial);
1831 if (it == NULL)
1832 return NULL;
1833 /* We set initial to NULL so that the subsequent code
1834 will create an empty array of the appropriate type
1835 and afterwards we can use array_iter_extend to populate
1836 the array.
1837 */
1838 initial = NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001839 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001840 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1841 if (descr->typecode == c) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001842 PyObject *a;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001843 Py_ssize_t len;
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001844
1845 if (initial == NULL || !(PyList_Check(initial)
1846 || PyTuple_Check(initial)))
Guido van Rossum778983b1993-02-19 15:55:02 +00001847 len = 0;
1848 else
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001849 len = PySequence_Size(initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001850
1851 a = newarrayobject(type, len, descr);
Guido van Rossum778983b1993-02-19 15:55:02 +00001852 if (a == NULL)
1853 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001854
Guido van Rossum778983b1993-02-19 15:55:02 +00001855 if (len > 0) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001856 Py_ssize_t i;
Guido van Rossum778983b1993-02-19 15:55:02 +00001857 for (i = 0; i < len; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001858 PyObject *v =
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001859 PySequence_GetItem(initial, i);
Raymond Hettinger85004cc2003-08-05 11:23:59 +00001860 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001861 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001862 return NULL;
1863 }
Raymond Hettinger85004cc2003-08-05 11:23:59 +00001864 if (setarrayitem(a, i, v) != 0) {
1865 Py_DECREF(v);
1866 Py_DECREF(a);
1867 return NULL;
1868 }
1869 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001870 }
Raymond Hettinger85004cc2003-08-05 11:23:59 +00001871 } else if (initial != NULL && PyString_Check(initial)) {
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00001872 PyObject *t_initial, *v;
1873 t_initial = PyTuple_Pack(1, initial);
1874 if (t_initial == NULL) {
1875 Py_DECREF(a);
1876 return NULL;
1877 }
1878 v = array_fromstring((arrayobject *)a,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001879 t_initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001880 Py_DECREF(t_initial);
Guido van Rossum778983b1993-02-19 15:55:02 +00001881 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001882 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001883 return NULL;
1884 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001885 Py_DECREF(v);
Martin v. Löwis99866332002-03-01 10:27:01 +00001886 } else if (initial != NULL && PyUnicode_Check(initial)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001887 Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001888 if (n > 0) {
1889 arrayobject *self = (arrayobject *)a;
1890 char *item = self->ob_item;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001891 item = (char *)PyMem_Realloc(item, n);
Martin v. Löwis99866332002-03-01 10:27:01 +00001892 if (item == NULL) {
1893 PyErr_NoMemory();
1894 Py_DECREF(a);
1895 return NULL;
1896 }
1897 self->ob_item = item;
1898 self->ob_size = n / sizeof(Py_UNICODE);
1899 memcpy(item, PyUnicode_AS_DATA(initial), n);
Raymond Hettinger6e2ee862004-03-14 04:37:50 +00001900 self->allocated = self->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +00001901 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001902 }
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001903 if (it != NULL) {
1904 if (array_iter_extend((arrayobject *)a, it) == -1) {
1905 Py_DECREF(it);
1906 Py_DECREF(a);
1907 return NULL;
1908 }
1909 Py_DECREF(it);
1910 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001911 return a;
1912 }
1913 }
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001914 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis99866332002-03-01 10:27:01 +00001915 "bad typecode (must be c, b, B, u, h, H, i, I, l, L, f or d)");
Guido van Rossum778983b1993-02-19 15:55:02 +00001916 return NULL;
1917}
1918
Guido van Rossum778983b1993-02-19 15:55:02 +00001919
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001920PyDoc_STRVAR(module_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001921"This module defines an object type which can efficiently represent\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001922an array of basic values: characters, integers, floating point\n\
1923numbers. Arrays are sequence types and behave very much like lists,\n\
1924except that the type of objects stored in them is constrained. The\n\
1925type is specified at object creation time by using a type code, which\n\
1926is a single character. The following type codes are defined:\n\
1927\n\
1928 Type code C Type Minimum size in bytes \n\
1929 'c' character 1 \n\
1930 'b' signed integer 1 \n\
1931 'B' unsigned integer 1 \n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001932 'u' Unicode character 2 \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001933 'h' signed integer 2 \n\
1934 'H' unsigned integer 2 \n\
1935 'i' signed integer 2 \n\
1936 'I' unsigned integer 2 \n\
1937 'l' signed integer 4 \n\
1938 'L' unsigned integer 4 \n\
1939 'f' floating point 4 \n\
1940 'd' floating point 8 \n\
1941\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001942The constructor is:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001943\n\
1944array(typecode [, initializer]) -- create a new array\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001945");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001946
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001947PyDoc_STRVAR(arraytype_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001948"array(typecode [, initializer]) -> array\n\
1949\n\
1950Return a new array whose items are restricted by typecode, and\n\
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001951initialized from the optional initializer value, which must be a list,\n\
1952string. or iterable over elements of the appropriate type.\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001953\n\
1954Arrays represent basic values and behave very much like lists, except\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001955the type of objects stored in them is constrained.\n\
1956\n\
1957Methods:\n\
1958\n\
1959append() -- append a new item to the end of the array\n\
1960buffer_info() -- return information giving the current memory info\n\
1961byteswap() -- byteswap all the items of the array\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001962count() -- return number of occurences of an object\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001963extend() -- extend array by appending multiple elements from an iterable\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001964fromfile() -- read items from a file object\n\
1965fromlist() -- append items from the list\n\
1966fromstring() -- append items from the string\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001967index() -- return index of first occurence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001968insert() -- insert a new item into the array at a provided position\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001969pop() -- remove and return item (default last)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001970read() -- DEPRECATED, use fromfile()\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001971remove() -- remove first occurence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001972reverse() -- reverse the order of the items in the array\n\
1973tofile() -- write all items to a file object\n\
1974tolist() -- return the array converted to an ordinary list\n\
1975tostring() -- return the array converted to a string\n\
1976write() -- DEPRECATED, use tofile()\n\
1977\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001978Attributes:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001979\n\
1980typecode -- the typecode character used to create the array\n\
1981itemsize -- the length in bytes of one array item\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001982");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001983
Raymond Hettinger625812f2003-01-07 01:58:52 +00001984static PyObject *array_iter(arrayobject *ao);
1985
Tim Peters0c322792002-07-17 16:49:03 +00001986static PyTypeObject Arraytype = {
Fred Drake0d40ba42000-02-04 20:33:49 +00001987 PyObject_HEAD_INIT(NULL)
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001988 0,
Guido van Rossum14648392001-12-08 18:02:58 +00001989 "array.array",
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001990 sizeof(arrayobject),
1991 0,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001992 (destructor)array_dealloc, /* tp_dealloc */
Martin v. Löwisaa158be2002-03-04 09:38:52 +00001993 0, /* tp_print */
Martin v. Löwis99866332002-03-01 10:27:01 +00001994 0, /* tp_getattr */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001995 0, /* tp_setattr */
1996 0, /* tp_compare */
1997 (reprfunc)array_repr, /* tp_repr */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001998 0, /* tp_as_number*/
1999 &array_as_sequence, /* tp_as_sequence*/
2000 &array_as_mapping, /* tp_as_mapping*/
Guido van Rossum9d19cb82001-01-18 01:02:55 +00002001 0, /* tp_hash */
2002 0, /* tp_call */
2003 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002004 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00002005 0, /* tp_setattro */
Tim Petersb870c752001-06-05 04:43:26 +00002006 &array_as_buffer, /* tp_as_buffer*/
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002007 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00002008 arraytype_doc, /* tp_doc */
2009 0, /* tp_traverse */
2010 0, /* tp_clear */
2011 array_richcompare, /* tp_richcompare */
Raymond Hettingercb87bc82004-05-31 00:35:52 +00002012 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
Raymond Hettinger625812f2003-01-07 01:58:52 +00002013 (getiterfunc)array_iter, /* tp_iter */
Martin v. Löwis99866332002-03-01 10:27:01 +00002014 0, /* tp_iternext */
2015 array_methods, /* tp_methods */
2016 0, /* tp_members */
2017 array_getsets, /* tp_getset */
2018 0, /* tp_base */
2019 0, /* tp_dict */
2020 0, /* tp_descr_get */
2021 0, /* tp_descr_set */
2022 0, /* tp_dictoffset */
2023 0, /* tp_init */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002024 PyType_GenericAlloc, /* tp_alloc */
Martin v. Löwis99866332002-03-01 10:27:01 +00002025 array_new, /* tp_new */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002026 PyObject_Del, /* tp_free */
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002027};
2028
Raymond Hettinger625812f2003-01-07 01:58:52 +00002029
2030/*********************** Array Iterator **************************/
2031
2032typedef struct {
2033 PyObject_HEAD
Martin v. Löwis18e16552006-02-15 17:27:45 +00002034 Py_ssize_t index;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002035 arrayobject *ao;
Martin v. Löwis18e16552006-02-15 17:27:45 +00002036 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002037} arrayiterobject;
2038
2039static PyTypeObject PyArrayIter_Type;
2040
2041#define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
2042
2043static PyObject *
2044array_iter(arrayobject *ao)
2045{
2046 arrayiterobject *it;
2047
2048 if (!array_Check(ao)) {
2049 PyErr_BadInternalCall();
2050 return NULL;
2051 }
2052
2053 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
2054 if (it == NULL)
2055 return NULL;
2056
2057 Py_INCREF(ao);
2058 it->ao = ao;
2059 it->index = 0;
2060 it->getitem = ao->ob_descr->getitem;
2061 PyObject_GC_Track(it);
2062 return (PyObject *)it;
2063}
2064
2065static PyObject *
Raymond Hettinger625812f2003-01-07 01:58:52 +00002066arrayiter_next(arrayiterobject *it)
2067{
2068 assert(PyArrayIter_Check(it));
2069 if (it->index < it->ao->ob_size)
2070 return (*it->getitem)(it->ao, it->index++);
2071 return NULL;
2072}
2073
2074static void
2075arrayiter_dealloc(arrayiterobject *it)
2076{
2077 PyObject_GC_UnTrack(it);
2078 Py_XDECREF(it->ao);
2079 PyObject_GC_Del(it);
2080}
2081
2082static int
2083arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2084{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002085 Py_VISIT(it->ao);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002086 return 0;
2087}
2088
2089static PyTypeObject PyArrayIter_Type = {
Jason Tishler84375702003-02-10 20:45:47 +00002090 PyObject_HEAD_INIT(NULL)
Raymond Hettinger625812f2003-01-07 01:58:52 +00002091 0, /* ob_size */
2092 "arrayiterator", /* tp_name */
2093 sizeof(arrayiterobject), /* tp_basicsize */
2094 0, /* tp_itemsize */
2095 /* methods */
2096 (destructor)arrayiter_dealloc, /* tp_dealloc */
2097 0, /* tp_print */
2098 0, /* tp_getattr */
2099 0, /* tp_setattr */
2100 0, /* tp_compare */
2101 0, /* tp_repr */
2102 0, /* tp_as_number */
2103 0, /* tp_as_sequence */
2104 0, /* tp_as_mapping */
2105 0, /* tp_hash */
2106 0, /* tp_call */
2107 0, /* tp_str */
2108 PyObject_GenericGetAttr, /* tp_getattro */
2109 0, /* tp_setattro */
2110 0, /* tp_as_buffer */
2111 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2112 0, /* tp_doc */
2113 (traverseproc)arrayiter_traverse, /* tp_traverse */
2114 0, /* tp_clear */
2115 0, /* tp_richcompare */
2116 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002117 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger625812f2003-01-07 01:58:52 +00002118 (iternextfunc)arrayiter_next, /* tp_iternext */
2119 0, /* tp_methods */
2120};
2121
2122
2123/*********************** Install Module **************************/
2124
Martin v. Löwis99866332002-03-01 10:27:01 +00002125/* No functions in array module. */
2126static PyMethodDef a_methods[] = {
2127 {NULL, NULL, 0, NULL} /* Sentinel */
2128};
2129
2130
Mark Hammondfe51c6d2002-08-02 02:27:13 +00002131PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00002132initarray(void)
Guido van Rossum778983b1993-02-19 15:55:02 +00002133{
Fred Drakef4e34842002-04-01 03:45:06 +00002134 PyObject *m;
Fred Drake0d40ba42000-02-04 20:33:49 +00002135
Guido van Rossum50e9fb92006-08-17 05:42:55 +00002136 if (PyType_Ready(&Arraytype) < 0)
2137 return;
Jason Tishler84375702003-02-10 20:45:47 +00002138 PyArrayIter_Type.ob_type = &PyType_Type;
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002139 m = Py_InitModule3("array", a_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00002140 if (m == NULL)
2141 return;
Fred Drakef4e34842002-04-01 03:45:06 +00002142
2143 Py_INCREF((PyObject *)&Arraytype);
2144 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
2145 Py_INCREF((PyObject *)&Arraytype);
2146 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
Guido van Rossuma0deb641998-10-14 13:45:06 +00002147 /* No need to check the error here, the caller will do that */
Guido van Rossum778983b1993-02-19 15:55:02 +00002148}