Exceptions raised during renaming in rotating file handlers are now passed to handleE...
[python.git] / Modules / arraymodule.c
blob3c603506732778bb056f056f1ec1c7468fce6867
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"
7 #include "structmember.h"
9 #ifdef STDC_HEADERS
10 #include <stddef.h>
11 #else /* !STDC_HEADERS */
12 #ifndef DONT_HAVE_SYS_TYPES_H
13 #include <sys/types.h> /* For size_t */
14 #endif /* DONT_HAVE_SYS_TYPES_H */
15 #endif /* !STDC_HEADERS */
17 struct arrayobject; /* Forward */
19 /* All possible arraydescr values are defined in the vector "descriptors"
20 * below. That's defined later because the appropriate get and set
21 * functions aren't visible yet.
23 struct arraydescr {
24 int typecode;
25 int itemsize;
26 PyObject * (*getitem)(struct arrayobject *, int);
27 int (*setitem)(struct arrayobject *, int, PyObject *);
30 typedef struct arrayobject {
31 PyObject_HEAD
32 int ob_size;
33 char *ob_item;
34 int allocated;
35 struct arraydescr *ob_descr;
36 PyObject *weakreflist; /* List of weak references */
37 } arrayobject;
39 static PyTypeObject Arraytype;
41 #define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
42 #define array_CheckExact(op) ((op)->ob_type == &Arraytype)
44 static int
45 array_resize(arrayobject *self, int newsize)
47 char *items;
48 size_t _new_size;
50 /* Bypass realloc() when a previous overallocation is large enough
51 to accommodate the newsize. If the newsize is 16 smaller than the
52 current size, then proceed with the realloc() to shrink the list.
55 if (self->allocated >= newsize &&
56 self->ob_size < newsize + 16 &&
57 self->ob_item != NULL) {
58 self->ob_size = newsize;
59 return 0;
62 /* This over-allocates proportional to the array size, making room
63 * for additional growth. The over-allocation is mild, but is
64 * enough to give linear-time amortized behavior over a long
65 * sequence of appends() in the presence of a poorly-performing
66 * system realloc().
67 * The growth pattern is: 0, 4, 8, 16, 25, 34, 46, 56, 67, 79, ...
68 * Note, the pattern starts out the same as for lists but then
69 * grows at a smaller rate so that larger arrays only overallocate
70 * by about 1/16th -- this is done because arrays are presumed to be more
71 * memory critical.
74 _new_size = (newsize >> 4) + (self->ob_size < 8 ? 3 : 7) + newsize;
75 items = self->ob_item;
76 /* XXX The following multiplication and division does not optimize away
77 like it does for lists since the size is not known at compile time */
78 if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize))
79 PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize));
80 else
81 items = NULL;
82 if (items == NULL) {
83 PyErr_NoMemory();
84 return -1;
86 self->ob_item = items;
87 self->ob_size = newsize;
88 self->allocated = _new_size;
89 return 0;
92 /****************************************************************************
93 Get and Set functions for each type.
94 A Get function takes an arrayobject* and an integer index, returning the
95 array value at that index wrapped in an appropriate PyObject*.
96 A Set function takes an arrayobject, integer index, and PyObject*; sets
97 the array value at that index to the raw C data extracted from the PyObject*,
98 and returns 0 if successful, else nonzero on failure (PyObject* not of an
99 appropriate type or value).
100 Note that the basic Get and Set functions do NOT check that the index is
101 in bounds; that's the responsibility of the caller.
102 ****************************************************************************/
104 static PyObject *
105 c_getitem(arrayobject *ap, int i)
107 return PyString_FromStringAndSize(&((char *)ap->ob_item)[i], 1);
110 static int
111 c_setitem(arrayobject *ap, int i, PyObject *v)
113 char x;
114 if (!PyArg_Parse(v, "c;array item must be char", &x))
115 return -1;
116 if (i >= 0)
117 ((char *)ap->ob_item)[i] = x;
118 return 0;
121 static PyObject *
122 b_getitem(arrayobject *ap, int i)
124 long x = ((char *)ap->ob_item)[i];
125 if (x >= 128)
126 x -= 256;
127 return PyInt_FromLong(x);
130 static int
131 b_setitem(arrayobject *ap, int i, PyObject *v)
133 short x;
134 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
135 must use the next size up that is signed ('h') and manually do
136 the overflow checking */
137 if (!PyArg_Parse(v, "h;array item must be integer", &x))
138 return -1;
139 else if (x < -128) {
140 PyErr_SetString(PyExc_OverflowError,
141 "signed char is less than minimum");
142 return -1;
144 else if (x > 127) {
145 PyErr_SetString(PyExc_OverflowError,
146 "signed char is greater than maximum");
147 return -1;
149 if (i >= 0)
150 ((char *)ap->ob_item)[i] = (char)x;
151 return 0;
154 static PyObject *
155 BB_getitem(arrayobject *ap, int i)
157 long x = ((unsigned char *)ap->ob_item)[i];
158 return PyInt_FromLong(x);
161 static int
162 BB_setitem(arrayobject *ap, int i, PyObject *v)
164 unsigned char x;
165 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
166 if (!PyArg_Parse(v, "b;array item must be integer", &x))
167 return -1;
168 if (i >= 0)
169 ((char *)ap->ob_item)[i] = x;
170 return 0;
173 #ifdef Py_USING_UNICODE
174 static PyObject *
175 u_getitem(arrayobject *ap, int i)
177 return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1);
180 static int
181 u_setitem(arrayobject *ap, int i, PyObject *v)
183 Py_UNICODE *p;
184 int len;
186 if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
187 return -1;
188 if (len != 1) {
189 PyErr_SetString(PyExc_TypeError, "array item must be unicode character");
190 return -1;
192 if (i >= 0)
193 ((Py_UNICODE *)ap->ob_item)[i] = p[0];
194 return 0;
196 #endif
198 static PyObject *
199 h_getitem(arrayobject *ap, int i)
201 return PyInt_FromLong((long) ((short *)ap->ob_item)[i]);
204 static int
205 h_setitem(arrayobject *ap, int i, PyObject *v)
207 short x;
208 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
209 if (!PyArg_Parse(v, "h;array item must be integer", &x))
210 return -1;
211 if (i >= 0)
212 ((short *)ap->ob_item)[i] = x;
213 return 0;
216 static PyObject *
217 HH_getitem(arrayobject *ap, int i)
219 return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
222 static int
223 HH_setitem(arrayobject *ap, int i, PyObject *v)
225 int x;
226 /* PyArg_Parse's 'h' formatter is for a signed short, therefore
227 must use the next size up and manually do the overflow checking */
228 if (!PyArg_Parse(v, "i;array item must be integer", &x))
229 return -1;
230 else if (x < 0) {
231 PyErr_SetString(PyExc_OverflowError,
232 "unsigned short is less than minimum");
233 return -1;
235 else if (x > USHRT_MAX) {
236 PyErr_SetString(PyExc_OverflowError,
237 "unsigned short is greater than maximum");
238 return -1;
240 if (i >= 0)
241 ((short *)ap->ob_item)[i] = (short)x;
242 return 0;
245 static PyObject *
246 i_getitem(arrayobject *ap, int i)
248 return PyInt_FromLong((long) ((int *)ap->ob_item)[i]);
251 static int
252 i_setitem(arrayobject *ap, int i, PyObject *v)
254 int x;
255 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
256 if (!PyArg_Parse(v, "i;array item must be integer", &x))
257 return -1;
258 if (i >= 0)
259 ((int *)ap->ob_item)[i] = x;
260 return 0;
263 static PyObject *
264 II_getitem(arrayobject *ap, int i)
266 return PyLong_FromUnsignedLong(
267 (unsigned long) ((unsigned int *)ap->ob_item)[i]);
270 static int
271 II_setitem(arrayobject *ap, int i, PyObject *v)
273 unsigned long x;
274 if (PyLong_Check(v)) {
275 x = PyLong_AsUnsignedLong(v);
276 if (x == (unsigned long) -1 && PyErr_Occurred())
277 return -1;
279 else {
280 long y;
281 if (!PyArg_Parse(v, "l;array item must be integer", &y))
282 return -1;
283 if (y < 0) {
284 PyErr_SetString(PyExc_OverflowError,
285 "unsigned int is less than minimum");
286 return -1;
288 x = (unsigned long)y;
291 if (x > UINT_MAX) {
292 PyErr_SetString(PyExc_OverflowError,
293 "unsigned int is greater than maximum");
294 return -1;
297 if (i >= 0)
298 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
299 return 0;
302 static PyObject *
303 l_getitem(arrayobject *ap, int i)
305 return PyInt_FromLong(((long *)ap->ob_item)[i]);
308 static int
309 l_setitem(arrayobject *ap, int i, PyObject *v)
311 long x;
312 if (!PyArg_Parse(v, "l;array item must be integer", &x))
313 return -1;
314 if (i >= 0)
315 ((long *)ap->ob_item)[i] = x;
316 return 0;
319 static PyObject *
320 LL_getitem(arrayobject *ap, int i)
322 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
325 static int
326 LL_setitem(arrayobject *ap, int i, PyObject *v)
328 unsigned long x;
329 if (PyLong_Check(v)) {
330 x = PyLong_AsUnsignedLong(v);
331 if (x == (unsigned long) -1 && PyErr_Occurred())
332 return -1;
334 else {
335 long y;
336 if (!PyArg_Parse(v, "l;array item must be integer", &y))
337 return -1;
338 if (y < 0) {
339 PyErr_SetString(PyExc_OverflowError,
340 "unsigned long is less than minimum");
341 return -1;
343 x = (unsigned long)y;
346 if (x > ULONG_MAX) {
347 PyErr_SetString(PyExc_OverflowError,
348 "unsigned long is greater than maximum");
349 return -1;
352 if (i >= 0)
353 ((unsigned long *)ap->ob_item)[i] = x;
354 return 0;
357 static PyObject *
358 f_getitem(arrayobject *ap, int i)
360 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
363 static int
364 f_setitem(arrayobject *ap, int i, PyObject *v)
366 float x;
367 if (!PyArg_Parse(v, "f;array item must be float", &x))
368 return -1;
369 if (i >= 0)
370 ((float *)ap->ob_item)[i] = x;
371 return 0;
374 static PyObject *
375 d_getitem(arrayobject *ap, int i)
377 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
380 static int
381 d_setitem(arrayobject *ap, int i, PyObject *v)
383 double x;
384 if (!PyArg_Parse(v, "d;array item must be float", &x))
385 return -1;
386 if (i >= 0)
387 ((double *)ap->ob_item)[i] = x;
388 return 0;
391 /* Description of types */
392 static struct arraydescr descriptors[] = {
393 {'c', sizeof(char), c_getitem, c_setitem},
394 {'b', sizeof(char), b_getitem, b_setitem},
395 {'B', sizeof(char), BB_getitem, BB_setitem},
396 #ifdef Py_USING_UNICODE
397 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem},
398 #endif
399 {'h', sizeof(short), h_getitem, h_setitem},
400 {'H', sizeof(short), HH_getitem, HH_setitem},
401 {'i', sizeof(int), i_getitem, i_setitem},
402 {'I', sizeof(int), II_getitem, II_setitem},
403 {'l', sizeof(long), l_getitem, l_setitem},
404 {'L', sizeof(long), LL_getitem, LL_setitem},
405 {'f', sizeof(float), f_getitem, f_setitem},
406 {'d', sizeof(double), d_getitem, d_setitem},
407 {'\0', 0, 0, 0} /* Sentinel */
410 /****************************************************************************
411 Implementations of array object methods.
412 ****************************************************************************/
414 static PyObject *
415 newarrayobject(PyTypeObject *type, int size, struct arraydescr *descr)
417 arrayobject *op;
418 size_t nbytes;
420 if (size < 0) {
421 PyErr_BadInternalCall();
422 return NULL;
425 nbytes = size * descr->itemsize;
426 /* Check for overflow */
427 if (nbytes / descr->itemsize != (size_t)size) {
428 return PyErr_NoMemory();
430 op = (arrayobject *) type->tp_alloc(type, 0);
431 if (op == NULL) {
432 return NULL;
434 op->ob_size = size;
435 if (size <= 0) {
436 op->ob_item = NULL;
438 else {
439 op->ob_item = PyMem_NEW(char, nbytes);
440 if (op->ob_item == NULL) {
441 PyObject_Del(op);
442 return PyErr_NoMemory();
445 op->ob_descr = descr;
446 op->allocated = size;
447 op->weakreflist = NULL;
448 return (PyObject *) op;
451 static PyObject *
452 getarrayitem(PyObject *op, int i)
454 register arrayobject *ap;
455 assert(array_Check(op));
456 ap = (arrayobject *)op;
457 assert(i>=0 && i<ap->ob_size);
458 return (*ap->ob_descr->getitem)(ap, i);
461 static int
462 ins1(arrayobject *self, int where, PyObject *v)
464 char *items;
465 int n = self->ob_size;
466 if (v == NULL) {
467 PyErr_BadInternalCall();
468 return -1;
470 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
471 return -1;
473 if (array_resize(self, n+1) == -1)
474 return -1;
475 items = self->ob_item;
476 if (where < 0) {
477 where += n;
478 if (where < 0)
479 where = 0;
481 if (where > n)
482 where = n;
483 /* appends don't need to call memmove() */
484 if (where != n)
485 memmove(items + (where+1)*self->ob_descr->itemsize,
486 items + where*self->ob_descr->itemsize,
487 (n-where)*self->ob_descr->itemsize);
488 return (*self->ob_descr->setitem)(self, where, v);
491 /* Methods */
493 static void
494 array_dealloc(arrayobject *op)
496 if (op->weakreflist != NULL)
497 PyObject_ClearWeakRefs((PyObject *) op);
498 if (op->ob_item != NULL)
499 PyMem_DEL(op->ob_item);
500 op->ob_type->tp_free((PyObject *)op);
503 static PyObject *
504 array_richcompare(PyObject *v, PyObject *w, int op)
506 arrayobject *va, *wa;
507 PyObject *vi = NULL;
508 PyObject *wi = NULL;
509 int i, k;
510 PyObject *res;
512 if (!array_Check(v) || !array_Check(w)) {
513 Py_INCREF(Py_NotImplemented);
514 return Py_NotImplemented;
517 va = (arrayobject *)v;
518 wa = (arrayobject *)w;
520 if (va->ob_size != wa->ob_size && (op == Py_EQ || op == Py_NE)) {
521 /* Shortcut: if the lengths differ, the arrays differ */
522 if (op == Py_EQ)
523 res = Py_False;
524 else
525 res = Py_True;
526 Py_INCREF(res);
527 return res;
530 /* Search for the first index where items are different */
531 k = 1;
532 for (i = 0; i < va->ob_size && i < wa->ob_size; i++) {
533 vi = getarrayitem(v, i);
534 wi = getarrayitem(w, i);
535 if (vi == NULL || wi == NULL) {
536 Py_XDECREF(vi);
537 Py_XDECREF(wi);
538 return NULL;
540 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
541 if (k == 0)
542 break; /* Keeping vi and wi alive! */
543 Py_DECREF(vi);
544 Py_DECREF(wi);
545 if (k < 0)
546 return NULL;
549 if (k) {
550 /* No more items to compare -- compare sizes */
551 int vs = va->ob_size;
552 int ws = wa->ob_size;
553 int cmp;
554 switch (op) {
555 case Py_LT: cmp = vs < ws; break;
556 case Py_LE: cmp = vs <= ws; break;
557 case Py_EQ: cmp = vs == ws; break;
558 case Py_NE: cmp = vs != ws; break;
559 case Py_GT: cmp = vs > ws; break;
560 case Py_GE: cmp = vs >= ws; break;
561 default: return NULL; /* cannot happen */
563 if (cmp)
564 res = Py_True;
565 else
566 res = Py_False;
567 Py_INCREF(res);
568 return res;
571 /* We have an item that differs. First, shortcuts for EQ/NE */
572 if (op == Py_EQ) {
573 Py_INCREF(Py_False);
574 res = Py_False;
576 else if (op == Py_NE) {
577 Py_INCREF(Py_True);
578 res = Py_True;
580 else {
581 /* Compare the final item again using the proper operator */
582 res = PyObject_RichCompare(vi, wi, op);
584 Py_DECREF(vi);
585 Py_DECREF(wi);
586 return res;
589 static int
590 array_length(arrayobject *a)
592 return a->ob_size;
595 static PyObject *
596 array_item(arrayobject *a, int i)
598 if (i < 0 || i >= a->ob_size) {
599 PyErr_SetString(PyExc_IndexError, "array index out of range");
600 return NULL;
602 return getarrayitem((PyObject *)a, i);
605 static PyObject *
606 array_slice(arrayobject *a, int ilow, int ihigh)
608 arrayobject *np;
609 if (ilow < 0)
610 ilow = 0;
611 else if (ilow > a->ob_size)
612 ilow = a->ob_size;
613 if (ihigh < 0)
614 ihigh = 0;
615 if (ihigh < ilow)
616 ihigh = ilow;
617 else if (ihigh > a->ob_size)
618 ihigh = a->ob_size;
619 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
620 if (np == NULL)
621 return NULL;
622 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
623 (ihigh-ilow) * a->ob_descr->itemsize);
624 return (PyObject *)np;
627 static PyObject *
628 array_copy(arrayobject *a, PyObject *unused)
630 return array_slice(a, 0, a->ob_size);
633 PyDoc_STRVAR(copy_doc,
634 "copy(array)\n\
636 Return a copy of the array.");
638 static PyObject *
639 array_concat(arrayobject *a, PyObject *bb)
641 int size;
642 arrayobject *np;
643 if (!array_Check(bb)) {
644 PyErr_Format(PyExc_TypeError,
645 "can only append array (not \"%.200s\") to array",
646 bb->ob_type->tp_name);
647 return NULL;
649 #define b ((arrayobject *)bb)
650 if (a->ob_descr != b->ob_descr) {
651 PyErr_BadArgument();
652 return NULL;
654 size = a->ob_size + b->ob_size;
655 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
656 if (np == NULL) {
657 return NULL;
659 memcpy(np->ob_item, a->ob_item, a->ob_size*a->ob_descr->itemsize);
660 memcpy(np->ob_item + a->ob_size*a->ob_descr->itemsize,
661 b->ob_item, b->ob_size*b->ob_descr->itemsize);
662 return (PyObject *)np;
663 #undef b
666 static PyObject *
667 array_repeat(arrayobject *a, int n)
669 int i;
670 int size;
671 arrayobject *np;
672 char *p;
673 int nbytes;
674 if (n < 0)
675 n = 0;
676 size = a->ob_size * n;
677 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
678 if (np == NULL)
679 return NULL;
680 p = np->ob_item;
681 nbytes = a->ob_size * a->ob_descr->itemsize;
682 for (i = 0; i < n; i++) {
683 memcpy(p, a->ob_item, nbytes);
684 p += nbytes;
686 return (PyObject *) np;
689 static int
690 array_ass_slice(arrayobject *a, int ilow, int ihigh, PyObject *v)
692 char *item;
693 int n; /* Size of replacement array */
694 int d; /* Change in size */
695 #define b ((arrayobject *)v)
696 if (v == NULL)
697 n = 0;
698 else if (array_Check(v)) {
699 n = b->ob_size;
700 if (a == b) {
701 /* Special case "a[i:j] = a" -- copy b first */
702 int ret;
703 v = array_slice(b, 0, n);
704 ret = array_ass_slice(a, ilow, ihigh, v);
705 Py_DECREF(v);
706 return ret;
708 if (b->ob_descr != a->ob_descr) {
709 PyErr_BadArgument();
710 return -1;
713 else {
714 PyErr_Format(PyExc_TypeError,
715 "can only assign array (not \"%.200s\") to array slice",
716 v->ob_type->tp_name);
717 return -1;
719 if (ilow < 0)
720 ilow = 0;
721 else if (ilow > a->ob_size)
722 ilow = a->ob_size;
723 if (ihigh < 0)
724 ihigh = 0;
725 if (ihigh < ilow)
726 ihigh = ilow;
727 else if (ihigh > a->ob_size)
728 ihigh = a->ob_size;
729 item = a->ob_item;
730 d = n - (ihigh-ilow);
731 if (d < 0) { /* Delete -d items */
732 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
733 item + ihigh*a->ob_descr->itemsize,
734 (a->ob_size-ihigh)*a->ob_descr->itemsize);
735 a->ob_size += d;
736 PyMem_RESIZE(item, char, a->ob_size*a->ob_descr->itemsize);
737 /* Can't fail */
738 a->ob_item = item;
739 a->allocated = a->ob_size;
741 else if (d > 0) { /* Insert d items */
742 PyMem_RESIZE(item, char,
743 (a->ob_size + d)*a->ob_descr->itemsize);
744 if (item == NULL) {
745 PyErr_NoMemory();
746 return -1;
748 memmove(item + (ihigh+d)*a->ob_descr->itemsize,
749 item + ihigh*a->ob_descr->itemsize,
750 (a->ob_size-ihigh)*a->ob_descr->itemsize);
751 a->ob_item = item;
752 a->ob_size += d;
753 a->allocated = a->ob_size;
755 if (n > 0)
756 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
757 n*b->ob_descr->itemsize);
758 return 0;
759 #undef b
762 static int
763 array_ass_item(arrayobject *a, int i, PyObject *v)
765 if (i < 0 || i >= a->ob_size) {
766 PyErr_SetString(PyExc_IndexError,
767 "array assignment index out of range");
768 return -1;
770 if (v == NULL)
771 return array_ass_slice(a, i, i+1, v);
772 return (*a->ob_descr->setitem)(a, i, v);
775 static int
776 setarrayitem(PyObject *a, int i, PyObject *v)
778 assert(array_Check(a));
779 return array_ass_item((arrayobject *)a, i, v);
782 static int
783 array_iter_extend(arrayobject *self, PyObject *bb)
785 PyObject *it, *v;
787 it = PyObject_GetIter(bb);
788 if (it == NULL)
789 return -1;
791 while ((v = PyIter_Next(it)) != NULL) {
792 if (ins1(self, (int) self->ob_size, v) != 0) {
793 Py_DECREF(v);
794 Py_DECREF(it);
795 return -1;
797 Py_DECREF(v);
799 Py_DECREF(it);
800 if (PyErr_Occurred())
801 return -1;
802 return 0;
805 static int
806 array_do_extend(arrayobject *self, PyObject *bb)
808 int size;
810 if (!array_Check(bb))
811 return array_iter_extend(self, bb);
812 #define b ((arrayobject *)bb)
813 if (self->ob_descr != b->ob_descr) {
814 PyErr_SetString(PyExc_TypeError,
815 "can only extend with array of same kind");
816 return -1;
818 size = self->ob_size + b->ob_size;
819 PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize);
820 if (self->ob_item == NULL) {
821 PyObject_Del(self);
822 PyErr_NoMemory();
823 return -1;
825 memcpy(self->ob_item + self->ob_size*self->ob_descr->itemsize,
826 b->ob_item, b->ob_size*b->ob_descr->itemsize);
827 self->ob_size = size;
828 self->allocated = size;
830 return 0;
831 #undef b
834 static PyObject *
835 array_inplace_concat(arrayobject *self, PyObject *bb)
837 if (!array_Check(bb)) {
838 PyErr_Format(PyExc_TypeError,
839 "can only extend array with array (not \"%.200s\")",
840 bb->ob_type->tp_name);
841 return NULL;
843 if (array_do_extend(self, bb) == -1)
844 return NULL;
845 Py_INCREF(self);
846 return (PyObject *)self;
849 static PyObject *
850 array_inplace_repeat(arrayobject *self, int n)
852 char *items, *p;
853 int size, i;
855 if (self->ob_size > 0) {
856 if (n < 0)
857 n = 0;
858 items = self->ob_item;
859 size = self->ob_size * self->ob_descr->itemsize;
860 if (n == 0) {
861 PyMem_FREE(items);
862 self->ob_item = NULL;
863 self->ob_size = 0;
864 self->allocated = 0;
866 else {
867 PyMem_Resize(items, char, n * size);
868 if (items == NULL)
869 return PyErr_NoMemory();
870 p = items;
871 for (i = 1; i < n; i++) {
872 p += size;
873 memcpy(p, items, size);
875 self->ob_item = items;
876 self->ob_size *= n;
877 self->allocated = self->ob_size;
880 Py_INCREF(self);
881 return (PyObject *)self;
885 static PyObject *
886 ins(arrayobject *self, int where, PyObject *v)
888 if (ins1(self, where, v) != 0)
889 return NULL;
890 Py_INCREF(Py_None);
891 return Py_None;
894 static PyObject *
895 array_count(arrayobject *self, PyObject *v)
897 int count = 0;
898 int i;
900 for (i = 0; i < self->ob_size; i++) {
901 PyObject *selfi = getarrayitem((PyObject *)self, i);
902 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
903 Py_DECREF(selfi);
904 if (cmp > 0)
905 count++;
906 else if (cmp < 0)
907 return NULL;
909 return PyInt_FromLong((long)count);
912 PyDoc_STRVAR(count_doc,
913 "count(x)\n\
915 Return number of occurences of x in the array.");
917 static PyObject *
918 array_index(arrayobject *self, PyObject *v)
920 int i;
922 for (i = 0; i < self->ob_size; i++) {
923 PyObject *selfi = getarrayitem((PyObject *)self, i);
924 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
925 Py_DECREF(selfi);
926 if (cmp > 0) {
927 return PyInt_FromLong((long)i);
929 else if (cmp < 0)
930 return NULL;
932 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
933 return NULL;
936 PyDoc_STRVAR(index_doc,
937 "index(x)\n\
939 Return index of first occurence of x in the array.");
941 static int
942 array_contains(arrayobject *self, PyObject *v)
944 int i, cmp;
946 for (i = 0, cmp = 0 ; cmp == 0 && i < self->ob_size; i++) {
947 PyObject *selfi = getarrayitem((PyObject *)self, i);
948 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
949 Py_DECREF(selfi);
951 return cmp;
954 static PyObject *
955 array_remove(arrayobject *self, PyObject *v)
957 int i;
959 for (i = 0; i < self->ob_size; i++) {
960 PyObject *selfi = getarrayitem((PyObject *)self,i);
961 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
962 Py_DECREF(selfi);
963 if (cmp > 0) {
964 if (array_ass_slice(self, i, i+1,
965 (PyObject *)NULL) != 0)
966 return NULL;
967 Py_INCREF(Py_None);
968 return Py_None;
970 else if (cmp < 0)
971 return NULL;
973 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
974 return NULL;
977 PyDoc_STRVAR(remove_doc,
978 "remove(x)\n\
980 Remove the first occurence of x in the array.");
982 static PyObject *
983 array_pop(arrayobject *self, PyObject *args)
985 int i = -1;
986 PyObject *v;
987 if (!PyArg_ParseTuple(args, "|i:pop", &i))
988 return NULL;
989 if (self->ob_size == 0) {
990 /* Special-case most common failure cause */
991 PyErr_SetString(PyExc_IndexError, "pop from empty array");
992 return NULL;
994 if (i < 0)
995 i += self->ob_size;
996 if (i < 0 || i >= self->ob_size) {
997 PyErr_SetString(PyExc_IndexError, "pop index out of range");
998 return NULL;
1000 v = getarrayitem((PyObject *)self,i);
1001 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
1002 Py_DECREF(v);
1003 return NULL;
1005 return v;
1008 PyDoc_STRVAR(pop_doc,
1009 "pop([i])\n\
1011 Return the i-th element and delete it from the array. i defaults to -1.");
1013 static PyObject *
1014 array_extend(arrayobject *self, PyObject *bb)
1016 if (array_do_extend(self, bb) == -1)
1017 return NULL;
1018 Py_INCREF(Py_None);
1019 return Py_None;
1022 PyDoc_STRVAR(extend_doc,
1023 "extend(array or iterable)\n\
1025 Append items to the end of the array.");
1027 static PyObject *
1028 array_insert(arrayobject *self, PyObject *args)
1030 int i;
1031 PyObject *v;
1032 if (!PyArg_ParseTuple(args, "iO:insert", &i, &v))
1033 return NULL;
1034 return ins(self, i, v);
1037 PyDoc_STRVAR(insert_doc,
1038 "insert(i,x)\n\
1040 Insert a new item x into the array before position i.");
1043 static PyObject *
1044 array_buffer_info(arrayobject *self, PyObject *unused)
1046 PyObject* retval = NULL;
1047 retval = PyTuple_New(2);
1048 if (!retval)
1049 return NULL;
1051 PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item));
1052 PyTuple_SET_ITEM(retval, 1, PyInt_FromLong((long)(self->ob_size)));
1054 return retval;
1057 PyDoc_STRVAR(buffer_info_doc,
1058 "buffer_info() -> (address, length)\n\
1060 Return a tuple (address, length) giving the current memory address and\n\
1061 the length in items of the buffer used to hold array's contents\n\
1062 The length should be multiplied by the itemsize attribute to calculate\n\
1063 the buffer length in bytes.");
1066 static PyObject *
1067 array_append(arrayobject *self, PyObject *v)
1069 return ins(self, (int) self->ob_size, v);
1072 PyDoc_STRVAR(append_doc,
1073 "append(x)\n\
1075 Append new value x to the end of the array.");
1078 static PyObject *
1079 array_byteswap(arrayobject *self, PyObject *unused)
1081 char *p;
1082 int i;
1084 switch (self->ob_descr->itemsize) {
1085 case 1:
1086 break;
1087 case 2:
1088 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 2) {
1089 char p0 = p[0];
1090 p[0] = p[1];
1091 p[1] = p0;
1093 break;
1094 case 4:
1095 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 4) {
1096 char p0 = p[0];
1097 char p1 = p[1];
1098 p[0] = p[3];
1099 p[1] = p[2];
1100 p[2] = p1;
1101 p[3] = p0;
1103 break;
1104 case 8:
1105 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 8) {
1106 char p0 = p[0];
1107 char p1 = p[1];
1108 char p2 = p[2];
1109 char p3 = p[3];
1110 p[0] = p[7];
1111 p[1] = p[6];
1112 p[2] = p[5];
1113 p[3] = p[4];
1114 p[4] = p3;
1115 p[5] = p2;
1116 p[6] = p1;
1117 p[7] = p0;
1119 break;
1120 default:
1121 PyErr_SetString(PyExc_RuntimeError,
1122 "don't know how to byteswap this array type");
1123 return NULL;
1125 Py_INCREF(Py_None);
1126 return Py_None;
1129 PyDoc_STRVAR(byteswap_doc,
1130 "byteswap()\n\
1132 Byteswap all items of the array. If the items in the array are not 1, 2,\n\
1133 4, or 8 bytes in size, RuntimeError is raised.");
1135 static PyObject *
1136 array_reduce(arrayobject *array)
1138 PyObject *dict, *result;
1140 dict = PyObject_GetAttrString((PyObject *)array, "__dict__");
1141 if (dict == NULL) {
1142 PyErr_Clear();
1143 dict = Py_None;
1144 Py_INCREF(dict);
1146 result = Py_BuildValue("O(cs#)O",
1147 array->ob_type,
1148 array->ob_descr->typecode,
1149 array->ob_item,
1150 array->ob_size * array->ob_descr->itemsize,
1151 dict);
1152 Py_DECREF(dict);
1153 return result;
1156 PyDoc_STRVAR(array_doc, "Return state information for pickling.");
1158 static PyObject *
1159 array_reverse(arrayobject *self, PyObject *unused)
1161 register int itemsize = self->ob_descr->itemsize;
1162 register char *p, *q;
1163 /* little buffer to hold items while swapping */
1164 char tmp[256]; /* 8 is probably enough -- but why skimp */
1165 assert(itemsize <= sizeof(tmp));
1167 if (self->ob_size > 1) {
1168 for (p = self->ob_item,
1169 q = self->ob_item + (self->ob_size - 1)*itemsize;
1170 p < q;
1171 p += itemsize, q -= itemsize) {
1172 /* memory areas guaranteed disjoint, so memcpy
1173 * is safe (& memmove may be slower).
1175 memcpy(tmp, p, itemsize);
1176 memcpy(p, q, itemsize);
1177 memcpy(q, tmp, itemsize);
1181 Py_INCREF(Py_None);
1182 return Py_None;
1185 PyDoc_STRVAR(reverse_doc,
1186 "reverse()\n\
1188 Reverse the order of the items in the array.");
1190 static PyObject *
1191 array_fromfile(arrayobject *self, PyObject *args)
1193 PyObject *f;
1194 int n;
1195 FILE *fp;
1196 if (!PyArg_ParseTuple(args, "Oi:fromfile", &f, &n))
1197 return NULL;
1198 fp = PyFile_AsFile(f);
1199 if (fp == NULL) {
1200 PyErr_SetString(PyExc_TypeError, "arg1 must be open file");
1201 return NULL;
1203 if (n > 0) {
1204 char *item = self->ob_item;
1205 int itemsize = self->ob_descr->itemsize;
1206 size_t nread;
1207 int newlength;
1208 size_t newbytes;
1209 /* Be careful here about overflow */
1210 if ((newlength = self->ob_size + n) <= 0 ||
1211 (newbytes = newlength * itemsize) / itemsize !=
1212 (size_t)newlength)
1213 goto nomem;
1214 PyMem_RESIZE(item, char, newbytes);
1215 if (item == NULL) {
1216 nomem:
1217 PyErr_NoMemory();
1218 return NULL;
1220 self->ob_item = item;
1221 self->ob_size += n;
1222 self->allocated = self->ob_size;
1223 nread = fread(item + (self->ob_size - n) * itemsize,
1224 itemsize, n, fp);
1225 if (nread < (size_t)n) {
1226 self->ob_size -= (n - nread);
1227 PyMem_RESIZE(item, char, self->ob_size*itemsize);
1228 self->ob_item = item;
1229 self->allocated = self->ob_size;
1230 PyErr_SetString(PyExc_EOFError,
1231 "not enough items in file");
1232 return NULL;
1235 Py_INCREF(Py_None);
1236 return Py_None;
1239 PyDoc_STRVAR(fromfile_doc,
1240 "fromfile(f, n)\n\
1242 Read n objects from the file object f and append them to the end of the\n\
1243 array. Also called as read.");
1246 static PyObject *
1247 array_tofile(arrayobject *self, PyObject *f)
1249 FILE *fp;
1251 fp = PyFile_AsFile(f);
1252 if (fp == NULL) {
1253 PyErr_SetString(PyExc_TypeError, "arg must be open file");
1254 return NULL;
1256 if (self->ob_size > 0) {
1257 if (fwrite(self->ob_item, self->ob_descr->itemsize,
1258 self->ob_size, fp) != (size_t)self->ob_size) {
1259 PyErr_SetFromErrno(PyExc_IOError);
1260 clearerr(fp);
1261 return NULL;
1264 Py_INCREF(Py_None);
1265 return Py_None;
1268 PyDoc_STRVAR(tofile_doc,
1269 "tofile(f)\n\
1271 Write all items (as machine values) to the file object f. Also called as\n\
1272 write.");
1275 static PyObject *
1276 array_fromlist(arrayobject *self, PyObject *list)
1278 int n;
1279 int itemsize = self->ob_descr->itemsize;
1281 if (!PyList_Check(list)) {
1282 PyErr_SetString(PyExc_TypeError, "arg must be list");
1283 return NULL;
1285 n = PyList_Size(list);
1286 if (n > 0) {
1287 char *item = self->ob_item;
1288 int i;
1289 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
1290 if (item == NULL) {
1291 PyErr_NoMemory();
1292 return NULL;
1294 self->ob_item = item;
1295 self->ob_size += n;
1296 self->allocated = self->ob_size;
1297 for (i = 0; i < n; i++) {
1298 PyObject *v = PyList_GetItem(list, i);
1299 if ((*self->ob_descr->setitem)(self,
1300 self->ob_size - n + i, v) != 0) {
1301 self->ob_size -= n;
1302 PyMem_RESIZE(item, char,
1303 self->ob_size * itemsize);
1304 self->ob_item = item;
1305 self->allocated = self->ob_size;
1306 return NULL;
1310 Py_INCREF(Py_None);
1311 return Py_None;
1314 PyDoc_STRVAR(fromlist_doc,
1315 "fromlist(list)\n\
1317 Append items to array from list.");
1320 static PyObject *
1321 array_tolist(arrayobject *self, PyObject *unused)
1323 PyObject *list = PyList_New(self->ob_size);
1324 int i;
1326 if (list == NULL)
1327 return NULL;
1328 for (i = 0; i < self->ob_size; i++) {
1329 PyObject *v = getarrayitem((PyObject *)self, i);
1330 if (v == NULL) {
1331 Py_DECREF(list);
1332 return NULL;
1334 PyList_SetItem(list, i, v);
1336 return list;
1339 PyDoc_STRVAR(tolist_doc,
1340 "tolist() -> list\n\
1342 Convert array to an ordinary list with the same items.");
1345 static PyObject *
1346 array_fromstring(arrayobject *self, PyObject *args)
1348 char *str;
1349 int n;
1350 int itemsize = self->ob_descr->itemsize;
1351 if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n))
1352 return NULL;
1353 if (n % itemsize != 0) {
1354 PyErr_SetString(PyExc_ValueError,
1355 "string length not a multiple of item size");
1356 return NULL;
1358 n = n / itemsize;
1359 if (n > 0) {
1360 char *item = self->ob_item;
1361 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
1362 if (item == NULL) {
1363 PyErr_NoMemory();
1364 return NULL;
1366 self->ob_item = item;
1367 self->ob_size += n;
1368 self->allocated = self->ob_size;
1369 memcpy(item + (self->ob_size - n) * itemsize,
1370 str, itemsize*n);
1372 Py_INCREF(Py_None);
1373 return Py_None;
1376 PyDoc_STRVAR(fromstring_doc,
1377 "fromstring(string)\n\
1379 Appends items from the string, interpreting it as an array of machine\n\
1380 values,as if it had been read from a file using the fromfile() method).");
1383 static PyObject *
1384 array_tostring(arrayobject *self, PyObject *unused)
1386 return PyString_FromStringAndSize(self->ob_item,
1387 self->ob_size * self->ob_descr->itemsize);
1390 PyDoc_STRVAR(tostring_doc,
1391 "tostring() -> string\n\
1393 Convert the array to an array of machine values and return the string\n\
1394 representation.");
1398 #ifdef Py_USING_UNICODE
1399 static PyObject *
1400 array_fromunicode(arrayobject *self, PyObject *args)
1402 Py_UNICODE *ustr;
1403 int n;
1405 if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
1406 return NULL;
1407 if (self->ob_descr->typecode != 'u') {
1408 PyErr_SetString(PyExc_ValueError,
1409 "fromunicode() may only be called on "
1410 "type 'u' arrays");
1411 return NULL;
1413 if (n > 0) {
1414 Py_UNICODE *item = (Py_UNICODE *) self->ob_item;
1415 PyMem_RESIZE(item, Py_UNICODE, self->ob_size + n);
1416 if (item == NULL) {
1417 PyErr_NoMemory();
1418 return NULL;
1420 self->ob_item = (char *) item;
1421 self->ob_size += n;
1422 self->allocated = self->ob_size;
1423 memcpy(item + self->ob_size - n,
1424 ustr, n * sizeof(Py_UNICODE));
1427 Py_INCREF(Py_None);
1428 return Py_None;
1431 PyDoc_STRVAR(fromunicode_doc,
1432 "fromunicode(ustr)\n\
1434 Extends this array with data from the unicode string ustr.\n\
1435 The array must be a type 'u' array; otherwise a ValueError\n\
1436 is raised. Use array.fromstring(ustr.decode(...)) to\n\
1437 append Unicode data to an array of some other type.");
1440 static PyObject *
1441 array_tounicode(arrayobject *self, PyObject *unused)
1443 if (self->ob_descr->typecode != 'u') {
1444 PyErr_SetString(PyExc_ValueError,
1445 "tounicode() may only be called on type 'u' arrays");
1446 return NULL;
1448 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, self->ob_size);
1451 PyDoc_STRVAR(tounicode_doc,
1452 "tounicode() -> unicode\n\
1454 Convert the array to a unicode string. The array must be\n\
1455 a type 'u' array; otherwise a ValueError is raised. Use\n\
1456 array.tostring().decode() to obtain a unicode string from\n\
1457 an array of some other type.");
1459 #endif /* Py_USING_UNICODE */
1462 static PyObject *
1463 array_get_typecode(arrayobject *a, void *closure)
1465 char tc = a->ob_descr->typecode;
1466 return PyString_FromStringAndSize(&tc, 1);
1469 static PyObject *
1470 array_get_itemsize(arrayobject *a, void *closure)
1472 return PyInt_FromLong((long)a->ob_descr->itemsize);
1475 static PyGetSetDef array_getsets [] = {
1476 {"typecode", (getter) array_get_typecode, NULL,
1477 "the typecode character used to create the array"},
1478 {"itemsize", (getter) array_get_itemsize, NULL,
1479 "the size, in bytes, of one array item"},
1480 {NULL}
1483 PyMethodDef array_methods[] = {
1484 {"append", (PyCFunction)array_append, METH_O,
1485 append_doc},
1486 {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS,
1487 buffer_info_doc},
1488 {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
1489 byteswap_doc},
1490 {"__copy__", (PyCFunction)array_copy, METH_NOARGS,
1491 copy_doc},
1492 {"count", (PyCFunction)array_count, METH_O,
1493 count_doc},
1494 {"__deepcopy__",(PyCFunction)array_copy, METH_NOARGS,
1495 copy_doc},
1496 {"extend", (PyCFunction)array_extend, METH_O,
1497 extend_doc},
1498 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
1499 fromfile_doc},
1500 {"fromlist", (PyCFunction)array_fromlist, METH_O,
1501 fromlist_doc},
1502 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
1503 fromstring_doc},
1504 #ifdef Py_USING_UNICODE
1505 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
1506 fromunicode_doc},
1507 #endif
1508 {"index", (PyCFunction)array_index, METH_O,
1509 index_doc},
1510 {"insert", (PyCFunction)array_insert, METH_VARARGS,
1511 insert_doc},
1512 {"pop", (PyCFunction)array_pop, METH_VARARGS,
1513 pop_doc},
1514 {"read", (PyCFunction)array_fromfile, METH_VARARGS,
1515 fromfile_doc},
1516 {"__reduce__", (PyCFunction)array_reduce, METH_NOARGS,
1517 array_doc},
1518 {"remove", (PyCFunction)array_remove, METH_O,
1519 remove_doc},
1520 {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
1521 reverse_doc},
1522 /* {"sort", (PyCFunction)array_sort, METH_VARARGS,
1523 sort_doc},*/
1524 {"tofile", (PyCFunction)array_tofile, METH_O,
1525 tofile_doc},
1526 {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
1527 tolist_doc},
1528 {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
1529 tostring_doc},
1530 #ifdef Py_USING_UNICODE
1531 {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
1532 tounicode_doc},
1533 #endif
1534 {"write", (PyCFunction)array_tofile, METH_O,
1535 tofile_doc},
1536 {NULL, NULL} /* sentinel */
1539 static PyObject *
1540 array_repr(arrayobject *a)
1542 char buf[256], typecode;
1543 PyObject *s, *t, *v = NULL;
1544 int len;
1546 len = a->ob_size;
1547 typecode = a->ob_descr->typecode;
1548 if (len == 0) {
1549 PyOS_snprintf(buf, sizeof(buf), "array('%c')", typecode);
1550 return PyString_FromString(buf);
1553 if (typecode == 'c')
1554 v = array_tostring(a, NULL);
1555 #ifdef Py_USING_UNICODE
1556 else if (typecode == 'u')
1557 v = array_tounicode(a, NULL);
1558 #endif
1559 else
1560 v = array_tolist(a, NULL);
1561 t = PyObject_Repr(v);
1562 Py_XDECREF(v);
1564 PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode);
1565 s = PyString_FromString(buf);
1566 PyString_ConcatAndDel(&s, t);
1567 PyString_ConcatAndDel(&s, PyString_FromString(")"));
1568 return s;
1571 static PyObject*
1572 array_subscr(arrayobject* self, PyObject* item)
1574 if (PyInt_Check(item)) {
1575 long i = PyInt_AS_LONG(item);
1576 if (i < 0)
1577 i += self->ob_size;
1578 return array_item(self, i);
1580 else if (PyLong_Check(item)) {
1581 long i = PyLong_AsLong(item);
1582 if (i == -1 && PyErr_Occurred())
1583 return NULL;
1584 if (i < 0)
1585 i += self->ob_size;
1586 return array_item(self, i);
1588 else if (PySlice_Check(item)) {
1589 int start, stop, step, slicelength, cur, i;
1590 PyObject* result;
1591 arrayobject* ar;
1592 int itemsize = self->ob_descr->itemsize;
1594 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
1595 &start, &stop, &step, &slicelength) < 0) {
1596 return NULL;
1599 if (slicelength <= 0) {
1600 return newarrayobject(&Arraytype, 0, self->ob_descr);
1602 else {
1603 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
1604 if (!result) return NULL;
1606 ar = (arrayobject*)result;
1608 for (cur = start, i = 0; i < slicelength;
1609 cur += step, i++) {
1610 memcpy(ar->ob_item + i*itemsize,
1611 self->ob_item + cur*itemsize,
1612 itemsize);
1615 return result;
1618 else {
1619 PyErr_SetString(PyExc_TypeError,
1620 "list indices must be integers");
1621 return NULL;
1625 static int
1626 array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
1628 if (PyInt_Check(item)) {
1629 long i = PyInt_AS_LONG(item);
1630 if (i < 0)
1631 i += self->ob_size;
1632 return array_ass_item(self, i, value);
1634 else if (PyLong_Check(item)) {
1635 long i = PyLong_AsLong(item);
1636 if (i == -1 && PyErr_Occurred())
1637 return -1;
1638 if (i < 0)
1639 i += self->ob_size;
1640 return array_ass_item(self, i, value);
1642 else if (PySlice_Check(item)) {
1643 int start, stop, step, slicelength;
1644 int itemsize = self->ob_descr->itemsize;
1646 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
1647 &start, &stop, &step, &slicelength) < 0) {
1648 return -1;
1651 /* treat A[slice(a,b)] = v _exactly_ like A[a:b] = v */
1652 if (step == 1 && ((PySliceObject*)item)->step == Py_None)
1653 return array_ass_slice(self, start, stop, value);
1655 if (value == NULL) {
1656 /* delete slice */
1657 int cur, i, extra;
1659 if (slicelength <= 0)
1660 return 0;
1662 if (step < 0) {
1663 stop = start + 1;
1664 start = stop + step*(slicelength - 1) - 1;
1665 step = -step;
1668 for (cur = start, i = 0; i < slicelength - 1;
1669 cur += step, i++) {
1670 memmove(self->ob_item + (cur - i)*itemsize,
1671 self->ob_item + (cur + 1)*itemsize,
1672 (step - 1) * itemsize);
1674 extra = self->ob_size - (cur + 1);
1675 if (extra > 0) {
1676 memmove(self->ob_item + (cur - i)*itemsize,
1677 self->ob_item + (cur + 1)*itemsize,
1678 extra*itemsize);
1681 self->ob_size -= slicelength;
1682 self->ob_item = PyMem_REALLOC(self->ob_item, itemsize*self->ob_size);
1683 self->allocated = self->ob_size;
1685 return 0;
1687 else {
1688 /* assign slice */
1689 int cur, i;
1690 arrayobject* av;
1692 if (!array_Check(value)) {
1693 PyErr_Format(PyExc_TypeError,
1694 "must assign array (not \"%.200s\") to slice",
1695 value->ob_type->tp_name);
1696 return -1;
1699 av = (arrayobject*)value;
1701 if (av->ob_size != slicelength) {
1702 PyErr_Format(PyExc_ValueError,
1703 "attempt to assign array of size %d to extended slice of size %d",
1704 av->ob_size, slicelength);
1705 return -1;
1708 if (!slicelength)
1709 return 0;
1711 /* protect against a[::-1] = a */
1712 if (self == av) {
1713 value = array_slice(av, 0, av->ob_size);
1714 av = (arrayobject*)value;
1716 else {
1717 Py_INCREF(value);
1720 for (cur = start, i = 0; i < slicelength;
1721 cur += step, i++) {
1722 memcpy(self->ob_item + cur*itemsize,
1723 av->ob_item + i*itemsize,
1724 itemsize);
1727 Py_DECREF(value);
1729 return 0;
1732 else {
1733 PyErr_SetString(PyExc_TypeError,
1734 "list indices must be integers");
1735 return -1;
1739 static PyMappingMethods array_as_mapping = {
1740 (inquiry)array_length,
1741 (binaryfunc)array_subscr,
1742 (objobjargproc)array_ass_subscr
1745 static int
1746 array_buffer_getreadbuf(arrayobject *self, int index, const void **ptr)
1748 if ( index != 0 ) {
1749 PyErr_SetString(PyExc_SystemError,
1750 "Accessing non-existent array segment");
1751 return -1;
1753 *ptr = (void *)self->ob_item;
1754 return self->ob_size*self->ob_descr->itemsize;
1757 static int
1758 array_buffer_getwritebuf(arrayobject *self, int index, const void **ptr)
1760 if ( index != 0 ) {
1761 PyErr_SetString(PyExc_SystemError,
1762 "Accessing non-existent array segment");
1763 return -1;
1765 *ptr = (void *)self->ob_item;
1766 return self->ob_size*self->ob_descr->itemsize;
1769 static int
1770 array_buffer_getsegcount(arrayobject *self, int *lenp)
1772 if ( lenp )
1773 *lenp = self->ob_size*self->ob_descr->itemsize;
1774 return 1;
1777 static PySequenceMethods array_as_sequence = {
1778 (inquiry)array_length, /*sq_length*/
1779 (binaryfunc)array_concat, /*sq_concat*/
1780 (intargfunc)array_repeat, /*sq_repeat*/
1781 (intargfunc)array_item, /*sq_item*/
1782 (intintargfunc)array_slice, /*sq_slice*/
1783 (intobjargproc)array_ass_item, /*sq_ass_item*/
1784 (intintobjargproc)array_ass_slice, /*sq_ass_slice*/
1785 (objobjproc)array_contains, /*sq_contains*/
1786 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
1787 (intargfunc)array_inplace_repeat /*sq_inplace_repeat*/
1790 static PyBufferProcs array_as_buffer = {
1791 (getreadbufferproc)array_buffer_getreadbuf,
1792 (getwritebufferproc)array_buffer_getwritebuf,
1793 (getsegcountproc)array_buffer_getsegcount,
1796 static PyObject *
1797 array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1799 char c;
1800 PyObject *initial = NULL, *it = NULL;
1801 struct arraydescr *descr;
1803 if (!_PyArg_NoKeywords("array.array()", kwds))
1804 return NULL;
1806 if (!PyArg_ParseTuple(args, "c|O:array", &c, &initial))
1807 return NULL;
1809 if (!(initial == NULL || PyList_Check(initial)
1810 || PyString_Check(initial) || PyTuple_Check(initial)
1811 || (c == 'u' && PyUnicode_Check(initial)))) {
1812 it = PyObject_GetIter(initial);
1813 if (it == NULL)
1814 return NULL;
1815 /* We set initial to NULL so that the subsequent code
1816 will create an empty array of the appropriate type
1817 and afterwards we can use array_iter_extend to populate
1818 the array.
1820 initial = NULL;
1822 for (descr = descriptors; descr->typecode != '\0'; descr++) {
1823 if (descr->typecode == c) {
1824 PyObject *a;
1825 int len;
1827 if (initial == NULL || !(PyList_Check(initial)
1828 || PyTuple_Check(initial)))
1829 len = 0;
1830 else
1831 len = PySequence_Size(initial);
1833 a = newarrayobject(type, len, descr);
1834 if (a == NULL)
1835 return NULL;
1837 if (len > 0) {
1838 int i;
1839 for (i = 0; i < len; i++) {
1840 PyObject *v =
1841 PySequence_GetItem(initial, i);
1842 if (v == NULL) {
1843 Py_DECREF(a);
1844 return NULL;
1846 if (setarrayitem(a, i, v) != 0) {
1847 Py_DECREF(v);
1848 Py_DECREF(a);
1849 return NULL;
1851 Py_DECREF(v);
1853 } else if (initial != NULL && PyString_Check(initial)) {
1854 PyObject *t_initial = PyTuple_Pack(1,
1855 initial);
1856 PyObject *v =
1857 array_fromstring((arrayobject *)a,
1858 t_initial);
1859 Py_DECREF(t_initial);
1860 if (v == NULL) {
1861 Py_DECREF(a);
1862 return NULL;
1864 Py_DECREF(v);
1865 #ifdef Py_USING_UNICODE
1866 } else if (initial != NULL && PyUnicode_Check(initial)) {
1867 int n = PyUnicode_GET_DATA_SIZE(initial);
1868 if (n > 0) {
1869 arrayobject *self = (arrayobject *)a;
1870 char *item = self->ob_item;
1871 item = PyMem_Realloc(item, n);
1872 if (item == NULL) {
1873 PyErr_NoMemory();
1874 Py_DECREF(a);
1875 return NULL;
1877 self->ob_item = item;
1878 self->ob_size = n / sizeof(Py_UNICODE);
1879 memcpy(item, PyUnicode_AS_DATA(initial), n);
1880 self->allocated = self->ob_size;
1882 #endif
1884 if (it != NULL) {
1885 if (array_iter_extend((arrayobject *)a, it) == -1) {
1886 Py_DECREF(it);
1887 Py_DECREF(a);
1888 return NULL;
1890 Py_DECREF(it);
1892 return a;
1895 PyErr_SetString(PyExc_ValueError,
1896 "bad typecode (must be c, b, B, u, h, H, i, I, l, L, f or d)");
1897 return NULL;
1901 PyDoc_STRVAR(module_doc,
1902 "This module defines an object type which can efficiently represent\n\
1903 an array of basic values: characters, integers, floating point\n\
1904 numbers. Arrays are sequence types and behave very much like lists,\n\
1905 except that the type of objects stored in them is constrained. The\n\
1906 type is specified at object creation time by using a type code, which\n\
1907 is a single character. The following type codes are defined:\n\
1909 Type code C Type Minimum size in bytes \n\
1910 'c' character 1 \n\
1911 'b' signed integer 1 \n\
1912 'B' unsigned integer 1 \n\
1913 'u' Unicode character 2 \n\
1914 'h' signed integer 2 \n\
1915 'H' unsigned integer 2 \n\
1916 'i' signed integer 2 \n\
1917 'I' unsigned integer 2 \n\
1918 'l' signed integer 4 \n\
1919 'L' unsigned integer 4 \n\
1920 'f' floating point 4 \n\
1921 'd' floating point 8 \n\
1923 The constructor is:\n\
1925 array(typecode [, initializer]) -- create a new array\n\
1928 PyDoc_STRVAR(arraytype_doc,
1929 "array(typecode [, initializer]) -> array\n\
1931 Return a new array whose items are restricted by typecode, and\n\
1932 initialized from the optional initializer value, which must be a list,\n\
1933 string. or iterable over elements of the appropriate type.\n\
1935 Arrays represent basic values and behave very much like lists, except\n\
1936 the type of objects stored in them is constrained.\n\
1938 Methods:\n\
1940 append() -- append a new item to the end of the array\n\
1941 buffer_info() -- return information giving the current memory info\n\
1942 byteswap() -- byteswap all the items of the array\n\
1943 count() -- return number of occurences of an object\n\
1944 extend() -- extend array by appending multiple elements from an iterable\n\
1945 fromfile() -- read items from a file object\n\
1946 fromlist() -- append items from the list\n\
1947 fromstring() -- append items from the string\n\
1948 index() -- return index of first occurence of an object\n\
1949 insert() -- insert a new item into the array at a provided position\n\
1950 pop() -- remove and return item (default last)\n\
1951 read() -- DEPRECATED, use fromfile()\n\
1952 remove() -- remove first occurence of an object\n\
1953 reverse() -- reverse the order of the items in the array\n\
1954 tofile() -- write all items to a file object\n\
1955 tolist() -- return the array converted to an ordinary list\n\
1956 tostring() -- return the array converted to a string\n\
1957 write() -- DEPRECATED, use tofile()\n\
1959 Attributes:\n\
1961 typecode -- the typecode character used to create the array\n\
1962 itemsize -- the length in bytes of one array item\n\
1965 static PyObject *array_iter(arrayobject *ao);
1967 static PyTypeObject Arraytype = {
1968 PyObject_HEAD_INIT(NULL)
1970 "array.array",
1971 sizeof(arrayobject),
1973 (destructor)array_dealloc, /* tp_dealloc */
1974 0, /* tp_print */
1975 0, /* tp_getattr */
1976 0, /* tp_setattr */
1977 0, /* tp_compare */
1978 (reprfunc)array_repr, /* tp_repr */
1979 0, /* tp_as _number*/
1980 &array_as_sequence, /* tp_as _sequence*/
1981 &array_as_mapping, /* tp_as _mapping*/
1982 0, /* tp_hash */
1983 0, /* tp_call */
1984 0, /* tp_str */
1985 PyObject_GenericGetAttr, /* tp_getattro */
1986 0, /* tp_setattro */
1987 &array_as_buffer, /* tp_as_buffer*/
1988 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
1989 arraytype_doc, /* tp_doc */
1990 0, /* tp_traverse */
1991 0, /* tp_clear */
1992 array_richcompare, /* tp_richcompare */
1993 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
1994 (getiterfunc)array_iter, /* tp_iter */
1995 0, /* tp_iternext */
1996 array_methods, /* tp_methods */
1997 0, /* tp_members */
1998 array_getsets, /* tp_getset */
1999 0, /* tp_base */
2000 0, /* tp_dict */
2001 0, /* tp_descr_get */
2002 0, /* tp_descr_set */
2003 0, /* tp_dictoffset */
2004 0, /* tp_init */
2005 PyType_GenericAlloc, /* tp_alloc */
2006 array_new, /* tp_new */
2007 PyObject_Del, /* tp_free */
2011 /*********************** Array Iterator **************************/
2013 typedef struct {
2014 PyObject_HEAD
2015 long index;
2016 arrayobject *ao;
2017 PyObject * (*getitem)(struct arrayobject *, int);
2018 } arrayiterobject;
2020 static PyTypeObject PyArrayIter_Type;
2022 #define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
2024 static PyObject *
2025 array_iter(arrayobject *ao)
2027 arrayiterobject *it;
2029 if (!array_Check(ao)) {
2030 PyErr_BadInternalCall();
2031 return NULL;
2034 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
2035 if (it == NULL)
2036 return NULL;
2038 Py_INCREF(ao);
2039 it->ao = ao;
2040 it->index = 0;
2041 it->getitem = ao->ob_descr->getitem;
2042 PyObject_GC_Track(it);
2043 return (PyObject *)it;
2046 static PyObject *
2047 arrayiter_next(arrayiterobject *it)
2049 assert(PyArrayIter_Check(it));
2050 if (it->index < it->ao->ob_size)
2051 return (*it->getitem)(it->ao, it->index++);
2052 return NULL;
2055 static void
2056 arrayiter_dealloc(arrayiterobject *it)
2058 PyObject_GC_UnTrack(it);
2059 Py_XDECREF(it->ao);
2060 PyObject_GC_Del(it);
2063 static int
2064 arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2066 if (it->ao != NULL)
2067 return visit((PyObject *)(it->ao), arg);
2068 return 0;
2071 static PyTypeObject PyArrayIter_Type = {
2072 PyObject_HEAD_INIT(NULL)
2073 0, /* ob_size */
2074 "arrayiterator", /* tp_name */
2075 sizeof(arrayiterobject), /* tp_basicsize */
2076 0, /* tp_itemsize */
2077 /* methods */
2078 (destructor)arrayiter_dealloc, /* tp_dealloc */
2079 0, /* tp_print */
2080 0, /* tp_getattr */
2081 0, /* tp_setattr */
2082 0, /* tp_compare */
2083 0, /* tp_repr */
2084 0, /* tp_as_number */
2085 0, /* tp_as_sequence */
2086 0, /* tp_as_mapping */
2087 0, /* tp_hash */
2088 0, /* tp_call */
2089 0, /* tp_str */
2090 PyObject_GenericGetAttr, /* tp_getattro */
2091 0, /* tp_setattro */
2092 0, /* tp_as_buffer */
2093 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2094 0, /* tp_doc */
2095 (traverseproc)arrayiter_traverse, /* tp_traverse */
2096 0, /* tp_clear */
2097 0, /* tp_richcompare */
2098 0, /* tp_weaklistoffset */
2099 PyObject_SelfIter, /* tp_iter */
2100 (iternextfunc)arrayiter_next, /* tp_iternext */
2101 0, /* tp_methods */
2105 /*********************** Install Module **************************/
2107 /* No functions in array module. */
2108 static PyMethodDef a_methods[] = {
2109 {NULL, NULL, 0, NULL} /* Sentinel */
2113 PyMODINIT_FUNC
2114 initarray(void)
2116 PyObject *m;
2118 Arraytype.ob_type = &PyType_Type;
2119 PyArrayIter_Type.ob_type = &PyType_Type;
2120 m = Py_InitModule3("array", a_methods, module_doc);
2122 Py_INCREF((PyObject *)&Arraytype);
2123 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
2124 Py_INCREF((PyObject *)&Arraytype);
2125 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
2126 /* No need to check the error here, the caller will do that */