blob: f14711563c5304d37b0285b9f71bcc125d9ad86d [file] [log] [blame]
Guido van Rossum63d101e1993-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öwis26a63482006-02-15 17:27:45 +00006#define PY_SSIZE_T_CLEAN
Roger E. Masse395316d1996-12-09 20:10:36 +00007#include "Python.h"
Raymond Hettingerd7640d52004-05-31 00:35:52 +00008#include "structmember.h"
Roger E. Masse5bb93e91996-12-09 22:24:19 +00009
Guido van Rossumfbf65551994-08-19 12:01:32 +000010#ifdef STDC_HEADERS
11#include <stddef.h>
Guido van Rossumf30f7f11999-08-27 20:33:52 +000012#else /* !STDC_HEADERS */
Martin v. Löwis1ad9a132006-06-10 12:23:46 +000013#ifdef HAVE_SYS_TYPES_H
Antoine Pitrou76748412010-05-09 14:46:46 +000014#include <sys/types.h> /* For size_t */
Martin v. Löwis1ad9a132006-06-10 12:23:46 +000015#endif /* HAVE_SYS_TYPES_H */
Guido van Rossumf30f7f11999-08-27 20:33:52 +000016#endif /* !STDC_HEADERS */
Guido van Rossum63d101e1993-02-19 15:55:02 +000017
18struct arrayobject; /* Forward */
19
Tim Peters88d20252000-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 Rossum63d101e1993-02-19 15:55:02 +000024struct arraydescr {
Antoine Pitrou76748412010-05-09 14:46:46 +000025 int typecode;
26 int itemsize;
27 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
28 int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *);
Guido van Rossum63d101e1993-02-19 15:55:02 +000029};
30
31typedef struct arrayobject {
Antoine Pitrou76748412010-05-09 14:46:46 +000032 PyObject_VAR_HEAD
33 char *ob_item;
34 Py_ssize_t allocated;
35 struct arraydescr *ob_descr;
36 PyObject *weakreflist; /* List of weak references */
Guido van Rossum63d101e1993-02-19 15:55:02 +000037} arrayobject;
38
Jeremy Hyltonf6c9c302002-07-17 16:30:39 +000039static PyTypeObject Arraytype;
Guido van Rossum63d101e1993-02-19 15:55:02 +000040
Martin v. Löwis669620f2002-03-01 10:27:01 +000041#define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
Christian Heimes313c5222007-12-19 02:37:44 +000042#define array_CheckExact(op) (Py_TYPE(op) == &Arraytype)
Guido van Rossum63d101e1993-02-19 15:55:02 +000043
Raymond Hettingerda1da2f2004-03-14 04:37:50 +000044static int
Martin v. Löwis26a63482006-02-15 17:27:45 +000045array_resize(arrayobject *self, Py_ssize_t newsize)
Raymond Hettingerda1da2f2004-03-14 04:37:50 +000046{
Antoine Pitrou76748412010-05-09 14:46:46 +000047 char *items;
48 size_t _new_size;
Raymond Hettingerda1da2f2004-03-14 04:37:50 +000049
Antoine Pitrou76748412010-05-09 14:46:46 +000050 /* 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 */
Raymond Hettingerda1da2f2004-03-14 04:37:50 +000054
Antoine Pitrou76748412010-05-09 14:46:46 +000055 if (self->allocated >= newsize &&
56 Py_SIZE(self) < newsize + 16 &&
57 self->ob_item != NULL) {
58 Py_SIZE(self) = newsize;
59 return 0;
60 }
Raymond Hettingerda1da2f2004-03-14 04:37:50 +000061
Antoine Pitrou76748412010-05-09 14:46:46 +000062 /* 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 */
Raymond Hettingerda1da2f2004-03-14 04:37:50 +000073
Antoine Pitrou76748412010-05-09 14:46:46 +000074 _new_size = (newsize >> 4) + (Py_SIZE(self) < 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 Py_SIZE(self) = newsize;
88 self->allocated = _new_size;
89 return 0;
Raymond Hettingerda1da2f2004-03-14 04:37:50 +000090}
91
Tim Peters88d20252000-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 Rossum63d101e1993-02-19 15:55:02 +0000103
Roger E. Masse395316d1996-12-09 20:10:36 +0000104static PyObject *
Martin v. Löwis26a63482006-02-15 17:27:45 +0000105c_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000106{
Antoine Pitrou76748412010-05-09 14:46:46 +0000107 return PyString_FromStringAndSize(&((char *)ap->ob_item)[i], 1);
Guido van Rossum63d101e1993-02-19 15:55:02 +0000108}
109
110static int
Martin v. Löwis26a63482006-02-15 17:27:45 +0000111c_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000112{
Antoine Pitrou76748412010-05-09 14:46:46 +0000113 char x;
114 if (!PyArg_Parse(v, "c;array item must be char", &x))
115 return -1;
116 if (i >= 0)
117 ((char *)ap->ob_item)[i] = x;
118 return 0;
Guido van Rossum63d101e1993-02-19 15:55:02 +0000119}
120
Roger E. Masse395316d1996-12-09 20:10:36 +0000121static PyObject *
Martin v. Löwis26a63482006-02-15 17:27:45 +0000122b_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000123{
Antoine Pitrou76748412010-05-09 14:46:46 +0000124 long x = ((char *)ap->ob_item)[i];
125 if (x >= 128)
126 x -= 256;
127 return PyInt_FromLong(x);
Guido van Rossum63d101e1993-02-19 15:55:02 +0000128}
129
130static int
Martin v. Löwis26a63482006-02-15 17:27:45 +0000131b_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000132{
Antoine Pitrou76748412010-05-09 14:46:46 +0000133 short x;
134 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
135 must use the next size up that is signed ('h') and manually do
136 the overflow checking */
137 if (!PyArg_Parse(v, "h;array item must be integer", &x))
138 return -1;
139 else if (x < -128) {
140 PyErr_SetString(PyExc_OverflowError,
141 "signed char is less than minimum");
142 return -1;
143 }
144 else if (x > 127) {
145 PyErr_SetString(PyExc_OverflowError,
146 "signed char is greater than maximum");
147 return -1;
148 }
149 if (i >= 0)
150 ((char *)ap->ob_item)[i] = (char)x;
151 return 0;
Guido van Rossum63d101e1993-02-19 15:55:02 +0000152}
153
Roger E. Masse395316d1996-12-09 20:10:36 +0000154static PyObject *
Martin v. Löwis26a63482006-02-15 17:27:45 +0000155BB_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum6f270b41997-01-03 19:09:47 +0000156{
Antoine Pitrou76748412010-05-09 14:46:46 +0000157 long x = ((unsigned char *)ap->ob_item)[i];
158 return PyInt_FromLong(x);
Guido van Rossum6f270b41997-01-03 19:09:47 +0000159}
160
Fred Draked2cf6ca2000-06-28 17:49:30 +0000161static int
Martin v. Löwis26a63482006-02-15 17:27:45 +0000162BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Draked2cf6ca2000-06-28 17:49:30 +0000163{
Antoine Pitrou76748412010-05-09 14:46:46 +0000164 unsigned char x;
165 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
166 if (!PyArg_Parse(v, "b;array item must be integer", &x))
167 return -1;
168 if (i >= 0)
169 ((char *)ap->ob_item)[i] = x;
170 return 0;
Fred Draked2cf6ca2000-06-28 17:49:30 +0000171}
Guido van Rossum6f270b41997-01-03 19:09:47 +0000172
Martin v. Löwis669620f2002-03-01 10:27:01 +0000173#ifdef Py_USING_UNICODE
174static PyObject *
Martin v. Löwis26a63482006-02-15 17:27:45 +0000175u_getitem(arrayobject *ap, Py_ssize_t i)
Martin v. Löwis669620f2002-03-01 10:27:01 +0000176{
Antoine Pitrou76748412010-05-09 14:46:46 +0000177 return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1);
Martin v. Löwis669620f2002-03-01 10:27:01 +0000178}
179
180static int
Martin v. Löwis26a63482006-02-15 17:27:45 +0000181u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Martin v. Löwis669620f2002-03-01 10:27:01 +0000182{
Antoine Pitrou76748412010-05-09 14:46:46 +0000183 Py_UNICODE *p;
184 Py_ssize_t len;
Martin v. Löwis669620f2002-03-01 10:27:01 +0000185
Antoine Pitrou76748412010-05-09 14:46:46 +0000186 if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
187 return -1;
188 if (len != 1) {
189 PyErr_SetString(PyExc_TypeError,
190 "array item must be unicode character");
191 return -1;
192 }
193 if (i >= 0)
194 ((Py_UNICODE *)ap->ob_item)[i] = p[0];
195 return 0;
Martin v. Löwis669620f2002-03-01 10:27:01 +0000196}
197#endif
198
Guido van Rossum6f270b41997-01-03 19:09:47 +0000199static PyObject *
Martin v. Löwis26a63482006-02-15 17:27:45 +0000200h_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000201{
Antoine Pitrou76748412010-05-09 14:46:46 +0000202 return PyInt_FromLong((long) ((short *)ap->ob_item)[i]);
Guido van Rossum63d101e1993-02-19 15:55:02 +0000203}
204
205static int
Martin v. Löwis26a63482006-02-15 17:27:45 +0000206h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000207{
Antoine Pitrou76748412010-05-09 14:46:46 +0000208 short x;
209 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
210 if (!PyArg_Parse(v, "h;array item must be integer", &x))
211 return -1;
212 if (i >= 0)
213 ((short *)ap->ob_item)[i] = x;
214 return 0;
Guido van Rossum63d101e1993-02-19 15:55:02 +0000215}
216
Roger E. Masse395316d1996-12-09 20:10:36 +0000217static PyObject *
Martin v. Löwis26a63482006-02-15 17:27:45 +0000218HH_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum6f270b41997-01-03 19:09:47 +0000219{
Antoine Pitrou76748412010-05-09 14:46:46 +0000220 return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
Guido van Rossum6f270b41997-01-03 19:09:47 +0000221}
222
Fred Draked2cf6ca2000-06-28 17:49:30 +0000223static int
Martin v. Löwis26a63482006-02-15 17:27:45 +0000224HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Draked2cf6ca2000-06-28 17:49:30 +0000225{
Antoine Pitrou76748412010-05-09 14:46:46 +0000226 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;
Fred Draked2cf6ca2000-06-28 17:49:30 +0000244}
Guido van Rossum6f270b41997-01-03 19:09:47 +0000245
246static PyObject *
Martin v. Löwis26a63482006-02-15 17:27:45 +0000247i_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum76774371993-11-03 15:01:26 +0000248{
Antoine Pitrou76748412010-05-09 14:46:46 +0000249 return PyInt_FromLong((long) ((int *)ap->ob_item)[i]);
Guido van Rossum76774371993-11-03 15:01:26 +0000250}
251
252static int
Martin v. Löwis26a63482006-02-15 17:27:45 +0000253i_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum76774371993-11-03 15:01:26 +0000254{
Antoine Pitrou76748412010-05-09 14:46:46 +0000255 int x;
256 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
257 if (!PyArg_Parse(v, "i;array item must be integer", &x))
258 return -1;
259 if (i >= 0)
260 ((int *)ap->ob_item)[i] = x;
261 return 0;
Guido van Rossum76774371993-11-03 15:01:26 +0000262}
263
Roger E. Masse395316d1996-12-09 20:10:36 +0000264static PyObject *
Martin v. Löwis26a63482006-02-15 17:27:45 +0000265II_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum6f270b41997-01-03 19:09:47 +0000266{
Antoine Pitrou76748412010-05-09 14:46:46 +0000267 return PyLong_FromUnsignedLong(
268 (unsigned long) ((unsigned int *)ap->ob_item)[i]);
Guido van Rossum6f270b41997-01-03 19:09:47 +0000269}
270
271static int
Martin v. Löwis26a63482006-02-15 17:27:45 +0000272II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum6f270b41997-01-03 19:09:47 +0000273{
Antoine Pitrou76748412010-05-09 14:46:46 +0000274 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 {
281 long y;
282 if (!PyArg_Parse(v, "l;array item must be integer", &y))
283 return -1;
284 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 Peters88d20252000-09-10 05:22:54 +0000290
Antoine Pitrou76748412010-05-09 14:46:46 +0000291 }
292 if (x > UINT_MAX) {
293 PyErr_SetString(PyExc_OverflowError,
294 "unsigned int is greater than maximum");
295 return -1;
296 }
Fred Draked2cf6ca2000-06-28 17:49:30 +0000297
Antoine Pitrou76748412010-05-09 14:46:46 +0000298 if (i >= 0)
299 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
300 return 0;
Guido van Rossum6f270b41997-01-03 19:09:47 +0000301}
302
303static PyObject *
Martin v. Löwis26a63482006-02-15 17:27:45 +0000304l_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000305{
Antoine Pitrou76748412010-05-09 14:46:46 +0000306 return PyInt_FromLong(((long *)ap->ob_item)[i]);
Guido van Rossum63d101e1993-02-19 15:55:02 +0000307}
308
309static int
Martin v. Löwis26a63482006-02-15 17:27:45 +0000310l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000311{
Antoine Pitrou76748412010-05-09 14:46:46 +0000312 long x;
313 if (!PyArg_Parse(v, "l;array item must be integer", &x))
314 return -1;
315 if (i >= 0)
316 ((long *)ap->ob_item)[i] = x;
317 return 0;
Guido van Rossum63d101e1993-02-19 15:55:02 +0000318}
319
Roger E. Masse395316d1996-12-09 20:10:36 +0000320static PyObject *
Martin v. Löwis26a63482006-02-15 17:27:45 +0000321LL_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum6f270b41997-01-03 19:09:47 +0000322{
Antoine Pitrou76748412010-05-09 14:46:46 +0000323 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
Guido van Rossum6f270b41997-01-03 19:09:47 +0000324}
325
326static int
Martin v. Löwis26a63482006-02-15 17:27:45 +0000327LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum6f270b41997-01-03 19:09:47 +0000328{
Antoine Pitrou76748412010-05-09 14:46:46 +0000329 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 {
336 long y;
337 if (!PyArg_Parse(v, "l;array item must be integer", &y))
338 return -1;
339 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 Peters88d20252000-09-10 05:22:54 +0000345
Antoine Pitrou76748412010-05-09 14:46:46 +0000346 }
347 if (x > ULONG_MAX) {
348 PyErr_SetString(PyExc_OverflowError,
349 "unsigned long is greater than maximum");
350 return -1;
351 }
Tim Peters88d20252000-09-10 05:22:54 +0000352
Antoine Pitrou76748412010-05-09 14:46:46 +0000353 if (i >= 0)
354 ((unsigned long *)ap->ob_item)[i] = x;
355 return 0;
Guido van Rossum6f270b41997-01-03 19:09:47 +0000356}
357
358static PyObject *
Martin v. Löwis26a63482006-02-15 17:27:45 +0000359f_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000360{
Antoine Pitrou76748412010-05-09 14:46:46 +0000361 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
Guido van Rossum63d101e1993-02-19 15:55:02 +0000362}
363
364static int
Martin v. Löwis26a63482006-02-15 17:27:45 +0000365f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000366{
Antoine Pitrou76748412010-05-09 14:46:46 +0000367 float x;
368 if (!PyArg_Parse(v, "f;array item must be float", &x))
369 return -1;
370 if (i >= 0)
371 ((float *)ap->ob_item)[i] = x;
372 return 0;
Guido van Rossum63d101e1993-02-19 15:55:02 +0000373}
374
Roger E. Masse395316d1996-12-09 20:10:36 +0000375static PyObject *
Martin v. Löwis26a63482006-02-15 17:27:45 +0000376d_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000377{
Antoine Pitrou76748412010-05-09 14:46:46 +0000378 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
Guido van Rossum63d101e1993-02-19 15:55:02 +0000379}
380
381static int
Martin v. Löwis26a63482006-02-15 17:27:45 +0000382d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000383{
Antoine Pitrou76748412010-05-09 14:46:46 +0000384 double x;
385 if (!PyArg_Parse(v, "d;array item must be float", &x))
386 return -1;
387 if (i >= 0)
388 ((double *)ap->ob_item)[i] = x;
389 return 0;
Guido van Rossum63d101e1993-02-19 15:55:02 +0000390}
391
392/* Description of types */
Guido van Rossumf94d2631993-06-17 12:35:49 +0000393static struct arraydescr descriptors[] = {
Antoine Pitrou76748412010-05-09 14:46:46 +0000394 {'c', sizeof(char), c_getitem, c_setitem},
395 {'b', sizeof(char), b_getitem, b_setitem},
396 {'B', sizeof(char), BB_getitem, BB_setitem},
Martin v. Löwis669620f2002-03-01 10:27:01 +0000397#ifdef Py_USING_UNICODE
Antoine Pitrou76748412010-05-09 14:46:46 +0000398 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem},
Martin v. Löwis669620f2002-03-01 10:27:01 +0000399#endif
Antoine Pitrou76748412010-05-09 14:46:46 +0000400 {'h', sizeof(short), h_getitem, h_setitem},
401 {'H', sizeof(short), HH_getitem, HH_setitem},
402 {'i', sizeof(int), i_getitem, i_setitem},
403 {'I', sizeof(int), II_getitem, II_setitem},
404 {'l', sizeof(long), l_getitem, l_setitem},
405 {'L', sizeof(long), LL_getitem, LL_setitem},
406 {'f', sizeof(float), f_getitem, f_setitem},
407 {'d', sizeof(double), d_getitem, d_setitem},
408 {'\0', 0, 0, 0} /* Sentinel */
Guido van Rossum63d101e1993-02-19 15:55:02 +0000409};
Tim Peters88d20252000-09-10 05:22:54 +0000410
411/****************************************************************************
412Implementations of array object methods.
413****************************************************************************/
Guido van Rossum63d101e1993-02-19 15:55:02 +0000414
Roger E. Masse395316d1996-12-09 20:10:36 +0000415static PyObject *
Martin v. Löwis26a63482006-02-15 17:27:45 +0000416newarrayobject(PyTypeObject *type, Py_ssize_t size, struct arraydescr *descr)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000417{
Antoine Pitrou76748412010-05-09 14:46:46 +0000418 arrayobject *op;
419 size_t nbytes;
Martin v. Löwis669620f2002-03-01 10:27:01 +0000420
Antoine Pitrou76748412010-05-09 14:46:46 +0000421 if (size < 0) {
422 PyErr_BadInternalCall();
423 return NULL;
424 }
Martin v. Löwis669620f2002-03-01 10:27:01 +0000425
Antoine Pitrou76748412010-05-09 14:46:46 +0000426 nbytes = size * descr->itemsize;
427 /* Check for overflow */
428 if (nbytes / descr->itemsize != (size_t)size) {
429 return PyErr_NoMemory();
430 }
431 op = (arrayobject *) type->tp_alloc(type, 0);
432 if (op == NULL) {
433 return NULL;
434 }
435 op->ob_descr = descr;
436 op->allocated = size;
437 op->weakreflist = NULL;
438 Py_SIZE(op) = size;
439 if (size <= 0) {
440 op->ob_item = NULL;
441 }
442 else {
443 op->ob_item = PyMem_NEW(char, nbytes);
444 if (op->ob_item == NULL) {
445 Py_DECREF(op);
446 return PyErr_NoMemory();
447 }
448 }
449 return (PyObject *) op;
Guido van Rossum63d101e1993-02-19 15:55:02 +0000450}
451
Roger E. Masse395316d1996-12-09 20:10:36 +0000452static PyObject *
Martin v. Löwis26a63482006-02-15 17:27:45 +0000453getarrayitem(PyObject *op, Py_ssize_t i)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000454{
Antoine Pitrou76748412010-05-09 14:46:46 +0000455 register arrayobject *ap;
456 assert(array_Check(op));
457 ap = (arrayobject *)op;
458 assert(i>=0 && i<Py_SIZE(ap));
459 return (*ap->ob_descr->getitem)(ap, i);
Guido van Rossum63d101e1993-02-19 15:55:02 +0000460}
461
462static int
Martin v. Löwis26a63482006-02-15 17:27:45 +0000463ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000464{
Antoine Pitrou76748412010-05-09 14:46:46 +0000465 char *items;
466 Py_ssize_t n = Py_SIZE(self);
467 if (v == NULL) {
468 PyErr_BadInternalCall();
469 return -1;
470 }
471 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
472 return -1;
Raymond Hettingerda1da2f2004-03-14 04:37:50 +0000473
Antoine Pitrou76748412010-05-09 14:46:46 +0000474 if (array_resize(self, n+1) == -1)
475 return -1;
476 items = self->ob_item;
477 if (where < 0) {
478 where += n;
479 if (where < 0)
480 where = 0;
481 }
482 if (where > n)
483 where = n;
484 /* appends don't need to call memmove() */
485 if (where != n)
486 memmove(items + (where+1)*self->ob_descr->itemsize,
487 items + where*self->ob_descr->itemsize,
488 (n-where)*self->ob_descr->itemsize);
489 return (*self->ob_descr->setitem)(self, where, v);
Guido van Rossum63d101e1993-02-19 15:55:02 +0000490}
491
Guido van Rossum63d101e1993-02-19 15:55:02 +0000492/* Methods */
493
494static void
Peter Schneider-Kamp846f68d2000-07-13 21:10:57 +0000495array_dealloc(arrayobject *op)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000496{
Antoine Pitrou76748412010-05-09 14:46:46 +0000497 if (op->weakreflist != NULL)
498 PyObject_ClearWeakRefs((PyObject *) op);
499 if (op->ob_item != NULL)
500 PyMem_DEL(op->ob_item);
501 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossum63d101e1993-02-19 15:55:02 +0000502}
503
Guido van Rossumce664542001-01-18 01:02:55 +0000504static PyObject *
505array_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000506{
Antoine Pitrou76748412010-05-09 14:46:46 +0000507 arrayobject *va, *wa;
508 PyObject *vi = NULL;
509 PyObject *wi = NULL;
510 Py_ssize_t i, k;
511 PyObject *res;
Guido van Rossumce664542001-01-18 01:02:55 +0000512
Antoine Pitrou76748412010-05-09 14:46:46 +0000513 if (!array_Check(v) || !array_Check(w)) {
514 Py_INCREF(Py_NotImplemented);
515 return Py_NotImplemented;
516 }
Guido van Rossumce664542001-01-18 01:02:55 +0000517
Antoine Pitrou76748412010-05-09 14:46:46 +0000518 va = (arrayobject *)v;
519 wa = (arrayobject *)w;
Guido van Rossumce664542001-01-18 01:02:55 +0000520
Antoine Pitrou76748412010-05-09 14:46:46 +0000521 if (Py_SIZE(va) != Py_SIZE(wa) && (op == Py_EQ || op == Py_NE)) {
522 /* Shortcut: if the lengths differ, the arrays differ */
523 if (op == Py_EQ)
524 res = Py_False;
525 else
526 res = Py_True;
527 Py_INCREF(res);
528 return res;
529 }
Guido van Rossumce664542001-01-18 01:02:55 +0000530
Antoine Pitrou76748412010-05-09 14:46:46 +0000531 /* Search for the first index where items are different */
532 k = 1;
533 for (i = 0; i < Py_SIZE(va) && i < Py_SIZE(wa); i++) {
534 vi = getarrayitem(v, i);
535 wi = getarrayitem(w, i);
536 if (vi == NULL || wi == NULL) {
537 Py_XDECREF(vi);
538 Py_XDECREF(wi);
539 return NULL;
540 }
541 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
542 if (k == 0)
543 break; /* Keeping vi and wi alive! */
544 Py_DECREF(vi);
545 Py_DECREF(wi);
546 if (k < 0)
547 return NULL;
548 }
Guido van Rossumce664542001-01-18 01:02:55 +0000549
Antoine Pitrou76748412010-05-09 14:46:46 +0000550 if (k) {
551 /* No more items to compare -- compare sizes */
552 Py_ssize_t vs = Py_SIZE(va);
553 Py_ssize_t ws = Py_SIZE(wa);
554 int cmp;
555 switch (op) {
556 case Py_LT: cmp = vs < ws; break;
557 case Py_LE: cmp = vs <= ws; break;
558 case Py_EQ: cmp = vs == ws; break;
559 case Py_NE: cmp = vs != ws; break;
560 case Py_GT: cmp = vs > ws; break;
561 case Py_GE: cmp = vs >= ws; break;
562 default: return NULL; /* cannot happen */
563 }
564 if (cmp)
565 res = Py_True;
566 else
567 res = Py_False;
568 Py_INCREF(res);
569 return res;
570 }
Guido van Rossumce664542001-01-18 01:02:55 +0000571
Antoine Pitrou76748412010-05-09 14:46:46 +0000572 /* We have an item that differs. First, shortcuts for EQ/NE */
573 if (op == Py_EQ) {
574 Py_INCREF(Py_False);
575 res = Py_False;
576 }
577 else if (op == Py_NE) {
578 Py_INCREF(Py_True);
579 res = Py_True;
580 }
581 else {
582 /* Compare the final item again using the proper operator */
583 res = PyObject_RichCompare(vi, wi, op);
584 }
585 Py_DECREF(vi);
586 Py_DECREF(wi);
587 return res;
Guido van Rossum63d101e1993-02-19 15:55:02 +0000588}
589
Martin v. Löwis26a63482006-02-15 17:27:45 +0000590static Py_ssize_t
Peter Schneider-Kamp846f68d2000-07-13 21:10:57 +0000591array_length(arrayobject *a)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000592{
Antoine Pitrou76748412010-05-09 14:46:46 +0000593 return Py_SIZE(a);
Guido van Rossum63d101e1993-02-19 15:55:02 +0000594}
595
Roger E. Masse395316d1996-12-09 20:10:36 +0000596static PyObject *
Martin v. Löwis26a63482006-02-15 17:27:45 +0000597array_item(arrayobject *a, Py_ssize_t i)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000598{
Antoine Pitrou76748412010-05-09 14:46:46 +0000599 if (i < 0 || i >= Py_SIZE(a)) {
600 PyErr_SetString(PyExc_IndexError, "array index out of range");
601 return NULL;
602 }
603 return getarrayitem((PyObject *)a, i);
Guido van Rossum63d101e1993-02-19 15:55:02 +0000604}
605
Roger E. Masse395316d1996-12-09 20:10:36 +0000606static PyObject *
Martin v. Löwis26a63482006-02-15 17:27:45 +0000607array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000608{
Antoine Pitrou76748412010-05-09 14:46:46 +0000609 arrayobject *np;
610 if (ilow < 0)
611 ilow = 0;
612 else if (ilow > Py_SIZE(a))
613 ilow = Py_SIZE(a);
614 if (ihigh < 0)
615 ihigh = 0;
616 if (ihigh < ilow)
617 ihigh = ilow;
618 else if (ihigh > Py_SIZE(a))
619 ihigh = Py_SIZE(a);
620 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
621 if (np == NULL)
622 return NULL;
623 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
624 (ihigh-ilow) * a->ob_descr->itemsize);
625 return (PyObject *)np;
Guido van Rossum63d101e1993-02-19 15:55:02 +0000626}
627
Roger E. Masse395316d1996-12-09 20:10:36 +0000628static PyObject *
Raymond Hettinger6d9eed12004-03-13 18:18:51 +0000629array_copy(arrayobject *a, PyObject *unused)
630{
Antoine Pitrou76748412010-05-09 14:46:46 +0000631 return array_slice(a, 0, Py_SIZE(a));
Raymond Hettinger6d9eed12004-03-13 18:18:51 +0000632}
633
634PyDoc_STRVAR(copy_doc,
635"copy(array)\n\
636\n\
637 Return a copy of the array.");
638
639static PyObject *
Peter Schneider-Kamp846f68d2000-07-13 21:10:57 +0000640array_concat(arrayobject *a, PyObject *bb)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000641{
Antoine Pitrou76748412010-05-09 14:46:46 +0000642 Py_ssize_t size;
643 arrayobject *np;
644 if (!array_Check(bb)) {
645 PyErr_Format(PyExc_TypeError,
646 "can only append array (not \"%.200s\") to array",
647 Py_TYPE(bb)->tp_name);
648 return NULL;
649 }
Guido van Rossum63d101e1993-02-19 15:55:02 +0000650#define b ((arrayobject *)bb)
Antoine Pitrou76748412010-05-09 14:46:46 +0000651 if (a->ob_descr != b->ob_descr) {
652 PyErr_BadArgument();
653 return NULL;
654 }
655 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) {
656 return PyErr_NoMemory();
657 }
658 size = Py_SIZE(a) + Py_SIZE(b);
659 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
660 if (np == NULL) {
661 return NULL;
662 }
663 memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize);
664 memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize,
665 b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
666 return (PyObject *)np;
Guido van Rossum63d101e1993-02-19 15:55:02 +0000667#undef b
668}
669
Roger E. Masse395316d1996-12-09 20:10:36 +0000670static PyObject *
Martin v. Löwis26a63482006-02-15 17:27:45 +0000671array_repeat(arrayobject *a, Py_ssize_t n)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000672{
Antoine Pitrou76748412010-05-09 14:46:46 +0000673 Py_ssize_t i;
674 Py_ssize_t size;
675 arrayobject *np;
676 char *p;
677 Py_ssize_t nbytes;
678 if (n < 0)
679 n = 0;
680 if ((Py_SIZE(a) != 0) && (n > PY_SSIZE_T_MAX / Py_SIZE(a))) {
681 return PyErr_NoMemory();
682 }
683 size = Py_SIZE(a) * n;
684 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
685 if (np == NULL)
686 return NULL;
687 p = np->ob_item;
688 nbytes = Py_SIZE(a) * a->ob_descr->itemsize;
689 for (i = 0; i < n; i++) {
690 memcpy(p, a->ob_item, nbytes);
691 p += nbytes;
692 }
693 return (PyObject *) np;
Guido van Rossum63d101e1993-02-19 15:55:02 +0000694}
695
696static int
Martin v. Löwis452728f2006-02-16 14:30:23 +0000697array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000698{
Antoine Pitrou76748412010-05-09 14:46:46 +0000699 char *item;
700 Py_ssize_t n; /* Size of replacement array */
701 Py_ssize_t d; /* Change in size */
Guido van Rossum63d101e1993-02-19 15:55:02 +0000702#define b ((arrayobject *)v)
Antoine Pitrou76748412010-05-09 14:46:46 +0000703 if (v == NULL)
704 n = 0;
705 else if (array_Check(v)) {
706 n = Py_SIZE(b);
707 if (a == b) {
708 /* Special case "a[i:j] = a" -- copy b first */
709 int ret;
710 v = array_slice(b, 0, n);
711 if (!v)
712 return -1;
713 ret = array_ass_slice(a, ilow, ihigh, v);
714 Py_DECREF(v);
715 return ret;
716 }
717 if (b->ob_descr != a->ob_descr) {
718 PyErr_BadArgument();
719 return -1;
720 }
721 }
722 else {
723 PyErr_Format(PyExc_TypeError,
724 "can only assign array (not \"%.200s\") to array slice",
725 Py_TYPE(v)->tp_name);
726 return -1;
727 }
728 if (ilow < 0)
729 ilow = 0;
730 else if (ilow > Py_SIZE(a))
731 ilow = Py_SIZE(a);
732 if (ihigh < 0)
733 ihigh = 0;
734 if (ihigh < ilow)
735 ihigh = ilow;
736 else if (ihigh > Py_SIZE(a))
737 ihigh = Py_SIZE(a);
738 item = a->ob_item;
739 d = n - (ihigh-ilow);
740 if (d < 0) { /* Delete -d items */
741 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
742 item + ihigh*a->ob_descr->itemsize,
743 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
744 Py_SIZE(a) += d;
745 PyMem_RESIZE(item, char, Py_SIZE(a)*a->ob_descr->itemsize);
746 /* Can't fail */
747 a->ob_item = item;
748 a->allocated = Py_SIZE(a);
749 }
750 else if (d > 0) { /* Insert d items */
751 PyMem_RESIZE(item, char,
752 (Py_SIZE(a) + d)*a->ob_descr->itemsize);
753 if (item == NULL) {
754 PyErr_NoMemory();
755 return -1;
756 }
757 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
758 item + ihigh*a->ob_descr->itemsize,
759 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
760 a->ob_item = item;
761 Py_SIZE(a) += d;
762 a->allocated = Py_SIZE(a);
763 }
764 if (n > 0)
765 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
766 n*b->ob_descr->itemsize);
767 return 0;
Guido van Rossum63d101e1993-02-19 15:55:02 +0000768#undef b
769}
770
771static int
Martin v. Löwis26a63482006-02-15 17:27:45 +0000772array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000773{
Antoine Pitrou76748412010-05-09 14:46:46 +0000774 if (i < 0 || i >= Py_SIZE(a)) {
775 PyErr_SetString(PyExc_IndexError,
776 "array assignment index out of range");
777 return -1;
778 }
779 if (v == NULL)
780 return array_ass_slice(a, i, i+1, v);
781 return (*a->ob_descr->setitem)(a, i, v);
Guido van Rossum63d101e1993-02-19 15:55:02 +0000782}
783
784static int
Martin v. Löwis26a63482006-02-15 17:27:45 +0000785setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000786{
Antoine Pitrou76748412010-05-09 14:46:46 +0000787 assert(array_Check(a));
788 return array_ass_item((arrayobject *)a, i, v);
Guido van Rossum63d101e1993-02-19 15:55:02 +0000789}
790
Martin v. Löwis669620f2002-03-01 10:27:01 +0000791static int
Raymond Hettingere5f628b2004-03-14 05:43:59 +0000792array_iter_extend(arrayobject *self, PyObject *bb)
793{
Antoine Pitrou76748412010-05-09 14:46:46 +0000794 PyObject *it, *v;
Raymond Hettingere5f628b2004-03-14 05:43:59 +0000795
Antoine Pitrou76748412010-05-09 14:46:46 +0000796 it = PyObject_GetIter(bb);
797 if (it == NULL)
798 return -1;
Raymond Hettingere5f628b2004-03-14 05:43:59 +0000799
Antoine Pitrou76748412010-05-09 14:46:46 +0000800 while ((v = PyIter_Next(it)) != NULL) {
Mark Dickinson2c3bac82010-08-06 09:44:48 +0000801 if (ins1(self, Py_SIZE(self), v) != 0) {
Antoine Pitrou76748412010-05-09 14:46:46 +0000802 Py_DECREF(v);
803 Py_DECREF(it);
804 return -1;
805 }
806 Py_DECREF(v);
807 }
808 Py_DECREF(it);
809 if (PyErr_Occurred())
810 return -1;
811 return 0;
Raymond Hettingere5f628b2004-03-14 05:43:59 +0000812}
813
814static int
Martin v. Löwis669620f2002-03-01 10:27:01 +0000815array_do_extend(arrayobject *self, PyObject *bb)
816{
Antoine Pitrou76748412010-05-09 14:46:46 +0000817 Py_ssize_t size;
818 char *old_item;
Martin v. Löwis669620f2002-03-01 10:27:01 +0000819
Antoine Pitrou76748412010-05-09 14:46:46 +0000820 if (!array_Check(bb))
821 return array_iter_extend(self, bb);
Martin v. Löwis669620f2002-03-01 10:27:01 +0000822#define b ((arrayobject *)bb)
Antoine Pitrou76748412010-05-09 14:46:46 +0000823 if (self->ob_descr != b->ob_descr) {
824 PyErr_SetString(PyExc_TypeError,
825 "can only extend with array of same kind");
826 return -1;
827 }
828 if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) ||
829 ((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
830 PyErr_NoMemory();
831 return -1;
832 }
833 size = Py_SIZE(self) + Py_SIZE(b);
834 old_item = self->ob_item;
835 PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize);
836 if (self->ob_item == NULL) {
837 self->ob_item = old_item;
838 PyErr_NoMemory();
839 return -1;
840 }
841 memcpy(self->ob_item + Py_SIZE(self)*self->ob_descr->itemsize,
842 b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
843 Py_SIZE(self) = size;
844 self->allocated = size;
Martin v. Löwis669620f2002-03-01 10:27:01 +0000845
Antoine Pitrou76748412010-05-09 14:46:46 +0000846 return 0;
Martin v. Löwis669620f2002-03-01 10:27:01 +0000847#undef b
848}
849
850static PyObject *
851array_inplace_concat(arrayobject *self, PyObject *bb)
852{
Antoine Pitrou76748412010-05-09 14:46:46 +0000853 if (!array_Check(bb)) {
854 PyErr_Format(PyExc_TypeError,
855 "can only extend array with array (not \"%.200s\")",
856 Py_TYPE(bb)->tp_name);
857 return NULL;
858 }
859 if (array_do_extend(self, bb) == -1)
860 return NULL;
861 Py_INCREF(self);
862 return (PyObject *)self;
Martin v. Löwis669620f2002-03-01 10:27:01 +0000863}
864
865static PyObject *
Martin v. Löwis26a63482006-02-15 17:27:45 +0000866array_inplace_repeat(arrayobject *self, Py_ssize_t n)
Martin v. Löwis669620f2002-03-01 10:27:01 +0000867{
Antoine Pitrou76748412010-05-09 14:46:46 +0000868 char *items, *p;
869 Py_ssize_t size, i;
Martin v. Löwis669620f2002-03-01 10:27:01 +0000870
Antoine Pitrou76748412010-05-09 14:46:46 +0000871 if (Py_SIZE(self) > 0) {
872 if (n < 0)
873 n = 0;
874 items = self->ob_item;
875 if ((self->ob_descr->itemsize != 0) &&
876 (Py_SIZE(self) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
877 return PyErr_NoMemory();
878 }
879 size = Py_SIZE(self) * self->ob_descr->itemsize;
880 if (n == 0) {
881 PyMem_FREE(items);
882 self->ob_item = NULL;
883 Py_SIZE(self) = 0;
884 self->allocated = 0;
885 }
886 else {
887 if (size > PY_SSIZE_T_MAX / n) {
888 return PyErr_NoMemory();
889 }
890 PyMem_RESIZE(items, char, n * size);
891 if (items == NULL)
892 return PyErr_NoMemory();
893 p = items;
894 for (i = 1; i < n; i++) {
895 p += size;
896 memcpy(p, items, size);
897 }
898 self->ob_item = items;
899 Py_SIZE(self) *= n;
900 self->allocated = Py_SIZE(self);
901 }
902 }
903 Py_INCREF(self);
904 return (PyObject *)self;
Martin v. Löwis669620f2002-03-01 10:27:01 +0000905}
906
907
Roger E. Masse395316d1996-12-09 20:10:36 +0000908static PyObject *
Martin v. Löwis26a63482006-02-15 17:27:45 +0000909ins(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum63d101e1993-02-19 15:55:02 +0000910{
Antoine Pitrou76748412010-05-09 14:46:46 +0000911 if (ins1(self, where, v) != 0)
912 return NULL;
913 Py_INCREF(Py_None);
914 return Py_None;
Guido van Rossum63d101e1993-02-19 15:55:02 +0000915}
916
Roger E. Masse395316d1996-12-09 20:10:36 +0000917static PyObject *
Raymond Hettinger3bd37bf2003-01-03 08:24:58 +0000918array_count(arrayobject *self, PyObject *v)
Peter Schneider-Kamp85f64432000-07-31 20:52:21 +0000919{
Antoine Pitrou76748412010-05-09 14:46:46 +0000920 Py_ssize_t count = 0;
921 Py_ssize_t i;
Peter Schneider-Kamp85f64432000-07-31 20:52:21 +0000922
Antoine Pitrou76748412010-05-09 14:46:46 +0000923 for (i = 0; i < Py_SIZE(self); i++) {
924 PyObject *selfi = getarrayitem((PyObject *)self, i);
925 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
926 Py_DECREF(selfi);
927 if (cmp > 0)
928 count++;
929 else if (cmp < 0)
930 return NULL;
931 }
932 return PyInt_FromSsize_t(count);
Peter Schneider-Kamp85f64432000-07-31 20:52:21 +0000933}
934
Martin v. Löwis1117ad52002-06-13 20:33:02 +0000935PyDoc_STRVAR(count_doc,
Tim Peters37982492000-09-16 22:31:29 +0000936"count(x)\n\
Peter Schneider-Kamp85f64432000-07-31 20:52:21 +0000937\n\
Mark Dickinsonaef7f682009-02-21 20:27:01 +0000938Return number of occurrences of x in the array.");
Peter Schneider-Kamp85f64432000-07-31 20:52:21 +0000939
940static PyObject *
Raymond Hettinger3bd37bf2003-01-03 08:24:58 +0000941array_index(arrayobject *self, PyObject *v)
Peter Schneider-Kamp85f64432000-07-31 20:52:21 +0000942{
Antoine Pitrou76748412010-05-09 14:46:46 +0000943 Py_ssize_t i;
Peter Schneider-Kamp85f64432000-07-31 20:52:21 +0000944
Antoine Pitrou76748412010-05-09 14:46:46 +0000945 for (i = 0; i < Py_SIZE(self); i++) {
946 PyObject *selfi = getarrayitem((PyObject *)self, i);
947 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
948 Py_DECREF(selfi);
949 if (cmp > 0) {
950 return PyInt_FromLong((long)i);
951 }
952 else if (cmp < 0)
953 return NULL;
954 }
955 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
956 return NULL;
Peter Schneider-Kamp85f64432000-07-31 20:52:21 +0000957}
958
Martin v. Löwis1117ad52002-06-13 20:33:02 +0000959PyDoc_STRVAR(index_doc,
Tim Peters37982492000-09-16 22:31:29 +0000960"index(x)\n\
Peter Schneider-Kamp85f64432000-07-31 20:52:21 +0000961\n\
Mark Dickinsonaef7f682009-02-21 20:27:01 +0000962Return index of first occurrence of x in the array.");
Peter Schneider-Kamp85f64432000-07-31 20:52:21 +0000963
Raymond Hettinger409b7522003-01-07 01:58:52 +0000964static int
965array_contains(arrayobject *self, PyObject *v)
966{
Antoine Pitrou76748412010-05-09 14:46:46 +0000967 Py_ssize_t i;
968 int cmp;
Raymond Hettinger409b7522003-01-07 01:58:52 +0000969
Antoine Pitrou76748412010-05-09 14:46:46 +0000970 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) {
971 PyObject *selfi = getarrayitem((PyObject *)self, i);
972 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
973 Py_DECREF(selfi);
974 }
975 return cmp;
Raymond Hettinger409b7522003-01-07 01:58:52 +0000976}
977
Peter Schneider-Kamp85f64432000-07-31 20:52:21 +0000978static PyObject *
Raymond Hettinger3bd37bf2003-01-03 08:24:58 +0000979array_remove(arrayobject *self, PyObject *v)
Peter Schneider-Kamp85f64432000-07-31 20:52:21 +0000980{
Antoine Pitrou76748412010-05-09 14:46:46 +0000981 int i;
Peter Schneider-Kamp85f64432000-07-31 20:52:21 +0000982
Antoine Pitrou76748412010-05-09 14:46:46 +0000983 for (i = 0; i < Py_SIZE(self); i++) {
984 PyObject *selfi = getarrayitem((PyObject *)self,i);
985 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
986 Py_DECREF(selfi);
987 if (cmp > 0) {
988 if (array_ass_slice(self, i, i+1,
989 (PyObject *)NULL) != 0)
990 return NULL;
991 Py_INCREF(Py_None);
992 return Py_None;
993 }
994 else if (cmp < 0)
995 return NULL;
996 }
997 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
998 return NULL;
Peter Schneider-Kamp85f64432000-07-31 20:52:21 +0000999}
1000
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001001PyDoc_STRVAR(remove_doc,
Tim Peters37982492000-09-16 22:31:29 +00001002"remove(x)\n\
Peter Schneider-Kamp85f64432000-07-31 20:52:21 +00001003\n\
Mark Dickinsonaef7f682009-02-21 20:27:01 +00001004Remove the first occurrence of x in the array.");
Peter Schneider-Kamp85f64432000-07-31 20:52:21 +00001005
1006static PyObject *
1007array_pop(arrayobject *self, PyObject *args)
1008{
Antoine Pitrou76748412010-05-09 14:46:46 +00001009 Py_ssize_t i = -1;
1010 PyObject *v;
1011 if (!PyArg_ParseTuple(args, "|n:pop", &i))
1012 return NULL;
1013 if (Py_SIZE(self) == 0) {
1014 /* Special-case most common failure cause */
1015 PyErr_SetString(PyExc_IndexError, "pop from empty array");
1016 return NULL;
1017 }
1018 if (i < 0)
1019 i += Py_SIZE(self);
1020 if (i < 0 || i >= Py_SIZE(self)) {
1021 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1022 return NULL;
1023 }
1024 v = getarrayitem((PyObject *)self,i);
1025 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
1026 Py_DECREF(v);
1027 return NULL;
1028 }
1029 return v;
Peter Schneider-Kamp85f64432000-07-31 20:52:21 +00001030}
1031
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001032PyDoc_STRVAR(pop_doc,
Tim Peters37982492000-09-16 22:31:29 +00001033"pop([i])\n\
Peter Schneider-Kamp85f64432000-07-31 20:52:21 +00001034\n\
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001035Return the i-th element and delete it from the array. i defaults to -1.");
Peter Schneider-Kamp85f64432000-07-31 20:52:21 +00001036
1037static PyObject *
Raymond Hettinger3bd37bf2003-01-03 08:24:58 +00001038array_extend(arrayobject *self, PyObject *bb)
Peter Schneider-Kamp85f64432000-07-31 20:52:21 +00001039{
Antoine Pitrou76748412010-05-09 14:46:46 +00001040 if (array_do_extend(self, bb) == -1)
1041 return NULL;
1042 Py_INCREF(Py_None);
1043 return Py_None;
Peter Schneider-Kamp85f64432000-07-31 20:52:21 +00001044}
1045
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001046PyDoc_STRVAR(extend_doc,
Raymond Hettingere5f628b2004-03-14 05:43:59 +00001047"extend(array or iterable)\n\
Peter Schneider-Kamp85f64432000-07-31 20:52:21 +00001048\n\
Raymond Hettingere5f628b2004-03-14 05:43:59 +00001049 Append items to the end of the array.");
Peter Schneider-Kamp85f64432000-07-31 20:52:21 +00001050
1051static PyObject *
Peter Schneider-Kamp846f68d2000-07-13 21:10:57 +00001052array_insert(arrayobject *self, PyObject *args)
Guido van Rossum63d101e1993-02-19 15:55:02 +00001053{
Antoine Pitrou76748412010-05-09 14:46:46 +00001054 Py_ssize_t i;
1055 PyObject *v;
1056 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
1057 return NULL;
1058 return ins(self, i, v);
Guido van Rossum63d101e1993-02-19 15:55:02 +00001059}
1060
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001061PyDoc_STRVAR(insert_doc,
Tim Peters37982492000-09-16 22:31:29 +00001062"insert(i,x)\n\
Guido van Rossum7627fef1998-10-13 14:27:22 +00001063\n\
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001064Insert a new item x into the array before position i.");
Guido van Rossum7627fef1998-10-13 14:27:22 +00001065
1066
Roger E. Masse395316d1996-12-09 20:10:36 +00001067static PyObject *
Raymond Hettinger3bd37bf2003-01-03 08:24:58 +00001068array_buffer_info(arrayobject *self, PyObject *unused)
Guido van Rossum29b13fa1997-08-12 14:55:56 +00001069{
Antoine Pitrou76748412010-05-09 14:46:46 +00001070 PyObject* retval = NULL;
1071 retval = PyTuple_New(2);
1072 if (!retval)
1073 return NULL;
Fred Draked2cf6ca2000-06-28 17:49:30 +00001074
Antoine Pitrou76748412010-05-09 14:46:46 +00001075 PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item));
1076 PyTuple_SET_ITEM(retval, 1, PyInt_FromLong((long)(Py_SIZE(self))));
Fred Draked2cf6ca2000-06-28 17:49:30 +00001077
Antoine Pitrou76748412010-05-09 14:46:46 +00001078 return retval;
Guido van Rossum29b13fa1997-08-12 14:55:56 +00001079}
1080
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001081PyDoc_STRVAR(buffer_info_doc,
Tim Peters37982492000-09-16 22:31:29 +00001082"buffer_info() -> (address, length)\n\
Guido van Rossum7627fef1998-10-13 14:27:22 +00001083\n\
1084Return a tuple (address, length) giving the current memory address and\n\
Guido van Rossum44ad8532001-07-27 16:05:32 +00001085the length in items of the buffer used to hold array's contents\n\
1086The length should be multiplied by the itemsize attribute to calculate\n\
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001087the buffer length in bytes.");
Guido van Rossum7627fef1998-10-13 14:27:22 +00001088
1089
Guido van Rossum29b13fa1997-08-12 14:55:56 +00001090static PyObject *
Raymond Hettinger3bd37bf2003-01-03 08:24:58 +00001091array_append(arrayobject *self, PyObject *v)
Guido van Rossum63d101e1993-02-19 15:55:02 +00001092{
Mark Dickinson2c3bac82010-08-06 09:44:48 +00001093 return ins(self, Py_SIZE(self), v);
Guido van Rossum63d101e1993-02-19 15:55:02 +00001094}
1095
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001096PyDoc_STRVAR(append_doc,
Guido van Rossum7627fef1998-10-13 14:27:22 +00001097"append(x)\n\
1098\n\
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001099Append new value x to the end of the array.");
Guido van Rossum7627fef1998-10-13 14:27:22 +00001100
1101
Roger E. Masse395316d1996-12-09 20:10:36 +00001102static PyObject *
Raymond Hettinger3bd37bf2003-01-03 08:24:58 +00001103array_byteswap(arrayobject *self, PyObject *unused)
Guido van Rossum63d101e1993-02-19 15:55:02 +00001104{
Antoine Pitrou76748412010-05-09 14:46:46 +00001105 char *p;
1106 Py_ssize_t i;
Fred Drakec27cccc1999-12-03 17:15:30 +00001107
Antoine Pitrou76748412010-05-09 14:46:46 +00001108 switch (self->ob_descr->itemsize) {
1109 case 1:
1110 break;
1111 case 2:
1112 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) {
1113 char p0 = p[0];
1114 p[0] = p[1];
1115 p[1] = p0;
1116 }
1117 break;
1118 case 4:
1119 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 4) {
1120 char p0 = p[0];
1121 char p1 = p[1];
1122 p[0] = p[3];
1123 p[1] = p[2];
1124 p[2] = p1;
1125 p[3] = p0;
1126 }
1127 break;
1128 case 8:
1129 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) {
1130 char p0 = p[0];
1131 char p1 = p[1];
1132 char p2 = p[2];
1133 char p3 = p[3];
1134 p[0] = p[7];
1135 p[1] = p[6];
1136 p[2] = p[5];
1137 p[3] = p[4];
1138 p[4] = p3;
1139 p[5] = p2;
1140 p[6] = p1;
1141 p[7] = p0;
1142 }
1143 break;
1144 default:
1145 PyErr_SetString(PyExc_RuntimeError,
1146 "don't know how to byteswap this array type");
1147 return NULL;
1148 }
1149 Py_INCREF(Py_None);
1150 return Py_None;
Guido van Rossum63d101e1993-02-19 15:55:02 +00001151}
1152
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001153PyDoc_STRVAR(byteswap_doc,
Fred Drakec27cccc1999-12-03 17:15:30 +00001154"byteswap()\n\
Guido van Rossum7627fef1998-10-13 14:27:22 +00001155\n\
Fred Drakec27cccc1999-12-03 17:15:30 +00001156Byteswap all items of the array. If the items in the array are not 1, 2,\n\
Martin v. Löwis1117ad52002-06-13 20:33:02 +000011574, or 8 bytes in size, RuntimeError is raised.");
Guido van Rossum7627fef1998-10-13 14:27:22 +00001158
Roger E. Masse395316d1996-12-09 20:10:36 +00001159static PyObject *
Raymond Hettinger3bd37bf2003-01-03 08:24:58 +00001160array_reverse(arrayobject *self, PyObject *unused)
Guido van Rossum63d101e1993-02-19 15:55:02 +00001161{
Antoine Pitrou76748412010-05-09 14:46:46 +00001162 register Py_ssize_t itemsize = self->ob_descr->itemsize;
1163 register char *p, *q;
1164 /* little buffer to hold items while swapping */
1165 char tmp[256]; /* 8 is probably enough -- but why skimp */
1166 assert((size_t)itemsize <= sizeof(tmp));
Guido van Rossum76774371993-11-03 15:01:26 +00001167
Antoine Pitrou76748412010-05-09 14:46:46 +00001168 if (Py_SIZE(self) > 1) {
1169 for (p = self->ob_item,
1170 q = self->ob_item + (Py_SIZE(self) - 1)*itemsize;
1171 p < q;
1172 p += itemsize, q -= itemsize) {
1173 /* memory areas guaranteed disjoint, so memcpy
1174 * is safe (& memmove may be slower).
1175 */
1176 memcpy(tmp, p, itemsize);
1177 memcpy(p, q, itemsize);
1178 memcpy(q, tmp, itemsize);
1179 }
1180 }
Tim Peters88d20252000-09-10 05:22:54 +00001181
Antoine Pitrou76748412010-05-09 14:46:46 +00001182 Py_INCREF(Py_None);
1183 return Py_None;
Guido van Rossum63d101e1993-02-19 15:55:02 +00001184}
Guido van Rossum76774371993-11-03 15:01:26 +00001185
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001186PyDoc_STRVAR(reverse_doc,
Guido van Rossum7627fef1998-10-13 14:27:22 +00001187"reverse()\n\
1188\n\
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001189Reverse the order of the items in the array.");
Guido van Rossum7627fef1998-10-13 14:27:22 +00001190
Roger E. Masse395316d1996-12-09 20:10:36 +00001191static PyObject *
Peter Schneider-Kamp846f68d2000-07-13 21:10:57 +00001192array_fromfile(arrayobject *self, PyObject *args)
Guido van Rossum63d101e1993-02-19 15:55:02 +00001193{
Antoine Pitrou76748412010-05-09 14:46:46 +00001194 PyObject *f;
1195 Py_ssize_t n;
1196 FILE *fp;
1197 if (!PyArg_ParseTuple(args, "On:fromfile", &f, &n))
1198 return NULL;
1199 fp = PyFile_AsFile(f);
1200 if (fp == NULL) {
1201 PyErr_SetString(PyExc_TypeError, "arg1 must be open file");
1202 return NULL;
1203 }
1204 if (n > 0) {
1205 char *item = self->ob_item;
1206 Py_ssize_t itemsize = self->ob_descr->itemsize;
1207 size_t nread;
1208 Py_ssize_t newlength;
1209 size_t newbytes;
1210 /* Be careful here about overflow */
1211 if ((newlength = Py_SIZE(self) + n) <= 0 ||
1212 (newbytes = newlength * itemsize) / itemsize !=
1213 (size_t)newlength)
1214 goto nomem;
1215 PyMem_RESIZE(item, char, newbytes);
1216 if (item == NULL) {
1217 nomem:
1218 PyErr_NoMemory();
1219 return NULL;
1220 }
1221 self->ob_item = item;
1222 Py_SIZE(self) += n;
1223 self->allocated = Py_SIZE(self);
1224 nread = fread(item + (Py_SIZE(self) - n) * itemsize,
1225 itemsize, n, fp);
1226 if (nread < (size_t)n) {
1227 Py_SIZE(self) -= (n - nread);
1228 PyMem_RESIZE(item, char, Py_SIZE(self)*itemsize);
1229 self->ob_item = item;
1230 self->allocated = Py_SIZE(self);
Antoine Pitrouc18c68f2010-07-21 16:47:28 +00001231 if (ferror(fp)) {
1232 PyErr_SetFromErrno(PyExc_IOError);
1233 clearerr(fp);
1234 }
1235 else {
1236 PyErr_SetString(PyExc_EOFError,
1237 "not enough items in file");
1238 }
Antoine Pitrou76748412010-05-09 14:46:46 +00001239 return NULL;
1240 }
1241 }
1242 Py_INCREF(Py_None);
1243 return Py_None;
Guido van Rossum63d101e1993-02-19 15:55:02 +00001244}
1245
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001246PyDoc_STRVAR(fromfile_doc,
Guido van Rossum7627fef1998-10-13 14:27:22 +00001247"fromfile(f, n)\n\
1248\n\
1249Read n objects from the file object f and append them to the end of the\n\
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001250array. Also called as read.");
Guido van Rossum7627fef1998-10-13 14:27:22 +00001251
1252
Roger E. Masse395316d1996-12-09 20:10:36 +00001253static PyObject *
Georg Brandl01725102008-03-25 08:37:23 +00001254array_fromfile_as_read(arrayobject *self, PyObject *args)
1255{
Antoine Pitrou76748412010-05-09 14:46:46 +00001256 if (PyErr_WarnPy3k("array.read() not supported in 3.x; "
1257 "use array.fromfile()", 1) < 0)
1258 return NULL;
1259 return array_fromfile(self, args);
Georg Brandl01725102008-03-25 08:37:23 +00001260}
1261
1262
1263static PyObject *
Raymond Hettinger3bd37bf2003-01-03 08:24:58 +00001264array_tofile(arrayobject *self, PyObject *f)
Guido van Rossum63d101e1993-02-19 15:55:02 +00001265{
Antoine Pitrou76748412010-05-09 14:46:46 +00001266 FILE *fp;
Raymond Hettinger3bd37bf2003-01-03 08:24:58 +00001267
Antoine Pitrou76748412010-05-09 14:46:46 +00001268 fp = PyFile_AsFile(f);
1269 if (fp == NULL) {
1270 PyErr_SetString(PyExc_TypeError, "arg must be open file");
1271 return NULL;
1272 }
1273 if (self->ob_size > 0) {
1274 if (fwrite(self->ob_item, self->ob_descr->itemsize,
1275 self->ob_size, fp) != (size_t)self->ob_size) {
1276 PyErr_SetFromErrno(PyExc_IOError);
1277 clearerr(fp);
1278 return NULL;
1279 }
1280 }
1281 Py_INCREF(Py_None);
1282 return Py_None;
Guido van Rossum63d101e1993-02-19 15:55:02 +00001283}
1284
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001285PyDoc_STRVAR(tofile_doc,
Guido van Rossum7627fef1998-10-13 14:27:22 +00001286"tofile(f)\n\
1287\n\
1288Write all items (as machine values) to the file object f. Also called as\n\
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001289write.");
Guido van Rossum7627fef1998-10-13 14:27:22 +00001290
1291
Roger E. Masse395316d1996-12-09 20:10:36 +00001292static PyObject *
Georg Brandl01725102008-03-25 08:37:23 +00001293array_tofile_as_write(arrayobject *self, PyObject *f)
1294{
Antoine Pitrou76748412010-05-09 14:46:46 +00001295 if (PyErr_WarnPy3k("array.write() not supported in 3.x; "
1296 "use array.tofile()", 1) < 0)
1297 return NULL;
1298 return array_tofile(self, f);
Georg Brandl01725102008-03-25 08:37:23 +00001299}
1300
1301
1302static PyObject *
Raymond Hettinger3bd37bf2003-01-03 08:24:58 +00001303array_fromlist(arrayobject *self, PyObject *list)
Guido van Rossum63d101e1993-02-19 15:55:02 +00001304{
Antoine Pitrou76748412010-05-09 14:46:46 +00001305 Py_ssize_t n;
1306 Py_ssize_t itemsize = self->ob_descr->itemsize;
Raymond Hettinger3bd37bf2003-01-03 08:24:58 +00001307
Antoine Pitrou76748412010-05-09 14:46:46 +00001308 if (!PyList_Check(list)) {
1309 PyErr_SetString(PyExc_TypeError, "arg must be list");
1310 return NULL;
1311 }
1312 n = PyList_Size(list);
1313 if (n > 0) {
1314 char *item = self->ob_item;
1315 Py_ssize_t i;
1316 PyMem_RESIZE(item, char, (Py_SIZE(self) + n) * itemsize);
1317 if (item == NULL) {
1318 PyErr_NoMemory();
1319 return NULL;
1320 }
1321 self->ob_item = item;
1322 Py_SIZE(self) += n;
1323 self->allocated = Py_SIZE(self);
1324 for (i = 0; i < n; i++) {
1325 PyObject *v = PyList_GetItem(list, i);
1326 if ((*self->ob_descr->setitem)(self,
1327 Py_SIZE(self) - n + i, v) != 0) {
1328 Py_SIZE(self) -= n;
1329 if (itemsize && (self->ob_size > PY_SSIZE_T_MAX / itemsize)) {
1330 return PyErr_NoMemory();
1331 }
1332 PyMem_RESIZE(item, char,
1333 Py_SIZE(self) * itemsize);
1334 self->ob_item = item;
1335 self->allocated = Py_SIZE(self);
1336 return NULL;
1337 }
1338 }
1339 }
1340 Py_INCREF(Py_None);
1341 return Py_None;
Guido van Rossum63d101e1993-02-19 15:55:02 +00001342}
1343
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001344PyDoc_STRVAR(fromlist_doc,
Guido van Rossum7627fef1998-10-13 14:27:22 +00001345"fromlist(list)\n\
1346\n\
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001347Append items to array from list.");
Guido van Rossum7627fef1998-10-13 14:27:22 +00001348
1349
Roger E. Masse395316d1996-12-09 20:10:36 +00001350static PyObject *
Raymond Hettinger3bd37bf2003-01-03 08:24:58 +00001351array_tolist(arrayobject *self, PyObject *unused)
Guido van Rossum63d101e1993-02-19 15:55:02 +00001352{
Antoine Pitrou76748412010-05-09 14:46:46 +00001353 PyObject *list = PyList_New(Py_SIZE(self));
1354 Py_ssize_t i;
Raymond Hettinger3bd37bf2003-01-03 08:24:58 +00001355
Antoine Pitrou76748412010-05-09 14:46:46 +00001356 if (list == NULL)
1357 return NULL;
1358 for (i = 0; i < Py_SIZE(self); i++) {
1359 PyObject *v = getarrayitem((PyObject *)self, i);
1360 if (v == NULL) {
1361 Py_DECREF(list);
1362 return NULL;
1363 }
1364 PyList_SetItem(list, i, v);
1365 }
1366 return list;
Guido van Rossum63d101e1993-02-19 15:55:02 +00001367}
1368
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001369PyDoc_STRVAR(tolist_doc,
Guido van Rossum401c6ae1998-10-14 02:52:31 +00001370"tolist() -> list\n\
Guido van Rossum7627fef1998-10-13 14:27:22 +00001371\n\
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001372Convert array to an ordinary list with the same items.");
Guido van Rossum7627fef1998-10-13 14:27:22 +00001373
1374
Roger E. Masse395316d1996-12-09 20:10:36 +00001375static PyObject *
Peter Schneider-Kamp846f68d2000-07-13 21:10:57 +00001376array_fromstring(arrayobject *self, PyObject *args)
Guido van Rossum63d101e1993-02-19 15:55:02 +00001377{
Antoine Pitrou76748412010-05-09 14:46:46 +00001378 char *str;
1379 Py_ssize_t n;
1380 int itemsize = self->ob_descr->itemsize;
1381 if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n))
1382 return NULL;
Serhiy Storchakaf0e62122015-07-26 08:49:37 +03001383 if (str == self->ob_item) {
1384 PyErr_SetString(PyExc_ValueError,
1385 "array.fromstring(x): x cannot be self");
1386 return NULL;
1387 }
Antoine Pitrou76748412010-05-09 14:46:46 +00001388 if (n % itemsize != 0) {
1389 PyErr_SetString(PyExc_ValueError,
1390 "string length not a multiple of item size");
1391 return NULL;
1392 }
1393 n = n / itemsize;
1394 if (n > 0) {
1395 char *item = self->ob_item;
1396 if ((n > PY_SSIZE_T_MAX - Py_SIZE(self)) ||
1397 ((Py_SIZE(self) + n) > PY_SSIZE_T_MAX / itemsize)) {
1398 return PyErr_NoMemory();
1399 }
1400 PyMem_RESIZE(item, char, (Py_SIZE(self) + n) * itemsize);
1401 if (item == NULL) {
1402 PyErr_NoMemory();
1403 return NULL;
1404 }
1405 self->ob_item = item;
1406 Py_SIZE(self) += n;
1407 self->allocated = Py_SIZE(self);
1408 memcpy(item + (Py_SIZE(self) - n) * itemsize,
1409 str, itemsize*n);
1410 }
1411 Py_INCREF(Py_None);
1412 return Py_None;
Guido van Rossum63d101e1993-02-19 15:55:02 +00001413}
1414
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001415PyDoc_STRVAR(fromstring_doc,
Guido van Rossum7627fef1998-10-13 14:27:22 +00001416"fromstring(string)\n\
1417\n\
1418Appends items from the string, interpreting it as an array of machine\n\
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001419values,as if it had been read from a file using the fromfile() method).");
Guido van Rossum7627fef1998-10-13 14:27:22 +00001420
1421
Roger E. Masse395316d1996-12-09 20:10:36 +00001422static PyObject *
Raymond Hettinger3bd37bf2003-01-03 08:24:58 +00001423array_tostring(arrayobject *self, PyObject *unused)
Guido van Rossum63d101e1993-02-19 15:55:02 +00001424{
Antoine Pitrou76748412010-05-09 14:46:46 +00001425 if (self->ob_size <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
1426 return PyString_FromStringAndSize(self->ob_item,
1427 Py_SIZE(self) * self->ob_descr->itemsize);
1428 } else {
1429 return PyErr_NoMemory();
1430 }
Guido van Rossum63d101e1993-02-19 15:55:02 +00001431}
1432
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001433PyDoc_STRVAR(tostring_doc,
Guido van Rossum7627fef1998-10-13 14:27:22 +00001434"tostring() -> string\n\
1435\n\
1436Convert the array to an array of machine values and return the string\n\
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001437representation.");
Guido van Rossum7627fef1998-10-13 14:27:22 +00001438
Martin v. Löwis669620f2002-03-01 10:27:01 +00001439
1440
1441#ifdef Py_USING_UNICODE
1442static PyObject *
1443array_fromunicode(arrayobject *self, PyObject *args)
1444{
Antoine Pitrou76748412010-05-09 14:46:46 +00001445 Py_UNICODE *ustr;
1446 Py_ssize_t n;
Martin v. Löwis669620f2002-03-01 10:27:01 +00001447
Antoine Pitrou76748412010-05-09 14:46:46 +00001448 if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
1449 return NULL;
1450 if (self->ob_descr->typecode != 'u') {
1451 PyErr_SetString(PyExc_ValueError,
1452 "fromunicode() may only be called on "
1453 "type 'u' arrays");
1454 return NULL;
1455 }
1456 if (n > 0) {
1457 Py_UNICODE *item = (Py_UNICODE *) self->ob_item;
1458 if (Py_SIZE(self) > PY_SSIZE_T_MAX - n) {
1459 return PyErr_NoMemory();
1460 }
1461 PyMem_RESIZE(item, Py_UNICODE, Py_SIZE(self) + n);
1462 if (item == NULL) {
1463 PyErr_NoMemory();
1464 return NULL;
1465 }
1466 self->ob_item = (char *) item;
1467 Py_SIZE(self) += n;
1468 self->allocated = Py_SIZE(self);
1469 memcpy(item + Py_SIZE(self) - n,
1470 ustr, n * sizeof(Py_UNICODE));
1471 }
Martin v. Löwis669620f2002-03-01 10:27:01 +00001472
Antoine Pitrou76748412010-05-09 14:46:46 +00001473 Py_INCREF(Py_None);
1474 return Py_None;
Martin v. Löwis669620f2002-03-01 10:27:01 +00001475}
1476
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001477PyDoc_STRVAR(fromunicode_doc,
Martin v. Löwis669620f2002-03-01 10:27:01 +00001478"fromunicode(ustr)\n\
1479\n\
1480Extends this array with data from the unicode string ustr.\n\
1481The array must be a type 'u' array; otherwise a ValueError\n\
1482is raised. Use array.fromstring(ustr.decode(...)) to\n\
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001483append Unicode data to an array of some other type.");
Martin v. Löwis669620f2002-03-01 10:27:01 +00001484
1485
1486static PyObject *
Raymond Hettinger3bd37bf2003-01-03 08:24:58 +00001487array_tounicode(arrayobject *self, PyObject *unused)
Martin v. Löwis669620f2002-03-01 10:27:01 +00001488{
Antoine Pitrou76748412010-05-09 14:46:46 +00001489 if (self->ob_descr->typecode != 'u') {
1490 PyErr_SetString(PyExc_ValueError,
1491 "tounicode() may only be called on type 'u' arrays");
1492 return NULL;
1493 }
1494 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_SIZE(self));
Martin v. Löwis669620f2002-03-01 10:27:01 +00001495}
1496
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001497PyDoc_STRVAR(tounicode_doc,
Martin v. Löwis669620f2002-03-01 10:27:01 +00001498"tounicode() -> unicode\n\
1499\n\
1500Convert the array to a unicode string. The array must be\n\
1501a type 'u' array; otherwise a ValueError is raised. Use\n\
1502array.tostring().decode() to obtain a unicode string from\n\
Martin v. Löwis1117ad52002-06-13 20:33:02 +00001503an array of some other type.");
Martin v. Löwis669620f2002-03-01 10:27:01 +00001504
1505#endif /* Py_USING_UNICODE */
1506
Alexandre Vassalotti03391d82009-07-15 18:19:47 +00001507static PyObject *
1508array_reduce(arrayobject *array)
1509{
Antoine Pitrou76748412010-05-09 14:46:46 +00001510 PyObject *dict, *result, *list;
Alexandre Vassalotti03391d82009-07-15 18:19:47 +00001511
Antoine Pitrou76748412010-05-09 14:46:46 +00001512 dict = PyObject_GetAttrString((PyObject *)array, "__dict__");
1513 if (dict == NULL) {
1514 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1515 return NULL;
1516 PyErr_Clear();
1517 dict = Py_None;
1518 Py_INCREF(dict);
1519 }
1520 /* Unlike in Python 3.x, we never use the more efficient memory
1521 * representation of an array for pickling. This is unfortunately
1522 * necessary to allow array objects to be unpickled by Python 3.x,
1523 * since str objects from 2.x are always decoded to unicode in
1524 * Python 3.x.
1525 */
1526 list = array_tolist(array, NULL);
1527 if (list == NULL) {
1528 Py_DECREF(dict);
1529 return NULL;
1530 }
1531 result = Py_BuildValue(
1532 "O(cO)O", Py_TYPE(array), array->ob_descr->typecode, list, dict);
1533 Py_DECREF(list);
1534 Py_DECREF(dict);
1535 return result;
Alexandre Vassalotti03391d82009-07-15 18:19:47 +00001536}
1537
1538PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
Martin v. Löwis669620f2002-03-01 10:27:01 +00001539
1540static PyObject *
Meador Inge4f5f3482012-08-10 22:05:45 -05001541array_sizeof(arrayobject *self, PyObject *unused)
1542{
1543 Py_ssize_t res;
Serhiy Storchakab117d792015-12-19 20:07:48 +02001544 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * self->ob_descr->itemsize;
Meador Inge4f5f3482012-08-10 22:05:45 -05001545 return PyLong_FromSsize_t(res);
1546}
1547
1548PyDoc_STRVAR(sizeof_doc,
1549"__sizeof__() -> int\n\
1550\n\
1551Size of the array in memory, in bytes.");
1552
1553static PyObject *
Martin v. Löwis669620f2002-03-01 10:27:01 +00001554array_get_typecode(arrayobject *a, void *closure)
1555{
Antoine Pitrou76748412010-05-09 14:46:46 +00001556 char tc = a->ob_descr->typecode;
1557 return PyString_FromStringAndSize(&tc, 1);
Martin v. Löwis669620f2002-03-01 10:27:01 +00001558}
1559
1560static PyObject *
1561array_get_itemsize(arrayobject *a, void *closure)
1562{
Antoine Pitrou76748412010-05-09 14:46:46 +00001563 return PyInt_FromLong((long)a->ob_descr->itemsize);
Martin v. Löwis669620f2002-03-01 10:27:01 +00001564}
1565
1566static PyGetSetDef array_getsets [] = {
Antoine Pitrou76748412010-05-09 14:46:46 +00001567 {"typecode", (getter) array_get_typecode, NULL,
1568 "the typecode character used to create the array"},
1569 {"itemsize", (getter) array_get_itemsize, NULL,
1570 "the size, in bytes, of one array item"},
1571 {NULL}
Martin v. Löwis669620f2002-03-01 10:27:01 +00001572};
1573
Martin v. Löwis289ff952008-06-13 07:47:47 +00001574static PyMethodDef array_methods[] = {
Antoine Pitrou76748412010-05-09 14:46:46 +00001575 {"append", (PyCFunction)array_append, METH_O,
1576 append_doc},
1577 {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS,
1578 buffer_info_doc},
1579 {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
1580 byteswap_doc},
1581 {"__copy__", (PyCFunction)array_copy, METH_NOARGS,
1582 copy_doc},
1583 {"count", (PyCFunction)array_count, METH_O,
1584 count_doc},
1585 {"__deepcopy__",(PyCFunction)array_copy, METH_O,
1586 copy_doc},
1587 {"extend", (PyCFunction)array_extend, METH_O,
1588 extend_doc},
1589 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
1590 fromfile_doc},
1591 {"fromlist", (PyCFunction)array_fromlist, METH_O,
1592 fromlist_doc},
1593 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
1594 fromstring_doc},
Martin v. Löwis669620f2002-03-01 10:27:01 +00001595#ifdef Py_USING_UNICODE
Antoine Pitrou76748412010-05-09 14:46:46 +00001596 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
1597 fromunicode_doc},
Martin v. Löwis669620f2002-03-01 10:27:01 +00001598#endif
Antoine Pitrou76748412010-05-09 14:46:46 +00001599 {"index", (PyCFunction)array_index, METH_O,
1600 index_doc},
1601 {"insert", (PyCFunction)array_insert, METH_VARARGS,
1602 insert_doc},
1603 {"pop", (PyCFunction)array_pop, METH_VARARGS,
1604 pop_doc},
1605 {"read", (PyCFunction)array_fromfile_as_read, METH_VARARGS,
1606 fromfile_doc},
1607 {"__reduce__", (PyCFunction)array_reduce, METH_NOARGS,
1608 reduce_doc},
1609 {"remove", (PyCFunction)array_remove, METH_O,
1610 remove_doc},
1611 {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
1612 reverse_doc},
1613/* {"sort", (PyCFunction)array_sort, METH_VARARGS,
1614 sort_doc},*/
1615 {"tofile", (PyCFunction)array_tofile, METH_O,
1616 tofile_doc},
1617 {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
1618 tolist_doc},
1619 {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
1620 tostring_doc},
Martin v. Löwis669620f2002-03-01 10:27:01 +00001621#ifdef Py_USING_UNICODE
Antoine Pitrou76748412010-05-09 14:46:46 +00001622 {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
1623 tounicode_doc},
Martin v. Löwis669620f2002-03-01 10:27:01 +00001624#endif
Antoine Pitrou76748412010-05-09 14:46:46 +00001625 {"write", (PyCFunction)array_tofile_as_write, METH_O,
1626 tofile_doc},
Meador Inge4f5f3482012-08-10 22:05:45 -05001627 {"__sizeof__", (PyCFunction)array_sizeof, METH_NOARGS,
1628 sizeof_doc},
Antoine Pitrou76748412010-05-09 14:46:46 +00001629 {NULL, NULL} /* sentinel */
Guido van Rossum63d101e1993-02-19 15:55:02 +00001630};
1631
Roger E. Masse395316d1996-12-09 20:10:36 +00001632static PyObject *
Peter Schneider-Kamp846f68d2000-07-13 21:10:57 +00001633array_repr(arrayobject *a)
Guido van Rossum63d101e1993-02-19 15:55:02 +00001634{
Antoine Pitrou76748412010-05-09 14:46:46 +00001635 char buf[256], typecode;
1636 PyObject *s, *t, *v = NULL;
1637 Py_ssize_t len;
Martin v. Löwis669620f2002-03-01 10:27:01 +00001638
Antoine Pitrou76748412010-05-09 14:46:46 +00001639 len = Py_SIZE(a);
1640 typecode = a->ob_descr->typecode;
1641 if (len == 0) {
1642 PyOS_snprintf(buf, sizeof(buf), "array('%c')", typecode);
1643 return PyString_FromString(buf);
1644 }
1645
1646 if (typecode == 'c')
1647 v = array_tostring(a, NULL);
Michael W. Hudson04d7b592002-05-13 10:14:59 +00001648#ifdef Py_USING_UNICODE
Antoine Pitrou76748412010-05-09 14:46:46 +00001649 else if (typecode == 'u')
1650 v = array_tounicode(a, NULL);
Michael W. Hudson04d7b592002-05-13 10:14:59 +00001651#endif
Antoine Pitrou76748412010-05-09 14:46:46 +00001652 else
1653 v = array_tolist(a, NULL);
1654 t = PyObject_Repr(v);
1655 Py_XDECREF(v);
Raymond Hettingerc6dd33e2003-04-23 17:27:00 +00001656
Antoine Pitrou76748412010-05-09 14:46:46 +00001657 PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode);
1658 s = PyString_FromString(buf);
1659 PyString_ConcatAndDel(&s, t);
1660 PyString_ConcatAndDel(&s, PyString_FromString(")"));
1661 return s;
Guido van Rossum63d101e1993-02-19 15:55:02 +00001662}
1663
Michael W. Hudsoncf68fe52002-06-19 15:44:15 +00001664static PyObject*
1665array_subscr(arrayobject* self, PyObject* item)
1666{
Antoine Pitrou76748412010-05-09 14:46:46 +00001667 if (PyIndex_Check(item)) {
1668 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
1669 if (i==-1 && PyErr_Occurred()) {
1670 return NULL;
1671 }
1672 if (i < 0)
1673 i += Py_SIZE(self);
1674 return array_item(self, i);
1675 }
1676 else if (PySlice_Check(item)) {
1677 Py_ssize_t start, stop, step, slicelength, cur, i;
1678 PyObject* result;
1679 arrayobject* ar;
1680 int itemsize = self->ob_descr->itemsize;
Michael W. Hudsoncf68fe52002-06-19 15:44:15 +00001681
Antoine Pitrou76748412010-05-09 14:46:46 +00001682 if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self),
1683 &start, &stop, &step, &slicelength) < 0) {
1684 return NULL;
1685 }
Michael W. Hudsoncf68fe52002-06-19 15:44:15 +00001686
Antoine Pitrou76748412010-05-09 14:46:46 +00001687 if (slicelength <= 0) {
1688 return newarrayobject(&Arraytype, 0, self->ob_descr);
1689 }
1690 else if (step == 1) {
1691 PyObject *result = newarrayobject(&Arraytype,
1692 slicelength, self->ob_descr);
1693 if (result == NULL)
1694 return NULL;
1695 memcpy(((arrayobject *)result)->ob_item,
1696 self->ob_item + start * itemsize,
1697 slicelength * itemsize);
1698 return result;
1699 }
1700 else {
1701 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
1702 if (!result) return NULL;
Michael W. Hudsoncf68fe52002-06-19 15:44:15 +00001703
Antoine Pitrou76748412010-05-09 14:46:46 +00001704 ar = (arrayobject*)result;
Michael W. Hudsoncf68fe52002-06-19 15:44:15 +00001705
Antoine Pitrou76748412010-05-09 14:46:46 +00001706 for (cur = start, i = 0; i < slicelength;
1707 cur += step, i++) {
1708 memcpy(ar->ob_item + i*itemsize,
1709 self->ob_item + cur*itemsize,
1710 itemsize);
1711 }
1712
1713 return result;
1714 }
1715 }
1716 else {
1717 PyErr_SetString(PyExc_TypeError,
1718 "array indices must be integers");
1719 return NULL;
1720 }
Michael W. Hudsoncf68fe52002-06-19 15:44:15 +00001721}
1722
1723static int
1724array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
1725{
Antoine Pitrou76748412010-05-09 14:46:46 +00001726 Py_ssize_t start, stop, step, slicelength, needed;
1727 arrayobject* other;
1728 int itemsize;
Thomas Woutersf6424442007-08-28 15:28:19 +00001729
Antoine Pitrou76748412010-05-09 14:46:46 +00001730 if (PyIndex_Check(item)) {
1731 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Mark Dickinson3caf1e02010-01-29 17:11:39 +00001732
Antoine Pitrou76748412010-05-09 14:46:46 +00001733 if (i == -1 && PyErr_Occurred())
1734 return -1;
1735 if (i < 0)
1736 i += Py_SIZE(self);
1737 if (i < 0 || i >= Py_SIZE(self)) {
1738 PyErr_SetString(PyExc_IndexError,
1739 "array assignment index out of range");
1740 return -1;
1741 }
1742 if (value == NULL) {
1743 /* Fall through to slice assignment */
1744 start = i;
1745 stop = i + 1;
1746 step = 1;
1747 slicelength = 1;
1748 }
1749 else
1750 return (*self->ob_descr->setitem)(self, i, value);
1751 }
1752 else if (PySlice_Check(item)) {
1753 if (PySlice_GetIndicesEx((PySliceObject *)item,
1754 Py_SIZE(self), &start, &stop,
1755 &step, &slicelength) < 0) {
1756 return -1;
1757 }
1758 }
1759 else {
1760 PyErr_SetString(PyExc_TypeError,
1761 "array indices must be integer");
1762 return -1;
1763 }
1764 if (value == NULL) {
1765 other = NULL;
1766 needed = 0;
1767 }
1768 else if (array_Check(value)) {
1769 other = (arrayobject *)value;
1770 needed = Py_SIZE(other);
1771 if (self == other) {
1772 /* Special case "self[i:j] = self" -- copy self first */
1773 int ret;
1774 value = array_slice(other, 0, needed);
1775 if (value == NULL)
1776 return -1;
1777 ret = array_ass_subscr(self, item, value);
1778 Py_DECREF(value);
1779 return ret;
1780 }
1781 if (other->ob_descr != self->ob_descr) {
1782 PyErr_BadArgument();
1783 return -1;
1784 }
1785 }
1786 else {
1787 PyErr_Format(PyExc_TypeError,
1788 "can only assign array (not \"%.200s\") to array slice",
1789 Py_TYPE(value)->tp_name);
1790 return -1;
1791 }
1792 itemsize = self->ob_descr->itemsize;
1793 /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
1794 if ((step > 0 && stop < start) ||
1795 (step < 0 && stop > start))
1796 stop = start;
1797 if (step == 1) {
1798 if (slicelength > needed) {
1799 memmove(self->ob_item + (start + needed) * itemsize,
1800 self->ob_item + stop * itemsize,
1801 (Py_SIZE(self) - stop) * itemsize);
1802 if (array_resize(self, Py_SIZE(self) +
1803 needed - slicelength) < 0)
1804 return -1;
1805 }
1806 else if (slicelength < needed) {
1807 if (array_resize(self, Py_SIZE(self) +
1808 needed - slicelength) < 0)
1809 return -1;
1810 memmove(self->ob_item + (start + needed) * itemsize,
1811 self->ob_item + stop * itemsize,
1812 (Py_SIZE(self) - start - needed) * itemsize);
1813 }
1814 if (needed > 0)
1815 memcpy(self->ob_item + start * itemsize,
1816 other->ob_item, needed * itemsize);
1817 return 0;
1818 }
1819 else if (needed == 0) {
1820 /* Delete slice */
1821 size_t cur;
1822 Py_ssize_t i;
Thomas Woutersf6424442007-08-28 15:28:19 +00001823
Antoine Pitrou76748412010-05-09 14:46:46 +00001824 if (step < 0) {
1825 stop = start + 1;
1826 start = stop + step * (slicelength - 1) - 1;
1827 step = -step;
1828 }
1829 for (cur = start, i = 0; i < slicelength;
1830 cur += step, i++) {
1831 Py_ssize_t lim = step - 1;
Thomas Woutersf6424442007-08-28 15:28:19 +00001832
Antoine Pitrou76748412010-05-09 14:46:46 +00001833 if (cur + step >= (size_t)Py_SIZE(self))
1834 lim = Py_SIZE(self) - cur - 1;
1835 memmove(self->ob_item + (cur - i) * itemsize,
1836 self->ob_item + (cur + 1) * itemsize,
1837 lim * itemsize);
1838 }
1839 cur = start + slicelength * step;
1840 if (cur < (size_t)Py_SIZE(self)) {
1841 memmove(self->ob_item + (cur-slicelength) * itemsize,
1842 self->ob_item + cur * itemsize,
1843 (Py_SIZE(self) - cur) * itemsize);
1844 }
1845 if (array_resize(self, Py_SIZE(self) - slicelength) < 0)
1846 return -1;
1847 return 0;
1848 }
1849 else {
1850 Py_ssize_t cur, i;
1851
1852 if (needed != slicelength) {
1853 PyErr_Format(PyExc_ValueError,
1854 "attempt to assign array of size %zd "
1855 "to extended slice of size %zd",
1856 needed, slicelength);
1857 return -1;
1858 }
1859 for (cur = start, i = 0; i < slicelength;
1860 cur += step, i++) {
1861 memcpy(self->ob_item + cur * itemsize,
1862 other->ob_item + i * itemsize,
1863 itemsize);
1864 }
1865 return 0;
1866 }
Michael W. Hudsoncf68fe52002-06-19 15:44:15 +00001867}
1868
1869static PyMappingMethods array_as_mapping = {
Antoine Pitrou76748412010-05-09 14:46:46 +00001870 (lenfunc)array_length,
1871 (binaryfunc)array_subscr,
1872 (objobjargproc)array_ass_subscr
Michael W. Hudsoncf68fe52002-06-19 15:44:15 +00001873};
1874
Raymond Hettinger17d76212007-04-02 22:54:21 +00001875static const void *emptybuf = "";
1876
Martin v. Löwis26a63482006-02-15 17:27:45 +00001877static Py_ssize_t
1878array_buffer_getreadbuf(arrayobject *self, Py_ssize_t index, const void **ptr)
Guido van Rossum638911b1997-05-05 22:15:02 +00001879{
Antoine Pitrou76748412010-05-09 14:46:46 +00001880 if ( index != 0 ) {
1881 PyErr_SetString(PyExc_SystemError,
1882 "Accessing non-existent array segment");
1883 return -1;
1884 }
1885 *ptr = (void *)self->ob_item;
1886 if (*ptr == NULL)
1887 *ptr = emptybuf;
1888 return Py_SIZE(self)*self->ob_descr->itemsize;
Guido van Rossum638911b1997-05-05 22:15:02 +00001889}
1890
Martin v. Löwis26a63482006-02-15 17:27:45 +00001891static Py_ssize_t
1892array_buffer_getwritebuf(arrayobject *self, Py_ssize_t index, const void **ptr)
Guido van Rossum638911b1997-05-05 22:15:02 +00001893{
Antoine Pitrou76748412010-05-09 14:46:46 +00001894 if ( index != 0 ) {
1895 PyErr_SetString(PyExc_SystemError,
1896 "Accessing non-existent array segment");
1897 return -1;
1898 }
1899 *ptr = (void *)self->ob_item;
1900 if (*ptr == NULL)
1901 *ptr = emptybuf;
1902 return Py_SIZE(self)*self->ob_descr->itemsize;
Guido van Rossum638911b1997-05-05 22:15:02 +00001903}
1904
Martin v. Löwis26a63482006-02-15 17:27:45 +00001905static Py_ssize_t
1906array_buffer_getsegcount(arrayobject *self, Py_ssize_t *lenp)
Guido van Rossum638911b1997-05-05 22:15:02 +00001907{
Antoine Pitrou76748412010-05-09 14:46:46 +00001908 if ( lenp )
1909 *lenp = Py_SIZE(self)*self->ob_descr->itemsize;
1910 return 1;
Guido van Rossum638911b1997-05-05 22:15:02 +00001911}
1912
Roger E. Masse395316d1996-12-09 20:10:36 +00001913static PySequenceMethods array_as_sequence = {
Antoine Pitrou76748412010-05-09 14:46:46 +00001914 (lenfunc)array_length, /*sq_length*/
1915 (binaryfunc)array_concat, /*sq_concat*/
1916 (ssizeargfunc)array_repeat, /*sq_repeat*/
1917 (ssizeargfunc)array_item, /*sq_item*/
1918 (ssizessizeargfunc)array_slice, /*sq_slice*/
1919 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
1920 (ssizessizeobjargproc)array_ass_slice, /*sq_ass_slice*/
1921 (objobjproc)array_contains, /*sq_contains*/
1922 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
1923 (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/
Guido van Rossum63d101e1993-02-19 15:55:02 +00001924};
1925
Guido van Rossum638911b1997-05-05 22:15:02 +00001926static PyBufferProcs array_as_buffer = {
Antoine Pitrou76748412010-05-09 14:46:46 +00001927 (readbufferproc)array_buffer_getreadbuf,
1928 (writebufferproc)array_buffer_getwritebuf,
1929 (segcountproc)array_buffer_getsegcount,
1930 NULL,
Guido van Rossum638911b1997-05-05 22:15:02 +00001931};
1932
Roger E. Masse395316d1996-12-09 20:10:36 +00001933static PyObject *
Martin v. Löwis669620f2002-03-01 10:27:01 +00001934array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum63d101e1993-02-19 15:55:02 +00001935{
Serhiy Storchaka97773132015-05-16 17:11:41 +03001936 int c = -1;
1937 PyObject *initial = NULL, *it = NULL, *typecode = NULL;
Antoine Pitrou76748412010-05-09 14:46:46 +00001938 struct arraydescr *descr;
Martin v. Löwis669620f2002-03-01 10:27:01 +00001939
Antoine Pitrou76748412010-05-09 14:46:46 +00001940 if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds))
1941 return NULL;
Martin v. Löwis669620f2002-03-01 10:27:01 +00001942
Serhiy Storchaka97773132015-05-16 17:11:41 +03001943 if (!PyArg_ParseTuple(args, "O|O:array", &typecode, &initial))
Antoine Pitrou76748412010-05-09 14:46:46 +00001944 return NULL;
Raymond Hettinger71dc7b22003-04-24 10:41:55 +00001945
Serhiy Storchaka97773132015-05-16 17:11:41 +03001946 if (PyString_Check(typecode) && PyString_GET_SIZE(typecode) == 1)
1947 c = (unsigned char)*PyString_AS_STRING(typecode);
Serhiy Storchakaf6d1b512015-05-31 11:56:48 +03001948#ifdef Py_USING_UNICODE
Serhiy Storchaka97773132015-05-16 17:11:41 +03001949 else if (PyUnicode_Check(typecode) && PyUnicode_GET_SIZE(typecode) == 1)
1950 c = *PyUnicode_AS_UNICODE(typecode);
Serhiy Storchakaf6d1b512015-05-31 11:56:48 +03001951#endif
Serhiy Storchaka97773132015-05-16 17:11:41 +03001952 else {
1953 PyErr_Format(PyExc_TypeError,
1954 "array() argument 1 or typecode must be char (string or "
1955 "ascii-unicode with length 1), not %s",
1956 Py_TYPE(typecode)->tp_name);
1957 return NULL;
1958 }
1959
Antoine Pitrou76748412010-05-09 14:46:46 +00001960 if (!(initial == NULL || PyList_Check(initial)
1961 || PyString_Check(initial) || PyTuple_Check(initial)
Alexander Belopolskyc99f5cf2011-01-11 22:35:58 +00001962 || (c == 'u' && PyUnicode_Check(initial)))) {
Antoine Pitrou76748412010-05-09 14:46:46 +00001963 it = PyObject_GetIter(initial);
1964 if (it == NULL)
1965 return NULL;
1966 /* We set initial to NULL so that the subsequent code
1967 will create an empty array of the appropriate type
1968 and afterwards we can use array_iter_extend to populate
1969 the array.
1970 */
1971 initial = NULL;
1972 }
1973 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1974 if (descr->typecode == c) {
1975 PyObject *a;
1976 Py_ssize_t len;
Martin v. Löwis669620f2002-03-01 10:27:01 +00001977
Alexander Belopolskyc99f5cf2011-01-11 22:35:58 +00001978 if (initial == NULL || !(PyList_Check(initial)
1979 || PyTuple_Check(initial)))
Antoine Pitrou76748412010-05-09 14:46:46 +00001980 len = 0;
1981 else
Alexander Belopolskyc99f5cf2011-01-11 22:35:58 +00001982 len = PySequence_Size(initial);
Martin v. Löwis669620f2002-03-01 10:27:01 +00001983
Antoine Pitrou76748412010-05-09 14:46:46 +00001984 a = newarrayobject(type, len, descr);
1985 if (a == NULL)
1986 return NULL;
1987
Alexander Belopolskyc99f5cf2011-01-11 22:35:58 +00001988 if (len > 0) {
Antoine Pitrou76748412010-05-09 14:46:46 +00001989 Py_ssize_t i;
1990 for (i = 0; i < len; i++) {
1991 PyObject *v =
1992 PySequence_GetItem(initial, i);
1993 if (v == NULL) {
1994 Py_DECREF(a);
1995 return NULL;
1996 }
1997 if (setarrayitem(a, i, v) != 0) {
1998 Py_DECREF(v);
1999 Py_DECREF(a);
2000 return NULL;
2001 }
2002 Py_DECREF(v);
2003 }
2004 } else if (initial != NULL && PyString_Check(initial)) {
2005 PyObject *t_initial, *v;
2006 t_initial = PyTuple_Pack(1, initial);
2007 if (t_initial == NULL) {
2008 Py_DECREF(a);
2009 return NULL;
2010 }
2011 v = array_fromstring((arrayobject *)a,
2012 t_initial);
2013 Py_DECREF(t_initial);
2014 if (v == NULL) {
2015 Py_DECREF(a);
2016 return NULL;
2017 }
2018 Py_DECREF(v);
Martin v. Löwis669620f2002-03-01 10:27:01 +00002019#ifdef Py_USING_UNICODE
Antoine Pitrou76748412010-05-09 14:46:46 +00002020 } else if (initial != NULL && PyUnicode_Check(initial)) {
2021 Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial);
2022 if (n > 0) {
2023 arrayobject *self = (arrayobject *)a;
2024 char *item = self->ob_item;
2025 item = (char *)PyMem_Realloc(item, n);
2026 if (item == NULL) {
2027 PyErr_NoMemory();
2028 Py_DECREF(a);
2029 return NULL;
2030 }
2031 self->ob_item = item;
2032 Py_SIZE(self) = n / sizeof(Py_UNICODE);
2033 memcpy(item, PyUnicode_AS_DATA(initial), n);
2034 self->allocated = Py_SIZE(self);
2035 }
Martin v. Löwis669620f2002-03-01 10:27:01 +00002036#endif
Antoine Pitrou76748412010-05-09 14:46:46 +00002037 }
2038 if (it != NULL) {
2039 if (array_iter_extend((arrayobject *)a, it) == -1) {
2040 Py_DECREF(it);
2041 Py_DECREF(a);
2042 return NULL;
2043 }
2044 Py_DECREF(it);
2045 }
2046 return a;
2047 }
2048 }
2049 PyErr_SetString(PyExc_ValueError,
2050 "bad typecode (must be c, b, B, u, h, H, i, I, l, L, f or d)");
2051 return NULL;
Guido van Rossum63d101e1993-02-19 15:55:02 +00002052}
2053
Guido van Rossum63d101e1993-02-19 15:55:02 +00002054
Martin v. Löwis1117ad52002-06-13 20:33:02 +00002055PyDoc_STRVAR(module_doc,
Martin v. Löwis669620f2002-03-01 10:27:01 +00002056"This module defines an object type which can efficiently represent\n\
Guido van Rossum7627fef1998-10-13 14:27:22 +00002057an array of basic values: characters, integers, floating point\n\
2058numbers. Arrays are sequence types and behave very much like lists,\n\
2059except that the type of objects stored in them is constrained. The\n\
2060type is specified at object creation time by using a type code, which\n\
2061is a single character. The following type codes are defined:\n\
2062\n\
2063 Type code C Type Minimum size in bytes \n\
2064 'c' character 1 \n\
2065 'b' signed integer 1 \n\
2066 'B' unsigned integer 1 \n\
Martin v. Löwis669620f2002-03-01 10:27:01 +00002067 'u' Unicode character 2 \n\
Guido van Rossum7627fef1998-10-13 14:27:22 +00002068 'h' signed integer 2 \n\
2069 'H' unsigned integer 2 \n\
2070 'i' signed integer 2 \n\
2071 'I' unsigned integer 2 \n\
2072 'l' signed integer 4 \n\
2073 'L' unsigned integer 4 \n\
2074 'f' floating point 4 \n\
2075 'd' floating point 8 \n\
2076\n\
Martin v. Löwis669620f2002-03-01 10:27:01 +00002077The constructor is:\n\
Guido van Rossum7627fef1998-10-13 14:27:22 +00002078\n\
2079array(typecode [, initializer]) -- create a new array\n\
Martin v. Löwis1117ad52002-06-13 20:33:02 +00002080");
Guido van Rossum7627fef1998-10-13 14:27:22 +00002081
Martin v. Löwis1117ad52002-06-13 20:33:02 +00002082PyDoc_STRVAR(arraytype_doc,
Martin v. Löwis669620f2002-03-01 10:27:01 +00002083"array(typecode [, initializer]) -> array\n\
2084\n\
2085Return a new array whose items are restricted by typecode, and\n\
Raymond Hettinger6a595062004-08-29 07:50:43 +00002086initialized from the optional initializer value, which must be a list,\n\
Florent Xiclunaf021f5c2011-12-09 23:40:27 +01002087string or iterable over elements of the appropriate type.\n\
Martin v. Löwis669620f2002-03-01 10:27:01 +00002088\n\
2089Arrays represent basic values and behave very much like lists, except\n\
Guido van Rossum7627fef1998-10-13 14:27:22 +00002090the type of objects stored in them is constrained.\n\
2091\n\
2092Methods:\n\
2093\n\
2094append() -- append a new item to the end of the array\n\
2095buffer_info() -- return information giving the current memory info\n\
2096byteswap() -- byteswap all the items of the array\n\
Mark Dickinsonaef7f682009-02-21 20:27:01 +00002097count() -- return number of occurrences of an object\n\
Raymond Hettingere5f628b2004-03-14 05:43:59 +00002098extend() -- extend array by appending multiple elements from an iterable\n\
Guido van Rossum7627fef1998-10-13 14:27:22 +00002099fromfile() -- read items from a file object\n\
2100fromlist() -- append items from the list\n\
2101fromstring() -- append items from the string\n\
Mark Dickinsonaef7f682009-02-21 20:27:01 +00002102index() -- return index of first occurrence of an object\n\
Guido van Rossum7627fef1998-10-13 14:27:22 +00002103insert() -- insert a new item into the array at a provided position\n\
Peter Schneider-Kamp85f64432000-07-31 20:52:21 +00002104pop() -- remove and return item (default last)\n\
Guido van Rossum7627fef1998-10-13 14:27:22 +00002105read() -- DEPRECATED, use fromfile()\n\
Mark Dickinsonaef7f682009-02-21 20:27:01 +00002106remove() -- remove first occurrence of an object\n\
Guido van Rossum7627fef1998-10-13 14:27:22 +00002107reverse() -- reverse the order of the items in the array\n\
2108tofile() -- write all items to a file object\n\
2109tolist() -- return the array converted to an ordinary list\n\
2110tostring() -- return the array converted to a string\n\
2111write() -- DEPRECATED, use tofile()\n\
2112\n\
Martin v. Löwis669620f2002-03-01 10:27:01 +00002113Attributes:\n\
Guido van Rossum7627fef1998-10-13 14:27:22 +00002114\n\
2115typecode -- the typecode character used to create the array\n\
2116itemsize -- the length in bytes of one array item\n\
Martin v. Löwis1117ad52002-06-13 20:33:02 +00002117");
Guido van Rossum7627fef1998-10-13 14:27:22 +00002118
Raymond Hettinger409b7522003-01-07 01:58:52 +00002119static PyObject *array_iter(arrayobject *ao);
2120
Tim Peters440f2472002-07-17 16:49:03 +00002121static PyTypeObject Arraytype = {
Antoine Pitrou76748412010-05-09 14:46:46 +00002122 PyVarObject_HEAD_INIT(NULL, 0)
2123 "array.array",
2124 sizeof(arrayobject),
2125 0,
2126 (destructor)array_dealloc, /* tp_dealloc */
2127 0, /* tp_print */
2128 0, /* tp_getattr */
2129 0, /* tp_setattr */
2130 0, /* tp_compare */
2131 (reprfunc)array_repr, /* tp_repr */
2132 0, /* tp_as_number*/
2133 &array_as_sequence, /* tp_as_sequence*/
2134 &array_as_mapping, /* tp_as_mapping*/
2135 0, /* tp_hash */
2136 0, /* tp_call */
2137 0, /* tp_str */
2138 PyObject_GenericGetAttr, /* tp_getattro */
2139 0, /* tp_setattro */
2140 &array_as_buffer, /* tp_as_buffer*/
2141 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
2142 arraytype_doc, /* tp_doc */
2143 0, /* tp_traverse */
2144 0, /* tp_clear */
2145 array_richcompare, /* tp_richcompare */
2146 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
2147 (getiterfunc)array_iter, /* tp_iter */
2148 0, /* tp_iternext */
2149 array_methods, /* tp_methods */
2150 0, /* tp_members */
2151 array_getsets, /* tp_getset */
2152 0, /* tp_base */
2153 0, /* tp_dict */
2154 0, /* tp_descr_get */
2155 0, /* tp_descr_set */
2156 0, /* tp_dictoffset */
2157 0, /* tp_init */
2158 PyType_GenericAlloc, /* tp_alloc */
2159 array_new, /* tp_new */
2160 PyObject_Del, /* tp_free */
Guido van Rossum7627fef1998-10-13 14:27:22 +00002161};
2162
Raymond Hettinger409b7522003-01-07 01:58:52 +00002163
2164/*********************** Array Iterator **************************/
2165
2166typedef struct {
Antoine Pitrou76748412010-05-09 14:46:46 +00002167 PyObject_HEAD
2168 Py_ssize_t index;
2169 arrayobject *ao;
2170 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
Raymond Hettinger409b7522003-01-07 01:58:52 +00002171} arrayiterobject;
2172
2173static PyTypeObject PyArrayIter_Type;
2174
2175#define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
2176
2177static PyObject *
2178array_iter(arrayobject *ao)
2179{
Antoine Pitrou76748412010-05-09 14:46:46 +00002180 arrayiterobject *it;
Raymond Hettinger409b7522003-01-07 01:58:52 +00002181
Antoine Pitrou76748412010-05-09 14:46:46 +00002182 if (!array_Check(ao)) {
2183 PyErr_BadInternalCall();
2184 return NULL;
2185 }
Raymond Hettinger409b7522003-01-07 01:58:52 +00002186
Antoine Pitrou76748412010-05-09 14:46:46 +00002187 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
2188 if (it == NULL)
2189 return NULL;
Raymond Hettinger409b7522003-01-07 01:58:52 +00002190
Antoine Pitrou76748412010-05-09 14:46:46 +00002191 Py_INCREF(ao);
2192 it->ao = ao;
2193 it->index = 0;
2194 it->getitem = ao->ob_descr->getitem;
2195 PyObject_GC_Track(it);
2196 return (PyObject *)it;
Raymond Hettinger409b7522003-01-07 01:58:52 +00002197}
2198
2199static PyObject *
Raymond Hettinger409b7522003-01-07 01:58:52 +00002200arrayiter_next(arrayiterobject *it)
2201{
Antoine Pitrou76748412010-05-09 14:46:46 +00002202 assert(PyArrayIter_Check(it));
2203 if (it->index < Py_SIZE(it->ao))
2204 return (*it->getitem)(it->ao, it->index++);
2205 return NULL;
Raymond Hettinger409b7522003-01-07 01:58:52 +00002206}
2207
2208static void
2209arrayiter_dealloc(arrayiterobject *it)
2210{
Antoine Pitrou76748412010-05-09 14:46:46 +00002211 PyObject_GC_UnTrack(it);
2212 Py_XDECREF(it->ao);
2213 PyObject_GC_Del(it);
Raymond Hettinger409b7522003-01-07 01:58:52 +00002214}
2215
2216static int
2217arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2218{
Antoine Pitrou76748412010-05-09 14:46:46 +00002219 Py_VISIT(it->ao);
2220 return 0;
Raymond Hettinger409b7522003-01-07 01:58:52 +00002221}
2222
2223static PyTypeObject PyArrayIter_Type = {
Antoine Pitrou76748412010-05-09 14:46:46 +00002224 PyVarObject_HEAD_INIT(NULL, 0)
2225 "arrayiterator", /* tp_name */
2226 sizeof(arrayiterobject), /* tp_basicsize */
2227 0, /* tp_itemsize */
2228 /* methods */
2229 (destructor)arrayiter_dealloc, /* tp_dealloc */
2230 0, /* tp_print */
2231 0, /* tp_getattr */
2232 0, /* tp_setattr */
2233 0, /* tp_compare */
2234 0, /* tp_repr */
2235 0, /* tp_as_number */
2236 0, /* tp_as_sequence */
2237 0, /* tp_as_mapping */
2238 0, /* tp_hash */
2239 0, /* tp_call */
2240 0, /* tp_str */
2241 PyObject_GenericGetAttr, /* tp_getattro */
2242 0, /* tp_setattro */
2243 0, /* tp_as_buffer */
2244 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2245 0, /* tp_doc */
2246 (traverseproc)arrayiter_traverse, /* tp_traverse */
2247 0, /* tp_clear */
2248 0, /* tp_richcompare */
2249 0, /* tp_weaklistoffset */
2250 PyObject_SelfIter, /* tp_iter */
2251 (iternextfunc)arrayiter_next, /* tp_iternext */
2252 0, /* tp_methods */
Raymond Hettinger409b7522003-01-07 01:58:52 +00002253};
2254
2255
2256/*********************** Install Module **************************/
2257
Martin v. Löwis669620f2002-03-01 10:27:01 +00002258/* No functions in array module. */
2259static PyMethodDef a_methods[] = {
2260 {NULL, NULL, 0, NULL} /* Sentinel */
2261};
2262
2263
Mark Hammond9243e352002-08-02 02:27:13 +00002264PyMODINIT_FUNC
Thomas Wouters4547bea2000-07-21 06:00:07 +00002265initarray(void)
Guido van Rossum63d101e1993-02-19 15:55:02 +00002266{
Antoine Pitrou76748412010-05-09 14:46:46 +00002267 PyObject *m;
Fred Drakee1999f72000-02-04 20:33:49 +00002268
Antoine Pitrou76748412010-05-09 14:46:46 +00002269 Arraytype.ob_type = &PyType_Type;
2270 PyArrayIter_Type.ob_type = &PyType_Type;
2271 m = Py_InitModule3("array", a_methods, module_doc);
2272 if (m == NULL)
2273 return;
Fred Draked85c4042002-04-01 03:45:06 +00002274
Antoine Pitrou76748412010-05-09 14:46:46 +00002275 Py_INCREF((PyObject *)&Arraytype);
2276 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
2277 Py_INCREF((PyObject *)&Arraytype);
2278 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
2279 /* No need to check the error here, the caller will do that */
Guido van Rossum63d101e1993-02-19 15:55:02 +00002280}