blob: 9a09c7d720eebdcab6d294eee59acbcadc859275 [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>
Guido van Rossum7f1de831999-08-27 20:33:52 +000041#else /* !STDC_HEADERS */
42#ifndef DONT_HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000043#include <sys/types.h> /* For size_t */
Guido van Rossum7f1de831999-08-27 20:33:52 +000044#endif /* DONT_HAVE_SYS_TYPES_H */
45#endif /* !STDC_HEADERS */
Guido van Rossum778983b1993-02-19 15:55:02 +000046
47struct arrayobject; /* Forward */
48
49struct arraydescr {
50 int typecode;
51 int itemsize;
Roger E. Masse2919eaa1996-12-09 20:10:36 +000052 PyObject * (*getitem) Py_FPROTO((struct arrayobject *, int));
53 int (*setitem) Py_FPROTO((struct arrayobject *, int, PyObject *));
Guido van Rossum778983b1993-02-19 15:55:02 +000054};
55
56typedef struct arrayobject {
Roger E. Masse2919eaa1996-12-09 20:10:36 +000057 PyObject_VAR_HEAD
Guido van Rossum778983b1993-02-19 15:55:02 +000058 char *ob_item;
59 struct arraydescr *ob_descr;
60} arrayobject;
61
Roger E. Masse2919eaa1996-12-09 20:10:36 +000062staticforward PyTypeObject Arraytype;
Guido van Rossum778983b1993-02-19 15:55:02 +000063
64#define is_arrayobject(op) ((op)->ob_type == &Arraytype)
65
Guido van Rossumb73cc041993-11-01 16:28:59 +000066/* Forward */
Roger E. Masse2919eaa1996-12-09 20:10:36 +000067static PyObject *newarrayobject Py_PROTO((int, struct arraydescr *));
Guido van Rossuma376cc51996-12-05 23:43:35 +000068#if 0
Roger E. Masse2919eaa1996-12-09 20:10:36 +000069static int getarraysize Py_PROTO((PyObject *));
Guido van Rossuma376cc51996-12-05 23:43:35 +000070#endif
Roger E. Masse2919eaa1996-12-09 20:10:36 +000071static PyObject *getarrayitem Py_PROTO((PyObject *, int));
72static int setarrayitem Py_PROTO((PyObject *, int, PyObject *));
Guido van Rossuma376cc51996-12-05 23:43:35 +000073#if 0
Roger E. Masse2919eaa1996-12-09 20:10:36 +000074static int insarrayitem Py_PROTO((PyObject *, int, PyObject *));
75static int addarrayitem Py_PROTO((PyObject *, PyObject *));
Guido van Rossuma376cc51996-12-05 23:43:35 +000076#endif
Guido van Rossum778983b1993-02-19 15:55:02 +000077
Roger E. Masse2919eaa1996-12-09 20:10:36 +000078static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +000079c_getitem(ap, i)
80 arrayobject *ap;
81 int i;
82{
Roger E. Masse2919eaa1996-12-09 20:10:36 +000083 return PyString_FromStringAndSize(&((char *)ap->ob_item)[i], 1);
Guido van Rossum778983b1993-02-19 15:55:02 +000084}
85
86static int
87c_setitem(ap, i, v)
88 arrayobject *ap;
89 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +000090 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +000091{
92 char x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +000093 if (!PyArg_Parse(v, "c;array item must be char", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +000094 return -1;
95 if (i >= 0)
96 ((char *)ap->ob_item)[i] = x;
97 return 0;
98}
99
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000100static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000101b_getitem(ap, i)
102 arrayobject *ap;
103 int i;
104{
105 long x = ((char *)ap->ob_item)[i];
106 if (x >= 128)
107 x -= 256;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000108 return PyInt_FromLong(x);
Guido van Rossum778983b1993-02-19 15:55:02 +0000109}
110
111static int
112b_setitem(ap, i, v)
113 arrayobject *ap;
114 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000115 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000116{
117 char x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000118 if (!PyArg_Parse(v, "b;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000119 return -1;
120 if (i >= 0)
121 ((char *)ap->ob_item)[i] = x;
122 return 0;
123}
124
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000125static PyObject *
Guido van Rossum1a747f81997-05-16 16:21:38 +0000126BB_getitem(ap, i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000127 arrayobject *ap;
128 int i;
129{
130 long x = ((unsigned char *)ap->ob_item)[i];
131 return PyInt_FromLong(x);
132}
133
Guido van Rossum1a747f81997-05-16 16:21:38 +0000134#define BB_setitem b_setitem
Guido van Rossum549ab711997-01-03 19:09:47 +0000135
136static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000137h_getitem(ap, i)
138 arrayobject *ap;
139 int i;
140{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000141 return PyInt_FromLong((long) ((short *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000142}
143
144static int
145h_setitem(ap, i, v)
146 arrayobject *ap;
147 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000148 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000149{
150 short x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000151 if (!PyArg_Parse(v, "h;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000152 return -1;
153 if (i >= 0)
154 ((short *)ap->ob_item)[i] = x;
155 return 0;
156}
157
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000158static PyObject *
Guido van Rossum1a747f81997-05-16 16:21:38 +0000159HH_getitem(ap, i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000160 arrayobject *ap;
161 int i;
162{
163 return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
164}
165
Guido van Rossum1a747f81997-05-16 16:21:38 +0000166#define HH_setitem h_setitem
Guido van Rossum549ab711997-01-03 19:09:47 +0000167
168static PyObject *
Guido van Rossume77a7571993-11-03 15:01:26 +0000169i_getitem(ap, i)
170 arrayobject *ap;
171 int i;
172{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000173 return PyInt_FromLong((long) ((int *)ap->ob_item)[i]);
Guido van Rossume77a7571993-11-03 15:01:26 +0000174}
175
176static int
177i_setitem(ap, i, v)
178 arrayobject *ap;
179 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000180 PyObject *v;
Guido van Rossume77a7571993-11-03 15:01:26 +0000181{
182 int x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000183 if (!PyArg_Parse(v, "i;array item must be integer", &x))
Guido van Rossume77a7571993-11-03 15:01:26 +0000184 return -1;
185 if (i >= 0)
186 ((int *)ap->ob_item)[i] = x;
187 return 0;
188}
189
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000190static PyObject *
Guido van Rossum1a747f81997-05-16 16:21:38 +0000191II_getitem(ap, i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000192 arrayobject *ap;
193 int i;
194{
195 return PyLong_FromUnsignedLong(
196 (unsigned long) ((unsigned int *)ap->ob_item)[i]);
197}
198
199static int
Guido van Rossum1a747f81997-05-16 16:21:38 +0000200II_setitem(ap, i, v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000201 arrayobject *ap;
202 int i;
203 PyObject *v;
204{
205 unsigned long x;
206 if (PyLong_Check(v)) {
207 x = PyLong_AsUnsignedLong(v);
208 if (x == (unsigned long) -1 && PyErr_Occurred())
209 return -1;
210 }
211 else {
212 if (!PyArg_Parse(v, "l;array item must be integer", &x))
213 return -1;
214 }
215 if (i >= 0)
216 ((unsigned int *)ap->ob_item)[i] = x;
217 return 0;
218}
219
220static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000221l_getitem(ap, i)
222 arrayobject *ap;
223 int i;
224{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000225 return PyInt_FromLong(((long *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000226}
227
228static int
229l_setitem(ap, i, v)
230 arrayobject *ap;
231 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000232 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000233{
234 long x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000235 if (!PyArg_Parse(v, "l;array item must be integer", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000236 return -1;
237 if (i >= 0)
238 ((long *)ap->ob_item)[i] = x;
239 return 0;
240}
241
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000242static PyObject *
Guido van Rossum1a747f81997-05-16 16:21:38 +0000243LL_getitem(ap, i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000244 arrayobject *ap;
245 int i;
246{
247 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
248}
249
250static int
Guido van Rossum1a747f81997-05-16 16:21:38 +0000251LL_setitem(ap, i, v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000252 arrayobject *ap;
253 int i;
254 PyObject *v;
255{
256 unsigned long x;
257 if (PyLong_Check(v)) {
258 x = PyLong_AsUnsignedLong(v);
259 if (x == (unsigned long) -1 && PyErr_Occurred())
260 return -1;
261 }
262 else {
263 if (!PyArg_Parse(v, "l;array item must be integer", &x))
264 return -1;
265 }
266 if (i >= 0)
267 ((unsigned long *)ap->ob_item)[i] = x;
268 return 0;
269}
270
271static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000272f_getitem(ap, i)
273 arrayobject *ap;
274 int i;
275{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000276 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000277}
278
279static int
280f_setitem(ap, i, v)
281 arrayobject *ap;
282 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000283 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000284{
285 float x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000286 if (!PyArg_Parse(v, "f;array item must be float", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000287 return -1;
288 if (i >= 0)
289 ((float *)ap->ob_item)[i] = x;
290 return 0;
291}
292
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000293static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000294d_getitem(ap, i)
295 arrayobject *ap;
296 int i;
297{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000298 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000299}
300
301static int
302d_setitem(ap, i, v)
303 arrayobject *ap;
304 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000305 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000306{
307 double x;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000308 if (!PyArg_Parse(v, "d;array item must be float", &x))
Guido van Rossum778983b1993-02-19 15:55:02 +0000309 return -1;
310 if (i >= 0)
311 ((double *)ap->ob_item)[i] = x;
312 return 0;
313}
314
315/* Description of types */
Guido van Rossum234f9421993-06-17 12:35:49 +0000316static struct arraydescr descriptors[] = {
Guido van Rossum778983b1993-02-19 15:55:02 +0000317 {'c', sizeof(char), c_getitem, c_setitem},
318 {'b', sizeof(char), b_getitem, b_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000319 {'B', sizeof(char), BB_getitem, BB_setitem},
Guido van Rossum778983b1993-02-19 15:55:02 +0000320 {'h', sizeof(short), h_getitem, h_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000321 {'H', sizeof(short), HH_getitem, HH_setitem},
Guido van Rossume77a7571993-11-03 15:01:26 +0000322 {'i', sizeof(int), i_getitem, i_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000323 {'I', sizeof(int), II_getitem, II_setitem},
Guido van Rossum778983b1993-02-19 15:55:02 +0000324 {'l', sizeof(long), l_getitem, l_setitem},
Guido van Rossum1a747f81997-05-16 16:21:38 +0000325 {'L', sizeof(long), LL_getitem, LL_setitem},
Guido van Rossum778983b1993-02-19 15:55:02 +0000326 {'f', sizeof(float), f_getitem, f_setitem},
327 {'d', sizeof(double), d_getitem, d_setitem},
328 {'\0', 0, 0, 0} /* Sentinel */
329};
Guido van Rossume77a7571993-11-03 15:01:26 +0000330/* If we ever allow items larger than double, we must change reverse()! */
Guido van Rossum778983b1993-02-19 15:55:02 +0000331
332
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000333static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000334newarrayobject(size, descr)
335 int size;
336 struct arraydescr *descr;
337{
Guido van Rossum778983b1993-02-19 15:55:02 +0000338 arrayobject *op;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000339 size_t nbytes;
Guido van Rossum778983b1993-02-19 15:55:02 +0000340 if (size < 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000341 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000342 return NULL;
343 }
344 nbytes = size * descr->itemsize;
345 /* Check for overflow */
Guido van Rossum7844e381997-04-11 20:44:04 +0000346 if (nbytes / descr->itemsize != (size_t)size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000347 return PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000348 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000349 op = PyObject_NewVar(arrayobject, &Arraytype, size);
Guido van Rossum778983b1993-02-19 15:55:02 +0000350 if (op == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000351 return PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000352 }
353 if (size <= 0) {
354 op->ob_item = NULL;
355 }
356 else {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000357 op->ob_item = PyMem_NEW(char, nbytes);
Guido van Rossum778983b1993-02-19 15:55:02 +0000358 if (op->ob_item == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000359 PyObject_Del(op);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000360 return PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000361 }
362 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000363 op->ob_descr = descr;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000364 return (PyObject *) op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000365}
366
Guido van Rossuma376cc51996-12-05 23:43:35 +0000367#if 0
Guido van Rossum62de97f1995-01-22 00:48:41 +0000368static int
Guido van Rossum778983b1993-02-19 15:55:02 +0000369getarraysize(op)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000370 PyObject *op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000371{
372 if (!is_arrayobject(op)) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000373 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000374 return -1;
375 }
376 return ((arrayobject *)op) -> ob_size;
377}
Guido van Rossuma376cc51996-12-05 23:43:35 +0000378#endif
Guido van Rossum778983b1993-02-19 15:55:02 +0000379
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000380static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000381getarrayitem(op, i)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000382 PyObject *op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000383 int i;
384{
385 register arrayobject *ap;
386 if (!is_arrayobject(op)) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000387 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000388 return NULL;
389 }
390 ap = (arrayobject *)op;
391 if (i < 0 || i >= ap->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000392 PyErr_SetString(PyExc_IndexError, "array index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000393 return NULL;
394 }
395 return (*ap->ob_descr->getitem)(ap, i);
396}
397
398static int
399ins1(self, where, v)
400 arrayobject *self;
401 int where;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000402 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000403{
Guido van Rossum778983b1993-02-19 15:55:02 +0000404 char *items;
405 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000406 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000407 return -1;
408 }
409 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
410 return -1;
411 items = self->ob_item;
Roger E. Masse5817f8f1996-12-09 22:24:19 +0000412 PyMem_RESIZE(items, char,
413 (self->ob_size+1) * self->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000414 if (items == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000415 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000416 return -1;
417 }
418 if (where < 0)
419 where = 0;
420 if (where > self->ob_size)
421 where = self->ob_size;
422 memmove(items + (where+1)*self->ob_descr->itemsize,
423 items + where*self->ob_descr->itemsize,
424 (self->ob_size-where)*self->ob_descr->itemsize);
425 self->ob_item = items;
426 self->ob_size++;
427 return (*self->ob_descr->setitem)(self, where, v);
428}
429
Guido van Rossuma376cc51996-12-05 23:43:35 +0000430#if 0
Guido van Rossum62de97f1995-01-22 00:48:41 +0000431static int
Guido van Rossum778983b1993-02-19 15:55:02 +0000432insarrayitem(op, where, newitem)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000433 PyObject *op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000434 int where;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000435 PyObject *newitem;
Guido van Rossum778983b1993-02-19 15:55:02 +0000436{
437 if (!is_arrayobject(op)) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000438 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000439 return -1;
440 }
441 return ins1((arrayobject *)op, where, newitem);
442}
443
Guido van Rossum62de97f1995-01-22 00:48:41 +0000444static int
Guido van Rossum778983b1993-02-19 15:55:02 +0000445addarrayitem(op, newitem)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000446 PyObject *op;
447 PyObject *newitem;
Guido van Rossum778983b1993-02-19 15:55:02 +0000448{
449 if (!is_arrayobject(op)) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000450 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000451 return -1;
452 }
453 return ins1((arrayobject *)op,
454 (int) ((arrayobject *)op)->ob_size, newitem);
455}
Guido van Rossuma376cc51996-12-05 23:43:35 +0000456#endif
Guido van Rossum778983b1993-02-19 15:55:02 +0000457
458/* Methods */
459
460static void
461array_dealloc(op)
462 arrayobject *op;
463{
Guido van Rossum778983b1993-02-19 15:55:02 +0000464 if (op->ob_item != NULL)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000465 PyMem_DEL(op->ob_item);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000466 PyObject_Del(op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000467}
468
469static int
470array_compare(v, w)
471 arrayobject *v, *w;
472{
473 int len = (v->ob_size < w->ob_size) ? v->ob_size : w->ob_size;
474 int i;
475 for (i = 0; i < len; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000476 PyObject *ai, *bi;
Guido van Rossum778983b1993-02-19 15:55:02 +0000477 int cmp;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000478 ai = getarrayitem((PyObject *)v, i);
479 bi = getarrayitem((PyObject *)w, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000480 if (ai && bi)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000481 cmp = PyObject_Compare(ai, bi);
Guido van Rossum778983b1993-02-19 15:55:02 +0000482 else
483 cmp = -1;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000484 Py_XDECREF(ai);
485 Py_XDECREF(bi);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000486 if (cmp != 0)
Guido van Rossum778983b1993-02-19 15:55:02 +0000487 return cmp;
Guido van Rossum778983b1993-02-19 15:55:02 +0000488 }
489 return v->ob_size - w->ob_size;
490}
491
492static int
493array_length(a)
494 arrayobject *a;
495{
496 return a->ob_size;
497}
498
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000499static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000500array_item(a, i)
501 arrayobject *a;
502 int i;
503{
504 if (i < 0 || i >= a->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000505 PyErr_SetString(PyExc_IndexError, "array index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000506 return NULL;
507 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000508 return getarrayitem((PyObject *)a, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000509}
510
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000511static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000512array_slice(a, ilow, ihigh)
513 arrayobject *a;
514 int ilow, ihigh;
515{
516 arrayobject *np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000517 if (ilow < 0)
518 ilow = 0;
519 else if (ilow > a->ob_size)
520 ilow = a->ob_size;
521 if (ihigh < 0)
522 ihigh = 0;
523 if (ihigh < ilow)
524 ihigh = ilow;
525 else if (ihigh > a->ob_size)
526 ihigh = a->ob_size;
527 np = (arrayobject *) newarrayobject(ihigh - ilow, a->ob_descr);
528 if (np == NULL)
529 return NULL;
530 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
531 (ihigh-ilow) * a->ob_descr->itemsize);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000532 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000533}
534
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000535static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000536array_concat(a, bb)
537 arrayobject *a;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000538 PyObject *bb;
Guido van Rossum778983b1993-02-19 15:55:02 +0000539{
540 int size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000541 arrayobject *np;
542 if (!is_arrayobject(bb)) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000543 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000544 return NULL;
545 }
546#define b ((arrayobject *)bb)
547 if (a->ob_descr != b->ob_descr) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000548 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000549 return NULL;
550 }
551 size = a->ob_size + b->ob_size;
552 np = (arrayobject *) newarrayobject(size, a->ob_descr);
553 if (np == NULL) {
554 return NULL;
555 }
556 memcpy(np->ob_item, a->ob_item, a->ob_size*a->ob_descr->itemsize);
557 memcpy(np->ob_item + a->ob_size*a->ob_descr->itemsize,
Guido van Rossum32be3a71993-11-05 10:16:27 +0000558 b->ob_item, b->ob_size*b->ob_descr->itemsize);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000559 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000560#undef b
561}
562
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000563static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000564array_repeat(a, n)
565 arrayobject *a;
566 int n;
567{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000568 int i;
Guido van Rossum778983b1993-02-19 15:55:02 +0000569 int size;
570 arrayobject *np;
571 char *p;
572 int nbytes;
573 if (n < 0)
574 n = 0;
575 size = a->ob_size * n;
576 np = (arrayobject *) newarrayobject(size, a->ob_descr);
577 if (np == NULL)
578 return NULL;
579 p = np->ob_item;
580 nbytes = a->ob_size * a->ob_descr->itemsize;
581 for (i = 0; i < n; i++) {
582 memcpy(p, a->ob_item, nbytes);
583 p += nbytes;
584 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000585 return (PyObject *) np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000586}
587
588static int
589array_ass_slice(a, ilow, ihigh, v)
590 arrayobject *a;
591 int ilow, ihigh;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000592 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000593{
594 char *item;
595 int n; /* Size of replacement array */
596 int d; /* Change in size */
Guido van Rossum778983b1993-02-19 15:55:02 +0000597#define b ((arrayobject *)v)
598 if (v == NULL)
599 n = 0;
600 else if (is_arrayobject(v)) {
601 n = b->ob_size;
602 if (a == b) {
603 /* Special case "a[i:j] = a" -- copy b first */
604 int ret;
605 v = array_slice(b, 0, n);
606 ret = array_ass_slice(a, ilow, ihigh, v);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000607 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000608 return ret;
609 }
610 if (b->ob_descr != a->ob_descr) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000611 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000612 return -1;
613 }
614 }
615 else {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000616 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000617 return -1;
618 }
619 if (ilow < 0)
620 ilow = 0;
621 else if (ilow > a->ob_size)
622 ilow = a->ob_size;
623 if (ihigh < 0)
624 ihigh = 0;
625 if (ihigh < ilow)
626 ihigh = ilow;
627 else if (ihigh > a->ob_size)
628 ihigh = a->ob_size;
629 item = a->ob_item;
630 d = n - (ihigh-ilow);
631 if (d < 0) { /* Delete -d items */
632 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
633 item + ihigh*a->ob_descr->itemsize,
634 (a->ob_size-ihigh)*a->ob_descr->itemsize);
635 a->ob_size += d;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000636 PyMem_RESIZE(item, char, a->ob_size*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000637 /* Can't fail */
638 a->ob_item = item;
639 }
640 else if (d > 0) { /* Insert d items */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000641 PyMem_RESIZE(item, char,
642 (a->ob_size + d)*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000643 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000644 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000645 return -1;
646 }
647 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
648 item + ihigh*a->ob_descr->itemsize,
649 (a->ob_size-ihigh)*a->ob_descr->itemsize);
650 a->ob_item = item;
651 a->ob_size += d;
652 }
653 if (n > 0)
654 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
655 n*b->ob_descr->itemsize);
656 return 0;
657#undef b
658}
659
660static int
661array_ass_item(a, i, v)
662 arrayobject *a;
663 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000664 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000665{
666 if (i < 0 || i >= a->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000667 PyErr_SetString(PyExc_IndexError,
668 "array assignment index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000669 return -1;
670 }
671 if (v == NULL)
672 return array_ass_slice(a, i, i+1, v);
673 return (*a->ob_descr->setitem)(a, i, v);
674}
675
676static int
677setarrayitem(a, i, v)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000678 PyObject *a;
Guido van Rossum778983b1993-02-19 15:55:02 +0000679 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000680 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000681{
682 if (!is_arrayobject(a)) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000683 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000684 return -1;
685 }
686 return array_ass_item((arrayobject *)a, i, v);
687}
688
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000689static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000690ins(self, where, v)
691 arrayobject *self;
692 int where;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000693 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000694{
695 if (ins1(self, where, v) != 0)
696 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000697 Py_INCREF(Py_None);
698 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000699}
700
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000701static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000702array_insert(self, args)
703 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000704 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000705{
706 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000707 PyObject *v;
708 if (!PyArg_Parse(args, "(iO)", &i, &v))
Guido van Rossum778983b1993-02-19 15:55:02 +0000709 return NULL;
710 return ins(self, i, v);
711}
712
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000713static char insert_doc [] =
714"insert (i,x)\n\
715\n\
716Insert a new item x into the array before position i.";
717
718
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000719static PyObject *
Guido van Rossumde4a4ca1997-08-12 14:55:56 +0000720array_buffer_info(self, args)
721 arrayobject *self;
722 PyObject *args;
723{
724 return Py_BuildValue("ll",
725 (long)(self->ob_item), (long)(self->ob_size));
726}
727
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000728static char buffer_info_doc [] =
729"buffer_info -> (address, length)\n\
730\n\
731Return a tuple (address, length) giving the current memory address and\n\
732the length in bytes of the buffer used to hold array's contents.";
733
734
Guido van Rossumde4a4ca1997-08-12 14:55:56 +0000735static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000736array_append(self, args)
737 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000738 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000739{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000740 PyObject *v;
741 if (!PyArg_Parse(args, "O", &v))
Guido van Rossum778983b1993-02-19 15:55:02 +0000742 return NULL;
743 return ins(self, (int) self->ob_size, v);
744}
745
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000746static char append_doc [] =
747"append(x)\n\
748\n\
749Append new value x to the end of the array.";
750
751
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000752static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000753array_byteswap(self, args)
754 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000755 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000756{
757 char *p;
758 int i;
Fred Drakebf272981999-12-03 17:15:30 +0000759
760 if (!PyArg_ParseTuple(args, ":byteswap"))
761 return NULL;
762
Guido van Rossum778983b1993-02-19 15:55:02 +0000763 switch (self->ob_descr->itemsize) {
764 case 1:
765 break;
766 case 2:
767 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 2) {
768 char p0 = p[0];
769 p[0] = p[1];
770 p[1] = p0;
771 }
772 break;
773 case 4:
774 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 4) {
775 char p0 = p[0];
776 char p1 = p[1];
777 p[0] = p[3];
778 p[1] = p[2];
779 p[2] = p1;
780 p[3] = p0;
781 }
782 break;
Guido van Rossume77a7571993-11-03 15:01:26 +0000783 case 8:
784 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 8) {
785 char p0 = p[0];
786 char p1 = p[1];
787 char p2 = p[2];
788 char p3 = p[3];
789 p[0] = p[7];
790 p[1] = p[6];
791 p[2] = p[5];
792 p[3] = p[4];
793 p[4] = p3;
794 p[5] = p2;
795 p[6] = p1;
796 p[7] = p0;
797 }
798 break;
Guido van Rossum778983b1993-02-19 15:55:02 +0000799 default:
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000800 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossum778983b1993-02-19 15:55:02 +0000801 "don't know how to byteswap this array type");
802 return NULL;
803 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000804 Py_INCREF(Py_None);
805 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000806}
807
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000808static char byteswap_doc [] =
Fred Drakebf272981999-12-03 17:15:30 +0000809"byteswap()\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000810\n\
Fred Drakebf272981999-12-03 17:15:30 +0000811Byteswap all items of the array. If the items in the array are not 1, 2,\n\
8124, or 8 bytes in size, RuntimeError is raised.";
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000813
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000814static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000815array_reverse(self, args)
816 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000817 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000818{
Guido van Rossume77a7571993-11-03 15:01:26 +0000819 register int itemsize = self->ob_descr->itemsize;
820 register char *p, *q;
821 char tmp[sizeof(double)]; /* Assume that's the max item size */
822
Guido van Rossum778983b1993-02-19 15:55:02 +0000823 if (args != NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000824 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000825 return NULL;
826 }
827
828 if (self->ob_size > 1) {
Guido van Rossume77a7571993-11-03 15:01:26 +0000829 for (p = self->ob_item,
830 q = self->ob_item + (self->ob_size - 1)*itemsize;
831 p < q;
832 p += itemsize, q -= itemsize) {
833 memmove(tmp, p, itemsize);
834 memmove(p, q, itemsize);
835 memmove(q, tmp, itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000836 }
837 }
838
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000839 Py_INCREF(Py_None);
840 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000841}
Guido van Rossume77a7571993-11-03 15:01:26 +0000842
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000843static char reverse_doc [] =
844"reverse()\n\
845\n\
846Reverse the order of the items in the array.";
847
Guido van Rossume77a7571993-11-03 15:01:26 +0000848/* The following routines were adapted from listobject.c but not converted.
849 To make them work you will have to work! */
Guido van Rossum778983b1993-02-19 15:55:02 +0000850
851#if 0
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000852static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000853array_index(self, args)
854 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000855 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000856{
857 int i;
858
859 if (args == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000860 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000861 return NULL;
862 }
863 for (i = 0; i < self->ob_size; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000864 if (PyObject_Compare(self->ob_item[i], args) == 0)
865 return PyInt_FromLong((long)i);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000866 /* XXX PyErr_Occurred */
Guido van Rossum778983b1993-02-19 15:55:02 +0000867 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000868 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in array");
Guido van Rossum778983b1993-02-19 15:55:02 +0000869 return NULL;
870}
871#endif
872
873#if 0
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000874static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000875array_count(self, args)
876 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000877 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000878{
879 int count = 0;
880 int i;
881
882 if (args == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000883 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000884 return NULL;
885 }
886 for (i = 0; i < self->ob_size; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000887 if (PyObject_Compare(self->ob_item[i], args) == 0)
Guido van Rossum778983b1993-02-19 15:55:02 +0000888 count++;
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000889 /* XXX PyErr_Occurred */
Guido van Rossum778983b1993-02-19 15:55:02 +0000890 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000891 return PyInt_FromLong((long)count);
Guido van Rossum778983b1993-02-19 15:55:02 +0000892}
893#endif
894
895#if 0
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000896static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000897array_remove(self, args)
898 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000899 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000900{
901 int i;
902
903 if (args == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000904 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000905 return NULL;
906 }
907 for (i = 0; i < self->ob_size; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000908 if (PyObject_Compare(self->ob_item[i], args) == 0) {
Roger E. Masse5817f8f1996-12-09 22:24:19 +0000909 if (array_ass_slice(self, i, i+1,
910 (PyObject *)NULL) != 0)
Guido van Rossum778983b1993-02-19 15:55:02 +0000911 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000912 Py_INCREF(Py_None);
913 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000914 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000915 /* XXX PyErr_Occurred */
Guido van Rossum778983b1993-02-19 15:55:02 +0000916 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000917 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in array");
Guido van Rossum778983b1993-02-19 15:55:02 +0000918 return NULL;
919}
920#endif
921
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000922static PyObject *
Guido van Rossume77a7571993-11-03 15:01:26 +0000923array_fromfile(self, args)
Guido van Rossum778983b1993-02-19 15:55:02 +0000924 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000925 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000926{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000927 PyObject *f;
Guido van Rossum778983b1993-02-19 15:55:02 +0000928 int n;
929 FILE *fp;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000930 if (!PyArg_Parse(args, "(Oi)", &f, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +0000931 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000932 fp = PyFile_AsFile(f);
Guido van Rossum778983b1993-02-19 15:55:02 +0000933 if (fp == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000934 PyErr_SetString(PyExc_TypeError, "arg1 must be open file");
Guido van Rossum778983b1993-02-19 15:55:02 +0000935 return NULL;
936 }
937 if (n > 0) {
938 char *item = self->ob_item;
939 int itemsize = self->ob_descr->itemsize;
940 int nread;
Guido van Rossum3791b0d1999-02-23 18:05:22 +0000941 int newlength;
942 size_t newbytes;
943 /* Be careful here about overflow */
944 if ((newlength = self->ob_size + n) <= 0 ||
Guido van Rossum481ac881999-03-19 21:50:11 +0000945 (newbytes = newlength * itemsize) / itemsize !=
946 (size_t)newlength)
Guido van Rossum3791b0d1999-02-23 18:05:22 +0000947 goto nomem;
948 PyMem_RESIZE(item, char, newbytes);
Guido van Rossum778983b1993-02-19 15:55:02 +0000949 if (item == NULL) {
Guido van Rossum3791b0d1999-02-23 18:05:22 +0000950 nomem:
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000951 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000952 return NULL;
953 }
954 self->ob_item = item;
955 self->ob_size += n;
956 nread = fread(item + (self->ob_size - n) * itemsize,
957 itemsize, n, fp);
958 if (nread < n) {
959 self->ob_size -= (n - nread);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000960 PyMem_RESIZE(item, char, self->ob_size*itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000961 self->ob_item = item;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000962 PyErr_SetString(PyExc_EOFError,
963 "not enough items in file");
Guido van Rossum778983b1993-02-19 15:55:02 +0000964 return NULL;
965 }
966 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000967 Py_INCREF(Py_None);
968 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000969}
970
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000971static char fromfile_doc [] =
972"fromfile(f, n)\n\
973\n\
974Read n objects from the file object f and append them to the end of the\n\
975array. Also called as read.";
976
977
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000978static PyObject *
Guido van Rossume77a7571993-11-03 15:01:26 +0000979array_tofile(self, args)
Guido van Rossum778983b1993-02-19 15:55:02 +0000980 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000981 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000982{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000983 PyObject *f;
Guido van Rossum778983b1993-02-19 15:55:02 +0000984 FILE *fp;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000985 if (!PyArg_Parse(args, "O", &f))
Guido van Rossum778983b1993-02-19 15:55:02 +0000986 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000987 fp = PyFile_AsFile(f);
Guido van Rossum778983b1993-02-19 15:55:02 +0000988 if (fp == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000989 PyErr_SetString(PyExc_TypeError, "arg must be open file");
Guido van Rossum778983b1993-02-19 15:55:02 +0000990 return NULL;
991 }
992 if (self->ob_size > 0) {
Guido van Rossum7844e381997-04-11 20:44:04 +0000993 if ((int)fwrite(self->ob_item, self->ob_descr->itemsize,
Guido van Rossum778983b1993-02-19 15:55:02 +0000994 self->ob_size, fp) != self->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000995 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum778983b1993-02-19 15:55:02 +0000996 clearerr(fp);
997 return NULL;
998 }
999 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001000 Py_INCREF(Py_None);
1001 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001002}
1003
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001004static char tofile_doc [] =
1005"tofile(f)\n\
1006\n\
1007Write all items (as machine values) to the file object f. Also called as\n\
1008write.";
1009
1010
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001011static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001012array_fromlist(self, args)
1013 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001014 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +00001015{
1016 int n;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001017 PyObject *list;
Guido van Rossum778983b1993-02-19 15:55:02 +00001018 int itemsize = self->ob_descr->itemsize;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001019 if (!PyArg_Parse(args, "O", &list))
Guido van Rossum778983b1993-02-19 15:55:02 +00001020 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001021 if (!PyList_Check(list)) {
1022 PyErr_SetString(PyExc_TypeError, "arg must be list");
Guido van Rossum778983b1993-02-19 15:55:02 +00001023 return NULL;
1024 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001025 n = PyList_Size(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001026 if (n > 0) {
1027 char *item = self->ob_item;
1028 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001029 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001030 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001031 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001032 return NULL;
1033 }
1034 self->ob_item = item;
1035 self->ob_size += n;
1036 for (i = 0; i < n; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001037 PyObject *v = PyList_GetItem(list, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001038 if ((*self->ob_descr->setitem)(self,
1039 self->ob_size - n + i, v) != 0) {
1040 self->ob_size -= n;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001041 PyMem_RESIZE(item, char,
1042 self->ob_size * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001043 self->ob_item = item;
1044 return NULL;
1045 }
1046 }
1047 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001048 Py_INCREF(Py_None);
1049 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001050}
1051
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001052static char fromlist_doc [] =
1053"fromlist(list)\n\
1054\n\
1055Append items to array from list.";
1056
1057
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001058static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001059array_tolist(self, args)
1060 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001061 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +00001062{
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001063 PyObject *list = PyList_New(self->ob_size);
Guido van Rossum778983b1993-02-19 15:55:02 +00001064 int i;
1065 if (list == NULL)
1066 return NULL;
1067 for (i = 0; i < self->ob_size; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001068 PyObject *v = getarrayitem((PyObject *)self, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001069 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001070 Py_DECREF(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001071 return NULL;
1072 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001073 PyList_SetItem(list, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001074 }
1075 return list;
1076}
1077
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001078static char tolist_doc [] =
Guido van Rossumfc6aba51998-10-14 02:52:31 +00001079"tolist() -> list\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001080\n\
1081Convert array to an ordinary list with the same items.";
1082
1083
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001084static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001085array_fromstring(self, args)
1086 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001087 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +00001088{
1089 char *str;
1090 int n;
1091 int itemsize = self->ob_descr->itemsize;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001092 if (!PyArg_Parse(args, "s#", &str, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +00001093 return NULL;
1094 if (n % itemsize != 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001095 PyErr_SetString(PyExc_ValueError,
Guido van Rossum778983b1993-02-19 15:55:02 +00001096 "string length not a multiple of item size");
1097 return NULL;
1098 }
1099 n = n / itemsize;
1100 if (n > 0) {
1101 char *item = self->ob_item;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001102 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001103 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001104 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001105 return NULL;
1106 }
1107 self->ob_item = item;
1108 self->ob_size += n;
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001109 memcpy(item + (self->ob_size - n) * itemsize,
1110 str, itemsize*n);
Guido van Rossum778983b1993-02-19 15:55:02 +00001111 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001112 Py_INCREF(Py_None);
1113 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001114}
1115
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001116static char fromstring_doc [] =
1117"fromstring(string)\n\
1118\n\
1119Appends items from the string, interpreting it as an array of machine\n\
1120values,as if it had been read from a file using the fromfile() method).";
1121
1122
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001123static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001124array_tostring(self, args)
1125 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001126 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +00001127{
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001128 if (!PyArg_Parse(args, ""))
Guido van Rossum778983b1993-02-19 15:55:02 +00001129 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001130 return PyString_FromStringAndSize(self->ob_item,
Guido van Rossum778983b1993-02-19 15:55:02 +00001131 self->ob_size * self->ob_descr->itemsize);
1132}
1133
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001134static char tostring_doc [] =
1135"tostring() -> string\n\
1136\n\
1137Convert the array to an array of machine values and return the string\n\
1138representation.";
1139
1140PyMethodDef array_methods[] = {
1141 {"append", (PyCFunction)array_append, 0, append_doc},
1142 {"buffer_info", (PyCFunction)array_buffer_info, 0, buffer_info_doc},
Fred Drakebf272981999-12-03 17:15:30 +00001143 {"byteswap", (PyCFunction)array_byteswap, METH_VARARGS,
1144 byteswap_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001145/* {"count", (method)array_count},*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001146 {"fromfile", (PyCFunction)array_fromfile, 0, fromfile_doc},
1147 {"fromlist", (PyCFunction)array_fromlist, 0, fromlist_doc},
1148 {"fromstring", (PyCFunction)array_fromstring, 0, fromstring_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001149/* {"index", (method)array_index},*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001150 {"insert", (PyCFunction)array_insert, 0, insert_doc},
1151 {"read", (PyCFunction)array_fromfile, 0, fromfile_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001152/* {"remove", (method)array_remove},*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001153 {"reverse", (PyCFunction)array_reverse, 0, reverse_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001154/* {"sort", (method)array_sort},*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001155 {"tofile", (PyCFunction)array_tofile, 0, tofile_doc},
1156 {"tolist", (PyCFunction)array_tolist, 0, tolist_doc},
1157 {"tostring", (PyCFunction)array_tostring, 0, tostring_doc},
1158 {"write", (PyCFunction)array_tofile, 0, tofile_doc},
Guido van Rossum778983b1993-02-19 15:55:02 +00001159 {NULL, NULL} /* sentinel */
1160};
1161
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001162static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001163array_getattr(a, name)
1164 arrayobject *a;
1165 char *name;
1166{
1167 if (strcmp(name, "typecode") == 0) {
1168 char tc = a->ob_descr->typecode;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001169 return PyString_FromStringAndSize(&tc, 1);
Guido van Rossum778983b1993-02-19 15:55:02 +00001170 }
1171 if (strcmp(name, "itemsize") == 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001172 return PyInt_FromLong((long)a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001173 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001174 if (strcmp(name, "__members__") == 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001175 PyObject *list = PyList_New(2);
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001176 if (list) {
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001177 PyList_SetItem(list, 0,
1178 PyString_FromString("typecode"));
1179 PyList_SetItem(list, 1,
1180 PyString_FromString("itemsize"));
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001181 if (PyErr_Occurred()) {
1182 Py_DECREF(list);
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001183 list = NULL;
1184 }
1185 }
1186 return list;
1187 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001188 return Py_FindMethod(array_methods, (PyObject *)a, name);
Guido van Rossum778983b1993-02-19 15:55:02 +00001189}
1190
1191static int
1192array_print(a, fp, flags)
1193 arrayobject *a;
1194 FILE *fp;
1195 int flags;
1196{
1197 int ok = 0;
1198 int i, len;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001199 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +00001200 len = a->ob_size;
1201 if (len == 0) {
1202 fprintf(fp, "array('%c')", a->ob_descr->typecode);
1203 return ok;
1204 }
1205 if (a->ob_descr->typecode == 'c') {
1206 fprintf(fp, "array('c', ");
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001207 v = array_tostring(a, (PyObject *)NULL);
1208 ok = PyObject_Print(v, fp, 0);
1209 Py_XDECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001210 fprintf(fp, ")");
1211 return ok;
1212 }
1213 fprintf(fp, "array('%c', [", a->ob_descr->typecode);
1214 for (i = 0; i < len && ok == 0; i++) {
1215 if (i > 0)
1216 fprintf(fp, ", ");
1217 v = (a->ob_descr->getitem)(a, i);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001218 ok = PyObject_Print(v, fp, 0);
1219 Py_XDECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001220 }
1221 fprintf(fp, "])");
1222 return ok;
1223}
1224
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001225static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001226array_repr(a)
1227 arrayobject *a;
1228{
1229 char buf[256];
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001230 PyObject *s, *t, *comma, *v;
Guido van Rossum778983b1993-02-19 15:55:02 +00001231 int i, len;
1232 len = a->ob_size;
1233 if (len == 0) {
1234 sprintf(buf, "array('%c')", a->ob_descr->typecode);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001235 return PyString_FromString(buf);
Guido van Rossum778983b1993-02-19 15:55:02 +00001236 }
1237 if (a->ob_descr->typecode == 'c') {
1238 sprintf(buf, "array('c', ");
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001239 s = PyString_FromString(buf);
1240 v = array_tostring(a, (PyObject *)NULL);
1241 t = PyObject_Repr(v);
1242 Py_XDECREF(v);
1243 PyString_ConcatAndDel(&s, t);
1244 PyString_ConcatAndDel(&s, PyString_FromString(")"));
Guido van Rossum778983b1993-02-19 15:55:02 +00001245 return s;
1246 }
1247 sprintf(buf, "array('%c', [", a->ob_descr->typecode);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001248 s = PyString_FromString(buf);
1249 comma = PyString_FromString(", ");
1250 for (i = 0; i < len && !PyErr_Occurred(); i++) {
Guido van Rossumb6775db1994-08-01 11:34:53 +00001251 if (i > 0)
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001252 PyString_Concat(&s, comma);
Guido van Rossum778983b1993-02-19 15:55:02 +00001253 v = (a->ob_descr->getitem)(a, i);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001254 t = PyObject_Repr(v);
1255 Py_XDECREF(v);
1256 PyString_ConcatAndDel(&s, t);
Guido van Rossum778983b1993-02-19 15:55:02 +00001257 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001258 Py_XDECREF(comma);
1259 PyString_ConcatAndDel(&s, PyString_FromString("])"));
Guido van Rossum778983b1993-02-19 15:55:02 +00001260 return s;
1261}
1262
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001263static int
1264array_buffer_getreadbuf(self, index, ptr)
1265 arrayobject *self;
1266 int index;
1267 const void **ptr;
1268{
1269 if ( index != 0 ) {
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001270 PyErr_SetString(PyExc_SystemError,
1271 "Accessing non-existent array segment");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001272 return -1;
1273 }
1274 *ptr = (void *)self->ob_item;
1275 return self->ob_size*self->ob_descr->itemsize;
1276}
1277
1278static int
1279array_buffer_getwritebuf(self, index, ptr)
1280 arrayobject *self;
1281 int index;
1282 const void **ptr;
1283{
1284 if ( index != 0 ) {
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001285 PyErr_SetString(PyExc_SystemError,
1286 "Accessing non-existent array segment");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001287 return -1;
1288 }
1289 *ptr = (void *)self->ob_item;
1290 return self->ob_size*self->ob_descr->itemsize;
1291}
1292
1293static int
1294array_buffer_getsegcount(self, lenp)
1295 arrayobject *self;
1296 int *lenp;
1297{
1298 if ( lenp )
1299 *lenp = self->ob_size*self->ob_descr->itemsize;
1300 return 1;
1301}
1302
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001303static PySequenceMethods array_as_sequence = {
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001304 (inquiry)array_length, /*sq_length*/
1305 (binaryfunc)array_concat, /*sq_concat*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001306 (intargfunc)array_repeat, /*sq_repeat*/
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001307 (intargfunc)array_item, /*sq_item*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001308 (intintargfunc)array_slice, /*sq_slice*/
1309 (intobjargproc)array_ass_item, /*sq_ass_item*/
1310 (intintobjargproc)array_ass_slice, /*sq_ass_slice*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001311};
1312
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001313static PyBufferProcs array_as_buffer = {
1314 (getreadbufferproc)array_buffer_getreadbuf,
1315 (getwritebufferproc)array_buffer_getwritebuf,
1316 (getsegcountproc)array_buffer_getsegcount,
1317};
1318
1319
Guido van Rossum778983b1993-02-19 15:55:02 +00001320
1321
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001322static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001323a_array(self, args)
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001324 PyObject *self;
1325 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +00001326{
1327 char c;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001328 PyObject *initial = NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001329 struct arraydescr *descr;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001330 if (!PyArg_Parse(args, "c", &c)) {
1331 PyErr_Clear();
1332 if (!PyArg_Parse(args, "(cO)", &c, &initial))
Guido van Rossum778983b1993-02-19 15:55:02 +00001333 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001334 if (!PyList_Check(initial) && !PyString_Check(initial)) {
1335 PyErr_SetString(PyExc_TypeError,
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001336 "array initializer must be list or string");
Guido van Rossum778983b1993-02-19 15:55:02 +00001337 return NULL;
1338 }
1339 }
1340 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1341 if (descr->typecode == c) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001342 PyObject *a;
Guido van Rossum778983b1993-02-19 15:55:02 +00001343 int len;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001344 if (initial == NULL || !PyList_Check(initial))
Guido van Rossum778983b1993-02-19 15:55:02 +00001345 len = 0;
1346 else
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001347 len = PyList_Size(initial);
Guido van Rossum778983b1993-02-19 15:55:02 +00001348 a = newarrayobject(len, descr);
1349 if (a == NULL)
1350 return NULL;
1351 if (len > 0) {
1352 int i;
1353 for (i = 0; i < len; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001354 PyObject *v =
1355 PyList_GetItem(initial, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001356 if (setarrayitem(a, i, v) != 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001357 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001358 return NULL;
1359 }
1360 }
1361 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001362 if (initial != NULL && PyString_Check(initial)) {
1363 PyObject *v =
Guido van Rossum778983b1993-02-19 15:55:02 +00001364 array_fromstring((arrayobject *)a, initial);
1365 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001366 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001367 return NULL;
1368 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001369 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001370 }
1371 return a;
1372 }
1373 }
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001374 PyErr_SetString(PyExc_ValueError,
Guido van Rossum549ab711997-01-03 19:09:47 +00001375 "bad typecode (must be c, b, B, h, H, i, I, l, L, f or d)");
Guido van Rossum778983b1993-02-19 15:55:02 +00001376 return NULL;
1377}
1378
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001379static char a_array_doc [] =
1380"array(typecode [, initializer]) -> array\n\
1381\n\
1382Return a new array whose items are restricted by typecode, and\n\
1383initialized from the optional initializer value, which must be a list\n\
1384or a string.";
1385
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001386static PyMethodDef a_methods[] = {
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001387 {"array", a_array, 0, a_array_doc},
Guido van Rossum778983b1993-02-19 15:55:02 +00001388 {NULL, NULL} /* sentinel */
1389};
1390
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001391static char module_doc [] =
1392"This module defines a new object type which can efficiently represent\n\
1393an array of basic values: characters, integers, floating point\n\
1394numbers. Arrays are sequence types and behave very much like lists,\n\
1395except that the type of objects stored in them is constrained. The\n\
1396type is specified at object creation time by using a type code, which\n\
1397is a single character. The following type codes are defined:\n\
1398\n\
1399 Type code C Type Minimum size in bytes \n\
1400 'c' character 1 \n\
1401 'b' signed integer 1 \n\
1402 'B' unsigned integer 1 \n\
1403 'h' signed integer 2 \n\
1404 'H' unsigned integer 2 \n\
1405 'i' signed integer 2 \n\
1406 'I' unsigned integer 2 \n\
1407 'l' signed integer 4 \n\
1408 'L' unsigned integer 4 \n\
1409 'f' floating point 4 \n\
1410 'd' floating point 8 \n\
1411\n\
1412Functions:\n\
1413\n\
1414array(typecode [, initializer]) -- create a new array\n\
1415\n\
1416Special Objects:\n\
1417\n\
1418ArrayType -- type object for array objects\n\
1419";
1420
1421static char arraytype_doc [] =
1422"An array represents basic values and behave very much like lists, except\n\
1423the type of objects stored in them is constrained.\n\
1424\n\
1425Methods:\n\
1426\n\
1427append() -- append a new item to the end of the array\n\
1428buffer_info() -- return information giving the current memory info\n\
1429byteswap() -- byteswap all the items of the array\n\
1430fromfile() -- read items from a file object\n\
1431fromlist() -- append items from the list\n\
1432fromstring() -- append items from the string\n\
1433insert() -- insert a new item into the array at a provided position\n\
1434read() -- DEPRECATED, use fromfile()\n\
1435reverse() -- reverse the order of the items in the array\n\
1436tofile() -- write all items to a file object\n\
1437tolist() -- return the array converted to an ordinary list\n\
1438tostring() -- return the array converted to a string\n\
1439write() -- DEPRECATED, use tofile()\n\
1440\n\
1441Variables:\n\
1442\n\
1443typecode -- the typecode character used to create the array\n\
1444itemsize -- the length in bytes of one array item\n\
1445";
1446
1447statichere PyTypeObject Arraytype = {
Fred Drake0d40ba42000-02-04 20:33:49 +00001448 PyObject_HEAD_INIT(NULL)
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001449 0,
1450 "array",
1451 sizeof(arrayobject),
1452 0,
1453 (destructor)array_dealloc, /*tp_dealloc*/
1454 (printfunc)array_print, /*tp_print*/
1455 (getattrfunc)array_getattr, /*tp_getattr*/
1456 0, /*tp_setattr*/
1457 (cmpfunc)array_compare, /*tp_compare*/
1458 (reprfunc)array_repr, /*tp_repr*/
1459 0, /*tp_as_number*/
1460 &array_as_sequence, /*tp_as_sequence*/
1461 0, /*tp_as_mapping*/
1462 0, /*tp_hash*/
1463 0, /*tp_call*/
1464 0, /*tp_str*/
1465 0, /*tp_getattro*/
1466 0, /*tp_setattro*/
1467 &array_as_buffer, /*tp_as_buffer*/
1468 0, /*tp_xxx4*/
1469 arraytype_doc, /*tp_doc*/
1470};
1471
Guido van Rossum3886bb61998-12-04 18:50:17 +00001472DL_EXPORT(void)
Guido van Rossum778983b1993-02-19 15:55:02 +00001473initarray()
1474{
Guido van Rossumb6190d31997-05-22 14:56:36 +00001475 PyObject *m, *d;
Fred Drake0d40ba42000-02-04 20:33:49 +00001476
1477 Arraytype.ob_type = &PyType_Type;
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001478 m = Py_InitModule3("array", a_methods, module_doc);
Guido van Rossumb6190d31997-05-22 14:56:36 +00001479 d = PyModule_GetDict(m);
Guido van Rossuma0deb641998-10-14 13:45:06 +00001480 PyDict_SetItemString(d, "ArrayType", (PyObject *)&Arraytype);
1481 /* No need to check the error here, the caller will do that */
Guido van Rossum778983b1993-02-19 15:55:02 +00001482}