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) (Py_TYPE(op) == &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 Py_SIZE(self
) < newsize
+ 16 &&
57 self
->ob_item
!= NULL
) {
58 Py_SIZE(self
) = 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) + (Py_SIZE(self
) < 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 Py_SIZE(self
) = 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);
435 op
->ob_descr
= descr
;
436 op
->allocated
= size
;
437 op
->weakreflist
= NULL
;
443 op
->ob_item
= PyMem_NEW(char, nbytes
);
444 if (op
->ob_item
== NULL
) {
446 return PyErr_NoMemory();
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
<Py_SIZE(ap
));
459 return (*ap
->ob_descr
->getitem
)(ap
, i
);
463 ins1(arrayobject
*self
, Py_ssize_t where
, PyObject
*v
)
466 Py_ssize_t n
= Py_SIZE(self
);
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 Py_TYPE(op
)->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 (Py_SIZE(va
) != Py_SIZE(wa
) && (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
< Py_SIZE(va
) && i
< Py_SIZE(wa
); 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
= Py_SIZE(va
);
553 Py_ssize_t ws
= Py_SIZE(wa
);
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
>= Py_SIZE(a
)) {
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
> Py_SIZE(a
))
618 else if (ihigh
> Py_SIZE(a
))
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, Py_SIZE(a
));
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 Py_TYPE(bb
)->tp_name
);
650 #define b ((arrayobject *)bb)
651 if (a
->ob_descr
!= b
->ob_descr
) {
655 if (Py_SIZE(a
) > PY_SSIZE_T_MAX
- Py_SIZE(b
)) {
656 return PyErr_NoMemory();
658 size
= Py_SIZE(a
) + Py_SIZE(b
);
659 np
= (arrayobject
*) newarrayobject(&Arraytype
, size
, a
->ob_descr
);
663 memcpy(np
->ob_item
, a
->ob_item
, Py_SIZE(a
)*a
->ob_descr
->itemsize
);
664 memcpy(np
->ob_item
+ Py_SIZE(a
)*a
->ob_descr
->itemsize
,
665 b
->ob_item
, Py_SIZE(b
)*b
->ob_descr
->itemsize
);
666 return (PyObject
*)np
;
671 array_repeat(arrayobject
*a
, Py_ssize_t n
)
680 if ((Py_SIZE(a
) != 0) && (n
> PY_SSIZE_T_MAX
/ Py_SIZE(a
))) {
681 return PyErr_NoMemory();
683 size
= Py_SIZE(a
) * n
;
684 np
= (arrayobject
*) newarrayobject(&Arraytype
, size
, a
->ob_descr
);
688 nbytes
= Py_SIZE(a
) * a
->ob_descr
->itemsize
;
689 for (i
= 0; i
< n
; i
++) {
690 memcpy(p
, a
->ob_item
, nbytes
);
693 return (PyObject
*) np
;
697 array_ass_slice(arrayobject
*a
, Py_ssize_t ilow
, Py_ssize_t ihigh
, PyObject
*v
)
700 Py_ssize_t n
; /* Size of replacement array */
701 Py_ssize_t d
; /* Change in size */
702 #define b ((arrayobject *)v)
705 else if (array_Check(v
)) {
708 /* Special case "a[i:j] = a" -- copy b first */
710 v
= array_slice(b
, 0, n
);
713 ret
= array_ass_slice(a
, ilow
, ihigh
, v
);
717 if (b
->ob_descr
!= a
->ob_descr
) {
723 PyErr_Format(PyExc_TypeError
,
724 "can only assign array (not \"%.200s\") to array slice",
725 Py_TYPE(v
)->tp_name
);
730 else if (ilow
> Py_SIZE(a
))
736 else if (ihigh
> Py_SIZE(a
))
739 d
= n
- (ihigh
-ilow
);
740 if (d
< 0) { /* Delete -d items */
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
);
745 PyMem_RESIZE(item
, char, Py_SIZE(a
)*a
->ob_descr
->itemsize
);
748 a
->allocated
= Py_SIZE(a
);
750 else if (d
> 0) { /* Insert d items */
751 PyMem_RESIZE(item
, char,
752 (Py_SIZE(a
) + d
)*a
->ob_descr
->itemsize
);
757 memmove(item
+ (ihigh
+d
)*a
->ob_descr
->itemsize
,
758 item
+ ihigh
*a
->ob_descr
->itemsize
,
759 (Py_SIZE(a
)-ihigh
)*a
->ob_descr
->itemsize
);
762 a
->allocated
= Py_SIZE(a
);
765 memcpy(item
+ ilow
*a
->ob_descr
->itemsize
, b
->ob_item
,
766 n
*b
->ob_descr
->itemsize
);
772 array_ass_item(arrayobject
*a
, Py_ssize_t i
, PyObject
*v
)
774 if (i
< 0 || i
>= Py_SIZE(a
)) {
775 PyErr_SetString(PyExc_IndexError
,
776 "array assignment index out of range");
780 return array_ass_slice(a
, i
, i
+1, v
);
781 return (*a
->ob_descr
->setitem
)(a
, i
, v
);
785 setarrayitem(PyObject
*a
, Py_ssize_t i
, PyObject
*v
)
787 assert(array_Check(a
));
788 return array_ass_item((arrayobject
*)a
, i
, v
);
792 array_iter_extend(arrayobject
*self
, PyObject
*bb
)
796 it
= PyObject_GetIter(bb
);
800 while ((v
= PyIter_Next(it
)) != NULL
) {
801 if (ins1(self
, (int) Py_SIZE(self
), v
) != 0) {
809 if (PyErr_Occurred())
815 array_do_extend(arrayobject
*self
, PyObject
*bb
)
820 if (!array_Check(bb
))
821 return array_iter_extend(self
, bb
);
822 #define b ((arrayobject *)bb)
823 if (self
->ob_descr
!= b
->ob_descr
) {
824 PyErr_SetString(PyExc_TypeError
,
825 "can only extend with array of same kind");
828 if ((Py_SIZE(self
) > PY_SSIZE_T_MAX
- Py_SIZE(b
)) ||
829 ((Py_SIZE(self
) + Py_SIZE(b
)) > PY_SSIZE_T_MAX
/ self
->ob_descr
->itemsize
)) {
833 size
= Py_SIZE(self
) + Py_SIZE(b
);
834 old_item
= self
->ob_item
;
835 PyMem_RESIZE(self
->ob_item
, char, size
*self
->ob_descr
->itemsize
);
836 if (self
->ob_item
== NULL
) {
837 self
->ob_item
= old_item
;
841 memcpy(self
->ob_item
+ Py_SIZE(self
)*self
->ob_descr
->itemsize
,
842 b
->ob_item
, Py_SIZE(b
)*b
->ob_descr
->itemsize
);
843 Py_SIZE(self
) = size
;
844 self
->allocated
= size
;
851 array_inplace_concat(arrayobject
*self
, PyObject
*bb
)
853 if (!array_Check(bb
)) {
854 PyErr_Format(PyExc_TypeError
,
855 "can only extend array with array (not \"%.200s\")",
856 Py_TYPE(bb
)->tp_name
);
859 if (array_do_extend(self
, bb
) == -1)
862 return (PyObject
*)self
;
866 array_inplace_repeat(arrayobject
*self
, Py_ssize_t n
)
871 if (Py_SIZE(self
) > 0) {
874 items
= self
->ob_item
;
875 if ((self
->ob_descr
->itemsize
!= 0) &&
876 (Py_SIZE(self
) > PY_SSIZE_T_MAX
/ self
->ob_descr
->itemsize
)) {
877 return PyErr_NoMemory();
879 size
= Py_SIZE(self
) * self
->ob_descr
->itemsize
;
882 self
->ob_item
= NULL
;
887 if (size
> PY_SSIZE_T_MAX
/ n
) {
888 return PyErr_NoMemory();
890 PyMem_RESIZE(items
, char, n
* size
);
892 return PyErr_NoMemory();
894 for (i
= 1; i
< n
; i
++) {
896 memcpy(p
, items
, size
);
898 self
->ob_item
= items
;
900 self
->allocated
= Py_SIZE(self
);
904 return (PyObject
*)self
;
909 ins(arrayobject
*self
, Py_ssize_t where
, PyObject
*v
)
911 if (ins1(self
, where
, v
) != 0)
918 array_count(arrayobject
*self
, PyObject
*v
)
920 Py_ssize_t count
= 0;
923 for (i
= 0; i
< Py_SIZE(self
); i
++) {
924 PyObject
*selfi
= getarrayitem((PyObject
*)self
, i
);
925 int cmp
= PyObject_RichCompareBool(selfi
, v
, Py_EQ
);
932 return PyInt_FromSsize_t(count
);
935 PyDoc_STRVAR(count_doc
,
938 Return number of occurrences of x in the array.");
941 array_index(arrayobject
*self
, PyObject
*v
)
945 for (i
= 0; i
< Py_SIZE(self
); i
++) {
946 PyObject
*selfi
= getarrayitem((PyObject
*)self
, i
);
947 int cmp
= PyObject_RichCompareBool(selfi
, v
, Py_EQ
);
950 return PyInt_FromLong((long)i
);
955 PyErr_SetString(PyExc_ValueError
, "array.index(x): x not in list");
959 PyDoc_STRVAR(index_doc
,
962 Return index of first occurrence of x in the array.");
965 array_contains(arrayobject
*self
, PyObject
*v
)
970 for (i
= 0, cmp
= 0 ; cmp
== 0 && i
< Py_SIZE(self
); i
++) {
971 PyObject
*selfi
= getarrayitem((PyObject
*)self
, i
);
972 cmp
= PyObject_RichCompareBool(selfi
, v
, Py_EQ
);
979 array_remove(arrayobject
*self
, PyObject
*v
)
983 for (i
= 0; i
< Py_SIZE(self
); i
++) {
984 PyObject
*selfi
= getarrayitem((PyObject
*)self
,i
);
985 int cmp
= PyObject_RichCompareBool(selfi
, v
, Py_EQ
);
988 if (array_ass_slice(self
, i
, i
+1,
989 (PyObject
*)NULL
) != 0)
997 PyErr_SetString(PyExc_ValueError
, "array.remove(x): x not in list");
1001 PyDoc_STRVAR(remove_doc
,
1004 Remove the first occurrence of x in the array.");
1007 array_pop(arrayobject
*self
, PyObject
*args
)
1011 if (!PyArg_ParseTuple(args
, "|n:pop", &i
))
1013 if (Py_SIZE(self
) == 0) {
1014 /* Special-case most common failure cause */
1015 PyErr_SetString(PyExc_IndexError
, "pop from empty array");
1020 if (i
< 0 || i
>= Py_SIZE(self
)) {
1021 PyErr_SetString(PyExc_IndexError
, "pop index out of range");
1024 v
= getarrayitem((PyObject
*)self
,i
);
1025 if (array_ass_slice(self
, i
, i
+1, (PyObject
*)NULL
) != 0) {
1032 PyDoc_STRVAR(pop_doc
,
1035 Return the i-th element and delete it from the array. i defaults to -1.");
1038 array_extend(arrayobject
*self
, PyObject
*bb
)
1040 if (array_do_extend(self
, bb
) == -1)
1046 PyDoc_STRVAR(extend_doc
,
1047 "extend(array or iterable)\n\
1049 Append items to the end of the array.");
1052 array_insert(arrayobject
*self
, PyObject
*args
)
1056 if (!PyArg_ParseTuple(args
, "nO:insert", &i
, &v
))
1058 return ins(self
, i
, v
);
1061 PyDoc_STRVAR(insert_doc
,
1064 Insert a new item x into the array before position i.");
1068 array_buffer_info(arrayobject
*self
, PyObject
*unused
)
1070 PyObject
* retval
= NULL
;
1071 retval
= PyTuple_New(2);
1075 PyTuple_SET_ITEM(retval
, 0, PyLong_FromVoidPtr(self
->ob_item
));
1076 PyTuple_SET_ITEM(retval
, 1, PyInt_FromLong((long)(Py_SIZE(self
))));
1081 PyDoc_STRVAR(buffer_info_doc
,
1082 "buffer_info() -> (address, length)\n\
1084 Return a tuple (address, length) giving the current memory address and\n\
1085 the length in items of the buffer used to hold array's contents\n\
1086 The length should be multiplied by the itemsize attribute to calculate\n\
1087 the buffer length in bytes.");
1091 array_append(arrayobject
*self
, PyObject
*v
)
1093 return ins(self
, (int) Py_SIZE(self
), v
);
1096 PyDoc_STRVAR(append_doc
,
1099 Append new value x to the end of the array.");
1103 array_byteswap(arrayobject
*self
, PyObject
*unused
)
1108 switch (self
->ob_descr
->itemsize
) {
1112 for (p
= self
->ob_item
, i
= Py_SIZE(self
); --i
>= 0; p
+= 2) {
1119 for (p
= self
->ob_item
, i
= Py_SIZE(self
); --i
>= 0; p
+= 4) {
1129 for (p
= self
->ob_item
, i
= Py_SIZE(self
); --i
>= 0; p
+= 8) {
1145 PyErr_SetString(PyExc_RuntimeError
,
1146 "don't know how to byteswap this array type");
1153 PyDoc_STRVAR(byteswap_doc
,
1156 Byteswap all items of the array. If the items in the array are not 1, 2,\n\
1157 4, or 8 bytes in size, RuntimeError is raised.");
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.");
1192 array_fromfile(arrayobject
*self
, PyObject
*args
)
1197 if (!PyArg_ParseTuple(args
, "On:fromfile", &f
, &n
))
1199 fp
= PyFile_AsFile(f
);
1201 PyErr_SetString(PyExc_TypeError
, "arg1 must be open file");
1205 char *item
= self
->ob_item
;
1206 Py_ssize_t itemsize
= self
->ob_descr
->itemsize
;
1208 Py_ssize_t newlength
;
1210 /* Be careful here about overflow */
1211 if ((newlength
= Py_SIZE(self
) + n
) <= 0 ||
1212 (newbytes
= newlength
* itemsize
) / itemsize
!=
1215 PyMem_RESIZE(item
, char, newbytes
);
1221 self
->ob_item
= item
;
1223 self
->allocated
= Py_SIZE(self
);
1224 nread
= fread(item
+ (Py_SIZE(self
) - n
) * itemsize
,
1226 if (nread
< (size_t)n
) {
1227 Py_SIZE(self
) -= (n
- nread
);
1228 PyMem_RESIZE(item
, char, Py_SIZE(self
)*itemsize
);
1229 self
->ob_item
= item
;
1230 self
->allocated
= Py_SIZE(self
);
1231 PyErr_SetString(PyExc_EOFError
,
1232 "not enough items in file");
1240 PyDoc_STRVAR(fromfile_doc
,
1243 Read n objects from the file object f and append them to the end of the\n\
1244 array. Also called as read.");
1248 array_fromfile_as_read(arrayobject
*self
, PyObject
*args
)
1250 if (PyErr_WarnPy3k("array.read() not supported in 3.x; "
1251 "use array.fromfile()", 1) < 0)
1253 return array_fromfile(self
, args
);
1258 array_tofile(arrayobject
*self
, PyObject
*f
)
1262 fp
= PyFile_AsFile(f
);
1264 PyErr_SetString(PyExc_TypeError
, "arg must be open file");
1267 if (self
->ob_size
> 0) {
1268 if (fwrite(self
->ob_item
, self
->ob_descr
->itemsize
,
1269 self
->ob_size
, fp
) != (size_t)self
->ob_size
) {
1270 PyErr_SetFromErrno(PyExc_IOError
);
1279 PyDoc_STRVAR(tofile_doc
,
1282 Write all items (as machine values) to the file object f. Also called as\n\
1287 array_tofile_as_write(arrayobject
*self
, PyObject
*f
)
1289 if (PyErr_WarnPy3k("array.write() not supported in 3.x; "
1290 "use array.tofile()", 1) < 0)
1292 return array_tofile(self
, f
);
1297 array_fromlist(arrayobject
*self
, PyObject
*list
)
1300 Py_ssize_t itemsize
= self
->ob_descr
->itemsize
;
1302 if (!PyList_Check(list
)) {
1303 PyErr_SetString(PyExc_TypeError
, "arg must be list");
1306 n
= PyList_Size(list
);
1308 char *item
= self
->ob_item
;
1310 PyMem_RESIZE(item
, char, (Py_SIZE(self
) + n
) * itemsize
);
1315 self
->ob_item
= item
;
1317 self
->allocated
= Py_SIZE(self
);
1318 for (i
= 0; i
< n
; i
++) {
1319 PyObject
*v
= PyList_GetItem(list
, i
);
1320 if ((*self
->ob_descr
->setitem
)(self
,
1321 Py_SIZE(self
) - n
+ i
, v
) != 0) {
1323 if (itemsize
&& (self
->ob_size
> PY_SSIZE_T_MAX
/ itemsize
)) {
1324 return PyErr_NoMemory();
1326 PyMem_RESIZE(item
, char,
1327 Py_SIZE(self
) * itemsize
);
1328 self
->ob_item
= item
;
1329 self
->allocated
= Py_SIZE(self
);
1338 PyDoc_STRVAR(fromlist_doc
,
1341 Append items to array from list.");
1345 array_tolist(arrayobject
*self
, PyObject
*unused
)
1347 PyObject
*list
= PyList_New(Py_SIZE(self
));
1352 for (i
= 0; i
< Py_SIZE(self
); i
++) {
1353 PyObject
*v
= getarrayitem((PyObject
*)self
, i
);
1358 PyList_SetItem(list
, i
, v
);
1363 PyDoc_STRVAR(tolist_doc
,
1364 "tolist() -> list\n\
1366 Convert array to an ordinary list with the same items.");
1370 array_fromstring(arrayobject
*self
, PyObject
*args
)
1374 int itemsize
= self
->ob_descr
->itemsize
;
1375 if (!PyArg_ParseTuple(args
, "s#:fromstring", &str
, &n
))
1377 if (n
% itemsize
!= 0) {
1378 PyErr_SetString(PyExc_ValueError
,
1379 "string length not a multiple of item size");
1384 char *item
= self
->ob_item
;
1385 if ((n
> PY_SSIZE_T_MAX
- Py_SIZE(self
)) ||
1386 ((Py_SIZE(self
) + n
) > PY_SSIZE_T_MAX
/ itemsize
)) {
1387 return PyErr_NoMemory();
1389 PyMem_RESIZE(item
, char, (Py_SIZE(self
) + n
) * itemsize
);
1394 self
->ob_item
= item
;
1396 self
->allocated
= Py_SIZE(self
);
1397 memcpy(item
+ (Py_SIZE(self
) - n
) * itemsize
,
1404 PyDoc_STRVAR(fromstring_doc
,
1405 "fromstring(string)\n\
1407 Appends items from the string, interpreting it as an array of machine\n\
1408 values,as if it had been read from a file using the fromfile() method).");
1412 array_tostring(arrayobject
*self
, PyObject
*unused
)
1414 if (self
->ob_size
<= PY_SSIZE_T_MAX
/ self
->ob_descr
->itemsize
) {
1415 return PyString_FromStringAndSize(self
->ob_item
,
1416 Py_SIZE(self
) * self
->ob_descr
->itemsize
);
1418 return PyErr_NoMemory();
1422 PyDoc_STRVAR(tostring_doc
,
1423 "tostring() -> string\n\
1425 Convert the array to an array of machine values and return the string\n\
1430 #ifdef Py_USING_UNICODE
1432 array_fromunicode(arrayobject
*self
, PyObject
*args
)
1437 if (!PyArg_ParseTuple(args
, "u#:fromunicode", &ustr
, &n
))
1439 if (self
->ob_descr
->typecode
!= 'u') {
1440 PyErr_SetString(PyExc_ValueError
,
1441 "fromunicode() may only be called on "
1446 Py_UNICODE
*item
= (Py_UNICODE
*) self
->ob_item
;
1447 if (Py_SIZE(self
) > PY_SSIZE_T_MAX
- n
) {
1448 return PyErr_NoMemory();
1450 PyMem_RESIZE(item
, Py_UNICODE
, Py_SIZE(self
) + n
);
1455 self
->ob_item
= (char *) item
;
1457 self
->allocated
= Py_SIZE(self
);
1458 memcpy(item
+ Py_SIZE(self
) - n
,
1459 ustr
, n
* sizeof(Py_UNICODE
));
1466 PyDoc_STRVAR(fromunicode_doc
,
1467 "fromunicode(ustr)\n\
1469 Extends this array with data from the unicode string ustr.\n\
1470 The array must be a type 'u' array; otherwise a ValueError\n\
1471 is raised. Use array.fromstring(ustr.decode(...)) to\n\
1472 append Unicode data to an array of some other type.");
1476 array_tounicode(arrayobject
*self
, PyObject
*unused
)
1478 if (self
->ob_descr
->typecode
!= 'u') {
1479 PyErr_SetString(PyExc_ValueError
,
1480 "tounicode() may only be called on type 'u' arrays");
1483 return PyUnicode_FromUnicode((Py_UNICODE
*) self
->ob_item
, Py_SIZE(self
));
1486 PyDoc_STRVAR(tounicode_doc
,
1487 "tounicode() -> unicode\n\
1489 Convert the array to a unicode string. The array must be\n\
1490 a type 'u' array; otherwise a ValueError is raised. Use\n\
1491 array.tostring().decode() to obtain a unicode string from\n\
1492 an array of some other type.");
1494 #endif /* Py_USING_UNICODE */
1497 array_reduce(arrayobject
*array
)
1499 PyObject
*dict
, *result
, *list
;
1501 dict
= PyObject_GetAttrString((PyObject
*)array
, "__dict__");
1503 if (!PyErr_ExceptionMatches(PyExc_AttributeError
))
1509 /* Unlike in Python 3.x, we never use the more efficient memory
1510 * representation of an array for pickling. This is unfortunately
1511 * necessary to allow array objects to be unpickled by Python 3.x,
1512 * since str objects from 2.x are always decoded to unicode in
1515 list
= array_tolist(array
, NULL
);
1520 result
= Py_BuildValue(
1521 "O(cO)O", Py_TYPE(array
), array
->ob_descr
->typecode
, list
, dict
);
1527 PyDoc_STRVAR(reduce_doc
, "Return state information for pickling.");
1530 array_get_typecode(arrayobject
*a
, void *closure
)
1532 char tc
= a
->ob_descr
->typecode
;
1533 return PyString_FromStringAndSize(&tc
, 1);
1537 array_get_itemsize(arrayobject
*a
, void *closure
)
1539 return PyInt_FromLong((long)a
->ob_descr
->itemsize
);
1542 static PyGetSetDef array_getsets
[] = {
1543 {"typecode", (getter
) array_get_typecode
, NULL
,
1544 "the typecode character used to create the array"},
1545 {"itemsize", (getter
) array_get_itemsize
, NULL
,
1546 "the size, in bytes, of one array item"},
1550 static PyMethodDef array_methods
[] = {
1551 {"append", (PyCFunction
)array_append
, METH_O
,
1553 {"buffer_info", (PyCFunction
)array_buffer_info
, METH_NOARGS
,
1555 {"byteswap", (PyCFunction
)array_byteswap
, METH_NOARGS
,
1557 {"__copy__", (PyCFunction
)array_copy
, METH_NOARGS
,
1559 {"count", (PyCFunction
)array_count
, METH_O
,
1561 {"__deepcopy__",(PyCFunction
)array_copy
, METH_O
,
1563 {"extend", (PyCFunction
)array_extend
, METH_O
,
1565 {"fromfile", (PyCFunction
)array_fromfile
, METH_VARARGS
,
1567 {"fromlist", (PyCFunction
)array_fromlist
, METH_O
,
1569 {"fromstring", (PyCFunction
)array_fromstring
, METH_VARARGS
,
1571 #ifdef Py_USING_UNICODE
1572 {"fromunicode", (PyCFunction
)array_fromunicode
, METH_VARARGS
,
1575 {"index", (PyCFunction
)array_index
, METH_O
,
1577 {"insert", (PyCFunction
)array_insert
, METH_VARARGS
,
1579 {"pop", (PyCFunction
)array_pop
, METH_VARARGS
,
1581 {"read", (PyCFunction
)array_fromfile_as_read
, METH_VARARGS
,
1583 {"__reduce__", (PyCFunction
)array_reduce
, METH_NOARGS
,
1585 {"remove", (PyCFunction
)array_remove
, METH_O
,
1587 {"reverse", (PyCFunction
)array_reverse
, METH_NOARGS
,
1589 /* {"sort", (PyCFunction)array_sort, METH_VARARGS,
1591 {"tofile", (PyCFunction
)array_tofile
, METH_O
,
1593 {"tolist", (PyCFunction
)array_tolist
, METH_NOARGS
,
1595 {"tostring", (PyCFunction
)array_tostring
, METH_NOARGS
,
1597 #ifdef Py_USING_UNICODE
1598 {"tounicode", (PyCFunction
)array_tounicode
, METH_NOARGS
,
1601 {"write", (PyCFunction
)array_tofile_as_write
, METH_O
,
1603 {NULL
, NULL
} /* sentinel */
1607 array_repr(arrayobject
*a
)
1609 char buf
[256], typecode
;
1610 PyObject
*s
, *t
, *v
= NULL
;
1614 typecode
= a
->ob_descr
->typecode
;
1616 PyOS_snprintf(buf
, sizeof(buf
), "array('%c')", typecode
);
1617 return PyString_FromString(buf
);
1620 if (typecode
== 'c')
1621 v
= array_tostring(a
, NULL
);
1622 #ifdef Py_USING_UNICODE
1623 else if (typecode
== 'u')
1624 v
= array_tounicode(a
, NULL
);
1627 v
= array_tolist(a
, NULL
);
1628 t
= PyObject_Repr(v
);
1631 PyOS_snprintf(buf
, sizeof(buf
), "array('%c', ", typecode
);
1632 s
= PyString_FromString(buf
);
1633 PyString_ConcatAndDel(&s
, t
);
1634 PyString_ConcatAndDel(&s
, PyString_FromString(")"));
1639 array_subscr(arrayobject
* self
, PyObject
* item
)
1641 if (PyIndex_Check(item
)) {
1642 Py_ssize_t i
= PyNumber_AsSsize_t(item
, PyExc_IndexError
);
1643 if (i
==-1 && PyErr_Occurred()) {
1648 return array_item(self
, i
);
1650 else if (PySlice_Check(item
)) {
1651 Py_ssize_t start
, stop
, step
, slicelength
, cur
, i
;
1654 int itemsize
= self
->ob_descr
->itemsize
;
1656 if (PySlice_GetIndicesEx((PySliceObject
*)item
, Py_SIZE(self
),
1657 &start
, &stop
, &step
, &slicelength
) < 0) {
1661 if (slicelength
<= 0) {
1662 return newarrayobject(&Arraytype
, 0, self
->ob_descr
);
1664 else if (step
== 1) {
1665 PyObject
*result
= newarrayobject(&Arraytype
,
1666 slicelength
, self
->ob_descr
);
1669 memcpy(((arrayobject
*)result
)->ob_item
,
1670 self
->ob_item
+ start
* itemsize
,
1671 slicelength
* itemsize
);
1675 result
= newarrayobject(&Arraytype
, slicelength
, self
->ob_descr
);
1676 if (!result
) return NULL
;
1678 ar
= (arrayobject
*)result
;
1680 for (cur
= start
, i
= 0; i
< slicelength
;
1682 memcpy(ar
->ob_item
+ i
*itemsize
,
1683 self
->ob_item
+ cur
*itemsize
,
1691 PyErr_SetString(PyExc_TypeError
,
1692 "array indices must be integers");
1698 array_ass_subscr(arrayobject
* self
, PyObject
* item
, PyObject
* value
)
1700 Py_ssize_t start
, stop
, step
, slicelength
, needed
;
1704 if (PyIndex_Check(item
)) {
1705 Py_ssize_t i
= PyNumber_AsSsize_t(item
, PyExc_IndexError
);
1707 if (i
== -1 && PyErr_Occurred())
1711 if (i
< 0 || i
>= Py_SIZE(self
)) {
1712 PyErr_SetString(PyExc_IndexError
,
1713 "array assignment index out of range");
1716 if (value
== NULL
) {
1717 /* Fall through to slice assignment */
1724 return (*self
->ob_descr
->setitem
)(self
, i
, value
);
1726 else if (PySlice_Check(item
)) {
1727 if (PySlice_GetIndicesEx((PySliceObject
*)item
,
1728 Py_SIZE(self
), &start
, &stop
,
1729 &step
, &slicelength
) < 0) {
1734 PyErr_SetString(PyExc_TypeError
,
1735 "array indices must be integer");
1738 if (value
== NULL
) {
1742 else if (array_Check(value
)) {
1743 other
= (arrayobject
*)value
;
1744 needed
= Py_SIZE(other
);
1745 if (self
== other
) {
1746 /* Special case "self[i:j] = self" -- copy self first */
1748 value
= array_slice(other
, 0, needed
);
1751 ret
= array_ass_subscr(self
, item
, value
);
1755 if (other
->ob_descr
!= self
->ob_descr
) {
1756 PyErr_BadArgument();
1761 PyErr_Format(PyExc_TypeError
,
1762 "can only assign array (not \"%.200s\") to array slice",
1763 Py_TYPE(value
)->tp_name
);
1766 itemsize
= self
->ob_descr
->itemsize
;
1767 /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
1768 if ((step
> 0 && stop
< start
) ||
1769 (step
< 0 && stop
> start
))
1772 if (slicelength
> needed
) {
1773 memmove(self
->ob_item
+ (start
+ needed
) * itemsize
,
1774 self
->ob_item
+ stop
* itemsize
,
1775 (Py_SIZE(self
) - stop
) * itemsize
);
1776 if (array_resize(self
, Py_SIZE(self
) +
1777 needed
- slicelength
) < 0)
1780 else if (slicelength
< needed
) {
1781 if (array_resize(self
, Py_SIZE(self
) +
1782 needed
- slicelength
) < 0)
1784 memmove(self
->ob_item
+ (start
+ needed
) * itemsize
,
1785 self
->ob_item
+ stop
* itemsize
,
1786 (Py_SIZE(self
) - start
- needed
) * itemsize
);
1789 memcpy(self
->ob_item
+ start
* itemsize
,
1790 other
->ob_item
, needed
* itemsize
);
1793 else if (needed
== 0) {
1799 start
= stop
+ step
* (slicelength
- 1) - 1;
1802 for (cur
= start
, i
= 0; i
< slicelength
;
1804 Py_ssize_t lim
= step
- 1;
1806 if (cur
+ step
>= Py_SIZE(self
))
1807 lim
= Py_SIZE(self
) - cur
- 1;
1808 memmove(self
->ob_item
+ (cur
- i
) * itemsize
,
1809 self
->ob_item
+ (cur
+ 1) * itemsize
,
1812 cur
= start
+ slicelength
* step
;
1813 if (cur
< Py_SIZE(self
)) {
1814 memmove(self
->ob_item
+ (cur
-slicelength
) * itemsize
,
1815 self
->ob_item
+ cur
* itemsize
,
1816 (Py_SIZE(self
) - cur
) * itemsize
);
1818 if (array_resize(self
, Py_SIZE(self
) - slicelength
) < 0)
1825 if (needed
!= slicelength
) {
1826 PyErr_Format(PyExc_ValueError
,
1827 "attempt to assign array of size %zd "
1828 "to extended slice of size %zd",
1829 needed
, slicelength
);
1832 for (cur
= start
, i
= 0; i
< slicelength
;
1834 memcpy(self
->ob_item
+ cur
* itemsize
,
1835 other
->ob_item
+ i
* itemsize
,
1842 static PyMappingMethods array_as_mapping
= {
1843 (lenfunc
)array_length
,
1844 (binaryfunc
)array_subscr
,
1845 (objobjargproc
)array_ass_subscr
1848 static const void *emptybuf
= "";
1851 array_buffer_getreadbuf(arrayobject
*self
, Py_ssize_t index
, const void **ptr
)
1854 PyErr_SetString(PyExc_SystemError
,
1855 "Accessing non-existent array segment");
1858 *ptr
= (void *)self
->ob_item
;
1861 return Py_SIZE(self
)*self
->ob_descr
->itemsize
;
1865 array_buffer_getwritebuf(arrayobject
*self
, Py_ssize_t index
, const void **ptr
)
1868 PyErr_SetString(PyExc_SystemError
,
1869 "Accessing non-existent array segment");
1872 *ptr
= (void *)self
->ob_item
;
1875 return Py_SIZE(self
)*self
->ob_descr
->itemsize
;
1879 array_buffer_getsegcount(arrayobject
*self
, Py_ssize_t
*lenp
)
1882 *lenp
= Py_SIZE(self
)*self
->ob_descr
->itemsize
;
1886 static PySequenceMethods array_as_sequence
= {
1887 (lenfunc
)array_length
, /*sq_length*/
1888 (binaryfunc
)array_concat
, /*sq_concat*/
1889 (ssizeargfunc
)array_repeat
, /*sq_repeat*/
1890 (ssizeargfunc
)array_item
, /*sq_item*/
1891 (ssizessizeargfunc
)array_slice
, /*sq_slice*/
1892 (ssizeobjargproc
)array_ass_item
, /*sq_ass_item*/
1893 (ssizessizeobjargproc
)array_ass_slice
, /*sq_ass_slice*/
1894 (objobjproc
)array_contains
, /*sq_contains*/
1895 (binaryfunc
)array_inplace_concat
, /*sq_inplace_concat*/
1896 (ssizeargfunc
)array_inplace_repeat
/*sq_inplace_repeat*/
1899 static PyBufferProcs array_as_buffer
= {
1900 (readbufferproc
)array_buffer_getreadbuf
,
1901 (writebufferproc
)array_buffer_getwritebuf
,
1902 (segcountproc
)array_buffer_getsegcount
,
1907 array_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1910 PyObject
*initial
= NULL
, *it
= NULL
;
1911 struct arraydescr
*descr
;
1913 if (type
== &Arraytype
&& !_PyArg_NoKeywords("array.array()", kwds
))
1916 if (!PyArg_ParseTuple(args
, "c|O:array", &c
, &initial
))
1919 if (!(initial
== NULL
|| PyList_Check(initial
)
1920 || PyString_Check(initial
) || PyTuple_Check(initial
)
1921 || (c
== 'u' && PyUnicode_Check(initial
)))) {
1922 it
= PyObject_GetIter(initial
);
1925 /* We set initial to NULL so that the subsequent code
1926 will create an empty array of the appropriate type
1927 and afterwards we can use array_iter_extend to populate
1932 for (descr
= descriptors
; descr
->typecode
!= '\0'; descr
++) {
1933 if (descr
->typecode
== c
) {
1937 if (initial
== NULL
|| !(PyList_Check(initial
)
1938 || PyTuple_Check(initial
)))
1941 len
= PySequence_Size(initial
);
1943 a
= newarrayobject(type
, len
, descr
);
1949 for (i
= 0; i
< len
; i
++) {
1951 PySequence_GetItem(initial
, i
);
1956 if (setarrayitem(a
, i
, v
) != 0) {
1963 } else if (initial
!= NULL
&& PyString_Check(initial
)) {
1964 PyObject
*t_initial
, *v
;
1965 t_initial
= PyTuple_Pack(1, initial
);
1966 if (t_initial
== NULL
) {
1970 v
= array_fromstring((arrayobject
*)a
,
1972 Py_DECREF(t_initial
);
1978 #ifdef Py_USING_UNICODE
1979 } else if (initial
!= NULL
&& PyUnicode_Check(initial
)) {
1980 Py_ssize_t n
= PyUnicode_GET_DATA_SIZE(initial
);
1982 arrayobject
*self
= (arrayobject
*)a
;
1983 char *item
= self
->ob_item
;
1984 item
= (char *)PyMem_Realloc(item
, n
);
1990 self
->ob_item
= item
;
1991 Py_SIZE(self
) = n
/ sizeof(Py_UNICODE
);
1992 memcpy(item
, PyUnicode_AS_DATA(initial
), n
);
1993 self
->allocated
= Py_SIZE(self
);
1998 if (array_iter_extend((arrayobject
*)a
, it
) == -1) {
2008 PyErr_SetString(PyExc_ValueError
,
2009 "bad typecode (must be c, b, B, u, h, H, i, I, l, L, f or d)");
2014 PyDoc_STRVAR(module_doc
,
2015 "This module defines an object type which can efficiently represent\n\
2016 an array of basic values: characters, integers, floating point\n\
2017 numbers. Arrays are sequence types and behave very much like lists,\n\
2018 except that the type of objects stored in them is constrained. The\n\
2019 type is specified at object creation time by using a type code, which\n\
2020 is a single character. The following type codes are defined:\n\
2022 Type code C Type Minimum size in bytes \n\
2024 'b' signed integer 1 \n\
2025 'B' unsigned integer 1 \n\
2026 'u' Unicode character 2 \n\
2027 'h' signed integer 2 \n\
2028 'H' unsigned integer 2 \n\
2029 'i' signed integer 2 \n\
2030 'I' unsigned integer 2 \n\
2031 'l' signed integer 4 \n\
2032 'L' unsigned integer 4 \n\
2033 'f' floating point 4 \n\
2034 'd' floating point 8 \n\
2036 The constructor is:\n\
2038 array(typecode [, initializer]) -- create a new array\n\
2041 PyDoc_STRVAR(arraytype_doc
,
2042 "array(typecode [, initializer]) -> array\n\
2044 Return a new array whose items are restricted by typecode, and\n\
2045 initialized from the optional initializer value, which must be a list,\n\
2046 string. or iterable over elements of the appropriate type.\n\
2048 Arrays represent basic values and behave very much like lists, except\n\
2049 the type of objects stored in them is constrained.\n\
2053 append() -- append a new item to the end of the array\n\
2054 buffer_info() -- return information giving the current memory info\n\
2055 byteswap() -- byteswap all the items of the array\n\
2056 count() -- return number of occurrences of an object\n\
2057 extend() -- extend array by appending multiple elements from an iterable\n\
2058 fromfile() -- read items from a file object\n\
2059 fromlist() -- append items from the list\n\
2060 fromstring() -- append items from the string\n\
2061 index() -- return index of first occurrence of an object\n\
2062 insert() -- insert a new item into the array at a provided position\n\
2063 pop() -- remove and return item (default last)\n\
2064 read() -- DEPRECATED, use fromfile()\n\
2065 remove() -- remove first occurrence of an object\n\
2066 reverse() -- reverse the order of the items in the array\n\
2067 tofile() -- write all items to a file object\n\
2068 tolist() -- return the array converted to an ordinary list\n\
2069 tostring() -- return the array converted to a string\n\
2070 write() -- DEPRECATED, use tofile()\n\
2074 typecode -- the typecode character used to create the array\n\
2075 itemsize -- the length in bytes of one array item\n\
2078 static PyObject
*array_iter(arrayobject
*ao
);
2080 static PyTypeObject Arraytype
= {
2081 PyVarObject_HEAD_INIT(NULL
, 0)
2083 sizeof(arrayobject
),
2085 (destructor
)array_dealloc
, /* tp_dealloc */
2090 (reprfunc
)array_repr
, /* tp_repr */
2091 0, /* tp_as_number*/
2092 &array_as_sequence
, /* tp_as_sequence*/
2093 &array_as_mapping
, /* tp_as_mapping*/
2097 PyObject_GenericGetAttr
, /* tp_getattro */
2098 0, /* tp_setattro */
2099 &array_as_buffer
, /* tp_as_buffer*/
2100 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_HAVE_WEAKREFS
, /* tp_flags */
2101 arraytype_doc
, /* tp_doc */
2102 0, /* tp_traverse */
2104 array_richcompare
, /* tp_richcompare */
2105 offsetof(arrayobject
, weakreflist
), /* tp_weaklistoffset */
2106 (getiterfunc
)array_iter
, /* tp_iter */
2107 0, /* tp_iternext */
2108 array_methods
, /* tp_methods */
2110 array_getsets
, /* tp_getset */
2113 0, /* tp_descr_get */
2114 0, /* tp_descr_set */
2115 0, /* tp_dictoffset */
2117 PyType_GenericAlloc
, /* tp_alloc */
2118 array_new
, /* tp_new */
2119 PyObject_Del
, /* tp_free */
2123 /*********************** Array Iterator **************************/
2129 PyObject
* (*getitem
)(struct arrayobject
*, Py_ssize_t
);
2132 static PyTypeObject PyArrayIter_Type
;
2134 #define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
2137 array_iter(arrayobject
*ao
)
2139 arrayiterobject
*it
;
2141 if (!array_Check(ao
)) {
2142 PyErr_BadInternalCall();
2146 it
= PyObject_GC_New(arrayiterobject
, &PyArrayIter_Type
);
2153 it
->getitem
= ao
->ob_descr
->getitem
;
2154 PyObject_GC_Track(it
);
2155 return (PyObject
*)it
;
2159 arrayiter_next(arrayiterobject
*it
)
2161 assert(PyArrayIter_Check(it
));
2162 if (it
->index
< Py_SIZE(it
->ao
))
2163 return (*it
->getitem
)(it
->ao
, it
->index
++);
2168 arrayiter_dealloc(arrayiterobject
*it
)
2170 PyObject_GC_UnTrack(it
);
2172 PyObject_GC_Del(it
);
2176 arrayiter_traverse(arrayiterobject
*it
, visitproc visit
, void *arg
)
2182 static PyTypeObject PyArrayIter_Type
= {
2183 PyVarObject_HEAD_INIT(NULL
, 0)
2184 "arrayiterator", /* tp_name */
2185 sizeof(arrayiterobject
), /* tp_basicsize */
2186 0, /* tp_itemsize */
2188 (destructor
)arrayiter_dealloc
, /* tp_dealloc */
2194 0, /* tp_as_number */
2195 0, /* tp_as_sequence */
2196 0, /* tp_as_mapping */
2200 PyObject_GenericGetAttr
, /* tp_getattro */
2201 0, /* tp_setattro */
2202 0, /* tp_as_buffer */
2203 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
,/* tp_flags */
2205 (traverseproc
)arrayiter_traverse
, /* tp_traverse */
2207 0, /* tp_richcompare */
2208 0, /* tp_weaklistoffset */
2209 PyObject_SelfIter
, /* tp_iter */
2210 (iternextfunc
)arrayiter_next
, /* tp_iternext */
2215 /*********************** Install Module **************************/
2217 /* No functions in array module. */
2218 static PyMethodDef a_methods
[] = {
2219 {NULL
, NULL
, 0, NULL
} /* Sentinel */
2228 Arraytype
.ob_type
= &PyType_Type
;
2229 PyArrayIter_Type
.ob_type
= &PyType_Type
;
2230 m
= Py_InitModule3("array", a_methods
, module_doc
);
2234 Py_INCREF((PyObject
*)&Arraytype
);
2235 PyModule_AddObject(m
, "ArrayType", (PyObject
*)&Arraytype
);
2236 Py_INCREF((PyObject
*)&Arraytype
);
2237 PyModule_AddObject(m
, "array", (PyObject
*)&Arraytype
);
2238 /* No need to check the error here, the caller will do that */