{"__copy__", (PyCFunction)array_copy, METH_NOARGS, copy_doc}, {"count", (PyCFunction)array_count, METH_O, count_doc}, {"__deepcopy__",(PyCFunction)array_copy, METH_O, copy_doc}, {"extend", (PyCFunction)array_extend, METH_O, extend_doc}, {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS, fromfile_doc}, {"fromlist", (PyCFunction)array_fromlist, METH_O, fromlist_doc}, {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS, fromstring_doc},#ifdef Py_USING_UNICODE {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS, fromunicode_doc},#endif {"index", (PyCFunction)array_index, METH_O, index_doc}, {"insert", (PyCFunction)array_insert, METH_VARARGS, insert_doc}, {"pop", (PyCFunction)array_pop, METH_VARARGS, pop_doc}, {"read", (PyCFunction)array_fromfile, METH_VARARGS, fromfile_doc}, {"__reduce__", (PyCFunction)array_reduce, METH_NOARGS, array_doc}, {"remove", (PyCFunction)array_remove, METH_O, remove_doc}, {"reverse", (PyCFunction)array_reverse, METH_NOARGS, reverse_doc},/* {"sort", (PyCFunction)array_sort, METH_VARARGS, sort_doc},*/ {"tofile", (PyCFunction)array_tofile, METH_O, tofile_doc}, {"tolist", (PyCFunction)array_tolist, METH_NOARGS, tolist_doc}, {"tostring", (PyCFunction)array_tostring, METH_NOARGS, tostring_doc},#ifdef Py_USING_UNICODE {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS, tounicode_doc},#endif {"write", (PyCFunction)array_tofile, METH_O, tofile_doc}, {NULL, NULL} /* sentinel */}; static PyObject *array_repr(arrayobject *a){ char buf[256], typecode; PyObject *s, *t, *v = NULL; Py_ssize_t len; len = a->ob_size; typecode = a->ob_descr->typecode; if (len == 0) { PyOS_snprintf(buf, sizeof(buf), "array('%c')", typecode); return PyString_FromString(buf); } if (typecode == 'c') v = array_tostring(a, NULL);#ifdef Py_USING_UNICODE else if (typecode == 'u') v = array_tounicode(a, NULL);#endif else v = array_tolist(a, NULL); t = PyObject_Repr(v); Py_XDECREF(v); PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode); s = PyString_FromString(buf); PyString_ConcatAndDel(&s, t); PyString_ConcatAndDel(&s, PyString_FromString(")")); return s;} static PyObject*array_subscr(arrayobject* self, PyObject* item){ if (PyIndex_Check(item)) { Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); if (i==-1 && PyErr_Occurred()) { return NULL; } if (i < 0) i += self->ob_size; return array_item(self, i); } else if (PySlice_Check(item)) { Py_ssize_t start, stop, step, slicelength, cur, i; PyObject* result; arrayobject* ar; int itemsize = self->ob_descr->itemsize; if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size, &start, &stop, &step, &slicelength) < 0) { return NULL; } if (slicelength <= 0) { return newarrayobject(&Arraytype, 0, self->ob_descr); } else { result = newarrayobject(&Arraytype, slicelength, self->ob_descr); if (!result) return NULL; ar = (arrayobject*)result; for (cur = start, i = 0; i < slicelength; cur += step, i++) { memcpy(ar->ob_item + i*itemsize, self->ob_item + cur*itemsize, itemsize); } return result; } } else { PyErr_SetString(PyExc_TypeError, "list indices must be integers"); return NULL; }} static intarray_ass_subscr(arrayobject* self, PyObject* item, PyObject* value){ if (PyIndex_Check(item)) { Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); if (i==-1 && PyErr_Occurred()) return -1; if (i < 0) i += self->ob_size; return array_ass_item(self, i, value); } else if (PySlice_Check(item)) { Py_ssize_t start, stop, step, slicelength; int itemsize = self->ob_descr->itemsize; if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size, &start, &stop, &step, &slicelength) < 0) { return -1; } /* treat A[slice(a,b)] = v _exactly_ like A[a:b] = v */ if (step == 1 && ((PySliceObject*)item)->step == Py_None) return array_ass_slice(self, start, stop, value); if (value == NULL) { /* delete slice */ Py_ssize_t cur, i, extra; if (slicelength <= 0) return 0; if (step < 0) { stop = start + 1; start = stop + step*(slicelength - 1) - 1; step = -step; } for (cur = start, i = 0; i < slicelength - 1; cur += step, i++) { memmove(self->ob_item + (cur - i)*itemsize, self->ob_item + (cur + 1)*itemsize, (step - 1) * itemsize); } extra = self->ob_size - (cur + 1); if (extra > 0) { memmove(self->ob_item + (cur - i)*itemsize, self->ob_item + (cur + 1)*itemsize, extra*itemsize); } self->ob_size -= slicelength; self->ob_item = (char *)PyMem_REALLOC(self->ob_item, itemsize*self->ob_size); self->allocated = self->ob_size; return 0; } else { /* assign slice */ Py_ssize_t cur, i; arrayobject* av; if (!array_Check(value)) { PyErr_Format(PyExc_TypeError, "must assign array (not \"%.200s\") to slice", value->ob_type->tp_name); return -1; } av = (arrayobject*)value; if (av->ob_size != slicelength) { PyErr_Format(PyExc_ValueError, "attempt to assign array of size %ld to extended slice of size %ld", /*XXX*/(long)av->ob_size, /*XXX*/(long)slicelength); return -1; } if (!slicelength) return 0; /* protect against a[::-1] = a */ if (self == av) { value = array_slice(av, 0, av->ob_size); av = (arrayobject*)value; if (!av) return -1; } else { Py_INCREF(value); } for (cur = start, i = 0; i < slicelength; cur += step, i++) { memcpy(self->ob_item + cur*itemsize, av->ob_item + i*itemsize, itemsize); } Py_DECREF(value); return 0; } } else { PyErr_SetString(PyExc_TypeError, "list indices must be integers"); return -1; }} static PyMappingMethods array_as_mapping = { (lenfunc)array_length, (binaryfunc)array_subscr, (objobjargproc)array_ass_subscr}; static const void *emptybuf = ""; static Py_ssize_tarray_buffer_getreadbuf(arrayobject *self, Py_ssize_t index, const void **ptr){ if ( index != 0 ) { PyErr_SetString(PyExc_SystemError, "Accessing non-existent array segment"); return -1; } *ptr = (void *)self->ob_item; if (*ptr == NULL) *ptr = emptybuf; return self->ob_size*self->ob_descr->itemsize;} static Py_ssize_tarray_buffer_getwritebuf(arrayobject *self, Py_ssize_t index, const void **ptr){ if ( index != 0 ) { PyErr_SetString(PyExc_SystemError, "Accessing non-existent array segment"); return -1; } *ptr = (void *)self->ob_item; if (*ptr == NULL) *ptr = emptybuf; return self->ob_size*self->ob_descr->itemsize;} static Py_ssize_tarray_buffer_getsegcount(arrayobject *self, Py_ssize_t *lenp){ if ( lenp ) *lenp = self->ob_size*self->ob_descr->itemsize; return 1;} static PySequenceMethods array_as_sequence = { (lenfunc)array_length, /*sq_length*/ (binaryfunc)array_concat, /*sq_concat*/ (ssizeargfunc)array_repeat, /*sq_repeat*/ (ssizeargfunc)array_item, /*sq_item*/ (ssizessizeargfunc)array_slice, /*sq_slice*/ (ssizeobjargproc)array_ass_item, /*sq_ass_item*/ (ssizessizeobjargproc)array_ass_slice, /*sq_ass_slice*/ (objobjproc)array_contains, /*sq_contains*/ (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/ (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/}; static PyBufferProcs array_as_buffer = { (readbufferproc)array_buffer_getreadbuf, (writebufferproc)array_buffer_getwritebuf, (segcountproc)array_buffer_getsegcount, NULL,}; static PyObject *array_new(PyTypeObject *type, PyObject *args, PyObject *kwds){ char c; PyObject *initial = NULL, *it = NULL; struct arraydescr *descr; if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds)) return NULL; if (!PyArg_ParseTuple(args, "c|O:array", &c, &initial)) return NULL; if (!(initial == NULL || PyList_Check(initial) || PyString_Check(initial) || PyTuple_Check(initial) || (c == 'u' && PyUnicode_Check(initial)))) { it = PyObject_GetIter(initial); if (it == NULL) return NULL; /* We set initial to NULL so that the subsequent code will create an empty array of the appropriate type and afterwards we can use array_iter_extend to populate the array. */ initial = NULL; } for (descr = descriptors; descr->typecode != '\0'; descr++) { if (descr->typecode == c) { PyObject *a; Py_ssize_t len; if (initial == NULL || !(PyList_Check(initial) || PyTuple_Check(initial))) len = 0; else len = PySequence_Size(initial); a = newarrayobject(type, len, descr); if (a == NULL) return NULL; if (len > 0) { Py_ssize_t i; for (i = 0; i < len; i++) { PyObject *v = PySequence_GetItem(initial, i); if (v == NULL) { Py_DECREF(a); return NULL; } if (setarrayitem(a, i, v) != 0) { Py_DECREF(v); Py_DECREF(a); return NULL; } Py_DECREF(v); } } else if (initial != NULL && PyString_Check(initial)) { PyObject *t_initial, *v; t_initial = PyTuple_Pack(1, initial); if (t_initial == NULL) { Py_DECREF(a); return NULL; } v = array_fromstring((arrayobject *)a, t_initial); Py_DECREF(t_initial); if (v == NULL) { Py_DECREF(a); return NULL; } Py_DECREF(v);#ifdef Py_USING_UNICODE } else if (initial != NULL && PyUnicode_Check(initial)) { Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial); if (n > 0) { arrayobject *self = (arrayobject *)a; char *item = self->ob_item; item = (char *)PyMem_Realloc(item, n); if (item == NULL) { PyErr_NoMemory(); Py_DECREF(a); return NULL; } self->ob_item = item; self->ob_size = n / sizeof(Py_UNICODE); memcpy(item, PyUnicode_AS_DATA(initial), n); self->allocated = self->ob_size; }#endif } if (it != NULL) { if (array_iter_extend((arrayobject *)a, it) == -1) { Py_DECREF(it); Py_DECREF(a); return NULL; } Py_DECREF(it); } return a; } } PyErr_SetString(PyExc_ValueError, "bad typecode (must be c, b, B, u, h, H, i, I, l, L, f or d)"); return NULL;} PyDoc_STRVAR(module_doc,"This module defines an object type which can efficiently represent\n\an array of basic values: characters, integers, floating point\n\numbers. Arrays are sequence types and behave very much like lists,\n\except that the type of objects stored in them is constrained. The\n\type is specified at object creation time by using a type code, which\n\is a single character. The following type codes are defined:\n\\n\ Type code C Type Minimum size in bytes \n\ 'c' character 1 \n\ 'b' signed integer 1 \n\ 'B' unsigned integer 1 \n\ 'u' Unicode character 2 \n\ 'h' signed integer 2 \n\ 'H' unsigned integer 2 \n\ 'i' signed integer 2 \n\ 'I' unsigned integer 2 \n\ 'l' signed integer 4 \n\ 'L' unsigned integer 4 \n\ 'f' floating point 4 \n\ 'd' floating point 8 \n\\n\The constructor is:\n\\n\array(typecode [, initializer]) -- create a new array\n\"); PyDoc_STRVAR(arraytype_doc,"array(typecode [, initializer]) -> array\n\\n\Return a new array whose items are restricted by typecode, and\n\initialized from the optional initializer value, which must be a list,\n\string. or iterable over elements of the appropriate type.\n\\n\Arrays represent basic values and behave very much like lists, except\n\the type of objects stored in them is constrained.\n\\n\Methods:\n\\n\append() -- append a new item to the end of the array\n\buffer_info() -- return information giving the current memory info\n\byteswap() -- byteswap all the items of the array\n\count() -- return number of occurences of an object\n\extend() -- extend array by appending multiple elements from an iterable\n\fromfile() -- read items from a file object\n\fromlist() -- append items from the list\n\fromstring() -- append items from the string\n\index() -- return index of first occurence of an object\n\insert() -- insert a new item into the array at a provided position\n\pop() -- remove and return item (default last)\n\read() -- DEPRECATED, use fromfile()\n\remove() -- remove first occurence of an object\n\reverse() -- reverse the order of the items in the array\n\tofile() -- write all items to a file object\n\tolist() -- return the array converted to an ordinary list\n\tostring() -- return the array converted to a string\n\write() -- DEPRECATED, use tofile()\n\\n\Attributes:\n\\n\typecode -- the typecode character used to create the array\n\itemsize -- the length in bytes of one array item\n\"); static PyObject *array_iter(arrayobject *ao); static PyTypeObject Arraytype = { PyObject_HEAD_INIT(NULL) 0, "array.array", sizeof(arrayobject), 0, (destructor)array_dealloc, /* tp_dealloc */ 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_compare */ (reprfunc)array_repr, /* tp_repr */ 0, /* tp_as_number*/ &array_as_sequence, /* tp_as_sequence*/ &array_as_mapping, /* tp_as_mapping*/ 0, /* tp_hash */ 0, /* tp_call */ 0, /* tp_str */ PyObject_GenericGetAttr, /* tp_getattro */ 0, /* tp_setattro */ &array_as_buffer, /* tp_as_buffer*/Lines 1501–2000 of 2141. Previous lines Load more
{"__copy__", (PyCFunction)array_copy, METH_NOARGS, copy_doc}, {"count", (PyCFunction)array_count, METH_O, count_doc}, {"__deepcopy__",(PyCFunction)array_copy, METH_O, copy_doc}, {"extend", (PyCFunction)array_extend, METH_O, extend_doc}, {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS, fromfile_doc}, {"fromlist", (PyCFunction)array_fromlist, METH_O, fromlist_doc}, {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS, fromstring_doc}, #ifdef Py_USING_UNICODE {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS, fromunicode_doc}, #endif {"index", (PyCFunction)array_index, METH_O, index_doc}, {"insert", (PyCFunction)array_insert, METH_VARARGS, insert_doc}, {"pop", (PyCFunction)array_pop, METH_VARARGS, pop_doc}, {"read", (PyCFunction)array_fromfile, METH_VARARGS, fromfile_doc}, {"__reduce__", (PyCFunction)array_reduce, METH_NOARGS, array_doc}, {"remove", (PyCFunction)array_remove, METH_O, remove_doc}, {"reverse", (PyCFunction)array_reverse, METH_NOARGS, reverse_doc}, /* {"sort", (PyCFunction)array_sort, METH_VARARGS, sort_doc},*/ {"tofile", (PyCFunction)array_tofile, METH_O, tofile_doc}, {"tolist", (PyCFunction)array_tolist, METH_NOARGS, tolist_doc}, {"tostring", (PyCFunction)array_tostring, METH_NOARGS, tostring_doc}, #ifdef Py_USING_UNICODE {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS, tounicode_doc}, #endif {"write", (PyCFunction)array_tofile, METH_O, tofile_doc}, {NULL, NULL} /* sentinel */ }; static PyObject * array_repr(arrayobject *a) { char buf[256], typecode; PyObject *s, *t, *v = NULL; Py_ssize_t len; len = a->ob_size; typecode = a->ob_descr->typecode; if (len == 0) { PyOS_snprintf(buf, sizeof(buf), "array('%c')", typecode); return PyString_FromString(buf); } if (typecode == 'c') v = array_tostring(a, NULL); #ifdef Py_USING_UNICODE else if (typecode == 'u') v = array_tounicode(a, NULL); #endif else v = array_tolist(a, NULL); t = PyObject_Repr(v); Py_XDECREF(v); PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode); s = PyString_FromString(buf); PyString_ConcatAndDel(&s, t); PyString_ConcatAndDel(&s, PyString_FromString(")")); return s; } static PyObject* array_subscr(arrayobject* self, PyObject* item) { if (PyIndex_Check(item)) { Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); if (i==-1 && PyErr_Occurred()) { return NULL; } if (i < 0) i += self->ob_size; return array_item(self, i); } else if (PySlice_Check(item)) { Py_ssize_t start, stop, step, slicelength, cur, i; PyObject* result; arrayobject* ar; int itemsize = self->ob_descr->itemsize; if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size, &start, &stop, &step, &slicelength) < 0) { return NULL; } if (slicelength <= 0) { return newarrayobject(&Arraytype, 0, self->ob_descr); } else { result = newarrayobject(&Arraytype, slicelength, self->ob_descr); if (!result) return NULL; ar = (arrayobject*)result; for (cur = start, i = 0; i < slicelength; cur += step, i++) { memcpy(ar->ob_item + i*itemsize, self->ob_item + cur*itemsize, itemsize); } return result; } } else { PyErr_SetString(PyExc_TypeError, "list indices must be integers"); return NULL; } } static int array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value) { if (PyIndex_Check(item)) { Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); if (i==-1 && PyErr_Occurred()) return -1; if (i < 0) i += self->ob_size; return array_ass_item(self, i, value); } else if (PySlice_Check(item)) { Py_ssize_t start, stop, step, slicelength; int itemsize = self->ob_descr->itemsize; if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size, &start, &stop, &step, &slicelength) < 0) { return -1; } /* treat A[slice(a,b)] = v _exactly_ like A[a:b] = v */ if (step == 1 && ((PySliceObject*)item)->step == Py_None) return array_ass_slice(self, start, stop, value); if (value == NULL) { /* delete slice */ Py_ssize_t cur, i, extra; if (slicelength <= 0) return 0; if (step < 0) { stop = start + 1; start = stop + step*(slicelength - 1) - 1; step = -step; } for (cur = start, i = 0; i < slicelength - 1; cur += step, i++) { memmove(self->ob_item + (cur - i)*itemsize, self->ob_item + (cur + 1)*itemsize, (step - 1) * itemsize); } extra = self->ob_size - (cur + 1); if (extra > 0) { memmove(self->ob_item + (cur - i)*itemsize, self->ob_item + (cur + 1)*itemsize, extra*itemsize); } self->ob_size -= slicelength; self->ob_item = (char *)PyMem_REALLOC(self->ob_item, itemsize*self->ob_size); self->allocated = self->ob_size; return 0; } else { /* assign slice */ Py_ssize_t cur, i; arrayobject* av; if (!array_Check(value)) { PyErr_Format(PyExc_TypeError, "must assign array (not \"%.200s\") to slice", value->ob_type->tp_name); return -1; } av = (arrayobject*)value; if (av->ob_size != slicelength) { PyErr_Format(PyExc_ValueError, "attempt to assign array of size %ld to extended slice of size %ld", /*XXX*/(long)av->ob_size, /*XXX*/(long)slicelength); return -1; } if (!slicelength) return 0; /* protect against a[::-1] = a */ if (self == av) { value = array_slice(av, 0, av->ob_size); av = (arrayobject*)value; if (!av) return -1; } else { Py_INCREF(value); } for (cur = start, i = 0; i < slicelength; cur += step, i++) { memcpy(self->ob_item + cur*itemsize, av->ob_item + i*itemsize, itemsize); } Py_DECREF(value); return 0; } } else { PyErr_SetString(PyExc_TypeError, "list indices must be integers"); return -1; } } static PyMappingMethods array_as_mapping = { (lenfunc)array_length, (binaryfunc)array_subscr, (objobjargproc)array_ass_subscr }; static const void *emptybuf = ""; static Py_ssize_t array_buffer_getreadbuf(arrayobject *self, Py_ssize_t index, const void **ptr) { if ( index != 0 ) { PyErr_SetString(PyExc_SystemError, "Accessing non-existent array segment"); return -1; } *ptr = (void *)self->ob_item; if (*ptr == NULL) *ptr = emptybuf; return self->ob_size*self->ob_descr->itemsize; } static Py_ssize_t array_buffer_getwritebuf(arrayobject *self, Py_ssize_t index, const void **ptr) { if ( index != 0 ) { PyErr_SetString(PyExc_SystemError, "Accessing non-existent array segment"); return -1; } *ptr = (void *)self->ob_item; if (*ptr == NULL) *ptr = emptybuf; return self->ob_size*self->ob_descr->itemsize; } static Py_ssize_t array_buffer_getsegcount(arrayobject *self, Py_ssize_t *lenp) { if ( lenp ) *lenp = self->ob_size*self->ob_descr->itemsize; return 1; } static PySequenceMethods array_as_sequence = { (lenfunc)array_length, /*sq_length*/ (binaryfunc)array_concat, /*sq_concat*/ (ssizeargfunc)array_repeat, /*sq_repeat*/ (ssizeargfunc)array_item, /*sq_item*/ (ssizessizeargfunc)array_slice, /*sq_slice*/ (ssizeobjargproc)array_ass_item, /*sq_ass_item*/ (ssizessizeobjargproc)array_ass_slice, /*sq_ass_slice*/ (objobjproc)array_contains, /*sq_contains*/ (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/ (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/ }; static PyBufferProcs array_as_buffer = { (readbufferproc)array_buffer_getreadbuf, (writebufferproc)array_buffer_getwritebuf, (segcountproc)array_buffer_getsegcount, NULL, }; static PyObject * array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { char c; PyObject *initial = NULL, *it = NULL; struct arraydescr *descr; if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds)) return NULL; if (!PyArg_ParseTuple(args, "c|O:array", &c, &initial)) return NULL; if (!(initial == NULL || PyList_Check(initial) || PyString_Check(initial) || PyTuple_Check(initial) || (c == 'u' && PyUnicode_Check(initial)))) { it = PyObject_GetIter(initial); if (it == NULL) return NULL; /* We set initial to NULL so that the subsequent code will create an empty array of the appropriate type and afterwards we can use array_iter_extend to populate the array. */ initial = NULL; } for (descr = descriptors; descr->typecode != '\0'; descr++) { if (descr->typecode == c) { PyObject *a; Py_ssize_t len; if (initial == NULL || !(PyList_Check(initial) || PyTuple_Check(initial))) len = 0; else len = PySequence_Size(initial); a = newarrayobject(type, len, descr); if (a == NULL) return NULL; if (len > 0) { Py_ssize_t i; for (i = 0; i < len; i++) { PyObject *v = PySequence_GetItem(initial, i); if (v == NULL) { Py_DECREF(a); return NULL; } if (setarrayitem(a, i, v) != 0) { Py_DECREF(v); Py_DECREF(a); return NULL; } Py_DECREF(v); } } else if (initial != NULL && PyString_Check(initial)) { PyObject *t_initial, *v; t_initial = PyTuple_Pack(1, initial); if (t_initial == NULL) { Py_DECREF(a); return NULL; } v = array_fromstring((arrayobject *)a, t_initial); Py_DECREF(t_initial); if (v == NULL) { Py_DECREF(a); return NULL; } Py_DECREF(v); #ifdef Py_USING_UNICODE } else if (initial != NULL && PyUnicode_Check(initial)) { Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial); if (n > 0) { arrayobject *self = (arrayobject *)a; char *item = self->ob_item; item = (char *)PyMem_Realloc(item, n); if (item == NULL) { PyErr_NoMemory(); Py_DECREF(a); return NULL; } self->ob_item = item; self->ob_size = n / sizeof(Py_UNICODE); memcpy(item, PyUnicode_AS_DATA(initial), n); self->allocated = self->ob_size; } #endif } if (it != NULL) { if (array_iter_extend((arrayobject *)a, it) == -1) { Py_DECREF(it); Py_DECREF(a); return NULL; } Py_DECREF(it); } return a; } } PyErr_SetString(PyExc_ValueError, "bad typecode (must be c, b, B, u, h, H, i, I, l, L, f or d)"); return NULL; } PyDoc_STRVAR(module_doc, "This module defines an object type which can efficiently represent\n\ an array of basic values: characters, integers, floating point\n\ numbers. Arrays are sequence types and behave very much like lists,\n\ except that the type of objects stored in them is constrained. The\n\ type is specified at object creation time by using a type code, which\n\ is a single character. The following type codes are defined:\n\ \n\ Type code C Type Minimum size in bytes \n\ 'c' character 1 \n\ 'b' signed integer 1 \n\ 'B' unsigned integer 1 \n\ 'u' Unicode character 2 \n\ 'h' signed integer 2 \n\ 'H' unsigned integer 2 \n\ 'i' signed integer 2 \n\ 'I' unsigned integer 2 \n\ 'l' signed integer 4 \n\ 'L' unsigned integer 4 \n\ 'f' floating point 4 \n\ 'd' floating point 8 \n\ \n\ The constructor is:\n\ \n\ array(typecode [, initializer]) -- create a new array\n\ "); PyDoc_STRVAR(arraytype_doc, "array(typecode [, initializer]) -> array\n\ \n\ Return a new array whose items are restricted by typecode, and\n\ initialized from the optional initializer value, which must be a list,\n\ string. or iterable over elements of the appropriate type.\n\ \n\ Arrays represent basic values and behave very much like lists, except\n\ the type of objects stored in them is constrained.\n\ \n\ Methods:\n\ \n\ append() -- append a new item to the end of the array\n\ buffer_info() -- return information giving the current memory info\n\ byteswap() -- byteswap all the items of the array\n\ count() -- return number of occurences of an object\n\ extend() -- extend array by appending multiple elements from an iterable\n\ fromfile() -- read items from a file object\n\ fromlist() -- append items from the list\n\ fromstring() -- append items from the string\n\ index() -- return index of first occurence of an object\n\ insert() -- insert a new item into the array at a provided position\n\ pop() -- remove and return item (default last)\n\ read() -- DEPRECATED, use fromfile()\n\ remove() -- remove first occurence of an object\n\ reverse() -- reverse the order of the items in the array\n\ tofile() -- write all items to a file object\n\ tolist() -- return the array converted to an ordinary list\n\ tostring() -- return the array converted to a string\n\ write() -- DEPRECATED, use tofile()\n\ \n\ Attributes:\n\ \n\ typecode -- the typecode character used to create the array\n\ itemsize -- the length in bytes of one array item\n\ "); static PyObject *array_iter(arrayobject *ao); static PyTypeObject Arraytype = { PyObject_HEAD_INIT(NULL) 0, "array.array", sizeof(arrayobject), 0, (destructor)array_dealloc, /* tp_dealloc */ 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_compare */ (reprfunc)array_repr, /* tp_repr */ 0, /* tp_as_number*/ &array_as_sequence, /* tp_as_sequence*/ &array_as_mapping, /* tp_as_mapping*/ 0, /* tp_hash */ 0, /* tp_call */ 0, /* tp_str */ PyObject_GenericGetAttr, /* tp_getattro */ 0, /* tp_setattro */ &array_as_buffer, /* tp_as_buffer*/