blob: 6920cbfd400fc7d89ebe64c197f141c40389565e [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
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000714static PyObject *
Guido van Rossumde4a4ca1997-08-12 14:55:56 +0000715array_buffer_info(self, args)
716 arrayobject *self;
717 PyObject *args;
718{
719 return Py_BuildValue("ll",
720 (long)(self->ob_item), (long)(self->ob_size));
721}
722
723static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000724array_append(self, args)
725 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000726 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000727{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000728 PyObject *v;
729 if (!PyArg_Parse(args, "O", &v))
Guido van Rossum778983b1993-02-19 15:55:02 +0000730 return NULL;
731 return ins(self, (int) self->ob_size, v);
732}
733
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000734static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000735array_byteswap(self, args)
736 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000737 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000738{
739 char *p;
740 int i;
741 switch (self->ob_descr->itemsize) {
742 case 1:
743 break;
744 case 2:
745 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 2) {
746 char p0 = p[0];
747 p[0] = p[1];
748 p[1] = p0;
749 }
750 break;
751 case 4:
752 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 4) {
753 char p0 = p[0];
754 char p1 = p[1];
755 p[0] = p[3];
756 p[1] = p[2];
757 p[2] = p1;
758 p[3] = p0;
759 }
760 break;
Guido van Rossume77a7571993-11-03 15:01:26 +0000761 case 8:
762 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 8) {
763 char p0 = p[0];
764 char p1 = p[1];
765 char p2 = p[2];
766 char p3 = p[3];
767 p[0] = p[7];
768 p[1] = p[6];
769 p[2] = p[5];
770 p[3] = p[4];
771 p[4] = p3;
772 p[5] = p2;
773 p[6] = p1;
774 p[7] = p0;
775 }
776 break;
Guido van Rossum778983b1993-02-19 15:55:02 +0000777 default:
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000778 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossum778983b1993-02-19 15:55:02 +0000779 "don't know how to byteswap this array type");
780 return NULL;
781 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000782 Py_INCREF(Py_None);
783 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000784}
785
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000786static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000787array_reverse(self, args)
788 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000789 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000790{
Guido van Rossume77a7571993-11-03 15:01:26 +0000791 register int itemsize = self->ob_descr->itemsize;
792 register char *p, *q;
793 char tmp[sizeof(double)]; /* Assume that's the max item size */
794
Guido van Rossum778983b1993-02-19 15:55:02 +0000795 if (args != NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000796 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000797 return NULL;
798 }
799
800 if (self->ob_size > 1) {
Guido van Rossume77a7571993-11-03 15:01:26 +0000801 for (p = self->ob_item,
802 q = self->ob_item + (self->ob_size - 1)*itemsize;
803 p < q;
804 p += itemsize, q -= itemsize) {
805 memmove(tmp, p, itemsize);
806 memmove(p, q, itemsize);
807 memmove(q, tmp, itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000808 }
809 }
810
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000811 Py_INCREF(Py_None);
812 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000813}
Guido van Rossume77a7571993-11-03 15:01:26 +0000814
815/* The following routines were adapted from listobject.c but not converted.
816 To make them work you will have to work! */
Guido van Rossum778983b1993-02-19 15:55:02 +0000817
818#if 0
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000819static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000820array_index(self, args)
821 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000822 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000823{
824 int i;
825
826 if (args == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000827 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000828 return NULL;
829 }
830 for (i = 0; i < self->ob_size; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000831 if (PyObject_Compare(self->ob_item[i], args) == 0)
832 return PyInt_FromLong((long)i);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000833 /* XXX PyErr_Occurred */
Guido van Rossum778983b1993-02-19 15:55:02 +0000834 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000835 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in array");
Guido van Rossum778983b1993-02-19 15:55:02 +0000836 return NULL;
837}
838#endif
839
840#if 0
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000841static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000842array_count(self, args)
843 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000844 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000845{
846 int count = 0;
847 int i;
848
849 if (args == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000850 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000851 return NULL;
852 }
853 for (i = 0; i < self->ob_size; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000854 if (PyObject_Compare(self->ob_item[i], args) == 0)
Guido van Rossum778983b1993-02-19 15:55:02 +0000855 count++;
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000856 /* XXX PyErr_Occurred */
Guido van Rossum778983b1993-02-19 15:55:02 +0000857 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000858 return PyInt_FromLong((long)count);
Guido van Rossum778983b1993-02-19 15:55:02 +0000859}
860#endif
861
862#if 0
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000863static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000864array_remove(self, args)
865 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000866 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000867{
868 int i;
869
870 if (args == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000871 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000872 return NULL;
873 }
874 for (i = 0; i < self->ob_size; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000875 if (PyObject_Compare(self->ob_item[i], args) == 0) {
Roger E. Masse5817f8f1996-12-09 22:24:19 +0000876 if (array_ass_slice(self, i, i+1,
877 (PyObject *)NULL) != 0)
Guido van Rossum778983b1993-02-19 15:55:02 +0000878 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000879 Py_INCREF(Py_None);
880 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000881 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000882 /* XXX PyErr_Occurred */
Guido van Rossum778983b1993-02-19 15:55:02 +0000883 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000884 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in array");
Guido van Rossum778983b1993-02-19 15:55:02 +0000885 return NULL;
886}
887#endif
888
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000889static PyObject *
Guido van Rossume77a7571993-11-03 15:01:26 +0000890array_fromfile(self, args)
Guido van Rossum778983b1993-02-19 15:55:02 +0000891 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000892 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000893{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000894 PyObject *f;
Guido van Rossum778983b1993-02-19 15:55:02 +0000895 int n;
896 FILE *fp;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000897 if (!PyArg_Parse(args, "(Oi)", &f, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +0000898 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000899 fp = PyFile_AsFile(f);
Guido van Rossum778983b1993-02-19 15:55:02 +0000900 if (fp == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000901 PyErr_SetString(PyExc_TypeError, "arg1 must be open file");
Guido van Rossum778983b1993-02-19 15:55:02 +0000902 return NULL;
903 }
904 if (n > 0) {
905 char *item = self->ob_item;
906 int itemsize = self->ob_descr->itemsize;
907 int nread;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000908 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000909 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000910 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000911 return NULL;
912 }
913 self->ob_item = item;
914 self->ob_size += n;
915 nread = fread(item + (self->ob_size - n) * itemsize,
916 itemsize, n, fp);
917 if (nread < n) {
918 self->ob_size -= (n - nread);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000919 PyMem_RESIZE(item, char, self->ob_size*itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000920 self->ob_item = item;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000921 PyErr_SetString(PyExc_EOFError,
922 "not enough items in file");
Guido van Rossum778983b1993-02-19 15:55:02 +0000923 return NULL;
924 }
925 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000926 Py_INCREF(Py_None);
927 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000928}
929
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000930static PyObject *
Guido van Rossume77a7571993-11-03 15:01:26 +0000931array_tofile(self, args)
Guido van Rossum778983b1993-02-19 15:55:02 +0000932 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000933 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000934{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000935 PyObject *f;
Guido van Rossum778983b1993-02-19 15:55:02 +0000936 FILE *fp;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000937 if (!PyArg_Parse(args, "O", &f))
Guido van Rossum778983b1993-02-19 15:55:02 +0000938 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000939 fp = PyFile_AsFile(f);
Guido van Rossum778983b1993-02-19 15:55:02 +0000940 if (fp == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000941 PyErr_SetString(PyExc_TypeError, "arg must be open file");
Guido van Rossum778983b1993-02-19 15:55:02 +0000942 return NULL;
943 }
944 if (self->ob_size > 0) {
Guido van Rossum7844e381997-04-11 20:44:04 +0000945 if ((int)fwrite(self->ob_item, self->ob_descr->itemsize,
Guido van Rossum778983b1993-02-19 15:55:02 +0000946 self->ob_size, fp) != self->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000947 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum778983b1993-02-19 15:55:02 +0000948 clearerr(fp);
949 return NULL;
950 }
951 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000952 Py_INCREF(Py_None);
953 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000954}
955
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000956static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000957array_fromlist(self, args)
958 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000959 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000960{
961 int n;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000962 PyObject *list;
Guido van Rossum778983b1993-02-19 15:55:02 +0000963 int itemsize = self->ob_descr->itemsize;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000964 if (!PyArg_Parse(args, "O", &list))
Guido van Rossum778983b1993-02-19 15:55:02 +0000965 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000966 if (!PyList_Check(list)) {
967 PyErr_SetString(PyExc_TypeError, "arg must be list");
Guido van Rossum778983b1993-02-19 15:55:02 +0000968 return NULL;
969 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000970 n = PyList_Size(list);
Guido van Rossum778983b1993-02-19 15:55:02 +0000971 if (n > 0) {
972 char *item = self->ob_item;
973 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000974 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000975 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000976 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000977 return NULL;
978 }
979 self->ob_item = item;
980 self->ob_size += n;
981 for (i = 0; i < n; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000982 PyObject *v = PyList_GetItem(list, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000983 if ((*self->ob_descr->setitem)(self,
984 self->ob_size - n + i, v) != 0) {
985 self->ob_size -= n;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000986 PyMem_RESIZE(item, char,
987 self->ob_size * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000988 self->ob_item = item;
989 return NULL;
990 }
991 }
992 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000993 Py_INCREF(Py_None);
994 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000995}
996
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000997static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000998array_tolist(self, args)
999 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001000 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +00001001{
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001002 PyObject *list = PyList_New(self->ob_size);
Guido van Rossum778983b1993-02-19 15:55:02 +00001003 int i;
1004 if (list == NULL)
1005 return NULL;
1006 for (i = 0; i < self->ob_size; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001007 PyObject *v = getarrayitem((PyObject *)self, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001008 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001009 Py_DECREF(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001010 return NULL;
1011 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001012 PyList_SetItem(list, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001013 }
1014 return list;
1015}
1016
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001017static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001018array_fromstring(self, args)
1019 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001020 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +00001021{
1022 char *str;
1023 int n;
1024 int itemsize = self->ob_descr->itemsize;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001025 if (!PyArg_Parse(args, "s#", &str, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +00001026 return NULL;
1027 if (n % itemsize != 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001028 PyErr_SetString(PyExc_ValueError,
Guido van Rossum778983b1993-02-19 15:55:02 +00001029 "string length not a multiple of item size");
1030 return NULL;
1031 }
1032 n = n / itemsize;
1033 if (n > 0) {
1034 char *item = self->ob_item;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001035 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001036 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001037 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001038 return NULL;
1039 }
1040 self->ob_item = item;
1041 self->ob_size += n;
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001042 memcpy(item + (self->ob_size - n) * itemsize,
1043 str, itemsize*n);
Guido van Rossum778983b1993-02-19 15:55:02 +00001044 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001045 Py_INCREF(Py_None);
1046 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001047}
1048
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001049static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001050array_tostring(self, args)
1051 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001052 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +00001053{
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001054 if (!PyArg_Parse(args, ""))
Guido van Rossum778983b1993-02-19 15:55:02 +00001055 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001056 return PyString_FromStringAndSize(self->ob_item,
Guido van Rossum778983b1993-02-19 15:55:02 +00001057 self->ob_size * self->ob_descr->itemsize);
1058}
1059
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001060static PyMethodDef array_methods[] = {
1061 {"append", (PyCFunction)array_append},
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001062 {"buffer_info", (PyCFunction)array_buffer_info},
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001063 {"byteswap", (PyCFunction)array_byteswap},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001064/* {"count", (method)array_count},*/
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001065 {"fromfile", (PyCFunction)array_fromfile},
1066 {"fromlist", (PyCFunction)array_fromlist},
1067 {"fromstring", (PyCFunction)array_fromstring},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001068/* {"index", (method)array_index},*/
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001069 {"insert", (PyCFunction)array_insert},
1070 {"read", (PyCFunction)array_fromfile},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001071/* {"remove", (method)array_remove},*/
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001072 {"reverse", (PyCFunction)array_reverse},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001073/* {"sort", (method)array_sort},*/
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001074 {"tofile", (PyCFunction)array_tofile},
1075 {"tolist", (PyCFunction)array_tolist},
1076 {"tostring", (PyCFunction)array_tostring},
1077 {"write", (PyCFunction)array_tofile},
Guido van Rossum778983b1993-02-19 15:55:02 +00001078 {NULL, NULL} /* sentinel */
1079};
1080
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001081static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001082array_getattr(a, name)
1083 arrayobject *a;
1084 char *name;
1085{
1086 if (strcmp(name, "typecode") == 0) {
1087 char tc = a->ob_descr->typecode;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001088 return PyString_FromStringAndSize(&tc, 1);
Guido van Rossum778983b1993-02-19 15:55:02 +00001089 }
1090 if (strcmp(name, "itemsize") == 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001091 return PyInt_FromLong((long)a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001092 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001093 if (strcmp(name, "__members__") == 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001094 PyObject *list = PyList_New(2);
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001095 if (list) {
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001096 PyList_SetItem(list, 0,
1097 PyString_FromString("typecode"));
1098 PyList_SetItem(list, 1,
1099 PyString_FromString("itemsize"));
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001100 if (PyErr_Occurred()) {
1101 Py_DECREF(list);
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001102 list = NULL;
1103 }
1104 }
1105 return list;
1106 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001107 return Py_FindMethod(array_methods, (PyObject *)a, name);
Guido van Rossum778983b1993-02-19 15:55:02 +00001108}
1109
1110static int
1111array_print(a, fp, flags)
1112 arrayobject *a;
1113 FILE *fp;
1114 int flags;
1115{
1116 int ok = 0;
1117 int i, len;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001118 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +00001119 len = a->ob_size;
1120 if (len == 0) {
1121 fprintf(fp, "array('%c')", a->ob_descr->typecode);
1122 return ok;
1123 }
1124 if (a->ob_descr->typecode == 'c') {
1125 fprintf(fp, "array('c', ");
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001126 v = array_tostring(a, (PyObject *)NULL);
1127 ok = PyObject_Print(v, fp, 0);
1128 Py_XDECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001129 fprintf(fp, ")");
1130 return ok;
1131 }
1132 fprintf(fp, "array('%c', [", a->ob_descr->typecode);
1133 for (i = 0; i < len && ok == 0; i++) {
1134 if (i > 0)
1135 fprintf(fp, ", ");
1136 v = (a->ob_descr->getitem)(a, i);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001137 ok = PyObject_Print(v, fp, 0);
1138 Py_XDECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001139 }
1140 fprintf(fp, "])");
1141 return ok;
1142}
1143
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001144static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001145array_repr(a)
1146 arrayobject *a;
1147{
1148 char buf[256];
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001149 PyObject *s, *t, *comma, *v;
Guido van Rossum778983b1993-02-19 15:55:02 +00001150 int i, len;
1151 len = a->ob_size;
1152 if (len == 0) {
1153 sprintf(buf, "array('%c')", a->ob_descr->typecode);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001154 return PyString_FromString(buf);
Guido van Rossum778983b1993-02-19 15:55:02 +00001155 }
1156 if (a->ob_descr->typecode == 'c') {
1157 sprintf(buf, "array('c', ");
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001158 s = PyString_FromString(buf);
1159 v = array_tostring(a, (PyObject *)NULL);
1160 t = PyObject_Repr(v);
1161 Py_XDECREF(v);
1162 PyString_ConcatAndDel(&s, t);
1163 PyString_ConcatAndDel(&s, PyString_FromString(")"));
Guido van Rossum778983b1993-02-19 15:55:02 +00001164 return s;
1165 }
1166 sprintf(buf, "array('%c', [", a->ob_descr->typecode);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001167 s = PyString_FromString(buf);
1168 comma = PyString_FromString(", ");
1169 for (i = 0; i < len && !PyErr_Occurred(); i++) {
Guido van Rossumb6775db1994-08-01 11:34:53 +00001170 if (i > 0)
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001171 PyString_Concat(&s, comma);
Guido van Rossum778983b1993-02-19 15:55:02 +00001172 v = (a->ob_descr->getitem)(a, i);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001173 t = PyObject_Repr(v);
1174 Py_XDECREF(v);
1175 PyString_ConcatAndDel(&s, t);
Guido van Rossum778983b1993-02-19 15:55:02 +00001176 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001177 Py_XDECREF(comma);
1178 PyString_ConcatAndDel(&s, PyString_FromString("])"));
Guido van Rossum778983b1993-02-19 15:55:02 +00001179 return s;
1180}
1181
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001182static int
1183array_buffer_getreadbuf(self, index, ptr)
1184 arrayobject *self;
1185 int index;
1186 const void **ptr;
1187{
1188 if ( index != 0 ) {
1189 PyErr_SetString(PyExc_SystemError, "Accessing non-existent array segment");
1190 return -1;
1191 }
1192 *ptr = (void *)self->ob_item;
1193 return self->ob_size*self->ob_descr->itemsize;
1194}
1195
1196static int
1197array_buffer_getwritebuf(self, index, ptr)
1198 arrayobject *self;
1199 int index;
1200 const void **ptr;
1201{
1202 if ( index != 0 ) {
1203 PyErr_SetString(PyExc_SystemError, "Accessing non-existent array segment");
1204 return -1;
1205 }
1206 *ptr = (void *)self->ob_item;
1207 return self->ob_size*self->ob_descr->itemsize;
1208}
1209
1210static int
1211array_buffer_getsegcount(self, lenp)
1212 arrayobject *self;
1213 int *lenp;
1214{
1215 if ( lenp )
1216 *lenp = self->ob_size*self->ob_descr->itemsize;
1217 return 1;
1218}
1219
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001220static PySequenceMethods array_as_sequence = {
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001221 (inquiry)array_length, /*sq_length*/
1222 (binaryfunc)array_concat, /*sq_concat*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001223 (intargfunc)array_repeat, /*sq_repeat*/
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001224 (intargfunc)array_item, /*sq_item*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001225 (intintargfunc)array_slice, /*sq_slice*/
1226 (intobjargproc)array_ass_item, /*sq_ass_item*/
1227 (intintobjargproc)array_ass_slice, /*sq_ass_slice*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001228};
1229
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001230static PyBufferProcs array_as_buffer = {
1231 (getreadbufferproc)array_buffer_getreadbuf,
1232 (getwritebufferproc)array_buffer_getwritebuf,
1233 (getsegcountproc)array_buffer_getsegcount,
1234};
1235
1236
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001237statichere PyTypeObject Arraytype = {
1238 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum778983b1993-02-19 15:55:02 +00001239 0,
1240 "array",
1241 sizeof(arrayobject),
1242 0,
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001243 (destructor)array_dealloc, /*tp_dealloc*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001244 (printfunc)array_print, /*tp_print*/
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001245 (getattrfunc)array_getattr, /*tp_getattr*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001246 0, /*tp_setattr*/
1247 (cmpfunc)array_compare, /*tp_compare*/
1248 (reprfunc)array_repr, /*tp_repr*/
1249 0, /*tp_as_number*/
1250 &array_as_sequence, /*tp_as_sequence*/
1251 0, /*tp_as_mapping*/
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001252 0, /*tp_hash*/
1253 0, /*tp_call*/
1254 0, /*tp_str*/
1255 0, /*tp_getattro*/
1256 0, /*tp_setattro*/
1257 &array_as_buffer, /*tp_as_buffer*/
1258 0, /*tp_xxx4*/
1259 0, /*tp_doc*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001260};
1261
1262
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001263static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001264a_array(self, args)
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001265 PyObject *self;
1266 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +00001267{
1268 char c;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001269 PyObject *initial = NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001270 struct arraydescr *descr;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001271 if (!PyArg_Parse(args, "c", &c)) {
1272 PyErr_Clear();
1273 if (!PyArg_Parse(args, "(cO)", &c, &initial))
Guido van Rossum778983b1993-02-19 15:55:02 +00001274 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001275 if (!PyList_Check(initial) && !PyString_Check(initial)) {
1276 PyErr_SetString(PyExc_TypeError,
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001277 "array initializer must be list or string");
Guido van Rossum778983b1993-02-19 15:55:02 +00001278 return NULL;
1279 }
1280 }
1281 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1282 if (descr->typecode == c) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001283 PyObject *a;
Guido van Rossum778983b1993-02-19 15:55:02 +00001284 int len;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001285 if (initial == NULL || !PyList_Check(initial))
Guido van Rossum778983b1993-02-19 15:55:02 +00001286 len = 0;
1287 else
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001288 len = PyList_Size(initial);
Guido van Rossum778983b1993-02-19 15:55:02 +00001289 a = newarrayobject(len, descr);
1290 if (a == NULL)
1291 return NULL;
1292 if (len > 0) {
1293 int i;
1294 for (i = 0; i < len; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001295 PyObject *v =
1296 PyList_GetItem(initial, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001297 if (setarrayitem(a, i, v) != 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001298 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001299 return NULL;
1300 }
1301 }
1302 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001303 if (initial != NULL && PyString_Check(initial)) {
1304 PyObject *v =
Guido van Rossum778983b1993-02-19 15:55:02 +00001305 array_fromstring((arrayobject *)a, initial);
1306 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001307 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001308 return NULL;
1309 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001310 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001311 }
1312 return a;
1313 }
1314 }
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001315 PyErr_SetString(PyExc_ValueError,
Guido van Rossum549ab711997-01-03 19:09:47 +00001316 "bad typecode (must be c, b, B, h, H, i, I, l, L, f or d)");
Guido van Rossum778983b1993-02-19 15:55:02 +00001317 return NULL;
1318}
1319
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001320static PyMethodDef a_methods[] = {
Guido van Rossum778983b1993-02-19 15:55:02 +00001321 {"array", a_array},
1322 {NULL, NULL} /* sentinel */
1323};
1324
1325void
1326initarray()
1327{
Guido van Rossumb6190d31997-05-22 14:56:36 +00001328 PyObject *m, *d;
1329 m = Py_InitModule("array", a_methods);
1330 d = PyModule_GetDict(m);
Guido van Rossum24995b91997-06-02 22:17:49 +00001331 if (PyDict_SetItemString(d, "ArrayType",
Guido van Rossumb6190d31997-05-22 14:56:36 +00001332 (PyObject *)&Arraytype) != 0)
Guido van Rossum24995b91997-06-02 22:17:49 +00001333 Py_FatalError("can't define array.ArrayType");
Guido van Rossum778983b1993-02-19 15:55:02 +00001334}