blob: c03160eabac4b0c7f5c168bd32e456e02978330d [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
Roger E. Masse2919eaa1996-12-09 20:10:36 +00006#include "Python.h"
Roger E. Masse5817f8f1996-12-09 22:24:19 +00007
Guido van Rossum0c709541994-08-19 12:01:32 +00008#ifdef STDC_HEADERS
9#include <stddef.h>
Guido van Rossum7f1de831999-08-27 20:33:52 +000010#else /* !STDC_HEADERS */
11#ifndef DONT_HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000012#include <sys/types.h> /* For size_t */
Guido van Rossum7f1de831999-08-27 20:33:52 +000013#endif /* DONT_HAVE_SYS_TYPES_H */
14#endif /* !STDC_HEADERS */
Guido van Rossum778983b1993-02-19 15:55:02 +000015
Neal Norwitz937ca982003-02-24 02:08:42 +000016/* Shamelessy stolen from listobject.c */
17static int
18roundupsize(int n)
19{
20 unsigned int nbits = 0;
21 unsigned int n2 = (unsigned int)n >> 5;
22
23 /* Round up:
24 * If n < 256, to a multiple of 8.
25 * If n < 2048, to a multiple of 64.
26 * If n < 16384, to a multiple of 512.
27 * If n < 131072, to a multiple of 4096.
28 * If n < 1048576, to a multiple of 32768.
29 * If n < 8388608, to a multiple of 262144.
30 * If n < 67108864, to a multiple of 2097152.
31 * If n < 536870912, to a multiple of 16777216.
32 * ...
33 * If n < 2**(5+3*i), to a multiple of 2**(3*i).
34 *
35 * This over-allocates proportional to the list size, making room
36 * for additional growth. The over-allocation is mild, but is
37 * enough to give linear-time amortized behavior over a long
38 * sequence of appends() in the presence of a poorly-performing
39 * system realloc() (which is a reality, e.g., across all flavors
40 * of Windows, with Win9x behavior being particularly bad -- and
41 * we've still got address space fragmentation problems on Win9x
42 * even with this scheme, although it requires much longer lists to
43 * provoke them than it used to).
44 */
45 do {
46 n2 >>= 3;
47 nbits += 3;
48 } while (n2);
49 return ((n >> nbits) + 1) << nbits;
50 }
51
52#define NRESIZE(var, type, nitems) \
53do { \
54 size_t _new_size = roundupsize(nitems); \
55 if (_new_size <= ((~(size_t)0) / sizeof(type))) \
56 PyMem_RESIZE(var, type, _new_size); \
57 else \
58 var = NULL; \
59} while (0)
60/* END SHAMELESSLY STOLEN CODE */
61
Guido van Rossum778983b1993-02-19 15:55:02 +000062struct arrayobject; /* Forward */
63
Tim Petersbb307342000-09-10 05:22:54 +000064/* All possible arraydescr values are defined in the vector "descriptors"
65 * below. That's defined later because the appropriate get and set
66 * functions aren't visible yet.
67 */
Guido van Rossum778983b1993-02-19 15:55:02 +000068struct arraydescr {
69 int typecode;
70 int itemsize;
Tim Petersdbd9ba62000-07-09 03:09:57 +000071 PyObject * (*getitem)(struct arrayobject *, int);
72 int (*setitem)(struct arrayobject *, int, PyObject *);
Guido van Rossum778983b1993-02-19 15:55:02 +000073};
74
75typedef struct arrayobject {
Martin v. Löwis99866332002-03-01 10:27:01 +000076 PyObject_HEAD
77 int ob_size;
Guido van Rossum778983b1993-02-19 15:55:02 +000078 char *ob_item;
79 struct arraydescr *ob_descr;
80} arrayobject;
81
Jeremy Hylton938ace62002-07-17 16:30:39 +000082static PyTypeObject Arraytype;
Guido van Rossum778983b1993-02-19 15:55:02 +000083
Martin v. Löwis99866332002-03-01 10:27:01 +000084#define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
85#define array_CheckExact(op) ((op)->ob_type == &Arraytype)
Guido van Rossum778983b1993-02-19 15:55:02 +000086
Tim Petersbb307342000-09-10 05:22:54 +000087/****************************************************************************
88Get and Set functions for each type.
89A Get function takes an arrayobject* and an integer index, returning the
90array value at that index wrapped in an appropriate PyObject*.
91A Set function takes an arrayobject, integer index, and PyObject*; sets
92the array value at that index to the raw C data extracted from the PyObject*,
93and returns 0 if successful, else nonzero on failure (PyObject* not of an
94appropriate type or value).
95Note that the basic Get and Set functions do NOT check that the index is
96in bounds; that's the responsibility of the caller.
97****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +000098
Roger E. Masse2919eaa1996-12-09 20:10:36 +000099static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000100c_getitem(arrayobject *ap, int i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000101{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000102 return PyString_FromStringAndSize(&((char *)ap->ob_item)[i], 1);
Guido van Rossum778983b1993-02-19 15:55:02 +0000103}
104
105static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000106c_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000107{
108 char x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000109 if (!PyArg_Parse(v, "c;array item must be char", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000110 return -1;
111 if (i >= 0)
Martin v. Löwis99866332002-03-01 10:27:01 +0000112 ((char *)ap->ob_item)[i] = x;
Guido van Rossum778983b1993-02-19 15:55:02 +0000113 return 0;
114}
115
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000116static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000117b_getitem(arrayobject *ap, int i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000118{
119 long x = ((char *)ap->ob_item)[i];
120 if (x >= 128)
121 x -= 256;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000122 return PyInt_FromLong(x);
Guido van Rossum778983b1993-02-19 15:55:02 +0000123}
124
125static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000126b_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000127{
Fred Drake541dc3b2000-06-28 17:49:30 +0000128 short x;
129 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
130 must use the next size up that is signed ('h') and manually do
131 the overflow checking */
132 if (!PyArg_Parse(v, "h;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000133 return -1;
Guido van Rossum9f754e02000-07-01 00:38:19 +0000134 else if (x < -128) {
Fred Drake541dc3b2000-06-28 17:49:30 +0000135 PyErr_SetString(PyExc_OverflowError,
136 "signed char is less than minimum");
137 return -1;
138 }
Guido van Rossum9f754e02000-07-01 00:38:19 +0000139 else if (x > 127) {
Fred Drake541dc3b2000-06-28 17:49:30 +0000140 PyErr_SetString(PyExc_OverflowError,
141 "signed char is greater than maximum");
142 return -1;
143 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000144 if (i >= 0)
Fred Drake541dc3b2000-06-28 17:49:30 +0000145 ((char *)ap->ob_item)[i] = (char)x;
Guido van Rossum778983b1993-02-19 15:55:02 +0000146 return 0;
147}
148
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000149static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000150BB_getitem(arrayobject *ap, int i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000151{
152 long x = ((unsigned char *)ap->ob_item)[i];
153 return PyInt_FromLong(x);
154}
155
Fred Drake541dc3b2000-06-28 17:49:30 +0000156static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000157BB_setitem(arrayobject *ap, int i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000158{
159 unsigned char x;
160 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
161 if (!PyArg_Parse(v, "b;array item must be integer", &x))
162 return -1;
163 if (i >= 0)
Martin v. Löwis99866332002-03-01 10:27:01 +0000164 ((char *)ap->ob_item)[i] = x;
Fred Drake541dc3b2000-06-28 17:49:30 +0000165 return 0;
166}
Guido van Rossum549ab711997-01-03 19:09:47 +0000167
Martin v. Löwis99866332002-03-01 10:27:01 +0000168#ifdef Py_USING_UNICODE
169static PyObject *
170u_getitem(arrayobject *ap, int i)
171{
172 return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1);
173}
174
175static int
176u_setitem(arrayobject *ap, int i, PyObject *v)
177{
178 Py_UNICODE *p;
179 int len;
180
181 if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
182 return -1;
183 if (len != 1) {
184 PyErr_SetString(PyExc_TypeError, "array item must be unicode character");
185 return -1;
186 }
187 if (i >= 0)
188 ((Py_UNICODE *)ap->ob_item)[i] = p[0];
189 return 0;
190}
191#endif
192
Guido van Rossum549ab711997-01-03 19:09:47 +0000193static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000194h_getitem(arrayobject *ap, int i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000195{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000196 return PyInt_FromLong((long) ((short *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000197}
198
199static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000200h_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000201{
202 short x;
Fred Drake541dc3b2000-06-28 17:49:30 +0000203 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000204 if (!PyArg_Parse(v, "h;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000205 return -1;
206 if (i >= 0)
207 ((short *)ap->ob_item)[i] = x;
208 return 0;
209}
210
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000211static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000212HH_getitem(arrayobject *ap, int i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000213{
214 return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
215}
216
Fred Drake541dc3b2000-06-28 17:49:30 +0000217static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000218HH_setitem(arrayobject *ap, int i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000219{
220 int x;
221 /* PyArg_Parse's 'h' formatter is for a signed short, therefore
222 must use the next size up and manually do the overflow checking */
223 if (!PyArg_Parse(v, "i;array item must be integer", &x))
224 return -1;
225 else if (x < 0) {
226 PyErr_SetString(PyExc_OverflowError,
227 "unsigned short is less than minimum");
228 return -1;
229 }
230 else if (x > USHRT_MAX) {
231 PyErr_SetString(PyExc_OverflowError,
232 "unsigned short is greater than maximum");
233 return -1;
234 }
235 if (i >= 0)
236 ((short *)ap->ob_item)[i] = (short)x;
237 return 0;
238}
Guido van Rossum549ab711997-01-03 19:09:47 +0000239
240static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000241i_getitem(arrayobject *ap, int i)
Guido van Rossume77a7571993-11-03 15:01:26 +0000242{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000243 return PyInt_FromLong((long) ((int *)ap->ob_item)[i]);
Guido van Rossume77a7571993-11-03 15:01:26 +0000244}
245
246static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000247i_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossume77a7571993-11-03 15:01:26 +0000248{
249 int x;
Fred Drake541dc3b2000-06-28 17:49:30 +0000250 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000251 if (!PyArg_Parse(v, "i;array item must be integer", &x))
Guido van Rossume77a7571993-11-03 15:01:26 +0000252 return -1;
253 if (i >= 0)
254 ((int *)ap->ob_item)[i] = x;
255 return 0;
256}
257
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000258static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000259II_getitem(arrayobject *ap, int i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000260{
261 return PyLong_FromUnsignedLong(
262 (unsigned long) ((unsigned int *)ap->ob_item)[i]);
263}
264
265static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000266II_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000267{
268 unsigned long x;
269 if (PyLong_Check(v)) {
270 x = PyLong_AsUnsignedLong(v);
271 if (x == (unsigned long) -1 && PyErr_Occurred())
272 return -1;
273 }
274 else {
Fred Drake541dc3b2000-06-28 17:49:30 +0000275 long y;
276 if (!PyArg_Parse(v, "l;array item must be integer", &y))
Guido van Rossum549ab711997-01-03 19:09:47 +0000277 return -1;
Fred Drake541dc3b2000-06-28 17:49:30 +0000278 if (y < 0) {
279 PyErr_SetString(PyExc_OverflowError,
280 "unsigned int is less than minimum");
281 return -1;
282 }
283 x = (unsigned long)y;
Tim Petersbb307342000-09-10 05:22:54 +0000284
Guido van Rossum549ab711997-01-03 19:09:47 +0000285 }
Fred Drake541dc3b2000-06-28 17:49:30 +0000286 if (x > UINT_MAX) {
287 PyErr_SetString(PyExc_OverflowError,
288 "unsigned int is greater than maximum");
289 return -1;
290 }
291
Guido van Rossum549ab711997-01-03 19:09:47 +0000292 if (i >= 0)
Fred Drake541dc3b2000-06-28 17:49:30 +0000293 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
Guido van Rossum549ab711997-01-03 19:09:47 +0000294 return 0;
295}
296
297static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000298l_getitem(arrayobject *ap, int i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000299{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000300 return PyInt_FromLong(((long *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000301}
302
303static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000304l_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000305{
306 long x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000307 if (!PyArg_Parse(v, "l;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000308 return -1;
309 if (i >= 0)
310 ((long *)ap->ob_item)[i] = x;
311 return 0;
312}
313
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000314static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000315LL_getitem(arrayobject *ap, int i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000316{
317 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
318}
319
320static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000321LL_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000322{
323 unsigned long x;
324 if (PyLong_Check(v)) {
325 x = PyLong_AsUnsignedLong(v);
326 if (x == (unsigned long) -1 && PyErr_Occurred())
327 return -1;
328 }
329 else {
Fred Drake541dc3b2000-06-28 17:49:30 +0000330 long y;
331 if (!PyArg_Parse(v, "l;array item must be integer", &y))
Guido van Rossum549ab711997-01-03 19:09:47 +0000332 return -1;
Fred Drake541dc3b2000-06-28 17:49:30 +0000333 if (y < 0) {
334 PyErr_SetString(PyExc_OverflowError,
335 "unsigned long is less than minimum");
336 return -1;
337 }
338 x = (unsigned long)y;
Tim Petersbb307342000-09-10 05:22:54 +0000339
Guido van Rossum549ab711997-01-03 19:09:47 +0000340 }
Fred Drake541dc3b2000-06-28 17:49:30 +0000341 if (x > ULONG_MAX) {
342 PyErr_SetString(PyExc_OverflowError,
343 "unsigned long is greater than maximum");
344 return -1;
345 }
Tim Petersbb307342000-09-10 05:22:54 +0000346
Guido van Rossum549ab711997-01-03 19:09:47 +0000347 if (i >= 0)
348 ((unsigned long *)ap->ob_item)[i] = x;
349 return 0;
350}
351
352static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000353f_getitem(arrayobject *ap, int i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000354{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000355 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000356}
357
358static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000359f_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000360{
361 float x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000362 if (!PyArg_Parse(v, "f;array item must be float", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000363 return -1;
364 if (i >= 0)
365 ((float *)ap->ob_item)[i] = x;
366 return 0;
367}
368
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000369static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000370d_getitem(arrayobject *ap, int i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000371{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000372 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000373}
374
375static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000376d_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000377{
378 double x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000379 if (!PyArg_Parse(v, "d;array item must be float", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000380 return -1;
381 if (i >= 0)
382 ((double *)ap->ob_item)[i] = x;
383 return 0;
384}
385
386/* Description of types */
Guido van Rossum234f9421993-06-17 12:35:49 +0000387static struct arraydescr descriptors[] = {
Guido van Rossum778983b1993-02-19 15:55:02 +0000388 {'c', sizeof(char), c_getitem, c_setitem},
389 {'b', sizeof(char), b_getitem, b_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000390 {'B', sizeof(char), BB_getitem, BB_setitem},
Martin v. Löwis99866332002-03-01 10:27:01 +0000391#ifdef Py_USING_UNICODE
392 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem},
393#endif
Guido van Rossum778983b1993-02-19 15:55:02 +0000394 {'h', sizeof(short), h_getitem, h_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000395 {'H', sizeof(short), HH_getitem, HH_setitem},
Guido van Rossume77a7571993-11-03 15:01:26 +0000396 {'i', sizeof(int), i_getitem, i_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000397 {'I', sizeof(int), II_getitem, II_setitem},
Guido van Rossum778983b1993-02-19 15:55:02 +0000398 {'l', sizeof(long), l_getitem, l_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000399 {'L', sizeof(long), LL_getitem, LL_setitem},
Guido van Rossum778983b1993-02-19 15:55:02 +0000400 {'f', sizeof(float), f_getitem, f_setitem},
401 {'d', sizeof(double), d_getitem, d_setitem},
402 {'\0', 0, 0, 0} /* Sentinel */
403};
Tim Petersbb307342000-09-10 05:22:54 +0000404
405/****************************************************************************
406Implementations of array object methods.
407****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +0000408
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000409static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +0000410newarrayobject(PyTypeObject *type, int size, struct arraydescr *descr)
Guido van Rossum778983b1993-02-19 15:55:02 +0000411{
Guido van Rossum778983b1993-02-19 15:55:02 +0000412 arrayobject *op;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000413 size_t nbytes;
Martin v. Löwis99866332002-03-01 10:27:01 +0000414
Guido van Rossum778983b1993-02-19 15:55:02 +0000415 if (size < 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000416 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000417 return NULL;
418 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000419
Guido van Rossum778983b1993-02-19 15:55:02 +0000420 nbytes = size * descr->itemsize;
421 /* Check for overflow */
Guido van Rossum7844e381997-04-11 20:44:04 +0000422 if (nbytes / descr->itemsize != (size_t)size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000423 return PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000424 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000425 op = (arrayobject *) type->tp_alloc(type, 0);
Guido van Rossum778983b1993-02-19 15:55:02 +0000426 if (op == NULL) {
Martin v. Löwis99866332002-03-01 10:27:01 +0000427 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +0000428 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000429 op->ob_size = size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000430 if (size <= 0) {
431 op->ob_item = NULL;
432 }
433 else {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000434 op->ob_item = PyMem_NEW(char, nbytes);
Guido van Rossum778983b1993-02-19 15:55:02 +0000435 if (op->ob_item == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000436 PyObject_Del(op);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000437 return PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000438 }
439 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000440 op->ob_descr = descr;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000441 return (PyObject *) op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000442}
443
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000444static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000445getarrayitem(PyObject *op, int i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000446{
447 register arrayobject *ap;
Martin v. Löwis99866332002-03-01 10:27:01 +0000448 assert(array_Check(op));
Guido van Rossum778983b1993-02-19 15:55:02 +0000449 ap = (arrayobject *)op;
450 if (i < 0 || i >= ap->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000451 PyErr_SetString(PyExc_IndexError, "array index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000452 return NULL;
453 }
454 return (*ap->ob_descr->getitem)(ap, i);
455}
456
457static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000458ins1(arrayobject *self, int where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000459{
Guido van Rossum778983b1993-02-19 15:55:02 +0000460 char *items;
461 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000462 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000463 return -1;
464 }
465 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
466 return -1;
467 items = self->ob_item;
Neal Norwitz937ca982003-02-24 02:08:42 +0000468 NRESIZE(items, char, (self->ob_size+1) * self->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000469 if (items == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000470 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000471 return -1;
472 }
473 if (where < 0)
474 where = 0;
475 if (where > self->ob_size)
476 where = self->ob_size;
477 memmove(items + (where+1)*self->ob_descr->itemsize,
478 items + where*self->ob_descr->itemsize,
479 (self->ob_size-where)*self->ob_descr->itemsize);
480 self->ob_item = items;
481 self->ob_size++;
482 return (*self->ob_descr->setitem)(self, where, v);
483}
484
Guido van Rossum778983b1993-02-19 15:55:02 +0000485/* Methods */
486
487static void
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000488array_dealloc(arrayobject *op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000489{
Guido van Rossum778983b1993-02-19 15:55:02 +0000490 if (op->ob_item != NULL)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000491 PyMem_DEL(op->ob_item);
Martin v. Löwis99866332002-03-01 10:27:01 +0000492 op->ob_type->tp_free((PyObject *)op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000493}
494
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000495static PyObject *
496array_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000497{
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000498 arrayobject *va, *wa;
499 PyObject *vi = NULL;
500 PyObject *wi = NULL;
501 int i, k;
502 PyObject *res;
503
Martin v. Löwis99866332002-03-01 10:27:01 +0000504 if (!array_Check(v) || !array_Check(w)) {
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000505 Py_INCREF(Py_NotImplemented);
506 return Py_NotImplemented;
Guido van Rossum778983b1993-02-19 15:55:02 +0000507 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000508
509 va = (arrayobject *)v;
510 wa = (arrayobject *)w;
511
512 if (va->ob_size != wa->ob_size && (op == Py_EQ || op == Py_NE)) {
513 /* Shortcut: if the lengths differ, the arrays differ */
514 if (op == Py_EQ)
515 res = Py_False;
516 else
517 res = Py_True;
518 Py_INCREF(res);
519 return res;
520 }
521
522 /* Search for the first index where items are different */
523 k = 1;
524 for (i = 0; i < va->ob_size && i < wa->ob_size; i++) {
525 vi = getarrayitem(v, i);
526 wi = getarrayitem(w, i);
527 if (vi == NULL || wi == NULL) {
528 Py_XDECREF(vi);
529 Py_XDECREF(wi);
530 return NULL;
531 }
532 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
533 if (k == 0)
534 break; /* Keeping vi and wi alive! */
535 Py_DECREF(vi);
536 Py_DECREF(wi);
537 if (k < 0)
538 return NULL;
539 }
540
541 if (k) {
542 /* No more items to compare -- compare sizes */
543 int vs = va->ob_size;
544 int ws = wa->ob_size;
545 int cmp;
546 switch (op) {
547 case Py_LT: cmp = vs < ws; break;
Guido van Rossum2b597e42001-01-25 22:12:43 +0000548 case Py_LE: cmp = vs <= ws; break;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000549 case Py_EQ: cmp = vs == ws; break;
550 case Py_NE: cmp = vs != ws; break;
551 case Py_GT: cmp = vs > ws; break;
552 case Py_GE: cmp = vs >= ws; break;
553 default: return NULL; /* cannot happen */
554 }
555 if (cmp)
556 res = Py_True;
557 else
558 res = Py_False;
559 Py_INCREF(res);
560 return res;
561 }
562
563 /* We have an item that differs. First, shortcuts for EQ/NE */
564 if (op == Py_EQ) {
565 Py_INCREF(Py_False);
566 res = Py_False;
567 }
568 else if (op == Py_NE) {
569 Py_INCREF(Py_True);
570 res = Py_True;
571 }
572 else {
573 /* Compare the final item again using the proper operator */
574 res = PyObject_RichCompare(vi, wi, op);
575 }
576 Py_DECREF(vi);
577 Py_DECREF(wi);
578 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +0000579}
580
581static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000582array_length(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +0000583{
584 return a->ob_size;
585}
586
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000587static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000588array_item(arrayobject *a, int i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000589{
590 if (i < 0 || i >= a->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000591 PyErr_SetString(PyExc_IndexError, "array index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000592 return NULL;
593 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000594 return getarrayitem((PyObject *)a, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000595}
596
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000597static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000598array_slice(arrayobject *a, int ilow, int ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000599{
600 arrayobject *np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000601 if (ilow < 0)
602 ilow = 0;
603 else if (ilow > a->ob_size)
604 ilow = a->ob_size;
605 if (ihigh < 0)
606 ihigh = 0;
607 if (ihigh < ilow)
608 ihigh = ilow;
609 else if (ihigh > a->ob_size)
610 ihigh = a->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000611 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
Guido van Rossum778983b1993-02-19 15:55:02 +0000612 if (np == NULL)
613 return NULL;
614 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
615 (ihigh-ilow) * a->ob_descr->itemsize);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000616 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000617}
618
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000619static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000620array_concat(arrayobject *a, PyObject *bb)
Guido van Rossum778983b1993-02-19 15:55:02 +0000621{
622 int size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000623 arrayobject *np;
Martin v. Löwis99866332002-03-01 10:27:01 +0000624 if (!array_Check(bb)) {
Fred Drake137507e2000-06-01 02:02:46 +0000625 PyErr_Format(PyExc_TypeError,
626 "can only append array (not \"%.200s\") to array",
627 bb->ob_type->tp_name);
Guido van Rossum778983b1993-02-19 15:55:02 +0000628 return NULL;
629 }
630#define b ((arrayobject *)bb)
631 if (a->ob_descr != b->ob_descr) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000632 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000633 return NULL;
634 }
635 size = a->ob_size + b->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000636 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
Guido van Rossum778983b1993-02-19 15:55:02 +0000637 if (np == NULL) {
638 return NULL;
639 }
640 memcpy(np->ob_item, a->ob_item, a->ob_size*a->ob_descr->itemsize);
641 memcpy(np->ob_item + a->ob_size*a->ob_descr->itemsize,
Guido van Rossum32be3a71993-11-05 10:16:27 +0000642 b->ob_item, b->ob_size*b->ob_descr->itemsize);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000643 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000644#undef b
645}
646
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000647static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000648array_repeat(arrayobject *a, int n)
Guido van Rossum778983b1993-02-19 15:55:02 +0000649{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000650 int i;
Guido van Rossum778983b1993-02-19 15:55:02 +0000651 int size;
652 arrayobject *np;
653 char *p;
654 int nbytes;
655 if (n < 0)
656 n = 0;
657 size = a->ob_size * n;
Martin v. Löwis99866332002-03-01 10:27:01 +0000658 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
Guido van Rossum778983b1993-02-19 15:55:02 +0000659 if (np == NULL)
660 return NULL;
661 p = np->ob_item;
662 nbytes = a->ob_size * a->ob_descr->itemsize;
663 for (i = 0; i < n; i++) {
664 memcpy(p, a->ob_item, nbytes);
665 p += nbytes;
666 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000667 return (PyObject *) np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000668}
669
670static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000671array_ass_slice(arrayobject *a, int ilow, int ihigh, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000672{
673 char *item;
674 int n; /* Size of replacement array */
675 int d; /* Change in size */
Guido van Rossum778983b1993-02-19 15:55:02 +0000676#define b ((arrayobject *)v)
677 if (v == NULL)
678 n = 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000679 else if (array_Check(v)) {
Guido van Rossum778983b1993-02-19 15:55:02 +0000680 n = b->ob_size;
681 if (a == b) {
682 /* Special case "a[i:j] = a" -- copy b first */
683 int ret;
684 v = array_slice(b, 0, n);
685 ret = array_ass_slice(a, ilow, ihigh, v);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000686 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000687 return ret;
688 }
689 if (b->ob_descr != a->ob_descr) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000690 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000691 return -1;
692 }
693 }
694 else {
Fred Drake137507e2000-06-01 02:02:46 +0000695 PyErr_Format(PyExc_TypeError,
696 "can only assign array (not \"%.200s\") to array slice",
697 v->ob_type->tp_name);
Guido van Rossum778983b1993-02-19 15:55:02 +0000698 return -1;
699 }
700 if (ilow < 0)
701 ilow = 0;
702 else if (ilow > a->ob_size)
703 ilow = a->ob_size;
704 if (ihigh < 0)
705 ihigh = 0;
706 if (ihigh < ilow)
707 ihigh = ilow;
708 else if (ihigh > a->ob_size)
709 ihigh = a->ob_size;
710 item = a->ob_item;
711 d = n - (ihigh-ilow);
712 if (d < 0) { /* Delete -d items */
713 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
714 item + ihigh*a->ob_descr->itemsize,
715 (a->ob_size-ihigh)*a->ob_descr->itemsize);
716 a->ob_size += d;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000717 PyMem_RESIZE(item, char, a->ob_size*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000718 /* Can't fail */
719 a->ob_item = item;
720 }
721 else if (d > 0) { /* Insert d items */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000722 PyMem_RESIZE(item, char,
723 (a->ob_size + d)*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000724 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000725 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000726 return -1;
727 }
728 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
729 item + ihigh*a->ob_descr->itemsize,
730 (a->ob_size-ihigh)*a->ob_descr->itemsize);
731 a->ob_item = item;
732 a->ob_size += d;
733 }
734 if (n > 0)
735 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
736 n*b->ob_descr->itemsize);
737 return 0;
738#undef b
739}
740
741static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000742array_ass_item(arrayobject *a, int i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000743{
744 if (i < 0 || i >= a->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000745 PyErr_SetString(PyExc_IndexError,
746 "array assignment index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000747 return -1;
748 }
749 if (v == NULL)
750 return array_ass_slice(a, i, i+1, v);
751 return (*a->ob_descr->setitem)(a, i, v);
752}
753
754static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000755setarrayitem(PyObject *a, int i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000756{
Martin v. Löwis99866332002-03-01 10:27:01 +0000757 assert(array_Check(a));
Guido van Rossum778983b1993-02-19 15:55:02 +0000758 return array_ass_item((arrayobject *)a, i, v);
759}
760
Martin v. Löwis99866332002-03-01 10:27:01 +0000761static int
762array_do_extend(arrayobject *self, PyObject *bb)
763{
764 int size;
765
766 if (!array_Check(bb)) {
767 PyErr_Format(PyExc_TypeError,
768 "can only extend array with array (not \"%.200s\")",
769 bb->ob_type->tp_name);
770 return -1;
771 }
772#define b ((arrayobject *)bb)
773 if (self->ob_descr != b->ob_descr) {
774 PyErr_SetString(PyExc_TypeError,
775 "can only extend with array of same kind");
776 return -1;
777 }
778 size = self->ob_size + b->ob_size;
779 PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize);
780 if (self->ob_item == NULL) {
781 PyObject_Del(self);
782 PyErr_NoMemory();
783 return -1;
784 }
785 memcpy(self->ob_item + self->ob_size*self->ob_descr->itemsize,
786 b->ob_item, b->ob_size*b->ob_descr->itemsize);
787 self->ob_size = size;
788
789 return 0;
790#undef b
791}
792
793static PyObject *
794array_inplace_concat(arrayobject *self, PyObject *bb)
795{
796 if (array_do_extend(self, bb) == -1)
797 return NULL;
798 Py_INCREF(self);
799 return (PyObject *)self;
800}
801
802static PyObject *
803array_inplace_repeat(arrayobject *self, int n)
804{
805 char *items, *p;
806 int size, i;
807
808 if (self->ob_size > 0) {
809 if (n < 0)
810 n = 0;
811 items = self->ob_item;
812 size = self->ob_size * self->ob_descr->itemsize;
813 if (n == 0) {
814 PyMem_FREE(items);
815 self->ob_item = NULL;
816 self->ob_size = 0;
817 }
818 else {
819 PyMem_Resize(items, char, n * size);
820 if (items == NULL)
821 return PyErr_NoMemory();
822 p = items;
823 for (i = 1; i < n; i++) {
824 p += size;
825 memcpy(p, items, size);
826 }
827 self->ob_item = items;
828 self->ob_size *= n;
829 }
830 }
831 Py_INCREF(self);
832 return (PyObject *)self;
833}
834
835
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000836static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000837ins(arrayobject *self, int where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000838{
839 if (ins1(self, where, v) != 0)
840 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000841 Py_INCREF(Py_None);
842 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000843}
844
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000845static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000846array_count(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000847{
848 int count = 0;
849 int i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000850
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000851 for (i = 0; i < self->ob_size; i++) {
852 PyObject *selfi = getarrayitem((PyObject *)self, i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000853 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000854 Py_DECREF(selfi);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000855 if (cmp > 0)
856 count++;
857 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000858 return NULL;
859 }
860 return PyInt_FromLong((long)count);
861}
862
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000863PyDoc_STRVAR(count_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000864"count(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000865\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000866Return number of occurences of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000867
868static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000869array_index(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000870{
871 int i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000872
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000873 for (i = 0; i < self->ob_size; i++) {
874 PyObject *selfi = getarrayitem((PyObject *)self, i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000875 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
876 Py_DECREF(selfi);
877 if (cmp > 0) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000878 return PyInt_FromLong((long)i);
879 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000880 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000881 return NULL;
882 }
883 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
884 return NULL;
885}
886
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000887PyDoc_STRVAR(index_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000888"index(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000889\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000890Return index of first occurence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000891
Raymond Hettinger625812f2003-01-07 01:58:52 +0000892static int
893array_contains(arrayobject *self, PyObject *v)
894{
895 int i, cmp;
896
897 for (i = 0, cmp = 0 ; cmp == 0 && i < self->ob_size; i++) {
898 PyObject *selfi = getarrayitem((PyObject *)self, i);
899 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
900 Py_DECREF(selfi);
901 }
902 return cmp;
903}
904
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000905static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000906array_remove(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000907{
908 int i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000909
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000910 for (i = 0; i < self->ob_size; i++) {
911 PyObject *selfi = getarrayitem((PyObject *)self,i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000912 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
913 Py_DECREF(selfi);
914 if (cmp > 0) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000915 if (array_ass_slice(self, i, i+1,
916 (PyObject *)NULL) != 0)
917 return NULL;
918 Py_INCREF(Py_None);
919 return Py_None;
920 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000921 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000922 return NULL;
923 }
924 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
925 return NULL;
926}
927
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000928PyDoc_STRVAR(remove_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000929"remove(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000930\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000931Remove the first occurence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000932
933static PyObject *
934array_pop(arrayobject *self, PyObject *args)
935{
936 int i = -1;
937 PyObject *v;
938 if (!PyArg_ParseTuple(args, "|i:pop", &i))
939 return NULL;
940 if (self->ob_size == 0) {
941 /* Special-case most common failure cause */
942 PyErr_SetString(PyExc_IndexError, "pop from empty array");
943 return NULL;
944 }
945 if (i < 0)
946 i += self->ob_size;
947 if (i < 0 || i >= self->ob_size) {
948 PyErr_SetString(PyExc_IndexError, "pop index out of range");
949 return NULL;
950 }
951 v = getarrayitem((PyObject *)self,i);
952 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
953 Py_DECREF(v);
954 return NULL;
955 }
956 return v;
957}
958
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000959PyDoc_STRVAR(pop_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000960"pop([i])\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000961\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000962Return the i-th element and delete it from the array. i defaults to -1.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000963
964static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000965array_extend(arrayobject *self, PyObject *bb)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000966{
Martin v. Löwis99866332002-03-01 10:27:01 +0000967 if (array_do_extend(self, bb) == -1)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000968 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +0000969 Py_INCREF(Py_None);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000970 return Py_None;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000971}
972
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000973PyDoc_STRVAR(extend_doc,
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000974"extend(array)\n\
975\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000976 Append array items to the end of the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000977
978static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000979array_insert(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +0000980{
981 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000982 PyObject *v;
Peter Schneider-Kampdc71cac2000-07-31 21:57:30 +0000983 if (!PyArg_ParseTuple(args, "iO:insert", &i, &v))
Guido van Rossum778983b1993-02-19 15:55:02 +0000984 return NULL;
985 return ins(self, i, v);
986}
987
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000988PyDoc_STRVAR(insert_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000989"insert(i,x)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000990\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000991Insert a new item x into the array before position i.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000992
993
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000994static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000995array_buffer_info(arrayobject *self, PyObject *unused)
Guido van Rossumde4a4ca1997-08-12 14:55:56 +0000996{
Tim Peters077a11d2000-09-16 22:31:29 +0000997 PyObject* retval = NULL;
Tim Peters077a11d2000-09-16 22:31:29 +0000998 retval = PyTuple_New(2);
999 if (!retval)
1000 return NULL;
Fred Drake541dc3b2000-06-28 17:49:30 +00001001
1002 PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item));
1003 PyTuple_SET_ITEM(retval, 1, PyInt_FromLong((long)(self->ob_size)));
1004
1005 return retval;
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001006}
1007
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001008PyDoc_STRVAR(buffer_info_doc,
Tim Peters077a11d2000-09-16 22:31:29 +00001009"buffer_info() -> (address, length)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001010\n\
1011Return a tuple (address, length) giving the current memory address and\n\
Guido van Rossum702d08e2001-07-27 16:05:32 +00001012the length in items of the buffer used to hold array's contents\n\
1013The length should be multiplied by the itemsize attribute to calculate\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001014the buffer length in bytes.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001015
1016
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001017static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001018array_append(arrayobject *self, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +00001019{
Guido van Rossum778983b1993-02-19 15:55:02 +00001020 return ins(self, (int) self->ob_size, v);
1021}
1022
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001023PyDoc_STRVAR(append_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001024"append(x)\n\
1025\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001026Append new value x to the end of the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001027
1028
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001029static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001030array_byteswap(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001031{
1032 char *p;
1033 int i;
Fred Drakebf272981999-12-03 17:15:30 +00001034
Guido van Rossum778983b1993-02-19 15:55:02 +00001035 switch (self->ob_descr->itemsize) {
1036 case 1:
1037 break;
1038 case 2:
1039 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 2) {
1040 char p0 = p[0];
1041 p[0] = p[1];
1042 p[1] = p0;
1043 }
1044 break;
1045 case 4:
1046 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 4) {
1047 char p0 = p[0];
1048 char p1 = p[1];
1049 p[0] = p[3];
1050 p[1] = p[2];
1051 p[2] = p1;
1052 p[3] = p0;
1053 }
1054 break;
Guido van Rossume77a7571993-11-03 15:01:26 +00001055 case 8:
1056 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 8) {
1057 char p0 = p[0];
1058 char p1 = p[1];
1059 char p2 = p[2];
1060 char p3 = p[3];
1061 p[0] = p[7];
1062 p[1] = p[6];
1063 p[2] = p[5];
1064 p[3] = p[4];
1065 p[4] = p3;
1066 p[5] = p2;
1067 p[6] = p1;
1068 p[7] = p0;
1069 }
1070 break;
Guido van Rossum778983b1993-02-19 15:55:02 +00001071 default:
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001072 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossum778983b1993-02-19 15:55:02 +00001073 "don't know how to byteswap this array type");
1074 return NULL;
1075 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001076 Py_INCREF(Py_None);
1077 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001078}
1079
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001080PyDoc_STRVAR(byteswap_doc,
Fred Drakebf272981999-12-03 17:15:30 +00001081"byteswap()\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001082\n\
Fred Drakebf272981999-12-03 17:15:30 +00001083Byteswap 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 +000010844, or 8 bytes in size, RuntimeError is raised.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001085
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001086static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001087array_reverse(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001088{
Guido van Rossume77a7571993-11-03 15:01:26 +00001089 register int itemsize = self->ob_descr->itemsize;
1090 register char *p, *q;
Tim Peters077a11d2000-09-16 22:31:29 +00001091 /* little buffer to hold items while swapping */
1092 char tmp[256]; /* 8 is probably enough -- but why skimp */
1093 assert(itemsize <= sizeof(tmp));
Guido van Rossume77a7571993-11-03 15:01:26 +00001094
Guido van Rossum778983b1993-02-19 15:55:02 +00001095 if (self->ob_size > 1) {
Guido van Rossume77a7571993-11-03 15:01:26 +00001096 for (p = self->ob_item,
1097 q = self->ob_item + (self->ob_size - 1)*itemsize;
1098 p < q;
1099 p += itemsize, q -= itemsize) {
Tim Peters077a11d2000-09-16 22:31:29 +00001100 /* memory areas guaranteed disjoint, so memcpy
1101 * is safe (& memmove may be slower).
1102 */
1103 memcpy(tmp, p, itemsize);
1104 memcpy(p, q, itemsize);
1105 memcpy(q, tmp, itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001106 }
1107 }
Tim Petersbb307342000-09-10 05:22:54 +00001108
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001109 Py_INCREF(Py_None);
1110 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001111}
Guido van Rossume77a7571993-11-03 15:01:26 +00001112
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001113PyDoc_STRVAR(reverse_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001114"reverse()\n\
1115\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001116Reverse the order of the items in the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001117
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001118static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001119array_fromfile(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001120{
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001121 PyObject *f;
Guido van Rossum778983b1993-02-19 15:55:02 +00001122 int n;
1123 FILE *fp;
Peter Schneider-Kampdc71cac2000-07-31 21:57:30 +00001124 if (!PyArg_ParseTuple(args, "Oi:fromfile", &f, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +00001125 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001126 fp = PyFile_AsFile(f);
Guido van Rossum778983b1993-02-19 15:55:02 +00001127 if (fp == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001128 PyErr_SetString(PyExc_TypeError, "arg1 must be open file");
Guido van Rossum778983b1993-02-19 15:55:02 +00001129 return NULL;
1130 }
1131 if (n > 0) {
1132 char *item = self->ob_item;
1133 int itemsize = self->ob_descr->itemsize;
Guido van Rossum7d0ae5e2000-06-28 21:27:21 +00001134 size_t nread;
Guido van Rossum3791b0d1999-02-23 18:05:22 +00001135 int newlength;
1136 size_t newbytes;
1137 /* Be careful here about overflow */
1138 if ((newlength = self->ob_size + n) <= 0 ||
Guido van Rossum481ac881999-03-19 21:50:11 +00001139 (newbytes = newlength * itemsize) / itemsize !=
1140 (size_t)newlength)
Guido van Rossum3791b0d1999-02-23 18:05:22 +00001141 goto nomem;
1142 PyMem_RESIZE(item, char, newbytes);
Guido van Rossum778983b1993-02-19 15:55:02 +00001143 if (item == NULL) {
Guido van Rossum3791b0d1999-02-23 18:05:22 +00001144 nomem:
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001145 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001146 return NULL;
1147 }
1148 self->ob_item = item;
1149 self->ob_size += n;
1150 nread = fread(item + (self->ob_size - n) * itemsize,
1151 itemsize, n, fp);
Guido van Rossum7d0ae5e2000-06-28 21:27:21 +00001152 if (nread < (size_t)n) {
Guido van Rossum778983b1993-02-19 15:55:02 +00001153 self->ob_size -= (n - nread);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001154 PyMem_RESIZE(item, char, self->ob_size*itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001155 self->ob_item = item;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001156 PyErr_SetString(PyExc_EOFError,
1157 "not enough items in file");
Guido van Rossum778983b1993-02-19 15:55:02 +00001158 return NULL;
1159 }
1160 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001161 Py_INCREF(Py_None);
1162 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001163}
1164
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001165PyDoc_STRVAR(fromfile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001166"fromfile(f, n)\n\
1167\n\
1168Read n objects from the file object f and append them to the end of the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001169array. Also called as read.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001170
1171
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001172static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001173array_tofile(arrayobject *self, PyObject *f)
Guido van Rossum778983b1993-02-19 15:55:02 +00001174{
Guido van Rossum778983b1993-02-19 15:55:02 +00001175 FILE *fp;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001176
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001177 fp = PyFile_AsFile(f);
Guido van Rossum778983b1993-02-19 15:55:02 +00001178 if (fp == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001179 PyErr_SetString(PyExc_TypeError, "arg must be open file");
Guido van Rossum778983b1993-02-19 15:55:02 +00001180 return NULL;
1181 }
1182 if (self->ob_size > 0) {
Trent Mick6c116dd2000-08-12 20:58:11 +00001183 if (fwrite(self->ob_item, self->ob_descr->itemsize,
1184 self->ob_size, fp) != (size_t)self->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001185 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum778983b1993-02-19 15:55:02 +00001186 clearerr(fp);
1187 return NULL;
1188 }
1189 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001190 Py_INCREF(Py_None);
1191 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001192}
1193
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001194PyDoc_STRVAR(tofile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001195"tofile(f)\n\
1196\n\
1197Write all items (as machine values) to the file object f. Also called as\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001198write.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001199
1200
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001201static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001202array_fromlist(arrayobject *self, PyObject *list)
Guido van Rossum778983b1993-02-19 15:55:02 +00001203{
1204 int n;
Guido van Rossum778983b1993-02-19 15:55:02 +00001205 int itemsize = self->ob_descr->itemsize;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001206
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001207 if (!PyList_Check(list)) {
1208 PyErr_SetString(PyExc_TypeError, "arg must be list");
Guido van Rossum778983b1993-02-19 15:55:02 +00001209 return NULL;
1210 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001211 n = PyList_Size(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001212 if (n > 0) {
1213 char *item = self->ob_item;
1214 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001215 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001216 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001217 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001218 return NULL;
1219 }
1220 self->ob_item = item;
1221 self->ob_size += n;
1222 for (i = 0; i < n; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001223 PyObject *v = PyList_GetItem(list, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001224 if ((*self->ob_descr->setitem)(self,
1225 self->ob_size - n + i, v) != 0) {
1226 self->ob_size -= n;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001227 PyMem_RESIZE(item, char,
1228 self->ob_size * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001229 self->ob_item = item;
1230 return NULL;
1231 }
1232 }
1233 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001234 Py_INCREF(Py_None);
1235 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001236}
1237
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001238PyDoc_STRVAR(fromlist_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001239"fromlist(list)\n\
1240\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001241Append items to array from list.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001242
1243
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001244static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001245array_tolist(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001246{
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001247 PyObject *list = PyList_New(self->ob_size);
Guido van Rossum778983b1993-02-19 15:55:02 +00001248 int i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001249
Guido van Rossum778983b1993-02-19 15:55:02 +00001250 if (list == NULL)
1251 return NULL;
1252 for (i = 0; i < self->ob_size; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001253 PyObject *v = getarrayitem((PyObject *)self, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001254 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001255 Py_DECREF(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001256 return NULL;
1257 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001258 PyList_SetItem(list, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001259 }
1260 return list;
1261}
1262
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001263PyDoc_STRVAR(tolist_doc,
Guido van Rossumfc6aba51998-10-14 02:52:31 +00001264"tolist() -> list\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001265\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001266Convert array to an ordinary list with the same items.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001267
1268
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001269static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001270array_fromstring(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001271{
1272 char *str;
1273 int n;
1274 int itemsize = self->ob_descr->itemsize;
Peter Schneider-Kampdc71cac2000-07-31 21:57:30 +00001275 if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +00001276 return NULL;
1277 if (n % itemsize != 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001278 PyErr_SetString(PyExc_ValueError,
Guido van Rossum778983b1993-02-19 15:55:02 +00001279 "string length not a multiple of item size");
1280 return NULL;
1281 }
1282 n = n / itemsize;
1283 if (n > 0) {
1284 char *item = self->ob_item;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001285 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001286 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001287 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001288 return NULL;
1289 }
1290 self->ob_item = item;
1291 self->ob_size += n;
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001292 memcpy(item + (self->ob_size - n) * itemsize,
1293 str, itemsize*n);
Guido van Rossum778983b1993-02-19 15:55:02 +00001294 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001295 Py_INCREF(Py_None);
1296 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001297}
1298
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001299PyDoc_STRVAR(fromstring_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001300"fromstring(string)\n\
1301\n\
1302Appends items from the string, interpreting it as an array of machine\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001303values,as if it had been read from a file using the fromfile() method).");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001304
1305
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001306static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001307array_tostring(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001308{
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001309 return PyString_FromStringAndSize(self->ob_item,
Guido van Rossum778983b1993-02-19 15:55:02 +00001310 self->ob_size * self->ob_descr->itemsize);
1311}
1312
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001313PyDoc_STRVAR(tostring_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001314"tostring() -> string\n\
1315\n\
1316Convert the array to an array of machine values and return the string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001317representation.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001318
Martin v. Löwis99866332002-03-01 10:27:01 +00001319
1320
1321#ifdef Py_USING_UNICODE
1322static PyObject *
1323array_fromunicode(arrayobject *self, PyObject *args)
1324{
1325 Py_UNICODE *ustr;
1326 int n;
1327
1328 if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
1329 return NULL;
1330 if (self->ob_descr->typecode != 'u') {
1331 PyErr_SetString(PyExc_ValueError,
1332 "fromunicode() may only be called on "
1333 "type 'u' arrays");
1334 return NULL;
1335 }
1336 if (n > 0) {
1337 Py_UNICODE *item = (Py_UNICODE *) self->ob_item;
1338 PyMem_RESIZE(item, Py_UNICODE, self->ob_size + n);
1339 if (item == NULL) {
1340 PyErr_NoMemory();
1341 return NULL;
1342 }
1343 self->ob_item = (char *) item;
1344 self->ob_size += n;
1345 memcpy(item + self->ob_size - n,
1346 ustr, n * sizeof(Py_UNICODE));
1347 }
1348
1349 Py_INCREF(Py_None);
1350 return Py_None;
1351}
1352
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001353PyDoc_STRVAR(fromunicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001354"fromunicode(ustr)\n\
1355\n\
1356Extends this array with data from the unicode string ustr.\n\
1357The array must be a type 'u' array; otherwise a ValueError\n\
1358is raised. Use array.fromstring(ustr.decode(...)) to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001359append Unicode data to an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001360
1361
1362static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001363array_tounicode(arrayobject *self, PyObject *unused)
Martin v. Löwis99866332002-03-01 10:27:01 +00001364{
Martin v. Löwis99866332002-03-01 10:27:01 +00001365 if (self->ob_descr->typecode != 'u') {
1366 PyErr_SetString(PyExc_ValueError,
1367 "tounicode() may only be called on type 'u' arrays");
1368 return NULL;
1369 }
1370 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, self->ob_size);
1371}
1372
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001373PyDoc_STRVAR(tounicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001374"tounicode() -> unicode\n\
1375\n\
1376Convert the array to a unicode string. The array must be\n\
1377a type 'u' array; otherwise a ValueError is raised. Use\n\
1378array.tostring().decode() to obtain a unicode string from\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001379an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001380
1381#endif /* Py_USING_UNICODE */
1382
1383
1384static PyObject *
1385array_get_typecode(arrayobject *a, void *closure)
1386{
1387 char tc = a->ob_descr->typecode;
1388 return PyString_FromStringAndSize(&tc, 1);
1389}
1390
1391static PyObject *
1392array_get_itemsize(arrayobject *a, void *closure)
1393{
1394 return PyInt_FromLong((long)a->ob_descr->itemsize);
1395}
1396
1397static PyGetSetDef array_getsets [] = {
1398 {"typecode", (getter) array_get_typecode, NULL,
1399 "the typecode character used to create the array"},
1400 {"itemsize", (getter) array_get_itemsize, NULL,
1401 "the size, in bytes, of one array item"},
1402 {NULL}
1403};
1404
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001405PyMethodDef array_methods[] = {
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001406 {"append", (PyCFunction)array_append, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001407 append_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001408 {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001409 buffer_info_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001410 {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001411 byteswap_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001412 {"count", (PyCFunction)array_count, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001413 count_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001414 {"extend", (PyCFunction)array_extend, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001415 extend_doc},
1416 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
1417 fromfile_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001418 {"fromlist", (PyCFunction)array_fromlist, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001419 fromlist_doc},
1420 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
1421 fromstring_doc},
Martin v. Löwis99866332002-03-01 10:27:01 +00001422#ifdef Py_USING_UNICODE
1423 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
1424 fromunicode_doc},
1425#endif
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001426 {"index", (PyCFunction)array_index, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001427 index_doc},
1428 {"insert", (PyCFunction)array_insert, METH_VARARGS,
1429 insert_doc},
1430 {"pop", (PyCFunction)array_pop, METH_VARARGS,
1431 pop_doc},
1432 {"read", (PyCFunction)array_fromfile, METH_VARARGS,
1433 fromfile_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001434 {"remove", (PyCFunction)array_remove, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001435 remove_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001436 {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001437 reverse_doc},
1438/* {"sort", (PyCFunction)array_sort, METH_VARARGS,
1439 sort_doc},*/
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001440 {"tofile", (PyCFunction)array_tofile, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001441 tofile_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001442 {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001443 tolist_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001444 {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001445 tostring_doc},
Martin v. Löwis99866332002-03-01 10:27:01 +00001446#ifdef Py_USING_UNICODE
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001447 {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
Martin v. Löwis99866332002-03-01 10:27:01 +00001448 tounicode_doc},
1449#endif
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001450 {"write", (PyCFunction)array_tofile, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001451 tofile_doc},
Guido van Rossum778983b1993-02-19 15:55:02 +00001452 {NULL, NULL} /* sentinel */
1453};
1454
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001455static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001456array_repr(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +00001457{
Martin v. Löwis99866332002-03-01 10:27:01 +00001458 char buf[256], typecode;
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001459 PyObject *s, *t, *v = NULL;
1460 int len;
Martin v. Löwis99866332002-03-01 10:27:01 +00001461
Guido van Rossum778983b1993-02-19 15:55:02 +00001462 len = a->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +00001463 typecode = a->ob_descr->typecode;
Guido van Rossum778983b1993-02-19 15:55:02 +00001464 if (len == 0) {
Martin v. Löwis99866332002-03-01 10:27:01 +00001465 PyOS_snprintf(buf, sizeof(buf), "array('%c')", typecode);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001466 return PyString_FromString(buf);
Guido van Rossum778983b1993-02-19 15:55:02 +00001467 }
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001468
1469 if (typecode == 'c')
1470 v = array_tostring(a, NULL);
Michael W. Hudson1755ad92002-05-13 10:14:59 +00001471#ifdef Py_USING_UNICODE
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001472 else if (typecode == 'u')
1473 v = array_tounicode(a, NULL);
Michael W. Hudson1755ad92002-05-13 10:14:59 +00001474#endif
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001475 else
1476 v = array_tolist(a, NULL);
1477 t = PyObject_Repr(v);
1478 Py_XDECREF(v);
1479
1480 PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001481 s = PyString_FromString(buf);
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00001482 PyString_ConcatAndDel(&s, t);
1483 PyString_ConcatAndDel(&s, PyString_FromString(")"));
Guido van Rossum778983b1993-02-19 15:55:02 +00001484 return s;
1485}
1486
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001487static PyObject*
1488array_subscr(arrayobject* self, PyObject* item)
1489{
1490 if (PyInt_Check(item)) {
1491 long i = PyInt_AS_LONG(item);
1492 if (i < 0)
1493 i += self->ob_size;
1494 return array_item(self, i);
1495 }
1496 else if (PyLong_Check(item)) {
1497 long i = PyLong_AsLong(item);
1498 if (i == -1 && PyErr_Occurred())
1499 return NULL;
1500 if (i < 0)
1501 i += self->ob_size;
1502 return array_item(self, i);
1503 }
1504 else if (PySlice_Check(item)) {
1505 int start, stop, step, slicelength, cur, i;
1506 PyObject* result;
1507 arrayobject* ar;
1508 int itemsize = self->ob_descr->itemsize;
1509
1510 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
1511 &start, &stop, &step, &slicelength) < 0) {
1512 return NULL;
1513 }
1514
1515 if (slicelength <= 0) {
1516 return newarrayobject(&Arraytype, 0, self->ob_descr);
1517 }
1518 else {
1519 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
1520 if (!result) return NULL;
1521
1522 ar = (arrayobject*)result;
1523
1524 for (cur = start, i = 0; i < slicelength;
1525 cur += step, i++) {
1526 memcpy(ar->ob_item + i*itemsize,
1527 self->ob_item + cur*itemsize,
1528 itemsize);
1529 }
1530
1531 return result;
1532 }
1533 }
1534 else {
1535 PyErr_SetString(PyExc_TypeError,
1536 "list indices must be integers");
1537 return NULL;
1538 }
1539}
1540
1541static int
1542array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
1543{
1544 if (PyInt_Check(item)) {
1545 long i = PyInt_AS_LONG(item);
1546 if (i < 0)
1547 i += self->ob_size;
1548 return array_ass_item(self, i, value);
1549 }
1550 else if (PyLong_Check(item)) {
1551 long i = PyLong_AsLong(item);
1552 if (i == -1 && PyErr_Occurred())
1553 return -1;
1554 if (i < 0)
1555 i += self->ob_size;
1556 return array_ass_item(self, i, value);
1557 }
1558 else if (PySlice_Check(item)) {
1559 int start, stop, step, slicelength;
1560 int itemsize = self->ob_descr->itemsize;
1561
1562 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
1563 &start, &stop, &step, &slicelength) < 0) {
1564 return -1;
1565 }
1566
1567 /* treat A[slice(a,b)] = v _exactly_ like A[a:b] = v */
1568 if (step == 1 && ((PySliceObject*)item)->step == Py_None)
1569 return array_ass_slice(self, start, stop, value);
1570
1571 if (value == NULL) {
1572 /* delete slice */
Michael W. Hudson56796f62002-07-29 14:35:04 +00001573 int cur, i, extra;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001574
1575 if (slicelength <= 0)
1576 return 0;
1577
1578 if (step < 0) {
1579 stop = start + 1;
1580 start = stop + step*(slicelength - 1) - 1;
1581 step = -step;
1582 }
1583
Michael W. Hudson56796f62002-07-29 14:35:04 +00001584 for (cur = start, i = 0; i < slicelength - 1;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001585 cur += step, i++) {
1586 memmove(self->ob_item + (cur - i)*itemsize,
1587 self->ob_item + (cur + 1)*itemsize,
1588 (step - 1) * itemsize);
1589 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00001590 extra = self->ob_size - (cur + 1);
1591 if (extra > 0) {
1592 memmove(self->ob_item + (cur - i)*itemsize,
1593 self->ob_item + (cur + 1)*itemsize,
1594 extra*itemsize);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001595 }
1596
1597 self->ob_size -= slicelength;
1598 self->ob_item = PyMem_REALLOC(self->ob_item, itemsize*self->ob_size);
1599
1600
1601 return 0;
1602 }
1603 else {
1604 /* assign slice */
1605 int cur, i;
1606 arrayobject* av;
1607
1608 if (!array_Check(value)) {
1609 PyErr_Format(PyExc_TypeError,
1610 "must assign array (not \"%.200s\") to slice",
1611 value->ob_type->tp_name);
1612 return -1;
1613 }
1614
1615 av = (arrayobject*)value;
1616
1617 if (av->ob_size != slicelength) {
1618 PyErr_Format(PyExc_ValueError,
1619 "attempt to assign array of size %d to extended slice of size %d",
1620 av->ob_size, slicelength);
1621 return -1;
1622 }
1623
1624 if (!slicelength)
1625 return 0;
1626
1627 /* protect against a[::-1] = a */
1628 if (self == av) {
1629 value = array_slice(av, 0, av->ob_size);
1630 av = (arrayobject*)value;
1631 }
1632 else {
1633 Py_INCREF(value);
1634 }
1635
1636 for (cur = start, i = 0; i < slicelength;
1637 cur += step, i++) {
1638 memcpy(self->ob_item + cur*itemsize,
1639 av->ob_item + i*itemsize,
1640 itemsize);
1641 }
1642
1643 Py_DECREF(value);
1644
1645 return 0;
1646 }
1647 }
1648 else {
1649 PyErr_SetString(PyExc_TypeError,
1650 "list indices must be integers");
1651 return -1;
1652 }
1653}
1654
1655static PyMappingMethods array_as_mapping = {
1656 (inquiry)array_length,
1657 (binaryfunc)array_subscr,
1658 (objobjargproc)array_ass_subscr
1659};
1660
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001661static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001662array_buffer_getreadbuf(arrayobject *self, int index, const void **ptr)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001663{
1664 if ( index != 0 ) {
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001665 PyErr_SetString(PyExc_SystemError,
1666 "Accessing non-existent array segment");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001667 return -1;
1668 }
1669 *ptr = (void *)self->ob_item;
1670 return self->ob_size*self->ob_descr->itemsize;
1671}
1672
1673static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001674array_buffer_getwritebuf(arrayobject *self, int index, const void **ptr)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001675{
1676 if ( index != 0 ) {
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001677 PyErr_SetString(PyExc_SystemError,
1678 "Accessing non-existent array segment");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001679 return -1;
1680 }
1681 *ptr = (void *)self->ob_item;
1682 return self->ob_size*self->ob_descr->itemsize;
1683}
1684
1685static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001686array_buffer_getsegcount(arrayobject *self, int *lenp)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001687{
1688 if ( lenp )
1689 *lenp = self->ob_size*self->ob_descr->itemsize;
1690 return 1;
1691}
1692
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001693static PySequenceMethods array_as_sequence = {
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001694 (inquiry)array_length, /*sq_length*/
1695 (binaryfunc)array_concat, /*sq_concat*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001696 (intargfunc)array_repeat, /*sq_repeat*/
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001697 (intargfunc)array_item, /*sq_item*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001698 (intintargfunc)array_slice, /*sq_slice*/
1699 (intobjargproc)array_ass_item, /*sq_ass_item*/
1700 (intintobjargproc)array_ass_slice, /*sq_ass_slice*/
Raymond Hettinger625812f2003-01-07 01:58:52 +00001701 (objobjproc)array_contains, /*sq_contains*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001702 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
1703 (intargfunc)array_inplace_repeat /*sq_inplace_repeat*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001704};
1705
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001706static PyBufferProcs array_as_buffer = {
1707 (getreadbufferproc)array_buffer_getreadbuf,
1708 (getwritebufferproc)array_buffer_getwritebuf,
1709 (getsegcountproc)array_buffer_getsegcount,
1710};
1711
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001712static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +00001713array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum778983b1993-02-19 15:55:02 +00001714{
1715 char c;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001716 PyObject *initial = NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001717 struct arraydescr *descr;
Martin v. Löwis99866332002-03-01 10:27:01 +00001718
1719 if (kwds != NULL) {
1720 int i = PyObject_Length(kwds);
1721 if (i < 0)
Guido van Rossum778983b1993-02-19 15:55:02 +00001722 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001723 else if (i > 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001724 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis99866332002-03-01 10:27:01 +00001725 "array.array constructor takes "
1726 "no keyword arguments");
Guido van Rossum778983b1993-02-19 15:55:02 +00001727 return NULL;
1728 }
1729 }
Martin v. Löwis99866332002-03-01 10:27:01 +00001730
1731 if (!PyArg_ParseTuple(args, "c|O:array", &c, &initial))
1732 return NULL;
1733
1734 if (!(initial == NULL || PyList_Check(initial)
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001735 || PyString_Check(initial) || PyTuple_Check(initial)
Martin v. Löwis99866332002-03-01 10:27:01 +00001736 || (c == 'u' && PyUnicode_Check(initial)))) {
1737 PyErr_SetString(PyExc_TypeError,
1738 "array initializer must be list or string");
1739 return NULL;
1740 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001741 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1742 if (descr->typecode == c) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001743 PyObject *a;
Guido van Rossum778983b1993-02-19 15:55:02 +00001744 int len;
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001745
1746 if (initial == NULL || !(PyList_Check(initial)
1747 || PyTuple_Check(initial)))
Guido van Rossum778983b1993-02-19 15:55:02 +00001748 len = 0;
1749 else
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001750 len = PySequence_Size(initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001751
1752 a = newarrayobject(type, len, descr);
Guido van Rossum778983b1993-02-19 15:55:02 +00001753 if (a == NULL)
1754 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001755
Guido van Rossum778983b1993-02-19 15:55:02 +00001756 if (len > 0) {
1757 int i;
1758 for (i = 0; i < len; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001759 PyObject *v =
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00001760 PySequence_GetItem(initial, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001761 if (setarrayitem(a, i, v) != 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001762 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001763 return NULL;
1764 }
1765 }
1766 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001767 if (initial != NULL && PyString_Check(initial)) {
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001768 PyObject *t_initial = Py_BuildValue("(O)",
1769 initial);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001770 PyObject *v =
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001771 array_fromstring((arrayobject *)a,
1772 t_initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001773 Py_DECREF(t_initial);
Guido van Rossum778983b1993-02-19 15:55:02 +00001774 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001775 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001776 return NULL;
1777 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001778 Py_DECREF(v);
Martin v. Löwis99866332002-03-01 10:27:01 +00001779#ifdef Py_USING_UNICODE
1780 } else if (initial != NULL && PyUnicode_Check(initial)) {
1781 int n = PyUnicode_GET_DATA_SIZE(initial);
1782 if (n > 0) {
1783 arrayobject *self = (arrayobject *)a;
1784 char *item = self->ob_item;
1785 item = PyMem_Realloc(item, n);
1786 if (item == NULL) {
1787 PyErr_NoMemory();
1788 Py_DECREF(a);
1789 return NULL;
1790 }
1791 self->ob_item = item;
1792 self->ob_size = n / sizeof(Py_UNICODE);
1793 memcpy(item, PyUnicode_AS_DATA(initial), n);
1794 }
1795#endif
Guido van Rossum778983b1993-02-19 15:55:02 +00001796 }
1797 return a;
1798 }
1799 }
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001800 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis99866332002-03-01 10:27:01 +00001801 "bad typecode (must be c, b, B, u, h, H, i, I, l, L, f or d)");
Guido van Rossum778983b1993-02-19 15:55:02 +00001802 return NULL;
1803}
1804
Guido van Rossum778983b1993-02-19 15:55:02 +00001805
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001806PyDoc_STRVAR(module_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001807"This module defines an object type which can efficiently represent\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001808an array of basic values: characters, integers, floating point\n\
1809numbers. Arrays are sequence types and behave very much like lists,\n\
1810except that the type of objects stored in them is constrained. The\n\
1811type is specified at object creation time by using a type code, which\n\
1812is a single character. The following type codes are defined:\n\
1813\n\
1814 Type code C Type Minimum size in bytes \n\
1815 'c' character 1 \n\
1816 'b' signed integer 1 \n\
1817 'B' unsigned integer 1 \n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001818 'u' Unicode character 2 \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001819 'h' signed integer 2 \n\
1820 'H' unsigned integer 2 \n\
1821 'i' signed integer 2 \n\
1822 'I' unsigned integer 2 \n\
1823 'l' signed integer 4 \n\
1824 'L' unsigned integer 4 \n\
1825 'f' floating point 4 \n\
1826 'd' floating point 8 \n\
1827\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001828The constructor is:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001829\n\
1830array(typecode [, initializer]) -- create a new array\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001831");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001832
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001833PyDoc_STRVAR(arraytype_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001834"array(typecode [, initializer]) -> array\n\
1835\n\
1836Return a new array whose items are restricted by typecode, and\n\
1837initialized from the optional initializer value, which must be a list\n\
1838or a string.\n\
1839\n\
1840Arrays represent basic values and behave very much like lists, except\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001841the type of objects stored in them is constrained.\n\
1842\n\
1843Methods:\n\
1844\n\
1845append() -- append a new item to the end of the array\n\
1846buffer_info() -- return information giving the current memory info\n\
1847byteswap() -- byteswap all the items of the array\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001848count() -- return number of occurences of an object\n\
1849extend() -- extend array by appending array elements\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001850fromfile() -- read items from a file object\n\
1851fromlist() -- append items from the list\n\
1852fromstring() -- append items from the string\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001853index() -- return index of first occurence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001854insert() -- insert a new item into the array at a provided position\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001855pop() -- remove and return item (default last)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001856read() -- DEPRECATED, use fromfile()\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001857remove() -- remove first occurence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001858reverse() -- reverse the order of the items in the array\n\
1859tofile() -- write all items to a file object\n\
1860tolist() -- return the array converted to an ordinary list\n\
1861tostring() -- return the array converted to a string\n\
1862write() -- DEPRECATED, use tofile()\n\
1863\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001864Attributes:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001865\n\
1866typecode -- the typecode character used to create the array\n\
1867itemsize -- the length in bytes of one array item\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001868");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001869
Raymond Hettinger625812f2003-01-07 01:58:52 +00001870static PyObject *array_iter(arrayobject *ao);
1871
Tim Peters0c322792002-07-17 16:49:03 +00001872static PyTypeObject Arraytype = {
Fred Drake0d40ba42000-02-04 20:33:49 +00001873 PyObject_HEAD_INIT(NULL)
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001874 0,
Guido van Rossum14648392001-12-08 18:02:58 +00001875 "array.array",
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001876 sizeof(arrayobject),
1877 0,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001878 (destructor)array_dealloc, /* tp_dealloc */
Martin v. Löwisaa158be2002-03-04 09:38:52 +00001879 0, /* tp_print */
Martin v. Löwis99866332002-03-01 10:27:01 +00001880 0, /* tp_getattr */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001881 0, /* tp_setattr */
1882 0, /* tp_compare */
1883 (reprfunc)array_repr, /* tp_repr */
1884 0, /* tp_as _number*/
1885 &array_as_sequence, /* tp_as _sequence*/
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001886 &array_as_mapping, /* tp_as _mapping*/
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001887 0, /* tp_hash */
1888 0, /* tp_call */
1889 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00001890 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001891 0, /* tp_setattro */
Tim Petersb870c752001-06-05 04:43:26 +00001892 &array_as_buffer, /* tp_as_buffer*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001893 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001894 arraytype_doc, /* tp_doc */
1895 0, /* tp_traverse */
1896 0, /* tp_clear */
1897 array_richcompare, /* tp_richcompare */
Martin v. Löwis99866332002-03-01 10:27:01 +00001898 0, /* tp_weaklistoffset */
Raymond Hettinger625812f2003-01-07 01:58:52 +00001899 (getiterfunc)array_iter, /* tp_iter */
Martin v. Löwis99866332002-03-01 10:27:01 +00001900 0, /* tp_iternext */
1901 array_methods, /* tp_methods */
1902 0, /* tp_members */
1903 array_getsets, /* tp_getset */
1904 0, /* tp_base */
1905 0, /* tp_dict */
1906 0, /* tp_descr_get */
1907 0, /* tp_descr_set */
1908 0, /* tp_dictoffset */
1909 0, /* tp_init */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00001910 PyType_GenericAlloc, /* tp_alloc */
Martin v. Löwis99866332002-03-01 10:27:01 +00001911 array_new, /* tp_new */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00001912 PyObject_Del, /* tp_free */
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001913};
1914
Raymond Hettinger625812f2003-01-07 01:58:52 +00001915
1916/*********************** Array Iterator **************************/
1917
1918typedef struct {
1919 PyObject_HEAD
1920 long index;
1921 arrayobject *ao;
1922 PyObject * (*getitem)(struct arrayobject *, int);
1923} arrayiterobject;
1924
1925static PyTypeObject PyArrayIter_Type;
1926
1927#define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
1928
1929static PyObject *
1930array_iter(arrayobject *ao)
1931{
1932 arrayiterobject *it;
1933
1934 if (!array_Check(ao)) {
1935 PyErr_BadInternalCall();
1936 return NULL;
1937 }
1938
1939 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
1940 if (it == NULL)
1941 return NULL;
1942
1943 Py_INCREF(ao);
1944 it->ao = ao;
1945 it->index = 0;
1946 it->getitem = ao->ob_descr->getitem;
1947 PyObject_GC_Track(it);
1948 return (PyObject *)it;
1949}
1950
1951static PyObject *
Raymond Hettinger625812f2003-01-07 01:58:52 +00001952arrayiter_next(arrayiterobject *it)
1953{
1954 assert(PyArrayIter_Check(it));
1955 if (it->index < it->ao->ob_size)
1956 return (*it->getitem)(it->ao, it->index++);
1957 return NULL;
1958}
1959
1960static void
1961arrayiter_dealloc(arrayiterobject *it)
1962{
1963 PyObject_GC_UnTrack(it);
1964 Py_XDECREF(it->ao);
1965 PyObject_GC_Del(it);
1966}
1967
1968static int
1969arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
1970{
1971 if (it->ao != NULL)
1972 return visit((PyObject *)(it->ao), arg);
1973 return 0;
1974}
1975
1976static PyTypeObject PyArrayIter_Type = {
Jason Tishler84375702003-02-10 20:45:47 +00001977 PyObject_HEAD_INIT(NULL)
Raymond Hettinger625812f2003-01-07 01:58:52 +00001978 0, /* ob_size */
1979 "arrayiterator", /* tp_name */
1980 sizeof(arrayiterobject), /* tp_basicsize */
1981 0, /* tp_itemsize */
1982 /* methods */
1983 (destructor)arrayiter_dealloc, /* tp_dealloc */
1984 0, /* tp_print */
1985 0, /* tp_getattr */
1986 0, /* tp_setattr */
1987 0, /* tp_compare */
1988 0, /* tp_repr */
1989 0, /* tp_as_number */
1990 0, /* tp_as_sequence */
1991 0, /* tp_as_mapping */
1992 0, /* tp_hash */
1993 0, /* tp_call */
1994 0, /* tp_str */
1995 PyObject_GenericGetAttr, /* tp_getattro */
1996 0, /* tp_setattro */
1997 0, /* tp_as_buffer */
1998 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1999 0, /* tp_doc */
2000 (traverseproc)arrayiter_traverse, /* tp_traverse */
2001 0, /* tp_clear */
2002 0, /* tp_richcompare */
2003 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002004 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger625812f2003-01-07 01:58:52 +00002005 (iternextfunc)arrayiter_next, /* tp_iternext */
2006 0, /* tp_methods */
2007};
2008
2009
2010/*********************** Install Module **************************/
2011
Martin v. Löwis99866332002-03-01 10:27:01 +00002012/* No functions in array module. */
2013static PyMethodDef a_methods[] = {
2014 {NULL, NULL, 0, NULL} /* Sentinel */
2015};
2016
2017
Mark Hammondfe51c6d2002-08-02 02:27:13 +00002018PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00002019initarray(void)
Guido van Rossum778983b1993-02-19 15:55:02 +00002020{
Fred Drakef4e34842002-04-01 03:45:06 +00002021 PyObject *m;
Fred Drake0d40ba42000-02-04 20:33:49 +00002022
Martin v. Löwis99866332002-03-01 10:27:01 +00002023 Arraytype.ob_type = &PyType_Type;
Jason Tishler84375702003-02-10 20:45:47 +00002024 PyArrayIter_Type.ob_type = &PyType_Type;
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002025 m = Py_InitModule3("array", a_methods, module_doc);
Fred Drakef4e34842002-04-01 03:45:06 +00002026
2027 Py_INCREF((PyObject *)&Arraytype);
2028 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
2029 Py_INCREF((PyObject *)&Arraytype);
2030 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
Guido van Rossuma0deb641998-10-14 13:45:06 +00002031 /* No need to check the error here, the caller will do that */
Guido van Rossum778983b1993-02-19 15:55:02 +00002032}