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
*);
32 typedef struct arrayobject
{
36 struct arraydescr
*ob_descr
;
37 PyObject
*weakreflist
; /* List of weak references */
38 int ob_exports
; /* Number of exported buffers */
41 static PyTypeObject Arraytype
;
43 #define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
44 #define array_CheckExact(op) (Py_TYPE(op) == &Arraytype)
47 array_resize(arrayobject
*self
, Py_ssize_t newsize
)
52 /* Bypass realloc() when a previous overallocation is large enough
53 to accommodate the newsize. If the newsize is 16 smaller than the
54 current size, then proceed with the realloc() to shrink the list.
57 if (self
->allocated
>= newsize
&&
58 Py_SIZE(self
) < newsize
+ 16 &&
59 self
->ob_item
!= NULL
) {
60 Py_SIZE(self
) = newsize
;
64 if (self
->ob_exports
> 0) {
65 PyErr_SetString(PyExc_BufferError
,
66 "cannot resize an array that is exporting data");
70 /* This over-allocates proportional to the array size, making room
71 * for additional growth. The over-allocation is mild, but is
72 * enough to give linear-time amortized behavior over a long
73 * sequence of appends() in the presence of a poorly-performing
75 * The growth pattern is: 0, 4, 8, 16, 25, 34, 46, 56, 67, 79, ...
76 * Note, the pattern starts out the same as for lists but then
77 * grows at a smaller rate so that larger arrays only overallocate
78 * by about 1/16th -- this is done because arrays are presumed to be more
82 _new_size
= (newsize
>> 4) + (Py_SIZE(self
) < 8 ? 3 : 7) + newsize
;
83 items
= self
->ob_item
;
84 /* XXX The following multiplication and division does not optimize away
85 like it does for lists since the size is not known at compile time */
86 if (_new_size
<= ((~(size_t)0) / self
->ob_descr
->itemsize
))
87 PyMem_RESIZE(items
, char, (_new_size
* self
->ob_descr
->itemsize
));
94 self
->ob_item
= items
;
95 Py_SIZE(self
) = newsize
;
96 self
->allocated
= _new_size
;
100 /****************************************************************************
101 Get and Set functions for each type.
102 A Get function takes an arrayobject* and an integer index, returning the
103 array value at that index wrapped in an appropriate PyObject*.
104 A Set function takes an arrayobject, integer index, and PyObject*; sets
105 the array value at that index to the raw C data extracted from the PyObject*,
106 and returns 0 if successful, else nonzero on failure (PyObject* not of an
107 appropriate type or value).
108 Note that the basic Get and Set functions do NOT check that the index is
109 in bounds; that's the responsibility of the caller.
110 ****************************************************************************/
113 b_getitem(arrayobject
*ap
, Py_ssize_t i
)
115 long x
= ((char *)ap
->ob_item
)[i
];
118 return PyLong_FromLong(x
);
122 b_setitem(arrayobject
*ap
, Py_ssize_t i
, PyObject
*v
)
125 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
126 must use the next size up that is signed ('h') and manually do
127 the overflow checking */
128 if (!PyArg_Parse(v
, "h;array item must be integer", &x
))
131 PyErr_SetString(PyExc_OverflowError
,
132 "signed char is less than minimum");
136 PyErr_SetString(PyExc_OverflowError
,
137 "signed char is greater than maximum");
141 ((char *)ap
->ob_item
)[i
] = (char)x
;
146 BB_getitem(arrayobject
*ap
, Py_ssize_t i
)
148 long x
= ((unsigned char *)ap
->ob_item
)[i
];
149 return PyLong_FromLong(x
);
153 BB_setitem(arrayobject
*ap
, Py_ssize_t i
, PyObject
*v
)
156 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
157 if (!PyArg_Parse(v
, "b;array item must be integer", &x
))
160 ((char *)ap
->ob_item
)[i
] = x
;
165 u_getitem(arrayobject
*ap
, Py_ssize_t i
)
167 return PyUnicode_FromUnicode(&((Py_UNICODE
*) ap
->ob_item
)[i
], 1);
171 u_setitem(arrayobject
*ap
, Py_ssize_t i
, PyObject
*v
)
176 if (!PyArg_Parse(v
, "u#;array item must be unicode character", &p
, &len
))
179 PyErr_SetString(PyExc_TypeError
,
180 "array item must be unicode character");
184 ((Py_UNICODE
*)ap
->ob_item
)[i
] = p
[0];
190 h_getitem(arrayobject
*ap
, Py_ssize_t i
)
192 return PyLong_FromLong((long) ((short *)ap
->ob_item
)[i
]);
197 h_setitem(arrayobject
*ap
, Py_ssize_t i
, PyObject
*v
)
200 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
201 if (!PyArg_Parse(v
, "h;array item must be integer", &x
))
204 ((short *)ap
->ob_item
)[i
] = x
;
209 HH_getitem(arrayobject
*ap
, Py_ssize_t i
)
211 return PyLong_FromLong((long) ((unsigned short *)ap
->ob_item
)[i
]);
215 HH_setitem(arrayobject
*ap
, Py_ssize_t i
, PyObject
*v
)
218 /* PyArg_Parse's 'h' formatter is for a signed short, therefore
219 must use the next size up and manually do the overflow checking */
220 if (!PyArg_Parse(v
, "i;array item must be integer", &x
))
223 PyErr_SetString(PyExc_OverflowError
,
224 "unsigned short is less than minimum");
227 else if (x
> USHRT_MAX
) {
228 PyErr_SetString(PyExc_OverflowError
,
229 "unsigned short is greater than maximum");
233 ((short *)ap
->ob_item
)[i
] = (short)x
;
238 i_getitem(arrayobject
*ap
, Py_ssize_t i
)
240 return PyLong_FromLong((long) ((int *)ap
->ob_item
)[i
]);
244 i_setitem(arrayobject
*ap
, Py_ssize_t i
, PyObject
*v
)
247 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
248 if (!PyArg_Parse(v
, "i;array item must be integer", &x
))
251 ((int *)ap
->ob_item
)[i
] = x
;
256 II_getitem(arrayobject
*ap
, Py_ssize_t i
)
258 return PyLong_FromUnsignedLong(
259 (unsigned long) ((unsigned int *)ap
->ob_item
)[i
]);
263 II_setitem(arrayobject
*ap
, Py_ssize_t i
, PyObject
*v
)
266 if (PyLong_Check(v
)) {
267 x
= PyLong_AsUnsignedLong(v
);
268 if (x
== (unsigned long) -1 && PyErr_Occurred())
273 if (!PyArg_Parse(v
, "l;array item must be integer", &y
))
276 PyErr_SetString(PyExc_OverflowError
,
277 "unsigned int is less than minimum");
280 x
= (unsigned long)y
;
284 PyErr_SetString(PyExc_OverflowError
,
285 "unsigned int is greater than maximum");
290 ((unsigned int *)ap
->ob_item
)[i
] = (unsigned int)x
;
295 l_getitem(arrayobject
*ap
, Py_ssize_t i
)
297 return PyLong_FromLong(((long *)ap
->ob_item
)[i
]);
301 l_setitem(arrayobject
*ap
, Py_ssize_t i
, PyObject
*v
)
304 if (!PyArg_Parse(v
, "l;array item must be integer", &x
))
307 ((long *)ap
->ob_item
)[i
] = x
;
312 LL_getitem(arrayobject
*ap
, Py_ssize_t i
)
314 return PyLong_FromUnsignedLong(((unsigned long *)ap
->ob_item
)[i
]);
318 LL_setitem(arrayobject
*ap
, Py_ssize_t i
, PyObject
*v
)
321 if (PyLong_Check(v
)) {
322 x
= PyLong_AsUnsignedLong(v
);
323 if (x
== (unsigned long) -1 && PyErr_Occurred())
328 if (!PyArg_Parse(v
, "l;array item must be integer", &y
))
331 PyErr_SetString(PyExc_OverflowError
,
332 "unsigned long is less than minimum");
335 x
= (unsigned long)y
;
339 PyErr_SetString(PyExc_OverflowError
,
340 "unsigned long is greater than maximum");
345 ((unsigned long *)ap
->ob_item
)[i
] = x
;
350 f_getitem(arrayobject
*ap
, Py_ssize_t i
)
352 return PyFloat_FromDouble((double) ((float *)ap
->ob_item
)[i
]);
356 f_setitem(arrayobject
*ap
, Py_ssize_t i
, PyObject
*v
)
359 if (!PyArg_Parse(v
, "f;array item must be float", &x
))
362 ((float *)ap
->ob_item
)[i
] = x
;
367 d_getitem(arrayobject
*ap
, Py_ssize_t i
)
369 return PyFloat_FromDouble(((double *)ap
->ob_item
)[i
]);
373 d_setitem(arrayobject
*ap
, Py_ssize_t i
, PyObject
*v
)
376 if (!PyArg_Parse(v
, "d;array item must be float", &x
))
379 ((double *)ap
->ob_item
)[i
] = x
;
384 /* Description of types */
385 static struct arraydescr descriptors
[] = {
386 {'b', 1, b_getitem
, b_setitem
, "b"},
387 {'B', 1, BB_getitem
, BB_setitem
, "B"},
388 {'u', sizeof(Py_UNICODE
), u_getitem
, u_setitem
, "u"},
389 {'h', sizeof(short), h_getitem
, h_setitem
, "h"},
390 {'H', sizeof(short), HH_getitem
, HH_setitem
, "H"},
391 {'i', sizeof(int), i_getitem
, i_setitem
, "i"},
392 {'I', sizeof(int), II_getitem
, II_setitem
, "I"},
393 {'l', sizeof(long), l_getitem
, l_setitem
, "l"},
394 {'L', sizeof(long), LL_getitem
, LL_setitem
, "L"},
395 {'f', sizeof(float), f_getitem
, f_setitem
, "f"},
396 {'d', sizeof(double), d_getitem
, d_setitem
, "d"},
397 {'\0', 0, 0, 0, 0} /* Sentinel */
400 /****************************************************************************
401 Implementations of array object methods.
402 ****************************************************************************/
405 newarrayobject(PyTypeObject
*type
, Py_ssize_t size
, struct arraydescr
*descr
)
411 PyErr_BadInternalCall();
415 nbytes
= size
* descr
->itemsize
;
416 /* Check for overflow */
417 if (nbytes
/ descr
->itemsize
!= (size_t)size
) {
418 return PyErr_NoMemory();
420 op
= (arrayobject
*) type
->tp_alloc(type
, 0);
429 op
->ob_item
= PyMem_NEW(char, nbytes
);
430 if (op
->ob_item
== NULL
) {
432 return PyErr_NoMemory();
435 op
->ob_descr
= descr
;
436 op
->allocated
= size
;
437 op
->weakreflist
= NULL
;
439 return (PyObject
*) op
;
443 getarrayitem(PyObject
*op
, Py_ssize_t i
)
445 register arrayobject
*ap
;
446 assert(array_Check(op
));
447 ap
= (arrayobject
*)op
;
448 assert(i
>=0 && i
<Py_SIZE(ap
));
449 return (*ap
->ob_descr
->getitem
)(ap
, i
);
453 ins1(arrayobject
*self
, Py_ssize_t where
, PyObject
*v
)
456 Py_ssize_t n
= Py_SIZE(self
);
458 PyErr_BadInternalCall();
461 if ((*self
->ob_descr
->setitem
)(self
, -1, v
) < 0)
464 if (array_resize(self
, n
+1) == -1)
466 items
= self
->ob_item
;
474 /* appends don't need to call memmove() */
476 memmove(items
+ (where
+1)*self
->ob_descr
->itemsize
,
477 items
+ where
*self
->ob_descr
->itemsize
,
478 (n
-where
)*self
->ob_descr
->itemsize
);
479 return (*self
->ob_descr
->setitem
)(self
, where
, v
);
485 array_dealloc(arrayobject
*op
)
487 if (op
->weakreflist
!= NULL
)
488 PyObject_ClearWeakRefs((PyObject
*) op
);
489 if (op
->ob_item
!= NULL
)
490 PyMem_DEL(op
->ob_item
);
491 Py_TYPE(op
)->tp_free((PyObject
*)op
);
495 array_richcompare(PyObject
*v
, PyObject
*w
, int op
)
497 arrayobject
*va
, *wa
;
503 if (!array_Check(v
) || !array_Check(w
)) {
504 Py_INCREF(Py_NotImplemented
);
505 return Py_NotImplemented
;
508 va
= (arrayobject
*)v
;
509 wa
= (arrayobject
*)w
;
511 if (Py_SIZE(va
) != Py_SIZE(wa
) && (op
== Py_EQ
|| op
== Py_NE
)) {
512 /* Shortcut: if the lengths differ, the arrays differ */
521 /* Search for the first index where items are different */
523 for (i
= 0; i
< Py_SIZE(va
) && i
< Py_SIZE(wa
); i
++) {
524 vi
= getarrayitem(v
, i
);
525 wi
= getarrayitem(w
, i
);
526 if (vi
== NULL
|| wi
== NULL
) {
531 k
= PyObject_RichCompareBool(vi
, wi
, Py_EQ
);
533 break; /* Keeping vi and wi alive! */
541 /* No more items to compare -- compare sizes */
542 Py_ssize_t vs
= Py_SIZE(va
);
543 Py_ssize_t ws
= Py_SIZE(wa
);
546 case Py_LT
: cmp
= vs
< ws
; break;
547 case Py_LE
: cmp
= vs
<= ws
; break;
548 case Py_EQ
: cmp
= vs
== ws
; break;
549 case Py_NE
: cmp
= vs
!= ws
; break;
550 case Py_GT
: cmp
= vs
> ws
; break;
551 case Py_GE
: cmp
= vs
>= ws
; break;
552 default: return NULL
; /* cannot happen */
562 /* We have an item that differs. First, shortcuts for EQ/NE */
567 else if (op
== Py_NE
) {
572 /* Compare the final item again using the proper operator */
573 res
= PyObject_RichCompare(vi
, wi
, op
);
581 array_length(arrayobject
*a
)
587 array_item(arrayobject
*a
, Py_ssize_t i
)
589 if (i
< 0 || i
>= Py_SIZE(a
)) {
590 PyErr_SetString(PyExc_IndexError
, "array index out of range");
593 return getarrayitem((PyObject
*)a
, i
);
597 array_slice(arrayobject
*a
, Py_ssize_t ilow
, Py_ssize_t ihigh
)
602 else if (ilow
> Py_SIZE(a
))
608 else if (ihigh
> Py_SIZE(a
))
610 np
= (arrayobject
*) newarrayobject(&Arraytype
, ihigh
- ilow
, a
->ob_descr
);
613 memcpy(np
->ob_item
, a
->ob_item
+ ilow
* a
->ob_descr
->itemsize
,
614 (ihigh
-ilow
) * a
->ob_descr
->itemsize
);
615 return (PyObject
*)np
;
619 array_copy(arrayobject
*a
, PyObject
*unused
)
621 return array_slice(a
, 0, Py_SIZE(a
));
624 PyDoc_STRVAR(copy_doc
,
627 Return a copy of the array.");
630 array_concat(arrayobject
*a
, PyObject
*bb
)
634 if (!array_Check(bb
)) {
635 PyErr_Format(PyExc_TypeError
,
636 "can only append array (not \"%.200s\") to array",
637 Py_TYPE(bb
)->tp_name
);
640 #define b ((arrayobject *)bb)
641 if (a
->ob_descr
!= b
->ob_descr
) {
645 size
= Py_SIZE(a
) + Py_SIZE(b
);
646 np
= (arrayobject
*) newarrayobject(&Arraytype
, size
, a
->ob_descr
);
650 memcpy(np
->ob_item
, a
->ob_item
, Py_SIZE(a
)*a
->ob_descr
->itemsize
);
651 memcpy(np
->ob_item
+ Py_SIZE(a
)*a
->ob_descr
->itemsize
,
652 b
->ob_item
, Py_SIZE(b
)*b
->ob_descr
->itemsize
);
653 return (PyObject
*)np
;
658 array_repeat(arrayobject
*a
, Py_ssize_t n
)
667 size
= Py_SIZE(a
) * n
;
668 np
= (arrayobject
*) newarrayobject(&Arraytype
, size
, a
->ob_descr
);
672 nbytes
= Py_SIZE(a
) * a
->ob_descr
->itemsize
;
673 for (i
= 0; i
< n
; i
++) {
674 memcpy(p
, a
->ob_item
, nbytes
);
677 return (PyObject
*) np
;
681 array_ass_slice(arrayobject
*a
, Py_ssize_t ilow
, Py_ssize_t ihigh
, PyObject
*v
)
684 Py_ssize_t n
; /* Size of replacement array */
685 Py_ssize_t d
; /* Change in size */
686 #define b ((arrayobject *)v)
689 else if (array_Check(v
)) {
692 /* Special case "a[i:j] = a" -- copy b first */
694 v
= array_slice(b
, 0, n
);
697 ret
= array_ass_slice(a
, ilow
, ihigh
, v
);
701 if (b
->ob_descr
!= a
->ob_descr
) {
707 PyErr_Format(PyExc_TypeError
,
708 "can only assign array (not \"%.200s\") to array slice",
709 Py_TYPE(v
)->tp_name
);
714 else if (ilow
> Py_SIZE(a
))
720 else if (ihigh
> Py_SIZE(a
))
723 d
= n
- (ihigh
-ilow
);
724 if (d
< 0) { /* Delete -d items */
725 memmove(item
+ (ihigh
+d
)*a
->ob_descr
->itemsize
,
726 item
+ ihigh
*a
->ob_descr
->itemsize
,
727 (Py_SIZE(a
)-ihigh
)*a
->ob_descr
->itemsize
);
729 PyMem_RESIZE(item
, char, Py_SIZE(a
)*a
->ob_descr
->itemsize
);
732 a
->allocated
= Py_SIZE(a
);
734 else if (d
> 0) { /* Insert d items */
735 PyMem_RESIZE(item
, char,
736 (Py_SIZE(a
) + d
)*a
->ob_descr
->itemsize
);
741 memmove(item
+ (ihigh
+d
)*a
->ob_descr
->itemsize
,
742 item
+ ihigh
*a
->ob_descr
->itemsize
,
743 (Py_SIZE(a
)-ihigh
)*a
->ob_descr
->itemsize
);
746 a
->allocated
= Py_SIZE(a
);
749 memcpy(item
+ ilow
*a
->ob_descr
->itemsize
, b
->ob_item
,
750 n
*b
->ob_descr
->itemsize
);
756 array_ass_item(arrayobject
*a
, Py_ssize_t i
, PyObject
*v
)
758 if (i
< 0 || i
>= Py_SIZE(a
)) {
759 PyErr_SetString(PyExc_IndexError
,
760 "array assignment index out of range");
764 return array_ass_slice(a
, i
, i
+1, v
);
765 return (*a
->ob_descr
->setitem
)(a
, i
, v
);
769 setarrayitem(PyObject
*a
, Py_ssize_t i
, PyObject
*v
)
771 assert(array_Check(a
));
772 return array_ass_item((arrayobject
*)a
, i
, v
);
776 array_iter_extend(arrayobject
*self
, PyObject
*bb
)
780 it
= PyObject_GetIter(bb
);
784 while ((v
= PyIter_Next(it
)) != NULL
) {
785 if (ins1(self
, (int) Py_SIZE(self
), v
) != 0) {
793 if (PyErr_Occurred())
799 array_do_extend(arrayobject
*self
, PyObject
*bb
)
803 if (!array_Check(bb
))
804 return array_iter_extend(self
, bb
);
805 #define b ((arrayobject *)bb)
806 if (self
->ob_descr
!= b
->ob_descr
) {
807 PyErr_SetString(PyExc_TypeError
,
808 "can only extend with array of same kind");
811 size
= Py_SIZE(self
) + Py_SIZE(b
);
812 PyMem_RESIZE(self
->ob_item
, char, size
*self
->ob_descr
->itemsize
);
813 if (self
->ob_item
== NULL
) {
818 memcpy(self
->ob_item
+ Py_SIZE(self
)*self
->ob_descr
->itemsize
,
819 b
->ob_item
, Py_SIZE(b
)*b
->ob_descr
->itemsize
);
820 Py_SIZE(self
) = size
;
821 self
->allocated
= size
;
828 array_inplace_concat(arrayobject
*self
, PyObject
*bb
)
830 if (!array_Check(bb
)) {
831 PyErr_Format(PyExc_TypeError
,
832 "can only extend array with array (not \"%.200s\")",
833 Py_TYPE(bb
)->tp_name
);
836 if (array_do_extend(self
, bb
) == -1)
839 return (PyObject
*)self
;
843 array_inplace_repeat(arrayobject
*self
, Py_ssize_t n
)
848 if (Py_SIZE(self
) > 0) {
851 items
= self
->ob_item
;
852 size
= Py_SIZE(self
) * self
->ob_descr
->itemsize
;
855 self
->ob_item
= NULL
;
860 PyMem_Resize(items
, char, n
* size
);
862 return PyErr_NoMemory();
864 for (i
= 1; i
< n
; i
++) {
866 memcpy(p
, items
, size
);
868 self
->ob_item
= items
;
870 self
->allocated
= Py_SIZE(self
);
874 return (PyObject
*)self
;
879 ins(arrayobject
*self
, Py_ssize_t where
, PyObject
*v
)
881 if (ins1(self
, where
, v
) != 0)
888 array_count(arrayobject
*self
, PyObject
*v
)
890 Py_ssize_t count
= 0;
893 for (i
= 0; i
< Py_SIZE(self
); i
++) {
894 PyObject
*selfi
= getarrayitem((PyObject
*)self
, i
);
895 int cmp
= PyObject_RichCompareBool(selfi
, v
, Py_EQ
);
902 return PyLong_FromSsize_t(count
);
905 PyDoc_STRVAR(count_doc
,
908 Return number of occurences of x in the array.");
911 array_index(arrayobject
*self
, PyObject
*v
)
915 for (i
= 0; i
< Py_SIZE(self
); i
++) {
916 PyObject
*selfi
= getarrayitem((PyObject
*)self
, i
);
917 int cmp
= PyObject_RichCompareBool(selfi
, v
, Py_EQ
);
920 return PyLong_FromLong((long)i
);
925 PyErr_SetString(PyExc_ValueError
, "array.index(x): x not in list");
929 PyDoc_STRVAR(index_doc
,
932 Return index of first occurence of x in the array.");
935 array_contains(arrayobject
*self
, PyObject
*v
)
940 for (i
= 0, cmp
= 0 ; cmp
== 0 && i
< Py_SIZE(self
); i
++) {
941 PyObject
*selfi
= getarrayitem((PyObject
*)self
, i
);
942 cmp
= PyObject_RichCompareBool(selfi
, v
, Py_EQ
);
949 array_remove(arrayobject
*self
, PyObject
*v
)
953 for (i
= 0; i
< Py_SIZE(self
); i
++) {
954 PyObject
*selfi
= getarrayitem((PyObject
*)self
,i
);
955 int cmp
= PyObject_RichCompareBool(selfi
, v
, Py_EQ
);
958 if (array_ass_slice(self
, i
, i
+1,
959 (PyObject
*)NULL
) != 0)
967 PyErr_SetString(PyExc_ValueError
, "array.remove(x): x not in list");
971 PyDoc_STRVAR(remove_doc
,
974 Remove the first occurence of x in the array.");
977 array_pop(arrayobject
*self
, PyObject
*args
)
981 if (!PyArg_ParseTuple(args
, "|n:pop", &i
))
983 if (Py_SIZE(self
) == 0) {
984 /* Special-case most common failure cause */
985 PyErr_SetString(PyExc_IndexError
, "pop from empty array");
990 if (i
< 0 || i
>= Py_SIZE(self
)) {
991 PyErr_SetString(PyExc_IndexError
, "pop index out of range");
994 v
= getarrayitem((PyObject
*)self
,i
);
995 if (array_ass_slice(self
, i
, i
+1, (PyObject
*)NULL
) != 0) {
1002 PyDoc_STRVAR(pop_doc
,
1005 Return the i-th element and delete it from the array. i defaults to -1.");
1008 array_extend(arrayobject
*self
, PyObject
*bb
)
1010 if (array_do_extend(self
, bb
) == -1)
1016 PyDoc_STRVAR(extend_doc
,
1017 "extend(array or iterable)\n\
1019 Append items to the end of the array.");
1022 array_insert(arrayobject
*self
, PyObject
*args
)
1026 if (!PyArg_ParseTuple(args
, "nO:insert", &i
, &v
))
1028 return ins(self
, i
, v
);
1031 PyDoc_STRVAR(insert_doc
,
1034 Insert a new item x into the array before position i.");
1038 array_buffer_info(arrayobject
*self
, PyObject
*unused
)
1040 PyObject
* retval
= NULL
;
1041 retval
= PyTuple_New(2);
1045 PyTuple_SET_ITEM(retval
, 0, PyLong_FromVoidPtr(self
->ob_item
));
1046 PyTuple_SET_ITEM(retval
, 1, PyLong_FromLong((long)(Py_SIZE(self
))));
1051 PyDoc_STRVAR(buffer_info_doc
,
1052 "buffer_info() -> (address, length)\n\
1054 Return a tuple (address, length) giving the current memory address and\n\
1055 the length in items of the buffer used to hold array's contents\n\
1056 The length should be multiplied by the itemsize attribute to calculate\n\
1057 the buffer length in bytes.");
1061 array_append(arrayobject
*self
, PyObject
*v
)
1063 return ins(self
, (int) Py_SIZE(self
), v
);
1066 PyDoc_STRVAR(append_doc
,
1069 Append new value x to the end of the array.");
1073 array_byteswap(arrayobject
*self
, PyObject
*unused
)
1078 switch (self
->ob_descr
->itemsize
) {
1082 for (p
= self
->ob_item
, i
= Py_SIZE(self
); --i
>= 0; p
+= 2) {
1089 for (p
= self
->ob_item
, i
= Py_SIZE(self
); --i
>= 0; p
+= 4) {
1099 for (p
= self
->ob_item
, i
= Py_SIZE(self
); --i
>= 0; p
+= 8) {
1115 PyErr_SetString(PyExc_RuntimeError
,
1116 "don't know how to byteswap this array type");
1123 PyDoc_STRVAR(byteswap_doc
,
1126 Byteswap all items of the array. If the items in the array are not 1, 2,\n\
1127 4, or 8 bytes in size, RuntimeError is raised.");
1130 array_reduce(arrayobject
*array
)
1132 PyObject
*dict
, *result
;
1134 dict
= PyObject_GetAttrString((PyObject
*)array
, "__dict__");
1140 if (Py_SIZE(array
) > 0) {
1141 result
= Py_BuildValue("O(cy#)O",
1143 array
->ob_descr
->typecode
,
1145 Py_SIZE(array
) * array
->ob_descr
->itemsize
,
1148 result
= Py_BuildValue("O(c)O",
1150 array
->ob_descr
->typecode
,
1157 PyDoc_STRVAR(array_doc
, "Return state information for pickling.");
1160 array_reverse(arrayobject
*self
, PyObject
*unused
)
1162 register Py_ssize_t itemsize
= self
->ob_descr
->itemsize
;
1163 register char *p
, *q
;
1164 /* little buffer to hold items while swapping */
1165 char tmp
[256]; /* 8 is probably enough -- but why skimp */
1166 assert((size_t)itemsize
<= sizeof(tmp
));
1168 if (Py_SIZE(self
) > 1) {
1169 for (p
= self
->ob_item
,
1170 q
= self
->ob_item
+ (Py_SIZE(self
) - 1)*itemsize
;
1172 p
+= itemsize
, q
-= itemsize
) {
1173 /* memory areas guaranteed disjoint, so memcpy
1174 * is safe (& memmove may be slower).
1176 memcpy(tmp
, p
, itemsize
);
1177 memcpy(p
, q
, itemsize
);
1178 memcpy(q
, tmp
, itemsize
);
1186 PyDoc_STRVAR(reverse_doc
,
1189 Reverse the order of the items in the array.");
1193 static PyObject
*array_fromstring(arrayobject
*self
, PyObject
*args
);
1196 array_fromfile(arrayobject
*self
, PyObject
*args
)
1198 PyObject
*f
, *b
, *res
;
1199 Py_ssize_t itemsize
= self
->ob_descr
->itemsize
;
1200 Py_ssize_t n
, nbytes
;
1202 if (!PyArg_ParseTuple(args
, "On:fromfile", &f
, &n
))
1205 nbytes
= n
* itemsize
;
1206 if (nbytes
< 0 || nbytes
/itemsize
!= n
) {
1211 b
= PyObject_CallMethod(f
, "read", "n", nbytes
);
1215 if (!PyString_Check(b
)) {
1216 PyErr_SetString(PyExc_TypeError
,
1217 "read() didn't return bytes");
1222 if (PyString_GET_SIZE(b
) != nbytes
) {
1223 PyErr_SetString(PyExc_EOFError
,
1224 "read() didn't return enough bytes");
1229 args
= Py_BuildValue("(O)", b
);
1234 res
= array_fromstring(self
, args
);
1240 PyDoc_STRVAR(fromfile_doc
,
1243 Read n objects from the file object f and append them to the end of the\n\
1248 array_tofile(arrayobject
*self
, PyObject
*f
)
1250 Py_ssize_t nbytes
= Py_SIZE(self
) * self
->ob_descr
->itemsize
;
1251 /* Write 64K blocks at a time */
1252 /* XXX Make the block size settable */
1253 int BLOCKSIZE
= 64*1024;
1254 Py_ssize_t nblocks
= (nbytes
+ BLOCKSIZE
- 1) / BLOCKSIZE
;
1257 if (Py_SIZE(self
) == 0)
1260 for (i
= 0; i
< nblocks
; i
++) {
1261 char* ptr
= self
->ob_item
+ i
*BLOCKSIZE
;
1262 Py_ssize_t size
= BLOCKSIZE
;
1263 PyObject
*bytes
, *res
;
1264 if (i
*BLOCKSIZE
+ size
> nbytes
)
1265 size
= nbytes
- i
*BLOCKSIZE
;
1266 bytes
= PyString_FromStringAndSize(ptr
, size
);
1269 res
= PyObject_CallMethod(f
, "write", "O", bytes
);
1273 Py_DECREF(res
); /* drop write result */
1281 PyDoc_STRVAR(tofile_doc
,
1284 Write all items (as machine values) to the file object f.");
1288 array_fromlist(arrayobject
*self
, PyObject
*list
)
1291 Py_ssize_t itemsize
= self
->ob_descr
->itemsize
;
1293 if (!PyList_Check(list
)) {
1294 PyErr_SetString(PyExc_TypeError
, "arg must be list");
1297 n
= PyList_Size(list
);
1299 char *item
= self
->ob_item
;
1301 PyMem_RESIZE(item
, char, (Py_SIZE(self
) + n
) * itemsize
);
1306 self
->ob_item
= item
;
1308 self
->allocated
= Py_SIZE(self
);
1309 for (i
= 0; i
< n
; i
++) {
1310 PyObject
*v
= PyList_GetItem(list
, i
);
1311 if ((*self
->ob_descr
->setitem
)(self
,
1312 Py_SIZE(self
) - n
+ i
, v
) != 0) {
1314 PyMem_RESIZE(item
, char,
1315 Py_SIZE(self
) * itemsize
);
1316 self
->ob_item
= item
;
1317 self
->allocated
= Py_SIZE(self
);
1326 PyDoc_STRVAR(fromlist_doc
,
1329 Append items to array from list.");
1332 array_tolist(arrayobject
*self
, PyObject
*unused
)
1334 PyObject
*list
= PyList_New(Py_SIZE(self
));
1339 for (i
= 0; i
< Py_SIZE(self
); i
++) {
1340 PyObject
*v
= getarrayitem((PyObject
*)self
, i
);
1345 PyList_SetItem(list
, i
, v
);
1350 PyDoc_STRVAR(tolist_doc
,
1351 "tolist() -> list\n\
1353 Convert array to an ordinary list with the same items.");
1357 array_fromstring(arrayobject
*self
, PyObject
*args
)
1361 int itemsize
= self
->ob_descr
->itemsize
;
1362 if (!PyArg_ParseTuple(args
, "s#:fromstring", &str
, &n
))
1364 if (n
% itemsize
!= 0) {
1365 PyErr_SetString(PyExc_ValueError
,
1366 "string length not a multiple of item size");
1371 char *item
= self
->ob_item
;
1372 PyMem_RESIZE(item
, char, (Py_SIZE(self
) + n
) * itemsize
);
1377 self
->ob_item
= item
;
1379 self
->allocated
= Py_SIZE(self
);
1380 memcpy(item
+ (Py_SIZE(self
) - n
) * itemsize
,
1387 PyDoc_STRVAR(fromstring_doc
,
1388 "fromstring(string)\n\
1390 Appends items from the string, interpreting it as an array of machine\n\
1391 values, as if it had been read from a file using the fromfile() method).");
1395 array_tostring(arrayobject
*self
, PyObject
*unused
)
1397 return PyString_FromStringAndSize(self
->ob_item
,
1398 Py_SIZE(self
) * self
->ob_descr
->itemsize
);
1401 PyDoc_STRVAR(tostring_doc
,
1402 "tostring() -> string\n\
1404 Convert the array to an array of machine values and return the string\n\
1410 array_fromunicode(arrayobject
*self
, PyObject
*args
)
1416 if (!PyArg_ParseTuple(args
, "u#:fromunicode", &ustr
, &n
))
1418 typecode
= self
->ob_descr
->typecode
;
1419 if ((typecode
!= 'u')) {
1420 PyErr_SetString(PyExc_ValueError
,
1421 "fromunicode() may only be called on "
1422 "unicode type arrays");
1426 Py_UNICODE
*item
= (Py_UNICODE
*) self
->ob_item
;
1427 PyMem_RESIZE(item
, Py_UNICODE
, Py_SIZE(self
) + n
);
1432 self
->ob_item
= (char *) item
;
1434 self
->allocated
= Py_SIZE(self
);
1435 memcpy(item
+ Py_SIZE(self
) - n
,
1436 ustr
, n
* sizeof(Py_UNICODE
));
1443 PyDoc_STRVAR(fromunicode_doc
,
1444 "fromunicode(ustr)\n\
1446 Extends this array with data from the unicode string ustr.\n\
1447 The array must be a unicode type array; otherwise a ValueError\n\
1448 is raised. Use array.fromstring(ustr.decode(...)) to\n\
1449 append Unicode data to an array of some other type.");
1453 array_tounicode(arrayobject
*self
, PyObject
*unused
)
1456 typecode
= self
->ob_descr
->typecode
;
1457 if ((typecode
!= 'u')) {
1458 PyErr_SetString(PyExc_ValueError
,
1459 "tounicode() may only be called on unicode type arrays");
1462 return PyUnicode_FromUnicode((Py_UNICODE
*) self
->ob_item
, Py_SIZE(self
));
1465 PyDoc_STRVAR(tounicode_doc
,
1466 "tounicode() -> unicode\n\
1468 Convert the array to a unicode string. The array must be\n\
1469 a unicode type array; otherwise a ValueError is raised. Use\n\
1470 array.tostring().decode() to obtain a unicode string from\n\
1471 an array of some other type.");
1476 array_get_typecode(arrayobject
*a
, void *closure
)
1478 char tc
= a
->ob_descr
->typecode
;
1479 return PyUnicode_FromStringAndSize(&tc
, 1);
1483 array_get_itemsize(arrayobject
*a
, void *closure
)
1485 return PyLong_FromLong((long)a
->ob_descr
->itemsize
);
1488 static PyGetSetDef array_getsets
[] = {
1489 {"typecode", (getter
) array_get_typecode
, NULL
,
1490 "the typecode character used to create the array"},
1491 {"itemsize", (getter
) array_get_itemsize
, NULL
,
1492 "the size, in bytes, of one array item"},
1496 PyMethodDef array_methods
[] = {
1497 {"append", (PyCFunction
)array_append
, METH_O
,
1499 {"buffer_info", (PyCFunction
)array_buffer_info
, METH_NOARGS
,
1501 {"byteswap", (PyCFunction
)array_byteswap
, METH_NOARGS
,
1503 {"__copy__", (PyCFunction
)array_copy
, METH_NOARGS
,
1505 {"count", (PyCFunction
)array_count
, METH_O
,
1507 {"__deepcopy__",(PyCFunction
)array_copy
, METH_O
,
1509 {"extend", (PyCFunction
)array_extend
, METH_O
,
1511 {"fromfile", (PyCFunction
)array_fromfile
, METH_VARARGS
,
1513 {"fromlist", (PyCFunction
)array_fromlist
, METH_O
,
1515 {"fromstring", (PyCFunction
)array_fromstring
, METH_VARARGS
,
1517 {"fromunicode", (PyCFunction
)array_fromunicode
, METH_VARARGS
,
1519 {"index", (PyCFunction
)array_index
, METH_O
,
1521 {"insert", (PyCFunction
)array_insert
, METH_VARARGS
,
1523 {"pop", (PyCFunction
)array_pop
, METH_VARARGS
,
1525 {"__reduce__", (PyCFunction
)array_reduce
, METH_NOARGS
,
1527 {"remove", (PyCFunction
)array_remove
, METH_O
,
1529 {"reverse", (PyCFunction
)array_reverse
, METH_NOARGS
,
1531 /* {"sort", (PyCFunction)array_sort, METH_VARARGS,
1533 {"tofile", (PyCFunction
)array_tofile
, METH_O
,
1535 {"tolist", (PyCFunction
)array_tolist
, METH_NOARGS
,
1537 {"tostring", (PyCFunction
)array_tostring
, METH_NOARGS
,
1539 {"tounicode", (PyCFunction
)array_tounicode
, METH_NOARGS
,
1541 {NULL
, NULL
} /* sentinel */
1545 array_repr(arrayobject
*a
)
1548 PyObject
*s
, *v
= NULL
;
1552 typecode
= a
->ob_descr
->typecode
;
1554 return PyUnicode_FromFormat("array('%c')", typecode
);
1556 if ((typecode
== 'u'))
1557 v
= array_tounicode(a
, NULL
);
1559 v
= array_tolist(a
, NULL
);
1561 s
= PyUnicode_FromFormat("array('%c', %R)", typecode
, v
);
1567 array_subscr(arrayobject
* self
, PyObject
* item
)
1569 if (PyIndex_Check(item
)) {
1570 Py_ssize_t i
= PyNumber_AsSsize_t(item
, PyExc_IndexError
);
1571 if (i
==-1 && PyErr_Occurred()) {
1576 return array_item(self
, i
);
1578 else if (PySlice_Check(item
)) {
1579 Py_ssize_t start
, stop
, step
, slicelength
, cur
, i
;
1582 int itemsize
= self
->ob_descr
->itemsize
;
1584 if (PySlice_GetIndicesEx((PySliceObject
*)item
, Py_SIZE(self
),
1585 &start
, &stop
, &step
, &slicelength
) < 0) {
1589 if (slicelength
<= 0) {
1590 return newarrayobject(&Arraytype
, 0, self
->ob_descr
);
1592 else if (step
== 1) {
1593 PyObject
*result
= newarrayobject(&Arraytype
,
1594 slicelength
, self
->ob_descr
);
1597 memcpy(((arrayobject
*)result
)->ob_item
,
1598 self
->ob_item
+ start
* itemsize
,
1599 slicelength
* itemsize
);
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
;
1610 memcpy(ar
->ob_item
+ i
*itemsize
,
1611 self
->ob_item
+ cur
*itemsize
,
1619 PyErr_SetString(PyExc_TypeError
,
1620 "array indices must be integers");
1626 array_ass_subscr(arrayobject
* self
, PyObject
* item
, PyObject
* value
)
1628 Py_ssize_t start
, stop
, step
, slicelength
, needed
;
1632 if (PyIndex_Check(item
)) {
1633 Py_ssize_t i
= PyNumber_AsSsize_t(item
, PyExc_IndexError
);
1635 if (i
== -1 && PyErr_Occurred())
1639 if (i
< 0 || i
>= Py_SIZE(self
)) {
1640 PyErr_SetString(PyExc_IndexError
,
1641 "array assignment index out of range");
1644 if (value
== NULL
) {
1645 /* Fall through to slice assignment */
1652 return (*self
->ob_descr
->setitem
)(self
, i
, value
);
1654 else if (PySlice_Check(item
)) {
1655 if (PySlice_GetIndicesEx((PySliceObject
*)item
,
1656 Py_SIZE(self
), &start
, &stop
,
1657 &step
, &slicelength
) < 0) {
1662 PyErr_SetString(PyExc_TypeError
,
1663 "array indices must be integer");
1666 if (value
== NULL
) {
1670 else if (array_Check(value
)) {
1671 other
= (arrayobject
*)value
;
1672 needed
= Py_SIZE(other
);
1673 if (self
== other
) {
1674 /* Special case "self[i:j] = self" -- copy self first */
1676 value
= array_slice(other
, 0, needed
);
1679 ret
= array_ass_subscr(self
, item
, value
);
1683 if (other
->ob_descr
!= self
->ob_descr
) {
1684 PyErr_BadArgument();
1689 PyErr_Format(PyExc_TypeError
,
1690 "can only assign array (not \"%.200s\") to array slice",
1691 Py_TYPE(value
)->tp_name
);
1694 itemsize
= self
->ob_descr
->itemsize
;
1695 /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
1696 if ((step
> 0 && stop
< start
) ||
1697 (step
< 0 && stop
> start
))
1700 if (slicelength
> needed
) {
1701 memmove(self
->ob_item
+ (start
+ needed
) * itemsize
,
1702 self
->ob_item
+ stop
* itemsize
,
1703 (Py_SIZE(self
) - stop
) * itemsize
);
1704 if (array_resize(self
, Py_SIZE(self
) +
1705 needed
- slicelength
) < 0)
1708 else if (slicelength
< needed
) {
1709 if (array_resize(self
, Py_SIZE(self
) +
1710 needed
- slicelength
) < 0)
1712 memmove(self
->ob_item
+ (start
+ needed
) * itemsize
,
1713 self
->ob_item
+ stop
* itemsize
,
1714 (Py_SIZE(self
) - start
- needed
) * itemsize
);
1717 memcpy(self
->ob_item
+ start
* itemsize
,
1718 other
->ob_item
, needed
* itemsize
);
1721 else if (needed
== 0) {
1727 start
= stop
+ step
* (slicelength
- 1) - 1;
1730 for (cur
= start
, i
= 0; i
< slicelength
;
1732 Py_ssize_t lim
= step
- 1;
1734 if (cur
+ step
>= Py_SIZE(self
))
1735 lim
= Py_SIZE(self
) - cur
- 1;
1736 memmove(self
->ob_item
+ (cur
- i
) * itemsize
,
1737 self
->ob_item
+ (cur
+ 1) * itemsize
,
1740 cur
= start
+ slicelength
* step
;
1741 if (cur
< Py_SIZE(self
)) {
1742 memmove(self
->ob_item
+ (cur
-slicelength
) * itemsize
,
1743 self
->ob_item
+ cur
* itemsize
,
1744 (Py_SIZE(self
) - cur
) * itemsize
);
1746 if (array_resize(self
, Py_SIZE(self
) - slicelength
) < 0)
1753 if (needed
!= slicelength
) {
1754 PyErr_Format(PyExc_ValueError
,
1755 "attempt to assign array of size %zd "
1756 "to extended slice of size %zd",
1757 needed
, slicelength
);
1760 for (cur
= start
, i
= 0; i
< slicelength
;
1762 memcpy(self
->ob_item
+ cur
* itemsize
,
1763 other
->ob_item
+ i
* itemsize
,
1770 static PyMappingMethods array_as_mapping
= {
1771 (lenfunc
)array_length
,
1772 (binaryfunc
)array_subscr
,
1773 (objobjargproc
)array_ass_subscr
1776 static const void *emptybuf
= "";
1780 array_buffer_getbuf(arrayobject
*self
, Py_buffer
*view
, int flags
)
1782 if ((flags
& PyBUF_LOCK
)) {
1783 PyErr_SetString(PyExc_BufferError
,
1784 "Cannot lock data");
1787 if (view
==NULL
) goto finish
;
1789 view
->buf
= (void *)self
->ob_item
;
1790 if (view
->buf
== NULL
)
1791 view
->buf
= (void *)emptybuf
;
1792 view
->len
= (Py_SIZE(self
)) * self
->ob_descr
->itemsize
;
1795 view
->itemsize
= self
->ob_descr
->itemsize
;
1796 view
->suboffsets
= NULL
;
1798 if ((flags
& PyBUF_ND
)==PyBUF_ND
) {
1799 view
->shape
= &((Py_SIZE(self
)));
1801 view
->strides
= NULL
;
1802 if ((flags
& PyBUF_STRIDES
)==PyBUF_STRIDES
)
1803 view
->strides
= &(view
->itemsize
);
1804 view
->format
= NULL
;
1805 view
->internal
= NULL
;
1806 if ((flags
& PyBUF_FORMAT
) == PyBUF_FORMAT
) {
1807 view
->format
= self
->ob_descr
->formats
;
1808 #ifdef Py_UNICODE_WIDE
1809 if (self
->ob_descr
->typecode
== 'u') {
1821 array_buffer_relbuf(arrayobject
*self
, Py_buffer
*view
)
1826 static PySequenceMethods array_as_sequence
= {
1827 (lenfunc
)array_length
, /*sq_length*/
1828 (binaryfunc
)array_concat
, /*sq_concat*/
1829 (ssizeargfunc
)array_repeat
, /*sq_repeat*/
1830 (ssizeargfunc
)array_item
, /*sq_item*/
1832 (ssizeobjargproc
)array_ass_item
, /*sq_ass_item*/
1834 (objobjproc
)array_contains
, /*sq_contains*/
1835 (binaryfunc
)array_inplace_concat
, /*sq_inplace_concat*/
1836 (ssizeargfunc
)array_inplace_repeat
/*sq_inplace_repeat*/
1839 static PyBufferProcs array_as_buffer
= {
1840 (getbufferproc
)array_buffer_getbuf
,
1841 (releasebufferproc
)array_buffer_relbuf
1845 array_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1848 PyObject
*initial
= NULL
, *it
= NULL
;
1849 struct arraydescr
*descr
;
1851 if (type
== &Arraytype
&& !_PyArg_NoKeywords("array.array()", kwds
))
1854 if (!PyArg_ParseTuple(args
, "C|O:array", &c
, &initial
))
1857 if (!(initial
== NULL
|| PyList_Check(initial
)
1858 || PyBytes_Check(initial
)
1859 || PyString_Check(initial
)
1860 || PyTuple_Check(initial
)
1861 || ((c
=='u') && PyUnicode_Check(initial
)))) {
1862 it
= PyObject_GetIter(initial
);
1865 /* We set initial to NULL so that the subsequent code
1866 will create an empty array of the appropriate type
1867 and afterwards we can use array_iter_extend to populate
1872 for (descr
= descriptors
; descr
->typecode
!= '\0'; descr
++) {
1873 if (descr
->typecode
== c
) {
1877 if (initial
== NULL
|| !(PyList_Check(initial
)
1878 || PyTuple_Check(initial
)))
1881 len
= PySequence_Size(initial
);
1883 a
= newarrayobject(type
, len
, descr
);
1889 for (i
= 0; i
< len
; i
++) {
1891 PySequence_GetItem(initial
, i
);
1896 if (setarrayitem(a
, i
, v
) != 0) {
1904 else if (initial
!= NULL
&& (PyBytes_Check(initial
) ||
1905 PyString_Check(initial
))) {
1906 PyObject
*t_initial
, *v
;
1907 t_initial
= PyTuple_Pack(1, initial
);
1908 if (t_initial
== NULL
) {
1912 v
= array_fromstring((arrayobject
*)a
,
1914 Py_DECREF(t_initial
);
1921 else if (initial
!= NULL
&& PyUnicode_Check(initial
)) {
1922 Py_ssize_t n
= PyUnicode_GET_DATA_SIZE(initial
);
1924 arrayobject
*self
= (arrayobject
*)a
;
1925 char *item
= self
->ob_item
;
1926 item
= (char *)PyMem_Realloc(item
, n
);
1932 self
->ob_item
= item
;
1933 Py_SIZE(self
) = n
/ sizeof(Py_UNICODE
);
1934 memcpy(item
, PyUnicode_AS_DATA(initial
), n
);
1935 self
->allocated
= Py_SIZE(self
);
1939 if (array_iter_extend((arrayobject
*)a
, it
) == -1) {
1949 PyErr_SetString(PyExc_ValueError
,
1950 "bad typecode (must be b, B, u, h, H, i, I, l, L, f or d)");
1955 PyDoc_STRVAR(module_doc
,
1956 "This module defines an object type which can efficiently represent\n\
1957 an array of basic values: characters, integers, floating point\n\
1958 numbers. Arrays are sequence types and behave very much like lists,\n\
1959 except that the type of objects stored in them is constrained. The\n\
1960 type is specified at object creation time by using a type code, which\n\
1961 is a single character. The following type codes are defined:\n\
1963 Type code C Type Minimum size in bytes \n\
1964 'b' signed integer 1 \n\
1965 'B' unsigned integer 1 \n\
1966 'u' Unicode character 2 (see note) \n\
1967 'h' signed integer 2 \n\
1968 'H' unsigned integer 2 \n\
1969 'i' signed integer 2 \n\
1970 'I' unsigned integer 2 \n\
1971 'w' unicode character 4 \n\
1972 'l' signed integer 4 \n\
1973 'L' unsigned integer 4 \n\
1974 'f' floating point 4 \n\
1975 'd' floating point 8 \n\
1977 NOTE: The 'u' typecode corresponds to Python's unicode character. On \n\
1978 narrow builds this is 2-bytes on wide builds this is 4-bytes.\n\
1980 The constructor is:\n\
1982 array(typecode [, initializer]) -- create a new array\n\
1985 PyDoc_STRVAR(arraytype_doc
,
1986 "array(typecode [, initializer]) -> array\n\
1988 Return a new array whose items are restricted by typecode, and\n\
1989 initialized from the optional initializer value, which must be a list,\n\
1990 string. or iterable over elements of the appropriate type.\n\
1992 Arrays represent basic values and behave very much like lists, except\n\
1993 the type of objects stored in them is constrained.\n\
1997 append() -- append a new item to the end of the array\n\
1998 buffer_info() -- return information giving the current memory info\n\
1999 byteswap() -- byteswap all the items of the array\n\
2000 count() -- return number of occurences of an object\n\
2001 extend() -- extend array by appending multiple elements from an iterable\n\
2002 fromfile() -- read items from a file object\n\
2003 fromlist() -- append items from the list\n\
2004 fromstring() -- append items from the string\n\
2005 index() -- return index of first occurence of an object\n\
2006 insert() -- insert a new item into the array at a provided position\n\
2007 pop() -- remove and return item (default last)\n\
2008 remove() -- remove first occurence of an object\n\
2009 reverse() -- reverse the order of the items in the array\n\
2010 tofile() -- write all items to a file object\n\
2011 tolist() -- return the array converted to an ordinary list\n\
2012 tostring() -- return the array converted to a string\n\
2016 typecode -- the typecode character used to create the array\n\
2017 itemsize -- the length in bytes of one array item\n\
2020 static PyObject
*array_iter(arrayobject
*ao
);
2022 static PyTypeObject Arraytype
= {
2023 PyVarObject_HEAD_INIT(NULL
, 0)
2025 sizeof(arrayobject
),
2027 (destructor
)array_dealloc
, /* tp_dealloc */
2032 (reprfunc
)array_repr
, /* tp_repr */
2033 0, /* tp_as_number*/
2034 &array_as_sequence
, /* tp_as_sequence*/
2035 &array_as_mapping
, /* tp_as_mapping*/
2039 PyObject_GenericGetAttr
, /* tp_getattro */
2040 0, /* tp_setattro */
2041 &array_as_buffer
, /* tp_as_buffer*/
2042 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
2043 arraytype_doc
, /* tp_doc */
2044 0, /* tp_traverse */
2046 array_richcompare
, /* tp_richcompare */
2047 offsetof(arrayobject
, weakreflist
), /* tp_weaklistoffset */
2048 (getiterfunc
)array_iter
, /* tp_iter */
2049 0, /* tp_iternext */
2050 array_methods
, /* tp_methods */
2052 array_getsets
, /* tp_getset */
2055 0, /* tp_descr_get */
2056 0, /* tp_descr_set */
2057 0, /* tp_dictoffset */
2059 PyType_GenericAlloc
, /* tp_alloc */
2060 array_new
, /* tp_new */
2061 PyObject_Del
, /* tp_free */
2065 /*********************** Array Iterator **************************/
2071 PyObject
* (*getitem
)(struct arrayobject
*, Py_ssize_t
);
2074 static PyTypeObject PyArrayIter_Type
;
2076 #define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
2079 array_iter(arrayobject
*ao
)
2081 arrayiterobject
*it
;
2083 if (!array_Check(ao
)) {
2084 PyErr_BadInternalCall();
2088 it
= PyObject_GC_New(arrayiterobject
, &PyArrayIter_Type
);
2095 it
->getitem
= ao
->ob_descr
->getitem
;
2096 PyObject_GC_Track(it
);
2097 return (PyObject
*)it
;
2101 arrayiter_next(arrayiterobject
*it
)
2103 assert(PyArrayIter_Check(it
));
2104 if (it
->index
< Py_SIZE(it
->ao
))
2105 return (*it
->getitem
)(it
->ao
, it
->index
++);
2110 arrayiter_dealloc(arrayiterobject
*it
)
2112 PyObject_GC_UnTrack(it
);
2114 PyObject_GC_Del(it
);
2118 arrayiter_traverse(arrayiterobject
*it
, visitproc visit
, void *arg
)
2124 static PyTypeObject PyArrayIter_Type
= {
2125 PyVarObject_HEAD_INIT(NULL
, 0)
2126 "arrayiterator", /* tp_name */
2127 sizeof(arrayiterobject
), /* tp_basicsize */
2128 0, /* tp_itemsize */
2130 (destructor
)arrayiter_dealloc
, /* tp_dealloc */
2136 0, /* tp_as_number */
2137 0, /* tp_as_sequence */
2138 0, /* tp_as_mapping */
2142 PyObject_GenericGetAttr
, /* tp_getattro */
2143 0, /* tp_setattro */
2144 0, /* tp_as_buffer */
2145 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
,/* tp_flags */
2147 (traverseproc
)arrayiter_traverse
, /* tp_traverse */
2149 0, /* tp_richcompare */
2150 0, /* tp_weaklistoffset */
2151 PyObject_SelfIter
, /* tp_iter */
2152 (iternextfunc
)arrayiter_next
, /* tp_iternext */
2157 /*********************** Install Module **************************/
2159 /* No functions in array module. */
2160 static PyMethodDef a_methods
[] = {
2161 {NULL
, NULL
, 0, NULL
} /* Sentinel */
2169 PyObject
*typecodes
;
2170 Py_ssize_t size
= 0;
2171 register Py_UNICODE
*p
;
2172 struct arraydescr
*descr
;
2174 if (PyType_Ready(&Arraytype
) < 0)
2176 Py_TYPE(&PyArrayIter_Type
) = &PyType_Type
;
2177 m
= Py_InitModule3("array", a_methods
, module_doc
);
2181 Py_INCREF((PyObject
*)&Arraytype
);
2182 PyModule_AddObject(m
, "ArrayType", (PyObject
*)&Arraytype
);
2183 Py_INCREF((PyObject
*)&Arraytype
);
2184 PyModule_AddObject(m
, "array", (PyObject
*)&Arraytype
);
2186 for (descr
=descriptors
; descr
->typecode
!= '\0'; descr
++) {
2190 typecodes
= PyUnicode_FromStringAndSize(NULL
, size
);
2191 p
= PyUnicode_AS_UNICODE(typecodes
);
2192 for (descr
= descriptors
; descr
->typecode
!= '\0'; descr
++) {
2193 *p
++ = (char)descr
->typecode
;
2196 PyModule_AddObject(m
, "typecodes", (PyObject
*)typecodes
);
2198 /* No need to check the error here, the caller will do that */