Mercurial > cpython-lru_cache-weakref
comparison Modules/arraymodule.c @ 61467:f197407bdba2 3.1
Recorded merge of revisions 81032 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
................
r81032 | antoine.pitrou | 2010-05-09 17:52:27 +0200 (dim., 09 mai 2010) | 9 lines
Recorded merge of revisions 81029 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r81029 | antoine.pitrou | 2010-05-09 16:46:46 +0200 (dim., 09 mai 2010) | 3 lines
Untabify C files. Will watch buildbots.
........
................
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Sun, 09 May 2010 16:14:21 +0000 |
| parents | 0293182469a7 |
| children | 6b28b88f6c4e |
comparison
equal
deleted
inserted
replaced
| 61459:f8c19c5f5dd2 | 61467:f197407bdba2 |
|---|---|
| 9 | 9 |
| 10 #ifdef STDC_HEADERS | 10 #ifdef STDC_HEADERS |
| 11 #include <stddef.h> | 11 #include <stddef.h> |
| 12 #else /* !STDC_HEADERS */ | 12 #else /* !STDC_HEADERS */ |
| 13 #ifdef HAVE_SYS_TYPES_H | 13 #ifdef HAVE_SYS_TYPES_H |
| 14 #include <sys/types.h> /* For size_t */ | 14 #include <sys/types.h> /* For size_t */ |
| 15 #endif /* HAVE_SYS_TYPES_H */ | 15 #endif /* HAVE_SYS_TYPES_H */ |
| 16 #endif /* !STDC_HEADERS */ | 16 #endif /* !STDC_HEADERS */ |
| 17 | 17 |
| 18 struct arrayobject; /* Forward */ | 18 struct arrayobject; /* Forward */ |
| 19 | 19 |
| 20 /* All possible arraydescr values are defined in the vector "descriptors" | 20 /* All possible arraydescr values are defined in the vector "descriptors" |
| 21 * below. That's defined later because the appropriate get and set | 21 * below. That's defined later because the appropriate get and set |
| 22 * functions aren't visible yet. | 22 * functions aren't visible yet. |
| 23 */ | 23 */ |
| 24 struct arraydescr { | 24 struct arraydescr { |
| 25 int typecode; | 25 int typecode; |
| 26 int itemsize; | 26 int itemsize; |
| 27 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t); | 27 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t); |
| 28 int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *); | 28 int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *); |
| 29 char *formats; | 29 char *formats; |
| 30 }; | 30 }; |
| 31 | 31 |
| 32 typedef struct arrayobject { | 32 typedef struct arrayobject { |
| 33 PyObject_VAR_HEAD | 33 PyObject_VAR_HEAD |
| 34 char *ob_item; | 34 char *ob_item; |
| 35 Py_ssize_t allocated; | 35 Py_ssize_t allocated; |
| 36 struct arraydescr *ob_descr; | 36 struct arraydescr *ob_descr; |
| 37 PyObject *weakreflist; /* List of weak references */ | 37 PyObject *weakreflist; /* List of weak references */ |
| 38 int ob_exports; /* Number of exported buffers */ | 38 int ob_exports; /* Number of exported buffers */ |
| 39 } arrayobject; | 39 } arrayobject; |
| 40 | 40 |
| 41 static PyTypeObject Arraytype; | 41 static PyTypeObject Arraytype; |
| 42 | 42 |
| 43 #define array_Check(op) PyObject_TypeCheck(op, &Arraytype) | 43 #define array_Check(op) PyObject_TypeCheck(op, &Arraytype) |
| 44 #define array_CheckExact(op) (Py_TYPE(op) == &Arraytype) | 44 #define array_CheckExact(op) (Py_TYPE(op) == &Arraytype) |
| 45 | 45 |
| 46 static int | 46 static int |
| 47 array_resize(arrayobject *self, Py_ssize_t newsize) | 47 array_resize(arrayobject *self, Py_ssize_t newsize) |
| 48 { | 48 { |
| 49 char *items; | 49 char *items; |
| 50 size_t _new_size; | 50 size_t _new_size; |
| 51 | 51 |
| 52 if (self->ob_exports > 0 && newsize != Py_SIZE(self)) { | 52 if (self->ob_exports > 0 && newsize != Py_SIZE(self)) { |
| 53 PyErr_SetString(PyExc_BufferError, | 53 PyErr_SetString(PyExc_BufferError, |
| 54 "cannot resize an array that is exporting buffers"); | 54 "cannot resize an array that is exporting buffers"); |
| 55 return -1; | 55 return -1; |
| 56 } | 56 } |
| 57 | 57 |
| 58 /* Bypass realloc() when a previous overallocation is large enough | 58 /* Bypass realloc() when a previous overallocation is large enough |
| 59 to accommodate the newsize. If the newsize is 16 smaller than the | 59 to accommodate the newsize. If the newsize is 16 smaller than the |
| 60 current size, then proceed with the realloc() to shrink the array. | 60 current size, then proceed with the realloc() to shrink the array. |
| 61 */ | 61 */ |
| 62 | 62 |
| 63 if (self->allocated >= newsize && | 63 if (self->allocated >= newsize && |
| 64 Py_SIZE(self) < newsize + 16 && | 64 Py_SIZE(self) < newsize + 16 && |
| 65 self->ob_item != NULL) { | 65 self->ob_item != NULL) { |
| 66 Py_SIZE(self) = newsize; | 66 Py_SIZE(self) = newsize; |
| 67 return 0; | 67 return 0; |
| 68 } | 68 } |
| 69 | 69 |
| 70 if (newsize == 0) { | 70 if (newsize == 0) { |
| 71 PyMem_FREE(self->ob_item); | 71 PyMem_FREE(self->ob_item); |
| 72 self->ob_item = NULL; | 72 self->ob_item = NULL; |
| 73 Py_SIZE(self) = 0; | 73 Py_SIZE(self) = 0; |
| 74 self->allocated = 0; | 74 self->allocated = 0; |
| 75 return 0; | 75 return 0; |
| 76 } | 76 } |
| 77 | 77 |
| 78 /* This over-allocates proportional to the array size, making room | 78 /* This over-allocates proportional to the array size, making room |
| 79 * for additional growth. The over-allocation is mild, but is | 79 * for additional growth. The over-allocation is mild, but is |
| 80 * enough to give linear-time amortized behavior over a long | 80 * enough to give linear-time amortized behavior over a long |
| 81 * sequence of appends() in the presence of a poorly-performing | 81 * sequence of appends() in the presence of a poorly-performing |
| 82 * system realloc(). | 82 * system realloc(). |
| 83 * The growth pattern is: 0, 4, 8, 16, 25, 34, 46, 56, 67, 79, ... | 83 * The growth pattern is: 0, 4, 8, 16, 25, 34, 46, 56, 67, 79, ... |
| 84 * Note, the pattern starts out the same as for lists but then | 84 * Note, the pattern starts out the same as for lists but then |
| 85 * grows at a smaller rate so that larger arrays only overallocate | 85 * grows at a smaller rate so that larger arrays only overallocate |
| 86 * by about 1/16th -- this is done because arrays are presumed to be more | 86 * by about 1/16th -- this is done because arrays are presumed to be more |
| 87 * memory critical. | 87 * memory critical. |
| 88 */ | 88 */ |
| 89 | 89 |
| 90 _new_size = (newsize >> 4) + (Py_SIZE(self) < 8 ? 3 : 7) + newsize; | 90 _new_size = (newsize >> 4) + (Py_SIZE(self) < 8 ? 3 : 7) + newsize; |
| 91 items = self->ob_item; | 91 items = self->ob_item; |
| 92 /* XXX The following multiplication and division does not optimize away | 92 /* XXX The following multiplication and division does not optimize away |
| 93 like it does for lists since the size is not known at compile time */ | 93 like it does for lists since the size is not known at compile time */ |
| 94 if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize)) | 94 if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize)) |
| 95 PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize)); | 95 PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize)); |
| 96 else | 96 else |
| 97 items = NULL; | 97 items = NULL; |
| 98 if (items == NULL) { | 98 if (items == NULL) { |
| 99 PyErr_NoMemory(); | 99 PyErr_NoMemory(); |
| 100 return -1; | 100 return -1; |
| 101 } | 101 } |
| 102 self->ob_item = items; | 102 self->ob_item = items; |
| 103 Py_SIZE(self) = newsize; | 103 Py_SIZE(self) = newsize; |
| 104 self->allocated = _new_size; | 104 self->allocated = _new_size; |
| 105 return 0; | 105 return 0; |
| 106 } | 106 } |
| 107 | 107 |
| 108 /**************************************************************************** | 108 /**************************************************************************** |
| 109 Get and Set functions for each type. | 109 Get and Set functions for each type. |
| 110 A Get function takes an arrayobject* and an integer index, returning the | 110 A Get function takes an arrayobject* and an integer index, returning the |
| 118 ****************************************************************************/ | 118 ****************************************************************************/ |
| 119 | 119 |
| 120 static PyObject * | 120 static PyObject * |
| 121 b_getitem(arrayobject *ap, Py_ssize_t i) | 121 b_getitem(arrayobject *ap, Py_ssize_t i) |
| 122 { | 122 { |
| 123 long x = ((char *)ap->ob_item)[i]; | 123 long x = ((char *)ap->ob_item)[i]; |
| 124 if (x >= 128) | 124 if (x >= 128) |
| 125 x -= 256; | 125 x -= 256; |
| 126 return PyLong_FromLong(x); | 126 return PyLong_FromLong(x); |
| 127 } | 127 } |
| 128 | 128 |
| 129 static int | 129 static int |
| 130 b_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) | 130 b_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
| 131 { | 131 { |
| 132 short x; | 132 short x; |
| 133 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore | 133 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore |
| 134 must use the next size up that is signed ('h') and manually do | 134 must use the next size up that is signed ('h') and manually do |
| 135 the overflow checking */ | 135 the overflow checking */ |
| 136 if (!PyArg_Parse(v, "h;array item must be integer", &x)) | 136 if (!PyArg_Parse(v, "h;array item must be integer", &x)) |
| 137 return -1; | 137 return -1; |
| 138 else if (x < -128) { | 138 else if (x < -128) { |
| 139 PyErr_SetString(PyExc_OverflowError, | 139 PyErr_SetString(PyExc_OverflowError, |
| 140 "signed char is less than minimum"); | 140 "signed char is less than minimum"); |
| 141 return -1; | 141 return -1; |
| 142 } | 142 } |
| 143 else if (x > 127) { | 143 else if (x > 127) { |
| 144 PyErr_SetString(PyExc_OverflowError, | 144 PyErr_SetString(PyExc_OverflowError, |
| 145 "signed char is greater than maximum"); | 145 "signed char is greater than maximum"); |
| 146 return -1; | 146 return -1; |
| 147 } | 147 } |
| 148 if (i >= 0) | 148 if (i >= 0) |
| 149 ((char *)ap->ob_item)[i] = (char)x; | 149 ((char *)ap->ob_item)[i] = (char)x; |
| 150 return 0; | 150 return 0; |
| 151 } | 151 } |
| 152 | 152 |
| 153 static PyObject * | 153 static PyObject * |
| 154 BB_getitem(arrayobject *ap, Py_ssize_t i) | 154 BB_getitem(arrayobject *ap, Py_ssize_t i) |
| 155 { | 155 { |
| 156 long x = ((unsigned char *)ap->ob_item)[i]; | 156 long x = ((unsigned char *)ap->ob_item)[i]; |
| 157 return PyLong_FromLong(x); | 157 return PyLong_FromLong(x); |
| 158 } | 158 } |
| 159 | 159 |
| 160 static int | 160 static int |
| 161 BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) | 161 BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
| 162 { | 162 { |
| 163 unsigned char x; | 163 unsigned char x; |
| 164 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */ | 164 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */ |
| 165 if (!PyArg_Parse(v, "b;array item must be integer", &x)) | 165 if (!PyArg_Parse(v, "b;array item must be integer", &x)) |
| 166 return -1; | 166 return -1; |
| 167 if (i >= 0) | 167 if (i >= 0) |
| 168 ((char *)ap->ob_item)[i] = x; | 168 ((char *)ap->ob_item)[i] = x; |
| 169 return 0; | 169 return 0; |
| 170 } | 170 } |
| 171 | 171 |
| 172 static PyObject * | 172 static PyObject * |
| 173 u_getitem(arrayobject *ap, Py_ssize_t i) | 173 u_getitem(arrayobject *ap, Py_ssize_t i) |
| 174 { | 174 { |
| 175 return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1); | 175 return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1); |
| 176 } | 176 } |
| 177 | 177 |
| 178 static int | 178 static int |
| 179 u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) | 179 u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
| 180 { | 180 { |
| 181 Py_UNICODE *p; | 181 Py_UNICODE *p; |
| 182 Py_ssize_t len; | 182 Py_ssize_t len; |
| 183 | 183 |
| 184 if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len)) | 184 if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len)) |
| 185 return -1; | 185 return -1; |
| 186 if (len != 1) { | 186 if (len != 1) { |
| 187 PyErr_SetString(PyExc_TypeError, | 187 PyErr_SetString(PyExc_TypeError, |
| 188 "array item must be unicode character"); | 188 "array item must be unicode character"); |
| 189 return -1; | 189 return -1; |
| 190 } | 190 } |
| 191 if (i >= 0) | 191 if (i >= 0) |
| 192 ((Py_UNICODE *)ap->ob_item)[i] = p[0]; | 192 ((Py_UNICODE *)ap->ob_item)[i] = p[0]; |
| 193 return 0; | 193 return 0; |
| 194 } | 194 } |
| 195 | 195 |
| 196 | 196 |
| 197 static PyObject * | 197 static PyObject * |
| 198 h_getitem(arrayobject *ap, Py_ssize_t i) | 198 h_getitem(arrayobject *ap, Py_ssize_t i) |
| 199 { | 199 { |
| 200 return PyLong_FromLong((long) ((short *)ap->ob_item)[i]); | 200 return PyLong_FromLong((long) ((short *)ap->ob_item)[i]); |
| 201 } | 201 } |
| 202 | 202 |
| 203 | 203 |
| 204 static int | 204 static int |
| 205 h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) | 205 h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
| 206 { | 206 { |
| 207 short x; | 207 short x; |
| 208 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */ | 208 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */ |
| 209 if (!PyArg_Parse(v, "h;array item must be integer", &x)) | 209 if (!PyArg_Parse(v, "h;array item must be integer", &x)) |
| 210 return -1; | 210 return -1; |
| 211 if (i >= 0) | 211 if (i >= 0) |
| 212 ((short *)ap->ob_item)[i] = x; | 212 ((short *)ap->ob_item)[i] = x; |
| 213 return 0; | 213 return 0; |
| 214 } | 214 } |
| 215 | 215 |
| 216 static PyObject * | 216 static PyObject * |
| 217 HH_getitem(arrayobject *ap, Py_ssize_t i) | 217 HH_getitem(arrayobject *ap, Py_ssize_t i) |
| 218 { | 218 { |
| 219 return PyLong_FromLong((long) ((unsigned short *)ap->ob_item)[i]); | 219 return PyLong_FromLong((long) ((unsigned short *)ap->ob_item)[i]); |
| 220 } | 220 } |
| 221 | 221 |
| 222 static int | 222 static int |
| 223 HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) | 223 HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
| 224 { | 224 { |
| 225 int x; | 225 int x; |
| 226 /* PyArg_Parse's 'h' formatter is for a signed short, therefore | 226 /* PyArg_Parse's 'h' formatter is for a signed short, therefore |
| 227 must use the next size up and manually do the overflow checking */ | 227 must use the next size up and manually do the overflow checking */ |
| 228 if (!PyArg_Parse(v, "i;array item must be integer", &x)) | 228 if (!PyArg_Parse(v, "i;array item must be integer", &x)) |
| 229 return -1; | 229 return -1; |
| 230 else if (x < 0) { | 230 else if (x < 0) { |
| 231 PyErr_SetString(PyExc_OverflowError, | 231 PyErr_SetString(PyExc_OverflowError, |
| 232 "unsigned short is less than minimum"); | 232 "unsigned short is less than minimum"); |
| 233 return -1; | 233 return -1; |
| 234 } | 234 } |
| 235 else if (x > USHRT_MAX) { | 235 else if (x > USHRT_MAX) { |
| 236 PyErr_SetString(PyExc_OverflowError, | 236 PyErr_SetString(PyExc_OverflowError, |
| 237 "unsigned short is greater than maximum"); | 237 "unsigned short is greater than maximum"); |
| 238 return -1; | 238 return -1; |
| 239 } | 239 } |
| 240 if (i >= 0) | 240 if (i >= 0) |
| 241 ((short *)ap->ob_item)[i] = (short)x; | 241 ((short *)ap->ob_item)[i] = (short)x; |
| 242 return 0; | 242 return 0; |
| 243 } | 243 } |
| 244 | 244 |
| 245 static PyObject * | 245 static PyObject * |
| 246 i_getitem(arrayobject *ap, Py_ssize_t i) | 246 i_getitem(arrayobject *ap, Py_ssize_t i) |
| 247 { | 247 { |
| 248 return PyLong_FromLong((long) ((int *)ap->ob_item)[i]); | 248 return PyLong_FromLong((long) ((int *)ap->ob_item)[i]); |
| 249 } | 249 } |
| 250 | 250 |
| 251 static int | 251 static int |
| 252 i_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) | 252 i_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
| 253 { | 253 { |
| 254 int x; | 254 int x; |
| 255 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */ | 255 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */ |
| 256 if (!PyArg_Parse(v, "i;array item must be integer", &x)) | 256 if (!PyArg_Parse(v, "i;array item must be integer", &x)) |
| 257 return -1; | 257 return -1; |
| 258 if (i >= 0) | 258 if (i >= 0) |
| 259 ((int *)ap->ob_item)[i] = x; | 259 ((int *)ap->ob_item)[i] = x; |
| 260 return 0; | 260 return 0; |
| 261 } | 261 } |
| 262 | 262 |
| 263 static PyObject * | 263 static PyObject * |
| 264 II_getitem(arrayobject *ap, Py_ssize_t i) | 264 II_getitem(arrayobject *ap, Py_ssize_t i) |
| 265 { | 265 { |
| 266 return PyLong_FromUnsignedLong( | 266 return PyLong_FromUnsignedLong( |
| 267 (unsigned long) ((unsigned int *)ap->ob_item)[i]); | 267 (unsigned long) ((unsigned int *)ap->ob_item)[i]); |
| 268 } | 268 } |
| 269 | 269 |
| 270 static int | 270 static int |
| 271 II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) | 271 II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
| 272 { | 272 { |
| 273 unsigned long x; | 273 unsigned long x; |
| 274 if (PyLong_Check(v)) { | 274 if (PyLong_Check(v)) { |
| 275 x = PyLong_AsUnsignedLong(v); | 275 x = PyLong_AsUnsignedLong(v); |
| 276 if (x == (unsigned long) -1 && PyErr_Occurred()) | 276 if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 277 return -1; | 277 return -1; |
| 278 } | 278 } |
| 279 else { | 279 else { |
| 280 long y; | 280 long y; |
| 281 if (!PyArg_Parse(v, "l;array item must be integer", &y)) | 281 if (!PyArg_Parse(v, "l;array item must be integer", &y)) |
| 282 return -1; | 282 return -1; |
| 283 if (y < 0) { | 283 if (y < 0) { |
| 284 PyErr_SetString(PyExc_OverflowError, | 284 PyErr_SetString(PyExc_OverflowError, |
| 285 "unsigned int is less than minimum"); | 285 "unsigned int is less than minimum"); |
| 286 return -1; | 286 return -1; |
| 287 } | 287 } |
| 288 x = (unsigned long)y; | 288 x = (unsigned long)y; |
| 289 | 289 |
| 290 } | 290 } |
| 291 if (x > UINT_MAX) { | 291 if (x > UINT_MAX) { |
| 292 PyErr_SetString(PyExc_OverflowError, | 292 PyErr_SetString(PyExc_OverflowError, |
| 293 "unsigned int is greater than maximum"); | 293 "unsigned int is greater than maximum"); |
| 294 return -1; | 294 return -1; |
| 295 } | 295 } |
| 296 | 296 |
| 297 if (i >= 0) | 297 if (i >= 0) |
| 298 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x; | 298 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x; |
| 299 return 0; | 299 return 0; |
| 300 } | 300 } |
| 301 | 301 |
| 302 static PyObject * | 302 static PyObject * |
| 303 l_getitem(arrayobject *ap, Py_ssize_t i) | 303 l_getitem(arrayobject *ap, Py_ssize_t i) |
| 304 { | 304 { |
| 305 return PyLong_FromLong(((long *)ap->ob_item)[i]); | 305 return PyLong_FromLong(((long *)ap->ob_item)[i]); |
| 306 } | 306 } |
| 307 | 307 |
| 308 static int | 308 static int |
| 309 l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) | 309 l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
| 310 { | 310 { |
| 311 long x; | 311 long x; |
| 312 if (!PyArg_Parse(v, "l;array item must be integer", &x)) | 312 if (!PyArg_Parse(v, "l;array item must be integer", &x)) |
| 313 return -1; | 313 return -1; |
| 314 if (i >= 0) | 314 if (i >= 0) |
| 315 ((long *)ap->ob_item)[i] = x; | 315 ((long *)ap->ob_item)[i] = x; |
| 316 return 0; | 316 return 0; |
| 317 } | 317 } |
| 318 | 318 |
| 319 static PyObject * | 319 static PyObject * |
| 320 LL_getitem(arrayobject *ap, Py_ssize_t i) | 320 LL_getitem(arrayobject *ap, Py_ssize_t i) |
| 321 { | 321 { |
| 322 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]); | 322 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]); |
| 323 } | 323 } |
| 324 | 324 |
| 325 static int | 325 static int |
| 326 LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) | 326 LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
| 327 { | 327 { |
| 328 unsigned long x; | 328 unsigned long x; |
| 329 if (PyLong_Check(v)) { | 329 if (PyLong_Check(v)) { |
| 330 x = PyLong_AsUnsignedLong(v); | 330 x = PyLong_AsUnsignedLong(v); |
| 331 if (x == (unsigned long) -1 && PyErr_Occurred()) | 331 if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 332 return -1; | 332 return -1; |
| 333 } | 333 } |
| 334 else { | 334 else { |
| 335 long y; | 335 long y; |
| 336 if (!PyArg_Parse(v, "l;array item must be integer", &y)) | 336 if (!PyArg_Parse(v, "l;array item must be integer", &y)) |
| 337 return -1; | 337 return -1; |
| 338 if (y < 0) { | 338 if (y < 0) { |
| 339 PyErr_SetString(PyExc_OverflowError, | 339 PyErr_SetString(PyExc_OverflowError, |
| 340 "unsigned long is less than minimum"); | 340 "unsigned long is less than minimum"); |
| 341 return -1; | 341 return -1; |
| 342 } | 342 } |
| 343 x = (unsigned long)y; | 343 x = (unsigned long)y; |
| 344 | 344 |
| 345 } | 345 } |
| 346 if (x > ULONG_MAX) { | 346 if (x > ULONG_MAX) { |
| 347 PyErr_SetString(PyExc_OverflowError, | 347 PyErr_SetString(PyExc_OverflowError, |
| 348 "unsigned long is greater than maximum"); | 348 "unsigned long is greater than maximum"); |
| 349 return -1; | 349 return -1; |
| 350 } | 350 } |
| 351 | 351 |
| 352 if (i >= 0) | 352 if (i >= 0) |
| 353 ((unsigned long *)ap->ob_item)[i] = x; | 353 ((unsigned long *)ap->ob_item)[i] = x; |
| 354 return 0; | 354 return 0; |
| 355 } | 355 } |
| 356 | 356 |
| 357 static PyObject * | 357 static PyObject * |
| 358 f_getitem(arrayobject *ap, Py_ssize_t i) | 358 f_getitem(arrayobject *ap, Py_ssize_t i) |
| 359 { | 359 { |
| 360 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]); | 360 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]); |
| 361 } | 361 } |
| 362 | 362 |
| 363 static int | 363 static int |
| 364 f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) | 364 f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
| 365 { | 365 { |
| 366 float x; | 366 float x; |
| 367 if (!PyArg_Parse(v, "f;array item must be float", &x)) | 367 if (!PyArg_Parse(v, "f;array item must be float", &x)) |
| 368 return -1; | 368 return -1; |
| 369 if (i >= 0) | 369 if (i >= 0) |
| 370 ((float *)ap->ob_item)[i] = x; | 370 ((float *)ap->ob_item)[i] = x; |
| 371 return 0; | 371 return 0; |
| 372 } | 372 } |
| 373 | 373 |
| 374 static PyObject * | 374 static PyObject * |
| 375 d_getitem(arrayobject *ap, Py_ssize_t i) | 375 d_getitem(arrayobject *ap, Py_ssize_t i) |
| 376 { | 376 { |
| 377 return PyFloat_FromDouble(((double *)ap->ob_item)[i]); | 377 return PyFloat_FromDouble(((double *)ap->ob_item)[i]); |
| 378 } | 378 } |
| 379 | 379 |
| 380 static int | 380 static int |
| 381 d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) | 381 d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
| 382 { | 382 { |
| 383 double x; | 383 double x; |
| 384 if (!PyArg_Parse(v, "d;array item must be float", &x)) | 384 if (!PyArg_Parse(v, "d;array item must be float", &x)) |
| 385 return -1; | 385 return -1; |
| 386 if (i >= 0) | 386 if (i >= 0) |
| 387 ((double *)ap->ob_item)[i] = x; | 387 ((double *)ap->ob_item)[i] = x; |
| 388 return 0; | 388 return 0; |
| 389 } | 389 } |
| 390 | 390 |
| 391 | 391 |
| 392 /* Description of types */ | 392 /* Description of types */ |
| 393 static struct arraydescr descriptors[] = { | 393 static struct arraydescr descriptors[] = { |
| 394 {'b', 1, b_getitem, b_setitem, "b"}, | 394 {'b', 1, b_getitem, b_setitem, "b"}, |
| 395 {'B', 1, BB_getitem, BB_setitem, "B"}, | 395 {'B', 1, BB_getitem, BB_setitem, "B"}, |
| 396 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem, "u"}, | 396 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem, "u"}, |
| 397 {'h', sizeof(short), h_getitem, h_setitem, "h"}, | 397 {'h', sizeof(short), h_getitem, h_setitem, "h"}, |
| 398 {'H', sizeof(short), HH_getitem, HH_setitem, "H"}, | 398 {'H', sizeof(short), HH_getitem, HH_setitem, "H"}, |
| 399 {'i', sizeof(int), i_getitem, i_setitem, "i"}, | 399 {'i', sizeof(int), i_getitem, i_setitem, "i"}, |
| 400 {'I', sizeof(int), II_getitem, II_setitem, "I"}, | 400 {'I', sizeof(int), II_getitem, II_setitem, "I"}, |
| 401 {'l', sizeof(long), l_getitem, l_setitem, "l"}, | 401 {'l', sizeof(long), l_getitem, l_setitem, "l"}, |
| 402 {'L', sizeof(long), LL_getitem, LL_setitem, "L"}, | 402 {'L', sizeof(long), LL_getitem, LL_setitem, "L"}, |
| 403 {'f', sizeof(float), f_getitem, f_setitem, "f"}, | 403 {'f', sizeof(float), f_getitem, f_setitem, "f"}, |
| 404 {'d', sizeof(double), d_getitem, d_setitem, "d"}, | 404 {'d', sizeof(double), d_getitem, d_setitem, "d"}, |
| 405 {'\0', 0, 0, 0, 0} /* Sentinel */ | 405 {'\0', 0, 0, 0, 0} /* Sentinel */ |
| 406 }; | 406 }; |
| 407 | 407 |
| 408 /**************************************************************************** | 408 /**************************************************************************** |
| 409 Implementations of array object methods. | 409 Implementations of array object methods. |
| 410 ****************************************************************************/ | 410 ****************************************************************************/ |
| 411 | 411 |
| 412 static PyObject * | 412 static PyObject * |
| 413 newarrayobject(PyTypeObject *type, Py_ssize_t size, struct arraydescr *descr) | 413 newarrayobject(PyTypeObject *type, Py_ssize_t size, struct arraydescr *descr) |
| 414 { | 414 { |
| 415 arrayobject *op; | 415 arrayobject *op; |
| 416 size_t nbytes; | 416 size_t nbytes; |
| 417 | 417 |
| 418 if (size < 0) { | 418 if (size < 0) { |
| 419 PyErr_BadInternalCall(); | 419 PyErr_BadInternalCall(); |
| 420 return NULL; | 420 return NULL; |
| 421 } | 421 } |
| 422 | 422 |
| 423 nbytes = size * descr->itemsize; | 423 nbytes = size * descr->itemsize; |
| 424 /* Check for overflow */ | 424 /* Check for overflow */ |
| 425 if (nbytes / descr->itemsize != (size_t)size) { | 425 if (nbytes / descr->itemsize != (size_t)size) { |
| 426 return PyErr_NoMemory(); | 426 return PyErr_NoMemory(); |
| 427 } | 427 } |
| 428 op = (arrayobject *) type->tp_alloc(type, 0); | 428 op = (arrayobject *) type->tp_alloc(type, 0); |
| 429 if (op == NULL) { | 429 if (op == NULL) { |
| 430 return NULL; | 430 return NULL; |
| 431 } | 431 } |
| 432 op->ob_descr = descr; | 432 op->ob_descr = descr; |
| 433 op->allocated = size; | 433 op->allocated = size; |
| 434 op->weakreflist = NULL; | 434 op->weakreflist = NULL; |
| 435 Py_SIZE(op) = size; | 435 Py_SIZE(op) = size; |
| 436 if (size <= 0) { | 436 if (size <= 0) { |
| 437 op->ob_item = NULL; | 437 op->ob_item = NULL; |
| 438 } | 438 } |
| 439 else { | 439 else { |
| 440 op->ob_item = PyMem_NEW(char, nbytes); | 440 op->ob_item = PyMem_NEW(char, nbytes); |
| 441 if (op->ob_item == NULL) { | 441 if (op->ob_item == NULL) { |
| 442 Py_DECREF(op); | 442 Py_DECREF(op); |
| 443 return PyErr_NoMemory(); | 443 return PyErr_NoMemory(); |
| 444 } | 444 } |
| 445 } | 445 } |
| 446 op->ob_exports = 0; | 446 op->ob_exports = 0; |
| 447 return (PyObject *) op; | 447 return (PyObject *) op; |
| 448 } | 448 } |
| 449 | 449 |
| 450 static PyObject * | 450 static PyObject * |
| 451 getarrayitem(PyObject *op, Py_ssize_t i) | 451 getarrayitem(PyObject *op, Py_ssize_t i) |
| 452 { | 452 { |
| 453 register arrayobject *ap; | 453 register arrayobject *ap; |
| 454 assert(array_Check(op)); | 454 assert(array_Check(op)); |
| 455 ap = (arrayobject *)op; | 455 ap = (arrayobject *)op; |
| 456 assert(i>=0 && i<Py_SIZE(ap)); | 456 assert(i>=0 && i<Py_SIZE(ap)); |
| 457 return (*ap->ob_descr->getitem)(ap, i); | 457 return (*ap->ob_descr->getitem)(ap, i); |
| 458 } | 458 } |
| 459 | 459 |
| 460 static int | 460 static int |
| 461 ins1(arrayobject *self, Py_ssize_t where, PyObject *v) | 461 ins1(arrayobject *self, Py_ssize_t where, PyObject *v) |
| 462 { | 462 { |
| 463 char *items; | 463 char *items; |
| 464 Py_ssize_t n = Py_SIZE(self); | 464 Py_ssize_t n = Py_SIZE(self); |
| 465 if (v == NULL) { | 465 if (v == NULL) { |
| 466 PyErr_BadInternalCall(); | 466 PyErr_BadInternalCall(); |
| 467 return -1; | 467 return -1; |
| 468 } | 468 } |
| 469 if ((*self->ob_descr->setitem)(self, -1, v) < 0) | 469 if ((*self->ob_descr->setitem)(self, -1, v) < 0) |
| 470 return -1; | 470 return -1; |
| 471 | 471 |
| 472 if (array_resize(self, n+1) == -1) | 472 if (array_resize(self, n+1) == -1) |
| 473 return -1; | 473 return -1; |
| 474 items = self->ob_item; | 474 items = self->ob_item; |
| 475 if (where < 0) { | 475 if (where < 0) { |
| 476 where += n; | 476 where += n; |
| 477 if (where < 0) | 477 if (where < 0) |
| 478 where = 0; | 478 where = 0; |
| 479 } | 479 } |
| 480 if (where > n) | 480 if (where > n) |
| 481 where = n; | 481 where = n; |
| 482 /* appends don't need to call memmove() */ | 482 /* appends don't need to call memmove() */ |
| 483 if (where != n) | 483 if (where != n) |
| 484 memmove(items + (where+1)*self->ob_descr->itemsize, | 484 memmove(items + (where+1)*self->ob_descr->itemsize, |
| 485 items + where*self->ob_descr->itemsize, | 485 items + where*self->ob_descr->itemsize, |
| 486 (n-where)*self->ob_descr->itemsize); | 486 (n-where)*self->ob_descr->itemsize); |
| 487 return (*self->ob_descr->setitem)(self, where, v); | 487 return (*self->ob_descr->setitem)(self, where, v); |
| 488 } | 488 } |
| 489 | 489 |
| 490 /* Methods */ | 490 /* Methods */ |
| 491 | 491 |
| 492 static void | 492 static void |
| 493 array_dealloc(arrayobject *op) | 493 array_dealloc(arrayobject *op) |
| 494 { | 494 { |
| 495 if (op->weakreflist != NULL) | 495 if (op->weakreflist != NULL) |
| 496 PyObject_ClearWeakRefs((PyObject *) op); | 496 PyObject_ClearWeakRefs((PyObject *) op); |
| 497 if (op->ob_item != NULL) | 497 if (op->ob_item != NULL) |
| 498 PyMem_DEL(op->ob_item); | 498 PyMem_DEL(op->ob_item); |
| 499 Py_TYPE(op)->tp_free((PyObject *)op); | 499 Py_TYPE(op)->tp_free((PyObject *)op); |
| 500 } | 500 } |
| 501 | 501 |
| 502 static PyObject * | 502 static PyObject * |
| 503 array_richcompare(PyObject *v, PyObject *w, int op) | 503 array_richcompare(PyObject *v, PyObject *w, int op) |
| 504 { | 504 { |
| 505 arrayobject *va, *wa; | 505 arrayobject *va, *wa; |
| 506 PyObject *vi = NULL; | 506 PyObject *vi = NULL; |
| 507 PyObject *wi = NULL; | 507 PyObject *wi = NULL; |
| 508 Py_ssize_t i, k; | 508 Py_ssize_t i, k; |
| 509 PyObject *res; | 509 PyObject *res; |
| 510 | 510 |
| 511 if (!array_Check(v) || !array_Check(w)) { | 511 if (!array_Check(v) || !array_Check(w)) { |
| 512 Py_INCREF(Py_NotImplemented); | 512 Py_INCREF(Py_NotImplemented); |
| 513 return Py_NotImplemented; | 513 return Py_NotImplemented; |
| 514 } | 514 } |
| 515 | 515 |
| 516 va = (arrayobject *)v; | 516 va = (arrayobject *)v; |
| 517 wa = (arrayobject *)w; | 517 wa = (arrayobject *)w; |
| 518 | 518 |
| 519 if (Py_SIZE(va) != Py_SIZE(wa) && (op == Py_EQ || op == Py_NE)) { | 519 if (Py_SIZE(va) != Py_SIZE(wa) && (op == Py_EQ || op == Py_NE)) { |
| 520 /* Shortcut: if the lengths differ, the arrays differ */ | 520 /* Shortcut: if the lengths differ, the arrays differ */ |
| 521 if (op == Py_EQ) | 521 if (op == Py_EQ) |
| 522 res = Py_False; | 522 res = Py_False; |
| 523 else | 523 else |
| 524 res = Py_True; | 524 res = Py_True; |
| 525 Py_INCREF(res); | 525 Py_INCREF(res); |
| 526 return res; | 526 return res; |
| 527 } | 527 } |
| 528 | 528 |
| 529 /* Search for the first index where items are different */ | 529 /* Search for the first index where items are different */ |
| 530 k = 1; | 530 k = 1; |
| 531 for (i = 0; i < Py_SIZE(va) && i < Py_SIZE(wa); i++) { | 531 for (i = 0; i < Py_SIZE(va) && i < Py_SIZE(wa); i++) { |
| 532 vi = getarrayitem(v, i); | 532 vi = getarrayitem(v, i); |
| 533 wi = getarrayitem(w, i); | 533 wi = getarrayitem(w, i); |
| 534 if (vi == NULL || wi == NULL) { | 534 if (vi == NULL || wi == NULL) { |
| 535 Py_XDECREF(vi); | 535 Py_XDECREF(vi); |
| 536 Py_XDECREF(wi); | 536 Py_XDECREF(wi); |
| 537 return NULL; | 537 return NULL; |
| 538 } | 538 } |
| 539 k = PyObject_RichCompareBool(vi, wi, Py_EQ); | 539 k = PyObject_RichCompareBool(vi, wi, Py_EQ); |
| 540 if (k == 0) | 540 if (k == 0) |
| 541 break; /* Keeping vi and wi alive! */ | 541 break; /* Keeping vi and wi alive! */ |
| 542 Py_DECREF(vi); | 542 Py_DECREF(vi); |
| 543 Py_DECREF(wi); | 543 Py_DECREF(wi); |
| 544 if (k < 0) | 544 if (k < 0) |
| 545 return NULL; | 545 return NULL; |
| 546 } | 546 } |
| 547 | 547 |
| 548 if (k) { | 548 if (k) { |
| 549 /* No more items to compare -- compare sizes */ | 549 /* No more items to compare -- compare sizes */ |
| 550 Py_ssize_t vs = Py_SIZE(va); | 550 Py_ssize_t vs = Py_SIZE(va); |
| 551 Py_ssize_t ws = Py_SIZE(wa); | 551 Py_ssize_t ws = Py_SIZE(wa); |
| 552 int cmp; | 552 int cmp; |
| 553 switch (op) { | 553 switch (op) { |
| 554 case Py_LT: cmp = vs < ws; break; | 554 case Py_LT: cmp = vs < ws; break; |
| 555 case Py_LE: cmp = vs <= ws; break; | 555 case Py_LE: cmp = vs <= ws; break; |
| 556 case Py_EQ: cmp = vs == ws; break; | 556 case Py_EQ: cmp = vs == ws; break; |
| 557 case Py_NE: cmp = vs != ws; break; | 557 case Py_NE: cmp = vs != ws; break; |
| 558 case Py_GT: cmp = vs > ws; break; | 558 case Py_GT: cmp = vs > ws; break; |
| 559 case Py_GE: cmp = vs >= ws; break; | 559 case Py_GE: cmp = vs >= ws; break; |
| 560 default: return NULL; /* cannot happen */ | 560 default: return NULL; /* cannot happen */ |
| 561 } | 561 } |
| 562 if (cmp) | 562 if (cmp) |
| 563 res = Py_True; | 563 res = Py_True; |
| 564 else | 564 else |
| 565 res = Py_False; | 565 res = Py_False; |
| 566 Py_INCREF(res); | 566 Py_INCREF(res); |
| 567 return res; | 567 return res; |
| 568 } | 568 } |
| 569 | 569 |
| 570 /* We have an item that differs. First, shortcuts for EQ/NE */ | 570 /* We have an item that differs. First, shortcuts for EQ/NE */ |
| 571 if (op == Py_EQ) { | 571 if (op == Py_EQ) { |
| 572 Py_INCREF(Py_False); | 572 Py_INCREF(Py_False); |
| 573 res = Py_False; | 573 res = Py_False; |
| 574 } | 574 } |
| 575 else if (op == Py_NE) { | 575 else if (op == Py_NE) { |
| 576 Py_INCREF(Py_True); | 576 Py_INCREF(Py_True); |
| 577 res = Py_True; | 577 res = Py_True; |
| 578 } | 578 } |
| 579 else { | 579 else { |
| 580 /* Compare the final item again using the proper operator */ | 580 /* Compare the final item again using the proper operator */ |
| 581 res = PyObject_RichCompare(vi, wi, op); | 581 res = PyObject_RichCompare(vi, wi, op); |
| 582 } | 582 } |
| 583 Py_DECREF(vi); | 583 Py_DECREF(vi); |
| 584 Py_DECREF(wi); | 584 Py_DECREF(wi); |
| 585 return res; | 585 return res; |
| 586 } | 586 } |
| 587 | 587 |
| 588 static Py_ssize_t | 588 static Py_ssize_t |
| 589 array_length(arrayobject *a) | 589 array_length(arrayobject *a) |
| 590 { | 590 { |
| 591 return Py_SIZE(a); | 591 return Py_SIZE(a); |
| 592 } | 592 } |
| 593 | 593 |
| 594 static PyObject * | 594 static PyObject * |
| 595 array_item(arrayobject *a, Py_ssize_t i) | 595 array_item(arrayobject *a, Py_ssize_t i) |
| 596 { | 596 { |
| 597 if (i < 0 || i >= Py_SIZE(a)) { | 597 if (i < 0 || i >= Py_SIZE(a)) { |
| 598 PyErr_SetString(PyExc_IndexError, "array index out of range"); | 598 PyErr_SetString(PyExc_IndexError, "array index out of range"); |
| 599 return NULL; | 599 return NULL; |
| 600 } | 600 } |
| 601 return getarrayitem((PyObject *)a, i); | 601 return getarrayitem((PyObject *)a, i); |
| 602 } | 602 } |
| 603 | 603 |
| 604 static PyObject * | 604 static PyObject * |
| 605 array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh) | 605 array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh) |
| 606 { | 606 { |
| 607 arrayobject *np; | 607 arrayobject *np; |
| 608 if (ilow < 0) | 608 if (ilow < 0) |
| 609 ilow = 0; | 609 ilow = 0; |
| 610 else if (ilow > Py_SIZE(a)) | 610 else if (ilow > Py_SIZE(a)) |
| 611 ilow = Py_SIZE(a); | 611 ilow = Py_SIZE(a); |
| 612 if (ihigh < 0) | 612 if (ihigh < 0) |
| 613 ihigh = 0; | 613 ihigh = 0; |
| 614 if (ihigh < ilow) | 614 if (ihigh < ilow) |
| 615 ihigh = ilow; | 615 ihigh = ilow; |
| 616 else if (ihigh > Py_SIZE(a)) | 616 else if (ihigh > Py_SIZE(a)) |
| 617 ihigh = Py_SIZE(a); | 617 ihigh = Py_SIZE(a); |
| 618 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr); | 618 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr); |
| 619 if (np == NULL) | 619 if (np == NULL) |
| 620 return NULL; | 620 return NULL; |
| 621 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize, | 621 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize, |
| 622 (ihigh-ilow) * a->ob_descr->itemsize); | 622 (ihigh-ilow) * a->ob_descr->itemsize); |
| 623 return (PyObject *)np; | 623 return (PyObject *)np; |
| 624 } | 624 } |
| 625 | 625 |
| 626 static PyObject * | 626 static PyObject * |
| 627 array_copy(arrayobject *a, PyObject *unused) | 627 array_copy(arrayobject *a, PyObject *unused) |
| 628 { | 628 { |
| 629 return array_slice(a, 0, Py_SIZE(a)); | 629 return array_slice(a, 0, Py_SIZE(a)); |
| 630 } | 630 } |
| 631 | 631 |
| 632 PyDoc_STRVAR(copy_doc, | 632 PyDoc_STRVAR(copy_doc, |
| 633 "copy(array)\n\ | 633 "copy(array)\n\ |
| 634 \n\ | 634 \n\ |
| 635 Return a copy of the array."); | 635 Return a copy of the array."); |
| 636 | 636 |
| 637 static PyObject * | 637 static PyObject * |
| 638 array_concat(arrayobject *a, PyObject *bb) | 638 array_concat(arrayobject *a, PyObject *bb) |
| 639 { | 639 { |
| 640 Py_ssize_t size; | 640 Py_ssize_t size; |
| 641 arrayobject *np; | 641 arrayobject *np; |
| 642 if (!array_Check(bb)) { | 642 if (!array_Check(bb)) { |
| 643 PyErr_Format(PyExc_TypeError, | 643 PyErr_Format(PyExc_TypeError, |
| 644 "can only append array (not \"%.200s\") to array", | 644 "can only append array (not \"%.200s\") to array", |
| 645 Py_TYPE(bb)->tp_name); | 645 Py_TYPE(bb)->tp_name); |
| 646 return NULL; | 646 return NULL; |
| 647 } | 647 } |
| 648 #define b ((arrayobject *)bb) | 648 #define b ((arrayobject *)bb) |
| 649 if (a->ob_descr != b->ob_descr) { | 649 if (a->ob_descr != b->ob_descr) { |
| 650 PyErr_BadArgument(); | 650 PyErr_BadArgument(); |
| 651 return NULL; | 651 return NULL; |
| 652 } | 652 } |
| 653 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) { | 653 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) { |
| 654 return PyErr_NoMemory(); | 654 return PyErr_NoMemory(); |
| 655 } | 655 } |
| 656 size = Py_SIZE(a) + Py_SIZE(b); | 656 size = Py_SIZE(a) + Py_SIZE(b); |
| 657 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr); | 657 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr); |
| 658 if (np == NULL) { | 658 if (np == NULL) { |
| 659 return NULL; | 659 return NULL; |
| 660 } | 660 } |
| 661 memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize); | 661 memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize); |
| 662 memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize, | 662 memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize, |
| 663 b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize); | 663 b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize); |
| 664 return (PyObject *)np; | 664 return (PyObject *)np; |
| 665 #undef b | 665 #undef b |
| 666 } | 666 } |
| 667 | 667 |
| 668 static PyObject * | 668 static PyObject * |
| 669 array_repeat(arrayobject *a, Py_ssize_t n) | 669 array_repeat(arrayobject *a, Py_ssize_t n) |
| 670 { | 670 { |
| 671 Py_ssize_t i; | 671 Py_ssize_t i; |
| 672 Py_ssize_t size; | 672 Py_ssize_t size; |
| 673 arrayobject *np; | 673 arrayobject *np; |
| 674 char *p; | 674 char *p; |
| 675 Py_ssize_t nbytes; | 675 Py_ssize_t nbytes; |
| 676 if (n < 0) | 676 if (n < 0) |
| 677 n = 0; | 677 n = 0; |
| 678 if ((Py_SIZE(a) != 0) && (n > PY_SSIZE_T_MAX / Py_SIZE(a))) { | 678 if ((Py_SIZE(a) != 0) && (n > PY_SSIZE_T_MAX / Py_SIZE(a))) { |
| 679 return PyErr_NoMemory(); | 679 return PyErr_NoMemory(); |
| 680 } | 680 } |
| 681 size = Py_SIZE(a) * n; | 681 size = Py_SIZE(a) * n; |
| 682 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr); | 682 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr); |
| 683 if (np == NULL) | 683 if (np == NULL) |
| 684 return NULL; | 684 return NULL; |
| 685 p = np->ob_item; | 685 p = np->ob_item; |
| 686 nbytes = Py_SIZE(a) * a->ob_descr->itemsize; | 686 nbytes = Py_SIZE(a) * a->ob_descr->itemsize; |
| 687 for (i = 0; i < n; i++) { | 687 for (i = 0; i < n; i++) { |
| 688 memcpy(p, a->ob_item, nbytes); | 688 memcpy(p, a->ob_item, nbytes); |
| 689 p += nbytes; | 689 p += nbytes; |
| 690 } | 690 } |
| 691 return (PyObject *) np; | 691 return (PyObject *) np; |
| 692 } | 692 } |
| 693 | 693 |
| 694 static int | 694 static int |
| 695 array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) | 695 array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) |
| 696 { | 696 { |
| 697 char *item; | 697 char *item; |
| 698 Py_ssize_t n; /* Size of replacement array */ | 698 Py_ssize_t n; /* Size of replacement array */ |
| 699 Py_ssize_t d; /* Change in size */ | 699 Py_ssize_t d; /* Change in size */ |
| 700 #define b ((arrayobject *)v) | 700 #define b ((arrayobject *)v) |
| 701 if (v == NULL) | 701 if (v == NULL) |
| 702 n = 0; | 702 n = 0; |
| 703 else if (array_Check(v)) { | 703 else if (array_Check(v)) { |
| 704 n = Py_SIZE(b); | 704 n = Py_SIZE(b); |
| 705 if (a == b) { | 705 if (a == b) { |
| 706 /* Special case "a[i:j] = a" -- copy b first */ | 706 /* Special case "a[i:j] = a" -- copy b first */ |
| 707 int ret; | 707 int ret; |
| 708 v = array_slice(b, 0, n); | 708 v = array_slice(b, 0, n); |
| 709 if (!v) | 709 if (!v) |
| 710 return -1; | 710 return -1; |
| 711 ret = array_ass_slice(a, ilow, ihigh, v); | 711 ret = array_ass_slice(a, ilow, ihigh, v); |
| 712 Py_DECREF(v); | 712 Py_DECREF(v); |
| 713 return ret; | 713 return ret; |
| 714 } | 714 } |
| 715 if (b->ob_descr != a->ob_descr) { | 715 if (b->ob_descr != a->ob_descr) { |
| 716 PyErr_BadArgument(); | 716 PyErr_BadArgument(); |
| 717 return -1; | 717 return -1; |
| 718 } | 718 } |
| 719 } | 719 } |
| 720 else { | 720 else { |
| 721 PyErr_Format(PyExc_TypeError, | 721 PyErr_Format(PyExc_TypeError, |
| 722 "can only assign array (not \"%.200s\") to array slice", | 722 "can only assign array (not \"%.200s\") to array slice", |
| 723 Py_TYPE(v)->tp_name); | 723 Py_TYPE(v)->tp_name); |
| 724 return -1; | 724 return -1; |
| 725 } | 725 } |
| 726 if (ilow < 0) | 726 if (ilow < 0) |
| 727 ilow = 0; | 727 ilow = 0; |
| 728 else if (ilow > Py_SIZE(a)) | 728 else if (ilow > Py_SIZE(a)) |
| 729 ilow = Py_SIZE(a); | 729 ilow = Py_SIZE(a); |
| 730 if (ihigh < 0) | 730 if (ihigh < 0) |
| 731 ihigh = 0; | 731 ihigh = 0; |
| 732 if (ihigh < ilow) | 732 if (ihigh < ilow) |
| 733 ihigh = ilow; | 733 ihigh = ilow; |
| 734 else if (ihigh > Py_SIZE(a)) | 734 else if (ihigh > Py_SIZE(a)) |
| 735 ihigh = Py_SIZE(a); | 735 ihigh = Py_SIZE(a); |
| 736 item = a->ob_item; | 736 item = a->ob_item; |
| 737 d = n - (ihigh-ilow); | 737 d = n - (ihigh-ilow); |
| 738 /* Issue #4509: If the array has exported buffers and the slice | 738 /* Issue #4509: If the array has exported buffers and the slice |
| 739 assignment would change the size of the array, fail early to make | 739 assignment would change the size of the array, fail early to make |
| 740 sure we don't modify it. */ | 740 sure we don't modify it. */ |
| 741 if (d != 0 && a->ob_exports > 0) { | 741 if (d != 0 && a->ob_exports > 0) { |
| 742 PyErr_SetString(PyExc_BufferError, | 742 PyErr_SetString(PyExc_BufferError, |
| 743 "cannot resize an array that is exporting buffers"); | 743 "cannot resize an array that is exporting buffers"); |
| 744 return -1; | 744 return -1; |
| 745 } | 745 } |
| 746 if (d < 0) { /* Delete -d items */ | 746 if (d < 0) { /* Delete -d items */ |
| 747 memmove(item + (ihigh+d)*a->ob_descr->itemsize, | 747 memmove(item + (ihigh+d)*a->ob_descr->itemsize, |
| 748 item + ihigh*a->ob_descr->itemsize, | 748 item + ihigh*a->ob_descr->itemsize, |
| 749 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize); | 749 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize); |
| 750 if (array_resize(a, Py_SIZE(a) + d) == -1) | 750 if (array_resize(a, Py_SIZE(a) + d) == -1) |
| 751 return -1; | 751 return -1; |
| 752 } | 752 } |
| 753 else if (d > 0) { /* Insert d items */ | 753 else if (d > 0) { /* Insert d items */ |
| 754 if (array_resize(a, Py_SIZE(a) + d)) | 754 if (array_resize(a, Py_SIZE(a) + d)) |
| 755 return -1; | 755 return -1; |
| 756 memmove(item + (ihigh+d)*a->ob_descr->itemsize, | 756 memmove(item + (ihigh+d)*a->ob_descr->itemsize, |
| 757 item + ihigh*a->ob_descr->itemsize, | 757 item + ihigh*a->ob_descr->itemsize, |
| 758 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize); | 758 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize); |
| 759 } | 759 } |
| 760 if (n > 0) | 760 if (n > 0) |
| 761 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item, | 761 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item, |
| 762 n*b->ob_descr->itemsize); | 762 n*b->ob_descr->itemsize); |
| 763 return 0; | 763 return 0; |
| 764 #undef b | 764 #undef b |
| 765 } | 765 } |
| 766 | 766 |
| 767 static int | 767 static int |
| 768 array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v) | 768 array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v) |
| 769 { | 769 { |
| 770 if (i < 0 || i >= Py_SIZE(a)) { | 770 if (i < 0 || i >= Py_SIZE(a)) { |
| 771 PyErr_SetString(PyExc_IndexError, | 771 PyErr_SetString(PyExc_IndexError, |
| 772 "array assignment index out of range"); | 772 "array assignment index out of range"); |
| 773 return -1; | 773 return -1; |
| 774 } | 774 } |
| 775 if (v == NULL) | 775 if (v == NULL) |
| 776 return array_ass_slice(a, i, i+1, v); | 776 return array_ass_slice(a, i, i+1, v); |
| 777 return (*a->ob_descr->setitem)(a, i, v); | 777 return (*a->ob_descr->setitem)(a, i, v); |
| 778 } | 778 } |
| 779 | 779 |
| 780 static int | 780 static int |
| 781 setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v) | 781 setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v) |
| 782 { | 782 { |
| 783 assert(array_Check(a)); | 783 assert(array_Check(a)); |
| 784 return array_ass_item((arrayobject *)a, i, v); | 784 return array_ass_item((arrayobject *)a, i, v); |
| 785 } | 785 } |
| 786 | 786 |
| 787 static int | 787 static int |
| 788 array_iter_extend(arrayobject *self, PyObject *bb) | 788 array_iter_extend(arrayobject *self, PyObject *bb) |
| 789 { | 789 { |
| 790 PyObject *it, *v; | 790 PyObject *it, *v; |
| 791 | 791 |
| 792 it = PyObject_GetIter(bb); | 792 it = PyObject_GetIter(bb); |
| 793 if (it == NULL) | 793 if (it == NULL) |
| 794 return -1; | 794 return -1; |
| 795 | 795 |
| 796 while ((v = PyIter_Next(it)) != NULL) { | 796 while ((v = PyIter_Next(it)) != NULL) { |
| 797 if (ins1(self, (int) Py_SIZE(self), v) != 0) { | 797 if (ins1(self, (int) Py_SIZE(self), v) != 0) { |
| 798 Py_DECREF(v); | 798 Py_DECREF(v); |
| 799 Py_DECREF(it); | 799 Py_DECREF(it); |
| 800 return -1; | 800 return -1; |
| 801 } | 801 } |
| 802 Py_DECREF(v); | 802 Py_DECREF(v); |
| 803 } | 803 } |
| 804 Py_DECREF(it); | 804 Py_DECREF(it); |
| 805 if (PyErr_Occurred()) | 805 if (PyErr_Occurred()) |
| 806 return -1; | 806 return -1; |
| 807 return 0; | 807 return 0; |
| 808 } | 808 } |
| 809 | 809 |
| 810 static int | 810 static int |
| 811 array_do_extend(arrayobject *self, PyObject *bb) | 811 array_do_extend(arrayobject *self, PyObject *bb) |
| 812 { | 812 { |
| 813 Py_ssize_t size, oldsize, bbsize; | 813 Py_ssize_t size, oldsize, bbsize; |
| 814 | 814 |
| 815 if (!array_Check(bb)) | 815 if (!array_Check(bb)) |
| 816 return array_iter_extend(self, bb); | 816 return array_iter_extend(self, bb); |
| 817 #define b ((arrayobject *)bb) | 817 #define b ((arrayobject *)bb) |
| 818 if (self->ob_descr != b->ob_descr) { | 818 if (self->ob_descr != b->ob_descr) { |
| 819 PyErr_SetString(PyExc_TypeError, | 819 PyErr_SetString(PyExc_TypeError, |
| 820 "can only extend with array of same kind"); | 820 "can only extend with array of same kind"); |
| 821 return -1; | 821 return -1; |
| 822 } | 822 } |
| 823 if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) || | 823 if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) || |
| 824 ((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) { | 824 ((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) { |
| 825 PyErr_NoMemory(); | 825 PyErr_NoMemory(); |
| 826 return -1; | 826 return -1; |
| 827 } | 827 } |
| 828 oldsize = Py_SIZE(self); | 828 oldsize = Py_SIZE(self); |
| 829 /* Get the size of bb before resizing the array since bb could be self. */ | 829 /* Get the size of bb before resizing the array since bb could be self. */ |
| 830 bbsize = Py_SIZE(bb); | 830 bbsize = Py_SIZE(bb); |
| 831 size = oldsize + Py_SIZE(b); | 831 size = oldsize + Py_SIZE(b); |
| 832 if (array_resize(self, size) == -1) | 832 if (array_resize(self, size) == -1) |
| 833 return -1; | 833 return -1; |
| 834 memcpy(self->ob_item + oldsize * self->ob_descr->itemsize, | 834 memcpy(self->ob_item + oldsize * self->ob_descr->itemsize, |
| 835 b->ob_item, bbsize * b->ob_descr->itemsize); | 835 b->ob_item, bbsize * b->ob_descr->itemsize); |
| 836 | 836 |
| 837 return 0; | 837 return 0; |
| 838 #undef b | 838 #undef b |
| 839 } | 839 } |
| 840 | 840 |
| 841 static PyObject * | 841 static PyObject * |
| 842 array_inplace_concat(arrayobject *self, PyObject *bb) | 842 array_inplace_concat(arrayobject *self, PyObject *bb) |
| 843 { | 843 { |
| 844 if (!array_Check(bb)) { | 844 if (!array_Check(bb)) { |
| 845 PyErr_Format(PyExc_TypeError, | 845 PyErr_Format(PyExc_TypeError, |
| 846 "can only extend array with array (not \"%.200s\")", | 846 "can only extend array with array (not \"%.200s\")", |
| 847 Py_TYPE(bb)->tp_name); | 847 Py_TYPE(bb)->tp_name); |
| 848 return NULL; | 848 return NULL; |
| 849 } | 849 } |
| 850 if (array_do_extend(self, bb) == -1) | 850 if (array_do_extend(self, bb) == -1) |
| 851 return NULL; | 851 return NULL; |
| 852 Py_INCREF(self); | 852 Py_INCREF(self); |
| 853 return (PyObject *)self; | 853 return (PyObject *)self; |
| 854 } | 854 } |
| 855 | 855 |
| 856 static PyObject * | 856 static PyObject * |
| 857 array_inplace_repeat(arrayobject *self, Py_ssize_t n) | 857 array_inplace_repeat(arrayobject *self, Py_ssize_t n) |
| 858 { | 858 { |
| 859 char *items, *p; | 859 char *items, *p; |
| 860 Py_ssize_t size, i; | 860 Py_ssize_t size, i; |
| 861 | 861 |
| 862 if (Py_SIZE(self) > 0) { | 862 if (Py_SIZE(self) > 0) { |
| 863 if (n < 0) | 863 if (n < 0) |
| 864 n = 0; | 864 n = 0; |
| 865 items = self->ob_item; | 865 items = self->ob_item; |
| 866 if ((self->ob_descr->itemsize != 0) && | 866 if ((self->ob_descr->itemsize != 0) && |
| 867 (Py_SIZE(self) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) { | 867 (Py_SIZE(self) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) { |
| 868 return PyErr_NoMemory(); | 868 return PyErr_NoMemory(); |
| 869 } | 869 } |
| 870 size = Py_SIZE(self) * self->ob_descr->itemsize; | 870 size = Py_SIZE(self) * self->ob_descr->itemsize; |
| 871 if (n > 0 && size > PY_SSIZE_T_MAX / n) { | 871 if (n > 0 && size > PY_SSIZE_T_MAX / n) { |
| 872 return PyErr_NoMemory(); | 872 return PyErr_NoMemory(); |
| 873 } | 873 } |
| 874 if (array_resize(self, n * Py_SIZE(self)) == -1) | 874 if (array_resize(self, n * Py_SIZE(self)) == -1) |
| 875 return NULL; | 875 return NULL; |
| 876 items = p = self->ob_item; | 876 items = p = self->ob_item; |
| 877 for (i = 1; i < n; i++) { | 877 for (i = 1; i < n; i++) { |
| 878 p += size; | 878 p += size; |
| 879 memcpy(p, items, size); | 879 memcpy(p, items, size); |
| 880 } | 880 } |
| 881 } | 881 } |
| 882 Py_INCREF(self); | 882 Py_INCREF(self); |
| 883 return (PyObject *)self; | 883 return (PyObject *)self; |
| 884 } | 884 } |
| 885 | 885 |
| 886 | 886 |
| 887 static PyObject * | 887 static PyObject * |
| 888 ins(arrayobject *self, Py_ssize_t where, PyObject *v) | 888 ins(arrayobject *self, Py_ssize_t where, PyObject *v) |
| 889 { | 889 { |
| 890 if (ins1(self, where, v) != 0) | 890 if (ins1(self, where, v) != 0) |
| 891 return NULL; | 891 return NULL; |
| 892 Py_INCREF(Py_None); | 892 Py_INCREF(Py_None); |
| 893 return Py_None; | 893 return Py_None; |
| 894 } | 894 } |
| 895 | 895 |
| 896 static PyObject * | 896 static PyObject * |
| 897 array_count(arrayobject *self, PyObject *v) | 897 array_count(arrayobject *self, PyObject *v) |
| 898 { | 898 { |
| 899 Py_ssize_t count = 0; | 899 Py_ssize_t count = 0; |
| 900 Py_ssize_t i; | 900 Py_ssize_t i; |
| 901 | 901 |
| 902 for (i = 0; i < Py_SIZE(self); i++) { | 902 for (i = 0; i < Py_SIZE(self); i++) { |
| 903 PyObject *selfi = getarrayitem((PyObject *)self, i); | 903 PyObject *selfi = getarrayitem((PyObject *)self, i); |
| 904 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); | 904 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
| 905 Py_DECREF(selfi); | 905 Py_DECREF(selfi); |
| 906 if (cmp > 0) | 906 if (cmp > 0) |
| 907 count++; | 907 count++; |
| 908 else if (cmp < 0) | 908 else if (cmp < 0) |
| 909 return NULL; | 909 return NULL; |
| 910 } | 910 } |
| 911 return PyLong_FromSsize_t(count); | 911 return PyLong_FromSsize_t(count); |
| 912 } | 912 } |
| 913 | 913 |
| 914 PyDoc_STRVAR(count_doc, | 914 PyDoc_STRVAR(count_doc, |
| 915 "count(x)\n\ | 915 "count(x)\n\ |
| 916 \n\ | 916 \n\ |
| 917 Return number of occurrences of x in the array."); | 917 Return number of occurrences of x in the array."); |
| 918 | 918 |
| 919 static PyObject * | 919 static PyObject * |
| 920 array_index(arrayobject *self, PyObject *v) | 920 array_index(arrayobject *self, PyObject *v) |
| 921 { | 921 { |
| 922 Py_ssize_t i; | 922 Py_ssize_t i; |
| 923 | 923 |
| 924 for (i = 0; i < Py_SIZE(self); i++) { | 924 for (i = 0; i < Py_SIZE(self); i++) { |
| 925 PyObject *selfi = getarrayitem((PyObject *)self, i); | 925 PyObject *selfi = getarrayitem((PyObject *)self, i); |
| 926 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); | 926 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
| 927 Py_DECREF(selfi); | 927 Py_DECREF(selfi); |
| 928 if (cmp > 0) { | 928 if (cmp > 0) { |
| 929 return PyLong_FromLong((long)i); | 929 return PyLong_FromLong((long)i); |
| 930 } | 930 } |
| 931 else if (cmp < 0) | 931 else if (cmp < 0) |
| 932 return NULL; | 932 return NULL; |
| 933 } | 933 } |
| 934 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list"); | 934 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list"); |
| 935 return NULL; | 935 return NULL; |
| 936 } | 936 } |
| 937 | 937 |
| 938 PyDoc_STRVAR(index_doc, | 938 PyDoc_STRVAR(index_doc, |
| 939 "index(x)\n\ | 939 "index(x)\n\ |
| 940 \n\ | 940 \n\ |
| 941 Return index of first occurrence of x in the array."); | 941 Return index of first occurrence of x in the array."); |
| 942 | 942 |
| 943 static int | 943 static int |
| 944 array_contains(arrayobject *self, PyObject *v) | 944 array_contains(arrayobject *self, PyObject *v) |
| 945 { | 945 { |
| 946 Py_ssize_t i; | 946 Py_ssize_t i; |
| 947 int cmp; | 947 int cmp; |
| 948 | 948 |
| 949 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) { | 949 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) { |
| 950 PyObject *selfi = getarrayitem((PyObject *)self, i); | 950 PyObject *selfi = getarrayitem((PyObject *)self, i); |
| 951 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); | 951 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
| 952 Py_DECREF(selfi); | 952 Py_DECREF(selfi); |
| 953 } | 953 } |
| 954 return cmp; | 954 return cmp; |
| 955 } | 955 } |
| 956 | 956 |
| 957 static PyObject * | 957 static PyObject * |
| 958 array_remove(arrayobject *self, PyObject *v) | 958 array_remove(arrayobject *self, PyObject *v) |
| 959 { | 959 { |
| 960 int i; | 960 int i; |
| 961 | 961 |
| 962 for (i = 0; i < Py_SIZE(self); i++) { | 962 for (i = 0; i < Py_SIZE(self); i++) { |
| 963 PyObject *selfi = getarrayitem((PyObject *)self,i); | 963 PyObject *selfi = getarrayitem((PyObject *)self,i); |
| 964 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); | 964 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
| 965 Py_DECREF(selfi); | 965 Py_DECREF(selfi); |
| 966 if (cmp > 0) { | 966 if (cmp > 0) { |
| 967 if (array_ass_slice(self, i, i+1, | 967 if (array_ass_slice(self, i, i+1, |
| 968 (PyObject *)NULL) != 0) | 968 (PyObject *)NULL) != 0) |
| 969 return NULL; | 969 return NULL; |
| 970 Py_INCREF(Py_None); | 970 Py_INCREF(Py_None); |
| 971 return Py_None; | 971 return Py_None; |
| 972 } | 972 } |
| 973 else if (cmp < 0) | 973 else if (cmp < 0) |
| 974 return NULL; | 974 return NULL; |
| 975 } | 975 } |
| 976 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list"); | 976 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list"); |
| 977 return NULL; | 977 return NULL; |
| 978 } | 978 } |
| 979 | 979 |
| 980 PyDoc_STRVAR(remove_doc, | 980 PyDoc_STRVAR(remove_doc, |
| 981 "remove(x)\n\ | 981 "remove(x)\n\ |
| 982 \n\ | 982 \n\ |
| 983 Remove the first occurrence of x in the array."); | 983 Remove the first occurrence of x in the array."); |
| 984 | 984 |
| 985 static PyObject * | 985 static PyObject * |
| 986 array_pop(arrayobject *self, PyObject *args) | 986 array_pop(arrayobject *self, PyObject *args) |
| 987 { | 987 { |
| 988 Py_ssize_t i = -1; | 988 Py_ssize_t i = -1; |
| 989 PyObject *v; | 989 PyObject *v; |
| 990 if (!PyArg_ParseTuple(args, "|n:pop", &i)) | 990 if (!PyArg_ParseTuple(args, "|n:pop", &i)) |
| 991 return NULL; | 991 return NULL; |
| 992 if (Py_SIZE(self) == 0) { | 992 if (Py_SIZE(self) == 0) { |
| 993 /* Special-case most common failure cause */ | 993 /* Special-case most common failure cause */ |
| 994 PyErr_SetString(PyExc_IndexError, "pop from empty array"); | 994 PyErr_SetString(PyExc_IndexError, "pop from empty array"); |
| 995 return NULL; | 995 return NULL; |
| 996 } | 996 } |
| 997 if (i < 0) | 997 if (i < 0) |
| 998 i += Py_SIZE(self); | 998 i += Py_SIZE(self); |
| 999 if (i < 0 || i >= Py_SIZE(self)) { | 999 if (i < 0 || i >= Py_SIZE(self)) { |
| 1000 PyErr_SetString(PyExc_IndexError, "pop index out of range"); | 1000 PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
| 1001 return NULL; | 1001 return NULL; |
| 1002 } | 1002 } |
| 1003 v = getarrayitem((PyObject *)self,i); | 1003 v = getarrayitem((PyObject *)self,i); |
| 1004 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) { | 1004 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) { |
| 1005 Py_DECREF(v); | 1005 Py_DECREF(v); |
| 1006 return NULL; | 1006 return NULL; |
| 1007 } | 1007 } |
| 1008 return v; | 1008 return v; |
| 1009 } | 1009 } |
| 1010 | 1010 |
| 1011 PyDoc_STRVAR(pop_doc, | 1011 PyDoc_STRVAR(pop_doc, |
| 1012 "pop([i])\n\ | 1012 "pop([i])\n\ |
| 1013 \n\ | 1013 \n\ |
| 1014 Return the i-th element and delete it from the array. i defaults to -1."); | 1014 Return the i-th element and delete it from the array. i defaults to -1."); |
| 1015 | 1015 |
| 1016 static PyObject * | 1016 static PyObject * |
| 1017 array_extend(arrayobject *self, PyObject *bb) | 1017 array_extend(arrayobject *self, PyObject *bb) |
| 1018 { | 1018 { |
| 1019 if (array_do_extend(self, bb) == -1) | 1019 if (array_do_extend(self, bb) == -1) |
| 1020 return NULL; | 1020 return NULL; |
| 1021 Py_INCREF(Py_None); | 1021 Py_INCREF(Py_None); |
| 1022 return Py_None; | 1022 return Py_None; |
| 1023 } | 1023 } |
| 1024 | 1024 |
| 1025 PyDoc_STRVAR(extend_doc, | 1025 PyDoc_STRVAR(extend_doc, |
| 1026 "extend(array or iterable)\n\ | 1026 "extend(array or iterable)\n\ |
| 1027 \n\ | 1027 \n\ |
| 1028 Append items to the end of the array."); | 1028 Append items to the end of the array."); |
| 1029 | 1029 |
| 1030 static PyObject * | 1030 static PyObject * |
| 1031 array_insert(arrayobject *self, PyObject *args) | 1031 array_insert(arrayobject *self, PyObject *args) |
| 1032 { | 1032 { |
| 1033 Py_ssize_t i; | 1033 Py_ssize_t i; |
| 1034 PyObject *v; | 1034 PyObject *v; |
| 1035 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v)) | 1035 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v)) |
| 1036 return NULL; | 1036 return NULL; |
| 1037 return ins(self, i, v); | 1037 return ins(self, i, v); |
| 1038 } | 1038 } |
| 1039 | 1039 |
| 1040 PyDoc_STRVAR(insert_doc, | 1040 PyDoc_STRVAR(insert_doc, |
| 1041 "insert(i,x)\n\ | 1041 "insert(i,x)\n\ |
| 1042 \n\ | 1042 \n\ |
| 1044 | 1044 |
| 1045 | 1045 |
| 1046 static PyObject * | 1046 static PyObject * |
| 1047 array_buffer_info(arrayobject *self, PyObject *unused) | 1047 array_buffer_info(arrayobject *self, PyObject *unused) |
| 1048 { | 1048 { |
| 1049 PyObject* retval = NULL; | 1049 PyObject* retval = NULL; |
| 1050 retval = PyTuple_New(2); | 1050 retval = PyTuple_New(2); |
| 1051 if (!retval) | 1051 if (!retval) |
| 1052 return NULL; | 1052 return NULL; |
| 1053 | 1053 |
| 1054 PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item)); | 1054 PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item)); |
| 1055 PyTuple_SET_ITEM(retval, 1, PyLong_FromLong((long)(Py_SIZE(self)))); | 1055 PyTuple_SET_ITEM(retval, 1, PyLong_FromLong((long)(Py_SIZE(self)))); |
| 1056 | 1056 |
| 1057 return retval; | 1057 return retval; |
| 1058 } | 1058 } |
| 1059 | 1059 |
| 1060 PyDoc_STRVAR(buffer_info_doc, | 1060 PyDoc_STRVAR(buffer_info_doc, |
| 1061 "buffer_info() -> (address, length)\n\ | 1061 "buffer_info() -> (address, length)\n\ |
| 1062 \n\ | 1062 \n\ |
| 1067 | 1067 |
| 1068 | 1068 |
| 1069 static PyObject * | 1069 static PyObject * |
| 1070 array_append(arrayobject *self, PyObject *v) | 1070 array_append(arrayobject *self, PyObject *v) |
| 1071 { | 1071 { |
| 1072 return ins(self, (int) Py_SIZE(self), v); | 1072 return ins(self, (int) Py_SIZE(self), v); |
| 1073 } | 1073 } |
| 1074 | 1074 |
| 1075 PyDoc_STRVAR(append_doc, | 1075 PyDoc_STRVAR(append_doc, |
| 1076 "append(x)\n\ | 1076 "append(x)\n\ |
| 1077 \n\ | 1077 \n\ |
| 1079 | 1079 |
| 1080 | 1080 |
| 1081 static PyObject * | 1081 static PyObject * |
| 1082 array_byteswap(arrayobject *self, PyObject *unused) | 1082 array_byteswap(arrayobject *self, PyObject *unused) |
| 1083 { | 1083 { |
| 1084 char *p; | 1084 char *p; |
| 1085 Py_ssize_t i; | 1085 Py_ssize_t i; |
| 1086 | 1086 |
| 1087 switch (self->ob_descr->itemsize) { | 1087 switch (self->ob_descr->itemsize) { |
| 1088 case 1: | 1088 case 1: |
| 1089 break; | 1089 break; |
| 1090 case 2: | 1090 case 2: |
| 1091 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) { | 1091 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) { |
| 1092 char p0 = p[0]; | 1092 char p0 = p[0]; |
| 1093 p[0] = p[1]; | 1093 p[0] = p[1]; |
| 1094 p[1] = p0; | 1094 p[1] = p0; |
| 1095 } | 1095 } |
| 1096 break; | 1096 break; |
| 1097 case 4: | 1097 case 4: |
| 1098 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 4) { | 1098 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 4) { |
| 1099 char p0 = p[0]; | 1099 char p0 = p[0]; |
| 1100 char p1 = p[1]; | 1100 char p1 = p[1]; |
| 1101 p[0] = p[3]; | 1101 p[0] = p[3]; |
| 1102 p[1] = p[2]; | 1102 p[1] = p[2]; |
| 1103 p[2] = p1; | 1103 p[2] = p1; |
| 1104 p[3] = p0; | 1104 p[3] = p0; |
| 1105 } | 1105 } |
| 1106 break; | 1106 break; |
| 1107 case 8: | 1107 case 8: |
| 1108 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) { | 1108 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) { |
| 1109 char p0 = p[0]; | 1109 char p0 = p[0]; |
| 1110 char p1 = p[1]; | 1110 char p1 = p[1]; |
| 1111 char p2 = p[2]; | 1111 char p2 = p[2]; |
| 1112 char p3 = p[3]; | 1112 char p3 = p[3]; |
| 1113 p[0] = p[7]; | 1113 p[0] = p[7]; |
| 1114 p[1] = p[6]; | 1114 p[1] = p[6]; |
| 1115 p[2] = p[5]; | 1115 p[2] = p[5]; |
| 1116 p[3] = p[4]; | 1116 p[3] = p[4]; |
| 1117 p[4] = p3; | 1117 p[4] = p3; |
| 1118 p[5] = p2; | 1118 p[5] = p2; |
| 1119 p[6] = p1; | 1119 p[6] = p1; |
| 1120 p[7] = p0; | 1120 p[7] = p0; |
| 1121 } | 1121 } |
| 1122 break; | 1122 break; |
| 1123 default: | 1123 default: |
| 1124 PyErr_SetString(PyExc_RuntimeError, | 1124 PyErr_SetString(PyExc_RuntimeError, |
| 1125 "don't know how to byteswap this array type"); | 1125 "don't know how to byteswap this array type"); |
| 1126 return NULL; | 1126 return NULL; |
| 1127 } | 1127 } |
| 1128 Py_INCREF(Py_None); | 1128 Py_INCREF(Py_None); |
| 1129 return Py_None; | 1129 return Py_None; |
| 1130 } | 1130 } |
| 1131 | 1131 |
| 1132 PyDoc_STRVAR(byteswap_doc, | 1132 PyDoc_STRVAR(byteswap_doc, |
| 1133 "byteswap()\n\ | 1133 "byteswap()\n\ |
| 1134 \n\ | 1134 \n\ |
| 1136 4, or 8 bytes in size, RuntimeError is raised."); | 1136 4, or 8 bytes in size, RuntimeError is raised."); |
| 1137 | 1137 |
| 1138 static PyObject * | 1138 static PyObject * |
| 1139 array_reduce(arrayobject *array) | 1139 array_reduce(arrayobject *array) |
| 1140 { | 1140 { |
| 1141 PyObject *dict, *result; | 1141 PyObject *dict, *result; |
| 1142 | 1142 |
| 1143 dict = PyObject_GetAttrString((PyObject *)array, "__dict__"); | 1143 dict = PyObject_GetAttrString((PyObject *)array, "__dict__"); |
| 1144 if (dict == NULL) { | 1144 if (dict == NULL) { |
| 1145 PyErr_Clear(); | 1145 PyErr_Clear(); |
| 1146 dict = Py_None; | 1146 dict = Py_None; |
| 1147 Py_INCREF(dict); | 1147 Py_INCREF(dict); |
| 1148 } | 1148 } |
| 1149 if (Py_SIZE(array) > 0) { | 1149 if (Py_SIZE(array) > 0) { |
| 1150 if (array->ob_descr->itemsize | 1150 if (array->ob_descr->itemsize |
| 1151 > PY_SSIZE_T_MAX / Py_SIZE(array)) { | 1151 > PY_SSIZE_T_MAX / Py_SIZE(array)) { |
| 1152 return PyErr_NoMemory(); | 1152 return PyErr_NoMemory(); |
| 1153 } | 1153 } |
| 1154 result = Py_BuildValue("O(Cy#)O", | 1154 result = Py_BuildValue("O(Cy#)O", |
| 1155 Py_TYPE(array), | 1155 Py_TYPE(array), |
| 1156 array->ob_descr->typecode, | 1156 array->ob_descr->typecode, |
| 1157 array->ob_item, | 1157 array->ob_item, |
| 1158 Py_SIZE(array) * array->ob_descr->itemsize, | 1158 Py_SIZE(array) * array->ob_descr->itemsize, |
| 1159 dict); | 1159 dict); |
| 1160 } else { | 1160 } else { |
| 1161 result = Py_BuildValue("O(C)O", | 1161 result = Py_BuildValue("O(C)O", |
| 1162 Py_TYPE(array), | 1162 Py_TYPE(array), |
| 1163 array->ob_descr->typecode, | 1163 array->ob_descr->typecode, |
| 1164 dict); | 1164 dict); |
| 1165 } | 1165 } |
| 1166 Py_DECREF(dict); | 1166 Py_DECREF(dict); |
| 1167 return result; | 1167 return result; |
| 1168 } | 1168 } |
| 1169 | 1169 |
| 1170 PyDoc_STRVAR(array_doc, "Return state information for pickling."); | 1170 PyDoc_STRVAR(array_doc, "Return state information for pickling."); |
| 1171 | 1171 |
| 1172 static PyObject * | 1172 static PyObject * |
| 1173 array_reverse(arrayobject *self, PyObject *unused) | 1173 array_reverse(arrayobject *self, PyObject *unused) |
| 1174 { | 1174 { |
| 1175 register Py_ssize_t itemsize = self->ob_descr->itemsize; | 1175 register Py_ssize_t itemsize = self->ob_descr->itemsize; |
| 1176 register char *p, *q; | 1176 register char *p, *q; |
| 1177 /* little buffer to hold items while swapping */ | 1177 /* little buffer to hold items while swapping */ |
| 1178 char tmp[256]; /* 8 is probably enough -- but why skimp */ | 1178 char tmp[256]; /* 8 is probably enough -- but why skimp */ |
| 1179 assert((size_t)itemsize <= sizeof(tmp)); | 1179 assert((size_t)itemsize <= sizeof(tmp)); |
| 1180 | 1180 |
| 1181 if (Py_SIZE(self) > 1) { | 1181 if (Py_SIZE(self) > 1) { |
| 1182 for (p = self->ob_item, | 1182 for (p = self->ob_item, |
| 1183 q = self->ob_item + (Py_SIZE(self) - 1)*itemsize; | 1183 q = self->ob_item + (Py_SIZE(self) - 1)*itemsize; |
| 1184 p < q; | 1184 p < q; |
| 1185 p += itemsize, q -= itemsize) { | 1185 p += itemsize, q -= itemsize) { |
| 1186 /* memory areas guaranteed disjoint, so memcpy | 1186 /* memory areas guaranteed disjoint, so memcpy |
| 1187 * is safe (& memmove may be slower). | 1187 * is safe (& memmove may be slower). |
| 1188 */ | 1188 */ |
| 1189 memcpy(tmp, p, itemsize); | 1189 memcpy(tmp, p, itemsize); |
| 1190 memcpy(p, q, itemsize); | 1190 memcpy(p, q, itemsize); |
| 1191 memcpy(q, tmp, itemsize); | 1191 memcpy(q, tmp, itemsize); |
| 1192 } | 1192 } |
| 1193 } | 1193 } |
| 1194 | 1194 |
| 1195 Py_INCREF(Py_None); | 1195 Py_INCREF(Py_None); |
| 1196 return Py_None; | 1196 return Py_None; |
| 1197 } | 1197 } |
| 1198 | 1198 |
| 1199 PyDoc_STRVAR(reverse_doc, | 1199 PyDoc_STRVAR(reverse_doc, |
| 1200 "reverse()\n\ | 1200 "reverse()\n\ |
| 1201 \n\ | 1201 \n\ |
| 1206 static PyObject *array_fromstring(arrayobject *self, PyObject *args); | 1206 static PyObject *array_fromstring(arrayobject *self, PyObject *args); |
| 1207 | 1207 |
| 1208 static PyObject * | 1208 static PyObject * |
| 1209 array_fromfile(arrayobject *self, PyObject *args) | 1209 array_fromfile(arrayobject *self, PyObject *args) |
| 1210 { | 1210 { |
| 1211 PyObject *f, *b, *res; | 1211 PyObject *f, *b, *res; |
| 1212 Py_ssize_t itemsize = self->ob_descr->itemsize; | 1212 Py_ssize_t itemsize = self->ob_descr->itemsize; |
| 1213 Py_ssize_t n, nbytes; | 1213 Py_ssize_t n, nbytes; |
| 1214 int not_enough_bytes; | 1214 int not_enough_bytes; |
| 1215 | 1215 |
| 1216 if (!PyArg_ParseTuple(args, "On:fromfile", &f, &n)) | 1216 if (!PyArg_ParseTuple(args, "On:fromfile", &f, &n)) |
| 1217 return NULL; | 1217 return NULL; |
| 1218 | 1218 |
| 1219 nbytes = n * itemsize; | 1219 nbytes = n * itemsize; |
| 1220 if (nbytes < 0 || nbytes/itemsize != n) { | 1220 if (nbytes < 0 || nbytes/itemsize != n) { |
| 1221 PyErr_NoMemory(); | 1221 PyErr_NoMemory(); |
| 1222 return NULL; | 1222 return NULL; |
| 1223 } | 1223 } |
| 1224 | 1224 |
| 1225 b = PyObject_CallMethod(f, "read", "n", nbytes); | 1225 b = PyObject_CallMethod(f, "read", "n", nbytes); |
| 1226 if (b == NULL) | 1226 if (b == NULL) |
| 1227 return NULL; | 1227 return NULL; |
| 1228 | 1228 |
| 1229 if (!PyBytes_Check(b)) { | 1229 if (!PyBytes_Check(b)) { |
| 1230 PyErr_SetString(PyExc_TypeError, | 1230 PyErr_SetString(PyExc_TypeError, |
| 1231 "read() didn't return bytes"); | 1231 "read() didn't return bytes"); |
| 1232 Py_DECREF(b); | 1232 Py_DECREF(b); |
| 1233 return NULL; | 1233 return NULL; |
| 1234 } | 1234 } |
| 1235 | 1235 |
| 1236 not_enough_bytes = (PyBytes_GET_SIZE(b) != nbytes); | 1236 not_enough_bytes = (PyBytes_GET_SIZE(b) != nbytes); |
| 1237 | 1237 |
| 1238 args = Py_BuildValue("(O)", b); | 1238 args = Py_BuildValue("(O)", b); |
| 1239 Py_DECREF(b); | 1239 Py_DECREF(b); |
| 1240 if (args == NULL) | 1240 if (args == NULL) |
| 1241 return NULL; | 1241 return NULL; |
| 1242 | 1242 |
| 1243 res = array_fromstring(self, args); | 1243 res = array_fromstring(self, args); |
| 1244 Py_DECREF(args); | 1244 Py_DECREF(args); |
| 1245 if (res == NULL) | 1245 if (res == NULL) |
| 1246 return NULL; | 1246 return NULL; |
| 1247 | 1247 |
| 1248 if (not_enough_bytes) { | 1248 if (not_enough_bytes) { |
| 1249 PyErr_SetString(PyExc_EOFError, | 1249 PyErr_SetString(PyExc_EOFError, |
| 1250 "read() didn't return enough bytes"); | 1250 "read() didn't return enough bytes"); |
| 1251 Py_DECREF(res); | 1251 Py_DECREF(res); |
| 1252 return NULL; | 1252 return NULL; |
| 1253 } | 1253 } |
| 1254 | 1254 |
| 1255 return res; | 1255 return res; |
| 1256 } | 1256 } |
| 1257 | 1257 |
| 1258 PyDoc_STRVAR(fromfile_doc, | 1258 PyDoc_STRVAR(fromfile_doc, |
| 1259 "fromfile(f, n)\n\ | 1259 "fromfile(f, n)\n\ |
| 1260 \n\ | 1260 \n\ |
| 1263 | 1263 |
| 1264 | 1264 |
| 1265 static PyObject * | 1265 static PyObject * |
| 1266 array_tofile(arrayobject *self, PyObject *f) | 1266 array_tofile(arrayobject *self, PyObject *f) |
| 1267 { | 1267 { |
| 1268 Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize; | 1268 Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize; |
| 1269 /* Write 64K blocks at a time */ | 1269 /* Write 64K blocks at a time */ |
| 1270 /* XXX Make the block size settable */ | 1270 /* XXX Make the block size settable */ |
| 1271 int BLOCKSIZE = 64*1024; | 1271 int BLOCKSIZE = 64*1024; |
| 1272 Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE; | 1272 Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE; |
| 1273 Py_ssize_t i; | 1273 Py_ssize_t i; |
| 1274 | 1274 |
| 1275 if (Py_SIZE(self) == 0) | 1275 if (Py_SIZE(self) == 0) |
| 1276 goto done; | 1276 goto done; |
| 1277 | 1277 |
| 1278 for (i = 0; i < nblocks; i++) { | 1278 for (i = 0; i < nblocks; i++) { |
| 1279 char* ptr = self->ob_item + i*BLOCKSIZE; | 1279 char* ptr = self->ob_item + i*BLOCKSIZE; |
| 1280 Py_ssize_t size = BLOCKSIZE; | 1280 Py_ssize_t size = BLOCKSIZE; |
| 1281 PyObject *bytes, *res; | 1281 PyObject *bytes, *res; |
| 1282 if (i*BLOCKSIZE + size > nbytes) | 1282 if (i*BLOCKSIZE + size > nbytes) |
| 1283 size = nbytes - i*BLOCKSIZE; | 1283 size = nbytes - i*BLOCKSIZE; |
| 1284 bytes = PyBytes_FromStringAndSize(ptr, size); | 1284 bytes = PyBytes_FromStringAndSize(ptr, size); |
| 1285 if (bytes == NULL) | 1285 if (bytes == NULL) |
| 1286 return NULL; | 1286 return NULL; |
| 1287 res = PyObject_CallMethod(f, "write", "O", bytes); | 1287 res = PyObject_CallMethod(f, "write", "O", bytes); |
| 1288 Py_DECREF(bytes); | 1288 Py_DECREF(bytes); |
| 1289 if (res == NULL) | 1289 if (res == NULL) |
| 1290 return NULL; | 1290 return NULL; |
| 1291 Py_DECREF(res); /* drop write result */ | 1291 Py_DECREF(res); /* drop write result */ |
| 1292 } | 1292 } |
| 1293 | 1293 |
| 1294 done: | 1294 done: |
| 1295 Py_INCREF(Py_None); | 1295 Py_INCREF(Py_None); |
| 1296 return Py_None; | 1296 return Py_None; |
| 1297 } | 1297 } |
| 1298 | 1298 |
| 1299 PyDoc_STRVAR(tofile_doc, | 1299 PyDoc_STRVAR(tofile_doc, |
| 1300 "tofile(f)\n\ | 1300 "tofile(f)\n\ |
| 1301 \n\ | 1301 \n\ |
| 1303 | 1303 |
| 1304 | 1304 |
| 1305 static PyObject * | 1305 static PyObject * |
| 1306 array_fromlist(arrayobject *self, PyObject *list) | 1306 array_fromlist(arrayobject *self, PyObject *list) |
| 1307 { | 1307 { |
| 1308 Py_ssize_t n; | 1308 Py_ssize_t n; |
| 1309 | 1309 |
| 1310 if (!PyList_Check(list)) { | 1310 if (!PyList_Check(list)) { |
| 1311 PyErr_SetString(PyExc_TypeError, "arg must be list"); | 1311 PyErr_SetString(PyExc_TypeError, "arg must be list"); |
| 1312 return NULL; | 1312 return NULL; |
| 1313 } | 1313 } |
| 1314 n = PyList_Size(list); | 1314 n = PyList_Size(list); |
| 1315 if (n > 0) { | 1315 if (n > 0) { |
| 1316 Py_ssize_t i, old_size; | 1316 Py_ssize_t i, old_size; |
| 1317 old_size = Py_SIZE(self); | 1317 old_size = Py_SIZE(self); |
| 1318 if (array_resize(self, old_size + n) == -1) | 1318 if (array_resize(self, old_size + n) == -1) |
| 1319 return NULL; | 1319 return NULL; |
| 1320 for (i = 0; i < n; i++) { | 1320 for (i = 0; i < n; i++) { |
| 1321 PyObject *v = PyList_GetItem(list, i); | 1321 PyObject *v = PyList_GetItem(list, i); |
| 1322 if ((*self->ob_descr->setitem)(self, | 1322 if ((*self->ob_descr->setitem)(self, |
| 1323 Py_SIZE(self) - n + i, v) != 0) { | 1323 Py_SIZE(self) - n + i, v) != 0) { |
| 1324 array_resize(self, old_size); | 1324 array_resize(self, old_size); |
| 1325 return NULL; | 1325 return NULL; |
| 1326 } | 1326 } |
| 1327 } | 1327 } |
| 1328 } | 1328 } |
| 1329 Py_INCREF(Py_None); | 1329 Py_INCREF(Py_None); |
| 1330 return Py_None; | 1330 return Py_None; |
| 1331 } | 1331 } |
| 1332 | 1332 |
| 1333 PyDoc_STRVAR(fromlist_doc, | 1333 PyDoc_STRVAR(fromlist_doc, |
| 1334 "fromlist(list)\n\ | 1334 "fromlist(list)\n\ |
| 1335 \n\ | 1335 \n\ |
| 1336 Append items to array from list."); | 1336 Append items to array from list."); |
| 1337 | 1337 |
| 1338 static PyObject * | 1338 static PyObject * |
| 1339 array_tolist(arrayobject *self, PyObject *unused) | 1339 array_tolist(arrayobject *self, PyObject *unused) |
| 1340 { | 1340 { |
| 1341 PyObject *list = PyList_New(Py_SIZE(self)); | 1341 PyObject *list = PyList_New(Py_SIZE(self)); |
| 1342 Py_ssize_t i; | 1342 Py_ssize_t i; |
| 1343 | 1343 |
| 1344 if (list == NULL) | 1344 if (list == NULL) |
| 1345 return NULL; | 1345 return NULL; |
| 1346 for (i = 0; i < Py_SIZE(self); i++) { | 1346 for (i = 0; i < Py_SIZE(self); i++) { |
| 1347 PyObject *v = getarrayitem((PyObject *)self, i); | 1347 PyObject *v = getarrayitem((PyObject *)self, i); |
| 1348 if (v == NULL) { | 1348 if (v == NULL) { |
| 1349 Py_DECREF(list); | 1349 Py_DECREF(list); |
| 1350 return NULL; | 1350 return NULL; |
| 1351 } | 1351 } |
| 1352 PyList_SetItem(list, i, v); | 1352 PyList_SetItem(list, i, v); |
| 1353 } | 1353 } |
| 1354 return list; | 1354 return list; |
| 1355 } | 1355 } |
| 1356 | 1356 |
| 1357 PyDoc_STRVAR(tolist_doc, | 1357 PyDoc_STRVAR(tolist_doc, |
| 1358 "tolist() -> list\n\ | 1358 "tolist() -> list\n\ |
| 1359 \n\ | 1359 \n\ |
| 1361 | 1361 |
| 1362 | 1362 |
| 1363 static PyObject * | 1363 static PyObject * |
| 1364 array_fromstring(arrayobject *self, PyObject *args) | 1364 array_fromstring(arrayobject *self, PyObject *args) |
| 1365 { | 1365 { |
| 1366 char *str; | 1366 char *str; |
| 1367 Py_ssize_t n; | 1367 Py_ssize_t n; |
| 1368 int itemsize = self->ob_descr->itemsize; | 1368 int itemsize = self->ob_descr->itemsize; |
| 1369 if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n)) | 1369 if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n)) |
| 1370 return NULL; | 1370 return NULL; |
| 1371 if (n % itemsize != 0) { | 1371 if (n % itemsize != 0) { |
| 1372 PyErr_SetString(PyExc_ValueError, | 1372 PyErr_SetString(PyExc_ValueError, |
| 1373 "string length not a multiple of item size"); | 1373 "string length not a multiple of item size"); |
| 1374 return NULL; | 1374 return NULL; |
| 1375 } | 1375 } |
| 1376 n = n / itemsize; | 1376 n = n / itemsize; |
| 1377 if (n > 0) { | 1377 if (n > 0) { |
| 1378 Py_ssize_t old_size = Py_SIZE(self); | 1378 Py_ssize_t old_size = Py_SIZE(self); |
| 1379 if ((n > PY_SSIZE_T_MAX - old_size) || | 1379 if ((n > PY_SSIZE_T_MAX - old_size) || |
| 1380 ((old_size + n) > PY_SSIZE_T_MAX / itemsize)) { | 1380 ((old_size + n) > PY_SSIZE_T_MAX / itemsize)) { |
| 1381 return PyErr_NoMemory(); | 1381 return PyErr_NoMemory(); |
| 1382 } | 1382 } |
| 1383 if (array_resize(self, old_size + n) == -1) | 1383 if (array_resize(self, old_size + n) == -1) |
| 1384 return NULL; | 1384 return NULL; |
| 1385 memcpy(self->ob_item + old_size * itemsize, | 1385 memcpy(self->ob_item + old_size * itemsize, |
| 1386 str, n * itemsize); | 1386 str, n * itemsize); |
| 1387 } | 1387 } |
| 1388 Py_INCREF(Py_None); | 1388 Py_INCREF(Py_None); |
| 1389 return Py_None; | 1389 return Py_None; |
| 1390 } | 1390 } |
| 1391 | 1391 |
| 1392 PyDoc_STRVAR(fromstring_doc, | 1392 PyDoc_STRVAR(fromstring_doc, |
| 1393 "fromstring(string)\n\ | 1393 "fromstring(string)\n\ |
| 1394 \n\ | 1394 \n\ |
| 1397 | 1397 |
| 1398 | 1398 |
| 1399 static PyObject * | 1399 static PyObject * |
| 1400 array_tostring(arrayobject *self, PyObject *unused) | 1400 array_tostring(arrayobject *self, PyObject *unused) |
| 1401 { | 1401 { |
| 1402 if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) { | 1402 if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) { |
| 1403 return PyBytes_FromStringAndSize(self->ob_item, | 1403 return PyBytes_FromStringAndSize(self->ob_item, |
| 1404 Py_SIZE(self) * self->ob_descr->itemsize); | 1404 Py_SIZE(self) * self->ob_descr->itemsize); |
| 1405 } else { | 1405 } else { |
| 1406 return PyErr_NoMemory(); | 1406 return PyErr_NoMemory(); |
| 1407 } | 1407 } |
| 1408 } | 1408 } |
| 1409 | 1409 |
| 1410 PyDoc_STRVAR(tostring_doc, | 1410 PyDoc_STRVAR(tostring_doc, |
| 1411 "tostring() -> string\n\ | 1411 "tostring() -> string\n\ |
| 1412 \n\ | 1412 \n\ |
| 1416 | 1416 |
| 1417 | 1417 |
| 1418 static PyObject * | 1418 static PyObject * |
| 1419 array_fromunicode(arrayobject *self, PyObject *args) | 1419 array_fromunicode(arrayobject *self, PyObject *args) |
| 1420 { | 1420 { |
| 1421 Py_UNICODE *ustr; | 1421 Py_UNICODE *ustr; |
| 1422 Py_ssize_t n; | 1422 Py_ssize_t n; |
| 1423 char typecode; | 1423 char typecode; |
| 1424 | 1424 |
| 1425 if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n)) | 1425 if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n)) |
| 1426 return NULL; | 1426 return NULL; |
| 1427 typecode = self->ob_descr->typecode; | 1427 typecode = self->ob_descr->typecode; |
| 1428 if ((typecode != 'u')) { | 1428 if ((typecode != 'u')) { |
| 1429 PyErr_SetString(PyExc_ValueError, | 1429 PyErr_SetString(PyExc_ValueError, |
| 1430 "fromunicode() may only be called on " | 1430 "fromunicode() may only be called on " |
| 1431 "unicode type arrays"); | 1431 "unicode type arrays"); |
| 1432 return NULL; | 1432 return NULL; |
| 1433 } | 1433 } |
| 1434 if (n > 0) { | 1434 if (n > 0) { |
| 1435 Py_ssize_t old_size = Py_SIZE(self); | 1435 Py_ssize_t old_size = Py_SIZE(self); |
| 1436 if (array_resize(self, old_size + n) == -1) | 1436 if (array_resize(self, old_size + n) == -1) |
| 1437 return NULL; | 1437 return NULL; |
| 1438 memcpy(self->ob_item + old_size * sizeof(Py_UNICODE), | 1438 memcpy(self->ob_item + old_size * sizeof(Py_UNICODE), |
| 1439 ustr, n * sizeof(Py_UNICODE)); | 1439 ustr, n * sizeof(Py_UNICODE)); |
| 1440 } | 1440 } |
| 1441 | 1441 |
| 1442 Py_INCREF(Py_None); | 1442 Py_INCREF(Py_None); |
| 1443 return Py_None; | 1443 return Py_None; |
| 1444 } | 1444 } |
| 1445 | 1445 |
| 1446 PyDoc_STRVAR(fromunicode_doc, | 1446 PyDoc_STRVAR(fromunicode_doc, |
| 1447 "fromunicode(ustr)\n\ | 1447 "fromunicode(ustr)\n\ |
| 1448 \n\ | 1448 \n\ |
| 1453 | 1453 |
| 1454 | 1454 |
| 1455 static PyObject * | 1455 static PyObject * |
| 1456 array_tounicode(arrayobject *self, PyObject *unused) | 1456 array_tounicode(arrayobject *self, PyObject *unused) |
| 1457 { | 1457 { |
| 1458 char typecode; | 1458 char typecode; |
| 1459 typecode = self->ob_descr->typecode; | 1459 typecode = self->ob_descr->typecode; |
| 1460 if ((typecode != 'u')) { | 1460 if ((typecode != 'u')) { |
| 1461 PyErr_SetString(PyExc_ValueError, | 1461 PyErr_SetString(PyExc_ValueError, |
| 1462 "tounicode() may only be called on unicode type arrays"); | 1462 "tounicode() may only be called on unicode type arrays"); |
| 1463 return NULL; | 1463 return NULL; |
| 1464 } | 1464 } |
| 1465 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_SIZE(self)); | 1465 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_SIZE(self)); |
| 1466 } | 1466 } |
| 1467 | 1467 |
| 1468 PyDoc_STRVAR(tounicode_doc, | 1468 PyDoc_STRVAR(tounicode_doc, |
| 1469 "tounicode() -> unicode\n\ | 1469 "tounicode() -> unicode\n\ |
| 1470 \n\ | 1470 \n\ |
| 1476 | 1476 |
| 1477 | 1477 |
| 1478 static PyObject * | 1478 static PyObject * |
| 1479 array_get_typecode(arrayobject *a, void *closure) | 1479 array_get_typecode(arrayobject *a, void *closure) |
| 1480 { | 1480 { |
| 1481 char tc = a->ob_descr->typecode; | 1481 char tc = a->ob_descr->typecode; |
| 1482 return PyUnicode_FromStringAndSize(&tc, 1); | 1482 return PyUnicode_FromStringAndSize(&tc, 1); |
| 1483 } | 1483 } |
| 1484 | 1484 |
| 1485 static PyObject * | 1485 static PyObject * |
| 1486 array_get_itemsize(arrayobject *a, void *closure) | 1486 array_get_itemsize(arrayobject *a, void *closure) |
| 1487 { | 1487 { |
| 1488 return PyLong_FromLong((long)a->ob_descr->itemsize); | 1488 return PyLong_FromLong((long)a->ob_descr->itemsize); |
| 1489 } | 1489 } |
| 1490 | 1490 |
| 1491 static PyGetSetDef array_getsets [] = { | 1491 static PyGetSetDef array_getsets [] = { |
| 1492 {"typecode", (getter) array_get_typecode, NULL, | 1492 {"typecode", (getter) array_get_typecode, NULL, |
| 1493 "the typecode character used to create the array"}, | 1493 "the typecode character used to create the array"}, |
| 1494 {"itemsize", (getter) array_get_itemsize, NULL, | 1494 {"itemsize", (getter) array_get_itemsize, NULL, |
| 1495 "the size, in bytes, of one array item"}, | 1495 "the size, in bytes, of one array item"}, |
| 1496 {NULL} | 1496 {NULL} |
| 1497 }; | 1497 }; |
| 1498 | 1498 |
| 1499 static PyMethodDef array_methods[] = { | 1499 static PyMethodDef array_methods[] = { |
| 1500 {"append", (PyCFunction)array_append, METH_O, | 1500 {"append", (PyCFunction)array_append, METH_O, |
| 1501 append_doc}, | 1501 append_doc}, |
| 1502 {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS, | 1502 {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS, |
| 1503 buffer_info_doc}, | 1503 buffer_info_doc}, |
| 1504 {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS, | 1504 {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS, |
| 1505 byteswap_doc}, | 1505 byteswap_doc}, |
| 1506 {"__copy__", (PyCFunction)array_copy, METH_NOARGS, | 1506 {"__copy__", (PyCFunction)array_copy, METH_NOARGS, |
| 1507 copy_doc}, | 1507 copy_doc}, |
| 1508 {"count", (PyCFunction)array_count, METH_O, | 1508 {"count", (PyCFunction)array_count, METH_O, |
| 1509 count_doc}, | 1509 count_doc}, |
| 1510 {"__deepcopy__",(PyCFunction)array_copy, METH_O, | 1510 {"__deepcopy__",(PyCFunction)array_copy, METH_O, |
| 1511 copy_doc}, | 1511 copy_doc}, |
| 1512 {"extend", (PyCFunction)array_extend, METH_O, | 1512 {"extend", (PyCFunction)array_extend, METH_O, |
| 1513 extend_doc}, | 1513 extend_doc}, |
| 1514 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS, | 1514 {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS, |
| 1515 fromfile_doc}, | 1515 fromfile_doc}, |
| 1516 {"fromlist", (PyCFunction)array_fromlist, METH_O, | 1516 {"fromlist", (PyCFunction)array_fromlist, METH_O, |
| 1517 fromlist_doc}, | 1517 fromlist_doc}, |
| 1518 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS, | 1518 {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS, |
| 1519 fromstring_doc}, | 1519 fromstring_doc}, |
| 1520 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS, | 1520 {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS, |
| 1521 fromunicode_doc}, | 1521 fromunicode_doc}, |
| 1522 {"index", (PyCFunction)array_index, METH_O, | 1522 {"index", (PyCFunction)array_index, METH_O, |
| 1523 index_doc}, | 1523 index_doc}, |
| 1524 {"insert", (PyCFunction)array_insert, METH_VARARGS, | 1524 {"insert", (PyCFunction)array_insert, METH_VARARGS, |
| 1525 insert_doc}, | 1525 insert_doc}, |
| 1526 {"pop", (PyCFunction)array_pop, METH_VARARGS, | 1526 {"pop", (PyCFunction)array_pop, METH_VARARGS, |
| 1527 pop_doc}, | 1527 pop_doc}, |
| 1528 {"__reduce__", (PyCFunction)array_reduce, METH_NOARGS, | 1528 {"__reduce__", (PyCFunction)array_reduce, METH_NOARGS, |
| 1529 array_doc}, | 1529 array_doc}, |
| 1530 {"remove", (PyCFunction)array_remove, METH_O, | 1530 {"remove", (PyCFunction)array_remove, METH_O, |
| 1531 remove_doc}, | 1531 remove_doc}, |
| 1532 {"reverse", (PyCFunction)array_reverse, METH_NOARGS, | 1532 {"reverse", (PyCFunction)array_reverse, METH_NOARGS, |
| 1533 reverse_doc}, | 1533 reverse_doc}, |
| 1534 /* {"sort", (PyCFunction)array_sort, METH_VARARGS, | 1534 /* {"sort", (PyCFunction)array_sort, METH_VARARGS, |
| 1535 sort_doc},*/ | 1535 sort_doc},*/ |
| 1536 {"tofile", (PyCFunction)array_tofile, METH_O, | 1536 {"tofile", (PyCFunction)array_tofile, METH_O, |
| 1537 tofile_doc}, | 1537 tofile_doc}, |
| 1538 {"tolist", (PyCFunction)array_tolist, METH_NOARGS, | 1538 {"tolist", (PyCFunction)array_tolist, METH_NOARGS, |
| 1539 tolist_doc}, | 1539 tolist_doc}, |
| 1540 {"tostring", (PyCFunction)array_tostring, METH_NOARGS, | 1540 {"tostring", (PyCFunction)array_tostring, METH_NOARGS, |
| 1541 tostring_doc}, | 1541 tostring_doc}, |
| 1542 {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS, | 1542 {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS, |
| 1543 tounicode_doc}, | 1543 tounicode_doc}, |
| 1544 {NULL, NULL} /* sentinel */ | 1544 {NULL, NULL} /* sentinel */ |
| 1545 }; | 1545 }; |
| 1546 | 1546 |
| 1547 static PyObject * | 1547 static PyObject * |
| 1548 array_repr(arrayobject *a) | 1548 array_repr(arrayobject *a) |
| 1549 { | 1549 { |
| 1550 char typecode; | 1550 char typecode; |
| 1551 PyObject *s, *v = NULL; | 1551 PyObject *s, *v = NULL; |
| 1552 Py_ssize_t len; | 1552 Py_ssize_t len; |
| 1553 | 1553 |
| 1554 len = Py_SIZE(a); | 1554 len = Py_SIZE(a); |
| 1555 typecode = a->ob_descr->typecode; | 1555 typecode = a->ob_descr->typecode; |
| 1556 if (len == 0) { | 1556 if (len == 0) { |
| 1557 return PyUnicode_FromFormat("array('%c')", typecode); | 1557 return PyUnicode_FromFormat("array('%c')", typecode); |
| 1558 } | 1558 } |
| 1559 if ((typecode == 'u')) | 1559 if ((typecode == 'u')) |
| 1560 v = array_tounicode(a, NULL); | 1560 v = array_tounicode(a, NULL); |
| 1561 else | 1561 else |
| 1562 v = array_tolist(a, NULL); | 1562 v = array_tolist(a, NULL); |
| 1563 | 1563 |
| 1564 s = PyUnicode_FromFormat("array('%c', %R)", typecode, v); | 1564 s = PyUnicode_FromFormat("array('%c', %R)", typecode, v); |
| 1565 Py_DECREF(v); | 1565 Py_DECREF(v); |
| 1566 return s; | 1566 return s; |
| 1567 } | 1567 } |
| 1568 | 1568 |
| 1569 static PyObject* | 1569 static PyObject* |
| 1570 array_subscr(arrayobject* self, PyObject* item) | 1570 array_subscr(arrayobject* self, PyObject* item) |
| 1571 { | 1571 { |
| 1572 if (PyIndex_Check(item)) { | 1572 if (PyIndex_Check(item)) { |
| 1573 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); | 1573 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 1574 if (i==-1 && PyErr_Occurred()) { | 1574 if (i==-1 && PyErr_Occurred()) { |
| 1575 return NULL; | 1575 return NULL; |
| 1576 } | 1576 } |
| 1577 if (i < 0) | 1577 if (i < 0) |
| 1578 i += Py_SIZE(self); | 1578 i += Py_SIZE(self); |
| 1579 return array_item(self, i); | 1579 return array_item(self, i); |
| 1580 } | 1580 } |
| 1581 else if (PySlice_Check(item)) { | 1581 else if (PySlice_Check(item)) { |
| 1582 Py_ssize_t start, stop, step, slicelength, cur, i; | 1582 Py_ssize_t start, stop, step, slicelength, cur, i; |
| 1583 PyObject* result; | 1583 PyObject* result; |
| 1584 arrayobject* ar; | 1584 arrayobject* ar; |
| 1585 int itemsize = self->ob_descr->itemsize; | 1585 int itemsize = self->ob_descr->itemsize; |
| 1586 | 1586 |
| 1587 if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self), | 1587 if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self), |
| 1588 &start, &stop, &step, &slicelength) < 0) { | 1588 &start, &stop, &step, &slicelength) < 0) { |
| 1589 return NULL; | 1589 return NULL; |
| 1590 } | 1590 } |
| 1591 | 1591 |
| 1592 if (slicelength <= 0) { | 1592 if (slicelength <= 0) { |
| 1593 return newarrayobject(&Arraytype, 0, self->ob_descr); | 1593 return newarrayobject(&Arraytype, 0, self->ob_descr); |
| 1594 } | 1594 } |
| 1595 else if (step == 1) { | 1595 else if (step == 1) { |
| 1596 PyObject *result = newarrayobject(&Arraytype, | 1596 PyObject *result = newarrayobject(&Arraytype, |
| 1597 slicelength, self->ob_descr); | 1597 slicelength, self->ob_descr); |
| 1598 if (result == NULL) | 1598 if (result == NULL) |
| 1599 return NULL; | 1599 return NULL; |
| 1600 memcpy(((arrayobject *)result)->ob_item, | 1600 memcpy(((arrayobject *)result)->ob_item, |
| 1601 self->ob_item + start * itemsize, | 1601 self->ob_item + start * itemsize, |
| 1602 slicelength * itemsize); | 1602 slicelength * itemsize); |
| 1603 return result; | 1603 return result; |
| 1604 } | 1604 } |
| 1605 else { | 1605 else { |
| 1606 result = newarrayobject(&Arraytype, slicelength, self->ob_descr); | 1606 result = newarrayobject(&Arraytype, slicelength, self->ob_descr); |
| 1607 if (!result) return NULL; | 1607 if (!result) return NULL; |
| 1608 | 1608 |
| 1609 ar = (arrayobject*)result; | 1609 ar = (arrayobject*)result; |
| 1610 | 1610 |
| 1611 for (cur = start, i = 0; i < slicelength; | 1611 for (cur = start, i = 0; i < slicelength; |
| 1612 cur += step, i++) { | 1612 cur += step, i++) { |
| 1613 memcpy(ar->ob_item + i*itemsize, | 1613 memcpy(ar->ob_item + i*itemsize, |
| 1614 self->ob_item + cur*itemsize, | 1614 self->ob_item + cur*itemsize, |
| 1615 itemsize); | 1615 itemsize); |
| 1616 } | 1616 } |
| 1617 | 1617 |
| 1618 return result; | 1618 return result; |
| 1619 } | 1619 } |
| 1620 } | 1620 } |
| 1621 else { | 1621 else { |
| 1622 PyErr_SetString(PyExc_TypeError, | 1622 PyErr_SetString(PyExc_TypeError, |
| 1623 "array indices must be integers"); | 1623 "array indices must be integers"); |
| 1624 return NULL; | 1624 return NULL; |
| 1625 } | 1625 } |
| 1626 } | 1626 } |
| 1627 | 1627 |
| 1628 static int | 1628 static int |
| 1629 array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value) | 1629 array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value) |
| 1630 { | 1630 { |
| 1631 Py_ssize_t start, stop, step, slicelength, needed; | 1631 Py_ssize_t start, stop, step, slicelength, needed; |
| 1632 arrayobject* other; | 1632 arrayobject* other; |
| 1633 int itemsize; | 1633 int itemsize; |
| 1634 | 1634 |
| 1635 if (PyIndex_Check(item)) { | 1635 if (PyIndex_Check(item)) { |
| 1636 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); | 1636 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 1637 | 1637 |
| 1638 if (i == -1 && PyErr_Occurred()) | 1638 if (i == -1 && PyErr_Occurred()) |
| 1639 return -1; | 1639 return -1; |
| 1640 if (i < 0) | 1640 if (i < 0) |
| 1641 i += Py_SIZE(self); | 1641 i += Py_SIZE(self); |
| 1642 if (i < 0 || i >= Py_SIZE(self)) { | 1642 if (i < 0 || i >= Py_SIZE(self)) { |
| 1643 PyErr_SetString(PyExc_IndexError, | 1643 PyErr_SetString(PyExc_IndexError, |
| 1644 "array assignment index out of range"); | 1644 "array assignment index out of range"); |
| 1645 return -1; | 1645 return -1; |
| 1646 } | 1646 } |
| 1647 if (value == NULL) { | 1647 if (value == NULL) { |
| 1648 /* Fall through to slice assignment */ | 1648 /* Fall through to slice assignment */ |
| 1649 start = i; | 1649 start = i; |
| 1650 stop = i + 1; | 1650 stop = i + 1; |
| 1651 step = 1; | 1651 step = 1; |
| 1652 slicelength = 1; | 1652 slicelength = 1; |
| 1653 } | 1653 } |
| 1654 else | 1654 else |
| 1655 return (*self->ob_descr->setitem)(self, i, value); | 1655 return (*self->ob_descr->setitem)(self, i, value); |
| 1656 } | 1656 } |
| 1657 else if (PySlice_Check(item)) { | 1657 else if (PySlice_Check(item)) { |
| 1658 if (PySlice_GetIndicesEx((PySliceObject *)item, | 1658 if (PySlice_GetIndicesEx((PySliceObject *)item, |
| 1659 Py_SIZE(self), &start, &stop, | 1659 Py_SIZE(self), &start, &stop, |
| 1660 &step, &slicelength) < 0) { | 1660 &step, &slicelength) < 0) { |
| 1661 return -1; | 1661 return -1; |
| 1662 } | 1662 } |
| 1663 } | 1663 } |
| 1664 else { | 1664 else { |
| 1665 PyErr_SetString(PyExc_TypeError, | 1665 PyErr_SetString(PyExc_TypeError, |
| 1666 "array indices must be integer"); | 1666 "array indices must be integer"); |
| 1667 return -1; | 1667 return -1; |
| 1668 } | 1668 } |
| 1669 if (value == NULL) { | 1669 if (value == NULL) { |
| 1670 other = NULL; | 1670 other = NULL; |
| 1671 needed = 0; | 1671 needed = 0; |
| 1672 } | 1672 } |
| 1673 else if (array_Check(value)) { | 1673 else if (array_Check(value)) { |
| 1674 other = (arrayobject *)value; | 1674 other = (arrayobject *)value; |
| 1675 needed = Py_SIZE(other); | 1675 needed = Py_SIZE(other); |
| 1676 if (self == other) { | 1676 if (self == other) { |
| 1677 /* Special case "self[i:j] = self" -- copy self first */ | 1677 /* Special case "self[i:j] = self" -- copy self first */ |
| 1678 int ret; | 1678 int ret; |
| 1679 value = array_slice(other, 0, needed); | 1679 value = array_slice(other, 0, needed); |
| 1680 if (value == NULL) | 1680 if (value == NULL) |
| 1681 return -1; | 1681 return -1; |
| 1682 ret = array_ass_subscr(self, item, value); | 1682 ret = array_ass_subscr(self, item, value); |
| 1683 Py_DECREF(value); | 1683 Py_DECREF(value); |
| 1684 return ret; | 1684 return ret; |
| 1685 } | 1685 } |
| 1686 if (other->ob_descr != self->ob_descr) { | 1686 if (other->ob_descr != self->ob_descr) { |
| 1687 PyErr_BadArgument(); | 1687 PyErr_BadArgument(); |
| 1688 return -1; | 1688 return -1; |
| 1689 } | 1689 } |
| 1690 } | 1690 } |
| 1691 else { | 1691 else { |
| 1692 PyErr_Format(PyExc_TypeError, | 1692 PyErr_Format(PyExc_TypeError, |
| 1693 "can only assign array (not \"%.200s\") to array slice", | 1693 "can only assign array (not \"%.200s\") to array slice", |
| 1694 Py_TYPE(value)->tp_name); | 1694 Py_TYPE(value)->tp_name); |
| 1695 return -1; | 1695 return -1; |
| 1696 } | 1696 } |
| 1697 itemsize = self->ob_descr->itemsize; | 1697 itemsize = self->ob_descr->itemsize; |
| 1698 /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */ | 1698 /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */ |
| 1699 if ((step > 0 && stop < start) || | 1699 if ((step > 0 && stop < start) || |
| 1700 (step < 0 && stop > start)) | 1700 (step < 0 && stop > start)) |
| 1701 stop = start; | 1701 stop = start; |
| 1702 | 1702 |
| 1703 /* Issue #4509: If the array has exported buffers and the slice | 1703 /* Issue #4509: If the array has exported buffers and the slice |
| 1704 assignment would change the size of the array, fail early to make | 1704 assignment would change the size of the array, fail early to make |
| 1705 sure we don't modify it. */ | 1705 sure we don't modify it. */ |
| 1706 if ((needed == 0 || slicelength != needed) && self->ob_exports > 0) { | 1706 if ((needed == 0 || slicelength != needed) && self->ob_exports > 0) { |
| 1707 PyErr_SetString(PyExc_BufferError, | 1707 PyErr_SetString(PyExc_BufferError, |
| 1708 "cannot resize an array that is exporting buffers"); | 1708 "cannot resize an array that is exporting buffers"); |
| 1709 return -1; | 1709 return -1; |
| 1710 } | 1710 } |
| 1711 | 1711 |
| 1712 if (step == 1) { | 1712 if (step == 1) { |
| 1713 if (slicelength > needed) { | 1713 if (slicelength > needed) { |
| 1714 memmove(self->ob_item + (start + needed) * itemsize, | 1714 memmove(self->ob_item + (start + needed) * itemsize, |
| 1715 self->ob_item + stop * itemsize, | 1715 self->ob_item + stop * itemsize, |
| 1716 (Py_SIZE(self) - stop) * itemsize); | 1716 (Py_SIZE(self) - stop) * itemsize); |
| 1717 if (array_resize(self, Py_SIZE(self) + | 1717 if (array_resize(self, Py_SIZE(self) + |
| 1718 needed - slicelength) < 0) | 1718 needed - slicelength) < 0) |
| 1719 return -1; | 1719 return -1; |
| 1720 } | 1720 } |
| 1721 else if (slicelength < needed) { | 1721 else if (slicelength < needed) { |
| 1722 if (array_resize(self, Py_SIZE(self) + | 1722 if (array_resize(self, Py_SIZE(self) + |
| 1723 needed - slicelength) < 0) | 1723 needed - slicelength) < 0) |
| 1724 return -1; | 1724 return -1; |
| 1725 memmove(self->ob_item + (start + needed) * itemsize, | 1725 memmove(self->ob_item + (start + needed) * itemsize, |
| 1726 self->ob_item + stop * itemsize, | 1726 self->ob_item + stop * itemsize, |
| 1727 (Py_SIZE(self) - start - needed) * itemsize); | 1727 (Py_SIZE(self) - start - needed) * itemsize); |
| 1728 } | 1728 } |
| 1729 if (needed > 0) | 1729 if (needed > 0) |
| 1730 memcpy(self->ob_item + start * itemsize, | 1730 memcpy(self->ob_item + start * itemsize, |
| 1731 other->ob_item, needed * itemsize); | 1731 other->ob_item, needed * itemsize); |
| 1732 return 0; | 1732 return 0; |
| 1733 } | 1733 } |
| 1734 else if (needed == 0) { | 1734 else if (needed == 0) { |
| 1735 /* Delete slice */ | 1735 /* Delete slice */ |
| 1736 size_t cur; | 1736 size_t cur; |
| 1737 Py_ssize_t i; | 1737 Py_ssize_t i; |
| 1738 | 1738 |
| 1739 if (step < 0) { | 1739 if (step < 0) { |
| 1740 stop = start + 1; | 1740 stop = start + 1; |
| 1741 start = stop + step * (slicelength - 1) - 1; | 1741 start = stop + step * (slicelength - 1) - 1; |
| 1742 step = -step; | 1742 step = -step; |
| 1743 } | 1743 } |
| 1744 for (cur = start, i = 0; i < slicelength; | 1744 for (cur = start, i = 0; i < slicelength; |
| 1745 cur += step, i++) { | 1745 cur += step, i++) { |
| 1746 Py_ssize_t lim = step - 1; | 1746 Py_ssize_t lim = step - 1; |
| 1747 | 1747 |
| 1748 if (cur + step >= (size_t)Py_SIZE(self)) | 1748 if (cur + step >= (size_t)Py_SIZE(self)) |
| 1749 lim = Py_SIZE(self) - cur - 1; | 1749 lim = Py_SIZE(self) - cur - 1; |
| 1750 memmove(self->ob_item + (cur - i) * itemsize, | 1750 memmove(self->ob_item + (cur - i) * itemsize, |
| 1751 self->ob_item + (cur + 1) * itemsize, | 1751 self->ob_item + (cur + 1) * itemsize, |
| 1752 lim * itemsize); | 1752 lim * itemsize); |
| 1753 } | 1753 } |
| 1754 cur = start + slicelength * step; | 1754 cur = start + slicelength * step; |
| 1755 if (cur < (size_t)Py_SIZE(self)) { | 1755 if (cur < (size_t)Py_SIZE(self)) { |
| 1756 memmove(self->ob_item + (cur-slicelength) * itemsize, | 1756 memmove(self->ob_item + (cur-slicelength) * itemsize, |
| 1757 self->ob_item + cur * itemsize, | 1757 self->ob_item + cur * itemsize, |
| 1758 (Py_SIZE(self) - cur) * itemsize); | 1758 (Py_SIZE(self) - cur) * itemsize); |
| 1759 } | 1759 } |
| 1760 if (array_resize(self, Py_SIZE(self) - slicelength) < 0) | 1760 if (array_resize(self, Py_SIZE(self) - slicelength) < 0) |
| 1761 return -1; | 1761 return -1; |
| 1762 return 0; | 1762 return 0; |
| 1763 } | 1763 } |
| 1764 else { | 1764 else { |
| 1765 Py_ssize_t cur, i; | 1765 Py_ssize_t cur, i; |
| 1766 | 1766 |
| 1767 if (needed != slicelength) { | 1767 if (needed != slicelength) { |
| 1768 PyErr_Format(PyExc_ValueError, | 1768 PyErr_Format(PyExc_ValueError, |
| 1769 "attempt to assign array of size %zd " | 1769 "attempt to assign array of size %zd " |
| 1770 "to extended slice of size %zd", | 1770 "to extended slice of size %zd", |
| 1771 needed, slicelength); | 1771 needed, slicelength); |
| 1772 return -1; | 1772 return -1; |
| 1773 } | 1773 } |
| 1774 for (cur = start, i = 0; i < slicelength; | 1774 for (cur = start, i = 0; i < slicelength; |
| 1775 cur += step, i++) { | 1775 cur += step, i++) { |
| 1776 memcpy(self->ob_item + cur * itemsize, | 1776 memcpy(self->ob_item + cur * itemsize, |
| 1777 other->ob_item + i * itemsize, | 1777 other->ob_item + i * itemsize, |
| 1778 itemsize); | 1778 itemsize); |
| 1779 } | 1779 } |
| 1780 return 0; | 1780 return 0; |
| 1781 } | 1781 } |
| 1782 } | 1782 } |
| 1783 | 1783 |
| 1784 static PyMappingMethods array_as_mapping = { | 1784 static PyMappingMethods array_as_mapping = { |
| 1785 (lenfunc)array_length, | 1785 (lenfunc)array_length, |
| 1786 (binaryfunc)array_subscr, | 1786 (binaryfunc)array_subscr, |
| 1787 (objobjargproc)array_ass_subscr | 1787 (objobjargproc)array_ass_subscr |
| 1788 }; | 1788 }; |
| 1789 | 1789 |
| 1790 static const void *emptybuf = ""; | 1790 static const void *emptybuf = ""; |
| 1791 | 1791 |
| 1792 | 1792 |
| 1793 static int | 1793 static int |
| 1794 array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags) | 1794 array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags) |
| 1795 { | 1795 { |
| 1796 if (view==NULL) goto finish; | 1796 if (view==NULL) goto finish; |
| 1797 | 1797 |
| 1798 view->buf = (void *)self->ob_item; | 1798 view->buf = (void *)self->ob_item; |
| 1799 view->obj = (PyObject*)self; | 1799 view->obj = (PyObject*)self; |
| 1800 Py_INCREF(self); | 1800 Py_INCREF(self); |
| 1801 if (view->buf == NULL) | 1801 if (view->buf == NULL) |
| 1802 view->buf = (void *)emptybuf; | 1802 view->buf = (void *)emptybuf; |
| 1803 view->len = (Py_SIZE(self)) * self->ob_descr->itemsize; | 1803 view->len = (Py_SIZE(self)) * self->ob_descr->itemsize; |
| 1804 view->readonly = 0; | 1804 view->readonly = 0; |
| 1805 view->ndim = 1; | 1805 view->ndim = 1; |
| 1806 view->itemsize = self->ob_descr->itemsize; | 1806 view->itemsize = self->ob_descr->itemsize; |
| 1807 view->suboffsets = NULL; | 1807 view->suboffsets = NULL; |
| 1808 view->shape = NULL; | 1808 view->shape = NULL; |
| 1809 if ((flags & PyBUF_ND)==PyBUF_ND) { | 1809 if ((flags & PyBUF_ND)==PyBUF_ND) { |
| 1810 view->shape = &((Py_SIZE(self))); | 1810 view->shape = &((Py_SIZE(self))); |
| 1811 } | 1811 } |
| 1812 view->strides = NULL; | 1812 view->strides = NULL; |
| 1813 if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES) | 1813 if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES) |
| 1814 view->strides = &(view->itemsize); | 1814 view->strides = &(view->itemsize); |
| 1815 view->format = NULL; | 1815 view->format = NULL; |
| 1816 view->internal = NULL; | 1816 view->internal = NULL; |
| 1817 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) { | 1817 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) { |
| 1818 view->format = self->ob_descr->formats; | 1818 view->format = self->ob_descr->formats; |
| 1819 #ifdef Py_UNICODE_WIDE | 1819 #ifdef Py_UNICODE_WIDE |
| 1820 if (self->ob_descr->typecode == 'u') { | 1820 if (self->ob_descr->typecode == 'u') { |
| 1821 view->format = "w"; | 1821 view->format = "w"; |
| 1822 } | 1822 } |
| 1823 #endif | 1823 #endif |
| 1824 } | 1824 } |
| 1825 | 1825 |
| 1826 finish: | 1826 finish: |
| 1827 self->ob_exports++; | 1827 self->ob_exports++; |
| 1828 return 0; | 1828 return 0; |
| 1829 } | 1829 } |
| 1830 | 1830 |
| 1831 static void | 1831 static void |
| 1832 array_buffer_relbuf(arrayobject *self, Py_buffer *view) | 1832 array_buffer_relbuf(arrayobject *self, Py_buffer *view) |
| 1833 { | 1833 { |
| 1834 self->ob_exports--; | 1834 self->ob_exports--; |
| 1835 } | 1835 } |
| 1836 | 1836 |
| 1837 static PySequenceMethods array_as_sequence = { | 1837 static PySequenceMethods array_as_sequence = { |
| 1838 (lenfunc)array_length, /*sq_length*/ | 1838 (lenfunc)array_length, /*sq_length*/ |
| 1839 (binaryfunc)array_concat, /*sq_concat*/ | 1839 (binaryfunc)array_concat, /*sq_concat*/ |
| 1840 (ssizeargfunc)array_repeat, /*sq_repeat*/ | 1840 (ssizeargfunc)array_repeat, /*sq_repeat*/ |
| 1841 (ssizeargfunc)array_item, /*sq_item*/ | 1841 (ssizeargfunc)array_item, /*sq_item*/ |
| 1842 0, /*sq_slice*/ | 1842 0, /*sq_slice*/ |
| 1843 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/ | 1843 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/ |
| 1844 0, /*sq_ass_slice*/ | 1844 0, /*sq_ass_slice*/ |
| 1845 (objobjproc)array_contains, /*sq_contains*/ | 1845 (objobjproc)array_contains, /*sq_contains*/ |
| 1846 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/ | 1846 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/ |
| 1847 (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/ | 1847 (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/ |
| 1848 }; | 1848 }; |
| 1849 | 1849 |
| 1850 static PyBufferProcs array_as_buffer = { | 1850 static PyBufferProcs array_as_buffer = { |
| 1851 (getbufferproc)array_buffer_getbuf, | 1851 (getbufferproc)array_buffer_getbuf, |
| 1852 (releasebufferproc)array_buffer_relbuf | 1852 (releasebufferproc)array_buffer_relbuf |
| 1853 }; | 1853 }; |
| 1854 | 1854 |
| 1855 static PyObject * | 1855 static PyObject * |
| 1856 array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | 1856 array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1857 { | 1857 { |
| 1858 int c; | 1858 int c; |
| 1859 PyObject *initial = NULL, *it = NULL; | 1859 PyObject *initial = NULL, *it = NULL; |
| 1860 struct arraydescr *descr; | 1860 struct arraydescr *descr; |
| 1861 | 1861 |
| 1862 if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds)) | 1862 if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds)) |
| 1863 return NULL; | 1863 return NULL; |
| 1864 | 1864 |
| 1865 if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial)) | 1865 if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial)) |
| 1866 return NULL; | 1866 return NULL; |
| 1867 | 1867 |
| 1868 if (!(initial == NULL || PyList_Check(initial) | 1868 if (!(initial == NULL || PyList_Check(initial) |
| 1869 || PyByteArray_Check(initial) | 1869 || PyByteArray_Check(initial) |
| 1870 || PyBytes_Check(initial) | 1870 || PyBytes_Check(initial) |
| 1871 || PyTuple_Check(initial) | 1871 || PyTuple_Check(initial) |
| 1872 || ((c=='u') && PyUnicode_Check(initial)))) { | 1872 || ((c=='u') && PyUnicode_Check(initial)))) { |
| 1873 it = PyObject_GetIter(initial); | 1873 it = PyObject_GetIter(initial); |
| 1874 if (it == NULL) | 1874 if (it == NULL) |
| 1875 return NULL; | 1875 return NULL; |
| 1876 /* We set initial to NULL so that the subsequent code | 1876 /* We set initial to NULL so that the subsequent code |
| 1877 will create an empty array of the appropriate type | 1877 will create an empty array of the appropriate type |
| 1878 and afterwards we can use array_iter_extend to populate | 1878 and afterwards we can use array_iter_extend to populate |
| 1879 the array. | 1879 the array. |
| 1880 */ | 1880 */ |
| 1881 initial = NULL; | 1881 initial = NULL; |
| 1882 } | 1882 } |
| 1883 for (descr = descriptors; descr->typecode != '\0'; descr++) { | 1883 for (descr = descriptors; descr->typecode != '\0'; descr++) { |
| 1884 if (descr->typecode == c) { | 1884 if (descr->typecode == c) { |
| 1885 PyObject *a; | 1885 PyObject *a; |
| 1886 Py_ssize_t len; | 1886 Py_ssize_t len; |
| 1887 | 1887 |
| 1888 if (initial == NULL || !(PyList_Check(initial) | 1888 if (initial == NULL || !(PyList_Check(initial) |
| 1889 || PyTuple_Check(initial))) | 1889 || PyTuple_Check(initial))) |
| 1890 len = 0; | 1890 len = 0; |
| 1891 else | 1891 else |
| 1892 len = PySequence_Size(initial); | 1892 len = PySequence_Size(initial); |
| 1893 | 1893 |
| 1894 a = newarrayobject(type, len, descr); | 1894 a = newarrayobject(type, len, descr); |
| 1895 if (a == NULL) | 1895 if (a == NULL) |
| 1896 return NULL; | 1896 return NULL; |
| 1897 | 1897 |
| 1898 if (len > 0) { | 1898 if (len > 0) { |
| 1899 Py_ssize_t i; | 1899 Py_ssize_t i; |
| 1900 for (i = 0; i < len; i++) { | 1900 for (i = 0; i < len; i++) { |
| 1901 PyObject *v = | 1901 PyObject *v = |
| 1902 PySequence_GetItem(initial, i); | 1902 PySequence_GetItem(initial, i); |
| 1903 if (v == NULL) { | 1903 if (v == NULL) { |
| 1904 Py_DECREF(a); | 1904 Py_DECREF(a); |
| 1905 return NULL; | 1905 return NULL; |
| 1906 } | 1906 } |
| 1907 if (setarrayitem(a, i, v) != 0) { | 1907 if (setarrayitem(a, i, v) != 0) { |
| 1908 Py_DECREF(v); | 1908 Py_DECREF(v); |
| 1909 Py_DECREF(a); | 1909 Py_DECREF(a); |
| 1910 return NULL; | 1910 return NULL; |
| 1911 } | 1911 } |
| 1912 Py_DECREF(v); | 1912 Py_DECREF(v); |
| 1913 } | 1913 } |
| 1914 } | 1914 } |
| 1915 else if (initial != NULL && (PyByteArray_Check(initial) || | 1915 else if (initial != NULL && (PyByteArray_Check(initial) || |
| 1916 PyBytes_Check(initial))) { | 1916 PyBytes_Check(initial))) { |
| 1917 PyObject *t_initial, *v; | 1917 PyObject *t_initial, *v; |
| 1918 t_initial = PyTuple_Pack(1, initial); | 1918 t_initial = PyTuple_Pack(1, initial); |
| 1919 if (t_initial == NULL) { | 1919 if (t_initial == NULL) { |
| 1920 Py_DECREF(a); | 1920 Py_DECREF(a); |
| 1921 return NULL; | 1921 return NULL; |
| 1922 } | 1922 } |
| 1923 v = array_fromstring((arrayobject *)a, | 1923 v = array_fromstring((arrayobject *)a, |
| 1924 t_initial); | 1924 t_initial); |
| 1925 Py_DECREF(t_initial); | 1925 Py_DECREF(t_initial); |
| 1926 if (v == NULL) { | 1926 if (v == NULL) { |
| 1927 Py_DECREF(a); | 1927 Py_DECREF(a); |
| 1928 return NULL; | 1928 return NULL; |
| 1929 } | 1929 } |
| 1930 Py_DECREF(v); | 1930 Py_DECREF(v); |
| 1931 } | 1931 } |
| 1932 else if (initial != NULL && PyUnicode_Check(initial)) { | 1932 else if (initial != NULL && PyUnicode_Check(initial)) { |
| 1933 Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial); | 1933 Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial); |
| 1934 if (n > 0) { | 1934 if (n > 0) { |
| 1935 arrayobject *self = (arrayobject *)a; | 1935 arrayobject *self = (arrayobject *)a; |
| 1936 char *item = self->ob_item; | 1936 char *item = self->ob_item; |
| 1937 item = (char *)PyMem_Realloc(item, n); | 1937 item = (char *)PyMem_Realloc(item, n); |
| 1938 if (item == NULL) { | 1938 if (item == NULL) { |
| 1939 PyErr_NoMemory(); | 1939 PyErr_NoMemory(); |
| 1940 Py_DECREF(a); | 1940 Py_DECREF(a); |
| 1941 return NULL; | 1941 return NULL; |
| 1942 } | 1942 } |
| 1943 self->ob_item = item; | 1943 self->ob_item = item; |
| 1944 Py_SIZE(self) = n / sizeof(Py_UNICODE); | 1944 Py_SIZE(self) = n / sizeof(Py_UNICODE); |
| 1945 memcpy(item, PyUnicode_AS_DATA(initial), n); | 1945 memcpy(item, PyUnicode_AS_DATA(initial), n); |
| 1946 self->allocated = Py_SIZE(self); | 1946 self->allocated = Py_SIZE(self); |
| 1947 } | 1947 } |
| 1948 } | 1948 } |
| 1949 if (it != NULL) { | 1949 if (it != NULL) { |
| 1950 if (array_iter_extend((arrayobject *)a, it) == -1) { | 1950 if (array_iter_extend((arrayobject *)a, it) == -1) { |
| 1951 Py_DECREF(it); | 1951 Py_DECREF(it); |
| 1952 Py_DECREF(a); | 1952 Py_DECREF(a); |
| 1953 return NULL; | 1953 return NULL; |
| 1954 } | 1954 } |
| 1955 Py_DECREF(it); | 1955 Py_DECREF(it); |
| 1956 } | 1956 } |
| 1957 return a; | 1957 return a; |
| 1958 } | 1958 } |
| 1959 } | 1959 } |
| 1960 PyErr_SetString(PyExc_ValueError, | 1960 PyErr_SetString(PyExc_ValueError, |
| 1961 "bad typecode (must be b, B, u, h, H, i, I, l, L, f or d)"); | 1961 "bad typecode (must be b, B, u, h, H, i, I, l, L, f or d)"); |
| 1962 return NULL; | 1962 return NULL; |
| 1963 } | 1963 } |
| 1964 | 1964 |
| 1965 | 1965 |
| 1966 PyDoc_STRVAR(module_doc, | 1966 PyDoc_STRVAR(module_doc, |
| 1967 "This module defines an object type which can efficiently represent\n\ | 1967 "This module defines an object type which can efficiently represent\n\ |
| 2028 "); | 2028 "); |
| 2029 | 2029 |
| 2030 static PyObject *array_iter(arrayobject *ao); | 2030 static PyObject *array_iter(arrayobject *ao); |
| 2031 | 2031 |
| 2032 static PyTypeObject Arraytype = { | 2032 static PyTypeObject Arraytype = { |
| 2033 PyVarObject_HEAD_INIT(NULL, 0) | 2033 PyVarObject_HEAD_INIT(NULL, 0) |
| 2034 "array.array", | 2034 "array.array", |
| 2035 sizeof(arrayobject), | 2035 sizeof(arrayobject), |
| 2036 0, | 2036 0, |
| 2037 (destructor)array_dealloc, /* tp_dealloc */ | 2037 (destructor)array_dealloc, /* tp_dealloc */ |
| 2038 0, /* tp_print */ | 2038 0, /* tp_print */ |
| 2039 0, /* tp_getattr */ | 2039 0, /* tp_getattr */ |
| 2040 0, /* tp_setattr */ | 2040 0, /* tp_setattr */ |
| 2041 0, /* tp_reserved */ | 2041 0, /* tp_reserved */ |
| 2042 (reprfunc)array_repr, /* tp_repr */ | 2042 (reprfunc)array_repr, /* tp_repr */ |
| 2043 0, /* tp_as_number*/ | 2043 0, /* tp_as_number*/ |
| 2044 &array_as_sequence, /* tp_as_sequence*/ | 2044 &array_as_sequence, /* tp_as_sequence*/ |
| 2045 &array_as_mapping, /* tp_as_mapping*/ | 2045 &array_as_mapping, /* tp_as_mapping*/ |
| 2046 0, /* tp_hash */ | 2046 0, /* tp_hash */ |
| 2047 0, /* tp_call */ | 2047 0, /* tp_call */ |
| 2048 0, /* tp_str */ | 2048 0, /* tp_str */ |
| 2049 PyObject_GenericGetAttr, /* tp_getattro */ | 2049 PyObject_GenericGetAttr, /* tp_getattro */ |
| 2050 0, /* tp_setattro */ | 2050 0, /* tp_setattro */ |
| 2051 &array_as_buffer, /* tp_as_buffer*/ | 2051 &array_as_buffer, /* tp_as_buffer*/ |
| 2052 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ | 2052 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2053 arraytype_doc, /* tp_doc */ | 2053 arraytype_doc, /* tp_doc */ |
| 2054 0, /* tp_traverse */ | 2054 0, /* tp_traverse */ |
| 2055 0, /* tp_clear */ | 2055 0, /* tp_clear */ |
| 2056 array_richcompare, /* tp_richcompare */ | 2056 array_richcompare, /* tp_richcompare */ |
| 2057 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */ | 2057 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */ |
| 2058 (getiterfunc)array_iter, /* tp_iter */ | 2058 (getiterfunc)array_iter, /* tp_iter */ |
| 2059 0, /* tp_iternext */ | 2059 0, /* tp_iternext */ |
| 2060 array_methods, /* tp_methods */ | 2060 array_methods, /* tp_methods */ |
| 2061 0, /* tp_members */ | 2061 0, /* tp_members */ |
| 2062 array_getsets, /* tp_getset */ | 2062 array_getsets, /* tp_getset */ |
| 2063 0, /* tp_base */ | 2063 0, /* tp_base */ |
| 2064 0, /* tp_dict */ | 2064 0, /* tp_dict */ |
| 2065 0, /* tp_descr_get */ | 2065 0, /* tp_descr_get */ |
| 2066 0, /* tp_descr_set */ | 2066 0, /* tp_descr_set */ |
| 2067 0, /* tp_dictoffset */ | 2067 0, /* tp_dictoffset */ |
| 2068 0, /* tp_init */ | 2068 0, /* tp_init */ |
| 2069 PyType_GenericAlloc, /* tp_alloc */ | 2069 PyType_GenericAlloc, /* tp_alloc */ |
| 2070 array_new, /* tp_new */ | 2070 array_new, /* tp_new */ |
| 2071 PyObject_Del, /* tp_free */ | 2071 PyObject_Del, /* tp_free */ |
| 2072 }; | 2072 }; |
| 2073 | 2073 |
| 2074 | 2074 |
| 2075 /*********************** Array Iterator **************************/ | 2075 /*********************** Array Iterator **************************/ |
| 2076 | 2076 |
| 2077 typedef struct { | 2077 typedef struct { |
| 2078 PyObject_HEAD | 2078 PyObject_HEAD |
| 2079 Py_ssize_t index; | 2079 Py_ssize_t index; |
| 2080 arrayobject *ao; | 2080 arrayobject *ao; |
| 2081 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t); | 2081 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t); |
| 2082 } arrayiterobject; | 2082 } arrayiterobject; |
| 2083 | 2083 |
| 2084 static PyTypeObject PyArrayIter_Type; | 2084 static PyTypeObject PyArrayIter_Type; |
| 2085 | 2085 |
| 2086 #define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type) | 2086 #define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type) |
| 2087 | 2087 |
| 2088 static PyObject * | 2088 static PyObject * |
| 2089 array_iter(arrayobject *ao) | 2089 array_iter(arrayobject *ao) |
| 2090 { | 2090 { |
| 2091 arrayiterobject *it; | 2091 arrayiterobject *it; |
| 2092 | 2092 |
| 2093 if (!array_Check(ao)) { | 2093 if (!array_Check(ao)) { |
| 2094 PyErr_BadInternalCall(); | 2094 PyErr_BadInternalCall(); |
| 2095 return NULL; | 2095 return NULL; |
| 2096 } | 2096 } |
| 2097 | 2097 |
| 2098 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type); | 2098 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type); |
| 2099 if (it == NULL) | 2099 if (it == NULL) |
| 2100 return NULL; | 2100 return NULL; |
| 2101 | 2101 |
| 2102 Py_INCREF(ao); | 2102 Py_INCREF(ao); |
| 2103 it->ao = ao; | 2103 it->ao = ao; |
| 2104 it->index = 0; | 2104 it->index = 0; |
| 2105 it->getitem = ao->ob_descr->getitem; | 2105 it->getitem = ao->ob_descr->getitem; |
| 2106 PyObject_GC_Track(it); | 2106 PyObject_GC_Track(it); |
| 2107 return (PyObject *)it; | 2107 return (PyObject *)it; |
| 2108 } | 2108 } |
| 2109 | 2109 |
| 2110 static PyObject * | 2110 static PyObject * |
| 2111 arrayiter_next(arrayiterobject *it) | 2111 arrayiter_next(arrayiterobject *it) |
| 2112 { | 2112 { |
| 2113 assert(PyArrayIter_Check(it)); | 2113 assert(PyArrayIter_Check(it)); |
| 2114 if (it->index < Py_SIZE(it->ao)) | 2114 if (it->index < Py_SIZE(it->ao)) |
| 2115 return (*it->getitem)(it->ao, it->index++); | 2115 return (*it->getitem)(it->ao, it->index++); |
| 2116 return NULL; | 2116 return NULL; |
| 2117 } | 2117 } |
| 2118 | 2118 |
| 2119 static void | 2119 static void |
| 2120 arrayiter_dealloc(arrayiterobject *it) | 2120 arrayiter_dealloc(arrayiterobject *it) |
| 2121 { | 2121 { |
| 2122 PyObject_GC_UnTrack(it); | 2122 PyObject_GC_UnTrack(it); |
| 2123 Py_XDECREF(it->ao); | 2123 Py_XDECREF(it->ao); |
| 2124 PyObject_GC_Del(it); | 2124 PyObject_GC_Del(it); |
| 2125 } | 2125 } |
| 2126 | 2126 |
| 2127 static int | 2127 static int |
| 2128 arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg) | 2128 arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg) |
| 2129 { | 2129 { |
| 2130 Py_VISIT(it->ao); | 2130 Py_VISIT(it->ao); |
| 2131 return 0; | 2131 return 0; |
| 2132 } | 2132 } |
| 2133 | 2133 |
| 2134 static PyTypeObject PyArrayIter_Type = { | 2134 static PyTypeObject PyArrayIter_Type = { |
| 2135 PyVarObject_HEAD_INIT(NULL, 0) | 2135 PyVarObject_HEAD_INIT(NULL, 0) |
| 2136 "arrayiterator", /* tp_name */ | 2136 "arrayiterator", /* tp_name */ |
| 2137 sizeof(arrayiterobject), /* tp_basicsize */ | 2137 sizeof(arrayiterobject), /* tp_basicsize */ |
| 2138 0, /* tp_itemsize */ | 2138 0, /* tp_itemsize */ |
| 2139 /* methods */ | 2139 /* methods */ |
| 2140 (destructor)arrayiter_dealloc, /* tp_dealloc */ | 2140 (destructor)arrayiter_dealloc, /* tp_dealloc */ |
| 2141 0, /* tp_print */ | 2141 0, /* tp_print */ |
| 2142 0, /* tp_getattr */ | 2142 0, /* tp_getattr */ |
| 2143 0, /* tp_setattr */ | 2143 0, /* tp_setattr */ |
| 2144 0, /* tp_reserved */ | 2144 0, /* tp_reserved */ |
| 2145 0, /* tp_repr */ | 2145 0, /* tp_repr */ |
| 2146 0, /* tp_as_number */ | 2146 0, /* tp_as_number */ |
| 2147 0, /* tp_as_sequence */ | 2147 0, /* tp_as_sequence */ |
| 2148 0, /* tp_as_mapping */ | 2148 0, /* tp_as_mapping */ |
| 2149 0, /* tp_hash */ | 2149 0, /* tp_hash */ |
| 2150 0, /* tp_call */ | 2150 0, /* tp_call */ |
| 2151 0, /* tp_str */ | 2151 0, /* tp_str */ |
| 2152 PyObject_GenericGetAttr, /* tp_getattro */ | 2152 PyObject_GenericGetAttr, /* tp_getattro */ |
| 2153 0, /* tp_setattro */ | 2153 0, /* tp_setattro */ |
| 2154 0, /* tp_as_buffer */ | 2154 0, /* tp_as_buffer */ |
| 2155 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ | 2155 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 2156 0, /* tp_doc */ | 2156 0, /* tp_doc */ |
| 2157 (traverseproc)arrayiter_traverse, /* tp_traverse */ | 2157 (traverseproc)arrayiter_traverse, /* tp_traverse */ |
| 2158 0, /* tp_clear */ | 2158 0, /* tp_clear */ |
| 2159 0, /* tp_richcompare */ | 2159 0, /* tp_richcompare */ |
| 2160 0, /* tp_weaklistoffset */ | 2160 0, /* tp_weaklistoffset */ |
| 2161 PyObject_SelfIter, /* tp_iter */ | 2161 PyObject_SelfIter, /* tp_iter */ |
| 2162 (iternextfunc)arrayiter_next, /* tp_iternext */ | 2162 (iternextfunc)arrayiter_next, /* tp_iternext */ |
| 2163 0, /* tp_methods */ | 2163 0, /* tp_methods */ |
| 2164 }; | 2164 }; |
| 2165 | 2165 |
| 2166 | 2166 |
| 2167 /*********************** Install Module **************************/ | 2167 /*********************** Install Module **************************/ |
| 2168 | 2168 |
| 2170 static PyMethodDef a_methods[] = { | 2170 static PyMethodDef a_methods[] = { |
| 2171 {NULL, NULL, 0, NULL} /* Sentinel */ | 2171 {NULL, NULL, 0, NULL} /* Sentinel */ |
| 2172 }; | 2172 }; |
| 2173 | 2173 |
| 2174 static struct PyModuleDef arraymodule = { | 2174 static struct PyModuleDef arraymodule = { |
| 2175 PyModuleDef_HEAD_INIT, | 2175 PyModuleDef_HEAD_INIT, |
| 2176 "array", | 2176 "array", |
| 2177 module_doc, | 2177 module_doc, |
| 2178 -1, | 2178 -1, |
| 2179 a_methods, | 2179 a_methods, |
| 2180 NULL, | 2180 NULL, |
| 2181 NULL, | 2181 NULL, |
| 2182 NULL, | 2182 NULL, |
| 2183 NULL | 2183 NULL |
| 2184 }; | 2184 }; |
| 2185 | 2185 |
| 2186 | 2186 |
| 2187 PyMODINIT_FUNC | 2187 PyMODINIT_FUNC |
| 2188 PyInit_array(void) | 2188 PyInit_array(void) |
| 2189 { | 2189 { |
| 2190 PyObject *m; | 2190 PyObject *m; |
| 2191 PyObject *typecodes; | 2191 PyObject *typecodes; |
| 2192 Py_ssize_t size = 0; | 2192 Py_ssize_t size = 0; |
| 2193 register Py_UNICODE *p; | 2193 register Py_UNICODE *p; |
| 2194 struct arraydescr *descr; | 2194 struct arraydescr *descr; |
| 2195 | 2195 |
| 2196 if (PyType_Ready(&Arraytype) < 0) | 2196 if (PyType_Ready(&Arraytype) < 0) |
| 2197 return NULL; | 2197 return NULL; |
| 2198 Py_TYPE(&PyArrayIter_Type) = &PyType_Type; | 2198 Py_TYPE(&PyArrayIter_Type) = &PyType_Type; |
| 2199 m = PyModule_Create(&arraymodule); | 2199 m = PyModule_Create(&arraymodule); |
| 2200 if (m == NULL) | 2200 if (m == NULL) |
| 2201 return NULL; | 2201 return NULL; |
| 2202 | 2202 |
| 2203 Py_INCREF((PyObject *)&Arraytype); | 2203 Py_INCREF((PyObject *)&Arraytype); |
| 2204 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype); | 2204 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype); |
| 2205 Py_INCREF((PyObject *)&Arraytype); | 2205 Py_INCREF((PyObject *)&Arraytype); |
| 2206 PyModule_AddObject(m, "array", (PyObject *)&Arraytype); | 2206 PyModule_AddObject(m, "array", (PyObject *)&Arraytype); |
| 2207 | 2207 |
| 2208 for (descr=descriptors; descr->typecode != '\0'; descr++) { | 2208 for (descr=descriptors; descr->typecode != '\0'; descr++) { |
| 2209 size++; | 2209 size++; |
| 2210 } | 2210 } |
| 2211 | 2211 |
| 2212 typecodes = PyUnicode_FromStringAndSize(NULL, size); | 2212 typecodes = PyUnicode_FromStringAndSize(NULL, size); |
| 2213 p = PyUnicode_AS_UNICODE(typecodes); | 2213 p = PyUnicode_AS_UNICODE(typecodes); |
| 2214 for (descr = descriptors; descr->typecode != '\0'; descr++) { | 2214 for (descr = descriptors; descr->typecode != '\0'; descr++) { |
| 2215 *p++ = (char)descr->typecode; | 2215 *p++ = (char)descr->typecode; |
| 2216 } | 2216 } |
| 2217 | 2217 |
| 2218 PyModule_AddObject(m, "typecodes", (PyObject *)typecodes); | 2218 PyModule_AddObject(m, "typecodes", (PyObject *)typecodes); |
| 2219 | 2219 |
| 2220 if (PyErr_Occurred()) { | 2220 if (PyErr_Occurred()) { |
| 2221 Py_DECREF(m); | 2221 Py_DECREF(m); |
| 2222 m = NULL; | 2222 m = NULL; |
| 2223 } | 2223 } |
| 2224 return m; | 2224 return m; |
| 2225 } | 2225 } |
