blob: fe6106c9a41c76ab68583c2d626c78a38e0c92b5 [file] [log] [blame]
Guido van Rossum778983b1993-02-19 15:55:02 +00001/* Array object implementation */
2
3/* An array is a uniform list -- all items have the same type.
4 The item type is restricted to simple C types like int or float */
5
Martin v. Löwis18e16552006-02-15 17:27:45 +00006#define PY_SSIZE_T_CLEAN
Roger E. Masse2919eaa1996-12-09 20:10:36 +00007#include "Python.h"
Raymond Hettingercb87bc82004-05-31 00:35:52 +00008#include "structmember.h"
Roger E. Masse5817f8f1996-12-09 22:24:19 +00009
Guido van Rossum0c709541994-08-19 12:01:32 +000010#ifdef STDC_HEADERS
11#include <stddef.h>
Guido van Rossum7f1de831999-08-27 20:33:52 +000012#else /* !STDC_HEADERS */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000013#ifdef HAVE_SYS_TYPES_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000014#include <sys/types.h> /* For size_t */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000015#endif /* HAVE_SYS_TYPES_H */
Guido van Rossum7f1de831999-08-27 20:33:52 +000016#endif /* !STDC_HEADERS */
Guido van Rossum778983b1993-02-19 15:55:02 +000017
18struct arrayobject; /* Forward */
19
Tim Petersbb307342000-09-10 05:22:54 +000020/* All possible arraydescr values are defined in the vector "descriptors"
21 * below. That's defined later because the appropriate get and set
22 * functions aren't visible yet.
23 */
Guido van Rossum778983b1993-02-19 15:55:02 +000024struct arraydescr {
Victor Stinner9f0b51e2010-11-09 09:38:30 +000025 Py_UNICODE typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026 int itemsize;
27 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
28 int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *);
29 char *formats;
30 int is_integer_type;
31 int is_signed;
Guido van Rossum778983b1993-02-19 15:55:02 +000032};
33
34typedef struct arrayobject {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 PyObject_VAR_HEAD
36 char *ob_item;
37 Py_ssize_t allocated;
38 struct arraydescr *ob_descr;
39 PyObject *weakreflist; /* List of weak references */
40 int ob_exports; /* Number of exported buffers */
Guido van Rossum778983b1993-02-19 15:55:02 +000041} arrayobject;
42
Jeremy Hylton938ace62002-07-17 16:30:39 +000043static PyTypeObject Arraytype;
Guido van Rossum778983b1993-02-19 15:55:02 +000044
Martin v. Löwis99866332002-03-01 10:27:01 +000045#define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
Christian Heimes90aa7642007-12-19 02:45:37 +000046#define array_CheckExact(op) (Py_TYPE(op) == &Arraytype)
Guido van Rossum778983b1993-02-19 15:55:02 +000047
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000048static int
Martin v. Löwis18e16552006-02-15 17:27:45 +000049array_resize(arrayobject *self, Py_ssize_t newsize)
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000050{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 char *items;
52 size_t _new_size;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 if (self->ob_exports > 0 && newsize != Py_SIZE(self)) {
55 PyErr_SetString(PyExc_BufferError,
56 "cannot resize an array that is exporting buffers");
57 return -1;
58 }
Antoine Pitrou3ad3a0d2008-12-18 17:08:32 +000059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 /* Bypass realloc() when a previous overallocation is large enough
61 to accommodate the newsize. If the newsize is 16 smaller than the
62 current size, then proceed with the realloc() to shrink the array.
63 */
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 if (self->allocated >= newsize &&
66 Py_SIZE(self) < newsize + 16 &&
67 self->ob_item != NULL) {
68 Py_SIZE(self) = newsize;
69 return 0;
70 }
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 if (newsize == 0) {
73 PyMem_FREE(self->ob_item);
74 self->ob_item = NULL;
75 Py_SIZE(self) = 0;
76 self->allocated = 0;
77 return 0;
78 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +000079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 /* This over-allocates proportional to the array size, making room
81 * for additional growth. The over-allocation is mild, but is
82 * enough to give linear-time amortized behavior over a long
83 * sequence of appends() in the presence of a poorly-performing
84 * system realloc().
85 * The growth pattern is: 0, 4, 8, 16, 25, 34, 46, 56, 67, 79, ...
86 * Note, the pattern starts out the same as for lists but then
87 * grows at a smaller rate so that larger arrays only overallocate
88 * by about 1/16th -- this is done because arrays are presumed to be more
89 * memory critical.
90 */
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 _new_size = (newsize >> 4) + (Py_SIZE(self) < 8 ? 3 : 7) + newsize;
93 items = self->ob_item;
94 /* XXX The following multiplication and division does not optimize away
95 like it does for lists since the size is not known at compile time */
96 if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize))
97 PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize));
98 else
99 items = NULL;
100 if (items == NULL) {
101 PyErr_NoMemory();
102 return -1;
103 }
104 self->ob_item = items;
105 Py_SIZE(self) = newsize;
106 self->allocated = _new_size;
107 return 0;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000108}
109
Tim Petersbb307342000-09-10 05:22:54 +0000110/****************************************************************************
111Get and Set functions for each type.
112A Get function takes an arrayobject* and an integer index, returning the
113array value at that index wrapped in an appropriate PyObject*.
114A Set function takes an arrayobject, integer index, and PyObject*; sets
115the array value at that index to the raw C data extracted from the PyObject*,
116and returns 0 if successful, else nonzero on failure (PyObject* not of an
117appropriate type or value).
118Note that the basic Get and Set functions do NOT check that the index is
119in bounds; that's the responsibility of the caller.
120****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +0000121
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000122static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000123b_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 long x = ((char *)ap->ob_item)[i];
126 if (x >= 128)
127 x -= 256;
128 return PyLong_FromLong(x);
Guido van Rossum778983b1993-02-19 15:55:02 +0000129}
130
131static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000132b_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000133{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 short x;
135 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
136 must use the next size up that is signed ('h') and manually do
137 the overflow checking */
138 if (!PyArg_Parse(v, "h;array item must be integer", &x))
139 return -1;
140 else if (x < -128) {
141 PyErr_SetString(PyExc_OverflowError,
142 "signed char is less than minimum");
143 return -1;
144 }
145 else if (x > 127) {
146 PyErr_SetString(PyExc_OverflowError,
147 "signed char is greater than maximum");
148 return -1;
149 }
150 if (i >= 0)
151 ((char *)ap->ob_item)[i] = (char)x;
152 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000153}
154
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000155static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000156BB_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 long x = ((unsigned char *)ap->ob_item)[i];
159 return PyLong_FromLong(x);
Guido van Rossum549ab711997-01-03 19:09:47 +0000160}
161
Fred Drake541dc3b2000-06-28 17:49:30 +0000162static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000163BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000164{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 unsigned char x;
166 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
167 if (!PyArg_Parse(v, "b;array item must be integer", &x))
168 return -1;
169 if (i >= 0)
170 ((char *)ap->ob_item)[i] = x;
171 return 0;
Fred Drake541dc3b2000-06-28 17:49:30 +0000172}
Guido van Rossum549ab711997-01-03 19:09:47 +0000173
Martin v. Löwis99866332002-03-01 10:27:01 +0000174static 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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000183 Py_UNICODE *p;
184 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +0000185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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}
Martin v. Löwis99866332002-03-01 10:27:01 +0000197
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +0000198
Guido van Rossum549ab711997-01-03 19:09:47 +0000199static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000200h_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 return PyLong_FromLong((long) ((short *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000203}
204
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +0000205
Guido van Rossum778983b1993-02-19 15:55:02 +0000206static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000207h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 short x;
210 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
211 if (!PyArg_Parse(v, "h;array item must be integer", &x))
212 return -1;
213 if (i >= 0)
214 ((short *)ap->ob_item)[i] = x;
215 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000216}
217
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000218static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000219HH_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 return PyLong_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000222}
223
Fred Drake541dc3b2000-06-28 17:49:30 +0000224static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000225HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 int x;
228 /* PyArg_Parse's 'h' formatter is for a signed short, therefore
229 must use the next size up and manually do the overflow checking */
230 if (!PyArg_Parse(v, "i;array item must be integer", &x))
231 return -1;
232 else if (x < 0) {
233 PyErr_SetString(PyExc_OverflowError,
234 "unsigned short is less than minimum");
235 return -1;
236 }
237 else if (x > USHRT_MAX) {
238 PyErr_SetString(PyExc_OverflowError,
239 "unsigned short is greater than maximum");
240 return -1;
241 }
242 if (i >= 0)
243 ((short *)ap->ob_item)[i] = (short)x;
244 return 0;
Fred Drake541dc3b2000-06-28 17:49:30 +0000245}
Guido van Rossum549ab711997-01-03 19:09:47 +0000246
247static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000248i_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossume77a7571993-11-03 15:01:26 +0000249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 return PyLong_FromLong((long) ((int *)ap->ob_item)[i]);
Guido van Rossume77a7571993-11-03 15:01:26 +0000251}
252
253static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000254i_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossume77a7571993-11-03 15:01:26 +0000255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 int x;
257 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
258 if (!PyArg_Parse(v, "i;array item must be integer", &x))
259 return -1;
260 if (i >= 0)
261 ((int *)ap->ob_item)[i] = x;
262 return 0;
Guido van Rossume77a7571993-11-03 15:01:26 +0000263}
264
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000265static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000266II_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 return PyLong_FromUnsignedLong(
269 (unsigned long) ((unsigned int *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000270}
271
272static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000273II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 unsigned long x;
276 if (PyLong_Check(v)) {
277 x = PyLong_AsUnsignedLong(v);
278 if (x == (unsigned long) -1 && PyErr_Occurred())
279 return -1;
280 }
281 else {
282 long y;
283 if (!PyArg_Parse(v, "l;array item must be integer", &y))
284 return -1;
285 if (y < 0) {
286 PyErr_SetString(PyExc_OverflowError,
287 "unsigned int is less than minimum");
288 return -1;
289 }
290 x = (unsigned long)y;
Tim Petersbb307342000-09-10 05:22:54 +0000291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 }
293 if (x > UINT_MAX) {
294 PyErr_SetString(PyExc_OverflowError,
295 "unsigned int is greater than maximum");
296 return -1;
297 }
Fred Drake541dc3b2000-06-28 17:49:30 +0000298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 if (i >= 0)
300 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
301 return 0;
Guido van Rossum549ab711997-01-03 19:09:47 +0000302}
303
304static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000305l_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 return PyLong_FromLong(((long *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000308}
309
310static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000311l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 long x;
314 if (!PyArg_Parse(v, "l;array item must be integer", &x))
315 return -1;
316 if (i >= 0)
317 ((long *)ap->ob_item)[i] = x;
318 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000319}
320
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000321static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000322LL_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000325}
326
327static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000328LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 unsigned long x;
331 if (PyLong_Check(v)) {
332 x = PyLong_AsUnsignedLong(v);
333 if (x == (unsigned long) -1 && PyErr_Occurred())
334 return -1;
335 }
336 else {
337 long y;
338 if (!PyArg_Parse(v, "l;array item must be integer", &y))
339 return -1;
340 if (y < 0) {
341 PyErr_SetString(PyExc_OverflowError,
342 "unsigned long is less than minimum");
343 return -1;
344 }
345 x = (unsigned long)y;
Tim Petersbb307342000-09-10 05:22:54 +0000346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 }
348 if (x > ULONG_MAX) {
349 PyErr_SetString(PyExc_OverflowError,
350 "unsigned long is greater than maximum");
351 return -1;
352 }
Tim Petersbb307342000-09-10 05:22:54 +0000353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 if (i >= 0)
355 ((unsigned long *)ap->ob_item)[i] = x;
356 return 0;
Guido van Rossum549ab711997-01-03 19:09:47 +0000357}
358
359static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000360f_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000363}
364
365static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000366f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000367{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 float x;
369 if (!PyArg_Parse(v, "f;array item must be float", &x))
370 return -1;
371 if (i >= 0)
372 ((float *)ap->ob_item)[i] = x;
373 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000374}
375
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000376static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000377d_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000380}
381
382static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000383d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 double x;
386 if (!PyArg_Parse(v, "d;array item must be float", &x))
387 return -1;
388 if (i >= 0)
389 ((double *)ap->ob_item)[i] = x;
390 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000391}
392
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000393
Alexandre Vassalottiad077152009-07-15 17:49:23 +0000394/* Description of types.
395 *
396 * Don't forget to update typecode_to_mformat_code() if you add a new
397 * typecode.
398 */
Guido van Rossum234f9421993-06-17 12:35:49 +0000399static struct arraydescr descriptors[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 {'b', 1, b_getitem, b_setitem, "b", 1, 1},
401 {'B', 1, BB_getitem, BB_setitem, "B", 1, 0},
402 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem, "u", 0, 0},
403 {'h', sizeof(short), h_getitem, h_setitem, "h", 1, 1},
404 {'H', sizeof(short), HH_getitem, HH_setitem, "H", 1, 0},
405 {'i', sizeof(int), i_getitem, i_setitem, "i", 1, 1},
406 {'I', sizeof(int), II_getitem, II_setitem, "I", 1, 0},
407 {'l', sizeof(long), l_getitem, l_setitem, "l", 1, 1},
408 {'L', sizeof(long), LL_getitem, LL_setitem, "L", 1, 0},
409 {'f', sizeof(float), f_getitem, f_setitem, "f", 0, 0},
410 {'d', sizeof(double), d_getitem, d_setitem, "d", 0, 0},
411 {'\0', 0, 0, 0, 0, 0, 0} /* Sentinel */
Guido van Rossum778983b1993-02-19 15:55:02 +0000412};
Tim Petersbb307342000-09-10 05:22:54 +0000413
414/****************************************************************************
415Implementations of array object methods.
416****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +0000417
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000418static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000419newarrayobject(PyTypeObject *type, Py_ssize_t size, struct arraydescr *descr)
Guido van Rossum778983b1993-02-19 15:55:02 +0000420{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 arrayobject *op;
422 size_t nbytes;
Martin v. Löwis99866332002-03-01 10:27:01 +0000423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 if (size < 0) {
425 PyErr_BadInternalCall();
426 return NULL;
427 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 nbytes = size * descr->itemsize;
430 /* Check for overflow */
431 if (nbytes / descr->itemsize != (size_t)size) {
432 return PyErr_NoMemory();
433 }
434 op = (arrayobject *) type->tp_alloc(type, 0);
435 if (op == NULL) {
436 return NULL;
437 }
438 op->ob_descr = descr;
439 op->allocated = size;
440 op->weakreflist = NULL;
441 Py_SIZE(op) = size;
442 if (size <= 0) {
443 op->ob_item = NULL;
444 }
445 else {
446 op->ob_item = PyMem_NEW(char, nbytes);
447 if (op->ob_item == NULL) {
448 Py_DECREF(op);
449 return PyErr_NoMemory();
450 }
451 }
452 op->ob_exports = 0;
453 return (PyObject *) op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000454}
455
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000456static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000457getarrayitem(PyObject *op, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 register arrayobject *ap;
460 assert(array_Check(op));
461 ap = (arrayobject *)op;
462 assert(i>=0 && i<Py_SIZE(ap));
463 return (*ap->ob_descr->getitem)(ap, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000464}
465
466static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000467ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 char *items;
470 Py_ssize_t n = Py_SIZE(self);
471 if (v == NULL) {
472 PyErr_BadInternalCall();
473 return -1;
474 }
475 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
476 return -1;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 if (array_resize(self, n+1) == -1)
479 return -1;
480 items = self->ob_item;
481 if (where < 0) {
482 where += n;
483 if (where < 0)
484 where = 0;
485 }
486 if (where > n)
487 where = n;
488 /* appends don't need to call memmove() */
489 if (where != n)
490 memmove(items + (where+1)*self->ob_descr->itemsize,
491 items + where*self->ob_descr->itemsize,
492 (n-where)*self->ob_descr->itemsize);
493 return (*self->ob_descr->setitem)(self, where, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000494}
495
Guido van Rossum778983b1993-02-19 15:55:02 +0000496/* Methods */
497
498static void
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000499array_dealloc(arrayobject *op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 if (op->weakreflist != NULL)
502 PyObject_ClearWeakRefs((PyObject *) op);
503 if (op->ob_item != NULL)
504 PyMem_DEL(op->ob_item);
505 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000506}
507
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000508static PyObject *
509array_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 arrayobject *va, *wa;
512 PyObject *vi = NULL;
513 PyObject *wi = NULL;
514 Py_ssize_t i, k;
515 PyObject *res;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 if (!array_Check(v) || !array_Check(w)) {
518 Py_INCREF(Py_NotImplemented);
519 return Py_NotImplemented;
520 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 va = (arrayobject *)v;
523 wa = (arrayobject *)w;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 if (Py_SIZE(va) != Py_SIZE(wa) && (op == Py_EQ || op == Py_NE)) {
526 /* Shortcut: if the lengths differ, the arrays differ */
527 if (op == Py_EQ)
528 res = Py_False;
529 else
530 res = Py_True;
531 Py_INCREF(res);
532 return res;
533 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 /* Search for the first index where items are different */
536 k = 1;
537 for (i = 0; i < Py_SIZE(va) && i < Py_SIZE(wa); i++) {
538 vi = getarrayitem(v, i);
539 wi = getarrayitem(w, i);
540 if (vi == NULL || wi == NULL) {
541 Py_XDECREF(vi);
542 Py_XDECREF(wi);
543 return NULL;
544 }
545 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
546 if (k == 0)
547 break; /* Keeping vi and wi alive! */
548 Py_DECREF(vi);
549 Py_DECREF(wi);
550 if (k < 0)
551 return NULL;
552 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 if (k) {
555 /* No more items to compare -- compare sizes */
556 Py_ssize_t vs = Py_SIZE(va);
557 Py_ssize_t ws = Py_SIZE(wa);
558 int cmp;
559 switch (op) {
560 case Py_LT: cmp = vs < ws; break;
561 case Py_LE: cmp = vs <= ws; break;
562 case Py_EQ: cmp = vs == ws; break;
563 case Py_NE: cmp = vs != ws; break;
564 case Py_GT: cmp = vs > ws; break;
565 case Py_GE: cmp = vs >= ws; break;
566 default: return NULL; /* cannot happen */
567 }
568 if (cmp)
569 res = Py_True;
570 else
571 res = Py_False;
572 Py_INCREF(res);
573 return res;
574 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 /* We have an item that differs. First, shortcuts for EQ/NE */
577 if (op == Py_EQ) {
578 Py_INCREF(Py_False);
579 res = Py_False;
580 }
581 else if (op == Py_NE) {
582 Py_INCREF(Py_True);
583 res = Py_True;
584 }
585 else {
586 /* Compare the final item again using the proper operator */
587 res = PyObject_RichCompare(vi, wi, op);
588 }
589 Py_DECREF(vi);
590 Py_DECREF(wi);
591 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +0000592}
593
Martin v. Löwis18e16552006-02-15 17:27:45 +0000594static Py_ssize_t
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000595array_length(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +0000596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 return Py_SIZE(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000598}
599
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000600static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000601array_item(arrayobject *a, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 if (i < 0 || i >= Py_SIZE(a)) {
604 PyErr_SetString(PyExc_IndexError, "array index out of range");
605 return NULL;
606 }
607 return getarrayitem((PyObject *)a, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000608}
609
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000610static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000611array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 arrayobject *np;
614 if (ilow < 0)
615 ilow = 0;
616 else if (ilow > Py_SIZE(a))
617 ilow = Py_SIZE(a);
618 if (ihigh < 0)
619 ihigh = 0;
620 if (ihigh < ilow)
621 ihigh = ilow;
622 else if (ihigh > Py_SIZE(a))
623 ihigh = Py_SIZE(a);
624 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
625 if (np == NULL)
626 return NULL;
627 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
628 (ihigh-ilow) * a->ob_descr->itemsize);
629 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000630}
631
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000632static PyObject *
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000633array_copy(arrayobject *a, PyObject *unused)
634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 return array_slice(a, 0, Py_SIZE(a));
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000636}
637
638PyDoc_STRVAR(copy_doc,
639"copy(array)\n\
640\n\
641 Return a copy of the array.");
642
643static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000644array_concat(arrayobject *a, PyObject *bb)
Guido van Rossum778983b1993-02-19 15:55:02 +0000645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 Py_ssize_t size;
647 arrayobject *np;
648 if (!array_Check(bb)) {
649 PyErr_Format(PyExc_TypeError,
650 "can only append array (not \"%.200s\") to array",
651 Py_TYPE(bb)->tp_name);
652 return NULL;
653 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000654#define b ((arrayobject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 if (a->ob_descr != b->ob_descr) {
656 PyErr_BadArgument();
657 return NULL;
658 }
659 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) {
660 return PyErr_NoMemory();
661 }
662 size = Py_SIZE(a) + Py_SIZE(b);
663 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
664 if (np == NULL) {
665 return NULL;
666 }
667 memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize);
668 memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize,
669 b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
670 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000671#undef b
672}
673
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000674static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000675array_repeat(arrayobject *a, Py_ssize_t n)
Guido van Rossum778983b1993-02-19 15:55:02 +0000676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 Py_ssize_t size;
678 arrayobject *np;
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000679 Py_ssize_t oldbytes, newbytes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 if (n < 0)
681 n = 0;
682 if ((Py_SIZE(a) != 0) && (n > PY_SSIZE_T_MAX / Py_SIZE(a))) {
683 return PyErr_NoMemory();
684 }
685 size = Py_SIZE(a) * n;
686 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
687 if (np == NULL)
688 return NULL;
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000689 if (n == 0)
690 return (PyObject *)np;
691 oldbytes = Py_SIZE(a) * a->ob_descr->itemsize;
692 newbytes = oldbytes * n;
693 /* this follows the code in unicode_repeat */
694 if (oldbytes == 1) {
695 memset(np->ob_item, a->ob_item[0], newbytes);
696 } else {
697 Py_ssize_t done = oldbytes;
698 Py_MEMCPY(np->ob_item, a->ob_item, oldbytes);
699 while (done < newbytes) {
700 Py_ssize_t ncopy = (done <= newbytes-done) ? done : newbytes-done;
701 Py_MEMCPY(np->ob_item+done, np->ob_item, ncopy);
702 done += ncopy;
703 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 }
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000705 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000706}
707
708static int
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000709array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000710{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 char *item;
712 Py_ssize_t n; /* Size of replacement array */
713 Py_ssize_t d; /* Change in size */
Guido van Rossum778983b1993-02-19 15:55:02 +0000714#define b ((arrayobject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 if (v == NULL)
716 n = 0;
717 else if (array_Check(v)) {
718 n = Py_SIZE(b);
719 if (a == b) {
720 /* Special case "a[i:j] = a" -- copy b first */
721 int ret;
722 v = array_slice(b, 0, n);
723 if (!v)
724 return -1;
725 ret = array_ass_slice(a, ilow, ihigh, v);
726 Py_DECREF(v);
727 return ret;
728 }
729 if (b->ob_descr != a->ob_descr) {
730 PyErr_BadArgument();
731 return -1;
732 }
733 }
734 else {
735 PyErr_Format(PyExc_TypeError,
736 "can only assign array (not \"%.200s\") to array slice",
737 Py_TYPE(v)->tp_name);
738 return -1;
739 }
740 if (ilow < 0)
741 ilow = 0;
742 else if (ilow > Py_SIZE(a))
743 ilow = Py_SIZE(a);
744 if (ihigh < 0)
745 ihigh = 0;
746 if (ihigh < ilow)
747 ihigh = ilow;
748 else if (ihigh > Py_SIZE(a))
749 ihigh = Py_SIZE(a);
750 item = a->ob_item;
751 d = n - (ihigh-ilow);
752 /* Issue #4509: If the array has exported buffers and the slice
753 assignment would change the size of the array, fail early to make
754 sure we don't modify it. */
755 if (d != 0 && a->ob_exports > 0) {
756 PyErr_SetString(PyExc_BufferError,
757 "cannot resize an array that is exporting buffers");
758 return -1;
759 }
760 if (d < 0) { /* Delete -d items */
761 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
762 item + ihigh*a->ob_descr->itemsize,
763 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
764 if (array_resize(a, Py_SIZE(a) + d) == -1)
765 return -1;
766 }
767 else if (d > 0) { /* Insert d items */
768 if (array_resize(a, Py_SIZE(a) + d))
769 return -1;
770 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
771 item + ihigh*a->ob_descr->itemsize,
772 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
773 }
774 if (n > 0)
775 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
776 n*b->ob_descr->itemsize);
777 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000778#undef b
779}
780
781static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000782array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 if (i < 0 || i >= Py_SIZE(a)) {
785 PyErr_SetString(PyExc_IndexError,
786 "array assignment index out of range");
787 return -1;
788 }
789 if (v == NULL)
790 return array_ass_slice(a, i, i+1, v);
791 return (*a->ob_descr->setitem)(a, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000792}
793
794static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000795setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000796{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 assert(array_Check(a));
798 return array_ass_item((arrayobject *)a, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000799}
800
Martin v. Löwis99866332002-03-01 10:27:01 +0000801static int
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000802array_iter_extend(arrayobject *self, PyObject *bb)
803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 PyObject *it, *v;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 it = PyObject_GetIter(bb);
807 if (it == NULL)
808 return -1;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 while ((v = PyIter_Next(it)) != NULL) {
Mark Dickinson346f0af2010-08-06 09:36:57 +0000811 if (ins1(self, Py_SIZE(self), v) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 Py_DECREF(v);
813 Py_DECREF(it);
814 return -1;
815 }
816 Py_DECREF(v);
817 }
818 Py_DECREF(it);
819 if (PyErr_Occurred())
820 return -1;
821 return 0;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000822}
823
824static int
Martin v. Löwis99866332002-03-01 10:27:01 +0000825array_do_extend(arrayobject *self, PyObject *bb)
826{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 Py_ssize_t size, oldsize, bbsize;
Martin v. Löwis99866332002-03-01 10:27:01 +0000828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 if (!array_Check(bb))
830 return array_iter_extend(self, bb);
831#define b ((arrayobject *)bb)
832 if (self->ob_descr != b->ob_descr) {
833 PyErr_SetString(PyExc_TypeError,
834 "can only extend with array of same kind");
835 return -1;
836 }
837 if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) ||
838 ((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
839 PyErr_NoMemory();
840 return -1;
841 }
842 oldsize = Py_SIZE(self);
843 /* Get the size of bb before resizing the array since bb could be self. */
844 bbsize = Py_SIZE(bb);
845 size = oldsize + Py_SIZE(b);
846 if (array_resize(self, size) == -1)
847 return -1;
848 memcpy(self->ob_item + oldsize * self->ob_descr->itemsize,
849 b->ob_item, bbsize * b->ob_descr->itemsize);
850
851 return 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000852#undef b
853}
854
855static PyObject *
856array_inplace_concat(arrayobject *self, PyObject *bb)
857{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 if (!array_Check(bb)) {
859 PyErr_Format(PyExc_TypeError,
860 "can only extend array with array (not \"%.200s\")",
861 Py_TYPE(bb)->tp_name);
862 return NULL;
863 }
864 if (array_do_extend(self, bb) == -1)
865 return NULL;
866 Py_INCREF(self);
867 return (PyObject *)self;
Martin v. Löwis99866332002-03-01 10:27:01 +0000868}
869
870static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000871array_inplace_repeat(arrayobject *self, Py_ssize_t n)
Martin v. Löwis99866332002-03-01 10:27:01 +0000872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 char *items, *p;
874 Py_ssize_t size, i;
Martin v. Löwis99866332002-03-01 10:27:01 +0000875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 if (Py_SIZE(self) > 0) {
877 if (n < 0)
878 n = 0;
879 items = self->ob_item;
880 if ((self->ob_descr->itemsize != 0) &&
881 (Py_SIZE(self) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
882 return PyErr_NoMemory();
883 }
884 size = Py_SIZE(self) * self->ob_descr->itemsize;
885 if (n > 0 && size > PY_SSIZE_T_MAX / n) {
886 return PyErr_NoMemory();
887 }
888 if (array_resize(self, n * Py_SIZE(self)) == -1)
889 return NULL;
890 items = p = self->ob_item;
891 for (i = 1; i < n; i++) {
892 p += size;
893 memcpy(p, items, size);
894 }
895 }
896 Py_INCREF(self);
897 return (PyObject *)self;
Martin v. Löwis99866332002-03-01 10:27:01 +0000898}
899
900
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000901static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000902ins(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000903{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 if (ins1(self, where, v) != 0)
905 return NULL;
906 Py_INCREF(Py_None);
907 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000908}
909
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000910static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000911array_count(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 Py_ssize_t count = 0;
914 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 for (i = 0; i < Py_SIZE(self); i++) {
917 PyObject *selfi = getarrayitem((PyObject *)self, i);
918 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
919 Py_DECREF(selfi);
920 if (cmp > 0)
921 count++;
922 else if (cmp < 0)
923 return NULL;
924 }
925 return PyLong_FromSsize_t(count);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000926}
927
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000928PyDoc_STRVAR(count_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000929"count(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000930\n\
Mark Dickinson934896d2009-02-21 20:59:32 +0000931Return number of occurrences of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000932
933static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000934array_index(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000935{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 for (i = 0; i < Py_SIZE(self); i++) {
939 PyObject *selfi = getarrayitem((PyObject *)self, i);
940 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
941 Py_DECREF(selfi);
942 if (cmp > 0) {
943 return PyLong_FromLong((long)i);
944 }
945 else if (cmp < 0)
946 return NULL;
947 }
948 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
949 return NULL;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000950}
951
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000952PyDoc_STRVAR(index_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000953"index(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000954\n\
Mark Dickinson934896d2009-02-21 20:59:32 +0000955Return index of first occurrence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000956
Raymond Hettinger625812f2003-01-07 01:58:52 +0000957static int
958array_contains(arrayobject *self, PyObject *v)
959{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 Py_ssize_t i;
961 int cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +0000962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) {
964 PyObject *selfi = getarrayitem((PyObject *)self, i);
965 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
966 Py_DECREF(selfi);
967 }
968 return cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +0000969}
970
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000971static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000972array_remove(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000973{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 int i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 for (i = 0; i < Py_SIZE(self); i++) {
977 PyObject *selfi = getarrayitem((PyObject *)self,i);
978 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
979 Py_DECREF(selfi);
980 if (cmp > 0) {
981 if (array_ass_slice(self, i, i+1,
982 (PyObject *)NULL) != 0)
983 return NULL;
984 Py_INCREF(Py_None);
985 return Py_None;
986 }
987 else if (cmp < 0)
988 return NULL;
989 }
990 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
991 return NULL;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000992}
993
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000994PyDoc_STRVAR(remove_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000995"remove(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000996\n\
Mark Dickinson934896d2009-02-21 20:59:32 +0000997Remove the first occurrence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000998
999static PyObject *
1000array_pop(arrayobject *self, PyObject *args)
1001{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 Py_ssize_t i = -1;
1003 PyObject *v;
1004 if (!PyArg_ParseTuple(args, "|n:pop", &i))
1005 return NULL;
1006 if (Py_SIZE(self) == 0) {
1007 /* Special-case most common failure cause */
1008 PyErr_SetString(PyExc_IndexError, "pop from empty array");
1009 return NULL;
1010 }
1011 if (i < 0)
1012 i += Py_SIZE(self);
1013 if (i < 0 || i >= Py_SIZE(self)) {
1014 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1015 return NULL;
1016 }
1017 v = getarrayitem((PyObject *)self,i);
1018 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
1019 Py_DECREF(v);
1020 return NULL;
1021 }
1022 return v;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001023}
1024
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001025PyDoc_STRVAR(pop_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001026"pop([i])\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001027\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001028Return the i-th element and delete it from the array. i defaults to -1.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001029
1030static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001031array_extend(arrayobject *self, PyObject *bb)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 if (array_do_extend(self, bb) == -1)
1034 return NULL;
1035 Py_INCREF(Py_None);
1036 return Py_None;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001037}
1038
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001039PyDoc_STRVAR(extend_doc,
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001040"extend(array or iterable)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001041\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001042 Append items to the end of the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001043
1044static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001045array_insert(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 Py_ssize_t i;
1048 PyObject *v;
1049 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
1050 return NULL;
1051 return ins(self, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001052}
1053
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001054PyDoc_STRVAR(insert_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001055"insert(i,x)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001056\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001057Insert a new item x into the array before position i.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001058
1059
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001060static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001061array_buffer_info(arrayobject *self, PyObject *unused)
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 PyObject* retval = NULL;
1064 retval = PyTuple_New(2);
1065 if (!retval)
1066 return NULL;
Fred Drake541dc3b2000-06-28 17:49:30 +00001067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item));
1069 PyTuple_SET_ITEM(retval, 1, PyLong_FromLong((long)(Py_SIZE(self))));
Fred Drake541dc3b2000-06-28 17:49:30 +00001070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 return retval;
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001072}
1073
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001074PyDoc_STRVAR(buffer_info_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001075"buffer_info() -> (address, length)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001076\n\
1077Return a tuple (address, length) giving the current memory address and\n\
Guido van Rossum702d08e2001-07-27 16:05:32 +00001078the length in items of the buffer used to hold array's contents\n\
1079The length should be multiplied by the itemsize attribute to calculate\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001080the buffer length in bytes.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001081
1082
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001083static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001084array_append(arrayobject *self, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +00001085{
Mark Dickinson346f0af2010-08-06 09:36:57 +00001086 return ins(self, Py_SIZE(self), v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001087}
1088
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001089PyDoc_STRVAR(append_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001090"append(x)\n\
1091\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001092Append new value x to the end of the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001093
1094
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001095static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001096array_byteswap(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001097{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 char *p;
1099 Py_ssize_t i;
Fred Drakebf272981999-12-03 17:15:30 +00001100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 switch (self->ob_descr->itemsize) {
1102 case 1:
1103 break;
1104 case 2:
1105 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) {
1106 char p0 = p[0];
1107 p[0] = p[1];
1108 p[1] = p0;
1109 }
1110 break;
1111 case 4:
1112 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 4) {
1113 char p0 = p[0];
1114 char p1 = p[1];
1115 p[0] = p[3];
1116 p[1] = p[2];
1117 p[2] = p1;
1118 p[3] = p0;
1119 }
1120 break;
1121 case 8:
1122 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) {
1123 char p0 = p[0];
1124 char p1 = p[1];
1125 char p2 = p[2];
1126 char p3 = p[3];
1127 p[0] = p[7];
1128 p[1] = p[6];
1129 p[2] = p[5];
1130 p[3] = p[4];
1131 p[4] = p3;
1132 p[5] = p2;
1133 p[6] = p1;
1134 p[7] = p0;
1135 }
1136 break;
1137 default:
1138 PyErr_SetString(PyExc_RuntimeError,
1139 "don't know how to byteswap this array type");
1140 return NULL;
1141 }
1142 Py_INCREF(Py_None);
1143 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001144}
1145
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001146PyDoc_STRVAR(byteswap_doc,
Fred Drakebf272981999-12-03 17:15:30 +00001147"byteswap()\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001148\n\
Fred Drakebf272981999-12-03 17:15:30 +00001149Byteswap 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 +000011504, or 8 bytes in size, RuntimeError is raised.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001151
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001152static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001153array_reverse(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001154{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 register Py_ssize_t itemsize = self->ob_descr->itemsize;
1156 register char *p, *q;
1157 /* little buffer to hold items while swapping */
1158 char tmp[256]; /* 8 is probably enough -- but why skimp */
1159 assert((size_t)itemsize <= sizeof(tmp));
Guido van Rossume77a7571993-11-03 15:01:26 +00001160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 if (Py_SIZE(self) > 1) {
1162 for (p = self->ob_item,
1163 q = self->ob_item + (Py_SIZE(self) - 1)*itemsize;
1164 p < q;
1165 p += itemsize, q -= itemsize) {
1166 /* memory areas guaranteed disjoint, so memcpy
1167 * is safe (& memmove may be slower).
1168 */
1169 memcpy(tmp, p, itemsize);
1170 memcpy(p, q, itemsize);
1171 memcpy(q, tmp, itemsize);
1172 }
1173 }
Tim Petersbb307342000-09-10 05:22:54 +00001174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 Py_INCREF(Py_None);
1176 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001177}
Guido van Rossume77a7571993-11-03 15:01:26 +00001178
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001179PyDoc_STRVAR(reverse_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001180"reverse()\n\
1181\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001182Reverse the order of the items in the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001183
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001184
1185/* Forward */
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001186static PyObject *array_frombytes(arrayobject *self, PyObject *args);
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001187
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001188static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001189array_fromfile(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 PyObject *f, *b, *res;
1192 Py_ssize_t itemsize = self->ob_descr->itemsize;
1193 Py_ssize_t n, nbytes;
1194 int not_enough_bytes;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 if (!PyArg_ParseTuple(args, "On:fromfile", &f, &n))
1197 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 nbytes = n * itemsize;
1200 if (nbytes < 0 || nbytes/itemsize != n) {
1201 PyErr_NoMemory();
1202 return NULL;
1203 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 b = PyObject_CallMethod(f, "read", "n", nbytes);
1206 if (b == NULL)
1207 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 if (!PyBytes_Check(b)) {
1210 PyErr_SetString(PyExc_TypeError,
1211 "read() didn't return bytes");
1212 Py_DECREF(b);
1213 return NULL;
1214 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 not_enough_bytes = (PyBytes_GET_SIZE(b) != nbytes);
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 args = Py_BuildValue("(O)", b);
1219 Py_DECREF(b);
1220 if (args == NULL)
1221 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001222
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001223 res = array_frombytes(self, args);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 Py_DECREF(args);
1225 if (res == NULL)
1226 return NULL;
Hirokazu Yamamoto54d0df62009-03-06 03:04:07 +00001227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 if (not_enough_bytes) {
1229 PyErr_SetString(PyExc_EOFError,
1230 "read() didn't return enough bytes");
1231 Py_DECREF(res);
1232 return NULL;
1233 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +00001236}
1237
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001238PyDoc_STRVAR(fromfile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001239"fromfile(f, n)\n\
1240\n\
1241Read n objects from the file object f and append them to the end of the\n\
Georg Brandlf25ef502008-02-01 11:30:18 +00001242array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001243
1244
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001245static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001246array_tofile(arrayobject *self, PyObject *f)
Guido van Rossum778983b1993-02-19 15:55:02 +00001247{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize;
1249 /* Write 64K blocks at a time */
1250 /* XXX Make the block size settable */
1251 int BLOCKSIZE = 64*1024;
1252 Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE;
1253 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 if (Py_SIZE(self) == 0)
1256 goto done;
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 for (i = 0; i < nblocks; i++) {
1259 char* ptr = self->ob_item + i*BLOCKSIZE;
1260 Py_ssize_t size = BLOCKSIZE;
1261 PyObject *bytes, *res;
1262 if (i*BLOCKSIZE + size > nbytes)
1263 size = nbytes - i*BLOCKSIZE;
1264 bytes = PyBytes_FromStringAndSize(ptr, size);
1265 if (bytes == NULL)
1266 return NULL;
1267 res = PyObject_CallMethod(f, "write", "O", bytes);
1268 Py_DECREF(bytes);
1269 if (res == NULL)
1270 return NULL;
1271 Py_DECREF(res); /* drop write result */
1272 }
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001273
1274 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 Py_INCREF(Py_None);
1276 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001277}
1278
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001279PyDoc_STRVAR(tofile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001280"tofile(f)\n\
1281\n\
Georg Brandlf25ef502008-02-01 11:30:18 +00001282Write all items (as machine values) to the file object f.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001283
1284
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001285static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001286array_fromlist(arrayobject *self, PyObject *list)
Guido van Rossum778983b1993-02-19 15:55:02 +00001287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 Py_ssize_t n;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 if (!PyList_Check(list)) {
1291 PyErr_SetString(PyExc_TypeError, "arg must be list");
1292 return NULL;
1293 }
1294 n = PyList_Size(list);
1295 if (n > 0) {
1296 Py_ssize_t i, old_size;
1297 old_size = Py_SIZE(self);
1298 if (array_resize(self, old_size + n) == -1)
1299 return NULL;
1300 for (i = 0; i < n; i++) {
1301 PyObject *v = PyList_GetItem(list, i);
1302 if ((*self->ob_descr->setitem)(self,
1303 Py_SIZE(self) - n + i, v) != 0) {
1304 array_resize(self, old_size);
1305 return NULL;
1306 }
1307 }
1308 }
1309 Py_INCREF(Py_None);
1310 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001311}
1312
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001313PyDoc_STRVAR(fromlist_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001314"fromlist(list)\n\
1315\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001316Append items to array from list.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001317
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001318static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001319array_tolist(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 PyObject *list = PyList_New(Py_SIZE(self));
1322 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 if (list == NULL)
1325 return NULL;
1326 for (i = 0; i < Py_SIZE(self); i++) {
1327 PyObject *v = getarrayitem((PyObject *)self, i);
1328 if (v == NULL) {
1329 Py_DECREF(list);
1330 return NULL;
1331 }
1332 PyList_SetItem(list, i, v);
1333 }
1334 return list;
Guido van Rossum778983b1993-02-19 15:55:02 +00001335}
1336
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001337PyDoc_STRVAR(tolist_doc,
Guido van Rossumfc6aba51998-10-14 02:52:31 +00001338"tolist() -> list\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001339\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001340Convert array to an ordinary list with the same items.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001341
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001342static PyObject *
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001343frombytes(arrayobject *self, Py_buffer *buffer)
Guido van Rossum778983b1993-02-19 15:55:02 +00001344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 int itemsize = self->ob_descr->itemsize;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001346 Py_ssize_t n;
1347 if (buffer->itemsize != 1) {
1348 PyBuffer_Release(buffer);
1349 PyErr_SetString(PyExc_TypeError, "string/buffer of bytes required.");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 return NULL;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001351 }
1352 n = buffer->len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 if (n % itemsize != 0) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001354 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 PyErr_SetString(PyExc_ValueError,
1356 "string length not a multiple of item size");
1357 return NULL;
1358 }
1359 n = n / itemsize;
1360 if (n > 0) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001361 Py_ssize_t old_size = Py_SIZE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 if ((n > PY_SSIZE_T_MAX - old_size) ||
1363 ((old_size + n) > PY_SSIZE_T_MAX / itemsize)) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001364 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 return PyErr_NoMemory();
1366 }
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001367 if (array_resize(self, old_size + n) == -1) {
1368 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 return NULL;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001370 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 memcpy(self->ob_item + old_size * itemsize,
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001372 buffer->buf, n * itemsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 }
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001374 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 Py_INCREF(Py_None);
1376 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001377}
1378
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001379static PyObject *
1380array_fromstring(arrayobject *self, PyObject *args)
1381{
1382 Py_buffer buffer;
1383 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1384 "fromstring() is deprecated. Use frombytes() instead.", 2) != 0)
1385 return NULL;
1386 if (!PyArg_ParseTuple(args, "s*:fromstring", &buffer))
1387 return NULL;
1388 else
1389 return frombytes(self, &buffer);
1390}
1391
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001392PyDoc_STRVAR(fromstring_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001393"fromstring(string)\n\
1394\n\
1395Appends items from the string, interpreting it as an array of machine\n\
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001396values, as if it had been read from a file using the fromfile() method).\n\
1397\n\
1398This method is deprecated. Use frombytes instead.");
1399
1400
1401static PyObject *
1402array_frombytes(arrayobject *self, PyObject *args)
1403{
1404 Py_buffer buffer;
1405 if (!PyArg_ParseTuple(args, "y*:frombytes", &buffer))
1406 return NULL;
1407 else
1408 return frombytes(self, &buffer);
1409}
1410
1411PyDoc_STRVAR(frombytes_doc,
1412"frombytes(bytestring)\n\
1413\n\
1414Appends items from the string, interpreting it as an array of machine\n\
Walter Dörwald93b30b52007-06-22 12:21:53 +00001415values, as if it had been read from a file using the fromfile() method).");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001416
1417
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001418static PyObject *
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001419array_tobytes(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001420{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
1422 return PyBytes_FromStringAndSize(self->ob_item,
1423 Py_SIZE(self) * self->ob_descr->itemsize);
1424 } else {
1425 return PyErr_NoMemory();
1426 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001427}
1428
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001429PyDoc_STRVAR(tobytes_doc,
1430"tobytes() -> bytes\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001431\n\
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001432Convert the array to an array of machine values and return the bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001433representation.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001434
Martin v. Löwis99866332002-03-01 10:27:01 +00001435
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001436static PyObject *
1437array_tostring(arrayobject *self, PyObject *unused)
1438{
Victor Stinner9f0b51e2010-11-09 09:38:30 +00001439 if (PyErr_WarnEx(PyExc_DeprecationWarning,
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001440 "tostring() is deprecated. Use tobytes() instead.", 2) != 0)
1441 return NULL;
1442 return array_tobytes(self, unused);
1443}
1444
1445PyDoc_STRVAR(tostring_doc,
1446"tostring() -> bytes\n\
1447\n\
1448Convert the array to an array of machine values and return the bytes\n\
1449representation.\n\
1450\n\
1451This method is deprecated. Use tobytes instead.");
1452
Martin v. Löwis99866332002-03-01 10:27:01 +00001453
Martin v. Löwis99866332002-03-01 10:27:01 +00001454static PyObject *
1455array_fromunicode(arrayobject *self, PyObject *args)
1456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 Py_UNICODE *ustr;
1458 Py_ssize_t n;
Amaury Forgeot d'Arc24aa26b2010-11-25 08:13:35 +00001459 Py_UNICODE typecode;
Martin v. Löwis99866332002-03-01 10:27:01 +00001460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
1462 return NULL;
1463 typecode = self->ob_descr->typecode;
1464 if ((typecode != 'u')) {
1465 PyErr_SetString(PyExc_ValueError,
1466 "fromunicode() may only be called on "
1467 "unicode type arrays");
1468 return NULL;
1469 }
1470 if (n > 0) {
1471 Py_ssize_t old_size = Py_SIZE(self);
1472 if (array_resize(self, old_size + n) == -1)
1473 return NULL;
1474 memcpy(self->ob_item + old_size * sizeof(Py_UNICODE),
1475 ustr, n * sizeof(Py_UNICODE));
1476 }
Martin v. Löwis99866332002-03-01 10:27:01 +00001477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 Py_INCREF(Py_None);
1479 return Py_None;
Martin v. Löwis99866332002-03-01 10:27:01 +00001480}
1481
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001482PyDoc_STRVAR(fromunicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001483"fromunicode(ustr)\n\
1484\n\
1485Extends this array with data from the unicode string ustr.\n\
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001486The array must be a unicode type array; otherwise a ValueError\n\
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001487is raised. Use array.frombytes(ustr.decode(...)) to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001488append Unicode data to an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001489
1490
1491static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001492array_tounicode(arrayobject *self, PyObject *unused)
Martin v. Löwis99866332002-03-01 10:27:01 +00001493{
Amaury Forgeot d'Arc24aa26b2010-11-25 08:13:35 +00001494 Py_UNICODE typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 typecode = self->ob_descr->typecode;
1496 if ((typecode != 'u')) {
1497 PyErr_SetString(PyExc_ValueError,
1498 "tounicode() may only be called on unicode type arrays");
1499 return NULL;
1500 }
1501 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_SIZE(self));
Martin v. Löwis99866332002-03-01 10:27:01 +00001502}
1503
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001504PyDoc_STRVAR(tounicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001505"tounicode() -> unicode\n\
1506\n\
1507Convert the array to a unicode string. The array must be\n\
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001508a unicode type array; otherwise a ValueError is raised. Use\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001509array.tostring().decode() to obtain a unicode string from\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001510an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001511
Martin v. Löwis99866332002-03-01 10:27:01 +00001512
1513
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001514/*********************** Pickling support ************************/
1515
1516enum machine_format_code {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 UNKNOWN_FORMAT = -1,
1518 /* UNKNOWN_FORMAT is used to indicate that the machine format for an
1519 * array type code cannot be interpreted. When this occurs, a list of
1520 * Python objects is used to represent the content of the array
1521 * instead of using the memory content of the array directly. In that
1522 * case, the array_reconstructor mechanism is bypassed completely, and
1523 * the standard array constructor is used instead.
1524 *
1525 * This is will most likely occur when the machine doesn't use IEEE
1526 * floating-point numbers.
1527 */
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 UNSIGNED_INT8 = 0,
1530 SIGNED_INT8 = 1,
1531 UNSIGNED_INT16_LE = 2,
1532 UNSIGNED_INT16_BE = 3,
1533 SIGNED_INT16_LE = 4,
1534 SIGNED_INT16_BE = 5,
1535 UNSIGNED_INT32_LE = 6,
1536 UNSIGNED_INT32_BE = 7,
1537 SIGNED_INT32_LE = 8,
1538 SIGNED_INT32_BE = 9,
1539 UNSIGNED_INT64_LE = 10,
1540 UNSIGNED_INT64_BE = 11,
1541 SIGNED_INT64_LE = 12,
1542 SIGNED_INT64_BE = 13,
1543 IEEE_754_FLOAT_LE = 14,
1544 IEEE_754_FLOAT_BE = 15,
1545 IEEE_754_DOUBLE_LE = 16,
1546 IEEE_754_DOUBLE_BE = 17,
1547 UTF16_LE = 18,
1548 UTF16_BE = 19,
1549 UTF32_LE = 20,
1550 UTF32_BE = 21
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001551};
1552#define MACHINE_FORMAT_CODE_MIN 0
1553#define MACHINE_FORMAT_CODE_MAX 21
1554
1555static const struct mformatdescr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 size_t size;
1557 int is_signed;
1558 int is_big_endian;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001559} mformat_descriptors[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 {1, 0, 0}, /* 0: UNSIGNED_INT8 */
1561 {1, 1, 0}, /* 1: SIGNED_INT8 */
1562 {2, 0, 0}, /* 2: UNSIGNED_INT16_LE */
1563 {2, 0, 1}, /* 3: UNSIGNED_INT16_BE */
1564 {2, 1, 0}, /* 4: SIGNED_INT16_LE */
1565 {2, 1, 1}, /* 5: SIGNED_INT16_BE */
1566 {4, 0, 0}, /* 6: UNSIGNED_INT32_LE */
1567 {4, 0, 1}, /* 7: UNSIGNED_INT32_BE */
1568 {4, 1, 0}, /* 8: SIGNED_INT32_LE */
1569 {4, 1, 1}, /* 9: SIGNED_INT32_BE */
1570 {8, 0, 0}, /* 10: UNSIGNED_INT64_LE */
1571 {8, 0, 1}, /* 11: UNSIGNED_INT64_BE */
1572 {8, 1, 0}, /* 12: SIGNED_INT64_LE */
1573 {8, 1, 1}, /* 13: SIGNED_INT64_BE */
1574 {4, 0, 0}, /* 14: IEEE_754_FLOAT_LE */
1575 {4, 0, 1}, /* 15: IEEE_754_FLOAT_BE */
1576 {8, 0, 0}, /* 16: IEEE_754_DOUBLE_LE */
1577 {8, 0, 1}, /* 17: IEEE_754_DOUBLE_BE */
1578 {4, 0, 0}, /* 18: UTF16_LE */
1579 {4, 0, 1}, /* 19: UTF16_BE */
1580 {8, 0, 0}, /* 20: UTF32_LE */
1581 {8, 0, 1} /* 21: UTF32_BE */
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001582};
1583
1584
1585/*
1586 * Internal: This function is used to find the machine format of a given
1587 * array type code. This returns UNKNOWN_FORMAT when the machine format cannot
1588 * be found.
1589 */
1590static enum machine_format_code
1591typecode_to_mformat_code(int typecode)
1592{
Alexandre Vassalotti7aaa7702009-07-17 03:51:27 +00001593#ifdef WORDS_BIGENDIAN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 const int is_big_endian = 1;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001595#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 const int is_big_endian = 0;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001597#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 size_t intsize;
1599 int is_signed;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 switch (typecode) {
1602 case 'b':
1603 return SIGNED_INT8;
1604 case 'B':
1605 return UNSIGNED_INT8;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 case 'u':
1608 if (sizeof(Py_UNICODE) == 2) {
1609 return UTF16_LE + is_big_endian;
1610 }
1611 if (sizeof(Py_UNICODE) == 4) {
1612 return UTF32_LE + is_big_endian;
1613 }
1614 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 case 'f':
1617 if (sizeof(float) == 4) {
1618 const float y = 16711938.0;
1619 if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0)
1620 return IEEE_754_FLOAT_BE;
1621 if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0)
1622 return IEEE_754_FLOAT_LE;
1623 }
1624 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 case 'd':
1627 if (sizeof(double) == 8) {
1628 const double x = 9006104071832581.0;
1629 if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0)
1630 return IEEE_754_DOUBLE_BE;
1631 if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
1632 return IEEE_754_DOUBLE_LE;
1633 }
1634 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 /* Integers */
1637 case 'h':
1638 intsize = sizeof(short);
1639 is_signed = 1;
1640 break;
1641 case 'H':
1642 intsize = sizeof(short);
1643 is_signed = 0;
1644 break;
1645 case 'i':
1646 intsize = sizeof(int);
1647 is_signed = 1;
1648 break;
1649 case 'I':
1650 intsize = sizeof(int);
1651 is_signed = 0;
1652 break;
1653 case 'l':
1654 intsize = sizeof(long);
1655 is_signed = 1;
1656 break;
1657 case 'L':
1658 intsize = sizeof(long);
1659 is_signed = 0;
1660 break;
1661 default:
1662 return UNKNOWN_FORMAT;
1663 }
1664 switch (intsize) {
1665 case 2:
1666 return UNSIGNED_INT16_LE + is_big_endian + (2 * is_signed);
1667 case 4:
1668 return UNSIGNED_INT32_LE + is_big_endian + (2 * is_signed);
1669 case 8:
1670 return UNSIGNED_INT64_LE + is_big_endian + (2 * is_signed);
1671 default:
1672 return UNKNOWN_FORMAT;
1673 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001674}
1675
1676/* Forward declaration. */
1677static PyObject *array_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
1678
1679/*
1680 * Internal: This function wraps the array constructor--i.e., array_new()--to
1681 * allow the creation of array objects from C code without having to deal
1682 * directly the tuple argument of array_new(). The typecode argument is a
1683 * Unicode character value, like 'i' or 'f' for example, representing an array
1684 * type code. The items argument is a bytes or a list object from which
1685 * contains the initial value of the array.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 *
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001687 * On success, this functions returns the array object created. Otherwise,
1688 * NULL is returned to indicate a failure.
1689 */
1690static PyObject *
Victor Stinner9f0b51e2010-11-09 09:38:30 +00001691make_array(PyTypeObject *arraytype, Py_UNICODE typecode, PyObject *items)
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001692{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 PyObject *new_args;
1694 PyObject *array_obj;
1695 PyObject *typecode_obj;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 assert(arraytype != NULL);
1698 assert(items != NULL);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001699
Victor Stinner9f0b51e2010-11-09 09:38:30 +00001700 typecode_obj = PyUnicode_FromUnicode(&typecode, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 if (typecode_obj == NULL)
1702 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 new_args = PyTuple_New(2);
1705 if (new_args == NULL)
1706 return NULL;
1707 Py_INCREF(items);
1708 PyTuple_SET_ITEM(new_args, 0, typecode_obj);
1709 PyTuple_SET_ITEM(new_args, 1, items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 array_obj = array_new(arraytype, new_args, NULL);
1712 Py_DECREF(new_args);
1713 if (array_obj == NULL)
1714 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 return array_obj;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001717}
1718
1719/*
1720 * This functions is a special constructor used when unpickling an array. It
1721 * provides a portable way to rebuild an array from its memory representation.
1722 */
1723static PyObject *
1724array_reconstructor(PyObject *self, PyObject *args)
1725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 PyTypeObject *arraytype;
1727 PyObject *items;
1728 PyObject *converted_items;
1729 PyObject *result;
Victor Stinner9f0b51e2010-11-09 09:38:30 +00001730 int typecode_int;
1731 Py_UNICODE typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 enum machine_format_code mformat_code;
1733 struct arraydescr *descr;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 if (!PyArg_ParseTuple(args, "OCiO:array._array_reconstructor",
Victor Stinner9f0b51e2010-11-09 09:38:30 +00001736 &arraytype, &typecode_int, &mformat_code, &items))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001738
Victor Stinner9f0b51e2010-11-09 09:38:30 +00001739 typecode = (Py_UNICODE)typecode_int;
1740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 if (!PyType_Check(arraytype)) {
1742 PyErr_Format(PyExc_TypeError,
1743 "first argument must a type object, not %.200s",
1744 Py_TYPE(arraytype)->tp_name);
1745 return NULL;
1746 }
1747 if (!PyType_IsSubtype(arraytype, &Arraytype)) {
1748 PyErr_Format(PyExc_TypeError,
1749 "%.200s is not a subtype of %.200s",
1750 arraytype->tp_name, Arraytype.tp_name);
1751 return NULL;
1752 }
1753 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1754 if (descr->typecode == typecode)
1755 break;
1756 }
1757 if (descr->typecode == '\0') {
1758 PyErr_SetString(PyExc_ValueError,
1759 "second argument must be a valid type code");
1760 return NULL;
1761 }
1762 if (mformat_code < MACHINE_FORMAT_CODE_MIN ||
1763 mformat_code > MACHINE_FORMAT_CODE_MAX) {
1764 PyErr_SetString(PyExc_ValueError,
1765 "third argument must be a valid machine format code.");
1766 return NULL;
1767 }
1768 if (!PyBytes_Check(items)) {
1769 PyErr_Format(PyExc_TypeError,
1770 "fourth argument should be bytes, not %.200s",
1771 Py_TYPE(items)->tp_name);
1772 return NULL;
1773 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 /* Fast path: No decoding has to be done. */
1776 if (mformat_code == typecode_to_mformat_code(typecode) ||
1777 mformat_code == UNKNOWN_FORMAT) {
1778 return make_array(arraytype, typecode, items);
1779 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 /* Slow path: Decode the byte string according to the given machine
1782 * format code. This occurs when the computer unpickling the array
1783 * object is architecturally different from the one that pickled the
1784 * array.
1785 */
1786 if (Py_SIZE(items) % mformat_descriptors[mformat_code].size != 0) {
1787 PyErr_SetString(PyExc_ValueError,
1788 "string length not a multiple of item size");
1789 return NULL;
1790 }
1791 switch (mformat_code) {
1792 case IEEE_754_FLOAT_LE:
1793 case IEEE_754_FLOAT_BE: {
1794 int i;
1795 int le = (mformat_code == IEEE_754_FLOAT_LE) ? 1 : 0;
1796 Py_ssize_t itemcount = Py_SIZE(items) / 4;
1797 const unsigned char *memstr =
1798 (unsigned char *)PyBytes_AS_STRING(items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 converted_items = PyList_New(itemcount);
1801 if (converted_items == NULL)
1802 return NULL;
1803 for (i = 0; i < itemcount; i++) {
1804 PyObject *pyfloat = PyFloat_FromDouble(
1805 _PyFloat_Unpack4(&memstr[i * 4], le));
1806 if (pyfloat == NULL) {
1807 Py_DECREF(converted_items);
1808 return NULL;
1809 }
1810 PyList_SET_ITEM(converted_items, i, pyfloat);
1811 }
1812 break;
1813 }
1814 case IEEE_754_DOUBLE_LE:
1815 case IEEE_754_DOUBLE_BE: {
1816 int i;
1817 int le = (mformat_code == IEEE_754_DOUBLE_LE) ? 1 : 0;
1818 Py_ssize_t itemcount = Py_SIZE(items) / 8;
1819 const unsigned char *memstr =
1820 (unsigned char *)PyBytes_AS_STRING(items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 converted_items = PyList_New(itemcount);
1823 if (converted_items == NULL)
1824 return NULL;
1825 for (i = 0; i < itemcount; i++) {
1826 PyObject *pyfloat = PyFloat_FromDouble(
1827 _PyFloat_Unpack8(&memstr[i * 8], le));
1828 if (pyfloat == NULL) {
1829 Py_DECREF(converted_items);
1830 return NULL;
1831 }
1832 PyList_SET_ITEM(converted_items, i, pyfloat);
1833 }
1834 break;
1835 }
1836 case UTF16_LE:
1837 case UTF16_BE: {
1838 int byteorder = (mformat_code == UTF16_LE) ? -1 : 1;
1839 converted_items = PyUnicode_DecodeUTF16(
1840 PyBytes_AS_STRING(items), Py_SIZE(items),
1841 "strict", &byteorder);
1842 if (converted_items == NULL)
1843 return NULL;
1844 break;
1845 }
1846 case UTF32_LE:
1847 case UTF32_BE: {
1848 int byteorder = (mformat_code == UTF32_LE) ? -1 : 1;
1849 converted_items = PyUnicode_DecodeUTF32(
1850 PyBytes_AS_STRING(items), Py_SIZE(items),
1851 "strict", &byteorder);
1852 if (converted_items == NULL)
1853 return NULL;
1854 break;
1855 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 case UNSIGNED_INT8:
1858 case SIGNED_INT8:
1859 case UNSIGNED_INT16_LE:
1860 case UNSIGNED_INT16_BE:
1861 case SIGNED_INT16_LE:
1862 case SIGNED_INT16_BE:
1863 case UNSIGNED_INT32_LE:
1864 case UNSIGNED_INT32_BE:
1865 case SIGNED_INT32_LE:
1866 case SIGNED_INT32_BE:
1867 case UNSIGNED_INT64_LE:
1868 case UNSIGNED_INT64_BE:
1869 case SIGNED_INT64_LE:
1870 case SIGNED_INT64_BE: {
1871 int i;
1872 const struct mformatdescr mf_descr =
1873 mformat_descriptors[mformat_code];
1874 Py_ssize_t itemcount = Py_SIZE(items) / mf_descr.size;
1875 const unsigned char *memstr =
1876 (unsigned char *)PyBytes_AS_STRING(items);
1877 struct arraydescr *descr;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 /* If possible, try to pack array's items using a data type
1880 * that fits better. This may result in an array with narrower
1881 * or wider elements.
1882 *
1883 * For example, if a 32-bit machine pickles a L-code array of
1884 * unsigned longs, then the array will be unpickled by 64-bit
1885 * machine as an I-code array of unsigned ints.
1886 *
1887 * XXX: Is it possible to write a unit test for this?
1888 */
1889 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1890 if (descr->is_integer_type &&
1891 descr->itemsize == mf_descr.size &&
1892 descr->is_signed == mf_descr.is_signed)
1893 typecode = descr->typecode;
1894 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 converted_items = PyList_New(itemcount);
1897 if (converted_items == NULL)
1898 return NULL;
1899 for (i = 0; i < itemcount; i++) {
1900 PyObject *pylong;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 pylong = _PyLong_FromByteArray(
1903 &memstr[i * mf_descr.size],
1904 mf_descr.size,
1905 !mf_descr.is_big_endian,
1906 mf_descr.is_signed);
1907 if (pylong == NULL) {
1908 Py_DECREF(converted_items);
1909 return NULL;
1910 }
1911 PyList_SET_ITEM(converted_items, i, pylong);
1912 }
1913 break;
1914 }
1915 case UNKNOWN_FORMAT:
1916 /* Impossible, but needed to shut up GCC about the unhandled
1917 * enumeration value.
1918 */
1919 default:
1920 PyErr_BadArgument();
1921 return NULL;
1922 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 result = make_array(arraytype, typecode, converted_items);
1925 Py_DECREF(converted_items);
1926 return result;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001927}
1928
1929static PyObject *
1930array_reduce_ex(arrayobject *array, PyObject *value)
1931{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 PyObject *dict;
1933 PyObject *result;
1934 PyObject *array_str;
1935 int typecode = array->ob_descr->typecode;
1936 int mformat_code;
1937 static PyObject *array_reconstructor = NULL;
1938 long protocol;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 if (array_reconstructor == NULL) {
1941 PyObject *array_module = PyImport_ImportModule("array");
1942 if (array_module == NULL)
1943 return NULL;
1944 array_reconstructor = PyObject_GetAttrString(
1945 array_module,
1946 "_array_reconstructor");
1947 Py_DECREF(array_module);
1948 if (array_reconstructor == NULL)
1949 return NULL;
1950 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 if (!PyLong_Check(value)) {
1953 PyErr_SetString(PyExc_TypeError,
1954 "__reduce_ex__ argument should an integer");
1955 return NULL;
1956 }
1957 protocol = PyLong_AsLong(value);
1958 if (protocol == -1 && PyErr_Occurred())
1959 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 dict = PyObject_GetAttrString((PyObject *)array, "__dict__");
1962 if (dict == NULL) {
1963 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1964 return NULL;
1965 PyErr_Clear();
1966 dict = Py_None;
1967 Py_INCREF(dict);
1968 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 mformat_code = typecode_to_mformat_code(typecode);
1971 if (mformat_code == UNKNOWN_FORMAT || protocol < 3) {
1972 /* Convert the array to a list if we got something weird
1973 * (e.g., non-IEEE floats), or we are pickling the array using
1974 * a Python 2.x compatible protocol.
1975 *
1976 * It is necessary to use a list representation for Python 2.x
1977 * compatible pickle protocol, since Python 2's str objects
1978 * are unpickled as unicode by Python 3. Thus it is impossible
1979 * to make arrays unpicklable by Python 3 by using their memory
1980 * representation, unless we resort to ugly hacks such as
1981 * coercing unicode objects to bytes in array_reconstructor.
1982 */
1983 PyObject *list;
1984 list = array_tolist(array, NULL);
1985 if (list == NULL) {
1986 Py_DECREF(dict);
1987 return NULL;
1988 }
1989 result = Py_BuildValue(
1990 "O(CO)O", Py_TYPE(array), typecode, list, dict);
1991 Py_DECREF(list);
1992 Py_DECREF(dict);
1993 return result;
1994 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001995
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001996 array_str = array_tobytes(array, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 if (array_str == NULL) {
1998 Py_DECREF(dict);
1999 return NULL;
2000 }
2001 result = Py_BuildValue(
2002 "O(OCiN)O", array_reconstructor, Py_TYPE(array), typecode,
2003 mformat_code, array_str, dict);
2004 Py_DECREF(dict);
2005 return result;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002006}
2007
2008PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2009
Martin v. Löwis99866332002-03-01 10:27:01 +00002010static PyObject *
2011array_get_typecode(arrayobject *a, void *closure)
2012{
Amaury Forgeot d'Arc24aa26b2010-11-25 08:13:35 +00002013 Py_UNICODE tc = a->ob_descr->typecode;
2014 return PyUnicode_FromUnicode(&tc, 1);
Martin v. Löwis99866332002-03-01 10:27:01 +00002015}
2016
2017static PyObject *
2018array_get_itemsize(arrayobject *a, void *closure)
2019{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 return PyLong_FromLong((long)a->ob_descr->itemsize);
Martin v. Löwis99866332002-03-01 10:27:01 +00002021}
2022
2023static PyGetSetDef array_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 {"typecode", (getter) array_get_typecode, NULL,
2025 "the typecode character used to create the array"},
2026 {"itemsize", (getter) array_get_itemsize, NULL,
2027 "the size, in bytes, of one array item"},
2028 {NULL}
Martin v. Löwis99866332002-03-01 10:27:01 +00002029};
2030
Martin v. Löwis59683e82008-06-13 07:50:45 +00002031static PyMethodDef array_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 {"append", (PyCFunction)array_append, METH_O,
2033 append_doc},
2034 {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS,
2035 buffer_info_doc},
2036 {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
2037 byteswap_doc},
2038 {"__copy__", (PyCFunction)array_copy, METH_NOARGS,
2039 copy_doc},
2040 {"count", (PyCFunction)array_count, METH_O,
2041 count_doc},
2042 {"__deepcopy__",(PyCFunction)array_copy, METH_O,
2043 copy_doc},
2044 {"extend", (PyCFunction)array_extend, METH_O,
2045 extend_doc},
2046 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
2047 fromfile_doc},
2048 {"fromlist", (PyCFunction)array_fromlist, METH_O,
2049 fromlist_doc},
2050 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
2051 fromstring_doc},
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00002052 {"frombytes", (PyCFunction)array_frombytes, METH_VARARGS,
2053 frombytes_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
2055 fromunicode_doc},
2056 {"index", (PyCFunction)array_index, METH_O,
2057 index_doc},
2058 {"insert", (PyCFunction)array_insert, METH_VARARGS,
2059 insert_doc},
2060 {"pop", (PyCFunction)array_pop, METH_VARARGS,
2061 pop_doc},
2062 {"__reduce_ex__", (PyCFunction)array_reduce_ex, METH_O,
2063 reduce_doc},
2064 {"remove", (PyCFunction)array_remove, METH_O,
2065 remove_doc},
2066 {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
2067 reverse_doc},
2068/* {"sort", (PyCFunction)array_sort, METH_VARARGS,
2069 sort_doc},*/
2070 {"tofile", (PyCFunction)array_tofile, METH_O,
2071 tofile_doc},
2072 {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
2073 tolist_doc},
2074 {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
2075 tostring_doc},
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00002076 {"tobytes", (PyCFunction)array_tobytes, METH_NOARGS,
2077 tobytes_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002078 {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
2079 tounicode_doc},
2080 {NULL, NULL} /* sentinel */
Guido van Rossum778983b1993-02-19 15:55:02 +00002081};
2082
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002083static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00002084array_repr(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +00002085{
Amaury Forgeot d'Arc24aa26b2010-11-25 08:13:35 +00002086 Py_UNICODE typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 PyObject *s, *v = NULL;
2088 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00002089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 len = Py_SIZE(a);
2091 typecode = a->ob_descr->typecode;
2092 if (len == 0) {
Amaury Forgeot d'Arc24aa26b2010-11-25 08:13:35 +00002093 return PyUnicode_FromFormat("array('%c')", (int)typecode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 }
2095 if ((typecode == 'u'))
2096 v = array_tounicode(a, NULL);
2097 else
2098 v = array_tolist(a, NULL);
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00002099
Amaury Forgeot d'Arc24aa26b2010-11-25 08:13:35 +00002100 s = PyUnicode_FromFormat("array('%c', %R)", (int)typecode, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 Py_DECREF(v);
2102 return s;
Guido van Rossum778983b1993-02-19 15:55:02 +00002103}
2104
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002105static PyObject*
2106array_subscr(arrayobject* self, PyObject* item)
2107{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 if (PyIndex_Check(item)) {
2109 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2110 if (i==-1 && PyErr_Occurred()) {
2111 return NULL;
2112 }
2113 if (i < 0)
2114 i += Py_SIZE(self);
2115 return array_item(self, i);
2116 }
2117 else if (PySlice_Check(item)) {
2118 Py_ssize_t start, stop, step, slicelength, cur, i;
2119 PyObject* result;
2120 arrayobject* ar;
2121 int itemsize = self->ob_descr->itemsize;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002122
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002123 if (PySlice_GetIndicesEx(item, Py_SIZE(self),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 &start, &stop, &step, &slicelength) < 0) {
2125 return NULL;
2126 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002128 if (slicelength <= 0) {
2129 return newarrayobject(&Arraytype, 0, self->ob_descr);
2130 }
2131 else if (step == 1) {
2132 PyObject *result = newarrayobject(&Arraytype,
2133 slicelength, self->ob_descr);
2134 if (result == NULL)
2135 return NULL;
2136 memcpy(((arrayobject *)result)->ob_item,
2137 self->ob_item + start * itemsize,
2138 slicelength * itemsize);
2139 return result;
2140 }
2141 else {
2142 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
2143 if (!result) return NULL;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145 ar = (arrayobject*)result;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 for (cur = start, i = 0; i < slicelength;
2148 cur += step, i++) {
2149 memcpy(ar->ob_item + i*itemsize,
2150 self->ob_item + cur*itemsize,
2151 itemsize);
2152 }
2153
2154 return result;
2155 }
2156 }
2157 else {
2158 PyErr_SetString(PyExc_TypeError,
2159 "array indices must be integers");
2160 return NULL;
2161 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002162}
2163
2164static int
2165array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
2166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 Py_ssize_t start, stop, step, slicelength, needed;
2168 arrayobject* other;
2169 int itemsize;
Thomas Woutersed03b412007-08-28 21:37:11 +00002170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 if (PyIndex_Check(item)) {
2172 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Alexandre Vassalotti47137252009-07-05 19:57:00 +00002173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 if (i == -1 && PyErr_Occurred())
2175 return -1;
2176 if (i < 0)
2177 i += Py_SIZE(self);
2178 if (i < 0 || i >= Py_SIZE(self)) {
2179 PyErr_SetString(PyExc_IndexError,
2180 "array assignment index out of range");
2181 return -1;
2182 }
2183 if (value == NULL) {
2184 /* Fall through to slice assignment */
2185 start = i;
2186 stop = i + 1;
2187 step = 1;
2188 slicelength = 1;
2189 }
2190 else
2191 return (*self->ob_descr->setitem)(self, i, value);
2192 }
2193 else if (PySlice_Check(item)) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002194 if (PySlice_GetIndicesEx(item,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 Py_SIZE(self), &start, &stop,
2196 &step, &slicelength) < 0) {
2197 return -1;
2198 }
2199 }
2200 else {
2201 PyErr_SetString(PyExc_TypeError,
2202 "array indices must be integer");
2203 return -1;
2204 }
2205 if (value == NULL) {
2206 other = NULL;
2207 needed = 0;
2208 }
2209 else if (array_Check(value)) {
2210 other = (arrayobject *)value;
2211 needed = Py_SIZE(other);
2212 if (self == other) {
2213 /* Special case "self[i:j] = self" -- copy self first */
2214 int ret;
2215 value = array_slice(other, 0, needed);
2216 if (value == NULL)
2217 return -1;
2218 ret = array_ass_subscr(self, item, value);
2219 Py_DECREF(value);
2220 return ret;
2221 }
2222 if (other->ob_descr != self->ob_descr) {
2223 PyErr_BadArgument();
2224 return -1;
2225 }
2226 }
2227 else {
2228 PyErr_Format(PyExc_TypeError,
2229 "can only assign array (not \"%.200s\") to array slice",
2230 Py_TYPE(value)->tp_name);
2231 return -1;
2232 }
2233 itemsize = self->ob_descr->itemsize;
2234 /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
2235 if ((step > 0 && stop < start) ||
2236 (step < 0 && stop > start))
2237 stop = start;
Alexandre Vassalotti47137252009-07-05 19:57:00 +00002238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002239 /* Issue #4509: If the array has exported buffers and the slice
2240 assignment would change the size of the array, fail early to make
2241 sure we don't modify it. */
2242 if ((needed == 0 || slicelength != needed) && self->ob_exports > 0) {
2243 PyErr_SetString(PyExc_BufferError,
2244 "cannot resize an array that is exporting buffers");
2245 return -1;
2246 }
Mark Dickinsonbc099642010-01-29 17:27:24 +00002247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 if (step == 1) {
2249 if (slicelength > needed) {
2250 memmove(self->ob_item + (start + needed) * itemsize,
2251 self->ob_item + stop * itemsize,
2252 (Py_SIZE(self) - stop) * itemsize);
2253 if (array_resize(self, Py_SIZE(self) +
2254 needed - slicelength) < 0)
2255 return -1;
2256 }
2257 else if (slicelength < needed) {
2258 if (array_resize(self, Py_SIZE(self) +
2259 needed - slicelength) < 0)
2260 return -1;
2261 memmove(self->ob_item + (start + needed) * itemsize,
2262 self->ob_item + stop * itemsize,
2263 (Py_SIZE(self) - start - needed) * itemsize);
2264 }
2265 if (needed > 0)
2266 memcpy(self->ob_item + start * itemsize,
2267 other->ob_item, needed * itemsize);
2268 return 0;
2269 }
2270 else if (needed == 0) {
2271 /* Delete slice */
2272 size_t cur;
2273 Py_ssize_t i;
Thomas Woutersed03b412007-08-28 21:37:11 +00002274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 if (step < 0) {
2276 stop = start + 1;
2277 start = stop + step * (slicelength - 1) - 1;
2278 step = -step;
2279 }
2280 for (cur = start, i = 0; i < slicelength;
2281 cur += step, i++) {
2282 Py_ssize_t lim = step - 1;
Thomas Woutersed03b412007-08-28 21:37:11 +00002283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 if (cur + step >= (size_t)Py_SIZE(self))
2285 lim = Py_SIZE(self) - cur - 1;
2286 memmove(self->ob_item + (cur - i) * itemsize,
2287 self->ob_item + (cur + 1) * itemsize,
2288 lim * itemsize);
2289 }
2290 cur = start + slicelength * step;
2291 if (cur < (size_t)Py_SIZE(self)) {
2292 memmove(self->ob_item + (cur-slicelength) * itemsize,
2293 self->ob_item + cur * itemsize,
2294 (Py_SIZE(self) - cur) * itemsize);
2295 }
2296 if (array_resize(self, Py_SIZE(self) - slicelength) < 0)
2297 return -1;
2298 return 0;
2299 }
2300 else {
2301 Py_ssize_t cur, i;
2302
2303 if (needed != slicelength) {
2304 PyErr_Format(PyExc_ValueError,
2305 "attempt to assign array of size %zd "
2306 "to extended slice of size %zd",
2307 needed, slicelength);
2308 return -1;
2309 }
2310 for (cur = start, i = 0; i < slicelength;
2311 cur += step, i++) {
2312 memcpy(self->ob_item + cur * itemsize,
2313 other->ob_item + i * itemsize,
2314 itemsize);
2315 }
2316 return 0;
2317 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002318}
2319
2320static PyMappingMethods array_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 (lenfunc)array_length,
2322 (binaryfunc)array_subscr,
2323 (objobjargproc)array_ass_subscr
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002324};
2325
Guido van Rossumd8faa362007-04-27 19:54:29 +00002326static const void *emptybuf = "";
2327
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002328
2329static int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00002330array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 if (view==NULL) goto finish;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 view->buf = (void *)self->ob_item;
2335 view->obj = (PyObject*)self;
2336 Py_INCREF(self);
2337 if (view->buf == NULL)
2338 view->buf = (void *)emptybuf;
2339 view->len = (Py_SIZE(self)) * self->ob_descr->itemsize;
2340 view->readonly = 0;
2341 view->ndim = 1;
2342 view->itemsize = self->ob_descr->itemsize;
2343 view->suboffsets = NULL;
2344 view->shape = NULL;
2345 if ((flags & PyBUF_ND)==PyBUF_ND) {
2346 view->shape = &((Py_SIZE(self)));
2347 }
2348 view->strides = NULL;
2349 if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES)
2350 view->strides = &(view->itemsize);
2351 view->format = NULL;
2352 view->internal = NULL;
2353 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
2354 view->format = self->ob_descr->formats;
Travis E. Oliphantddacf962007-10-13 21:03:27 +00002355#ifdef Py_UNICODE_WIDE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 if (self->ob_descr->typecode == 'u') {
2357 view->format = "w";
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002358 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359#endif
2360 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002361
2362 finish:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 self->ob_exports++;
2364 return 0;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002365}
2366
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002367static void
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00002368array_buffer_relbuf(arrayobject *self, Py_buffer *view)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 self->ob_exports--;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002371}
2372
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002373static PySequenceMethods array_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 (lenfunc)array_length, /*sq_length*/
2375 (binaryfunc)array_concat, /*sq_concat*/
2376 (ssizeargfunc)array_repeat, /*sq_repeat*/
2377 (ssizeargfunc)array_item, /*sq_item*/
2378 0, /*sq_slice*/
2379 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
2380 0, /*sq_ass_slice*/
2381 (objobjproc)array_contains, /*sq_contains*/
2382 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
2383 (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/
Guido van Rossum778983b1993-02-19 15:55:02 +00002384};
2385
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002386static PyBufferProcs array_as_buffer = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 (getbufferproc)array_buffer_getbuf,
2388 (releasebufferproc)array_buffer_relbuf
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002389};
2390
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002391static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +00002392array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum778983b1993-02-19 15:55:02 +00002393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 int c;
2395 PyObject *initial = NULL, *it = NULL;
2396 struct arraydescr *descr;
Martin v. Löwis99866332002-03-01 10:27:01 +00002397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds))
2399 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00002400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
2402 return NULL;
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00002403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 if (!(initial == NULL || PyList_Check(initial)
2405 || PyByteArray_Check(initial)
2406 || PyBytes_Check(initial)
2407 || PyTuple_Check(initial)
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002408 || ((c=='u') && PyUnicode_Check(initial))
2409 || (array_Check(initial)
2410 && c == ((arrayobject*)initial)->ob_descr->typecode))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 it = PyObject_GetIter(initial);
2412 if (it == NULL)
2413 return NULL;
2414 /* We set initial to NULL so that the subsequent code
2415 will create an empty array of the appropriate type
2416 and afterwards we can use array_iter_extend to populate
2417 the array.
2418 */
2419 initial = NULL;
2420 }
2421 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2422 if (descr->typecode == c) {
2423 PyObject *a;
2424 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00002425
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002426 if (initial == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 len = 0;
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002428 else if (PyList_Check(initial))
2429 len = PyList_GET_SIZE(initial);
2430 else if (PyTuple_Check(initial) || array_Check(initial))
2431 len = Py_SIZE(initial);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 else
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002433 len = 0;
Martin v. Löwis99866332002-03-01 10:27:01 +00002434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 a = newarrayobject(type, len, descr);
2436 if (a == NULL)
2437 return NULL;
2438
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002439 if (len > 0 && !array_Check(initial)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 Py_ssize_t i;
2441 for (i = 0; i < len; i++) {
2442 PyObject *v =
2443 PySequence_GetItem(initial, i);
2444 if (v == NULL) {
2445 Py_DECREF(a);
2446 return NULL;
2447 }
2448 if (setarrayitem(a, i, v) != 0) {
2449 Py_DECREF(v);
2450 Py_DECREF(a);
2451 return NULL;
2452 }
2453 Py_DECREF(v);
2454 }
2455 }
2456 else if (initial != NULL && (PyByteArray_Check(initial) ||
2457 PyBytes_Check(initial))) {
2458 PyObject *t_initial, *v;
2459 t_initial = PyTuple_Pack(1, initial);
2460 if (t_initial == NULL) {
2461 Py_DECREF(a);
2462 return NULL;
2463 }
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00002464 v = array_frombytes((arrayobject *)a,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 t_initial);
2466 Py_DECREF(t_initial);
2467 if (v == NULL) {
2468 Py_DECREF(a);
2469 return NULL;
2470 }
2471 Py_DECREF(v);
2472 }
2473 else if (initial != NULL && PyUnicode_Check(initial)) {
2474 Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial);
2475 if (n > 0) {
2476 arrayobject *self = (arrayobject *)a;
2477 char *item = self->ob_item;
2478 item = (char *)PyMem_Realloc(item, n);
2479 if (item == NULL) {
2480 PyErr_NoMemory();
2481 Py_DECREF(a);
2482 return NULL;
2483 }
2484 self->ob_item = item;
2485 Py_SIZE(self) = n / sizeof(Py_UNICODE);
2486 memcpy(item, PyUnicode_AS_DATA(initial), n);
2487 self->allocated = Py_SIZE(self);
2488 }
2489 }
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002490 else if (initial != NULL && array_Check(initial)) {
2491 arrayobject *self = (arrayobject *)a;
2492 arrayobject *other = (arrayobject *)initial;
2493 memcpy(self->ob_item, other->ob_item, len * other->ob_descr->itemsize);
2494 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002495 if (it != NULL) {
2496 if (array_iter_extend((arrayobject *)a, it) == -1) {
2497 Py_DECREF(it);
2498 Py_DECREF(a);
2499 return NULL;
2500 }
2501 Py_DECREF(it);
2502 }
2503 return a;
2504 }
2505 }
2506 PyErr_SetString(PyExc_ValueError,
2507 "bad typecode (must be b, B, u, h, H, i, I, l, L, f or d)");
2508 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00002509}
2510
Guido van Rossum778983b1993-02-19 15:55:02 +00002511
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002512PyDoc_STRVAR(module_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00002513"This module defines an object type which can efficiently represent\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002514an array of basic values: characters, integers, floating point\n\
2515numbers. Arrays are sequence types and behave very much like lists,\n\
2516except that the type of objects stored in them is constrained. The\n\
2517type is specified at object creation time by using a type code, which\n\
2518is a single character. The following type codes are defined:\n\
2519\n\
2520 Type code C Type Minimum size in bytes \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002521 'b' signed integer 1 \n\
2522 'B' unsigned integer 1 \n\
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00002523 'u' Unicode character 2 (see note) \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002524 'h' signed integer 2 \n\
2525 'H' unsigned integer 2 \n\
2526 'i' signed integer 2 \n\
2527 'I' unsigned integer 2 \n\
2528 'l' signed integer 4 \n\
2529 'L' unsigned integer 4 \n\
2530 'f' floating point 4 \n\
2531 'd' floating point 8 \n\
2532\n\
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00002533NOTE: The 'u' typecode corresponds to Python's unicode character. On \n\
2534narrow builds this is 2-bytes on wide builds this is 4-bytes.\n\
2535\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002536The constructor is:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002537\n\
2538array(typecode [, initializer]) -- create a new array\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002539");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002540
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002541PyDoc_STRVAR(arraytype_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00002542"array(typecode [, initializer]) -> array\n\
2543\n\
2544Return a new array whose items are restricted by typecode, and\n\
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00002545initialized from the optional initializer value, which must be a list,\n\
2546string. or iterable over elements of the appropriate type.\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002547\n\
2548Arrays represent basic values and behave very much like lists, except\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002549the type of objects stored in them is constrained.\n\
2550\n\
2551Methods:\n\
2552\n\
2553append() -- append a new item to the end of the array\n\
2554buffer_info() -- return information giving the current memory info\n\
2555byteswap() -- byteswap all the items of the array\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002556count() -- return number of occurrences of an object\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00002557extend() -- extend array by appending multiple elements from an iterable\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002558fromfile() -- read items from a file object\n\
2559fromlist() -- append items from the list\n\
2560fromstring() -- append items from the string\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002561index() -- return index of first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002562insert() -- insert a new item into the array at a provided position\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00002563pop() -- remove and return item (default last)\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002564remove() -- remove first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002565reverse() -- reverse the order of the items in the array\n\
2566tofile() -- write all items to a file object\n\
2567tolist() -- return the array converted to an ordinary list\n\
2568tostring() -- return the array converted to a string\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002569\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002570Attributes:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002571\n\
2572typecode -- the typecode character used to create the array\n\
2573itemsize -- the length in bytes of one array item\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002574");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002575
Raymond Hettinger625812f2003-01-07 01:58:52 +00002576static PyObject *array_iter(arrayobject *ao);
2577
Tim Peters0c322792002-07-17 16:49:03 +00002578static PyTypeObject Arraytype = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 PyVarObject_HEAD_INIT(NULL, 0)
2580 "array.array",
2581 sizeof(arrayobject),
2582 0,
2583 (destructor)array_dealloc, /* tp_dealloc */
2584 0, /* tp_print */
2585 0, /* tp_getattr */
2586 0, /* tp_setattr */
2587 0, /* tp_reserved */
2588 (reprfunc)array_repr, /* tp_repr */
2589 0, /* tp_as_number*/
2590 &array_as_sequence, /* tp_as_sequence*/
2591 &array_as_mapping, /* tp_as_mapping*/
2592 0, /* tp_hash */
2593 0, /* tp_call */
2594 0, /* tp_str */
2595 PyObject_GenericGetAttr, /* tp_getattro */
2596 0, /* tp_setattro */
2597 &array_as_buffer, /* tp_as_buffer*/
2598 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2599 arraytype_doc, /* tp_doc */
2600 0, /* tp_traverse */
2601 0, /* tp_clear */
2602 array_richcompare, /* tp_richcompare */
2603 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
2604 (getiterfunc)array_iter, /* tp_iter */
2605 0, /* tp_iternext */
2606 array_methods, /* tp_methods */
2607 0, /* tp_members */
2608 array_getsets, /* tp_getset */
2609 0, /* tp_base */
2610 0, /* tp_dict */
2611 0, /* tp_descr_get */
2612 0, /* tp_descr_set */
2613 0, /* tp_dictoffset */
2614 0, /* tp_init */
2615 PyType_GenericAlloc, /* tp_alloc */
2616 array_new, /* tp_new */
2617 PyObject_Del, /* tp_free */
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002618};
2619
Raymond Hettinger625812f2003-01-07 01:58:52 +00002620
2621/*********************** Array Iterator **************************/
2622
2623typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002624 PyObject_HEAD
2625 Py_ssize_t index;
2626 arrayobject *ao;
2627 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002628} arrayiterobject;
2629
2630static PyTypeObject PyArrayIter_Type;
2631
2632#define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
2633
2634static PyObject *
2635array_iter(arrayobject *ao)
2636{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002637 arrayiterobject *it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002639 if (!array_Check(ao)) {
2640 PyErr_BadInternalCall();
2641 return NULL;
2642 }
Raymond Hettinger625812f2003-01-07 01:58:52 +00002643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002644 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
2645 if (it == NULL)
2646 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002648 Py_INCREF(ao);
2649 it->ao = ao;
2650 it->index = 0;
2651 it->getitem = ao->ob_descr->getitem;
2652 PyObject_GC_Track(it);
2653 return (PyObject *)it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002654}
2655
2656static PyObject *
Raymond Hettinger625812f2003-01-07 01:58:52 +00002657arrayiter_next(arrayiterobject *it)
2658{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002659 assert(PyArrayIter_Check(it));
2660 if (it->index < Py_SIZE(it->ao))
2661 return (*it->getitem)(it->ao, it->index++);
2662 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002663}
2664
2665static void
2666arrayiter_dealloc(arrayiterobject *it)
2667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 PyObject_GC_UnTrack(it);
2669 Py_XDECREF(it->ao);
2670 PyObject_GC_Del(it);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002671}
2672
2673static int
2674arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002676 Py_VISIT(it->ao);
2677 return 0;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002678}
2679
2680static PyTypeObject PyArrayIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002681 PyVarObject_HEAD_INIT(NULL, 0)
2682 "arrayiterator", /* tp_name */
2683 sizeof(arrayiterobject), /* tp_basicsize */
2684 0, /* tp_itemsize */
2685 /* methods */
2686 (destructor)arrayiter_dealloc, /* tp_dealloc */
2687 0, /* tp_print */
2688 0, /* tp_getattr */
2689 0, /* tp_setattr */
2690 0, /* tp_reserved */
2691 0, /* tp_repr */
2692 0, /* tp_as_number */
2693 0, /* tp_as_sequence */
2694 0, /* tp_as_mapping */
2695 0, /* tp_hash */
2696 0, /* tp_call */
2697 0, /* tp_str */
2698 PyObject_GenericGetAttr, /* tp_getattro */
2699 0, /* tp_setattro */
2700 0, /* tp_as_buffer */
2701 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2702 0, /* tp_doc */
2703 (traverseproc)arrayiter_traverse, /* tp_traverse */
2704 0, /* tp_clear */
2705 0, /* tp_richcompare */
2706 0, /* tp_weaklistoffset */
2707 PyObject_SelfIter, /* tp_iter */
2708 (iternextfunc)arrayiter_next, /* tp_iternext */
2709 0, /* tp_methods */
Raymond Hettinger625812f2003-01-07 01:58:52 +00002710};
2711
2712
2713/*********************** Install Module **************************/
2714
Martin v. Löwis99866332002-03-01 10:27:01 +00002715/* No functions in array module. */
2716static PyMethodDef a_methods[] = {
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002717 {"_array_reconstructor", array_reconstructor, METH_VARARGS,
2718 PyDoc_STR("Internal. Used for pickling support.")},
Martin v. Löwis99866332002-03-01 10:27:01 +00002719 {NULL, NULL, 0, NULL} /* Sentinel */
2720};
2721
Martin v. Löwis1a214512008-06-11 05:26:20 +00002722static struct PyModuleDef arraymodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002723 PyModuleDef_HEAD_INIT,
2724 "array",
2725 module_doc,
2726 -1,
2727 a_methods,
2728 NULL,
2729 NULL,
2730 NULL,
2731 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002732};
2733
Martin v. Löwis99866332002-03-01 10:27:01 +00002734
Mark Hammondfe51c6d2002-08-02 02:27:13 +00002735PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002736PyInit_array(void)
Guido van Rossum778983b1993-02-19 15:55:02 +00002737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002738 PyObject *m;
2739 PyObject *typecodes;
2740 Py_ssize_t size = 0;
2741 register Py_UNICODE *p;
2742 struct arraydescr *descr;
Fred Drake0d40ba42000-02-04 20:33:49 +00002743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 if (PyType_Ready(&Arraytype) < 0)
2745 return NULL;
2746 Py_TYPE(&PyArrayIter_Type) = &PyType_Type;
2747 m = PyModule_Create(&arraymodule);
2748 if (m == NULL)
2749 return NULL;
Fred Drakef4e34842002-04-01 03:45:06 +00002750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 Py_INCREF((PyObject *)&Arraytype);
2752 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
2753 Py_INCREF((PyObject *)&Arraytype);
2754 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00002755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 for (descr=descriptors; descr->typecode != '\0'; descr++) {
2757 size++;
2758 }
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00002759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 typecodes = PyUnicode_FromStringAndSize(NULL, size);
2761 p = PyUnicode_AS_UNICODE(typecodes);
2762 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2763 *p++ = (char)descr->typecode;
2764 }
2765
2766 PyModule_AddObject(m, "typecodes", (PyObject *)typecodes);
2767
2768 if (PyErr_Occurred()) {
2769 Py_DECREF(m);
2770 m = NULL;
2771 }
2772 return m;
Guido van Rossum778983b1993-02-19 15:55:02 +00002773}