blob: 6e79be1f9be762101d36621534ad00d5f6960e87 [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
Roger E. Masse2919eaa1996-12-09 20:10:36 +000036staticforward 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 *
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000801array_count(arrayobject *self, PyObject *args)
802{
803 int count = 0;
804 int i;
805 PyObject *v;
806
807 if (!PyArg_ParseTuple(args, "O:count", &v))
808 return NULL;
809 for (i = 0; i < self->ob_size; i++) {
810 PyObject *selfi = getarrayitem((PyObject *)self, i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000811 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000812 Py_DECREF(selfi);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000813 if (cmp > 0)
814 count++;
815 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000816 return NULL;
817 }
818 return PyInt_FromLong((long)count);
819}
820
821static char count_doc [] =
Tim Peters077a11d2000-09-16 22:31:29 +0000822"count(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000823\n\
824Return number of occurences of x in the array.";
825
826static PyObject *
827array_index(arrayobject *self, PyObject *args)
828{
829 int i;
830 PyObject *v;
831
832 if (!PyArg_ParseTuple(args, "O:index", &v))
833 return NULL;
834 for (i = 0; i < self->ob_size; i++) {
835 PyObject *selfi = getarrayitem((PyObject *)self, i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000836 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
837 Py_DECREF(selfi);
838 if (cmp > 0) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000839 return PyInt_FromLong((long)i);
840 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000841 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000842 return NULL;
843 }
844 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
845 return NULL;
846}
847
848static char index_doc [] =
Tim Peters077a11d2000-09-16 22:31:29 +0000849"index(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000850\n\
851Return index of first occurence of x in the array.";
852
853static PyObject *
854array_remove(arrayobject *self, PyObject *args)
855{
856 int i;
857 PyObject *v;
858
859 if (!PyArg_ParseTuple(args, "O:remove", &v))
860 return NULL;
861 for (i = 0; i < self->ob_size; i++) {
862 PyObject *selfi = getarrayitem((PyObject *)self,i);
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000863 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
864 Py_DECREF(selfi);
865 if (cmp > 0) {
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000866 if (array_ass_slice(self, i, i+1,
867 (PyObject *)NULL) != 0)
868 return NULL;
869 Py_INCREF(Py_None);
870 return Py_None;
871 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000872 else if (cmp < 0)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000873 return NULL;
874 }
875 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
876 return NULL;
877}
878
879static char remove_doc [] =
Tim Peters077a11d2000-09-16 22:31:29 +0000880"remove(x)\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000881\n\
882Remove the first occurence of x in the array.";
883
884static PyObject *
885array_pop(arrayobject *self, PyObject *args)
886{
887 int i = -1;
888 PyObject *v;
889 if (!PyArg_ParseTuple(args, "|i:pop", &i))
890 return NULL;
891 if (self->ob_size == 0) {
892 /* Special-case most common failure cause */
893 PyErr_SetString(PyExc_IndexError, "pop from empty array");
894 return NULL;
895 }
896 if (i < 0)
897 i += self->ob_size;
898 if (i < 0 || i >= self->ob_size) {
899 PyErr_SetString(PyExc_IndexError, "pop index out of range");
900 return NULL;
901 }
902 v = getarrayitem((PyObject *)self,i);
903 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
904 Py_DECREF(v);
905 return NULL;
906 }
907 return v;
908}
909
910static char pop_doc [] =
Tim Peters077a11d2000-09-16 22:31:29 +0000911"pop([i])\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000912\n\
913Return the i-th element and delete it from the array. i defaults to -1.";
914
915static PyObject *
Fred Drake8ce159a2000-08-31 05:18:54 +0000916array_extend(arrayobject *self, PyObject *args)
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000917{
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000918 PyObject *bb;
Tim Petersbb307342000-09-10 05:22:54 +0000919
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000920 if (!PyArg_ParseTuple(args, "O:extend", &bb))
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +0000921 return NULL;
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
928static char extend_doc [] =
929"extend(array)\n\
930\n\
931 Append array items to the end of the array.";
932
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
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000943static char insert_doc [] =
Tim Peters077a11d2000-09-16 22:31:29 +0000944"insert(i,x)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000945\n\
946Insert a new item x into the array before position i.";
947
948
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000949static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000950array_buffer_info(arrayobject *self, PyObject *args)
Guido van Rossumde4a4ca1997-08-12 14:55:56 +0000951{
Tim Peters077a11d2000-09-16 22:31:29 +0000952 PyObject* retval = NULL;
953 if (!PyArg_ParseTuple(args, ":buffer_info"))
954 return NULL;
955 retval = PyTuple_New(2);
956 if (!retval)
957 return NULL;
Fred Drake541dc3b2000-06-28 17:49:30 +0000958
959 PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item));
960 PyTuple_SET_ITEM(retval, 1, PyInt_FromLong((long)(self->ob_size)));
961
962 return retval;
Guido van Rossumde4a4ca1997-08-12 14:55:56 +0000963}
964
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000965static char buffer_info_doc [] =
Tim Peters077a11d2000-09-16 22:31:29 +0000966"buffer_info() -> (address, length)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000967\n\
968Return a tuple (address, length) giving the current memory address and\n\
Guido van Rossum702d08e2001-07-27 16:05:32 +0000969the length in items of the buffer used to hold array's contents\n\
970The length should be multiplied by the itemsize attribute to calculate\n\
971the buffer length in bytes.";
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000972
973
Guido van Rossumde4a4ca1997-08-12 14:55:56 +0000974static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000975array_append(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +0000976{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000977 PyObject *v;
Peter Schneider-Kampdc71cac2000-07-31 21:57:30 +0000978 if (!PyArg_ParseTuple(args, "O:append", &v))
Guido van Rossum778983b1993-02-19 15:55:02 +0000979 return NULL;
980 return ins(self, (int) self->ob_size, v);
981}
982
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000983static char append_doc [] =
984"append(x)\n\
985\n\
986Append new value x to the end of the array.";
987
988
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000989static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000990array_byteswap(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +0000991{
992 char *p;
993 int i;
Fred Drakebf272981999-12-03 17:15:30 +0000994
995 if (!PyArg_ParseTuple(args, ":byteswap"))
996 return NULL;
997
Guido van Rossum778983b1993-02-19 15:55:02 +0000998 switch (self->ob_descr->itemsize) {
999 case 1:
1000 break;
1001 case 2:
1002 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 2) {
1003 char p0 = p[0];
1004 p[0] = p[1];
1005 p[1] = p0;
1006 }
1007 break;
1008 case 4:
1009 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 4) {
1010 char p0 = p[0];
1011 char p1 = p[1];
1012 p[0] = p[3];
1013 p[1] = p[2];
1014 p[2] = p1;
1015 p[3] = p0;
1016 }
1017 break;
Guido van Rossume77a7571993-11-03 15:01:26 +00001018 case 8:
1019 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 8) {
1020 char p0 = p[0];
1021 char p1 = p[1];
1022 char p2 = p[2];
1023 char p3 = p[3];
1024 p[0] = p[7];
1025 p[1] = p[6];
1026 p[2] = p[5];
1027 p[3] = p[4];
1028 p[4] = p3;
1029 p[5] = p2;
1030 p[6] = p1;
1031 p[7] = p0;
1032 }
1033 break;
Guido van Rossum778983b1993-02-19 15:55:02 +00001034 default:
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001035 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossum778983b1993-02-19 15:55:02 +00001036 "don't know how to byteswap this array type");
1037 return NULL;
1038 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001039 Py_INCREF(Py_None);
1040 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001041}
1042
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001043static char byteswap_doc [] =
Fred Drakebf272981999-12-03 17:15:30 +00001044"byteswap()\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001045\n\
Fred Drakebf272981999-12-03 17:15:30 +00001046Byteswap all items of the array. If the items in the array are not 1, 2,\n\
10474, or 8 bytes in size, RuntimeError is raised.";
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001048
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001049static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001050array_reverse(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001051{
Guido van Rossume77a7571993-11-03 15:01:26 +00001052 register int itemsize = self->ob_descr->itemsize;
1053 register char *p, *q;
Tim Peters077a11d2000-09-16 22:31:29 +00001054 /* little buffer to hold items while swapping */
1055 char tmp[256]; /* 8 is probably enough -- but why skimp */
1056 assert(itemsize <= sizeof(tmp));
Guido van Rossume77a7571993-11-03 15:01:26 +00001057
Tim Peters077a11d2000-09-16 22:31:29 +00001058 if (!PyArg_ParseTuple(args, ":reverse"))
1059 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001060
1061 if (self->ob_size > 1) {
Guido van Rossume77a7571993-11-03 15:01:26 +00001062 for (p = self->ob_item,
1063 q = self->ob_item + (self->ob_size - 1)*itemsize;
1064 p < q;
1065 p += itemsize, q -= itemsize) {
Tim Peters077a11d2000-09-16 22:31:29 +00001066 /* memory areas guaranteed disjoint, so memcpy
1067 * is safe (& memmove may be slower).
1068 */
1069 memcpy(tmp, p, itemsize);
1070 memcpy(p, q, itemsize);
1071 memcpy(q, tmp, itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001072 }
1073 }
Tim Petersbb307342000-09-10 05:22:54 +00001074
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001075 Py_INCREF(Py_None);
1076 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001077}
Guido van Rossume77a7571993-11-03 15:01:26 +00001078
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001079static char reverse_doc [] =
1080"reverse()\n\
1081\n\
1082Reverse the order of the items in the array.";
1083
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001084static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001085array_fromfile(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001086{
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001087 PyObject *f;
Guido van Rossum778983b1993-02-19 15:55:02 +00001088 int n;
1089 FILE *fp;
Peter Schneider-Kampdc71cac2000-07-31 21:57:30 +00001090 if (!PyArg_ParseTuple(args, "Oi:fromfile", &f, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +00001091 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001092 fp = PyFile_AsFile(f);
Guido van Rossum778983b1993-02-19 15:55:02 +00001093 if (fp == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001094 PyErr_SetString(PyExc_TypeError, "arg1 must be open file");
Guido van Rossum778983b1993-02-19 15:55:02 +00001095 return NULL;
1096 }
1097 if (n > 0) {
1098 char *item = self->ob_item;
1099 int itemsize = self->ob_descr->itemsize;
Guido van Rossum7d0ae5e2000-06-28 21:27:21 +00001100 size_t nread;
Guido van Rossum3791b0d1999-02-23 18:05:22 +00001101 int newlength;
1102 size_t newbytes;
1103 /* Be careful here about overflow */
1104 if ((newlength = self->ob_size + n) <= 0 ||
Guido van Rossum481ac881999-03-19 21:50:11 +00001105 (newbytes = newlength * itemsize) / itemsize !=
1106 (size_t)newlength)
Guido van Rossum3791b0d1999-02-23 18:05:22 +00001107 goto nomem;
1108 PyMem_RESIZE(item, char, newbytes);
Guido van Rossum778983b1993-02-19 15:55:02 +00001109 if (item == NULL) {
Guido van Rossum3791b0d1999-02-23 18:05:22 +00001110 nomem:
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001111 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001112 return NULL;
1113 }
1114 self->ob_item = item;
1115 self->ob_size += n;
1116 nread = fread(item + (self->ob_size - n) * itemsize,
1117 itemsize, n, fp);
Guido van Rossum7d0ae5e2000-06-28 21:27:21 +00001118 if (nread < (size_t)n) {
Guido van Rossum778983b1993-02-19 15:55:02 +00001119 self->ob_size -= (n - nread);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001120 PyMem_RESIZE(item, char, self->ob_size*itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001121 self->ob_item = item;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001122 PyErr_SetString(PyExc_EOFError,
1123 "not enough items in file");
Guido van Rossum778983b1993-02-19 15:55:02 +00001124 return NULL;
1125 }
1126 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001127 Py_INCREF(Py_None);
1128 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001129}
1130
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001131static char fromfile_doc [] =
1132"fromfile(f, n)\n\
1133\n\
1134Read n objects from the file object f and append them to the end of the\n\
1135array. Also called as read.";
1136
1137
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001138static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001139array_tofile(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001140{
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001141 PyObject *f;
Guido van Rossum778983b1993-02-19 15:55:02 +00001142 FILE *fp;
Peter Schneider-Kampdc71cac2000-07-31 21:57:30 +00001143 if (!PyArg_ParseTuple(args, "O:tofile", &f))
Guido van Rossum778983b1993-02-19 15:55:02 +00001144 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001145 fp = PyFile_AsFile(f);
Guido van Rossum778983b1993-02-19 15:55:02 +00001146 if (fp == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001147 PyErr_SetString(PyExc_TypeError, "arg must be open file");
Guido van Rossum778983b1993-02-19 15:55:02 +00001148 return NULL;
1149 }
1150 if (self->ob_size > 0) {
Trent Mick6c116dd2000-08-12 20:58:11 +00001151 if (fwrite(self->ob_item, self->ob_descr->itemsize,
1152 self->ob_size, fp) != (size_t)self->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001153 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum778983b1993-02-19 15:55:02 +00001154 clearerr(fp);
1155 return NULL;
1156 }
1157 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001158 Py_INCREF(Py_None);
1159 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001160}
1161
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001162static char tofile_doc [] =
1163"tofile(f)\n\
1164\n\
1165Write all items (as machine values) to the file object f. Also called as\n\
1166write.";
1167
1168
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001169static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001170array_fromlist(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001171{
1172 int n;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001173 PyObject *list;
Guido van Rossum778983b1993-02-19 15:55:02 +00001174 int itemsize = self->ob_descr->itemsize;
Peter Schneider-Kampdc71cac2000-07-31 21:57:30 +00001175 if (!PyArg_ParseTuple(args, "O:fromlist", &list))
Guido van Rossum778983b1993-02-19 15:55:02 +00001176 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001177 if (!PyList_Check(list)) {
1178 PyErr_SetString(PyExc_TypeError, "arg must be list");
Guido van Rossum778983b1993-02-19 15:55:02 +00001179 return NULL;
1180 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001181 n = PyList_Size(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001182 if (n > 0) {
1183 char *item = self->ob_item;
1184 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001185 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001186 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001187 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001188 return NULL;
1189 }
1190 self->ob_item = item;
1191 self->ob_size += n;
1192 for (i = 0; i < n; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001193 PyObject *v = PyList_GetItem(list, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001194 if ((*self->ob_descr->setitem)(self,
1195 self->ob_size - n + i, v) != 0) {
1196 self->ob_size -= n;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001197 PyMem_RESIZE(item, char,
1198 self->ob_size * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001199 self->ob_item = item;
1200 return NULL;
1201 }
1202 }
1203 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001204 Py_INCREF(Py_None);
1205 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001206}
1207
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001208static char fromlist_doc [] =
1209"fromlist(list)\n\
1210\n\
1211Append items to array from list.";
1212
1213
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001214static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001215array_tolist(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001216{
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001217 PyObject *list = PyList_New(self->ob_size);
Guido van Rossum778983b1993-02-19 15:55:02 +00001218 int i;
Peter Schneider-Kampdc71cac2000-07-31 21:57:30 +00001219 if (!PyArg_ParseTuple(args, ":tolist"))
1220 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001221 if (list == NULL)
1222 return NULL;
1223 for (i = 0; i < self->ob_size; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001224 PyObject *v = getarrayitem((PyObject *)self, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001225 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001226 Py_DECREF(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001227 return NULL;
1228 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001229 PyList_SetItem(list, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001230 }
1231 return list;
1232}
1233
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001234static char tolist_doc [] =
Guido van Rossumfc6aba51998-10-14 02:52:31 +00001235"tolist() -> list\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001236\n\
1237Convert array to an ordinary list with the same items.";
1238
1239
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001240static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001241array_fromstring(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001242{
1243 char *str;
1244 int n;
1245 int itemsize = self->ob_descr->itemsize;
Peter Schneider-Kampdc71cac2000-07-31 21:57:30 +00001246 if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +00001247 return NULL;
1248 if (n % itemsize != 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001249 PyErr_SetString(PyExc_ValueError,
Guido van Rossum778983b1993-02-19 15:55:02 +00001250 "string length not a multiple of item size");
1251 return NULL;
1252 }
1253 n = n / itemsize;
1254 if (n > 0) {
1255 char *item = self->ob_item;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001256 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001257 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001258 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001259 return NULL;
1260 }
1261 self->ob_item = item;
1262 self->ob_size += n;
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001263 memcpy(item + (self->ob_size - n) * itemsize,
1264 str, itemsize*n);
Guido van Rossum778983b1993-02-19 15:55:02 +00001265 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001266 Py_INCREF(Py_None);
1267 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001268}
1269
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001270static char fromstring_doc [] =
1271"fromstring(string)\n\
1272\n\
1273Appends items from the string, interpreting it as an array of machine\n\
1274values,as if it had been read from a file using the fromfile() method).";
1275
1276
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001277static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001278array_tostring(arrayobject *self, PyObject *args)
Guido van Rossum778983b1993-02-19 15:55:02 +00001279{
Peter Schneider-Kampdc71cac2000-07-31 21:57:30 +00001280 if (!PyArg_ParseTuple(args, ":tostring"))
Guido van Rossum778983b1993-02-19 15:55:02 +00001281 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001282 return PyString_FromStringAndSize(self->ob_item,
Guido van Rossum778983b1993-02-19 15:55:02 +00001283 self->ob_size * self->ob_descr->itemsize);
1284}
1285
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001286static char tostring_doc [] =
1287"tostring() -> string\n\
1288\n\
1289Convert the array to an array of machine values and return the string\n\
1290representation.";
1291
Martin v. Löwis99866332002-03-01 10:27:01 +00001292
1293
1294#ifdef Py_USING_UNICODE
1295static PyObject *
1296array_fromunicode(arrayobject *self, PyObject *args)
1297{
1298 Py_UNICODE *ustr;
1299 int n;
1300
1301 if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
1302 return NULL;
1303 if (self->ob_descr->typecode != 'u') {
1304 PyErr_SetString(PyExc_ValueError,
1305 "fromunicode() may only be called on "
1306 "type 'u' arrays");
1307 return NULL;
1308 }
1309 if (n > 0) {
1310 Py_UNICODE *item = (Py_UNICODE *) self->ob_item;
1311 PyMem_RESIZE(item, Py_UNICODE, self->ob_size + n);
1312 if (item == NULL) {
1313 PyErr_NoMemory();
1314 return NULL;
1315 }
1316 self->ob_item = (char *) item;
1317 self->ob_size += n;
1318 memcpy(item + self->ob_size - n,
1319 ustr, n * sizeof(Py_UNICODE));
1320 }
1321
1322 Py_INCREF(Py_None);
1323 return Py_None;
1324}
1325
1326static char fromunicode_doc[] =
1327"fromunicode(ustr)\n\
1328\n\
1329Extends this array with data from the unicode string ustr.\n\
1330The array must be a type 'u' array; otherwise a ValueError\n\
1331is raised. Use array.fromstring(ustr.decode(...)) to\n\
1332append Unicode data to an array of some other type.";
1333
1334
1335static PyObject *
1336array_tounicode(arrayobject *self, PyObject *args)
1337{
1338 if (!PyArg_ParseTuple(args, ":tounicode"))
1339 return NULL;
1340 if (self->ob_descr->typecode != 'u') {
1341 PyErr_SetString(PyExc_ValueError,
1342 "tounicode() may only be called on type 'u' arrays");
1343 return NULL;
1344 }
1345 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, self->ob_size);
1346}
1347
1348static char tounicode_doc [] =
1349"tounicode() -> unicode\n\
1350\n\
1351Convert the array to a unicode string. The array must be\n\
1352a type 'u' array; otherwise a ValueError is raised. Use\n\
1353array.tostring().decode() to obtain a unicode string from\n\
1354an array of some other type.";
1355
1356#endif /* Py_USING_UNICODE */
1357
1358
1359static PyObject *
1360array_get_typecode(arrayobject *a, void *closure)
1361{
1362 char tc = a->ob_descr->typecode;
1363 return PyString_FromStringAndSize(&tc, 1);
1364}
1365
1366static PyObject *
1367array_get_itemsize(arrayobject *a, void *closure)
1368{
1369 return PyInt_FromLong((long)a->ob_descr->itemsize);
1370}
1371
1372static PyGetSetDef array_getsets [] = {
1373 {"typecode", (getter) array_get_typecode, NULL,
1374 "the typecode character used to create the array"},
1375 {"itemsize", (getter) array_get_itemsize, NULL,
1376 "the size, in bytes, of one array item"},
1377 {NULL}
1378};
1379
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001380PyMethodDef array_methods[] = {
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001381 {"append", (PyCFunction)array_append, METH_VARARGS,
1382 append_doc},
1383 {"buffer_info", (PyCFunction)array_buffer_info, METH_VARARGS,
1384 buffer_info_doc},
1385 {"byteswap", (PyCFunction)array_byteswap, METH_VARARGS,
1386 byteswap_doc},
1387 {"count", (PyCFunction)array_count, METH_VARARGS,
1388 count_doc},
1389 {"extend", (PyCFunction)array_extend, METH_VARARGS,
1390 extend_doc},
1391 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
1392 fromfile_doc},
1393 {"fromlist", (PyCFunction)array_fromlist, METH_VARARGS,
1394 fromlist_doc},
1395 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
1396 fromstring_doc},
Martin v. Löwis99866332002-03-01 10:27:01 +00001397#ifdef Py_USING_UNICODE
1398 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
1399 fromunicode_doc},
1400#endif
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001401 {"index", (PyCFunction)array_index, METH_VARARGS,
1402 index_doc},
1403 {"insert", (PyCFunction)array_insert, METH_VARARGS,
1404 insert_doc},
1405 {"pop", (PyCFunction)array_pop, METH_VARARGS,
1406 pop_doc},
1407 {"read", (PyCFunction)array_fromfile, METH_VARARGS,
1408 fromfile_doc},
1409 {"remove", (PyCFunction)array_remove, METH_VARARGS,
1410 remove_doc},
1411 {"reverse", (PyCFunction)array_reverse, METH_VARARGS,
1412 reverse_doc},
1413/* {"sort", (PyCFunction)array_sort, METH_VARARGS,
1414 sort_doc},*/
1415 {"tofile", (PyCFunction)array_tofile, METH_VARARGS,
1416 tofile_doc},
1417 {"tolist", (PyCFunction)array_tolist, METH_VARARGS,
1418 tolist_doc},
1419 {"tostring", (PyCFunction)array_tostring, METH_VARARGS,
1420 tostring_doc},
Martin v. Löwis99866332002-03-01 10:27:01 +00001421#ifdef Py_USING_UNICODE
1422 {"tounicode", (PyCFunction)array_tounicode, METH_VARARGS,
1423 tounicode_doc},
1424#endif
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001425 {"write", (PyCFunction)array_tofile, METH_VARARGS,
1426 tofile_doc},
Guido van Rossum778983b1993-02-19 15:55:02 +00001427 {NULL, NULL} /* sentinel */
1428};
1429
Guido van Rossum778983b1993-02-19 15:55:02 +00001430static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001431array_print(arrayobject *a, FILE *fp, int flags)
Guido van Rossum778983b1993-02-19 15:55:02 +00001432{
1433 int ok = 0;
1434 int i, len;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001435 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +00001436 len = a->ob_size;
1437 if (len == 0) {
1438 fprintf(fp, "array('%c')", a->ob_descr->typecode);
1439 return ok;
1440 }
1441 if (a->ob_descr->typecode == 'c') {
Tim Peters7c1cb462000-11-10 19:04:19 +00001442 PyObject *t_empty = PyTuple_New(0);
Guido van Rossum778983b1993-02-19 15:55:02 +00001443 fprintf(fp, "array('c', ");
Peter Schneider-Kampdc71cac2000-07-31 21:57:30 +00001444 v = array_tostring(a, t_empty);
Tim Peters7c1cb462000-11-10 19:04:19 +00001445 Py_DECREF(t_empty);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001446 ok = PyObject_Print(v, fp, 0);
1447 Py_XDECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001448 fprintf(fp, ")");
1449 return ok;
1450 }
1451 fprintf(fp, "array('%c', [", a->ob_descr->typecode);
1452 for (i = 0; i < len && ok == 0; i++) {
1453 if (i > 0)
1454 fprintf(fp, ", ");
1455 v = (a->ob_descr->getitem)(a, i);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001456 ok = PyObject_Print(v, fp, 0);
1457 Py_XDECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001458 }
1459 fprintf(fp, "])");
1460 return ok;
1461}
1462
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001463static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001464array_repr(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +00001465{
Martin v. Löwis99866332002-03-01 10:27:01 +00001466 char buf[256], typecode;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001467 PyObject *s, *t, *comma, *v;
Guido van Rossum778983b1993-02-19 15:55:02 +00001468 int i, len;
Martin v. Löwis99866332002-03-01 10:27:01 +00001469
Guido van Rossum778983b1993-02-19 15:55:02 +00001470 len = a->ob_size;
Martin v. Löwis99866332002-03-01 10:27:01 +00001471 typecode = a->ob_descr->typecode;
Guido van Rossum778983b1993-02-19 15:55:02 +00001472 if (len == 0) {
Martin v. Löwis99866332002-03-01 10:27:01 +00001473 PyOS_snprintf(buf, sizeof(buf), "array('%c')", typecode);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001474 return PyString_FromString(buf);
Guido van Rossum778983b1993-02-19 15:55:02 +00001475 }
Martin v. Löwis99866332002-03-01 10:27:01 +00001476
1477 if (typecode == 'c' || typecode == 'u') {
Tim Peters7c1cb462000-11-10 19:04:19 +00001478 PyObject *t_empty = PyTuple_New(0);
Martin v. Löwis99866332002-03-01 10:27:01 +00001479 PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001480 s = PyString_FromString(buf);
Martin v. Löwis99866332002-03-01 10:27:01 +00001481 if (typecode == 'c')
1482 v = array_tostring(a, t_empty);
1483 else
1484 v = array_tounicode(a, t_empty);
Tim Peters7c1cb462000-11-10 19:04:19 +00001485 Py_DECREF(t_empty);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001486 t = PyObject_Repr(v);
1487 Py_XDECREF(v);
1488 PyString_ConcatAndDel(&s, t);
1489 PyString_ConcatAndDel(&s, PyString_FromString(")"));
Guido van Rossum778983b1993-02-19 15:55:02 +00001490 return s;
1491 }
Martin v. Löwis99866332002-03-01 10:27:01 +00001492 PyOS_snprintf(buf, sizeof(buf), "array('%c', [", typecode);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001493 s = PyString_FromString(buf);
1494 comma = PyString_FromString(", ");
1495 for (i = 0; i < len && !PyErr_Occurred(); i++) {
Guido van Rossumb6775db1994-08-01 11:34:53 +00001496 if (i > 0)
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001497 PyString_Concat(&s, comma);
Guido van Rossum778983b1993-02-19 15:55:02 +00001498 v = (a->ob_descr->getitem)(a, i);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001499 t = PyObject_Repr(v);
1500 Py_XDECREF(v);
1501 PyString_ConcatAndDel(&s, t);
Guido van Rossum778983b1993-02-19 15:55:02 +00001502 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001503 Py_XDECREF(comma);
1504 PyString_ConcatAndDel(&s, PyString_FromString("])"));
Guido van Rossum778983b1993-02-19 15:55:02 +00001505 return s;
1506}
1507
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001508static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001509array_buffer_getreadbuf(arrayobject *self, int index, const void **ptr)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001510{
1511 if ( index != 0 ) {
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001512 PyErr_SetString(PyExc_SystemError,
1513 "Accessing non-existent array segment");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001514 return -1;
1515 }
1516 *ptr = (void *)self->ob_item;
1517 return self->ob_size*self->ob_descr->itemsize;
1518}
1519
1520static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001521array_buffer_getwritebuf(arrayobject *self, int index, const void **ptr)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001522{
1523 if ( index != 0 ) {
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001524 PyErr_SetString(PyExc_SystemError,
1525 "Accessing non-existent array segment");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001526 return -1;
1527 }
1528 *ptr = (void *)self->ob_item;
1529 return self->ob_size*self->ob_descr->itemsize;
1530}
1531
1532static int
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00001533array_buffer_getsegcount(arrayobject *self, int *lenp)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001534{
1535 if ( lenp )
1536 *lenp = self->ob_size*self->ob_descr->itemsize;
1537 return 1;
1538}
1539
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001540static PySequenceMethods array_as_sequence = {
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001541 (inquiry)array_length, /*sq_length*/
1542 (binaryfunc)array_concat, /*sq_concat*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001543 (intargfunc)array_repeat, /*sq_repeat*/
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001544 (intargfunc)array_item, /*sq_item*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001545 (intintargfunc)array_slice, /*sq_slice*/
1546 (intobjargproc)array_ass_item, /*sq_ass_item*/
1547 (intintobjargproc)array_ass_slice, /*sq_ass_slice*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001548 NULL, /*sq_contains*/
1549 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
1550 (intargfunc)array_inplace_repeat /*sq_inplace_repeat*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001551};
1552
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001553static PyBufferProcs array_as_buffer = {
1554 (getreadbufferproc)array_buffer_getreadbuf,
1555 (getwritebufferproc)array_buffer_getwritebuf,
1556 (getsegcountproc)array_buffer_getsegcount,
1557};
1558
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001559static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +00001560array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum778983b1993-02-19 15:55:02 +00001561{
1562 char c;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001563 PyObject *initial = NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001564 struct arraydescr *descr;
Martin v. Löwis99866332002-03-01 10:27:01 +00001565
1566 if (kwds != NULL) {
1567 int i = PyObject_Length(kwds);
1568 if (i < 0)
Guido van Rossum778983b1993-02-19 15:55:02 +00001569 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001570 else if (i > 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001571 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis99866332002-03-01 10:27:01 +00001572 "array.array constructor takes "
1573 "no keyword arguments");
Guido van Rossum778983b1993-02-19 15:55:02 +00001574 return NULL;
1575 }
1576 }
Martin v. Löwis99866332002-03-01 10:27:01 +00001577
1578 if (!PyArg_ParseTuple(args, "c|O:array", &c, &initial))
1579 return NULL;
1580
1581 if (!(initial == NULL || PyList_Check(initial)
1582 || PyString_Check(initial)
1583 || (c == 'u' && PyUnicode_Check(initial)))) {
1584 PyErr_SetString(PyExc_TypeError,
1585 "array initializer must be list or string");
1586 return NULL;
1587 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001588 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1589 if (descr->typecode == c) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001590 PyObject *a;
Guido van Rossum778983b1993-02-19 15:55:02 +00001591 int len;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001592 if (initial == NULL || !PyList_Check(initial))
Guido van Rossum778983b1993-02-19 15:55:02 +00001593 len = 0;
1594 else
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001595 len = PyList_Size(initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001596
1597 a = newarrayobject(type, len, descr);
Guido van Rossum778983b1993-02-19 15:55:02 +00001598 if (a == NULL)
1599 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00001600
Guido van Rossum778983b1993-02-19 15:55:02 +00001601 if (len > 0) {
1602 int i;
1603 for (i = 0; i < len; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001604 PyObject *v =
Martin v. Löwis99866332002-03-01 10:27:01 +00001605 PyList_GetItem(initial, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001606 if (setarrayitem(a, i, v) != 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001607 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001608 return NULL;
1609 }
1610 }
1611 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001612 if (initial != NULL && PyString_Check(initial)) {
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001613 PyObject *t_initial = Py_BuildValue("(O)",
1614 initial);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001615 PyObject *v =
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001616 array_fromstring((arrayobject *)a,
1617 t_initial);
Martin v. Löwis99866332002-03-01 10:27:01 +00001618 Py_DECREF(t_initial);
Guido van Rossum778983b1993-02-19 15:55:02 +00001619 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001620 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001621 return NULL;
1622 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001623 Py_DECREF(v);
Martin v. Löwis99866332002-03-01 10:27:01 +00001624#ifdef Py_USING_UNICODE
1625 } else if (initial != NULL && PyUnicode_Check(initial)) {
1626 int n = PyUnicode_GET_DATA_SIZE(initial);
1627 if (n > 0) {
1628 arrayobject *self = (arrayobject *)a;
1629 char *item = self->ob_item;
1630 item = PyMem_Realloc(item, n);
1631 if (item == NULL) {
1632 PyErr_NoMemory();
1633 Py_DECREF(a);
1634 return NULL;
1635 }
1636 self->ob_item = item;
1637 self->ob_size = n / sizeof(Py_UNICODE);
1638 memcpy(item, PyUnicode_AS_DATA(initial), n);
1639 }
1640#endif
Guido van Rossum778983b1993-02-19 15:55:02 +00001641 }
1642 return a;
1643 }
1644 }
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001645 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis99866332002-03-01 10:27:01 +00001646 "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 +00001647 return NULL;
1648}
1649
Guido van Rossum778983b1993-02-19 15:55:02 +00001650
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001651static char module_doc [] =
Martin v. Löwis99866332002-03-01 10:27:01 +00001652"This module defines an object type which can efficiently represent\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001653an array of basic values: characters, integers, floating point\n\
1654numbers. Arrays are sequence types and behave very much like lists,\n\
1655except that the type of objects stored in them is constrained. The\n\
1656type is specified at object creation time by using a type code, which\n\
1657is a single character. The following type codes are defined:\n\
1658\n\
1659 Type code C Type Minimum size in bytes \n\
1660 'c' character 1 \n\
1661 'b' signed integer 1 \n\
1662 'B' unsigned integer 1 \n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001663 'u' Unicode character 2 \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001664 'h' signed integer 2 \n\
1665 'H' unsigned integer 2 \n\
1666 'i' signed integer 2 \n\
1667 'I' unsigned integer 2 \n\
1668 'l' signed integer 4 \n\
1669 'L' unsigned integer 4 \n\
1670 'f' floating point 4 \n\
1671 'd' floating point 8 \n\
1672\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001673The constructor is:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001674\n\
1675array(typecode [, initializer]) -- create a new array\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001676";
1677
1678static char arraytype_doc [] =
Martin v. Löwis99866332002-03-01 10:27:01 +00001679"array(typecode [, initializer]) -> array\n\
1680\n\
1681Return a new array whose items are restricted by typecode, and\n\
1682initialized from the optional initializer value, which must be a list\n\
1683or a string.\n\
1684\n\
1685Arrays represent basic values and behave very much like lists, except\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001686the type of objects stored in them is constrained.\n\
1687\n\
1688Methods:\n\
1689\n\
1690append() -- append a new item to the end of the array\n\
1691buffer_info() -- return information giving the current memory info\n\
1692byteswap() -- byteswap all the items of the array\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001693count() -- return number of occurences of an object\n\
1694extend() -- extend array by appending array elements\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001695fromfile() -- read items from a file object\n\
1696fromlist() -- append items from the list\n\
1697fromstring() -- append items from the string\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001698index() -- return index of first occurence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001699insert() -- insert a new item into the array at a provided position\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001700pop() -- remove and return item (default last)\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001701read() -- DEPRECATED, use fromfile()\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001702remove() -- remove first occurence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001703reverse() -- reverse the order of the items in the array\n\
1704tofile() -- write all items to a file object\n\
1705tolist() -- return the array converted to an ordinary list\n\
1706tostring() -- return the array converted to a string\n\
1707write() -- DEPRECATED, use tofile()\n\
1708\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00001709Attributes:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001710\n\
1711typecode -- the typecode character used to create the array\n\
1712itemsize -- the length in bytes of one array item\n\
1713";
1714
1715statichere PyTypeObject Arraytype = {
Fred Drake0d40ba42000-02-04 20:33:49 +00001716 PyObject_HEAD_INIT(NULL)
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001717 0,
Guido van Rossum14648392001-12-08 18:02:58 +00001718 "array.array",
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001719 sizeof(arrayobject),
1720 0,
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001721 (destructor)array_dealloc, /* tp_dealloc */
1722 (printfunc)array_print, /* tp_print */
Martin v. Löwis99866332002-03-01 10:27:01 +00001723 0, /* tp_getattr */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001724 0, /* tp_setattr */
1725 0, /* tp_compare */
1726 (reprfunc)array_repr, /* tp_repr */
1727 0, /* tp_as _number*/
1728 &array_as_sequence, /* tp_as _sequence*/
1729 0, /* tp_as _mapping*/
1730 0, /* tp_hash */
1731 0, /* tp_call */
1732 0, /* tp_str */
Martin v. Löwis99866332002-03-01 10:27:01 +00001733 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001734 0, /* tp_setattro */
Tim Petersb870c752001-06-05 04:43:26 +00001735 &array_as_buffer, /* tp_as_buffer*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001736 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum9d19cb82001-01-18 01:02:55 +00001737 arraytype_doc, /* tp_doc */
1738 0, /* tp_traverse */
1739 0, /* tp_clear */
1740 array_richcompare, /* tp_richcompare */
Martin v. Löwis99866332002-03-01 10:27:01 +00001741 0, /* tp_weaklistoffset */
1742 0, /* tp_iter */
1743 0, /* tp_iternext */
1744 array_methods, /* tp_methods */
1745 0, /* tp_members */
1746 array_getsets, /* tp_getset */
1747 0, /* tp_base */
1748 0, /* tp_dict */
1749 0, /* tp_descr_get */
1750 0, /* tp_descr_set */
1751 0, /* tp_dictoffset */
1752 0, /* tp_init */
1753 PyType_GenericAlloc, /* tp_alloc */
1754 array_new, /* tp_new */
1755 _PyObject_Del, /* tp_free */
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001756};
1757
Martin v. Löwis99866332002-03-01 10:27:01 +00001758/* No functions in array module. */
1759static PyMethodDef a_methods[] = {
1760 {NULL, NULL, 0, NULL} /* Sentinel */
1761};
1762
1763
Guido van Rossum3886bb61998-12-04 18:50:17 +00001764DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001765initarray(void)
Guido van Rossum778983b1993-02-19 15:55:02 +00001766{
Guido van Rossumb6190d31997-05-22 14:56:36 +00001767 PyObject *m, *d;
Fred Drake0d40ba42000-02-04 20:33:49 +00001768
Martin v. Löwis99866332002-03-01 10:27:01 +00001769 Arraytype.ob_type = &PyType_Type;
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001770 m = Py_InitModule3("array", a_methods, module_doc);
Guido van Rossumb6190d31997-05-22 14:56:36 +00001771 d = PyModule_GetDict(m);
Guido van Rossuma0deb641998-10-14 13:45:06 +00001772 PyDict_SetItemString(d, "ArrayType", (PyObject *)&Arraytype);
Martin v. Löwis99866332002-03-01 10:27:01 +00001773 PyDict_SetItemString(d, "array", (PyObject *)&Arraytype);
Guido van Rossuma0deb641998-10-14 13:45:06 +00001774 /* No need to check the error here, the caller will do that */
Guido van Rossum778983b1993-02-19 15:55:02 +00001775}