blob: 656f5a68af1bed999857dd6eca7e1074a21e5218 [file] [log] [blame]
Guido van Rossum778983b1993-02-19 15:55:02 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum778983b1993-02-19 15:55:02 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum778983b1993-02-19 15:55:02 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum778983b1993-02-19 15:55:02 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum778983b1993-02-19 15:55:02 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum778983b1993-02-19 15:55:02 +000029
30******************************************************************/
31
32/* Array object implementation */
33
34/* An array is a uniform list -- all items have the same type.
35 The item type is restricted to simple C types like int or float */
36
Roger E. Masse2919eaa1996-12-09 20:10:36 +000037#include "Python.h"
Roger E. Masse5817f8f1996-12-09 22:24:19 +000038
Guido van Rossum0c709541994-08-19 12:01:32 +000039#ifdef STDC_HEADERS
40#include <stddef.h>
41#else
Guido van Rossumb6775db1994-08-01 11:34:53 +000042#include <sys/types.h> /* For size_t */
Guido van Rossum0c709541994-08-19 12:01:32 +000043#endif
Guido van Rossum778983b1993-02-19 15:55:02 +000044
45struct arrayobject; /* Forward */
46
47struct arraydescr {
48 int typecode;
49 int itemsize;
Roger E. Masse2919eaa1996-12-09 20:10:36 +000050 PyObject * (*getitem) Py_FPROTO((struct arrayobject *, int));
51 int (*setitem) Py_FPROTO((struct arrayobject *, int, PyObject *));
Guido van Rossum778983b1993-02-19 15:55:02 +000052};
53
54typedef struct arrayobject {
Roger E. Masse2919eaa1996-12-09 20:10:36 +000055 PyObject_VAR_HEAD
Guido van Rossum778983b1993-02-19 15:55:02 +000056 char *ob_item;
57 struct arraydescr *ob_descr;
58} arrayobject;
59
Roger E. Masse2919eaa1996-12-09 20:10:36 +000060staticforward PyTypeObject Arraytype;
Guido van Rossum778983b1993-02-19 15:55:02 +000061
62#define is_arrayobject(op) ((op)->ob_type == &Arraytype)
63
Guido van Rossumb73cc041993-11-01 16:28:59 +000064/* Forward */
Roger E. Masse2919eaa1996-12-09 20:10:36 +000065static PyObject *newarrayobject Py_PROTO((int, struct arraydescr *));
Guido van Rossuma376cc51996-12-05 23:43:35 +000066#if 0
Roger E. Masse2919eaa1996-12-09 20:10:36 +000067static int getarraysize Py_PROTO((PyObject *));
Guido van Rossuma376cc51996-12-05 23:43:35 +000068#endif
Roger E. Masse2919eaa1996-12-09 20:10:36 +000069static PyObject *getarrayitem Py_PROTO((PyObject *, int));
70static int setarrayitem Py_PROTO((PyObject *, int, PyObject *));
Guido van Rossuma376cc51996-12-05 23:43:35 +000071#if 0
Roger E. Masse2919eaa1996-12-09 20:10:36 +000072static int insarrayitem Py_PROTO((PyObject *, int, PyObject *));
73static int addarrayitem Py_PROTO((PyObject *, PyObject *));
Guido van Rossuma376cc51996-12-05 23:43:35 +000074#endif
Guido van Rossum778983b1993-02-19 15:55:02 +000075
Roger E. Masse2919eaa1996-12-09 20:10:36 +000076static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +000077c_getitem(ap, i)
78 arrayobject *ap;
79 int i;
80{
Roger E. Masse2919eaa1996-12-09 20:10:36 +000081 return PyString_FromStringAndSize(&((char *)ap->ob_item)[i], 1);
Guido van Rossum778983b1993-02-19 15:55:02 +000082}
83
84static int
85c_setitem(ap, i, v)
86 arrayobject *ap;
87 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +000088 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +000089{
90 char x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +000091 if (!PyArg_Parse(v, "c;array item must be char", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +000092 return -1;
93 if (i >= 0)
94 ((char *)ap->ob_item)[i] = x;
95 return 0;
96}
97
Roger E. Masse2919eaa1996-12-09 20:10:36 +000098static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +000099b_getitem(ap, i)
100 arrayobject *ap;
101 int i;
102{
103 long x = ((char *)ap->ob_item)[i];
104 if (x >= 128)
105 x -= 256;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000106 return PyInt_FromLong(x);
Guido van Rossum778983b1993-02-19 15:55:02 +0000107}
108
109static int
110b_setitem(ap, i, v)
111 arrayobject *ap;
112 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000113 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000114{
115 char x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000116 if (!PyArg_Parse(v, "b;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000117 return -1;
118 if (i >= 0)
119 ((char *)ap->ob_item)[i] = x;
120 return 0;
121}
122
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000123static PyObject *
Guido van Rossum1a747f81997-05-16 16:21:38 +0000124BB_getitem(ap, i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000125 arrayobject *ap;
126 int i;
127{
128 long x = ((unsigned char *)ap->ob_item)[i];
129 return PyInt_FromLong(x);
130}
131
Guido van Rossum1a747f81997-05-16 16:21:38 +0000132#define BB_setitem b_setitem
Guido van Rossum549ab711997-01-03 19:09:47 +0000133
134static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000135h_getitem(ap, i)
136 arrayobject *ap;
137 int i;
138{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000139 return PyInt_FromLong((long) ((short *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000140}
141
142static int
143h_setitem(ap, i, v)
144 arrayobject *ap;
145 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000146 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000147{
148 short x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000149 if (!PyArg_Parse(v, "h;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000150 return -1;
151 if (i >= 0)
152 ((short *)ap->ob_item)[i] = x;
153 return 0;
154}
155
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000156static PyObject *
Guido van Rossum1a747f81997-05-16 16:21:38 +0000157HH_getitem(ap, i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000158 arrayobject *ap;
159 int i;
160{
161 return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
162}
163
Guido van Rossum1a747f81997-05-16 16:21:38 +0000164#define HH_setitem h_setitem
Guido van Rossum549ab711997-01-03 19:09:47 +0000165
166static PyObject *
Guido van Rossume77a7571993-11-03 15:01:26 +0000167i_getitem(ap, i)
168 arrayobject *ap;
169 int i;
170{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000171 return PyInt_FromLong((long) ((int *)ap->ob_item)[i]);
Guido van Rossume77a7571993-11-03 15:01:26 +0000172}
173
174static int
175i_setitem(ap, i, v)
176 arrayobject *ap;
177 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000178 PyObject *v;
Guido van Rossume77a7571993-11-03 15:01:26 +0000179{
180 int x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000181 if (!PyArg_Parse(v, "i;array item must be integer", &x))
Guido van Rossume77a7571993-11-03 15:01:26 +0000182 return -1;
183 if (i >= 0)
184 ((int *)ap->ob_item)[i] = x;
185 return 0;
186}
187
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000188static PyObject *
Guido van Rossum1a747f81997-05-16 16:21:38 +0000189II_getitem(ap, i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000190 arrayobject *ap;
191 int i;
192{
193 return PyLong_FromUnsignedLong(
194 (unsigned long) ((unsigned int *)ap->ob_item)[i]);
195}
196
197static int
Guido van Rossum1a747f81997-05-16 16:21:38 +0000198II_setitem(ap, i, v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000199 arrayobject *ap;
200 int i;
201 PyObject *v;
202{
203 unsigned long x;
204 if (PyLong_Check(v)) {
205 x = PyLong_AsUnsignedLong(v);
206 if (x == (unsigned long) -1 && PyErr_Occurred())
207 return -1;
208 }
209 else {
210 if (!PyArg_Parse(v, "l;array item must be integer", &x))
211 return -1;
212 }
213 if (i >= 0)
214 ((unsigned int *)ap->ob_item)[i] = x;
215 return 0;
216}
217
218static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000219l_getitem(ap, i)
220 arrayobject *ap;
221 int i;
222{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000223 return PyInt_FromLong(((long *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000224}
225
226static int
227l_setitem(ap, i, v)
228 arrayobject *ap;
229 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000230 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000231{
232 long x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000233 if (!PyArg_Parse(v, "l;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000234 return -1;
235 if (i >= 0)
236 ((long *)ap->ob_item)[i] = x;
237 return 0;
238}
239
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000240static PyObject *
Guido van Rossum1a747f81997-05-16 16:21:38 +0000241LL_getitem(ap, i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000242 arrayobject *ap;
243 int i;
244{
245 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
246}
247
248static int
Guido van Rossum1a747f81997-05-16 16:21:38 +0000249LL_setitem(ap, i, v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000250 arrayobject *ap;
251 int i;
252 PyObject *v;
253{
254 unsigned long x;
255 if (PyLong_Check(v)) {
256 x = PyLong_AsUnsignedLong(v);
257 if (x == (unsigned long) -1 && PyErr_Occurred())
258 return -1;
259 }
260 else {
261 if (!PyArg_Parse(v, "l;array item must be integer", &x))
262 return -1;
263 }
264 if (i >= 0)
265 ((unsigned long *)ap->ob_item)[i] = x;
266 return 0;
267}
268
269static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000270f_getitem(ap, i)
271 arrayobject *ap;
272 int i;
273{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000274 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000275}
276
277static int
278f_setitem(ap, i, v)
279 arrayobject *ap;
280 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000281 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000282{
283 float x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000284 if (!PyArg_Parse(v, "f;array item must be float", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000285 return -1;
286 if (i >= 0)
287 ((float *)ap->ob_item)[i] = x;
288 return 0;
289}
290
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000291static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000292d_getitem(ap, i)
293 arrayobject *ap;
294 int i;
295{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000296 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000297}
298
299static int
300d_setitem(ap, i, v)
301 arrayobject *ap;
302 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000303 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000304{
305 double x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000306 if (!PyArg_Parse(v, "d;array item must be float", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000307 return -1;
308 if (i >= 0)
309 ((double *)ap->ob_item)[i] = x;
310 return 0;
311}
312
313/* Description of types */
Guido van Rossum234f9421993-06-17 12:35:49 +0000314static struct arraydescr descriptors[] = {
Guido van Rossum778983b1993-02-19 15:55:02 +0000315 {'c', sizeof(char), c_getitem, c_setitem},
316 {'b', sizeof(char), b_getitem, b_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000317 {'B', sizeof(char), BB_getitem, BB_setitem},
Guido van Rossum778983b1993-02-19 15:55:02 +0000318 {'h', sizeof(short), h_getitem, h_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000319 {'H', sizeof(short), HH_getitem, HH_setitem},
Guido van Rossume77a7571993-11-03 15:01:26 +0000320 {'i', sizeof(int), i_getitem, i_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000321 {'I', sizeof(int), II_getitem, II_setitem},
Guido van Rossum778983b1993-02-19 15:55:02 +0000322 {'l', sizeof(long), l_getitem, l_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000323 {'L', sizeof(long), LL_getitem, LL_setitem},
Guido van Rossum778983b1993-02-19 15:55:02 +0000324 {'f', sizeof(float), f_getitem, f_setitem},
325 {'d', sizeof(double), d_getitem, d_setitem},
326 {'\0', 0, 0, 0} /* Sentinel */
327};
Guido van Rossume77a7571993-11-03 15:01:26 +0000328/* If we ever allow items larger than double, we must change reverse()! */
Guido van Rossum778983b1993-02-19 15:55:02 +0000329
330
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000331static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000332newarrayobject(size, descr)
333 int size;
334 struct arraydescr *descr;
335{
Guido van Rossum778983b1993-02-19 15:55:02 +0000336 arrayobject *op;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000337 size_t nbytes;
Guido van Rossum778983b1993-02-19 15:55:02 +0000338 if (size < 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000339 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000340 return NULL;
341 }
342 nbytes = size * descr->itemsize;
343 /* Check for overflow */
Guido van Rossum7844e381997-04-11 20:44:04 +0000344 if (nbytes / descr->itemsize != (size_t)size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000345 return PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000346 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000347 op = PyMem_NEW(arrayobject, 1);
Guido van Rossum778983b1993-02-19 15:55:02 +0000348 if (op == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000349 return PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000350 }
351 if (size <= 0) {
352 op->ob_item = NULL;
353 }
354 else {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000355 op->ob_item = PyMem_NEW(char, nbytes);
Guido van Rossum778983b1993-02-19 15:55:02 +0000356 if (op->ob_item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000357 PyMem_DEL(op);
358 return PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000359 }
360 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000361 op->ob_type = &Arraytype;
362 op->ob_size = size;
363 op->ob_descr = descr;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000364 _Py_NewReference(op);
365 return (PyObject *) op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000366}
367
Guido van Rossuma376cc51996-12-05 23:43:35 +0000368#if 0
Guido van Rossum62de97f1995-01-22 00:48:41 +0000369static int
Guido van Rossum778983b1993-02-19 15:55:02 +0000370getarraysize(op)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000371 PyObject *op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000372{
373 if (!is_arrayobject(op)) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000374 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000375 return -1;
376 }
377 return ((arrayobject *)op) -> ob_size;
378}
Guido van Rossuma376cc51996-12-05 23:43:35 +0000379#endif
Guido van Rossum778983b1993-02-19 15:55:02 +0000380
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000381static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000382getarrayitem(op, i)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000383 PyObject *op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000384 int i;
385{
386 register arrayobject *ap;
387 if (!is_arrayobject(op)) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000388 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000389 return NULL;
390 }
391 ap = (arrayobject *)op;
392 if (i < 0 || i >= ap->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000393 PyErr_SetString(PyExc_IndexError, "array index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000394 return NULL;
395 }
396 return (*ap->ob_descr->getitem)(ap, i);
397}
398
399static int
400ins1(self, where, v)
401 arrayobject *self;
402 int where;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000403 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000404{
Guido van Rossum778983b1993-02-19 15:55:02 +0000405 char *items;
406 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000407 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000408 return -1;
409 }
410 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
411 return -1;
412 items = self->ob_item;
Roger E. Masse5817f8f1996-12-09 22:24:19 +0000413 PyMem_RESIZE(items, char,
414 (self->ob_size+1) * self->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000415 if (items == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000416 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000417 return -1;
418 }
419 if (where < 0)
420 where = 0;
421 if (where > self->ob_size)
422 where = self->ob_size;
423 memmove(items + (where+1)*self->ob_descr->itemsize,
424 items + where*self->ob_descr->itemsize,
425 (self->ob_size-where)*self->ob_descr->itemsize);
426 self->ob_item = items;
427 self->ob_size++;
428 return (*self->ob_descr->setitem)(self, where, v);
429}
430
Guido van Rossuma376cc51996-12-05 23:43:35 +0000431#if 0
Guido van Rossum62de97f1995-01-22 00:48:41 +0000432static int
Guido van Rossum778983b1993-02-19 15:55:02 +0000433insarrayitem(op, where, newitem)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000434 PyObject *op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000435 int where;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000436 PyObject *newitem;
Guido van Rossum778983b1993-02-19 15:55:02 +0000437{
438 if (!is_arrayobject(op)) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000439 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000440 return -1;
441 }
442 return ins1((arrayobject *)op, where, newitem);
443}
444
Guido van Rossum62de97f1995-01-22 00:48:41 +0000445static int
Guido van Rossum778983b1993-02-19 15:55:02 +0000446addarrayitem(op, newitem)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000447 PyObject *op;
448 PyObject *newitem;
Guido van Rossum778983b1993-02-19 15:55:02 +0000449{
450 if (!is_arrayobject(op)) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000451 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000452 return -1;
453 }
454 return ins1((arrayobject *)op,
455 (int) ((arrayobject *)op)->ob_size, newitem);
456}
Guido van Rossuma376cc51996-12-05 23:43:35 +0000457#endif
Guido van Rossum778983b1993-02-19 15:55:02 +0000458
459/* Methods */
460
461static void
462array_dealloc(op)
463 arrayobject *op;
464{
Guido van Rossum778983b1993-02-19 15:55:02 +0000465 if (op->ob_item != NULL)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000466 PyMem_DEL(op->ob_item);
467 PyMem_DEL(op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000468}
469
470static int
471array_compare(v, w)
472 arrayobject *v, *w;
473{
474 int len = (v->ob_size < w->ob_size) ? v->ob_size : w->ob_size;
475 int i;
476 for (i = 0; i < len; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000477 PyObject *ai, *bi;
Guido van Rossum778983b1993-02-19 15:55:02 +0000478 int cmp;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000479 ai = getarrayitem((PyObject *)v, i);
480 bi = getarrayitem((PyObject *)w, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000481 if (ai && bi)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000482 cmp = PyObject_Compare(ai, bi);
Guido van Rossum778983b1993-02-19 15:55:02 +0000483 else
484 cmp = -1;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000485 Py_XDECREF(ai);
486 Py_XDECREF(bi);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000487 if (cmp != 0)
Guido van Rossum778983b1993-02-19 15:55:02 +0000488 return cmp;
Guido van Rossum778983b1993-02-19 15:55:02 +0000489 }
490 return v->ob_size - w->ob_size;
491}
492
493static int
494array_length(a)
495 arrayobject *a;
496{
497 return a->ob_size;
498}
499
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000500static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000501array_item(a, i)
502 arrayobject *a;
503 int i;
504{
505 if (i < 0 || i >= a->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000506 PyErr_SetString(PyExc_IndexError, "array index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000507 return NULL;
508 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000509 return getarrayitem((PyObject *)a, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000510}
511
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000512static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000513array_slice(a, ilow, ihigh)
514 arrayobject *a;
515 int ilow, ihigh;
516{
517 arrayobject *np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000518 if (ilow < 0)
519 ilow = 0;
520 else if (ilow > a->ob_size)
521 ilow = a->ob_size;
522 if (ihigh < 0)
523 ihigh = 0;
524 if (ihigh < ilow)
525 ihigh = ilow;
526 else if (ihigh > a->ob_size)
527 ihigh = a->ob_size;
528 np = (arrayobject *) newarrayobject(ihigh - ilow, a->ob_descr);
529 if (np == NULL)
530 return NULL;
531 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
532 (ihigh-ilow) * a->ob_descr->itemsize);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000533 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000534}
535
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000536static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000537array_concat(a, bb)
538 arrayobject *a;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000539 PyObject *bb;
Guido van Rossum778983b1993-02-19 15:55:02 +0000540{
541 int size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000542 arrayobject *np;
543 if (!is_arrayobject(bb)) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000544 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000545 return NULL;
546 }
547#define b ((arrayobject *)bb)
548 if (a->ob_descr != b->ob_descr) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000549 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000550 return NULL;
551 }
552 size = a->ob_size + b->ob_size;
553 np = (arrayobject *) newarrayobject(size, a->ob_descr);
554 if (np == NULL) {
555 return NULL;
556 }
557 memcpy(np->ob_item, a->ob_item, a->ob_size*a->ob_descr->itemsize);
558 memcpy(np->ob_item + a->ob_size*a->ob_descr->itemsize,
Guido van Rossum32be3a71993-11-05 10:16:27 +0000559 b->ob_item, b->ob_size*b->ob_descr->itemsize);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000560 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000561#undef b
562}
563
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000564static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000565array_repeat(a, n)
566 arrayobject *a;
567 int n;
568{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000569 int i;
Guido van Rossum778983b1993-02-19 15:55:02 +0000570 int size;
571 arrayobject *np;
572 char *p;
573 int nbytes;
574 if (n < 0)
575 n = 0;
576 size = a->ob_size * n;
577 np = (arrayobject *) newarrayobject(size, a->ob_descr);
578 if (np == NULL)
579 return NULL;
580 p = np->ob_item;
581 nbytes = a->ob_size * a->ob_descr->itemsize;
582 for (i = 0; i < n; i++) {
583 memcpy(p, a->ob_item, nbytes);
584 p += nbytes;
585 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000586 return (PyObject *) np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000587}
588
589static int
590array_ass_slice(a, ilow, ihigh, v)
591 arrayobject *a;
592 int ilow, ihigh;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000593 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000594{
595 char *item;
596 int n; /* Size of replacement array */
597 int d; /* Change in size */
Guido van Rossum778983b1993-02-19 15:55:02 +0000598#define b ((arrayobject *)v)
599 if (v == NULL)
600 n = 0;
601 else if (is_arrayobject(v)) {
602 n = b->ob_size;
603 if (a == b) {
604 /* Special case "a[i:j] = a" -- copy b first */
605 int ret;
606 v = array_slice(b, 0, n);
607 ret = array_ass_slice(a, ilow, ihigh, v);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000608 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000609 return ret;
610 }
611 if (b->ob_descr != a->ob_descr) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000612 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000613 return -1;
614 }
615 }
616 else {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000617 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000618 return -1;
619 }
620 if (ilow < 0)
621 ilow = 0;
622 else if (ilow > a->ob_size)
623 ilow = a->ob_size;
624 if (ihigh < 0)
625 ihigh = 0;
626 if (ihigh < ilow)
627 ihigh = ilow;
628 else if (ihigh > a->ob_size)
629 ihigh = a->ob_size;
630 item = a->ob_item;
631 d = n - (ihigh-ilow);
632 if (d < 0) { /* Delete -d items */
633 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
634 item + ihigh*a->ob_descr->itemsize,
635 (a->ob_size-ihigh)*a->ob_descr->itemsize);
636 a->ob_size += d;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000637 PyMem_RESIZE(item, char, a->ob_size*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000638 /* Can't fail */
639 a->ob_item = item;
640 }
641 else if (d > 0) { /* Insert d items */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000642 PyMem_RESIZE(item, char,
643 (a->ob_size + d)*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000644 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000645 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000646 return -1;
647 }
648 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
649 item + ihigh*a->ob_descr->itemsize,
650 (a->ob_size-ihigh)*a->ob_descr->itemsize);
651 a->ob_item = item;
652 a->ob_size += d;
653 }
654 if (n > 0)
655 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
656 n*b->ob_descr->itemsize);
657 return 0;
658#undef b
659}
660
661static int
662array_ass_item(a, i, v)
663 arrayobject *a;
664 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000665 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000666{
667 if (i < 0 || i >= a->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000668 PyErr_SetString(PyExc_IndexError,
669 "array assignment index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000670 return -1;
671 }
672 if (v == NULL)
673 return array_ass_slice(a, i, i+1, v);
674 return (*a->ob_descr->setitem)(a, i, v);
675}
676
677static int
678setarrayitem(a, i, v)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000679 PyObject *a;
Guido van Rossum778983b1993-02-19 15:55:02 +0000680 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000681 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000682{
683 if (!is_arrayobject(a)) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000684 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000685 return -1;
686 }
687 return array_ass_item((arrayobject *)a, i, v);
688}
689
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000690static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000691ins(self, where, v)
692 arrayobject *self;
693 int where;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000694 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000695{
696 if (ins1(self, where, v) != 0)
697 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000698 Py_INCREF(Py_None);
699 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000700}
701
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000702static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000703array_insert(self, args)
704 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000705 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000706{
707 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000708 PyObject *v;
709 if (!PyArg_Parse(args, "(iO)", &i, &v))
Guido van Rossum778983b1993-02-19 15:55:02 +0000710 return NULL;
711 return ins(self, i, v);
712}
713
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000714static char insert_doc [] =
715"insert (i,x)\n\
716\n\
717Insert a new item x into the array before position i.";
718
719
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000720static PyObject *
Guido van Rossumde4a4ca1997-08-12 14:55:56 +0000721array_buffer_info(self, args)
722 arrayobject *self;
723 PyObject *args;
724{
725 return Py_BuildValue("ll",
726 (long)(self->ob_item), (long)(self->ob_size));
727}
728
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000729static char buffer_info_doc [] =
730"buffer_info -> (address, length)\n\
731\n\
732Return a tuple (address, length) giving the current memory address and\n\
733the length in bytes of the buffer used to hold array's contents.";
734
735
Guido van Rossumde4a4ca1997-08-12 14:55:56 +0000736static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000737array_append(self, args)
738 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000739 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000740{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000741 PyObject *v;
742 if (!PyArg_Parse(args, "O", &v))
Guido van Rossum778983b1993-02-19 15:55:02 +0000743 return NULL;
744 return ins(self, (int) self->ob_size, v);
745}
746
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000747static char append_doc [] =
748"append(x)\n\
749\n\
750Append new value x to the end of the array.";
751
752
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000753static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000754array_byteswap(self, args)
755 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000756 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000757{
758 char *p;
759 int i;
760 switch (self->ob_descr->itemsize) {
761 case 1:
762 break;
763 case 2:
764 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 2) {
765 char p0 = p[0];
766 p[0] = p[1];
767 p[1] = p0;
768 }
769 break;
770 case 4:
771 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 4) {
772 char p0 = p[0];
773 char p1 = p[1];
774 p[0] = p[3];
775 p[1] = p[2];
776 p[2] = p1;
777 p[3] = p0;
778 }
779 break;
Guido van Rossume77a7571993-11-03 15:01:26 +0000780 case 8:
781 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 8) {
782 char p0 = p[0];
783 char p1 = p[1];
784 char p2 = p[2];
785 char p3 = p[3];
786 p[0] = p[7];
787 p[1] = p[6];
788 p[2] = p[5];
789 p[3] = p[4];
790 p[4] = p3;
791 p[5] = p2;
792 p[6] = p1;
793 p[7] = p0;
794 }
795 break;
Guido van Rossum778983b1993-02-19 15:55:02 +0000796 default:
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000797 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossum778983b1993-02-19 15:55:02 +0000798 "don't know how to byteswap this array type");
799 return NULL;
800 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000801 Py_INCREF(Py_None);
802 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000803}
804
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000805static char byteswap_doc [] =
806"byteswap(x)\n\
807\n\
808Byteswap all items of the array. This is only supported for integer\n\
809values of x, which determines the size of the blocks swapped.";
810
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000811static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000812array_reverse(self, args)
813 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000814 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000815{
Guido van Rossume77a7571993-11-03 15:01:26 +0000816 register int itemsize = self->ob_descr->itemsize;
817 register char *p, *q;
818 char tmp[sizeof(double)]; /* Assume that's the max item size */
819
Guido van Rossum778983b1993-02-19 15:55:02 +0000820 if (args != NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000821 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000822 return NULL;
823 }
824
825 if (self->ob_size > 1) {
Guido van Rossume77a7571993-11-03 15:01:26 +0000826 for (p = self->ob_item,
827 q = self->ob_item + (self->ob_size - 1)*itemsize;
828 p < q;
829 p += itemsize, q -= itemsize) {
830 memmove(tmp, p, itemsize);
831 memmove(p, q, itemsize);
832 memmove(q, tmp, itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000833 }
834 }
835
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000836 Py_INCREF(Py_None);
837 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000838}
Guido van Rossume77a7571993-11-03 15:01:26 +0000839
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000840static char reverse_doc [] =
841"reverse()\n\
842\n\
843Reverse the order of the items in the array.";
844
Guido van Rossume77a7571993-11-03 15:01:26 +0000845/* The following routines were adapted from listobject.c but not converted.
846 To make them work you will have to work! */
Guido van Rossum778983b1993-02-19 15:55:02 +0000847
848#if 0
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000849static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000850array_index(self, args)
851 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000852 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000853{
854 int i;
855
856 if (args == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000857 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000858 return NULL;
859 }
860 for (i = 0; i < self->ob_size; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000861 if (PyObject_Compare(self->ob_item[i], args) == 0)
862 return PyInt_FromLong((long)i);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000863 /* XXX PyErr_Occurred */
Guido van Rossum778983b1993-02-19 15:55:02 +0000864 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000865 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in array");
Guido van Rossum778983b1993-02-19 15:55:02 +0000866 return NULL;
867}
868#endif
869
870#if 0
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000871static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000872array_count(self, args)
873 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000874 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000875{
876 int count = 0;
877 int i;
878
879 if (args == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000880 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000881 return NULL;
882 }
883 for (i = 0; i < self->ob_size; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000884 if (PyObject_Compare(self->ob_item[i], args) == 0)
Guido van Rossum778983b1993-02-19 15:55:02 +0000885 count++;
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000886 /* XXX PyErr_Occurred */
Guido van Rossum778983b1993-02-19 15:55:02 +0000887 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000888 return PyInt_FromLong((long)count);
Guido van Rossum778983b1993-02-19 15:55:02 +0000889}
890#endif
891
892#if 0
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000893static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000894array_remove(self, args)
895 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000896 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000897{
898 int i;
899
900 if (args == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000901 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000902 return NULL;
903 }
904 for (i = 0; i < self->ob_size; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000905 if (PyObject_Compare(self->ob_item[i], args) == 0) {
Roger E. Masse5817f8f1996-12-09 22:24:19 +0000906 if (array_ass_slice(self, i, i+1,
907 (PyObject *)NULL) != 0)
Guido van Rossum778983b1993-02-19 15:55:02 +0000908 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000909 Py_INCREF(Py_None);
910 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000911 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000912 /* XXX PyErr_Occurred */
Guido van Rossum778983b1993-02-19 15:55:02 +0000913 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000914 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in array");
Guido van Rossum778983b1993-02-19 15:55:02 +0000915 return NULL;
916}
917#endif
918
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000919static PyObject *
Guido van Rossume77a7571993-11-03 15:01:26 +0000920array_fromfile(self, args)
Guido van Rossum778983b1993-02-19 15:55:02 +0000921 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000922 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000923{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000924 PyObject *f;
Guido van Rossum778983b1993-02-19 15:55:02 +0000925 int n;
926 FILE *fp;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000927 if (!PyArg_Parse(args, "(Oi)", &f, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +0000928 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000929 fp = PyFile_AsFile(f);
Guido van Rossum778983b1993-02-19 15:55:02 +0000930 if (fp == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000931 PyErr_SetString(PyExc_TypeError, "arg1 must be open file");
Guido van Rossum778983b1993-02-19 15:55:02 +0000932 return NULL;
933 }
934 if (n > 0) {
935 char *item = self->ob_item;
936 int itemsize = self->ob_descr->itemsize;
937 int nread;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000938 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000939 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000940 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000941 return NULL;
942 }
943 self->ob_item = item;
944 self->ob_size += n;
945 nread = fread(item + (self->ob_size - n) * itemsize,
946 itemsize, n, fp);
947 if (nread < n) {
948 self->ob_size -= (n - nread);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000949 PyMem_RESIZE(item, char, self->ob_size*itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000950 self->ob_item = item;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000951 PyErr_SetString(PyExc_EOFError,
952 "not enough items in file");
Guido van Rossum778983b1993-02-19 15:55:02 +0000953 return NULL;
954 }
955 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000956 Py_INCREF(Py_None);
957 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000958}
959
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000960static char fromfile_doc [] =
961"fromfile(f, n)\n\
962\n\
963Read n objects from the file object f and append them to the end of the\n\
964array. Also called as read.";
965
966
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000967static PyObject *
Guido van Rossume77a7571993-11-03 15:01:26 +0000968array_tofile(self, args)
Guido van Rossum778983b1993-02-19 15:55:02 +0000969 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000970 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000971{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000972 PyObject *f;
Guido van Rossum778983b1993-02-19 15:55:02 +0000973 FILE *fp;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000974 if (!PyArg_Parse(args, "O", &f))
Guido van Rossum778983b1993-02-19 15:55:02 +0000975 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000976 fp = PyFile_AsFile(f);
Guido van Rossum778983b1993-02-19 15:55:02 +0000977 if (fp == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000978 PyErr_SetString(PyExc_TypeError, "arg must be open file");
Guido van Rossum778983b1993-02-19 15:55:02 +0000979 return NULL;
980 }
981 if (self->ob_size > 0) {
Guido van Rossum7844e381997-04-11 20:44:04 +0000982 if ((int)fwrite(self->ob_item, self->ob_descr->itemsize,
Guido van Rossum778983b1993-02-19 15:55:02 +0000983 self->ob_size, fp) != self->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000984 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum778983b1993-02-19 15:55:02 +0000985 clearerr(fp);
986 return NULL;
987 }
988 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000989 Py_INCREF(Py_None);
990 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000991}
992
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000993static char tofile_doc [] =
994"tofile(f)\n\
995\n\
996Write all items (as machine values) to the file object f. Also called as\n\
997write.";
998
999
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001000static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001001array_fromlist(self, args)
1002 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001003 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +00001004{
1005 int n;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001006 PyObject *list;
Guido van Rossum778983b1993-02-19 15:55:02 +00001007 int itemsize = self->ob_descr->itemsize;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001008 if (!PyArg_Parse(args, "O", &list))
Guido van Rossum778983b1993-02-19 15:55:02 +00001009 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001010 if (!PyList_Check(list)) {
1011 PyErr_SetString(PyExc_TypeError, "arg must be list");
Guido van Rossum778983b1993-02-19 15:55:02 +00001012 return NULL;
1013 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001014 n = PyList_Size(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001015 if (n > 0) {
1016 char *item = self->ob_item;
1017 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001018 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001019 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001020 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001021 return NULL;
1022 }
1023 self->ob_item = item;
1024 self->ob_size += n;
1025 for (i = 0; i < n; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001026 PyObject *v = PyList_GetItem(list, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001027 if ((*self->ob_descr->setitem)(self,
1028 self->ob_size - n + i, v) != 0) {
1029 self->ob_size -= n;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001030 PyMem_RESIZE(item, char,
1031 self->ob_size * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001032 self->ob_item = item;
1033 return NULL;
1034 }
1035 }
1036 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001037 Py_INCREF(Py_None);
1038 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001039}
1040
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001041static char fromlist_doc [] =
1042"fromlist(list)\n\
1043\n\
1044Append items to array from list.";
1045
1046
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001047static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001048array_tolist(self, args)
1049 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001050 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +00001051{
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001052 PyObject *list = PyList_New(self->ob_size);
Guido van Rossum778983b1993-02-19 15:55:02 +00001053 int i;
1054 if (list == NULL)
1055 return NULL;
1056 for (i = 0; i < self->ob_size; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001057 PyObject *v = getarrayitem((PyObject *)self, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001058 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001059 Py_DECREF(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001060 return NULL;
1061 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001062 PyList_SetItem(list, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001063 }
1064 return list;
1065}
1066
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001067static char tolist_doc [] =
Guido van Rossumfc6aba51998-10-14 02:52:31 +00001068"tolist() -> list\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001069\n\
1070Convert array to an ordinary list with the same items.";
1071
1072
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001073static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001074array_fromstring(self, args)
1075 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001076 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +00001077{
1078 char *str;
1079 int n;
1080 int itemsize = self->ob_descr->itemsize;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001081 if (!PyArg_Parse(args, "s#", &str, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +00001082 return NULL;
1083 if (n % itemsize != 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001084 PyErr_SetString(PyExc_ValueError,
Guido van Rossum778983b1993-02-19 15:55:02 +00001085 "string length not a multiple of item size");
1086 return NULL;
1087 }
1088 n = n / itemsize;
1089 if (n > 0) {
1090 char *item = self->ob_item;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001091 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001092 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001093 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001094 return NULL;
1095 }
1096 self->ob_item = item;
1097 self->ob_size += n;
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001098 memcpy(item + (self->ob_size - n) * itemsize,
1099 str, itemsize*n);
Guido van Rossum778983b1993-02-19 15:55:02 +00001100 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001101 Py_INCREF(Py_None);
1102 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001103}
1104
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001105static char fromstring_doc [] =
1106"fromstring(string)\n\
1107\n\
1108Appends items from the string, interpreting it as an array of machine\n\
1109values,as if it had been read from a file using the fromfile() method).";
1110
1111
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001112static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001113array_tostring(self, args)
1114 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001115 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +00001116{
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001117 if (!PyArg_Parse(args, ""))
Guido van Rossum778983b1993-02-19 15:55:02 +00001118 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001119 return PyString_FromStringAndSize(self->ob_item,
Guido van Rossum778983b1993-02-19 15:55:02 +00001120 self->ob_size * self->ob_descr->itemsize);
1121}
1122
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001123static char tostring_doc [] =
1124"tostring() -> string\n\
1125\n\
1126Convert the array to an array of machine values and return the string\n\
1127representation.";
1128
1129PyMethodDef array_methods[] = {
1130 {"append", (PyCFunction)array_append, 0, append_doc},
1131 {"buffer_info", (PyCFunction)array_buffer_info, 0, buffer_info_doc},
1132 {"byteswap", (PyCFunction)array_byteswap, 0, byteswap_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001133/* {"count", (method)array_count},*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001134 {"fromfile", (PyCFunction)array_fromfile, 0, fromfile_doc},
1135 {"fromlist", (PyCFunction)array_fromlist, 0, fromlist_doc},
1136 {"fromstring", (PyCFunction)array_fromstring, 0, fromstring_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001137/* {"index", (method)array_index},*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001138 {"insert", (PyCFunction)array_insert, 0, insert_doc},
1139 {"read", (PyCFunction)array_fromfile, 0, fromfile_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001140/* {"remove", (method)array_remove},*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001141 {"reverse", (PyCFunction)array_reverse, 0, reverse_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001142/* {"sort", (method)array_sort},*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001143 {"tofile", (PyCFunction)array_tofile, 0, tofile_doc},
1144 {"tolist", (PyCFunction)array_tolist, 0, tolist_doc},
1145 {"tostring", (PyCFunction)array_tostring, 0, tostring_doc},
1146 {"write", (PyCFunction)array_tofile, 0, tofile_doc},
Guido van Rossum778983b1993-02-19 15:55:02 +00001147 {NULL, NULL} /* sentinel */
1148};
1149
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001150static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001151array_getattr(a, name)
1152 arrayobject *a;
1153 char *name;
1154{
1155 if (strcmp(name, "typecode") == 0) {
1156 char tc = a->ob_descr->typecode;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001157 return PyString_FromStringAndSize(&tc, 1);
Guido van Rossum778983b1993-02-19 15:55:02 +00001158 }
1159 if (strcmp(name, "itemsize") == 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001160 return PyInt_FromLong((long)a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001161 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001162 if (strcmp(name, "__members__") == 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001163 PyObject *list = PyList_New(2);
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001164 if (list) {
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001165 PyList_SetItem(list, 0,
1166 PyString_FromString("typecode"));
1167 PyList_SetItem(list, 1,
1168 PyString_FromString("itemsize"));
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001169 if (PyErr_Occurred()) {
1170 Py_DECREF(list);
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001171 list = NULL;
1172 }
1173 }
1174 return list;
1175 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001176 return Py_FindMethod(array_methods, (PyObject *)a, name);
Guido van Rossum778983b1993-02-19 15:55:02 +00001177}
1178
1179static int
1180array_print(a, fp, flags)
1181 arrayobject *a;
1182 FILE *fp;
1183 int flags;
1184{
1185 int ok = 0;
1186 int i, len;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001187 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +00001188 len = a->ob_size;
1189 if (len == 0) {
1190 fprintf(fp, "array('%c')", a->ob_descr->typecode);
1191 return ok;
1192 }
1193 if (a->ob_descr->typecode == 'c') {
1194 fprintf(fp, "array('c', ");
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001195 v = array_tostring(a, (PyObject *)NULL);
1196 ok = PyObject_Print(v, fp, 0);
1197 Py_XDECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001198 fprintf(fp, ")");
1199 return ok;
1200 }
1201 fprintf(fp, "array('%c', [", a->ob_descr->typecode);
1202 for (i = 0; i < len && ok == 0; i++) {
1203 if (i > 0)
1204 fprintf(fp, ", ");
1205 v = (a->ob_descr->getitem)(a, i);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001206 ok = PyObject_Print(v, fp, 0);
1207 Py_XDECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001208 }
1209 fprintf(fp, "])");
1210 return ok;
1211}
1212
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001213static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001214array_repr(a)
1215 arrayobject *a;
1216{
1217 char buf[256];
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001218 PyObject *s, *t, *comma, *v;
Guido van Rossum778983b1993-02-19 15:55:02 +00001219 int i, len;
1220 len = a->ob_size;
1221 if (len == 0) {
1222 sprintf(buf, "array('%c')", a->ob_descr->typecode);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001223 return PyString_FromString(buf);
Guido van Rossum778983b1993-02-19 15:55:02 +00001224 }
1225 if (a->ob_descr->typecode == 'c') {
1226 sprintf(buf, "array('c', ");
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001227 s = PyString_FromString(buf);
1228 v = array_tostring(a, (PyObject *)NULL);
1229 t = PyObject_Repr(v);
1230 Py_XDECREF(v);
1231 PyString_ConcatAndDel(&s, t);
1232 PyString_ConcatAndDel(&s, PyString_FromString(")"));
Guido van Rossum778983b1993-02-19 15:55:02 +00001233 return s;
1234 }
1235 sprintf(buf, "array('%c', [", a->ob_descr->typecode);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001236 s = PyString_FromString(buf);
1237 comma = PyString_FromString(", ");
1238 for (i = 0; i < len && !PyErr_Occurred(); i++) {
Guido van Rossumb6775db1994-08-01 11:34:53 +00001239 if (i > 0)
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001240 PyString_Concat(&s, comma);
Guido van Rossum778983b1993-02-19 15:55:02 +00001241 v = (a->ob_descr->getitem)(a, i);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001242 t = PyObject_Repr(v);
1243 Py_XDECREF(v);
1244 PyString_ConcatAndDel(&s, t);
Guido van Rossum778983b1993-02-19 15:55:02 +00001245 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001246 Py_XDECREF(comma);
1247 PyString_ConcatAndDel(&s, PyString_FromString("])"));
Guido van Rossum778983b1993-02-19 15:55:02 +00001248 return s;
1249}
1250
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001251static int
1252array_buffer_getreadbuf(self, index, ptr)
1253 arrayobject *self;
1254 int index;
1255 const void **ptr;
1256{
1257 if ( index != 0 ) {
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001258 PyErr_SetString(PyExc_SystemError,
1259 "Accessing non-existent array segment");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001260 return -1;
1261 }
1262 *ptr = (void *)self->ob_item;
1263 return self->ob_size*self->ob_descr->itemsize;
1264}
1265
1266static int
1267array_buffer_getwritebuf(self, index, ptr)
1268 arrayobject *self;
1269 int index;
1270 const void **ptr;
1271{
1272 if ( index != 0 ) {
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001273 PyErr_SetString(PyExc_SystemError,
1274 "Accessing non-existent array segment");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001275 return -1;
1276 }
1277 *ptr = (void *)self->ob_item;
1278 return self->ob_size*self->ob_descr->itemsize;
1279}
1280
1281static int
1282array_buffer_getsegcount(self, lenp)
1283 arrayobject *self;
1284 int *lenp;
1285{
1286 if ( lenp )
1287 *lenp = self->ob_size*self->ob_descr->itemsize;
1288 return 1;
1289}
1290
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001291static PySequenceMethods array_as_sequence = {
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001292 (inquiry)array_length, /*sq_length*/
1293 (binaryfunc)array_concat, /*sq_concat*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001294 (intargfunc)array_repeat, /*sq_repeat*/
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001295 (intargfunc)array_item, /*sq_item*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001296 (intintargfunc)array_slice, /*sq_slice*/
1297 (intobjargproc)array_ass_item, /*sq_ass_item*/
1298 (intintobjargproc)array_ass_slice, /*sq_ass_slice*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001299};
1300
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001301static PyBufferProcs array_as_buffer = {
1302 (getreadbufferproc)array_buffer_getreadbuf,
1303 (getwritebufferproc)array_buffer_getwritebuf,
1304 (getsegcountproc)array_buffer_getsegcount,
1305};
1306
1307
Guido van Rossum778983b1993-02-19 15:55:02 +00001308
1309
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001310static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001311a_array(self, args)
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001312 PyObject *self;
1313 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +00001314{
1315 char c;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001316 PyObject *initial = NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001317 struct arraydescr *descr;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001318 if (!PyArg_Parse(args, "c", &c)) {
1319 PyErr_Clear();
1320 if (!PyArg_Parse(args, "(cO)", &c, &initial))
Guido van Rossum778983b1993-02-19 15:55:02 +00001321 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001322 if (!PyList_Check(initial) && !PyString_Check(initial)) {
1323 PyErr_SetString(PyExc_TypeError,
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001324 "array initializer must be list or string");
Guido van Rossum778983b1993-02-19 15:55:02 +00001325 return NULL;
1326 }
1327 }
1328 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1329 if (descr->typecode == c) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001330 PyObject *a;
Guido van Rossum778983b1993-02-19 15:55:02 +00001331 int len;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001332 if (initial == NULL || !PyList_Check(initial))
Guido van Rossum778983b1993-02-19 15:55:02 +00001333 len = 0;
1334 else
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001335 len = PyList_Size(initial);
Guido van Rossum778983b1993-02-19 15:55:02 +00001336 a = newarrayobject(len, descr);
1337 if (a == NULL)
1338 return NULL;
1339 if (len > 0) {
1340 int i;
1341 for (i = 0; i < len; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001342 PyObject *v =
1343 PyList_GetItem(initial, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001344 if (setarrayitem(a, i, v) != 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001345 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001346 return NULL;
1347 }
1348 }
1349 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001350 if (initial != NULL && PyString_Check(initial)) {
1351 PyObject *v =
Guido van Rossum778983b1993-02-19 15:55:02 +00001352 array_fromstring((arrayobject *)a, initial);
1353 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001354 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001355 return NULL;
1356 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001357 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001358 }
1359 return a;
1360 }
1361 }
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001362 PyErr_SetString(PyExc_ValueError,
Guido van Rossum549ab711997-01-03 19:09:47 +00001363 "bad typecode (must be c, b, B, h, H, i, I, l, L, f or d)");
Guido van Rossum778983b1993-02-19 15:55:02 +00001364 return NULL;
1365}
1366
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001367static char a_array_doc [] =
1368"array(typecode [, initializer]) -> array\n\
1369\n\
1370Return a new array whose items are restricted by typecode, and\n\
1371initialized from the optional initializer value, which must be a list\n\
1372or a string.";
1373
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001374static PyMethodDef a_methods[] = {
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001375 {"array", a_array, 0, a_array_doc},
Guido van Rossum778983b1993-02-19 15:55:02 +00001376 {NULL, NULL} /* sentinel */
1377};
1378
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001379static char module_doc [] =
1380"This module defines a new object type which can efficiently represent\n\
1381an array of basic values: characters, integers, floating point\n\
1382numbers. Arrays are sequence types and behave very much like lists,\n\
1383except that the type of objects stored in them is constrained. The\n\
1384type is specified at object creation time by using a type code, which\n\
1385is a single character. The following type codes are defined:\n\
1386\n\
1387 Type code C Type Minimum size in bytes \n\
1388 'c' character 1 \n\
1389 'b' signed integer 1 \n\
1390 'B' unsigned integer 1 \n\
1391 'h' signed integer 2 \n\
1392 'H' unsigned integer 2 \n\
1393 'i' signed integer 2 \n\
1394 'I' unsigned integer 2 \n\
1395 'l' signed integer 4 \n\
1396 'L' unsigned integer 4 \n\
1397 'f' floating point 4 \n\
1398 'd' floating point 8 \n\
1399\n\
1400Functions:\n\
1401\n\
1402array(typecode [, initializer]) -- create a new array\n\
1403\n\
1404Special Objects:\n\
1405\n\
1406ArrayType -- type object for array objects\n\
1407";
1408
1409static char arraytype_doc [] =
1410"An array represents basic values and behave very much like lists, except\n\
1411the type of objects stored in them is constrained.\n\
1412\n\
1413Methods:\n\
1414\n\
1415append() -- append a new item to the end of the array\n\
1416buffer_info() -- return information giving the current memory info\n\
1417byteswap() -- byteswap all the items of the array\n\
1418fromfile() -- read items from a file object\n\
1419fromlist() -- append items from the list\n\
1420fromstring() -- append items from the string\n\
1421insert() -- insert a new item into the array at a provided position\n\
1422read() -- DEPRECATED, use fromfile()\n\
1423reverse() -- reverse the order of the items in the array\n\
1424tofile() -- write all items to a file object\n\
1425tolist() -- return the array converted to an ordinary list\n\
1426tostring() -- return the array converted to a string\n\
1427write() -- DEPRECATED, use tofile()\n\
1428\n\
1429Variables:\n\
1430\n\
1431typecode -- the typecode character used to create the array\n\
1432itemsize -- the length in bytes of one array item\n\
1433";
1434
1435statichere PyTypeObject Arraytype = {
1436 PyObject_HEAD_INIT(&PyType_Type)
1437 0,
1438 "array",
1439 sizeof(arrayobject),
1440 0,
1441 (destructor)array_dealloc, /*tp_dealloc*/
1442 (printfunc)array_print, /*tp_print*/
1443 (getattrfunc)array_getattr, /*tp_getattr*/
1444 0, /*tp_setattr*/
1445 (cmpfunc)array_compare, /*tp_compare*/
1446 (reprfunc)array_repr, /*tp_repr*/
1447 0, /*tp_as_number*/
1448 &array_as_sequence, /*tp_as_sequence*/
1449 0, /*tp_as_mapping*/
1450 0, /*tp_hash*/
1451 0, /*tp_call*/
1452 0, /*tp_str*/
1453 0, /*tp_getattro*/
1454 0, /*tp_setattro*/
1455 &array_as_buffer, /*tp_as_buffer*/
1456 0, /*tp_xxx4*/
1457 arraytype_doc, /*tp_doc*/
1458};
1459
Guido van Rossum3886bb61998-12-04 18:50:17 +00001460DL_EXPORT(void)
Guido van Rossum778983b1993-02-19 15:55:02 +00001461initarray()
1462{
Guido van Rossumb6190d31997-05-22 14:56:36 +00001463 PyObject *m, *d;
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001464 m = Py_InitModule3("array", a_methods, module_doc);
Guido van Rossumb6190d31997-05-22 14:56:36 +00001465 d = PyModule_GetDict(m);
Guido van Rossuma0deb641998-10-14 13:45:06 +00001466 PyDict_SetItemString(d, "ArrayType", (PyObject *)&Arraytype);
1467 /* No need to check the error here, the caller will do that */
Guido van Rossum778983b1993-02-19 15:55:02 +00001468}