diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2006-02-15 17:27:45 (GMT) |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2006-02-15 17:27:45 (GMT) |
commit | 18e165558b24d29e7e0ca501842b9236589b012a (patch) | |
tree | 841678b5dc1aff3aa48701fee33a6ba7be00a72b /Modules/arraymodule.c | |
parent | 44829297348d9121a03fc7df2fac557b583cc7fa (diff) | |
download | cpython-18e165558b24d29e7e0ca501842b9236589b012a.zip cpython-18e165558b24d29e7e0ca501842b9236589b012a.tar.gz cpython-18e165558b24d29e7e0ca501842b9236589b012a.tar.bz2 |
Merge ssize_t branch.
Diffstat (limited to 'Modules/arraymodule.c')
-rw-r--r-- | Modules/arraymodule.c | 193 |
1 files changed, 99 insertions, 94 deletions
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 4c7cdf2..704b745 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -3,6 +3,7 @@ /* An array is a uniform list -- all items have the same type. The item type is restricted to simple C types like int or float */ +#define PY_SSIZE_T_CLEAN #include "Python.h" #include "structmember.h" @@ -23,15 +24,15 @@ struct arrayobject; /* Forward */ struct arraydescr { int typecode; int itemsize; - PyObject * (*getitem)(struct arrayobject *, int); - int (*setitem)(struct arrayobject *, int, PyObject *); + PyObject * (*getitem)(struct arrayobject *, Py_ssize_t); + int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *); }; typedef struct arrayobject { PyObject_HEAD - int ob_size; + Py_ssize_t ob_size; char *ob_item; - int allocated; + Py_ssize_t allocated; struct arraydescr *ob_descr; PyObject *weakreflist; /* List of weak references */ } arrayobject; @@ -42,7 +43,7 @@ static PyTypeObject Arraytype; #define array_CheckExact(op) ((op)->ob_type == &Arraytype) static int -array_resize(arrayobject *self, int newsize) +array_resize(arrayobject *self, Py_ssize_t newsize) { char *items; size_t _new_size; @@ -102,13 +103,13 @@ in bounds; that's the responsibility of the caller. ****************************************************************************/ static PyObject * -c_getitem(arrayobject *ap, int i) +c_getitem(arrayobject *ap, Py_ssize_t i) { return PyString_FromStringAndSize(&((char *)ap->ob_item)[i], 1); } static int -c_setitem(arrayobject *ap, int i, PyObject *v) +c_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) { char x; if (!PyArg_Parse(v, "c;array item must be char", &x)) @@ -119,7 +120,7 @@ c_setitem(arrayobject *ap, int i, PyObject *v) } static PyObject * -b_getitem(arrayobject *ap, int i) +b_getitem(arrayobject *ap, Py_ssize_t i) { long x = ((char *)ap->ob_item)[i]; if (x >= 128) @@ -128,7 +129,7 @@ b_getitem(arrayobject *ap, int i) } static int -b_setitem(arrayobject *ap, int i, PyObject *v) +b_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) { short x; /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore @@ -152,14 +153,14 @@ b_setitem(arrayobject *ap, int i, PyObject *v) } static PyObject * -BB_getitem(arrayobject *ap, int i) +BB_getitem(arrayobject *ap, Py_ssize_t i) { long x = ((unsigned char *)ap->ob_item)[i]; return PyInt_FromLong(x); } static int -BB_setitem(arrayobject *ap, int i, PyObject *v) +BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) { unsigned char x; /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */ @@ -172,16 +173,16 @@ BB_setitem(arrayobject *ap, int i, PyObject *v) #ifdef Py_USING_UNICODE static PyObject * -u_getitem(arrayobject *ap, int i) +u_getitem(arrayobject *ap, Py_ssize_t i) { return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1); } static int -u_setitem(arrayobject *ap, int i, PyObject *v) +u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) { Py_UNICODE *p; - int len; + Py_ssize_t len; if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len)) return -1; @@ -196,13 +197,13 @@ u_setitem(arrayobject *ap, int i, PyObject *v) #endif static PyObject * -h_getitem(arrayobject *ap, int i) +h_getitem(arrayobject *ap, Py_ssize_t i) { return PyInt_FromLong((long) ((short *)ap->ob_item)[i]); } static int -h_setitem(arrayobject *ap, int i, PyObject *v) +h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) { short x; /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */ @@ -214,13 +215,13 @@ h_setitem(arrayobject *ap, int i, PyObject *v) } static PyObject * -HH_getitem(arrayobject *ap, int i) +HH_getitem(arrayobject *ap, Py_ssize_t i) { return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[i]); } static int -HH_setitem(arrayobject *ap, int i, PyObject *v) +HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) { int x; /* PyArg_Parse's 'h' formatter is for a signed short, therefore @@ -243,13 +244,13 @@ HH_setitem(arrayobject *ap, int i, PyObject *v) } static PyObject * -i_getitem(arrayobject *ap, int i) +i_getitem(arrayobject *ap, Py_ssize_t i) { return PyInt_FromLong((long) ((int *)ap->ob_item)[i]); } static int -i_setitem(arrayobject *ap, int i, PyObject *v) +i_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) { int x; /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */ @@ -261,14 +262,14 @@ i_setitem(arrayobject *ap, int i, PyObject *v) } static PyObject * -II_getitem(arrayobject *ap, int i) +II_getitem(arrayobject *ap, Py_ssize_t i) { return PyLong_FromUnsignedLong( (unsigned long) ((unsigned int *)ap->ob_item)[i]); } static int -II_setitem(arrayobject *ap, int i, PyObject *v) +II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) { unsigned long x; if (PyLong_Check(v)) { @@ -300,13 +301,13 @@ II_setitem(arrayobject *ap, int i, PyObject *v) } static PyObject * -l_getitem(arrayobject *ap, int i) +l_getitem(arrayobject *ap, Py_ssize_t i) { return PyInt_FromLong(((long *)ap->ob_item)[i]); } static int -l_setitem(arrayobject *ap, int i, PyObject *v) +l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) { long x; if (!PyArg_Parse(v, "l;array item must be integer", &x)) @@ -317,13 +318,13 @@ l_setitem(arrayobject *ap, int i, PyObject *v) } static PyObject * -LL_getitem(arrayobject *ap, int i) +LL_getitem(arrayobject *ap, Py_ssize_t i) { return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]); } static int -LL_setitem(arrayobject *ap, int i, PyObject *v) +LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) { unsigned long x; if (PyLong_Check(v)) { @@ -355,13 +356,13 @@ LL_setitem(arrayobject *ap, int i, PyObject *v) } static PyObject * -f_getitem(arrayobject *ap, int i) +f_getitem(arrayobject *ap, Py_ssize_t i) { return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]); } static int -f_setitem(arrayobject *ap, int i, PyObject *v) +f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) { float x; if (!PyArg_Parse(v, "f;array item must be float", &x)) @@ -372,13 +373,13 @@ f_setitem(arrayobject *ap, int i, PyObject *v) } static PyObject * -d_getitem(arrayobject *ap, int i) +d_getitem(arrayobject *ap, Py_ssize_t i) { return PyFloat_FromDouble(((double *)ap->ob_item)[i]); } static int -d_setitem(arrayobject *ap, int i, PyObject *v) +d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) { double x; if (!PyArg_Parse(v, "d;array item must be float", &x)) @@ -412,7 +413,7 @@ Implementations of array object methods. ****************************************************************************/ static PyObject * -newarrayobject(PyTypeObject *type, int size, struct arraydescr *descr) +newarrayobject(PyTypeObject *type, Py_ssize_t size, struct arraydescr *descr) { arrayobject *op; size_t nbytes; @@ -449,7 +450,7 @@ newarrayobject(PyTypeObject *type, int size, struct arraydescr *descr) } static PyObject * -getarrayitem(PyObject *op, int i) +getarrayitem(PyObject *op, Py_ssize_t i) { register arrayobject *ap; assert(array_Check(op)); @@ -459,10 +460,10 @@ getarrayitem(PyObject *op, int i) } static int -ins1(arrayobject *self, int where, PyObject *v) +ins1(arrayobject *self, Py_ssize_t where, PyObject *v) { char *items; - int n = self->ob_size; + Py_ssize_t n = self->ob_size; if (v == NULL) { PyErr_BadInternalCall(); return -1; @@ -506,7 +507,7 @@ array_richcompare(PyObject *v, PyObject *w, int op) arrayobject *va, *wa; PyObject *vi = NULL; PyObject *wi = NULL; - int i, k; + Py_ssize_t i, k; PyObject *res; if (!array_Check(v) || !array_Check(w)) { @@ -548,8 +549,8 @@ array_richcompare(PyObject *v, PyObject *w, int op) if (k) { /* No more items to compare -- compare sizes */ - int vs = va->ob_size; - int ws = wa->ob_size; + Py_ssize_t vs = va->ob_size; + Py_ssize_t ws = wa->ob_size; int cmp; switch (op) { case Py_LT: cmp = vs < ws; break; @@ -586,14 +587,14 @@ array_richcompare(PyObject *v, PyObject *w, int op) return res; } -static int +static Py_ssize_t array_length(arrayobject *a) { return a->ob_size; } static PyObject * -array_item(arrayobject *a, int i) +array_item(arrayobject *a, Py_ssize_t i) { if (i < 0 || i >= a->ob_size) { PyErr_SetString(PyExc_IndexError, "array index out of range"); @@ -603,7 +604,7 @@ array_item(arrayobject *a, int i) } static PyObject * -array_slice(arrayobject *a, int ilow, int ihigh) +array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh) { arrayobject *np; if (ilow < 0) @@ -638,7 +639,7 @@ PyDoc_STRVAR(copy_doc, static PyObject * array_concat(arrayobject *a, PyObject *bb) { - int size; + Py_ssize_t size; arrayobject *np; if (!array_Check(bb)) { PyErr_Format(PyExc_TypeError, @@ -664,13 +665,13 @@ array_concat(arrayobject *a, PyObject *bb) } static PyObject * -array_repeat(arrayobject *a, int n) +array_repeat(arrayobject *a, Py_ssize_t n) { - int i; - int size; + Py_ssize_t i; + Py_ssize_t size; arrayobject *np; char *p; - int nbytes; + Py_ssize_t nbytes; if (n < 0) n = 0; size = a->ob_size * n; @@ -760,7 +761,7 @@ array_ass_slice(arrayobject *a, int ilow, int ihigh, PyObject *v) } static int -array_ass_item(arrayobject *a, int i, PyObject *v) +array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v) { if (i < 0 || i >= a->ob_size) { PyErr_SetString(PyExc_IndexError, @@ -773,7 +774,7 @@ array_ass_item(arrayobject *a, int i, PyObject *v) } static int -setarrayitem(PyObject *a, int i, PyObject *v) +setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v) { assert(array_Check(a)); return array_ass_item((arrayobject *)a, i, v); @@ -805,7 +806,7 @@ array_iter_extend(arrayobject *self, PyObject *bb) static int array_do_extend(arrayobject *self, PyObject *bb) { - int size; + Py_ssize_t size; if (!array_Check(bb)) return array_iter_extend(self, bb); @@ -847,10 +848,10 @@ array_inplace_concat(arrayobject *self, PyObject *bb) } static PyObject * -array_inplace_repeat(arrayobject *self, int n) +array_inplace_repeat(arrayobject *self, Py_ssize_t n) { char *items, *p; - int size, i; + Py_ssize_t size, i; if (self->ob_size > 0) { if (n < 0) @@ -883,7 +884,7 @@ array_inplace_repeat(arrayobject *self, int n) static PyObject * -ins(arrayobject *self, int where, PyObject *v) +ins(arrayobject *self, Py_ssize_t where, PyObject *v) { if (ins1(self, where, v) != 0) return NULL; @@ -894,8 +895,8 @@ ins(arrayobject *self, int where, PyObject *v) static PyObject * array_count(arrayobject *self, PyObject *v) { - int count = 0; - int i; + Py_ssize_t count = 0; + Py_ssize_t i; for (i = 0; i < self->ob_size; i++) { PyObject *selfi = getarrayitem((PyObject *)self, i); @@ -906,7 +907,10 @@ array_count(arrayobject *self, PyObject *v) else if (cmp < 0) return NULL; } - return PyInt_FromLong((long)count); + if (i < LONG_MAX) + return PyInt_FromLong((long)count); + else + return PyLong_FromLong(count); } PyDoc_STRVAR(count_doc, @@ -917,7 +921,7 @@ Return number of occurences of x in the array."); static PyObject * array_index(arrayobject *self, PyObject *v) { - int i; + Py_ssize_t i; for (i = 0; i < self->ob_size; i++) { PyObject *selfi = getarrayitem((PyObject *)self, i); @@ -941,7 +945,8 @@ Return index of first occurence of x in the array."); static int array_contains(arrayobject *self, PyObject *v) { - int i, cmp; + Py_ssize_t i; + int cmp; for (i = 0, cmp = 0 ; cmp == 0 && i < self->ob_size; i++) { PyObject *selfi = getarrayitem((PyObject *)self, i); @@ -1079,7 +1084,7 @@ static PyObject * array_byteswap(arrayobject *self, PyObject *unused) { char *p; - int i; + Py_ssize_t i; switch (self->ob_descr->itemsize) { case 1: @@ -1158,7 +1163,7 @@ PyDoc_STRVAR(array_doc, "Return state information for pickling."); static PyObject * array_reverse(arrayobject *self, PyObject *unused) { - register int itemsize = self->ob_descr->itemsize; + register Py_ssize_t itemsize = self->ob_descr->itemsize; register char *p, *q; /* little buffer to hold items while swapping */ char tmp[256]; /* 8 is probably enough -- but why skimp */ @@ -1223,7 +1228,7 @@ array_fromfile(arrayobject *self, PyObject *args) nread = fread(item + (self->ob_size - n) * itemsize, itemsize, n, fp); if (nread < (size_t)n) { - self->ob_size -= (n - nread); + self->ob_size -= (n - nread); PyMem_RESIZE(item, char, self->ob_size*itemsize); self->ob_item = item; self->allocated = self->ob_size; @@ -1275,8 +1280,8 @@ write."); static PyObject * array_fromlist(arrayobject *self, PyObject *list) { - int n; - int itemsize = self->ob_descr->itemsize; + Py_ssize_t n; + Py_ssize_t itemsize = self->ob_descr->itemsize; if (!PyList_Check(list)) { PyErr_SetString(PyExc_TypeError, "arg must be list"); @@ -1285,7 +1290,7 @@ array_fromlist(arrayobject *self, PyObject *list) n = PyList_Size(list); if (n > 0) { char *item = self->ob_item; - int i; + Py_ssize_t i; PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize); if (item == NULL) { PyErr_NoMemory(); @@ -1321,7 +1326,7 @@ static PyObject * array_tolist(arrayobject *self, PyObject *unused) { PyObject *list = PyList_New(self->ob_size); - int i; + Py_ssize_t i; if (list == NULL) return NULL; @@ -1346,7 +1351,7 @@ static PyObject * array_fromstring(arrayobject *self, PyObject *args) { char *str; - int n; + Py_ssize_t n; int itemsize = self->ob_descr->itemsize; if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n)) return NULL; @@ -1400,7 +1405,7 @@ static PyObject * array_fromunicode(arrayobject *self, PyObject *args) { Py_UNICODE *ustr; - int n; + Py_ssize_t n; if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n)) return NULL; @@ -1541,7 +1546,7 @@ array_repr(arrayobject *a) { char buf[256], typecode; PyObject *s, *t, *v = NULL; - int len; + Py_ssize_t len; len = a->ob_size; typecode = a->ob_descr->typecode; @@ -1586,7 +1591,7 @@ array_subscr(arrayobject* self, PyObject* item) return array_item(self, i); } else if (PySlice_Check(item)) { - int start, stop, step, slicelength, cur, i; + Py_ssize_t start, stop, step, slicelength, cur, i; PyObject* result; arrayobject* ar; int itemsize = self->ob_descr->itemsize; @@ -1640,7 +1645,7 @@ array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value) return array_ass_item(self, i, value); } else if (PySlice_Check(item)) { - int start, stop, step, slicelength; + Py_ssize_t start, stop, step, slicelength; int itemsize = self->ob_descr->itemsize; if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size, @@ -1654,7 +1659,7 @@ array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value) if (value == NULL) { /* delete slice */ - int cur, i, extra; + Py_ssize_t cur, i, extra; if (slicelength <= 0) return 0; @@ -1686,7 +1691,7 @@ array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value) } else { /* assign slice */ - int cur, i; + Py_ssize_t cur, i; arrayobject* av; if (!array_Check(value)) { @@ -1700,8 +1705,8 @@ array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value) if (av->ob_size != slicelength) { PyErr_Format(PyExc_ValueError, - "attempt to assign array of size %d to extended slice of size %d", - av->ob_size, slicelength); + "attempt to assign array of size %ld to extended slice of size %ld", + /*XXX*/(long)av->ob_size, /*XXX*/(long)slicelength); return -1; } @@ -1737,13 +1742,13 @@ array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value) } static PyMappingMethods array_as_mapping = { - (inquiry)array_length, + (lenfunc)array_length, (binaryfunc)array_subscr, (objobjargproc)array_ass_subscr }; -static int -array_buffer_getreadbuf(arrayobject *self, int index, const void **ptr) +static Py_ssize_t +array_buffer_getreadbuf(arrayobject *self, Py_ssize_t index, const void **ptr) { if ( index != 0 ) { PyErr_SetString(PyExc_SystemError, @@ -1754,8 +1759,8 @@ array_buffer_getreadbuf(arrayobject *self, int index, const void **ptr) return self->ob_size*self->ob_descr->itemsize; } -static int -array_buffer_getwritebuf(arrayobject *self, int index, const void **ptr) +static Py_ssize_t +array_buffer_getwritebuf(arrayobject *self, Py_ssize_t index, const void **ptr) { if ( index != 0 ) { PyErr_SetString(PyExc_SystemError, @@ -1766,8 +1771,8 @@ array_buffer_getwritebuf(arrayobject *self, int index, const void **ptr) return self->ob_size*self->ob_descr->itemsize; } -static int -array_buffer_getsegcount(arrayobject *self, int *lenp) +static Py_ssize_t +array_buffer_getsegcount(arrayobject *self, Py_ssize_t *lenp) { if ( lenp ) *lenp = self->ob_size*self->ob_descr->itemsize; @@ -1775,22 +1780,22 @@ array_buffer_getsegcount(arrayobject *self, int *lenp) } static PySequenceMethods array_as_sequence = { - (inquiry)array_length, /*sq_length*/ + (lenfunc)array_length, /*sq_length*/ (binaryfunc)array_concat, /*sq_concat*/ - (intargfunc)array_repeat, /*sq_repeat*/ - (intargfunc)array_item, /*sq_item*/ - (intintargfunc)array_slice, /*sq_slice*/ - (intobjargproc)array_ass_item, /*sq_ass_item*/ - (intintobjargproc)array_ass_slice, /*sq_ass_slice*/ + (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*/ - (intargfunc)array_inplace_repeat /*sq_inplace_repeat*/ + (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/ }; static PyBufferProcs array_as_buffer = { - (getreadbufferproc)array_buffer_getreadbuf, - (getwritebufferproc)array_buffer_getwritebuf, - (getsegcountproc)array_buffer_getsegcount, + (readbufferproc)array_buffer_getreadbuf, + (writebufferproc)array_buffer_getwritebuf, + (segcountproc)array_buffer_getsegcount, }; static PyObject * @@ -1822,7 +1827,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) for (descr = descriptors; descr->typecode != '\0'; descr++) { if (descr->typecode == c) { PyObject *a; - int len; + Py_ssize_t len; if (initial == NULL || !(PyList_Check(initial) || PyTuple_Check(initial))) @@ -1835,7 +1840,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return NULL; if (len > 0) { - int i; + Py_ssize_t i; for (i = 0; i < len; i++) { PyObject *v = PySequence_GetItem(initial, i); @@ -1864,7 +1869,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) Py_DECREF(v); #ifdef Py_USING_UNICODE } else if (initial != NULL && PyUnicode_Check(initial)) { - int n = PyUnicode_GET_DATA_SIZE(initial); + Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial); if (n > 0) { arrayobject *self = (arrayobject *)a; char *item = self->ob_item; @@ -2012,9 +2017,9 @@ static PyTypeObject Arraytype = { typedef struct { PyObject_HEAD - long index; + Py_ssize_t index; arrayobject *ao; - PyObject * (*getitem)(struct arrayobject *, int); + PyObject * (*getitem)(struct arrayobject *, Py_ssize_t); } arrayiterobject; static PyTypeObject PyArrayIter_Type; |