1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Array object implementation */
34 /* An array is a uniform list -- all items have the same type.
35 The item type is restricted to simple C types like int or float */
41 #else /* !STDC_HEADERS */
42 #ifndef DONT_HAVE_SYS_TYPES_H
43 #include <sys/types.h> /* For size_t */
44 #endif /* DONT_HAVE_SYS_TYPES_H */
45 #endif /* !STDC_HEADERS */
47 struct arrayobject
; /* Forward */
52 PyObject
* (*getitem
) Py_FPROTO((struct arrayobject
*, int));
53 int (*setitem
) Py_FPROTO((struct arrayobject
*, int, PyObject
*));
56 typedef struct arrayobject
{
59 struct arraydescr
*ob_descr
;
62 staticforward PyTypeObject Arraytype
;
64 #define is_arrayobject(op) ((op)->ob_type == &Arraytype)
67 static PyObject
*newarrayobject
Py_PROTO((int, struct arraydescr
*));
69 static int getarraysize
Py_PROTO((PyObject
*));
71 static PyObject
*getarrayitem
Py_PROTO((PyObject
*, int));
72 static int setarrayitem
Py_PROTO((PyObject
*, int, PyObject
*));
74 static int insarrayitem
Py_PROTO((PyObject
*, int, PyObject
*));
75 static int addarrayitem
Py_PROTO((PyObject
*, PyObject
*));
83 return PyString_FromStringAndSize(&((char *)ap
->ob_item
)[i
], 1);
93 if (!PyArg_Parse(v
, "c;array item must be char", &x
))
96 ((char *)ap
->ob_item
)[i
] = x
;
105 long x
= ((char *)ap
->ob_item
)[i
];
108 return PyInt_FromLong(x
);
118 if (!PyArg_Parse(v
, "b;array item must be integer", &x
))
121 ((char *)ap
->ob_item
)[i
] = x
;
130 long x
= ((unsigned char *)ap
->ob_item
)[i
];
131 return PyInt_FromLong(x
);
134 #define BB_setitem b_setitem
141 return PyInt_FromLong((long) ((short *)ap
->ob_item
)[i
]);
151 if (!PyArg_Parse(v
, "h;array item must be integer", &x
))
154 ((short *)ap
->ob_item
)[i
] = x
;
163 return PyInt_FromLong((long) ((unsigned short *)ap
->ob_item
)[i
]);
166 #define HH_setitem h_setitem
173 return PyInt_FromLong((long) ((int *)ap
->ob_item
)[i
]);
183 if (!PyArg_Parse(v
, "i;array item must be integer", &x
))
186 ((int *)ap
->ob_item
)[i
] = x
;
195 return PyLong_FromUnsignedLong(
196 (unsigned long) ((unsigned int *)ap
->ob_item
)[i
]);
206 if (PyLong_Check(v
)) {
207 x
= PyLong_AsUnsignedLong(v
);
208 if (x
== (unsigned long) -1 && PyErr_Occurred())
212 if (!PyArg_Parse(v
, "l;array item must be integer", &x
))
216 ((unsigned int *)ap
->ob_item
)[i
] = x
;
225 return PyInt_FromLong(((long *)ap
->ob_item
)[i
]);
235 if (!PyArg_Parse(v
, "l;array item must be integer", &x
))
238 ((long *)ap
->ob_item
)[i
] = x
;
247 return PyLong_FromUnsignedLong(((unsigned long *)ap
->ob_item
)[i
]);
257 if (PyLong_Check(v
)) {
258 x
= PyLong_AsUnsignedLong(v
);
259 if (x
== (unsigned long) -1 && PyErr_Occurred())
263 if (!PyArg_Parse(v
, "l;array item must be integer", &x
))
267 ((unsigned long *)ap
->ob_item
)[i
] = x
;
276 return PyFloat_FromDouble((double) ((float *)ap
->ob_item
)[i
]);
286 if (!PyArg_Parse(v
, "f;array item must be float", &x
))
289 ((float *)ap
->ob_item
)[i
] = x
;
298 return PyFloat_FromDouble(((double *)ap
->ob_item
)[i
]);
308 if (!PyArg_Parse(v
, "d;array item must be float", &x
))
311 ((double *)ap
->ob_item
)[i
] = x
;
315 /* Description of types */
316 static struct arraydescr descriptors
[] = {
317 {'c', sizeof(char), c_getitem
, c_setitem
},
318 {'b', sizeof(char), b_getitem
, b_setitem
},
319 {'B', sizeof(char), BB_getitem
, BB_setitem
},
320 {'h', sizeof(short), h_getitem
, h_setitem
},
321 {'H', sizeof(short), HH_getitem
, HH_setitem
},
322 {'i', sizeof(int), i_getitem
, i_setitem
},
323 {'I', sizeof(int), II_getitem
, II_setitem
},
324 {'l', sizeof(long), l_getitem
, l_setitem
},
325 {'L', sizeof(long), LL_getitem
, LL_setitem
},
326 {'f', sizeof(float), f_getitem
, f_setitem
},
327 {'d', sizeof(double), d_getitem
, d_setitem
},
328 {'\0', 0, 0, 0} /* Sentinel */
330 /* If we ever allow items larger than double, we must change reverse()! */
334 newarrayobject(size
, descr
)
336 struct arraydescr
*descr
;
341 PyErr_BadInternalCall();
344 nbytes
= size
* descr
->itemsize
;
345 /* Check for overflow */
346 if (nbytes
/ descr
->itemsize
!= (size_t)size
) {
347 return PyErr_NoMemory();
349 op
= PyMem_NEW(arrayobject
, 1);
351 return PyErr_NoMemory();
357 op
->ob_item
= PyMem_NEW(char, nbytes
);
358 if (op
->ob_item
== NULL
) {
360 return PyErr_NoMemory();
363 op
->ob_type
= &Arraytype
;
365 op
->ob_descr
= descr
;
366 _Py_NewReference((PyObject
*)op
);
367 return (PyObject
*) op
;
375 if (!is_arrayobject(op
)) {
376 PyErr_BadInternalCall();
379 return ((arrayobject
*)op
) -> ob_size
;
388 register arrayobject
*ap
;
389 if (!is_arrayobject(op
)) {
390 PyErr_BadInternalCall();
393 ap
= (arrayobject
*)op
;
394 if (i
< 0 || i
>= ap
->ob_size
) {
395 PyErr_SetString(PyExc_IndexError
, "array index out of range");
398 return (*ap
->ob_descr
->getitem
)(ap
, i
);
409 PyErr_BadInternalCall();
412 if ((*self
->ob_descr
->setitem
)(self
, -1, v
) < 0)
414 items
= self
->ob_item
;
415 PyMem_RESIZE(items
, char,
416 (self
->ob_size
+1) * self
->ob_descr
->itemsize
);
423 if (where
> self
->ob_size
)
424 where
= self
->ob_size
;
425 memmove(items
+ (where
+1)*self
->ob_descr
->itemsize
,
426 items
+ where
*self
->ob_descr
->itemsize
,
427 (self
->ob_size
-where
)*self
->ob_descr
->itemsize
);
428 self
->ob_item
= items
;
430 return (*self
->ob_descr
->setitem
)(self
, where
, v
);
435 insarrayitem(op
, where
, newitem
)
440 if (!is_arrayobject(op
)) {
441 PyErr_BadInternalCall();
444 return ins1((arrayobject
*)op
, where
, newitem
);
448 addarrayitem(op
, newitem
)
452 if (!is_arrayobject(op
)) {
453 PyErr_BadInternalCall();
456 return ins1((arrayobject
*)op
,
457 (int) ((arrayobject
*)op
)->ob_size
, newitem
);
467 if (op
->ob_item
!= NULL
)
468 PyMem_DEL(op
->ob_item
);
476 int len
= (v
->ob_size
< w
->ob_size
) ? v
->ob_size
: w
->ob_size
;
478 for (i
= 0; i
< len
; i
++) {
481 ai
= getarrayitem((PyObject
*)v
, i
);
482 bi
= getarrayitem((PyObject
*)w
, i
);
484 cmp
= PyObject_Compare(ai
, bi
);
492 return v
->ob_size
- w
->ob_size
;
507 if (i
< 0 || i
>= a
->ob_size
) {
508 PyErr_SetString(PyExc_IndexError
, "array index out of range");
511 return getarrayitem((PyObject
*)a
, i
);
515 array_slice(a
, ilow
, ihigh
)
522 else if (ilow
> a
->ob_size
)
528 else if (ihigh
> a
->ob_size
)
530 np
= (arrayobject
*) newarrayobject(ihigh
- ilow
, a
->ob_descr
);
533 memcpy(np
->ob_item
, a
->ob_item
+ ilow
* a
->ob_descr
->itemsize
,
534 (ihigh
-ilow
) * a
->ob_descr
->itemsize
);
535 return (PyObject
*)np
;
545 if (!is_arrayobject(bb
)) {
549 #define b ((arrayobject *)bb)
550 if (a
->ob_descr
!= b
->ob_descr
) {
554 size
= a
->ob_size
+ b
->ob_size
;
555 np
= (arrayobject
*) newarrayobject(size
, a
->ob_descr
);
559 memcpy(np
->ob_item
, a
->ob_item
, a
->ob_size
*a
->ob_descr
->itemsize
);
560 memcpy(np
->ob_item
+ a
->ob_size
*a
->ob_descr
->itemsize
,
561 b
->ob_item
, b
->ob_size
*b
->ob_descr
->itemsize
);
562 return (PyObject
*)np
;
578 size
= a
->ob_size
* n
;
579 np
= (arrayobject
*) newarrayobject(size
, a
->ob_descr
);
583 nbytes
= a
->ob_size
* a
->ob_descr
->itemsize
;
584 for (i
= 0; i
< n
; i
++) {
585 memcpy(p
, a
->ob_item
, nbytes
);
588 return (PyObject
*) np
;
592 array_ass_slice(a
, ilow
, ihigh
, v
)
598 int n
; /* Size of replacement array */
599 int d
; /* Change in size */
600 #define b ((arrayobject *)v)
603 else if (is_arrayobject(v
)) {
606 /* Special case "a[i:j] = a" -- copy b first */
608 v
= array_slice(b
, 0, n
);
609 ret
= array_ass_slice(a
, ilow
, ihigh
, v
);
613 if (b
->ob_descr
!= a
->ob_descr
) {
624 else if (ilow
> a
->ob_size
)
630 else if (ihigh
> a
->ob_size
)
633 d
= n
- (ihigh
-ilow
);
634 if (d
< 0) { /* Delete -d items */
635 memmove(item
+ (ihigh
+d
)*a
->ob_descr
->itemsize
,
636 item
+ ihigh
*a
->ob_descr
->itemsize
,
637 (a
->ob_size
-ihigh
)*a
->ob_descr
->itemsize
);
639 PyMem_RESIZE(item
, char, a
->ob_size
*a
->ob_descr
->itemsize
);
643 else if (d
> 0) { /* Insert d items */
644 PyMem_RESIZE(item
, char,
645 (a
->ob_size
+ d
)*a
->ob_descr
->itemsize
);
650 memmove(item
+ (ihigh
+d
)*a
->ob_descr
->itemsize
,
651 item
+ ihigh
*a
->ob_descr
->itemsize
,
652 (a
->ob_size
-ihigh
)*a
->ob_descr
->itemsize
);
657 memcpy(item
+ ilow
*a
->ob_descr
->itemsize
, b
->ob_item
,
658 n
*b
->ob_descr
->itemsize
);
664 array_ass_item(a
, i
, v
)
669 if (i
< 0 || i
>= a
->ob_size
) {
670 PyErr_SetString(PyExc_IndexError
,
671 "array assignment index out of range");
675 return array_ass_slice(a
, i
, i
+1, v
);
676 return (*a
->ob_descr
->setitem
)(a
, i
, v
);
680 setarrayitem(a
, i
, v
)
685 if (!is_arrayobject(a
)) {
686 PyErr_BadInternalCall();
689 return array_ass_item((arrayobject
*)a
, i
, v
);
698 if (ins1(self
, where
, v
) != 0)
705 array_insert(self
, args
)
711 if (!PyArg_Parse(args
, "(iO)", &i
, &v
))
713 return ins(self
, i
, v
);
716 static char insert_doc
[] =
719 Insert a new item x into the array before position i.";
723 array_buffer_info(self
, args
)
727 return Py_BuildValue("ll",
728 (long)(self
->ob_item
), (long)(self
->ob_size
));
731 static char buffer_info_doc
[] =
732 "buffer_info -> (address, length)\n\
734 Return a tuple (address, length) giving the current memory address and\n\
735 the length in bytes of the buffer used to hold array's contents.";
739 array_append(self
, args
)
744 if (!PyArg_Parse(args
, "O", &v
))
746 return ins(self
, (int) self
->ob_size
, v
);
749 static char append_doc
[] =
752 Append new value x to the end of the array.";
756 array_byteswap(self
, args
)
763 if (!PyArg_ParseTuple(args
, ":byteswap"))
766 switch (self
->ob_descr
->itemsize
) {
770 for (p
= self
->ob_item
, i
= self
->ob_size
; --i
>= 0; p
+= 2) {
777 for (p
= self
->ob_item
, i
= self
->ob_size
; --i
>= 0; p
+= 4) {
787 for (p
= self
->ob_item
, i
= self
->ob_size
; --i
>= 0; p
+= 8) {
803 PyErr_SetString(PyExc_RuntimeError
,
804 "don't know how to byteswap this array type");
811 static char byteswap_doc
[] =
814 Byteswap all items of the array. If the items in the array are not 1, 2,\n\
815 4, or 8 bytes in size, RuntimeError is raised.";
818 array_reverse(self
, args
)
822 register int itemsize
= self
->ob_descr
->itemsize
;
823 register char *p
, *q
;
824 char tmp
[sizeof(double)]; /* Assume that's the max item size */
831 if (self
->ob_size
> 1) {
832 for (p
= self
->ob_item
,
833 q
= self
->ob_item
+ (self
->ob_size
- 1)*itemsize
;
835 p
+= itemsize
, q
-= itemsize
) {
836 memmove(tmp
, p
, itemsize
);
837 memmove(p
, q
, itemsize
);
838 memmove(q
, tmp
, itemsize
);
846 static char reverse_doc
[] =
849 Reverse the order of the items in the array.";
851 /* The following routines were adapted from listobject.c but not converted.
852 To make them work you will have to work! */
856 array_index(self
, args
)
866 for (i
= 0; i
< self
->ob_size
; i
++) {
867 if (PyObject_Compare(self
->ob_item
[i
], args
) == 0)
868 return PyInt_FromLong((long)i
);
869 /* XXX PyErr_Occurred */
871 PyErr_SetString(PyExc_ValueError
, "array.index(x): x not in array");
878 array_count(self
, args
)
889 for (i
= 0; i
< self
->ob_size
; i
++) {
890 if (PyObject_Compare(self
->ob_item
[i
], args
) == 0)
892 /* XXX PyErr_Occurred */
894 return PyInt_FromLong((long)count
);
900 array_remove(self
, args
)
910 for (i
= 0; i
< self
->ob_size
; i
++) {
911 if (PyObject_Compare(self
->ob_item
[i
], args
) == 0) {
912 if (array_ass_slice(self
, i
, i
+1,
913 (PyObject
*)NULL
) != 0)
918 /* XXX PyErr_Occurred */
920 PyErr_SetString(PyExc_ValueError
, "array.remove(x): x not in array");
926 array_fromfile(self
, args
)
933 if (!PyArg_Parse(args
, "(Oi)", &f
, &n
))
935 fp
= PyFile_AsFile(f
);
937 PyErr_SetString(PyExc_TypeError
, "arg1 must be open file");
941 char *item
= self
->ob_item
;
942 int itemsize
= self
->ob_descr
->itemsize
;
946 /* Be careful here about overflow */
947 if ((newlength
= self
->ob_size
+ n
) <= 0 ||
948 (newbytes
= newlength
* itemsize
) / itemsize
!=
951 PyMem_RESIZE(item
, char, newbytes
);
957 self
->ob_item
= item
;
959 nread
= fread(item
+ (self
->ob_size
- n
) * itemsize
,
962 self
->ob_size
-= (n
- nread
);
963 PyMem_RESIZE(item
, char, self
->ob_size
*itemsize
);
964 self
->ob_item
= item
;
965 PyErr_SetString(PyExc_EOFError
,
966 "not enough items in file");
974 static char fromfile_doc
[] =
977 Read n objects from the file object f and append them to the end of the\n\
978 array. Also called as read.";
982 array_tofile(self
, args
)
988 if (!PyArg_Parse(args
, "O", &f
))
990 fp
= PyFile_AsFile(f
);
992 PyErr_SetString(PyExc_TypeError
, "arg must be open file");
995 if (self
->ob_size
> 0) {
996 if ((int)fwrite(self
->ob_item
, self
->ob_descr
->itemsize
,
997 self
->ob_size
, fp
) != self
->ob_size
) {
998 PyErr_SetFromErrno(PyExc_IOError
);
1007 static char tofile_doc
[] =
1010 Write all items (as machine values) to the file object f. Also called as\n\
1015 array_fromlist(self
, args
)
1021 int itemsize
= self
->ob_descr
->itemsize
;
1022 if (!PyArg_Parse(args
, "O", &list
))
1024 if (!PyList_Check(list
)) {
1025 PyErr_SetString(PyExc_TypeError
, "arg must be list");
1028 n
= PyList_Size(list
);
1030 char *item
= self
->ob_item
;
1032 PyMem_RESIZE(item
, char, (self
->ob_size
+ n
) * itemsize
);
1037 self
->ob_item
= item
;
1039 for (i
= 0; i
< n
; i
++) {
1040 PyObject
*v
= PyList_GetItem(list
, i
);
1041 if ((*self
->ob_descr
->setitem
)(self
,
1042 self
->ob_size
- n
+ i
, v
) != 0) {
1044 PyMem_RESIZE(item
, char,
1045 self
->ob_size
* itemsize
);
1046 self
->ob_item
= item
;
1055 static char fromlist_doc
[] =
1058 Append items to array from list.";
1062 array_tolist(self
, args
)
1066 PyObject
*list
= PyList_New(self
->ob_size
);
1070 for (i
= 0; i
< self
->ob_size
; i
++) {
1071 PyObject
*v
= getarrayitem((PyObject
*)self
, i
);
1076 PyList_SetItem(list
, i
, v
);
1081 static char tolist_doc
[] =
1082 "tolist() -> list\n\
1084 Convert array to an ordinary list with the same items.";
1088 array_fromstring(self
, args
)
1094 int itemsize
= self
->ob_descr
->itemsize
;
1095 if (!PyArg_Parse(args
, "s#", &str
, &n
))
1097 if (n
% itemsize
!= 0) {
1098 PyErr_SetString(PyExc_ValueError
,
1099 "string length not a multiple of item size");
1104 char *item
= self
->ob_item
;
1105 PyMem_RESIZE(item
, char, (self
->ob_size
+ n
) * itemsize
);
1110 self
->ob_item
= item
;
1112 memcpy(item
+ (self
->ob_size
- n
) * itemsize
,
1119 static char fromstring_doc
[] =
1120 "fromstring(string)\n\
1122 Appends items from the string, interpreting it as an array of machine\n\
1123 values,as if it had been read from a file using the fromfile() method).";
1127 array_tostring(self
, args
)
1131 if (!PyArg_Parse(args
, ""))
1133 return PyString_FromStringAndSize(self
->ob_item
,
1134 self
->ob_size
* self
->ob_descr
->itemsize
);
1137 static char tostring_doc
[] =
1138 "tostring() -> string\n\
1140 Convert the array to an array of machine values and return the string\n\
1143 PyMethodDef array_methods
[] = {
1144 {"append", (PyCFunction
)array_append
, 0, append_doc
},
1145 {"buffer_info", (PyCFunction
)array_buffer_info
, 0, buffer_info_doc
},
1146 {"byteswap", (PyCFunction
)array_byteswap
, METH_VARARGS
,
1148 /* {"count", (method)array_count},*/
1149 {"fromfile", (PyCFunction
)array_fromfile
, 0, fromfile_doc
},
1150 {"fromlist", (PyCFunction
)array_fromlist
, 0, fromlist_doc
},
1151 {"fromstring", (PyCFunction
)array_fromstring
, 0, fromstring_doc
},
1152 /* {"index", (method)array_index},*/
1153 {"insert", (PyCFunction
)array_insert
, 0, insert_doc
},
1154 {"read", (PyCFunction
)array_fromfile
, 0, fromfile_doc
},
1155 /* {"remove", (method)array_remove},*/
1156 {"reverse", (PyCFunction
)array_reverse
, 0, reverse_doc
},
1157 /* {"sort", (method)array_sort},*/
1158 {"tofile", (PyCFunction
)array_tofile
, 0, tofile_doc
},
1159 {"tolist", (PyCFunction
)array_tolist
, 0, tolist_doc
},
1160 {"tostring", (PyCFunction
)array_tostring
, 0, tostring_doc
},
1161 {"write", (PyCFunction
)array_tofile
, 0, tofile_doc
},
1162 {NULL
, NULL
} /* sentinel */
1166 array_getattr(a
, name
)
1170 if (strcmp(name
, "typecode") == 0) {
1171 char tc
= a
->ob_descr
->typecode
;
1172 return PyString_FromStringAndSize(&tc
, 1);
1174 if (strcmp(name
, "itemsize") == 0) {
1175 return PyInt_FromLong((long)a
->ob_descr
->itemsize
);
1177 if (strcmp(name
, "__members__") == 0) {
1178 PyObject
*list
= PyList_New(2);
1180 PyList_SetItem(list
, 0,
1181 PyString_FromString("typecode"));
1182 PyList_SetItem(list
, 1,
1183 PyString_FromString("itemsize"));
1184 if (PyErr_Occurred()) {
1191 return Py_FindMethod(array_methods
, (PyObject
*)a
, name
);
1195 array_print(a
, fp
, flags
)
1205 fprintf(fp
, "array('%c')", a
->ob_descr
->typecode
);
1208 if (a
->ob_descr
->typecode
== 'c') {
1209 fprintf(fp
, "array('c', ");
1210 v
= array_tostring(a
, (PyObject
*)NULL
);
1211 ok
= PyObject_Print(v
, fp
, 0);
1216 fprintf(fp
, "array('%c', [", a
->ob_descr
->typecode
);
1217 for (i
= 0; i
< len
&& ok
== 0; i
++) {
1220 v
= (a
->ob_descr
->getitem
)(a
, i
);
1221 ok
= PyObject_Print(v
, fp
, 0);
1233 PyObject
*s
, *t
, *comma
, *v
;
1237 sprintf(buf
, "array('%c')", a
->ob_descr
->typecode
);
1238 return PyString_FromString(buf
);
1240 if (a
->ob_descr
->typecode
== 'c') {
1241 sprintf(buf
, "array('c', ");
1242 s
= PyString_FromString(buf
);
1243 v
= array_tostring(a
, (PyObject
*)NULL
);
1244 t
= PyObject_Repr(v
);
1246 PyString_ConcatAndDel(&s
, t
);
1247 PyString_ConcatAndDel(&s
, PyString_FromString(")"));
1250 sprintf(buf
, "array('%c', [", a
->ob_descr
->typecode
);
1251 s
= PyString_FromString(buf
);
1252 comma
= PyString_FromString(", ");
1253 for (i
= 0; i
< len
&& !PyErr_Occurred(); i
++) {
1255 PyString_Concat(&s
, comma
);
1256 v
= (a
->ob_descr
->getitem
)(a
, i
);
1257 t
= PyObject_Repr(v
);
1259 PyString_ConcatAndDel(&s
, t
);
1262 PyString_ConcatAndDel(&s
, PyString_FromString("])"));
1267 array_buffer_getreadbuf(self
, index
, ptr
)
1273 PyErr_SetString(PyExc_SystemError
,
1274 "Accessing non-existent array segment");
1277 *ptr
= (void *)self
->ob_item
;
1278 return self
->ob_size
*self
->ob_descr
->itemsize
;
1282 array_buffer_getwritebuf(self
, index
, ptr
)
1288 PyErr_SetString(PyExc_SystemError
,
1289 "Accessing non-existent array segment");
1292 *ptr
= (void *)self
->ob_item
;
1293 return self
->ob_size
*self
->ob_descr
->itemsize
;
1297 array_buffer_getsegcount(self
, lenp
)
1302 *lenp
= self
->ob_size
*self
->ob_descr
->itemsize
;
1306 static PySequenceMethods array_as_sequence
= {
1307 (inquiry
)array_length
, /*sq_length*/
1308 (binaryfunc
)array_concat
, /*sq_concat*/
1309 (intargfunc
)array_repeat
, /*sq_repeat*/
1310 (intargfunc
)array_item
, /*sq_item*/
1311 (intintargfunc
)array_slice
, /*sq_slice*/
1312 (intobjargproc
)array_ass_item
, /*sq_ass_item*/
1313 (intintobjargproc
)array_ass_slice
, /*sq_ass_slice*/
1316 static PyBufferProcs array_as_buffer
= {
1317 (getreadbufferproc
)array_buffer_getreadbuf
,
1318 (getwritebufferproc
)array_buffer_getwritebuf
,
1319 (getsegcountproc
)array_buffer_getsegcount
,
1331 PyObject
*initial
= NULL
;
1332 struct arraydescr
*descr
;
1333 if (!PyArg_Parse(args
, "c", &c
)) {
1335 if (!PyArg_Parse(args
, "(cO)", &c
, &initial
))
1337 if (!PyList_Check(initial
) && !PyString_Check(initial
)) {
1338 PyErr_SetString(PyExc_TypeError
,
1339 "array initializer must be list or string");
1343 for (descr
= descriptors
; descr
->typecode
!= '\0'; descr
++) {
1344 if (descr
->typecode
== c
) {
1347 if (initial
== NULL
|| !PyList_Check(initial
))
1350 len
= PyList_Size(initial
);
1351 a
= newarrayobject(len
, descr
);
1356 for (i
= 0; i
< len
; i
++) {
1358 PyList_GetItem(initial
, i
);
1359 if (setarrayitem(a
, i
, v
) != 0) {
1365 if (initial
!= NULL
&& PyString_Check(initial
)) {
1367 array_fromstring((arrayobject
*)a
, initial
);
1377 PyErr_SetString(PyExc_ValueError
,
1378 "bad typecode (must be c, b, B, h, H, i, I, l, L, f or d)");
1382 static char a_array_doc
[] =
1383 "array(typecode [, initializer]) -> array\n\
1385 Return a new array whose items are restricted by typecode, and\n\
1386 initialized from the optional initializer value, which must be a list\n\
1389 static PyMethodDef a_methods
[] = {
1390 {"array", a_array
, 0, a_array_doc
},
1391 {NULL
, NULL
} /* sentinel */
1394 static char module_doc
[] =
1395 "This module defines a new object type which can efficiently represent\n\
1396 an array of basic values: characters, integers, floating point\n\
1397 numbers. Arrays are sequence types and behave very much like lists,\n\
1398 except that the type of objects stored in them is constrained. The\n\
1399 type is specified at object creation time by using a type code, which\n\
1400 is a single character. The following type codes are defined:\n\
1402 Type code C Type Minimum size in bytes \n\
1404 'b' signed integer 1 \n\
1405 'B' unsigned integer 1 \n\
1406 'h' signed integer 2 \n\
1407 'H' unsigned integer 2 \n\
1408 'i' signed integer 2 \n\
1409 'I' unsigned integer 2 \n\
1410 'l' signed integer 4 \n\
1411 'L' unsigned integer 4 \n\
1412 'f' floating point 4 \n\
1413 'd' floating point 8 \n\
1417 array(typecode [, initializer]) -- create a new array\n\
1421 ArrayType -- type object for array objects\n\
1424 static char arraytype_doc
[] =
1425 "An array represents basic values and behave very much like lists, except\n\
1426 the type of objects stored in them is constrained.\n\
1430 append() -- append a new item to the end of the array\n\
1431 buffer_info() -- return information giving the current memory info\n\
1432 byteswap() -- byteswap all the items of the array\n\
1433 fromfile() -- read items from a file object\n\
1434 fromlist() -- append items from the list\n\
1435 fromstring() -- append items from the string\n\
1436 insert() -- insert a new item into the array at a provided position\n\
1437 read() -- DEPRECATED, use fromfile()\n\
1438 reverse() -- reverse the order of the items in the array\n\
1439 tofile() -- write all items to a file object\n\
1440 tolist() -- return the array converted to an ordinary list\n\
1441 tostring() -- return the array converted to a string\n\
1442 write() -- DEPRECATED, use tofile()\n\
1446 typecode -- the typecode character used to create the array\n\
1447 itemsize -- the length in bytes of one array item\n\
1450 statichere PyTypeObject Arraytype
= {
1451 PyObject_HEAD_INIT(NULL
)
1454 sizeof(arrayobject
),
1456 (destructor
)array_dealloc
, /*tp_dealloc*/
1457 (printfunc
)array_print
, /*tp_print*/
1458 (getattrfunc
)array_getattr
, /*tp_getattr*/
1460 (cmpfunc
)array_compare
, /*tp_compare*/
1461 (reprfunc
)array_repr
, /*tp_repr*/
1463 &array_as_sequence
, /*tp_as_sequence*/
1464 0, /*tp_as_mapping*/
1470 &array_as_buffer
, /*tp_as_buffer*/
1472 arraytype_doc
, /*tp_doc*/
1480 Arraytype
.ob_type
= &PyType_Type
;
1481 m
= Py_InitModule3("array", a_methods
, module_doc
);
1482 d
= PyModule_GetDict(m
);
1483 PyDict_SetItemString(d
, "ArrayType", (PyObject
*)&Arraytype
);
1484 /* No need to check the error here, the caller will do that */