blob: dcd931b2823e17b044b100a2e296a8a50d258405 [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 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000349 op = PyMem_NEW(arrayobject, 1);
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) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000359 PyMem_DEL(op);
360 return PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000361 }
362 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000363 op->ob_type = &Arraytype;
364 op->ob_size = size;
365 op->ob_descr = descr;
Guido van Rossumbffd6832000-01-20 22:32:56 +0000366 _Py_NewReference((PyObject *)op);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000367 return (PyObject *) op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000368}
369
Guido van Rossuma376cc51996-12-05 23:43:35 +0000370#if 0
Guido van Rossum62de97f1995-01-22 00:48:41 +0000371static int
Guido van Rossum778983b1993-02-19 15:55:02 +0000372getarraysize(op)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000373 PyObject *op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000374{
375 if (!is_arrayobject(op)) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000376 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000377 return -1;
378 }
379 return ((arrayobject *)op) -> ob_size;
380}
Guido van Rossuma376cc51996-12-05 23:43:35 +0000381#endif
Guido van Rossum778983b1993-02-19 15:55:02 +0000382
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000383static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000384getarrayitem(op, i)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000385 PyObject *op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000386 int i;
387{
388 register arrayobject *ap;
389 if (!is_arrayobject(op)) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000390 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000391 return NULL;
392 }
393 ap = (arrayobject *)op;
394 if (i < 0 || i >= ap->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000395 PyErr_SetString(PyExc_IndexError, "array index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000396 return NULL;
397 }
398 return (*ap->ob_descr->getitem)(ap, i);
399}
400
401static int
402ins1(self, where, v)
403 arrayobject *self;
404 int where;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000405 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000406{
Guido van Rossum778983b1993-02-19 15:55:02 +0000407 char *items;
408 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000409 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000410 return -1;
411 }
412 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
413 return -1;
414 items = self->ob_item;
Roger E. Masse5817f8f1996-12-09 22:24:19 +0000415 PyMem_RESIZE(items, char,
416 (self->ob_size+1) * self->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000417 if (items == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000418 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000419 return -1;
420 }
421 if (where < 0)
422 where = 0;
423 if (where > self->ob_size)
424 where = self->ob_size;
425 memmove(items + (where+1)*self->ob_descr->itemsize,
426 items + where*self->ob_descr->itemsize,
427 (self->ob_size-where)*self->ob_descr->itemsize);
428 self->ob_item = items;
429 self->ob_size++;
430 return (*self->ob_descr->setitem)(self, where, v);
431}
432
Guido van Rossuma376cc51996-12-05 23:43:35 +0000433#if 0
Guido van Rossum62de97f1995-01-22 00:48:41 +0000434static int
Guido van Rossum778983b1993-02-19 15:55:02 +0000435insarrayitem(op, where, newitem)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000436 PyObject *op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000437 int where;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000438 PyObject *newitem;
Guido van Rossum778983b1993-02-19 15:55:02 +0000439{
440 if (!is_arrayobject(op)) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000441 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000442 return -1;
443 }
444 return ins1((arrayobject *)op, where, newitem);
445}
446
Guido van Rossum62de97f1995-01-22 00:48:41 +0000447static int
Guido van Rossum778983b1993-02-19 15:55:02 +0000448addarrayitem(op, newitem)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000449 PyObject *op;
450 PyObject *newitem;
Guido van Rossum778983b1993-02-19 15:55:02 +0000451{
452 if (!is_arrayobject(op)) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000453 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000454 return -1;
455 }
456 return ins1((arrayobject *)op,
457 (int) ((arrayobject *)op)->ob_size, newitem);
458}
Guido van Rossuma376cc51996-12-05 23:43:35 +0000459#endif
Guido van Rossum778983b1993-02-19 15:55:02 +0000460
461/* Methods */
462
463static void
464array_dealloc(op)
465 arrayobject *op;
466{
Guido van Rossum778983b1993-02-19 15:55:02 +0000467 if (op->ob_item != NULL)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000468 PyMem_DEL(op->ob_item);
469 PyMem_DEL(op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000470}
471
472static int
473array_compare(v, w)
474 arrayobject *v, *w;
475{
476 int len = (v->ob_size < w->ob_size) ? v->ob_size : w->ob_size;
477 int i;
478 for (i = 0; i < len; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000479 PyObject *ai, *bi;
Guido van Rossum778983b1993-02-19 15:55:02 +0000480 int cmp;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000481 ai = getarrayitem((PyObject *)v, i);
482 bi = getarrayitem((PyObject *)w, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000483 if (ai && bi)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000484 cmp = PyObject_Compare(ai, bi);
Guido van Rossum778983b1993-02-19 15:55:02 +0000485 else
486 cmp = -1;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000487 Py_XDECREF(ai);
488 Py_XDECREF(bi);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000489 if (cmp != 0)
Guido van Rossum778983b1993-02-19 15:55:02 +0000490 return cmp;
Guido van Rossum778983b1993-02-19 15:55:02 +0000491 }
492 return v->ob_size - w->ob_size;
493}
494
495static int
496array_length(a)
497 arrayobject *a;
498{
499 return a->ob_size;
500}
501
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000502static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000503array_item(a, i)
504 arrayobject *a;
505 int i;
506{
507 if (i < 0 || i >= a->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000508 PyErr_SetString(PyExc_IndexError, "array index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000509 return NULL;
510 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000511 return getarrayitem((PyObject *)a, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000512}
513
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000514static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000515array_slice(a, ilow, ihigh)
516 arrayobject *a;
517 int ilow, ihigh;
518{
519 arrayobject *np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000520 if (ilow < 0)
521 ilow = 0;
522 else if (ilow > a->ob_size)
523 ilow = a->ob_size;
524 if (ihigh < 0)
525 ihigh = 0;
526 if (ihigh < ilow)
527 ihigh = ilow;
528 else if (ihigh > a->ob_size)
529 ihigh = a->ob_size;
530 np = (arrayobject *) newarrayobject(ihigh - ilow, a->ob_descr);
531 if (np == NULL)
532 return NULL;
533 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
534 (ihigh-ilow) * a->ob_descr->itemsize);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000535 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000536}
537
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000538static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000539array_concat(a, bb)
540 arrayobject *a;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000541 PyObject *bb;
Guido van Rossum778983b1993-02-19 15:55:02 +0000542{
543 int size;
Guido van Rossum778983b1993-02-19 15:55:02 +0000544 arrayobject *np;
545 if (!is_arrayobject(bb)) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000546 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000547 return NULL;
548 }
549#define b ((arrayobject *)bb)
550 if (a->ob_descr != b->ob_descr) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000551 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000552 return NULL;
553 }
554 size = a->ob_size + b->ob_size;
555 np = (arrayobject *) newarrayobject(size, a->ob_descr);
556 if (np == NULL) {
557 return NULL;
558 }
559 memcpy(np->ob_item, a->ob_item, a->ob_size*a->ob_descr->itemsize);
560 memcpy(np->ob_item + a->ob_size*a->ob_descr->itemsize,
Guido van Rossum32be3a71993-11-05 10:16:27 +0000561 b->ob_item, b->ob_size*b->ob_descr->itemsize);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000562 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000563#undef b
564}
565
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000566static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000567array_repeat(a, n)
568 arrayobject *a;
569 int n;
570{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000571 int i;
Guido van Rossum778983b1993-02-19 15:55:02 +0000572 int size;
573 arrayobject *np;
574 char *p;
575 int nbytes;
576 if (n < 0)
577 n = 0;
578 size = a->ob_size * n;
579 np = (arrayobject *) newarrayobject(size, a->ob_descr);
580 if (np == NULL)
581 return NULL;
582 p = np->ob_item;
583 nbytes = a->ob_size * a->ob_descr->itemsize;
584 for (i = 0; i < n; i++) {
585 memcpy(p, a->ob_item, nbytes);
586 p += nbytes;
587 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000588 return (PyObject *) np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000589}
590
591static int
592array_ass_slice(a, ilow, ihigh, v)
593 arrayobject *a;
594 int ilow, ihigh;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000595 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000596{
597 char *item;
598 int n; /* Size of replacement array */
599 int d; /* Change in size */
Guido van Rossum778983b1993-02-19 15:55:02 +0000600#define b ((arrayobject *)v)
601 if (v == NULL)
602 n = 0;
603 else if (is_arrayobject(v)) {
604 n = b->ob_size;
605 if (a == b) {
606 /* Special case "a[i:j] = a" -- copy b first */
607 int ret;
608 v = array_slice(b, 0, n);
609 ret = array_ass_slice(a, ilow, ihigh, v);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000610 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000611 return ret;
612 }
613 if (b->ob_descr != a->ob_descr) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000614 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000615 return -1;
616 }
617 }
618 else {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000619 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000620 return -1;
621 }
622 if (ilow < 0)
623 ilow = 0;
624 else if (ilow > a->ob_size)
625 ilow = a->ob_size;
626 if (ihigh < 0)
627 ihigh = 0;
628 if (ihigh < ilow)
629 ihigh = ilow;
630 else if (ihigh > a->ob_size)
631 ihigh = a->ob_size;
632 item = a->ob_item;
633 d = n - (ihigh-ilow);
634 if (d < 0) { /* Delete -d items */
635 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
636 item + ihigh*a->ob_descr->itemsize,
637 (a->ob_size-ihigh)*a->ob_descr->itemsize);
638 a->ob_size += d;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000639 PyMem_RESIZE(item, char, a->ob_size*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000640 /* Can't fail */
641 a->ob_item = item;
642 }
643 else if (d > 0) { /* Insert d items */
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000644 PyMem_RESIZE(item, char,
645 (a->ob_size + d)*a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000646 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000647 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000648 return -1;
649 }
650 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
651 item + ihigh*a->ob_descr->itemsize,
652 (a->ob_size-ihigh)*a->ob_descr->itemsize);
653 a->ob_item = item;
654 a->ob_size += d;
655 }
656 if (n > 0)
657 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
658 n*b->ob_descr->itemsize);
659 return 0;
660#undef b
661}
662
663static int
664array_ass_item(a, i, v)
665 arrayobject *a;
666 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000667 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000668{
669 if (i < 0 || i >= a->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000670 PyErr_SetString(PyExc_IndexError,
671 "array assignment index out of range");
Guido van Rossum778983b1993-02-19 15:55:02 +0000672 return -1;
673 }
674 if (v == NULL)
675 return array_ass_slice(a, i, i+1, v);
676 return (*a->ob_descr->setitem)(a, i, v);
677}
678
679static int
680setarrayitem(a, i, v)
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000681 PyObject *a;
Guido van Rossum778983b1993-02-19 15:55:02 +0000682 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000683 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000684{
685 if (!is_arrayobject(a)) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000686 PyErr_BadInternalCall();
Guido van Rossum778983b1993-02-19 15:55:02 +0000687 return -1;
688 }
689 return array_ass_item((arrayobject *)a, i, v);
690}
691
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000692static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000693ins(self, where, v)
694 arrayobject *self;
695 int where;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000696 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +0000697{
698 if (ins1(self, where, v) != 0)
699 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000700 Py_INCREF(Py_None);
701 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000702}
703
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000704static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000705array_insert(self, args)
706 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000707 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000708{
709 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000710 PyObject *v;
711 if (!PyArg_Parse(args, "(iO)", &i, &v))
Guido van Rossum778983b1993-02-19 15:55:02 +0000712 return NULL;
713 return ins(self, i, v);
714}
715
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000716static char insert_doc [] =
717"insert (i,x)\n\
718\n\
719Insert a new item x into the array before position i.";
720
721
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000722static PyObject *
Guido van Rossumde4a4ca1997-08-12 14:55:56 +0000723array_buffer_info(self, args)
724 arrayobject *self;
725 PyObject *args;
726{
727 return Py_BuildValue("ll",
728 (long)(self->ob_item), (long)(self->ob_size));
729}
730
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000731static char buffer_info_doc [] =
732"buffer_info -> (address, length)\n\
733\n\
734Return a tuple (address, length) giving the current memory address and\n\
735the length in bytes of the buffer used to hold array's contents.";
736
737
Guido van Rossumde4a4ca1997-08-12 14:55:56 +0000738static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000739array_append(self, args)
740 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000741 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000742{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000743 PyObject *v;
744 if (!PyArg_Parse(args, "O", &v))
Guido van Rossum778983b1993-02-19 15:55:02 +0000745 return NULL;
746 return ins(self, (int) self->ob_size, v);
747}
748
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000749static char append_doc [] =
750"append(x)\n\
751\n\
752Append new value x to the end of the array.";
753
754
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000755static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000756array_byteswap(self, args)
757 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000758 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000759{
760 char *p;
761 int i;
Fred Drakebf272981999-12-03 17:15:30 +0000762
763 if (!PyArg_ParseTuple(args, ":byteswap"))
764 return NULL;
765
Guido van Rossum778983b1993-02-19 15:55:02 +0000766 switch (self->ob_descr->itemsize) {
767 case 1:
768 break;
769 case 2:
770 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 2) {
771 char p0 = p[0];
772 p[0] = p[1];
773 p[1] = p0;
774 }
775 break;
776 case 4:
777 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 4) {
778 char p0 = p[0];
779 char p1 = p[1];
780 p[0] = p[3];
781 p[1] = p[2];
782 p[2] = p1;
783 p[3] = p0;
784 }
785 break;
Guido van Rossume77a7571993-11-03 15:01:26 +0000786 case 8:
787 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 8) {
788 char p0 = p[0];
789 char p1 = p[1];
790 char p2 = p[2];
791 char p3 = p[3];
792 p[0] = p[7];
793 p[1] = p[6];
794 p[2] = p[5];
795 p[3] = p[4];
796 p[4] = p3;
797 p[5] = p2;
798 p[6] = p1;
799 p[7] = p0;
800 }
801 break;
Guido van Rossum778983b1993-02-19 15:55:02 +0000802 default:
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000803 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossum778983b1993-02-19 15:55:02 +0000804 "don't know how to byteswap this array type");
805 return NULL;
806 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000807 Py_INCREF(Py_None);
808 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000809}
810
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000811static char byteswap_doc [] =
Fred Drakebf272981999-12-03 17:15:30 +0000812"byteswap()\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000813\n\
Fred Drakebf272981999-12-03 17:15:30 +0000814Byteswap all items of the array. If the items in the array are not 1, 2,\n\
8154, or 8 bytes in size, RuntimeError is raised.";
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000816
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000817static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000818array_reverse(self, args)
819 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000820 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000821{
Guido van Rossume77a7571993-11-03 15:01:26 +0000822 register int itemsize = self->ob_descr->itemsize;
823 register char *p, *q;
824 char tmp[sizeof(double)]; /* Assume that's the max item size */
825
Guido van Rossum778983b1993-02-19 15:55:02 +0000826 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
831 if (self->ob_size > 1) {
Guido van Rossume77a7571993-11-03 15:01:26 +0000832 for (p = self->ob_item,
833 q = self->ob_item + (self->ob_size - 1)*itemsize;
834 p < q;
835 p += itemsize, q -= itemsize) {
836 memmove(tmp, p, itemsize);
837 memmove(p, q, itemsize);
838 memmove(q, tmp, itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000839 }
840 }
841
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000842 Py_INCREF(Py_None);
843 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000844}
Guido van Rossume77a7571993-11-03 15:01:26 +0000845
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000846static char reverse_doc [] =
847"reverse()\n\
848\n\
849Reverse the order of the items in the array.";
850
Guido van Rossume77a7571993-11-03 15:01:26 +0000851/* The following routines were adapted from listobject.c but not converted.
852 To make them work you will have to work! */
Guido van Rossum778983b1993-02-19 15:55:02 +0000853
854#if 0
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000855static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000856array_index(self, args)
857 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000858 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000859{
860 int i;
861
862 if (args == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000863 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000864 return NULL;
865 }
866 for (i = 0; i < self->ob_size; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000867 if (PyObject_Compare(self->ob_item[i], args) == 0)
868 return PyInt_FromLong((long)i);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000869 /* XXX PyErr_Occurred */
Guido van Rossum778983b1993-02-19 15:55:02 +0000870 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000871 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in array");
Guido van Rossum778983b1993-02-19 15:55:02 +0000872 return NULL;
873}
874#endif
875
876#if 0
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000877static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000878array_count(self, args)
879 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000880 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000881{
882 int count = 0;
883 int i;
884
885 if (args == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000886 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000887 return NULL;
888 }
889 for (i = 0; i < self->ob_size; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000890 if (PyObject_Compare(self->ob_item[i], args) == 0)
Guido van Rossum778983b1993-02-19 15:55:02 +0000891 count++;
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000892 /* XXX PyErr_Occurred */
Guido van Rossum778983b1993-02-19 15:55:02 +0000893 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000894 return PyInt_FromLong((long)count);
Guido van Rossum778983b1993-02-19 15:55:02 +0000895}
896#endif
897
898#if 0
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000899static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +0000900array_remove(self, args)
901 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000902 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000903{
904 int i;
905
906 if (args == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000907 PyErr_BadArgument();
Guido van Rossum778983b1993-02-19 15:55:02 +0000908 return NULL;
909 }
910 for (i = 0; i < self->ob_size; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000911 if (PyObject_Compare(self->ob_item[i], args) == 0) {
Roger E. Masse5817f8f1996-12-09 22:24:19 +0000912 if (array_ass_slice(self, i, i+1,
913 (PyObject *)NULL) != 0)
Guido van Rossum778983b1993-02-19 15:55:02 +0000914 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000915 Py_INCREF(Py_None);
916 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000917 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000918 /* XXX PyErr_Occurred */
Guido van Rossum778983b1993-02-19 15:55:02 +0000919 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000920 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in array");
Guido van Rossum778983b1993-02-19 15:55:02 +0000921 return NULL;
922}
923#endif
924
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000925static PyObject *
Guido van Rossume77a7571993-11-03 15:01:26 +0000926array_fromfile(self, args)
Guido van Rossum778983b1993-02-19 15:55:02 +0000927 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000928 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000929{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000930 PyObject *f;
Guido van Rossum778983b1993-02-19 15:55:02 +0000931 int n;
932 FILE *fp;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000933 if (!PyArg_Parse(args, "(Oi)", &f, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +0000934 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000935 fp = PyFile_AsFile(f);
Guido van Rossum778983b1993-02-19 15:55:02 +0000936 if (fp == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000937 PyErr_SetString(PyExc_TypeError, "arg1 must be open file");
Guido van Rossum778983b1993-02-19 15:55:02 +0000938 return NULL;
939 }
940 if (n > 0) {
941 char *item = self->ob_item;
942 int itemsize = self->ob_descr->itemsize;
943 int nread;
Guido van Rossum3791b0d1999-02-23 18:05:22 +0000944 int newlength;
945 size_t newbytes;
946 /* Be careful here about overflow */
947 if ((newlength = self->ob_size + n) <= 0 ||
Guido van Rossum481ac881999-03-19 21:50:11 +0000948 (newbytes = newlength * itemsize) / itemsize !=
949 (size_t)newlength)
Guido van Rossum3791b0d1999-02-23 18:05:22 +0000950 goto nomem;
951 PyMem_RESIZE(item, char, newbytes);
Guido van Rossum778983b1993-02-19 15:55:02 +0000952 if (item == NULL) {
Guido van Rossum3791b0d1999-02-23 18:05:22 +0000953 nomem:
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000954 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +0000955 return NULL;
956 }
957 self->ob_item = item;
958 self->ob_size += n;
959 nread = fread(item + (self->ob_size - n) * itemsize,
960 itemsize, n, fp);
961 if (nread < n) {
962 self->ob_size -= (n - nread);
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000963 PyMem_RESIZE(item, char, self->ob_size*itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +0000964 self->ob_item = item;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000965 PyErr_SetString(PyExc_EOFError,
966 "not enough items in file");
Guido van Rossum778983b1993-02-19 15:55:02 +0000967 return NULL;
968 }
969 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000970 Py_INCREF(Py_None);
971 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +0000972}
973
Guido van Rossumb39b90d1998-10-13 14:27:22 +0000974static char fromfile_doc [] =
975"fromfile(f, n)\n\
976\n\
977Read n objects from the file object f and append them to the end of the\n\
978array. Also called as read.";
979
980
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000981static PyObject *
Guido van Rossume77a7571993-11-03 15:01:26 +0000982array_tofile(self, args)
Guido van Rossum778983b1993-02-19 15:55:02 +0000983 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000984 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +0000985{
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000986 PyObject *f;
Guido van Rossum778983b1993-02-19 15:55:02 +0000987 FILE *fp;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000988 if (!PyArg_Parse(args, "O", &f))
Guido van Rossum778983b1993-02-19 15:55:02 +0000989 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000990 fp = PyFile_AsFile(f);
Guido van Rossum778983b1993-02-19 15:55:02 +0000991 if (fp == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000992 PyErr_SetString(PyExc_TypeError, "arg must be open file");
Guido van Rossum778983b1993-02-19 15:55:02 +0000993 return NULL;
994 }
995 if (self->ob_size > 0) {
Guido van Rossum7844e381997-04-11 20:44:04 +0000996 if ((int)fwrite(self->ob_item, self->ob_descr->itemsize,
Guido van Rossum778983b1993-02-19 15:55:02 +0000997 self->ob_size, fp) != self->ob_size) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000998 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum778983b1993-02-19 15:55:02 +0000999 clearerr(fp);
1000 return NULL;
1001 }
1002 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001003 Py_INCREF(Py_None);
1004 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001005}
1006
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001007static char tofile_doc [] =
1008"tofile(f)\n\
1009\n\
1010Write all items (as machine values) to the file object f. Also called as\n\
1011write.";
1012
1013
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001014static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001015array_fromlist(self, args)
1016 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001017 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +00001018{
1019 int n;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001020 PyObject *list;
Guido van Rossum778983b1993-02-19 15:55:02 +00001021 int itemsize = self->ob_descr->itemsize;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001022 if (!PyArg_Parse(args, "O", &list))
Guido van Rossum778983b1993-02-19 15:55:02 +00001023 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001024 if (!PyList_Check(list)) {
1025 PyErr_SetString(PyExc_TypeError, "arg must be list");
Guido van Rossum778983b1993-02-19 15:55:02 +00001026 return NULL;
1027 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001028 n = PyList_Size(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001029 if (n > 0) {
1030 char *item = self->ob_item;
1031 int i;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001032 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001033 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001034 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001035 return NULL;
1036 }
1037 self->ob_item = item;
1038 self->ob_size += n;
1039 for (i = 0; i < n; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001040 PyObject *v = PyList_GetItem(list, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001041 if ((*self->ob_descr->setitem)(self,
1042 self->ob_size - n + i, v) != 0) {
1043 self->ob_size -= n;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001044 PyMem_RESIZE(item, char,
1045 self->ob_size * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001046 self->ob_item = item;
1047 return NULL;
1048 }
1049 }
1050 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001051 Py_INCREF(Py_None);
1052 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001053}
1054
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001055static char fromlist_doc [] =
1056"fromlist(list)\n\
1057\n\
1058Append items to array from list.";
1059
1060
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001061static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001062array_tolist(self, args)
1063 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001064 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +00001065{
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001066 PyObject *list = PyList_New(self->ob_size);
Guido van Rossum778983b1993-02-19 15:55:02 +00001067 int i;
1068 if (list == NULL)
1069 return NULL;
1070 for (i = 0; i < self->ob_size; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001071 PyObject *v = getarrayitem((PyObject *)self, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001072 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001073 Py_DECREF(list);
Guido van Rossum778983b1993-02-19 15:55:02 +00001074 return NULL;
1075 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001076 PyList_SetItem(list, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001077 }
1078 return list;
1079}
1080
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001081static char tolist_doc [] =
Guido van Rossumfc6aba51998-10-14 02:52:31 +00001082"tolist() -> list\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001083\n\
1084Convert array to an ordinary list with the same items.";
1085
1086
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001087static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001088array_fromstring(self, args)
1089 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001090 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +00001091{
1092 char *str;
1093 int n;
1094 int itemsize = self->ob_descr->itemsize;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001095 if (!PyArg_Parse(args, "s#", &str, &n))
Guido van Rossum778983b1993-02-19 15:55:02 +00001096 return NULL;
1097 if (n % itemsize != 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001098 PyErr_SetString(PyExc_ValueError,
Guido van Rossum778983b1993-02-19 15:55:02 +00001099 "string length not a multiple of item size");
1100 return NULL;
1101 }
1102 n = n / itemsize;
1103 if (n > 0) {
1104 char *item = self->ob_item;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001105 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001106 if (item == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001107 PyErr_NoMemory();
Guido van Rossum778983b1993-02-19 15:55:02 +00001108 return NULL;
1109 }
1110 self->ob_item = item;
1111 self->ob_size += n;
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001112 memcpy(item + (self->ob_size - n) * itemsize,
1113 str, itemsize*n);
Guido van Rossum778983b1993-02-19 15:55:02 +00001114 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001115 Py_INCREF(Py_None);
1116 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001117}
1118
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001119static char fromstring_doc [] =
1120"fromstring(string)\n\
1121\n\
1122Appends items from the string, interpreting it as an array of machine\n\
1123values,as if it had been read from a file using the fromfile() method).";
1124
1125
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001126static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001127array_tostring(self, args)
1128 arrayobject *self;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001129 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +00001130{
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001131 if (!PyArg_Parse(args, ""))
Guido van Rossum778983b1993-02-19 15:55:02 +00001132 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001133 return PyString_FromStringAndSize(self->ob_item,
Guido van Rossum778983b1993-02-19 15:55:02 +00001134 self->ob_size * self->ob_descr->itemsize);
1135}
1136
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001137static char tostring_doc [] =
1138"tostring() -> string\n\
1139\n\
1140Convert the array to an array of machine values and return the string\n\
1141representation.";
1142
1143PyMethodDef array_methods[] = {
1144 {"append", (PyCFunction)array_append, 0, append_doc},
1145 {"buffer_info", (PyCFunction)array_buffer_info, 0, buffer_info_doc},
Fred Drakebf272981999-12-03 17:15:30 +00001146 {"byteswap", (PyCFunction)array_byteswap, METH_VARARGS,
1147 byteswap_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001148/* {"count", (method)array_count},*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001149 {"fromfile", (PyCFunction)array_fromfile, 0, fromfile_doc},
1150 {"fromlist", (PyCFunction)array_fromlist, 0, fromlist_doc},
1151 {"fromstring", (PyCFunction)array_fromstring, 0, fromstring_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001152/* {"index", (method)array_index},*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001153 {"insert", (PyCFunction)array_insert, 0, insert_doc},
1154 {"read", (PyCFunction)array_fromfile, 0, fromfile_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001155/* {"remove", (method)array_remove},*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001156 {"reverse", (PyCFunction)array_reverse, 0, reverse_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001157/* {"sort", (method)array_sort},*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001158 {"tofile", (PyCFunction)array_tofile, 0, tofile_doc},
1159 {"tolist", (PyCFunction)array_tolist, 0, tolist_doc},
1160 {"tostring", (PyCFunction)array_tostring, 0, tostring_doc},
1161 {"write", (PyCFunction)array_tofile, 0, tofile_doc},
Guido van Rossum778983b1993-02-19 15:55:02 +00001162 {NULL, NULL} /* sentinel */
1163};
1164
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001165static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001166array_getattr(a, name)
1167 arrayobject *a;
1168 char *name;
1169{
1170 if (strcmp(name, "typecode") == 0) {
1171 char tc = a->ob_descr->typecode;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001172 return PyString_FromStringAndSize(&tc, 1);
Guido van Rossum778983b1993-02-19 15:55:02 +00001173 }
1174 if (strcmp(name, "itemsize") == 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001175 return PyInt_FromLong((long)a->ob_descr->itemsize);
Guido van Rossum778983b1993-02-19 15:55:02 +00001176 }
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001177 if (strcmp(name, "__members__") == 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001178 PyObject *list = PyList_New(2);
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001179 if (list) {
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001180 PyList_SetItem(list, 0,
1181 PyString_FromString("typecode"));
1182 PyList_SetItem(list, 1,
1183 PyString_FromString("itemsize"));
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001184 if (PyErr_Occurred()) {
1185 Py_DECREF(list);
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001186 list = NULL;
1187 }
1188 }
1189 return list;
1190 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001191 return Py_FindMethod(array_methods, (PyObject *)a, name);
Guido van Rossum778983b1993-02-19 15:55:02 +00001192}
1193
1194static int
1195array_print(a, fp, flags)
1196 arrayobject *a;
1197 FILE *fp;
1198 int flags;
1199{
1200 int ok = 0;
1201 int i, len;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001202 PyObject *v;
Guido van Rossum778983b1993-02-19 15:55:02 +00001203 len = a->ob_size;
1204 if (len == 0) {
1205 fprintf(fp, "array('%c')", a->ob_descr->typecode);
1206 return ok;
1207 }
1208 if (a->ob_descr->typecode == 'c') {
1209 fprintf(fp, "array('c', ");
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001210 v = array_tostring(a, (PyObject *)NULL);
1211 ok = PyObject_Print(v, fp, 0);
1212 Py_XDECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001213 fprintf(fp, ")");
1214 return ok;
1215 }
1216 fprintf(fp, "array('%c', [", a->ob_descr->typecode);
1217 for (i = 0; i < len && ok == 0; i++) {
1218 if (i > 0)
1219 fprintf(fp, ", ");
1220 v = (a->ob_descr->getitem)(a, i);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001221 ok = PyObject_Print(v, fp, 0);
1222 Py_XDECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001223 }
1224 fprintf(fp, "])");
1225 return ok;
1226}
1227
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001228static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001229array_repr(a)
1230 arrayobject *a;
1231{
1232 char buf[256];
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001233 PyObject *s, *t, *comma, *v;
Guido van Rossum778983b1993-02-19 15:55:02 +00001234 int i, len;
1235 len = a->ob_size;
1236 if (len == 0) {
1237 sprintf(buf, "array('%c')", a->ob_descr->typecode);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001238 return PyString_FromString(buf);
Guido van Rossum778983b1993-02-19 15:55:02 +00001239 }
1240 if (a->ob_descr->typecode == 'c') {
1241 sprintf(buf, "array('c', ");
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001242 s = PyString_FromString(buf);
1243 v = array_tostring(a, (PyObject *)NULL);
1244 t = PyObject_Repr(v);
1245 Py_XDECREF(v);
1246 PyString_ConcatAndDel(&s, t);
1247 PyString_ConcatAndDel(&s, PyString_FromString(")"));
Guido van Rossum778983b1993-02-19 15:55:02 +00001248 return s;
1249 }
1250 sprintf(buf, "array('%c', [", a->ob_descr->typecode);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001251 s = PyString_FromString(buf);
1252 comma = PyString_FromString(", ");
1253 for (i = 0; i < len && !PyErr_Occurred(); i++) {
Guido van Rossumb6775db1994-08-01 11:34:53 +00001254 if (i > 0)
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001255 PyString_Concat(&s, comma);
Guido van Rossum778983b1993-02-19 15:55:02 +00001256 v = (a->ob_descr->getitem)(a, i);
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001257 t = PyObject_Repr(v);
1258 Py_XDECREF(v);
1259 PyString_ConcatAndDel(&s, t);
Guido van Rossum778983b1993-02-19 15:55:02 +00001260 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001261 Py_XDECREF(comma);
1262 PyString_ConcatAndDel(&s, PyString_FromString("])"));
Guido van Rossum778983b1993-02-19 15:55:02 +00001263 return s;
1264}
1265
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001266static int
1267array_buffer_getreadbuf(self, index, ptr)
1268 arrayobject *self;
1269 int index;
1270 const void **ptr;
1271{
1272 if ( index != 0 ) {
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001273 PyErr_SetString(PyExc_SystemError,
1274 "Accessing non-existent array segment");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001275 return -1;
1276 }
1277 *ptr = (void *)self->ob_item;
1278 return self->ob_size*self->ob_descr->itemsize;
1279}
1280
1281static int
1282array_buffer_getwritebuf(self, index, ptr)
1283 arrayobject *self;
1284 int index;
1285 const void **ptr;
1286{
1287 if ( index != 0 ) {
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001288 PyErr_SetString(PyExc_SystemError,
1289 "Accessing non-existent array segment");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001290 return -1;
1291 }
1292 *ptr = (void *)self->ob_item;
1293 return self->ob_size*self->ob_descr->itemsize;
1294}
1295
1296static int
1297array_buffer_getsegcount(self, lenp)
1298 arrayobject *self;
1299 int *lenp;
1300{
1301 if ( lenp )
1302 *lenp = self->ob_size*self->ob_descr->itemsize;
1303 return 1;
1304}
1305
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001306static PySequenceMethods array_as_sequence = {
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001307 (inquiry)array_length, /*sq_length*/
1308 (binaryfunc)array_concat, /*sq_concat*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001309 (intargfunc)array_repeat, /*sq_repeat*/
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001310 (intargfunc)array_item, /*sq_item*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001311 (intintargfunc)array_slice, /*sq_slice*/
1312 (intobjargproc)array_ass_item, /*sq_ass_item*/
1313 (intintobjargproc)array_ass_slice, /*sq_ass_slice*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001314};
1315
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001316static PyBufferProcs array_as_buffer = {
1317 (getreadbufferproc)array_buffer_getreadbuf,
1318 (getwritebufferproc)array_buffer_getwritebuf,
1319 (getsegcountproc)array_buffer_getsegcount,
1320};
1321
1322
Guido van Rossum778983b1993-02-19 15:55:02 +00001323
1324
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001325static PyObject *
Guido van Rossum778983b1993-02-19 15:55:02 +00001326a_array(self, args)
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001327 PyObject *self;
1328 PyObject *args;
Guido van Rossum778983b1993-02-19 15:55:02 +00001329{
1330 char c;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001331 PyObject *initial = NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001332 struct arraydescr *descr;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001333 if (!PyArg_Parse(args, "c", &c)) {
1334 PyErr_Clear();
1335 if (!PyArg_Parse(args, "(cO)", &c, &initial))
Guido van Rossum778983b1993-02-19 15:55:02 +00001336 return NULL;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001337 if (!PyList_Check(initial) && !PyString_Check(initial)) {
1338 PyErr_SetString(PyExc_TypeError,
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001339 "array initializer must be list or string");
Guido van Rossum778983b1993-02-19 15:55:02 +00001340 return NULL;
1341 }
1342 }
1343 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1344 if (descr->typecode == c) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001345 PyObject *a;
Guido van Rossum778983b1993-02-19 15:55:02 +00001346 int len;
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001347 if (initial == NULL || !PyList_Check(initial))
Guido van Rossum778983b1993-02-19 15:55:02 +00001348 len = 0;
1349 else
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001350 len = PyList_Size(initial);
Guido van Rossum778983b1993-02-19 15:55:02 +00001351 a = newarrayobject(len, descr);
1352 if (a == NULL)
1353 return NULL;
1354 if (len > 0) {
1355 int i;
1356 for (i = 0; i < len; i++) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001357 PyObject *v =
1358 PyList_GetItem(initial, i);
Guido van Rossum778983b1993-02-19 15:55:02 +00001359 if (setarrayitem(a, i, v) != 0) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001360 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001361 return NULL;
1362 }
1363 }
1364 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001365 if (initial != NULL && PyString_Check(initial)) {
1366 PyObject *v =
Guido van Rossum778983b1993-02-19 15:55:02 +00001367 array_fromstring((arrayobject *)a, initial);
1368 if (v == NULL) {
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001369 Py_DECREF(a);
Guido van Rossum778983b1993-02-19 15:55:02 +00001370 return NULL;
1371 }
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001372 Py_DECREF(v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001373 }
1374 return a;
1375 }
1376 }
Roger E. Masse5817f8f1996-12-09 22:24:19 +00001377 PyErr_SetString(PyExc_ValueError,
Guido van Rossum549ab711997-01-03 19:09:47 +00001378 "bad typecode (must be c, b, B, h, H, i, I, l, L, f or d)");
Guido van Rossum778983b1993-02-19 15:55:02 +00001379 return NULL;
1380}
1381
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001382static char a_array_doc [] =
1383"array(typecode [, initializer]) -> array\n\
1384\n\
1385Return a new array whose items are restricted by typecode, and\n\
1386initialized from the optional initializer value, which must be a list\n\
1387or a string.";
1388
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001389static PyMethodDef a_methods[] = {
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001390 {"array", a_array, 0, a_array_doc},
Guido van Rossum778983b1993-02-19 15:55:02 +00001391 {NULL, NULL} /* sentinel */
1392};
1393
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001394static char module_doc [] =
1395"This module defines a new object type which can efficiently represent\n\
1396an array of basic values: characters, integers, floating point\n\
1397numbers. Arrays are sequence types and behave very much like lists,\n\
1398except that the type of objects stored in them is constrained. The\n\
1399type is specified at object creation time by using a type code, which\n\
1400is a single character. The following type codes are defined:\n\
1401\n\
1402 Type code C Type Minimum size in bytes \n\
1403 'c' character 1 \n\
1404 'b' signed integer 1 \n\
1405 'B' unsigned integer 1 \n\
1406 'h' signed integer 2 \n\
1407 'H' unsigned integer 2 \n\
1408 'i' signed integer 2 \n\
1409 'I' unsigned integer 2 \n\
1410 'l' signed integer 4 \n\
1411 'L' unsigned integer 4 \n\
1412 'f' floating point 4 \n\
1413 'd' floating point 8 \n\
1414\n\
1415Functions:\n\
1416\n\
1417array(typecode [, initializer]) -- create a new array\n\
1418\n\
1419Special Objects:\n\
1420\n\
1421ArrayType -- type object for array objects\n\
1422";
1423
1424static char arraytype_doc [] =
1425"An array represents basic values and behave very much like lists, except\n\
1426the type of objects stored in them is constrained.\n\
1427\n\
1428Methods:\n\
1429\n\
1430append() -- append a new item to the end of the array\n\
1431buffer_info() -- return information giving the current memory info\n\
1432byteswap() -- byteswap all the items of the array\n\
1433fromfile() -- read items from a file object\n\
1434fromlist() -- append items from the list\n\
1435fromstring() -- append items from the string\n\
1436insert() -- insert a new item into the array at a provided position\n\
1437read() -- DEPRECATED, use fromfile()\n\
1438reverse() -- reverse the order of the items in the array\n\
1439tofile() -- write all items to a file object\n\
1440tolist() -- return the array converted to an ordinary list\n\
1441tostring() -- return the array converted to a string\n\
1442write() -- DEPRECATED, use tofile()\n\
1443\n\
1444Variables:\n\
1445\n\
1446typecode -- the typecode character used to create the array\n\
1447itemsize -- the length in bytes of one array item\n\
1448";
1449
1450statichere PyTypeObject Arraytype = {
Fred Drake0d40ba42000-02-04 20:33:49 +00001451 PyObject_HEAD_INIT(NULL)
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001452 0,
1453 "array",
1454 sizeof(arrayobject),
1455 0,
1456 (destructor)array_dealloc, /*tp_dealloc*/
1457 (printfunc)array_print, /*tp_print*/
1458 (getattrfunc)array_getattr, /*tp_getattr*/
1459 0, /*tp_setattr*/
1460 (cmpfunc)array_compare, /*tp_compare*/
1461 (reprfunc)array_repr, /*tp_repr*/
1462 0, /*tp_as_number*/
1463 &array_as_sequence, /*tp_as_sequence*/
1464 0, /*tp_as_mapping*/
1465 0, /*tp_hash*/
1466 0, /*tp_call*/
1467 0, /*tp_str*/
1468 0, /*tp_getattro*/
1469 0, /*tp_setattro*/
1470 &array_as_buffer, /*tp_as_buffer*/
1471 0, /*tp_xxx4*/
1472 arraytype_doc, /*tp_doc*/
1473};
1474
Guido van Rossum3886bb61998-12-04 18:50:17 +00001475DL_EXPORT(void)
Guido van Rossum778983b1993-02-19 15:55:02 +00001476initarray()
1477{
Guido van Rossumb6190d31997-05-22 14:56:36 +00001478 PyObject *m, *d;
Fred Drake0d40ba42000-02-04 20:33:49 +00001479
1480 Arraytype.ob_type = &PyType_Type;
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001481 m = Py_InitModule3("array", a_methods, module_doc);
Guido van Rossumb6190d31997-05-22 14:56:36 +00001482 d = PyModule_GetDict(m);
Guido van Rossuma0deb641998-10-14 13:45:06 +00001483 PyDict_SetItemString(d, "ArrayType", (PyObject *)&Arraytype);
1484 /* No need to check the error here, the caller will do that */
Guido van Rossum778983b1993-02-19 15:55:02 +00001485}