blob: 81c9c363d36b4a89b6108b1b56ca68be11e258e5 [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
Brian Curtindfc80e32011-08-10 20:28:54 -0500517 if (!array_Check(v) || !array_Check(w))
518 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 va = (arrayobject *)v;
521 wa = (arrayobject *)w;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 if (Py_SIZE(va) != Py_SIZE(wa) && (op == Py_EQ || op == Py_NE)) {
524 /* Shortcut: if the lengths differ, the arrays differ */
525 if (op == Py_EQ)
526 res = Py_False;
527 else
528 res = Py_True;
529 Py_INCREF(res);
530 return res;
531 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 /* Search for the first index where items are different */
534 k = 1;
535 for (i = 0; i < Py_SIZE(va) && i < Py_SIZE(wa); i++) {
536 vi = getarrayitem(v, i);
537 wi = getarrayitem(w, i);
538 if (vi == NULL || wi == NULL) {
539 Py_XDECREF(vi);
540 Py_XDECREF(wi);
541 return NULL;
542 }
543 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
544 if (k == 0)
545 break; /* Keeping vi and wi alive! */
546 Py_DECREF(vi);
547 Py_DECREF(wi);
548 if (k < 0)
549 return NULL;
550 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 if (k) {
553 /* No more items to compare -- compare sizes */
554 Py_ssize_t vs = Py_SIZE(va);
555 Py_ssize_t ws = Py_SIZE(wa);
556 int cmp;
557 switch (op) {
558 case Py_LT: cmp = vs < ws; break;
559 case Py_LE: cmp = vs <= ws; break;
560 case Py_EQ: cmp = vs == ws; break;
561 case Py_NE: cmp = vs != ws; break;
562 case Py_GT: cmp = vs > ws; break;
563 case Py_GE: cmp = vs >= ws; break;
564 default: return NULL; /* cannot happen */
565 }
566 if (cmp)
567 res = Py_True;
568 else
569 res = Py_False;
570 Py_INCREF(res);
571 return res;
572 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 /* We have an item that differs. First, shortcuts for EQ/NE */
575 if (op == Py_EQ) {
576 Py_INCREF(Py_False);
577 res = Py_False;
578 }
579 else if (op == Py_NE) {
580 Py_INCREF(Py_True);
581 res = Py_True;
582 }
583 else {
584 /* Compare the final item again using the proper operator */
585 res = PyObject_RichCompare(vi, wi, op);
586 }
587 Py_DECREF(vi);
588 Py_DECREF(wi);
589 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +0000590}
591
Martin v. Löwis18e16552006-02-15 17:27:45 +0000592static Py_ssize_t
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000593array_length(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +0000594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 return Py_SIZE(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000596}
597
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000598static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000599array_item(arrayobject *a, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 if (i < 0 || i >= Py_SIZE(a)) {
602 PyErr_SetString(PyExc_IndexError, "array index out of range");
603 return NULL;
604 }
605 return getarrayitem((PyObject *)a, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000606}
607
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000608static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000609array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 arrayobject *np;
612 if (ilow < 0)
613 ilow = 0;
614 else if (ilow > Py_SIZE(a))
615 ilow = Py_SIZE(a);
616 if (ihigh < 0)
617 ihigh = 0;
618 if (ihigh < ilow)
619 ihigh = ilow;
620 else if (ihigh > Py_SIZE(a))
621 ihigh = Py_SIZE(a);
622 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
623 if (np == NULL)
624 return NULL;
625 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
626 (ihigh-ilow) * a->ob_descr->itemsize);
627 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000628}
629
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000630static PyObject *
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000631array_copy(arrayobject *a, PyObject *unused)
632{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 return array_slice(a, 0, Py_SIZE(a));
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000634}
635
636PyDoc_STRVAR(copy_doc,
637"copy(array)\n\
638\n\
639 Return a copy of the array.");
640
641static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000642array_concat(arrayobject *a, PyObject *bb)
Guido van Rossum778983b1993-02-19 15:55:02 +0000643{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 Py_ssize_t size;
645 arrayobject *np;
646 if (!array_Check(bb)) {
647 PyErr_Format(PyExc_TypeError,
648 "can only append array (not \"%.200s\") to array",
649 Py_TYPE(bb)->tp_name);
650 return NULL;
651 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000652#define b ((arrayobject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 if (a->ob_descr != b->ob_descr) {
654 PyErr_BadArgument();
655 return NULL;
656 }
657 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) {
658 return PyErr_NoMemory();
659 }
660 size = Py_SIZE(a) + Py_SIZE(b);
661 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
662 if (np == NULL) {
663 return NULL;
664 }
665 memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize);
666 memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize,
667 b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
668 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000669#undef b
670}
671
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000672static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000673array_repeat(arrayobject *a, Py_ssize_t n)
Guido van Rossum778983b1993-02-19 15:55:02 +0000674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 Py_ssize_t size;
676 arrayobject *np;
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000677 Py_ssize_t oldbytes, newbytes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 if (n < 0)
679 n = 0;
680 if ((Py_SIZE(a) != 0) && (n > PY_SSIZE_T_MAX / Py_SIZE(a))) {
681 return PyErr_NoMemory();
682 }
683 size = Py_SIZE(a) * n;
684 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
685 if (np == NULL)
686 return NULL;
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000687 if (n == 0)
688 return (PyObject *)np;
689 oldbytes = Py_SIZE(a) * a->ob_descr->itemsize;
690 newbytes = oldbytes * n;
691 /* this follows the code in unicode_repeat */
692 if (oldbytes == 1) {
693 memset(np->ob_item, a->ob_item[0], newbytes);
694 } else {
695 Py_ssize_t done = oldbytes;
696 Py_MEMCPY(np->ob_item, a->ob_item, oldbytes);
697 while (done < newbytes) {
698 Py_ssize_t ncopy = (done <= newbytes-done) ? done : newbytes-done;
699 Py_MEMCPY(np->ob_item+done, np->ob_item, ncopy);
700 done += ncopy;
701 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 }
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000703 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000704}
705
706static int
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000707array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 char *item;
710 Py_ssize_t n; /* Size of replacement array */
711 Py_ssize_t d; /* Change in size */
Guido van Rossum778983b1993-02-19 15:55:02 +0000712#define b ((arrayobject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 if (v == NULL)
714 n = 0;
715 else if (array_Check(v)) {
716 n = Py_SIZE(b);
717 if (a == b) {
718 /* Special case "a[i:j] = a" -- copy b first */
719 int ret;
720 v = array_slice(b, 0, n);
721 if (!v)
722 return -1;
723 ret = array_ass_slice(a, ilow, ihigh, v);
724 Py_DECREF(v);
725 return ret;
726 }
727 if (b->ob_descr != a->ob_descr) {
728 PyErr_BadArgument();
729 return -1;
730 }
731 }
732 else {
733 PyErr_Format(PyExc_TypeError,
734 "can only assign array (not \"%.200s\") to array slice",
735 Py_TYPE(v)->tp_name);
736 return -1;
737 }
738 if (ilow < 0)
739 ilow = 0;
740 else if (ilow > Py_SIZE(a))
741 ilow = Py_SIZE(a);
742 if (ihigh < 0)
743 ihigh = 0;
744 if (ihigh < ilow)
745 ihigh = ilow;
746 else if (ihigh > Py_SIZE(a))
747 ihigh = Py_SIZE(a);
748 item = a->ob_item;
749 d = n - (ihigh-ilow);
750 /* Issue #4509: If the array has exported buffers and the slice
751 assignment would change the size of the array, fail early to make
752 sure we don't modify it. */
753 if (d != 0 && a->ob_exports > 0) {
754 PyErr_SetString(PyExc_BufferError,
755 "cannot resize an array that is exporting buffers");
756 return -1;
757 }
758 if (d < 0) { /* Delete -d items */
759 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
760 item + ihigh*a->ob_descr->itemsize,
761 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
762 if (array_resize(a, Py_SIZE(a) + d) == -1)
763 return -1;
764 }
765 else if (d > 0) { /* Insert d items */
766 if (array_resize(a, Py_SIZE(a) + d))
767 return -1;
768 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
769 item + ihigh*a->ob_descr->itemsize,
770 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
771 }
772 if (n > 0)
773 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
774 n*b->ob_descr->itemsize);
775 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000776#undef b
777}
778
779static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000780array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000781{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 if (i < 0 || i >= Py_SIZE(a)) {
783 PyErr_SetString(PyExc_IndexError,
784 "array assignment index out of range");
785 return -1;
786 }
787 if (v == NULL)
788 return array_ass_slice(a, i, i+1, v);
789 return (*a->ob_descr->setitem)(a, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000790}
791
792static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000793setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000794{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 assert(array_Check(a));
796 return array_ass_item((arrayobject *)a, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000797}
798
Martin v. Löwis99866332002-03-01 10:27:01 +0000799static int
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000800array_iter_extend(arrayobject *self, PyObject *bb)
801{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 PyObject *it, *v;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 it = PyObject_GetIter(bb);
805 if (it == NULL)
806 return -1;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 while ((v = PyIter_Next(it)) != NULL) {
Mark Dickinson346f0af2010-08-06 09:36:57 +0000809 if (ins1(self, Py_SIZE(self), v) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 Py_DECREF(v);
811 Py_DECREF(it);
812 return -1;
813 }
814 Py_DECREF(v);
815 }
816 Py_DECREF(it);
817 if (PyErr_Occurred())
818 return -1;
819 return 0;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000820}
821
822static int
Martin v. Löwis99866332002-03-01 10:27:01 +0000823array_do_extend(arrayobject *self, PyObject *bb)
824{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 Py_ssize_t size, oldsize, bbsize;
Martin v. Löwis99866332002-03-01 10:27:01 +0000826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 if (!array_Check(bb))
828 return array_iter_extend(self, bb);
829#define b ((arrayobject *)bb)
830 if (self->ob_descr != b->ob_descr) {
831 PyErr_SetString(PyExc_TypeError,
832 "can only extend with array of same kind");
833 return -1;
834 }
835 if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) ||
836 ((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
837 PyErr_NoMemory();
838 return -1;
839 }
840 oldsize = Py_SIZE(self);
841 /* Get the size of bb before resizing the array since bb could be self. */
842 bbsize = Py_SIZE(bb);
843 size = oldsize + Py_SIZE(b);
844 if (array_resize(self, size) == -1)
845 return -1;
846 memcpy(self->ob_item + oldsize * self->ob_descr->itemsize,
847 b->ob_item, bbsize * b->ob_descr->itemsize);
848
849 return 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000850#undef b
851}
852
853static PyObject *
854array_inplace_concat(arrayobject *self, PyObject *bb)
855{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 if (!array_Check(bb)) {
857 PyErr_Format(PyExc_TypeError,
858 "can only extend array with array (not \"%.200s\")",
859 Py_TYPE(bb)->tp_name);
860 return NULL;
861 }
862 if (array_do_extend(self, bb) == -1)
863 return NULL;
864 Py_INCREF(self);
865 return (PyObject *)self;
Martin v. Löwis99866332002-03-01 10:27:01 +0000866}
867
868static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000869array_inplace_repeat(arrayobject *self, Py_ssize_t n)
Martin v. Löwis99866332002-03-01 10:27:01 +0000870{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 char *items, *p;
872 Py_ssize_t size, i;
Martin v. Löwis99866332002-03-01 10:27:01 +0000873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 if (Py_SIZE(self) > 0) {
875 if (n < 0)
876 n = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 if ((self->ob_descr->itemsize != 0) &&
878 (Py_SIZE(self) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
879 return PyErr_NoMemory();
880 }
881 size = Py_SIZE(self) * self->ob_descr->itemsize;
882 if (n > 0 && size > PY_SSIZE_T_MAX / n) {
883 return PyErr_NoMemory();
884 }
885 if (array_resize(self, n * Py_SIZE(self)) == -1)
886 return NULL;
887 items = p = self->ob_item;
888 for (i = 1; i < n; i++) {
889 p += size;
890 memcpy(p, items, size);
891 }
892 }
893 Py_INCREF(self);
894 return (PyObject *)self;
Martin v. Löwis99866332002-03-01 10:27:01 +0000895}
896
897
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000898static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000899ins(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 if (ins1(self, where, v) != 0)
902 return NULL;
903 Py_INCREF(Py_None);
904 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000905}
906
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000907static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000908array_count(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000909{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 Py_ssize_t count = 0;
911 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 for (i = 0; i < Py_SIZE(self); i++) {
914 PyObject *selfi = getarrayitem((PyObject *)self, i);
915 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
916 Py_DECREF(selfi);
917 if (cmp > 0)
918 count++;
919 else if (cmp < 0)
920 return NULL;
921 }
922 return PyLong_FromSsize_t(count);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000923}
924
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000925PyDoc_STRVAR(count_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000926"count(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000927\n\
Mark Dickinson934896d2009-02-21 20:59:32 +0000928Return number of occurrences of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000929
930static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000931array_index(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000932{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 for (i = 0; i < Py_SIZE(self); i++) {
936 PyObject *selfi = getarrayitem((PyObject *)self, i);
937 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
938 Py_DECREF(selfi);
939 if (cmp > 0) {
940 return PyLong_FromLong((long)i);
941 }
942 else if (cmp < 0)
943 return NULL;
944 }
945 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
946 return NULL;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000947}
948
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000949PyDoc_STRVAR(index_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000950"index(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000951\n\
Mark Dickinson934896d2009-02-21 20:59:32 +0000952Return index of first occurrence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000953
Raymond Hettinger625812f2003-01-07 01:58:52 +0000954static int
955array_contains(arrayobject *self, PyObject *v)
956{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 Py_ssize_t i;
958 int cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +0000959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) {
961 PyObject *selfi = getarrayitem((PyObject *)self, i);
962 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
963 Py_DECREF(selfi);
964 }
965 return cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +0000966}
967
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000968static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000969array_remove(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000970{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 int i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 for (i = 0; i < Py_SIZE(self); i++) {
974 PyObject *selfi = getarrayitem((PyObject *)self,i);
975 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
976 Py_DECREF(selfi);
977 if (cmp > 0) {
978 if (array_ass_slice(self, i, i+1,
979 (PyObject *)NULL) != 0)
980 return NULL;
981 Py_INCREF(Py_None);
982 return Py_None;
983 }
984 else if (cmp < 0)
985 return NULL;
986 }
987 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
988 return NULL;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000989}
990
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000991PyDoc_STRVAR(remove_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000992"remove(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000993\n\
Mark Dickinson934896d2009-02-21 20:59:32 +0000994Remove the first occurrence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000995
996static PyObject *
997array_pop(arrayobject *self, PyObject *args)
998{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 Py_ssize_t i = -1;
1000 PyObject *v;
1001 if (!PyArg_ParseTuple(args, "|n:pop", &i))
1002 return NULL;
1003 if (Py_SIZE(self) == 0) {
1004 /* Special-case most common failure cause */
1005 PyErr_SetString(PyExc_IndexError, "pop from empty array");
1006 return NULL;
1007 }
1008 if (i < 0)
1009 i += Py_SIZE(self);
1010 if (i < 0 || i >= Py_SIZE(self)) {
1011 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1012 return NULL;
1013 }
1014 v = getarrayitem((PyObject *)self,i);
1015 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
1016 Py_DECREF(v);
1017 return NULL;
1018 }
1019 return v;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001020}
1021
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001022PyDoc_STRVAR(pop_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001023"pop([i])\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001024\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001025Return the i-th element and delete it from the array. i defaults to -1.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001026
1027static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001028array_extend(arrayobject *self, PyObject *bb)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 if (array_do_extend(self, bb) == -1)
1031 return NULL;
1032 Py_INCREF(Py_None);
1033 return Py_None;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001034}
1035
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001036PyDoc_STRVAR(extend_doc,
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001037"extend(array or iterable)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001038\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001039 Append items to the end of the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001040
1041static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001042array_insert(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001043{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 Py_ssize_t i;
1045 PyObject *v;
1046 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
1047 return NULL;
1048 return ins(self, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001049}
1050
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001051PyDoc_STRVAR(insert_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001052"insert(i,x)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001053\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001054Insert a new item x into the array before position i.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001055
1056
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001057static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001058array_buffer_info(arrayobject *self, PyObject *unused)
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001059{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 PyObject* retval = NULL;
1061 retval = PyTuple_New(2);
1062 if (!retval)
1063 return NULL;
Fred Drake541dc3b2000-06-28 17:49:30 +00001064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item));
1066 PyTuple_SET_ITEM(retval, 1, PyLong_FromLong((long)(Py_SIZE(self))));
Fred Drake541dc3b2000-06-28 17:49:30 +00001067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 return retval;
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001069}
1070
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001071PyDoc_STRVAR(buffer_info_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001072"buffer_info() -> (address, length)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001073\n\
1074Return a tuple (address, length) giving the current memory address and\n\
Guido van Rossum702d08e2001-07-27 16:05:32 +00001075the length in items of the buffer used to hold array's contents\n\
1076The length should be multiplied by the itemsize attribute to calculate\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001077the buffer length in bytes.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001078
1079
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001080static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001081array_append(arrayobject *self, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +00001082{
Mark Dickinson346f0af2010-08-06 09:36:57 +00001083 return ins(self, Py_SIZE(self), v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001084}
1085
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001086PyDoc_STRVAR(append_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001087"append(x)\n\
1088\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001089Append new value x to the end of the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001090
1091
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001092static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001093array_byteswap(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 char *p;
1096 Py_ssize_t i;
Fred Drakebf272981999-12-03 17:15:30 +00001097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 switch (self->ob_descr->itemsize) {
1099 case 1:
1100 break;
1101 case 2:
1102 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) {
1103 char p0 = p[0];
1104 p[0] = p[1];
1105 p[1] = p0;
1106 }
1107 break;
1108 case 4:
1109 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 4) {
1110 char p0 = p[0];
1111 char p1 = p[1];
1112 p[0] = p[3];
1113 p[1] = p[2];
1114 p[2] = p1;
1115 p[3] = p0;
1116 }
1117 break;
1118 case 8:
1119 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) {
1120 char p0 = p[0];
1121 char p1 = p[1];
1122 char p2 = p[2];
1123 char p3 = p[3];
1124 p[0] = p[7];
1125 p[1] = p[6];
1126 p[2] = p[5];
1127 p[3] = p[4];
1128 p[4] = p3;
1129 p[5] = p2;
1130 p[6] = p1;
1131 p[7] = p0;
1132 }
1133 break;
1134 default:
1135 PyErr_SetString(PyExc_RuntimeError,
1136 "don't know how to byteswap this array type");
1137 return NULL;
1138 }
1139 Py_INCREF(Py_None);
1140 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001141}
1142
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001143PyDoc_STRVAR(byteswap_doc,
Fred Drakebf272981999-12-03 17:15:30 +00001144"byteswap()\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001145\n\
Fred Drakebf272981999-12-03 17:15:30 +00001146Byteswap 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 +000011474, or 8 bytes in size, RuntimeError is raised.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001148
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001149static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001150array_reverse(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 register Py_ssize_t itemsize = self->ob_descr->itemsize;
1153 register char *p, *q;
1154 /* little buffer to hold items while swapping */
1155 char tmp[256]; /* 8 is probably enough -- but why skimp */
1156 assert((size_t)itemsize <= sizeof(tmp));
Guido van Rossume77a7571993-11-03 15:01:26 +00001157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 if (Py_SIZE(self) > 1) {
1159 for (p = self->ob_item,
1160 q = self->ob_item + (Py_SIZE(self) - 1)*itemsize;
1161 p < q;
1162 p += itemsize, q -= itemsize) {
1163 /* memory areas guaranteed disjoint, so memcpy
1164 * is safe (& memmove may be slower).
1165 */
1166 memcpy(tmp, p, itemsize);
1167 memcpy(p, q, itemsize);
1168 memcpy(q, tmp, itemsize);
1169 }
1170 }
Tim Petersbb307342000-09-10 05:22:54 +00001171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 Py_INCREF(Py_None);
1173 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001174}
Guido van Rossume77a7571993-11-03 15:01:26 +00001175
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001176PyDoc_STRVAR(reverse_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001177"reverse()\n\
1178\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001179Reverse the order of the items in the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001180
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001181
1182/* Forward */
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001183static PyObject *array_frombytes(arrayobject *self, PyObject *args);
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001184
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001185static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001186array_fromfile(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 PyObject *f, *b, *res;
1189 Py_ssize_t itemsize = self->ob_descr->itemsize;
1190 Py_ssize_t n, nbytes;
1191 int not_enough_bytes;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 if (!PyArg_ParseTuple(args, "On:fromfile", &f, &n))
1194 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 nbytes = n * itemsize;
1197 if (nbytes < 0 || nbytes/itemsize != n) {
1198 PyErr_NoMemory();
1199 return NULL;
1200 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 b = PyObject_CallMethod(f, "read", "n", nbytes);
1203 if (b == NULL)
1204 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 if (!PyBytes_Check(b)) {
1207 PyErr_SetString(PyExc_TypeError,
1208 "read() didn't return bytes");
1209 Py_DECREF(b);
1210 return NULL;
1211 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 not_enough_bytes = (PyBytes_GET_SIZE(b) != nbytes);
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 args = Py_BuildValue("(O)", b);
1216 Py_DECREF(b);
1217 if (args == NULL)
1218 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001219
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001220 res = array_frombytes(self, args);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 Py_DECREF(args);
1222 if (res == NULL)
1223 return NULL;
Hirokazu Yamamoto54d0df62009-03-06 03:04:07 +00001224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 if (not_enough_bytes) {
1226 PyErr_SetString(PyExc_EOFError,
1227 "read() didn't return enough bytes");
1228 Py_DECREF(res);
1229 return NULL;
1230 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +00001233}
1234
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001235PyDoc_STRVAR(fromfile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001236"fromfile(f, n)\n\
1237\n\
1238Read n objects from the file object f and append them to the end of the\n\
Georg Brandlf25ef502008-02-01 11:30:18 +00001239array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001240
1241
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001242static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001243array_tofile(arrayobject *self, PyObject *f)
Guido van Rossum778983b1993-02-19 15:55:02 +00001244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize;
1246 /* Write 64K blocks at a time */
1247 /* XXX Make the block size settable */
1248 int BLOCKSIZE = 64*1024;
1249 Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE;
1250 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 if (Py_SIZE(self) == 0)
1253 goto done;
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 for (i = 0; i < nblocks; i++) {
1256 char* ptr = self->ob_item + i*BLOCKSIZE;
1257 Py_ssize_t size = BLOCKSIZE;
1258 PyObject *bytes, *res;
1259 if (i*BLOCKSIZE + size > nbytes)
1260 size = nbytes - i*BLOCKSIZE;
1261 bytes = PyBytes_FromStringAndSize(ptr, size);
1262 if (bytes == NULL)
1263 return NULL;
1264 res = PyObject_CallMethod(f, "write", "O", bytes);
1265 Py_DECREF(bytes);
1266 if (res == NULL)
1267 return NULL;
1268 Py_DECREF(res); /* drop write result */
1269 }
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001270
1271 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 Py_INCREF(Py_None);
1273 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001274}
1275
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001276PyDoc_STRVAR(tofile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001277"tofile(f)\n\
1278\n\
Georg Brandlf25ef502008-02-01 11:30:18 +00001279Write all items (as machine values) to the file object f.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001280
1281
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001282static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001283array_fromlist(arrayobject *self, PyObject *list)
Guido van Rossum778983b1993-02-19 15:55:02 +00001284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 Py_ssize_t n;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 if (!PyList_Check(list)) {
1288 PyErr_SetString(PyExc_TypeError, "arg must be list");
1289 return NULL;
1290 }
1291 n = PyList_Size(list);
1292 if (n > 0) {
1293 Py_ssize_t i, old_size;
1294 old_size = Py_SIZE(self);
1295 if (array_resize(self, old_size + n) == -1)
1296 return NULL;
1297 for (i = 0; i < n; i++) {
1298 PyObject *v = PyList_GetItem(list, i);
1299 if ((*self->ob_descr->setitem)(self,
1300 Py_SIZE(self) - n + i, v) != 0) {
1301 array_resize(self, old_size);
1302 return NULL;
1303 }
1304 }
1305 }
1306 Py_INCREF(Py_None);
1307 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001308}
1309
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001310PyDoc_STRVAR(fromlist_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001311"fromlist(list)\n\
1312\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001313Append items to array from list.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001314
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001315static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001316array_tolist(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 PyObject *list = PyList_New(Py_SIZE(self));
1319 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 if (list == NULL)
1322 return NULL;
1323 for (i = 0; i < Py_SIZE(self); i++) {
1324 PyObject *v = getarrayitem((PyObject *)self, i);
1325 if (v == NULL) {
1326 Py_DECREF(list);
1327 return NULL;
1328 }
1329 PyList_SetItem(list, i, v);
1330 }
1331 return list;
Guido van Rossum778983b1993-02-19 15:55:02 +00001332}
1333
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001334PyDoc_STRVAR(tolist_doc,
Guido van Rossumfc6aba51998-10-14 02:52:31 +00001335"tolist() -> list\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001336\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001337Convert array to an ordinary list with the same items.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001338
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001339static PyObject *
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001340frombytes(arrayobject *self, Py_buffer *buffer)
Guido van Rossum778983b1993-02-19 15:55:02 +00001341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 int itemsize = self->ob_descr->itemsize;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001343 Py_ssize_t n;
1344 if (buffer->itemsize != 1) {
1345 PyBuffer_Release(buffer);
1346 PyErr_SetString(PyExc_TypeError, "string/buffer of bytes required.");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 return NULL;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001348 }
1349 n = buffer->len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 if (n % itemsize != 0) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001351 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 PyErr_SetString(PyExc_ValueError,
1353 "string length not a multiple of item size");
1354 return NULL;
1355 }
1356 n = n / itemsize;
1357 if (n > 0) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001358 Py_ssize_t old_size = Py_SIZE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 if ((n > PY_SSIZE_T_MAX - old_size) ||
1360 ((old_size + n) > PY_SSIZE_T_MAX / itemsize)) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001361 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 return PyErr_NoMemory();
1363 }
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001364 if (array_resize(self, old_size + n) == -1) {
1365 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 return NULL;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001367 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 memcpy(self->ob_item + old_size * itemsize,
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001369 buffer->buf, n * itemsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 }
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001371 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 Py_INCREF(Py_None);
1373 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001374}
1375
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001376static PyObject *
1377array_fromstring(arrayobject *self, PyObject *args)
1378{
1379 Py_buffer buffer;
1380 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1381 "fromstring() is deprecated. Use frombytes() instead.", 2) != 0)
1382 return NULL;
1383 if (!PyArg_ParseTuple(args, "s*:fromstring", &buffer))
1384 return NULL;
1385 else
1386 return frombytes(self, &buffer);
1387}
1388
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001389PyDoc_STRVAR(fromstring_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001390"fromstring(string)\n\
1391\n\
1392Appends items from the string, interpreting it as an array of machine\n\
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001393values, as if it had been read from a file using the fromfile() method).\n\
1394\n\
1395This method is deprecated. Use frombytes instead.");
1396
1397
1398static PyObject *
1399array_frombytes(arrayobject *self, PyObject *args)
1400{
1401 Py_buffer buffer;
1402 if (!PyArg_ParseTuple(args, "y*:frombytes", &buffer))
1403 return NULL;
1404 else
1405 return frombytes(self, &buffer);
1406}
1407
1408PyDoc_STRVAR(frombytes_doc,
1409"frombytes(bytestring)\n\
1410\n\
1411Appends items from the string, interpreting it as an array of machine\n\
Walter Dörwald93b30b52007-06-22 12:21:53 +00001412values, as if it had been read from a file using the fromfile() method).");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001413
1414
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001415static PyObject *
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001416array_tobytes(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001417{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
1419 return PyBytes_FromStringAndSize(self->ob_item,
1420 Py_SIZE(self) * self->ob_descr->itemsize);
1421 } else {
1422 return PyErr_NoMemory();
1423 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001424}
1425
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001426PyDoc_STRVAR(tobytes_doc,
1427"tobytes() -> bytes\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001428\n\
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001429Convert the array to an array of machine values and return the bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001430representation.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001431
Martin v. Löwis99866332002-03-01 10:27:01 +00001432
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001433static PyObject *
1434array_tostring(arrayobject *self, PyObject *unused)
1435{
Victor Stinner9f0b51e2010-11-09 09:38:30 +00001436 if (PyErr_WarnEx(PyExc_DeprecationWarning,
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001437 "tostring() is deprecated. Use tobytes() instead.", 2) != 0)
1438 return NULL;
1439 return array_tobytes(self, unused);
1440}
1441
1442PyDoc_STRVAR(tostring_doc,
1443"tostring() -> bytes\n\
1444\n\
1445Convert the array to an array of machine values and return the bytes\n\
1446representation.\n\
1447\n\
1448This method is deprecated. Use tobytes instead.");
1449
Martin v. Löwis99866332002-03-01 10:27:01 +00001450
Martin v. Löwis99866332002-03-01 10:27:01 +00001451static PyObject *
1452array_fromunicode(arrayobject *self, PyObject *args)
1453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 Py_UNICODE *ustr;
1455 Py_ssize_t n;
Amaury Forgeot d'Arc24aa26b2010-11-25 08:13:35 +00001456 Py_UNICODE typecode;
Martin v. Löwis99866332002-03-01 10:27:01 +00001457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
1459 return NULL;
1460 typecode = self->ob_descr->typecode;
1461 if ((typecode != 'u')) {
1462 PyErr_SetString(PyExc_ValueError,
1463 "fromunicode() may only be called on "
1464 "unicode type arrays");
1465 return NULL;
1466 }
1467 if (n > 0) {
1468 Py_ssize_t old_size = Py_SIZE(self);
1469 if (array_resize(self, old_size + n) == -1)
1470 return NULL;
1471 memcpy(self->ob_item + old_size * sizeof(Py_UNICODE),
1472 ustr, n * sizeof(Py_UNICODE));
1473 }
Martin v. Löwis99866332002-03-01 10:27:01 +00001474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 Py_INCREF(Py_None);
1476 return Py_None;
Martin v. Löwis99866332002-03-01 10:27:01 +00001477}
1478
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001479PyDoc_STRVAR(fromunicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001480"fromunicode(ustr)\n\
1481\n\
1482Extends this array with data from the unicode string ustr.\n\
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001483The array must be a unicode type array; otherwise a ValueError\n\
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001484is raised. Use array.frombytes(ustr.decode(...)) to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001485append Unicode data to an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001486
1487
1488static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001489array_tounicode(arrayobject *self, PyObject *unused)
Martin v. Löwis99866332002-03-01 10:27:01 +00001490{
Amaury Forgeot d'Arc24aa26b2010-11-25 08:13:35 +00001491 Py_UNICODE typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 typecode = self->ob_descr->typecode;
1493 if ((typecode != 'u')) {
1494 PyErr_SetString(PyExc_ValueError,
1495 "tounicode() may only be called on unicode type arrays");
1496 return NULL;
1497 }
1498 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_SIZE(self));
Martin v. Löwis99866332002-03-01 10:27:01 +00001499}
1500
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001501PyDoc_STRVAR(tounicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001502"tounicode() -> unicode\n\
1503\n\
1504Convert the array to a unicode string. The array must be\n\
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001505a unicode type array; otherwise a ValueError is raised. Use\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001506array.tostring().decode() to obtain a unicode string from\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001507an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001508
Martin v. Löwis99866332002-03-01 10:27:01 +00001509
1510
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001511/*********************** Pickling support ************************/
1512
1513enum machine_format_code {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 UNKNOWN_FORMAT = -1,
1515 /* UNKNOWN_FORMAT is used to indicate that the machine format for an
1516 * array type code cannot be interpreted. When this occurs, a list of
1517 * Python objects is used to represent the content of the array
1518 * instead of using the memory content of the array directly. In that
1519 * case, the array_reconstructor mechanism is bypassed completely, and
1520 * the standard array constructor is used instead.
1521 *
1522 * This is will most likely occur when the machine doesn't use IEEE
1523 * floating-point numbers.
1524 */
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 UNSIGNED_INT8 = 0,
1527 SIGNED_INT8 = 1,
1528 UNSIGNED_INT16_LE = 2,
1529 UNSIGNED_INT16_BE = 3,
1530 SIGNED_INT16_LE = 4,
1531 SIGNED_INT16_BE = 5,
1532 UNSIGNED_INT32_LE = 6,
1533 UNSIGNED_INT32_BE = 7,
1534 SIGNED_INT32_LE = 8,
1535 SIGNED_INT32_BE = 9,
1536 UNSIGNED_INT64_LE = 10,
1537 UNSIGNED_INT64_BE = 11,
1538 SIGNED_INT64_LE = 12,
1539 SIGNED_INT64_BE = 13,
1540 IEEE_754_FLOAT_LE = 14,
1541 IEEE_754_FLOAT_BE = 15,
1542 IEEE_754_DOUBLE_LE = 16,
1543 IEEE_754_DOUBLE_BE = 17,
1544 UTF16_LE = 18,
1545 UTF16_BE = 19,
1546 UTF32_LE = 20,
1547 UTF32_BE = 21
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001548};
1549#define MACHINE_FORMAT_CODE_MIN 0
1550#define MACHINE_FORMAT_CODE_MAX 21
1551
1552static const struct mformatdescr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 size_t size;
1554 int is_signed;
1555 int is_big_endian;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001556} mformat_descriptors[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 {1, 0, 0}, /* 0: UNSIGNED_INT8 */
1558 {1, 1, 0}, /* 1: SIGNED_INT8 */
1559 {2, 0, 0}, /* 2: UNSIGNED_INT16_LE */
1560 {2, 0, 1}, /* 3: UNSIGNED_INT16_BE */
1561 {2, 1, 0}, /* 4: SIGNED_INT16_LE */
1562 {2, 1, 1}, /* 5: SIGNED_INT16_BE */
1563 {4, 0, 0}, /* 6: UNSIGNED_INT32_LE */
1564 {4, 0, 1}, /* 7: UNSIGNED_INT32_BE */
1565 {4, 1, 0}, /* 8: SIGNED_INT32_LE */
1566 {4, 1, 1}, /* 9: SIGNED_INT32_BE */
1567 {8, 0, 0}, /* 10: UNSIGNED_INT64_LE */
1568 {8, 0, 1}, /* 11: UNSIGNED_INT64_BE */
1569 {8, 1, 0}, /* 12: SIGNED_INT64_LE */
1570 {8, 1, 1}, /* 13: SIGNED_INT64_BE */
1571 {4, 0, 0}, /* 14: IEEE_754_FLOAT_LE */
1572 {4, 0, 1}, /* 15: IEEE_754_FLOAT_BE */
1573 {8, 0, 0}, /* 16: IEEE_754_DOUBLE_LE */
1574 {8, 0, 1}, /* 17: IEEE_754_DOUBLE_BE */
1575 {4, 0, 0}, /* 18: UTF16_LE */
1576 {4, 0, 1}, /* 19: UTF16_BE */
1577 {8, 0, 0}, /* 20: UTF32_LE */
1578 {8, 0, 1} /* 21: UTF32_BE */
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001579};
1580
1581
1582/*
1583 * Internal: This function is used to find the machine format of a given
1584 * array type code. This returns UNKNOWN_FORMAT when the machine format cannot
1585 * be found.
1586 */
1587static enum machine_format_code
1588typecode_to_mformat_code(int typecode)
1589{
Alexandre Vassalotti7aaa7702009-07-17 03:51:27 +00001590#ifdef WORDS_BIGENDIAN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 const int is_big_endian = 1;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001592#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 const int is_big_endian = 0;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001594#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 size_t intsize;
1596 int is_signed;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 switch (typecode) {
1599 case 'b':
1600 return SIGNED_INT8;
1601 case 'B':
1602 return UNSIGNED_INT8;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 case 'u':
1605 if (sizeof(Py_UNICODE) == 2) {
1606 return UTF16_LE + is_big_endian;
1607 }
1608 if (sizeof(Py_UNICODE) == 4) {
1609 return UTF32_LE + is_big_endian;
1610 }
1611 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 case 'f':
1614 if (sizeof(float) == 4) {
1615 const float y = 16711938.0;
1616 if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0)
1617 return IEEE_754_FLOAT_BE;
1618 if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0)
1619 return IEEE_754_FLOAT_LE;
1620 }
1621 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 case 'd':
1624 if (sizeof(double) == 8) {
1625 const double x = 9006104071832581.0;
1626 if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0)
1627 return IEEE_754_DOUBLE_BE;
1628 if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
1629 return IEEE_754_DOUBLE_LE;
1630 }
1631 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 /* Integers */
1634 case 'h':
1635 intsize = sizeof(short);
1636 is_signed = 1;
1637 break;
1638 case 'H':
1639 intsize = sizeof(short);
1640 is_signed = 0;
1641 break;
1642 case 'i':
1643 intsize = sizeof(int);
1644 is_signed = 1;
1645 break;
1646 case 'I':
1647 intsize = sizeof(int);
1648 is_signed = 0;
1649 break;
1650 case 'l':
1651 intsize = sizeof(long);
1652 is_signed = 1;
1653 break;
1654 case 'L':
1655 intsize = sizeof(long);
1656 is_signed = 0;
1657 break;
1658 default:
1659 return UNKNOWN_FORMAT;
1660 }
1661 switch (intsize) {
1662 case 2:
1663 return UNSIGNED_INT16_LE + is_big_endian + (2 * is_signed);
1664 case 4:
1665 return UNSIGNED_INT32_LE + is_big_endian + (2 * is_signed);
1666 case 8:
1667 return UNSIGNED_INT64_LE + is_big_endian + (2 * is_signed);
1668 default:
1669 return UNKNOWN_FORMAT;
1670 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001671}
1672
1673/* Forward declaration. */
1674static PyObject *array_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
1675
1676/*
1677 * Internal: This function wraps the array constructor--i.e., array_new()--to
1678 * allow the creation of array objects from C code without having to deal
1679 * directly the tuple argument of array_new(). The typecode argument is a
1680 * Unicode character value, like 'i' or 'f' for example, representing an array
1681 * type code. The items argument is a bytes or a list object from which
1682 * contains the initial value of the array.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 *
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001684 * On success, this functions returns the array object created. Otherwise,
1685 * NULL is returned to indicate a failure.
1686 */
1687static PyObject *
Victor Stinner9f0b51e2010-11-09 09:38:30 +00001688make_array(PyTypeObject *arraytype, Py_UNICODE typecode, PyObject *items)
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 PyObject *new_args;
1691 PyObject *array_obj;
1692 PyObject *typecode_obj;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 assert(arraytype != NULL);
1695 assert(items != NULL);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001696
Victor Stinner9f0b51e2010-11-09 09:38:30 +00001697 typecode_obj = PyUnicode_FromUnicode(&typecode, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 if (typecode_obj == NULL)
1699 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 new_args = PyTuple_New(2);
1702 if (new_args == NULL)
1703 return NULL;
1704 Py_INCREF(items);
1705 PyTuple_SET_ITEM(new_args, 0, typecode_obj);
1706 PyTuple_SET_ITEM(new_args, 1, items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 array_obj = array_new(arraytype, new_args, NULL);
1709 Py_DECREF(new_args);
1710 if (array_obj == NULL)
1711 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 return array_obj;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001714}
1715
1716/*
1717 * This functions is a special constructor used when unpickling an array. It
1718 * provides a portable way to rebuild an array from its memory representation.
1719 */
1720static PyObject *
1721array_reconstructor(PyObject *self, PyObject *args)
1722{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 PyTypeObject *arraytype;
1724 PyObject *items;
1725 PyObject *converted_items;
1726 PyObject *result;
Victor Stinner9f0b51e2010-11-09 09:38:30 +00001727 int typecode_int;
1728 Py_UNICODE typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 enum machine_format_code mformat_code;
1730 struct arraydescr *descr;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 if (!PyArg_ParseTuple(args, "OCiO:array._array_reconstructor",
Victor Stinner9f0b51e2010-11-09 09:38:30 +00001733 &arraytype, &typecode_int, &mformat_code, &items))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001735
Victor Stinner9f0b51e2010-11-09 09:38:30 +00001736 typecode = (Py_UNICODE)typecode_int;
1737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 if (!PyType_Check(arraytype)) {
1739 PyErr_Format(PyExc_TypeError,
1740 "first argument must a type object, not %.200s",
1741 Py_TYPE(arraytype)->tp_name);
1742 return NULL;
1743 }
1744 if (!PyType_IsSubtype(arraytype, &Arraytype)) {
1745 PyErr_Format(PyExc_TypeError,
1746 "%.200s is not a subtype of %.200s",
1747 arraytype->tp_name, Arraytype.tp_name);
1748 return NULL;
1749 }
1750 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1751 if (descr->typecode == typecode)
1752 break;
1753 }
1754 if (descr->typecode == '\0') {
1755 PyErr_SetString(PyExc_ValueError,
1756 "second argument must be a valid type code");
1757 return NULL;
1758 }
1759 if (mformat_code < MACHINE_FORMAT_CODE_MIN ||
1760 mformat_code > MACHINE_FORMAT_CODE_MAX) {
1761 PyErr_SetString(PyExc_ValueError,
1762 "third argument must be a valid machine format code.");
1763 return NULL;
1764 }
1765 if (!PyBytes_Check(items)) {
1766 PyErr_Format(PyExc_TypeError,
1767 "fourth argument should be bytes, not %.200s",
1768 Py_TYPE(items)->tp_name);
1769 return NULL;
1770 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 /* Fast path: No decoding has to be done. */
1773 if (mformat_code == typecode_to_mformat_code(typecode) ||
1774 mformat_code == UNKNOWN_FORMAT) {
1775 return make_array(arraytype, typecode, items);
1776 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 /* Slow path: Decode the byte string according to the given machine
1779 * format code. This occurs when the computer unpickling the array
1780 * object is architecturally different from the one that pickled the
1781 * array.
1782 */
1783 if (Py_SIZE(items) % mformat_descriptors[mformat_code].size != 0) {
1784 PyErr_SetString(PyExc_ValueError,
1785 "string length not a multiple of item size");
1786 return NULL;
1787 }
1788 switch (mformat_code) {
1789 case IEEE_754_FLOAT_LE:
1790 case IEEE_754_FLOAT_BE: {
1791 int i;
1792 int le = (mformat_code == IEEE_754_FLOAT_LE) ? 1 : 0;
1793 Py_ssize_t itemcount = Py_SIZE(items) / 4;
1794 const unsigned char *memstr =
1795 (unsigned char *)PyBytes_AS_STRING(items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 converted_items = PyList_New(itemcount);
1798 if (converted_items == NULL)
1799 return NULL;
1800 for (i = 0; i < itemcount; i++) {
1801 PyObject *pyfloat = PyFloat_FromDouble(
1802 _PyFloat_Unpack4(&memstr[i * 4], le));
1803 if (pyfloat == NULL) {
1804 Py_DECREF(converted_items);
1805 return NULL;
1806 }
1807 PyList_SET_ITEM(converted_items, i, pyfloat);
1808 }
1809 break;
1810 }
1811 case IEEE_754_DOUBLE_LE:
1812 case IEEE_754_DOUBLE_BE: {
1813 int i;
1814 int le = (mformat_code == IEEE_754_DOUBLE_LE) ? 1 : 0;
1815 Py_ssize_t itemcount = Py_SIZE(items) / 8;
1816 const unsigned char *memstr =
1817 (unsigned char *)PyBytes_AS_STRING(items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 converted_items = PyList_New(itemcount);
1820 if (converted_items == NULL)
1821 return NULL;
1822 for (i = 0; i < itemcount; i++) {
1823 PyObject *pyfloat = PyFloat_FromDouble(
1824 _PyFloat_Unpack8(&memstr[i * 8], le));
1825 if (pyfloat == NULL) {
1826 Py_DECREF(converted_items);
1827 return NULL;
1828 }
1829 PyList_SET_ITEM(converted_items, i, pyfloat);
1830 }
1831 break;
1832 }
1833 case UTF16_LE:
1834 case UTF16_BE: {
1835 int byteorder = (mformat_code == UTF16_LE) ? -1 : 1;
1836 converted_items = PyUnicode_DecodeUTF16(
1837 PyBytes_AS_STRING(items), Py_SIZE(items),
1838 "strict", &byteorder);
1839 if (converted_items == NULL)
1840 return NULL;
1841 break;
1842 }
1843 case UTF32_LE:
1844 case UTF32_BE: {
1845 int byteorder = (mformat_code == UTF32_LE) ? -1 : 1;
1846 converted_items = PyUnicode_DecodeUTF32(
1847 PyBytes_AS_STRING(items), Py_SIZE(items),
1848 "strict", &byteorder);
1849 if (converted_items == NULL)
1850 return NULL;
1851 break;
1852 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 case UNSIGNED_INT8:
1855 case SIGNED_INT8:
1856 case UNSIGNED_INT16_LE:
1857 case UNSIGNED_INT16_BE:
1858 case SIGNED_INT16_LE:
1859 case SIGNED_INT16_BE:
1860 case UNSIGNED_INT32_LE:
1861 case UNSIGNED_INT32_BE:
1862 case SIGNED_INT32_LE:
1863 case SIGNED_INT32_BE:
1864 case UNSIGNED_INT64_LE:
1865 case UNSIGNED_INT64_BE:
1866 case SIGNED_INT64_LE:
1867 case SIGNED_INT64_BE: {
1868 int i;
1869 const struct mformatdescr mf_descr =
1870 mformat_descriptors[mformat_code];
1871 Py_ssize_t itemcount = Py_SIZE(items) / mf_descr.size;
1872 const unsigned char *memstr =
1873 (unsigned char *)PyBytes_AS_STRING(items);
1874 struct arraydescr *descr;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 /* If possible, try to pack array's items using a data type
1877 * that fits better. This may result in an array with narrower
1878 * or wider elements.
1879 *
1880 * For example, if a 32-bit machine pickles a L-code array of
1881 * unsigned longs, then the array will be unpickled by 64-bit
1882 * machine as an I-code array of unsigned ints.
1883 *
1884 * XXX: Is it possible to write a unit test for this?
1885 */
1886 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1887 if (descr->is_integer_type &&
1888 descr->itemsize == mf_descr.size &&
1889 descr->is_signed == mf_descr.is_signed)
1890 typecode = descr->typecode;
1891 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 converted_items = PyList_New(itemcount);
1894 if (converted_items == NULL)
1895 return NULL;
1896 for (i = 0; i < itemcount; i++) {
1897 PyObject *pylong;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 pylong = _PyLong_FromByteArray(
1900 &memstr[i * mf_descr.size],
1901 mf_descr.size,
1902 !mf_descr.is_big_endian,
1903 mf_descr.is_signed);
1904 if (pylong == NULL) {
1905 Py_DECREF(converted_items);
1906 return NULL;
1907 }
1908 PyList_SET_ITEM(converted_items, i, pylong);
1909 }
1910 break;
1911 }
1912 case UNKNOWN_FORMAT:
1913 /* Impossible, but needed to shut up GCC about the unhandled
1914 * enumeration value.
1915 */
1916 default:
1917 PyErr_BadArgument();
1918 return NULL;
1919 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 result = make_array(arraytype, typecode, converted_items);
1922 Py_DECREF(converted_items);
1923 return result;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001924}
1925
1926static PyObject *
1927array_reduce_ex(arrayobject *array, PyObject *value)
1928{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 PyObject *dict;
1930 PyObject *result;
1931 PyObject *array_str;
1932 int typecode = array->ob_descr->typecode;
1933 int mformat_code;
1934 static PyObject *array_reconstructor = NULL;
1935 long protocol;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 if (array_reconstructor == NULL) {
1938 PyObject *array_module = PyImport_ImportModule("array");
1939 if (array_module == NULL)
1940 return NULL;
1941 array_reconstructor = PyObject_GetAttrString(
1942 array_module,
1943 "_array_reconstructor");
1944 Py_DECREF(array_module);
1945 if (array_reconstructor == NULL)
1946 return NULL;
1947 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 if (!PyLong_Check(value)) {
1950 PyErr_SetString(PyExc_TypeError,
1951 "__reduce_ex__ argument should an integer");
1952 return NULL;
1953 }
1954 protocol = PyLong_AsLong(value);
1955 if (protocol == -1 && PyErr_Occurred())
1956 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 dict = PyObject_GetAttrString((PyObject *)array, "__dict__");
1959 if (dict == NULL) {
1960 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1961 return NULL;
1962 PyErr_Clear();
1963 dict = Py_None;
1964 Py_INCREF(dict);
1965 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 mformat_code = typecode_to_mformat_code(typecode);
1968 if (mformat_code == UNKNOWN_FORMAT || protocol < 3) {
1969 /* Convert the array to a list if we got something weird
1970 * (e.g., non-IEEE floats), or we are pickling the array using
1971 * a Python 2.x compatible protocol.
1972 *
1973 * It is necessary to use a list representation for Python 2.x
1974 * compatible pickle protocol, since Python 2's str objects
1975 * are unpickled as unicode by Python 3. Thus it is impossible
1976 * to make arrays unpicklable by Python 3 by using their memory
1977 * representation, unless we resort to ugly hacks such as
1978 * coercing unicode objects to bytes in array_reconstructor.
1979 */
1980 PyObject *list;
1981 list = array_tolist(array, NULL);
1982 if (list == NULL) {
1983 Py_DECREF(dict);
1984 return NULL;
1985 }
1986 result = Py_BuildValue(
1987 "O(CO)O", Py_TYPE(array), typecode, list, dict);
1988 Py_DECREF(list);
1989 Py_DECREF(dict);
1990 return result;
1991 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001992
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001993 array_str = array_tobytes(array, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 if (array_str == NULL) {
1995 Py_DECREF(dict);
1996 return NULL;
1997 }
1998 result = Py_BuildValue(
1999 "O(OCiN)O", array_reconstructor, Py_TYPE(array), typecode,
2000 mformat_code, array_str, dict);
2001 Py_DECREF(dict);
2002 return result;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002003}
2004
2005PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2006
Martin v. Löwis99866332002-03-01 10:27:01 +00002007static PyObject *
2008array_get_typecode(arrayobject *a, void *closure)
2009{
Amaury Forgeot d'Arc24aa26b2010-11-25 08:13:35 +00002010 Py_UNICODE tc = a->ob_descr->typecode;
2011 return PyUnicode_FromUnicode(&tc, 1);
Martin v. Löwis99866332002-03-01 10:27:01 +00002012}
2013
2014static PyObject *
2015array_get_itemsize(arrayobject *a, void *closure)
2016{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 return PyLong_FromLong((long)a->ob_descr->itemsize);
Martin v. Löwis99866332002-03-01 10:27:01 +00002018}
2019
2020static PyGetSetDef array_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 {"typecode", (getter) array_get_typecode, NULL,
2022 "the typecode character used to create the array"},
2023 {"itemsize", (getter) array_get_itemsize, NULL,
2024 "the size, in bytes, of one array item"},
2025 {NULL}
Martin v. Löwis99866332002-03-01 10:27:01 +00002026};
2027
Martin v. Löwis59683e82008-06-13 07:50:45 +00002028static PyMethodDef array_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 {"append", (PyCFunction)array_append, METH_O,
2030 append_doc},
2031 {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS,
2032 buffer_info_doc},
2033 {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
2034 byteswap_doc},
2035 {"__copy__", (PyCFunction)array_copy, METH_NOARGS,
2036 copy_doc},
2037 {"count", (PyCFunction)array_count, METH_O,
2038 count_doc},
2039 {"__deepcopy__",(PyCFunction)array_copy, METH_O,
2040 copy_doc},
2041 {"extend", (PyCFunction)array_extend, METH_O,
2042 extend_doc},
2043 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
2044 fromfile_doc},
2045 {"fromlist", (PyCFunction)array_fromlist, METH_O,
2046 fromlist_doc},
2047 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
2048 fromstring_doc},
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00002049 {"frombytes", (PyCFunction)array_frombytes, METH_VARARGS,
2050 frombytes_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
2052 fromunicode_doc},
2053 {"index", (PyCFunction)array_index, METH_O,
2054 index_doc},
2055 {"insert", (PyCFunction)array_insert, METH_VARARGS,
2056 insert_doc},
2057 {"pop", (PyCFunction)array_pop, METH_VARARGS,
2058 pop_doc},
2059 {"__reduce_ex__", (PyCFunction)array_reduce_ex, METH_O,
2060 reduce_doc},
2061 {"remove", (PyCFunction)array_remove, METH_O,
2062 remove_doc},
2063 {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
2064 reverse_doc},
2065/* {"sort", (PyCFunction)array_sort, METH_VARARGS,
2066 sort_doc},*/
2067 {"tofile", (PyCFunction)array_tofile, METH_O,
2068 tofile_doc},
2069 {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
2070 tolist_doc},
2071 {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
2072 tostring_doc},
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00002073 {"tobytes", (PyCFunction)array_tobytes, METH_NOARGS,
2074 tobytes_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
2076 tounicode_doc},
2077 {NULL, NULL} /* sentinel */
Guido van Rossum778983b1993-02-19 15:55:02 +00002078};
2079
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002080static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00002081array_repr(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +00002082{
Amaury Forgeot d'Arc24aa26b2010-11-25 08:13:35 +00002083 Py_UNICODE typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 PyObject *s, *v = NULL;
2085 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00002086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 len = Py_SIZE(a);
2088 typecode = a->ob_descr->typecode;
2089 if (len == 0) {
Amaury Forgeot d'Arc24aa26b2010-11-25 08:13:35 +00002090 return PyUnicode_FromFormat("array('%c')", (int)typecode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 }
Brett Cannon4a5e5de2011-06-07 20:09:32 -07002092 if (typecode == 'u')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 v = array_tounicode(a, NULL);
2094 else
2095 v = array_tolist(a, NULL);
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00002096
Amaury Forgeot d'Arc24aa26b2010-11-25 08:13:35 +00002097 s = PyUnicode_FromFormat("array('%c', %R)", (int)typecode, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 Py_DECREF(v);
2099 return s;
Guido van Rossum778983b1993-02-19 15:55:02 +00002100}
2101
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002102static PyObject*
2103array_subscr(arrayobject* self, PyObject* item)
2104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 if (PyIndex_Check(item)) {
2106 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2107 if (i==-1 && PyErr_Occurred()) {
2108 return NULL;
2109 }
2110 if (i < 0)
2111 i += Py_SIZE(self);
2112 return array_item(self, i);
2113 }
2114 else if (PySlice_Check(item)) {
2115 Py_ssize_t start, stop, step, slicelength, cur, i;
2116 PyObject* result;
2117 arrayobject* ar;
2118 int itemsize = self->ob_descr->itemsize;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002119
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002120 if (PySlice_GetIndicesEx(item, Py_SIZE(self),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 &start, &stop, &step, &slicelength) < 0) {
2122 return NULL;
2123 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002125 if (slicelength <= 0) {
2126 return newarrayobject(&Arraytype, 0, self->ob_descr);
2127 }
2128 else if (step == 1) {
2129 PyObject *result = newarrayobject(&Arraytype,
2130 slicelength, self->ob_descr);
2131 if (result == NULL)
2132 return NULL;
2133 memcpy(((arrayobject *)result)->ob_item,
2134 self->ob_item + start * itemsize,
2135 slicelength * itemsize);
2136 return result;
2137 }
2138 else {
2139 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
2140 if (!result) return NULL;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002142 ar = (arrayobject*)result;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 for (cur = start, i = 0; i < slicelength;
2145 cur += step, i++) {
2146 memcpy(ar->ob_item + i*itemsize,
2147 self->ob_item + cur*itemsize,
2148 itemsize);
2149 }
2150
2151 return result;
2152 }
2153 }
2154 else {
2155 PyErr_SetString(PyExc_TypeError,
2156 "array indices must be integers");
2157 return NULL;
2158 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002159}
2160
2161static int
2162array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
2163{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 Py_ssize_t start, stop, step, slicelength, needed;
2165 arrayobject* other;
2166 int itemsize;
Thomas Woutersed03b412007-08-28 21:37:11 +00002167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 if (PyIndex_Check(item)) {
2169 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Alexandre Vassalotti47137252009-07-05 19:57:00 +00002170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 if (i == -1 && PyErr_Occurred())
2172 return -1;
2173 if (i < 0)
2174 i += Py_SIZE(self);
2175 if (i < 0 || i >= Py_SIZE(self)) {
2176 PyErr_SetString(PyExc_IndexError,
2177 "array assignment index out of range");
2178 return -1;
2179 }
2180 if (value == NULL) {
2181 /* Fall through to slice assignment */
2182 start = i;
2183 stop = i + 1;
2184 step = 1;
2185 slicelength = 1;
2186 }
2187 else
2188 return (*self->ob_descr->setitem)(self, i, value);
2189 }
2190 else if (PySlice_Check(item)) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002191 if (PySlice_GetIndicesEx(item,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 Py_SIZE(self), &start, &stop,
2193 &step, &slicelength) < 0) {
2194 return -1;
2195 }
2196 }
2197 else {
2198 PyErr_SetString(PyExc_TypeError,
2199 "array indices must be integer");
2200 return -1;
2201 }
2202 if (value == NULL) {
2203 other = NULL;
2204 needed = 0;
2205 }
2206 else if (array_Check(value)) {
2207 other = (arrayobject *)value;
2208 needed = Py_SIZE(other);
2209 if (self == other) {
2210 /* Special case "self[i:j] = self" -- copy self first */
2211 int ret;
2212 value = array_slice(other, 0, needed);
2213 if (value == NULL)
2214 return -1;
2215 ret = array_ass_subscr(self, item, value);
2216 Py_DECREF(value);
2217 return ret;
2218 }
2219 if (other->ob_descr != self->ob_descr) {
2220 PyErr_BadArgument();
2221 return -1;
2222 }
2223 }
2224 else {
2225 PyErr_Format(PyExc_TypeError,
2226 "can only assign array (not \"%.200s\") to array slice",
2227 Py_TYPE(value)->tp_name);
2228 return -1;
2229 }
2230 itemsize = self->ob_descr->itemsize;
2231 /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
2232 if ((step > 0 && stop < start) ||
2233 (step < 0 && stop > start))
2234 stop = start;
Alexandre Vassalotti47137252009-07-05 19:57:00 +00002235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 /* Issue #4509: If the array has exported buffers and the slice
2237 assignment would change the size of the array, fail early to make
2238 sure we don't modify it. */
2239 if ((needed == 0 || slicelength != needed) && self->ob_exports > 0) {
2240 PyErr_SetString(PyExc_BufferError,
2241 "cannot resize an array that is exporting buffers");
2242 return -1;
2243 }
Mark Dickinsonbc099642010-01-29 17:27:24 +00002244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 if (step == 1) {
2246 if (slicelength > needed) {
2247 memmove(self->ob_item + (start + needed) * itemsize,
2248 self->ob_item + stop * itemsize,
2249 (Py_SIZE(self) - stop) * itemsize);
2250 if (array_resize(self, Py_SIZE(self) +
2251 needed - slicelength) < 0)
2252 return -1;
2253 }
2254 else if (slicelength < needed) {
2255 if (array_resize(self, Py_SIZE(self) +
2256 needed - slicelength) < 0)
2257 return -1;
2258 memmove(self->ob_item + (start + needed) * itemsize,
2259 self->ob_item + stop * itemsize,
2260 (Py_SIZE(self) - start - needed) * itemsize);
2261 }
2262 if (needed > 0)
2263 memcpy(self->ob_item + start * itemsize,
2264 other->ob_item, needed * itemsize);
2265 return 0;
2266 }
2267 else if (needed == 0) {
2268 /* Delete slice */
2269 size_t cur;
2270 Py_ssize_t i;
Thomas Woutersed03b412007-08-28 21:37:11 +00002271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 if (step < 0) {
2273 stop = start + 1;
2274 start = stop + step * (slicelength - 1) - 1;
2275 step = -step;
2276 }
2277 for (cur = start, i = 0; i < slicelength;
2278 cur += step, i++) {
2279 Py_ssize_t lim = step - 1;
Thomas Woutersed03b412007-08-28 21:37:11 +00002280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 if (cur + step >= (size_t)Py_SIZE(self))
2282 lim = Py_SIZE(self) - cur - 1;
2283 memmove(self->ob_item + (cur - i) * itemsize,
2284 self->ob_item + (cur + 1) * itemsize,
2285 lim * itemsize);
2286 }
2287 cur = start + slicelength * step;
2288 if (cur < (size_t)Py_SIZE(self)) {
2289 memmove(self->ob_item + (cur-slicelength) * itemsize,
2290 self->ob_item + cur * itemsize,
2291 (Py_SIZE(self) - cur) * itemsize);
2292 }
2293 if (array_resize(self, Py_SIZE(self) - slicelength) < 0)
2294 return -1;
2295 return 0;
2296 }
2297 else {
2298 Py_ssize_t cur, i;
2299
2300 if (needed != slicelength) {
2301 PyErr_Format(PyExc_ValueError,
2302 "attempt to assign array of size %zd "
2303 "to extended slice of size %zd",
2304 needed, slicelength);
2305 return -1;
2306 }
2307 for (cur = start, i = 0; i < slicelength;
2308 cur += step, i++) {
2309 memcpy(self->ob_item + cur * itemsize,
2310 other->ob_item + i * itemsize,
2311 itemsize);
2312 }
2313 return 0;
2314 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002315}
2316
2317static PyMappingMethods array_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 (lenfunc)array_length,
2319 (binaryfunc)array_subscr,
2320 (objobjargproc)array_ass_subscr
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002321};
2322
Guido van Rossumd8faa362007-04-27 19:54:29 +00002323static const void *emptybuf = "";
2324
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002325
2326static int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00002327array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002329 if (view==NULL) goto finish;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 view->buf = (void *)self->ob_item;
2332 view->obj = (PyObject*)self;
2333 Py_INCREF(self);
2334 if (view->buf == NULL)
2335 view->buf = (void *)emptybuf;
2336 view->len = (Py_SIZE(self)) * self->ob_descr->itemsize;
2337 view->readonly = 0;
2338 view->ndim = 1;
2339 view->itemsize = self->ob_descr->itemsize;
2340 view->suboffsets = NULL;
2341 view->shape = NULL;
2342 if ((flags & PyBUF_ND)==PyBUF_ND) {
2343 view->shape = &((Py_SIZE(self)));
2344 }
2345 view->strides = NULL;
2346 if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES)
2347 view->strides = &(view->itemsize);
2348 view->format = NULL;
2349 view->internal = NULL;
2350 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
2351 view->format = self->ob_descr->formats;
Travis E. Oliphantddacf962007-10-13 21:03:27 +00002352#ifdef Py_UNICODE_WIDE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 if (self->ob_descr->typecode == 'u') {
2354 view->format = "w";
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002355 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356#endif
2357 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002358
2359 finish:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 self->ob_exports++;
2361 return 0;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002362}
2363
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002364static void
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00002365array_buffer_relbuf(arrayobject *self, Py_buffer *view)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 self->ob_exports--;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002368}
2369
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002370static PySequenceMethods array_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 (lenfunc)array_length, /*sq_length*/
2372 (binaryfunc)array_concat, /*sq_concat*/
2373 (ssizeargfunc)array_repeat, /*sq_repeat*/
2374 (ssizeargfunc)array_item, /*sq_item*/
2375 0, /*sq_slice*/
2376 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
2377 0, /*sq_ass_slice*/
2378 (objobjproc)array_contains, /*sq_contains*/
2379 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
2380 (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/
Guido van Rossum778983b1993-02-19 15:55:02 +00002381};
2382
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002383static PyBufferProcs array_as_buffer = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 (getbufferproc)array_buffer_getbuf,
2385 (releasebufferproc)array_buffer_relbuf
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002386};
2387
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002388static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +00002389array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum778983b1993-02-19 15:55:02 +00002390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 int c;
2392 PyObject *initial = NULL, *it = NULL;
2393 struct arraydescr *descr;
Martin v. Löwis99866332002-03-01 10:27:01 +00002394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds))
2396 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00002397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
2399 return NULL;
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00002400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 if (!(initial == NULL || PyList_Check(initial)
2402 || PyByteArray_Check(initial)
2403 || PyBytes_Check(initial)
2404 || PyTuple_Check(initial)
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002405 || ((c=='u') && PyUnicode_Check(initial))
2406 || (array_Check(initial)
2407 && c == ((arrayobject*)initial)->ob_descr->typecode))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 it = PyObject_GetIter(initial);
2409 if (it == NULL)
2410 return NULL;
2411 /* We set initial to NULL so that the subsequent code
2412 will create an empty array of the appropriate type
2413 and afterwards we can use array_iter_extend to populate
2414 the array.
2415 */
2416 initial = NULL;
2417 }
2418 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2419 if (descr->typecode == c) {
2420 PyObject *a;
2421 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00002422
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002423 if (initial == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 len = 0;
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002425 else if (PyList_Check(initial))
2426 len = PyList_GET_SIZE(initial);
2427 else if (PyTuple_Check(initial) || array_Check(initial))
2428 len = Py_SIZE(initial);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 else
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002430 len = 0;
Martin v. Löwis99866332002-03-01 10:27:01 +00002431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 a = newarrayobject(type, len, descr);
2433 if (a == NULL)
2434 return NULL;
2435
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002436 if (len > 0 && !array_Check(initial)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 Py_ssize_t i;
2438 for (i = 0; i < len; i++) {
2439 PyObject *v =
2440 PySequence_GetItem(initial, i);
2441 if (v == NULL) {
2442 Py_DECREF(a);
2443 return NULL;
2444 }
2445 if (setarrayitem(a, i, v) != 0) {
2446 Py_DECREF(v);
2447 Py_DECREF(a);
2448 return NULL;
2449 }
2450 Py_DECREF(v);
2451 }
2452 }
2453 else if (initial != NULL && (PyByteArray_Check(initial) ||
2454 PyBytes_Check(initial))) {
2455 PyObject *t_initial, *v;
2456 t_initial = PyTuple_Pack(1, initial);
2457 if (t_initial == NULL) {
2458 Py_DECREF(a);
2459 return NULL;
2460 }
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00002461 v = array_frombytes((arrayobject *)a,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 t_initial);
2463 Py_DECREF(t_initial);
2464 if (v == NULL) {
2465 Py_DECREF(a);
2466 return NULL;
2467 }
2468 Py_DECREF(v);
2469 }
2470 else if (initial != NULL && PyUnicode_Check(initial)) {
2471 Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial);
2472 if (n > 0) {
2473 arrayobject *self = (arrayobject *)a;
2474 char *item = self->ob_item;
2475 item = (char *)PyMem_Realloc(item, n);
2476 if (item == NULL) {
2477 PyErr_NoMemory();
2478 Py_DECREF(a);
2479 return NULL;
2480 }
2481 self->ob_item = item;
2482 Py_SIZE(self) = n / sizeof(Py_UNICODE);
2483 memcpy(item, PyUnicode_AS_DATA(initial), n);
2484 self->allocated = Py_SIZE(self);
2485 }
2486 }
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002487 else if (initial != NULL && array_Check(initial)) {
2488 arrayobject *self = (arrayobject *)a;
2489 arrayobject *other = (arrayobject *)initial;
2490 memcpy(self->ob_item, other->ob_item, len * other->ob_descr->itemsize);
2491 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 if (it != NULL) {
2493 if (array_iter_extend((arrayobject *)a, it) == -1) {
2494 Py_DECREF(it);
2495 Py_DECREF(a);
2496 return NULL;
2497 }
2498 Py_DECREF(it);
2499 }
2500 return a;
2501 }
2502 }
2503 PyErr_SetString(PyExc_ValueError,
2504 "bad typecode (must be b, B, u, h, H, i, I, l, L, f or d)");
2505 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00002506}
2507
Guido van Rossum778983b1993-02-19 15:55:02 +00002508
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002509PyDoc_STRVAR(module_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00002510"This module defines an object type which can efficiently represent\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002511an array of basic values: characters, integers, floating point\n\
2512numbers. Arrays are sequence types and behave very much like lists,\n\
2513except that the type of objects stored in them is constrained. The\n\
2514type is specified at object creation time by using a type code, which\n\
2515is a single character. The following type codes are defined:\n\
2516\n\
2517 Type code C Type Minimum size in bytes \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002518 'b' signed integer 1 \n\
2519 'B' unsigned integer 1 \n\
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00002520 'u' Unicode character 2 (see note) \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002521 'h' signed integer 2 \n\
2522 'H' unsigned integer 2 \n\
2523 'i' signed integer 2 \n\
2524 'I' unsigned integer 2 \n\
2525 'l' signed integer 4 \n\
2526 'L' unsigned integer 4 \n\
2527 'f' floating point 4 \n\
2528 'd' floating point 8 \n\
2529\n\
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00002530NOTE: The 'u' typecode corresponds to Python's unicode character. On \n\
2531narrow builds this is 2-bytes on wide builds this is 4-bytes.\n\
2532\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002533The constructor is:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002534\n\
2535array(typecode [, initializer]) -- create a new array\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002536");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002537
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002538PyDoc_STRVAR(arraytype_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00002539"array(typecode [, initializer]) -> array\n\
2540\n\
2541Return a new array whose items are restricted by typecode, and\n\
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00002542initialized from the optional initializer value, which must be a list,\n\
2543string. or iterable over elements of the appropriate type.\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002544\n\
2545Arrays represent basic values and behave very much like lists, except\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002546the type of objects stored in them is constrained.\n\
2547\n\
2548Methods:\n\
2549\n\
2550append() -- append a new item to the end of the array\n\
2551buffer_info() -- return information giving the current memory info\n\
2552byteswap() -- byteswap all the items of the array\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002553count() -- return number of occurrences of an object\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00002554extend() -- extend array by appending multiple elements from an iterable\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002555fromfile() -- read items from a file object\n\
2556fromlist() -- append items from the list\n\
2557fromstring() -- append items from the string\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002558index() -- return index of first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002559insert() -- insert a new item into the array at a provided position\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00002560pop() -- remove and return item (default last)\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002561remove() -- remove first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002562reverse() -- reverse the order of the items in the array\n\
2563tofile() -- write all items to a file object\n\
2564tolist() -- return the array converted to an ordinary list\n\
2565tostring() -- return the array converted to a string\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002566\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002567Attributes:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002568\n\
2569typecode -- the typecode character used to create the array\n\
2570itemsize -- the length in bytes of one array item\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002571");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002572
Raymond Hettinger625812f2003-01-07 01:58:52 +00002573static PyObject *array_iter(arrayobject *ao);
2574
Tim Peters0c322792002-07-17 16:49:03 +00002575static PyTypeObject Arraytype = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002576 PyVarObject_HEAD_INIT(NULL, 0)
2577 "array.array",
2578 sizeof(arrayobject),
2579 0,
2580 (destructor)array_dealloc, /* tp_dealloc */
2581 0, /* tp_print */
2582 0, /* tp_getattr */
2583 0, /* tp_setattr */
2584 0, /* tp_reserved */
2585 (reprfunc)array_repr, /* tp_repr */
2586 0, /* tp_as_number*/
2587 &array_as_sequence, /* tp_as_sequence*/
2588 &array_as_mapping, /* tp_as_mapping*/
2589 0, /* tp_hash */
2590 0, /* tp_call */
2591 0, /* tp_str */
2592 PyObject_GenericGetAttr, /* tp_getattro */
2593 0, /* tp_setattro */
2594 &array_as_buffer, /* tp_as_buffer*/
2595 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2596 arraytype_doc, /* tp_doc */
2597 0, /* tp_traverse */
2598 0, /* tp_clear */
2599 array_richcompare, /* tp_richcompare */
2600 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
2601 (getiterfunc)array_iter, /* tp_iter */
2602 0, /* tp_iternext */
2603 array_methods, /* tp_methods */
2604 0, /* tp_members */
2605 array_getsets, /* tp_getset */
2606 0, /* tp_base */
2607 0, /* tp_dict */
2608 0, /* tp_descr_get */
2609 0, /* tp_descr_set */
2610 0, /* tp_dictoffset */
2611 0, /* tp_init */
2612 PyType_GenericAlloc, /* tp_alloc */
2613 array_new, /* tp_new */
2614 PyObject_Del, /* tp_free */
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002615};
2616
Raymond Hettinger625812f2003-01-07 01:58:52 +00002617
2618/*********************** Array Iterator **************************/
2619
2620typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 PyObject_HEAD
2622 Py_ssize_t index;
2623 arrayobject *ao;
2624 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002625} arrayiterobject;
2626
2627static PyTypeObject PyArrayIter_Type;
2628
2629#define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
2630
2631static PyObject *
2632array_iter(arrayobject *ao)
2633{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002634 arrayiterobject *it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002636 if (!array_Check(ao)) {
2637 PyErr_BadInternalCall();
2638 return NULL;
2639 }
Raymond Hettinger625812f2003-01-07 01:58:52 +00002640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002641 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
2642 if (it == NULL)
2643 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 Py_INCREF(ao);
2646 it->ao = ao;
2647 it->index = 0;
2648 it->getitem = ao->ob_descr->getitem;
2649 PyObject_GC_Track(it);
2650 return (PyObject *)it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002651}
2652
2653static PyObject *
Raymond Hettinger625812f2003-01-07 01:58:52 +00002654arrayiter_next(arrayiterobject *it)
2655{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656 assert(PyArrayIter_Check(it));
2657 if (it->index < Py_SIZE(it->ao))
2658 return (*it->getitem)(it->ao, it->index++);
2659 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002660}
2661
2662static void
2663arrayiter_dealloc(arrayiterobject *it)
2664{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 PyObject_GC_UnTrack(it);
2666 Py_XDECREF(it->ao);
2667 PyObject_GC_Del(it);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002668}
2669
2670static int
2671arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2672{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 Py_VISIT(it->ao);
2674 return 0;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002675}
2676
2677static PyTypeObject PyArrayIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002678 PyVarObject_HEAD_INIT(NULL, 0)
2679 "arrayiterator", /* tp_name */
2680 sizeof(arrayiterobject), /* tp_basicsize */
2681 0, /* tp_itemsize */
2682 /* methods */
2683 (destructor)arrayiter_dealloc, /* tp_dealloc */
2684 0, /* tp_print */
2685 0, /* tp_getattr */
2686 0, /* tp_setattr */
2687 0, /* tp_reserved */
2688 0, /* tp_repr */
2689 0, /* tp_as_number */
2690 0, /* tp_as_sequence */
2691 0, /* tp_as_mapping */
2692 0, /* tp_hash */
2693 0, /* tp_call */
2694 0, /* tp_str */
2695 PyObject_GenericGetAttr, /* tp_getattro */
2696 0, /* tp_setattro */
2697 0, /* tp_as_buffer */
2698 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2699 0, /* tp_doc */
2700 (traverseproc)arrayiter_traverse, /* tp_traverse */
2701 0, /* tp_clear */
2702 0, /* tp_richcompare */
2703 0, /* tp_weaklistoffset */
2704 PyObject_SelfIter, /* tp_iter */
2705 (iternextfunc)arrayiter_next, /* tp_iternext */
2706 0, /* tp_methods */
Raymond Hettinger625812f2003-01-07 01:58:52 +00002707};
2708
2709
2710/*********************** Install Module **************************/
2711
Martin v. Löwis99866332002-03-01 10:27:01 +00002712/* No functions in array module. */
2713static PyMethodDef a_methods[] = {
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002714 {"_array_reconstructor", array_reconstructor, METH_VARARGS,
2715 PyDoc_STR("Internal. Used for pickling support.")},
Martin v. Löwis99866332002-03-01 10:27:01 +00002716 {NULL, NULL, 0, NULL} /* Sentinel */
2717};
2718
Martin v. Löwis1a214512008-06-11 05:26:20 +00002719static struct PyModuleDef arraymodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002720 PyModuleDef_HEAD_INIT,
2721 "array",
2722 module_doc,
2723 -1,
2724 a_methods,
2725 NULL,
2726 NULL,
2727 NULL,
2728 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002729};
2730
Martin v. Löwis99866332002-03-01 10:27:01 +00002731
Mark Hammondfe51c6d2002-08-02 02:27:13 +00002732PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002733PyInit_array(void)
Guido van Rossum778983b1993-02-19 15:55:02 +00002734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002735 PyObject *m;
2736 PyObject *typecodes;
2737 Py_ssize_t size = 0;
2738 register Py_UNICODE *p;
2739 struct arraydescr *descr;
Fred Drake0d40ba42000-02-04 20:33:49 +00002740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 if (PyType_Ready(&Arraytype) < 0)
2742 return NULL;
2743 Py_TYPE(&PyArrayIter_Type) = &PyType_Type;
2744 m = PyModule_Create(&arraymodule);
2745 if (m == NULL)
2746 return NULL;
Fred Drakef4e34842002-04-01 03:45:06 +00002747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002748 Py_INCREF((PyObject *)&Arraytype);
2749 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
2750 Py_INCREF((PyObject *)&Arraytype);
2751 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00002752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002753 for (descr=descriptors; descr->typecode != '\0'; descr++) {
2754 size++;
2755 }
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00002756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 typecodes = PyUnicode_FromStringAndSize(NULL, size);
2758 p = PyUnicode_AS_UNICODE(typecodes);
2759 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2760 *p++ = (char)descr->typecode;
2761 }
2762
2763 PyModule_AddObject(m, "typecodes", (PyObject *)typecodes);
2764
2765 if (PyErr_Occurred()) {
2766 Py_DECREF(m);
2767 m = NULL;
2768 }
2769 return m;
Guido van Rossum778983b1993-02-19 15:55:02 +00002770}