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