blob: 59f20a5da81a2c8e2f8af83d60428d785c7a3158 [file] [log] [blame]
Guido van Rossum778983b1993-02-19 15:55:02 +00001/* Array object implementation */
2
3/* An array is a uniform list -- all items have the same type.
4 The item type is restricted to simple C types like int or float */
5
Martin v. Löwis18e16552006-02-15 17:27:45 +00006#define PY_SSIZE_T_CLEAN
Roger E. Masse2919eaa1996-12-09 20:10:36 +00007#include "Python.h"
Raymond Hettingercb87bc82004-05-31 00:35:52 +00008#include "structmember.h"
Roger E. Masse5817f8f1996-12-09 22:24:19 +00009
Guido van Rossum0c709541994-08-19 12:01:32 +000010#ifdef STDC_HEADERS
11#include <stddef.h>
Guido van Rossum7f1de831999-08-27 20:33:52 +000012#else /* !STDC_HEADERS */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000013#ifdef HAVE_SYS_TYPES_H
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000014#include <sys/types.h> /* For size_t */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000015#endif /* HAVE_SYS_TYPES_H */
Guido van Rossum7f1de831999-08-27 20:33:52 +000016#endif /* !STDC_HEADERS */
Guido van Rossum778983b1993-02-19 15:55:02 +000017
18struct arrayobject; /* Forward */
19
Tim Petersbb307342000-09-10 05:22:54 +000020/* All possible arraydescr values are defined in the vector "descriptors"
21 * below. That's defined later because the appropriate get and set
22 * functions aren't visible yet.
23 */
Guido van Rossum778983b1993-02-19 15:55:02 +000024struct arraydescr {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000025 int typecode;
26 int itemsize;
27 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
28 int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *);
29 char *formats;
Guido van Rossum778983b1993-02-19 15:55:02 +000030};
31
32typedef struct arrayobject {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000033 PyObject_VAR_HEAD
34 char *ob_item;
35 Py_ssize_t allocated;
36 struct arraydescr *ob_descr;
37 PyObject *weakreflist; /* List of weak references */
38 int ob_exports; /* Number of exported buffers */
Guido van Rossum778983b1993-02-19 15:55:02 +000039} arrayobject;
40
Jeremy Hylton938ace62002-07-17 16:30:39 +000041static PyTypeObject Arraytype;
Guido van Rossum778983b1993-02-19 15:55:02 +000042
Martin v. Löwis99866332002-03-01 10:27:01 +000043#define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
Christian Heimes90aa7642007-12-19 02:45:37 +000044#define array_CheckExact(op) (Py_TYPE(op) == &Arraytype)
Guido van Rossum778983b1993-02-19 15:55:02 +000045
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000046static int
Martin v. Löwis18e16552006-02-15 17:27:45 +000047array_resize(arrayobject *self, Py_ssize_t newsize)
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000048{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000049 char *items;
50 size_t _new_size;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000051
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000052 if (self->ob_exports > 0 && newsize != Py_SIZE(self)) {
53 PyErr_SetString(PyExc_BufferError,
54 "cannot resize an array that is exporting buffers");
55 return -1;
56 }
Antoine Pitrou3ad3a0d2008-12-18 17:08:32 +000057
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000058 /* Bypass realloc() when a previous overallocation is large enough
59 to accommodate the newsize. If the newsize is 16 smaller than the
60 current size, then proceed with the realloc() to shrink the array.
61 */
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000062
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000063 if (self->allocated >= newsize &&
64 Py_SIZE(self) < newsize + 16 &&
65 self->ob_item != NULL) {
66 Py_SIZE(self) = newsize;
67 return 0;
68 }
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000069
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000070 if (newsize == 0) {
71 PyMem_FREE(self->ob_item);
72 self->ob_item = NULL;
73 Py_SIZE(self) = 0;
74 self->allocated = 0;
75 return 0;
76 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +000077
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000078 /* This over-allocates proportional to the array size, making room
79 * for additional growth. The over-allocation is mild, but is
80 * enough to give linear-time amortized behavior over a long
81 * sequence of appends() in the presence of a poorly-performing
82 * system realloc().
83 * The growth pattern is: 0, 4, 8, 16, 25, 34, 46, 56, 67, 79, ...
84 * Note, the pattern starts out the same as for lists but then
85 * grows at a smaller rate so that larger arrays only overallocate
86 * by about 1/16th -- this is done because arrays are presumed to be more
87 * memory critical.
88 */
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000089
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000090 _new_size = (newsize >> 4) + (Py_SIZE(self) < 8 ? 3 : 7) + newsize;
91 items = self->ob_item;
92 /* XXX The following multiplication and division does not optimize away
93 like it does for lists since the size is not known at compile time */
94 if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize))
95 PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize));
96 else
97 items = NULL;
98 if (items == NULL) {
99 PyErr_NoMemory();
100 return -1;
101 }
102 self->ob_item = items;
103 Py_SIZE(self) = newsize;
104 self->allocated = _new_size;
105 return 0;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000106}
107
Tim Petersbb307342000-09-10 05:22:54 +0000108/****************************************************************************
109Get and Set functions for each type.
110A Get function takes an arrayobject* and an integer index, returning the
111array value at that index wrapped in an appropriate PyObject*.
112A Set function takes an arrayobject, integer index, and PyObject*; sets
113the array value at that index to the raw C data extracted from the PyObject*,
114and returns 0 if successful, else nonzero on failure (PyObject* not of an
115appropriate type or value).
116Note that the basic Get and Set functions do NOT check that the index is
117in bounds; that's the responsibility of the caller.
118****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +0000119
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000120static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000121b_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000122{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000123 long x = ((char *)ap->ob_item)[i];
124 if (x >= 128)
125 x -= 256;
126 return PyLong_FromLong(x);
Guido van Rossum778983b1993-02-19 15:55:02 +0000127}
128
129static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000130b_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000131{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000132 short x;
133 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
134 must use the next size up that is signed ('h') and manually do
135 the overflow checking */
136 if (!PyArg_Parse(v, "h;array item must be integer", &x))
137 return -1;
138 else if (x < -128) {
139 PyErr_SetString(PyExc_OverflowError,
140 "signed char is less than minimum");
141 return -1;
142 }
143 else if (x > 127) {
144 PyErr_SetString(PyExc_OverflowError,
145 "signed char is greater than maximum");
146 return -1;
147 }
148 if (i >= 0)
149 ((char *)ap->ob_item)[i] = (char)x;
150 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000151}
152
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000153static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000154BB_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000155{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000156 long x = ((unsigned char *)ap->ob_item)[i];
157 return PyLong_FromLong(x);
Guido van Rossum549ab711997-01-03 19:09:47 +0000158}
159
Fred Drake541dc3b2000-06-28 17:49:30 +0000160static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000161BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000162{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000163 unsigned char x;
164 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
165 if (!PyArg_Parse(v, "b;array item must be integer", &x))
166 return -1;
167 if (i >= 0)
168 ((char *)ap->ob_item)[i] = x;
169 return 0;
Fred Drake541dc3b2000-06-28 17:49:30 +0000170}
Guido van Rossum549ab711997-01-03 19:09:47 +0000171
Martin v. Löwis99866332002-03-01 10:27:01 +0000172static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000173u_getitem(arrayobject *ap, Py_ssize_t i)
Martin v. Löwis99866332002-03-01 10:27:01 +0000174{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000175 return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1);
Martin v. Löwis99866332002-03-01 10:27:01 +0000176}
177
178static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000179u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Martin v. Löwis99866332002-03-01 10:27:01 +0000180{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000181 Py_UNICODE *p;
182 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +0000183
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000184 if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
185 return -1;
186 if (len != 1) {
187 PyErr_SetString(PyExc_TypeError,
188 "array item must be unicode character");
189 return -1;
190 }
191 if (i >= 0)
192 ((Py_UNICODE *)ap->ob_item)[i] = p[0];
193 return 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000194}
Martin v. Löwis99866332002-03-01 10:27:01 +0000195
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +0000196
Guido van Rossum549ab711997-01-03 19:09:47 +0000197static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000198h_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000199{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000200 return PyLong_FromLong((long) ((short *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000201}
202
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +0000203
Guido van Rossum778983b1993-02-19 15:55:02 +0000204static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000205h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000206{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000207 short x;
208 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
209 if (!PyArg_Parse(v, "h;array item must be integer", &x))
210 return -1;
211 if (i >= 0)
212 ((short *)ap->ob_item)[i] = x;
213 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000214}
215
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000216static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000217HH_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000218{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000219 return PyLong_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000220}
221
Fred Drake541dc3b2000-06-28 17:49:30 +0000222static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000223HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000224{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000225 int x;
226 /* PyArg_Parse's 'h' formatter is for a signed short, therefore
227 must use the next size up and manually do the overflow checking */
228 if (!PyArg_Parse(v, "i;array item must be integer", &x))
229 return -1;
230 else if (x < 0) {
231 PyErr_SetString(PyExc_OverflowError,
232 "unsigned short is less than minimum");
233 return -1;
234 }
235 else if (x > USHRT_MAX) {
236 PyErr_SetString(PyExc_OverflowError,
237 "unsigned short is greater than maximum");
238 return -1;
239 }
240 if (i >= 0)
241 ((short *)ap->ob_item)[i] = (short)x;
242 return 0;
Fred Drake541dc3b2000-06-28 17:49:30 +0000243}
Guido van Rossum549ab711997-01-03 19:09:47 +0000244
245static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000246i_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossume77a7571993-11-03 15:01:26 +0000247{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000248 return PyLong_FromLong((long) ((int *)ap->ob_item)[i]);
Guido van Rossume77a7571993-11-03 15:01:26 +0000249}
250
251static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000252i_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossume77a7571993-11-03 15:01:26 +0000253{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000254 int x;
255 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
256 if (!PyArg_Parse(v, "i;array item must be integer", &x))
257 return -1;
258 if (i >= 0)
259 ((int *)ap->ob_item)[i] = x;
260 return 0;
Guido van Rossume77a7571993-11-03 15:01:26 +0000261}
262
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000263static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000264II_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000265{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000266 return PyLong_FromUnsignedLong(
267 (unsigned long) ((unsigned int *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000268}
269
270static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000271II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000272{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000273 unsigned long x;
274 if (PyLong_Check(v)) {
275 x = PyLong_AsUnsignedLong(v);
276 if (x == (unsigned long) -1 && PyErr_Occurred())
277 return -1;
278 }
279 else {
280 long y;
281 if (!PyArg_Parse(v, "l;array item must be integer", &y))
282 return -1;
283 if (y < 0) {
284 PyErr_SetString(PyExc_OverflowError,
285 "unsigned int is less than minimum");
286 return -1;
287 }
288 x = (unsigned long)y;
Tim Petersbb307342000-09-10 05:22:54 +0000289
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000290 }
291 if (x > UINT_MAX) {
292 PyErr_SetString(PyExc_OverflowError,
293 "unsigned int is greater than maximum");
294 return -1;
295 }
Fred Drake541dc3b2000-06-28 17:49:30 +0000296
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000297 if (i >= 0)
298 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
299 return 0;
Guido van Rossum549ab711997-01-03 19:09:47 +0000300}
301
302static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000303l_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000304{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000305 return PyLong_FromLong(((long *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000306}
307
308static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000309l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000310{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000311 long x;
312 if (!PyArg_Parse(v, "l;array item must be integer", &x))
313 return -1;
314 if (i >= 0)
315 ((long *)ap->ob_item)[i] = x;
316 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000317}
318
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000319static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000320LL_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000321{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000322 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000323}
324
325static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000326LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000327{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000328 unsigned long x;
329 if (PyLong_Check(v)) {
330 x = PyLong_AsUnsignedLong(v);
331 if (x == (unsigned long) -1 && PyErr_Occurred())
332 return -1;
333 }
334 else {
335 long y;
336 if (!PyArg_Parse(v, "l;array item must be integer", &y))
337 return -1;
338 if (y < 0) {
339 PyErr_SetString(PyExc_OverflowError,
340 "unsigned long is less than minimum");
341 return -1;
342 }
343 x = (unsigned long)y;
Tim Petersbb307342000-09-10 05:22:54 +0000344
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000345 }
346 if (x > ULONG_MAX) {
347 PyErr_SetString(PyExc_OverflowError,
348 "unsigned long is greater than maximum");
349 return -1;
350 }
Tim Petersbb307342000-09-10 05:22:54 +0000351
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000352 if (i >= 0)
353 ((unsigned long *)ap->ob_item)[i] = x;
354 return 0;
Guido van Rossum549ab711997-01-03 19:09:47 +0000355}
356
357static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000358f_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000359{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000360 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000361}
362
363static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000364f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000365{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000366 float x;
367 if (!PyArg_Parse(v, "f;array item must be float", &x))
368 return -1;
369 if (i >= 0)
370 ((float *)ap->ob_item)[i] = x;
371 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000372}
373
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000374static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000375d_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000376{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000377 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000378}
379
380static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000381d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000382{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000383 double x;
384 if (!PyArg_Parse(v, "d;array item must be float", &x))
385 return -1;
386 if (i >= 0)
387 ((double *)ap->ob_item)[i] = x;
388 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000389}
390
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000391
Guido van Rossum778983b1993-02-19 15:55:02 +0000392/* Description of types */
Guido van Rossum234f9421993-06-17 12:35:49 +0000393static struct arraydescr descriptors[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000394 {'b', 1, b_getitem, b_setitem, "b"},
395 {'B', 1, BB_getitem, BB_setitem, "B"},
396 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem, "u"},
397 {'h', sizeof(short), h_getitem, h_setitem, "h"},
398 {'H', sizeof(short), HH_getitem, HH_setitem, "H"},
399 {'i', sizeof(int), i_getitem, i_setitem, "i"},
400 {'I', sizeof(int), II_getitem, II_setitem, "I"},
401 {'l', sizeof(long), l_getitem, l_setitem, "l"},
402 {'L', sizeof(long), LL_getitem, LL_setitem, "L"},
403 {'f', sizeof(float), f_getitem, f_setitem, "f"},
404 {'d', sizeof(double), d_getitem, d_setitem, "d"},
405 {'\0', 0, 0, 0, 0} /* Sentinel */
Guido van Rossum778983b1993-02-19 15:55:02 +0000406};
Tim Petersbb307342000-09-10 05:22:54 +0000407
408/****************************************************************************
409Implementations of array object methods.
410****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +0000411
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000412static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000413newarrayobject(PyTypeObject *type, Py_ssize_t size, struct arraydescr *descr)
Guido van Rossum778983b1993-02-19 15:55:02 +0000414{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000415 arrayobject *op;
416 size_t nbytes;
Martin v. Löwis99866332002-03-01 10:27:01 +0000417
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000418 if (size < 0) {
419 PyErr_BadInternalCall();
420 return NULL;
421 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000422
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000423 nbytes = size * descr->itemsize;
424 /* Check for overflow */
425 if (nbytes / descr->itemsize != (size_t)size) {
426 return PyErr_NoMemory();
427 }
428 op = (arrayobject *) type->tp_alloc(type, 0);
429 if (op == NULL) {
430 return NULL;
431 }
432 op->ob_descr = descr;
433 op->allocated = size;
434 op->weakreflist = NULL;
435 Py_SIZE(op) = size;
436 if (size <= 0) {
437 op->ob_item = NULL;
438 }
439 else {
440 op->ob_item = PyMem_NEW(char, nbytes);
441 if (op->ob_item == NULL) {
442 Py_DECREF(op);
443 return PyErr_NoMemory();
444 }
445 }
446 op->ob_exports = 0;
447 return (PyObject *) op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000448}
449
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000450static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000451getarrayitem(PyObject *op, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000452{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000453 register arrayobject *ap;
454 assert(array_Check(op));
455 ap = (arrayobject *)op;
456 assert(i>=0 && i<Py_SIZE(ap));
457 return (*ap->ob_descr->getitem)(ap, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000458}
459
460static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000461ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000462{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000463 char *items;
464 Py_ssize_t n = Py_SIZE(self);
465 if (v == NULL) {
466 PyErr_BadInternalCall();
467 return -1;
468 }
469 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
470 return -1;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000471
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000472 if (array_resize(self, n+1) == -1)
473 return -1;
474 items = self->ob_item;
475 if (where < 0) {
476 where += n;
477 if (where < 0)
478 where = 0;
479 }
480 if (where > n)
481 where = n;
482 /* appends don't need to call memmove() */
483 if (where != n)
484 memmove(items + (where+1)*self->ob_descr->itemsize,
485 items + where*self->ob_descr->itemsize,
486 (n-where)*self->ob_descr->itemsize);
487 return (*self->ob_descr->setitem)(self, where, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000488}
489
Guido van Rossum778983b1993-02-19 15:55:02 +0000490/* Methods */
491
492static void
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000493array_dealloc(arrayobject *op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000494{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000495 if (op->weakreflist != NULL)
496 PyObject_ClearWeakRefs((PyObject *) op);
497 if (op->ob_item != NULL)
498 PyMem_DEL(op->ob_item);
499 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000500}
501
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000502static PyObject *
503array_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000504{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000505 arrayobject *va, *wa;
506 PyObject *vi = NULL;
507 PyObject *wi = NULL;
508 Py_ssize_t i, k;
509 PyObject *res;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000510
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000511 if (!array_Check(v) || !array_Check(w)) {
512 Py_INCREF(Py_NotImplemented);
513 return Py_NotImplemented;
514 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000515
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000516 va = (arrayobject *)v;
517 wa = (arrayobject *)w;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000518
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000519 if (Py_SIZE(va) != Py_SIZE(wa) && (op == Py_EQ || op == Py_NE)) {
520 /* Shortcut: if the lengths differ, the arrays differ */
521 if (op == Py_EQ)
522 res = Py_False;
523 else
524 res = Py_True;
525 Py_INCREF(res);
526 return res;
527 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000528
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000529 /* Search for the first index where items are different */
530 k = 1;
531 for (i = 0; i < Py_SIZE(va) && i < Py_SIZE(wa); i++) {
532 vi = getarrayitem(v, i);
533 wi = getarrayitem(w, i);
534 if (vi == NULL || wi == NULL) {
535 Py_XDECREF(vi);
536 Py_XDECREF(wi);
537 return NULL;
538 }
539 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
540 if (k == 0)
541 break; /* Keeping vi and wi alive! */
542 Py_DECREF(vi);
543 Py_DECREF(wi);
544 if (k < 0)
545 return NULL;
546 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000547
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000548 if (k) {
549 /* No more items to compare -- compare sizes */
550 Py_ssize_t vs = Py_SIZE(va);
551 Py_ssize_t ws = Py_SIZE(wa);
552 int cmp;
553 switch (op) {
554 case Py_LT: cmp = vs < ws; break;
555 case Py_LE: cmp = vs <= ws; break;
556 case Py_EQ: cmp = vs == ws; break;
557 case Py_NE: cmp = vs != ws; break;
558 case Py_GT: cmp = vs > ws; break;
559 case Py_GE: cmp = vs >= ws; break;
560 default: return NULL; /* cannot happen */
561 }
562 if (cmp)
563 res = Py_True;
564 else
565 res = Py_False;
566 Py_INCREF(res);
567 return res;
568 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000569
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000570 /* We have an item that differs. First, shortcuts for EQ/NE */
571 if (op == Py_EQ) {
572 Py_INCREF(Py_False);
573 res = Py_False;
574 }
575 else if (op == Py_NE) {
576 Py_INCREF(Py_True);
577 res = Py_True;
578 }
579 else {
580 /* Compare the final item again using the proper operator */
581 res = PyObject_RichCompare(vi, wi, op);
582 }
583 Py_DECREF(vi);
584 Py_DECREF(wi);
585 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +0000586}
587
Martin v. Löwis18e16552006-02-15 17:27:45 +0000588static Py_ssize_t
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000589array_length(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +0000590{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000591 return Py_SIZE(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000592}
593
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000594static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000595array_item(arrayobject *a, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000596{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000597 if (i < 0 || i >= Py_SIZE(a)) {
598 PyErr_SetString(PyExc_IndexError, "array index out of range");
599 return NULL;
600 }
601 return getarrayitem((PyObject *)a, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000602}
603
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000604static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000605array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000606{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000607 arrayobject *np;
608 if (ilow < 0)
609 ilow = 0;
610 else if (ilow > Py_SIZE(a))
611 ilow = Py_SIZE(a);
612 if (ihigh < 0)
613 ihigh = 0;
614 if (ihigh < ilow)
615 ihigh = ilow;
616 else if (ihigh > Py_SIZE(a))
617 ihigh = Py_SIZE(a);
618 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
619 if (np == NULL)
620 return NULL;
621 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
622 (ihigh-ilow) * a->ob_descr->itemsize);
623 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000624}
625
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000626static PyObject *
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000627array_copy(arrayobject *a, PyObject *unused)
628{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000629 return array_slice(a, 0, Py_SIZE(a));
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000630}
631
632PyDoc_STRVAR(copy_doc,
633"copy(array)\n\
634\n\
635 Return a copy of the array.");
636
637static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000638array_concat(arrayobject *a, PyObject *bb)
Guido van Rossum778983b1993-02-19 15:55:02 +0000639{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000640 Py_ssize_t size;
641 arrayobject *np;
642 if (!array_Check(bb)) {
643 PyErr_Format(PyExc_TypeError,
644 "can only append array (not \"%.200s\") to array",
645 Py_TYPE(bb)->tp_name);
646 return NULL;
647 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000648#define b ((arrayobject *)bb)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000649 if (a->ob_descr != b->ob_descr) {
650 PyErr_BadArgument();
651 return NULL;
652 }
653 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) {
654 return PyErr_NoMemory();
655 }
656 size = Py_SIZE(a) + Py_SIZE(b);
657 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
658 if (np == NULL) {
659 return NULL;
660 }
661 memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize);
662 memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize,
663 b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
664 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000665#undef b
666}
667
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000668static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000669array_repeat(arrayobject *a, Py_ssize_t n)
Guido van Rossum778983b1993-02-19 15:55:02 +0000670{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000671 Py_ssize_t i;
672 Py_ssize_t size;
673 arrayobject *np;
674 char *p;
675 Py_ssize_t nbytes;
676 if (n < 0)
677 n = 0;
678 if ((Py_SIZE(a) != 0) && (n > PY_SSIZE_T_MAX / Py_SIZE(a))) {
679 return PyErr_NoMemory();
680 }
681 size = Py_SIZE(a) * n;
682 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
683 if (np == NULL)
684 return NULL;
685 p = np->ob_item;
686 nbytes = Py_SIZE(a) * a->ob_descr->itemsize;
687 for (i = 0; i < n; i++) {
688 memcpy(p, a->ob_item, nbytes);
689 p += nbytes;
690 }
691 return (PyObject *) np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000692}
693
694static int
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000695array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000696{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000697 char *item;
698 Py_ssize_t n; /* Size of replacement array */
699 Py_ssize_t d; /* Change in size */
Guido van Rossum778983b1993-02-19 15:55:02 +0000700#define b ((arrayobject *)v)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000701 if (v == NULL)
702 n = 0;
703 else if (array_Check(v)) {
704 n = Py_SIZE(b);
705 if (a == b) {
706 /* Special case "a[i:j] = a" -- copy b first */
707 int ret;
708 v = array_slice(b, 0, n);
709 if (!v)
710 return -1;
711 ret = array_ass_slice(a, ilow, ihigh, v);
712 Py_DECREF(v);
713 return ret;
714 }
715 if (b->ob_descr != a->ob_descr) {
716 PyErr_BadArgument();
717 return -1;
718 }
719 }
720 else {
721 PyErr_Format(PyExc_TypeError,
722 "can only assign array (not \"%.200s\") to array slice",
723 Py_TYPE(v)->tp_name);
724 return -1;
725 }
726 if (ilow < 0)
727 ilow = 0;
728 else if (ilow > Py_SIZE(a))
729 ilow = Py_SIZE(a);
730 if (ihigh < 0)
731 ihigh = 0;
732 if (ihigh < ilow)
733 ihigh = ilow;
734 else if (ihigh > Py_SIZE(a))
735 ihigh = Py_SIZE(a);
736 item = a->ob_item;
737 d = n - (ihigh-ilow);
738 /* Issue #4509: If the array has exported buffers and the slice
739 assignment would change the size of the array, fail early to make
740 sure we don't modify it. */
741 if (d != 0 && a->ob_exports > 0) {
742 PyErr_SetString(PyExc_BufferError,
743 "cannot resize an array that is exporting buffers");
744 return -1;
745 }
746 if (d < 0) { /* Delete -d items */
747 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
748 item + ihigh*a->ob_descr->itemsize,
749 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
750 if (array_resize(a, Py_SIZE(a) + d) == -1)
751 return -1;
752 }
753 else if (d > 0) { /* Insert d items */
754 if (array_resize(a, Py_SIZE(a) + d))
755 return -1;
756 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
757 item + ihigh*a->ob_descr->itemsize,
758 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
759 }
760 if (n > 0)
761 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
762 n*b->ob_descr->itemsize);
763 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000764#undef b
765}
766
767static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000768array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000769{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000770 if (i < 0 || i >= Py_SIZE(a)) {
771 PyErr_SetString(PyExc_IndexError,
772 "array assignment index out of range");
773 return -1;
774 }
775 if (v == NULL)
776 return array_ass_slice(a, i, i+1, v);
777 return (*a->ob_descr->setitem)(a, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000778}
779
780static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000781setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000782{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000783 assert(array_Check(a));
784 return array_ass_item((arrayobject *)a, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000785}
786
Martin v. Löwis99866332002-03-01 10:27:01 +0000787static int
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000788array_iter_extend(arrayobject *self, PyObject *bb)
789{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000790 PyObject *it, *v;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000791
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000792 it = PyObject_GetIter(bb);
793 if (it == NULL)
794 return -1;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000795
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000796 while ((v = PyIter_Next(it)) != NULL) {
Mark Dickinson13197f42010-08-06 09:42:28 +0000797 if (ins1(self, Py_SIZE(self), v) != 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000798 Py_DECREF(v);
799 Py_DECREF(it);
800 return -1;
801 }
802 Py_DECREF(v);
803 }
804 Py_DECREF(it);
805 if (PyErr_Occurred())
806 return -1;
807 return 0;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000808}
809
810static int
Martin v. Löwis99866332002-03-01 10:27:01 +0000811array_do_extend(arrayobject *self, PyObject *bb)
812{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000813 Py_ssize_t size, oldsize, bbsize;
Martin v. Löwis99866332002-03-01 10:27:01 +0000814
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000815 if (!array_Check(bb))
816 return array_iter_extend(self, bb);
817#define b ((arrayobject *)bb)
818 if (self->ob_descr != b->ob_descr) {
819 PyErr_SetString(PyExc_TypeError,
820 "can only extend with array of same kind");
821 return -1;
822 }
823 if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) ||
824 ((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
825 PyErr_NoMemory();
826 return -1;
827 }
828 oldsize = Py_SIZE(self);
829 /* Get the size of bb before resizing the array since bb could be self. */
830 bbsize = Py_SIZE(bb);
831 size = oldsize + Py_SIZE(b);
832 if (array_resize(self, size) == -1)
833 return -1;
834 memcpy(self->ob_item + oldsize * self->ob_descr->itemsize,
835 b->ob_item, bbsize * b->ob_descr->itemsize);
836
837 return 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000838#undef b
839}
840
841static PyObject *
842array_inplace_concat(arrayobject *self, PyObject *bb)
843{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000844 if (!array_Check(bb)) {
845 PyErr_Format(PyExc_TypeError,
846 "can only extend array with array (not \"%.200s\")",
847 Py_TYPE(bb)->tp_name);
848 return NULL;
849 }
850 if (array_do_extend(self, bb) == -1)
851 return NULL;
852 Py_INCREF(self);
853 return (PyObject *)self;
Martin v. Löwis99866332002-03-01 10:27:01 +0000854}
855
856static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000857array_inplace_repeat(arrayobject *self, Py_ssize_t n)
Martin v. Löwis99866332002-03-01 10:27:01 +0000858{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000859 char *items, *p;
860 Py_ssize_t size, i;
Martin v. Löwis99866332002-03-01 10:27:01 +0000861
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000862 if (Py_SIZE(self) > 0) {
863 if (n < 0)
864 n = 0;
865 items = self->ob_item;
866 if ((self->ob_descr->itemsize != 0) &&
867 (Py_SIZE(self) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
868 return PyErr_NoMemory();
869 }
870 size = Py_SIZE(self) * self->ob_descr->itemsize;
871 if (n > 0 && size > PY_SSIZE_T_MAX / n) {
872 return PyErr_NoMemory();
873 }
874 if (array_resize(self, n * Py_SIZE(self)) == -1)
875 return NULL;
876 items = p = self->ob_item;
877 for (i = 1; i < n; i++) {
878 p += size;
879 memcpy(p, items, size);
880 }
881 }
882 Py_INCREF(self);
883 return (PyObject *)self;
Martin v. Löwis99866332002-03-01 10:27:01 +0000884}
885
886
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000887static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000888ins(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000889{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000890 if (ins1(self, where, v) != 0)
891 return NULL;
892 Py_INCREF(Py_None);
893 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000894}
895
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000896static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000897array_count(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000898{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000899 Py_ssize_t count = 0;
900 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000901
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000902 for (i = 0; i < Py_SIZE(self); i++) {
903 PyObject *selfi = getarrayitem((PyObject *)self, i);
904 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
905 Py_DECREF(selfi);
906 if (cmp > 0)
907 count++;
908 else if (cmp < 0)
909 return NULL;
910 }
911 return PyLong_FromSsize_t(count);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000912}
913
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000914PyDoc_STRVAR(count_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000915"count(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000916\n\
Mark Dickinson934896d2009-02-21 20:59:32 +0000917Return number of occurrences of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000918
919static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000920array_index(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000921{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000922 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000923
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000924 for (i = 0; i < Py_SIZE(self); i++) {
925 PyObject *selfi = getarrayitem((PyObject *)self, i);
926 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
927 Py_DECREF(selfi);
928 if (cmp > 0) {
929 return PyLong_FromLong((long)i);
930 }
931 else if (cmp < 0)
932 return NULL;
933 }
934 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
935 return NULL;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000936}
937
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000938PyDoc_STRVAR(index_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000939"index(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000940\n\
Mark Dickinson934896d2009-02-21 20:59:32 +0000941Return index of first occurrence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000942
Raymond Hettinger625812f2003-01-07 01:58:52 +0000943static int
944array_contains(arrayobject *self, PyObject *v)
945{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000946 Py_ssize_t i;
947 int cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +0000948
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000949 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) {
950 PyObject *selfi = getarrayitem((PyObject *)self, i);
951 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
952 Py_DECREF(selfi);
953 }
954 return cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +0000955}
956
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000957static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000958array_remove(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000959{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000960 int i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000961
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000962 for (i = 0; i < Py_SIZE(self); i++) {
963 PyObject *selfi = getarrayitem((PyObject *)self,i);
964 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
965 Py_DECREF(selfi);
966 if (cmp > 0) {
967 if (array_ass_slice(self, i, i+1,
968 (PyObject *)NULL) != 0)
969 return NULL;
970 Py_INCREF(Py_None);
971 return Py_None;
972 }
973 else if (cmp < 0)
974 return NULL;
975 }
976 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
977 return NULL;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000978}
979
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000980PyDoc_STRVAR(remove_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000981"remove(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000982\n\
Mark Dickinson934896d2009-02-21 20:59:32 +0000983Remove the first occurrence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000984
985static PyObject *
986array_pop(arrayobject *self, PyObject *args)
987{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000988 Py_ssize_t i = -1;
989 PyObject *v;
990 if (!PyArg_ParseTuple(args, "|n:pop", &i))
991 return NULL;
992 if (Py_SIZE(self) == 0) {
993 /* Special-case most common failure cause */
994 PyErr_SetString(PyExc_IndexError, "pop from empty array");
995 return NULL;
996 }
997 if (i < 0)
998 i += Py_SIZE(self);
999 if (i < 0 || i >= Py_SIZE(self)) {
1000 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1001 return NULL;
1002 }
1003 v = getarrayitem((PyObject *)self,i);
1004 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
1005 Py_DECREF(v);
1006 return NULL;
1007 }
1008 return v;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001009}
1010
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001011PyDoc_STRVAR(pop_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001012"pop([i])\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001013\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001014Return the i-th element and delete it from the array. i defaults to -1.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001015
1016static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001017array_extend(arrayobject *self, PyObject *bb)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001018{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001019 if (array_do_extend(self, bb) == -1)
1020 return NULL;
1021 Py_INCREF(Py_None);
1022 return Py_None;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001023}
1024
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001025PyDoc_STRVAR(extend_doc,
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001026"extend(array or iterable)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001027\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001028 Append items to the end of the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001029
1030static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001031array_insert(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001032{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001033 Py_ssize_t i;
1034 PyObject *v;
1035 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
1036 return NULL;
1037 return ins(self, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001038}
1039
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001040PyDoc_STRVAR(insert_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001041"insert(i,x)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001042\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001043Insert a new item x into the array before position i.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001044
1045
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001046static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001047array_buffer_info(arrayobject *self, PyObject *unused)
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001048{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001049 PyObject* retval = NULL;
1050 retval = PyTuple_New(2);
1051 if (!retval)
1052 return NULL;
Fred Drake541dc3b2000-06-28 17:49:30 +00001053
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001054 PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item));
1055 PyTuple_SET_ITEM(retval, 1, PyLong_FromLong((long)(Py_SIZE(self))));
Fred Drake541dc3b2000-06-28 17:49:30 +00001056
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001057 return retval;
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001058}
1059
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001060PyDoc_STRVAR(buffer_info_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001061"buffer_info() -> (address, length)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001062\n\
1063Return a tuple (address, length) giving the current memory address and\n\
Guido van Rossum702d08e2001-07-27 16:05:32 +00001064the length in items of the buffer used to hold array's contents\n\
1065The length should be multiplied by the itemsize attribute to calculate\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001066the buffer length in bytes.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001067
1068
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001069static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001070array_append(arrayobject *self, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +00001071{
Mark Dickinson13197f42010-08-06 09:42:28 +00001072 return ins(self, Py_SIZE(self), v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001073}
1074
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001075PyDoc_STRVAR(append_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001076"append(x)\n\
1077\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001078Append new value x to the end of the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001079
1080
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001081static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001082array_byteswap(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001083{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001084 char *p;
1085 Py_ssize_t i;
Fred Drakebf272981999-12-03 17:15:30 +00001086
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001087 switch (self->ob_descr->itemsize) {
1088 case 1:
1089 break;
1090 case 2:
1091 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) {
1092 char p0 = p[0];
1093 p[0] = p[1];
1094 p[1] = p0;
1095 }
1096 break;
1097 case 4:
1098 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 4) {
1099 char p0 = p[0];
1100 char p1 = p[1];
1101 p[0] = p[3];
1102 p[1] = p[2];
1103 p[2] = p1;
1104 p[3] = p0;
1105 }
1106 break;
1107 case 8:
1108 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) {
1109 char p0 = p[0];
1110 char p1 = p[1];
1111 char p2 = p[2];
1112 char p3 = p[3];
1113 p[0] = p[7];
1114 p[1] = p[6];
1115 p[2] = p[5];
1116 p[3] = p[4];
1117 p[4] = p3;
1118 p[5] = p2;
1119 p[6] = p1;
1120 p[7] = p0;
1121 }
1122 break;
1123 default:
1124 PyErr_SetString(PyExc_RuntimeError,
1125 "don't know how to byteswap this array type");
1126 return NULL;
1127 }
1128 Py_INCREF(Py_None);
1129 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001130}
1131
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001132PyDoc_STRVAR(byteswap_doc,
Fred Drakebf272981999-12-03 17:15:30 +00001133"byteswap()\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001134\n\
Fred Drakebf272981999-12-03 17:15:30 +00001135Byteswap all items of the array. If the items in the array are not 1, 2,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011364, or 8 bytes in size, RuntimeError is raised.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001137
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001138static PyObject *
Raymond Hettingerb0900e62004-12-16 16:23:40 +00001139array_reduce(arrayobject *array)
1140{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001141 PyObject *dict, *result;
Raymond Hettingerb0900e62004-12-16 16:23:40 +00001142
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001143 dict = PyObject_GetAttrString((PyObject *)array, "__dict__");
1144 if (dict == NULL) {
1145 PyErr_Clear();
1146 dict = Py_None;
1147 Py_INCREF(dict);
1148 }
1149 if (Py_SIZE(array) > 0) {
1150 if (array->ob_descr->itemsize
1151 > PY_SSIZE_T_MAX / Py_SIZE(array)) {
1152 return PyErr_NoMemory();
1153 }
1154 result = Py_BuildValue("O(Cy#)O",
1155 Py_TYPE(array),
1156 array->ob_descr->typecode,
1157 array->ob_item,
1158 Py_SIZE(array) * array->ob_descr->itemsize,
1159 dict);
1160 } else {
1161 result = Py_BuildValue("O(C)O",
1162 Py_TYPE(array),
1163 array->ob_descr->typecode,
1164 dict);
1165 }
1166 Py_DECREF(dict);
1167 return result;
Raymond Hettingerb0900e62004-12-16 16:23:40 +00001168}
1169
1170PyDoc_STRVAR(array_doc, "Return state information for pickling.");
1171
1172static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001173array_reverse(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001174{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001175 register Py_ssize_t itemsize = self->ob_descr->itemsize;
1176 register char *p, *q;
1177 /* little buffer to hold items while swapping */
1178 char tmp[256]; /* 8 is probably enough -- but why skimp */
1179 assert((size_t)itemsize <= sizeof(tmp));
Guido van Rossume77a7571993-11-03 15:01:26 +00001180
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001181 if (Py_SIZE(self) > 1) {
1182 for (p = self->ob_item,
1183 q = self->ob_item + (Py_SIZE(self) - 1)*itemsize;
1184 p < q;
1185 p += itemsize, q -= itemsize) {
1186 /* memory areas guaranteed disjoint, so memcpy
1187 * is safe (& memmove may be slower).
1188 */
1189 memcpy(tmp, p, itemsize);
1190 memcpy(p, q, itemsize);
1191 memcpy(q, tmp, itemsize);
1192 }
1193 }
Tim Petersbb307342000-09-10 05:22:54 +00001194
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001195 Py_INCREF(Py_None);
1196 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001197}
Guido van Rossume77a7571993-11-03 15:01:26 +00001198
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001199PyDoc_STRVAR(reverse_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001200"reverse()\n\
1201\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001202Reverse the order of the items in the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001203
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001204
1205/* Forward */
1206static PyObject *array_fromstring(arrayobject *self, PyObject *args);
1207
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001208static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001209array_fromfile(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001210{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001211 PyObject *f, *b, *res;
1212 Py_ssize_t itemsize = self->ob_descr->itemsize;
1213 Py_ssize_t n, nbytes;
1214 int not_enough_bytes;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001215
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001216 if (!PyArg_ParseTuple(args, "On:fromfile", &f, &n))
1217 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001218
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001219 nbytes = n * itemsize;
1220 if (nbytes < 0 || nbytes/itemsize != n) {
1221 PyErr_NoMemory();
1222 return NULL;
1223 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001224
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001225 b = PyObject_CallMethod(f, "read", "n", nbytes);
1226 if (b == NULL)
1227 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001228
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001229 if (!PyBytes_Check(b)) {
1230 PyErr_SetString(PyExc_TypeError,
1231 "read() didn't return bytes");
1232 Py_DECREF(b);
1233 return NULL;
1234 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001235
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001236 not_enough_bytes = (PyBytes_GET_SIZE(b) != nbytes);
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001237
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001238 args = Py_BuildValue("(O)", b);
1239 Py_DECREF(b);
1240 if (args == NULL)
1241 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001242
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001243 res = array_fromstring(self, args);
1244 Py_DECREF(args);
1245 if (res == NULL)
1246 return NULL;
Hirokazu Yamamoto54d0df62009-03-06 03:04:07 +00001247
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001248 if (not_enough_bytes) {
1249 PyErr_SetString(PyExc_EOFError,
1250 "read() didn't return enough bytes");
1251 Py_DECREF(res);
1252 return NULL;
1253 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001254
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001255 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +00001256}
1257
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001258PyDoc_STRVAR(fromfile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001259"fromfile(f, n)\n\
1260\n\
1261Read n objects from the file object f and append them to the end of the\n\
Georg Brandlf25ef502008-02-01 11:30:18 +00001262array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001263
1264
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001265static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001266array_tofile(arrayobject *self, PyObject *f)
Guido van Rossum778983b1993-02-19 15:55:02 +00001267{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001268 Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize;
1269 /* Write 64K blocks at a time */
1270 /* XXX Make the block size settable */
1271 int BLOCKSIZE = 64*1024;
1272 Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE;
1273 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001274
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001275 if (Py_SIZE(self) == 0)
1276 goto done;
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001277
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001278 for (i = 0; i < nblocks; i++) {
1279 char* ptr = self->ob_item + i*BLOCKSIZE;
1280 Py_ssize_t size = BLOCKSIZE;
1281 PyObject *bytes, *res;
1282 if (i*BLOCKSIZE + size > nbytes)
1283 size = nbytes - i*BLOCKSIZE;
1284 bytes = PyBytes_FromStringAndSize(ptr, size);
1285 if (bytes == NULL)
1286 return NULL;
1287 res = PyObject_CallMethod(f, "write", "O", bytes);
1288 Py_DECREF(bytes);
1289 if (res == NULL)
1290 return NULL;
1291 Py_DECREF(res); /* drop write result */
1292 }
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001293
1294 done:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001295 Py_INCREF(Py_None);
1296 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001297}
1298
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001299PyDoc_STRVAR(tofile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001300"tofile(f)\n\
1301\n\
Georg Brandlf25ef502008-02-01 11:30:18 +00001302Write all items (as machine values) to the file object f.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001303
1304
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001305static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001306array_fromlist(arrayobject *self, PyObject *list)
Guido van Rossum778983b1993-02-19 15:55:02 +00001307{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001308 Py_ssize_t n;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001309
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001310 if (!PyList_Check(list)) {
1311 PyErr_SetString(PyExc_TypeError, "arg must be list");
1312 return NULL;
1313 }
1314 n = PyList_Size(list);
1315 if (n > 0) {
1316 Py_ssize_t i, old_size;
1317 old_size = Py_SIZE(self);
1318 if (array_resize(self, old_size + n) == -1)
1319 return NULL;
1320 for (i = 0; i < n; i++) {
1321 PyObject *v = PyList_GetItem(list, i);
1322 if ((*self->ob_descr->setitem)(self,
1323 Py_SIZE(self) - n + i, v) != 0) {
1324 array_resize(self, old_size);
1325 return NULL;
1326 }
1327 }
1328 }
1329 Py_INCREF(Py_None);
1330 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001331}
1332
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001333PyDoc_STRVAR(fromlist_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001334"fromlist(list)\n\
1335\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001336Append items to array from list.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001337
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001338static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001339array_tolist(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001340{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001341 PyObject *list = PyList_New(Py_SIZE(self));
1342 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001343
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001344 if (list == NULL)
1345 return NULL;
1346 for (i = 0; i < Py_SIZE(self); i++) {
1347 PyObject *v = getarrayitem((PyObject *)self, i);
1348 if (v == NULL) {
1349 Py_DECREF(list);
1350 return NULL;
1351 }
1352 PyList_SetItem(list, i, v);
1353 }
1354 return list;
Guido van Rossum778983b1993-02-19 15:55:02 +00001355}
1356
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001357PyDoc_STRVAR(tolist_doc,
Guido van Rossumfc6aba51998-10-14 02:52:31 +00001358"tolist() -> list\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001359\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001360Convert array to an ordinary list with the same items.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001361
1362
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001363static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001364array_fromstring(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001365{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001366 char *str;
1367 Py_ssize_t n;
1368 int itemsize = self->ob_descr->itemsize;
1369 if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n))
1370 return NULL;
1371 if (n % itemsize != 0) {
1372 PyErr_SetString(PyExc_ValueError,
1373 "string length not a multiple of item size");
1374 return NULL;
1375 }
1376 n = n / itemsize;
1377 if (n > 0) {
1378 Py_ssize_t old_size = Py_SIZE(self);
1379 if ((n > PY_SSIZE_T_MAX - old_size) ||
1380 ((old_size + n) > PY_SSIZE_T_MAX / itemsize)) {
1381 return PyErr_NoMemory();
1382 }
1383 if (array_resize(self, old_size + n) == -1)
1384 return NULL;
1385 memcpy(self->ob_item + old_size * itemsize,
1386 str, n * itemsize);
1387 }
1388 Py_INCREF(Py_None);
1389 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001390}
1391
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001392PyDoc_STRVAR(fromstring_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001393"fromstring(string)\n\
1394\n\
1395Appends items from the string, interpreting it as an array of machine\n\
Walter Dörwald93b30b52007-06-22 12:21:53 +00001396values, as if it had been read from a file using the fromfile() method).");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001397
1398
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001399static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001400array_tostring(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001401{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001402 if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
1403 return PyBytes_FromStringAndSize(self->ob_item,
1404 Py_SIZE(self) * self->ob_descr->itemsize);
1405 } else {
1406 return PyErr_NoMemory();
1407 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001408}
1409
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001410PyDoc_STRVAR(tostring_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001411"tostring() -> string\n\
1412\n\
1413Convert the array to an array of machine values and return the string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001414representation.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001415
Martin v. Löwis99866332002-03-01 10:27:01 +00001416
1417
Martin v. Löwis99866332002-03-01 10:27:01 +00001418static PyObject *
1419array_fromunicode(arrayobject *self, PyObject *args)
1420{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001421 Py_UNICODE *ustr;
1422 Py_ssize_t n;
1423 char typecode;
Martin v. Löwis99866332002-03-01 10:27:01 +00001424
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001425 if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
1426 return NULL;
1427 typecode = self->ob_descr->typecode;
1428 if ((typecode != 'u')) {
1429 PyErr_SetString(PyExc_ValueError,
1430 "fromunicode() may only be called on "
1431 "unicode type arrays");
1432 return NULL;
1433 }
1434 if (n > 0) {
1435 Py_ssize_t old_size = Py_SIZE(self);
1436 if (array_resize(self, old_size + n) == -1)
1437 return NULL;
1438 memcpy(self->ob_item + old_size * sizeof(Py_UNICODE),
1439 ustr, n * sizeof(Py_UNICODE));
1440 }
Martin v. Löwis99866332002-03-01 10:27:01 +00001441
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001442 Py_INCREF(Py_None);
1443 return Py_None;
Martin v. Löwis99866332002-03-01 10:27:01 +00001444}
1445
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001446PyDoc_STRVAR(fromunicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001447"fromunicode(ustr)\n\
1448\n\
1449Extends this array with data from the unicode string ustr.\n\
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001450The array must be a unicode type array; otherwise a ValueError\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001451is raised. Use array.fromstring(ustr.decode(...)) to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001452append Unicode data to an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001453
1454
1455static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001456array_tounicode(arrayobject *self, PyObject *unused)
Martin v. Löwis99866332002-03-01 10:27:01 +00001457{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001458 char typecode;
1459 typecode = self->ob_descr->typecode;
1460 if ((typecode != 'u')) {
1461 PyErr_SetString(PyExc_ValueError,
1462 "tounicode() may only be called on unicode type arrays");
1463 return NULL;
1464 }
1465 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_SIZE(self));
Martin v. Löwis99866332002-03-01 10:27:01 +00001466}
1467
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001468PyDoc_STRVAR(tounicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001469"tounicode() -> unicode\n\
1470\n\
1471Convert the array to a unicode string. The array must be\n\
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001472a unicode type array; otherwise a ValueError is raised. Use\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001473array.tostring().decode() to obtain a unicode string from\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001474an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001475
Martin v. Löwis99866332002-03-01 10:27:01 +00001476
1477
1478static PyObject *
1479array_get_typecode(arrayobject *a, void *closure)
1480{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001481 char tc = a->ob_descr->typecode;
1482 return PyUnicode_FromStringAndSize(&tc, 1);
Martin v. Löwis99866332002-03-01 10:27:01 +00001483}
1484
1485static PyObject *
1486array_get_itemsize(arrayobject *a, void *closure)
1487{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001488 return PyLong_FromLong((long)a->ob_descr->itemsize);
Martin v. Löwis99866332002-03-01 10:27:01 +00001489}
1490
1491static PyGetSetDef array_getsets [] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001492 {"typecode", (getter) array_get_typecode, NULL,
1493 "the typecode character used to create the array"},
1494 {"itemsize", (getter) array_get_itemsize, NULL,
1495 "the size, in bytes, of one array item"},
1496 {NULL}
Martin v. Löwis99866332002-03-01 10:27:01 +00001497};
1498
Martin v. Löwis59683e82008-06-13 07:50:45 +00001499static PyMethodDef array_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001500 {"append", (PyCFunction)array_append, METH_O,
1501 append_doc},
1502 {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS,
1503 buffer_info_doc},
1504 {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
1505 byteswap_doc},
1506 {"__copy__", (PyCFunction)array_copy, METH_NOARGS,
1507 copy_doc},
1508 {"count", (PyCFunction)array_count, METH_O,
1509 count_doc},
1510 {"__deepcopy__",(PyCFunction)array_copy, METH_O,
1511 copy_doc},
1512 {"extend", (PyCFunction)array_extend, METH_O,
1513 extend_doc},
1514 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
1515 fromfile_doc},
1516 {"fromlist", (PyCFunction)array_fromlist, METH_O,
1517 fromlist_doc},
1518 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
1519 fromstring_doc},
1520 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
1521 fromunicode_doc},
1522 {"index", (PyCFunction)array_index, METH_O,
1523 index_doc},
1524 {"insert", (PyCFunction)array_insert, METH_VARARGS,
1525 insert_doc},
1526 {"pop", (PyCFunction)array_pop, METH_VARARGS,
1527 pop_doc},
1528 {"__reduce__", (PyCFunction)array_reduce, METH_NOARGS,
1529 array_doc},
1530 {"remove", (PyCFunction)array_remove, METH_O,
1531 remove_doc},
1532 {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
1533 reverse_doc},
1534/* {"sort", (PyCFunction)array_sort, METH_VARARGS,
1535 sort_doc},*/
1536 {"tofile", (PyCFunction)array_tofile, METH_O,
1537 tofile_doc},
1538 {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
1539 tolist_doc},
1540 {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
1541 tostring_doc},
1542 {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
1543 tounicode_doc},
1544 {NULL, NULL} /* sentinel */
Guido van Rossum778983b1993-02-19 15:55:02 +00001545};
1546
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001547static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001548array_repr(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +00001549{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001550 char typecode;
1551 PyObject *s, *v = NULL;
1552 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00001553
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001554 len = Py_SIZE(a);
1555 typecode = a->ob_descr->typecode;
1556 if (len == 0) {
1557 return PyUnicode_FromFormat("array('%c')", typecode);
1558 }
1559 if ((typecode == 'u'))
1560 v = array_tounicode(a, NULL);
1561 else
1562 v = array_tolist(a, NULL);
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001563
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001564 s = PyUnicode_FromFormat("array('%c', %R)", typecode, v);
1565 Py_DECREF(v);
1566 return s;
Guido van Rossum778983b1993-02-19 15:55:02 +00001567}
1568
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001569static PyObject*
1570array_subscr(arrayobject* self, PyObject* item)
1571{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001572 if (PyIndex_Check(item)) {
1573 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
1574 if (i==-1 && PyErr_Occurred()) {
1575 return NULL;
1576 }
1577 if (i < 0)
1578 i += Py_SIZE(self);
1579 return array_item(self, i);
1580 }
1581 else if (PySlice_Check(item)) {
1582 Py_ssize_t start, stop, step, slicelength, cur, i;
1583 PyObject* result;
1584 arrayobject* ar;
1585 int itemsize = self->ob_descr->itemsize;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001586
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001587 if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self),
1588 &start, &stop, &step, &slicelength) < 0) {
1589 return NULL;
1590 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001591
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001592 if (slicelength <= 0) {
1593 return newarrayobject(&Arraytype, 0, self->ob_descr);
1594 }
1595 else if (step == 1) {
1596 PyObject *result = newarrayobject(&Arraytype,
1597 slicelength, self->ob_descr);
1598 if (result == NULL)
1599 return NULL;
1600 memcpy(((arrayobject *)result)->ob_item,
1601 self->ob_item + start * itemsize,
1602 slicelength * itemsize);
1603 return result;
1604 }
1605 else {
1606 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
1607 if (!result) return NULL;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001608
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001609 ar = (arrayobject*)result;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001610
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001611 for (cur = start, i = 0; i < slicelength;
1612 cur += step, i++) {
1613 memcpy(ar->ob_item + i*itemsize,
1614 self->ob_item + cur*itemsize,
1615 itemsize);
1616 }
1617
1618 return result;
1619 }
1620 }
1621 else {
1622 PyErr_SetString(PyExc_TypeError,
1623 "array indices must be integers");
1624 return NULL;
1625 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001626}
1627
1628static int
1629array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
1630{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001631 Py_ssize_t start, stop, step, slicelength, needed;
1632 arrayobject* other;
1633 int itemsize;
Thomas Woutersed03b412007-08-28 21:37:11 +00001634
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001635 if (PyIndex_Check(item)) {
1636 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Georg Brandldf475152009-08-13 09:05:38 +00001637
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001638 if (i == -1 && PyErr_Occurred())
1639 return -1;
1640 if (i < 0)
1641 i += Py_SIZE(self);
1642 if (i < 0 || i >= Py_SIZE(self)) {
1643 PyErr_SetString(PyExc_IndexError,
1644 "array assignment index out of range");
1645 return -1;
1646 }
1647 if (value == NULL) {
1648 /* Fall through to slice assignment */
1649 start = i;
1650 stop = i + 1;
1651 step = 1;
1652 slicelength = 1;
1653 }
1654 else
1655 return (*self->ob_descr->setitem)(self, i, value);
1656 }
1657 else if (PySlice_Check(item)) {
1658 if (PySlice_GetIndicesEx((PySliceObject *)item,
1659 Py_SIZE(self), &start, &stop,
1660 &step, &slicelength) < 0) {
1661 return -1;
1662 }
1663 }
1664 else {
1665 PyErr_SetString(PyExc_TypeError,
1666 "array indices must be integer");
1667 return -1;
1668 }
1669 if (value == NULL) {
1670 other = NULL;
1671 needed = 0;
1672 }
1673 else if (array_Check(value)) {
1674 other = (arrayobject *)value;
1675 needed = Py_SIZE(other);
1676 if (self == other) {
1677 /* Special case "self[i:j] = self" -- copy self first */
1678 int ret;
1679 value = array_slice(other, 0, needed);
1680 if (value == NULL)
1681 return -1;
1682 ret = array_ass_subscr(self, item, value);
1683 Py_DECREF(value);
1684 return ret;
1685 }
1686 if (other->ob_descr != self->ob_descr) {
1687 PyErr_BadArgument();
1688 return -1;
1689 }
1690 }
1691 else {
1692 PyErr_Format(PyExc_TypeError,
1693 "can only assign array (not \"%.200s\") to array slice",
1694 Py_TYPE(value)->tp_name);
1695 return -1;
1696 }
1697 itemsize = self->ob_descr->itemsize;
1698 /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
1699 if ((step > 0 && stop < start) ||
1700 (step < 0 && stop > start))
1701 stop = start;
Georg Brandldf475152009-08-13 09:05:38 +00001702
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001703 /* Issue #4509: If the array has exported buffers and the slice
1704 assignment would change the size of the array, fail early to make
1705 sure we don't modify it. */
1706 if ((needed == 0 || slicelength != needed) && self->ob_exports > 0) {
1707 PyErr_SetString(PyExc_BufferError,
1708 "cannot resize an array that is exporting buffers");
1709 return -1;
1710 }
Mark Dickinsona53f2c92010-01-29 17:29:21 +00001711
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001712 if (step == 1) {
1713 if (slicelength > needed) {
1714 memmove(self->ob_item + (start + needed) * itemsize,
1715 self->ob_item + stop * itemsize,
1716 (Py_SIZE(self) - stop) * itemsize);
1717 if (array_resize(self, Py_SIZE(self) +
1718 needed - slicelength) < 0)
1719 return -1;
1720 }
1721 else if (slicelength < needed) {
1722 if (array_resize(self, Py_SIZE(self) +
1723 needed - slicelength) < 0)
1724 return -1;
1725 memmove(self->ob_item + (start + needed) * itemsize,
1726 self->ob_item + stop * itemsize,
1727 (Py_SIZE(self) - start - needed) * itemsize);
1728 }
1729 if (needed > 0)
1730 memcpy(self->ob_item + start * itemsize,
1731 other->ob_item, needed * itemsize);
1732 return 0;
1733 }
1734 else if (needed == 0) {
1735 /* Delete slice */
1736 size_t cur;
1737 Py_ssize_t i;
Thomas Woutersed03b412007-08-28 21:37:11 +00001738
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001739 if (step < 0) {
1740 stop = start + 1;
1741 start = stop + step * (slicelength - 1) - 1;
1742 step = -step;
1743 }
1744 for (cur = start, i = 0; i < slicelength;
1745 cur += step, i++) {
1746 Py_ssize_t lim = step - 1;
Thomas Woutersed03b412007-08-28 21:37:11 +00001747
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001748 if (cur + step >= (size_t)Py_SIZE(self))
1749 lim = Py_SIZE(self) - cur - 1;
1750 memmove(self->ob_item + (cur - i) * itemsize,
1751 self->ob_item + (cur + 1) * itemsize,
1752 lim * itemsize);
1753 }
1754 cur = start + slicelength * step;
1755 if (cur < (size_t)Py_SIZE(self)) {
1756 memmove(self->ob_item + (cur-slicelength) * itemsize,
1757 self->ob_item + cur * itemsize,
1758 (Py_SIZE(self) - cur) * itemsize);
1759 }
1760 if (array_resize(self, Py_SIZE(self) - slicelength) < 0)
1761 return -1;
1762 return 0;
1763 }
1764 else {
1765 Py_ssize_t cur, i;
1766
1767 if (needed != slicelength) {
1768 PyErr_Format(PyExc_ValueError,
1769 "attempt to assign array of size %zd "
1770 "to extended slice of size %zd",
1771 needed, slicelength);
1772 return -1;
1773 }
1774 for (cur = start, i = 0; i < slicelength;
1775 cur += step, i++) {
1776 memcpy(self->ob_item + cur * itemsize,
1777 other->ob_item + i * itemsize,
1778 itemsize);
1779 }
1780 return 0;
1781 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001782}
1783
1784static PyMappingMethods array_as_mapping = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001785 (lenfunc)array_length,
1786 (binaryfunc)array_subscr,
1787 (objobjargproc)array_ass_subscr
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001788};
1789
Guido van Rossumd8faa362007-04-27 19:54:29 +00001790static const void *emptybuf = "";
1791
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001792
1793static int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00001794array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001795{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001796 if (view==NULL) goto finish;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001797
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001798 view->buf = (void *)self->ob_item;
1799 view->obj = (PyObject*)self;
1800 Py_INCREF(self);
1801 if (view->buf == NULL)
1802 view->buf = (void *)emptybuf;
1803 view->len = (Py_SIZE(self)) * self->ob_descr->itemsize;
1804 view->readonly = 0;
1805 view->ndim = 1;
1806 view->itemsize = self->ob_descr->itemsize;
1807 view->suboffsets = NULL;
1808 view->shape = NULL;
1809 if ((flags & PyBUF_ND)==PyBUF_ND) {
1810 view->shape = &((Py_SIZE(self)));
1811 }
1812 view->strides = NULL;
1813 if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES)
1814 view->strides = &(view->itemsize);
1815 view->format = NULL;
1816 view->internal = NULL;
1817 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
1818 view->format = self->ob_descr->formats;
Travis E. Oliphantddacf962007-10-13 21:03:27 +00001819#ifdef Py_UNICODE_WIDE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001820 if (self->ob_descr->typecode == 'u') {
1821 view->format = "w";
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001822 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001823#endif
1824 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001825
1826 finish:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001827 self->ob_exports++;
1828 return 0;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001829}
1830
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001831static void
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00001832array_buffer_relbuf(arrayobject *self, Py_buffer *view)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001833{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001834 self->ob_exports--;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001835}
1836
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001837static PySequenceMethods array_as_sequence = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001838 (lenfunc)array_length, /*sq_length*/
1839 (binaryfunc)array_concat, /*sq_concat*/
1840 (ssizeargfunc)array_repeat, /*sq_repeat*/
1841 (ssizeargfunc)array_item, /*sq_item*/
1842 0, /*sq_slice*/
1843 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
1844 0, /*sq_ass_slice*/
1845 (objobjproc)array_contains, /*sq_contains*/
1846 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
1847 (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001848};
1849
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001850static PyBufferProcs array_as_buffer = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001851 (getbufferproc)array_buffer_getbuf,
1852 (releasebufferproc)array_buffer_relbuf
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001853};
1854
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001855static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +00001856array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum778983b1993-02-19 15:55:02 +00001857{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001858 int c;
1859 PyObject *initial = NULL, *it = NULL;
1860 struct arraydescr *descr;
Martin v. Löwis99866332002-03-01 10:27:01 +00001861
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001862 if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds))
1863 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001864
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001865 if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
1866 return NULL;
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001867
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001868 if (!(initial == NULL || PyList_Check(initial)
1869 || PyByteArray_Check(initial)
1870 || PyBytes_Check(initial)
1871 || PyTuple_Check(initial)
1872 || ((c=='u') && PyUnicode_Check(initial)))) {
1873 it = PyObject_GetIter(initial);
1874 if (it == NULL)
1875 return NULL;
1876 /* We set initial to NULL so that the subsequent code
1877 will create an empty array of the appropriate type
1878 and afterwards we can use array_iter_extend to populate
1879 the array.
1880 */
1881 initial = NULL;
1882 }
1883 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1884 if (descr->typecode == c) {
1885 PyObject *a;
1886 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00001887
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001888 if (initial == NULL || !(PyList_Check(initial)
1889 || PyTuple_Check(initial)))
1890 len = 0;
1891 else
1892 len = PySequence_Size(initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001893
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001894 a = newarrayobject(type, len, descr);
1895 if (a == NULL)
1896 return NULL;
1897
1898 if (len > 0) {
1899 Py_ssize_t i;
1900 for (i = 0; i < len; i++) {
1901 PyObject *v =
1902 PySequence_GetItem(initial, i);
1903 if (v == NULL) {
1904 Py_DECREF(a);
1905 return NULL;
1906 }
1907 if (setarrayitem(a, i, v) != 0) {
1908 Py_DECREF(v);
1909 Py_DECREF(a);
1910 return NULL;
1911 }
1912 Py_DECREF(v);
1913 }
1914 }
1915 else if (initial != NULL && (PyByteArray_Check(initial) ||
1916 PyBytes_Check(initial))) {
1917 PyObject *t_initial, *v;
1918 t_initial = PyTuple_Pack(1, initial);
1919 if (t_initial == NULL) {
1920 Py_DECREF(a);
1921 return NULL;
1922 }
1923 v = array_fromstring((arrayobject *)a,
1924 t_initial);
1925 Py_DECREF(t_initial);
1926 if (v == NULL) {
1927 Py_DECREF(a);
1928 return NULL;
1929 }
1930 Py_DECREF(v);
1931 }
1932 else if (initial != NULL && PyUnicode_Check(initial)) {
1933 Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial);
1934 if (n > 0) {
1935 arrayobject *self = (arrayobject *)a;
1936 char *item = self->ob_item;
1937 item = (char *)PyMem_Realloc(item, n);
1938 if (item == NULL) {
1939 PyErr_NoMemory();
1940 Py_DECREF(a);
1941 return NULL;
1942 }
1943 self->ob_item = item;
1944 Py_SIZE(self) = n / sizeof(Py_UNICODE);
1945 memcpy(item, PyUnicode_AS_DATA(initial), n);
1946 self->allocated = Py_SIZE(self);
1947 }
1948 }
1949 if (it != NULL) {
1950 if (array_iter_extend((arrayobject *)a, it) == -1) {
1951 Py_DECREF(it);
1952 Py_DECREF(a);
1953 return NULL;
1954 }
1955 Py_DECREF(it);
1956 }
1957 return a;
1958 }
1959 }
1960 PyErr_SetString(PyExc_ValueError,
1961 "bad typecode (must be b, B, u, h, H, i, I, l, L, f or d)");
1962 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001963}
1964
Guido van Rossum778983b1993-02-19 15:55:02 +00001965
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001966PyDoc_STRVAR(module_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001967"This module defines an object type which can efficiently represent\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001968an array of basic values: characters, integers, floating point\n\
1969numbers. Arrays are sequence types and behave very much like lists,\n\
1970except that the type of objects stored in them is constrained. The\n\
1971type is specified at object creation time by using a type code, which\n\
1972is a single character. The following type codes are defined:\n\
1973\n\
1974 Type code C Type Minimum size in bytes \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001975 'b' signed integer 1 \n\
1976 'B' unsigned integer 1 \n\
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00001977 'u' Unicode character 2 (see note) \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001978 'h' signed integer 2 \n\
1979 'H' unsigned integer 2 \n\
1980 'i' signed integer 2 \n\
1981 'I' unsigned integer 2 \n\
1982 'l' signed integer 4 \n\
1983 'L' unsigned integer 4 \n\
1984 'f' floating point 4 \n\
1985 'd' floating point 8 \n\
1986\n\
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00001987NOTE: The 'u' typecode corresponds to Python's unicode character. On \n\
1988narrow builds this is 2-bytes on wide builds this is 4-bytes.\n\
1989\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001990The constructor is:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001991\n\
1992array(typecode [, initializer]) -- create a new array\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001993");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001994
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001995PyDoc_STRVAR(arraytype_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001996"array(typecode [, initializer]) -> array\n\
1997\n\
1998Return a new array whose items are restricted by typecode, and\n\
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00001999initialized from the optional initializer value, which must be a list,\n\
2000string. or iterable over elements of the appropriate type.\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002001\n\
2002Arrays represent basic values and behave very much like lists, except\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002003the type of objects stored in them is constrained.\n\
2004\n\
2005Methods:\n\
2006\n\
2007append() -- append a new item to the end of the array\n\
2008buffer_info() -- return information giving the current memory info\n\
2009byteswap() -- byteswap all the items of the array\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002010count() -- return number of occurrences of an object\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00002011extend() -- extend array by appending multiple elements from an iterable\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002012fromfile() -- read items from a file object\n\
2013fromlist() -- append items from the list\n\
2014fromstring() -- append items from the string\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002015index() -- return index of first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002016insert() -- insert a new item into the array at a provided position\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00002017pop() -- remove and return item (default last)\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002018remove() -- remove first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002019reverse() -- reverse the order of the items in the array\n\
2020tofile() -- write all items to a file object\n\
2021tolist() -- return the array converted to an ordinary list\n\
2022tostring() -- return the array converted to a string\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002023\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002024Attributes:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002025\n\
2026typecode -- the typecode character used to create the array\n\
2027itemsize -- the length in bytes of one array item\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002028");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002029
Raymond Hettinger625812f2003-01-07 01:58:52 +00002030static PyObject *array_iter(arrayobject *ao);
2031
Tim Peters0c322792002-07-17 16:49:03 +00002032static PyTypeObject Arraytype = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002033 PyVarObject_HEAD_INIT(NULL, 0)
2034 "array.array",
2035 sizeof(arrayobject),
2036 0,
2037 (destructor)array_dealloc, /* tp_dealloc */
2038 0, /* tp_print */
2039 0, /* tp_getattr */
2040 0, /* tp_setattr */
2041 0, /* tp_reserved */
2042 (reprfunc)array_repr, /* tp_repr */
2043 0, /* tp_as_number*/
2044 &array_as_sequence, /* tp_as_sequence*/
2045 &array_as_mapping, /* tp_as_mapping*/
2046 0, /* tp_hash */
2047 0, /* tp_call */
2048 0, /* tp_str */
2049 PyObject_GenericGetAttr, /* tp_getattro */
2050 0, /* tp_setattro */
2051 &array_as_buffer, /* tp_as_buffer*/
2052 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2053 arraytype_doc, /* tp_doc */
2054 0, /* tp_traverse */
2055 0, /* tp_clear */
2056 array_richcompare, /* tp_richcompare */
2057 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
2058 (getiterfunc)array_iter, /* tp_iter */
2059 0, /* tp_iternext */
2060 array_methods, /* tp_methods */
2061 0, /* tp_members */
2062 array_getsets, /* tp_getset */
2063 0, /* tp_base */
2064 0, /* tp_dict */
2065 0, /* tp_descr_get */
2066 0, /* tp_descr_set */
2067 0, /* tp_dictoffset */
2068 0, /* tp_init */
2069 PyType_GenericAlloc, /* tp_alloc */
2070 array_new, /* tp_new */
2071 PyObject_Del, /* tp_free */
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002072};
2073
Raymond Hettinger625812f2003-01-07 01:58:52 +00002074
2075/*********************** Array Iterator **************************/
2076
2077typedef struct {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002078 PyObject_HEAD
2079 Py_ssize_t index;
2080 arrayobject *ao;
2081 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002082} arrayiterobject;
2083
2084static PyTypeObject PyArrayIter_Type;
2085
2086#define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
2087
2088static PyObject *
2089array_iter(arrayobject *ao)
2090{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002091 arrayiterobject *it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002092
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002093 if (!array_Check(ao)) {
2094 PyErr_BadInternalCall();
2095 return NULL;
2096 }
Raymond Hettinger625812f2003-01-07 01:58:52 +00002097
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002098 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
2099 if (it == NULL)
2100 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002101
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002102 Py_INCREF(ao);
2103 it->ao = ao;
2104 it->index = 0;
2105 it->getitem = ao->ob_descr->getitem;
2106 PyObject_GC_Track(it);
2107 return (PyObject *)it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002108}
2109
2110static PyObject *
Raymond Hettinger625812f2003-01-07 01:58:52 +00002111arrayiter_next(arrayiterobject *it)
2112{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002113 assert(PyArrayIter_Check(it));
2114 if (it->index < Py_SIZE(it->ao))
2115 return (*it->getitem)(it->ao, it->index++);
2116 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002117}
2118
2119static void
2120arrayiter_dealloc(arrayiterobject *it)
2121{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002122 PyObject_GC_UnTrack(it);
2123 Py_XDECREF(it->ao);
2124 PyObject_GC_Del(it);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002125}
2126
2127static int
2128arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2129{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002130 Py_VISIT(it->ao);
2131 return 0;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002132}
2133
2134static PyTypeObject PyArrayIter_Type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002135 PyVarObject_HEAD_INIT(NULL, 0)
2136 "arrayiterator", /* tp_name */
2137 sizeof(arrayiterobject), /* tp_basicsize */
2138 0, /* tp_itemsize */
2139 /* methods */
2140 (destructor)arrayiter_dealloc, /* tp_dealloc */
2141 0, /* tp_print */
2142 0, /* tp_getattr */
2143 0, /* tp_setattr */
2144 0, /* tp_reserved */
2145 0, /* tp_repr */
2146 0, /* tp_as_number */
2147 0, /* tp_as_sequence */
2148 0, /* tp_as_mapping */
2149 0, /* tp_hash */
2150 0, /* tp_call */
2151 0, /* tp_str */
2152 PyObject_GenericGetAttr, /* tp_getattro */
2153 0, /* tp_setattro */
2154 0, /* tp_as_buffer */
2155 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2156 0, /* tp_doc */
2157 (traverseproc)arrayiter_traverse, /* tp_traverse */
2158 0, /* tp_clear */
2159 0, /* tp_richcompare */
2160 0, /* tp_weaklistoffset */
2161 PyObject_SelfIter, /* tp_iter */
2162 (iternextfunc)arrayiter_next, /* tp_iternext */
2163 0, /* tp_methods */
Raymond Hettinger625812f2003-01-07 01:58:52 +00002164};
2165
2166
2167/*********************** Install Module **************************/
2168
Martin v. Löwis99866332002-03-01 10:27:01 +00002169/* No functions in array module. */
2170static PyMethodDef a_methods[] = {
2171 {NULL, NULL, 0, NULL} /* Sentinel */
2172};
2173
Martin v. Löwis1a214512008-06-11 05:26:20 +00002174static struct PyModuleDef arraymodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002175 PyModuleDef_HEAD_INIT,
2176 "array",
2177 module_doc,
2178 -1,
2179 a_methods,
2180 NULL,
2181 NULL,
2182 NULL,
2183 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002184};
2185
Martin v. Löwis99866332002-03-01 10:27:01 +00002186
Mark Hammondfe51c6d2002-08-02 02:27:13 +00002187PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002188PyInit_array(void)
Guido van Rossum778983b1993-02-19 15:55:02 +00002189{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002190 PyObject *m;
2191 PyObject *typecodes;
2192 Py_ssize_t size = 0;
2193 register Py_UNICODE *p;
2194 struct arraydescr *descr;
Fred Drake0d40ba42000-02-04 20:33:49 +00002195
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002196 if (PyType_Ready(&Arraytype) < 0)
2197 return NULL;
2198 Py_TYPE(&PyArrayIter_Type) = &PyType_Type;
2199 m = PyModule_Create(&arraymodule);
2200 if (m == NULL)
2201 return NULL;
Fred Drakef4e34842002-04-01 03:45:06 +00002202
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002203 Py_INCREF((PyObject *)&Arraytype);
2204 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
2205 Py_INCREF((PyObject *)&Arraytype);
2206 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00002207
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002208 for (descr=descriptors; descr->typecode != '\0'; descr++) {
2209 size++;
2210 }
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00002211
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002212 typecodes = PyUnicode_FromStringAndSize(NULL, size);
2213 p = PyUnicode_AS_UNICODE(typecodes);
2214 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2215 *p++ = (char)descr->typecode;
2216 }
2217
2218 PyModule_AddObject(m, "typecodes", (PyObject *)typecodes);
2219
2220 if (PyErr_Occurred()) {
2221 Py_DECREF(m);
2222 m = NULL;
2223 }
2224 return m;
Guido van Rossum778983b1993-02-19 15:55:02 +00002225}