Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1 | /* Array object implementation */ |
| 2 | |
| 3 | /* An array is a uniform list -- all items have the same type. |
| 4 | The item type is restricted to simple C types like int or float */ |
| 5 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 6 | #include "Python.h" |
Raymond Hettinger | cb87bc8 | 2004-05-31 00:35:52 +0000 | [diff] [blame] | 7 | #include "structmember.h" |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 0c70954 | 1994-08-19 12:01:32 +0000 | [diff] [blame] | 9 | #ifdef STDC_HEADERS |
| 10 | #include <stddef.h> |
Guido van Rossum | 7f1de83 | 1999-08-27 20:33:52 +0000 | [diff] [blame] | 11 | #else /* !STDC_HEADERS */ |
| 12 | #ifndef DONT_HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 13 | #include <sys/types.h> /* For size_t */ |
Guido van Rossum | 7f1de83 | 1999-08-27 20:33:52 +0000 | [diff] [blame] | 14 | #endif /* DONT_HAVE_SYS_TYPES_H */ |
| 15 | #endif /* !STDC_HEADERS */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 16 | |
| 17 | struct arrayobject; /* Forward */ |
| 18 | |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 19 | /* All possible arraydescr values are defined in the vector "descriptors" |
| 20 | * below. That's defined later because the appropriate get and set |
| 21 | * functions aren't visible yet. |
| 22 | */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 23 | struct arraydescr { |
| 24 | int typecode; |
| 25 | int itemsize; |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 26 | PyObject * (*getitem)(struct arrayobject *, int); |
| 27 | int (*setitem)(struct arrayobject *, int, PyObject *); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | typedef struct arrayobject { |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 31 | PyObject_HEAD |
| 32 | int ob_size; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 33 | char *ob_item; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 34 | int allocated; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 35 | struct arraydescr *ob_descr; |
Raymond Hettinger | cb87bc8 | 2004-05-31 00:35:52 +0000 | [diff] [blame] | 36 | PyObject *weakreflist; /* List of weak references */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 37 | } arrayobject; |
| 38 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 39 | static PyTypeObject Arraytype; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 40 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 41 | #define array_Check(op) PyObject_TypeCheck(op, &Arraytype) |
| 42 | #define array_CheckExact(op) ((op)->ob_type == &Arraytype) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 43 | |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 44 | static int |
| 45 | array_resize(arrayobject *self, int newsize) |
| 46 | { |
| 47 | char *items; |
| 48 | size_t _new_size; |
| 49 | |
| 50 | /* Bypass realloc() when a previous overallocation is large enough |
| 51 | to accommodate the newsize. If the newsize is 16 smaller than the |
| 52 | current size, then proceed with the realloc() to shrink the list. |
| 53 | */ |
| 54 | |
| 55 | if (self->allocated >= newsize && |
| 56 | self->ob_size < newsize + 16 && |
| 57 | self->ob_item != NULL) { |
| 58 | self->ob_size = newsize; |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | /* This over-allocates proportional to the array size, making room |
| 63 | * for additional growth. The over-allocation is mild, but is |
| 64 | * enough to give linear-time amortized behavior over a long |
| 65 | * sequence of appends() in the presence of a poorly-performing |
| 66 | * system realloc(). |
| 67 | * The growth pattern is: 0, 4, 8, 16, 25, 34, 46, 56, 67, 79, ... |
| 68 | * Note, the pattern starts out the same as for lists but then |
| 69 | * grows at a smaller rate so that larger arrays only overallocate |
| 70 | * by about 1/16th -- this is done because arrays are presumed to be more |
| 71 | * memory critical. |
| 72 | */ |
| 73 | |
| 74 | _new_size = (newsize >> 4) + (self->ob_size < 8 ? 3 : 7) + newsize; |
| 75 | items = self->ob_item; |
| 76 | /* XXX The following multiplication and division does not optimize away |
| 77 | like it does for lists since the size is not known at compile time */ |
| 78 | if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize)) |
| 79 | PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize)); |
| 80 | else |
| 81 | items = NULL; |
| 82 | if (items == NULL) { |
| 83 | PyErr_NoMemory(); |
| 84 | return -1; |
| 85 | } |
| 86 | self->ob_item = items; |
| 87 | self->ob_size = newsize; |
| 88 | self->allocated = _new_size; |
| 89 | return 0; |
| 90 | } |
| 91 | |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 92 | /**************************************************************************** |
| 93 | Get and Set functions for each type. |
| 94 | A Get function takes an arrayobject* and an integer index, returning the |
| 95 | array value at that index wrapped in an appropriate PyObject*. |
| 96 | A Set function takes an arrayobject, integer index, and PyObject*; sets |
| 97 | the array value at that index to the raw C data extracted from the PyObject*, |
| 98 | and returns 0 if successful, else nonzero on failure (PyObject* not of an |
| 99 | appropriate type or value). |
| 100 | Note that the basic Get and Set functions do NOT check that the index is |
| 101 | in bounds; that's the responsibility of the caller. |
| 102 | ****************************************************************************/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 103 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 104 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 105 | c_getitem(arrayobject *ap, int i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 106 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 107 | return PyString_FromStringAndSize(&((char *)ap->ob_item)[i], 1); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 111 | c_setitem(arrayobject *ap, int i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 112 | { |
| 113 | char x; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 114 | if (!PyArg_Parse(v, "c;array item must be char", &x)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 115 | return -1; |
| 116 | if (i >= 0) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 117 | ((char *)ap->ob_item)[i] = x; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 121 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 122 | b_getitem(arrayobject *ap, int i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 123 | { |
| 124 | long x = ((char *)ap->ob_item)[i]; |
| 125 | if (x >= 128) |
| 126 | x -= 256; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 127 | return PyInt_FromLong(x); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 131 | b_setitem(arrayobject *ap, int i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 132 | { |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 133 | short x; |
| 134 | /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore |
| 135 | must use the next size up that is signed ('h') and manually do |
| 136 | the overflow checking */ |
| 137 | if (!PyArg_Parse(v, "h;array item must be integer", &x)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 138 | return -1; |
Guido van Rossum | 9f754e0 | 2000-07-01 00:38:19 +0000 | [diff] [blame] | 139 | else if (x < -128) { |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 140 | PyErr_SetString(PyExc_OverflowError, |
| 141 | "signed char is less than minimum"); |
| 142 | return -1; |
| 143 | } |
Guido van Rossum | 9f754e0 | 2000-07-01 00:38:19 +0000 | [diff] [blame] | 144 | else if (x > 127) { |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 145 | PyErr_SetString(PyExc_OverflowError, |
| 146 | "signed char is greater than maximum"); |
| 147 | return -1; |
| 148 | } |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 149 | if (i >= 0) |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 150 | ((char *)ap->ob_item)[i] = (char)x; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 154 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 155 | BB_getitem(arrayobject *ap, int i) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 156 | { |
| 157 | long x = ((unsigned char *)ap->ob_item)[i]; |
| 158 | return PyInt_FromLong(x); |
| 159 | } |
| 160 | |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 161 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 162 | BB_setitem(arrayobject *ap, int i, PyObject *v) |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 163 | { |
| 164 | unsigned char x; |
| 165 | /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */ |
| 166 | if (!PyArg_Parse(v, "b;array item must be integer", &x)) |
| 167 | return -1; |
| 168 | if (i >= 0) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 169 | ((char *)ap->ob_item)[i] = x; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 170 | return 0; |
| 171 | } |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 172 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 173 | #ifdef Py_USING_UNICODE |
| 174 | static PyObject * |
| 175 | u_getitem(arrayobject *ap, int i) |
| 176 | { |
| 177 | return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1); |
| 178 | } |
| 179 | |
| 180 | static int |
| 181 | u_setitem(arrayobject *ap, int i, PyObject *v) |
| 182 | { |
| 183 | Py_UNICODE *p; |
| 184 | int len; |
| 185 | |
| 186 | if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len)) |
| 187 | return -1; |
| 188 | if (len != 1) { |
| 189 | PyErr_SetString(PyExc_TypeError, "array item must be unicode character"); |
| 190 | return -1; |
| 191 | } |
| 192 | if (i >= 0) |
| 193 | ((Py_UNICODE *)ap->ob_item)[i] = p[0]; |
| 194 | return 0; |
| 195 | } |
| 196 | #endif |
| 197 | |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 198 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 199 | h_getitem(arrayobject *ap, int i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 200 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 201 | return PyInt_FromLong((long) ((short *)ap->ob_item)[i]); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 205 | h_setitem(arrayobject *ap, int i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 206 | { |
| 207 | short x; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 208 | /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */ |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 209 | if (!PyArg_Parse(v, "h;array item must be integer", &x)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 210 | return -1; |
| 211 | if (i >= 0) |
| 212 | ((short *)ap->ob_item)[i] = x; |
| 213 | return 0; |
| 214 | } |
| 215 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 216 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 217 | HH_getitem(arrayobject *ap, int i) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 218 | { |
| 219 | return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[i]); |
| 220 | } |
| 221 | |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 222 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 223 | HH_setitem(arrayobject *ap, int i, PyObject *v) |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 224 | { |
| 225 | int x; |
| 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 */ |
| 228 | if (!PyArg_Parse(v, "i;array item must be integer", &x)) |
| 229 | return -1; |
| 230 | else if (x < 0) { |
| 231 | PyErr_SetString(PyExc_OverflowError, |
| 232 | "unsigned short is less than minimum"); |
| 233 | return -1; |
| 234 | } |
| 235 | else if (x > USHRT_MAX) { |
| 236 | PyErr_SetString(PyExc_OverflowError, |
| 237 | "unsigned short is greater than maximum"); |
| 238 | return -1; |
| 239 | } |
| 240 | if (i >= 0) |
| 241 | ((short *)ap->ob_item)[i] = (short)x; |
| 242 | return 0; |
| 243 | } |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 244 | |
| 245 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 246 | i_getitem(arrayobject *ap, int i) |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 247 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 248 | return PyInt_FromLong((long) ((int *)ap->ob_item)[i]); |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 252 | i_setitem(arrayobject *ap, int i, PyObject *v) |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 253 | { |
| 254 | int x; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 255 | /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */ |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 256 | if (!PyArg_Parse(v, "i;array item must be integer", &x)) |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 257 | return -1; |
| 258 | if (i >= 0) |
| 259 | ((int *)ap->ob_item)[i] = x; |
| 260 | return 0; |
| 261 | } |
| 262 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 263 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 264 | II_getitem(arrayobject *ap, int i) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 265 | { |
| 266 | return PyLong_FromUnsignedLong( |
| 267 | (unsigned long) ((unsigned int *)ap->ob_item)[i]); |
| 268 | } |
| 269 | |
| 270 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 271 | II_setitem(arrayobject *ap, int i, PyObject *v) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 272 | { |
| 273 | unsigned long x; |
| 274 | if (PyLong_Check(v)) { |
| 275 | x = PyLong_AsUnsignedLong(v); |
| 276 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 277 | return -1; |
| 278 | } |
| 279 | else { |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 280 | long y; |
| 281 | if (!PyArg_Parse(v, "l;array item must be integer", &y)) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 282 | return -1; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 283 | if (y < 0) { |
| 284 | PyErr_SetString(PyExc_OverflowError, |
| 285 | "unsigned int is less than minimum"); |
| 286 | return -1; |
| 287 | } |
| 288 | x = (unsigned long)y; |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 289 | |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 290 | } |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 291 | if (x > UINT_MAX) { |
| 292 | PyErr_SetString(PyExc_OverflowError, |
| 293 | "unsigned int is greater than maximum"); |
| 294 | return -1; |
| 295 | } |
| 296 | |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 297 | if (i >= 0) |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 298 | ((unsigned int *)ap->ob_item)[i] = (unsigned int)x; |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 303 | l_getitem(arrayobject *ap, int i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 304 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 305 | return PyInt_FromLong(((long *)ap->ob_item)[i]); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 309 | l_setitem(arrayobject *ap, int i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 310 | { |
| 311 | long x; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 312 | if (!PyArg_Parse(v, "l;array item must be integer", &x)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 313 | return -1; |
| 314 | if (i >= 0) |
| 315 | ((long *)ap->ob_item)[i] = x; |
| 316 | return 0; |
| 317 | } |
| 318 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 319 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 320 | LL_getitem(arrayobject *ap, int i) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 321 | { |
| 322 | return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]); |
| 323 | } |
| 324 | |
| 325 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 326 | LL_setitem(arrayobject *ap, int i, PyObject *v) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 327 | { |
| 328 | unsigned long x; |
| 329 | if (PyLong_Check(v)) { |
| 330 | x = PyLong_AsUnsignedLong(v); |
| 331 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 332 | return -1; |
| 333 | } |
| 334 | else { |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 335 | long y; |
| 336 | if (!PyArg_Parse(v, "l;array item must be integer", &y)) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 337 | return -1; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 338 | if (y < 0) { |
| 339 | PyErr_SetString(PyExc_OverflowError, |
| 340 | "unsigned long is less than minimum"); |
| 341 | return -1; |
| 342 | } |
| 343 | x = (unsigned long)y; |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 344 | |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 345 | } |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 346 | if (x > ULONG_MAX) { |
| 347 | PyErr_SetString(PyExc_OverflowError, |
| 348 | "unsigned long is greater than maximum"); |
| 349 | return -1; |
| 350 | } |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 351 | |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 352 | if (i >= 0) |
| 353 | ((unsigned long *)ap->ob_item)[i] = x; |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 358 | f_getitem(arrayobject *ap, int i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 359 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 360 | return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 364 | f_setitem(arrayobject *ap, int i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 365 | { |
| 366 | float x; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 367 | if (!PyArg_Parse(v, "f;array item must be float", &x)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 368 | return -1; |
| 369 | if (i >= 0) |
| 370 | ((float *)ap->ob_item)[i] = x; |
| 371 | return 0; |
| 372 | } |
| 373 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 374 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 375 | d_getitem(arrayobject *ap, int i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 376 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 377 | return PyFloat_FromDouble(((double *)ap->ob_item)[i]); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 381 | d_setitem(arrayobject *ap, int i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 382 | { |
| 383 | double x; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 384 | if (!PyArg_Parse(v, "d;array item must be float", &x)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 385 | return -1; |
| 386 | if (i >= 0) |
| 387 | ((double *)ap->ob_item)[i] = x; |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | /* Description of types */ |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 392 | static struct arraydescr descriptors[] = { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 393 | {'c', sizeof(char), c_getitem, c_setitem}, |
| 394 | {'b', sizeof(char), b_getitem, b_setitem}, |
Guido van Rossum | 1a747f8 | 1997-05-16 16:21:38 +0000 | [diff] [blame] | 395 | {'B', sizeof(char), BB_getitem, BB_setitem}, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 396 | #ifdef Py_USING_UNICODE |
| 397 | {'u', sizeof(Py_UNICODE), u_getitem, u_setitem}, |
| 398 | #endif |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 399 | {'h', sizeof(short), h_getitem, h_setitem}, |
Guido van Rossum | 1a747f8 | 1997-05-16 16:21:38 +0000 | [diff] [blame] | 400 | {'H', sizeof(short), HH_getitem, HH_setitem}, |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 401 | {'i', sizeof(int), i_getitem, i_setitem}, |
Guido van Rossum | 1a747f8 | 1997-05-16 16:21:38 +0000 | [diff] [blame] | 402 | {'I', sizeof(int), II_getitem, II_setitem}, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 403 | {'l', sizeof(long), l_getitem, l_setitem}, |
Guido van Rossum | 1a747f8 | 1997-05-16 16:21:38 +0000 | [diff] [blame] | 404 | {'L', sizeof(long), LL_getitem, LL_setitem}, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 405 | {'f', sizeof(float), f_getitem, f_setitem}, |
| 406 | {'d', sizeof(double), d_getitem, d_setitem}, |
| 407 | {'\0', 0, 0, 0} /* Sentinel */ |
| 408 | }; |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 409 | |
| 410 | /**************************************************************************** |
| 411 | Implementations of array object methods. |
| 412 | ****************************************************************************/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 413 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 414 | static PyObject * |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 415 | newarrayobject(PyTypeObject *type, int size, struct arraydescr *descr) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 416 | { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 417 | arrayobject *op; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 418 | size_t nbytes; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 419 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 420 | if (size < 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 421 | PyErr_BadInternalCall(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 422 | return NULL; |
| 423 | } |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 424 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 425 | nbytes = size * descr->itemsize; |
| 426 | /* Check for overflow */ |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 427 | if (nbytes / descr->itemsize != (size_t)size) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 428 | return PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 429 | } |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 430 | op = (arrayobject *) type->tp_alloc(type, 0); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 431 | if (op == NULL) { |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 432 | return NULL; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 433 | } |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 434 | op->ob_size = size; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 435 | if (size <= 0) { |
| 436 | op->ob_item = NULL; |
| 437 | } |
| 438 | else { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 439 | op->ob_item = PyMem_NEW(char, nbytes); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 440 | if (op->ob_item == NULL) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 441 | PyObject_Del(op); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 442 | return PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 443 | } |
| 444 | } |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 445 | op->ob_descr = descr; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 446 | op->allocated = size; |
Raymond Hettinger | cb87bc8 | 2004-05-31 00:35:52 +0000 | [diff] [blame] | 447 | op->weakreflist = NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 448 | return (PyObject *) op; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 451 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 452 | getarrayitem(PyObject *op, int i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 453 | { |
| 454 | register arrayobject *ap; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 455 | assert(array_Check(op)); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 456 | ap = (arrayobject *)op; |
Walter Dörwald | 8bb1ae9 | 2003-05-23 10:01:07 +0000 | [diff] [blame] | 457 | assert(i>=0 && i<ap->ob_size); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 458 | return (*ap->ob_descr->getitem)(ap, i); |
| 459 | } |
| 460 | |
| 461 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 462 | ins1(arrayobject *self, int where, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 463 | { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 464 | char *items; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 465 | int n = self->ob_size; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 466 | if (v == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 467 | PyErr_BadInternalCall(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 468 | return -1; |
| 469 | } |
| 470 | if ((*self->ob_descr->setitem)(self, -1, v) < 0) |
| 471 | return -1; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 472 | |
| 473 | if (array_resize(self, n+1) == -1) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 474 | return -1; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 475 | items = self->ob_item; |
Walter Dörwald | 9e46abe | 2003-05-18 03:15:10 +0000 | [diff] [blame] | 476 | if (where < 0) { |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 477 | where += n; |
Walter Dörwald | 9e46abe | 2003-05-18 03:15:10 +0000 | [diff] [blame] | 478 | if (where < 0) |
| 479 | where = 0; |
| 480 | } |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 481 | if (where > n) |
| 482 | where = n; |
| 483 | /* appends don't need to call memmove() */ |
| 484 | if (where != n) |
| 485 | memmove(items + (where+1)*self->ob_descr->itemsize, |
| 486 | items + where*self->ob_descr->itemsize, |
| 487 | (n-where)*self->ob_descr->itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 488 | return (*self->ob_descr->setitem)(self, where, v); |
| 489 | } |
| 490 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 491 | /* Methods */ |
| 492 | |
| 493 | static void |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 494 | array_dealloc(arrayobject *op) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 495 | { |
Raymond Hettinger | cb87bc8 | 2004-05-31 00:35:52 +0000 | [diff] [blame] | 496 | if (op->weakreflist != NULL) |
| 497 | PyObject_ClearWeakRefs((PyObject *) op); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 498 | if (op->ob_item != NULL) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 499 | PyMem_DEL(op->ob_item); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 500 | op->ob_type->tp_free((PyObject *)op); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 503 | static PyObject * |
| 504 | array_richcompare(PyObject *v, PyObject *w, int op) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 505 | { |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 506 | arrayobject *va, *wa; |
| 507 | PyObject *vi = NULL; |
| 508 | PyObject *wi = NULL; |
| 509 | int i, k; |
| 510 | PyObject *res; |
| 511 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 512 | if (!array_Check(v) || !array_Check(w)) { |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 513 | Py_INCREF(Py_NotImplemented); |
| 514 | return Py_NotImplemented; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 515 | } |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 516 | |
| 517 | va = (arrayobject *)v; |
| 518 | wa = (arrayobject *)w; |
| 519 | |
| 520 | if (va->ob_size != wa->ob_size && (op == Py_EQ || op == Py_NE)) { |
| 521 | /* Shortcut: if the lengths differ, the arrays differ */ |
| 522 | if (op == Py_EQ) |
| 523 | res = Py_False; |
| 524 | else |
| 525 | res = Py_True; |
| 526 | Py_INCREF(res); |
| 527 | return res; |
| 528 | } |
| 529 | |
| 530 | /* Search for the first index where items are different */ |
| 531 | k = 1; |
| 532 | for (i = 0; i < va->ob_size && i < wa->ob_size; i++) { |
| 533 | vi = getarrayitem(v, i); |
| 534 | wi = getarrayitem(w, i); |
| 535 | if (vi == NULL || wi == NULL) { |
| 536 | Py_XDECREF(vi); |
| 537 | Py_XDECREF(wi); |
| 538 | return NULL; |
| 539 | } |
| 540 | k = PyObject_RichCompareBool(vi, wi, Py_EQ); |
| 541 | if (k == 0) |
| 542 | break; /* Keeping vi and wi alive! */ |
| 543 | Py_DECREF(vi); |
| 544 | Py_DECREF(wi); |
| 545 | if (k < 0) |
| 546 | return NULL; |
| 547 | } |
| 548 | |
| 549 | if (k) { |
| 550 | /* No more items to compare -- compare sizes */ |
| 551 | int vs = va->ob_size; |
| 552 | int ws = wa->ob_size; |
| 553 | int cmp; |
| 554 | switch (op) { |
| 555 | case Py_LT: cmp = vs < ws; break; |
Guido van Rossum | 2b597e4 | 2001-01-25 22:12:43 +0000 | [diff] [blame] | 556 | case Py_LE: cmp = vs <= ws; break; |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 557 | case Py_EQ: cmp = vs == ws; break; |
| 558 | case Py_NE: cmp = vs != ws; break; |
| 559 | case Py_GT: cmp = vs > ws; break; |
| 560 | case Py_GE: cmp = vs >= ws; break; |
| 561 | default: return NULL; /* cannot happen */ |
| 562 | } |
| 563 | if (cmp) |
| 564 | res = Py_True; |
| 565 | else |
| 566 | res = Py_False; |
| 567 | Py_INCREF(res); |
| 568 | return res; |
| 569 | } |
| 570 | |
| 571 | /* We have an item that differs. First, shortcuts for EQ/NE */ |
| 572 | if (op == Py_EQ) { |
| 573 | Py_INCREF(Py_False); |
| 574 | res = Py_False; |
| 575 | } |
| 576 | else if (op == Py_NE) { |
| 577 | Py_INCREF(Py_True); |
| 578 | res = Py_True; |
| 579 | } |
| 580 | else { |
| 581 | /* Compare the final item again using the proper operator */ |
| 582 | res = PyObject_RichCompare(vi, wi, op); |
| 583 | } |
| 584 | Py_DECREF(vi); |
| 585 | Py_DECREF(wi); |
| 586 | return res; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 590 | array_length(arrayobject *a) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 591 | { |
| 592 | return a->ob_size; |
| 593 | } |
| 594 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 595 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 596 | array_item(arrayobject *a, int i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 597 | { |
| 598 | if (i < 0 || i >= a->ob_size) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 599 | PyErr_SetString(PyExc_IndexError, "array index out of range"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 600 | return NULL; |
| 601 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 602 | return getarrayitem((PyObject *)a, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 605 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 606 | array_slice(arrayobject *a, int ilow, int ihigh) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 607 | { |
| 608 | arrayobject *np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 609 | if (ilow < 0) |
| 610 | ilow = 0; |
| 611 | else if (ilow > a->ob_size) |
| 612 | ilow = a->ob_size; |
| 613 | if (ihigh < 0) |
| 614 | ihigh = 0; |
| 615 | if (ihigh < ilow) |
| 616 | ihigh = ilow; |
| 617 | else if (ihigh > a->ob_size) |
| 618 | ihigh = a->ob_size; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 619 | np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 620 | if (np == NULL) |
| 621 | return NULL; |
| 622 | memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize, |
| 623 | (ihigh-ilow) * a->ob_descr->itemsize); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 624 | return (PyObject *)np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 627 | static PyObject * |
Raymond Hettinger | 3aa82c0 | 2004-03-13 18:18:51 +0000 | [diff] [blame] | 628 | array_copy(arrayobject *a, PyObject *unused) |
| 629 | { |
| 630 | return array_slice(a, 0, a->ob_size); |
| 631 | } |
| 632 | |
| 633 | PyDoc_STRVAR(copy_doc, |
| 634 | "copy(array)\n\ |
| 635 | \n\ |
| 636 | Return a copy of the array."); |
| 637 | |
| 638 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 639 | array_concat(arrayobject *a, PyObject *bb) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 640 | { |
| 641 | int size; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 642 | arrayobject *np; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 643 | if (!array_Check(bb)) { |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 644 | PyErr_Format(PyExc_TypeError, |
| 645 | "can only append array (not \"%.200s\") to array", |
| 646 | bb->ob_type->tp_name); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 647 | return NULL; |
| 648 | } |
| 649 | #define b ((arrayobject *)bb) |
| 650 | if (a->ob_descr != b->ob_descr) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 651 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 652 | return NULL; |
| 653 | } |
| 654 | size = a->ob_size + b->ob_size; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 655 | np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 656 | if (np == NULL) { |
| 657 | return NULL; |
| 658 | } |
| 659 | memcpy(np->ob_item, a->ob_item, a->ob_size*a->ob_descr->itemsize); |
| 660 | memcpy(np->ob_item + a->ob_size*a->ob_descr->itemsize, |
Guido van Rossum | 32be3a7 | 1993-11-05 10:16:27 +0000 | [diff] [blame] | 661 | b->ob_item, b->ob_size*b->ob_descr->itemsize); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 662 | return (PyObject *)np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 663 | #undef b |
| 664 | } |
| 665 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 666 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 667 | array_repeat(arrayobject *a, int n) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 668 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 669 | int i; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 670 | int size; |
| 671 | arrayobject *np; |
| 672 | char *p; |
| 673 | int nbytes; |
| 674 | if (n < 0) |
| 675 | n = 0; |
| 676 | size = a->ob_size * n; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 677 | np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 678 | if (np == NULL) |
| 679 | return NULL; |
| 680 | p = np->ob_item; |
| 681 | nbytes = a->ob_size * a->ob_descr->itemsize; |
| 682 | for (i = 0; i < n; i++) { |
| 683 | memcpy(p, a->ob_item, nbytes); |
| 684 | p += nbytes; |
| 685 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 686 | return (PyObject *) np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 690 | array_ass_slice(arrayobject *a, int ilow, int ihigh, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 691 | { |
| 692 | char *item; |
| 693 | int n; /* Size of replacement array */ |
| 694 | int d; /* Change in size */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 695 | #define b ((arrayobject *)v) |
| 696 | if (v == NULL) |
| 697 | n = 0; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 698 | else if (array_Check(v)) { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 699 | n = b->ob_size; |
| 700 | if (a == b) { |
| 701 | /* Special case "a[i:j] = a" -- copy b first */ |
| 702 | int ret; |
| 703 | v = array_slice(b, 0, n); |
| 704 | ret = array_ass_slice(a, ilow, ihigh, v); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 705 | Py_DECREF(v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 706 | return ret; |
| 707 | } |
| 708 | if (b->ob_descr != a->ob_descr) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 709 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 710 | return -1; |
| 711 | } |
| 712 | } |
| 713 | else { |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 714 | PyErr_Format(PyExc_TypeError, |
| 715 | "can only assign array (not \"%.200s\") to array slice", |
| 716 | v->ob_type->tp_name); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 717 | return -1; |
| 718 | } |
| 719 | if (ilow < 0) |
| 720 | ilow = 0; |
| 721 | else if (ilow > a->ob_size) |
| 722 | ilow = a->ob_size; |
| 723 | if (ihigh < 0) |
| 724 | ihigh = 0; |
| 725 | if (ihigh < ilow) |
| 726 | ihigh = ilow; |
| 727 | else if (ihigh > a->ob_size) |
| 728 | ihigh = a->ob_size; |
| 729 | item = a->ob_item; |
| 730 | d = n - (ihigh-ilow); |
| 731 | if (d < 0) { /* Delete -d items */ |
| 732 | memmove(item + (ihigh+d)*a->ob_descr->itemsize, |
| 733 | item + ihigh*a->ob_descr->itemsize, |
| 734 | (a->ob_size-ihigh)*a->ob_descr->itemsize); |
| 735 | a->ob_size += d; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 736 | PyMem_RESIZE(item, char, a->ob_size*a->ob_descr->itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 737 | /* Can't fail */ |
| 738 | a->ob_item = item; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 739 | a->allocated = a->ob_size; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 740 | } |
| 741 | else if (d > 0) { /* Insert d items */ |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 742 | PyMem_RESIZE(item, char, |
| 743 | (a->ob_size + d)*a->ob_descr->itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 744 | if (item == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 745 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 746 | return -1; |
| 747 | } |
| 748 | memmove(item + (ihigh+d)*a->ob_descr->itemsize, |
| 749 | item + ihigh*a->ob_descr->itemsize, |
| 750 | (a->ob_size-ihigh)*a->ob_descr->itemsize); |
| 751 | a->ob_item = item; |
| 752 | a->ob_size += d; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 753 | a->allocated = a->ob_size; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 754 | } |
| 755 | if (n > 0) |
| 756 | memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item, |
| 757 | n*b->ob_descr->itemsize); |
| 758 | return 0; |
| 759 | #undef b |
| 760 | } |
| 761 | |
| 762 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 763 | array_ass_item(arrayobject *a, int i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 764 | { |
| 765 | if (i < 0 || i >= a->ob_size) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 766 | PyErr_SetString(PyExc_IndexError, |
| 767 | "array assignment index out of range"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 768 | return -1; |
| 769 | } |
| 770 | if (v == NULL) |
| 771 | return array_ass_slice(a, i, i+1, v); |
| 772 | return (*a->ob_descr->setitem)(a, i, v); |
| 773 | } |
| 774 | |
| 775 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 776 | setarrayitem(PyObject *a, int i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 777 | { |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 778 | assert(array_Check(a)); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 779 | return array_ass_item((arrayobject *)a, i, v); |
| 780 | } |
| 781 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 782 | static int |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 783 | array_iter_extend(arrayobject *self, PyObject *bb) |
| 784 | { |
| 785 | PyObject *it, *v; |
| 786 | |
| 787 | it = PyObject_GetIter(bb); |
| 788 | if (it == NULL) |
| 789 | return -1; |
| 790 | |
| 791 | while ((v = PyIter_Next(it)) != NULL) { |
| 792 | if (ins1(self, (int) self->ob_size, v) != 0) { |
| 793 | Py_DECREF(v); |
| 794 | Py_DECREF(it); |
| 795 | return -1; |
| 796 | } |
| 797 | Py_DECREF(v); |
| 798 | } |
| 799 | Py_DECREF(it); |
| 800 | if (PyErr_Occurred()) |
| 801 | return -1; |
| 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | static int |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 806 | array_do_extend(arrayobject *self, PyObject *bb) |
| 807 | { |
| 808 | int size; |
| 809 | |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 810 | if (!array_Check(bb)) |
| 811 | return array_iter_extend(self, bb); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 812 | #define b ((arrayobject *)bb) |
| 813 | if (self->ob_descr != b->ob_descr) { |
| 814 | PyErr_SetString(PyExc_TypeError, |
| 815 | "can only extend with array of same kind"); |
| 816 | return -1; |
| 817 | } |
| 818 | size = self->ob_size + b->ob_size; |
| 819 | PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize); |
| 820 | if (self->ob_item == NULL) { |
| 821 | PyObject_Del(self); |
| 822 | PyErr_NoMemory(); |
| 823 | return -1; |
| 824 | } |
| 825 | memcpy(self->ob_item + self->ob_size*self->ob_descr->itemsize, |
| 826 | b->ob_item, b->ob_size*b->ob_descr->itemsize); |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 827 | self->ob_size = size; |
| 828 | self->allocated = size; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 829 | |
| 830 | return 0; |
| 831 | #undef b |
| 832 | } |
| 833 | |
| 834 | static PyObject * |
| 835 | array_inplace_concat(arrayobject *self, PyObject *bb) |
| 836 | { |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 837 | if (!array_Check(bb)) { |
| 838 | PyErr_Format(PyExc_TypeError, |
| 839 | "can only extend array with array (not \"%.200s\")", |
| 840 | bb->ob_type->tp_name); |
| 841 | return NULL; |
| 842 | } |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 843 | if (array_do_extend(self, bb) == -1) |
| 844 | return NULL; |
| 845 | Py_INCREF(self); |
| 846 | return (PyObject *)self; |
| 847 | } |
| 848 | |
| 849 | static PyObject * |
| 850 | array_inplace_repeat(arrayobject *self, int n) |
| 851 | { |
| 852 | char *items, *p; |
| 853 | int size, i; |
| 854 | |
| 855 | if (self->ob_size > 0) { |
| 856 | if (n < 0) |
| 857 | n = 0; |
| 858 | items = self->ob_item; |
| 859 | size = self->ob_size * self->ob_descr->itemsize; |
| 860 | if (n == 0) { |
| 861 | PyMem_FREE(items); |
| 862 | self->ob_item = NULL; |
| 863 | self->ob_size = 0; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 864 | self->allocated = 0; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 865 | } |
| 866 | else { |
| 867 | PyMem_Resize(items, char, n * size); |
| 868 | if (items == NULL) |
| 869 | return PyErr_NoMemory(); |
| 870 | p = items; |
| 871 | for (i = 1; i < n; i++) { |
| 872 | p += size; |
| 873 | memcpy(p, items, size); |
| 874 | } |
| 875 | self->ob_item = items; |
| 876 | self->ob_size *= n; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 877 | self->allocated = self->ob_size; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | Py_INCREF(self); |
| 881 | return (PyObject *)self; |
| 882 | } |
| 883 | |
| 884 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 885 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 886 | ins(arrayobject *self, int where, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 887 | { |
| 888 | if (ins1(self, where, v) != 0) |
| 889 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 890 | Py_INCREF(Py_None); |
| 891 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 894 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 895 | array_count(arrayobject *self, PyObject *v) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 896 | { |
| 897 | int count = 0; |
| 898 | int i; |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 899 | |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 900 | for (i = 0; i < self->ob_size; i++) { |
| 901 | PyObject *selfi = getarrayitem((PyObject *)self, i); |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 902 | int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 903 | Py_DECREF(selfi); |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 904 | if (cmp > 0) |
| 905 | count++; |
| 906 | else if (cmp < 0) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 907 | return NULL; |
| 908 | } |
| 909 | return PyInt_FromLong((long)count); |
| 910 | } |
| 911 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 912 | PyDoc_STRVAR(count_doc, |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 913 | "count(x)\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 914 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 915 | Return number of occurences of x in the array."); |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 916 | |
| 917 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 918 | array_index(arrayobject *self, PyObject *v) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 919 | { |
| 920 | int i; |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 921 | |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 922 | for (i = 0; i < self->ob_size; i++) { |
| 923 | PyObject *selfi = getarrayitem((PyObject *)self, i); |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 924 | int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
| 925 | Py_DECREF(selfi); |
| 926 | if (cmp > 0) { |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 927 | return PyInt_FromLong((long)i); |
| 928 | } |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 929 | else if (cmp < 0) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 930 | return NULL; |
| 931 | } |
| 932 | PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list"); |
| 933 | return NULL; |
| 934 | } |
| 935 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 936 | PyDoc_STRVAR(index_doc, |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 937 | "index(x)\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 938 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 939 | Return index of first occurence of x in the array."); |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 940 | |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 941 | static int |
| 942 | array_contains(arrayobject *self, PyObject *v) |
| 943 | { |
| 944 | int i, cmp; |
| 945 | |
| 946 | for (i = 0, cmp = 0 ; cmp == 0 && i < self->ob_size; i++) { |
| 947 | PyObject *selfi = getarrayitem((PyObject *)self, i); |
| 948 | cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
| 949 | Py_DECREF(selfi); |
| 950 | } |
| 951 | return cmp; |
| 952 | } |
| 953 | |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 954 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 955 | array_remove(arrayobject *self, PyObject *v) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 956 | { |
| 957 | int i; |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 958 | |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 959 | for (i = 0; i < self->ob_size; i++) { |
| 960 | PyObject *selfi = getarrayitem((PyObject *)self,i); |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 961 | int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
| 962 | Py_DECREF(selfi); |
| 963 | if (cmp > 0) { |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 964 | if (array_ass_slice(self, i, i+1, |
| 965 | (PyObject *)NULL) != 0) |
| 966 | return NULL; |
| 967 | Py_INCREF(Py_None); |
| 968 | return Py_None; |
| 969 | } |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 970 | else if (cmp < 0) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 971 | return NULL; |
| 972 | } |
| 973 | PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list"); |
| 974 | return NULL; |
| 975 | } |
| 976 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 977 | PyDoc_STRVAR(remove_doc, |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 978 | "remove(x)\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 979 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 980 | Remove the first occurence of x in the array."); |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 981 | |
| 982 | static PyObject * |
| 983 | array_pop(arrayobject *self, PyObject *args) |
| 984 | { |
| 985 | int i = -1; |
| 986 | PyObject *v; |
| 987 | if (!PyArg_ParseTuple(args, "|i:pop", &i)) |
| 988 | return NULL; |
| 989 | if (self->ob_size == 0) { |
| 990 | /* Special-case most common failure cause */ |
| 991 | PyErr_SetString(PyExc_IndexError, "pop from empty array"); |
| 992 | return NULL; |
| 993 | } |
| 994 | if (i < 0) |
| 995 | i += self->ob_size; |
| 996 | if (i < 0 || i >= self->ob_size) { |
| 997 | PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
| 998 | return NULL; |
| 999 | } |
| 1000 | v = getarrayitem((PyObject *)self,i); |
| 1001 | if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) { |
| 1002 | Py_DECREF(v); |
| 1003 | return NULL; |
| 1004 | } |
| 1005 | return v; |
| 1006 | } |
| 1007 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1008 | PyDoc_STRVAR(pop_doc, |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 1009 | "pop([i])\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1010 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1011 | Return the i-th element and delete it from the array. i defaults to -1."); |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1012 | |
| 1013 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1014 | array_extend(arrayobject *self, PyObject *bb) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1015 | { |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1016 | if (array_do_extend(self, bb) == -1) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1017 | return NULL; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1018 | Py_INCREF(Py_None); |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1019 | return Py_None; |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1022 | PyDoc_STRVAR(extend_doc, |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 1023 | "extend(array or iterable)\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1024 | \n\ |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 1025 | Append items to the end of the array."); |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1026 | |
| 1027 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 1028 | array_insert(arrayobject *self, PyObject *args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1029 | { |
| 1030 | int i; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1031 | PyObject *v; |
Peter Schneider-Kamp | dc71cac | 2000-07-31 21:57:30 +0000 | [diff] [blame] | 1032 | if (!PyArg_ParseTuple(args, "iO:insert", &i, &v)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1033 | return NULL; |
| 1034 | return ins(self, i, v); |
| 1035 | } |
| 1036 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1037 | PyDoc_STRVAR(insert_doc, |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 1038 | "insert(i,x)\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1039 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1040 | Insert a new item x into the array before position i."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1041 | |
| 1042 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1043 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1044 | array_buffer_info(arrayobject *self, PyObject *unused) |
Guido van Rossum | de4a4ca | 1997-08-12 14:55:56 +0000 | [diff] [blame] | 1045 | { |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 1046 | PyObject* retval = NULL; |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 1047 | retval = PyTuple_New(2); |
| 1048 | if (!retval) |
| 1049 | return NULL; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 1050 | |
| 1051 | PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item)); |
| 1052 | PyTuple_SET_ITEM(retval, 1, PyInt_FromLong((long)(self->ob_size))); |
| 1053 | |
| 1054 | return retval; |
Guido van Rossum | de4a4ca | 1997-08-12 14:55:56 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1057 | PyDoc_STRVAR(buffer_info_doc, |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 1058 | "buffer_info() -> (address, length)\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1059 | \n\ |
| 1060 | Return a tuple (address, length) giving the current memory address and\n\ |
Guido van Rossum | 702d08e | 2001-07-27 16:05:32 +0000 | [diff] [blame] | 1061 | the length in items of the buffer used to hold array's contents\n\ |
| 1062 | The length should be multiplied by the itemsize attribute to calculate\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1063 | the buffer length in bytes."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1064 | |
| 1065 | |
Guido van Rossum | de4a4ca | 1997-08-12 14:55:56 +0000 | [diff] [blame] | 1066 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1067 | array_append(arrayobject *self, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1068 | { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1069 | return ins(self, (int) self->ob_size, v); |
| 1070 | } |
| 1071 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1072 | PyDoc_STRVAR(append_doc, |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1073 | "append(x)\n\ |
| 1074 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1075 | Append new value x to the end of the array."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1076 | |
| 1077 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1078 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1079 | array_byteswap(arrayobject *self, PyObject *unused) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1080 | { |
| 1081 | char *p; |
| 1082 | int i; |
Fred Drake | bf27298 | 1999-12-03 17:15:30 +0000 | [diff] [blame] | 1083 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1084 | switch (self->ob_descr->itemsize) { |
| 1085 | case 1: |
| 1086 | break; |
| 1087 | case 2: |
| 1088 | for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 2) { |
| 1089 | char p0 = p[0]; |
| 1090 | p[0] = p[1]; |
| 1091 | p[1] = p0; |
| 1092 | } |
| 1093 | break; |
| 1094 | case 4: |
| 1095 | for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 4) { |
| 1096 | char p0 = p[0]; |
| 1097 | char p1 = p[1]; |
| 1098 | p[0] = p[3]; |
| 1099 | p[1] = p[2]; |
| 1100 | p[2] = p1; |
| 1101 | p[3] = p0; |
| 1102 | } |
| 1103 | break; |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 1104 | case 8: |
| 1105 | for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 8) { |
| 1106 | char p0 = p[0]; |
| 1107 | char p1 = p[1]; |
| 1108 | char p2 = p[2]; |
| 1109 | char p3 = p[3]; |
| 1110 | p[0] = p[7]; |
| 1111 | p[1] = p[6]; |
| 1112 | p[2] = p[5]; |
| 1113 | p[3] = p[4]; |
| 1114 | p[4] = p3; |
| 1115 | p[5] = p2; |
| 1116 | p[6] = p1; |
| 1117 | p[7] = p0; |
| 1118 | } |
| 1119 | break; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1120 | default: |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1121 | PyErr_SetString(PyExc_RuntimeError, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1122 | "don't know how to byteswap this array type"); |
| 1123 | return NULL; |
| 1124 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1125 | Py_INCREF(Py_None); |
| 1126 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1129 | PyDoc_STRVAR(byteswap_doc, |
Fred Drake | bf27298 | 1999-12-03 17:15:30 +0000 | [diff] [blame] | 1130 | "byteswap()\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1131 | \n\ |
Fred Drake | bf27298 | 1999-12-03 17:15:30 +0000 | [diff] [blame] | 1132 | Byteswap all items of the array. If the items in the array are not 1, 2,\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1133 | 4, or 8 bytes in size, RuntimeError is raised."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1134 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1135 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1136 | array_reverse(arrayobject *self, PyObject *unused) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1137 | { |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 1138 | register int itemsize = self->ob_descr->itemsize; |
| 1139 | register char *p, *q; |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 1140 | /* little buffer to hold items while swapping */ |
| 1141 | char tmp[256]; /* 8 is probably enough -- but why skimp */ |
| 1142 | assert(itemsize <= sizeof(tmp)); |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 1143 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1144 | if (self->ob_size > 1) { |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 1145 | for (p = self->ob_item, |
| 1146 | q = self->ob_item + (self->ob_size - 1)*itemsize; |
| 1147 | p < q; |
| 1148 | p += itemsize, q -= itemsize) { |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 1149 | /* memory areas guaranteed disjoint, so memcpy |
| 1150 | * is safe (& memmove may be slower). |
| 1151 | */ |
| 1152 | memcpy(tmp, p, itemsize); |
| 1153 | memcpy(p, q, itemsize); |
| 1154 | memcpy(q, tmp, itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1155 | } |
| 1156 | } |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 1157 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1158 | Py_INCREF(Py_None); |
| 1159 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1160 | } |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 1161 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1162 | PyDoc_STRVAR(reverse_doc, |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1163 | "reverse()\n\ |
| 1164 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1165 | Reverse the order of the items in the array."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1166 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1167 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 1168 | array_fromfile(arrayobject *self, PyObject *args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1169 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1170 | PyObject *f; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1171 | int n; |
| 1172 | FILE *fp; |
Peter Schneider-Kamp | dc71cac | 2000-07-31 21:57:30 +0000 | [diff] [blame] | 1173 | if (!PyArg_ParseTuple(args, "Oi:fromfile", &f, &n)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1174 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1175 | fp = PyFile_AsFile(f); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1176 | if (fp == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1177 | PyErr_SetString(PyExc_TypeError, "arg1 must be open file"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1178 | return NULL; |
| 1179 | } |
| 1180 | if (n > 0) { |
| 1181 | char *item = self->ob_item; |
| 1182 | int itemsize = self->ob_descr->itemsize; |
Guido van Rossum | 7d0ae5e | 2000-06-28 21:27:21 +0000 | [diff] [blame] | 1183 | size_t nread; |
Guido van Rossum | 3791b0d | 1999-02-23 18:05:22 +0000 | [diff] [blame] | 1184 | int newlength; |
| 1185 | size_t newbytes; |
| 1186 | /* Be careful here about overflow */ |
| 1187 | if ((newlength = self->ob_size + n) <= 0 || |
Guido van Rossum | 481ac88 | 1999-03-19 21:50:11 +0000 | [diff] [blame] | 1188 | (newbytes = newlength * itemsize) / itemsize != |
| 1189 | (size_t)newlength) |
Guido van Rossum | 3791b0d | 1999-02-23 18:05:22 +0000 | [diff] [blame] | 1190 | goto nomem; |
| 1191 | PyMem_RESIZE(item, char, newbytes); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1192 | if (item == NULL) { |
Guido van Rossum | 3791b0d | 1999-02-23 18:05:22 +0000 | [diff] [blame] | 1193 | nomem: |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1194 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1195 | return NULL; |
| 1196 | } |
| 1197 | self->ob_item = item; |
| 1198 | self->ob_size += n; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 1199 | self->allocated = self->ob_size; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1200 | nread = fread(item + (self->ob_size - n) * itemsize, |
| 1201 | itemsize, n, fp); |
Guido van Rossum | 7d0ae5e | 2000-06-28 21:27:21 +0000 | [diff] [blame] | 1202 | if (nread < (size_t)n) { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1203 | self->ob_size -= (n - nread); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1204 | PyMem_RESIZE(item, char, self->ob_size*itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1205 | self->ob_item = item; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 1206 | self->allocated = self->ob_size; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1207 | PyErr_SetString(PyExc_EOFError, |
| 1208 | "not enough items in file"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1209 | return NULL; |
| 1210 | } |
| 1211 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1212 | Py_INCREF(Py_None); |
| 1213 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1216 | PyDoc_STRVAR(fromfile_doc, |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1217 | "fromfile(f, n)\n\ |
| 1218 | \n\ |
| 1219 | Read n objects from the file object f and append them to the end of the\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1220 | array. Also called as read."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1221 | |
| 1222 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1223 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1224 | array_tofile(arrayobject *self, PyObject *f) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1225 | { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1226 | FILE *fp; |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1227 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1228 | fp = PyFile_AsFile(f); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1229 | if (fp == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1230 | PyErr_SetString(PyExc_TypeError, "arg must be open file"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1231 | return NULL; |
| 1232 | } |
| 1233 | if (self->ob_size > 0) { |
Trent Mick | 6c116dd | 2000-08-12 20:58:11 +0000 | [diff] [blame] | 1234 | if (fwrite(self->ob_item, self->ob_descr->itemsize, |
| 1235 | self->ob_size, fp) != (size_t)self->ob_size) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1236 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1237 | clearerr(fp); |
| 1238 | return NULL; |
| 1239 | } |
| 1240 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1241 | Py_INCREF(Py_None); |
| 1242 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1245 | PyDoc_STRVAR(tofile_doc, |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1246 | "tofile(f)\n\ |
| 1247 | \n\ |
| 1248 | Write all items (as machine values) to the file object f. Also called as\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1249 | write."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1250 | |
| 1251 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1252 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1253 | array_fromlist(arrayobject *self, PyObject *list) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1254 | { |
| 1255 | int n; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1256 | int itemsize = self->ob_descr->itemsize; |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1257 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1258 | if (!PyList_Check(list)) { |
| 1259 | PyErr_SetString(PyExc_TypeError, "arg must be list"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1260 | return NULL; |
| 1261 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1262 | n = PyList_Size(list); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1263 | if (n > 0) { |
| 1264 | char *item = self->ob_item; |
| 1265 | int i; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1266 | PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1267 | if (item == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1268 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1269 | return NULL; |
| 1270 | } |
| 1271 | self->ob_item = item; |
| 1272 | self->ob_size += n; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 1273 | self->allocated = self->ob_size; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1274 | for (i = 0; i < n; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1275 | PyObject *v = PyList_GetItem(list, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1276 | if ((*self->ob_descr->setitem)(self, |
| 1277 | self->ob_size - n + i, v) != 0) { |
| 1278 | self->ob_size -= n; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1279 | PyMem_RESIZE(item, char, |
| 1280 | self->ob_size * itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1281 | self->ob_item = item; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 1282 | self->allocated = self->ob_size; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1283 | return NULL; |
| 1284 | } |
| 1285 | } |
| 1286 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1287 | Py_INCREF(Py_None); |
| 1288 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1291 | PyDoc_STRVAR(fromlist_doc, |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1292 | "fromlist(list)\n\ |
| 1293 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1294 | Append items to array from list."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1295 | |
| 1296 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1297 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1298 | array_tolist(arrayobject *self, PyObject *unused) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1299 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1300 | PyObject *list = PyList_New(self->ob_size); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1301 | int i; |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1302 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1303 | if (list == NULL) |
| 1304 | return NULL; |
| 1305 | for (i = 0; i < self->ob_size; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1306 | PyObject *v = getarrayitem((PyObject *)self, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1307 | if (v == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1308 | Py_DECREF(list); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1309 | return NULL; |
| 1310 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1311 | PyList_SetItem(list, i, v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1312 | } |
| 1313 | return list; |
| 1314 | } |
| 1315 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1316 | PyDoc_STRVAR(tolist_doc, |
Guido van Rossum | fc6aba5 | 1998-10-14 02:52:31 +0000 | [diff] [blame] | 1317 | "tolist() -> list\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1318 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1319 | Convert array to an ordinary list with the same items."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1320 | |
| 1321 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1322 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 1323 | array_fromstring(arrayobject *self, PyObject *args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1324 | { |
| 1325 | char *str; |
| 1326 | int n; |
| 1327 | int itemsize = self->ob_descr->itemsize; |
Peter Schneider-Kamp | dc71cac | 2000-07-31 21:57:30 +0000 | [diff] [blame] | 1328 | if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1329 | return NULL; |
| 1330 | if (n % itemsize != 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1331 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1332 | "string length not a multiple of item size"); |
| 1333 | return NULL; |
| 1334 | } |
| 1335 | n = n / itemsize; |
| 1336 | if (n > 0) { |
| 1337 | char *item = self->ob_item; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1338 | PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1339 | if (item == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1340 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1341 | return NULL; |
| 1342 | } |
| 1343 | self->ob_item = item; |
| 1344 | self->ob_size += n; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 1345 | self->allocated = self->ob_size; |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1346 | memcpy(item + (self->ob_size - n) * itemsize, |
| 1347 | str, itemsize*n); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1348 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1349 | Py_INCREF(Py_None); |
| 1350 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1353 | PyDoc_STRVAR(fromstring_doc, |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1354 | "fromstring(string)\n\ |
| 1355 | \n\ |
| 1356 | Appends items from the string, interpreting it as an array of machine\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1357 | values,as if it had been read from a file using the fromfile() method)."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1358 | |
| 1359 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1360 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1361 | array_tostring(arrayobject *self, PyObject *unused) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1362 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1363 | return PyString_FromStringAndSize(self->ob_item, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1364 | self->ob_size * self->ob_descr->itemsize); |
| 1365 | } |
| 1366 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1367 | PyDoc_STRVAR(tostring_doc, |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1368 | "tostring() -> string\n\ |
| 1369 | \n\ |
| 1370 | Convert the array to an array of machine values and return the string\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1371 | representation."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1372 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1373 | |
| 1374 | |
| 1375 | #ifdef Py_USING_UNICODE |
| 1376 | static PyObject * |
| 1377 | array_fromunicode(arrayobject *self, PyObject *args) |
| 1378 | { |
| 1379 | Py_UNICODE *ustr; |
| 1380 | int n; |
| 1381 | |
| 1382 | if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n)) |
| 1383 | return NULL; |
| 1384 | if (self->ob_descr->typecode != 'u') { |
| 1385 | PyErr_SetString(PyExc_ValueError, |
| 1386 | "fromunicode() may only be called on " |
| 1387 | "type 'u' arrays"); |
| 1388 | return NULL; |
| 1389 | } |
| 1390 | if (n > 0) { |
| 1391 | Py_UNICODE *item = (Py_UNICODE *) self->ob_item; |
| 1392 | PyMem_RESIZE(item, Py_UNICODE, self->ob_size + n); |
| 1393 | if (item == NULL) { |
| 1394 | PyErr_NoMemory(); |
| 1395 | return NULL; |
| 1396 | } |
| 1397 | self->ob_item = (char *) item; |
| 1398 | self->ob_size += n; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 1399 | self->allocated = self->ob_size; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1400 | memcpy(item + self->ob_size - n, |
| 1401 | ustr, n * sizeof(Py_UNICODE)); |
| 1402 | } |
| 1403 | |
| 1404 | Py_INCREF(Py_None); |
| 1405 | return Py_None; |
| 1406 | } |
| 1407 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1408 | PyDoc_STRVAR(fromunicode_doc, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1409 | "fromunicode(ustr)\n\ |
| 1410 | \n\ |
| 1411 | Extends this array with data from the unicode string ustr.\n\ |
| 1412 | The array must be a type 'u' array; otherwise a ValueError\n\ |
| 1413 | is raised. Use array.fromstring(ustr.decode(...)) to\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1414 | append Unicode data to an array of some other type."); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1415 | |
| 1416 | |
| 1417 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1418 | array_tounicode(arrayobject *self, PyObject *unused) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1419 | { |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1420 | if (self->ob_descr->typecode != 'u') { |
| 1421 | PyErr_SetString(PyExc_ValueError, |
| 1422 | "tounicode() may only be called on type 'u' arrays"); |
| 1423 | return NULL; |
| 1424 | } |
| 1425 | return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, self->ob_size); |
| 1426 | } |
| 1427 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1428 | PyDoc_STRVAR(tounicode_doc, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1429 | "tounicode() -> unicode\n\ |
| 1430 | \n\ |
| 1431 | Convert the array to a unicode string. The array must be\n\ |
| 1432 | a type 'u' array; otherwise a ValueError is raised. Use\n\ |
| 1433 | array.tostring().decode() to obtain a unicode string from\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1434 | an array of some other type."); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1435 | |
| 1436 | #endif /* Py_USING_UNICODE */ |
| 1437 | |
| 1438 | |
| 1439 | static PyObject * |
| 1440 | array_get_typecode(arrayobject *a, void *closure) |
| 1441 | { |
| 1442 | char tc = a->ob_descr->typecode; |
| 1443 | return PyString_FromStringAndSize(&tc, 1); |
| 1444 | } |
| 1445 | |
| 1446 | static PyObject * |
| 1447 | array_get_itemsize(arrayobject *a, void *closure) |
| 1448 | { |
| 1449 | return PyInt_FromLong((long)a->ob_descr->itemsize); |
| 1450 | } |
| 1451 | |
| 1452 | static PyGetSetDef array_getsets [] = { |
| 1453 | {"typecode", (getter) array_get_typecode, NULL, |
| 1454 | "the typecode character used to create the array"}, |
| 1455 | {"itemsize", (getter) array_get_itemsize, NULL, |
| 1456 | "the size, in bytes, of one array item"}, |
| 1457 | {NULL} |
| 1458 | }; |
| 1459 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1460 | PyMethodDef array_methods[] = { |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1461 | {"append", (PyCFunction)array_append, METH_O, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1462 | append_doc}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1463 | {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1464 | buffer_info_doc}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1465 | {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1466 | byteswap_doc}, |
Raymond Hettinger | 3aa82c0 | 2004-03-13 18:18:51 +0000 | [diff] [blame] | 1467 | {"__copy__", (PyCFunction)array_copy, METH_NOARGS, |
| 1468 | copy_doc}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1469 | {"count", (PyCFunction)array_count, METH_O, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1470 | count_doc}, |
Raymond Hettinger | 3aa82c0 | 2004-03-13 18:18:51 +0000 | [diff] [blame] | 1471 | {"__deepcopy__",(PyCFunction)array_copy, METH_NOARGS, |
| 1472 | copy_doc}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1473 | {"extend", (PyCFunction)array_extend, METH_O, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1474 | extend_doc}, |
| 1475 | {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS, |
| 1476 | fromfile_doc}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1477 | {"fromlist", (PyCFunction)array_fromlist, METH_O, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1478 | fromlist_doc}, |
| 1479 | {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS, |
| 1480 | fromstring_doc}, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1481 | #ifdef Py_USING_UNICODE |
| 1482 | {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS, |
| 1483 | fromunicode_doc}, |
| 1484 | #endif |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1485 | {"index", (PyCFunction)array_index, METH_O, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1486 | index_doc}, |
| 1487 | {"insert", (PyCFunction)array_insert, METH_VARARGS, |
| 1488 | insert_doc}, |
| 1489 | {"pop", (PyCFunction)array_pop, METH_VARARGS, |
| 1490 | pop_doc}, |
| 1491 | {"read", (PyCFunction)array_fromfile, METH_VARARGS, |
| 1492 | fromfile_doc}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1493 | {"remove", (PyCFunction)array_remove, METH_O, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1494 | remove_doc}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1495 | {"reverse", (PyCFunction)array_reverse, METH_NOARGS, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1496 | reverse_doc}, |
| 1497 | /* {"sort", (PyCFunction)array_sort, METH_VARARGS, |
| 1498 | sort_doc},*/ |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1499 | {"tofile", (PyCFunction)array_tofile, METH_O, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1500 | tofile_doc}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1501 | {"tolist", (PyCFunction)array_tolist, METH_NOARGS, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1502 | tolist_doc}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1503 | {"tostring", (PyCFunction)array_tostring, METH_NOARGS, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1504 | tostring_doc}, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1505 | #ifdef Py_USING_UNICODE |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1506 | {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1507 | tounicode_doc}, |
| 1508 | #endif |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1509 | {"write", (PyCFunction)array_tofile, METH_O, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1510 | tofile_doc}, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1511 | {NULL, NULL} /* sentinel */ |
| 1512 | }; |
| 1513 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1514 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 1515 | array_repr(arrayobject *a) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1516 | { |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1517 | char buf[256], typecode; |
Raymond Hettinger | 88ba1e3 | 2003-04-23 17:27:00 +0000 | [diff] [blame] | 1518 | PyObject *s, *t, *v = NULL; |
| 1519 | int len; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1520 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1521 | len = a->ob_size; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1522 | typecode = a->ob_descr->typecode; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1523 | if (len == 0) { |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1524 | PyOS_snprintf(buf, sizeof(buf), "array('%c')", typecode); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1525 | return PyString_FromString(buf); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1526 | } |
Raymond Hettinger | 88ba1e3 | 2003-04-23 17:27:00 +0000 | [diff] [blame] | 1527 | |
| 1528 | if (typecode == 'c') |
| 1529 | v = array_tostring(a, NULL); |
Michael W. Hudson | 1755ad9 | 2002-05-13 10:14:59 +0000 | [diff] [blame] | 1530 | #ifdef Py_USING_UNICODE |
Raymond Hettinger | 88ba1e3 | 2003-04-23 17:27:00 +0000 | [diff] [blame] | 1531 | else if (typecode == 'u') |
| 1532 | v = array_tounicode(a, NULL); |
Michael W. Hudson | 1755ad9 | 2002-05-13 10:14:59 +0000 | [diff] [blame] | 1533 | #endif |
Raymond Hettinger | 88ba1e3 | 2003-04-23 17:27:00 +0000 | [diff] [blame] | 1534 | else |
| 1535 | v = array_tolist(a, NULL); |
| 1536 | t = PyObject_Repr(v); |
| 1537 | Py_XDECREF(v); |
| 1538 | |
| 1539 | PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1540 | s = PyString_FromString(buf); |
Raymond Hettinger | 88ba1e3 | 2003-04-23 17:27:00 +0000 | [diff] [blame] | 1541 | PyString_ConcatAndDel(&s, t); |
| 1542 | PyString_ConcatAndDel(&s, PyString_FromString(")")); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1543 | return s; |
| 1544 | } |
| 1545 | |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1546 | static PyObject* |
| 1547 | array_subscr(arrayobject* self, PyObject* item) |
| 1548 | { |
| 1549 | if (PyInt_Check(item)) { |
| 1550 | long i = PyInt_AS_LONG(item); |
| 1551 | if (i < 0) |
| 1552 | i += self->ob_size; |
| 1553 | return array_item(self, i); |
| 1554 | } |
| 1555 | else if (PyLong_Check(item)) { |
| 1556 | long i = PyLong_AsLong(item); |
| 1557 | if (i == -1 && PyErr_Occurred()) |
| 1558 | return NULL; |
| 1559 | if (i < 0) |
| 1560 | i += self->ob_size; |
| 1561 | return array_item(self, i); |
| 1562 | } |
| 1563 | else if (PySlice_Check(item)) { |
| 1564 | int start, stop, step, slicelength, cur, i; |
| 1565 | PyObject* result; |
| 1566 | arrayobject* ar; |
| 1567 | int itemsize = self->ob_descr->itemsize; |
| 1568 | |
| 1569 | if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size, |
| 1570 | &start, &stop, &step, &slicelength) < 0) { |
| 1571 | return NULL; |
| 1572 | } |
| 1573 | |
| 1574 | if (slicelength <= 0) { |
| 1575 | return newarrayobject(&Arraytype, 0, self->ob_descr); |
| 1576 | } |
| 1577 | else { |
| 1578 | result = newarrayobject(&Arraytype, slicelength, self->ob_descr); |
| 1579 | if (!result) return NULL; |
| 1580 | |
| 1581 | ar = (arrayobject*)result; |
| 1582 | |
| 1583 | for (cur = start, i = 0; i < slicelength; |
| 1584 | cur += step, i++) { |
| 1585 | memcpy(ar->ob_item + i*itemsize, |
| 1586 | self->ob_item + cur*itemsize, |
| 1587 | itemsize); |
| 1588 | } |
| 1589 | |
| 1590 | return result; |
| 1591 | } |
| 1592 | } |
| 1593 | else { |
| 1594 | PyErr_SetString(PyExc_TypeError, |
| 1595 | "list indices must be integers"); |
| 1596 | return NULL; |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | static int |
| 1601 | array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value) |
| 1602 | { |
| 1603 | if (PyInt_Check(item)) { |
| 1604 | long i = PyInt_AS_LONG(item); |
| 1605 | if (i < 0) |
| 1606 | i += self->ob_size; |
| 1607 | return array_ass_item(self, i, value); |
| 1608 | } |
| 1609 | else if (PyLong_Check(item)) { |
| 1610 | long i = PyLong_AsLong(item); |
| 1611 | if (i == -1 && PyErr_Occurred()) |
| 1612 | return -1; |
| 1613 | if (i < 0) |
| 1614 | i += self->ob_size; |
| 1615 | return array_ass_item(self, i, value); |
| 1616 | } |
| 1617 | else if (PySlice_Check(item)) { |
| 1618 | int start, stop, step, slicelength; |
| 1619 | int itemsize = self->ob_descr->itemsize; |
| 1620 | |
| 1621 | if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size, |
| 1622 | &start, &stop, &step, &slicelength) < 0) { |
| 1623 | return -1; |
| 1624 | } |
| 1625 | |
| 1626 | /* treat A[slice(a,b)] = v _exactly_ like A[a:b] = v */ |
| 1627 | if (step == 1 && ((PySliceObject*)item)->step == Py_None) |
| 1628 | return array_ass_slice(self, start, stop, value); |
| 1629 | |
| 1630 | if (value == NULL) { |
| 1631 | /* delete slice */ |
Michael W. Hudson | 56796f6 | 2002-07-29 14:35:04 +0000 | [diff] [blame] | 1632 | int cur, i, extra; |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1633 | |
| 1634 | if (slicelength <= 0) |
| 1635 | return 0; |
| 1636 | |
| 1637 | if (step < 0) { |
| 1638 | stop = start + 1; |
| 1639 | start = stop + step*(slicelength - 1) - 1; |
| 1640 | step = -step; |
| 1641 | } |
| 1642 | |
Michael W. Hudson | 56796f6 | 2002-07-29 14:35:04 +0000 | [diff] [blame] | 1643 | for (cur = start, i = 0; i < slicelength - 1; |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1644 | cur += step, i++) { |
| 1645 | memmove(self->ob_item + (cur - i)*itemsize, |
| 1646 | self->ob_item + (cur + 1)*itemsize, |
| 1647 | (step - 1) * itemsize); |
| 1648 | } |
Michael W. Hudson | 56796f6 | 2002-07-29 14:35:04 +0000 | [diff] [blame] | 1649 | extra = self->ob_size - (cur + 1); |
| 1650 | if (extra > 0) { |
| 1651 | memmove(self->ob_item + (cur - i)*itemsize, |
| 1652 | self->ob_item + (cur + 1)*itemsize, |
| 1653 | extra*itemsize); |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
| 1656 | self->ob_size -= slicelength; |
| 1657 | self->ob_item = PyMem_REALLOC(self->ob_item, itemsize*self->ob_size); |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 1658 | self->allocated = self->ob_size; |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1659 | |
| 1660 | return 0; |
| 1661 | } |
| 1662 | else { |
| 1663 | /* assign slice */ |
| 1664 | int cur, i; |
| 1665 | arrayobject* av; |
| 1666 | |
| 1667 | if (!array_Check(value)) { |
| 1668 | PyErr_Format(PyExc_TypeError, |
| 1669 | "must assign array (not \"%.200s\") to slice", |
| 1670 | value->ob_type->tp_name); |
| 1671 | return -1; |
| 1672 | } |
| 1673 | |
| 1674 | av = (arrayobject*)value; |
| 1675 | |
| 1676 | if (av->ob_size != slicelength) { |
| 1677 | PyErr_Format(PyExc_ValueError, |
| 1678 | "attempt to assign array of size %d to extended slice of size %d", |
| 1679 | av->ob_size, slicelength); |
| 1680 | return -1; |
| 1681 | } |
| 1682 | |
| 1683 | if (!slicelength) |
| 1684 | return 0; |
| 1685 | |
| 1686 | /* protect against a[::-1] = a */ |
| 1687 | if (self == av) { |
| 1688 | value = array_slice(av, 0, av->ob_size); |
| 1689 | av = (arrayobject*)value; |
| 1690 | } |
| 1691 | else { |
| 1692 | Py_INCREF(value); |
| 1693 | } |
| 1694 | |
| 1695 | for (cur = start, i = 0; i < slicelength; |
| 1696 | cur += step, i++) { |
| 1697 | memcpy(self->ob_item + cur*itemsize, |
| 1698 | av->ob_item + i*itemsize, |
| 1699 | itemsize); |
| 1700 | } |
| 1701 | |
| 1702 | Py_DECREF(value); |
| 1703 | |
| 1704 | return 0; |
| 1705 | } |
| 1706 | } |
| 1707 | else { |
| 1708 | PyErr_SetString(PyExc_TypeError, |
| 1709 | "list indices must be integers"); |
| 1710 | return -1; |
| 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | static PyMappingMethods array_as_mapping = { |
| 1715 | (inquiry)array_length, |
| 1716 | (binaryfunc)array_subscr, |
| 1717 | (objobjargproc)array_ass_subscr |
| 1718 | }; |
| 1719 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1720 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 1721 | array_buffer_getreadbuf(arrayobject *self, int index, const void **ptr) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1722 | { |
| 1723 | if ( index != 0 ) { |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1724 | PyErr_SetString(PyExc_SystemError, |
| 1725 | "Accessing non-existent array segment"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1726 | return -1; |
| 1727 | } |
| 1728 | *ptr = (void *)self->ob_item; |
| 1729 | return self->ob_size*self->ob_descr->itemsize; |
| 1730 | } |
| 1731 | |
| 1732 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 1733 | array_buffer_getwritebuf(arrayobject *self, int index, const void **ptr) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1734 | { |
| 1735 | if ( index != 0 ) { |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1736 | PyErr_SetString(PyExc_SystemError, |
| 1737 | "Accessing non-existent array segment"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1738 | return -1; |
| 1739 | } |
| 1740 | *ptr = (void *)self->ob_item; |
| 1741 | return self->ob_size*self->ob_descr->itemsize; |
| 1742 | } |
| 1743 | |
| 1744 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 1745 | array_buffer_getsegcount(arrayobject *self, int *lenp) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1746 | { |
| 1747 | if ( lenp ) |
| 1748 | *lenp = self->ob_size*self->ob_descr->itemsize; |
| 1749 | return 1; |
| 1750 | } |
| 1751 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1752 | static PySequenceMethods array_as_sequence = { |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1753 | (inquiry)array_length, /*sq_length*/ |
| 1754 | (binaryfunc)array_concat, /*sq_concat*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1755 | (intargfunc)array_repeat, /*sq_repeat*/ |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1756 | (intargfunc)array_item, /*sq_item*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1757 | (intintargfunc)array_slice, /*sq_slice*/ |
| 1758 | (intobjargproc)array_ass_item, /*sq_ass_item*/ |
| 1759 | (intintobjargproc)array_ass_slice, /*sq_ass_slice*/ |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 1760 | (objobjproc)array_contains, /*sq_contains*/ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1761 | (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/ |
| 1762 | (intargfunc)array_inplace_repeat /*sq_inplace_repeat*/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1763 | }; |
| 1764 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1765 | static PyBufferProcs array_as_buffer = { |
| 1766 | (getreadbufferproc)array_buffer_getreadbuf, |
| 1767 | (getwritebufferproc)array_buffer_getwritebuf, |
| 1768 | (getsegcountproc)array_buffer_getsegcount, |
| 1769 | }; |
| 1770 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1771 | static PyObject * |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1772 | array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1773 | { |
| 1774 | char c; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1775 | PyObject *initial = NULL; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1776 | struct arraydescr *descr; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1777 | |
| 1778 | if (kwds != NULL) { |
| 1779 | int i = PyObject_Length(kwds); |
| 1780 | if (i < 0) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1781 | return NULL; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1782 | else if (i > 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1783 | PyErr_SetString(PyExc_TypeError, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1784 | "array.array constructor takes " |
| 1785 | "no keyword arguments"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1786 | return NULL; |
| 1787 | } |
| 1788 | } |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1789 | |
| 1790 | if (!PyArg_ParseTuple(args, "c|O:array", &c, &initial)) |
| 1791 | return NULL; |
| 1792 | |
| 1793 | if (!(initial == NULL || PyList_Check(initial) |
Raymond Hettinger | 84fc9aa | 2003-04-24 10:41:55 +0000 | [diff] [blame] | 1794 | || PyString_Check(initial) || PyTuple_Check(initial) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1795 | || (c == 'u' && PyUnicode_Check(initial)))) { |
| 1796 | PyErr_SetString(PyExc_TypeError, |
| 1797 | "array initializer must be list or string"); |
| 1798 | return NULL; |
| 1799 | } |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1800 | for (descr = descriptors; descr->typecode != '\0'; descr++) { |
| 1801 | if (descr->typecode == c) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1802 | PyObject *a; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1803 | int len; |
Raymond Hettinger | 84fc9aa | 2003-04-24 10:41:55 +0000 | [diff] [blame] | 1804 | |
| 1805 | if (initial == NULL || !(PyList_Check(initial) |
| 1806 | || PyTuple_Check(initial))) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1807 | len = 0; |
| 1808 | else |
Raymond Hettinger | 84fc9aa | 2003-04-24 10:41:55 +0000 | [diff] [blame] | 1809 | len = PySequence_Size(initial); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1810 | |
| 1811 | a = newarrayobject(type, len, descr); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1812 | if (a == NULL) |
| 1813 | return NULL; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1814 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1815 | if (len > 0) { |
| 1816 | int i; |
| 1817 | for (i = 0; i < len; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1818 | PyObject *v = |
Raymond Hettinger | 84fc9aa | 2003-04-24 10:41:55 +0000 | [diff] [blame] | 1819 | PySequence_GetItem(initial, i); |
Raymond Hettinger | 85004cc | 2003-08-05 11:23:59 +0000 | [diff] [blame] | 1820 | if (v == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1821 | Py_DECREF(a); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1822 | return NULL; |
| 1823 | } |
Raymond Hettinger | 85004cc | 2003-08-05 11:23:59 +0000 | [diff] [blame] | 1824 | if (setarrayitem(a, i, v) != 0) { |
| 1825 | Py_DECREF(v); |
| 1826 | Py_DECREF(a); |
| 1827 | return NULL; |
| 1828 | } |
| 1829 | Py_DECREF(v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1830 | } |
Raymond Hettinger | 85004cc | 2003-08-05 11:23:59 +0000 | [diff] [blame] | 1831 | } else if (initial != NULL && PyString_Check(initial)) { |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1832 | PyObject *t_initial = PyTuple_Pack(1, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1833 | initial); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1834 | PyObject *v = |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1835 | array_fromstring((arrayobject *)a, |
| 1836 | t_initial); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1837 | Py_DECREF(t_initial); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1838 | if (v == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1839 | Py_DECREF(a); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1840 | return NULL; |
| 1841 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1842 | Py_DECREF(v); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1843 | #ifdef Py_USING_UNICODE |
| 1844 | } else if (initial != NULL && PyUnicode_Check(initial)) { |
| 1845 | int n = PyUnicode_GET_DATA_SIZE(initial); |
| 1846 | if (n > 0) { |
| 1847 | arrayobject *self = (arrayobject *)a; |
| 1848 | char *item = self->ob_item; |
| 1849 | item = PyMem_Realloc(item, n); |
| 1850 | if (item == NULL) { |
| 1851 | PyErr_NoMemory(); |
| 1852 | Py_DECREF(a); |
| 1853 | return NULL; |
| 1854 | } |
| 1855 | self->ob_item = item; |
| 1856 | self->ob_size = n / sizeof(Py_UNICODE); |
| 1857 | memcpy(item, PyUnicode_AS_DATA(initial), n); |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 1858 | self->allocated = self->ob_size; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1859 | } |
| 1860 | #endif |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1861 | } |
| 1862 | return a; |
| 1863 | } |
| 1864 | } |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1865 | PyErr_SetString(PyExc_ValueError, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1866 | "bad typecode (must be c, b, B, u, h, H, i, I, l, L, f or d)"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1867 | return NULL; |
| 1868 | } |
| 1869 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1870 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1871 | PyDoc_STRVAR(module_doc, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1872 | "This module defines an object type which can efficiently represent\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1873 | an array of basic values: characters, integers, floating point\n\ |
| 1874 | numbers. Arrays are sequence types and behave very much like lists,\n\ |
| 1875 | except that the type of objects stored in them is constrained. The\n\ |
| 1876 | type is specified at object creation time by using a type code, which\n\ |
| 1877 | is a single character. The following type codes are defined:\n\ |
| 1878 | \n\ |
| 1879 | Type code C Type Minimum size in bytes \n\ |
| 1880 | 'c' character 1 \n\ |
| 1881 | 'b' signed integer 1 \n\ |
| 1882 | 'B' unsigned integer 1 \n\ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1883 | 'u' Unicode character 2 \n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1884 | 'h' signed integer 2 \n\ |
| 1885 | 'H' unsigned integer 2 \n\ |
| 1886 | 'i' signed integer 2 \n\ |
| 1887 | 'I' unsigned integer 2 \n\ |
| 1888 | 'l' signed integer 4 \n\ |
| 1889 | 'L' unsigned integer 4 \n\ |
| 1890 | 'f' floating point 4 \n\ |
| 1891 | 'd' floating point 8 \n\ |
| 1892 | \n\ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1893 | The constructor is:\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1894 | \n\ |
| 1895 | array(typecode [, initializer]) -- create a new array\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1896 | "); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1897 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1898 | PyDoc_STRVAR(arraytype_doc, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1899 | "array(typecode [, initializer]) -> array\n\ |
| 1900 | \n\ |
| 1901 | Return a new array whose items are restricted by typecode, and\n\ |
| 1902 | initialized from the optional initializer value, which must be a list\n\ |
| 1903 | or a string.\n\ |
| 1904 | \n\ |
| 1905 | Arrays represent basic values and behave very much like lists, except\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1906 | the type of objects stored in them is constrained.\n\ |
| 1907 | \n\ |
| 1908 | Methods:\n\ |
| 1909 | \n\ |
| 1910 | append() -- append a new item to the end of the array\n\ |
| 1911 | buffer_info() -- return information giving the current memory info\n\ |
| 1912 | byteswap() -- byteswap all the items of the array\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1913 | count() -- return number of occurences of an object\n\ |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 1914 | extend() -- extend array by appending multiple elements from an iterable\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1915 | fromfile() -- read items from a file object\n\ |
| 1916 | fromlist() -- append items from the list\n\ |
| 1917 | fromstring() -- append items from the string\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1918 | index() -- return index of first occurence of an object\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1919 | insert() -- insert a new item into the array at a provided position\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1920 | pop() -- remove and return item (default last)\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1921 | read() -- DEPRECATED, use fromfile()\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1922 | remove() -- remove first occurence of an object\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1923 | reverse() -- reverse the order of the items in the array\n\ |
| 1924 | tofile() -- write all items to a file object\n\ |
| 1925 | tolist() -- return the array converted to an ordinary list\n\ |
| 1926 | tostring() -- return the array converted to a string\n\ |
| 1927 | write() -- DEPRECATED, use tofile()\n\ |
| 1928 | \n\ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1929 | Attributes:\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1930 | \n\ |
| 1931 | typecode -- the typecode character used to create the array\n\ |
| 1932 | itemsize -- the length in bytes of one array item\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1933 | "); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1934 | |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 1935 | static PyObject *array_iter(arrayobject *ao); |
| 1936 | |
Tim Peters | 0c32279 | 2002-07-17 16:49:03 +0000 | [diff] [blame] | 1937 | static PyTypeObject Arraytype = { |
Fred Drake | 0d40ba4 | 2000-02-04 20:33:49 +0000 | [diff] [blame] | 1938 | PyObject_HEAD_INIT(NULL) |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1939 | 0, |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 1940 | "array.array", |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1941 | sizeof(arrayobject), |
| 1942 | 0, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1943 | (destructor)array_dealloc, /* tp_dealloc */ |
Martin v. Löwis | aa158be | 2002-03-04 09:38:52 +0000 | [diff] [blame] | 1944 | 0, /* tp_print */ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1945 | 0, /* tp_getattr */ |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1946 | 0, /* tp_setattr */ |
| 1947 | 0, /* tp_compare */ |
| 1948 | (reprfunc)array_repr, /* tp_repr */ |
| 1949 | 0, /* tp_as _number*/ |
| 1950 | &array_as_sequence, /* tp_as _sequence*/ |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1951 | &array_as_mapping, /* tp_as _mapping*/ |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1952 | 0, /* tp_hash */ |
| 1953 | 0, /* tp_call */ |
| 1954 | 0, /* tp_str */ |
Jason Tishler | fb8595d | 2003-01-06 12:41:26 +0000 | [diff] [blame] | 1955 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1956 | 0, /* tp_setattro */ |
Tim Peters | b870c75 | 2001-06-05 04:43:26 +0000 | [diff] [blame] | 1957 | &array_as_buffer, /* tp_as_buffer*/ |
Raymond Hettinger | cb87bc8 | 2004-05-31 00:35:52 +0000 | [diff] [blame] | 1958 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1959 | arraytype_doc, /* tp_doc */ |
| 1960 | 0, /* tp_traverse */ |
| 1961 | 0, /* tp_clear */ |
| 1962 | array_richcompare, /* tp_richcompare */ |
Raymond Hettinger | cb87bc8 | 2004-05-31 00:35:52 +0000 | [diff] [blame] | 1963 | offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */ |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 1964 | (getiterfunc)array_iter, /* tp_iter */ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1965 | 0, /* tp_iternext */ |
| 1966 | array_methods, /* tp_methods */ |
| 1967 | 0, /* tp_members */ |
| 1968 | array_getsets, /* tp_getset */ |
| 1969 | 0, /* tp_base */ |
| 1970 | 0, /* tp_dict */ |
| 1971 | 0, /* tp_descr_get */ |
| 1972 | 0, /* tp_descr_set */ |
| 1973 | 0, /* tp_dictoffset */ |
| 1974 | 0, /* tp_init */ |
Jason Tishler | fb8595d | 2003-01-06 12:41:26 +0000 | [diff] [blame] | 1975 | PyType_GenericAlloc, /* tp_alloc */ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1976 | array_new, /* tp_new */ |
Jason Tishler | fb8595d | 2003-01-06 12:41:26 +0000 | [diff] [blame] | 1977 | PyObject_Del, /* tp_free */ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1978 | }; |
| 1979 | |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 1980 | |
| 1981 | /*********************** Array Iterator **************************/ |
| 1982 | |
| 1983 | typedef struct { |
| 1984 | PyObject_HEAD |
| 1985 | long index; |
| 1986 | arrayobject *ao; |
| 1987 | PyObject * (*getitem)(struct arrayobject *, int); |
| 1988 | } arrayiterobject; |
| 1989 | |
| 1990 | static PyTypeObject PyArrayIter_Type; |
| 1991 | |
| 1992 | #define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type) |
| 1993 | |
| 1994 | static PyObject * |
| 1995 | array_iter(arrayobject *ao) |
| 1996 | { |
| 1997 | arrayiterobject *it; |
| 1998 | |
| 1999 | if (!array_Check(ao)) { |
| 2000 | PyErr_BadInternalCall(); |
| 2001 | return NULL; |
| 2002 | } |
| 2003 | |
| 2004 | it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type); |
| 2005 | if (it == NULL) |
| 2006 | return NULL; |
| 2007 | |
| 2008 | Py_INCREF(ao); |
| 2009 | it->ao = ao; |
| 2010 | it->index = 0; |
| 2011 | it->getitem = ao->ob_descr->getitem; |
| 2012 | PyObject_GC_Track(it); |
| 2013 | return (PyObject *)it; |
| 2014 | } |
| 2015 | |
| 2016 | static PyObject * |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2017 | arrayiter_next(arrayiterobject *it) |
| 2018 | { |
| 2019 | assert(PyArrayIter_Check(it)); |
| 2020 | if (it->index < it->ao->ob_size) |
| 2021 | return (*it->getitem)(it->ao, it->index++); |
| 2022 | return NULL; |
| 2023 | } |
| 2024 | |
| 2025 | static void |
| 2026 | arrayiter_dealloc(arrayiterobject *it) |
| 2027 | { |
| 2028 | PyObject_GC_UnTrack(it); |
| 2029 | Py_XDECREF(it->ao); |
| 2030 | PyObject_GC_Del(it); |
| 2031 | } |
| 2032 | |
| 2033 | static int |
| 2034 | arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg) |
| 2035 | { |
| 2036 | if (it->ao != NULL) |
| 2037 | return visit((PyObject *)(it->ao), arg); |
| 2038 | return 0; |
| 2039 | } |
| 2040 | |
| 2041 | static PyTypeObject PyArrayIter_Type = { |
Jason Tishler | 8437570 | 2003-02-10 20:45:47 +0000 | [diff] [blame] | 2042 | PyObject_HEAD_INIT(NULL) |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2043 | 0, /* ob_size */ |
| 2044 | "arrayiterator", /* tp_name */ |
| 2045 | sizeof(arrayiterobject), /* tp_basicsize */ |
| 2046 | 0, /* tp_itemsize */ |
| 2047 | /* methods */ |
| 2048 | (destructor)arrayiter_dealloc, /* tp_dealloc */ |
| 2049 | 0, /* tp_print */ |
| 2050 | 0, /* tp_getattr */ |
| 2051 | 0, /* tp_setattr */ |
| 2052 | 0, /* tp_compare */ |
| 2053 | 0, /* tp_repr */ |
| 2054 | 0, /* tp_as_number */ |
| 2055 | 0, /* tp_as_sequence */ |
| 2056 | 0, /* tp_as_mapping */ |
| 2057 | 0, /* tp_hash */ |
| 2058 | 0, /* tp_call */ |
| 2059 | 0, /* tp_str */ |
| 2060 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2061 | 0, /* tp_setattro */ |
| 2062 | 0, /* tp_as_buffer */ |
| 2063 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 2064 | 0, /* tp_doc */ |
| 2065 | (traverseproc)arrayiter_traverse, /* tp_traverse */ |
| 2066 | 0, /* tp_clear */ |
| 2067 | 0, /* tp_richcompare */ |
| 2068 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 2069 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2070 | (iternextfunc)arrayiter_next, /* tp_iternext */ |
| 2071 | 0, /* tp_methods */ |
| 2072 | }; |
| 2073 | |
| 2074 | |
| 2075 | /*********************** Install Module **************************/ |
| 2076 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2077 | /* No functions in array module. */ |
| 2078 | static PyMethodDef a_methods[] = { |
| 2079 | {NULL, NULL, 0, NULL} /* Sentinel */ |
| 2080 | }; |
| 2081 | |
| 2082 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 2083 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 2084 | initarray(void) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 2085 | { |
Fred Drake | f4e3484 | 2002-04-01 03:45:06 +0000 | [diff] [blame] | 2086 | PyObject *m; |
Fred Drake | 0d40ba4 | 2000-02-04 20:33:49 +0000 | [diff] [blame] | 2087 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2088 | Arraytype.ob_type = &PyType_Type; |
Jason Tishler | 8437570 | 2003-02-10 20:45:47 +0000 | [diff] [blame] | 2089 | PyArrayIter_Type.ob_type = &PyType_Type; |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2090 | m = Py_InitModule3("array", a_methods, module_doc); |
Fred Drake | f4e3484 | 2002-04-01 03:45:06 +0000 | [diff] [blame] | 2091 | |
| 2092 | Py_INCREF((PyObject *)&Arraytype); |
| 2093 | PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype); |
| 2094 | Py_INCREF((PyObject *)&Arraytype); |
| 2095 | PyModule_AddObject(m, "array", (PyObject *)&Arraytype); |
Guido van Rossum | a0deb64 | 1998-10-14 13:45:06 +0000 | [diff] [blame] | 2096 | /* No need to check the error here, the caller will do that */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 2097 | } |