blob: 6ece49ff2a62bdee69944e22f94751a61b0c3e0b [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 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000025 int typecode;
26 int itemsize;
27 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
28 int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *);
29 char *formats;
30 int is_integer_type;
31 int is_signed;
Guido van Rossum778983b1993-02-19 15:55:02 +000032};
33
34typedef struct arrayobject {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 PyObject_VAR_HEAD
36 char *ob_item;
37 Py_ssize_t allocated;
38 struct arraydescr *ob_descr;
39 PyObject *weakreflist; /* List of weak references */
40 int ob_exports; /* Number of exported buffers */
Guido van Rossum778983b1993-02-19 15:55:02 +000041} arrayobject;
42
Jeremy Hylton938ace62002-07-17 16:30:39 +000043static PyTypeObject Arraytype;
Guido van Rossum778983b1993-02-19 15:55:02 +000044
Martin v. Löwis99866332002-03-01 10:27:01 +000045#define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
Christian Heimes90aa7642007-12-19 02:45:37 +000046#define array_CheckExact(op) (Py_TYPE(op) == &Arraytype)
Guido van Rossum778983b1993-02-19 15:55:02 +000047
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000048static int
Martin v. Löwis18e16552006-02-15 17:27:45 +000049array_resize(arrayobject *self, Py_ssize_t newsize)
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000050{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 char *items;
52 size_t _new_size;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 if (self->ob_exports > 0 && newsize != Py_SIZE(self)) {
55 PyErr_SetString(PyExc_BufferError,
56 "cannot resize an array that is exporting buffers");
57 return -1;
58 }
Antoine Pitrou3ad3a0d2008-12-18 17:08:32 +000059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 /* Bypass realloc() when a previous overallocation is large enough
61 to accommodate the newsize. If the newsize is 16 smaller than the
62 current size, then proceed with the realloc() to shrink the array.
63 */
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 if (self->allocated >= newsize &&
66 Py_SIZE(self) < newsize + 16 &&
67 self->ob_item != NULL) {
68 Py_SIZE(self) = newsize;
69 return 0;
70 }
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 if (newsize == 0) {
73 PyMem_FREE(self->ob_item);
74 self->ob_item = NULL;
75 Py_SIZE(self) = 0;
76 self->allocated = 0;
77 return 0;
78 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +000079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 /* This over-allocates proportional to the array size, making room
81 * for additional growth. The over-allocation is mild, but is
82 * enough to give linear-time amortized behavior over a long
83 * sequence of appends() in the presence of a poorly-performing
84 * system realloc().
85 * The growth pattern is: 0, 4, 8, 16, 25, 34, 46, 56, 67, 79, ...
86 * Note, the pattern starts out the same as for lists but then
87 * grows at a smaller rate so that larger arrays only overallocate
88 * by about 1/16th -- this is done because arrays are presumed to be more
89 * memory critical.
90 */
Raymond Hettinger6e2ee862004-03-14 04:37:50 +000091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 _new_size = (newsize >> 4) + (Py_SIZE(self) < 8 ? 3 : 7) + newsize;
93 items = self->ob_item;
94 /* XXX The following multiplication and division does not optimize away
95 like it does for lists since the size is not known at compile time */
96 if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize))
97 PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize));
98 else
99 items = NULL;
100 if (items == NULL) {
101 PyErr_NoMemory();
102 return -1;
103 }
104 self->ob_item = items;
105 Py_SIZE(self) = newsize;
106 self->allocated = _new_size;
107 return 0;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000108}
109
Tim Petersbb307342000-09-10 05:22:54 +0000110/****************************************************************************
111Get and Set functions for each type.
112A Get function takes an arrayobject* and an integer index, returning the
113array value at that index wrapped in an appropriate PyObject*.
114A Set function takes an arrayobject, integer index, and PyObject*; sets
115the array value at that index to the raw C data extracted from the PyObject*,
116and returns 0 if successful, else nonzero on failure (PyObject* not of an
117appropriate type or value).
118Note that the basic Get and Set functions do NOT check that the index is
119in bounds; that's the responsibility of the caller.
120****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +0000121
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000122static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000123b_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 long x = ((char *)ap->ob_item)[i];
126 if (x >= 128)
127 x -= 256;
128 return PyLong_FromLong(x);
Guido van Rossum778983b1993-02-19 15:55:02 +0000129}
130
131static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000132b_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000133{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 short x;
135 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
136 must use the next size up that is signed ('h') and manually do
137 the overflow checking */
138 if (!PyArg_Parse(v, "h;array item must be integer", &x))
139 return -1;
140 else if (x < -128) {
141 PyErr_SetString(PyExc_OverflowError,
142 "signed char is less than minimum");
143 return -1;
144 }
145 else if (x > 127) {
146 PyErr_SetString(PyExc_OverflowError,
147 "signed char is greater than maximum");
148 return -1;
149 }
150 if (i >= 0)
151 ((char *)ap->ob_item)[i] = (char)x;
152 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000153}
154
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000155static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000156BB_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 long x = ((unsigned char *)ap->ob_item)[i];
159 return PyLong_FromLong(x);
Guido van Rossum549ab711997-01-03 19:09:47 +0000160}
161
Fred Drake541dc3b2000-06-28 17:49:30 +0000162static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000163BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000164{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 unsigned char x;
166 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
167 if (!PyArg_Parse(v, "b;array item must be integer", &x))
168 return -1;
169 if (i >= 0)
170 ((char *)ap->ob_item)[i] = x;
171 return 0;
Fred Drake541dc3b2000-06-28 17:49:30 +0000172}
Guido van Rossum549ab711997-01-03 19:09:47 +0000173
Martin v. Löwis99866332002-03-01 10:27:01 +0000174static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000175u_getitem(arrayobject *ap, Py_ssize_t i)
Martin v. Löwis99866332002-03-01 10:27:01 +0000176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1);
Martin v. Löwis99866332002-03-01 10:27:01 +0000178}
179
180static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000181u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Martin v. Löwis99866332002-03-01 10:27:01 +0000182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 Py_UNICODE *p;
184 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +0000185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
187 return -1;
188 if (len != 1) {
189 PyErr_SetString(PyExc_TypeError,
190 "array item must be unicode character");
191 return -1;
192 }
193 if (i >= 0)
194 ((Py_UNICODE *)ap->ob_item)[i] = p[0];
195 return 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000196}
Martin v. Löwis99866332002-03-01 10:27:01 +0000197
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +0000198
Guido van Rossum549ab711997-01-03 19:09:47 +0000199static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000200h_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 return PyLong_FromLong((long) ((short *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000203}
204
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +0000205
Guido van Rossum778983b1993-02-19 15:55:02 +0000206static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000207h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 short x;
210 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
211 if (!PyArg_Parse(v, "h;array item must be integer", &x))
212 return -1;
213 if (i >= 0)
214 ((short *)ap->ob_item)[i] = x;
215 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000216}
217
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000218static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000219HH_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 return PyLong_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000222}
223
Fred Drake541dc3b2000-06-28 17:49:30 +0000224static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000225HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 int x;
228 /* PyArg_Parse's 'h' formatter is for a signed short, therefore
229 must use the next size up and manually do the overflow checking */
230 if (!PyArg_Parse(v, "i;array item must be integer", &x))
231 return -1;
232 else if (x < 0) {
233 PyErr_SetString(PyExc_OverflowError,
234 "unsigned short is less than minimum");
235 return -1;
236 }
237 else if (x > USHRT_MAX) {
238 PyErr_SetString(PyExc_OverflowError,
239 "unsigned short is greater than maximum");
240 return -1;
241 }
242 if (i >= 0)
243 ((short *)ap->ob_item)[i] = (short)x;
244 return 0;
Fred Drake541dc3b2000-06-28 17:49:30 +0000245}
Guido van Rossum549ab711997-01-03 19:09:47 +0000246
247static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000248i_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossume77a7571993-11-03 15:01:26 +0000249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 return PyLong_FromLong((long) ((int *)ap->ob_item)[i]);
Guido van Rossume77a7571993-11-03 15:01:26 +0000251}
252
253static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000254i_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossume77a7571993-11-03 15:01:26 +0000255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 int x;
257 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
258 if (!PyArg_Parse(v, "i;array item must be integer", &x))
259 return -1;
260 if (i >= 0)
261 ((int *)ap->ob_item)[i] = x;
262 return 0;
Guido van Rossume77a7571993-11-03 15:01:26 +0000263}
264
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000265static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000266II_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 return PyLong_FromUnsignedLong(
269 (unsigned long) ((unsigned int *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000270}
271
272static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000273II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 unsigned long x;
276 if (PyLong_Check(v)) {
277 x = PyLong_AsUnsignedLong(v);
278 if (x == (unsigned long) -1 && PyErr_Occurred())
279 return -1;
280 }
281 else {
282 long y;
283 if (!PyArg_Parse(v, "l;array item must be integer", &y))
284 return -1;
285 if (y < 0) {
286 PyErr_SetString(PyExc_OverflowError,
287 "unsigned int is less than minimum");
288 return -1;
289 }
290 x = (unsigned long)y;
Tim Petersbb307342000-09-10 05:22:54 +0000291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 }
293 if (x > UINT_MAX) {
294 PyErr_SetString(PyExc_OverflowError,
295 "unsigned int is greater than maximum");
296 return -1;
297 }
Fred Drake541dc3b2000-06-28 17:49:30 +0000298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 if (i >= 0)
300 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
301 return 0;
Guido van Rossum549ab711997-01-03 19:09:47 +0000302}
303
304static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000305l_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 return PyLong_FromLong(((long *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000308}
309
310static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000311l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 long x;
314 if (!PyArg_Parse(v, "l;array item must be integer", &x))
315 return -1;
316 if (i >= 0)
317 ((long *)ap->ob_item)[i] = x;
318 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000319}
320
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000321static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000322LL_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000325}
326
327static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000328LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 unsigned long x;
331 if (PyLong_Check(v)) {
332 x = PyLong_AsUnsignedLong(v);
333 if (x == (unsigned long) -1 && PyErr_Occurred())
334 return -1;
335 }
336 else {
337 long y;
338 if (!PyArg_Parse(v, "l;array item must be integer", &y))
339 return -1;
340 if (y < 0) {
341 PyErr_SetString(PyExc_OverflowError,
342 "unsigned long is less than minimum");
343 return -1;
344 }
345 x = (unsigned long)y;
Tim Petersbb307342000-09-10 05:22:54 +0000346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 }
348 if (x > ULONG_MAX) {
349 PyErr_SetString(PyExc_OverflowError,
350 "unsigned long is greater than maximum");
351 return -1;
352 }
Tim Petersbb307342000-09-10 05:22:54 +0000353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 if (i >= 0)
355 ((unsigned long *)ap->ob_item)[i] = x;
356 return 0;
Guido van Rossum549ab711997-01-03 19:09:47 +0000357}
358
359static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000360f_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000363}
364
365static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000366f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000367{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 float x;
369 if (!PyArg_Parse(v, "f;array item must be float", &x))
370 return -1;
371 if (i >= 0)
372 ((float *)ap->ob_item)[i] = x;
373 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000374}
375
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000376static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000377d_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000380}
381
382static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000383d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 double x;
386 if (!PyArg_Parse(v, "d;array item must be float", &x))
387 return -1;
388 if (i >= 0)
389 ((double *)ap->ob_item)[i] = x;
390 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000391}
392
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000393
Alexandre Vassalottiad077152009-07-15 17:49:23 +0000394/* Description of types.
395 *
396 * Don't forget to update typecode_to_mformat_code() if you add a new
397 * typecode.
398 */
Guido van Rossum234f9421993-06-17 12:35:49 +0000399static struct arraydescr descriptors[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 {'b', 1, b_getitem, b_setitem, "b", 1, 1},
401 {'B', 1, BB_getitem, BB_setitem, "B", 1, 0},
402 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem, "u", 0, 0},
403 {'h', sizeof(short), h_getitem, h_setitem, "h", 1, 1},
404 {'H', sizeof(short), HH_getitem, HH_setitem, "H", 1, 0},
405 {'i', sizeof(int), i_getitem, i_setitem, "i", 1, 1},
406 {'I', sizeof(int), II_getitem, II_setitem, "I", 1, 0},
407 {'l', sizeof(long), l_getitem, l_setitem, "l", 1, 1},
408 {'L', sizeof(long), LL_getitem, LL_setitem, "L", 1, 0},
409 {'f', sizeof(float), f_getitem, f_setitem, "f", 0, 0},
410 {'d', sizeof(double), d_getitem, d_setitem, "d", 0, 0},
411 {'\0', 0, 0, 0, 0, 0, 0} /* Sentinel */
Guido van Rossum778983b1993-02-19 15:55:02 +0000412};
Tim Petersbb307342000-09-10 05:22:54 +0000413
414/****************************************************************************
415Implementations of array object methods.
416****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +0000417
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000418static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000419newarrayobject(PyTypeObject *type, Py_ssize_t size, struct arraydescr *descr)
Guido van Rossum778983b1993-02-19 15:55:02 +0000420{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 arrayobject *op;
422 size_t nbytes;
Martin v. Löwis99866332002-03-01 10:27:01 +0000423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 if (size < 0) {
425 PyErr_BadInternalCall();
426 return NULL;
427 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 nbytes = size * descr->itemsize;
430 /* Check for overflow */
431 if (nbytes / descr->itemsize != (size_t)size) {
432 return PyErr_NoMemory();
433 }
434 op = (arrayobject *) type->tp_alloc(type, 0);
435 if (op == NULL) {
436 return NULL;
437 }
438 op->ob_descr = descr;
439 op->allocated = size;
440 op->weakreflist = NULL;
441 Py_SIZE(op) = size;
442 if (size <= 0) {
443 op->ob_item = NULL;
444 }
445 else {
446 op->ob_item = PyMem_NEW(char, nbytes);
447 if (op->ob_item == NULL) {
448 Py_DECREF(op);
449 return PyErr_NoMemory();
450 }
451 }
452 op->ob_exports = 0;
453 return (PyObject *) op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000454}
455
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000456static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000457getarrayitem(PyObject *op, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 register arrayobject *ap;
460 assert(array_Check(op));
461 ap = (arrayobject *)op;
462 assert(i>=0 && i<Py_SIZE(ap));
463 return (*ap->ob_descr->getitem)(ap, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000464}
465
466static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000467ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 char *items;
470 Py_ssize_t n = Py_SIZE(self);
471 if (v == NULL) {
472 PyErr_BadInternalCall();
473 return -1;
474 }
475 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
476 return -1;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 if (array_resize(self, n+1) == -1)
479 return -1;
480 items = self->ob_item;
481 if (where < 0) {
482 where += n;
483 if (where < 0)
484 where = 0;
485 }
486 if (where > n)
487 where = n;
488 /* appends don't need to call memmove() */
489 if (where != n)
490 memmove(items + (where+1)*self->ob_descr->itemsize,
491 items + where*self->ob_descr->itemsize,
492 (n-where)*self->ob_descr->itemsize);
493 return (*self->ob_descr->setitem)(self, where, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000494}
495
Guido van Rossum778983b1993-02-19 15:55:02 +0000496/* Methods */
497
498static void
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000499array_dealloc(arrayobject *op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 if (op->weakreflist != NULL)
502 PyObject_ClearWeakRefs((PyObject *) op);
503 if (op->ob_item != NULL)
504 PyMem_DEL(op->ob_item);
505 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000506}
507
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000508static PyObject *
509array_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 arrayobject *va, *wa;
512 PyObject *vi = NULL;
513 PyObject *wi = NULL;
514 Py_ssize_t i, k;
515 PyObject *res;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 if (!array_Check(v) || !array_Check(w)) {
518 Py_INCREF(Py_NotImplemented);
519 return Py_NotImplemented;
520 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 va = (arrayobject *)v;
523 wa = (arrayobject *)w;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 if (Py_SIZE(va) != Py_SIZE(wa) && (op == Py_EQ || op == Py_NE)) {
526 /* Shortcut: if the lengths differ, the arrays differ */
527 if (op == Py_EQ)
528 res = Py_False;
529 else
530 res = Py_True;
531 Py_INCREF(res);
532 return res;
533 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 /* Search for the first index where items are different */
536 k = 1;
537 for (i = 0; i < Py_SIZE(va) && i < Py_SIZE(wa); i++) {
538 vi = getarrayitem(v, i);
539 wi = getarrayitem(w, i);
540 if (vi == NULL || wi == NULL) {
541 Py_XDECREF(vi);
542 Py_XDECREF(wi);
543 return NULL;
544 }
545 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
546 if (k == 0)
547 break; /* Keeping vi and wi alive! */
548 Py_DECREF(vi);
549 Py_DECREF(wi);
550 if (k < 0)
551 return NULL;
552 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 if (k) {
555 /* No more items to compare -- compare sizes */
556 Py_ssize_t vs = Py_SIZE(va);
557 Py_ssize_t ws = Py_SIZE(wa);
558 int cmp;
559 switch (op) {
560 case Py_LT: cmp = vs < ws; break;
561 case Py_LE: cmp = vs <= ws; break;
562 case Py_EQ: cmp = vs == ws; break;
563 case Py_NE: cmp = vs != ws; break;
564 case Py_GT: cmp = vs > ws; break;
565 case Py_GE: cmp = vs >= ws; break;
566 default: return NULL; /* cannot happen */
567 }
568 if (cmp)
569 res = Py_True;
570 else
571 res = Py_False;
572 Py_INCREF(res);
573 return res;
574 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 /* We have an item that differs. First, shortcuts for EQ/NE */
577 if (op == Py_EQ) {
578 Py_INCREF(Py_False);
579 res = Py_False;
580 }
581 else if (op == Py_NE) {
582 Py_INCREF(Py_True);
583 res = Py_True;
584 }
585 else {
586 /* Compare the final item again using the proper operator */
587 res = PyObject_RichCompare(vi, wi, op);
588 }
589 Py_DECREF(vi);
590 Py_DECREF(wi);
591 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +0000592}
593
Martin v. Löwis18e16552006-02-15 17:27:45 +0000594static Py_ssize_t
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000595array_length(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +0000596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 return Py_SIZE(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000598}
599
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000600static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000601array_item(arrayobject *a, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 if (i < 0 || i >= Py_SIZE(a)) {
604 PyErr_SetString(PyExc_IndexError, "array index out of range");
605 return NULL;
606 }
607 return getarrayitem((PyObject *)a, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000608}
609
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000610static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000611array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 arrayobject *np;
614 if (ilow < 0)
615 ilow = 0;
616 else if (ilow > Py_SIZE(a))
617 ilow = Py_SIZE(a);
618 if (ihigh < 0)
619 ihigh = 0;
620 if (ihigh < ilow)
621 ihigh = ilow;
622 else if (ihigh > Py_SIZE(a))
623 ihigh = Py_SIZE(a);
624 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
625 if (np == NULL)
626 return NULL;
627 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
628 (ihigh-ilow) * a->ob_descr->itemsize);
629 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000630}
631
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000632static PyObject *
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000633array_copy(arrayobject *a, PyObject *unused)
634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 return array_slice(a, 0, Py_SIZE(a));
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000636}
637
638PyDoc_STRVAR(copy_doc,
639"copy(array)\n\
640\n\
641 Return a copy of the array.");
642
643static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000644array_concat(arrayobject *a, PyObject *bb)
Guido van Rossum778983b1993-02-19 15:55:02 +0000645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 Py_ssize_t size;
647 arrayobject *np;
648 if (!array_Check(bb)) {
649 PyErr_Format(PyExc_TypeError,
650 "can only append array (not \"%.200s\") to array",
651 Py_TYPE(bb)->tp_name);
652 return NULL;
653 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000654#define b ((arrayobject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 if (a->ob_descr != b->ob_descr) {
656 PyErr_BadArgument();
657 return NULL;
658 }
659 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) {
660 return PyErr_NoMemory();
661 }
662 size = Py_SIZE(a) + Py_SIZE(b);
663 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
664 if (np == NULL) {
665 return NULL;
666 }
667 memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize);
668 memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize,
669 b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
670 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000671#undef b
672}
673
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000674static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000675array_repeat(arrayobject *a, Py_ssize_t n)
Guido van Rossum778983b1993-02-19 15:55:02 +0000676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 Py_ssize_t i;
678 Py_ssize_t size;
679 arrayobject *np;
680 char *p;
681 Py_ssize_t nbytes;
682 if (n < 0)
683 n = 0;
684 if ((Py_SIZE(a) != 0) && (n > PY_SSIZE_T_MAX / Py_SIZE(a))) {
685 return PyErr_NoMemory();
686 }
687 size = Py_SIZE(a) * n;
688 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
689 if (np == NULL)
690 return NULL;
691 p = np->ob_item;
692 nbytes = Py_SIZE(a) * a->ob_descr->itemsize;
693 for (i = 0; i < n; i++) {
694 memcpy(p, a->ob_item, nbytes);
695 p += nbytes;
696 }
697 return (PyObject *) np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000698}
699
700static int
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000701array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 char *item;
704 Py_ssize_t n; /* Size of replacement array */
705 Py_ssize_t d; /* Change in size */
Guido van Rossum778983b1993-02-19 15:55:02 +0000706#define b ((arrayobject *)v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 if (v == NULL)
708 n = 0;
709 else if (array_Check(v)) {
710 n = Py_SIZE(b);
711 if (a == b) {
712 /* Special case "a[i:j] = a" -- copy b first */
713 int ret;
714 v = array_slice(b, 0, n);
715 if (!v)
716 return -1;
717 ret = array_ass_slice(a, ilow, ihigh, v);
718 Py_DECREF(v);
719 return ret;
720 }
721 if (b->ob_descr != a->ob_descr) {
722 PyErr_BadArgument();
723 return -1;
724 }
725 }
726 else {
727 PyErr_Format(PyExc_TypeError,
728 "can only assign array (not \"%.200s\") to array slice",
729 Py_TYPE(v)->tp_name);
730 return -1;
731 }
732 if (ilow < 0)
733 ilow = 0;
734 else if (ilow > Py_SIZE(a))
735 ilow = Py_SIZE(a);
736 if (ihigh < 0)
737 ihigh = 0;
738 if (ihigh < ilow)
739 ihigh = ilow;
740 else if (ihigh > Py_SIZE(a))
741 ihigh = Py_SIZE(a);
742 item = a->ob_item;
743 d = n - (ihigh-ilow);
744 /* Issue #4509: If the array has exported buffers and the slice
745 assignment would change the size of the array, fail early to make
746 sure we don't modify it. */
747 if (d != 0 && a->ob_exports > 0) {
748 PyErr_SetString(PyExc_BufferError,
749 "cannot resize an array that is exporting buffers");
750 return -1;
751 }
752 if (d < 0) { /* Delete -d items */
753 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
754 item + ihigh*a->ob_descr->itemsize,
755 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
756 if (array_resize(a, Py_SIZE(a) + d) == -1)
757 return -1;
758 }
759 else if (d > 0) { /* Insert d items */
760 if (array_resize(a, Py_SIZE(a) + d))
761 return -1;
762 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
763 item + ihigh*a->ob_descr->itemsize,
764 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
765 }
766 if (n > 0)
767 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
768 n*b->ob_descr->itemsize);
769 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000770#undef b
771}
772
773static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000774array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000775{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 if (i < 0 || i >= Py_SIZE(a)) {
777 PyErr_SetString(PyExc_IndexError,
778 "array assignment index out of range");
779 return -1;
780 }
781 if (v == NULL)
782 return array_ass_slice(a, i, i+1, v);
783 return (*a->ob_descr->setitem)(a, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000784}
785
786static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000787setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 assert(array_Check(a));
790 return array_ass_item((arrayobject *)a, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000791}
792
Martin v. Löwis99866332002-03-01 10:27:01 +0000793static int
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000794array_iter_extend(arrayobject *self, PyObject *bb)
795{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 PyObject *it, *v;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 it = PyObject_GetIter(bb);
799 if (it == NULL)
800 return -1;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 while ((v = PyIter_Next(it)) != NULL) {
Mark Dickinson346f0af2010-08-06 09:36:57 +0000803 if (ins1(self, Py_SIZE(self), v) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 Py_DECREF(v);
805 Py_DECREF(it);
806 return -1;
807 }
808 Py_DECREF(v);
809 }
810 Py_DECREF(it);
811 if (PyErr_Occurred())
812 return -1;
813 return 0;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000814}
815
816static int
Martin v. Löwis99866332002-03-01 10:27:01 +0000817array_do_extend(arrayobject *self, PyObject *bb)
818{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 Py_ssize_t size, oldsize, bbsize;
Martin v. Löwis99866332002-03-01 10:27:01 +0000820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 if (!array_Check(bb))
822 return array_iter_extend(self, bb);
823#define b ((arrayobject *)bb)
824 if (self->ob_descr != b->ob_descr) {
825 PyErr_SetString(PyExc_TypeError,
826 "can only extend with array of same kind");
827 return -1;
828 }
829 if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) ||
830 ((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
831 PyErr_NoMemory();
832 return -1;
833 }
834 oldsize = Py_SIZE(self);
835 /* Get the size of bb before resizing the array since bb could be self. */
836 bbsize = Py_SIZE(bb);
837 size = oldsize + Py_SIZE(b);
838 if (array_resize(self, size) == -1)
839 return -1;
840 memcpy(self->ob_item + oldsize * self->ob_descr->itemsize,
841 b->ob_item, bbsize * b->ob_descr->itemsize);
842
843 return 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000844#undef b
845}
846
847static PyObject *
848array_inplace_concat(arrayobject *self, PyObject *bb)
849{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 if (!array_Check(bb)) {
851 PyErr_Format(PyExc_TypeError,
852 "can only extend array with array (not \"%.200s\")",
853 Py_TYPE(bb)->tp_name);
854 return NULL;
855 }
856 if (array_do_extend(self, bb) == -1)
857 return NULL;
858 Py_INCREF(self);
859 return (PyObject *)self;
Martin v. Löwis99866332002-03-01 10:27:01 +0000860}
861
862static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000863array_inplace_repeat(arrayobject *self, Py_ssize_t n)
Martin v. Löwis99866332002-03-01 10:27:01 +0000864{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 char *items, *p;
866 Py_ssize_t size, i;
Martin v. Löwis99866332002-03-01 10:27:01 +0000867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 if (Py_SIZE(self) > 0) {
869 if (n < 0)
870 n = 0;
871 items = self->ob_item;
872 if ((self->ob_descr->itemsize != 0) &&
873 (Py_SIZE(self) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
874 return PyErr_NoMemory();
875 }
876 size = Py_SIZE(self) * self->ob_descr->itemsize;
877 if (n > 0 && size > PY_SSIZE_T_MAX / n) {
878 return PyErr_NoMemory();
879 }
880 if (array_resize(self, n * Py_SIZE(self)) == -1)
881 return NULL;
882 items = p = self->ob_item;
883 for (i = 1; i < n; i++) {
884 p += size;
885 memcpy(p, items, size);
886 }
887 }
888 Py_INCREF(self);
889 return (PyObject *)self;
Martin v. Löwis99866332002-03-01 10:27:01 +0000890}
891
892
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000893static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000894ins(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000895{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 if (ins1(self, where, v) != 0)
897 return NULL;
898 Py_INCREF(Py_None);
899 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000900}
901
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000902static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000903array_count(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 Py_ssize_t count = 0;
906 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 for (i = 0; i < Py_SIZE(self); i++) {
909 PyObject *selfi = getarrayitem((PyObject *)self, i);
910 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
911 Py_DECREF(selfi);
912 if (cmp > 0)
913 count++;
914 else if (cmp < 0)
915 return NULL;
916 }
917 return PyLong_FromSsize_t(count);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000918}
919
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000920PyDoc_STRVAR(count_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000921"count(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000922\n\
Mark Dickinson934896d2009-02-21 20:59:32 +0000923Return number of occurrences of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000924
925static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000926array_index(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000927{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 for (i = 0; i < Py_SIZE(self); i++) {
931 PyObject *selfi = getarrayitem((PyObject *)self, i);
932 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
933 Py_DECREF(selfi);
934 if (cmp > 0) {
935 return PyLong_FromLong((long)i);
936 }
937 else if (cmp < 0)
938 return NULL;
939 }
940 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
941 return NULL;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000942}
943
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000944PyDoc_STRVAR(index_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000945"index(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000946\n\
Mark Dickinson934896d2009-02-21 20:59:32 +0000947Return index of first occurrence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000948
Raymond Hettinger625812f2003-01-07 01:58:52 +0000949static int
950array_contains(arrayobject *self, PyObject *v)
951{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 Py_ssize_t i;
953 int cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +0000954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) {
956 PyObject *selfi = getarrayitem((PyObject *)self, i);
957 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
958 Py_DECREF(selfi);
959 }
960 return cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +0000961}
962
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000963static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000964array_remove(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000965{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 int i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 for (i = 0; i < Py_SIZE(self); i++) {
969 PyObject *selfi = getarrayitem((PyObject *)self,i);
970 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
971 Py_DECREF(selfi);
972 if (cmp > 0) {
973 if (array_ass_slice(self, i, i+1,
974 (PyObject *)NULL) != 0)
975 return NULL;
976 Py_INCREF(Py_None);
977 return Py_None;
978 }
979 else if (cmp < 0)
980 return NULL;
981 }
982 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
983 return NULL;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000984}
985
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000986PyDoc_STRVAR(remove_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000987"remove(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000988\n\
Mark Dickinson934896d2009-02-21 20:59:32 +0000989Remove the first occurrence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000990
991static PyObject *
992array_pop(arrayobject *self, PyObject *args)
993{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 Py_ssize_t i = -1;
995 PyObject *v;
996 if (!PyArg_ParseTuple(args, "|n:pop", &i))
997 return NULL;
998 if (Py_SIZE(self) == 0) {
999 /* Special-case most common failure cause */
1000 PyErr_SetString(PyExc_IndexError, "pop from empty array");
1001 return NULL;
1002 }
1003 if (i < 0)
1004 i += Py_SIZE(self);
1005 if (i < 0 || i >= Py_SIZE(self)) {
1006 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1007 return NULL;
1008 }
1009 v = getarrayitem((PyObject *)self,i);
1010 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
1011 Py_DECREF(v);
1012 return NULL;
1013 }
1014 return v;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001015}
1016
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001017PyDoc_STRVAR(pop_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001018"pop([i])\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001019\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001020Return the i-th element and delete it from the array. i defaults to -1.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001021
1022static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001023array_extend(arrayobject *self, PyObject *bb)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001024{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 if (array_do_extend(self, bb) == -1)
1026 return NULL;
1027 Py_INCREF(Py_None);
1028 return Py_None;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001029}
1030
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001031PyDoc_STRVAR(extend_doc,
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001032"extend(array or iterable)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001033\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001034 Append items to the end of the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001035
1036static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001037array_insert(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001038{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 Py_ssize_t i;
1040 PyObject *v;
1041 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
1042 return NULL;
1043 return ins(self, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001044}
1045
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001046PyDoc_STRVAR(insert_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001047"insert(i,x)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001048\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001049Insert a new item x into the array before position i.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001050
1051
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001052static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001053array_buffer_info(arrayobject *self, PyObject *unused)
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001054{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 PyObject* retval = NULL;
1056 retval = PyTuple_New(2);
1057 if (!retval)
1058 return NULL;
Fred Drake541dc3b2000-06-28 17:49:30 +00001059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item));
1061 PyTuple_SET_ITEM(retval, 1, PyLong_FromLong((long)(Py_SIZE(self))));
Fred Drake541dc3b2000-06-28 17:49:30 +00001062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 return retval;
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001064}
1065
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001066PyDoc_STRVAR(buffer_info_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001067"buffer_info() -> (address, length)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001068\n\
1069Return a tuple (address, length) giving the current memory address and\n\
Guido van Rossum702d08e2001-07-27 16:05:32 +00001070the length in items of the buffer used to hold array's contents\n\
1071The length should be multiplied by the itemsize attribute to calculate\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001072the buffer length in bytes.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001073
1074
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001075static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001076array_append(arrayobject *self, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +00001077{
Mark Dickinson346f0af2010-08-06 09:36:57 +00001078 return ins(self, Py_SIZE(self), v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001079}
1080
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001081PyDoc_STRVAR(append_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001082"append(x)\n\
1083\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001084Append new value x to the end of the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001085
1086
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001087static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001088array_byteswap(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001089{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 char *p;
1091 Py_ssize_t i;
Fred Drakebf272981999-12-03 17:15:30 +00001092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 switch (self->ob_descr->itemsize) {
1094 case 1:
1095 break;
1096 case 2:
1097 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) {
1098 char p0 = p[0];
1099 p[0] = p[1];
1100 p[1] = p0;
1101 }
1102 break;
1103 case 4:
1104 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 4) {
1105 char p0 = p[0];
1106 char p1 = p[1];
1107 p[0] = p[3];
1108 p[1] = p[2];
1109 p[2] = p1;
1110 p[3] = p0;
1111 }
1112 break;
1113 case 8:
1114 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) {
1115 char p0 = p[0];
1116 char p1 = p[1];
1117 char p2 = p[2];
1118 char p3 = p[3];
1119 p[0] = p[7];
1120 p[1] = p[6];
1121 p[2] = p[5];
1122 p[3] = p[4];
1123 p[4] = p3;
1124 p[5] = p2;
1125 p[6] = p1;
1126 p[7] = p0;
1127 }
1128 break;
1129 default:
1130 PyErr_SetString(PyExc_RuntimeError,
1131 "don't know how to byteswap this array type");
1132 return NULL;
1133 }
1134 Py_INCREF(Py_None);
1135 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001136}
1137
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001138PyDoc_STRVAR(byteswap_doc,
Fred Drakebf272981999-12-03 17:15:30 +00001139"byteswap()\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001140\n\
Fred Drakebf272981999-12-03 17:15:30 +00001141Byteswap 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 +000011424, or 8 bytes in size, RuntimeError is raised.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001143
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001144static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001145array_reverse(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 register Py_ssize_t itemsize = self->ob_descr->itemsize;
1148 register char *p, *q;
1149 /* little buffer to hold items while swapping */
1150 char tmp[256]; /* 8 is probably enough -- but why skimp */
1151 assert((size_t)itemsize <= sizeof(tmp));
Guido van Rossume77a7571993-11-03 15:01:26 +00001152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 if (Py_SIZE(self) > 1) {
1154 for (p = self->ob_item,
1155 q = self->ob_item + (Py_SIZE(self) - 1)*itemsize;
1156 p < q;
1157 p += itemsize, q -= itemsize) {
1158 /* memory areas guaranteed disjoint, so memcpy
1159 * is safe (& memmove may be slower).
1160 */
1161 memcpy(tmp, p, itemsize);
1162 memcpy(p, q, itemsize);
1163 memcpy(q, tmp, itemsize);
1164 }
1165 }
Tim Petersbb307342000-09-10 05:22:54 +00001166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 Py_INCREF(Py_None);
1168 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001169}
Guido van Rossume77a7571993-11-03 15:01:26 +00001170
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001171PyDoc_STRVAR(reverse_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001172"reverse()\n\
1173\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001174Reverse the order of the items in the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001175
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001176
1177/* Forward */
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001178static PyObject *array_frombytes(arrayobject *self, PyObject *args);
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001179
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001180static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001181array_fromfile(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 PyObject *f, *b, *res;
1184 Py_ssize_t itemsize = self->ob_descr->itemsize;
1185 Py_ssize_t n, nbytes;
1186 int not_enough_bytes;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 if (!PyArg_ParseTuple(args, "On:fromfile", &f, &n))
1189 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 nbytes = n * itemsize;
1192 if (nbytes < 0 || nbytes/itemsize != n) {
1193 PyErr_NoMemory();
1194 return NULL;
1195 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 b = PyObject_CallMethod(f, "read", "n", nbytes);
1198 if (b == NULL)
1199 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 if (!PyBytes_Check(b)) {
1202 PyErr_SetString(PyExc_TypeError,
1203 "read() didn't return bytes");
1204 Py_DECREF(b);
1205 return NULL;
1206 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 not_enough_bytes = (PyBytes_GET_SIZE(b) != nbytes);
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 args = Py_BuildValue("(O)", b);
1211 Py_DECREF(b);
1212 if (args == NULL)
1213 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001214
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001215 res = array_frombytes(self, args);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 Py_DECREF(args);
1217 if (res == NULL)
1218 return NULL;
Hirokazu Yamamoto54d0df62009-03-06 03:04:07 +00001219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 if (not_enough_bytes) {
1221 PyErr_SetString(PyExc_EOFError,
1222 "read() didn't return enough bytes");
1223 Py_DECREF(res);
1224 return NULL;
1225 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +00001228}
1229
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001230PyDoc_STRVAR(fromfile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001231"fromfile(f, n)\n\
1232\n\
1233Read n objects from the file object f and append them to the end of the\n\
Georg Brandlf25ef502008-02-01 11:30:18 +00001234array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001235
1236
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001237static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001238array_tofile(arrayobject *self, PyObject *f)
Guido van Rossum778983b1993-02-19 15:55:02 +00001239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize;
1241 /* Write 64K blocks at a time */
1242 /* XXX Make the block size settable */
1243 int BLOCKSIZE = 64*1024;
1244 Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE;
1245 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 if (Py_SIZE(self) == 0)
1248 goto done;
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 for (i = 0; i < nblocks; i++) {
1251 char* ptr = self->ob_item + i*BLOCKSIZE;
1252 Py_ssize_t size = BLOCKSIZE;
1253 PyObject *bytes, *res;
1254 if (i*BLOCKSIZE + size > nbytes)
1255 size = nbytes - i*BLOCKSIZE;
1256 bytes = PyBytes_FromStringAndSize(ptr, size);
1257 if (bytes == NULL)
1258 return NULL;
1259 res = PyObject_CallMethod(f, "write", "O", bytes);
1260 Py_DECREF(bytes);
1261 if (res == NULL)
1262 return NULL;
1263 Py_DECREF(res); /* drop write result */
1264 }
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001265
1266 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 Py_INCREF(Py_None);
1268 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001269}
1270
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001271PyDoc_STRVAR(tofile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001272"tofile(f)\n\
1273\n\
Georg Brandlf25ef502008-02-01 11:30:18 +00001274Write all items (as machine values) to the file object f.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001275
1276
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001277static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001278array_fromlist(arrayobject *self, PyObject *list)
Guido van Rossum778983b1993-02-19 15:55:02 +00001279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 Py_ssize_t n;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 if (!PyList_Check(list)) {
1283 PyErr_SetString(PyExc_TypeError, "arg must be list");
1284 return NULL;
1285 }
1286 n = PyList_Size(list);
1287 if (n > 0) {
1288 Py_ssize_t i, old_size;
1289 old_size = Py_SIZE(self);
1290 if (array_resize(self, old_size + n) == -1)
1291 return NULL;
1292 for (i = 0; i < n; i++) {
1293 PyObject *v = PyList_GetItem(list, i);
1294 if ((*self->ob_descr->setitem)(self,
1295 Py_SIZE(self) - n + i, v) != 0) {
1296 array_resize(self, old_size);
1297 return NULL;
1298 }
1299 }
1300 }
1301 Py_INCREF(Py_None);
1302 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001303}
1304
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001305PyDoc_STRVAR(fromlist_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001306"fromlist(list)\n\
1307\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001308Append items to array from list.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001309
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001310static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001311array_tolist(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 PyObject *list = PyList_New(Py_SIZE(self));
1314 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 if (list == NULL)
1317 return NULL;
1318 for (i = 0; i < Py_SIZE(self); i++) {
1319 PyObject *v = getarrayitem((PyObject *)self, i);
1320 if (v == NULL) {
1321 Py_DECREF(list);
1322 return NULL;
1323 }
1324 PyList_SetItem(list, i, v);
1325 }
1326 return list;
Guido van Rossum778983b1993-02-19 15:55:02 +00001327}
1328
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001329PyDoc_STRVAR(tolist_doc,
Guido van Rossumfc6aba51998-10-14 02:52:31 +00001330"tolist() -> list\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001331\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001332Convert array to an ordinary list with the same items.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001333
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001334static PyObject *
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001335frombytes(arrayobject *self, Py_buffer *buffer)
Guido van Rossum778983b1993-02-19 15:55:02 +00001336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 int itemsize = self->ob_descr->itemsize;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001338 Py_ssize_t n;
1339 if (buffer->itemsize != 1) {
1340 PyBuffer_Release(buffer);
1341 PyErr_SetString(PyExc_TypeError, "string/buffer of bytes required.");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 return NULL;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001343 }
1344 n = buffer->len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 if (n % itemsize != 0) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001346 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 PyErr_SetString(PyExc_ValueError,
1348 "string length not a multiple of item size");
1349 return NULL;
1350 }
1351 n = n / itemsize;
1352 if (n > 0) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001353 Py_ssize_t old_size = Py_SIZE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 if ((n > PY_SSIZE_T_MAX - old_size) ||
1355 ((old_size + n) > PY_SSIZE_T_MAX / itemsize)) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001356 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 return PyErr_NoMemory();
1358 }
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001359 if (array_resize(self, old_size + n) == -1) {
1360 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 return NULL;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001362 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 memcpy(self->ob_item + old_size * itemsize,
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001364 buffer->buf, n * itemsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 }
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001366 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 Py_INCREF(Py_None);
1368 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001369}
1370
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001371static PyObject *
1372array_fromstring(arrayobject *self, PyObject *args)
1373{
1374 Py_buffer buffer;
1375 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1376 "fromstring() is deprecated. Use frombytes() instead.", 2) != 0)
1377 return NULL;
1378 if (!PyArg_ParseTuple(args, "s*:fromstring", &buffer))
1379 return NULL;
1380 else
1381 return frombytes(self, &buffer);
1382}
1383
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001384PyDoc_STRVAR(fromstring_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001385"fromstring(string)\n\
1386\n\
1387Appends items from the string, interpreting it as an array of machine\n\
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001388values, as if it had been read from a file using the fromfile() method).\n\
1389\n\
1390This method is deprecated. Use frombytes instead.");
1391
1392
1393static PyObject *
1394array_frombytes(arrayobject *self, PyObject *args)
1395{
1396 Py_buffer buffer;
1397 if (!PyArg_ParseTuple(args, "y*:frombytes", &buffer))
1398 return NULL;
1399 else
1400 return frombytes(self, &buffer);
1401}
1402
1403PyDoc_STRVAR(frombytes_doc,
1404"frombytes(bytestring)\n\
1405\n\
1406Appends items from the string, interpreting it as an array of machine\n\
Walter Dörwald93b30b52007-06-22 12:21:53 +00001407values, as if it had been read from a file using the fromfile() method).");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001408
1409
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001410static PyObject *
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001411array_tobytes(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
1414 return PyBytes_FromStringAndSize(self->ob_item,
1415 Py_SIZE(self) * self->ob_descr->itemsize);
1416 } else {
1417 return PyErr_NoMemory();
1418 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001419}
1420
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001421PyDoc_STRVAR(tobytes_doc,
1422"tobytes() -> bytes\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001423\n\
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001424Convert the array to an array of machine values and return the bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001425representation.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001426
Martin v. Löwis99866332002-03-01 10:27:01 +00001427
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001428static PyObject *
1429array_tostring(arrayobject *self, PyObject *unused)
1430{
1431 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1432 "tostring() is deprecated. Use tobytes() instead.", 2) != 0)
1433 return NULL;
1434 return array_tobytes(self, unused);
1435}
1436
1437PyDoc_STRVAR(tostring_doc,
1438"tostring() -> bytes\n\
1439\n\
1440Convert the array to an array of machine values and return the bytes\n\
1441representation.\n\
1442\n\
1443This method is deprecated. Use tobytes instead.");
1444
Martin v. Löwis99866332002-03-01 10:27:01 +00001445
Martin v. Löwis99866332002-03-01 10:27:01 +00001446static PyObject *
1447array_fromunicode(arrayobject *self, PyObject *args)
1448{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 Py_UNICODE *ustr;
1450 Py_ssize_t n;
1451 char typecode;
Martin v. Löwis99866332002-03-01 10:27:01 +00001452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
1454 return NULL;
1455 typecode = self->ob_descr->typecode;
1456 if ((typecode != 'u')) {
1457 PyErr_SetString(PyExc_ValueError,
1458 "fromunicode() may only be called on "
1459 "unicode type arrays");
1460 return NULL;
1461 }
1462 if (n > 0) {
1463 Py_ssize_t old_size = Py_SIZE(self);
1464 if (array_resize(self, old_size + n) == -1)
1465 return NULL;
1466 memcpy(self->ob_item + old_size * sizeof(Py_UNICODE),
1467 ustr, n * sizeof(Py_UNICODE));
1468 }
Martin v. Löwis99866332002-03-01 10:27:01 +00001469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 Py_INCREF(Py_None);
1471 return Py_None;
Martin v. Löwis99866332002-03-01 10:27:01 +00001472}
1473
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001474PyDoc_STRVAR(fromunicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001475"fromunicode(ustr)\n\
1476\n\
1477Extends this array with data from the unicode string ustr.\n\
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001478The array must be a unicode type array; otherwise a ValueError\n\
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001479is raised. Use array.frombytes(ustr.decode(...)) to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001480append Unicode data to an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001481
1482
1483static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001484array_tounicode(arrayobject *self, PyObject *unused)
Martin v. Löwis99866332002-03-01 10:27:01 +00001485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 char typecode;
1487 typecode = self->ob_descr->typecode;
1488 if ((typecode != 'u')) {
1489 PyErr_SetString(PyExc_ValueError,
1490 "tounicode() may only be called on unicode type arrays");
1491 return NULL;
1492 }
1493 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_SIZE(self));
Martin v. Löwis99866332002-03-01 10:27:01 +00001494}
1495
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001496PyDoc_STRVAR(tounicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001497"tounicode() -> unicode\n\
1498\n\
1499Convert the array to a unicode string. The array must be\n\
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001500a unicode type array; otherwise a ValueError is raised. Use\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001501array.tostring().decode() to obtain a unicode string from\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001502an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001503
Martin v. Löwis99866332002-03-01 10:27:01 +00001504
1505
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001506/*********************** Pickling support ************************/
1507
1508enum machine_format_code {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 UNKNOWN_FORMAT = -1,
1510 /* UNKNOWN_FORMAT is used to indicate that the machine format for an
1511 * array type code cannot be interpreted. When this occurs, a list of
1512 * Python objects is used to represent the content of the array
1513 * instead of using the memory content of the array directly. In that
1514 * case, the array_reconstructor mechanism is bypassed completely, and
1515 * the standard array constructor is used instead.
1516 *
1517 * This is will most likely occur when the machine doesn't use IEEE
1518 * floating-point numbers.
1519 */
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 UNSIGNED_INT8 = 0,
1522 SIGNED_INT8 = 1,
1523 UNSIGNED_INT16_LE = 2,
1524 UNSIGNED_INT16_BE = 3,
1525 SIGNED_INT16_LE = 4,
1526 SIGNED_INT16_BE = 5,
1527 UNSIGNED_INT32_LE = 6,
1528 UNSIGNED_INT32_BE = 7,
1529 SIGNED_INT32_LE = 8,
1530 SIGNED_INT32_BE = 9,
1531 UNSIGNED_INT64_LE = 10,
1532 UNSIGNED_INT64_BE = 11,
1533 SIGNED_INT64_LE = 12,
1534 SIGNED_INT64_BE = 13,
1535 IEEE_754_FLOAT_LE = 14,
1536 IEEE_754_FLOAT_BE = 15,
1537 IEEE_754_DOUBLE_LE = 16,
1538 IEEE_754_DOUBLE_BE = 17,
1539 UTF16_LE = 18,
1540 UTF16_BE = 19,
1541 UTF32_LE = 20,
1542 UTF32_BE = 21
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001543};
1544#define MACHINE_FORMAT_CODE_MIN 0
1545#define MACHINE_FORMAT_CODE_MAX 21
1546
1547static const struct mformatdescr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 size_t size;
1549 int is_signed;
1550 int is_big_endian;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001551} mformat_descriptors[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 {1, 0, 0}, /* 0: UNSIGNED_INT8 */
1553 {1, 1, 0}, /* 1: SIGNED_INT8 */
1554 {2, 0, 0}, /* 2: UNSIGNED_INT16_LE */
1555 {2, 0, 1}, /* 3: UNSIGNED_INT16_BE */
1556 {2, 1, 0}, /* 4: SIGNED_INT16_LE */
1557 {2, 1, 1}, /* 5: SIGNED_INT16_BE */
1558 {4, 0, 0}, /* 6: UNSIGNED_INT32_LE */
1559 {4, 0, 1}, /* 7: UNSIGNED_INT32_BE */
1560 {4, 1, 0}, /* 8: SIGNED_INT32_LE */
1561 {4, 1, 1}, /* 9: SIGNED_INT32_BE */
1562 {8, 0, 0}, /* 10: UNSIGNED_INT64_LE */
1563 {8, 0, 1}, /* 11: UNSIGNED_INT64_BE */
1564 {8, 1, 0}, /* 12: SIGNED_INT64_LE */
1565 {8, 1, 1}, /* 13: SIGNED_INT64_BE */
1566 {4, 0, 0}, /* 14: IEEE_754_FLOAT_LE */
1567 {4, 0, 1}, /* 15: IEEE_754_FLOAT_BE */
1568 {8, 0, 0}, /* 16: IEEE_754_DOUBLE_LE */
1569 {8, 0, 1}, /* 17: IEEE_754_DOUBLE_BE */
1570 {4, 0, 0}, /* 18: UTF16_LE */
1571 {4, 0, 1}, /* 19: UTF16_BE */
1572 {8, 0, 0}, /* 20: UTF32_LE */
1573 {8, 0, 1} /* 21: UTF32_BE */
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001574};
1575
1576
1577/*
1578 * Internal: This function is used to find the machine format of a given
1579 * array type code. This returns UNKNOWN_FORMAT when the machine format cannot
1580 * be found.
1581 */
1582static enum machine_format_code
1583typecode_to_mformat_code(int typecode)
1584{
Alexandre Vassalotti7aaa7702009-07-17 03:51:27 +00001585#ifdef WORDS_BIGENDIAN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 const int is_big_endian = 1;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001587#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 const int is_big_endian = 0;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001589#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 size_t intsize;
1591 int is_signed;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 switch (typecode) {
1594 case 'b':
1595 return SIGNED_INT8;
1596 case 'B':
1597 return UNSIGNED_INT8;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 case 'u':
1600 if (sizeof(Py_UNICODE) == 2) {
1601 return UTF16_LE + is_big_endian;
1602 }
1603 if (sizeof(Py_UNICODE) == 4) {
1604 return UTF32_LE + is_big_endian;
1605 }
1606 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 case 'f':
1609 if (sizeof(float) == 4) {
1610 const float y = 16711938.0;
1611 if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0)
1612 return IEEE_754_FLOAT_BE;
1613 if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0)
1614 return IEEE_754_FLOAT_LE;
1615 }
1616 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 case 'd':
1619 if (sizeof(double) == 8) {
1620 const double x = 9006104071832581.0;
1621 if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0)
1622 return IEEE_754_DOUBLE_BE;
1623 if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
1624 return IEEE_754_DOUBLE_LE;
1625 }
1626 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 /* Integers */
1629 case 'h':
1630 intsize = sizeof(short);
1631 is_signed = 1;
1632 break;
1633 case 'H':
1634 intsize = sizeof(short);
1635 is_signed = 0;
1636 break;
1637 case 'i':
1638 intsize = sizeof(int);
1639 is_signed = 1;
1640 break;
1641 case 'I':
1642 intsize = sizeof(int);
1643 is_signed = 0;
1644 break;
1645 case 'l':
1646 intsize = sizeof(long);
1647 is_signed = 1;
1648 break;
1649 case 'L':
1650 intsize = sizeof(long);
1651 is_signed = 0;
1652 break;
1653 default:
1654 return UNKNOWN_FORMAT;
1655 }
1656 switch (intsize) {
1657 case 2:
1658 return UNSIGNED_INT16_LE + is_big_endian + (2 * is_signed);
1659 case 4:
1660 return UNSIGNED_INT32_LE + is_big_endian + (2 * is_signed);
1661 case 8:
1662 return UNSIGNED_INT64_LE + is_big_endian + (2 * is_signed);
1663 default:
1664 return UNKNOWN_FORMAT;
1665 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001666}
1667
1668/* Forward declaration. */
1669static PyObject *array_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
1670
1671/*
1672 * Internal: This function wraps the array constructor--i.e., array_new()--to
1673 * allow the creation of array objects from C code without having to deal
1674 * directly the tuple argument of array_new(). The typecode argument is a
1675 * Unicode character value, like 'i' or 'f' for example, representing an array
1676 * type code. The items argument is a bytes or a list object from which
1677 * contains the initial value of the array.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 *
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001679 * On success, this functions returns the array object created. Otherwise,
1680 * NULL is returned to indicate a failure.
1681 */
1682static PyObject *
1683make_array(PyTypeObject *arraytype, int typecode, PyObject *items)
1684{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 PyObject *new_args;
1686 PyObject *array_obj;
1687 PyObject *typecode_obj;
1688 Py_UNICODE typecode_str[1] = {typecode};
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 assert(arraytype != NULL);
1691 assert(items != NULL);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 typecode_obj = PyUnicode_FromUnicode(typecode_str, 1);
1694 if (typecode_obj == NULL)
1695 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 new_args = PyTuple_New(2);
1698 if (new_args == NULL)
1699 return NULL;
1700 Py_INCREF(items);
1701 PyTuple_SET_ITEM(new_args, 0, typecode_obj);
1702 PyTuple_SET_ITEM(new_args, 1, items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 array_obj = array_new(arraytype, new_args, NULL);
1705 Py_DECREF(new_args);
1706 if (array_obj == NULL)
1707 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 return array_obj;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001710}
1711
1712/*
1713 * This functions is a special constructor used when unpickling an array. It
1714 * provides a portable way to rebuild an array from its memory representation.
1715 */
1716static PyObject *
1717array_reconstructor(PyObject *self, PyObject *args)
1718{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 PyTypeObject *arraytype;
1720 PyObject *items;
1721 PyObject *converted_items;
1722 PyObject *result;
1723 int typecode;
1724 enum machine_format_code mformat_code;
1725 struct arraydescr *descr;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 if (!PyArg_ParseTuple(args, "OCiO:array._array_reconstructor",
1728 &arraytype, &typecode, &mformat_code, &items))
1729 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 if (!PyType_Check(arraytype)) {
1732 PyErr_Format(PyExc_TypeError,
1733 "first argument must a type object, not %.200s",
1734 Py_TYPE(arraytype)->tp_name);
1735 return NULL;
1736 }
1737 if (!PyType_IsSubtype(arraytype, &Arraytype)) {
1738 PyErr_Format(PyExc_TypeError,
1739 "%.200s is not a subtype of %.200s",
1740 arraytype->tp_name, Arraytype.tp_name);
1741 return NULL;
1742 }
1743 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1744 if (descr->typecode == typecode)
1745 break;
1746 }
1747 if (descr->typecode == '\0') {
1748 PyErr_SetString(PyExc_ValueError,
1749 "second argument must be a valid type code");
1750 return NULL;
1751 }
1752 if (mformat_code < MACHINE_FORMAT_CODE_MIN ||
1753 mformat_code > MACHINE_FORMAT_CODE_MAX) {
1754 PyErr_SetString(PyExc_ValueError,
1755 "third argument must be a valid machine format code.");
1756 return NULL;
1757 }
1758 if (!PyBytes_Check(items)) {
1759 PyErr_Format(PyExc_TypeError,
1760 "fourth argument should be bytes, not %.200s",
1761 Py_TYPE(items)->tp_name);
1762 return NULL;
1763 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 /* Fast path: No decoding has to be done. */
1766 if (mformat_code == typecode_to_mformat_code(typecode) ||
1767 mformat_code == UNKNOWN_FORMAT) {
1768 return make_array(arraytype, typecode, items);
1769 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 /* Slow path: Decode the byte string according to the given machine
1772 * format code. This occurs when the computer unpickling the array
1773 * object is architecturally different from the one that pickled the
1774 * array.
1775 */
1776 if (Py_SIZE(items) % mformat_descriptors[mformat_code].size != 0) {
1777 PyErr_SetString(PyExc_ValueError,
1778 "string length not a multiple of item size");
1779 return NULL;
1780 }
1781 switch (mformat_code) {
1782 case IEEE_754_FLOAT_LE:
1783 case IEEE_754_FLOAT_BE: {
1784 int i;
1785 int le = (mformat_code == IEEE_754_FLOAT_LE) ? 1 : 0;
1786 Py_ssize_t itemcount = Py_SIZE(items) / 4;
1787 const unsigned char *memstr =
1788 (unsigned char *)PyBytes_AS_STRING(items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 converted_items = PyList_New(itemcount);
1791 if (converted_items == NULL)
1792 return NULL;
1793 for (i = 0; i < itemcount; i++) {
1794 PyObject *pyfloat = PyFloat_FromDouble(
1795 _PyFloat_Unpack4(&memstr[i * 4], le));
1796 if (pyfloat == NULL) {
1797 Py_DECREF(converted_items);
1798 return NULL;
1799 }
1800 PyList_SET_ITEM(converted_items, i, pyfloat);
1801 }
1802 break;
1803 }
1804 case IEEE_754_DOUBLE_LE:
1805 case IEEE_754_DOUBLE_BE: {
1806 int i;
1807 int le = (mformat_code == IEEE_754_DOUBLE_LE) ? 1 : 0;
1808 Py_ssize_t itemcount = Py_SIZE(items) / 8;
1809 const unsigned char *memstr =
1810 (unsigned char *)PyBytes_AS_STRING(items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 converted_items = PyList_New(itemcount);
1813 if (converted_items == NULL)
1814 return NULL;
1815 for (i = 0; i < itemcount; i++) {
1816 PyObject *pyfloat = PyFloat_FromDouble(
1817 _PyFloat_Unpack8(&memstr[i * 8], le));
1818 if (pyfloat == NULL) {
1819 Py_DECREF(converted_items);
1820 return NULL;
1821 }
1822 PyList_SET_ITEM(converted_items, i, pyfloat);
1823 }
1824 break;
1825 }
1826 case UTF16_LE:
1827 case UTF16_BE: {
1828 int byteorder = (mformat_code == UTF16_LE) ? -1 : 1;
1829 converted_items = PyUnicode_DecodeUTF16(
1830 PyBytes_AS_STRING(items), Py_SIZE(items),
1831 "strict", &byteorder);
1832 if (converted_items == NULL)
1833 return NULL;
1834 break;
1835 }
1836 case UTF32_LE:
1837 case UTF32_BE: {
1838 int byteorder = (mformat_code == UTF32_LE) ? -1 : 1;
1839 converted_items = PyUnicode_DecodeUTF32(
1840 PyBytes_AS_STRING(items), Py_SIZE(items),
1841 "strict", &byteorder);
1842 if (converted_items == NULL)
1843 return NULL;
1844 break;
1845 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 case UNSIGNED_INT8:
1848 case SIGNED_INT8:
1849 case UNSIGNED_INT16_LE:
1850 case UNSIGNED_INT16_BE:
1851 case SIGNED_INT16_LE:
1852 case SIGNED_INT16_BE:
1853 case UNSIGNED_INT32_LE:
1854 case UNSIGNED_INT32_BE:
1855 case SIGNED_INT32_LE:
1856 case SIGNED_INT32_BE:
1857 case UNSIGNED_INT64_LE:
1858 case UNSIGNED_INT64_BE:
1859 case SIGNED_INT64_LE:
1860 case SIGNED_INT64_BE: {
1861 int i;
1862 const struct mformatdescr mf_descr =
1863 mformat_descriptors[mformat_code];
1864 Py_ssize_t itemcount = Py_SIZE(items) / mf_descr.size;
1865 const unsigned char *memstr =
1866 (unsigned char *)PyBytes_AS_STRING(items);
1867 struct arraydescr *descr;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 /* If possible, try to pack array's items using a data type
1870 * that fits better. This may result in an array with narrower
1871 * or wider elements.
1872 *
1873 * For example, if a 32-bit machine pickles a L-code array of
1874 * unsigned longs, then the array will be unpickled by 64-bit
1875 * machine as an I-code array of unsigned ints.
1876 *
1877 * XXX: Is it possible to write a unit test for this?
1878 */
1879 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1880 if (descr->is_integer_type &&
1881 descr->itemsize == mf_descr.size &&
1882 descr->is_signed == mf_descr.is_signed)
1883 typecode = descr->typecode;
1884 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 converted_items = PyList_New(itemcount);
1887 if (converted_items == NULL)
1888 return NULL;
1889 for (i = 0; i < itemcount; i++) {
1890 PyObject *pylong;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 pylong = _PyLong_FromByteArray(
1893 &memstr[i * mf_descr.size],
1894 mf_descr.size,
1895 !mf_descr.is_big_endian,
1896 mf_descr.is_signed);
1897 if (pylong == NULL) {
1898 Py_DECREF(converted_items);
1899 return NULL;
1900 }
1901 PyList_SET_ITEM(converted_items, i, pylong);
1902 }
1903 break;
1904 }
1905 case UNKNOWN_FORMAT:
1906 /* Impossible, but needed to shut up GCC about the unhandled
1907 * enumeration value.
1908 */
1909 default:
1910 PyErr_BadArgument();
1911 return NULL;
1912 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 result = make_array(arraytype, typecode, converted_items);
1915 Py_DECREF(converted_items);
1916 return result;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001917}
1918
1919static PyObject *
1920array_reduce_ex(arrayobject *array, PyObject *value)
1921{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 PyObject *dict;
1923 PyObject *result;
1924 PyObject *array_str;
1925 int typecode = array->ob_descr->typecode;
1926 int mformat_code;
1927 static PyObject *array_reconstructor = NULL;
1928 long protocol;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 if (array_reconstructor == NULL) {
1931 PyObject *array_module = PyImport_ImportModule("array");
1932 if (array_module == NULL)
1933 return NULL;
1934 array_reconstructor = PyObject_GetAttrString(
1935 array_module,
1936 "_array_reconstructor");
1937 Py_DECREF(array_module);
1938 if (array_reconstructor == NULL)
1939 return NULL;
1940 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 if (!PyLong_Check(value)) {
1943 PyErr_SetString(PyExc_TypeError,
1944 "__reduce_ex__ argument should an integer");
1945 return NULL;
1946 }
1947 protocol = PyLong_AsLong(value);
1948 if (protocol == -1 && PyErr_Occurred())
1949 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 dict = PyObject_GetAttrString((PyObject *)array, "__dict__");
1952 if (dict == NULL) {
1953 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1954 return NULL;
1955 PyErr_Clear();
1956 dict = Py_None;
1957 Py_INCREF(dict);
1958 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 mformat_code = typecode_to_mformat_code(typecode);
1961 if (mformat_code == UNKNOWN_FORMAT || protocol < 3) {
1962 /* Convert the array to a list if we got something weird
1963 * (e.g., non-IEEE floats), or we are pickling the array using
1964 * a Python 2.x compatible protocol.
1965 *
1966 * It is necessary to use a list representation for Python 2.x
1967 * compatible pickle protocol, since Python 2's str objects
1968 * are unpickled as unicode by Python 3. Thus it is impossible
1969 * to make arrays unpicklable by Python 3 by using their memory
1970 * representation, unless we resort to ugly hacks such as
1971 * coercing unicode objects to bytes in array_reconstructor.
1972 */
1973 PyObject *list;
1974 list = array_tolist(array, NULL);
1975 if (list == NULL) {
1976 Py_DECREF(dict);
1977 return NULL;
1978 }
1979 result = Py_BuildValue(
1980 "O(CO)O", Py_TYPE(array), typecode, list, dict);
1981 Py_DECREF(list);
1982 Py_DECREF(dict);
1983 return result;
1984 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001985
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001986 array_str = array_tobytes(array, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 if (array_str == NULL) {
1988 Py_DECREF(dict);
1989 return NULL;
1990 }
1991 result = Py_BuildValue(
1992 "O(OCiN)O", array_reconstructor, Py_TYPE(array), typecode,
1993 mformat_code, array_str, dict);
1994 Py_DECREF(dict);
1995 return result;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001996}
1997
1998PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1999
Martin v. Löwis99866332002-03-01 10:27:01 +00002000static PyObject *
2001array_get_typecode(arrayobject *a, void *closure)
2002{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 char tc = a->ob_descr->typecode;
2004 return PyUnicode_FromStringAndSize(&tc, 1);
Martin v. Löwis99866332002-03-01 10:27:01 +00002005}
2006
2007static PyObject *
2008array_get_itemsize(arrayobject *a, void *closure)
2009{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 return PyLong_FromLong((long)a->ob_descr->itemsize);
Martin v. Löwis99866332002-03-01 10:27:01 +00002011}
2012
2013static PyGetSetDef array_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 {"typecode", (getter) array_get_typecode, NULL,
2015 "the typecode character used to create the array"},
2016 {"itemsize", (getter) array_get_itemsize, NULL,
2017 "the size, in bytes, of one array item"},
2018 {NULL}
Martin v. Löwis99866332002-03-01 10:27:01 +00002019};
2020
Martin v. Löwis59683e82008-06-13 07:50:45 +00002021static PyMethodDef array_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 {"append", (PyCFunction)array_append, METH_O,
2023 append_doc},
2024 {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS,
2025 buffer_info_doc},
2026 {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
2027 byteswap_doc},
2028 {"__copy__", (PyCFunction)array_copy, METH_NOARGS,
2029 copy_doc},
2030 {"count", (PyCFunction)array_count, METH_O,
2031 count_doc},
2032 {"__deepcopy__",(PyCFunction)array_copy, METH_O,
2033 copy_doc},
2034 {"extend", (PyCFunction)array_extend, METH_O,
2035 extend_doc},
2036 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
2037 fromfile_doc},
2038 {"fromlist", (PyCFunction)array_fromlist, METH_O,
2039 fromlist_doc},
2040 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
2041 fromstring_doc},
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00002042 {"frombytes", (PyCFunction)array_frombytes, METH_VARARGS,
2043 frombytes_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
2045 fromunicode_doc},
2046 {"index", (PyCFunction)array_index, METH_O,
2047 index_doc},
2048 {"insert", (PyCFunction)array_insert, METH_VARARGS,
2049 insert_doc},
2050 {"pop", (PyCFunction)array_pop, METH_VARARGS,
2051 pop_doc},
2052 {"__reduce_ex__", (PyCFunction)array_reduce_ex, METH_O,
2053 reduce_doc},
2054 {"remove", (PyCFunction)array_remove, METH_O,
2055 remove_doc},
2056 {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
2057 reverse_doc},
2058/* {"sort", (PyCFunction)array_sort, METH_VARARGS,
2059 sort_doc},*/
2060 {"tofile", (PyCFunction)array_tofile, METH_O,
2061 tofile_doc},
2062 {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
2063 tolist_doc},
2064 {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
2065 tostring_doc},
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00002066 {"tobytes", (PyCFunction)array_tobytes, METH_NOARGS,
2067 tobytes_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
2069 tounicode_doc},
2070 {NULL, NULL} /* sentinel */
Guido van Rossum778983b1993-02-19 15:55:02 +00002071};
2072
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002073static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00002074array_repr(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +00002075{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 char typecode;
2077 PyObject *s, *v = NULL;
2078 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00002079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 len = Py_SIZE(a);
2081 typecode = a->ob_descr->typecode;
2082 if (len == 0) {
2083 return PyUnicode_FromFormat("array('%c')", typecode);
2084 }
2085 if ((typecode == 'u'))
2086 v = array_tounicode(a, NULL);
2087 else
2088 v = array_tolist(a, NULL);
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00002089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 s = PyUnicode_FromFormat("array('%c', %R)", typecode, v);
2091 Py_DECREF(v);
2092 return s;
Guido van Rossum778983b1993-02-19 15:55:02 +00002093}
2094
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002095static PyObject*
2096array_subscr(arrayobject* self, PyObject* item)
2097{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 if (PyIndex_Check(item)) {
2099 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2100 if (i==-1 && PyErr_Occurred()) {
2101 return NULL;
2102 }
2103 if (i < 0)
2104 i += Py_SIZE(self);
2105 return array_item(self, i);
2106 }
2107 else if (PySlice_Check(item)) {
2108 Py_ssize_t start, stop, step, slicelength, cur, i;
2109 PyObject* result;
2110 arrayobject* ar;
2111 int itemsize = self->ob_descr->itemsize;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113 if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self),
2114 &start, &stop, &step, &slicelength) < 0) {
2115 return NULL;
2116 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 if (slicelength <= 0) {
2119 return newarrayobject(&Arraytype, 0, self->ob_descr);
2120 }
2121 else if (step == 1) {
2122 PyObject *result = newarrayobject(&Arraytype,
2123 slicelength, self->ob_descr);
2124 if (result == NULL)
2125 return NULL;
2126 memcpy(((arrayobject *)result)->ob_item,
2127 self->ob_item + start * itemsize,
2128 slicelength * itemsize);
2129 return result;
2130 }
2131 else {
2132 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
2133 if (!result) return NULL;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135 ar = (arrayobject*)result;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 for (cur = start, i = 0; i < slicelength;
2138 cur += step, i++) {
2139 memcpy(ar->ob_item + i*itemsize,
2140 self->ob_item + cur*itemsize,
2141 itemsize);
2142 }
2143
2144 return result;
2145 }
2146 }
2147 else {
2148 PyErr_SetString(PyExc_TypeError,
2149 "array indices must be integers");
2150 return NULL;
2151 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002152}
2153
2154static int
2155array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
2156{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 Py_ssize_t start, stop, step, slicelength, needed;
2158 arrayobject* other;
2159 int itemsize;
Thomas Woutersed03b412007-08-28 21:37:11 +00002160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 if (PyIndex_Check(item)) {
2162 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Alexandre Vassalotti47137252009-07-05 19:57:00 +00002163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 if (i == -1 && PyErr_Occurred())
2165 return -1;
2166 if (i < 0)
2167 i += Py_SIZE(self);
2168 if (i < 0 || i >= Py_SIZE(self)) {
2169 PyErr_SetString(PyExc_IndexError,
2170 "array assignment index out of range");
2171 return -1;
2172 }
2173 if (value == NULL) {
2174 /* Fall through to slice assignment */
2175 start = i;
2176 stop = i + 1;
2177 step = 1;
2178 slicelength = 1;
2179 }
2180 else
2181 return (*self->ob_descr->setitem)(self, i, value);
2182 }
2183 else if (PySlice_Check(item)) {
2184 if (PySlice_GetIndicesEx((PySliceObject *)item,
2185 Py_SIZE(self), &start, &stop,
2186 &step, &slicelength) < 0) {
2187 return -1;
2188 }
2189 }
2190 else {
2191 PyErr_SetString(PyExc_TypeError,
2192 "array indices must be integer");
2193 return -1;
2194 }
2195 if (value == NULL) {
2196 other = NULL;
2197 needed = 0;
2198 }
2199 else if (array_Check(value)) {
2200 other = (arrayobject *)value;
2201 needed = Py_SIZE(other);
2202 if (self == other) {
2203 /* Special case "self[i:j] = self" -- copy self first */
2204 int ret;
2205 value = array_slice(other, 0, needed);
2206 if (value == NULL)
2207 return -1;
2208 ret = array_ass_subscr(self, item, value);
2209 Py_DECREF(value);
2210 return ret;
2211 }
2212 if (other->ob_descr != self->ob_descr) {
2213 PyErr_BadArgument();
2214 return -1;
2215 }
2216 }
2217 else {
2218 PyErr_Format(PyExc_TypeError,
2219 "can only assign array (not \"%.200s\") to array slice",
2220 Py_TYPE(value)->tp_name);
2221 return -1;
2222 }
2223 itemsize = self->ob_descr->itemsize;
2224 /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
2225 if ((step > 0 && stop < start) ||
2226 (step < 0 && stop > start))
2227 stop = start;
Alexandre Vassalotti47137252009-07-05 19:57:00 +00002228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 /* Issue #4509: If the array has exported buffers and the slice
2230 assignment would change the size of the array, fail early to make
2231 sure we don't modify it. */
2232 if ((needed == 0 || slicelength != needed) && self->ob_exports > 0) {
2233 PyErr_SetString(PyExc_BufferError,
2234 "cannot resize an array that is exporting buffers");
2235 return -1;
2236 }
Mark Dickinsonbc099642010-01-29 17:27:24 +00002237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 if (step == 1) {
2239 if (slicelength > needed) {
2240 memmove(self->ob_item + (start + needed) * itemsize,
2241 self->ob_item + stop * itemsize,
2242 (Py_SIZE(self) - stop) * itemsize);
2243 if (array_resize(self, Py_SIZE(self) +
2244 needed - slicelength) < 0)
2245 return -1;
2246 }
2247 else if (slicelength < needed) {
2248 if (array_resize(self, Py_SIZE(self) +
2249 needed - slicelength) < 0)
2250 return -1;
2251 memmove(self->ob_item + (start + needed) * itemsize,
2252 self->ob_item + stop * itemsize,
2253 (Py_SIZE(self) - start - needed) * itemsize);
2254 }
2255 if (needed > 0)
2256 memcpy(self->ob_item + start * itemsize,
2257 other->ob_item, needed * itemsize);
2258 return 0;
2259 }
2260 else if (needed == 0) {
2261 /* Delete slice */
2262 size_t cur;
2263 Py_ssize_t i;
Thomas Woutersed03b412007-08-28 21:37:11 +00002264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 if (step < 0) {
2266 stop = start + 1;
2267 start = stop + step * (slicelength - 1) - 1;
2268 step = -step;
2269 }
2270 for (cur = start, i = 0; i < slicelength;
2271 cur += step, i++) {
2272 Py_ssize_t lim = step - 1;
Thomas Woutersed03b412007-08-28 21:37:11 +00002273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 if (cur + step >= (size_t)Py_SIZE(self))
2275 lim = Py_SIZE(self) - cur - 1;
2276 memmove(self->ob_item + (cur - i) * itemsize,
2277 self->ob_item + (cur + 1) * itemsize,
2278 lim * itemsize);
2279 }
2280 cur = start + slicelength * step;
2281 if (cur < (size_t)Py_SIZE(self)) {
2282 memmove(self->ob_item + (cur-slicelength) * itemsize,
2283 self->ob_item + cur * itemsize,
2284 (Py_SIZE(self) - cur) * itemsize);
2285 }
2286 if (array_resize(self, Py_SIZE(self) - slicelength) < 0)
2287 return -1;
2288 return 0;
2289 }
2290 else {
2291 Py_ssize_t cur, i;
2292
2293 if (needed != slicelength) {
2294 PyErr_Format(PyExc_ValueError,
2295 "attempt to assign array of size %zd "
2296 "to extended slice of size %zd",
2297 needed, slicelength);
2298 return -1;
2299 }
2300 for (cur = start, i = 0; i < slicelength;
2301 cur += step, i++) {
2302 memcpy(self->ob_item + cur * itemsize,
2303 other->ob_item + i * itemsize,
2304 itemsize);
2305 }
2306 return 0;
2307 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002308}
2309
2310static PyMappingMethods array_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 (lenfunc)array_length,
2312 (binaryfunc)array_subscr,
2313 (objobjargproc)array_ass_subscr
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002314};
2315
Guido van Rossumd8faa362007-04-27 19:54:29 +00002316static const void *emptybuf = "";
2317
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002318
2319static int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00002320array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002321{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 if (view==NULL) goto finish;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 view->buf = (void *)self->ob_item;
2325 view->obj = (PyObject*)self;
2326 Py_INCREF(self);
2327 if (view->buf == NULL)
2328 view->buf = (void *)emptybuf;
2329 view->len = (Py_SIZE(self)) * self->ob_descr->itemsize;
2330 view->readonly = 0;
2331 view->ndim = 1;
2332 view->itemsize = self->ob_descr->itemsize;
2333 view->suboffsets = NULL;
2334 view->shape = NULL;
2335 if ((flags & PyBUF_ND)==PyBUF_ND) {
2336 view->shape = &((Py_SIZE(self)));
2337 }
2338 view->strides = NULL;
2339 if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES)
2340 view->strides = &(view->itemsize);
2341 view->format = NULL;
2342 view->internal = NULL;
2343 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
2344 view->format = self->ob_descr->formats;
Travis E. Oliphantddacf962007-10-13 21:03:27 +00002345#ifdef Py_UNICODE_WIDE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 if (self->ob_descr->typecode == 'u') {
2347 view->format = "w";
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002348 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349#endif
2350 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002351
2352 finish:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 self->ob_exports++;
2354 return 0;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002355}
2356
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002357static void
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00002358array_buffer_relbuf(arrayobject *self, Py_buffer *view)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 self->ob_exports--;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002361}
2362
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002363static PySequenceMethods array_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 (lenfunc)array_length, /*sq_length*/
2365 (binaryfunc)array_concat, /*sq_concat*/
2366 (ssizeargfunc)array_repeat, /*sq_repeat*/
2367 (ssizeargfunc)array_item, /*sq_item*/
2368 0, /*sq_slice*/
2369 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
2370 0, /*sq_ass_slice*/
2371 (objobjproc)array_contains, /*sq_contains*/
2372 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
2373 (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/
Guido van Rossum778983b1993-02-19 15:55:02 +00002374};
2375
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002376static PyBufferProcs array_as_buffer = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 (getbufferproc)array_buffer_getbuf,
2378 (releasebufferproc)array_buffer_relbuf
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002379};
2380
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002381static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +00002382array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum778983b1993-02-19 15:55:02 +00002383{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 int c;
2385 PyObject *initial = NULL, *it = NULL;
2386 struct arraydescr *descr;
Martin v. Löwis99866332002-03-01 10:27:01 +00002387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds))
2389 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00002390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
2392 return NULL;
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00002393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 if (!(initial == NULL || PyList_Check(initial)
2395 || PyByteArray_Check(initial)
2396 || PyBytes_Check(initial)
2397 || PyTuple_Check(initial)
2398 || ((c=='u') && PyUnicode_Check(initial)))) {
2399 it = PyObject_GetIter(initial);
2400 if (it == NULL)
2401 return NULL;
2402 /* We set initial to NULL so that the subsequent code
2403 will create an empty array of the appropriate type
2404 and afterwards we can use array_iter_extend to populate
2405 the array.
2406 */
2407 initial = NULL;
2408 }
2409 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2410 if (descr->typecode == c) {
2411 PyObject *a;
2412 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00002413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 if (initial == NULL || !(PyList_Check(initial)
2415 || PyTuple_Check(initial)))
2416 len = 0;
2417 else
2418 len = PySequence_Size(initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00002419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 a = newarrayobject(type, len, descr);
2421 if (a == NULL)
2422 return NULL;
2423
2424 if (len > 0) {
2425 Py_ssize_t i;
2426 for (i = 0; i < len; i++) {
2427 PyObject *v =
2428 PySequence_GetItem(initial, i);
2429 if (v == NULL) {
2430 Py_DECREF(a);
2431 return NULL;
2432 }
2433 if (setarrayitem(a, i, v) != 0) {
2434 Py_DECREF(v);
2435 Py_DECREF(a);
2436 return NULL;
2437 }
2438 Py_DECREF(v);
2439 }
2440 }
2441 else if (initial != NULL && (PyByteArray_Check(initial) ||
2442 PyBytes_Check(initial))) {
2443 PyObject *t_initial, *v;
2444 t_initial = PyTuple_Pack(1, initial);
2445 if (t_initial == NULL) {
2446 Py_DECREF(a);
2447 return NULL;
2448 }
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00002449 v = array_frombytes((arrayobject *)a,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002450 t_initial);
2451 Py_DECREF(t_initial);
2452 if (v == NULL) {
2453 Py_DECREF(a);
2454 return NULL;
2455 }
2456 Py_DECREF(v);
2457 }
2458 else if (initial != NULL && PyUnicode_Check(initial)) {
2459 Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial);
2460 if (n > 0) {
2461 arrayobject *self = (arrayobject *)a;
2462 char *item = self->ob_item;
2463 item = (char *)PyMem_Realloc(item, n);
2464 if (item == NULL) {
2465 PyErr_NoMemory();
2466 Py_DECREF(a);
2467 return NULL;
2468 }
2469 self->ob_item = item;
2470 Py_SIZE(self) = n / sizeof(Py_UNICODE);
2471 memcpy(item, PyUnicode_AS_DATA(initial), n);
2472 self->allocated = Py_SIZE(self);
2473 }
2474 }
2475 if (it != NULL) {
2476 if (array_iter_extend((arrayobject *)a, it) == -1) {
2477 Py_DECREF(it);
2478 Py_DECREF(a);
2479 return NULL;
2480 }
2481 Py_DECREF(it);
2482 }
2483 return a;
2484 }
2485 }
2486 PyErr_SetString(PyExc_ValueError,
2487 "bad typecode (must be b, B, u, h, H, i, I, l, L, f or d)");
2488 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00002489}
2490
Guido van Rossum778983b1993-02-19 15:55:02 +00002491
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002492PyDoc_STRVAR(module_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00002493"This module defines an object type which can efficiently represent\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002494an array of basic values: characters, integers, floating point\n\
2495numbers. Arrays are sequence types and behave very much like lists,\n\
2496except that the type of objects stored in them is constrained. The\n\
2497type is specified at object creation time by using a type code, which\n\
2498is a single character. The following type codes are defined:\n\
2499\n\
2500 Type code C Type Minimum size in bytes \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002501 'b' signed integer 1 \n\
2502 'B' unsigned integer 1 \n\
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00002503 'u' Unicode character 2 (see note) \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002504 'h' signed integer 2 \n\
2505 'H' unsigned integer 2 \n\
2506 'i' signed integer 2 \n\
2507 'I' unsigned integer 2 \n\
2508 'l' signed integer 4 \n\
2509 'L' unsigned integer 4 \n\
2510 'f' floating point 4 \n\
2511 'd' floating point 8 \n\
2512\n\
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00002513NOTE: The 'u' typecode corresponds to Python's unicode character. On \n\
2514narrow builds this is 2-bytes on wide builds this is 4-bytes.\n\
2515\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002516The constructor is:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002517\n\
2518array(typecode [, initializer]) -- create a new array\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002519");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002520
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002521PyDoc_STRVAR(arraytype_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00002522"array(typecode [, initializer]) -> array\n\
2523\n\
2524Return a new array whose items are restricted by typecode, and\n\
Raymond Hettinger6ab78cd2004-08-29 07:50:43 +00002525initialized from the optional initializer value, which must be a list,\n\
2526string. or iterable over elements of the appropriate type.\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002527\n\
2528Arrays represent basic values and behave very much like lists, except\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002529the type of objects stored in them is constrained.\n\
2530\n\
2531Methods:\n\
2532\n\
2533append() -- append a new item to the end of the array\n\
2534buffer_info() -- return information giving the current memory info\n\
2535byteswap() -- byteswap all the items of the array\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002536count() -- return number of occurrences of an object\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00002537extend() -- extend array by appending multiple elements from an iterable\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002538fromfile() -- read items from a file object\n\
2539fromlist() -- append items from the list\n\
2540fromstring() -- append items from the string\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002541index() -- return index of first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002542insert() -- insert a new item into the array at a provided position\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00002543pop() -- remove and return item (default last)\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002544remove() -- remove first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002545reverse() -- reverse the order of the items in the array\n\
2546tofile() -- write all items to a file object\n\
2547tolist() -- return the array converted to an ordinary list\n\
2548tostring() -- return the array converted to a string\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002549\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002550Attributes:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002551\n\
2552typecode -- the typecode character used to create the array\n\
2553itemsize -- the length in bytes of one array item\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002554");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002555
Raymond Hettinger625812f2003-01-07 01:58:52 +00002556static PyObject *array_iter(arrayobject *ao);
2557
Tim Peters0c322792002-07-17 16:49:03 +00002558static PyTypeObject Arraytype = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002559 PyVarObject_HEAD_INIT(NULL, 0)
2560 "array.array",
2561 sizeof(arrayobject),
2562 0,
2563 (destructor)array_dealloc, /* tp_dealloc */
2564 0, /* tp_print */
2565 0, /* tp_getattr */
2566 0, /* tp_setattr */
2567 0, /* tp_reserved */
2568 (reprfunc)array_repr, /* tp_repr */
2569 0, /* tp_as_number*/
2570 &array_as_sequence, /* tp_as_sequence*/
2571 &array_as_mapping, /* tp_as_mapping*/
2572 0, /* tp_hash */
2573 0, /* tp_call */
2574 0, /* tp_str */
2575 PyObject_GenericGetAttr, /* tp_getattro */
2576 0, /* tp_setattro */
2577 &array_as_buffer, /* tp_as_buffer*/
2578 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2579 arraytype_doc, /* tp_doc */
2580 0, /* tp_traverse */
2581 0, /* tp_clear */
2582 array_richcompare, /* tp_richcompare */
2583 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
2584 (getiterfunc)array_iter, /* tp_iter */
2585 0, /* tp_iternext */
2586 array_methods, /* tp_methods */
2587 0, /* tp_members */
2588 array_getsets, /* tp_getset */
2589 0, /* tp_base */
2590 0, /* tp_dict */
2591 0, /* tp_descr_get */
2592 0, /* tp_descr_set */
2593 0, /* tp_dictoffset */
2594 0, /* tp_init */
2595 PyType_GenericAlloc, /* tp_alloc */
2596 array_new, /* tp_new */
2597 PyObject_Del, /* tp_free */
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002598};
2599
Raymond Hettinger625812f2003-01-07 01:58:52 +00002600
2601/*********************** Array Iterator **************************/
2602
2603typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002604 PyObject_HEAD
2605 Py_ssize_t index;
2606 arrayobject *ao;
2607 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002608} arrayiterobject;
2609
2610static PyTypeObject PyArrayIter_Type;
2611
2612#define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
2613
2614static PyObject *
2615array_iter(arrayobject *ao)
2616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002617 arrayiterobject *it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002619 if (!array_Check(ao)) {
2620 PyErr_BadInternalCall();
2621 return NULL;
2622 }
Raymond Hettinger625812f2003-01-07 01:58:52 +00002623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002624 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
2625 if (it == NULL)
2626 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002628 Py_INCREF(ao);
2629 it->ao = ao;
2630 it->index = 0;
2631 it->getitem = ao->ob_descr->getitem;
2632 PyObject_GC_Track(it);
2633 return (PyObject *)it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002634}
2635
2636static PyObject *
Raymond Hettinger625812f2003-01-07 01:58:52 +00002637arrayiter_next(arrayiterobject *it)
2638{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002639 assert(PyArrayIter_Check(it));
2640 if (it->index < Py_SIZE(it->ao))
2641 return (*it->getitem)(it->ao, it->index++);
2642 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002643}
2644
2645static void
2646arrayiter_dealloc(arrayiterobject *it)
2647{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002648 PyObject_GC_UnTrack(it);
2649 Py_XDECREF(it->ao);
2650 PyObject_GC_Del(it);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002651}
2652
2653static int
2654arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2655{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656 Py_VISIT(it->ao);
2657 return 0;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002658}
2659
2660static PyTypeObject PyArrayIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002661 PyVarObject_HEAD_INIT(NULL, 0)
2662 "arrayiterator", /* tp_name */
2663 sizeof(arrayiterobject), /* tp_basicsize */
2664 0, /* tp_itemsize */
2665 /* methods */
2666 (destructor)arrayiter_dealloc, /* tp_dealloc */
2667 0, /* tp_print */
2668 0, /* tp_getattr */
2669 0, /* tp_setattr */
2670 0, /* tp_reserved */
2671 0, /* tp_repr */
2672 0, /* tp_as_number */
2673 0, /* tp_as_sequence */
2674 0, /* tp_as_mapping */
2675 0, /* tp_hash */
2676 0, /* tp_call */
2677 0, /* tp_str */
2678 PyObject_GenericGetAttr, /* tp_getattro */
2679 0, /* tp_setattro */
2680 0, /* tp_as_buffer */
2681 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2682 0, /* tp_doc */
2683 (traverseproc)arrayiter_traverse, /* tp_traverse */
2684 0, /* tp_clear */
2685 0, /* tp_richcompare */
2686 0, /* tp_weaklistoffset */
2687 PyObject_SelfIter, /* tp_iter */
2688 (iternextfunc)arrayiter_next, /* tp_iternext */
2689 0, /* tp_methods */
Raymond Hettinger625812f2003-01-07 01:58:52 +00002690};
2691
2692
2693/*********************** Install Module **************************/
2694
Martin v. Löwis99866332002-03-01 10:27:01 +00002695/* No functions in array module. */
2696static PyMethodDef a_methods[] = {
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002697 {"_array_reconstructor", array_reconstructor, METH_VARARGS,
2698 PyDoc_STR("Internal. Used for pickling support.")},
Martin v. Löwis99866332002-03-01 10:27:01 +00002699 {NULL, NULL, 0, NULL} /* Sentinel */
2700};
2701
Martin v. Löwis1a214512008-06-11 05:26:20 +00002702static struct PyModuleDef arraymodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002703 PyModuleDef_HEAD_INIT,
2704 "array",
2705 module_doc,
2706 -1,
2707 a_methods,
2708 NULL,
2709 NULL,
2710 NULL,
2711 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002712};
2713
Martin v. Löwis99866332002-03-01 10:27:01 +00002714
Mark Hammondfe51c6d2002-08-02 02:27:13 +00002715PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002716PyInit_array(void)
Guido van Rossum778983b1993-02-19 15:55:02 +00002717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 PyObject *m;
2719 PyObject *typecodes;
2720 Py_ssize_t size = 0;
2721 register Py_UNICODE *p;
2722 struct arraydescr *descr;
Fred Drake0d40ba42000-02-04 20:33:49 +00002723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002724 if (PyType_Ready(&Arraytype) < 0)
2725 return NULL;
2726 Py_TYPE(&PyArrayIter_Type) = &PyType_Type;
2727 m = PyModule_Create(&arraymodule);
2728 if (m == NULL)
2729 return NULL;
Fred Drakef4e34842002-04-01 03:45:06 +00002730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002731 Py_INCREF((PyObject *)&Arraytype);
2732 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
2733 Py_INCREF((PyObject *)&Arraytype);
2734 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00002735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002736 for (descr=descriptors; descr->typecode != '\0'; descr++) {
2737 size++;
2738 }
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00002739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 typecodes = PyUnicode_FromStringAndSize(NULL, size);
2741 p = PyUnicode_AS_UNICODE(typecodes);
2742 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2743 *p++ = (char)descr->typecode;
2744 }
2745
2746 PyModule_AddObject(m, "typecodes", (PyObject *)typecodes);
2747
2748 if (PyErr_Occurred()) {
2749 Py_DECREF(m);
2750 m = NULL;
2751 }
2752 return m;
Guido van Rossum778983b1993-02-19 15:55:02 +00002753}