1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
11 /* Array object implementation */
13 /* An array is a uniform list -- all items have the same type.
14 The item type is restricted to simple C types like int or float */
20 #else /* !STDC_HEADERS */
21 #ifndef DONT_HAVE_SYS_TYPES_H
22 #include <sys/types.h> /* For size_t */
23 #endif /* DONT_HAVE_SYS_TYPES_H */
24 #endif /* !STDC_HEADERS */
27 #endif /* HAVE_LIMITS_H */
29 struct arrayobject
; /* Forward */
34 PyObject
* (*getitem
)(struct arrayobject
*, int);
35 int (*setitem
)(struct arrayobject
*, int, PyObject
*);
38 typedef struct arrayobject
{
41 struct arraydescr
*ob_descr
;
44 staticforward PyTypeObject Arraytype
;
46 #define is_arrayobject(op) ((op)->ob_type == &Arraytype)
49 static PyObject
*newarrayobject(int, struct arraydescr
*);
51 static int getarraysize(PyObject
*);
53 static PyObject
*getarrayitem(PyObject
*, int);
54 static int setarrayitem(PyObject
*, int, PyObject
*);
56 static int insarrayitem(PyObject
*, int, PyObject
*);
57 static int addarrayitem(PyObject
*, PyObject
*);
61 c_getitem(arrayobject
*ap
, int i
)
63 return PyString_FromStringAndSize(&((char *)ap
->ob_item
)[i
], 1);
67 c_setitem(arrayobject
*ap
, int i
, PyObject
*v
)
70 if (!PyArg_Parse(v
, "c;array item must be char", &x
))
73 ((char *)ap
->ob_item
)[i
] = x
;
78 b_getitem(arrayobject
*ap
, int i
)
80 long x
= ((char *)ap
->ob_item
)[i
];
83 return PyInt_FromLong(x
);
87 b_setitem(arrayobject
*ap
, int i
, PyObject
*v
)
90 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
91 must use the next size up that is signed ('h') and manually do
92 the overflow checking */
93 if (!PyArg_Parse(v
, "h;array item must be integer", &x
))
96 PyErr_SetString(PyExc_OverflowError
,
97 "signed char is less than minimum");
101 PyErr_SetString(PyExc_OverflowError
,
102 "signed char is greater than maximum");
106 ((char *)ap
->ob_item
)[i
] = (char)x
;
111 BB_getitem(arrayobject
*ap
, int i
)
113 long x
= ((unsigned char *)ap
->ob_item
)[i
];
114 return PyInt_FromLong(x
);
118 BB_setitem(arrayobject
*ap
, int i
, PyObject
*v
)
121 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
122 if (!PyArg_Parse(v
, "b;array item must be integer", &x
))
125 ((char *)ap
->ob_item
)[i
] = x
;
130 h_getitem(arrayobject
*ap
, int i
)
132 return PyInt_FromLong((long) ((short *)ap
->ob_item
)[i
]);
136 h_setitem(arrayobject
*ap
, int i
, PyObject
*v
)
139 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
140 if (!PyArg_Parse(v
, "h;array item must be integer", &x
))
143 ((short *)ap
->ob_item
)[i
] = x
;
148 HH_getitem(arrayobject
*ap
, int i
)
150 return PyInt_FromLong((long) ((unsigned short *)ap
->ob_item
)[i
]);
154 HH_setitem(arrayobject
*ap
, int i
, PyObject
*v
)
157 /* PyArg_Parse's 'h' formatter is for a signed short, therefore
158 must use the next size up and manually do the overflow checking */
159 if (!PyArg_Parse(v
, "i;array item must be integer", &x
))
162 PyErr_SetString(PyExc_OverflowError
,
163 "unsigned short is less than minimum");
166 else if (x
> USHRT_MAX
) {
167 PyErr_SetString(PyExc_OverflowError
,
168 "unsigned short is greater than maximum");
172 ((short *)ap
->ob_item
)[i
] = (short)x
;
177 i_getitem(arrayobject
*ap
, int i
)
179 return PyInt_FromLong((long) ((int *)ap
->ob_item
)[i
]);
183 i_setitem(arrayobject
*ap
, int i
, PyObject
*v
)
186 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
187 if (!PyArg_Parse(v
, "i;array item must be integer", &x
))
190 ((int *)ap
->ob_item
)[i
] = x
;
195 II_getitem(arrayobject
*ap
, int i
)
197 return PyLong_FromUnsignedLong(
198 (unsigned long) ((unsigned int *)ap
->ob_item
)[i
]);
202 II_setitem(arrayobject
*ap
, int i
, PyObject
*v
)
205 if (PyLong_Check(v
)) {
206 x
= PyLong_AsUnsignedLong(v
);
207 if (x
== (unsigned long) -1 && PyErr_Occurred())
212 if (!PyArg_Parse(v
, "l;array item must be integer", &y
))
215 PyErr_SetString(PyExc_OverflowError
,
216 "unsigned int is less than minimum");
219 x
= (unsigned long)y
;
223 PyErr_SetString(PyExc_OverflowError
,
224 "unsigned int is greater than maximum");
229 ((unsigned int *)ap
->ob_item
)[i
] = (unsigned int)x
;
234 l_getitem(arrayobject
*ap
, int i
)
236 return PyInt_FromLong(((long *)ap
->ob_item
)[i
]);
240 l_setitem(arrayobject
*ap
, int i
, PyObject
*v
)
243 if (!PyArg_Parse(v
, "l;array item must be integer", &x
))
246 ((long *)ap
->ob_item
)[i
] = x
;
251 LL_getitem(arrayobject
*ap
, int i
)
253 return PyLong_FromUnsignedLong(((unsigned long *)ap
->ob_item
)[i
]);
257 LL_setitem(arrayobject
*ap
, int i
, PyObject
*v
)
260 if (PyLong_Check(v
)) {
261 x
= PyLong_AsUnsignedLong(v
);
262 if (x
== (unsigned long) -1 && PyErr_Occurred())
267 if (!PyArg_Parse(v
, "l;array item must be integer", &y
))
270 PyErr_SetString(PyExc_OverflowError
,
271 "unsigned long is less than minimum");
274 x
= (unsigned long)y
;
278 PyErr_SetString(PyExc_OverflowError
,
279 "unsigned long is greater than maximum");
284 ((unsigned long *)ap
->ob_item
)[i
] = x
;
289 f_getitem(arrayobject
*ap
, int i
)
291 return PyFloat_FromDouble((double) ((float *)ap
->ob_item
)[i
]);
295 f_setitem(arrayobject
*ap
, int i
, PyObject
*v
)
298 if (!PyArg_Parse(v
, "f;array item must be float", &x
))
301 ((float *)ap
->ob_item
)[i
] = x
;
306 d_getitem(arrayobject
*ap
, int i
)
308 return PyFloat_FromDouble(((double *)ap
->ob_item
)[i
]);
312 d_setitem(arrayobject
*ap
, int i
, PyObject
*v
)
315 if (!PyArg_Parse(v
, "d;array item must be float", &x
))
318 ((double *)ap
->ob_item
)[i
] = x
;
322 /* Description of types */
323 static struct arraydescr descriptors
[] = {
324 {'c', sizeof(char), c_getitem
, c_setitem
},
325 {'b', sizeof(char), b_getitem
, b_setitem
},
326 {'B', sizeof(char), BB_getitem
, BB_setitem
},
327 {'h', sizeof(short), h_getitem
, h_setitem
},
328 {'H', sizeof(short), HH_getitem
, HH_setitem
},
329 {'i', sizeof(int), i_getitem
, i_setitem
},
330 {'I', sizeof(int), II_getitem
, II_setitem
},
331 {'l', sizeof(long), l_getitem
, l_setitem
},
332 {'L', sizeof(long), LL_getitem
, LL_setitem
},
333 {'f', sizeof(float), f_getitem
, f_setitem
},
334 {'d', sizeof(double), d_getitem
, d_setitem
},
335 {'\0', 0, 0, 0} /* Sentinel */
337 /* If we ever allow items larger than double, we must change reverse()! */
341 newarrayobject(int size
, struct arraydescr
*descr
)
346 PyErr_BadInternalCall();
349 nbytes
= size
* descr
->itemsize
;
350 /* Check for overflow */
351 if (nbytes
/ descr
->itemsize
!= (size_t)size
) {
352 return PyErr_NoMemory();
354 op
= PyObject_NewVar(arrayobject
, &Arraytype
, size
);
356 return PyErr_NoMemory();
362 op
->ob_item
= PyMem_NEW(char, nbytes
);
363 if (op
->ob_item
== NULL
) {
365 return PyErr_NoMemory();
368 op
->ob_descr
= descr
;
369 return (PyObject
*) op
;
374 getarraysize(PyObject
*op
)
376 if (!is_arrayobject(op
)) {
377 PyErr_BadInternalCall();
380 return ((arrayobject
*)op
) -> ob_size
;
385 getarrayitem(PyObject
*op
, int i
)
387 register arrayobject
*ap
;
388 if (!is_arrayobject(op
)) {
389 PyErr_BadInternalCall();
392 ap
= (arrayobject
*)op
;
393 if (i
< 0 || i
>= ap
->ob_size
) {
394 PyErr_SetString(PyExc_IndexError
, "array index out of range");
397 return (*ap
->ob_descr
->getitem
)(ap
, i
);
401 ins1(arrayobject
*self
, int where
, PyObject
*v
)
405 PyErr_BadInternalCall();
408 if ((*self
->ob_descr
->setitem
)(self
, -1, v
) < 0)
410 items
= self
->ob_item
;
411 PyMem_RESIZE(items
, char,
412 (self
->ob_size
+1) * self
->ob_descr
->itemsize
);
419 if (where
> self
->ob_size
)
420 where
= self
->ob_size
;
421 memmove(items
+ (where
+1)*self
->ob_descr
->itemsize
,
422 items
+ where
*self
->ob_descr
->itemsize
,
423 (self
->ob_size
-where
)*self
->ob_descr
->itemsize
);
424 self
->ob_item
= items
;
426 return (*self
->ob_descr
->setitem
)(self
, where
, v
);
431 insarrayitem(PyObject
*op
, int where
, PyObject
*newitem
)
433 if (!is_arrayobject(op
)) {
434 PyErr_BadInternalCall();
437 return ins1((arrayobject
*)op
, where
, newitem
);
441 addarrayitem(PyObject
*op
, PyObject
*newitem
)
443 if (!is_arrayobject(op
)) {
444 PyErr_BadInternalCall();
447 return ins1((arrayobject
*)op
,
448 (int) ((arrayobject
*)op
)->ob_size
, newitem
);
455 array_dealloc(arrayobject
*op
)
457 if (op
->ob_item
!= NULL
)
458 PyMem_DEL(op
->ob_item
);
463 array_compare(arrayobject
*v
, arrayobject
*w
)
465 int len
= (v
->ob_size
< w
->ob_size
) ? v
->ob_size
: w
->ob_size
;
467 for (i
= 0; i
< len
; i
++) {
470 ai
= getarrayitem((PyObject
*)v
, i
);
471 bi
= getarrayitem((PyObject
*)w
, i
);
473 cmp
= PyObject_Compare(ai
, bi
);
481 return v
->ob_size
- w
->ob_size
;
485 array_length(arrayobject
*a
)
491 array_item(arrayobject
*a
, int i
)
493 if (i
< 0 || i
>= a
->ob_size
) {
494 PyErr_SetString(PyExc_IndexError
, "array index out of range");
497 return getarrayitem((PyObject
*)a
, i
);
501 array_slice(arrayobject
*a
, int ilow
, int ihigh
)
506 else if (ilow
> a
->ob_size
)
512 else if (ihigh
> a
->ob_size
)
514 np
= (arrayobject
*) newarrayobject(ihigh
- ilow
, a
->ob_descr
);
517 memcpy(np
->ob_item
, a
->ob_item
+ ilow
* a
->ob_descr
->itemsize
,
518 (ihigh
-ilow
) * a
->ob_descr
->itemsize
);
519 return (PyObject
*)np
;
523 array_concat(arrayobject
*a
, PyObject
*bb
)
527 if (!is_arrayobject(bb
)) {
528 PyErr_Format(PyExc_TypeError
,
529 "can only append array (not \"%.200s\") to array",
530 bb
->ob_type
->tp_name
);
533 #define b ((arrayobject *)bb)
534 if (a
->ob_descr
!= b
->ob_descr
) {
538 size
= a
->ob_size
+ b
->ob_size
;
539 np
= (arrayobject
*) newarrayobject(size
, a
->ob_descr
);
543 memcpy(np
->ob_item
, a
->ob_item
, a
->ob_size
*a
->ob_descr
->itemsize
);
544 memcpy(np
->ob_item
+ a
->ob_size
*a
->ob_descr
->itemsize
,
545 b
->ob_item
, b
->ob_size
*b
->ob_descr
->itemsize
);
546 return (PyObject
*)np
;
551 array_repeat(arrayobject
*a
, int n
)
560 size
= a
->ob_size
* n
;
561 np
= (arrayobject
*) newarrayobject(size
, a
->ob_descr
);
565 nbytes
= a
->ob_size
* a
->ob_descr
->itemsize
;
566 for (i
= 0; i
< n
; i
++) {
567 memcpy(p
, a
->ob_item
, nbytes
);
570 return (PyObject
*) np
;
574 array_ass_slice(arrayobject
*a
, int ilow
, int ihigh
, PyObject
*v
)
577 int n
; /* Size of replacement array */
578 int d
; /* Change in size */
579 #define b ((arrayobject *)v)
582 else if (is_arrayobject(v
)) {
585 /* Special case "a[i:j] = a" -- copy b first */
587 v
= array_slice(b
, 0, n
);
588 ret
= array_ass_slice(a
, ilow
, ihigh
, v
);
592 if (b
->ob_descr
!= a
->ob_descr
) {
598 PyErr_Format(PyExc_TypeError
,
599 "can only assign array (not \"%.200s\") to array slice",
600 v
->ob_type
->tp_name
);
605 else if (ilow
> a
->ob_size
)
611 else if (ihigh
> a
->ob_size
)
614 d
= n
- (ihigh
-ilow
);
615 if (d
< 0) { /* Delete -d items */
616 memmove(item
+ (ihigh
+d
)*a
->ob_descr
->itemsize
,
617 item
+ ihigh
*a
->ob_descr
->itemsize
,
618 (a
->ob_size
-ihigh
)*a
->ob_descr
->itemsize
);
620 PyMem_RESIZE(item
, char, a
->ob_size
*a
->ob_descr
->itemsize
);
624 else if (d
> 0) { /* Insert d items */
625 PyMem_RESIZE(item
, char,
626 (a
->ob_size
+ d
)*a
->ob_descr
->itemsize
);
631 memmove(item
+ (ihigh
+d
)*a
->ob_descr
->itemsize
,
632 item
+ ihigh
*a
->ob_descr
->itemsize
,
633 (a
->ob_size
-ihigh
)*a
->ob_descr
->itemsize
);
638 memcpy(item
+ ilow
*a
->ob_descr
->itemsize
, b
->ob_item
,
639 n
*b
->ob_descr
->itemsize
);
645 array_ass_item(arrayobject
*a
, int i
, PyObject
*v
)
647 if (i
< 0 || i
>= a
->ob_size
) {
648 PyErr_SetString(PyExc_IndexError
,
649 "array assignment index out of range");
653 return array_ass_slice(a
, i
, i
+1, v
);
654 return (*a
->ob_descr
->setitem
)(a
, i
, v
);
658 setarrayitem(PyObject
*a
, int i
, PyObject
*v
)
660 if (!is_arrayobject(a
)) {
661 PyErr_BadInternalCall();
664 return array_ass_item((arrayobject
*)a
, i
, v
);
668 ins(arrayobject
*self
, int where
, PyObject
*v
)
670 if (ins1(self
, where
, v
) != 0)
677 array_count(arrayobject
*self
, PyObject
*args
)
683 if (!PyArg_ParseTuple(args
, "O:count", &v
))
685 for (i
= 0; i
< self
->ob_size
; i
++) {
686 PyObject
*selfi
= getarrayitem((PyObject
*)self
, i
);
687 if (PyObject_Compare(selfi
, v
) == 0)
690 if (PyErr_Occurred())
693 return PyInt_FromLong((long)count
);
696 static char count_doc
[] =
699 Return number of occurences of x in the array.";
702 array_index(arrayobject
*self
, PyObject
*args
)
707 if (!PyArg_ParseTuple(args
, "O:index", &v
))
709 for (i
= 0; i
< self
->ob_size
; i
++) {
710 PyObject
*selfi
= getarrayitem((PyObject
*)self
, i
);
711 if (PyObject_Compare(selfi
, v
) == 0) {
713 return PyInt_FromLong((long)i
);
716 if (PyErr_Occurred())
719 PyErr_SetString(PyExc_ValueError
, "array.index(x): x not in list");
723 static char index_doc
[] =
726 Return index of first occurence of x in the array.";
729 array_remove(arrayobject
*self
, PyObject
*args
)
734 if (!PyArg_ParseTuple(args
, "O:remove", &v
))
736 for (i
= 0; i
< self
->ob_size
; i
++) {
737 PyObject
*selfi
= getarrayitem((PyObject
*)self
,i
);
738 if (PyObject_Compare(selfi
, v
) == 0) {
740 if (array_ass_slice(self
, i
, i
+1,
741 (PyObject
*)NULL
) != 0)
747 if (PyErr_Occurred())
750 PyErr_SetString(PyExc_ValueError
, "array.remove(x): x not in list");
754 static char remove_doc
[] =
757 Remove the first occurence of x in the array.";
760 array_pop(arrayobject
*self
, PyObject
*args
)
764 if (!PyArg_ParseTuple(args
, "|i:pop", &i
))
766 if (self
->ob_size
== 0) {
767 /* Special-case most common failure cause */
768 PyErr_SetString(PyExc_IndexError
, "pop from empty array");
773 if (i
< 0 || i
>= self
->ob_size
) {
774 PyErr_SetString(PyExc_IndexError
, "pop index out of range");
777 v
= getarrayitem((PyObject
*)self
,i
);
778 if (array_ass_slice(self
, i
, i
+1, (PyObject
*)NULL
) != 0) {
785 static char pop_doc
[] =
788 Return the i-th element and delete it from the array. i defaults to -1.";
791 array_extend(self
, args
)
798 if (!PyArg_ParseTuple(args
, "O:extend", &bb
))
801 if (!is_arrayobject(bb
)) {
802 PyErr_Format(PyExc_TypeError
,
803 "can only extend array with array (not \"%.200s\")",
804 bb
->ob_type
->tp_name
);
807 #define b ((arrayobject *)bb)
808 if (self
->ob_descr
!= b
->ob_descr
) {
809 PyErr_SetString(PyExc_TypeError
,
810 "can only extend with array of same kind");
813 size
= self
->ob_size
+ b
->ob_size
;
814 PyMem_RESIZE(self
->ob_item
, char, size
*self
->ob_descr
->itemsize
);
815 if (self
->ob_item
== NULL
) {
817 return PyErr_NoMemory();
819 memcpy(self
->ob_item
+ self
->ob_size
*self
->ob_descr
->itemsize
,
820 b
->ob_item
, b
->ob_size
*b
->ob_descr
->itemsize
);
821 self
->ob_size
= size
;
827 static char extend_doc
[] =
830 Append array items to the end of the array.";
833 array_insert(arrayobject
*self
, PyObject
*args
)
837 if (!PyArg_ParseTuple(args
, "iO:insert", &i
, &v
))
839 return ins(self
, i
, v
);
842 static char insert_doc
[] =
845 Insert a new item x into the array before position i.";
849 array_buffer_info(arrayobject
*self
, PyObject
*args
)
851 PyObject
* retval
= PyTuple_New(2);
852 if (!retval
) return NULL
;
854 PyTuple_SET_ITEM(retval
, 0, PyLong_FromVoidPtr(self
->ob_item
));
855 PyTuple_SET_ITEM(retval
, 1, PyInt_FromLong((long)(self
->ob_size
)));
860 static char buffer_info_doc
[] =
861 "buffer_info -> (address, length)\n\
863 Return a tuple (address, length) giving the current memory address and\n\
864 the length in bytes of the buffer used to hold array's contents.";
868 array_append(arrayobject
*self
, PyObject
*args
)
871 if (!PyArg_ParseTuple(args
, "O:append", &v
))
873 return ins(self
, (int) self
->ob_size
, v
);
876 static char append_doc
[] =
879 Append new value x to the end of the array.";
883 array_byteswap(arrayobject
*self
, PyObject
*args
)
888 if (!PyArg_ParseTuple(args
, ":byteswap"))
891 switch (self
->ob_descr
->itemsize
) {
895 for (p
= self
->ob_item
, i
= self
->ob_size
; --i
>= 0; p
+= 2) {
902 for (p
= self
->ob_item
, i
= self
->ob_size
; --i
>= 0; p
+= 4) {
912 for (p
= self
->ob_item
, i
= self
->ob_size
; --i
>= 0; p
+= 8) {
928 PyErr_SetString(PyExc_RuntimeError
,
929 "don't know how to byteswap this array type");
936 static char byteswap_doc
[] =
939 Byteswap all items of the array. If the items in the array are not 1, 2,\n\
940 4, or 8 bytes in size, RuntimeError is raised.";
943 array_reverse(arrayobject
*self
, PyObject
*args
)
945 register int itemsize
= self
->ob_descr
->itemsize
;
946 register char *p
, *q
;
947 char tmp
[sizeof(double)]; /* Assume that's the max item size */
950 PyErr_SetString(PyExc_TypeError
,
951 "<array>.reverse requires exactly 0 arguments");
955 if (self
->ob_size
> 1) {
956 for (p
= self
->ob_item
,
957 q
= self
->ob_item
+ (self
->ob_size
- 1)*itemsize
;
959 p
+= itemsize
, q
-= itemsize
) {
960 memmove(tmp
, p
, itemsize
);
961 memmove(p
, q
, itemsize
);
962 memmove(q
, tmp
, itemsize
);
970 static char reverse_doc
[] =
973 Reverse the order of the items in the array.";
976 array_fromfile(arrayobject
*self
, PyObject
*args
)
981 if (!PyArg_ParseTuple(args
, "Oi:fromfile", &f
, &n
))
983 fp
= PyFile_AsFile(f
);
985 PyErr_SetString(PyExc_TypeError
, "arg1 must be open file");
989 char *item
= self
->ob_item
;
990 int itemsize
= self
->ob_descr
->itemsize
;
994 /* Be careful here about overflow */
995 if ((newlength
= self
->ob_size
+ n
) <= 0 ||
996 (newbytes
= newlength
* itemsize
) / itemsize
!=
999 PyMem_RESIZE(item
, char, newbytes
);
1005 self
->ob_item
= item
;
1007 nread
= fread(item
+ (self
->ob_size
- n
) * itemsize
,
1009 if (nread
< (size_t)n
) {
1010 self
->ob_size
-= (n
- nread
);
1011 PyMem_RESIZE(item
, char, self
->ob_size
*itemsize
);
1012 self
->ob_item
= item
;
1013 PyErr_SetString(PyExc_EOFError
,
1014 "not enough items in file");
1022 static char fromfile_doc
[] =
1025 Read n objects from the file object f and append them to the end of the\n\
1026 array. Also called as read.";
1030 array_tofile(arrayobject
*self
, PyObject
*args
)
1034 if (!PyArg_ParseTuple(args
, "O:tofile", &f
))
1036 fp
= PyFile_AsFile(f
);
1038 PyErr_SetString(PyExc_TypeError
, "arg must be open file");
1041 if (self
->ob_size
> 0) {
1042 if (fwrite(self
->ob_item
, self
->ob_descr
->itemsize
,
1043 self
->ob_size
, fp
) != (size_t)self
->ob_size
) {
1044 PyErr_SetFromErrno(PyExc_IOError
);
1053 static char tofile_doc
[] =
1056 Write all items (as machine values) to the file object f. Also called as\n\
1061 array_fromlist(arrayobject
*self
, PyObject
*args
)
1065 int itemsize
= self
->ob_descr
->itemsize
;
1066 if (!PyArg_ParseTuple(args
, "O:fromlist", &list
))
1068 if (!PyList_Check(list
)) {
1069 PyErr_SetString(PyExc_TypeError
, "arg must be list");
1072 n
= PyList_Size(list
);
1074 char *item
= self
->ob_item
;
1076 PyMem_RESIZE(item
, char, (self
->ob_size
+ n
) * itemsize
);
1081 self
->ob_item
= item
;
1083 for (i
= 0; i
< n
; i
++) {
1084 PyObject
*v
= PyList_GetItem(list
, i
);
1085 if ((*self
->ob_descr
->setitem
)(self
,
1086 self
->ob_size
- n
+ i
, v
) != 0) {
1088 PyMem_RESIZE(item
, char,
1089 self
->ob_size
* itemsize
);
1090 self
->ob_item
= item
;
1099 static char fromlist_doc
[] =
1102 Append items to array from list.";
1106 array_tolist(arrayobject
*self
, PyObject
*args
)
1108 PyObject
*list
= PyList_New(self
->ob_size
);
1110 if (!PyArg_ParseTuple(args
, ":tolist"))
1114 for (i
= 0; i
< self
->ob_size
; i
++) {
1115 PyObject
*v
= getarrayitem((PyObject
*)self
, i
);
1120 PyList_SetItem(list
, i
, v
);
1125 static char tolist_doc
[] =
1126 "tolist() -> list\n\
1128 Convert array to an ordinary list with the same items.";
1132 array_fromstring(arrayobject
*self
, PyObject
*args
)
1136 int itemsize
= self
->ob_descr
->itemsize
;
1137 if (!PyArg_ParseTuple(args
, "s#:fromstring", &str
, &n
))
1139 if (n
% itemsize
!= 0) {
1140 PyErr_SetString(PyExc_ValueError
,
1141 "string length not a multiple of item size");
1146 char *item
= self
->ob_item
;
1147 PyMem_RESIZE(item
, char, (self
->ob_size
+ n
) * itemsize
);
1152 self
->ob_item
= item
;
1154 memcpy(item
+ (self
->ob_size
- n
) * itemsize
,
1161 static char fromstring_doc
[] =
1162 "fromstring(string)\n\
1164 Appends items from the string, interpreting it as an array of machine\n\
1165 values,as if it had been read from a file using the fromfile() method).";
1169 array_tostring(arrayobject
*self
, PyObject
*args
)
1171 if (!PyArg_ParseTuple(args
, ":tostring"))
1173 return PyString_FromStringAndSize(self
->ob_item
,
1174 self
->ob_size
* self
->ob_descr
->itemsize
);
1177 static char tostring_doc
[] =
1178 "tostring() -> string\n\
1180 Convert the array to an array of machine values and return the string\n\
1183 PyMethodDef array_methods
[] = {
1184 {"append", (PyCFunction
)array_append
, METH_VARARGS
, append_doc
},
1185 {"buffer_info", (PyCFunction
)array_buffer_info
, METH_VARARGS
, buffer_info_doc
},
1186 {"byteswap", (PyCFunction
)array_byteswap
, METH_VARARGS
, byteswap_doc
},
1187 {"count", (PyCFunction
)array_count
, METH_VARARGS
, count_doc
},
1188 {"extend", (PyCFunction
)array_extend
, METH_VARARGS
, extend_doc
},
1189 {"fromfile", (PyCFunction
)array_fromfile
, METH_VARARGS
, fromfile_doc
},
1190 {"fromlist", (PyCFunction
)array_fromlist
, METH_VARARGS
, fromlist_doc
},
1191 {"fromstring", (PyCFunction
)array_fromstring
, METH_VARARGS
, fromstring_doc
},
1192 {"index", (PyCFunction
)array_index
, METH_VARARGS
, index_doc
},
1193 {"insert", (PyCFunction
)array_insert
, METH_VARARGS
, insert_doc
},
1194 {"pop", (PyCFunction
)array_pop
, METH_VARARGS
, pop_doc
},
1195 {"read", (PyCFunction
)array_fromfile
, METH_VARARGS
, fromfile_doc
},
1196 {"remove", (PyCFunction
)array_remove
, METH_VARARGS
, remove_doc
},
1197 {"reverse", (PyCFunction
)array_reverse
, METH_VARARGS
, reverse_doc
},
1198 /* {"sort", (PyCFunction)array_sort, METH_VARARGS, sort_doc},*/
1199 {"tofile", (PyCFunction
)array_tofile
, METH_VARARGS
, tofile_doc
},
1200 {"tolist", (PyCFunction
)array_tolist
, METH_VARARGS
, tolist_doc
},
1201 {"tostring", (PyCFunction
)array_tostring
, METH_VARARGS
, tostring_doc
},
1202 {"write", (PyCFunction
)array_tofile
, METH_VARARGS
, tofile_doc
},
1203 {NULL
, NULL
} /* sentinel */
1207 array_getattr(arrayobject
*a
, char *name
)
1209 if (strcmp(name
, "typecode") == 0) {
1210 char tc
= a
->ob_descr
->typecode
;
1211 return PyString_FromStringAndSize(&tc
, 1);
1213 if (strcmp(name
, "itemsize") == 0) {
1214 return PyInt_FromLong((long)a
->ob_descr
->itemsize
);
1216 if (strcmp(name
, "__members__") == 0) {
1217 PyObject
*list
= PyList_New(2);
1219 PyList_SetItem(list
, 0,
1220 PyString_FromString("typecode"));
1221 PyList_SetItem(list
, 1,
1222 PyString_FromString("itemsize"));
1223 if (PyErr_Occurred()) {
1230 return Py_FindMethod(array_methods
, (PyObject
*)a
, name
);
1234 array_print(arrayobject
*a
, FILE *fp
, int flags
)
1238 PyObject
*t_empty
= PyTuple_New(0);
1242 fprintf(fp
, "array('%c')", a
->ob_descr
->typecode
);
1245 if (a
->ob_descr
->typecode
== 'c') {
1246 fprintf(fp
, "array('c', ");
1247 v
= array_tostring(a
, t_empty
);
1248 Py_DECREF(t_empty
);;
1249 ok
= PyObject_Print(v
, fp
, 0);
1254 fprintf(fp
, "array('%c', [", a
->ob_descr
->typecode
);
1255 for (i
= 0; i
< len
&& ok
== 0; i
++) {
1258 v
= (a
->ob_descr
->getitem
)(a
, i
);
1259 ok
= PyObject_Print(v
, fp
, 0);
1267 array_repr(arrayobject
*a
)
1270 PyObject
*s
, *t
, *comma
, *v
;
1274 sprintf(buf
, "array('%c')", a
->ob_descr
->typecode
);
1275 return PyString_FromString(buf
);
1277 if (a
->ob_descr
->typecode
== 'c') {
1278 sprintf(buf
, "array('c', ");
1279 s
= PyString_FromString(buf
);
1280 v
= array_tostring(a
, (PyObject
*)NULL
);
1281 t
= PyObject_Repr(v
);
1283 PyString_ConcatAndDel(&s
, t
);
1284 PyString_ConcatAndDel(&s
, PyString_FromString(")"));
1287 sprintf(buf
, "array('%c', [", a
->ob_descr
->typecode
);
1288 s
= PyString_FromString(buf
);
1289 comma
= PyString_FromString(", ");
1290 for (i
= 0; i
< len
&& !PyErr_Occurred(); i
++) {
1292 PyString_Concat(&s
, comma
);
1293 v
= (a
->ob_descr
->getitem
)(a
, i
);
1294 t
= PyObject_Repr(v
);
1296 PyString_ConcatAndDel(&s
, t
);
1299 PyString_ConcatAndDel(&s
, PyString_FromString("])"));
1304 array_buffer_getreadbuf(arrayobject
*self
, int index
, const void **ptr
)
1307 PyErr_SetString(PyExc_SystemError
,
1308 "Accessing non-existent array segment");
1311 *ptr
= (void *)self
->ob_item
;
1312 return self
->ob_size
*self
->ob_descr
->itemsize
;
1316 array_buffer_getwritebuf(arrayobject
*self
, int index
, const void **ptr
)
1319 PyErr_SetString(PyExc_SystemError
,
1320 "Accessing non-existent array segment");
1323 *ptr
= (void *)self
->ob_item
;
1324 return self
->ob_size
*self
->ob_descr
->itemsize
;
1328 array_buffer_getsegcount(arrayobject
*self
, int *lenp
)
1331 *lenp
= self
->ob_size
*self
->ob_descr
->itemsize
;
1335 static PySequenceMethods array_as_sequence
= {
1336 (inquiry
)array_length
, /*sq_length*/
1337 (binaryfunc
)array_concat
, /*sq_concat*/
1338 (intargfunc
)array_repeat
, /*sq_repeat*/
1339 (intargfunc
)array_item
, /*sq_item*/
1340 (intintargfunc
)array_slice
, /*sq_slice*/
1341 (intobjargproc
)array_ass_item
, /*sq_ass_item*/
1342 (intintobjargproc
)array_ass_slice
, /*sq_ass_slice*/
1345 static PyBufferProcs array_as_buffer
= {
1346 (getreadbufferproc
)array_buffer_getreadbuf
,
1347 (getwritebufferproc
)array_buffer_getwritebuf
,
1348 (getsegcountproc
)array_buffer_getsegcount
,
1355 a_array(PyObject
*self
, PyObject
*args
)
1358 PyObject
*initial
= NULL
;
1359 struct arraydescr
*descr
;
1360 if (!PyArg_ParseTuple(args
, "c:array", &c
)) {
1362 if (!PyArg_ParseTuple(args
, "cO:array", &c
, &initial
))
1364 if (!PyList_Check(initial
) && !PyString_Check(initial
)) {
1365 PyErr_SetString(PyExc_TypeError
,
1366 "array initializer must be list or string");
1370 for (descr
= descriptors
; descr
->typecode
!= '\0'; descr
++) {
1371 if (descr
->typecode
== c
) {
1374 if (initial
== NULL
|| !PyList_Check(initial
))
1377 len
= PyList_Size(initial
);
1378 a
= newarrayobject(len
, descr
);
1383 for (i
= 0; i
< len
; i
++) {
1385 PyList_GetItem(initial
, i
);
1386 if (setarrayitem(a
, i
, v
) != 0) {
1392 if (initial
!= NULL
&& PyString_Check(initial
)) {
1393 PyObject
*t_initial
= Py_BuildValue("(O)", initial
);
1395 array_fromstring((arrayobject
*)a
, t_initial
);
1396 Py_DECREF(t_initial
);
1406 PyErr_SetString(PyExc_ValueError
,
1407 "bad typecode (must be c, b, B, h, H, i, I, l, L, f or d)");
1411 static char a_array_doc
[] =
1412 "array(typecode [, initializer]) -> array\n\
1414 Return a new array whose items are restricted by typecode, and\n\
1415 initialized from the optional initializer value, which must be a list\n\
1418 static PyMethodDef a_methods
[] = {
1419 {"array", a_array
, METH_VARARGS
, a_array_doc
},
1420 {NULL
, NULL
} /* sentinel */
1423 static char module_doc
[] =
1424 "This module defines a new object type which can efficiently represent\n\
1425 an array of basic values: characters, integers, floating point\n\
1426 numbers. Arrays are sequence types and behave very much like lists,\n\
1427 except that the type of objects stored in them is constrained. The\n\
1428 type is specified at object creation time by using a type code, which\n\
1429 is a single character. The following type codes are defined:\n\
1431 Type code C Type Minimum size in bytes \n\
1433 'b' signed integer 1 \n\
1434 'B' unsigned integer 1 \n\
1435 'h' signed integer 2 \n\
1436 'H' unsigned integer 2 \n\
1437 'i' signed integer 2 \n\
1438 'I' unsigned integer 2 \n\
1439 'l' signed integer 4 \n\
1440 'L' unsigned integer 4 \n\
1441 'f' floating point 4 \n\
1442 'd' floating point 8 \n\
1446 array(typecode [, initializer]) -- create a new array\n\
1450 ArrayType -- type object for array objects\n\
1453 static char arraytype_doc
[] =
1454 "An array represents basic values and behave very much like lists, except\n\
1455 the type of objects stored in them is constrained.\n\
1459 append() -- append a new item to the end of the array\n\
1460 buffer_info() -- return information giving the current memory info\n\
1461 byteswap() -- byteswap all the items of the array\n\
1462 count() -- return number of occurences of an object\n\
1463 extend() -- extend array by appending array elements\n\
1464 fromfile() -- read items from a file object\n\
1465 fromlist() -- append items from the list\n\
1466 fromstring() -- append items from the string\n\
1467 index() -- return index of first occurence of an object\n\
1468 insert() -- insert a new item into the array at a provided position\n\
1469 pop() -- remove and return item (default last)\n\
1470 read() -- DEPRECATED, use fromfile()\n\
1471 remove() -- remove first occurence of an object\n\
1472 reverse() -- reverse the order of the items in the array\n\
1473 tofile() -- write all items to a file object\n\
1474 tolist() -- return the array converted to an ordinary list\n\
1475 tostring() -- return the array converted to a string\n\
1476 write() -- DEPRECATED, use tofile()\n\
1480 typecode -- the typecode character used to create the array\n\
1481 itemsize -- the length in bytes of one array item\n\
1484 statichere PyTypeObject Arraytype
= {
1485 PyObject_HEAD_INIT(NULL
)
1488 sizeof(arrayobject
),
1490 (destructor
)array_dealloc
, /*tp_dealloc*/
1491 (printfunc
)array_print
, /*tp_print*/
1492 (getattrfunc
)array_getattr
, /*tp_getattr*/
1494 (cmpfunc
)array_compare
, /*tp_compare*/
1495 (reprfunc
)array_repr
, /*tp_repr*/
1497 &array_as_sequence
, /*tp_as_sequence*/
1498 0, /*tp_as_mapping*/
1504 &array_as_buffer
, /*tp_as_buffer*/
1506 arraytype_doc
, /*tp_doc*/
1514 Arraytype
.ob_type
= &PyType_Type
;
1515 m
= Py_InitModule3("array", a_methods
, module_doc
);
1516 d
= PyModule_GetDict(m
);
1517 PyDict_SetItemString(d
, "ArrayType", (PyObject
*)&Arraytype
);
1518 /* No need to check the error here, the caller will do that */