blob: e048d995a394b7331b3415330b281d2e61181703 [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
16struct arrayobject; /* Forward */
17
Tim Petersbb307342000-09-10 05:22:54 +000018/* All possible arraydescr values are defined in the vector "descriptors"
19 * below. That's defined later because the appropriate get and set
20 * functions aren't visible yet.
21 */
Guido van Rossum778983b1993-02-19 15:55:02 +000022struct arraydescr {
23 int typecode;
24 int itemsize;
Tim Petersdbd9ba62000-07-09 03:09:57 +000025 PyObject * (*getitem)(struct arrayobject *, int);
26 int (*setitem)(struct arrayobject *, int, PyObject *);
Guido van Rossum778983b1993-02-19 15:55:02 +000027};
28
29typedef struct arrayobject {
Martin v. Löwis99866332002-03-01 10:27:01 +000030 PyObject_HEAD
31 int ob_size;
Guido van Rossum778983b1993-02-19 15:55:02 +000032 char *ob_item;
33 struct arraydescr *ob_descr;
34} arrayobject;
35
Jeremy Hylton938ace62002-07-17 16:30:39 +000036static PyTypeObject Arraytype;
Guido van Rossum778983b1993-02-19 15:55:02 +000037
Martin v. Löwis99866332002-03-01 10:27:01 +000038#define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
39#define array_CheckExact(op) ((op)->ob_type == &Arraytype)
Guido van Rossum778983b1993-02-19 15:55:02 +000040
Tim Petersbb307342000-09-10 05:22:54 +000041/****************************************************************************
42Get and Set functions for each type.
43A Get function takes an arrayobject* and an integer index, returning the
44array value at that index wrapped in an appropriate PyObject*.
45A Set function takes an arrayobject, integer index, and PyObject*; sets
46the array value at that index to the raw C data extracted from the PyObject*,
47and returns 0 if successful, else nonzero on failure (PyObject* not of an
48appropriate type or value).
49Note that the basic Get and Set functions do NOT check that the index is
50in bounds; that's the responsibility of the caller.
51****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +000052
Roger E. Masse2919eaa1996-12-09 20:10:36 +000053static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +000054c_getitem(arrayobject *ap, int i)
Guido van Rossum778983b1993-02-19 15:55:02 +000055{
Roger E. Masse2919eaa1996-12-09 20:10:36 +000056 return PyString_FromStringAndSize(&((char *)ap->ob_item)[i], 1);
Guido van Rossum778983b1993-02-19 15:55:02 +000057}
58
59static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +000060c_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +000061{
62 char x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +000063 if (!PyArg_Parse(v, "c;array item must be char", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +000064 return -1;
65 if (i >= 0)
Martin v. Löwis99866332002-03-01 10:27:01 +000066 ((char *)ap->ob_item)[i] = x;
Guido van Rossum778983b1993-02-19 15:55:02 +000067 return 0;
68}
69
Roger E. Masse2919eaa1996-12-09 20:10:36 +000070static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +000071b_getitem(arrayobject *ap, int i)
Guido van Rossum778983b1993-02-19 15:55:02 +000072{
73 long x = ((char *)ap->ob_item)[i];
74 if (x >= 128)
75 x -= 256;
Roger E. Masse2919eaa1996-12-09 20:10:36 +000076 return PyInt_FromLong(x);
Guido van Rossum778983b1993-02-19 15:55:02 +000077}
78
79static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +000080b_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +000081{
Fred Drake541dc3b2000-06-28 17:49:30 +000082 short x;
83 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
84 must use the next size up that is signed ('h') and manually do
85 the overflow checking */
86 if (!PyArg_Parse(v, "h;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +000087 return -1;
Guido van Rossum9f754e02000-07-01 00:38:19 +000088 else if (x < -128) {
Fred Drake541dc3b2000-06-28 17:49:30 +000089 PyErr_SetString(PyExc_OverflowError,
90 "signed char is less than minimum");
91 return -1;
92 }
Guido van Rossum9f754e02000-07-01 00:38:19 +000093 else if (x > 127) {
Fred Drake541dc3b2000-06-28 17:49:30 +000094 PyErr_SetString(PyExc_OverflowError,
95 "signed char is greater than maximum");
96 return -1;
97 }
Guido van Rossum778983b1993-02-19 15:55:02 +000098 if (i >= 0)
Fred Drake541dc3b2000-06-28 17:49:30 +000099 ((char *)ap->ob_item)[i] = (char)x;
Guido van Rossum778983b1993-02-19 15:55:02 +0000100 return 0;
101}
102
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000103static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000104BB_getitem(arrayobject *ap, int i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000105{
106 long x = ((unsigned char *)ap->ob_item)[i];
107 return PyInt_FromLong(x);
108}
109
Fred Drake541dc3b2000-06-28 17:49:30 +0000110static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000111BB_setitem(arrayobject *ap, int i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000112{
113 unsigned char x;
114 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
115 if (!PyArg_Parse(v, "b;array item must be integer", &x))
116 return -1;
117 if (i >= 0)
Martin v. Löwis99866332002-03-01 10:27:01 +0000118 ((char *)ap->ob_item)[i] = x;
Fred Drake541dc3b2000-06-28 17:49:30 +0000119 return 0;
120}
Guido van Rossum549ab711997-01-03 19:09:47 +0000121
Martin v. Löwis99866332002-03-01 10:27:01 +0000122#ifdef Py_USING_UNICODE
123static PyObject *
124u_getitem(arrayobject *ap, int i)
125{
126 return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1);
127}
128
129static int
130u_setitem(arrayobject *ap, int i, PyObject *v)
131{
132 Py_UNICODE *p;
133 int len;
134
135 if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
136 return -1;
137 if (len != 1) {
138 PyErr_SetString(PyExc_TypeError, "array item must be unicode character");
139 return -1;
140 }
141 if (i >= 0)
142 ((Py_UNICODE *)ap->ob_item)[i] = p[0];
143 return 0;
144}
145#endif
146
Guido van Rossum549ab711997-01-03 19:09:47 +0000147static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000148h_getitem(arrayobject *ap, int i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000149{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000150 return PyInt_FromLong((long) ((short *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000151}
152
153static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000154h_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000155{
156 short x;
Fred Drake541dc3b2000-06-28 17:49:30 +0000157 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000158 if (!PyArg_Parse(v, "h;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000159 return -1;
160 if (i >= 0)
161 ((short *)ap->ob_item)[i] = x;
162 return 0;
163}
164
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000165static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000166HH_getitem(arrayobject *ap, int i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000167{
168 return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
169}
170
Fred Drake541dc3b2000-06-28 17:49:30 +0000171static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000172HH_setitem(arrayobject *ap, int i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000173{
174 int x;
175 /* PyArg_Parse's 'h' formatter is for a signed short, therefore
176 must use the next size up and manually do the overflow checking */
177 if (!PyArg_Parse(v, "i;array item must be integer", &x))
178 return -1;
179 else if (x < 0) {
180 PyErr_SetString(PyExc_OverflowError,
181 "unsigned short is less than minimum");
182 return -1;
183 }
184 else if (x > USHRT_MAX) {
185 PyErr_SetString(PyExc_OverflowError,
186 "unsigned short is greater than maximum");
187 return -1;
188 }
189 if (i >= 0)
190 ((short *)ap->ob_item)[i] = (short)x;
191 return 0;
192}
Guido van Rossum549ab711997-01-03 19:09:47 +0000193
194static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000195i_getitem(arrayobject *ap, int i)
Guido van Rossume77a7571993-11-03 15:01:26 +0000196{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000197 return PyInt_FromLong((long) ((int *)ap->ob_item)[i]);
Guido van Rossume77a7571993-11-03 15:01:26 +0000198}
199
200static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000201i_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossume77a7571993-11-03 15:01:26 +0000202{
203 int x;
Fred Drake541dc3b2000-06-28 17:49:30 +0000204 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000205 if (!PyArg_Parse(v, "i;array item must be integer", &x))
Guido van Rossume77a7571993-11-03 15:01:26 +0000206 return -1;
207 if (i >= 0)
208 ((int *)ap->ob_item)[i] = x;
209 return 0;
210}
211
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000212static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000213II_getitem(arrayobject *ap, int i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000214{
215 return PyLong_FromUnsignedLong(
216 (unsigned long) ((unsigned int *)ap->ob_item)[i]);
217}
218
219static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000220II_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000221{
222 unsigned long x;
223 if (PyLong_Check(v)) {
224 x = PyLong_AsUnsignedLong(v);
225 if (x == (unsigned long) -1 && PyErr_Occurred())
226 return -1;
227 }
228 else {
Fred Drake541dc3b2000-06-28 17:49:30 +0000229 long y;
230 if (!PyArg_Parse(v, "l;array item must be integer", &y))
Guido van Rossum549ab711997-01-03 19:09:47 +0000231 return -1;
Fred Drake541dc3b2000-06-28 17:49:30 +0000232 if (y < 0) {
233 PyErr_SetString(PyExc_OverflowError,
234 "unsigned int is less than minimum");
235 return -1;
236 }
237 x = (unsigned long)y;
Tim Petersbb307342000-09-10 05:22:54 +0000238
Guido van Rossum549ab711997-01-03 19:09:47 +0000239 }
Fred Drake541dc3b2000-06-28 17:49:30 +0000240 if (x > UINT_MAX) {
241 PyErr_SetString(PyExc_OverflowError,
242 "unsigned int is greater than maximum");
243 return -1;
244 }
245
Guido van Rossum549ab711997-01-03 19:09:47 +0000246 if (i >= 0)
Fred Drake541dc3b2000-06-28 17:49:30 +0000247 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
Guido van Rossum549ab711997-01-03 19:09:47 +0000248 return 0;
249}
250
251static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000252l_getitem(arrayobject *ap, int i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000253{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000254 return PyInt_FromLong(((long *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000255}
256
257static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000258l_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000259{
260 long x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000261 if (!PyArg_Parse(v, "l;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000262 return -1;
263 if (i >= 0)
264 ((long *)ap->ob_item)[i] = x;
265 return 0;
266}
267
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000268static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000269LL_getitem(arrayobject *ap, int i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000270{
271 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
272}
273
274static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000275LL_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000276{
277 unsigned long x;
278 if (PyLong_Check(v)) {
279 x = PyLong_AsUnsignedLong(v);
280 if (x == (unsigned long) -1 && PyErr_Occurred())
281 return -1;
282 }
283 else {
Fred Drake541dc3b2000-06-28 17:49:30 +0000284 long y;
285 if (!PyArg_Parse(v, "l;array item must be integer", &y))
Guido van Rossum549ab711997-01-03 19:09:47 +0000286 return -1;
Fred Drake541dc3b2000-06-28 17:49:30 +0000287 if (y < 0) {
288 PyErr_SetString(PyExc_OverflowError,
289 "unsigned long is less than minimum");
290 return -1;
291 }
292 x = (unsigned long)y;
Tim Petersbb307342000-09-10 05:22:54 +0000293
Guido van Rossum549ab711997-01-03 19:09:47 +0000294 }
Fred Drake541dc3b2000-06-28 17:49:30 +0000295 if (x > ULONG_MAX) {
296 PyErr_SetString(PyExc_OverflowError,
297 "unsigned long is greater than maximum");
298 return -1;
299 }
Tim Petersbb307342000-09-10 05:22:54 +0000300
Guido van Rossum549ab711997-01-03 19:09:47 +0000301 if (i >= 0)
302 ((unsigned long *)ap->ob_item)[i] = x;
303 return 0;
304}
305
306static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000307f_getitem(arrayobject *ap, int i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000308{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000309 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000310}
311
312static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000313f_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000314{
315 float x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000316 if (!PyArg_Parse(v, "f;array item must be float", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000317 return -1;
318 if (i >= 0)
319 ((float *)ap->ob_item)[i] = x;
320 return 0;
321}
322
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000323static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000324d_getitem(arrayobject *ap, int i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000325{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000326 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000327}
328
329static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000330d_setitem(arrayobject *ap, int i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000331{
332 double x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000333 if (!PyArg_Parse(v, "d;array item must be float", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000334 return -1;
335 if (i >= 0)
336 ((double *)ap->ob_item)[i] = x;
337 return 0;
338}
339
340/* Description of types */
Guido van Rossum234f9421993-06-17 12:35:49 +0000341static struct arraydescr descriptors[] = {
Guido van Rossum778983b1993-02-19 15:55:02 +0000342 {'c', sizeof(char), c_getitem, c_setitem},
343 {'b', sizeof(char), b_getitem, b_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000344 {'B', sizeof(char), BB_getitem, BB_setitem},
Martin v. Löwis99866332002-03-01 10:27:01 +0000345#ifdef Py_USING_UNICODE
346 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem},
347#endif
Guido van Rossum778983b1993-02-19 15:55:02 +0000348 {'h', sizeof(short), h_getitem, h_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000349 {'H', sizeof(short), HH_getitem, HH_setitem},
Guido van Rossume77a7571993-11-03 15:01:26 +0000350 {'i', sizeof(int), i_getitem, i_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000351 {'I', sizeof(int), II_getitem, II_setitem},
Guido van Rossum778983b1993-02-19 15:55:02 +0000352 {'l', sizeof(long), l_getitem, l_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000353 {'L', sizeof(long), LL_getitem, LL_setitem},
Guido van Rossum778983b1993-02-19 15:55:02 +0000354 {'f', sizeof(float), f_getitem, f_setitem},
355 {'d', sizeof(double), d_getitem, d_setitem},
356 {'\0', 0, 0, 0} /* Sentinel */
357};
Tim Petersbb307342000-09-10 05:22:54 +0000358
359/****************************************************************************
360Implementations of array object methods.
361****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +0000362
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000363static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +0000364newarrayobject(PyTypeObject *type, int size, struct arraydescr *descr)
Guido van Rossum778983b1993-02-19 15:55:02 +0000365{
Guido van Rossum778983b1993-02-19 15:55:02 +0000366 arrayobject *op;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000367 size_t nbytes;
Martin v. Löwis99866332002-03-01 10:27:01 +0000368
Guido van Rossum778983b1993-02-19 15:55:02 +0000369 if (size < 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000370 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000371 return NULL;
372 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000373
Guido van Rossum778983b1993-02-19 15:55:02 +0000374 nbytes = size * descr->itemsize;
375 /* Check for overflow */
Guido van Rossum7844e381997-04-11 20:44:04 +0000376 if (nbytes / descr->itemsize != (size_t)size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000377 return PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000378 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000379 op = (arrayobject *) type->tp_alloc(type, 0);
Guido van Rossum778983b1993-02-19 15:55:02 +0000380 if (op == NULL) {
Martin v. Löwis99866332002-03-01 10:27:01 +0000381 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +0000382 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000383 op->ob_size = size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000384 if (size <= 0) {
385 op->ob_item = NULL;
386 }
387 else {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000388 op->ob_item = PyMem_NEW(char, nbytes);
Guido van Rossum778983b1993-02-19 15:55:02 +0000389 if (op->ob_item == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000390 PyObject_Del(op);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000391 return PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000392 }
393 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000394 op->ob_descr = descr;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000395 return (PyObject *) op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000396}
397
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000398static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000399getarrayitem(PyObject *op, int i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000400{
401 register arrayobject *ap;
Martin v. Löwis99866332002-03-01 10:27:01 +0000402 assert(array_Check(op));
Guido van Rossum778983b1993-02-19 15:55:02 +0000403 ap = (arrayobject *)op;
404 if (i < 0 || i >= ap->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000405 PyErr_SetString(PyExc_IndexError, "array index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000406 return NULL;
407 }
408 return (*ap->ob_descr->getitem)(ap, i);
409}
410
411static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000412ins1(arrayobject *self, int where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000413{
Guido van Rossum778983b1993-02-19 15:55:02 +0000414 char *items;
415 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000416 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000417 return -1;
418 }
419 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
420 return -1;
421 items = self->ob_item;
Roger E. Masse5817f8f1996-12-09 22:24:19 +0000422 PyMem_RESIZE(items, char,
423 (self->ob_size+1) * self->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000424 if (items == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000425 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000426 return -1;
427 }
428 if (where < 0)
429 where = 0;
430 if (where > self->ob_size)
431 where = self->ob_size;
432 memmove(items + (where+1)*self->ob_descr->itemsize,
433 items + where*self->ob_descr->itemsize,
434 (self->ob_size-where)*self->ob_descr->itemsize);
435 self->ob_item = items;
436 self->ob_size++;
437 return (*self->ob_descr->setitem)(self, where, v);
438}
439
Guido van Rossum778983b1993-02-19 15:55:02 +0000440/* Methods */
441
442static void
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000443array_dealloc(arrayobject *op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000444{
Guido van Rossum778983b1993-02-19 15:55:02 +0000445 if (op->ob_item != NULL)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000446 PyMem_DEL(op->ob_item);
Martin v. Löwis99866332002-03-01 10:27:01 +0000447 op->ob_type->tp_free((PyObject *)op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000448}
449
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000450static PyObject *
451array_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000452{
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000453 arrayobject *va, *wa;
454 PyObject *vi = NULL;
455 PyObject *wi = NULL;
456 int i, k;
457 PyObject *res;
458
Martin v. Löwis99866332002-03-01 10:27:01 +0000459 if (!array_Check(v) || !array_Check(w)) {
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000460 Py_INCREF(Py_NotImplemented);
461 return Py_NotImplemented;
Guido van Rossum778983b1993-02-19 15:55:02 +0000462 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000463
464 va = (arrayobject *)v;
465 wa = (arrayobject *)w;
466
467 if (va->ob_size != wa->ob_size && (op == Py_EQ || op == Py_NE)) {
468 /* Shortcut: if the lengths differ, the arrays differ */
469 if (op == Py_EQ)
470 res = Py_False;
471 else
472 res = Py_True;
473 Py_INCREF(res);
474 return res;
475 }
476
477 /* Search for the first index where items are different */
478 k = 1;
479 for (i = 0; i < va->ob_size && i < wa->ob_size; i++) {
480 vi = getarrayitem(v, i);
481 wi = getarrayitem(w, i);
482 if (vi == NULL || wi == NULL) {
483 Py_XDECREF(vi);
484 Py_XDECREF(wi);
485 return NULL;
486 }
487 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
488 if (k == 0)
489 break; /* Keeping vi and wi alive! */
490 Py_DECREF(vi);
491 Py_DECREF(wi);
492 if (k < 0)
493 return NULL;
494 }
495
496 if (k) {
497 /* No more items to compare -- compare sizes */
498 int vs = va->ob_size;
499 int ws = wa->ob_size;
500 int cmp;
501 switch (op) {
502 case Py_LT: cmp = vs < ws; break;
Guido van Rossum2b597e42001-01-25 22:12:43 +0000503 case Py_LE: cmp = vs <= ws; break;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000504 case Py_EQ: cmp = vs == ws; break;
505 case Py_NE: cmp = vs != ws; break;
506 case Py_GT: cmp = vs > ws; break;
507 case Py_GE: cmp = vs >= ws; break;
508 default: return NULL; /* cannot happen */
509 }
510 if (cmp)
511 res = Py_True;
512 else
513 res = Py_False;
514 Py_INCREF(res);
515 return res;
516 }
517
518 /* We have an item that differs. First, shortcuts for EQ/NE */
519 if (op == Py_EQ) {
520 Py_INCREF(Py_False);
521 res = Py_False;
522 }
523 else if (op == Py_NE) {
524 Py_INCREF(Py_True);
525 res = Py_True;
526 }
527 else {
528 /* Compare the final item again using the proper operator */
529 res = PyObject_RichCompare(vi, wi, op);
530 }
531 Py_DECREF(vi);
532 Py_DECREF(wi);
533 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +0000534}
535
536static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000537array_length(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +0000538{
539 return a->ob_size;
540}
541
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000542static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000543array_item(arrayobject *a, int i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000544{
545 if (i < 0 || i >= a->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000546 PyErr_SetString(PyExc_IndexError, "array index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000547 return NULL;
548 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000549 return getarrayitem((PyObject *)a, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000550}
551
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000552static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000553array_slice(arrayobject *a, int ilow, int ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000554{
555 arrayobject *np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000556 if (ilow < 0)
557 ilow = 0;
558 else if (ilow > a->ob_size)
559 ilow = a->ob_size;
560 if (ihigh < 0)
561 ihigh = 0;
562 if (ihigh < ilow)
563 ihigh = ilow;
564 else if (ihigh > a->ob_size)
565 ihigh = a->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000566 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
Guido van Rossum778983b1993-02-19 15:55:02 +0000567 if (np == NULL)
568 return NULL;
569 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
570 (ihigh-ilow) * a->ob_descr->itemsize);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000571 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000572}
573
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000574static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000575array_concat(arrayobject *a, PyObject *bb)
Guido van Rossum778983b1993-02-19 15:55:02 +0000576{
577 int size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000578 arrayobject *np;
Martin v. Löwis99866332002-03-01 10:27:01 +0000579 if (!array_Check(bb)) {
Fred Drake137507e2000-06-01 02:02:46 +0000580 PyErr_Format(PyExc_TypeError,
581 "can only append array (not \"%.200s\") to array",
582 bb->ob_type->tp_name);
Guido van Rossum778983b1993-02-19 15:55:02 +0000583 return NULL;
584 }
585#define b ((arrayobject *)bb)
586 if (a->ob_descr != b->ob_descr) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000587 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000588 return NULL;
589 }
590 size = a->ob_size + b->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +0000591 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
Guido van Rossum778983b1993-02-19 15:55:02 +0000592 if (np == NULL) {
593 return NULL;
594 }
595 memcpy(np->ob_item, a->ob_item, a->ob_size*a->ob_descr->itemsize);
596 memcpy(np->ob_item + a->ob_size*a->ob_descr->itemsize,
Guido van Rossum32be3a71993-11-05 10:16:27 +0000597 b->ob_item, b->ob_size*b->ob_descr->itemsize);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000598 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000599#undef b
600}
601
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000602static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000603array_repeat(arrayobject *a, int n)
Guido van Rossum778983b1993-02-19 15:55:02 +0000604{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000605 int i;
Guido van Rossum778983b1993-02-19 15:55:02 +0000606 int size;
607 arrayobject *np;
608 char *p;
609 int nbytes;
610 if (n < 0)
611 n = 0;
612 size = a->ob_size * n;
Martin v. Löwis99866332002-03-01 10:27:01 +0000613 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
Guido van Rossum778983b1993-02-19 15:55:02 +0000614 if (np == NULL)
615 return NULL;
616 p = np->ob_item;
617 nbytes = a->ob_size * a->ob_descr->itemsize;
618 for (i = 0; i < n; i++) {
619 memcpy(p, a->ob_item, nbytes);
620 p += nbytes;
621 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000622 return (PyObject *) np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000623}
624
625static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000626array_ass_slice(arrayobject *a, int ilow, int ihigh, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000627{
628 char *item;
629 int n; /* Size of replacement array */
630 int d; /* Change in size */
Guido van Rossum778983b1993-02-19 15:55:02 +0000631#define b ((arrayobject *)v)
632 if (v == NULL)
633 n = 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000634 else if (array_Check(v)) {
Guido van Rossum778983b1993-02-19 15:55:02 +0000635 n = b->ob_size;
636 if (a == b) {
637 /* Special case "a[i:j] = a" -- copy b first */
638 int ret;
639 v = array_slice(b, 0, n);
640 ret = array_ass_slice(a, ilow, ihigh, v);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000641 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000642 return ret;
643 }
644 if (b->ob_descr != a->ob_descr) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000645 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000646 return -1;
647 }
648 }
649 else {
Fred Drake137507e2000-06-01 02:02:46 +0000650 PyErr_Format(PyExc_TypeError,
651 "can only assign array (not \"%.200s\") to array slice",
652 v->ob_type->tp_name);
Guido van Rossum778983b1993-02-19 15:55:02 +0000653 return -1;
654 }
655 if (ilow < 0)
656 ilow = 0;
657 else if (ilow > a->ob_size)
658 ilow = a->ob_size;
659 if (ihigh < 0)
660 ihigh = 0;
661 if (ihigh < ilow)
662 ihigh = ilow;
663 else if (ihigh > a->ob_size)
664 ihigh = a->ob_size;
665 item = a->ob_item;
666 d = n - (ihigh-ilow);
667 if (d < 0) { /* Delete -d items */
668 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
669 item + ihigh*a->ob_descr->itemsize,
670 (a->ob_size-ihigh)*a->ob_descr->itemsize);
671 a->ob_size += d;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000672 PyMem_RESIZE(item, char, a->ob_size*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000673 /* Can't fail */
674 a->ob_item = item;
675 }
676 else if (d > 0) { /* Insert d items */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000677 PyMem_RESIZE(item, char,
678 (a->ob_size + d)*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000679 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000680 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000681 return -1;
682 }
683 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
684 item + ihigh*a->ob_descr->itemsize,
685 (a->ob_size-ihigh)*a->ob_descr->itemsize);
686 a->ob_item = item;
687 a->ob_size += d;
688 }
689 if (n > 0)
690 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
691 n*b->ob_descr->itemsize);
692 return 0;
693#undef b
694}
695
696static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000697array_ass_item(arrayobject *a, int i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000698{
699 if (i < 0 || i >= a->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000700 PyErr_SetString(PyExc_IndexError,
701 "array assignment index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000702 return -1;
703 }
704 if (v == NULL)
705 return array_ass_slice(a, i, i+1, v);
706 return (*a->ob_descr->setitem)(a, i, v);
707}
708
709static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000710setarrayitem(PyObject *a, int i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000711{
Martin v. Löwis99866332002-03-01 10:27:01 +0000712 assert(array_Check(a));
Guido van Rossum778983b1993-02-19 15:55:02 +0000713 return array_ass_item((arrayobject *)a, i, v);
714}
715
Martin v. Löwis99866332002-03-01 10:27:01 +0000716static int
717array_do_extend(arrayobject *self, PyObject *bb)
718{
719 int size;
720
721 if (!array_Check(bb)) {
722 PyErr_Format(PyExc_TypeError,
723 "can only extend array with array (not \"%.200s\")",
724 bb->ob_type->tp_name);
725 return -1;
726 }
727#define b ((arrayobject *)bb)
728 if (self->ob_descr != b->ob_descr) {
729 PyErr_SetString(PyExc_TypeError,
730 "can only extend with array of same kind");
731 return -1;
732 }
733 size = self->ob_size + b->ob_size;
734 PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize);
735 if (self->ob_item == NULL) {
736 PyObject_Del(self);
737 PyErr_NoMemory();
738 return -1;
739 }
740 memcpy(self->ob_item + self->ob_size*self->ob_descr->itemsize,
741 b->ob_item, b->ob_size*b->ob_descr->itemsize);
742 self->ob_size = size;
743
744 return 0;
745#undef b
746}
747
748static PyObject *
749array_inplace_concat(arrayobject *self, PyObject *bb)
750{
751 if (array_do_extend(self, bb) == -1)
752 return NULL;
753 Py_INCREF(self);
754 return (PyObject *)self;
755}
756
757static PyObject *
758array_inplace_repeat(arrayobject *self, int n)
759{
760 char *items, *p;
761 int size, i;
762
763 if (self->ob_size > 0) {
764 if (n < 0)
765 n = 0;
766 items = self->ob_item;
767 size = self->ob_size * self->ob_descr->itemsize;
768 if (n == 0) {
769 PyMem_FREE(items);
770 self->ob_item = NULL;
771 self->ob_size = 0;
772 }
773 else {
774 PyMem_Resize(items, char, n * size);
775 if (items == NULL)
776 return PyErr_NoMemory();
777 p = items;
778 for (i = 1; i < n; i++) {
779 p += size;
780 memcpy(p, items, size);
781 }
782 self->ob_item = items;
783 self->ob_size *= n;
784 }
785 }
786 Py_INCREF(self);
787 return (PyObject *)self;
788}
789
790
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000791static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000792ins(arrayobject *self, int where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000793{
794 if (ins1(self, where, v) != 0)
795 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000796 Py_INCREF(Py_None);
797 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000798}
799
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000800static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000801array_count(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000802{
803 int count = 0;
804 int i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000805
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000806 for (i = 0; i < self->ob_size; i++) {
807 PyObject *selfi = getarrayitem((PyObject *)self, i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000808 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000809 Py_DECREF(selfi);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000810 if (cmp > 0)
811 count++;
812 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000813 return NULL;
814 }
815 return PyInt_FromLong((long)count);
816}
817
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000818PyDoc_STRVAR(count_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000819"count(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000820\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000821Return number of occurences of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000822
823static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000824array_index(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000825{
826 int i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000827
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000828 for (i = 0; i < self->ob_size; i++) {
829 PyObject *selfi = getarrayitem((PyObject *)self, i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000830 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
831 Py_DECREF(selfi);
832 if (cmp > 0) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000833 return PyInt_FromLong((long)i);
834 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000835 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000836 return NULL;
837 }
838 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
839 return NULL;
840}
841
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000842PyDoc_STRVAR(index_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000843"index(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000844\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000845Return index of first occurence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000846
Raymond Hettinger625812f2003-01-07 01:58:52 +0000847static int
848array_contains(arrayobject *self, PyObject *v)
849{
850 int i, cmp;
851
852 for (i = 0, cmp = 0 ; cmp == 0 && i < self->ob_size; i++) {
853 PyObject *selfi = getarrayitem((PyObject *)self, i);
854 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
855 Py_DECREF(selfi);
856 }
857 return cmp;
858}
859
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000860static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000861array_remove(arrayobject *self, PyObject *v)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000862{
863 int i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000864
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000865 for (i = 0; i < self->ob_size; i++) {
866 PyObject *selfi = getarrayitem((PyObject *)self,i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000867 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
868 Py_DECREF(selfi);
869 if (cmp > 0) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000870 if (array_ass_slice(self, i, i+1,
871 (PyObject *)NULL) != 0)
872 return NULL;
873 Py_INCREF(Py_None);
874 return Py_None;
875 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000876 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000877 return NULL;
878 }
879 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
880 return NULL;
881}
882
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000883PyDoc_STRVAR(remove_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000884"remove(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000885\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000886Remove the first occurence of x in the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000887
888static PyObject *
889array_pop(arrayobject *self, PyObject *args)
890{
891 int i = -1;
892 PyObject *v;
893 if (!PyArg_ParseTuple(args, "|i:pop", &i))
894 return NULL;
895 if (self->ob_size == 0) {
896 /* Special-case most common failure cause */
897 PyErr_SetString(PyExc_IndexError, "pop from empty array");
898 return NULL;
899 }
900 if (i < 0)
901 i += self->ob_size;
902 if (i < 0 || i >= self->ob_size) {
903 PyErr_SetString(PyExc_IndexError, "pop index out of range");
904 return NULL;
905 }
906 v = getarrayitem((PyObject *)self,i);
907 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
908 Py_DECREF(v);
909 return NULL;
910 }
911 return v;
912}
913
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000914PyDoc_STRVAR(pop_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000915"pop([i])\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000916\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000917Return the i-th element and delete it from the array. i defaults to -1.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000918
919static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000920array_extend(arrayobject *self, PyObject *bb)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000921{
Martin v. Löwis99866332002-03-01 10:27:01 +0000922 if (array_do_extend(self, bb) == -1)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000923 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +0000924 Py_INCREF(Py_None);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000925 return Py_None;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000926}
927
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000928PyDoc_STRVAR(extend_doc,
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000929"extend(array)\n\
930\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000931 Append array items to the end of the array.");
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000932
933static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000934array_insert(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +0000935{
936 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000937 PyObject *v;
Peter Schneider-Kampdc71cac2000-07-31 21:57:30 +0000938 if (!PyArg_ParseTuple(args, "iO:insert", &i, &v))
Guido van Rossum778983b1993-02-19 15:55:02 +0000939 return NULL;
940 return ins(self, i, v);
941}
942
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000943PyDoc_STRVAR(insert_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000944"insert(i,x)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000945\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000946Insert a new item x into the array before position i.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000947
948
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000949static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000950array_buffer_info(arrayobject *self, PyObject *unused)
Guido van Rossumde4a4ca1997-08-12 14:55:56 +0000951{
Tim Peters077a11d2000-09-16 22:31:29 +0000952 PyObject* retval = NULL;
Tim Peters077a11d2000-09-16 22:31:29 +0000953 retval = PyTuple_New(2);
954 if (!retval)
955 return NULL;
Fred Drake541dc3b2000-06-28 17:49:30 +0000956
957 PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item));
958 PyTuple_SET_ITEM(retval, 1, PyInt_FromLong((long)(self->ob_size)));
959
960 return retval;
Guido van Rossumde4a4ca1997-08-12 14:55:56 +0000961}
962
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000963PyDoc_STRVAR(buffer_info_doc,
Tim Peters077a11d2000-09-16 22:31:29 +0000964"buffer_info() -> (address, length)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000965\n\
966Return a tuple (address, length) giving the current memory address and\n\
Guido van Rossum702d08e2001-07-27 16:05:32 +0000967the length in items of the buffer used to hold array's contents\n\
968The length should be multiplied by the itemsize attribute to calculate\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000969the buffer length in bytes.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000970
971
Guido van Rossumde4a4ca1997-08-12 14:55:56 +0000972static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000973array_append(arrayobject *self, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000974{
Guido van Rossum778983b1993-02-19 15:55:02 +0000975 return ins(self, (int) self->ob_size, v);
976}
977
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000978PyDoc_STRVAR(append_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000979"append(x)\n\
980\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000981Append new value x to the end of the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000982
983
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000984static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000985array_byteswap(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +0000986{
987 char *p;
988 int i;
Fred Drakebf272981999-12-03 17:15:30 +0000989
Guido van Rossum778983b1993-02-19 15:55:02 +0000990 switch (self->ob_descr->itemsize) {
991 case 1:
992 break;
993 case 2:
994 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 2) {
995 char p0 = p[0];
996 p[0] = p[1];
997 p[1] = p0;
998 }
999 break;
1000 case 4:
1001 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 4) {
1002 char p0 = p[0];
1003 char p1 = p[1];
1004 p[0] = p[3];
1005 p[1] = p[2];
1006 p[2] = p1;
1007 p[3] = p0;
1008 }
1009 break;
Guido van Rossume77a7571993-11-03 15:01:26 +00001010 case 8:
1011 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 8) {
1012 char p0 = p[0];
1013 char p1 = p[1];
1014 char p2 = p[2];
1015 char p3 = p[3];
1016 p[0] = p[7];
1017 p[1] = p[6];
1018 p[2] = p[5];
1019 p[3] = p[4];
1020 p[4] = p3;
1021 p[5] = p2;
1022 p[6] = p1;
1023 p[7] = p0;
1024 }
1025 break;
Guido van Rossum778983b1993-02-19 15:55:02 +00001026 default:
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001027 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossum778983b1993-02-19 15:55:02 +00001028 "don't know how to byteswap this array type");
1029 return NULL;
1030 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001031 Py_INCREF(Py_None);
1032 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001033}
1034
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001035PyDoc_STRVAR(byteswap_doc,
Fred Drakebf272981999-12-03 17:15:30 +00001036"byteswap()\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001037\n\
Fred Drakebf272981999-12-03 17:15:30 +00001038Byteswap 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 +000010394, or 8 bytes in size, RuntimeError is raised.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001040
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001041static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001042array_reverse(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001043{
Guido van Rossume77a7571993-11-03 15:01:26 +00001044 register int itemsize = self->ob_descr->itemsize;
1045 register char *p, *q;
Tim Peters077a11d2000-09-16 22:31:29 +00001046 /* little buffer to hold items while swapping */
1047 char tmp[256]; /* 8 is probably enough -- but why skimp */
1048 assert(itemsize <= sizeof(tmp));
Guido van Rossume77a7571993-11-03 15:01:26 +00001049
Guido van Rossum778983b1993-02-19 15:55:02 +00001050 if (self->ob_size > 1) {
Guido van Rossume77a7571993-11-03 15:01:26 +00001051 for (p = self->ob_item,
1052 q = self->ob_item + (self->ob_size - 1)*itemsize;
1053 p < q;
1054 p += itemsize, q -= itemsize) {
Tim Peters077a11d2000-09-16 22:31:29 +00001055 /* memory areas guaranteed disjoint, so memcpy
1056 * is safe (& memmove may be slower).
1057 */
1058 memcpy(tmp, p, itemsize);
1059 memcpy(p, q, itemsize);
1060 memcpy(q, tmp, itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001061 }
1062 }
Tim Petersbb307342000-09-10 05:22:54 +00001063
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001064 Py_INCREF(Py_None);
1065 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001066}
Guido van Rossume77a7571993-11-03 15:01:26 +00001067
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001068PyDoc_STRVAR(reverse_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001069"reverse()\n\
1070\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001071Reverse the order of the items in the array.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001072
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001073static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001074array_fromfile(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001075{
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001076 PyObject *f;
Guido van Rossum778983b1993-02-19 15:55:02 +00001077 int n;
1078 FILE *fp;
Peter Schneider-Kampdc71cac2000-07-31 21:57:30 +00001079 if (!PyArg_ParseTuple(args, "Oi:fromfile", &f, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +00001080 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001081 fp = PyFile_AsFile(f);
Guido van Rossum778983b1993-02-19 15:55:02 +00001082 if (fp == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001083 PyErr_SetString(PyExc_TypeError, "arg1 must be open file");
Guido van Rossum778983b1993-02-19 15:55:02 +00001084 return NULL;
1085 }
1086 if (n > 0) {
1087 char *item = self->ob_item;
1088 int itemsize = self->ob_descr->itemsize;
Guido van Rossum7d0ae5e2000-06-28 21:27:21 +00001089 size_t nread;
Guido van Rossum3791b0d1999-02-23 18:05:22 +00001090 int newlength;
1091 size_t newbytes;
1092 /* Be careful here about overflow */
1093 if ((newlength = self->ob_size + n) <= 0 ||
Guido van Rossum481ac881999-03-19 21:50:11 +00001094 (newbytes = newlength * itemsize) / itemsize !=
1095 (size_t)newlength)
Guido van Rossum3791b0d1999-02-23 18:05:22 +00001096 goto nomem;
1097 PyMem_RESIZE(item, char, newbytes);
Guido van Rossum778983b1993-02-19 15:55:02 +00001098 if (item == NULL) {
Guido van Rossum3791b0d1999-02-23 18:05:22 +00001099 nomem:
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001100 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001101 return NULL;
1102 }
1103 self->ob_item = item;
1104 self->ob_size += n;
1105 nread = fread(item + (self->ob_size - n) * itemsize,
1106 itemsize, n, fp);
Guido van Rossum7d0ae5e2000-06-28 21:27:21 +00001107 if (nread < (size_t)n) {
Guido van Rossum778983b1993-02-19 15:55:02 +00001108 self->ob_size -= (n - nread);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001109 PyMem_RESIZE(item, char, self->ob_size*itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001110 self->ob_item = item;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001111 PyErr_SetString(PyExc_EOFError,
1112 "not enough items in file");
Guido van Rossum778983b1993-02-19 15:55:02 +00001113 return NULL;
1114 }
1115 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001116 Py_INCREF(Py_None);
1117 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001118}
1119
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001120PyDoc_STRVAR(fromfile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001121"fromfile(f, n)\n\
1122\n\
1123Read 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 +00001124array. Also called as read.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001125
1126
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001127static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001128array_tofile(arrayobject *self, PyObject *f)
Guido van Rossum778983b1993-02-19 15:55:02 +00001129{
Guido van Rossum778983b1993-02-19 15:55:02 +00001130 FILE *fp;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001131
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001132 fp = PyFile_AsFile(f);
Guido van Rossum778983b1993-02-19 15:55:02 +00001133 if (fp == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001134 PyErr_SetString(PyExc_TypeError, "arg must be open file");
Guido van Rossum778983b1993-02-19 15:55:02 +00001135 return NULL;
1136 }
1137 if (self->ob_size > 0) {
Trent Mick6c116dd2000-08-12 20:58:11 +00001138 if (fwrite(self->ob_item, self->ob_descr->itemsize,
1139 self->ob_size, fp) != (size_t)self->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001140 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum778983b1993-02-19 15:55:02 +00001141 clearerr(fp);
1142 return NULL;
1143 }
1144 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001145 Py_INCREF(Py_None);
1146 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001147}
1148
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001149PyDoc_STRVAR(tofile_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001150"tofile(f)\n\
1151\n\
1152Write all items (as machine values) to the file object f. Also called as\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001153write.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001154
1155
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001156static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001157array_fromlist(arrayobject *self, PyObject *list)
Guido van Rossum778983b1993-02-19 15:55:02 +00001158{
1159 int n;
Guido van Rossum778983b1993-02-19 15:55:02 +00001160 int itemsize = self->ob_descr->itemsize;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001161
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001162 if (!PyList_Check(list)) {
1163 PyErr_SetString(PyExc_TypeError, "arg must be list");
Guido van Rossum778983b1993-02-19 15:55:02 +00001164 return NULL;
1165 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001166 n = PyList_Size(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001167 if (n > 0) {
1168 char *item = self->ob_item;
1169 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001170 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001171 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001172 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001173 return NULL;
1174 }
1175 self->ob_item = item;
1176 self->ob_size += n;
1177 for (i = 0; i < n; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001178 PyObject *v = PyList_GetItem(list, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001179 if ((*self->ob_descr->setitem)(self,
1180 self->ob_size - n + i, v) != 0) {
1181 self->ob_size -= n;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001182 PyMem_RESIZE(item, char,
1183 self->ob_size * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001184 self->ob_item = item;
1185 return NULL;
1186 }
1187 }
1188 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001189 Py_INCREF(Py_None);
1190 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001191}
1192
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001193PyDoc_STRVAR(fromlist_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001194"fromlist(list)\n\
1195\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001196Append items to array from list.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001197
1198
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001199static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001200array_tolist(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001201{
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001202 PyObject *list = PyList_New(self->ob_size);
Guido van Rossum778983b1993-02-19 15:55:02 +00001203 int i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001204
Guido van Rossum778983b1993-02-19 15:55:02 +00001205 if (list == NULL)
1206 return NULL;
1207 for (i = 0; i < self->ob_size; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001208 PyObject *v = getarrayitem((PyObject *)self, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001209 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001210 Py_DECREF(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001211 return NULL;
1212 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001213 PyList_SetItem(list, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001214 }
1215 return list;
1216}
1217
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001218PyDoc_STRVAR(tolist_doc,
Guido van Rossumfc6aba51998-10-14 02:52:31 +00001219"tolist() -> list\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001220\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001221Convert array to an ordinary list with the same items.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001222
1223
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001224static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001225array_fromstring(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001226{
1227 char *str;
1228 int n;
1229 int itemsize = self->ob_descr->itemsize;
Peter Schneider-Kampdc71cac2000-07-31 21:57:30 +00001230 if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +00001231 return NULL;
1232 if (n % itemsize != 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001233 PyErr_SetString(PyExc_ValueError,
Guido van Rossum778983b1993-02-19 15:55:02 +00001234 "string length not a multiple of item size");
1235 return NULL;
1236 }
1237 n = n / itemsize;
1238 if (n > 0) {
1239 char *item = self->ob_item;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001240 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001241 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001242 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001243 return NULL;
1244 }
1245 self->ob_item = item;
1246 self->ob_size += n;
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001247 memcpy(item + (self->ob_size - n) * itemsize,
1248 str, itemsize*n);
Guido van Rossum778983b1993-02-19 15:55:02 +00001249 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001250 Py_INCREF(Py_None);
1251 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001252}
1253
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001254PyDoc_STRVAR(fromstring_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001255"fromstring(string)\n\
1256\n\
1257Appends items from the string, interpreting it as an array of machine\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001258values,as if it had been read from a file using the fromfile() method).");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001259
1260
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001261static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001262array_tostring(arrayobject *self, PyObject *unused)
Guido van Rossum778983b1993-02-19 15:55:02 +00001263{
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001264 return PyString_FromStringAndSize(self->ob_item,
Guido van Rossum778983b1993-02-19 15:55:02 +00001265 self->ob_size * self->ob_descr->itemsize);
1266}
1267
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001268PyDoc_STRVAR(tostring_doc,
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001269"tostring() -> string\n\
1270\n\
1271Convert the array to an array of machine values and return the string\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001272representation.");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001273
Martin v. Löwis99866332002-03-01 10:27:01 +00001274
1275
1276#ifdef Py_USING_UNICODE
1277static PyObject *
1278array_fromunicode(arrayobject *self, PyObject *args)
1279{
1280 Py_UNICODE *ustr;
1281 int n;
1282
1283 if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
1284 return NULL;
1285 if (self->ob_descr->typecode != 'u') {
1286 PyErr_SetString(PyExc_ValueError,
1287 "fromunicode() may only be called on "
1288 "type 'u' arrays");
1289 return NULL;
1290 }
1291 if (n > 0) {
1292 Py_UNICODE *item = (Py_UNICODE *) self->ob_item;
1293 PyMem_RESIZE(item, Py_UNICODE, self->ob_size + n);
1294 if (item == NULL) {
1295 PyErr_NoMemory();
1296 return NULL;
1297 }
1298 self->ob_item = (char *) item;
1299 self->ob_size += n;
1300 memcpy(item + self->ob_size - n,
1301 ustr, n * sizeof(Py_UNICODE));
1302 }
1303
1304 Py_INCREF(Py_None);
1305 return Py_None;
1306}
1307
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001308PyDoc_STRVAR(fromunicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001309"fromunicode(ustr)\n\
1310\n\
1311Extends this array with data from the unicode string ustr.\n\
1312The array must be a type 'u' array; otherwise a ValueError\n\
1313is raised. Use array.fromstring(ustr.decode(...)) to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001314append Unicode data to an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001315
1316
1317static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001318array_tounicode(arrayobject *self, PyObject *unused)
Martin v. Löwis99866332002-03-01 10:27:01 +00001319{
Martin v. Löwis99866332002-03-01 10:27:01 +00001320 if (self->ob_descr->typecode != 'u') {
1321 PyErr_SetString(PyExc_ValueError,
1322 "tounicode() may only be called on type 'u' arrays");
1323 return NULL;
1324 }
1325 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, self->ob_size);
1326}
1327
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001328PyDoc_STRVAR(tounicode_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001329"tounicode() -> unicode\n\
1330\n\
1331Convert the array to a unicode string. The array must be\n\
1332a type 'u' array; otherwise a ValueError is raised. Use\n\
1333array.tostring().decode() to obtain a unicode string from\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001334an array of some other type.");
Martin v. Löwis99866332002-03-01 10:27:01 +00001335
1336#endif /* Py_USING_UNICODE */
1337
1338
1339static PyObject *
1340array_get_typecode(arrayobject *a, void *closure)
1341{
1342 char tc = a->ob_descr->typecode;
1343 return PyString_FromStringAndSize(&tc, 1);
1344}
1345
1346static PyObject *
1347array_get_itemsize(arrayobject *a, void *closure)
1348{
1349 return PyInt_FromLong((long)a->ob_descr->itemsize);
1350}
1351
1352static PyGetSetDef array_getsets [] = {
1353 {"typecode", (getter) array_get_typecode, NULL,
1354 "the typecode character used to create the array"},
1355 {"itemsize", (getter) array_get_itemsize, NULL,
1356 "the size, in bytes, of one array item"},
1357 {NULL}
1358};
1359
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001360PyMethodDef array_methods[] = {
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001361 {"append", (PyCFunction)array_append, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001362 append_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001363 {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001364 buffer_info_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001365 {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001366 byteswap_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001367 {"count", (PyCFunction)array_count, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001368 count_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001369 {"extend", (PyCFunction)array_extend, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001370 extend_doc},
1371 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
1372 fromfile_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001373 {"fromlist", (PyCFunction)array_fromlist, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001374 fromlist_doc},
1375 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
1376 fromstring_doc},
Martin v. Löwis99866332002-03-01 10:27:01 +00001377#ifdef Py_USING_UNICODE
1378 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
1379 fromunicode_doc},
1380#endif
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001381 {"index", (PyCFunction)array_index, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001382 index_doc},
1383 {"insert", (PyCFunction)array_insert, METH_VARARGS,
1384 insert_doc},
1385 {"pop", (PyCFunction)array_pop, METH_VARARGS,
1386 pop_doc},
1387 {"read", (PyCFunction)array_fromfile, METH_VARARGS,
1388 fromfile_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001389 {"remove", (PyCFunction)array_remove, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001390 remove_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001391 {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001392 reverse_doc},
1393/* {"sort", (PyCFunction)array_sort, METH_VARARGS,
1394 sort_doc},*/
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001395 {"tofile", (PyCFunction)array_tofile, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001396 tofile_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001397 {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001398 tolist_doc},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001399 {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001400 tostring_doc},
Martin v. Löwis99866332002-03-01 10:27:01 +00001401#ifdef Py_USING_UNICODE
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001402 {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
Martin v. Löwis99866332002-03-01 10:27:01 +00001403 tounicode_doc},
1404#endif
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001405 {"write", (PyCFunction)array_tofile, METH_O,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001406 tofile_doc},
Guido van Rossum778983b1993-02-19 15:55:02 +00001407 {NULL, NULL} /* sentinel */
1408};
1409
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001410static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001411array_repr(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +00001412{
Martin v. Löwis99866332002-03-01 10:27:01 +00001413 char buf[256], typecode;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001414 PyObject *s, *t, *comma, *v;
Guido van Rossum778983b1993-02-19 15:55:02 +00001415 int i, len;
Martin v. Löwis99866332002-03-01 10:27:01 +00001416
Guido van Rossum778983b1993-02-19 15:55:02 +00001417 len = a->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +00001418 typecode = a->ob_descr->typecode;
Guido van Rossum778983b1993-02-19 15:55:02 +00001419 if (len == 0) {
Martin v. Löwis99866332002-03-01 10:27:01 +00001420 PyOS_snprintf(buf, sizeof(buf), "array('%c')", typecode);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001421 return PyString_FromString(buf);
Guido van Rossum778983b1993-02-19 15:55:02 +00001422 }
Martin v. Löwis99866332002-03-01 10:27:01 +00001423
1424 if (typecode == 'c' || typecode == 'u') {
Martin v. Löwis99866332002-03-01 10:27:01 +00001425 PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001426 s = PyString_FromString(buf);
Michael W. Hudson1755ad92002-05-13 10:14:59 +00001427#ifdef Py_USING_UNICODE
Martin v. Löwis99866332002-03-01 10:27:01 +00001428 if (typecode == 'c')
Michael W. Hudson1755ad92002-05-13 10:14:59 +00001429#endif
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001430 v = array_tostring(a, NULL);
Michael W. Hudson1755ad92002-05-13 10:14:59 +00001431#ifdef Py_USING_UNICODE
Martin v. Löwis99866332002-03-01 10:27:01 +00001432 else
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001433 v = array_tounicode(a, NULL);
Michael W. Hudson1755ad92002-05-13 10:14:59 +00001434#endif
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001435 t = PyObject_Repr(v);
1436 Py_XDECREF(v);
1437 PyString_ConcatAndDel(&s, t);
1438 PyString_ConcatAndDel(&s, PyString_FromString(")"));
Guido van Rossum778983b1993-02-19 15:55:02 +00001439 return s;
1440 }
Martin v. Löwis99866332002-03-01 10:27:01 +00001441 PyOS_snprintf(buf, sizeof(buf), "array('%c', [", typecode);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001442 s = PyString_FromString(buf);
1443 comma = PyString_FromString(", ");
1444 for (i = 0; i < len && !PyErr_Occurred(); i++) {
Guido van Rossumb6775db1994-08-01 11:34:53 +00001445 if (i > 0)
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001446 PyString_Concat(&s, comma);
Guido van Rossum778983b1993-02-19 15:55:02 +00001447 v = (a->ob_descr->getitem)(a, i);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001448 t = PyObject_Repr(v);
1449 Py_XDECREF(v);
1450 PyString_ConcatAndDel(&s, t);
Guido van Rossum778983b1993-02-19 15:55:02 +00001451 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001452 Py_XDECREF(comma);
1453 PyString_ConcatAndDel(&s, PyString_FromString("])"));
Guido van Rossum778983b1993-02-19 15:55:02 +00001454 return s;
1455}
1456
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001457static PyObject*
1458array_subscr(arrayobject* self, PyObject* item)
1459{
1460 if (PyInt_Check(item)) {
1461 long i = PyInt_AS_LONG(item);
1462 if (i < 0)
1463 i += self->ob_size;
1464 return array_item(self, i);
1465 }
1466 else if (PyLong_Check(item)) {
1467 long i = PyLong_AsLong(item);
1468 if (i == -1 && PyErr_Occurred())
1469 return NULL;
1470 if (i < 0)
1471 i += self->ob_size;
1472 return array_item(self, i);
1473 }
1474 else if (PySlice_Check(item)) {
1475 int start, stop, step, slicelength, cur, i;
1476 PyObject* result;
1477 arrayobject* ar;
1478 int itemsize = self->ob_descr->itemsize;
1479
1480 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
1481 &start, &stop, &step, &slicelength) < 0) {
1482 return NULL;
1483 }
1484
1485 if (slicelength <= 0) {
1486 return newarrayobject(&Arraytype, 0, self->ob_descr);
1487 }
1488 else {
1489 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
1490 if (!result) return NULL;
1491
1492 ar = (arrayobject*)result;
1493
1494 for (cur = start, i = 0; i < slicelength;
1495 cur += step, i++) {
1496 memcpy(ar->ob_item + i*itemsize,
1497 self->ob_item + cur*itemsize,
1498 itemsize);
1499 }
1500
1501 return result;
1502 }
1503 }
1504 else {
1505 PyErr_SetString(PyExc_TypeError,
1506 "list indices must be integers");
1507 return NULL;
1508 }
1509}
1510
1511static int
1512array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
1513{
1514 if (PyInt_Check(item)) {
1515 long i = PyInt_AS_LONG(item);
1516 if (i < 0)
1517 i += self->ob_size;
1518 return array_ass_item(self, i, value);
1519 }
1520 else if (PyLong_Check(item)) {
1521 long i = PyLong_AsLong(item);
1522 if (i == -1 && PyErr_Occurred())
1523 return -1;
1524 if (i < 0)
1525 i += self->ob_size;
1526 return array_ass_item(self, i, value);
1527 }
1528 else if (PySlice_Check(item)) {
1529 int start, stop, step, slicelength;
1530 int itemsize = self->ob_descr->itemsize;
1531
1532 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
1533 &start, &stop, &step, &slicelength) < 0) {
1534 return -1;
1535 }
1536
1537 /* treat A[slice(a,b)] = v _exactly_ like A[a:b] = v */
1538 if (step == 1 && ((PySliceObject*)item)->step == Py_None)
1539 return array_ass_slice(self, start, stop, value);
1540
1541 if (value == NULL) {
1542 /* delete slice */
Michael W. Hudson56796f62002-07-29 14:35:04 +00001543 int cur, i, extra;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001544
1545 if (slicelength <= 0)
1546 return 0;
1547
1548 if (step < 0) {
1549 stop = start + 1;
1550 start = stop + step*(slicelength - 1) - 1;
1551 step = -step;
1552 }
1553
Michael W. Hudson56796f62002-07-29 14:35:04 +00001554 for (cur = start, i = 0; i < slicelength - 1;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001555 cur += step, i++) {
1556 memmove(self->ob_item + (cur - i)*itemsize,
1557 self->ob_item + (cur + 1)*itemsize,
1558 (step - 1) * itemsize);
1559 }
Michael W. Hudson56796f62002-07-29 14:35:04 +00001560 extra = self->ob_size - (cur + 1);
1561 if (extra > 0) {
1562 memmove(self->ob_item + (cur - i)*itemsize,
1563 self->ob_item + (cur + 1)*itemsize,
1564 extra*itemsize);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001565 }
1566
1567 self->ob_size -= slicelength;
1568 self->ob_item = PyMem_REALLOC(self->ob_item, itemsize*self->ob_size);
1569
1570
1571 return 0;
1572 }
1573 else {
1574 /* assign slice */
1575 int cur, i;
1576 arrayobject* av;
1577
1578 if (!array_Check(value)) {
1579 PyErr_Format(PyExc_TypeError,
1580 "must assign array (not \"%.200s\") to slice",
1581 value->ob_type->tp_name);
1582 return -1;
1583 }
1584
1585 av = (arrayobject*)value;
1586
1587 if (av->ob_size != slicelength) {
1588 PyErr_Format(PyExc_ValueError,
1589 "attempt to assign array of size %d to extended slice of size %d",
1590 av->ob_size, slicelength);
1591 return -1;
1592 }
1593
1594 if (!slicelength)
1595 return 0;
1596
1597 /* protect against a[::-1] = a */
1598 if (self == av) {
1599 value = array_slice(av, 0, av->ob_size);
1600 av = (arrayobject*)value;
1601 }
1602 else {
1603 Py_INCREF(value);
1604 }
1605
1606 for (cur = start, i = 0; i < slicelength;
1607 cur += step, i++) {
1608 memcpy(self->ob_item + cur*itemsize,
1609 av->ob_item + i*itemsize,
1610 itemsize);
1611 }
1612
1613 Py_DECREF(value);
1614
1615 return 0;
1616 }
1617 }
1618 else {
1619 PyErr_SetString(PyExc_TypeError,
1620 "list indices must be integers");
1621 return -1;
1622 }
1623}
1624
1625static PyMappingMethods array_as_mapping = {
1626 (inquiry)array_length,
1627 (binaryfunc)array_subscr,
1628 (objobjargproc)array_ass_subscr
1629};
1630
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001631static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001632array_buffer_getreadbuf(arrayobject *self, int index, const void **ptr)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001633{
1634 if ( index != 0 ) {
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001635 PyErr_SetString(PyExc_SystemError,
1636 "Accessing non-existent array segment");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001637 return -1;
1638 }
1639 *ptr = (void *)self->ob_item;
1640 return self->ob_size*self->ob_descr->itemsize;
1641}
1642
1643static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001644array_buffer_getwritebuf(arrayobject *self, int index, const void **ptr)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001645{
1646 if ( index != 0 ) {
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001647 PyErr_SetString(PyExc_SystemError,
1648 "Accessing non-existent array segment");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001649 return -1;
1650 }
1651 *ptr = (void *)self->ob_item;
1652 return self->ob_size*self->ob_descr->itemsize;
1653}
1654
1655static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001656array_buffer_getsegcount(arrayobject *self, int *lenp)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001657{
1658 if ( lenp )
1659 *lenp = self->ob_size*self->ob_descr->itemsize;
1660 return 1;
1661}
1662
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001663static PySequenceMethods array_as_sequence = {
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001664 (inquiry)array_length, /*sq_length*/
1665 (binaryfunc)array_concat, /*sq_concat*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001666 (intargfunc)array_repeat, /*sq_repeat*/
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001667 (intargfunc)array_item, /*sq_item*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001668 (intintargfunc)array_slice, /*sq_slice*/
1669 (intobjargproc)array_ass_item, /*sq_ass_item*/
1670 (intintobjargproc)array_ass_slice, /*sq_ass_slice*/
Raymond Hettinger625812f2003-01-07 01:58:52 +00001671 (objobjproc)array_contains, /*sq_contains*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001672 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
1673 (intargfunc)array_inplace_repeat /*sq_inplace_repeat*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001674};
1675
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001676static PyBufferProcs array_as_buffer = {
1677 (getreadbufferproc)array_buffer_getreadbuf,
1678 (getwritebufferproc)array_buffer_getwritebuf,
1679 (getsegcountproc)array_buffer_getsegcount,
1680};
1681
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001682static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +00001683array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum778983b1993-02-19 15:55:02 +00001684{
1685 char c;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001686 PyObject *initial = NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001687 struct arraydescr *descr;
Martin v. Löwis99866332002-03-01 10:27:01 +00001688
1689 if (kwds != NULL) {
1690 int i = PyObject_Length(kwds);
1691 if (i < 0)
Guido van Rossum778983b1993-02-19 15:55:02 +00001692 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001693 else if (i > 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001694 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis99866332002-03-01 10:27:01 +00001695 "array.array constructor takes "
1696 "no keyword arguments");
Guido van Rossum778983b1993-02-19 15:55:02 +00001697 return NULL;
1698 }
1699 }
Martin v. Löwis99866332002-03-01 10:27:01 +00001700
1701 if (!PyArg_ParseTuple(args, "c|O:array", &c, &initial))
1702 return NULL;
1703
1704 if (!(initial == NULL || PyList_Check(initial)
1705 || PyString_Check(initial)
1706 || (c == 'u' && PyUnicode_Check(initial)))) {
1707 PyErr_SetString(PyExc_TypeError,
1708 "array initializer must be list or string");
1709 return NULL;
1710 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001711 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1712 if (descr->typecode == c) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001713 PyObject *a;
Guido van Rossum778983b1993-02-19 15:55:02 +00001714 int len;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001715 if (initial == NULL || !PyList_Check(initial))
Guido van Rossum778983b1993-02-19 15:55:02 +00001716 len = 0;
1717 else
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001718 len = PyList_Size(initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001719
1720 a = newarrayobject(type, len, descr);
Guido van Rossum778983b1993-02-19 15:55:02 +00001721 if (a == NULL)
1722 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001723
Guido van Rossum778983b1993-02-19 15:55:02 +00001724 if (len > 0) {
1725 int i;
1726 for (i = 0; i < len; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001727 PyObject *v =
Martin v. Löwis99866332002-03-01 10:27:01 +00001728 PyList_GetItem(initial, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001729 if (setarrayitem(a, i, v) != 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001730 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001731 return NULL;
1732 }
1733 }
1734 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001735 if (initial != NULL && PyString_Check(initial)) {
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001736 PyObject *t_initial = Py_BuildValue("(O)",
1737 initial);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001738 PyObject *v =
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001739 array_fromstring((arrayobject *)a,
1740 t_initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001741 Py_DECREF(t_initial);
Guido van Rossum778983b1993-02-19 15:55:02 +00001742 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001743 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001744 return NULL;
1745 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001746 Py_DECREF(v);
Martin v. Löwis99866332002-03-01 10:27:01 +00001747#ifdef Py_USING_UNICODE
1748 } else if (initial != NULL && PyUnicode_Check(initial)) {
1749 int n = PyUnicode_GET_DATA_SIZE(initial);
1750 if (n > 0) {
1751 arrayobject *self = (arrayobject *)a;
1752 char *item = self->ob_item;
1753 item = PyMem_Realloc(item, n);
1754 if (item == NULL) {
1755 PyErr_NoMemory();
1756 Py_DECREF(a);
1757 return NULL;
1758 }
1759 self->ob_item = item;
1760 self->ob_size = n / sizeof(Py_UNICODE);
1761 memcpy(item, PyUnicode_AS_DATA(initial), n);
1762 }
1763#endif
Guido van Rossum778983b1993-02-19 15:55:02 +00001764 }
1765 return a;
1766 }
1767 }
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001768 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis99866332002-03-01 10:27:01 +00001769 "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 +00001770 return NULL;
1771}
1772
Guido van Rossum778983b1993-02-19 15:55:02 +00001773
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001774PyDoc_STRVAR(module_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001775"This module defines an object type which can efficiently represent\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001776an array of basic values: characters, integers, floating point\n\
1777numbers. Arrays are sequence types and behave very much like lists,\n\
1778except that the type of objects stored in them is constrained. The\n\
1779type is specified at object creation time by using a type code, which\n\
1780is a single character. The following type codes are defined:\n\
1781\n\
1782 Type code C Type Minimum size in bytes \n\
1783 'c' character 1 \n\
1784 'b' signed integer 1 \n\
1785 'B' unsigned integer 1 \n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001786 'u' Unicode character 2 \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001787 'h' signed integer 2 \n\
1788 'H' unsigned integer 2 \n\
1789 'i' signed integer 2 \n\
1790 'I' unsigned integer 2 \n\
1791 'l' signed integer 4 \n\
1792 'L' unsigned integer 4 \n\
1793 'f' floating point 4 \n\
1794 'd' floating point 8 \n\
1795\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001796The constructor is:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001797\n\
1798array(typecode [, initializer]) -- create a new array\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001799");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001800
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001801PyDoc_STRVAR(arraytype_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00001802"array(typecode [, initializer]) -> array\n\
1803\n\
1804Return a new array whose items are restricted by typecode, and\n\
1805initialized from the optional initializer value, which must be a list\n\
1806or a string.\n\
1807\n\
1808Arrays represent basic values and behave very much like lists, except\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001809the type of objects stored in them is constrained.\n\
1810\n\
1811Methods:\n\
1812\n\
1813append() -- append a new item to the end of the array\n\
1814buffer_info() -- return information giving the current memory info\n\
1815byteswap() -- byteswap all the items of the array\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001816count() -- return number of occurences of an object\n\
1817extend() -- extend array by appending array elements\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001818fromfile() -- read items from a file object\n\
1819fromlist() -- append items from the list\n\
1820fromstring() -- append items from the string\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001821index() -- return index of first occurence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001822insert() -- insert a new item into the array at a provided position\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001823pop() -- remove and return item (default last)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001824read() -- DEPRECATED, use fromfile()\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001825remove() -- remove first occurence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001826reverse() -- reverse the order of the items in the array\n\
1827tofile() -- write all items to a file object\n\
1828tolist() -- return the array converted to an ordinary list\n\
1829tostring() -- return the array converted to a string\n\
1830write() -- DEPRECATED, use tofile()\n\
1831\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001832Attributes:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001833\n\
1834typecode -- the typecode character used to create the array\n\
1835itemsize -- the length in bytes of one array item\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001836");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001837
Raymond Hettinger625812f2003-01-07 01:58:52 +00001838static PyObject *array_iter(arrayobject *ao);
1839
Tim Peters0c322792002-07-17 16:49:03 +00001840static PyTypeObject Arraytype = {
Fred Drake0d40ba42000-02-04 20:33:49 +00001841 PyObject_HEAD_INIT(NULL)
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001842 0,
Guido van Rossum14648392001-12-08 18:02:58 +00001843 "array.array",
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001844 sizeof(arrayobject),
1845 0,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001846 (destructor)array_dealloc, /* tp_dealloc */
Martin v. Löwisaa158be2002-03-04 09:38:52 +00001847 0, /* tp_print */
Martin v. Löwis99866332002-03-01 10:27:01 +00001848 0, /* tp_getattr */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001849 0, /* tp_setattr */
1850 0, /* tp_compare */
1851 (reprfunc)array_repr, /* tp_repr */
1852 0, /* tp_as _number*/
1853 &array_as_sequence, /* tp_as _sequence*/
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00001854 &array_as_mapping, /* tp_as _mapping*/
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001855 0, /* tp_hash */
1856 0, /* tp_call */
1857 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00001858 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001859 0, /* tp_setattro */
Tim Petersb870c752001-06-05 04:43:26 +00001860 &array_as_buffer, /* tp_as_buffer*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001861 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001862 arraytype_doc, /* tp_doc */
1863 0, /* tp_traverse */
1864 0, /* tp_clear */
1865 array_richcompare, /* tp_richcompare */
Martin v. Löwis99866332002-03-01 10:27:01 +00001866 0, /* tp_weaklistoffset */
Raymond Hettinger625812f2003-01-07 01:58:52 +00001867 (getiterfunc)array_iter, /* tp_iter */
Martin v. Löwis99866332002-03-01 10:27:01 +00001868 0, /* tp_iternext */
1869 array_methods, /* tp_methods */
1870 0, /* tp_members */
1871 array_getsets, /* tp_getset */
1872 0, /* tp_base */
1873 0, /* tp_dict */
1874 0, /* tp_descr_get */
1875 0, /* tp_descr_set */
1876 0, /* tp_dictoffset */
1877 0, /* tp_init */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00001878 PyType_GenericAlloc, /* tp_alloc */
Martin v. Löwis99866332002-03-01 10:27:01 +00001879 array_new, /* tp_new */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00001880 PyObject_Del, /* tp_free */
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001881};
1882
Raymond Hettinger625812f2003-01-07 01:58:52 +00001883
1884/*********************** Array Iterator **************************/
1885
1886typedef struct {
1887 PyObject_HEAD
1888 long index;
1889 arrayobject *ao;
1890 PyObject * (*getitem)(struct arrayobject *, int);
1891} arrayiterobject;
1892
1893static PyTypeObject PyArrayIter_Type;
1894
1895#define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
1896
1897static PyObject *
1898array_iter(arrayobject *ao)
1899{
1900 arrayiterobject *it;
1901
1902 if (!array_Check(ao)) {
1903 PyErr_BadInternalCall();
1904 return NULL;
1905 }
1906
1907 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
1908 if (it == NULL)
1909 return NULL;
1910
1911 Py_INCREF(ao);
1912 it->ao = ao;
1913 it->index = 0;
1914 it->getitem = ao->ob_descr->getitem;
1915 PyObject_GC_Track(it);
1916 return (PyObject *)it;
1917}
1918
1919static PyObject *
1920arrayiter_getiter(PyObject *it)
1921{
1922 Py_INCREF(it);
1923 return it;
1924}
1925
1926static PyObject *
1927arrayiter_next(arrayiterobject *it)
1928{
1929 assert(PyArrayIter_Check(it));
1930 if (it->index < it->ao->ob_size)
1931 return (*it->getitem)(it->ao, it->index++);
1932 return NULL;
1933}
1934
1935static void
1936arrayiter_dealloc(arrayiterobject *it)
1937{
1938 PyObject_GC_UnTrack(it);
1939 Py_XDECREF(it->ao);
1940 PyObject_GC_Del(it);
1941}
1942
1943static int
1944arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
1945{
1946 if (it->ao != NULL)
1947 return visit((PyObject *)(it->ao), arg);
1948 return 0;
1949}
1950
1951static PyTypeObject PyArrayIter_Type = {
1952 PyObject_HEAD_INIT(&PyType_Type)
1953 0, /* ob_size */
1954 "arrayiterator", /* tp_name */
1955 sizeof(arrayiterobject), /* tp_basicsize */
1956 0, /* tp_itemsize */
1957 /* methods */
1958 (destructor)arrayiter_dealloc, /* tp_dealloc */
1959 0, /* tp_print */
1960 0, /* tp_getattr */
1961 0, /* tp_setattr */
1962 0, /* tp_compare */
1963 0, /* tp_repr */
1964 0, /* tp_as_number */
1965 0, /* tp_as_sequence */
1966 0, /* tp_as_mapping */
1967 0, /* tp_hash */
1968 0, /* tp_call */
1969 0, /* tp_str */
1970 PyObject_GenericGetAttr, /* tp_getattro */
1971 0, /* tp_setattro */
1972 0, /* tp_as_buffer */
1973 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1974 0, /* tp_doc */
1975 (traverseproc)arrayiter_traverse, /* tp_traverse */
1976 0, /* tp_clear */
1977 0, /* tp_richcompare */
1978 0, /* tp_weaklistoffset */
1979 (getiterfunc)arrayiter_getiter, /* tp_iter */
1980 (iternextfunc)arrayiter_next, /* tp_iternext */
1981 0, /* tp_methods */
1982};
1983
1984
1985/*********************** Install Module **************************/
1986
Martin v. Löwis99866332002-03-01 10:27:01 +00001987/* No functions in array module. */
1988static PyMethodDef a_methods[] = {
1989 {NULL, NULL, 0, NULL} /* Sentinel */
1990};
1991
1992
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001993PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001994initarray(void)
Guido van Rossum778983b1993-02-19 15:55:02 +00001995{
Fred Drakef4e34842002-04-01 03:45:06 +00001996 PyObject *m;
Fred Drake0d40ba42000-02-04 20:33:49 +00001997
Martin v. Löwis99866332002-03-01 10:27:01 +00001998 Arraytype.ob_type = &PyType_Type;
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001999 m = Py_InitModule3("array", a_methods, module_doc);
Fred Drakef4e34842002-04-01 03:45:06 +00002000
2001 Py_INCREF((PyObject *)&Arraytype);
2002 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
2003 Py_INCREF((PyObject *)&Arraytype);
2004 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
Guido van Rossuma0deb641998-10-14 13:45:06 +00002005 /* No need to check the error here, the caller will do that */
Guido van Rossum778983b1993-02-19 15:55:02 +00002006}