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 */
10 #else /* !STDC_HEADERS */
11 #ifndef DONT_HAVE_SYS_TYPES_H
12 #include <sys/types.h> /* For size_t */
13 #endif /* DONT_HAVE_SYS_TYPES_H */
14 #endif /* !STDC_HEADERS */
16 /* Shamelessy stolen from listobject.c */
20 unsigned int nbits
= 0;
21 unsigned int n2
= (unsigned int)n
>> 5;
24 * If n < 256, to a multiple of 8.
25 * If n < 2048, to a multiple of 64.
26 * If n < 16384, to a multiple of 512.
27 * If n < 131072, to a multiple of 4096.
28 * If n < 1048576, to a multiple of 32768.
29 * If n < 8388608, to a multiple of 262144.
30 * If n < 67108864, to a multiple of 2097152.
31 * If n < 536870912, to a multiple of 16777216.
33 * If n < 2**(5+3*i), to a multiple of 2**(3*i).
35 * This over-allocates proportional to the list size, making room
36 * for additional growth. The over-allocation is mild, but is
37 * enough to give linear-time amortized behavior over a long
38 * sequence of appends() in the presence of a poorly-performing
39 * system realloc() (which is a reality, e.g., across all flavors
40 * of Windows, with Win9x behavior being particularly bad -- and
41 * we've still got address space fragmentation problems on Win9x
42 * even with this scheme, although it requires much longer lists to
43 * provoke them than it used to).
49 return ((n
>> nbits
) + 1) << nbits
;
52 #define NRESIZE(var, type, nitems) \
54 size_t _new_size = roundupsize(nitems); \
55 if (_new_size <= ((~(size_t)0) / sizeof(type))) \
56 PyMem_RESIZE(var, type, _new_size); \
60 /* END SHAMELESSLY STOLEN CODE */
62 struct arrayobject
; /* Forward */
64 /* All possible arraydescr values are defined in the vector "descriptors"
65 * below. That's defined later because the appropriate get and set
66 * functions aren't visible yet.
71 PyObject
* (*getitem
)(struct arrayobject
*, int);
72 int (*setitem
)(struct arrayobject
*, int, PyObject
*);
75 typedef struct arrayobject
{
79 struct arraydescr
*ob_descr
;
82 static PyTypeObject Arraytype
;
84 #define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
85 #define array_CheckExact(op) ((op)->ob_type == &Arraytype)
87 /****************************************************************************
88 Get and Set functions for each type.
89 A Get function takes an arrayobject* and an integer index, returning the
90 array value at that index wrapped in an appropriate PyObject*.
91 A Set function takes an arrayobject, integer index, and PyObject*; sets
92 the array value at that index to the raw C data extracted from the PyObject*,
93 and returns 0 if successful, else nonzero on failure (PyObject* not of an
94 appropriate type or value).
95 Note that the basic Get and Set functions do NOT check that the index is
96 in bounds; that's the responsibility of the caller.
97 ****************************************************************************/
100 c_getitem(arrayobject
*ap
, int i
)
102 return PyString_FromStringAndSize(&((char *)ap
->ob_item
)[i
], 1);
106 c_setitem(arrayobject
*ap
, int i
, PyObject
*v
)
109 if (!PyArg_Parse(v
, "c;array item must be char", &x
))
112 ((char *)ap
->ob_item
)[i
] = x
;
117 b_getitem(arrayobject
*ap
, int i
)
119 long x
= ((char *)ap
->ob_item
)[i
];
122 return PyInt_FromLong(x
);
126 b_setitem(arrayobject
*ap
, int i
, PyObject
*v
)
129 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
130 must use the next size up that is signed ('h') and manually do
131 the overflow checking */
132 if (!PyArg_Parse(v
, "h;array item must be integer", &x
))
135 PyErr_SetString(PyExc_OverflowError
,
136 "signed char is less than minimum");
140 PyErr_SetString(PyExc_OverflowError
,
141 "signed char is greater than maximum");
145 ((char *)ap
->ob_item
)[i
] = (char)x
;
150 BB_getitem(arrayobject
*ap
, int i
)
152 long x
= ((unsigned char *)ap
->ob_item
)[i
];
153 return PyInt_FromLong(x
);
157 BB_setitem(arrayobject
*ap
, int i
, PyObject
*v
)
160 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
161 if (!PyArg_Parse(v
, "b;array item must be integer", &x
))
164 ((char *)ap
->ob_item
)[i
] = x
;
168 #ifdef Py_USING_UNICODE
170 u_getitem(arrayobject
*ap
, int i
)
172 return PyUnicode_FromUnicode(&((Py_UNICODE
*) ap
->ob_item
)[i
], 1);
176 u_setitem(arrayobject
*ap
, int i
, PyObject
*v
)
181 if (!PyArg_Parse(v
, "u#;array item must be unicode character", &p
, &len
))
184 PyErr_SetString(PyExc_TypeError
, "array item must be unicode character");
188 ((Py_UNICODE
*)ap
->ob_item
)[i
] = p
[0];
194 h_getitem(arrayobject
*ap
, int i
)
196 return PyInt_FromLong((long) ((short *)ap
->ob_item
)[i
]);
200 h_setitem(arrayobject
*ap
, int i
, PyObject
*v
)
203 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
204 if (!PyArg_Parse(v
, "h;array item must be integer", &x
))
207 ((short *)ap
->ob_item
)[i
] = x
;
212 HH_getitem(arrayobject
*ap
, int i
)
214 return PyInt_FromLong((long) ((unsigned short *)ap
->ob_item
)[i
]);
218 HH_setitem(arrayobject
*ap
, int i
, PyObject
*v
)
221 /* PyArg_Parse's 'h' formatter is for a signed short, therefore
222 must use the next size up and manually do the overflow checking */
223 if (!PyArg_Parse(v
, "i;array item must be integer", &x
))
226 PyErr_SetString(PyExc_OverflowError
,
227 "unsigned short is less than minimum");
230 else if (x
> USHRT_MAX
) {
231 PyErr_SetString(PyExc_OverflowError
,
232 "unsigned short is greater than maximum");
236 ((short *)ap
->ob_item
)[i
] = (short)x
;
241 i_getitem(arrayobject
*ap
, int i
)
243 return PyInt_FromLong((long) ((int *)ap
->ob_item
)[i
]);
247 i_setitem(arrayobject
*ap
, int i
, PyObject
*v
)
250 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
251 if (!PyArg_Parse(v
, "i;array item must be integer", &x
))
254 ((int *)ap
->ob_item
)[i
] = x
;
259 II_getitem(arrayobject
*ap
, int i
)
261 return PyLong_FromUnsignedLong(
262 (unsigned long) ((unsigned int *)ap
->ob_item
)[i
]);
266 II_setitem(arrayobject
*ap
, int i
, PyObject
*v
)
269 if (PyLong_Check(v
)) {
270 x
= PyLong_AsUnsignedLong(v
);
271 if (x
== (unsigned long) -1 && PyErr_Occurred())
276 if (!PyArg_Parse(v
, "l;array item must be integer", &y
))
279 PyErr_SetString(PyExc_OverflowError
,
280 "unsigned int is less than minimum");
283 x
= (unsigned long)y
;
287 PyErr_SetString(PyExc_OverflowError
,
288 "unsigned int is greater than maximum");
293 ((unsigned int *)ap
->ob_item
)[i
] = (unsigned int)x
;
298 l_getitem(arrayobject
*ap
, int i
)
300 return PyInt_FromLong(((long *)ap
->ob_item
)[i
]);
304 l_setitem(arrayobject
*ap
, int i
, PyObject
*v
)
307 if (!PyArg_Parse(v
, "l;array item must be integer", &x
))
310 ((long *)ap
->ob_item
)[i
] = x
;
315 LL_getitem(arrayobject
*ap
, int i
)
317 return PyLong_FromUnsignedLong(((unsigned long *)ap
->ob_item
)[i
]);
321 LL_setitem(arrayobject
*ap
, int i
, PyObject
*v
)
324 if (PyLong_Check(v
)) {
325 x
= PyLong_AsUnsignedLong(v
);
326 if (x
== (unsigned long) -1 && PyErr_Occurred())
331 if (!PyArg_Parse(v
, "l;array item must be integer", &y
))
334 PyErr_SetString(PyExc_OverflowError
,
335 "unsigned long is less than minimum");
338 x
= (unsigned long)y
;
342 PyErr_SetString(PyExc_OverflowError
,
343 "unsigned long is greater than maximum");
348 ((unsigned long *)ap
->ob_item
)[i
] = x
;
353 f_getitem(arrayobject
*ap
, int i
)
355 return PyFloat_FromDouble((double) ((float *)ap
->ob_item
)[i
]);
359 f_setitem(arrayobject
*ap
, int i
, PyObject
*v
)
362 if (!PyArg_Parse(v
, "f;array item must be float", &x
))
365 ((float *)ap
->ob_item
)[i
] = x
;
370 d_getitem(arrayobject
*ap
, int i
)
372 return PyFloat_FromDouble(((double *)ap
->ob_item
)[i
]);
376 d_setitem(arrayobject
*ap
, int i
, PyObject
*v
)
379 if (!PyArg_Parse(v
, "d;array item must be float", &x
))
382 ((double *)ap
->ob_item
)[i
] = x
;
386 /* Description of types */
387 static struct arraydescr descriptors
[] = {
388 {'c', sizeof(char), c_getitem
, c_setitem
},
389 {'b', sizeof(char), b_getitem
, b_setitem
},
390 {'B', sizeof(char), BB_getitem
, BB_setitem
},
391 #ifdef Py_USING_UNICODE
392 {'u', sizeof(Py_UNICODE
), u_getitem
, u_setitem
},
394 {'h', sizeof(short), h_getitem
, h_setitem
},
395 {'H', sizeof(short), HH_getitem
, HH_setitem
},
396 {'i', sizeof(int), i_getitem
, i_setitem
},
397 {'I', sizeof(int), II_getitem
, II_setitem
},
398 {'l', sizeof(long), l_getitem
, l_setitem
},
399 {'L', sizeof(long), LL_getitem
, LL_setitem
},
400 {'f', sizeof(float), f_getitem
, f_setitem
},
401 {'d', sizeof(double), d_getitem
, d_setitem
},
402 {'\0', 0, 0, 0} /* Sentinel */
405 /****************************************************************************
406 Implementations of array object methods.
407 ****************************************************************************/
410 newarrayobject(PyTypeObject
*type
, int size
, struct arraydescr
*descr
)
416 PyErr_BadInternalCall();
420 nbytes
= size
* descr
->itemsize
;
421 /* Check for overflow */
422 if (nbytes
/ descr
->itemsize
!= (size_t)size
) {
423 return PyErr_NoMemory();
425 op
= (arrayobject
*) type
->tp_alloc(type
, 0);
434 op
->ob_item
= PyMem_NEW(char, nbytes
);
435 if (op
->ob_item
== NULL
) {
437 return PyErr_NoMemory();
440 op
->ob_descr
= descr
;
441 return (PyObject
*) op
;
445 getarrayitem(PyObject
*op
, int i
)
447 register arrayobject
*ap
;
448 assert(array_Check(op
));
449 ap
= (arrayobject
*)op
;
450 assert(i
>=0 && i
<ap
->ob_size
);
451 return (*ap
->ob_descr
->getitem
)(ap
, i
);
455 ins1(arrayobject
*self
, int where
, PyObject
*v
)
459 PyErr_BadInternalCall();
462 if ((*self
->ob_descr
->setitem
)(self
, -1, v
) < 0)
464 items
= self
->ob_item
;
465 NRESIZE(items
, char, (self
->ob_size
+1) * self
->ob_descr
->itemsize
);
471 where
+= self
->ob_size
;
475 if (where
> self
->ob_size
)
476 where
= self
->ob_size
;
477 memmove(items
+ (where
+1)*self
->ob_descr
->itemsize
,
478 items
+ where
*self
->ob_descr
->itemsize
,
479 (self
->ob_size
-where
)*self
->ob_descr
->itemsize
);
480 self
->ob_item
= items
;
482 return (*self
->ob_descr
->setitem
)(self
, where
, v
);
488 array_dealloc(arrayobject
*op
)
490 if (op
->ob_item
!= NULL
)
491 PyMem_DEL(op
->ob_item
);
492 op
->ob_type
->tp_free((PyObject
*)op
);
496 array_richcompare(PyObject
*v
, PyObject
*w
, int op
)
498 arrayobject
*va
, *wa
;
504 if (!array_Check(v
) || !array_Check(w
)) {
505 Py_INCREF(Py_NotImplemented
);
506 return Py_NotImplemented
;
509 va
= (arrayobject
*)v
;
510 wa
= (arrayobject
*)w
;
512 if (va
->ob_size
!= wa
->ob_size
&& (op
== Py_EQ
|| op
== Py_NE
)) {
513 /* Shortcut: if the lengths differ, the arrays differ */
522 /* Search for the first index where items are different */
524 for (i
= 0; i
< va
->ob_size
&& i
< wa
->ob_size
; i
++) {
525 vi
= getarrayitem(v
, i
);
526 wi
= getarrayitem(w
, i
);
527 if (vi
== NULL
|| wi
== NULL
) {
532 k
= PyObject_RichCompareBool(vi
, wi
, Py_EQ
);
534 break; /* Keeping vi and wi alive! */
542 /* No more items to compare -- compare sizes */
543 int vs
= va
->ob_size
;
544 int ws
= wa
->ob_size
;
547 case Py_LT
: cmp
= vs
< ws
; break;
548 case Py_LE
: cmp
= vs
<= ws
; break;
549 case Py_EQ
: cmp
= vs
== ws
; break;
550 case Py_NE
: cmp
= vs
!= ws
; break;
551 case Py_GT
: cmp
= vs
> ws
; break;
552 case Py_GE
: cmp
= vs
>= ws
; break;
553 default: return NULL
; /* cannot happen */
563 /* We have an item that differs. First, shortcuts for EQ/NE */
568 else if (op
== Py_NE
) {
573 /* Compare the final item again using the proper operator */
574 res
= PyObject_RichCompare(vi
, wi
, op
);
582 array_length(arrayobject
*a
)
588 array_item(arrayobject
*a
, int i
)
590 if (i
< 0 || i
>= a
->ob_size
) {
591 PyErr_SetString(PyExc_IndexError
, "array index out of range");
594 return getarrayitem((PyObject
*)a
, i
);
598 array_slice(arrayobject
*a
, int ilow
, int ihigh
)
603 else if (ilow
> a
->ob_size
)
609 else if (ihigh
> a
->ob_size
)
611 np
= (arrayobject
*) newarrayobject(&Arraytype
, ihigh
- ilow
, a
->ob_descr
);
614 memcpy(np
->ob_item
, a
->ob_item
+ ilow
* a
->ob_descr
->itemsize
,
615 (ihigh
-ilow
) * a
->ob_descr
->itemsize
);
616 return (PyObject
*)np
;
620 array_concat(arrayobject
*a
, PyObject
*bb
)
624 if (!array_Check(bb
)) {
625 PyErr_Format(PyExc_TypeError
,
626 "can only append array (not \"%.200s\") to array",
627 bb
->ob_type
->tp_name
);
630 #define b ((arrayobject *)bb)
631 if (a
->ob_descr
!= b
->ob_descr
) {
635 size
= a
->ob_size
+ b
->ob_size
;
636 np
= (arrayobject
*) newarrayobject(&Arraytype
, size
, a
->ob_descr
);
640 memcpy(np
->ob_item
, a
->ob_item
, a
->ob_size
*a
->ob_descr
->itemsize
);
641 memcpy(np
->ob_item
+ a
->ob_size
*a
->ob_descr
->itemsize
,
642 b
->ob_item
, b
->ob_size
*b
->ob_descr
->itemsize
);
643 return (PyObject
*)np
;
648 array_repeat(arrayobject
*a
, int n
)
657 size
= a
->ob_size
* n
;
658 np
= (arrayobject
*) newarrayobject(&Arraytype
, size
, a
->ob_descr
);
662 nbytes
= a
->ob_size
* a
->ob_descr
->itemsize
;
663 for (i
= 0; i
< n
; i
++) {
664 memcpy(p
, a
->ob_item
, nbytes
);
667 return (PyObject
*) np
;
671 array_ass_slice(arrayobject
*a
, int ilow
, int ihigh
, PyObject
*v
)
674 int n
; /* Size of replacement array */
675 int d
; /* Change in size */
676 #define b ((arrayobject *)v)
679 else if (array_Check(v
)) {
682 /* Special case "a[i:j] = a" -- copy b first */
684 v
= array_slice(b
, 0, n
);
685 ret
= array_ass_slice(a
, ilow
, ihigh
, v
);
689 if (b
->ob_descr
!= a
->ob_descr
) {
695 PyErr_Format(PyExc_TypeError
,
696 "can only assign array (not \"%.200s\") to array slice",
697 v
->ob_type
->tp_name
);
702 else if (ilow
> a
->ob_size
)
708 else if (ihigh
> a
->ob_size
)
711 d
= n
- (ihigh
-ilow
);
712 if (d
< 0) { /* Delete -d items */
713 memmove(item
+ (ihigh
+d
)*a
->ob_descr
->itemsize
,
714 item
+ ihigh
*a
->ob_descr
->itemsize
,
715 (a
->ob_size
-ihigh
)*a
->ob_descr
->itemsize
);
717 PyMem_RESIZE(item
, char, a
->ob_size
*a
->ob_descr
->itemsize
);
721 else if (d
> 0) { /* Insert d items */
722 PyMem_RESIZE(item
, char,
723 (a
->ob_size
+ d
)*a
->ob_descr
->itemsize
);
728 memmove(item
+ (ihigh
+d
)*a
->ob_descr
->itemsize
,
729 item
+ ihigh
*a
->ob_descr
->itemsize
,
730 (a
->ob_size
-ihigh
)*a
->ob_descr
->itemsize
);
735 memcpy(item
+ ilow
*a
->ob_descr
->itemsize
, b
->ob_item
,
736 n
*b
->ob_descr
->itemsize
);
742 array_ass_item(arrayobject
*a
, int i
, PyObject
*v
)
744 if (i
< 0 || i
>= a
->ob_size
) {
745 PyErr_SetString(PyExc_IndexError
,
746 "array assignment index out of range");
750 return array_ass_slice(a
, i
, i
+1, v
);
751 return (*a
->ob_descr
->setitem
)(a
, i
, v
);
755 setarrayitem(PyObject
*a
, int i
, PyObject
*v
)
757 assert(array_Check(a
));
758 return array_ass_item((arrayobject
*)a
, i
, v
);
762 array_do_extend(arrayobject
*self
, PyObject
*bb
)
766 if (!array_Check(bb
)) {
767 PyErr_Format(PyExc_TypeError
,
768 "can only extend array with array (not \"%.200s\")",
769 bb
->ob_type
->tp_name
);
772 #define b ((arrayobject *)bb)
773 if (self
->ob_descr
!= b
->ob_descr
) {
774 PyErr_SetString(PyExc_TypeError
,
775 "can only extend with array of same kind");
778 size
= self
->ob_size
+ b
->ob_size
;
779 PyMem_RESIZE(self
->ob_item
, char, size
*self
->ob_descr
->itemsize
);
780 if (self
->ob_item
== NULL
) {
785 memcpy(self
->ob_item
+ self
->ob_size
*self
->ob_descr
->itemsize
,
786 b
->ob_item
, b
->ob_size
*b
->ob_descr
->itemsize
);
787 self
->ob_size
= size
;
794 array_inplace_concat(arrayobject
*self
, PyObject
*bb
)
796 if (array_do_extend(self
, bb
) == -1)
799 return (PyObject
*)self
;
803 array_inplace_repeat(arrayobject
*self
, int n
)
808 if (self
->ob_size
> 0) {
811 items
= self
->ob_item
;
812 size
= self
->ob_size
* self
->ob_descr
->itemsize
;
815 self
->ob_item
= NULL
;
819 PyMem_Resize(items
, char, n
* size
);
821 return PyErr_NoMemory();
823 for (i
= 1; i
< n
; i
++) {
825 memcpy(p
, items
, size
);
827 self
->ob_item
= items
;
832 return (PyObject
*)self
;
837 ins(arrayobject
*self
, int where
, PyObject
*v
)
839 if (ins1(self
, where
, v
) != 0)
846 array_count(arrayobject
*self
, PyObject
*v
)
851 for (i
= 0; i
< self
->ob_size
; i
++) {
852 PyObject
*selfi
= getarrayitem((PyObject
*)self
, i
);
853 int cmp
= PyObject_RichCompareBool(selfi
, v
, Py_EQ
);
860 return PyInt_FromLong((long)count
);
863 PyDoc_STRVAR(count_doc
,
866 Return number of occurences of x in the array.");
869 array_index(arrayobject
*self
, PyObject
*v
)
873 for (i
= 0; i
< self
->ob_size
; i
++) {
874 PyObject
*selfi
= getarrayitem((PyObject
*)self
, i
);
875 int cmp
= PyObject_RichCompareBool(selfi
, v
, Py_EQ
);
878 return PyInt_FromLong((long)i
);
883 PyErr_SetString(PyExc_ValueError
, "array.index(x): x not in list");
887 PyDoc_STRVAR(index_doc
,
890 Return index of first occurence of x in the array.");
893 array_contains(arrayobject
*self
, PyObject
*v
)
897 for (i
= 0, cmp
= 0 ; cmp
== 0 && i
< self
->ob_size
; i
++) {
898 PyObject
*selfi
= getarrayitem((PyObject
*)self
, i
);
899 cmp
= PyObject_RichCompareBool(selfi
, v
, Py_EQ
);
906 array_remove(arrayobject
*self
, PyObject
*v
)
910 for (i
= 0; i
< self
->ob_size
; i
++) {
911 PyObject
*selfi
= getarrayitem((PyObject
*)self
,i
);
912 int cmp
= PyObject_RichCompareBool(selfi
, v
, Py_EQ
);
915 if (array_ass_slice(self
, i
, i
+1,
916 (PyObject
*)NULL
) != 0)
924 PyErr_SetString(PyExc_ValueError
, "array.remove(x): x not in list");
928 PyDoc_STRVAR(remove_doc
,
931 Remove the first occurence of x in the array.");
934 array_pop(arrayobject
*self
, PyObject
*args
)
938 if (!PyArg_ParseTuple(args
, "|i:pop", &i
))
940 if (self
->ob_size
== 0) {
941 /* Special-case most common failure cause */
942 PyErr_SetString(PyExc_IndexError
, "pop from empty array");
947 if (i
< 0 || i
>= self
->ob_size
) {
948 PyErr_SetString(PyExc_IndexError
, "pop index out of range");
951 v
= getarrayitem((PyObject
*)self
,i
);
952 if (array_ass_slice(self
, i
, i
+1, (PyObject
*)NULL
) != 0) {
959 PyDoc_STRVAR(pop_doc
,
962 Return the i-th element and delete it from the array. i defaults to -1.");
965 array_extend(arrayobject
*self
, PyObject
*bb
)
967 if (array_do_extend(self
, bb
) == -1)
973 PyDoc_STRVAR(extend_doc
,
976 Append array items to the end of the array.");
979 array_insert(arrayobject
*self
, PyObject
*args
)
983 if (!PyArg_ParseTuple(args
, "iO:insert", &i
, &v
))
985 return ins(self
, i
, v
);
988 PyDoc_STRVAR(insert_doc
,
991 Insert a new item x into the array before position i.");
995 array_buffer_info(arrayobject
*self
, PyObject
*unused
)
997 PyObject
* retval
= NULL
;
998 retval
= PyTuple_New(2);
1002 PyTuple_SET_ITEM(retval
, 0, PyLong_FromVoidPtr(self
->ob_item
));
1003 PyTuple_SET_ITEM(retval
, 1, PyInt_FromLong((long)(self
->ob_size
)));
1008 PyDoc_STRVAR(buffer_info_doc
,
1009 "buffer_info() -> (address, length)\n\
1011 Return a tuple (address, length) giving the current memory address and\n\
1012 the length in items of the buffer used to hold array's contents\n\
1013 The length should be multiplied by the itemsize attribute to calculate\n\
1014 the buffer length in bytes.");
1018 array_append(arrayobject
*self
, PyObject
*v
)
1020 return ins(self
, (int) self
->ob_size
, v
);
1023 PyDoc_STRVAR(append_doc
,
1026 Append new value x to the end of the array.");
1030 array_byteswap(arrayobject
*self
, PyObject
*unused
)
1035 switch (self
->ob_descr
->itemsize
) {
1039 for (p
= self
->ob_item
, i
= self
->ob_size
; --i
>= 0; p
+= 2) {
1046 for (p
= self
->ob_item
, i
= self
->ob_size
; --i
>= 0; p
+= 4) {
1056 for (p
= self
->ob_item
, i
= self
->ob_size
; --i
>= 0; p
+= 8) {
1072 PyErr_SetString(PyExc_RuntimeError
,
1073 "don't know how to byteswap this array type");
1080 PyDoc_STRVAR(byteswap_doc
,
1083 Byteswap all items of the array. If the items in the array are not 1, 2,\n\
1084 4, or 8 bytes in size, RuntimeError is raised.");
1087 array_reverse(arrayobject
*self
, PyObject
*unused
)
1089 register int itemsize
= self
->ob_descr
->itemsize
;
1090 register char *p
, *q
;
1091 /* little buffer to hold items while swapping */
1092 char tmp
[256]; /* 8 is probably enough -- but why skimp */
1093 assert(itemsize
<= sizeof(tmp
));
1095 if (self
->ob_size
> 1) {
1096 for (p
= self
->ob_item
,
1097 q
= self
->ob_item
+ (self
->ob_size
- 1)*itemsize
;
1099 p
+= itemsize
, q
-= itemsize
) {
1100 /* memory areas guaranteed disjoint, so memcpy
1101 * is safe (& memmove may be slower).
1103 memcpy(tmp
, p
, itemsize
);
1104 memcpy(p
, q
, itemsize
);
1105 memcpy(q
, tmp
, itemsize
);
1113 PyDoc_STRVAR(reverse_doc
,
1116 Reverse the order of the items in the array.");
1119 array_fromfile(arrayobject
*self
, PyObject
*args
)
1124 if (!PyArg_ParseTuple(args
, "Oi:fromfile", &f
, &n
))
1126 fp
= PyFile_AsFile(f
);
1128 PyErr_SetString(PyExc_TypeError
, "arg1 must be open file");
1132 char *item
= self
->ob_item
;
1133 int itemsize
= self
->ob_descr
->itemsize
;
1137 /* Be careful here about overflow */
1138 if ((newlength
= self
->ob_size
+ n
) <= 0 ||
1139 (newbytes
= newlength
* itemsize
) / itemsize
!=
1142 PyMem_RESIZE(item
, char, newbytes
);
1148 self
->ob_item
= item
;
1150 nread
= fread(item
+ (self
->ob_size
- n
) * itemsize
,
1152 if (nread
< (size_t)n
) {
1153 self
->ob_size
-= (n
- nread
);
1154 PyMem_RESIZE(item
, char, self
->ob_size
*itemsize
);
1155 self
->ob_item
= item
;
1156 PyErr_SetString(PyExc_EOFError
,
1157 "not enough items in file");
1165 PyDoc_STRVAR(fromfile_doc
,
1168 Read n objects from the file object f and append them to the end of the\n\
1169 array. Also called as read.");
1173 array_tofile(arrayobject
*self
, PyObject
*f
)
1177 fp
= PyFile_AsFile(f
);
1179 PyErr_SetString(PyExc_TypeError
, "arg must be open file");
1182 if (self
->ob_size
> 0) {
1183 if (fwrite(self
->ob_item
, self
->ob_descr
->itemsize
,
1184 self
->ob_size
, fp
) != (size_t)self
->ob_size
) {
1185 PyErr_SetFromErrno(PyExc_IOError
);
1194 PyDoc_STRVAR(tofile_doc
,
1197 Write all items (as machine values) to the file object f. Also called as\n\
1202 array_fromlist(arrayobject
*self
, PyObject
*list
)
1205 int itemsize
= self
->ob_descr
->itemsize
;
1207 if (!PyList_Check(list
)) {
1208 PyErr_SetString(PyExc_TypeError
, "arg must be list");
1211 n
= PyList_Size(list
);
1213 char *item
= self
->ob_item
;
1215 PyMem_RESIZE(item
, char, (self
->ob_size
+ n
) * itemsize
);
1220 self
->ob_item
= item
;
1222 for (i
= 0; i
< n
; i
++) {
1223 PyObject
*v
= PyList_GetItem(list
, i
);
1224 if ((*self
->ob_descr
->setitem
)(self
,
1225 self
->ob_size
- n
+ i
, v
) != 0) {
1227 PyMem_RESIZE(item
, char,
1228 self
->ob_size
* itemsize
);
1229 self
->ob_item
= item
;
1238 PyDoc_STRVAR(fromlist_doc
,
1241 Append items to array from list.");
1245 array_tolist(arrayobject
*self
, PyObject
*unused
)
1247 PyObject
*list
= PyList_New(self
->ob_size
);
1252 for (i
= 0; i
< self
->ob_size
; i
++) {
1253 PyObject
*v
= getarrayitem((PyObject
*)self
, i
);
1258 PyList_SetItem(list
, i
, v
);
1263 PyDoc_STRVAR(tolist_doc
,
1264 "tolist() -> list\n\
1266 Convert array to an ordinary list with the same items.");
1270 array_fromstring(arrayobject
*self
, PyObject
*args
)
1274 int itemsize
= self
->ob_descr
->itemsize
;
1275 if (!PyArg_ParseTuple(args
, "s#:fromstring", &str
, &n
))
1277 if (n
% itemsize
!= 0) {
1278 PyErr_SetString(PyExc_ValueError
,
1279 "string length not a multiple of item size");
1284 char *item
= self
->ob_item
;
1285 PyMem_RESIZE(item
, char, (self
->ob_size
+ n
) * itemsize
);
1290 self
->ob_item
= item
;
1292 memcpy(item
+ (self
->ob_size
- n
) * itemsize
,
1299 PyDoc_STRVAR(fromstring_doc
,
1300 "fromstring(string)\n\
1302 Appends items from the string, interpreting it as an array of machine\n\
1303 values,as if it had been read from a file using the fromfile() method).");
1307 array_tostring(arrayobject
*self
, PyObject
*unused
)
1309 return PyString_FromStringAndSize(self
->ob_item
,
1310 self
->ob_size
* self
->ob_descr
->itemsize
);
1313 PyDoc_STRVAR(tostring_doc
,
1314 "tostring() -> string\n\
1316 Convert the array to an array of machine values and return the string\n\
1321 #ifdef Py_USING_UNICODE
1323 array_fromunicode(arrayobject
*self
, PyObject
*args
)
1328 if (!PyArg_ParseTuple(args
, "u#:fromunicode", &ustr
, &n
))
1330 if (self
->ob_descr
->typecode
!= 'u') {
1331 PyErr_SetString(PyExc_ValueError
,
1332 "fromunicode() may only be called on "
1337 Py_UNICODE
*item
= (Py_UNICODE
*) self
->ob_item
;
1338 PyMem_RESIZE(item
, Py_UNICODE
, self
->ob_size
+ n
);
1343 self
->ob_item
= (char *) item
;
1345 memcpy(item
+ self
->ob_size
- n
,
1346 ustr
, n
* sizeof(Py_UNICODE
));
1353 PyDoc_STRVAR(fromunicode_doc
,
1354 "fromunicode(ustr)\n\
1356 Extends this array with data from the unicode string ustr.\n\
1357 The array must be a type 'u' array; otherwise a ValueError\n\
1358 is raised. Use array.fromstring(ustr.decode(...)) to\n\
1359 append Unicode data to an array of some other type.");
1363 array_tounicode(arrayobject
*self
, PyObject
*unused
)
1365 if (self
->ob_descr
->typecode
!= 'u') {
1366 PyErr_SetString(PyExc_ValueError
,
1367 "tounicode() may only be called on type 'u' arrays");
1370 return PyUnicode_FromUnicode((Py_UNICODE
*) self
->ob_item
, self
->ob_size
);
1373 PyDoc_STRVAR(tounicode_doc
,
1374 "tounicode() -> unicode\n\
1376 Convert the array to a unicode string. The array must be\n\
1377 a type 'u' array; otherwise a ValueError is raised. Use\n\
1378 array.tostring().decode() to obtain a unicode string from\n\
1379 an array of some other type.");
1381 #endif /* Py_USING_UNICODE */
1385 array_get_typecode(arrayobject
*a
, void *closure
)
1387 char tc
= a
->ob_descr
->typecode
;
1388 return PyString_FromStringAndSize(&tc
, 1);
1392 array_get_itemsize(arrayobject
*a
, void *closure
)
1394 return PyInt_FromLong((long)a
->ob_descr
->itemsize
);
1397 static PyGetSetDef array_getsets
[] = {
1398 {"typecode", (getter
) array_get_typecode
, NULL
,
1399 "the typecode character used to create the array"},
1400 {"itemsize", (getter
) array_get_itemsize
, NULL
,
1401 "the size, in bytes, of one array item"},
1405 PyMethodDef array_methods
[] = {
1406 {"append", (PyCFunction
)array_append
, METH_O
,
1408 {"buffer_info", (PyCFunction
)array_buffer_info
, METH_NOARGS
,
1410 {"byteswap", (PyCFunction
)array_byteswap
, METH_NOARGS
,
1412 {"count", (PyCFunction
)array_count
, METH_O
,
1414 {"extend", (PyCFunction
)array_extend
, METH_O
,
1416 {"fromfile", (PyCFunction
)array_fromfile
, METH_VARARGS
,
1418 {"fromlist", (PyCFunction
)array_fromlist
, METH_O
,
1420 {"fromstring", (PyCFunction
)array_fromstring
, METH_VARARGS
,
1422 #ifdef Py_USING_UNICODE
1423 {"fromunicode", (PyCFunction
)array_fromunicode
, METH_VARARGS
,
1426 {"index", (PyCFunction
)array_index
, METH_O
,
1428 {"insert", (PyCFunction
)array_insert
, METH_VARARGS
,
1430 {"pop", (PyCFunction
)array_pop
, METH_VARARGS
,
1432 {"read", (PyCFunction
)array_fromfile
, METH_VARARGS
,
1434 {"remove", (PyCFunction
)array_remove
, METH_O
,
1436 {"reverse", (PyCFunction
)array_reverse
, METH_NOARGS
,
1438 /* {"sort", (PyCFunction)array_sort, METH_VARARGS,
1440 {"tofile", (PyCFunction
)array_tofile
, METH_O
,
1442 {"tolist", (PyCFunction
)array_tolist
, METH_NOARGS
,
1444 {"tostring", (PyCFunction
)array_tostring
, METH_NOARGS
,
1446 #ifdef Py_USING_UNICODE
1447 {"tounicode", (PyCFunction
)array_tounicode
, METH_NOARGS
,
1450 {"write", (PyCFunction
)array_tofile
, METH_O
,
1452 {NULL
, NULL
} /* sentinel */
1456 array_repr(arrayobject
*a
)
1458 char buf
[256], typecode
;
1459 PyObject
*s
, *t
, *v
= NULL
;
1463 typecode
= a
->ob_descr
->typecode
;
1465 PyOS_snprintf(buf
, sizeof(buf
), "array('%c')", typecode
);
1466 return PyString_FromString(buf
);
1469 if (typecode
== 'c')
1470 v
= array_tostring(a
, NULL
);
1471 #ifdef Py_USING_UNICODE
1472 else if (typecode
== 'u')
1473 v
= array_tounicode(a
, NULL
);
1476 v
= array_tolist(a
, NULL
);
1477 t
= PyObject_Repr(v
);
1480 PyOS_snprintf(buf
, sizeof(buf
), "array('%c', ", typecode
);
1481 s
= PyString_FromString(buf
);
1482 PyString_ConcatAndDel(&s
, t
);
1483 PyString_ConcatAndDel(&s
, PyString_FromString(")"));
1488 array_subscr(arrayobject
* self
, PyObject
* item
)
1490 if (PyInt_Check(item
)) {
1491 long i
= PyInt_AS_LONG(item
);
1494 return array_item(self
, i
);
1496 else if (PyLong_Check(item
)) {
1497 long i
= PyLong_AsLong(item
);
1498 if (i
== -1 && PyErr_Occurred())
1502 return array_item(self
, i
);
1504 else if (PySlice_Check(item
)) {
1505 int start
, stop
, step
, slicelength
, cur
, i
;
1508 int itemsize
= self
->ob_descr
->itemsize
;
1510 if (PySlice_GetIndicesEx((PySliceObject
*)item
, self
->ob_size
,
1511 &start
, &stop
, &step
, &slicelength
) < 0) {
1515 if (slicelength
<= 0) {
1516 return newarrayobject(&Arraytype
, 0, self
->ob_descr
);
1519 result
= newarrayobject(&Arraytype
, slicelength
, self
->ob_descr
);
1520 if (!result
) return NULL
;
1522 ar
= (arrayobject
*)result
;
1524 for (cur
= start
, i
= 0; i
< slicelength
;
1526 memcpy(ar
->ob_item
+ i
*itemsize
,
1527 self
->ob_item
+ cur
*itemsize
,
1535 PyErr_SetString(PyExc_TypeError
,
1536 "list indices must be integers");
1542 array_ass_subscr(arrayobject
* self
, PyObject
* item
, PyObject
* value
)
1544 if (PyInt_Check(item
)) {
1545 long i
= PyInt_AS_LONG(item
);
1548 return array_ass_item(self
, i
, value
);
1550 else if (PyLong_Check(item
)) {
1551 long i
= PyLong_AsLong(item
);
1552 if (i
== -1 && PyErr_Occurred())
1556 return array_ass_item(self
, i
, value
);
1558 else if (PySlice_Check(item
)) {
1559 int start
, stop
, step
, slicelength
;
1560 int itemsize
= self
->ob_descr
->itemsize
;
1562 if (PySlice_GetIndicesEx((PySliceObject
*)item
, self
->ob_size
,
1563 &start
, &stop
, &step
, &slicelength
) < 0) {
1567 /* treat A[slice(a,b)] = v _exactly_ like A[a:b] = v */
1568 if (step
== 1 && ((PySliceObject
*)item
)->step
== Py_None
)
1569 return array_ass_slice(self
, start
, stop
, value
);
1571 if (value
== NULL
) {
1575 if (slicelength
<= 0)
1580 start
= stop
+ step
*(slicelength
- 1) - 1;
1584 for (cur
= start
, i
= 0; i
< slicelength
- 1;
1586 memmove(self
->ob_item
+ (cur
- i
)*itemsize
,
1587 self
->ob_item
+ (cur
+ 1)*itemsize
,
1588 (step
- 1) * itemsize
);
1590 extra
= self
->ob_size
- (cur
+ 1);
1592 memmove(self
->ob_item
+ (cur
- i
)*itemsize
,
1593 self
->ob_item
+ (cur
+ 1)*itemsize
,
1597 self
->ob_size
-= slicelength
;
1598 self
->ob_item
= PyMem_REALLOC(self
->ob_item
, itemsize
*self
->ob_size
);
1608 if (!array_Check(value
)) {
1609 PyErr_Format(PyExc_TypeError
,
1610 "must assign array (not \"%.200s\") to slice",
1611 value
->ob_type
->tp_name
);
1615 av
= (arrayobject
*)value
;
1617 if (av
->ob_size
!= slicelength
) {
1618 PyErr_Format(PyExc_ValueError
,
1619 "attempt to assign array of size %d to extended slice of size %d",
1620 av
->ob_size
, slicelength
);
1627 /* protect against a[::-1] = a */
1629 value
= array_slice(av
, 0, av
->ob_size
);
1630 av
= (arrayobject
*)value
;
1636 for (cur
= start
, i
= 0; i
< slicelength
;
1638 memcpy(self
->ob_item
+ cur
*itemsize
,
1639 av
->ob_item
+ i
*itemsize
,
1649 PyErr_SetString(PyExc_TypeError
,
1650 "list indices must be integers");
1655 static PyMappingMethods array_as_mapping
= {
1656 (inquiry
)array_length
,
1657 (binaryfunc
)array_subscr
,
1658 (objobjargproc
)array_ass_subscr
1662 array_buffer_getreadbuf(arrayobject
*self
, int index
, const void **ptr
)
1665 PyErr_SetString(PyExc_SystemError
,
1666 "Accessing non-existent array segment");
1669 *ptr
= (void *)self
->ob_item
;
1670 return self
->ob_size
*self
->ob_descr
->itemsize
;
1674 array_buffer_getwritebuf(arrayobject
*self
, int index
, const void **ptr
)
1677 PyErr_SetString(PyExc_SystemError
,
1678 "Accessing non-existent array segment");
1681 *ptr
= (void *)self
->ob_item
;
1682 return self
->ob_size
*self
->ob_descr
->itemsize
;
1686 array_buffer_getsegcount(arrayobject
*self
, int *lenp
)
1689 *lenp
= self
->ob_size
*self
->ob_descr
->itemsize
;
1693 static PySequenceMethods array_as_sequence
= {
1694 (inquiry
)array_length
, /*sq_length*/
1695 (binaryfunc
)array_concat
, /*sq_concat*/
1696 (intargfunc
)array_repeat
, /*sq_repeat*/
1697 (intargfunc
)array_item
, /*sq_item*/
1698 (intintargfunc
)array_slice
, /*sq_slice*/
1699 (intobjargproc
)array_ass_item
, /*sq_ass_item*/
1700 (intintobjargproc
)array_ass_slice
, /*sq_ass_slice*/
1701 (objobjproc
)array_contains
, /*sq_contains*/
1702 (binaryfunc
)array_inplace_concat
, /*sq_inplace_concat*/
1703 (intargfunc
)array_inplace_repeat
/*sq_inplace_repeat*/
1706 static PyBufferProcs array_as_buffer
= {
1707 (getreadbufferproc
)array_buffer_getreadbuf
,
1708 (getwritebufferproc
)array_buffer_getwritebuf
,
1709 (getsegcountproc
)array_buffer_getsegcount
,
1713 array_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1716 PyObject
*initial
= NULL
;
1717 struct arraydescr
*descr
;
1720 int i
= PyObject_Length(kwds
);
1724 PyErr_SetString(PyExc_TypeError
,
1725 "array.array constructor takes "
1726 "no keyword arguments");
1731 if (!PyArg_ParseTuple(args
, "c|O:array", &c
, &initial
))
1734 if (!(initial
== NULL
|| PyList_Check(initial
)
1735 || PyString_Check(initial
) || PyTuple_Check(initial
)
1736 || (c
== 'u' && PyUnicode_Check(initial
)))) {
1737 PyErr_SetString(PyExc_TypeError
,
1738 "array initializer must be list or string");
1741 for (descr
= descriptors
; descr
->typecode
!= '\0'; descr
++) {
1742 if (descr
->typecode
== c
) {
1746 if (initial
== NULL
|| !(PyList_Check(initial
)
1747 || PyTuple_Check(initial
)))
1750 len
= PySequence_Size(initial
);
1752 a
= newarrayobject(type
, len
, descr
);
1758 for (i
= 0; i
< len
; i
++) {
1760 PySequence_GetItem(initial
, i
);
1761 if (setarrayitem(a
, i
, v
) != 0) {
1767 if (initial
!= NULL
&& PyString_Check(initial
)) {
1768 PyObject
*t_initial
= Py_BuildValue("(O)",
1771 array_fromstring((arrayobject
*)a
,
1773 Py_DECREF(t_initial
);
1779 #ifdef Py_USING_UNICODE
1780 } else if (initial
!= NULL
&& PyUnicode_Check(initial
)) {
1781 int n
= PyUnicode_GET_DATA_SIZE(initial
);
1783 arrayobject
*self
= (arrayobject
*)a
;
1784 char *item
= self
->ob_item
;
1785 item
= PyMem_Realloc(item
, n
);
1791 self
->ob_item
= item
;
1792 self
->ob_size
= n
/ sizeof(Py_UNICODE
);
1793 memcpy(item
, PyUnicode_AS_DATA(initial
), n
);
1800 PyErr_SetString(PyExc_ValueError
,
1801 "bad typecode (must be c, b, B, u, h, H, i, I, l, L, f or d)");
1806 PyDoc_STRVAR(module_doc
,
1807 "This module defines an object type which can efficiently represent\n\
1808 an array of basic values: characters, integers, floating point\n\
1809 numbers. Arrays are sequence types and behave very much like lists,\n\
1810 except that the type of objects stored in them is constrained. The\n\
1811 type is specified at object creation time by using a type code, which\n\
1812 is a single character. The following type codes are defined:\n\
1814 Type code C Type Minimum size in bytes \n\
1816 'b' signed integer 1 \n\
1817 'B' unsigned integer 1 \n\
1818 'u' Unicode character 2 \n\
1819 'h' signed integer 2 \n\
1820 'H' unsigned integer 2 \n\
1821 'i' signed integer 2 \n\
1822 'I' unsigned integer 2 \n\
1823 'l' signed integer 4 \n\
1824 'L' unsigned integer 4 \n\
1825 'f' floating point 4 \n\
1826 'd' floating point 8 \n\
1828 The constructor is:\n\
1830 array(typecode [, initializer]) -- create a new array\n\
1833 PyDoc_STRVAR(arraytype_doc
,
1834 "array(typecode [, initializer]) -> array\n\
1836 Return a new array whose items are restricted by typecode, and\n\
1837 initialized from the optional initializer value, which must be a list\n\
1840 Arrays represent basic values and behave very much like lists, except\n\
1841 the type of objects stored in them is constrained.\n\
1845 append() -- append a new item to the end of the array\n\
1846 buffer_info() -- return information giving the current memory info\n\
1847 byteswap() -- byteswap all the items of the array\n\
1848 count() -- return number of occurences of an object\n\
1849 extend() -- extend array by appending array elements\n\
1850 fromfile() -- read items from a file object\n\
1851 fromlist() -- append items from the list\n\
1852 fromstring() -- append items from the string\n\
1853 index() -- return index of first occurence of an object\n\
1854 insert() -- insert a new item into the array at a provided position\n\
1855 pop() -- remove and return item (default last)\n\
1856 read() -- DEPRECATED, use fromfile()\n\
1857 remove() -- remove first occurence of an object\n\
1858 reverse() -- reverse the order of the items in the array\n\
1859 tofile() -- write all items to a file object\n\
1860 tolist() -- return the array converted to an ordinary list\n\
1861 tostring() -- return the array converted to a string\n\
1862 write() -- DEPRECATED, use tofile()\n\
1866 typecode -- the typecode character used to create the array\n\
1867 itemsize -- the length in bytes of one array item\n\
1870 static PyObject
*array_iter(arrayobject
*ao
);
1872 static PyTypeObject Arraytype
= {
1873 PyObject_HEAD_INIT(NULL
)
1876 sizeof(arrayobject
),
1878 (destructor
)array_dealloc
, /* tp_dealloc */
1883 (reprfunc
)array_repr
, /* tp_repr */
1884 0, /* tp_as _number*/
1885 &array_as_sequence
, /* tp_as _sequence*/
1886 &array_as_mapping
, /* tp_as _mapping*/
1890 PyObject_GenericGetAttr
, /* tp_getattro */
1891 0, /* tp_setattro */
1892 &array_as_buffer
, /* tp_as_buffer*/
1893 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
1894 arraytype_doc
, /* tp_doc */
1895 0, /* tp_traverse */
1897 array_richcompare
, /* tp_richcompare */
1898 0, /* tp_weaklistoffset */
1899 (getiterfunc
)array_iter
, /* tp_iter */
1900 0, /* tp_iternext */
1901 array_methods
, /* tp_methods */
1903 array_getsets
, /* tp_getset */
1906 0, /* tp_descr_get */
1907 0, /* tp_descr_set */
1908 0, /* tp_dictoffset */
1910 PyType_GenericAlloc
, /* tp_alloc */
1911 array_new
, /* tp_new */
1912 PyObject_Del
, /* tp_free */
1916 /*********************** Array Iterator **************************/
1922 PyObject
* (*getitem
)(struct arrayobject
*, int);
1925 static PyTypeObject PyArrayIter_Type
;
1927 #define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
1930 array_iter(arrayobject
*ao
)
1932 arrayiterobject
*it
;
1934 if (!array_Check(ao
)) {
1935 PyErr_BadInternalCall();
1939 it
= PyObject_GC_New(arrayiterobject
, &PyArrayIter_Type
);
1946 it
->getitem
= ao
->ob_descr
->getitem
;
1947 PyObject_GC_Track(it
);
1948 return (PyObject
*)it
;
1952 arrayiter_next(arrayiterobject
*it
)
1954 assert(PyArrayIter_Check(it
));
1955 if (it
->index
< it
->ao
->ob_size
)
1956 return (*it
->getitem
)(it
->ao
, it
->index
++);
1961 arrayiter_dealloc(arrayiterobject
*it
)
1963 PyObject_GC_UnTrack(it
);
1965 PyObject_GC_Del(it
);
1969 arrayiter_traverse(arrayiterobject
*it
, visitproc visit
, void *arg
)
1972 return visit((PyObject
*)(it
->ao
), arg
);
1976 static PyTypeObject PyArrayIter_Type
= {
1977 PyObject_HEAD_INIT(NULL
)
1979 "arrayiterator", /* tp_name */
1980 sizeof(arrayiterobject
), /* tp_basicsize */
1981 0, /* tp_itemsize */
1983 (destructor
)arrayiter_dealloc
, /* tp_dealloc */
1989 0, /* tp_as_number */
1990 0, /* tp_as_sequence */
1991 0, /* tp_as_mapping */
1995 PyObject_GenericGetAttr
, /* tp_getattro */
1996 0, /* tp_setattro */
1997 0, /* tp_as_buffer */
1998 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
,/* tp_flags */
2000 (traverseproc
)arrayiter_traverse
, /* tp_traverse */
2002 0, /* tp_richcompare */
2003 0, /* tp_weaklistoffset */
2004 PyObject_SelfIter
, /* tp_iter */
2005 (iternextfunc
)arrayiter_next
, /* tp_iternext */
2010 /*********************** Install Module **************************/
2012 /* No functions in array module. */
2013 static PyMethodDef a_methods
[] = {
2014 {NULL
, NULL
, 0, NULL
} /* Sentinel */
2023 Arraytype
.ob_type
= &PyType_Type
;
2024 PyArrayIter_Type
.ob_type
= &PyType_Type
;
2025 m
= Py_InitModule3("array", a_methods
, module_doc
);
2027 Py_INCREF((PyObject
*)&Arraytype
);
2028 PyModule_AddObject(m
, "ArrayType", (PyObject
*)&Arraytype
);
2029 Py_INCREF((PyObject
*)&Arraytype
);
2030 PyModule_AddObject(m
, "array", (PyObject
*)&Arraytype
);
2031 /* No need to check the error here, the caller will do that */