This commit was manufactured by cvs2svn to create tag 'r22a4-fork'.
[python/dscho.git] / Modules / arraymodule.c
blob6168bb08c61cb2158460a4cb8fcc78f11aef9fa3
1 /* Array object implementation */
3 /* An array is a uniform list -- all items have the same type.
4 The item type is restricted to simple C types like int or float */
6 #include "Python.h"
8 #ifdef STDC_HEADERS
9 #include <stddef.h>
10 #else /* !STDC_HEADERS */
11 #ifndef DONT_HAVE_SYS_TYPES_H
12 #include <sys/types.h> /* For size_t */
13 #endif /* DONT_HAVE_SYS_TYPES_H */
14 #endif /* !STDC_HEADERS */
16 struct arrayobject; /* Forward */
18 /* All possible arraydescr values are defined in the vector "descriptors"
19 * below. That's defined later because the appropriate get and set
20 * functions aren't visible yet.
22 struct arraydescr {
23 int typecode;
24 int itemsize;
25 PyObject * (*getitem)(struct arrayobject *, int);
26 int (*setitem)(struct arrayobject *, int, PyObject *);
29 typedef struct arrayobject {
30 PyObject_VAR_HEAD
31 char *ob_item;
32 struct arraydescr *ob_descr;
33 } arrayobject;
35 staticforward PyTypeObject Arraytype;
37 #define is_arrayobject(op) ((op)->ob_type == &Arraytype)
39 /****************************************************************************
40 Get and Set functions for each type.
41 A Get function takes an arrayobject* and an integer index, returning the
42 array value at that index wrapped in an appropriate PyObject*.
43 A Set function takes an arrayobject, integer index, and PyObject*; sets
44 the array value at that index to the raw C data extracted from the PyObject*,
45 and returns 0 if successful, else nonzero on failure (PyObject* not of an
46 appropriate type or value).
47 Note that the basic Get and Set functions do NOT check that the index is
48 in bounds; that's the responsibility of the caller.
49 ****************************************************************************/
51 static PyObject *
52 c_getitem(arrayobject *ap, int i)
54 return PyString_FromStringAndSize(&((char *)ap->ob_item)[i], 1);
57 static int
58 c_setitem(arrayobject *ap, int i, PyObject *v)
60 char x;
61 if (!PyArg_Parse(v, "c;array item must be char", &x))
62 return -1;
63 if (i >= 0)
64 ((char *)ap->ob_item)[i] = x;
65 return 0;
68 static PyObject *
69 b_getitem(arrayobject *ap, int i)
71 long x = ((char *)ap->ob_item)[i];
72 if (x >= 128)
73 x -= 256;
74 return PyInt_FromLong(x);
77 static int
78 b_setitem(arrayobject *ap, int i, PyObject *v)
80 short x;
81 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
82 must use the next size up that is signed ('h') and manually do
83 the overflow checking */
84 if (!PyArg_Parse(v, "h;array item must be integer", &x))
85 return -1;
86 else if (x < -128) {
87 PyErr_SetString(PyExc_OverflowError,
88 "signed char is less than minimum");
89 return -1;
91 else if (x > 127) {
92 PyErr_SetString(PyExc_OverflowError,
93 "signed char is greater than maximum");
94 return -1;
96 if (i >= 0)
97 ((char *)ap->ob_item)[i] = (char)x;
98 return 0;
101 static PyObject *
102 BB_getitem(arrayobject *ap, int i)
104 long x = ((unsigned char *)ap->ob_item)[i];
105 return PyInt_FromLong(x);
108 static int
109 BB_setitem(arrayobject *ap, int i, PyObject *v)
111 unsigned char x;
112 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
113 if (!PyArg_Parse(v, "b;array item must be integer", &x))
114 return -1;
115 if (i >= 0)
116 ((char *)ap->ob_item)[i] = x;
117 return 0;
120 static PyObject *
121 h_getitem(arrayobject *ap, int i)
123 return PyInt_FromLong((long) ((short *)ap->ob_item)[i]);
126 static int
127 h_setitem(arrayobject *ap, int i, PyObject *v)
129 short x;
130 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
131 if (!PyArg_Parse(v, "h;array item must be integer", &x))
132 return -1;
133 if (i >= 0)
134 ((short *)ap->ob_item)[i] = x;
135 return 0;
138 static PyObject *
139 HH_getitem(arrayobject *ap, int i)
141 return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
144 static int
145 HH_setitem(arrayobject *ap, int i, PyObject *v)
147 int x;
148 /* PyArg_Parse's 'h' formatter is for a signed short, therefore
149 must use the next size up and manually do the overflow checking */
150 if (!PyArg_Parse(v, "i;array item must be integer", &x))
151 return -1;
152 else if (x < 0) {
153 PyErr_SetString(PyExc_OverflowError,
154 "unsigned short is less than minimum");
155 return -1;
157 else if (x > USHRT_MAX) {
158 PyErr_SetString(PyExc_OverflowError,
159 "unsigned short is greater than maximum");
160 return -1;
162 if (i >= 0)
163 ((short *)ap->ob_item)[i] = (short)x;
164 return 0;
167 static PyObject *
168 i_getitem(arrayobject *ap, int i)
170 return PyInt_FromLong((long) ((int *)ap->ob_item)[i]);
173 static int
174 i_setitem(arrayobject *ap, int i, PyObject *v)
176 int x;
177 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
178 if (!PyArg_Parse(v, "i;array item must be integer", &x))
179 return -1;
180 if (i >= 0)
181 ((int *)ap->ob_item)[i] = x;
182 return 0;
185 static PyObject *
186 II_getitem(arrayobject *ap, int i)
188 return PyLong_FromUnsignedLong(
189 (unsigned long) ((unsigned int *)ap->ob_item)[i]);
192 static int
193 II_setitem(arrayobject *ap, int i, PyObject *v)
195 unsigned long x;
196 if (PyLong_Check(v)) {
197 x = PyLong_AsUnsignedLong(v);
198 if (x == (unsigned long) -1 && PyErr_Occurred())
199 return -1;
201 else {
202 long y;
203 if (!PyArg_Parse(v, "l;array item must be integer", &y))
204 return -1;
205 if (y < 0) {
206 PyErr_SetString(PyExc_OverflowError,
207 "unsigned int is less than minimum");
208 return -1;
210 x = (unsigned long)y;
213 if (x > UINT_MAX) {
214 PyErr_SetString(PyExc_OverflowError,
215 "unsigned int is greater than maximum");
216 return -1;
219 if (i >= 0)
220 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
221 return 0;
224 static PyObject *
225 l_getitem(arrayobject *ap, int i)
227 return PyInt_FromLong(((long *)ap->ob_item)[i]);
230 static int
231 l_setitem(arrayobject *ap, int i, PyObject *v)
233 long x;
234 if (!PyArg_Parse(v, "l;array item must be integer", &x))
235 return -1;
236 if (i >= 0)
237 ((long *)ap->ob_item)[i] = x;
238 return 0;
241 static PyObject *
242 LL_getitem(arrayobject *ap, int i)
244 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
247 static int
248 LL_setitem(arrayobject *ap, int i, PyObject *v)
250 unsigned long x;
251 if (PyLong_Check(v)) {
252 x = PyLong_AsUnsignedLong(v);
253 if (x == (unsigned long) -1 && PyErr_Occurred())
254 return -1;
256 else {
257 long y;
258 if (!PyArg_Parse(v, "l;array item must be integer", &y))
259 return -1;
260 if (y < 0) {
261 PyErr_SetString(PyExc_OverflowError,
262 "unsigned long is less than minimum");
263 return -1;
265 x = (unsigned long)y;
268 if (x > ULONG_MAX) {
269 PyErr_SetString(PyExc_OverflowError,
270 "unsigned long is greater than maximum");
271 return -1;
274 if (i >= 0)
275 ((unsigned long *)ap->ob_item)[i] = x;
276 return 0;
279 static PyObject *
280 f_getitem(arrayobject *ap, int i)
282 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
285 static int
286 f_setitem(arrayobject *ap, int i, PyObject *v)
288 float x;
289 if (!PyArg_Parse(v, "f;array item must be float", &x))
290 return -1;
291 if (i >= 0)
292 ((float *)ap->ob_item)[i] = x;
293 return 0;
296 static PyObject *
297 d_getitem(arrayobject *ap, int i)
299 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
302 static int
303 d_setitem(arrayobject *ap, int i, PyObject *v)
305 double x;
306 if (!PyArg_Parse(v, "d;array item must be float", &x))
307 return -1;
308 if (i >= 0)
309 ((double *)ap->ob_item)[i] = x;
310 return 0;
313 /* Description of types */
314 static struct arraydescr descriptors[] = {
315 {'c', sizeof(char), c_getitem, c_setitem},
316 {'b', sizeof(char), b_getitem, b_setitem},
317 {'B', sizeof(char), BB_getitem, BB_setitem},
318 {'h', sizeof(short), h_getitem, h_setitem},
319 {'H', sizeof(short), HH_getitem, HH_setitem},
320 {'i', sizeof(int), i_getitem, i_setitem},
321 {'I', sizeof(int), II_getitem, II_setitem},
322 {'l', sizeof(long), l_getitem, l_setitem},
323 {'L', sizeof(long), LL_getitem, LL_setitem},
324 {'f', sizeof(float), f_getitem, f_setitem},
325 {'d', sizeof(double), d_getitem, d_setitem},
326 {'\0', 0, 0, 0} /* Sentinel */
329 /****************************************************************************
330 Implementations of array object methods.
331 ****************************************************************************/
333 static PyObject *
334 newarrayobject(int size, struct arraydescr *descr)
336 arrayobject *op;
337 size_t nbytes;
338 if (size < 0) {
339 PyErr_BadInternalCall();
340 return NULL;
342 nbytes = size * descr->itemsize;
343 /* Check for overflow */
344 if (nbytes / descr->itemsize != (size_t)size) {
345 return PyErr_NoMemory();
347 op = PyObject_NewVar(arrayobject, &Arraytype, size);
348 if (op == NULL) {
349 return PyErr_NoMemory();
351 if (size <= 0) {
352 op->ob_item = NULL;
354 else {
355 op->ob_item = PyMem_NEW(char, nbytes);
356 if (op->ob_item == NULL) {
357 PyObject_Del(op);
358 return PyErr_NoMemory();
361 op->ob_descr = descr;
362 return (PyObject *) op;
365 static PyObject *
366 getarrayitem(PyObject *op, int i)
368 register arrayobject *ap;
369 assert(is_arrayobject(op));
370 ap = (arrayobject *)op;
371 if (i < 0 || i >= ap->ob_size) {
372 PyErr_SetString(PyExc_IndexError, "array index out of range");
373 return NULL;
375 return (*ap->ob_descr->getitem)(ap, i);
378 static int
379 ins1(arrayobject *self, int where, PyObject *v)
381 char *items;
382 if (v == NULL) {
383 PyErr_BadInternalCall();
384 return -1;
386 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
387 return -1;
388 items = self->ob_item;
389 PyMem_RESIZE(items, char,
390 (self->ob_size+1) * self->ob_descr->itemsize);
391 if (items == NULL) {
392 PyErr_NoMemory();
393 return -1;
395 if (where < 0)
396 where = 0;
397 if (where > self->ob_size)
398 where = self->ob_size;
399 memmove(items + (where+1)*self->ob_descr->itemsize,
400 items + where*self->ob_descr->itemsize,
401 (self->ob_size-where)*self->ob_descr->itemsize);
402 self->ob_item = items;
403 self->ob_size++;
404 return (*self->ob_descr->setitem)(self, where, v);
407 /* Methods */
409 static void
410 array_dealloc(arrayobject *op)
412 if (op->ob_item != NULL)
413 PyMem_DEL(op->ob_item);
414 PyObject_Del(op);
417 static PyObject *
418 array_richcompare(PyObject *v, PyObject *w, int op)
420 arrayobject *va, *wa;
421 PyObject *vi = NULL;
422 PyObject *wi = NULL;
423 int i, k;
424 PyObject *res;
426 if (!is_arrayobject(v) || !is_arrayobject(w)) {
427 Py_INCREF(Py_NotImplemented);
428 return Py_NotImplemented;
431 va = (arrayobject *)v;
432 wa = (arrayobject *)w;
434 if (va->ob_size != wa->ob_size && (op == Py_EQ || op == Py_NE)) {
435 /* Shortcut: if the lengths differ, the arrays differ */
436 if (op == Py_EQ)
437 res = Py_False;
438 else
439 res = Py_True;
440 Py_INCREF(res);
441 return res;
444 /* Search for the first index where items are different */
445 k = 1;
446 for (i = 0; i < va->ob_size && i < wa->ob_size; i++) {
447 vi = getarrayitem(v, i);
448 wi = getarrayitem(w, i);
449 if (vi == NULL || wi == NULL) {
450 Py_XDECREF(vi);
451 Py_XDECREF(wi);
452 return NULL;
454 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
455 if (k == 0)
456 break; /* Keeping vi and wi alive! */
457 Py_DECREF(vi);
458 Py_DECREF(wi);
459 if (k < 0)
460 return NULL;
463 if (k) {
464 /* No more items to compare -- compare sizes */
465 int vs = va->ob_size;
466 int ws = wa->ob_size;
467 int cmp;
468 switch (op) {
469 case Py_LT: cmp = vs < ws; break;
470 case Py_LE: cmp = vs <= ws; break;
471 case Py_EQ: cmp = vs == ws; break;
472 case Py_NE: cmp = vs != ws; break;
473 case Py_GT: cmp = vs > ws; break;
474 case Py_GE: cmp = vs >= ws; break;
475 default: return NULL; /* cannot happen */
477 if (cmp)
478 res = Py_True;
479 else
480 res = Py_False;
481 Py_INCREF(res);
482 return res;
485 /* We have an item that differs. First, shortcuts for EQ/NE */
486 if (op == Py_EQ) {
487 Py_INCREF(Py_False);
488 res = Py_False;
490 else if (op == Py_NE) {
491 Py_INCREF(Py_True);
492 res = Py_True;
494 else {
495 /* Compare the final item again using the proper operator */
496 res = PyObject_RichCompare(vi, wi, op);
498 Py_DECREF(vi);
499 Py_DECREF(wi);
500 return res;
503 static int
504 array_length(arrayobject *a)
506 return a->ob_size;
509 static PyObject *
510 array_item(arrayobject *a, int i)
512 if (i < 0 || i >= a->ob_size) {
513 PyErr_SetString(PyExc_IndexError, "array index out of range");
514 return NULL;
516 return getarrayitem((PyObject *)a, i);
519 static PyObject *
520 array_slice(arrayobject *a, int ilow, int ihigh)
522 arrayobject *np;
523 if (ilow < 0)
524 ilow = 0;
525 else if (ilow > a->ob_size)
526 ilow = a->ob_size;
527 if (ihigh < 0)
528 ihigh = 0;
529 if (ihigh < ilow)
530 ihigh = ilow;
531 else if (ihigh > a->ob_size)
532 ihigh = a->ob_size;
533 np = (arrayobject *) newarrayobject(ihigh - ilow, a->ob_descr);
534 if (np == NULL)
535 return NULL;
536 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
537 (ihigh-ilow) * a->ob_descr->itemsize);
538 return (PyObject *)np;
541 static PyObject *
542 array_concat(arrayobject *a, PyObject *bb)
544 int size;
545 arrayobject *np;
546 if (!is_arrayobject(bb)) {
547 PyErr_Format(PyExc_TypeError,
548 "can only append array (not \"%.200s\") to array",
549 bb->ob_type->tp_name);
550 return NULL;
552 #define b ((arrayobject *)bb)
553 if (a->ob_descr != b->ob_descr) {
554 PyErr_BadArgument();
555 return NULL;
557 size = a->ob_size + b->ob_size;
558 np = (arrayobject *) newarrayobject(size, a->ob_descr);
559 if (np == NULL) {
560 return NULL;
562 memcpy(np->ob_item, a->ob_item, a->ob_size*a->ob_descr->itemsize);
563 memcpy(np->ob_item + a->ob_size*a->ob_descr->itemsize,
564 b->ob_item, b->ob_size*b->ob_descr->itemsize);
565 return (PyObject *)np;
566 #undef b
569 static PyObject *
570 array_repeat(arrayobject *a, int n)
572 int i;
573 int size;
574 arrayobject *np;
575 char *p;
576 int nbytes;
577 if (n < 0)
578 n = 0;
579 size = a->ob_size * n;
580 np = (arrayobject *) newarrayobject(size, a->ob_descr);
581 if (np == NULL)
582 return NULL;
583 p = np->ob_item;
584 nbytes = a->ob_size * a->ob_descr->itemsize;
585 for (i = 0; i < n; i++) {
586 memcpy(p, a->ob_item, nbytes);
587 p += nbytes;
589 return (PyObject *) np;
592 static int
593 array_ass_slice(arrayobject *a, int ilow, int ihigh, PyObject *v)
595 char *item;
596 int n; /* Size of replacement array */
597 int d; /* Change in size */
598 #define b ((arrayobject *)v)
599 if (v == NULL)
600 n = 0;
601 else if (is_arrayobject(v)) {
602 n = b->ob_size;
603 if (a == b) {
604 /* Special case "a[i:j] = a" -- copy b first */
605 int ret;
606 v = array_slice(b, 0, n);
607 ret = array_ass_slice(a, ilow, ihigh, v);
608 Py_DECREF(v);
609 return ret;
611 if (b->ob_descr != a->ob_descr) {
612 PyErr_BadArgument();
613 return -1;
616 else {
617 PyErr_Format(PyExc_TypeError,
618 "can only assign array (not \"%.200s\") to array slice",
619 v->ob_type->tp_name);
620 return -1;
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;
639 PyMem_RESIZE(item, char, a->ob_size*a->ob_descr->itemsize);
640 /* Can't fail */
641 a->ob_item = item;
643 else if (d > 0) { /* Insert d items */
644 PyMem_RESIZE(item, char,
645 (a->ob_size + d)*a->ob_descr->itemsize);
646 if (item == NULL) {
647 PyErr_NoMemory();
648 return -1;
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;
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
663 static int
664 array_ass_item(arrayobject *a, int i, PyObject *v)
666 if (i < 0 || i >= a->ob_size) {
667 PyErr_SetString(PyExc_IndexError,
668 "array assignment index out of range");
669 return -1;
671 if (v == NULL)
672 return array_ass_slice(a, i, i+1, v);
673 return (*a->ob_descr->setitem)(a, i, v);
676 static int
677 setarrayitem(PyObject *a, int i, PyObject *v)
679 assert(is_arrayobject(a));
680 return array_ass_item((arrayobject *)a, i, v);
683 static PyObject *
684 ins(arrayobject *self, int where, PyObject *v)
686 if (ins1(self, where, v) != 0)
687 return NULL;
688 Py_INCREF(Py_None);
689 return Py_None;
692 static PyObject *
693 array_count(arrayobject *self, PyObject *args)
695 int count = 0;
696 int i;
697 PyObject *v;
699 if (!PyArg_ParseTuple(args, "O:count", &v))
700 return NULL;
701 for (i = 0; i < self->ob_size; i++) {
702 PyObject *selfi = getarrayitem((PyObject *)self, i);
703 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
704 Py_DECREF(selfi);
705 if (cmp > 0)
706 count++;
707 else if (cmp < 0)
708 return NULL;
710 return PyInt_FromLong((long)count);
713 static char count_doc [] =
714 "count(x)\n\
716 Return number of occurences of x in the array.";
718 static PyObject *
719 array_index(arrayobject *self, PyObject *args)
721 int i;
722 PyObject *v;
724 if (!PyArg_ParseTuple(args, "O:index", &v))
725 return NULL;
726 for (i = 0; i < self->ob_size; i++) {
727 PyObject *selfi = getarrayitem((PyObject *)self, i);
728 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
729 Py_DECREF(selfi);
730 if (cmp > 0) {
731 return PyInt_FromLong((long)i);
733 else if (cmp < 0)
734 return NULL;
736 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
737 return NULL;
740 static char index_doc [] =
741 "index(x)\n\
743 Return index of first occurence of x in the array.";
745 static PyObject *
746 array_remove(arrayobject *self, PyObject *args)
748 int i;
749 PyObject *v;
751 if (!PyArg_ParseTuple(args, "O:remove", &v))
752 return NULL;
753 for (i = 0; i < self->ob_size; i++) {
754 PyObject *selfi = getarrayitem((PyObject *)self,i);
755 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
756 Py_DECREF(selfi);
757 if (cmp > 0) {
758 if (array_ass_slice(self, i, i+1,
759 (PyObject *)NULL) != 0)
760 return NULL;
761 Py_INCREF(Py_None);
762 return Py_None;
764 else if (cmp < 0)
765 return NULL;
767 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
768 return NULL;
771 static char remove_doc [] =
772 "remove(x)\n\
774 Remove the first occurence of x in the array.";
776 static PyObject *
777 array_pop(arrayobject *self, PyObject *args)
779 int i = -1;
780 PyObject *v;
781 if (!PyArg_ParseTuple(args, "|i:pop", &i))
782 return NULL;
783 if (self->ob_size == 0) {
784 /* Special-case most common failure cause */
785 PyErr_SetString(PyExc_IndexError, "pop from empty array");
786 return NULL;
788 if (i < 0)
789 i += self->ob_size;
790 if (i < 0 || i >= self->ob_size) {
791 PyErr_SetString(PyExc_IndexError, "pop index out of range");
792 return NULL;
794 v = getarrayitem((PyObject *)self,i);
795 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
796 Py_DECREF(v);
797 return NULL;
799 return v;
802 static char pop_doc [] =
803 "pop([i])\n\
805 Return the i-th element and delete it from the array. i defaults to -1.";
807 static PyObject *
808 array_extend(arrayobject *self, PyObject *args)
810 int size;
811 PyObject *bb;
813 if (!PyArg_ParseTuple(args, "O:extend", &bb))
814 return NULL;
816 if (!is_arrayobject(bb)) {
817 PyErr_Format(PyExc_TypeError,
818 "can only extend array with array (not \"%.200s\")",
819 bb->ob_type->tp_name);
820 return NULL;
822 #define b ((arrayobject *)bb)
823 if (self->ob_descr != b->ob_descr) {
824 PyErr_SetString(PyExc_TypeError,
825 "can only extend with array of same kind");
826 return NULL;
828 size = self->ob_size + b->ob_size;
829 PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize);
830 if (self->ob_item == NULL) {
831 PyObject_Del(self);
832 return PyErr_NoMemory();
834 memcpy(self->ob_item + self->ob_size*self->ob_descr->itemsize,
835 b->ob_item, b->ob_size*b->ob_descr->itemsize);
836 self->ob_size = size;
837 Py_INCREF(Py_None);
838 return Py_None;
839 #undef b
842 static char extend_doc [] =
843 "extend(array)\n\
845 Append array items to the end of the array.";
847 static PyObject *
848 array_insert(arrayobject *self, PyObject *args)
850 int i;
851 PyObject *v;
852 if (!PyArg_ParseTuple(args, "iO:insert", &i, &v))
853 return NULL;
854 return ins(self, i, v);
857 static char insert_doc [] =
858 "insert(i,x)\n\
860 Insert a new item x into the array before position i.";
863 static PyObject *
864 array_buffer_info(arrayobject *self, PyObject *args)
866 PyObject* retval = NULL;
867 if (!PyArg_ParseTuple(args, ":buffer_info"))
868 return NULL;
869 retval = PyTuple_New(2);
870 if (!retval)
871 return NULL;
873 PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item));
874 PyTuple_SET_ITEM(retval, 1, PyInt_FromLong((long)(self->ob_size)));
876 return retval;
879 static char buffer_info_doc [] =
880 "buffer_info() -> (address, length)\n\
882 Return a tuple (address, length) giving the current memory address and\n\
883 the length in items of the buffer used to hold array's contents\n\
884 The length should be multiplied by the itemsize attribute to calculate\n\
885 the buffer length in bytes.";
888 static PyObject *
889 array_append(arrayobject *self, PyObject *args)
891 PyObject *v;
892 if (!PyArg_ParseTuple(args, "O:append", &v))
893 return NULL;
894 return ins(self, (int) self->ob_size, v);
897 static char append_doc [] =
898 "append(x)\n\
900 Append new value x to the end of the array.";
903 static PyObject *
904 array_byteswap(arrayobject *self, PyObject *args)
906 char *p;
907 int i;
909 if (!PyArg_ParseTuple(args, ":byteswap"))
910 return NULL;
912 switch (self->ob_descr->itemsize) {
913 case 1:
914 break;
915 case 2:
916 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 2) {
917 char p0 = p[0];
918 p[0] = p[1];
919 p[1] = p0;
921 break;
922 case 4:
923 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 4) {
924 char p0 = p[0];
925 char p1 = p[1];
926 p[0] = p[3];
927 p[1] = p[2];
928 p[2] = p1;
929 p[3] = p0;
931 break;
932 case 8:
933 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 8) {
934 char p0 = p[0];
935 char p1 = p[1];
936 char p2 = p[2];
937 char p3 = p[3];
938 p[0] = p[7];
939 p[1] = p[6];
940 p[2] = p[5];
941 p[3] = p[4];
942 p[4] = p3;
943 p[5] = p2;
944 p[6] = p1;
945 p[7] = p0;
947 break;
948 default:
949 PyErr_SetString(PyExc_RuntimeError,
950 "don't know how to byteswap this array type");
951 return NULL;
953 Py_INCREF(Py_None);
954 return Py_None;
957 static char byteswap_doc [] =
958 "byteswap()\n\
960 Byteswap all items of the array. If the items in the array are not 1, 2,\n\
961 4, or 8 bytes in size, RuntimeError is raised.";
963 static PyObject *
964 array_reverse(arrayobject *self, PyObject *args)
966 register int itemsize = self->ob_descr->itemsize;
967 register char *p, *q;
968 /* little buffer to hold items while swapping */
969 char tmp[256]; /* 8 is probably enough -- but why skimp */
970 assert(itemsize <= sizeof(tmp));
972 if (!PyArg_ParseTuple(args, ":reverse"))
973 return NULL;
975 if (self->ob_size > 1) {
976 for (p = self->ob_item,
977 q = self->ob_item + (self->ob_size - 1)*itemsize;
978 p < q;
979 p += itemsize, q -= itemsize) {
980 /* memory areas guaranteed disjoint, so memcpy
981 * is safe (& memmove may be slower).
983 memcpy(tmp, p, itemsize);
984 memcpy(p, q, itemsize);
985 memcpy(q, tmp, itemsize);
989 Py_INCREF(Py_None);
990 return Py_None;
993 static char reverse_doc [] =
994 "reverse()\n\
996 Reverse the order of the items in the array.";
998 static PyObject *
999 array_fromfile(arrayobject *self, PyObject *args)
1001 PyObject *f;
1002 int n;
1003 FILE *fp;
1004 if (!PyArg_ParseTuple(args, "Oi:fromfile", &f, &n))
1005 return NULL;
1006 fp = PyFile_AsFile(f);
1007 if (fp == NULL) {
1008 PyErr_SetString(PyExc_TypeError, "arg1 must be open file");
1009 return NULL;
1011 if (n > 0) {
1012 char *item = self->ob_item;
1013 int itemsize = self->ob_descr->itemsize;
1014 size_t nread;
1015 int newlength;
1016 size_t newbytes;
1017 /* Be careful here about overflow */
1018 if ((newlength = self->ob_size + n) <= 0 ||
1019 (newbytes = newlength * itemsize) / itemsize !=
1020 (size_t)newlength)
1021 goto nomem;
1022 PyMem_RESIZE(item, char, newbytes);
1023 if (item == NULL) {
1024 nomem:
1025 PyErr_NoMemory();
1026 return NULL;
1028 self->ob_item = item;
1029 self->ob_size += n;
1030 nread = fread(item + (self->ob_size - n) * itemsize,
1031 itemsize, n, fp);
1032 if (nread < (size_t)n) {
1033 self->ob_size -= (n - nread);
1034 PyMem_RESIZE(item, char, self->ob_size*itemsize);
1035 self->ob_item = item;
1036 PyErr_SetString(PyExc_EOFError,
1037 "not enough items in file");
1038 return NULL;
1041 Py_INCREF(Py_None);
1042 return Py_None;
1045 static char fromfile_doc [] =
1046 "fromfile(f, n)\n\
1048 Read n objects from the file object f and append them to the end of the\n\
1049 array. Also called as read.";
1052 static PyObject *
1053 array_tofile(arrayobject *self, PyObject *args)
1055 PyObject *f;
1056 FILE *fp;
1057 if (!PyArg_ParseTuple(args, "O:tofile", &f))
1058 return NULL;
1059 fp = PyFile_AsFile(f);
1060 if (fp == NULL) {
1061 PyErr_SetString(PyExc_TypeError, "arg must be open file");
1062 return NULL;
1064 if (self->ob_size > 0) {
1065 if (fwrite(self->ob_item, self->ob_descr->itemsize,
1066 self->ob_size, fp) != (size_t)self->ob_size) {
1067 PyErr_SetFromErrno(PyExc_IOError);
1068 clearerr(fp);
1069 return NULL;
1072 Py_INCREF(Py_None);
1073 return Py_None;
1076 static char tofile_doc [] =
1077 "tofile(f)\n\
1079 Write all items (as machine values) to the file object f. Also called as\n\
1080 write.";
1083 static PyObject *
1084 array_fromlist(arrayobject *self, PyObject *args)
1086 int n;
1087 PyObject *list;
1088 int itemsize = self->ob_descr->itemsize;
1089 if (!PyArg_ParseTuple(args, "O:fromlist", &list))
1090 return NULL;
1091 if (!PyList_Check(list)) {
1092 PyErr_SetString(PyExc_TypeError, "arg must be list");
1093 return NULL;
1095 n = PyList_Size(list);
1096 if (n > 0) {
1097 char *item = self->ob_item;
1098 int i;
1099 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
1100 if (item == NULL) {
1101 PyErr_NoMemory();
1102 return NULL;
1104 self->ob_item = item;
1105 self->ob_size += n;
1106 for (i = 0; i < n; i++) {
1107 PyObject *v = PyList_GetItem(list, i);
1108 if ((*self->ob_descr->setitem)(self,
1109 self->ob_size - n + i, v) != 0) {
1110 self->ob_size -= n;
1111 PyMem_RESIZE(item, char,
1112 self->ob_size * itemsize);
1113 self->ob_item = item;
1114 return NULL;
1118 Py_INCREF(Py_None);
1119 return Py_None;
1122 static char fromlist_doc [] =
1123 "fromlist(list)\n\
1125 Append items to array from list.";
1128 static PyObject *
1129 array_tolist(arrayobject *self, PyObject *args)
1131 PyObject *list = PyList_New(self->ob_size);
1132 int i;
1133 if (!PyArg_ParseTuple(args, ":tolist"))
1134 return NULL;
1135 if (list == NULL)
1136 return NULL;
1137 for (i = 0; i < self->ob_size; i++) {
1138 PyObject *v = getarrayitem((PyObject *)self, i);
1139 if (v == NULL) {
1140 Py_DECREF(list);
1141 return NULL;
1143 PyList_SetItem(list, i, v);
1145 return list;
1148 static char tolist_doc [] =
1149 "tolist() -> list\n\
1151 Convert array to an ordinary list with the same items.";
1154 static PyObject *
1155 array_fromstring(arrayobject *self, PyObject *args)
1157 char *str;
1158 int n;
1159 int itemsize = self->ob_descr->itemsize;
1160 if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n))
1161 return NULL;
1162 if (n % itemsize != 0) {
1163 PyErr_SetString(PyExc_ValueError,
1164 "string length not a multiple of item size");
1165 return NULL;
1167 n = n / itemsize;
1168 if (n > 0) {
1169 char *item = self->ob_item;
1170 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
1171 if (item == NULL) {
1172 PyErr_NoMemory();
1173 return NULL;
1175 self->ob_item = item;
1176 self->ob_size += n;
1177 memcpy(item + (self->ob_size - n) * itemsize,
1178 str, itemsize*n);
1180 Py_INCREF(Py_None);
1181 return Py_None;
1184 static char fromstring_doc [] =
1185 "fromstring(string)\n\
1187 Appends items from the string, interpreting it as an array of machine\n\
1188 values,as if it had been read from a file using the fromfile() method).";
1191 static PyObject *
1192 array_tostring(arrayobject *self, PyObject *args)
1194 if (!PyArg_ParseTuple(args, ":tostring"))
1195 return NULL;
1196 return PyString_FromStringAndSize(self->ob_item,
1197 self->ob_size * self->ob_descr->itemsize);
1200 static char tostring_doc [] =
1201 "tostring() -> string\n\
1203 Convert the array to an array of machine values and return the string\n\
1204 representation.";
1206 PyMethodDef array_methods[] = {
1207 {"append", (PyCFunction)array_append, METH_VARARGS,
1208 append_doc},
1209 {"buffer_info", (PyCFunction)array_buffer_info, METH_VARARGS,
1210 buffer_info_doc},
1211 {"byteswap", (PyCFunction)array_byteswap, METH_VARARGS,
1212 byteswap_doc},
1213 {"count", (PyCFunction)array_count, METH_VARARGS,
1214 count_doc},
1215 {"extend", (PyCFunction)array_extend, METH_VARARGS,
1216 extend_doc},
1217 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
1218 fromfile_doc},
1219 {"fromlist", (PyCFunction)array_fromlist, METH_VARARGS,
1220 fromlist_doc},
1221 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
1222 fromstring_doc},
1223 {"index", (PyCFunction)array_index, METH_VARARGS,
1224 index_doc},
1225 {"insert", (PyCFunction)array_insert, METH_VARARGS,
1226 insert_doc},
1227 {"pop", (PyCFunction)array_pop, METH_VARARGS,
1228 pop_doc},
1229 {"read", (PyCFunction)array_fromfile, METH_VARARGS,
1230 fromfile_doc},
1231 {"remove", (PyCFunction)array_remove, METH_VARARGS,
1232 remove_doc},
1233 {"reverse", (PyCFunction)array_reverse, METH_VARARGS,
1234 reverse_doc},
1235 /* {"sort", (PyCFunction)array_sort, METH_VARARGS,
1236 sort_doc},*/
1237 {"tofile", (PyCFunction)array_tofile, METH_VARARGS,
1238 tofile_doc},
1239 {"tolist", (PyCFunction)array_tolist, METH_VARARGS,
1240 tolist_doc},
1241 {"tostring", (PyCFunction)array_tostring, METH_VARARGS,
1242 tostring_doc},
1243 {"write", (PyCFunction)array_tofile, METH_VARARGS,
1244 tofile_doc},
1245 {NULL, NULL} /* sentinel */
1248 static PyObject *
1249 array_getattr(arrayobject *a, char *name)
1251 if (strcmp(name, "typecode") == 0) {
1252 char tc = a->ob_descr->typecode;
1253 return PyString_FromStringAndSize(&tc, 1);
1255 if (strcmp(name, "itemsize") == 0) {
1256 return PyInt_FromLong((long)a->ob_descr->itemsize);
1258 if (strcmp(name, "__members__") == 0) {
1259 PyObject *list = PyList_New(2);
1260 if (list) {
1261 PyList_SetItem(list, 0,
1262 PyString_FromString("typecode"));
1263 PyList_SetItem(list, 1,
1264 PyString_FromString("itemsize"));
1265 if (PyErr_Occurred()) {
1266 Py_DECREF(list);
1267 list = NULL;
1270 return list;
1272 return Py_FindMethod(array_methods, (PyObject *)a, name);
1275 static int
1276 array_print(arrayobject *a, FILE *fp, int flags)
1278 int ok = 0;
1279 int i, len;
1280 PyObject *v;
1281 len = a->ob_size;
1282 if (len == 0) {
1283 fprintf(fp, "array('%c')", a->ob_descr->typecode);
1284 return ok;
1286 if (a->ob_descr->typecode == 'c') {
1287 PyObject *t_empty = PyTuple_New(0);
1288 fprintf(fp, "array('c', ");
1289 v = array_tostring(a, t_empty);
1290 Py_DECREF(t_empty);
1291 ok = PyObject_Print(v, fp, 0);
1292 Py_XDECREF(v);
1293 fprintf(fp, ")");
1294 return ok;
1296 fprintf(fp, "array('%c', [", a->ob_descr->typecode);
1297 for (i = 0; i < len && ok == 0; i++) {
1298 if (i > 0)
1299 fprintf(fp, ", ");
1300 v = (a->ob_descr->getitem)(a, i);
1301 ok = PyObject_Print(v, fp, 0);
1302 Py_XDECREF(v);
1304 fprintf(fp, "])");
1305 return ok;
1308 static PyObject *
1309 array_repr(arrayobject *a)
1311 char buf[256];
1312 PyObject *s, *t, *comma, *v;
1313 int i, len;
1314 len = a->ob_size;
1315 if (len == 0) {
1316 sprintf(buf, "array('%c')", a->ob_descr->typecode);
1317 return PyString_FromString(buf);
1319 if (a->ob_descr->typecode == 'c') {
1320 PyObject *t_empty = PyTuple_New(0);
1321 sprintf(buf, "array('c', ");
1322 s = PyString_FromString(buf);
1323 v = array_tostring(a, t_empty);
1324 Py_DECREF(t_empty);
1325 t = PyObject_Repr(v);
1326 Py_XDECREF(v);
1327 PyString_ConcatAndDel(&s, t);
1328 PyString_ConcatAndDel(&s, PyString_FromString(")"));
1329 return s;
1331 sprintf(buf, "array('%c', [", a->ob_descr->typecode);
1332 s = PyString_FromString(buf);
1333 comma = PyString_FromString(", ");
1334 for (i = 0; i < len && !PyErr_Occurred(); i++) {
1335 if (i > 0)
1336 PyString_Concat(&s, comma);
1337 v = (a->ob_descr->getitem)(a, i);
1338 t = PyObject_Repr(v);
1339 Py_XDECREF(v);
1340 PyString_ConcatAndDel(&s, t);
1342 Py_XDECREF(comma);
1343 PyString_ConcatAndDel(&s, PyString_FromString("])"));
1344 return s;
1347 static int
1348 array_buffer_getreadbuf(arrayobject *self, int index, const void **ptr)
1350 if ( index != 0 ) {
1351 PyErr_SetString(PyExc_SystemError,
1352 "Accessing non-existent array segment");
1353 return -1;
1355 *ptr = (void *)self->ob_item;
1356 return self->ob_size*self->ob_descr->itemsize;
1359 static int
1360 array_buffer_getwritebuf(arrayobject *self, int index, const void **ptr)
1362 if ( index != 0 ) {
1363 PyErr_SetString(PyExc_SystemError,
1364 "Accessing non-existent array segment");
1365 return -1;
1367 *ptr = (void *)self->ob_item;
1368 return self->ob_size*self->ob_descr->itemsize;
1371 static int
1372 array_buffer_getsegcount(arrayobject *self, int *lenp)
1374 if ( lenp )
1375 *lenp = self->ob_size*self->ob_descr->itemsize;
1376 return 1;
1379 static PySequenceMethods array_as_sequence = {
1380 (inquiry)array_length, /*sq_length*/
1381 (binaryfunc)array_concat, /*sq_concat*/
1382 (intargfunc)array_repeat, /*sq_repeat*/
1383 (intargfunc)array_item, /*sq_item*/
1384 (intintargfunc)array_slice, /*sq_slice*/
1385 (intobjargproc)array_ass_item, /*sq_ass_item*/
1386 (intintobjargproc)array_ass_slice, /*sq_ass_slice*/
1389 static PyBufferProcs array_as_buffer = {
1390 (getreadbufferproc)array_buffer_getreadbuf,
1391 (getwritebufferproc)array_buffer_getwritebuf,
1392 (getsegcountproc)array_buffer_getsegcount,
1395 static PyObject *
1396 a_array(PyObject *self, PyObject *args)
1398 char c;
1399 PyObject *initial = NULL;
1400 struct arraydescr *descr;
1401 if (!PyArg_ParseTuple(args, "c:array", &c)) {
1402 PyErr_Clear();
1403 if (!PyArg_ParseTuple(args, "cO:array", &c, &initial))
1404 return NULL;
1405 if (!PyList_Check(initial) && !PyString_Check(initial)) {
1406 PyErr_SetString(PyExc_TypeError,
1407 "array initializer must be list or string");
1408 return NULL;
1411 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1412 if (descr->typecode == c) {
1413 PyObject *a;
1414 int len;
1415 if (initial == NULL || !PyList_Check(initial))
1416 len = 0;
1417 else
1418 len = PyList_Size(initial);
1419 a = newarrayobject(len, descr);
1420 if (a == NULL)
1421 return NULL;
1422 if (len > 0) {
1423 int i;
1424 for (i = 0; i < len; i++) {
1425 PyObject *v =
1426 PyList_GetItem(initial, i);
1427 if (setarrayitem(a, i, v) != 0) {
1428 Py_DECREF(a);
1429 return NULL;
1433 if (initial != NULL && PyString_Check(initial)) {
1434 PyObject *t_initial = Py_BuildValue("(O)",
1435 initial);
1436 PyObject *v =
1437 array_fromstring((arrayobject *)a,
1438 t_initial);
1439 Py_DECREF(t_initial);
1440 if (v == NULL) {
1441 Py_DECREF(a);
1442 return NULL;
1444 Py_DECREF(v);
1446 return a;
1449 PyErr_SetString(PyExc_ValueError,
1450 "bad typecode (must be c, b, B, h, H, i, I, l, L, f or d)");
1451 return NULL;
1454 static char a_array_doc [] =
1455 "array(typecode [, initializer]) -> array\n\
1457 Return a new array whose items are restricted by typecode, and\n\
1458 initialized from the optional initializer value, which must be a list\n\
1459 or a string.";
1461 static PyMethodDef a_methods[] = {
1462 {"array", a_array, METH_VARARGS, a_array_doc},
1463 {NULL, NULL} /* sentinel */
1466 static char module_doc [] =
1467 "This module defines a new object type which can efficiently represent\n\
1468 an array of basic values: characters, integers, floating point\n\
1469 numbers. Arrays are sequence types and behave very much like lists,\n\
1470 except that the type of objects stored in them is constrained. The\n\
1471 type is specified at object creation time by using a type code, which\n\
1472 is a single character. The following type codes are defined:\n\
1474 Type code C Type Minimum size in bytes \n\
1475 'c' character 1 \n\
1476 'b' signed integer 1 \n\
1477 'B' unsigned integer 1 \n\
1478 'h' signed integer 2 \n\
1479 'H' unsigned integer 2 \n\
1480 'i' signed integer 2 \n\
1481 'I' unsigned integer 2 \n\
1482 'l' signed integer 4 \n\
1483 'L' unsigned integer 4 \n\
1484 'f' floating point 4 \n\
1485 'd' floating point 8 \n\
1487 Functions:\n\
1489 array(typecode [, initializer]) -- create a new array\n\
1491 Special Objects:\n\
1493 ArrayType -- type object for array objects\n\
1496 static char arraytype_doc [] =
1497 "An array represents basic values and behave very much like lists, except\n\
1498 the type of objects stored in them is constrained.\n\
1500 Methods:\n\
1502 append() -- append a new item to the end of the array\n\
1503 buffer_info() -- return information giving the current memory info\n\
1504 byteswap() -- byteswap all the items of the array\n\
1505 count() -- return number of occurences of an object\n\
1506 extend() -- extend array by appending array elements\n\
1507 fromfile() -- read items from a file object\n\
1508 fromlist() -- append items from the list\n\
1509 fromstring() -- append items from the string\n\
1510 index() -- return index of first occurence of an object\n\
1511 insert() -- insert a new item into the array at a provided position\n\
1512 pop() -- remove and return item (default last)\n\
1513 read() -- DEPRECATED, use fromfile()\n\
1514 remove() -- remove first occurence of an object\n\
1515 reverse() -- reverse the order of the items in the array\n\
1516 tofile() -- write all items to a file object\n\
1517 tolist() -- return the array converted to an ordinary list\n\
1518 tostring() -- return the array converted to a string\n\
1519 write() -- DEPRECATED, use tofile()\n\
1521 Variables:\n\
1523 typecode -- the typecode character used to create the array\n\
1524 itemsize -- the length in bytes of one array item\n\
1527 statichere PyTypeObject Arraytype = {
1528 PyObject_HEAD_INIT(NULL)
1530 "array",
1531 sizeof(arrayobject),
1533 (destructor)array_dealloc, /* tp_dealloc */
1534 (printfunc)array_print, /* tp_print */
1535 (getattrfunc)array_getattr, /* tp_getattr */
1536 0, /* tp_setattr */
1537 0, /* tp_compare */
1538 (reprfunc)array_repr, /* tp_repr */
1539 0, /* tp_as _number*/
1540 &array_as_sequence, /* tp_as _sequence*/
1541 0, /* tp_as _mapping*/
1542 0, /* tp_hash */
1543 0, /* tp_call */
1544 0, /* tp_str */
1545 0, /* tp_getattro */
1546 0, /* tp_setattro */
1547 &array_as_buffer, /* tp_as_buffer*/
1548 Py_TPFLAGS_DEFAULT, /* tp_flags */
1549 arraytype_doc, /* tp_doc */
1550 0, /* tp_traverse */
1551 0, /* tp_clear */
1552 array_richcompare, /* tp_richcompare */
1555 DL_EXPORT(void)
1556 initarray(void)
1558 PyObject *m, *d;
1560 Arraytype.ob_type = &PyType_Type;
1561 m = Py_InitModule3("array", a_methods, module_doc);
1562 d = PyModule_GetDict(m);
1563 PyDict_SetItemString(d, "ArrayType", (PyObject *)&Arraytype);
1564 /* No need to check the error here, the caller will do that */