blob: f6f597a046cdcf33fd9077576f3d05c9089dd797 [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 */
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000013#ifdef HAVE_SYS_TYPES_H
Antoine Pitrouc83ea132010-05-09 14:46:46 +000014#include <sys/types.h> /* For size_t */
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +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 Pitrouc83ea132010-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 Rossum778983b1993-02-19 15:55:02 +000029};
30
31typedef struct arrayobject {
Antoine Pitrouc83ea132010-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 Rossum778983b1993-02-19 15:55:02 +000037} arrayobject;
38
Jeremy Hylton938ace62002-07-17 16:30:39 +000039static PyTypeObject Arraytype;
Guido van Rossum778983b1993-02-19 15:55:02 +000040
Martin v. Löwis99866332002-03-01 10:27:01 +000041#define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
Christian Heimese93237d2007-12-19 02:37:44 +000042#define array_CheckExact(op) (Py_TYPE(op) == &Arraytype)
Guido van Rossum778983b1993-02-19 15:55:02 +000043
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000044static int
Martin v. Löwis18e16552006-02-15 17:27:45 +000045array_resize(arrayobject *self, Py_ssize_t newsize)
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000046{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000047 char *items;
48 size_t _new_size;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000049
Antoine Pitrouc83ea132010-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 Hettinger6e2ee862004-03-14 04:37:50 +000054
Antoine Pitrouc83ea132010-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 Hettinger6e2ee862004-03-14 04:37:50 +000061
Antoine Pitrouc83ea132010-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 Hettinger6e2ee862004-03-14 04:37:50 +000073
Antoine Pitrouc83ea132010-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 Hettinger6e2ee862004-03-14 04:37:50 +000090}
91
Tim Petersbb307342000-09-10 05:22:54 +000092/****************************************************************************
93Get and Set functions for each type.
94A Get function takes an arrayobject* and an integer index, returning the
95array value at that index wrapped in an appropriate PyObject*.
96A Set function takes an arrayobject, integer index, and PyObject*; sets
97the array value at that index to the raw C data extracted from the PyObject*,
98and returns 0 if successful, else nonzero on failure (PyObject* not of an
99appropriate type or value).
100Note that the basic Get and Set functions do NOT check that the index is
101in bounds; that's the responsibility of the caller.
102****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +0000103
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000104static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000105c_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000106{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000107 return PyString_FromStringAndSize(&((char *)ap->ob_item)[i], 1);
Guido van Rossum778983b1993-02-19 15:55:02 +0000108}
109
110static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000111c_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000112{
Antoine Pitrouc83ea132010-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 Rossum778983b1993-02-19 15:55:02 +0000119}
120
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000121static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000122b_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000123{
Antoine Pitrouc83ea132010-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 Rossum778983b1993-02-19 15:55:02 +0000128}
129
130static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000131b_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000132{
Antoine Pitrouc83ea132010-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 Rossum778983b1993-02-19 15:55:02 +0000152}
153
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000154static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000155BB_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000156{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000157 long x = ((unsigned char *)ap->ob_item)[i];
158 return PyInt_FromLong(x);
Guido van Rossum549ab711997-01-03 19:09:47 +0000159}
160
Fred Drake541dc3b2000-06-28 17:49:30 +0000161static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000162BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000163{
Antoine Pitrouc83ea132010-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 Drake541dc3b2000-06-28 17:49:30 +0000171}
Guido van Rossum549ab711997-01-03 19:09:47 +0000172
Martin v. Löwis99866332002-03-01 10:27:01 +0000173#ifdef Py_USING_UNICODE
174static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000175u_getitem(arrayobject *ap, Py_ssize_t i)
Martin v. Löwis99866332002-03-01 10:27:01 +0000176{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000177 return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1);
Martin v. Löwis99866332002-03-01 10:27:01 +0000178}
179
180static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000181u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Martin v. Löwis99866332002-03-01 10:27:01 +0000182{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000183 Py_UNICODE *p;
184 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +0000185
Antoine Pitrouc83ea132010-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öwis99866332002-03-01 10:27:01 +0000196}
197#endif
198
Guido van Rossum549ab711997-01-03 19:09:47 +0000199static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000200h_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000201{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000202 return PyInt_FromLong((long) ((short *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000203}
204
205static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000206h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000207{
Antoine Pitrouc83ea132010-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 Rossum778983b1993-02-19 15:55:02 +0000215}
216
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000217static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000218HH_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000219{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000220 return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000221}
222
Fred Drake541dc3b2000-06-28 17:49:30 +0000223static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000224HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000225{
Antoine Pitrouc83ea132010-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 Drake541dc3b2000-06-28 17:49:30 +0000244}
Guido van Rossum549ab711997-01-03 19:09:47 +0000245
246static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000247i_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossume77a7571993-11-03 15:01:26 +0000248{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000249 return PyInt_FromLong((long) ((int *)ap->ob_item)[i]);
Guido van Rossume77a7571993-11-03 15:01:26 +0000250}
251
252static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000253i_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossume77a7571993-11-03 15:01:26 +0000254{
Antoine Pitrouc83ea132010-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 Rossume77a7571993-11-03 15:01:26 +0000262}
263
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000264static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000265II_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000266{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000267 return PyLong_FromUnsignedLong(
268 (unsigned long) ((unsigned int *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000269}
270
271static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000272II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000273{
Antoine Pitrouc83ea132010-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 Petersbb307342000-09-10 05:22:54 +0000290
Antoine Pitrouc83ea132010-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 Drake541dc3b2000-06-28 17:49:30 +0000297
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000298 if (i >= 0)
299 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
300 return 0;
Guido van Rossum549ab711997-01-03 19:09:47 +0000301}
302
303static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000304l_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000305{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000306 return PyInt_FromLong(((long *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000307}
308
309static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000310l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000311{
Antoine Pitrouc83ea132010-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 Rossum778983b1993-02-19 15:55:02 +0000318}
319
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000320static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000321LL_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000322{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000323 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000324}
325
326static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000327LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000328{
Antoine Pitrouc83ea132010-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 Petersbb307342000-09-10 05:22:54 +0000345
Antoine Pitrouc83ea132010-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 Petersbb307342000-09-10 05:22:54 +0000352
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000353 if (i >= 0)
354 ((unsigned long *)ap->ob_item)[i] = x;
355 return 0;
Guido van Rossum549ab711997-01-03 19:09:47 +0000356}
357
358static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000359f_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000360{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000361 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000362}
363
364static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000365f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000366{
Antoine Pitrouc83ea132010-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 Rossum778983b1993-02-19 15:55:02 +0000373}
374
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000375static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000376d_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000377{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000378 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000379}
380
381static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000382d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000383{
Antoine Pitrouc83ea132010-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 Rossum778983b1993-02-19 15:55:02 +0000390}
391
392/* Description of types */
Guido van Rossum234f9421993-06-17 12:35:49 +0000393static struct arraydescr descriptors[] = {
Antoine Pitrouc83ea132010-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öwis99866332002-03-01 10:27:01 +0000397#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000398 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem},
Martin v. Löwis99866332002-03-01 10:27:01 +0000399#endif
Antoine Pitrouc83ea132010-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 Rossum778983b1993-02-19 15:55:02 +0000409};
Tim Petersbb307342000-09-10 05:22:54 +0000410
411/****************************************************************************
412Implementations of array object methods.
413****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +0000414
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000415static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000416newarrayobject(PyTypeObject *type, Py_ssize_t size, struct arraydescr *descr)
Guido van Rossum778983b1993-02-19 15:55:02 +0000417{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000418 arrayobject *op;
419 size_t nbytes;
Martin v. Löwis99866332002-03-01 10:27:01 +0000420
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000421 if (size < 0) {
422 PyErr_BadInternalCall();
423 return NULL;
424 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000425
Antoine Pitrouc83ea132010-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 Rossum778983b1993-02-19 15:55:02 +0000450}
451
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000452static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000453getarrayitem(PyObject *op, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000454{
Antoine Pitrouc83ea132010-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 Rossum778983b1993-02-19 15:55:02 +0000460}
461
462static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000463ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000464{
Antoine Pitrouc83ea132010-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 Hettinger6e2ee862004-03-14 04:37:50 +0000473
Antoine Pitrouc83ea132010-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 Rossum778983b1993-02-19 15:55:02 +0000490}
491
Guido van Rossum778983b1993-02-19 15:55:02 +0000492/* Methods */
493
494static void
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000495array_dealloc(arrayobject *op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000496{
Antoine Pitrouc83ea132010-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 Rossum778983b1993-02-19 15:55:02 +0000502}
503
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000504static PyObject *
505array_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000506{
Antoine Pitrouc83ea132010-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 Rossum9d19cb82001-01-18 01:02:55 +0000512
Antoine Pitrouc83ea132010-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 Rossum9d19cb82001-01-18 01:02:55 +0000517
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000518 va = (arrayobject *)v;
519 wa = (arrayobject *)w;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000520
Antoine Pitrouc83ea132010-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 Rossum9d19cb82001-01-18 01:02:55 +0000530
Antoine Pitrouc83ea132010-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 Rossum9d19cb82001-01-18 01:02:55 +0000549
Antoine Pitrouc83ea132010-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 Rossum9d19cb82001-01-18 01:02:55 +0000571
Antoine Pitrouc83ea132010-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 Rossum778983b1993-02-19 15:55:02 +0000588}
589
Martin v. Löwis18e16552006-02-15 17:27:45 +0000590static Py_ssize_t
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000591array_length(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +0000592{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000593 return Py_SIZE(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000594}
595
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000596static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000597array_item(arrayobject *a, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000598{
Antoine Pitrouc83ea132010-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 Rossum778983b1993-02-19 15:55:02 +0000604}
605
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000606static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000607array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000608{
Antoine Pitrouc83ea132010-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;
Martin Panter6eec8782016-09-07 11:04:41 +0000623 if (ihigh > ilow) {
624 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
625 (ihigh-ilow) * a->ob_descr->itemsize);
626 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000627 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000628}
629
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000630static PyObject *
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000631array_copy(arrayobject *a, PyObject *unused)
632{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000633 return array_slice(a, 0, Py_SIZE(a));
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000634}
635
636PyDoc_STRVAR(copy_doc,
637"copy(array)\n\
638\n\
639 Return a copy of the array.");
640
641static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000642array_concat(arrayobject *a, PyObject *bb)
Guido van Rossum778983b1993-02-19 15:55:02 +0000643{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000644 Py_ssize_t size;
645 arrayobject *np;
646 if (!array_Check(bb)) {
647 PyErr_Format(PyExc_TypeError,
648 "can only append array (not \"%.200s\") to array",
649 Py_TYPE(bb)->tp_name);
650 return NULL;
651 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000652#define b ((arrayobject *)bb)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000653 if (a->ob_descr != b->ob_descr) {
654 PyErr_BadArgument();
655 return NULL;
656 }
657 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) {
658 return PyErr_NoMemory();
659 }
660 size = Py_SIZE(a) + Py_SIZE(b);
661 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
662 if (np == NULL) {
663 return NULL;
664 }
Martin Panter6eec8782016-09-07 11:04:41 +0000665 if (Py_SIZE(a) > 0) {
666 memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize);
667 }
668 if (Py_SIZE(b) > 0) {
669 memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize,
670 b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
671 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000672 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000673#undef b
674}
675
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000676static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000677array_repeat(arrayobject *a, Py_ssize_t n)
Guido van Rossum778983b1993-02-19 15:55:02 +0000678{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000679 Py_ssize_t i;
680 Py_ssize_t size;
681 arrayobject *np;
682 char *p;
683 Py_ssize_t nbytes;
684 if (n < 0)
685 n = 0;
686 if ((Py_SIZE(a) != 0) && (n > PY_SSIZE_T_MAX / Py_SIZE(a))) {
687 return PyErr_NoMemory();
688 }
689 size = Py_SIZE(a) * n;
690 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
691 if (np == NULL)
692 return NULL;
Martin Panter6eec8782016-09-07 11:04:41 +0000693 if (size == 0)
694 return (PyObject *)np;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000695 p = np->ob_item;
696 nbytes = Py_SIZE(a) * a->ob_descr->itemsize;
697 for (i = 0; i < n; i++) {
698 memcpy(p, a->ob_item, nbytes);
699 p += nbytes;
700 }
701 return (PyObject *) np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000702}
703
704static int
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000705array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000706{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000707 char *item;
708 Py_ssize_t n; /* Size of replacement array */
709 Py_ssize_t d; /* Change in size */
Guido van Rossum778983b1993-02-19 15:55:02 +0000710#define b ((arrayobject *)v)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000711 if (v == NULL)
712 n = 0;
713 else if (array_Check(v)) {
714 n = Py_SIZE(b);
715 if (a == b) {
716 /* Special case "a[i:j] = a" -- copy b first */
717 int ret;
718 v = array_slice(b, 0, n);
719 if (!v)
720 return -1;
721 ret = array_ass_slice(a, ilow, ihigh, v);
722 Py_DECREF(v);
723 return ret;
724 }
725 if (b->ob_descr != a->ob_descr) {
726 PyErr_BadArgument();
727 return -1;
728 }
729 }
730 else {
731 PyErr_Format(PyExc_TypeError,
732 "can only assign array (not \"%.200s\") to array slice",
733 Py_TYPE(v)->tp_name);
734 return -1;
735 }
736 if (ilow < 0)
737 ilow = 0;
738 else if (ilow > Py_SIZE(a))
739 ilow = Py_SIZE(a);
740 if (ihigh < 0)
741 ihigh = 0;
742 if (ihigh < ilow)
743 ihigh = ilow;
744 else if (ihigh > Py_SIZE(a))
745 ihigh = Py_SIZE(a);
746 item = a->ob_item;
747 d = n - (ihigh-ilow);
748 if (d < 0) { /* Delete -d items */
749 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
750 item + ihigh*a->ob_descr->itemsize,
751 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
752 Py_SIZE(a) += d;
753 PyMem_RESIZE(item, char, Py_SIZE(a)*a->ob_descr->itemsize);
754 /* Can't fail */
755 a->ob_item = item;
756 a->allocated = Py_SIZE(a);
757 }
758 else if (d > 0) { /* Insert d items */
759 PyMem_RESIZE(item, char,
760 (Py_SIZE(a) + d)*a->ob_descr->itemsize);
761 if (item == NULL) {
762 PyErr_NoMemory();
763 return -1;
764 }
765 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
766 item + ihigh*a->ob_descr->itemsize,
767 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
768 a->ob_item = item;
769 Py_SIZE(a) += d;
770 a->allocated = Py_SIZE(a);
771 }
772 if (n > 0)
773 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
774 n*b->ob_descr->itemsize);
775 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000776#undef b
777}
778
779static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000780array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000781{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000782 if (i < 0 || i >= Py_SIZE(a)) {
783 PyErr_SetString(PyExc_IndexError,
784 "array assignment index out of range");
785 return -1;
786 }
787 if (v == NULL)
788 return array_ass_slice(a, i, i+1, v);
789 return (*a->ob_descr->setitem)(a, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000790}
791
792static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000793setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000794{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000795 assert(array_Check(a));
796 return array_ass_item((arrayobject *)a, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000797}
798
Martin v. Löwis99866332002-03-01 10:27:01 +0000799static int
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000800array_iter_extend(arrayobject *self, PyObject *bb)
801{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000802 PyObject *it, *v;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000803
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000804 it = PyObject_GetIter(bb);
805 if (it == NULL)
806 return -1;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000807
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000808 while ((v = PyIter_Next(it)) != NULL) {
Mark Dickinson5e684332010-08-06 09:44:48 +0000809 if (ins1(self, Py_SIZE(self), v) != 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000810 Py_DECREF(v);
811 Py_DECREF(it);
812 return -1;
813 }
814 Py_DECREF(v);
815 }
816 Py_DECREF(it);
817 if (PyErr_Occurred())
818 return -1;
819 return 0;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000820}
821
822static int
Martin v. Löwis99866332002-03-01 10:27:01 +0000823array_do_extend(arrayobject *self, PyObject *bb)
824{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000825 Py_ssize_t size;
826 char *old_item;
Martin v. Löwis99866332002-03-01 10:27:01 +0000827
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000828 if (!array_Check(bb))
829 return array_iter_extend(self, bb);
Martin v. Löwis99866332002-03-01 10:27:01 +0000830#define b ((arrayobject *)bb)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000831 if (self->ob_descr != b->ob_descr) {
832 PyErr_SetString(PyExc_TypeError,
833 "can only extend with array of same kind");
834 return -1;
835 }
836 if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) ||
837 ((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
838 PyErr_NoMemory();
839 return -1;
840 }
841 size = Py_SIZE(self) + Py_SIZE(b);
842 old_item = self->ob_item;
843 PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize);
844 if (self->ob_item == NULL) {
845 self->ob_item = old_item;
846 PyErr_NoMemory();
847 return -1;
848 }
Martin Panter6eec8782016-09-07 11:04:41 +0000849 if (Py_SIZE(b) > 0) {
850 memcpy(self->ob_item + Py_SIZE(self)*self->ob_descr->itemsize,
851 b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
852 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000853 Py_SIZE(self) = size;
854 self->allocated = size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000855
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000856 return 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000857#undef b
858}
859
860static PyObject *
861array_inplace_concat(arrayobject *self, PyObject *bb)
862{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000863 if (!array_Check(bb)) {
864 PyErr_Format(PyExc_TypeError,
865 "can only extend array with array (not \"%.200s\")",
866 Py_TYPE(bb)->tp_name);
867 return NULL;
868 }
869 if (array_do_extend(self, bb) == -1)
870 return NULL;
871 Py_INCREF(self);
872 return (PyObject *)self;
Martin v. Löwis99866332002-03-01 10:27:01 +0000873}
874
875static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000876array_inplace_repeat(arrayobject *self, Py_ssize_t n)
Martin v. Löwis99866332002-03-01 10:27:01 +0000877{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000878 char *items, *p;
879 Py_ssize_t size, i;
Martin v. Löwis99866332002-03-01 10:27:01 +0000880
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000881 if (Py_SIZE(self) > 0) {
882 if (n < 0)
883 n = 0;
884 items = self->ob_item;
885 if ((self->ob_descr->itemsize != 0) &&
886 (Py_SIZE(self) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
887 return PyErr_NoMemory();
888 }
889 size = Py_SIZE(self) * self->ob_descr->itemsize;
890 if (n == 0) {
891 PyMem_FREE(items);
892 self->ob_item = NULL;
893 Py_SIZE(self) = 0;
894 self->allocated = 0;
895 }
896 else {
897 if (size > PY_SSIZE_T_MAX / n) {
898 return PyErr_NoMemory();
899 }
900 PyMem_RESIZE(items, char, n * size);
901 if (items == NULL)
902 return PyErr_NoMemory();
903 p = items;
904 for (i = 1; i < n; i++) {
905 p += size;
906 memcpy(p, items, size);
907 }
908 self->ob_item = items;
909 Py_SIZE(self) *= n;
910 self->allocated = Py_SIZE(self);
911 }
912 }
913 Py_INCREF(self);
914 return (PyObject *)self;
Martin v. Löwis99866332002-03-01 10:27:01 +0000915}
916
917
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000918static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000919ins(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000920{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000921 if (ins1(self, where, v) != 0)
922 return NULL;
923 Py_INCREF(Py_None);
924 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000925}
926
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000927static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000928array_count(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000929{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000930 Py_ssize_t count = 0;
931 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000932
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000933 for (i = 0; i < Py_SIZE(self); i++) {
934 PyObject *selfi = getarrayitem((PyObject *)self, i);
935 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
936 Py_DECREF(selfi);
937 if (cmp > 0)
938 count++;
939 else if (cmp < 0)
940 return NULL;
941 }
942 return PyInt_FromSsize_t(count);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000943}
944
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000945PyDoc_STRVAR(count_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000946"count(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000947\n\
Mark Dickinson3e4caeb2009-02-21 20:27:01 +0000948Return number of occurrences of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000949
950static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000951array_index(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000952{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000953 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000954
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000955 for (i = 0; i < Py_SIZE(self); i++) {
956 PyObject *selfi = getarrayitem((PyObject *)self, i);
957 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
958 Py_DECREF(selfi);
959 if (cmp > 0) {
960 return PyInt_FromLong((long)i);
961 }
962 else if (cmp < 0)
963 return NULL;
964 }
965 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
966 return NULL;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000967}
968
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000969PyDoc_STRVAR(index_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000970"index(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000971\n\
Mark Dickinson3e4caeb2009-02-21 20:27:01 +0000972Return index of first occurrence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000973
Raymond Hettinger625812f2003-01-07 01:58:52 +0000974static int
975array_contains(arrayobject *self, PyObject *v)
976{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000977 Py_ssize_t i;
978 int cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +0000979
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000980 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) {
981 PyObject *selfi = getarrayitem((PyObject *)self, i);
982 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
983 Py_DECREF(selfi);
984 }
985 return cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +0000986}
987
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000988static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000989array_remove(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000990{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000991 int i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000992
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000993 for (i = 0; i < Py_SIZE(self); i++) {
994 PyObject *selfi = getarrayitem((PyObject *)self,i);
995 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
996 Py_DECREF(selfi);
997 if (cmp > 0) {
998 if (array_ass_slice(self, i, i+1,
999 (PyObject *)NULL) != 0)
1000 return NULL;
1001 Py_INCREF(Py_None);
1002 return Py_None;
1003 }
1004 else if (cmp < 0)
1005 return NULL;
1006 }
1007 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
1008 return NULL;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001009}
1010
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001011PyDoc_STRVAR(remove_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001012"remove(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001013\n\
Mark Dickinson3e4caeb2009-02-21 20:27:01 +00001014Remove the first occurrence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001015
1016static PyObject *
1017array_pop(arrayobject *self, PyObject *args)
1018{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001019 Py_ssize_t i = -1;
1020 PyObject *v;
1021 if (!PyArg_ParseTuple(args, "|n:pop", &i))
1022 return NULL;
1023 if (Py_SIZE(self) == 0) {
1024 /* Special-case most common failure cause */
1025 PyErr_SetString(PyExc_IndexError, "pop from empty array");
1026 return NULL;
1027 }
1028 if (i < 0)
1029 i += Py_SIZE(self);
1030 if (i < 0 || i >= Py_SIZE(self)) {
1031 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1032 return NULL;
1033 }
1034 v = getarrayitem((PyObject *)self,i);
1035 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
1036 Py_DECREF(v);
1037 return NULL;
1038 }
1039 return v;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001040}
1041
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001042PyDoc_STRVAR(pop_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001043"pop([i])\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001044\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001045Return the i-th element and delete it from the array. i defaults to -1.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001046
1047static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001048array_extend(arrayobject *self, PyObject *bb)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001049{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001050 if (array_do_extend(self, bb) == -1)
1051 return NULL;
1052 Py_INCREF(Py_None);
1053 return Py_None;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001054}
1055
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001056PyDoc_STRVAR(extend_doc,
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001057"extend(array or iterable)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001058\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001059 Append items to the end of the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001060
1061static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001062array_insert(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001063{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001064 Py_ssize_t i;
1065 PyObject *v;
1066 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
1067 return NULL;
1068 return ins(self, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001069}
1070
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001071PyDoc_STRVAR(insert_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001072"insert(i,x)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001073\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001074Insert a new item x into the array before position i.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001075
1076
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001077static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001078array_buffer_info(arrayobject *self, PyObject *unused)
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001079{
Serhiy Storchaka0cc5a2b2016-06-24 00:00:32 +03001080 PyObject *retval = NULL, *v;
1081
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001082 retval = PyTuple_New(2);
1083 if (!retval)
1084 return NULL;
Fred Drake541dc3b2000-06-28 17:49:30 +00001085
Serhiy Storchaka0cc5a2b2016-06-24 00:00:32 +03001086 v = PyLong_FromVoidPtr(self->ob_item);
1087 if (v == NULL) {
1088 Py_DECREF(retval);
1089 return NULL;
1090 }
1091 PyTuple_SET_ITEM(retval, 0, v);
1092
Serhiy Storchakaff0d8752016-06-24 08:38:59 +03001093 v = PyInt_FromSsize_t(Py_SIZE(self));
Serhiy Storchaka0cc5a2b2016-06-24 00:00:32 +03001094 if (v == NULL) {
1095 Py_DECREF(retval);
1096 return NULL;
1097 }
1098 PyTuple_SET_ITEM(retval, 1, v);
Fred Drake541dc3b2000-06-28 17:49:30 +00001099
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001100 return retval;
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001101}
1102
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001103PyDoc_STRVAR(buffer_info_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001104"buffer_info() -> (address, length)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001105\n\
1106Return a tuple (address, length) giving the current memory address and\n\
Guido van Rossum702d08e2001-07-27 16:05:32 +00001107the length in items of the buffer used to hold array's contents\n\
1108The length should be multiplied by the itemsize attribute to calculate\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001109the buffer length in bytes.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001110
1111
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001112static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001113array_append(arrayobject *self, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +00001114{
Mark Dickinson5e684332010-08-06 09:44:48 +00001115 return ins(self, Py_SIZE(self), v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001116}
1117
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001118PyDoc_STRVAR(append_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001119"append(x)\n\
1120\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001121Append new value x to the end of the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001122
1123
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001124static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001125array_byteswap(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001126{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001127 char *p;
1128 Py_ssize_t i;
Fred Drakebf272981999-12-03 17:15:30 +00001129
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001130 switch (self->ob_descr->itemsize) {
1131 case 1:
1132 break;
1133 case 2:
1134 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) {
1135 char p0 = p[0];
1136 p[0] = p[1];
1137 p[1] = p0;
1138 }
1139 break;
1140 case 4:
1141 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 4) {
1142 char p0 = p[0];
1143 char p1 = p[1];
1144 p[0] = p[3];
1145 p[1] = p[2];
1146 p[2] = p1;
1147 p[3] = p0;
1148 }
1149 break;
1150 case 8:
1151 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) {
1152 char p0 = p[0];
1153 char p1 = p[1];
1154 char p2 = p[2];
1155 char p3 = p[3];
1156 p[0] = p[7];
1157 p[1] = p[6];
1158 p[2] = p[5];
1159 p[3] = p[4];
1160 p[4] = p3;
1161 p[5] = p2;
1162 p[6] = p1;
1163 p[7] = p0;
1164 }
1165 break;
1166 default:
1167 PyErr_SetString(PyExc_RuntimeError,
1168 "don't know how to byteswap this array type");
1169 return NULL;
1170 }
1171 Py_INCREF(Py_None);
1172 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001173}
1174
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001175PyDoc_STRVAR(byteswap_doc,
Fred Drakebf272981999-12-03 17:15:30 +00001176"byteswap()\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001177\n\
Fred Drakebf272981999-12-03 17:15:30 +00001178Byteswap 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 +000011794, or 8 bytes in size, RuntimeError is raised.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001180
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001181static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001182array_reverse(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001183{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001184 register Py_ssize_t itemsize = self->ob_descr->itemsize;
1185 register char *p, *q;
1186 /* little buffer to hold items while swapping */
1187 char tmp[256]; /* 8 is probably enough -- but why skimp */
1188 assert((size_t)itemsize <= sizeof(tmp));
Guido van Rossume77a7571993-11-03 15:01:26 +00001189
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001190 if (Py_SIZE(self) > 1) {
1191 for (p = self->ob_item,
1192 q = self->ob_item + (Py_SIZE(self) - 1)*itemsize;
1193 p < q;
1194 p += itemsize, q -= itemsize) {
1195 /* memory areas guaranteed disjoint, so memcpy
1196 * is safe (& memmove may be slower).
1197 */
1198 memcpy(tmp, p, itemsize);
1199 memcpy(p, q, itemsize);
1200 memcpy(q, tmp, itemsize);
1201 }
1202 }
Tim Petersbb307342000-09-10 05:22:54 +00001203
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001204 Py_INCREF(Py_None);
1205 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001206}
Guido van Rossume77a7571993-11-03 15:01:26 +00001207
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001208PyDoc_STRVAR(reverse_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001209"reverse()\n\
1210\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001211Reverse the order of the items in the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001212
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001213static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001214array_fromfile(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001215{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001216 PyObject *f;
1217 Py_ssize_t n;
1218 FILE *fp;
1219 if (!PyArg_ParseTuple(args, "On:fromfile", &f, &n))
1220 return NULL;
1221 fp = PyFile_AsFile(f);
1222 if (fp == NULL) {
1223 PyErr_SetString(PyExc_TypeError, "arg1 must be open file");
1224 return NULL;
1225 }
1226 if (n > 0) {
1227 char *item = self->ob_item;
1228 Py_ssize_t itemsize = self->ob_descr->itemsize;
1229 size_t nread;
1230 Py_ssize_t newlength;
1231 size_t newbytes;
1232 /* Be careful here about overflow */
1233 if ((newlength = Py_SIZE(self) + n) <= 0 ||
1234 (newbytes = newlength * itemsize) / itemsize !=
1235 (size_t)newlength)
1236 goto nomem;
1237 PyMem_RESIZE(item, char, newbytes);
1238 if (item == NULL) {
1239 nomem:
1240 PyErr_NoMemory();
1241 return NULL;
1242 }
1243 self->ob_item = item;
1244 Py_SIZE(self) += n;
1245 self->allocated = Py_SIZE(self);
1246 nread = fread(item + (Py_SIZE(self) - n) * itemsize,
1247 itemsize, n, fp);
1248 if (nread < (size_t)n) {
1249 Py_SIZE(self) -= (n - nread);
1250 PyMem_RESIZE(item, char, Py_SIZE(self)*itemsize);
1251 self->ob_item = item;
1252 self->allocated = Py_SIZE(self);
Antoine Pitrou7a7013e2010-07-21 16:47:28 +00001253 if (ferror(fp)) {
1254 PyErr_SetFromErrno(PyExc_IOError);
1255 clearerr(fp);
1256 }
1257 else {
1258 PyErr_SetString(PyExc_EOFError,
1259 "not enough items in file");
1260 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001261 return NULL;
1262 }
1263 }
1264 Py_INCREF(Py_None);
1265 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001266}
1267
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001268PyDoc_STRVAR(fromfile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001269"fromfile(f, n)\n\
1270\n\
1271Read n objects from the file object f and append them to the end of the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001272array. Also called as read.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001273
1274
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001275static PyObject *
Georg Brandl1e7c3752008-03-25 08:37:23 +00001276array_fromfile_as_read(arrayobject *self, PyObject *args)
1277{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001278 if (PyErr_WarnPy3k("array.read() not supported in 3.x; "
1279 "use array.fromfile()", 1) < 0)
1280 return NULL;
1281 return array_fromfile(self, args);
Georg Brandl1e7c3752008-03-25 08:37:23 +00001282}
1283
1284
1285static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001286array_tofile(arrayobject *self, PyObject *f)
Guido van Rossum778983b1993-02-19 15:55:02 +00001287{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001288 FILE *fp;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001289
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001290 fp = PyFile_AsFile(f);
1291 if (fp == NULL) {
1292 PyErr_SetString(PyExc_TypeError, "arg must be open file");
1293 return NULL;
1294 }
1295 if (self->ob_size > 0) {
1296 if (fwrite(self->ob_item, self->ob_descr->itemsize,
1297 self->ob_size, fp) != (size_t)self->ob_size) {
1298 PyErr_SetFromErrno(PyExc_IOError);
1299 clearerr(fp);
1300 return NULL;
1301 }
1302 }
1303 Py_INCREF(Py_None);
1304 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001305}
1306
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001307PyDoc_STRVAR(tofile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001308"tofile(f)\n\
1309\n\
1310Write all items (as machine values) to the file object f. Also called as\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001311write.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001312
1313
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001314static PyObject *
Georg Brandl1e7c3752008-03-25 08:37:23 +00001315array_tofile_as_write(arrayobject *self, PyObject *f)
1316{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001317 if (PyErr_WarnPy3k("array.write() not supported in 3.x; "
1318 "use array.tofile()", 1) < 0)
1319 return NULL;
1320 return array_tofile(self, f);
Georg Brandl1e7c3752008-03-25 08:37:23 +00001321}
1322
1323
1324static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001325array_fromlist(arrayobject *self, PyObject *list)
Guido van Rossum778983b1993-02-19 15:55:02 +00001326{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001327 Py_ssize_t n;
1328 Py_ssize_t itemsize = self->ob_descr->itemsize;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001329
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001330 if (!PyList_Check(list)) {
1331 PyErr_SetString(PyExc_TypeError, "arg must be list");
1332 return NULL;
1333 }
1334 n = PyList_Size(list);
1335 if (n > 0) {
1336 char *item = self->ob_item;
1337 Py_ssize_t i;
1338 PyMem_RESIZE(item, char, (Py_SIZE(self) + n) * itemsize);
1339 if (item == NULL) {
1340 PyErr_NoMemory();
1341 return NULL;
1342 }
1343 self->ob_item = item;
1344 Py_SIZE(self) += n;
1345 self->allocated = Py_SIZE(self);
1346 for (i = 0; i < n; i++) {
1347 PyObject *v = PyList_GetItem(list, i);
1348 if ((*self->ob_descr->setitem)(self,
1349 Py_SIZE(self) - n + i, v) != 0) {
1350 Py_SIZE(self) -= n;
1351 if (itemsize && (self->ob_size > PY_SSIZE_T_MAX / itemsize)) {
1352 return PyErr_NoMemory();
1353 }
1354 PyMem_RESIZE(item, char,
1355 Py_SIZE(self) * itemsize);
1356 self->ob_item = item;
1357 self->allocated = Py_SIZE(self);
1358 return NULL;
1359 }
1360 }
1361 }
1362 Py_INCREF(Py_None);
1363 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001364}
1365
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001366PyDoc_STRVAR(fromlist_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001367"fromlist(list)\n\
1368\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001369Append items to array from list.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001370
1371
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001372static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001373array_tolist(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001374{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001375 PyObject *list = PyList_New(Py_SIZE(self));
1376 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001377
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001378 if (list == NULL)
1379 return NULL;
1380 for (i = 0; i < Py_SIZE(self); i++) {
1381 PyObject *v = getarrayitem((PyObject *)self, i);
1382 if (v == NULL) {
1383 Py_DECREF(list);
1384 return NULL;
1385 }
1386 PyList_SetItem(list, i, v);
1387 }
1388 return list;
Guido van Rossum778983b1993-02-19 15:55:02 +00001389}
1390
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001391PyDoc_STRVAR(tolist_doc,
Guido van Rossumfc6aba51998-10-14 02:52:31 +00001392"tolist() -> list\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001393\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001394Convert array to an ordinary list with the same items.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001395
1396
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001397static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001398array_fromstring(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001399{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001400 char *str;
1401 Py_ssize_t n;
1402 int itemsize = self->ob_descr->itemsize;
1403 if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n))
1404 return NULL;
Serhiy Storchakacf74c192015-07-26 08:49:37 +03001405 if (str == self->ob_item) {
1406 PyErr_SetString(PyExc_ValueError,
1407 "array.fromstring(x): x cannot be self");
1408 return NULL;
1409 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001410 if (n % itemsize != 0) {
1411 PyErr_SetString(PyExc_ValueError,
1412 "string length not a multiple of item size");
1413 return NULL;
1414 }
1415 n = n / itemsize;
1416 if (n > 0) {
1417 char *item = self->ob_item;
1418 if ((n > PY_SSIZE_T_MAX - Py_SIZE(self)) ||
1419 ((Py_SIZE(self) + n) > PY_SSIZE_T_MAX / itemsize)) {
1420 return PyErr_NoMemory();
1421 }
1422 PyMem_RESIZE(item, char, (Py_SIZE(self) + n) * itemsize);
1423 if (item == NULL) {
1424 PyErr_NoMemory();
1425 return NULL;
1426 }
1427 self->ob_item = item;
1428 Py_SIZE(self) += n;
1429 self->allocated = Py_SIZE(self);
1430 memcpy(item + (Py_SIZE(self) - n) * itemsize,
1431 str, itemsize*n);
1432 }
1433 Py_INCREF(Py_None);
1434 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001435}
1436
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001437PyDoc_STRVAR(fromstring_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001438"fromstring(string)\n\
1439\n\
1440Appends items from the string, interpreting it as an array of machine\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001441values,as if it had been read from a file using the fromfile() method).");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001442
1443
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001444static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001445array_tostring(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001446{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001447 if (self->ob_size <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
1448 return PyString_FromStringAndSize(self->ob_item,
1449 Py_SIZE(self) * self->ob_descr->itemsize);
1450 } else {
1451 return PyErr_NoMemory();
1452 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001453}
1454
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001455PyDoc_STRVAR(tostring_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001456"tostring() -> string\n\
1457\n\
1458Convert the array to an array of machine values and return the string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001459representation.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001460
Martin v. Löwis99866332002-03-01 10:27:01 +00001461
1462
1463#ifdef Py_USING_UNICODE
1464static PyObject *
1465array_fromunicode(arrayobject *self, PyObject *args)
1466{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001467 Py_UNICODE *ustr;
1468 Py_ssize_t n;
Martin v. Löwis99866332002-03-01 10:27:01 +00001469
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001470 if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
1471 return NULL;
1472 if (self->ob_descr->typecode != 'u') {
1473 PyErr_SetString(PyExc_ValueError,
1474 "fromunicode() may only be called on "
1475 "type 'u' arrays");
1476 return NULL;
1477 }
1478 if (n > 0) {
1479 Py_UNICODE *item = (Py_UNICODE *) self->ob_item;
1480 if (Py_SIZE(self) > PY_SSIZE_T_MAX - n) {
1481 return PyErr_NoMemory();
1482 }
1483 PyMem_RESIZE(item, Py_UNICODE, Py_SIZE(self) + n);
1484 if (item == NULL) {
1485 PyErr_NoMemory();
1486 return NULL;
1487 }
1488 self->ob_item = (char *) item;
1489 Py_SIZE(self) += n;
1490 self->allocated = Py_SIZE(self);
1491 memcpy(item + Py_SIZE(self) - n,
1492 ustr, n * sizeof(Py_UNICODE));
1493 }
Martin v. Löwis99866332002-03-01 10:27:01 +00001494
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001495 Py_INCREF(Py_None);
1496 return Py_None;
Martin v. Löwis99866332002-03-01 10:27:01 +00001497}
1498
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001499PyDoc_STRVAR(fromunicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001500"fromunicode(ustr)\n\
1501\n\
1502Extends this array with data from the unicode string ustr.\n\
1503The array must be a type 'u' array; otherwise a ValueError\n\
1504is raised. Use array.fromstring(ustr.decode(...)) to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001505append Unicode data to an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001506
1507
1508static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001509array_tounicode(arrayobject *self, PyObject *unused)
Martin v. Löwis99866332002-03-01 10:27:01 +00001510{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001511 if (self->ob_descr->typecode != 'u') {
1512 PyErr_SetString(PyExc_ValueError,
1513 "tounicode() may only be called on type 'u' arrays");
1514 return NULL;
1515 }
1516 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_SIZE(self));
Martin v. Löwis99866332002-03-01 10:27:01 +00001517}
1518
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001519PyDoc_STRVAR(tounicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001520"tounicode() -> unicode\n\
1521\n\
1522Convert the array to a unicode string. The array must be\n\
1523a type 'u' array; otherwise a ValueError is raised. Use\n\
1524array.tostring().decode() to obtain a unicode string from\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001525an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001526
1527#endif /* Py_USING_UNICODE */
1528
Alexandre Vassalotti999ecc02009-07-15 18:19:47 +00001529static PyObject *
1530array_reduce(arrayobject *array)
1531{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001532 PyObject *dict, *result, *list;
Alexandre Vassalotti999ecc02009-07-15 18:19:47 +00001533
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001534 dict = PyObject_GetAttrString((PyObject *)array, "__dict__");
1535 if (dict == NULL) {
1536 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1537 return NULL;
1538 PyErr_Clear();
1539 dict = Py_None;
1540 Py_INCREF(dict);
1541 }
1542 /* Unlike in Python 3.x, we never use the more efficient memory
1543 * representation of an array for pickling. This is unfortunately
1544 * necessary to allow array objects to be unpickled by Python 3.x,
1545 * since str objects from 2.x are always decoded to unicode in
1546 * Python 3.x.
1547 */
1548 list = array_tolist(array, NULL);
1549 if (list == NULL) {
1550 Py_DECREF(dict);
1551 return NULL;
1552 }
1553 result = Py_BuildValue(
1554 "O(cO)O", Py_TYPE(array), array->ob_descr->typecode, list, dict);
1555 Py_DECREF(list);
1556 Py_DECREF(dict);
1557 return result;
Alexandre Vassalotti999ecc02009-07-15 18:19:47 +00001558}
1559
1560PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001561
1562static PyObject *
Meador Inge2d639d52012-08-10 22:05:45 -05001563array_sizeof(arrayobject *self, PyObject *unused)
1564{
1565 Py_ssize_t res;
Serhiy Storchakac06a6d02015-12-19 20:07:48 +02001566 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * self->ob_descr->itemsize;
Meador Inge2d639d52012-08-10 22:05:45 -05001567 return PyLong_FromSsize_t(res);
1568}
1569
1570PyDoc_STRVAR(sizeof_doc,
1571"__sizeof__() -> int\n\
1572\n\
1573Size of the array in memory, in bytes.");
1574
1575static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +00001576array_get_typecode(arrayobject *a, void *closure)
1577{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001578 char tc = a->ob_descr->typecode;
1579 return PyString_FromStringAndSize(&tc, 1);
Martin v. Löwis99866332002-03-01 10:27:01 +00001580}
1581
1582static PyObject *
1583array_get_itemsize(arrayobject *a, void *closure)
1584{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001585 return PyInt_FromLong((long)a->ob_descr->itemsize);
Martin v. Löwis99866332002-03-01 10:27:01 +00001586}
1587
1588static PyGetSetDef array_getsets [] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001589 {"typecode", (getter) array_get_typecode, NULL,
1590 "the typecode character used to create the array"},
1591 {"itemsize", (getter) array_get_itemsize, NULL,
1592 "the size, in bytes, of one array item"},
1593 {NULL}
Martin v. Löwis99866332002-03-01 10:27:01 +00001594};
1595
Martin v. Löwis111c1802008-06-13 07:47:47 +00001596static PyMethodDef array_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001597 {"append", (PyCFunction)array_append, METH_O,
1598 append_doc},
1599 {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS,
1600 buffer_info_doc},
1601 {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
1602 byteswap_doc},
1603 {"__copy__", (PyCFunction)array_copy, METH_NOARGS,
1604 copy_doc},
1605 {"count", (PyCFunction)array_count, METH_O,
1606 count_doc},
1607 {"__deepcopy__",(PyCFunction)array_copy, METH_O,
1608 copy_doc},
1609 {"extend", (PyCFunction)array_extend, METH_O,
1610 extend_doc},
1611 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
1612 fromfile_doc},
1613 {"fromlist", (PyCFunction)array_fromlist, METH_O,
1614 fromlist_doc},
1615 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
1616 fromstring_doc},
Martin v. Löwis99866332002-03-01 10:27:01 +00001617#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001618 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
1619 fromunicode_doc},
Martin v. Löwis99866332002-03-01 10:27:01 +00001620#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001621 {"index", (PyCFunction)array_index, METH_O,
1622 index_doc},
1623 {"insert", (PyCFunction)array_insert, METH_VARARGS,
1624 insert_doc},
1625 {"pop", (PyCFunction)array_pop, METH_VARARGS,
1626 pop_doc},
1627 {"read", (PyCFunction)array_fromfile_as_read, METH_VARARGS,
1628 fromfile_doc},
1629 {"__reduce__", (PyCFunction)array_reduce, METH_NOARGS,
1630 reduce_doc},
1631 {"remove", (PyCFunction)array_remove, METH_O,
1632 remove_doc},
1633 {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
1634 reverse_doc},
1635/* {"sort", (PyCFunction)array_sort, METH_VARARGS,
1636 sort_doc},*/
1637 {"tofile", (PyCFunction)array_tofile, METH_O,
1638 tofile_doc},
1639 {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
1640 tolist_doc},
1641 {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
1642 tostring_doc},
Martin v. Löwis99866332002-03-01 10:27:01 +00001643#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001644 {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
1645 tounicode_doc},
Martin v. Löwis99866332002-03-01 10:27:01 +00001646#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001647 {"write", (PyCFunction)array_tofile_as_write, METH_O,
1648 tofile_doc},
Meador Inge2d639d52012-08-10 22:05:45 -05001649 {"__sizeof__", (PyCFunction)array_sizeof, METH_NOARGS,
1650 sizeof_doc},
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001651 {NULL, NULL} /* sentinel */
Guido van Rossum778983b1993-02-19 15:55:02 +00001652};
1653
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001654static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001655array_repr(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +00001656{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001657 char buf[256], typecode;
1658 PyObject *s, *t, *v = NULL;
1659 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00001660
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001661 len = Py_SIZE(a);
1662 typecode = a->ob_descr->typecode;
1663 if (len == 0) {
1664 PyOS_snprintf(buf, sizeof(buf), "array('%c')", typecode);
1665 return PyString_FromString(buf);
1666 }
1667
1668 if (typecode == 'c')
1669 v = array_tostring(a, NULL);
Michael W. Hudson1755ad92002-05-13 10:14:59 +00001670#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001671 else if (typecode == 'u')
1672 v = array_tounicode(a, NULL);
Michael W. Hudson1755ad92002-05-13 10:14:59 +00001673#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001674 else
1675 v = array_tolist(a, NULL);
1676 t = PyObject_Repr(v);
1677 Py_XDECREF(v);
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001678
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001679 PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode);
1680 s = PyString_FromString(buf);
1681 PyString_ConcatAndDel(&s, t);
1682 PyString_ConcatAndDel(&s, PyString_FromString(")"));
1683 return s;
Guido van Rossum778983b1993-02-19 15:55:02 +00001684}
1685
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001686static PyObject*
1687array_subscr(arrayobject* self, PyObject* item)
1688{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001689 if (PyIndex_Check(item)) {
1690 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
1691 if (i==-1 && PyErr_Occurred()) {
1692 return NULL;
1693 }
1694 if (i < 0)
1695 i += Py_SIZE(self);
1696 return array_item(self, i);
1697 }
1698 else if (PySlice_Check(item)) {
1699 Py_ssize_t start, stop, step, slicelength, cur, i;
1700 PyObject* result;
1701 arrayobject* ar;
1702 int itemsize = self->ob_descr->itemsize;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001703
Serhiy Storchaka5e793212017-04-15 20:11:12 +03001704 if (_PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001705 return NULL;
1706 }
Serhiy Storchakae41390a2017-04-08 11:48:57 +03001707 slicelength = _PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
1708 step);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001709
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001710 if (slicelength <= 0) {
1711 return newarrayobject(&Arraytype, 0, self->ob_descr);
1712 }
1713 else if (step == 1) {
1714 PyObject *result = newarrayobject(&Arraytype,
1715 slicelength, self->ob_descr);
1716 if (result == NULL)
1717 return NULL;
1718 memcpy(((arrayobject *)result)->ob_item,
1719 self->ob_item + start * itemsize,
1720 slicelength * itemsize);
1721 return result;
1722 }
1723 else {
1724 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
1725 if (!result) return NULL;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001726
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001727 ar = (arrayobject*)result;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001728
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001729 for (cur = start, i = 0; i < slicelength;
1730 cur += step, i++) {
1731 memcpy(ar->ob_item + i*itemsize,
1732 self->ob_item + cur*itemsize,
1733 itemsize);
1734 }
1735
1736 return result;
1737 }
1738 }
1739 else {
1740 PyErr_SetString(PyExc_TypeError,
1741 "array indices must be integers");
1742 return NULL;
1743 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001744}
1745
1746static int
1747array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
1748{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001749 Py_ssize_t start, stop, step, slicelength, needed;
1750 arrayobject* other;
1751 int itemsize;
Thomas Wouters3ccec682007-08-28 15:28:19 +00001752
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001753 if (PyIndex_Check(item)) {
1754 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Mark Dickinson36ecd672010-01-29 17:11:39 +00001755
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001756 if (i == -1 && PyErr_Occurred())
1757 return -1;
1758 if (i < 0)
1759 i += Py_SIZE(self);
1760 if (i < 0 || i >= Py_SIZE(self)) {
1761 PyErr_SetString(PyExc_IndexError,
1762 "array assignment index out of range");
1763 return -1;
1764 }
1765 if (value == NULL) {
1766 /* Fall through to slice assignment */
1767 start = i;
1768 stop = i + 1;
1769 step = 1;
1770 slicelength = 1;
1771 }
1772 else
1773 return (*self->ob_descr->setitem)(self, i, value);
1774 }
1775 else if (PySlice_Check(item)) {
Serhiy Storchaka5e793212017-04-15 20:11:12 +03001776 if (_PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001777 return -1;
1778 }
Serhiy Storchakae41390a2017-04-08 11:48:57 +03001779 slicelength = _PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
1780 step);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001781 }
1782 else {
1783 PyErr_SetString(PyExc_TypeError,
1784 "array indices must be integer");
1785 return -1;
1786 }
1787 if (value == NULL) {
1788 other = NULL;
1789 needed = 0;
1790 }
1791 else if (array_Check(value)) {
1792 other = (arrayobject *)value;
1793 needed = Py_SIZE(other);
1794 if (self == other) {
1795 /* Special case "self[i:j] = self" -- copy self first */
1796 int ret;
1797 value = array_slice(other, 0, needed);
1798 if (value == NULL)
1799 return -1;
1800 ret = array_ass_subscr(self, item, value);
1801 Py_DECREF(value);
1802 return ret;
1803 }
1804 if (other->ob_descr != self->ob_descr) {
1805 PyErr_BadArgument();
1806 return -1;
1807 }
1808 }
1809 else {
1810 PyErr_Format(PyExc_TypeError,
1811 "can only assign array (not \"%.200s\") to array slice",
1812 Py_TYPE(value)->tp_name);
1813 return -1;
1814 }
1815 itemsize = self->ob_descr->itemsize;
1816 /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
1817 if ((step > 0 && stop < start) ||
1818 (step < 0 && stop > start))
1819 stop = start;
1820 if (step == 1) {
1821 if (slicelength > needed) {
1822 memmove(self->ob_item + (start + needed) * itemsize,
1823 self->ob_item + stop * itemsize,
1824 (Py_SIZE(self) - stop) * itemsize);
1825 if (array_resize(self, Py_SIZE(self) +
1826 needed - slicelength) < 0)
1827 return -1;
1828 }
1829 else if (slicelength < needed) {
1830 if (array_resize(self, Py_SIZE(self) +
1831 needed - slicelength) < 0)
1832 return -1;
1833 memmove(self->ob_item + (start + needed) * itemsize,
1834 self->ob_item + stop * itemsize,
1835 (Py_SIZE(self) - start - needed) * itemsize);
1836 }
1837 if (needed > 0)
1838 memcpy(self->ob_item + start * itemsize,
1839 other->ob_item, needed * itemsize);
1840 return 0;
1841 }
1842 else if (needed == 0) {
1843 /* Delete slice */
1844 size_t cur;
1845 Py_ssize_t i;
Thomas Wouters3ccec682007-08-28 15:28:19 +00001846
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001847 if (step < 0) {
1848 stop = start + 1;
1849 start = stop + step * (slicelength - 1) - 1;
1850 step = -step;
1851 }
1852 for (cur = start, i = 0; i < slicelength;
1853 cur += step, i++) {
1854 Py_ssize_t lim = step - 1;
Thomas Wouters3ccec682007-08-28 15:28:19 +00001855
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001856 if (cur + step >= (size_t)Py_SIZE(self))
1857 lim = Py_SIZE(self) - cur - 1;
1858 memmove(self->ob_item + (cur - i) * itemsize,
1859 self->ob_item + (cur + 1) * itemsize,
1860 lim * itemsize);
1861 }
1862 cur = start + slicelength * step;
1863 if (cur < (size_t)Py_SIZE(self)) {
1864 memmove(self->ob_item + (cur-slicelength) * itemsize,
1865 self->ob_item + cur * itemsize,
1866 (Py_SIZE(self) - cur) * itemsize);
1867 }
1868 if (array_resize(self, Py_SIZE(self) - slicelength) < 0)
1869 return -1;
1870 return 0;
1871 }
1872 else {
1873 Py_ssize_t cur, i;
1874
1875 if (needed != slicelength) {
1876 PyErr_Format(PyExc_ValueError,
1877 "attempt to assign array of size %zd "
1878 "to extended slice of size %zd",
1879 needed, slicelength);
1880 return -1;
1881 }
1882 for (cur = start, i = 0; i < slicelength;
1883 cur += step, i++) {
1884 memcpy(self->ob_item + cur * itemsize,
1885 other->ob_item + i * itemsize,
1886 itemsize);
1887 }
1888 return 0;
1889 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001890}
1891
1892static PyMappingMethods array_as_mapping = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001893 (lenfunc)array_length,
1894 (binaryfunc)array_subscr,
1895 (objobjargproc)array_ass_subscr
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001896};
1897
Raymond Hettinger01a807d2007-04-02 22:54:21 +00001898static const void *emptybuf = "";
1899
Martin v. Löwis18e16552006-02-15 17:27:45 +00001900static Py_ssize_t
1901array_buffer_getreadbuf(arrayobject *self, Py_ssize_t index, const void **ptr)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001902{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001903 if ( index != 0 ) {
1904 PyErr_SetString(PyExc_SystemError,
1905 "Accessing non-existent array segment");
1906 return -1;
1907 }
1908 *ptr = (void *)self->ob_item;
1909 if (*ptr == NULL)
1910 *ptr = emptybuf;
1911 return Py_SIZE(self)*self->ob_descr->itemsize;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001912}
1913
Martin v. Löwis18e16552006-02-15 17:27:45 +00001914static Py_ssize_t
1915array_buffer_getwritebuf(arrayobject *self, Py_ssize_t index, const void **ptr)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001916{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001917 if ( index != 0 ) {
1918 PyErr_SetString(PyExc_SystemError,
1919 "Accessing non-existent array segment");
1920 return -1;
1921 }
1922 *ptr = (void *)self->ob_item;
1923 if (*ptr == NULL)
1924 *ptr = emptybuf;
1925 return Py_SIZE(self)*self->ob_descr->itemsize;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001926}
1927
Martin v. Löwis18e16552006-02-15 17:27:45 +00001928static Py_ssize_t
1929array_buffer_getsegcount(arrayobject *self, Py_ssize_t *lenp)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001930{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001931 if ( lenp )
1932 *lenp = Py_SIZE(self)*self->ob_descr->itemsize;
1933 return 1;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001934}
1935
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001936static PySequenceMethods array_as_sequence = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001937 (lenfunc)array_length, /*sq_length*/
1938 (binaryfunc)array_concat, /*sq_concat*/
1939 (ssizeargfunc)array_repeat, /*sq_repeat*/
1940 (ssizeargfunc)array_item, /*sq_item*/
1941 (ssizessizeargfunc)array_slice, /*sq_slice*/
1942 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
1943 (ssizessizeobjargproc)array_ass_slice, /*sq_ass_slice*/
1944 (objobjproc)array_contains, /*sq_contains*/
1945 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
1946 (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001947};
1948
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001949static PyBufferProcs array_as_buffer = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001950 (readbufferproc)array_buffer_getreadbuf,
1951 (writebufferproc)array_buffer_getwritebuf,
1952 (segcountproc)array_buffer_getsegcount,
1953 NULL,
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001954};
1955
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001956static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +00001957array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum778983b1993-02-19 15:55:02 +00001958{
Serhiy Storchakab70091a2015-05-16 17:11:41 +03001959 int c = -1;
1960 PyObject *initial = NULL, *it = NULL, *typecode = NULL;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001961 struct arraydescr *descr;
Martin v. Löwis99866332002-03-01 10:27:01 +00001962
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001963 if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds))
1964 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001965
Serhiy Storchakab70091a2015-05-16 17:11:41 +03001966 if (!PyArg_ParseTuple(args, "O|O:array", &typecode, &initial))
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001967 return NULL;
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001968
Serhiy Storchakab70091a2015-05-16 17:11:41 +03001969 if (PyString_Check(typecode) && PyString_GET_SIZE(typecode) == 1)
1970 c = (unsigned char)*PyString_AS_STRING(typecode);
Serhiy Storchakadc967c12015-05-31 11:56:48 +03001971#ifdef Py_USING_UNICODE
Serhiy Storchakab70091a2015-05-16 17:11:41 +03001972 else if (PyUnicode_Check(typecode) && PyUnicode_GET_SIZE(typecode) == 1)
1973 c = *PyUnicode_AS_UNICODE(typecode);
Serhiy Storchakadc967c12015-05-31 11:56:48 +03001974#endif
Serhiy Storchakab70091a2015-05-16 17:11:41 +03001975 else {
1976 PyErr_Format(PyExc_TypeError,
1977 "array() argument 1 or typecode must be char (string or "
1978 "ascii-unicode with length 1), not %s",
1979 Py_TYPE(typecode)->tp_name);
1980 return NULL;
1981 }
1982
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001983 if (!(initial == NULL || PyList_Check(initial)
1984 || PyString_Check(initial) || PyTuple_Check(initial)
Alexander Belopolsky4ea1aac2011-01-11 22:35:58 +00001985 || (c == 'u' && PyUnicode_Check(initial)))) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001986 it = PyObject_GetIter(initial);
1987 if (it == NULL)
1988 return NULL;
1989 /* We set initial to NULL so that the subsequent code
1990 will create an empty array of the appropriate type
1991 and afterwards we can use array_iter_extend to populate
1992 the array.
1993 */
1994 initial = NULL;
1995 }
1996 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1997 if (descr->typecode == c) {
1998 PyObject *a;
1999 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00002000
Alexander Belopolsky4ea1aac2011-01-11 22:35:58 +00002001 if (initial == NULL || !(PyList_Check(initial)
2002 || PyTuple_Check(initial)))
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002003 len = 0;
2004 else
Alexander Belopolsky4ea1aac2011-01-11 22:35:58 +00002005 len = PySequence_Size(initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00002006
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002007 a = newarrayobject(type, len, descr);
2008 if (a == NULL)
2009 return NULL;
2010
Alexander Belopolsky4ea1aac2011-01-11 22:35:58 +00002011 if (len > 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002012 Py_ssize_t i;
2013 for (i = 0; i < len; i++) {
2014 PyObject *v =
2015 PySequence_GetItem(initial, i);
2016 if (v == NULL) {
2017 Py_DECREF(a);
2018 return NULL;
2019 }
2020 if (setarrayitem(a, i, v) != 0) {
2021 Py_DECREF(v);
2022 Py_DECREF(a);
2023 return NULL;
2024 }
2025 Py_DECREF(v);
2026 }
2027 } else if (initial != NULL && PyString_Check(initial)) {
2028 PyObject *t_initial, *v;
2029 t_initial = PyTuple_Pack(1, initial);
2030 if (t_initial == NULL) {
2031 Py_DECREF(a);
2032 return NULL;
2033 }
2034 v = array_fromstring((arrayobject *)a,
2035 t_initial);
2036 Py_DECREF(t_initial);
2037 if (v == NULL) {
2038 Py_DECREF(a);
2039 return NULL;
2040 }
2041 Py_DECREF(v);
Martin v. Löwis99866332002-03-01 10:27:01 +00002042#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002043 } else if (initial != NULL && PyUnicode_Check(initial)) {
2044 Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial);
2045 if (n > 0) {
2046 arrayobject *self = (arrayobject *)a;
2047 char *item = self->ob_item;
2048 item = (char *)PyMem_Realloc(item, n);
2049 if (item == NULL) {
2050 PyErr_NoMemory();
2051 Py_DECREF(a);
2052 return NULL;
2053 }
2054 self->ob_item = item;
2055 Py_SIZE(self) = n / sizeof(Py_UNICODE);
2056 memcpy(item, PyUnicode_AS_DATA(initial), n);
2057 self->allocated = Py_SIZE(self);
2058 }
Martin v. Löwis99866332002-03-01 10:27:01 +00002059#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002060 }
2061 if (it != NULL) {
2062 if (array_iter_extend((arrayobject *)a, it) == -1) {
2063 Py_DECREF(it);
2064 Py_DECREF(a);
2065 return NULL;
2066 }
2067 Py_DECREF(it);
2068 }
2069 return a;
2070 }
2071 }
2072 PyErr_SetString(PyExc_ValueError,
2073 "bad typecode (must be c, b, B, u, h, H, i, I, l, L, f or d)");
2074 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00002075}
2076
Guido van Rossum778983b1993-02-19 15:55:02 +00002077
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002078PyDoc_STRVAR(module_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00002079"This module defines an object type which can efficiently represent\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002080an array of basic values: characters, integers, floating point\n\
2081numbers. Arrays are sequence types and behave very much like lists,\n\
2082except that the type of objects stored in them is constrained. The\n\
2083type is specified at object creation time by using a type code, which\n\
2084is a single character. The following type codes are defined:\n\
2085\n\
2086 Type code C Type Minimum size in bytes \n\
2087 'c' character 1 \n\
2088 'b' signed integer 1 \n\
2089 'B' unsigned integer 1 \n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002090 'u' Unicode character 2 \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002091 'h' signed integer 2 \n\
2092 'H' unsigned integer 2 \n\
2093 'i' signed integer 2 \n\
2094 'I' unsigned integer 2 \n\
2095 'l' signed integer 4 \n\
2096 'L' unsigned integer 4 \n\
2097 'f' floating point 4 \n\
2098 'd' floating point 8 \n\
2099\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002100The constructor is:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002101\n\
2102array(typecode [, initializer]) -- create a new array\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002103");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002104
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002105PyDoc_STRVAR(arraytype_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00002106"array(typecode [, initializer]) -> array\n\
2107\n\
2108Return a new array whose items are restricted by typecode, and\n\
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00002109initialized from the optional initializer value, which must be a list,\n\
Florent Xiclunab918bdc2011-12-09 23:40:27 +01002110string or iterable over elements of the appropriate type.\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002111\n\
2112Arrays represent basic values and behave very much like lists, except\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002113the type of objects stored in them is constrained.\n\
2114\n\
2115Methods:\n\
2116\n\
2117append() -- append a new item to the end of the array\n\
2118buffer_info() -- return information giving the current memory info\n\
2119byteswap() -- byteswap all the items of the array\n\
Mark Dickinson3e4caeb2009-02-21 20:27:01 +00002120count() -- return number of occurrences of an object\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00002121extend() -- extend array by appending multiple elements from an iterable\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002122fromfile() -- read items from a file object\n\
2123fromlist() -- append items from the list\n\
2124fromstring() -- append items from the string\n\
Mark Dickinson3e4caeb2009-02-21 20:27:01 +00002125index() -- return index of first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002126insert() -- insert a new item into the array at a provided position\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00002127pop() -- remove and return item (default last)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002128read() -- DEPRECATED, use fromfile()\n\
Mark Dickinson3e4caeb2009-02-21 20:27:01 +00002129remove() -- remove first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002130reverse() -- reverse the order of the items in the array\n\
2131tofile() -- write all items to a file object\n\
2132tolist() -- return the array converted to an ordinary list\n\
2133tostring() -- return the array converted to a string\n\
2134write() -- DEPRECATED, use tofile()\n\
2135\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002136Attributes:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002137\n\
2138typecode -- the typecode character used to create the array\n\
2139itemsize -- the length in bytes of one array item\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002140");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002141
Raymond Hettinger625812f2003-01-07 01:58:52 +00002142static PyObject *array_iter(arrayobject *ao);
2143
Tim Peters0c322792002-07-17 16:49:03 +00002144static PyTypeObject Arraytype = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002145 PyVarObject_HEAD_INIT(NULL, 0)
2146 "array.array",
2147 sizeof(arrayobject),
2148 0,
2149 (destructor)array_dealloc, /* tp_dealloc */
2150 0, /* tp_print */
2151 0, /* tp_getattr */
2152 0, /* tp_setattr */
2153 0, /* tp_compare */
2154 (reprfunc)array_repr, /* tp_repr */
2155 0, /* tp_as_number*/
2156 &array_as_sequence, /* tp_as_sequence*/
2157 &array_as_mapping, /* tp_as_mapping*/
2158 0, /* tp_hash */
2159 0, /* tp_call */
2160 0, /* tp_str */
2161 PyObject_GenericGetAttr, /* tp_getattro */
2162 0, /* tp_setattro */
2163 &array_as_buffer, /* tp_as_buffer*/
2164 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
2165 arraytype_doc, /* tp_doc */
2166 0, /* tp_traverse */
2167 0, /* tp_clear */
2168 array_richcompare, /* tp_richcompare */
2169 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
2170 (getiterfunc)array_iter, /* tp_iter */
2171 0, /* tp_iternext */
2172 array_methods, /* tp_methods */
2173 0, /* tp_members */
2174 array_getsets, /* tp_getset */
2175 0, /* tp_base */
2176 0, /* tp_dict */
2177 0, /* tp_descr_get */
2178 0, /* tp_descr_set */
2179 0, /* tp_dictoffset */
2180 0, /* tp_init */
2181 PyType_GenericAlloc, /* tp_alloc */
2182 array_new, /* tp_new */
2183 PyObject_Del, /* tp_free */
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002184};
2185
Raymond Hettinger625812f2003-01-07 01:58:52 +00002186
2187/*********************** Array Iterator **************************/
2188
2189typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002190 PyObject_HEAD
2191 Py_ssize_t index;
2192 arrayobject *ao;
2193 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002194} arrayiterobject;
2195
2196static PyTypeObject PyArrayIter_Type;
2197
2198#define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
2199
2200static PyObject *
2201array_iter(arrayobject *ao)
2202{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002203 arrayiterobject *it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002204
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002205 if (!array_Check(ao)) {
2206 PyErr_BadInternalCall();
2207 return NULL;
2208 }
Raymond Hettinger625812f2003-01-07 01:58:52 +00002209
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002210 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
2211 if (it == NULL)
2212 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002213
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002214 Py_INCREF(ao);
2215 it->ao = ao;
2216 it->index = 0;
2217 it->getitem = ao->ob_descr->getitem;
2218 PyObject_GC_Track(it);
2219 return (PyObject *)it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002220}
2221
2222static PyObject *
Raymond Hettinger625812f2003-01-07 01:58:52 +00002223arrayiter_next(arrayiterobject *it)
2224{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002225 assert(PyArrayIter_Check(it));
2226 if (it->index < Py_SIZE(it->ao))
2227 return (*it->getitem)(it->ao, it->index++);
2228 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002229}
2230
2231static void
2232arrayiter_dealloc(arrayiterobject *it)
2233{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002234 PyObject_GC_UnTrack(it);
2235 Py_XDECREF(it->ao);
2236 PyObject_GC_Del(it);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002237}
2238
2239static int
2240arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2241{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002242 Py_VISIT(it->ao);
2243 return 0;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002244}
2245
2246static PyTypeObject PyArrayIter_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002247 PyVarObject_HEAD_INIT(NULL, 0)
2248 "arrayiterator", /* tp_name */
2249 sizeof(arrayiterobject), /* tp_basicsize */
2250 0, /* tp_itemsize */
2251 /* methods */
2252 (destructor)arrayiter_dealloc, /* tp_dealloc */
2253 0, /* tp_print */
2254 0, /* tp_getattr */
2255 0, /* tp_setattr */
2256 0, /* tp_compare */
2257 0, /* tp_repr */
2258 0, /* tp_as_number */
2259 0, /* tp_as_sequence */
2260 0, /* tp_as_mapping */
2261 0, /* tp_hash */
2262 0, /* tp_call */
2263 0, /* tp_str */
2264 PyObject_GenericGetAttr, /* tp_getattro */
2265 0, /* tp_setattro */
2266 0, /* tp_as_buffer */
2267 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2268 0, /* tp_doc */
2269 (traverseproc)arrayiter_traverse, /* tp_traverse */
2270 0, /* tp_clear */
2271 0, /* tp_richcompare */
2272 0, /* tp_weaklistoffset */
2273 PyObject_SelfIter, /* tp_iter */
2274 (iternextfunc)arrayiter_next, /* tp_iternext */
2275 0, /* tp_methods */
Raymond Hettinger625812f2003-01-07 01:58:52 +00002276};
2277
2278
2279/*********************** Install Module **************************/
2280
Martin v. Löwis99866332002-03-01 10:27:01 +00002281/* No functions in array module. */
2282static PyMethodDef a_methods[] = {
2283 {NULL, NULL, 0, NULL} /* Sentinel */
2284};
2285
2286
Mark Hammondfe51c6d2002-08-02 02:27:13 +00002287PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00002288initarray(void)
Guido van Rossum778983b1993-02-19 15:55:02 +00002289{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002290 PyObject *m;
Fred Drake0d40ba42000-02-04 20:33:49 +00002291
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002292 Arraytype.ob_type = &PyType_Type;
2293 PyArrayIter_Type.ob_type = &PyType_Type;
2294 m = Py_InitModule3("array", a_methods, module_doc);
2295 if (m == NULL)
2296 return;
Fred Drakef4e34842002-04-01 03:45:06 +00002297
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002298 Py_INCREF((PyObject *)&Arraytype);
2299 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
2300 Py_INCREF((PyObject *)&Arraytype);
2301 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
2302 /* No need to check the error here, the caller will do that */
Guido van Rossum778983b1993-02-19 15:55:02 +00002303}