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