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 #define PY_SSIZE_T_CLEAN
8 #include "structmember.h"
12 #else /* !STDC_HEADERS */
13 #ifdef HAVE_SYS_TYPES_H
14 #include <sys/types.h> /* For size_t */
15 #endif /* HAVE_SYS_TYPES_H */
16 #endif /* !STDC_HEADERS */
18 struct arrayobject
; /* Forward */
20 /* All possible arraydescr values are defined in the vector "descriptors"
21 * below. That's defined later because the appropriate get and set
22 * functions aren't visible yet.
27 PyObject
* (*getitem
)(struct arrayobject
*, Py_ssize_t
);
28 int (*setitem
)(struct arrayobject
*, Py_ssize_t
, PyObject
*);
31 typedef struct arrayobject
{
35 struct arraydescr
*ob_descr
;
36 PyObject
*weakreflist
; /* List of weak references */
39 static PyTypeObject Arraytype
;
41 #define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
42 #define array_CheckExact(op) ((op)->ob_type == &Arraytype)
45 array_resize(arrayobject
*self
, Py_ssize_t newsize
)
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
;
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
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
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
));
86 self
->ob_item
= items
;
87 self
->ob_size
= newsize
;
88 self
->allocated
= _new_size
;
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 ****************************************************************************/
105 c_getitem(arrayobject
*ap
, Py_ssize_t i
)
107 return PyString_FromStringAndSize(&((char *)ap
->ob_item
)[i
], 1);
111 c_setitem(arrayobject
*ap
, Py_ssize_t i
, PyObject
*v
)
114 if (!PyArg_Parse(v
, "c;array item must be char", &x
))
117 ((char *)ap
->ob_item
)[i
] = x
;
122 b_getitem(arrayobject
*ap
, Py_ssize_t i
)
124 long x
= ((char *)ap
->ob_item
)[i
];
127 return PyInt_FromLong(x
);
131 b_setitem(arrayobject
*ap
, Py_ssize_t i
, PyObject
*v
)
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
))
140 PyErr_SetString(PyExc_OverflowError
,
141 "signed char is less than minimum");
145 PyErr_SetString(PyExc_OverflowError
,
146 "signed char is greater than maximum");
150 ((char *)ap
->ob_item
)[i
] = (char)x
;
155 BB_getitem(arrayobject
*ap
, Py_ssize_t i
)
157 long x
= ((unsigned char *)ap
->ob_item
)[i
];
158 return PyInt_FromLong(x
);
162 BB_setitem(arrayobject
*ap
, Py_ssize_t i
, PyObject
*v
)
165 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
166 if (!PyArg_Parse(v
, "b;array item must be integer", &x
))
169 ((char *)ap
->ob_item
)[i
] = x
;
173 #ifdef Py_USING_UNICODE
175 u_getitem(arrayobject
*ap
, Py_ssize_t i
)
177 return PyUnicode_FromUnicode(&((Py_UNICODE
*) ap
->ob_item
)[i
], 1);
181 u_setitem(arrayobject
*ap
, Py_ssize_t i
, PyObject
*v
)
186 if (!PyArg_Parse(v
, "u#;array item must be unicode character", &p
, &len
))
189 PyErr_SetString(PyExc_TypeError
,
190 "array item must be unicode character");
194 ((Py_UNICODE
*)ap
->ob_item
)[i
] = p
[0];
200 h_getitem(arrayobject
*ap
, Py_ssize_t i
)
202 return PyInt_FromLong((long) ((short *)ap
->ob_item
)[i
]);
206 h_setitem(arrayobject
*ap
, Py_ssize_t i
, PyObject
*v
)
209 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
210 if (!PyArg_Parse(v
, "h;array item must be integer", &x
))
213 ((short *)ap
->ob_item
)[i
] = x
;
218 HH_getitem(arrayobject
*ap
, Py_ssize_t i
)
220 return PyInt_FromLong((long) ((unsigned short *)ap
->ob_item
)[i
]);
224 HH_setitem(arrayobject
*ap
, Py_ssize_t i
, PyObject
*v
)
227 /* PyArg_Parse's 'h' formatter is for a signed short, therefore
228 must use the next size up and manually do the overflow checking */
229 if (!PyArg_Parse(v
, "i;array item must be integer", &x
))
232 PyErr_SetString(PyExc_OverflowError
,
233 "unsigned short is less than minimum");
236 else if (x
> USHRT_MAX
) {
237 PyErr_SetString(PyExc_OverflowError
,
238 "unsigned short is greater than maximum");
242 ((short *)ap
->ob_item
)[i
] = (short)x
;
247 i_getitem(arrayobject
*ap
, Py_ssize_t i
)
249 return PyInt_FromLong((long) ((int *)ap
->ob_item
)[i
]);
253 i_setitem(arrayobject
*ap
, Py_ssize_t i
, PyObject
*v
)
256 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
257 if (!PyArg_Parse(v
, "i;array item must be integer", &x
))
260 ((int *)ap
->ob_item
)[i
] = x
;
265 II_getitem(arrayobject
*ap
, Py_ssize_t i
)
267 return PyLong_FromUnsignedLong(
268 (unsigned long) ((unsigned int *)ap
->ob_item
)[i
]);
272 II_setitem(arrayobject
*ap
, Py_ssize_t i
, PyObject
*v
)
275 if (PyLong_Check(v
)) {
276 x
= PyLong_AsUnsignedLong(v
);
277 if (x
== (unsigned long) -1 && PyErr_Occurred())
282 if (!PyArg_Parse(v
, "l;array item must be integer", &y
))
285 PyErr_SetString(PyExc_OverflowError
,
286 "unsigned int is less than minimum");
289 x
= (unsigned long)y
;
293 PyErr_SetString(PyExc_OverflowError
,
294 "unsigned int is greater than maximum");
299 ((unsigned int *)ap
->ob_item
)[i
] = (unsigned int)x
;
304 l_getitem(arrayobject
*ap
, Py_ssize_t i
)
306 return PyInt_FromLong(((long *)ap
->ob_item
)[i
]);
310 l_setitem(arrayobject
*ap
, Py_ssize_t i
, PyObject
*v
)
313 if (!PyArg_Parse(v
, "l;array item must be integer", &x
))
316 ((long *)ap
->ob_item
)[i
] = x
;
321 LL_getitem(arrayobject
*ap
, Py_ssize_t i
)
323 return PyLong_FromUnsignedLong(((unsigned long *)ap
->ob_item
)[i
]);
327 LL_setitem(arrayobject
*ap
, Py_ssize_t i
, PyObject
*v
)
330 if (PyLong_Check(v
)) {
331 x
= PyLong_AsUnsignedLong(v
);
332 if (x
== (unsigned long) -1 && PyErr_Occurred())
337 if (!PyArg_Parse(v
, "l;array item must be integer", &y
))
340 PyErr_SetString(PyExc_OverflowError
,
341 "unsigned long is less than minimum");
344 x
= (unsigned long)y
;
348 PyErr_SetString(PyExc_OverflowError
,
349 "unsigned long is greater than maximum");
354 ((unsigned long *)ap
->ob_item
)[i
] = x
;
359 f_getitem(arrayobject
*ap
, Py_ssize_t i
)
361 return PyFloat_FromDouble((double) ((float *)ap
->ob_item
)[i
]);
365 f_setitem(arrayobject
*ap
, Py_ssize_t i
, PyObject
*v
)
368 if (!PyArg_Parse(v
, "f;array item must be float", &x
))
371 ((float *)ap
->ob_item
)[i
] = x
;
376 d_getitem(arrayobject
*ap
, Py_ssize_t i
)
378 return PyFloat_FromDouble(((double *)ap
->ob_item
)[i
]);
382 d_setitem(arrayobject
*ap
, Py_ssize_t i
, PyObject
*v
)
385 if (!PyArg_Parse(v
, "d;array item must be float", &x
))
388 ((double *)ap
->ob_item
)[i
] = x
;
392 /* Description of types */
393 static struct arraydescr descriptors
[] = {
394 {'c', sizeof(char), c_getitem
, c_setitem
},
395 {'b', sizeof(char), b_getitem
, b_setitem
},
396 {'B', sizeof(char), BB_getitem
, BB_setitem
},
397 #ifdef Py_USING_UNICODE
398 {'u', sizeof(Py_UNICODE
), u_getitem
, u_setitem
},
400 {'h', sizeof(short), h_getitem
, h_setitem
},
401 {'H', sizeof(short), HH_getitem
, HH_setitem
},
402 {'i', sizeof(int), i_getitem
, i_setitem
},
403 {'I', sizeof(int), II_getitem
, II_setitem
},
404 {'l', sizeof(long), l_getitem
, l_setitem
},
405 {'L', sizeof(long), LL_getitem
, LL_setitem
},
406 {'f', sizeof(float), f_getitem
, f_setitem
},
407 {'d', sizeof(double), d_getitem
, d_setitem
},
408 {'\0', 0, 0, 0} /* Sentinel */
411 /****************************************************************************
412 Implementations of array object methods.
413 ****************************************************************************/
416 newarrayobject(PyTypeObject
*type
, Py_ssize_t size
, struct arraydescr
*descr
)
422 PyErr_BadInternalCall();
426 nbytes
= size
* descr
->itemsize
;
427 /* Check for overflow */
428 if (nbytes
/ descr
->itemsize
!= (size_t)size
) {
429 return PyErr_NoMemory();
431 op
= (arrayobject
*) type
->tp_alloc(type
, 0);
440 op
->ob_item
= PyMem_NEW(char, nbytes
);
441 if (op
->ob_item
== NULL
) {
443 return PyErr_NoMemory();
446 op
->ob_descr
= descr
;
447 op
->allocated
= size
;
448 op
->weakreflist
= NULL
;
449 return (PyObject
*) op
;
453 getarrayitem(PyObject
*op
, Py_ssize_t i
)
455 register arrayobject
*ap
;
456 assert(array_Check(op
));
457 ap
= (arrayobject
*)op
;
458 assert(i
>=0 && i
<ap
->ob_size
);
459 return (*ap
->ob_descr
->getitem
)(ap
, i
);
463 ins1(arrayobject
*self
, Py_ssize_t where
, PyObject
*v
)
466 Py_ssize_t n
= self
->ob_size
;
468 PyErr_BadInternalCall();
471 if ((*self
->ob_descr
->setitem
)(self
, -1, v
) < 0)
474 if (array_resize(self
, n
+1) == -1)
476 items
= self
->ob_item
;
484 /* appends don't need to call memmove() */
486 memmove(items
+ (where
+1)*self
->ob_descr
->itemsize
,
487 items
+ where
*self
->ob_descr
->itemsize
,
488 (n
-where
)*self
->ob_descr
->itemsize
);
489 return (*self
->ob_descr
->setitem
)(self
, where
, v
);
495 array_dealloc(arrayobject
*op
)
497 if (op
->weakreflist
!= NULL
)
498 PyObject_ClearWeakRefs((PyObject
*) op
);
499 if (op
->ob_item
!= NULL
)
500 PyMem_DEL(op
->ob_item
);
501 op
->ob_type
->tp_free((PyObject
*)op
);
505 array_richcompare(PyObject
*v
, PyObject
*w
, int op
)
507 arrayobject
*va
, *wa
;
513 if (!array_Check(v
) || !array_Check(w
)) {
514 Py_INCREF(Py_NotImplemented
);
515 return Py_NotImplemented
;
518 va
= (arrayobject
*)v
;
519 wa
= (arrayobject
*)w
;
521 if (va
->ob_size
!= wa
->ob_size
&& (op
== Py_EQ
|| op
== Py_NE
)) {
522 /* Shortcut: if the lengths differ, the arrays differ */
531 /* Search for the first index where items are different */
533 for (i
= 0; i
< va
->ob_size
&& i
< wa
->ob_size
; i
++) {
534 vi
= getarrayitem(v
, i
);
535 wi
= getarrayitem(w
, i
);
536 if (vi
== NULL
|| wi
== NULL
) {
541 k
= PyObject_RichCompareBool(vi
, wi
, Py_EQ
);
543 break; /* Keeping vi and wi alive! */
551 /* No more items to compare -- compare sizes */
552 Py_ssize_t vs
= va
->ob_size
;
553 Py_ssize_t ws
= wa
->ob_size
;
556 case Py_LT
: cmp
= vs
< ws
; break;
557 case Py_LE
: cmp
= vs
<= ws
; break;
558 case Py_EQ
: cmp
= vs
== ws
; break;
559 case Py_NE
: cmp
= vs
!= ws
; break;
560 case Py_GT
: cmp
= vs
> ws
; break;
561 case Py_GE
: cmp
= vs
>= ws
; break;
562 default: return NULL
; /* cannot happen */
572 /* We have an item that differs. First, shortcuts for EQ/NE */
577 else if (op
== Py_NE
) {
582 /* Compare the final item again using the proper operator */
583 res
= PyObject_RichCompare(vi
, wi
, op
);
591 array_length(arrayobject
*a
)
597 array_item(arrayobject
*a
, Py_ssize_t i
)
599 if (i
< 0 || i
>= a
->ob_size
) {
600 PyErr_SetString(PyExc_IndexError
, "array index out of range");
603 return getarrayitem((PyObject
*)a
, i
);
607 array_slice(arrayobject
*a
, Py_ssize_t ilow
, Py_ssize_t ihigh
)
612 else if (ilow
> a
->ob_size
)
618 else if (ihigh
> a
->ob_size
)
620 np
= (arrayobject
*) newarrayobject(&Arraytype
, ihigh
- ilow
, a
->ob_descr
);
623 memcpy(np
->ob_item
, a
->ob_item
+ ilow
* a
->ob_descr
->itemsize
,
624 (ihigh
-ilow
) * a
->ob_descr
->itemsize
);
625 return (PyObject
*)np
;
629 array_copy(arrayobject
*a
, PyObject
*unused
)
631 return array_slice(a
, 0, a
->ob_size
);
634 PyDoc_STRVAR(copy_doc
,
637 Return a copy of the array.");
640 array_concat(arrayobject
*a
, PyObject
*bb
)
644 if (!array_Check(bb
)) {
645 PyErr_Format(PyExc_TypeError
,
646 "can only append array (not \"%.200s\") to array",
647 bb
->ob_type
->tp_name
);
650 #define b ((arrayobject *)bb)
651 if (a
->ob_descr
!= b
->ob_descr
) {
655 size
= a
->ob_size
+ b
->ob_size
;
656 np
= (arrayobject
*) newarrayobject(&Arraytype
, size
, a
->ob_descr
);
660 memcpy(np
->ob_item
, a
->ob_item
, a
->ob_size
*a
->ob_descr
->itemsize
);
661 memcpy(np
->ob_item
+ a
->ob_size
*a
->ob_descr
->itemsize
,
662 b
->ob_item
, b
->ob_size
*b
->ob_descr
->itemsize
);
663 return (PyObject
*)np
;
668 array_repeat(arrayobject
*a
, Py_ssize_t n
)
677 size
= a
->ob_size
* n
;
678 np
= (arrayobject
*) newarrayobject(&Arraytype
, size
, a
->ob_descr
);
682 nbytes
= a
->ob_size
* a
->ob_descr
->itemsize
;
683 for (i
= 0; i
< n
; i
++) {
684 memcpy(p
, a
->ob_item
, nbytes
);
687 return (PyObject
*) np
;
691 array_ass_slice(arrayobject
*a
, Py_ssize_t ilow
, Py_ssize_t ihigh
, PyObject
*v
)
694 Py_ssize_t n
; /* Size of replacement array */
695 Py_ssize_t d
; /* Change in size */
696 #define b ((arrayobject *)v)
699 else if (array_Check(v
)) {
702 /* Special case "a[i:j] = a" -- copy b first */
704 v
= array_slice(b
, 0, n
);
707 ret
= array_ass_slice(a
, ilow
, ihigh
, v
);
711 if (b
->ob_descr
!= a
->ob_descr
) {
717 PyErr_Format(PyExc_TypeError
,
718 "can only assign array (not \"%.200s\") to array slice",
719 v
->ob_type
->tp_name
);
724 else if (ilow
> a
->ob_size
)
730 else if (ihigh
> a
->ob_size
)
733 d
= n
- (ihigh
-ilow
);
734 if (d
< 0) { /* Delete -d items */
735 memmove(item
+ (ihigh
+d
)*a
->ob_descr
->itemsize
,
736 item
+ ihigh
*a
->ob_descr
->itemsize
,
737 (a
->ob_size
-ihigh
)*a
->ob_descr
->itemsize
);
739 PyMem_RESIZE(item
, char, a
->ob_size
*a
->ob_descr
->itemsize
);
742 a
->allocated
= a
->ob_size
;
744 else if (d
> 0) { /* Insert d items */
745 PyMem_RESIZE(item
, char,
746 (a
->ob_size
+ d
)*a
->ob_descr
->itemsize
);
751 memmove(item
+ (ihigh
+d
)*a
->ob_descr
->itemsize
,
752 item
+ ihigh
*a
->ob_descr
->itemsize
,
753 (a
->ob_size
-ihigh
)*a
->ob_descr
->itemsize
);
756 a
->allocated
= a
->ob_size
;
759 memcpy(item
+ ilow
*a
->ob_descr
->itemsize
, b
->ob_item
,
760 n
*b
->ob_descr
->itemsize
);
766 array_ass_item(arrayobject
*a
, Py_ssize_t i
, PyObject
*v
)
768 if (i
< 0 || i
>= a
->ob_size
) {
769 PyErr_SetString(PyExc_IndexError
,
770 "array assignment index out of range");
774 return array_ass_slice(a
, i
, i
+1, v
);
775 return (*a
->ob_descr
->setitem
)(a
, i
, v
);
779 setarrayitem(PyObject
*a
, Py_ssize_t i
, PyObject
*v
)
781 assert(array_Check(a
));
782 return array_ass_item((arrayobject
*)a
, i
, v
);
786 array_iter_extend(arrayobject
*self
, PyObject
*bb
)
790 it
= PyObject_GetIter(bb
);
794 while ((v
= PyIter_Next(it
)) != NULL
) {
795 if (ins1(self
, (int) self
->ob_size
, v
) != 0) {
803 if (PyErr_Occurred())
809 array_do_extend(arrayobject
*self
, PyObject
*bb
)
813 if (!array_Check(bb
))
814 return array_iter_extend(self
, bb
);
815 #define b ((arrayobject *)bb)
816 if (self
->ob_descr
!= b
->ob_descr
) {
817 PyErr_SetString(PyExc_TypeError
,
818 "can only extend with array of same kind");
821 size
= self
->ob_size
+ b
->ob_size
;
822 PyMem_RESIZE(self
->ob_item
, char, size
*self
->ob_descr
->itemsize
);
823 if (self
->ob_item
== NULL
) {
828 memcpy(self
->ob_item
+ self
->ob_size
*self
->ob_descr
->itemsize
,
829 b
->ob_item
, b
->ob_size
*b
->ob_descr
->itemsize
);
830 self
->ob_size
= size
;
831 self
->allocated
= size
;
838 array_inplace_concat(arrayobject
*self
, PyObject
*bb
)
840 if (!array_Check(bb
)) {
841 PyErr_Format(PyExc_TypeError
,
842 "can only extend array with array (not \"%.200s\")",
843 bb
->ob_type
->tp_name
);
846 if (array_do_extend(self
, bb
) == -1)
849 return (PyObject
*)self
;
853 array_inplace_repeat(arrayobject
*self
, Py_ssize_t n
)
858 if (self
->ob_size
> 0) {
861 items
= self
->ob_item
;
862 size
= self
->ob_size
* self
->ob_descr
->itemsize
;
865 self
->ob_item
= NULL
;
870 PyMem_Resize(items
, char, n
* size
);
872 return PyErr_NoMemory();
874 for (i
= 1; i
< n
; i
++) {
876 memcpy(p
, items
, size
);
878 self
->ob_item
= items
;
880 self
->allocated
= self
->ob_size
;
884 return (PyObject
*)self
;
889 ins(arrayobject
*self
, Py_ssize_t where
, PyObject
*v
)
891 if (ins1(self
, where
, v
) != 0)
898 array_count(arrayobject
*self
, PyObject
*v
)
900 Py_ssize_t count
= 0;
903 for (i
= 0; i
< self
->ob_size
; i
++) {
904 PyObject
*selfi
= getarrayitem((PyObject
*)self
, i
);
905 int cmp
= PyObject_RichCompareBool(selfi
, v
, Py_EQ
);
912 return PyInt_FromSsize_t(count
);
915 PyDoc_STRVAR(count_doc
,
918 Return number of occurences of x in the array.");
921 array_index(arrayobject
*self
, PyObject
*v
)
925 for (i
= 0; i
< self
->ob_size
; i
++) {
926 PyObject
*selfi
= getarrayitem((PyObject
*)self
, i
);
927 int cmp
= PyObject_RichCompareBool(selfi
, v
, Py_EQ
);
930 return PyInt_FromLong((long)i
);
935 PyErr_SetString(PyExc_ValueError
, "array.index(x): x not in list");
939 PyDoc_STRVAR(index_doc
,
942 Return index of first occurence of x in the array.");
945 array_contains(arrayobject
*self
, PyObject
*v
)
950 for (i
= 0, cmp
= 0 ; cmp
== 0 && i
< self
->ob_size
; i
++) {
951 PyObject
*selfi
= getarrayitem((PyObject
*)self
, i
);
952 cmp
= PyObject_RichCompareBool(selfi
, v
, Py_EQ
);
959 array_remove(arrayobject
*self
, PyObject
*v
)
963 for (i
= 0; i
< self
->ob_size
; i
++) {
964 PyObject
*selfi
= getarrayitem((PyObject
*)self
,i
);
965 int cmp
= PyObject_RichCompareBool(selfi
, v
, Py_EQ
);
968 if (array_ass_slice(self
, i
, i
+1,
969 (PyObject
*)NULL
) != 0)
977 PyErr_SetString(PyExc_ValueError
, "array.remove(x): x not in list");
981 PyDoc_STRVAR(remove_doc
,
984 Remove the first occurence of x in the array.");
987 array_pop(arrayobject
*self
, PyObject
*args
)
991 if (!PyArg_ParseTuple(args
, "|n:pop", &i
))
993 if (self
->ob_size
== 0) {
994 /* Special-case most common failure cause */
995 PyErr_SetString(PyExc_IndexError
, "pop from empty array");
1000 if (i
< 0 || i
>= self
->ob_size
) {
1001 PyErr_SetString(PyExc_IndexError
, "pop index out of range");
1004 v
= getarrayitem((PyObject
*)self
,i
);
1005 if (array_ass_slice(self
, i
, i
+1, (PyObject
*)NULL
) != 0) {
1012 PyDoc_STRVAR(pop_doc
,
1015 Return the i-th element and delete it from the array. i defaults to -1.");
1018 array_extend(arrayobject
*self
, PyObject
*bb
)
1020 if (array_do_extend(self
, bb
) == -1)
1026 PyDoc_STRVAR(extend_doc
,
1027 "extend(array or iterable)\n\
1029 Append items to the end of the array.");
1032 array_insert(arrayobject
*self
, PyObject
*args
)
1036 if (!PyArg_ParseTuple(args
, "nO:insert", &i
, &v
))
1038 return ins(self
, i
, v
);
1041 PyDoc_STRVAR(insert_doc
,
1044 Insert a new item x into the array before position i.");
1048 array_buffer_info(arrayobject
*self
, PyObject
*unused
)
1050 PyObject
* retval
= NULL
;
1051 retval
= PyTuple_New(2);
1055 PyTuple_SET_ITEM(retval
, 0, PyLong_FromVoidPtr(self
->ob_item
));
1056 PyTuple_SET_ITEM(retval
, 1, PyInt_FromLong((long)(self
->ob_size
)));
1061 PyDoc_STRVAR(buffer_info_doc
,
1062 "buffer_info() -> (address, length)\n\
1064 Return a tuple (address, length) giving the current memory address and\n\
1065 the length in items of the buffer used to hold array's contents\n\
1066 The length should be multiplied by the itemsize attribute to calculate\n\
1067 the buffer length in bytes.");
1071 array_append(arrayobject
*self
, PyObject
*v
)
1073 return ins(self
, (int) self
->ob_size
, v
);
1076 PyDoc_STRVAR(append_doc
,
1079 Append new value x to the end of the array.");
1083 array_byteswap(arrayobject
*self
, PyObject
*unused
)
1088 switch (self
->ob_descr
->itemsize
) {
1092 for (p
= self
->ob_item
, i
= self
->ob_size
; --i
>= 0; p
+= 2) {
1099 for (p
= self
->ob_item
, i
= self
->ob_size
; --i
>= 0; p
+= 4) {
1109 for (p
= self
->ob_item
, i
= self
->ob_size
; --i
>= 0; p
+= 8) {
1125 PyErr_SetString(PyExc_RuntimeError
,
1126 "don't know how to byteswap this array type");
1133 PyDoc_STRVAR(byteswap_doc
,
1136 Byteswap all items of the array. If the items in the array are not 1, 2,\n\
1137 4, or 8 bytes in size, RuntimeError is raised.");
1140 array_reduce(arrayobject
*array
)
1142 PyObject
*dict
, *result
;
1144 dict
= PyObject_GetAttrString((PyObject
*)array
, "__dict__");
1150 result
= Py_BuildValue("O(cs#)O",
1152 array
->ob_descr
->typecode
,
1154 array
->ob_size
* array
->ob_descr
->itemsize
,
1160 PyDoc_STRVAR(array_doc
, "Return state information for pickling.");
1163 array_reverse(arrayobject
*self
, PyObject
*unused
)
1165 register Py_ssize_t itemsize
= self
->ob_descr
->itemsize
;
1166 register char *p
, *q
;
1167 /* little buffer to hold items while swapping */
1168 char tmp
[256]; /* 8 is probably enough -- but why skimp */
1169 assert((size_t)itemsize
<= sizeof(tmp
));
1171 if (self
->ob_size
> 1) {
1172 for (p
= self
->ob_item
,
1173 q
= self
->ob_item
+ (self
->ob_size
- 1)*itemsize
;
1175 p
+= itemsize
, q
-= itemsize
) {
1176 /* memory areas guaranteed disjoint, so memcpy
1177 * is safe (& memmove may be slower).
1179 memcpy(tmp
, p
, itemsize
);
1180 memcpy(p
, q
, itemsize
);
1181 memcpy(q
, tmp
, itemsize
);
1189 PyDoc_STRVAR(reverse_doc
,
1192 Reverse the order of the items in the array.");
1195 array_fromfile(arrayobject
*self
, PyObject
*args
)
1200 if (!PyArg_ParseTuple(args
, "On:fromfile", &f
, &n
))
1202 fp
= PyFile_AsFile(f
);
1204 PyErr_SetString(PyExc_TypeError
, "arg1 must be open file");
1208 char *item
= self
->ob_item
;
1209 Py_ssize_t itemsize
= self
->ob_descr
->itemsize
;
1211 Py_ssize_t newlength
;
1213 /* Be careful here about overflow */
1214 if ((newlength
= self
->ob_size
+ n
) <= 0 ||
1215 (newbytes
= newlength
* itemsize
) / itemsize
!=
1218 PyMem_RESIZE(item
, char, newbytes
);
1224 self
->ob_item
= item
;
1226 self
->allocated
= self
->ob_size
;
1227 nread
= fread(item
+ (self
->ob_size
- n
) * itemsize
,
1229 if (nread
< (size_t)n
) {
1230 self
->ob_size
-= (n
- nread
);
1231 PyMem_RESIZE(item
, char, self
->ob_size
*itemsize
);
1232 self
->ob_item
= item
;
1233 self
->allocated
= self
->ob_size
;
1234 PyErr_SetString(PyExc_EOFError
,
1235 "not enough items in file");
1243 PyDoc_STRVAR(fromfile_doc
,
1246 Read n objects from the file object f and append them to the end of the\n\
1247 array. Also called as read.");
1251 array_tofile(arrayobject
*self
, PyObject
*f
)
1255 fp
= PyFile_AsFile(f
);
1257 PyErr_SetString(PyExc_TypeError
, "arg must be open file");
1260 if (self
->ob_size
> 0) {
1261 if (fwrite(self
->ob_item
, self
->ob_descr
->itemsize
,
1262 self
->ob_size
, fp
) != (size_t)self
->ob_size
) {
1263 PyErr_SetFromErrno(PyExc_IOError
);
1272 PyDoc_STRVAR(tofile_doc
,
1275 Write all items (as machine values) to the file object f. Also called as\n\
1280 array_fromlist(arrayobject
*self
, PyObject
*list
)
1283 Py_ssize_t itemsize
= self
->ob_descr
->itemsize
;
1285 if (!PyList_Check(list
)) {
1286 PyErr_SetString(PyExc_TypeError
, "arg must be list");
1289 n
= PyList_Size(list
);
1291 char *item
= self
->ob_item
;
1293 PyMem_RESIZE(item
, char, (self
->ob_size
+ n
) * itemsize
);
1298 self
->ob_item
= item
;
1300 self
->allocated
= self
->ob_size
;
1301 for (i
= 0; i
< n
; i
++) {
1302 PyObject
*v
= PyList_GetItem(list
, i
);
1303 if ((*self
->ob_descr
->setitem
)(self
,
1304 self
->ob_size
- n
+ i
, v
) != 0) {
1306 PyMem_RESIZE(item
, char,
1307 self
->ob_size
* itemsize
);
1308 self
->ob_item
= item
;
1309 self
->allocated
= self
->ob_size
;
1318 PyDoc_STRVAR(fromlist_doc
,
1321 Append items to array from list.");
1325 array_tolist(arrayobject
*self
, PyObject
*unused
)
1327 PyObject
*list
= PyList_New(self
->ob_size
);
1332 for (i
= 0; i
< self
->ob_size
; i
++) {
1333 PyObject
*v
= getarrayitem((PyObject
*)self
, i
);
1338 PyList_SetItem(list
, i
, v
);
1343 PyDoc_STRVAR(tolist_doc
,
1344 "tolist() -> list\n\
1346 Convert array to an ordinary list with the same items.");
1350 array_fromstring(arrayobject
*self
, PyObject
*args
)
1354 int itemsize
= self
->ob_descr
->itemsize
;
1355 if (!PyArg_ParseTuple(args
, "s#:fromstring", &str
, &n
))
1357 if (n
% itemsize
!= 0) {
1358 PyErr_SetString(PyExc_ValueError
,
1359 "string length not a multiple of item size");
1364 char *item
= self
->ob_item
;
1365 PyMem_RESIZE(item
, char, (self
->ob_size
+ n
) * itemsize
);
1370 self
->ob_item
= item
;
1372 self
->allocated
= self
->ob_size
;
1373 memcpy(item
+ (self
->ob_size
- n
) * itemsize
,
1380 PyDoc_STRVAR(fromstring_doc
,
1381 "fromstring(string)\n\
1383 Appends items from the string, interpreting it as an array of machine\n\
1384 values,as if it had been read from a file using the fromfile() method).");
1388 array_tostring(arrayobject
*self
, PyObject
*unused
)
1390 return PyString_FromStringAndSize(self
->ob_item
,
1391 self
->ob_size
* self
->ob_descr
->itemsize
);
1394 PyDoc_STRVAR(tostring_doc
,
1395 "tostring() -> string\n\
1397 Convert the array to an array of machine values and return the string\n\
1402 #ifdef Py_USING_UNICODE
1404 array_fromunicode(arrayobject
*self
, PyObject
*args
)
1409 if (!PyArg_ParseTuple(args
, "u#:fromunicode", &ustr
, &n
))
1411 if (self
->ob_descr
->typecode
!= 'u') {
1412 PyErr_SetString(PyExc_ValueError
,
1413 "fromunicode() may only be called on "
1418 Py_UNICODE
*item
= (Py_UNICODE
*) self
->ob_item
;
1419 PyMem_RESIZE(item
, Py_UNICODE
, self
->ob_size
+ n
);
1424 self
->ob_item
= (char *) item
;
1426 self
->allocated
= self
->ob_size
;
1427 memcpy(item
+ self
->ob_size
- n
,
1428 ustr
, n
* sizeof(Py_UNICODE
));
1435 PyDoc_STRVAR(fromunicode_doc
,
1436 "fromunicode(ustr)\n\
1438 Extends this array with data from the unicode string ustr.\n\
1439 The array must be a type 'u' array; otherwise a ValueError\n\
1440 is raised. Use array.fromstring(ustr.decode(...)) to\n\
1441 append Unicode data to an array of some other type.");
1445 array_tounicode(arrayobject
*self
, PyObject
*unused
)
1447 if (self
->ob_descr
->typecode
!= 'u') {
1448 PyErr_SetString(PyExc_ValueError
,
1449 "tounicode() may only be called on type 'u' arrays");
1452 return PyUnicode_FromUnicode((Py_UNICODE
*) self
->ob_item
, self
->ob_size
);
1455 PyDoc_STRVAR(tounicode_doc
,
1456 "tounicode() -> unicode\n\
1458 Convert the array to a unicode string. The array must be\n\
1459 a type 'u' array; otherwise a ValueError is raised. Use\n\
1460 array.tostring().decode() to obtain a unicode string from\n\
1461 an array of some other type.");
1463 #endif /* Py_USING_UNICODE */
1467 array_get_typecode(arrayobject
*a
, void *closure
)
1469 char tc
= a
->ob_descr
->typecode
;
1470 return PyString_FromStringAndSize(&tc
, 1);
1474 array_get_itemsize(arrayobject
*a
, void *closure
)
1476 return PyInt_FromLong((long)a
->ob_descr
->itemsize
);
1479 static PyGetSetDef array_getsets
[] = {
1480 {"typecode", (getter
) array_get_typecode
, NULL
,
1481 "the typecode character used to create the array"},
1482 {"itemsize", (getter
) array_get_itemsize
, NULL
,
1483 "the size, in bytes, of one array item"},
1487 PyMethodDef array_methods
[] = {
1488 {"append", (PyCFunction
)array_append
, METH_O
,
1490 {"buffer_info", (PyCFunction
)array_buffer_info
, METH_NOARGS
,
1492 {"byteswap", (PyCFunction
)array_byteswap
, METH_NOARGS
,
1494 {"__copy__", (PyCFunction
)array_copy
, METH_NOARGS
,
1496 {"count", (PyCFunction
)array_count
, METH_O
,
1498 {"__deepcopy__",(PyCFunction
)array_copy
, METH_O
,
1500 {"extend", (PyCFunction
)array_extend
, METH_O
,
1502 {"fromfile", (PyCFunction
)array_fromfile
, METH_VARARGS
,
1504 {"fromlist", (PyCFunction
)array_fromlist
, METH_O
,
1506 {"fromstring", (PyCFunction
)array_fromstring
, METH_VARARGS
,
1508 #ifdef Py_USING_UNICODE
1509 {"fromunicode", (PyCFunction
)array_fromunicode
, METH_VARARGS
,
1512 {"index", (PyCFunction
)array_index
, METH_O
,
1514 {"insert", (PyCFunction
)array_insert
, METH_VARARGS
,
1516 {"pop", (PyCFunction
)array_pop
, METH_VARARGS
,
1518 {"read", (PyCFunction
)array_fromfile
, METH_VARARGS
,
1520 {"__reduce__", (PyCFunction
)array_reduce
, METH_NOARGS
,
1522 {"remove", (PyCFunction
)array_remove
, METH_O
,
1524 {"reverse", (PyCFunction
)array_reverse
, METH_NOARGS
,
1526 /* {"sort", (PyCFunction)array_sort, METH_VARARGS,
1528 {"tofile", (PyCFunction
)array_tofile
, METH_O
,
1530 {"tolist", (PyCFunction
)array_tolist
, METH_NOARGS
,
1532 {"tostring", (PyCFunction
)array_tostring
, METH_NOARGS
,
1534 #ifdef Py_USING_UNICODE
1535 {"tounicode", (PyCFunction
)array_tounicode
, METH_NOARGS
,
1538 {"write", (PyCFunction
)array_tofile
, METH_O
,
1540 {NULL
, NULL
} /* sentinel */
1544 array_repr(arrayobject
*a
)
1546 char buf
[256], typecode
;
1547 PyObject
*s
, *t
, *v
= NULL
;
1551 typecode
= a
->ob_descr
->typecode
;
1553 PyOS_snprintf(buf
, sizeof(buf
), "array('%c')", typecode
);
1554 return PyString_FromString(buf
);
1557 if (typecode
== 'c')
1558 v
= array_tostring(a
, NULL
);
1559 #ifdef Py_USING_UNICODE
1560 else if (typecode
== 'u')
1561 v
= array_tounicode(a
, NULL
);
1564 v
= array_tolist(a
, NULL
);
1565 t
= PyObject_Repr(v
);
1568 PyOS_snprintf(buf
, sizeof(buf
), "array('%c', ", typecode
);
1569 s
= PyString_FromString(buf
);
1570 PyString_ConcatAndDel(&s
, t
);
1571 PyString_ConcatAndDel(&s
, PyString_FromString(")"));
1576 array_subscr(arrayobject
* self
, PyObject
* item
)
1578 if (PyIndex_Check(item
)) {
1579 Py_ssize_t i
= PyNumber_AsSsize_t(item
, PyExc_IndexError
);
1580 if (i
==-1 && PyErr_Occurred()) {
1585 return array_item(self
, i
);
1587 else if (PySlice_Check(item
)) {
1588 Py_ssize_t start
, stop
, step
, slicelength
, cur
, i
;
1591 int itemsize
= self
->ob_descr
->itemsize
;
1593 if (PySlice_GetIndicesEx((PySliceObject
*)item
, self
->ob_size
,
1594 &start
, &stop
, &step
, &slicelength
) < 0) {
1598 if (slicelength
<= 0) {
1599 return newarrayobject(&Arraytype
, 0, self
->ob_descr
);
1602 result
= newarrayobject(&Arraytype
, slicelength
, self
->ob_descr
);
1603 if (!result
) return NULL
;
1605 ar
= (arrayobject
*)result
;
1607 for (cur
= start
, i
= 0; i
< slicelength
;
1609 memcpy(ar
->ob_item
+ i
*itemsize
,
1610 self
->ob_item
+ cur
*itemsize
,
1618 PyErr_SetString(PyExc_TypeError
,
1619 "list indices must be integers");
1625 array_ass_subscr(arrayobject
* self
, PyObject
* item
, PyObject
* value
)
1627 if (PyIndex_Check(item
)) {
1628 Py_ssize_t i
= PyNumber_AsSsize_t(item
, PyExc_IndexError
);
1629 if (i
==-1 && PyErr_Occurred())
1633 return array_ass_item(self
, i
, value
);
1635 else if (PySlice_Check(item
)) {
1636 Py_ssize_t start
, stop
, step
, slicelength
;
1637 int itemsize
= self
->ob_descr
->itemsize
;
1639 if (PySlice_GetIndicesEx((PySliceObject
*)item
, self
->ob_size
,
1640 &start
, &stop
, &step
, &slicelength
) < 0) {
1644 /* treat A[slice(a,b)] = v _exactly_ like A[a:b] = v */
1645 if (step
== 1 && ((PySliceObject
*)item
)->step
== Py_None
)
1646 return array_ass_slice(self
, start
, stop
, value
);
1648 if (value
== NULL
) {
1650 Py_ssize_t cur
, i
, extra
;
1652 if (slicelength
<= 0)
1657 start
= stop
+ step
*(slicelength
- 1) - 1;
1661 for (cur
= start
, i
= 0; i
< slicelength
- 1;
1663 memmove(self
->ob_item
+ (cur
- i
)*itemsize
,
1664 self
->ob_item
+ (cur
+ 1)*itemsize
,
1665 (step
- 1) * itemsize
);
1667 extra
= self
->ob_size
- (cur
+ 1);
1669 memmove(self
->ob_item
+ (cur
- i
)*itemsize
,
1670 self
->ob_item
+ (cur
+ 1)*itemsize
,
1674 self
->ob_size
-= slicelength
;
1675 self
->ob_item
= (char *)PyMem_REALLOC(self
->ob_item
,
1676 itemsize
*self
->ob_size
);
1677 self
->allocated
= self
->ob_size
;
1686 if (!array_Check(value
)) {
1687 PyErr_Format(PyExc_TypeError
,
1688 "must assign array (not \"%.200s\") to slice",
1689 value
->ob_type
->tp_name
);
1693 av
= (arrayobject
*)value
;
1695 if (av
->ob_size
!= slicelength
) {
1696 PyErr_Format(PyExc_ValueError
,
1697 "attempt to assign array of size %ld to extended slice of size %ld",
1698 /*XXX*/(long)av
->ob_size
, /*XXX*/(long)slicelength
);
1705 /* protect against a[::-1] = a */
1707 value
= array_slice(av
, 0, av
->ob_size
);
1708 av
= (arrayobject
*)value
;
1716 for (cur
= start
, i
= 0; i
< slicelength
;
1718 memcpy(self
->ob_item
+ cur
*itemsize
,
1719 av
->ob_item
+ i
*itemsize
,
1729 PyErr_SetString(PyExc_TypeError
,
1730 "list indices must be integers");
1735 static PyMappingMethods array_as_mapping
= {
1736 (lenfunc
)array_length
,
1737 (binaryfunc
)array_subscr
,
1738 (objobjargproc
)array_ass_subscr
1742 array_buffer_getreadbuf(arrayobject
*self
, Py_ssize_t index
, const void **ptr
)
1745 PyErr_SetString(PyExc_SystemError
,
1746 "Accessing non-existent array segment");
1749 *ptr
= (void *)self
->ob_item
;
1750 return self
->ob_size
*self
->ob_descr
->itemsize
;
1754 array_buffer_getwritebuf(arrayobject
*self
, Py_ssize_t index
, const void **ptr
)
1757 PyErr_SetString(PyExc_SystemError
,
1758 "Accessing non-existent array segment");
1761 *ptr
= (void *)self
->ob_item
;
1762 return self
->ob_size
*self
->ob_descr
->itemsize
;
1766 array_buffer_getsegcount(arrayobject
*self
, Py_ssize_t
*lenp
)
1769 *lenp
= self
->ob_size
*self
->ob_descr
->itemsize
;
1773 static PySequenceMethods array_as_sequence
= {
1774 (lenfunc
)array_length
, /*sq_length*/
1775 (binaryfunc
)array_concat
, /*sq_concat*/
1776 (ssizeargfunc
)array_repeat
, /*sq_repeat*/
1777 (ssizeargfunc
)array_item
, /*sq_item*/
1778 (ssizessizeargfunc
)array_slice
, /*sq_slice*/
1779 (ssizeobjargproc
)array_ass_item
, /*sq_ass_item*/
1780 (ssizessizeobjargproc
)array_ass_slice
, /*sq_ass_slice*/
1781 (objobjproc
)array_contains
, /*sq_contains*/
1782 (binaryfunc
)array_inplace_concat
, /*sq_inplace_concat*/
1783 (ssizeargfunc
)array_inplace_repeat
/*sq_inplace_repeat*/
1786 static PyBufferProcs array_as_buffer
= {
1787 (readbufferproc
)array_buffer_getreadbuf
,
1788 (writebufferproc
)array_buffer_getwritebuf
,
1789 (segcountproc
)array_buffer_getsegcount
,
1794 array_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1797 PyObject
*initial
= NULL
, *it
= NULL
;
1798 struct arraydescr
*descr
;
1800 if (type
== &Arraytype
&& !_PyArg_NoKeywords("array.array()", kwds
))
1803 if (!PyArg_ParseTuple(args
, "c|O:array", &c
, &initial
))
1806 if (!(initial
== NULL
|| PyList_Check(initial
)
1807 || PyString_Check(initial
) || PyTuple_Check(initial
)
1808 || (c
== 'u' && PyUnicode_Check(initial
)))) {
1809 it
= PyObject_GetIter(initial
);
1812 /* We set initial to NULL so that the subsequent code
1813 will create an empty array of the appropriate type
1814 and afterwards we can use array_iter_extend to populate
1819 for (descr
= descriptors
; descr
->typecode
!= '\0'; descr
++) {
1820 if (descr
->typecode
== c
) {
1824 if (initial
== NULL
|| !(PyList_Check(initial
)
1825 || PyTuple_Check(initial
)))
1828 len
= PySequence_Size(initial
);
1830 a
= newarrayobject(type
, len
, descr
);
1836 for (i
= 0; i
< len
; i
++) {
1838 PySequence_GetItem(initial
, i
);
1843 if (setarrayitem(a
, i
, v
) != 0) {
1850 } else if (initial
!= NULL
&& PyString_Check(initial
)) {
1851 PyObject
*t_initial
, *v
;
1852 t_initial
= PyTuple_Pack(1, initial
);
1853 if (t_initial
== NULL
) {
1857 v
= array_fromstring((arrayobject
*)a
,
1859 Py_DECREF(t_initial
);
1865 #ifdef Py_USING_UNICODE
1866 } else if (initial
!= NULL
&& PyUnicode_Check(initial
)) {
1867 Py_ssize_t n
= PyUnicode_GET_DATA_SIZE(initial
);
1869 arrayobject
*self
= (arrayobject
*)a
;
1870 char *item
= self
->ob_item
;
1871 item
= (char *)PyMem_Realloc(item
, n
);
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
;
1885 if (array_iter_extend((arrayobject
*)a
, it
) == -1) {
1895 PyErr_SetString(PyExc_ValueError
,
1896 "bad typecode (must be c, b, B, u, h, H, i, I, l, L, f or d)");
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\
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\
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\
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
)
1971 sizeof(arrayobject
),
1973 (destructor
)array_dealloc
, /* tp_dealloc */
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*/
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 */
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 */
1998 array_getsets
, /* tp_getset */
2001 0, /* tp_descr_get */
2002 0, /* tp_descr_set */
2003 0, /* tp_dictoffset */
2005 PyType_GenericAlloc
, /* tp_alloc */
2006 array_new
, /* tp_new */
2007 PyObject_Del
, /* tp_free */
2011 /*********************** Array Iterator **************************/
2017 PyObject
* (*getitem
)(struct arrayobject
*, Py_ssize_t
);
2020 static PyTypeObject PyArrayIter_Type
;
2022 #define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
2025 array_iter(arrayobject
*ao
)
2027 arrayiterobject
*it
;
2029 if (!array_Check(ao
)) {
2030 PyErr_BadInternalCall();
2034 it
= PyObject_GC_New(arrayiterobject
, &PyArrayIter_Type
);
2041 it
->getitem
= ao
->ob_descr
->getitem
;
2042 PyObject_GC_Track(it
);
2043 return (PyObject
*)it
;
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
++);
2056 arrayiter_dealloc(arrayiterobject
*it
)
2058 PyObject_GC_UnTrack(it
);
2060 PyObject_GC_Del(it
);
2064 arrayiter_traverse(arrayiterobject
*it
, visitproc visit
, void *arg
)
2070 static PyTypeObject PyArrayIter_Type
= {
2071 PyObject_HEAD_INIT(NULL
)
2073 "arrayiterator", /* tp_name */
2074 sizeof(arrayiterobject
), /* tp_basicsize */
2075 0, /* tp_itemsize */
2077 (destructor
)arrayiter_dealloc
, /* tp_dealloc */
2083 0, /* tp_as_number */
2084 0, /* tp_as_sequence */
2085 0, /* tp_as_mapping */
2089 PyObject_GenericGetAttr
, /* tp_getattro */
2090 0, /* tp_setattro */
2091 0, /* tp_as_buffer */
2092 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
,/* tp_flags */
2094 (traverseproc
)arrayiter_traverse
, /* tp_traverse */
2096 0, /* tp_richcompare */
2097 0, /* tp_weaklistoffset */
2098 PyObject_SelfIter
, /* tp_iter */
2099 (iternextfunc
)arrayiter_next
, /* tp_iternext */
2104 /*********************** Install Module **************************/
2106 /* No functions in array module. */
2107 static PyMethodDef a_methods
[] = {
2108 {NULL
, NULL
, 0, NULL
} /* Sentinel */
2117 Arraytype
.ob_type
= &PyType_Type
;
2118 PyArrayIter_Type
.ob_type
= &PyType_Type
;
2119 m
= Py_InitModule3("array", a_methods
, module_doc
);
2123 Py_INCREF((PyObject
*)&Arraytype
);
2124 PyModule_AddObject(m
, "ArrayType", (PyObject
*)&Arraytype
);
2125 Py_INCREF((PyObject
*)&Arraytype
);
2126 PyModule_AddObject(m
, "array", (PyObject
*)&Arraytype
);
2127 /* No need to check the error here, the caller will do that */