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 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 { |
Victor Stinner | 9f0b51e | 2010-11-09 09:38:30 +0000 | [diff] [blame] | 25 | Py_UNICODE typecode; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 26 | int itemsize; |
| 27 | PyObject * (*getitem)(struct arrayobject *, Py_ssize_t); |
| 28 | int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *); |
| 29 | char *formats; |
| 30 | int is_integer_type; |
| 31 | int is_signed; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | typedef struct arrayobject { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 35 | PyObject_VAR_HEAD |
| 36 | char *ob_item; |
| 37 | Py_ssize_t allocated; |
| 38 | struct arraydescr *ob_descr; |
| 39 | PyObject *weakreflist; /* List of weak references */ |
| 40 | int ob_exports; /* Number of exported buffers */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 41 | } arrayobject; |
| 42 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 43 | static PyTypeObject Arraytype; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 44 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 45 | #define array_Check(op) PyObject_TypeCheck(op, &Arraytype) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 46 | #define array_CheckExact(op) (Py_TYPE(op) == &Arraytype) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 47 | |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 48 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 49 | array_resize(arrayobject *self, Py_ssize_t newsize) |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 50 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 51 | char *items; |
| 52 | size_t _new_size; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 53 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 54 | if (self->ob_exports > 0 && newsize != Py_SIZE(self)) { |
| 55 | PyErr_SetString(PyExc_BufferError, |
| 56 | "cannot resize an array that is exporting buffers"); |
| 57 | return -1; |
| 58 | } |
Antoine Pitrou | 3ad3a0d | 2008-12-18 17:08:32 +0000 | [diff] [blame] | 59 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | /* Bypass realloc() when a previous overallocation is large enough |
| 61 | to accommodate the newsize. If the newsize is 16 smaller than the |
| 62 | current size, then proceed with the realloc() to shrink the array. |
| 63 | */ |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 64 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 65 | if (self->allocated >= newsize && |
| 66 | Py_SIZE(self) < newsize + 16 && |
| 67 | self->ob_item != NULL) { |
| 68 | Py_SIZE(self) = newsize; |
| 69 | return 0; |
| 70 | } |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 71 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | if (newsize == 0) { |
| 73 | PyMem_FREE(self->ob_item); |
| 74 | self->ob_item = NULL; |
| 75 | Py_SIZE(self) = 0; |
| 76 | self->allocated = 0; |
| 77 | return 0; |
| 78 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 79 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 80 | /* This over-allocates proportional to the array size, making room |
| 81 | * for additional growth. The over-allocation is mild, but is |
| 82 | * enough to give linear-time amortized behavior over a long |
| 83 | * sequence of appends() in the presence of a poorly-performing |
| 84 | * system realloc(). |
| 85 | * The growth pattern is: 0, 4, 8, 16, 25, 34, 46, 56, 67, 79, ... |
| 86 | * Note, the pattern starts out the same as for lists but then |
| 87 | * grows at a smaller rate so that larger arrays only overallocate |
| 88 | * by about 1/16th -- this is done because arrays are presumed to be more |
| 89 | * memory critical. |
| 90 | */ |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 91 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 92 | _new_size = (newsize >> 4) + (Py_SIZE(self) < 8 ? 3 : 7) + newsize; |
| 93 | items = self->ob_item; |
| 94 | /* XXX The following multiplication and division does not optimize away |
| 95 | like it does for lists since the size is not known at compile time */ |
| 96 | if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize)) |
| 97 | PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize)); |
| 98 | else |
| 99 | items = NULL; |
| 100 | if (items == NULL) { |
| 101 | PyErr_NoMemory(); |
| 102 | return -1; |
| 103 | } |
| 104 | self->ob_item = items; |
| 105 | Py_SIZE(self) = newsize; |
| 106 | self->allocated = _new_size; |
| 107 | return 0; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 110 | /**************************************************************************** |
| 111 | Get and Set functions for each type. |
| 112 | A Get function takes an arrayobject* and an integer index, returning the |
| 113 | array value at that index wrapped in an appropriate PyObject*. |
| 114 | A Set function takes an arrayobject, integer index, and PyObject*; sets |
| 115 | the array value at that index to the raw C data extracted from the PyObject*, |
| 116 | and returns 0 if successful, else nonzero on failure (PyObject* not of an |
| 117 | appropriate type or value). |
| 118 | Note that the basic Get and Set functions do NOT check that the index is |
| 119 | in bounds; that's the responsibility of the caller. |
| 120 | ****************************************************************************/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 121 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 122 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 123 | b_getitem(arrayobject *ap, Py_ssize_t i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 124 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 125 | long x = ((char *)ap->ob_item)[i]; |
| 126 | if (x >= 128) |
| 127 | x -= 256; |
| 128 | return PyLong_FromLong(x); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 132 | b_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 133 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 134 | short x; |
| 135 | /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore |
| 136 | must use the next size up that is signed ('h') and manually do |
| 137 | the overflow checking */ |
| 138 | if (!PyArg_Parse(v, "h;array item must be integer", &x)) |
| 139 | return -1; |
| 140 | else if (x < -128) { |
| 141 | PyErr_SetString(PyExc_OverflowError, |
| 142 | "signed char is less than minimum"); |
| 143 | return -1; |
| 144 | } |
| 145 | else if (x > 127) { |
| 146 | PyErr_SetString(PyExc_OverflowError, |
| 147 | "signed char is greater than maximum"); |
| 148 | return -1; |
| 149 | } |
| 150 | if (i >= 0) |
| 151 | ((char *)ap->ob_item)[i] = (char)x; |
| 152 | return 0; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 155 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 156 | BB_getitem(arrayobject *ap, Py_ssize_t i) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 157 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | long x = ((unsigned char *)ap->ob_item)[i]; |
| 159 | return PyLong_FromLong(x); |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 162 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 163 | BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 164 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | unsigned char x; |
| 166 | /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */ |
| 167 | if (!PyArg_Parse(v, "b;array item must be integer", &x)) |
| 168 | return -1; |
| 169 | if (i >= 0) |
| 170 | ((char *)ap->ob_item)[i] = x; |
| 171 | return 0; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 172 | } |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 173 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 174 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 175 | u_getitem(arrayobject *ap, Py_ssize_t i) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 176 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 177 | return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 181 | u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 182 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 183 | Py_UNICODE *p; |
| 184 | Py_ssize_t len; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 185 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 186 | if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len)) |
| 187 | return -1; |
| 188 | if (len != 1) { |
| 189 | PyErr_SetString(PyExc_TypeError, |
| 190 | "array item must be unicode character"); |
| 191 | return -1; |
| 192 | } |
| 193 | if (i >= 0) |
| 194 | ((Py_UNICODE *)ap->ob_item)[i] = p[0]; |
| 195 | return 0; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 196 | } |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 197 | |
Travis E. Oliphant | d5c0add | 2007-10-12 22:05:15 +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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 202 | return PyLong_FromLong((long) ((short *)ap->ob_item)[i]); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Travis E. Oliphant | d5c0add | 2007-10-12 22:05:15 +0000 | [diff] [blame] | 205 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 206 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 207 | h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 208 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | short x; |
| 210 | /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */ |
| 211 | if (!PyArg_Parse(v, "h;array item must be integer", &x)) |
| 212 | return -1; |
| 213 | if (i >= 0) |
| 214 | ((short *)ap->ob_item)[i] = x; |
| 215 | return 0; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 218 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 219 | HH_getitem(arrayobject *ap, Py_ssize_t i) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 220 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 221 | return PyLong_FromLong((long) ((unsigned short *)ap->ob_item)[i]); |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 224 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 225 | HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 226 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 227 | int x; |
| 228 | /* PyArg_Parse's 'h' formatter is for a signed short, therefore |
| 229 | must use the next size up and manually do the overflow checking */ |
| 230 | if (!PyArg_Parse(v, "i;array item must be integer", &x)) |
| 231 | return -1; |
| 232 | else if (x < 0) { |
| 233 | PyErr_SetString(PyExc_OverflowError, |
| 234 | "unsigned short is less than minimum"); |
| 235 | return -1; |
| 236 | } |
| 237 | else if (x > USHRT_MAX) { |
| 238 | PyErr_SetString(PyExc_OverflowError, |
| 239 | "unsigned short is greater than maximum"); |
| 240 | return -1; |
| 241 | } |
| 242 | if (i >= 0) |
| 243 | ((short *)ap->ob_item)[i] = (short)x; |
| 244 | return 0; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 245 | } |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 246 | |
| 247 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 248 | i_getitem(arrayobject *ap, Py_ssize_t i) |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 249 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 | return PyLong_FromLong((long) ((int *)ap->ob_item)[i]); |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 254 | i_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 255 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 256 | int x; |
| 257 | /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */ |
| 258 | if (!PyArg_Parse(v, "i;array item must be integer", &x)) |
| 259 | return -1; |
| 260 | if (i >= 0) |
| 261 | ((int *)ap->ob_item)[i] = x; |
| 262 | return 0; |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 265 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 266 | II_getitem(arrayobject *ap, Py_ssize_t i) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 267 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 268 | return PyLong_FromUnsignedLong( |
| 269 | (unsigned long) ((unsigned int *)ap->ob_item)[i]); |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 273 | II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 274 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 275 | unsigned long x; |
| 276 | if (PyLong_Check(v)) { |
| 277 | x = PyLong_AsUnsignedLong(v); |
| 278 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 279 | return -1; |
| 280 | } |
| 281 | else { |
| 282 | long y; |
| 283 | if (!PyArg_Parse(v, "l;array item must be integer", &y)) |
| 284 | return -1; |
| 285 | if (y < 0) { |
| 286 | PyErr_SetString(PyExc_OverflowError, |
| 287 | "unsigned int is less than minimum"); |
| 288 | return -1; |
| 289 | } |
| 290 | x = (unsigned long)y; |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 291 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | } |
| 293 | if (x > UINT_MAX) { |
| 294 | PyErr_SetString(PyExc_OverflowError, |
| 295 | "unsigned int is greater than maximum"); |
| 296 | return -1; |
| 297 | } |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | if (i >= 0) |
| 300 | ((unsigned int *)ap->ob_item)[i] = (unsigned int)x; |
| 301 | return 0; |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 305 | l_getitem(arrayobject *ap, Py_ssize_t i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 306 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 307 | return PyLong_FromLong(((long *)ap->ob_item)[i]); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 311 | l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 312 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 313 | long x; |
| 314 | if (!PyArg_Parse(v, "l;array item must be integer", &x)) |
| 315 | return -1; |
| 316 | if (i >= 0) |
| 317 | ((long *)ap->ob_item)[i] = x; |
| 318 | return 0; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 321 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 322 | LL_getitem(arrayobject *ap, Py_ssize_t i) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 323 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 324 | return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]); |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 328 | LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 329 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 330 | unsigned long x; |
| 331 | if (PyLong_Check(v)) { |
| 332 | x = PyLong_AsUnsignedLong(v); |
| 333 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 334 | return -1; |
| 335 | } |
| 336 | else { |
| 337 | long y; |
| 338 | if (!PyArg_Parse(v, "l;array item must be integer", &y)) |
| 339 | return -1; |
| 340 | if (y < 0) { |
| 341 | PyErr_SetString(PyExc_OverflowError, |
| 342 | "unsigned long is less than minimum"); |
| 343 | return -1; |
| 344 | } |
| 345 | x = (unsigned long)y; |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 346 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 347 | } |
| 348 | if (x > ULONG_MAX) { |
| 349 | PyErr_SetString(PyExc_OverflowError, |
| 350 | "unsigned long is greater than maximum"); |
| 351 | return -1; |
| 352 | } |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 353 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 354 | if (i >= 0) |
| 355 | ((unsigned long *)ap->ob_item)[i] = x; |
| 356 | return 0; |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 360 | f_getitem(arrayobject *ap, Py_ssize_t i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 361 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 362 | return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 366 | f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 367 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 368 | float x; |
| 369 | if (!PyArg_Parse(v, "f;array item must be float", &x)) |
| 370 | return -1; |
| 371 | if (i >= 0) |
| 372 | ((float *)ap->ob_item)[i] = x; |
| 373 | return 0; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 376 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 377 | d_getitem(arrayobject *ap, Py_ssize_t i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 378 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 379 | return PyFloat_FromDouble(((double *)ap->ob_item)[i]); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 383 | d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 384 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 385 | double x; |
| 386 | if (!PyArg_Parse(v, "d;array item must be float", &x)) |
| 387 | return -1; |
| 388 | if (i >= 0) |
| 389 | ((double *)ap->ob_item)[i] = x; |
| 390 | return 0; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 393 | |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 394 | /* Description of types. |
| 395 | * |
| 396 | * Don't forget to update typecode_to_mformat_code() if you add a new |
| 397 | * typecode. |
| 398 | */ |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 399 | static struct arraydescr descriptors[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 400 | {'b', 1, b_getitem, b_setitem, "b", 1, 1}, |
| 401 | {'B', 1, BB_getitem, BB_setitem, "B", 1, 0}, |
| 402 | {'u', sizeof(Py_UNICODE), u_getitem, u_setitem, "u", 0, 0}, |
| 403 | {'h', sizeof(short), h_getitem, h_setitem, "h", 1, 1}, |
| 404 | {'H', sizeof(short), HH_getitem, HH_setitem, "H", 1, 0}, |
| 405 | {'i', sizeof(int), i_getitem, i_setitem, "i", 1, 1}, |
| 406 | {'I', sizeof(int), II_getitem, II_setitem, "I", 1, 0}, |
| 407 | {'l', sizeof(long), l_getitem, l_setitem, "l", 1, 1}, |
| 408 | {'L', sizeof(long), LL_getitem, LL_setitem, "L", 1, 0}, |
| 409 | {'f', sizeof(float), f_getitem, f_setitem, "f", 0, 0}, |
| 410 | {'d', sizeof(double), d_getitem, d_setitem, "d", 0, 0}, |
| 411 | {'\0', 0, 0, 0, 0, 0, 0} /* Sentinel */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 412 | }; |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 413 | |
| 414 | /**************************************************************************** |
| 415 | Implementations of array object methods. |
| 416 | ****************************************************************************/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 417 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 418 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 419 | newarrayobject(PyTypeObject *type, Py_ssize_t size, struct arraydescr *descr) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 420 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 421 | arrayobject *op; |
| 422 | size_t nbytes; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 423 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 424 | if (size < 0) { |
| 425 | PyErr_BadInternalCall(); |
| 426 | return NULL; |
| 427 | } |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 428 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 429 | nbytes = size * descr->itemsize; |
| 430 | /* Check for overflow */ |
| 431 | if (nbytes / descr->itemsize != (size_t)size) { |
| 432 | return PyErr_NoMemory(); |
| 433 | } |
| 434 | op = (arrayobject *) type->tp_alloc(type, 0); |
| 435 | if (op == NULL) { |
| 436 | return NULL; |
| 437 | } |
| 438 | op->ob_descr = descr; |
| 439 | op->allocated = size; |
| 440 | op->weakreflist = NULL; |
| 441 | Py_SIZE(op) = size; |
| 442 | if (size <= 0) { |
| 443 | op->ob_item = NULL; |
| 444 | } |
| 445 | else { |
| 446 | op->ob_item = PyMem_NEW(char, nbytes); |
| 447 | if (op->ob_item == NULL) { |
| 448 | Py_DECREF(op); |
| 449 | return PyErr_NoMemory(); |
| 450 | } |
| 451 | } |
| 452 | op->ob_exports = 0; |
| 453 | return (PyObject *) op; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 456 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 457 | getarrayitem(PyObject *op, Py_ssize_t i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 458 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 459 | register arrayobject *ap; |
| 460 | assert(array_Check(op)); |
| 461 | ap = (arrayobject *)op; |
| 462 | assert(i>=0 && i<Py_SIZE(ap)); |
| 463 | return (*ap->ob_descr->getitem)(ap, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 467 | ins1(arrayobject *self, Py_ssize_t where, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 468 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 469 | char *items; |
| 470 | Py_ssize_t n = Py_SIZE(self); |
| 471 | if (v == NULL) { |
| 472 | PyErr_BadInternalCall(); |
| 473 | return -1; |
| 474 | } |
| 475 | if ((*self->ob_descr->setitem)(self, -1, v) < 0) |
| 476 | return -1; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 477 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 478 | if (array_resize(self, n+1) == -1) |
| 479 | return -1; |
| 480 | items = self->ob_item; |
| 481 | if (where < 0) { |
| 482 | where += n; |
| 483 | if (where < 0) |
| 484 | where = 0; |
| 485 | } |
| 486 | if (where > n) |
| 487 | where = n; |
| 488 | /* appends don't need to call memmove() */ |
| 489 | if (where != n) |
| 490 | memmove(items + (where+1)*self->ob_descr->itemsize, |
| 491 | items + where*self->ob_descr->itemsize, |
| 492 | (n-where)*self->ob_descr->itemsize); |
| 493 | return (*self->ob_descr->setitem)(self, where, v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 496 | /* Methods */ |
| 497 | |
| 498 | static void |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 499 | array_dealloc(arrayobject *op) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 500 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 501 | if (op->weakreflist != NULL) |
| 502 | PyObject_ClearWeakRefs((PyObject *) op); |
| 503 | if (op->ob_item != NULL) |
| 504 | PyMem_DEL(op->ob_item); |
| 505 | Py_TYPE(op)->tp_free((PyObject *)op); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 508 | static PyObject * |
| 509 | array_richcompare(PyObject *v, PyObject *w, int op) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 510 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 511 | arrayobject *va, *wa; |
| 512 | PyObject *vi = NULL; |
| 513 | PyObject *wi = NULL; |
| 514 | Py_ssize_t i, k; |
| 515 | PyObject *res; |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 516 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 517 | if (!array_Check(v) || !array_Check(w)) { |
| 518 | Py_INCREF(Py_NotImplemented); |
| 519 | return Py_NotImplemented; |
| 520 | } |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 521 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 522 | va = (arrayobject *)v; |
| 523 | wa = (arrayobject *)w; |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 524 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 525 | if (Py_SIZE(va) != Py_SIZE(wa) && (op == Py_EQ || op == Py_NE)) { |
| 526 | /* Shortcut: if the lengths differ, the arrays differ */ |
| 527 | if (op == Py_EQ) |
| 528 | res = Py_False; |
| 529 | else |
| 530 | res = Py_True; |
| 531 | Py_INCREF(res); |
| 532 | return res; |
| 533 | } |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 534 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 535 | /* Search for the first index where items are different */ |
| 536 | k = 1; |
| 537 | for (i = 0; i < Py_SIZE(va) && i < Py_SIZE(wa); i++) { |
| 538 | vi = getarrayitem(v, i); |
| 539 | wi = getarrayitem(w, i); |
| 540 | if (vi == NULL || wi == NULL) { |
| 541 | Py_XDECREF(vi); |
| 542 | Py_XDECREF(wi); |
| 543 | return NULL; |
| 544 | } |
| 545 | k = PyObject_RichCompareBool(vi, wi, Py_EQ); |
| 546 | if (k == 0) |
| 547 | break; /* Keeping vi and wi alive! */ |
| 548 | Py_DECREF(vi); |
| 549 | Py_DECREF(wi); |
| 550 | if (k < 0) |
| 551 | return NULL; |
| 552 | } |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 553 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 554 | if (k) { |
| 555 | /* No more items to compare -- compare sizes */ |
| 556 | Py_ssize_t vs = Py_SIZE(va); |
| 557 | Py_ssize_t ws = Py_SIZE(wa); |
| 558 | int cmp; |
| 559 | switch (op) { |
| 560 | case Py_LT: cmp = vs < ws; break; |
| 561 | case Py_LE: cmp = vs <= ws; break; |
| 562 | case Py_EQ: cmp = vs == ws; break; |
| 563 | case Py_NE: cmp = vs != ws; break; |
| 564 | case Py_GT: cmp = vs > ws; break; |
| 565 | case Py_GE: cmp = vs >= ws; break; |
| 566 | default: return NULL; /* cannot happen */ |
| 567 | } |
| 568 | if (cmp) |
| 569 | res = Py_True; |
| 570 | else |
| 571 | res = Py_False; |
| 572 | Py_INCREF(res); |
| 573 | return res; |
| 574 | } |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 575 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 576 | /* We have an item that differs. First, shortcuts for EQ/NE */ |
| 577 | if (op == Py_EQ) { |
| 578 | Py_INCREF(Py_False); |
| 579 | res = Py_False; |
| 580 | } |
| 581 | else if (op == Py_NE) { |
| 582 | Py_INCREF(Py_True); |
| 583 | res = Py_True; |
| 584 | } |
| 585 | else { |
| 586 | /* Compare the final item again using the proper operator */ |
| 587 | res = PyObject_RichCompare(vi, wi, op); |
| 588 | } |
| 589 | Py_DECREF(vi); |
| 590 | Py_DECREF(wi); |
| 591 | return res; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 594 | static Py_ssize_t |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 595 | array_length(arrayobject *a) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 596 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 597 | return Py_SIZE(a); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 600 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 601 | array_item(arrayobject *a, Py_ssize_t i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 602 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 603 | if (i < 0 || i >= Py_SIZE(a)) { |
| 604 | PyErr_SetString(PyExc_IndexError, "array index out of range"); |
| 605 | return NULL; |
| 606 | } |
| 607 | return getarrayitem((PyObject *)a, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 610 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 611 | 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] | 612 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 613 | arrayobject *np; |
| 614 | if (ilow < 0) |
| 615 | ilow = 0; |
| 616 | else if (ilow > Py_SIZE(a)) |
| 617 | ilow = Py_SIZE(a); |
| 618 | if (ihigh < 0) |
| 619 | ihigh = 0; |
| 620 | if (ihigh < ilow) |
| 621 | ihigh = ilow; |
| 622 | else if (ihigh > Py_SIZE(a)) |
| 623 | ihigh = Py_SIZE(a); |
| 624 | np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr); |
| 625 | if (np == NULL) |
| 626 | return NULL; |
| 627 | memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize, |
| 628 | (ihigh-ilow) * a->ob_descr->itemsize); |
| 629 | return (PyObject *)np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 632 | static PyObject * |
Raymond Hettinger | 3aa82c0 | 2004-03-13 18:18:51 +0000 | [diff] [blame] | 633 | array_copy(arrayobject *a, PyObject *unused) |
| 634 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 635 | return array_slice(a, 0, Py_SIZE(a)); |
Raymond Hettinger | 3aa82c0 | 2004-03-13 18:18:51 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | PyDoc_STRVAR(copy_doc, |
| 639 | "copy(array)\n\ |
| 640 | \n\ |
| 641 | Return a copy of the array."); |
| 642 | |
| 643 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 644 | array_concat(arrayobject *a, PyObject *bb) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 645 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 646 | Py_ssize_t size; |
| 647 | arrayobject *np; |
| 648 | if (!array_Check(bb)) { |
| 649 | PyErr_Format(PyExc_TypeError, |
| 650 | "can only append array (not \"%.200s\") to array", |
| 651 | Py_TYPE(bb)->tp_name); |
| 652 | return NULL; |
| 653 | } |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 654 | #define b ((arrayobject *)bb) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 655 | if (a->ob_descr != b->ob_descr) { |
| 656 | PyErr_BadArgument(); |
| 657 | return NULL; |
| 658 | } |
| 659 | if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) { |
| 660 | return PyErr_NoMemory(); |
| 661 | } |
| 662 | size = Py_SIZE(a) + Py_SIZE(b); |
| 663 | np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr); |
| 664 | if (np == NULL) { |
| 665 | return NULL; |
| 666 | } |
| 667 | memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize); |
| 668 | memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize, |
| 669 | b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize); |
| 670 | return (PyObject *)np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 671 | #undef b |
| 672 | } |
| 673 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 674 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 675 | array_repeat(arrayobject *a, Py_ssize_t n) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 676 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 677 | Py_ssize_t size; |
| 678 | arrayobject *np; |
Georg Brandl | c29cc6a | 2010-12-04 11:02:04 +0000 | [diff] [blame] | 679 | Py_ssize_t oldbytes, newbytes; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 680 | if (n < 0) |
| 681 | n = 0; |
| 682 | if ((Py_SIZE(a) != 0) && (n > PY_SSIZE_T_MAX / Py_SIZE(a))) { |
| 683 | return PyErr_NoMemory(); |
| 684 | } |
| 685 | size = Py_SIZE(a) * n; |
| 686 | np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr); |
| 687 | if (np == NULL) |
| 688 | return NULL; |
Georg Brandl | c29cc6a | 2010-12-04 11:02:04 +0000 | [diff] [blame] | 689 | if (n == 0) |
| 690 | return (PyObject *)np; |
| 691 | oldbytes = Py_SIZE(a) * a->ob_descr->itemsize; |
| 692 | newbytes = oldbytes * n; |
| 693 | /* this follows the code in unicode_repeat */ |
| 694 | if (oldbytes == 1) { |
| 695 | memset(np->ob_item, a->ob_item[0], newbytes); |
| 696 | } else { |
| 697 | Py_ssize_t done = oldbytes; |
| 698 | Py_MEMCPY(np->ob_item, a->ob_item, oldbytes); |
| 699 | while (done < newbytes) { |
| 700 | Py_ssize_t ncopy = (done <= newbytes-done) ? done : newbytes-done; |
| 701 | Py_MEMCPY(np->ob_item+done, np->ob_item, ncopy); |
| 702 | done += ncopy; |
| 703 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | } |
Georg Brandl | c29cc6a | 2010-12-04 11:02:04 +0000 | [diff] [blame] | 705 | return (PyObject *)np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | static int |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 709 | 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] | 710 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 711 | char *item; |
| 712 | Py_ssize_t n; /* Size of replacement array */ |
| 713 | Py_ssize_t d; /* Change in size */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 714 | #define b ((arrayobject *)v) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 715 | if (v == NULL) |
| 716 | n = 0; |
| 717 | else if (array_Check(v)) { |
| 718 | n = Py_SIZE(b); |
| 719 | if (a == b) { |
| 720 | /* Special case "a[i:j] = a" -- copy b first */ |
| 721 | int ret; |
| 722 | v = array_slice(b, 0, n); |
| 723 | if (!v) |
| 724 | return -1; |
| 725 | ret = array_ass_slice(a, ilow, ihigh, v); |
| 726 | Py_DECREF(v); |
| 727 | return ret; |
| 728 | } |
| 729 | if (b->ob_descr != a->ob_descr) { |
| 730 | PyErr_BadArgument(); |
| 731 | return -1; |
| 732 | } |
| 733 | } |
| 734 | else { |
| 735 | PyErr_Format(PyExc_TypeError, |
| 736 | "can only assign array (not \"%.200s\") to array slice", |
| 737 | Py_TYPE(v)->tp_name); |
| 738 | return -1; |
| 739 | } |
| 740 | if (ilow < 0) |
| 741 | ilow = 0; |
| 742 | else if (ilow > Py_SIZE(a)) |
| 743 | ilow = Py_SIZE(a); |
| 744 | if (ihigh < 0) |
| 745 | ihigh = 0; |
| 746 | if (ihigh < ilow) |
| 747 | ihigh = ilow; |
| 748 | else if (ihigh > Py_SIZE(a)) |
| 749 | ihigh = Py_SIZE(a); |
| 750 | item = a->ob_item; |
| 751 | d = n - (ihigh-ilow); |
| 752 | /* Issue #4509: If the array has exported buffers and the slice |
| 753 | assignment would change the size of the array, fail early to make |
| 754 | sure we don't modify it. */ |
| 755 | if (d != 0 && a->ob_exports > 0) { |
| 756 | PyErr_SetString(PyExc_BufferError, |
| 757 | "cannot resize an array that is exporting buffers"); |
| 758 | return -1; |
| 759 | } |
| 760 | if (d < 0) { /* Delete -d items */ |
| 761 | memmove(item + (ihigh+d)*a->ob_descr->itemsize, |
| 762 | item + ihigh*a->ob_descr->itemsize, |
| 763 | (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize); |
| 764 | if (array_resize(a, Py_SIZE(a) + d) == -1) |
| 765 | return -1; |
| 766 | } |
| 767 | else if (d > 0) { /* Insert d items */ |
| 768 | if (array_resize(a, Py_SIZE(a) + d)) |
| 769 | return -1; |
| 770 | memmove(item + (ihigh+d)*a->ob_descr->itemsize, |
| 771 | item + ihigh*a->ob_descr->itemsize, |
| 772 | (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize); |
| 773 | } |
| 774 | if (n > 0) |
| 775 | memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item, |
| 776 | n*b->ob_descr->itemsize); |
| 777 | return 0; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 778 | #undef b |
| 779 | } |
| 780 | |
| 781 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 782 | array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 783 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 784 | if (i < 0 || i >= Py_SIZE(a)) { |
| 785 | PyErr_SetString(PyExc_IndexError, |
| 786 | "array assignment index out of range"); |
| 787 | return -1; |
| 788 | } |
| 789 | if (v == NULL) |
| 790 | return array_ass_slice(a, i, i+1, v); |
| 791 | return (*a->ob_descr->setitem)(a, i, v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 795 | setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 796 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 797 | assert(array_Check(a)); |
| 798 | return array_ass_item((arrayobject *)a, i, v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 801 | static int |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 802 | array_iter_extend(arrayobject *self, PyObject *bb) |
| 803 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 804 | PyObject *it, *v; |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 805 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 806 | it = PyObject_GetIter(bb); |
| 807 | if (it == NULL) |
| 808 | return -1; |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 809 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 810 | while ((v = PyIter_Next(it)) != NULL) { |
Mark Dickinson | 346f0af | 2010-08-06 09:36:57 +0000 | [diff] [blame] | 811 | if (ins1(self, Py_SIZE(self), v) != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 812 | Py_DECREF(v); |
| 813 | Py_DECREF(it); |
| 814 | return -1; |
| 815 | } |
| 816 | Py_DECREF(v); |
| 817 | } |
| 818 | Py_DECREF(it); |
| 819 | if (PyErr_Occurred()) |
| 820 | return -1; |
| 821 | return 0; |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | static int |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 825 | array_do_extend(arrayobject *self, PyObject *bb) |
| 826 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 827 | Py_ssize_t size, oldsize, bbsize; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 828 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 829 | if (!array_Check(bb)) |
| 830 | return array_iter_extend(self, bb); |
| 831 | #define b ((arrayobject *)bb) |
| 832 | if (self->ob_descr != b->ob_descr) { |
| 833 | PyErr_SetString(PyExc_TypeError, |
| 834 | "can only extend with array of same kind"); |
| 835 | return -1; |
| 836 | } |
| 837 | if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) || |
| 838 | ((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) { |
| 839 | PyErr_NoMemory(); |
| 840 | return -1; |
| 841 | } |
| 842 | oldsize = Py_SIZE(self); |
| 843 | /* Get the size of bb before resizing the array since bb could be self. */ |
| 844 | bbsize = Py_SIZE(bb); |
| 845 | size = oldsize + Py_SIZE(b); |
| 846 | if (array_resize(self, size) == -1) |
| 847 | return -1; |
| 848 | memcpy(self->ob_item + oldsize * self->ob_descr->itemsize, |
| 849 | b->ob_item, bbsize * b->ob_descr->itemsize); |
| 850 | |
| 851 | return 0; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 852 | #undef b |
| 853 | } |
| 854 | |
| 855 | static PyObject * |
| 856 | array_inplace_concat(arrayobject *self, PyObject *bb) |
| 857 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 858 | if (!array_Check(bb)) { |
| 859 | PyErr_Format(PyExc_TypeError, |
| 860 | "can only extend array with array (not \"%.200s\")", |
| 861 | Py_TYPE(bb)->tp_name); |
| 862 | return NULL; |
| 863 | } |
| 864 | if (array_do_extend(self, bb) == -1) |
| 865 | return NULL; |
| 866 | Py_INCREF(self); |
| 867 | return (PyObject *)self; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 871 | array_inplace_repeat(arrayobject *self, Py_ssize_t n) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 872 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 873 | char *items, *p; |
| 874 | Py_ssize_t size, i; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 876 | if (Py_SIZE(self) > 0) { |
| 877 | if (n < 0) |
| 878 | n = 0; |
| 879 | items = self->ob_item; |
| 880 | if ((self->ob_descr->itemsize != 0) && |
| 881 | (Py_SIZE(self) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) { |
| 882 | return PyErr_NoMemory(); |
| 883 | } |
| 884 | size = Py_SIZE(self) * self->ob_descr->itemsize; |
| 885 | if (n > 0 && size > PY_SSIZE_T_MAX / n) { |
| 886 | return PyErr_NoMemory(); |
| 887 | } |
| 888 | if (array_resize(self, n * Py_SIZE(self)) == -1) |
| 889 | return NULL; |
| 890 | items = p = self->ob_item; |
| 891 | for (i = 1; i < n; i++) { |
| 892 | p += size; |
| 893 | memcpy(p, items, size); |
| 894 | } |
| 895 | } |
| 896 | Py_INCREF(self); |
| 897 | return (PyObject *)self; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 901 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 902 | ins(arrayobject *self, Py_ssize_t where, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 903 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 904 | if (ins1(self, where, v) != 0) |
| 905 | return NULL; |
| 906 | Py_INCREF(Py_None); |
| 907 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 910 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 911 | array_count(arrayobject *self, PyObject *v) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 912 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 913 | Py_ssize_t count = 0; |
| 914 | Py_ssize_t i; |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 915 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 916 | for (i = 0; i < Py_SIZE(self); i++) { |
| 917 | PyObject *selfi = getarrayitem((PyObject *)self, i); |
| 918 | int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
| 919 | Py_DECREF(selfi); |
| 920 | if (cmp > 0) |
| 921 | count++; |
| 922 | else if (cmp < 0) |
| 923 | return NULL; |
| 924 | } |
| 925 | return PyLong_FromSsize_t(count); |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 928 | PyDoc_STRVAR(count_doc, |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 929 | "count(x)\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 930 | \n\ |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 931 | Return number of occurrences of x in the array."); |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 932 | |
| 933 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 934 | array_index(arrayobject *self, PyObject *v) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 935 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 936 | Py_ssize_t i; |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 938 | for (i = 0; i < Py_SIZE(self); i++) { |
| 939 | PyObject *selfi = getarrayitem((PyObject *)self, i); |
| 940 | int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
| 941 | Py_DECREF(selfi); |
| 942 | if (cmp > 0) { |
| 943 | return PyLong_FromLong((long)i); |
| 944 | } |
| 945 | else if (cmp < 0) |
| 946 | return NULL; |
| 947 | } |
| 948 | PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list"); |
| 949 | return NULL; |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 950 | } |
| 951 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 952 | PyDoc_STRVAR(index_doc, |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 953 | "index(x)\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 954 | \n\ |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 955 | Return index of first occurrence of x in the array."); |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 956 | |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 957 | static int |
| 958 | array_contains(arrayobject *self, PyObject *v) |
| 959 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 960 | Py_ssize_t i; |
| 961 | int cmp; |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 962 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 963 | for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) { |
| 964 | PyObject *selfi = getarrayitem((PyObject *)self, i); |
| 965 | cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
| 966 | Py_DECREF(selfi); |
| 967 | } |
| 968 | return cmp; |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 971 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 972 | array_remove(arrayobject *self, PyObject *v) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 973 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 974 | int i; |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 975 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 976 | for (i = 0; i < Py_SIZE(self); i++) { |
| 977 | PyObject *selfi = getarrayitem((PyObject *)self,i); |
| 978 | int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
| 979 | Py_DECREF(selfi); |
| 980 | if (cmp > 0) { |
| 981 | if (array_ass_slice(self, i, i+1, |
| 982 | (PyObject *)NULL) != 0) |
| 983 | return NULL; |
| 984 | Py_INCREF(Py_None); |
| 985 | return Py_None; |
| 986 | } |
| 987 | else if (cmp < 0) |
| 988 | return NULL; |
| 989 | } |
| 990 | PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list"); |
| 991 | return NULL; |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 994 | PyDoc_STRVAR(remove_doc, |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 995 | "remove(x)\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 996 | \n\ |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 997 | Remove the first occurrence of x in the array."); |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 998 | |
| 999 | static PyObject * |
| 1000 | array_pop(arrayobject *self, PyObject *args) |
| 1001 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1002 | Py_ssize_t i = -1; |
| 1003 | PyObject *v; |
| 1004 | if (!PyArg_ParseTuple(args, "|n:pop", &i)) |
| 1005 | return NULL; |
| 1006 | if (Py_SIZE(self) == 0) { |
| 1007 | /* Special-case most common failure cause */ |
| 1008 | PyErr_SetString(PyExc_IndexError, "pop from empty array"); |
| 1009 | return NULL; |
| 1010 | } |
| 1011 | if (i < 0) |
| 1012 | i += Py_SIZE(self); |
| 1013 | if (i < 0 || i >= Py_SIZE(self)) { |
| 1014 | PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
| 1015 | return NULL; |
| 1016 | } |
| 1017 | v = getarrayitem((PyObject *)self,i); |
| 1018 | if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) { |
| 1019 | Py_DECREF(v); |
| 1020 | return NULL; |
| 1021 | } |
| 1022 | return v; |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1025 | PyDoc_STRVAR(pop_doc, |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 1026 | "pop([i])\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1027 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1028 | 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] | 1029 | |
| 1030 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1031 | array_extend(arrayobject *self, PyObject *bb) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1032 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | if (array_do_extend(self, bb) == -1) |
| 1034 | return NULL; |
| 1035 | Py_INCREF(Py_None); |
| 1036 | return Py_None; |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1039 | PyDoc_STRVAR(extend_doc, |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 1040 | "extend(array or iterable)\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1041 | \n\ |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 1042 | Append items to the end of the array."); |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1043 | |
| 1044 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 1045 | array_insert(arrayobject *self, PyObject *args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1046 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1047 | Py_ssize_t i; |
| 1048 | PyObject *v; |
| 1049 | if (!PyArg_ParseTuple(args, "nO:insert", &i, &v)) |
| 1050 | return NULL; |
| 1051 | return ins(self, i, v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1054 | PyDoc_STRVAR(insert_doc, |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 1055 | "insert(i,x)\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1056 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1057 | Insert a new item x into the array before position i."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1058 | |
| 1059 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1060 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1061 | array_buffer_info(arrayobject *self, PyObject *unused) |
Guido van Rossum | de4a4ca | 1997-08-12 14:55:56 +0000 | [diff] [blame] | 1062 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1063 | PyObject* retval = NULL; |
| 1064 | retval = PyTuple_New(2); |
| 1065 | if (!retval) |
| 1066 | return NULL; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 1067 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1068 | PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item)); |
| 1069 | PyTuple_SET_ITEM(retval, 1, PyLong_FromLong((long)(Py_SIZE(self)))); |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 1070 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1071 | return retval; |
Guido van Rossum | de4a4ca | 1997-08-12 14:55:56 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1074 | PyDoc_STRVAR(buffer_info_doc, |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 1075 | "buffer_info() -> (address, length)\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1076 | \n\ |
| 1077 | 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] | 1078 | the length in items of the buffer used to hold array's contents\n\ |
| 1079 | 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] | 1080 | the buffer length in bytes."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1081 | |
| 1082 | |
Guido van Rossum | de4a4ca | 1997-08-12 14:55:56 +0000 | [diff] [blame] | 1083 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1084 | array_append(arrayobject *self, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1085 | { |
Mark Dickinson | 346f0af | 2010-08-06 09:36:57 +0000 | [diff] [blame] | 1086 | return ins(self, Py_SIZE(self), v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1089 | PyDoc_STRVAR(append_doc, |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1090 | "append(x)\n\ |
| 1091 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1092 | Append new value x to the end of the array."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1093 | |
| 1094 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1095 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1096 | array_byteswap(arrayobject *self, PyObject *unused) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1097 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1098 | char *p; |
| 1099 | Py_ssize_t i; |
Fred Drake | bf27298 | 1999-12-03 17:15:30 +0000 | [diff] [blame] | 1100 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1101 | switch (self->ob_descr->itemsize) { |
| 1102 | case 1: |
| 1103 | break; |
| 1104 | case 2: |
| 1105 | for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) { |
| 1106 | char p0 = p[0]; |
| 1107 | p[0] = p[1]; |
| 1108 | p[1] = p0; |
| 1109 | } |
| 1110 | break; |
| 1111 | case 4: |
| 1112 | for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 4) { |
| 1113 | char p0 = p[0]; |
| 1114 | char p1 = p[1]; |
| 1115 | p[0] = p[3]; |
| 1116 | p[1] = p[2]; |
| 1117 | p[2] = p1; |
| 1118 | p[3] = p0; |
| 1119 | } |
| 1120 | break; |
| 1121 | case 8: |
| 1122 | for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) { |
| 1123 | char p0 = p[0]; |
| 1124 | char p1 = p[1]; |
| 1125 | char p2 = p[2]; |
| 1126 | char p3 = p[3]; |
| 1127 | p[0] = p[7]; |
| 1128 | p[1] = p[6]; |
| 1129 | p[2] = p[5]; |
| 1130 | p[3] = p[4]; |
| 1131 | p[4] = p3; |
| 1132 | p[5] = p2; |
| 1133 | p[6] = p1; |
| 1134 | p[7] = p0; |
| 1135 | } |
| 1136 | break; |
| 1137 | default: |
| 1138 | PyErr_SetString(PyExc_RuntimeError, |
| 1139 | "don't know how to byteswap this array type"); |
| 1140 | return NULL; |
| 1141 | } |
| 1142 | Py_INCREF(Py_None); |
| 1143 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1146 | PyDoc_STRVAR(byteswap_doc, |
Fred Drake | bf27298 | 1999-12-03 17:15:30 +0000 | [diff] [blame] | 1147 | "byteswap()\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1148 | \n\ |
Fred Drake | bf27298 | 1999-12-03 17:15:30 +0000 | [diff] [blame] | 1149 | 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] | 1150 | 4, or 8 bytes in size, RuntimeError is raised."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1151 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1152 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1153 | array_reverse(arrayobject *self, PyObject *unused) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1154 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 | register Py_ssize_t itemsize = self->ob_descr->itemsize; |
| 1156 | register char *p, *q; |
| 1157 | /* little buffer to hold items while swapping */ |
| 1158 | char tmp[256]; /* 8 is probably enough -- but why skimp */ |
| 1159 | assert((size_t)itemsize <= sizeof(tmp)); |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 1160 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1161 | if (Py_SIZE(self) > 1) { |
| 1162 | for (p = self->ob_item, |
| 1163 | q = self->ob_item + (Py_SIZE(self) - 1)*itemsize; |
| 1164 | p < q; |
| 1165 | p += itemsize, q -= itemsize) { |
| 1166 | /* memory areas guaranteed disjoint, so memcpy |
| 1167 | * is safe (& memmove may be slower). |
| 1168 | */ |
| 1169 | memcpy(tmp, p, itemsize); |
| 1170 | memcpy(p, q, itemsize); |
| 1171 | memcpy(q, tmp, itemsize); |
| 1172 | } |
| 1173 | } |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 1174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1175 | Py_INCREF(Py_None); |
| 1176 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1177 | } |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 1178 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1179 | PyDoc_STRVAR(reverse_doc, |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1180 | "reverse()\n\ |
| 1181 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1182 | Reverse the order of the items in the array."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1183 | |
Guido van Rossum | 2c94aa5 | 2007-05-24 19:02:32 +0000 | [diff] [blame] | 1184 | |
| 1185 | /* Forward */ |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1186 | static PyObject *array_frombytes(arrayobject *self, PyObject *args); |
Guido van Rossum | 2c94aa5 | 2007-05-24 19:02:32 +0000 | [diff] [blame] | 1187 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1188 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 1189 | array_fromfile(arrayobject *self, PyObject *args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1190 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1191 | PyObject *f, *b, *res; |
| 1192 | Py_ssize_t itemsize = self->ob_descr->itemsize; |
| 1193 | Py_ssize_t n, nbytes; |
| 1194 | int not_enough_bytes; |
Guido van Rossum | 2c94aa5 | 2007-05-24 19:02:32 +0000 | [diff] [blame] | 1195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1196 | if (!PyArg_ParseTuple(args, "On:fromfile", &f, &n)) |
| 1197 | return NULL; |
Guido van Rossum | 2c94aa5 | 2007-05-24 19:02:32 +0000 | [diff] [blame] | 1198 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1199 | nbytes = n * itemsize; |
| 1200 | if (nbytes < 0 || nbytes/itemsize != n) { |
| 1201 | PyErr_NoMemory(); |
| 1202 | return NULL; |
| 1203 | } |
Guido van Rossum | 2c94aa5 | 2007-05-24 19:02:32 +0000 | [diff] [blame] | 1204 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1205 | b = PyObject_CallMethod(f, "read", "n", nbytes); |
| 1206 | if (b == NULL) |
| 1207 | return NULL; |
Guido van Rossum | 2c94aa5 | 2007-05-24 19:02:32 +0000 | [diff] [blame] | 1208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1209 | if (!PyBytes_Check(b)) { |
| 1210 | PyErr_SetString(PyExc_TypeError, |
| 1211 | "read() didn't return bytes"); |
| 1212 | Py_DECREF(b); |
| 1213 | return NULL; |
| 1214 | } |
Guido van Rossum | 2c94aa5 | 2007-05-24 19:02:32 +0000 | [diff] [blame] | 1215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1216 | not_enough_bytes = (PyBytes_GET_SIZE(b) != nbytes); |
Guido van Rossum | 2c94aa5 | 2007-05-24 19:02:32 +0000 | [diff] [blame] | 1217 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1218 | args = Py_BuildValue("(O)", b); |
| 1219 | Py_DECREF(b); |
| 1220 | if (args == NULL) |
| 1221 | return NULL; |
Guido van Rossum | 2c94aa5 | 2007-05-24 19:02:32 +0000 | [diff] [blame] | 1222 | |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1223 | res = array_frombytes(self, args); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1224 | Py_DECREF(args); |
| 1225 | if (res == NULL) |
| 1226 | return NULL; |
Hirokazu Yamamoto | 54d0df6 | 2009-03-06 03:04:07 +0000 | [diff] [blame] | 1227 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1228 | if (not_enough_bytes) { |
| 1229 | PyErr_SetString(PyExc_EOFError, |
| 1230 | "read() didn't return enough bytes"); |
| 1231 | Py_DECREF(res); |
| 1232 | return NULL; |
| 1233 | } |
Guido van Rossum | 2c94aa5 | 2007-05-24 19:02:32 +0000 | [diff] [blame] | 1234 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1235 | return res; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1238 | PyDoc_STRVAR(fromfile_doc, |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1239 | "fromfile(f, n)\n\ |
| 1240 | \n\ |
| 1241 | Read n objects from the file object f and append them to the end of the\n\ |
Georg Brandl | f25ef50 | 2008-02-01 11:30:18 +0000 | [diff] [blame] | 1242 | array."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1243 | |
| 1244 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1245 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1246 | array_tofile(arrayobject *self, PyObject *f) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1247 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1248 | Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize; |
| 1249 | /* Write 64K blocks at a time */ |
| 1250 | /* XXX Make the block size settable */ |
| 1251 | int BLOCKSIZE = 64*1024; |
| 1252 | Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE; |
| 1253 | Py_ssize_t i; |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1255 | if (Py_SIZE(self) == 0) |
| 1256 | goto done; |
Guido van Rossum | b5ddcfd | 2007-04-11 17:08:28 +0000 | [diff] [blame] | 1257 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1258 | for (i = 0; i < nblocks; i++) { |
| 1259 | char* ptr = self->ob_item + i*BLOCKSIZE; |
| 1260 | Py_ssize_t size = BLOCKSIZE; |
| 1261 | PyObject *bytes, *res; |
| 1262 | if (i*BLOCKSIZE + size > nbytes) |
| 1263 | size = nbytes - i*BLOCKSIZE; |
| 1264 | bytes = PyBytes_FromStringAndSize(ptr, size); |
| 1265 | if (bytes == NULL) |
| 1266 | return NULL; |
| 1267 | res = PyObject_CallMethod(f, "write", "O", bytes); |
| 1268 | Py_DECREF(bytes); |
| 1269 | if (res == NULL) |
| 1270 | return NULL; |
| 1271 | Py_DECREF(res); /* drop write result */ |
| 1272 | } |
Guido van Rossum | b5ddcfd | 2007-04-11 17:08:28 +0000 | [diff] [blame] | 1273 | |
| 1274 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1275 | Py_INCREF(Py_None); |
| 1276 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1279 | PyDoc_STRVAR(tofile_doc, |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1280 | "tofile(f)\n\ |
| 1281 | \n\ |
Georg Brandl | f25ef50 | 2008-02-01 11:30:18 +0000 | [diff] [blame] | 1282 | Write all items (as machine values) to the file object f."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1283 | |
| 1284 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1285 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1286 | array_fromlist(arrayobject *self, PyObject *list) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1287 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1288 | Py_ssize_t n; |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1290 | if (!PyList_Check(list)) { |
| 1291 | PyErr_SetString(PyExc_TypeError, "arg must be list"); |
| 1292 | return NULL; |
| 1293 | } |
| 1294 | n = PyList_Size(list); |
| 1295 | if (n > 0) { |
| 1296 | Py_ssize_t i, old_size; |
| 1297 | old_size = Py_SIZE(self); |
| 1298 | if (array_resize(self, old_size + n) == -1) |
| 1299 | return NULL; |
| 1300 | for (i = 0; i < n; i++) { |
| 1301 | PyObject *v = PyList_GetItem(list, i); |
| 1302 | if ((*self->ob_descr->setitem)(self, |
| 1303 | Py_SIZE(self) - n + i, v) != 0) { |
| 1304 | array_resize(self, old_size); |
| 1305 | return NULL; |
| 1306 | } |
| 1307 | } |
| 1308 | } |
| 1309 | Py_INCREF(Py_None); |
| 1310 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1313 | PyDoc_STRVAR(fromlist_doc, |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1314 | "fromlist(list)\n\ |
| 1315 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1316 | Append items to array from list."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1317 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1318 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1319 | array_tolist(arrayobject *self, PyObject *unused) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1320 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1321 | PyObject *list = PyList_New(Py_SIZE(self)); |
| 1322 | Py_ssize_t i; |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1323 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1324 | if (list == NULL) |
| 1325 | return NULL; |
| 1326 | for (i = 0; i < Py_SIZE(self); i++) { |
| 1327 | PyObject *v = getarrayitem((PyObject *)self, i); |
| 1328 | if (v == NULL) { |
| 1329 | Py_DECREF(list); |
| 1330 | return NULL; |
| 1331 | } |
| 1332 | PyList_SetItem(list, i, v); |
| 1333 | } |
| 1334 | return list; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1337 | PyDoc_STRVAR(tolist_doc, |
Guido van Rossum | fc6aba5 | 1998-10-14 02:52:31 +0000 | [diff] [blame] | 1338 | "tolist() -> list\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1339 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1340 | Convert array to an ordinary list with the same items."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1341 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1342 | static PyObject * |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1343 | frombytes(arrayobject *self, Py_buffer *buffer) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1344 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1345 | int itemsize = self->ob_descr->itemsize; |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1346 | Py_ssize_t n; |
| 1347 | if (buffer->itemsize != 1) { |
| 1348 | PyBuffer_Release(buffer); |
| 1349 | PyErr_SetString(PyExc_TypeError, "string/buffer of bytes required."); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1350 | return NULL; |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1351 | } |
| 1352 | n = buffer->len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1353 | if (n % itemsize != 0) { |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1354 | PyBuffer_Release(buffer); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1355 | PyErr_SetString(PyExc_ValueError, |
| 1356 | "string length not a multiple of item size"); |
| 1357 | return NULL; |
| 1358 | } |
| 1359 | n = n / itemsize; |
| 1360 | if (n > 0) { |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1361 | Py_ssize_t old_size = Py_SIZE(self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1362 | if ((n > PY_SSIZE_T_MAX - old_size) || |
| 1363 | ((old_size + n) > PY_SSIZE_T_MAX / itemsize)) { |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1364 | PyBuffer_Release(buffer); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1365 | return PyErr_NoMemory(); |
| 1366 | } |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1367 | if (array_resize(self, old_size + n) == -1) { |
| 1368 | PyBuffer_Release(buffer); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1369 | return NULL; |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1370 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1371 | memcpy(self->ob_item + old_size * itemsize, |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1372 | buffer->buf, n * itemsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1373 | } |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1374 | PyBuffer_Release(buffer); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1375 | Py_INCREF(Py_None); |
| 1376 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1379 | static PyObject * |
| 1380 | array_fromstring(arrayobject *self, PyObject *args) |
| 1381 | { |
| 1382 | Py_buffer buffer; |
| 1383 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 1384 | "fromstring() is deprecated. Use frombytes() instead.", 2) != 0) |
| 1385 | return NULL; |
| 1386 | if (!PyArg_ParseTuple(args, "s*:fromstring", &buffer)) |
| 1387 | return NULL; |
| 1388 | else |
| 1389 | return frombytes(self, &buffer); |
| 1390 | } |
| 1391 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1392 | PyDoc_STRVAR(fromstring_doc, |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1393 | "fromstring(string)\n\ |
| 1394 | \n\ |
| 1395 | Appends items from the string, interpreting it as an array of machine\n\ |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1396 | values, as if it had been read from a file using the fromfile() method).\n\ |
| 1397 | \n\ |
| 1398 | This method is deprecated. Use frombytes instead."); |
| 1399 | |
| 1400 | |
| 1401 | static PyObject * |
| 1402 | array_frombytes(arrayobject *self, PyObject *args) |
| 1403 | { |
| 1404 | Py_buffer buffer; |
| 1405 | if (!PyArg_ParseTuple(args, "y*:frombytes", &buffer)) |
| 1406 | return NULL; |
| 1407 | else |
| 1408 | return frombytes(self, &buffer); |
| 1409 | } |
| 1410 | |
| 1411 | PyDoc_STRVAR(frombytes_doc, |
| 1412 | "frombytes(bytestring)\n\ |
| 1413 | \n\ |
| 1414 | Appends items from the string, interpreting it as an array of machine\n\ |
Walter Dörwald | 93b30b5 | 2007-06-22 12:21:53 +0000 | [diff] [blame] | 1415 | 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] | 1416 | |
| 1417 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1418 | static PyObject * |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1419 | array_tobytes(arrayobject *self, PyObject *unused) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1420 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1421 | if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) { |
| 1422 | return PyBytes_FromStringAndSize(self->ob_item, |
| 1423 | Py_SIZE(self) * self->ob_descr->itemsize); |
| 1424 | } else { |
| 1425 | return PyErr_NoMemory(); |
| 1426 | } |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1429 | PyDoc_STRVAR(tobytes_doc, |
| 1430 | "tobytes() -> bytes\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1431 | \n\ |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1432 | Convert the array to an array of machine values and return the bytes\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1433 | representation."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1434 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1435 | |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1436 | static PyObject * |
| 1437 | array_tostring(arrayobject *self, PyObject *unused) |
| 1438 | { |
Victor Stinner | 9f0b51e | 2010-11-09 09:38:30 +0000 | [diff] [blame] | 1439 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1440 | "tostring() is deprecated. Use tobytes() instead.", 2) != 0) |
| 1441 | return NULL; |
| 1442 | return array_tobytes(self, unused); |
| 1443 | } |
| 1444 | |
| 1445 | PyDoc_STRVAR(tostring_doc, |
| 1446 | "tostring() -> bytes\n\ |
| 1447 | \n\ |
| 1448 | Convert the array to an array of machine values and return the bytes\n\ |
| 1449 | representation.\n\ |
| 1450 | \n\ |
| 1451 | This method is deprecated. Use tobytes instead."); |
| 1452 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1453 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1454 | static PyObject * |
| 1455 | array_fromunicode(arrayobject *self, PyObject *args) |
| 1456 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1457 | Py_UNICODE *ustr; |
| 1458 | Py_ssize_t n; |
Amaury Forgeot d'Arc | 24aa26b | 2010-11-25 08:13:35 +0000 | [diff] [blame] | 1459 | Py_UNICODE typecode; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1460 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1461 | if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n)) |
| 1462 | return NULL; |
| 1463 | typecode = self->ob_descr->typecode; |
Gregory P. Smith | 9504b13 | 2012-12-10 20:20:20 -0800 | [diff] [blame] | 1464 | if (typecode != 'u') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1465 | PyErr_SetString(PyExc_ValueError, |
| 1466 | "fromunicode() may only be called on " |
| 1467 | "unicode type arrays"); |
| 1468 | return NULL; |
| 1469 | } |
| 1470 | if (n > 0) { |
| 1471 | Py_ssize_t old_size = Py_SIZE(self); |
| 1472 | if (array_resize(self, old_size + n) == -1) |
| 1473 | return NULL; |
| 1474 | memcpy(self->ob_item + old_size * sizeof(Py_UNICODE), |
| 1475 | ustr, n * sizeof(Py_UNICODE)); |
| 1476 | } |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1477 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1478 | Py_INCREF(Py_None); |
| 1479 | return Py_None; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1482 | PyDoc_STRVAR(fromunicode_doc, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1483 | "fromunicode(ustr)\n\ |
| 1484 | \n\ |
| 1485 | Extends this array with data from the unicode string ustr.\n\ |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 1486 | The array must be a unicode type array; otherwise a ValueError\n\ |
Florent Xicluna | c45fb25 | 2011-10-24 13:14:55 +0200 | [diff] [blame] | 1487 | is raised. Use array.frombytes(ustr.encode(...)) to\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1488 | append Unicode data to an array of some other type."); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1489 | |
| 1490 | |
| 1491 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1492 | array_tounicode(arrayobject *self, PyObject *unused) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1493 | { |
Amaury Forgeot d'Arc | 24aa26b | 2010-11-25 08:13:35 +0000 | [diff] [blame] | 1494 | Py_UNICODE typecode; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1495 | typecode = self->ob_descr->typecode; |
Gregory P. Smith | 9504b13 | 2012-12-10 20:20:20 -0800 | [diff] [blame] | 1496 | if (typecode != 'u') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1497 | PyErr_SetString(PyExc_ValueError, |
| 1498 | "tounicode() may only be called on unicode type arrays"); |
| 1499 | return NULL; |
| 1500 | } |
| 1501 | return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_SIZE(self)); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1504 | PyDoc_STRVAR(tounicode_doc, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1505 | "tounicode() -> unicode\n\ |
| 1506 | \n\ |
| 1507 | Convert the array to a unicode string. The array must be\n\ |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 1508 | a unicode type array; otherwise a ValueError is raised. Use\n\ |
Florent Xicluna | c45fb25 | 2011-10-24 13:14:55 +0200 | [diff] [blame] | 1509 | array.tobytes().decode() to obtain a unicode string from\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1510 | an array of some other type."); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1511 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1512 | |
Meador Inge | 03b4d50 | 2012-08-10 22:35:45 -0500 | [diff] [blame] | 1513 | static PyObject * |
| 1514 | array_sizeof(arrayobject *self, PyObject *unused) |
| 1515 | { |
| 1516 | Py_ssize_t res; |
| 1517 | res = sizeof(arrayobject) + self->allocated * self->ob_descr->itemsize; |
| 1518 | return PyLong_FromSsize_t(res); |
| 1519 | } |
| 1520 | |
| 1521 | PyDoc_STRVAR(sizeof_doc, |
| 1522 | "__sizeof__() -> int\n\ |
| 1523 | \n\ |
| 1524 | Size of the array in memory, in bytes."); |
| 1525 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1526 | |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1527 | /*********************** Pickling support ************************/ |
| 1528 | |
| 1529 | enum machine_format_code { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1530 | UNKNOWN_FORMAT = -1, |
| 1531 | /* UNKNOWN_FORMAT is used to indicate that the machine format for an |
| 1532 | * array type code cannot be interpreted. When this occurs, a list of |
| 1533 | * Python objects is used to represent the content of the array |
| 1534 | * instead of using the memory content of the array directly. In that |
| 1535 | * case, the array_reconstructor mechanism is bypassed completely, and |
| 1536 | * the standard array constructor is used instead. |
| 1537 | * |
| 1538 | * This is will most likely occur when the machine doesn't use IEEE |
| 1539 | * floating-point numbers. |
| 1540 | */ |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1541 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1542 | UNSIGNED_INT8 = 0, |
| 1543 | SIGNED_INT8 = 1, |
| 1544 | UNSIGNED_INT16_LE = 2, |
| 1545 | UNSIGNED_INT16_BE = 3, |
| 1546 | SIGNED_INT16_LE = 4, |
| 1547 | SIGNED_INT16_BE = 5, |
| 1548 | UNSIGNED_INT32_LE = 6, |
| 1549 | UNSIGNED_INT32_BE = 7, |
| 1550 | SIGNED_INT32_LE = 8, |
| 1551 | SIGNED_INT32_BE = 9, |
| 1552 | UNSIGNED_INT64_LE = 10, |
| 1553 | UNSIGNED_INT64_BE = 11, |
| 1554 | SIGNED_INT64_LE = 12, |
| 1555 | SIGNED_INT64_BE = 13, |
| 1556 | IEEE_754_FLOAT_LE = 14, |
| 1557 | IEEE_754_FLOAT_BE = 15, |
| 1558 | IEEE_754_DOUBLE_LE = 16, |
| 1559 | IEEE_754_DOUBLE_BE = 17, |
| 1560 | UTF16_LE = 18, |
| 1561 | UTF16_BE = 19, |
| 1562 | UTF32_LE = 20, |
| 1563 | UTF32_BE = 21 |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1564 | }; |
| 1565 | #define MACHINE_FORMAT_CODE_MIN 0 |
| 1566 | #define MACHINE_FORMAT_CODE_MAX 21 |
| 1567 | |
| 1568 | static const struct mformatdescr { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1569 | size_t size; |
| 1570 | int is_signed; |
| 1571 | int is_big_endian; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1572 | } mformat_descriptors[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1573 | {1, 0, 0}, /* 0: UNSIGNED_INT8 */ |
| 1574 | {1, 1, 0}, /* 1: SIGNED_INT8 */ |
| 1575 | {2, 0, 0}, /* 2: UNSIGNED_INT16_LE */ |
| 1576 | {2, 0, 1}, /* 3: UNSIGNED_INT16_BE */ |
| 1577 | {2, 1, 0}, /* 4: SIGNED_INT16_LE */ |
| 1578 | {2, 1, 1}, /* 5: SIGNED_INT16_BE */ |
| 1579 | {4, 0, 0}, /* 6: UNSIGNED_INT32_LE */ |
| 1580 | {4, 0, 1}, /* 7: UNSIGNED_INT32_BE */ |
| 1581 | {4, 1, 0}, /* 8: SIGNED_INT32_LE */ |
| 1582 | {4, 1, 1}, /* 9: SIGNED_INT32_BE */ |
| 1583 | {8, 0, 0}, /* 10: UNSIGNED_INT64_LE */ |
| 1584 | {8, 0, 1}, /* 11: UNSIGNED_INT64_BE */ |
| 1585 | {8, 1, 0}, /* 12: SIGNED_INT64_LE */ |
| 1586 | {8, 1, 1}, /* 13: SIGNED_INT64_BE */ |
| 1587 | {4, 0, 0}, /* 14: IEEE_754_FLOAT_LE */ |
| 1588 | {4, 0, 1}, /* 15: IEEE_754_FLOAT_BE */ |
| 1589 | {8, 0, 0}, /* 16: IEEE_754_DOUBLE_LE */ |
| 1590 | {8, 0, 1}, /* 17: IEEE_754_DOUBLE_BE */ |
| 1591 | {4, 0, 0}, /* 18: UTF16_LE */ |
| 1592 | {4, 0, 1}, /* 19: UTF16_BE */ |
| 1593 | {8, 0, 0}, /* 20: UTF32_LE */ |
| 1594 | {8, 0, 1} /* 21: UTF32_BE */ |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1595 | }; |
| 1596 | |
| 1597 | |
| 1598 | /* |
| 1599 | * Internal: This function is used to find the machine format of a given |
| 1600 | * array type code. This returns UNKNOWN_FORMAT when the machine format cannot |
| 1601 | * be found. |
| 1602 | */ |
| 1603 | static enum machine_format_code |
| 1604 | typecode_to_mformat_code(int typecode) |
| 1605 | { |
Alexandre Vassalotti | 7aaa770 | 2009-07-17 03:51:27 +0000 | [diff] [blame] | 1606 | #ifdef WORDS_BIGENDIAN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1607 | const int is_big_endian = 1; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1608 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1609 | const int is_big_endian = 0; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1610 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1611 | size_t intsize; |
| 1612 | int is_signed; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1614 | switch (typecode) { |
| 1615 | case 'b': |
| 1616 | return SIGNED_INT8; |
| 1617 | case 'B': |
| 1618 | return UNSIGNED_INT8; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1620 | case 'u': |
| 1621 | if (sizeof(Py_UNICODE) == 2) { |
| 1622 | return UTF16_LE + is_big_endian; |
| 1623 | } |
| 1624 | if (sizeof(Py_UNICODE) == 4) { |
| 1625 | return UTF32_LE + is_big_endian; |
| 1626 | } |
| 1627 | return UNKNOWN_FORMAT; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1629 | case 'f': |
| 1630 | if (sizeof(float) == 4) { |
| 1631 | const float y = 16711938.0; |
| 1632 | if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0) |
| 1633 | return IEEE_754_FLOAT_BE; |
| 1634 | if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0) |
| 1635 | return IEEE_754_FLOAT_LE; |
| 1636 | } |
| 1637 | return UNKNOWN_FORMAT; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1638 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1639 | case 'd': |
| 1640 | if (sizeof(double) == 8) { |
| 1641 | const double x = 9006104071832581.0; |
| 1642 | if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0) |
| 1643 | return IEEE_754_DOUBLE_BE; |
| 1644 | if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0) |
| 1645 | return IEEE_754_DOUBLE_LE; |
| 1646 | } |
| 1647 | return UNKNOWN_FORMAT; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1648 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1649 | /* Integers */ |
| 1650 | case 'h': |
| 1651 | intsize = sizeof(short); |
| 1652 | is_signed = 1; |
| 1653 | break; |
| 1654 | case 'H': |
| 1655 | intsize = sizeof(short); |
| 1656 | is_signed = 0; |
| 1657 | break; |
| 1658 | case 'i': |
| 1659 | intsize = sizeof(int); |
| 1660 | is_signed = 1; |
| 1661 | break; |
| 1662 | case 'I': |
| 1663 | intsize = sizeof(int); |
| 1664 | is_signed = 0; |
| 1665 | break; |
| 1666 | case 'l': |
| 1667 | intsize = sizeof(long); |
| 1668 | is_signed = 1; |
| 1669 | break; |
| 1670 | case 'L': |
| 1671 | intsize = sizeof(long); |
| 1672 | is_signed = 0; |
| 1673 | break; |
| 1674 | default: |
| 1675 | return UNKNOWN_FORMAT; |
| 1676 | } |
| 1677 | switch (intsize) { |
| 1678 | case 2: |
| 1679 | return UNSIGNED_INT16_LE + is_big_endian + (2 * is_signed); |
| 1680 | case 4: |
| 1681 | return UNSIGNED_INT32_LE + is_big_endian + (2 * is_signed); |
| 1682 | case 8: |
| 1683 | return UNSIGNED_INT64_LE + is_big_endian + (2 * is_signed); |
| 1684 | default: |
| 1685 | return UNKNOWN_FORMAT; |
| 1686 | } |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | /* Forward declaration. */ |
| 1690 | static PyObject *array_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 1691 | |
| 1692 | /* |
| 1693 | * Internal: This function wraps the array constructor--i.e., array_new()--to |
| 1694 | * allow the creation of array objects from C code without having to deal |
| 1695 | * directly the tuple argument of array_new(). The typecode argument is a |
| 1696 | * Unicode character value, like 'i' or 'f' for example, representing an array |
| 1697 | * type code. The items argument is a bytes or a list object from which |
| 1698 | * contains the initial value of the array. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1699 | * |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1700 | * On success, this functions returns the array object created. Otherwise, |
| 1701 | * NULL is returned to indicate a failure. |
| 1702 | */ |
| 1703 | static PyObject * |
Victor Stinner | 9f0b51e | 2010-11-09 09:38:30 +0000 | [diff] [blame] | 1704 | make_array(PyTypeObject *arraytype, Py_UNICODE typecode, PyObject *items) |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1705 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1706 | PyObject *new_args; |
| 1707 | PyObject *array_obj; |
| 1708 | PyObject *typecode_obj; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1709 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1710 | assert(arraytype != NULL); |
| 1711 | assert(items != NULL); |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1712 | |
Victor Stinner | 9f0b51e | 2010-11-09 09:38:30 +0000 | [diff] [blame] | 1713 | typecode_obj = PyUnicode_FromUnicode(&typecode, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1714 | if (typecode_obj == NULL) |
| 1715 | return NULL; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1716 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1717 | new_args = PyTuple_New(2); |
| 1718 | if (new_args == NULL) |
| 1719 | return NULL; |
| 1720 | Py_INCREF(items); |
| 1721 | PyTuple_SET_ITEM(new_args, 0, typecode_obj); |
| 1722 | PyTuple_SET_ITEM(new_args, 1, items); |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1723 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1724 | array_obj = array_new(arraytype, new_args, NULL); |
| 1725 | Py_DECREF(new_args); |
| 1726 | if (array_obj == NULL) |
| 1727 | return NULL; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1728 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1729 | return array_obj; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | /* |
| 1733 | * This functions is a special constructor used when unpickling an array. It |
| 1734 | * provides a portable way to rebuild an array from its memory representation. |
| 1735 | */ |
| 1736 | static PyObject * |
| 1737 | array_reconstructor(PyObject *self, PyObject *args) |
| 1738 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1739 | PyTypeObject *arraytype; |
| 1740 | PyObject *items; |
| 1741 | PyObject *converted_items; |
| 1742 | PyObject *result; |
Victor Stinner | 9f0b51e | 2010-11-09 09:38:30 +0000 | [diff] [blame] | 1743 | int typecode_int; |
| 1744 | Py_UNICODE typecode; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1745 | enum machine_format_code mformat_code; |
| 1746 | struct arraydescr *descr; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1747 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1748 | if (!PyArg_ParseTuple(args, "OCiO:array._array_reconstructor", |
Victor Stinner | 9f0b51e | 2010-11-09 09:38:30 +0000 | [diff] [blame] | 1749 | &arraytype, &typecode_int, &mformat_code, &items)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1750 | return NULL; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1751 | |
Victor Stinner | 9f0b51e | 2010-11-09 09:38:30 +0000 | [diff] [blame] | 1752 | typecode = (Py_UNICODE)typecode_int; |
| 1753 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1754 | if (!PyType_Check(arraytype)) { |
| 1755 | PyErr_Format(PyExc_TypeError, |
| 1756 | "first argument must a type object, not %.200s", |
| 1757 | Py_TYPE(arraytype)->tp_name); |
| 1758 | return NULL; |
| 1759 | } |
| 1760 | if (!PyType_IsSubtype(arraytype, &Arraytype)) { |
| 1761 | PyErr_Format(PyExc_TypeError, |
| 1762 | "%.200s is not a subtype of %.200s", |
| 1763 | arraytype->tp_name, Arraytype.tp_name); |
| 1764 | return NULL; |
| 1765 | } |
| 1766 | for (descr = descriptors; descr->typecode != '\0'; descr++) { |
| 1767 | if (descr->typecode == typecode) |
| 1768 | break; |
| 1769 | } |
| 1770 | if (descr->typecode == '\0') { |
| 1771 | PyErr_SetString(PyExc_ValueError, |
| 1772 | "second argument must be a valid type code"); |
| 1773 | return NULL; |
| 1774 | } |
| 1775 | if (mformat_code < MACHINE_FORMAT_CODE_MIN || |
| 1776 | mformat_code > MACHINE_FORMAT_CODE_MAX) { |
| 1777 | PyErr_SetString(PyExc_ValueError, |
| 1778 | "third argument must be a valid machine format code."); |
| 1779 | return NULL; |
| 1780 | } |
| 1781 | if (!PyBytes_Check(items)) { |
| 1782 | PyErr_Format(PyExc_TypeError, |
| 1783 | "fourth argument should be bytes, not %.200s", |
| 1784 | Py_TYPE(items)->tp_name); |
| 1785 | return NULL; |
| 1786 | } |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1787 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1788 | /* Fast path: No decoding has to be done. */ |
| 1789 | if (mformat_code == typecode_to_mformat_code(typecode) || |
| 1790 | mformat_code == UNKNOWN_FORMAT) { |
| 1791 | return make_array(arraytype, typecode, items); |
| 1792 | } |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1793 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1794 | /* Slow path: Decode the byte string according to the given machine |
| 1795 | * format code. This occurs when the computer unpickling the array |
| 1796 | * object is architecturally different from the one that pickled the |
| 1797 | * array. |
| 1798 | */ |
| 1799 | if (Py_SIZE(items) % mformat_descriptors[mformat_code].size != 0) { |
| 1800 | PyErr_SetString(PyExc_ValueError, |
| 1801 | "string length not a multiple of item size"); |
| 1802 | return NULL; |
| 1803 | } |
| 1804 | switch (mformat_code) { |
| 1805 | case IEEE_754_FLOAT_LE: |
| 1806 | case IEEE_754_FLOAT_BE: { |
| 1807 | int i; |
| 1808 | int le = (mformat_code == IEEE_754_FLOAT_LE) ? 1 : 0; |
| 1809 | Py_ssize_t itemcount = Py_SIZE(items) / 4; |
| 1810 | const unsigned char *memstr = |
| 1811 | (unsigned char *)PyBytes_AS_STRING(items); |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1812 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1813 | converted_items = PyList_New(itemcount); |
| 1814 | if (converted_items == NULL) |
| 1815 | return NULL; |
| 1816 | for (i = 0; i < itemcount; i++) { |
| 1817 | PyObject *pyfloat = PyFloat_FromDouble( |
| 1818 | _PyFloat_Unpack4(&memstr[i * 4], le)); |
| 1819 | if (pyfloat == NULL) { |
| 1820 | Py_DECREF(converted_items); |
| 1821 | return NULL; |
| 1822 | } |
| 1823 | PyList_SET_ITEM(converted_items, i, pyfloat); |
| 1824 | } |
| 1825 | break; |
| 1826 | } |
| 1827 | case IEEE_754_DOUBLE_LE: |
| 1828 | case IEEE_754_DOUBLE_BE: { |
| 1829 | int i; |
| 1830 | int le = (mformat_code == IEEE_754_DOUBLE_LE) ? 1 : 0; |
| 1831 | Py_ssize_t itemcount = Py_SIZE(items) / 8; |
| 1832 | const unsigned char *memstr = |
| 1833 | (unsigned char *)PyBytes_AS_STRING(items); |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1834 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1835 | converted_items = PyList_New(itemcount); |
| 1836 | if (converted_items == NULL) |
| 1837 | return NULL; |
| 1838 | for (i = 0; i < itemcount; i++) { |
| 1839 | PyObject *pyfloat = PyFloat_FromDouble( |
| 1840 | _PyFloat_Unpack8(&memstr[i * 8], le)); |
| 1841 | if (pyfloat == NULL) { |
| 1842 | Py_DECREF(converted_items); |
| 1843 | return NULL; |
| 1844 | } |
| 1845 | PyList_SET_ITEM(converted_items, i, pyfloat); |
| 1846 | } |
| 1847 | break; |
| 1848 | } |
| 1849 | case UTF16_LE: |
| 1850 | case UTF16_BE: { |
| 1851 | int byteorder = (mformat_code == UTF16_LE) ? -1 : 1; |
| 1852 | converted_items = PyUnicode_DecodeUTF16( |
| 1853 | PyBytes_AS_STRING(items), Py_SIZE(items), |
| 1854 | "strict", &byteorder); |
| 1855 | if (converted_items == NULL) |
| 1856 | return NULL; |
| 1857 | break; |
| 1858 | } |
| 1859 | case UTF32_LE: |
| 1860 | case UTF32_BE: { |
| 1861 | int byteorder = (mformat_code == UTF32_LE) ? -1 : 1; |
| 1862 | converted_items = PyUnicode_DecodeUTF32( |
| 1863 | PyBytes_AS_STRING(items), Py_SIZE(items), |
| 1864 | "strict", &byteorder); |
| 1865 | if (converted_items == NULL) |
| 1866 | return NULL; |
| 1867 | break; |
| 1868 | } |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1869 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1870 | case UNSIGNED_INT8: |
| 1871 | case SIGNED_INT8: |
| 1872 | case UNSIGNED_INT16_LE: |
| 1873 | case UNSIGNED_INT16_BE: |
| 1874 | case SIGNED_INT16_LE: |
| 1875 | case SIGNED_INT16_BE: |
| 1876 | case UNSIGNED_INT32_LE: |
| 1877 | case UNSIGNED_INT32_BE: |
| 1878 | case SIGNED_INT32_LE: |
| 1879 | case SIGNED_INT32_BE: |
| 1880 | case UNSIGNED_INT64_LE: |
| 1881 | case UNSIGNED_INT64_BE: |
| 1882 | case SIGNED_INT64_LE: |
| 1883 | case SIGNED_INT64_BE: { |
| 1884 | int i; |
| 1885 | const struct mformatdescr mf_descr = |
| 1886 | mformat_descriptors[mformat_code]; |
| 1887 | Py_ssize_t itemcount = Py_SIZE(items) / mf_descr.size; |
| 1888 | const unsigned char *memstr = |
| 1889 | (unsigned char *)PyBytes_AS_STRING(items); |
| 1890 | struct arraydescr *descr; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1891 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1892 | /* If possible, try to pack array's items using a data type |
| 1893 | * that fits better. This may result in an array with narrower |
| 1894 | * or wider elements. |
| 1895 | * |
| 1896 | * For example, if a 32-bit machine pickles a L-code array of |
| 1897 | * unsigned longs, then the array will be unpickled by 64-bit |
| 1898 | * machine as an I-code array of unsigned ints. |
| 1899 | * |
| 1900 | * XXX: Is it possible to write a unit test for this? |
| 1901 | */ |
| 1902 | for (descr = descriptors; descr->typecode != '\0'; descr++) { |
| 1903 | if (descr->is_integer_type && |
| 1904 | descr->itemsize == mf_descr.size && |
| 1905 | descr->is_signed == mf_descr.is_signed) |
| 1906 | typecode = descr->typecode; |
| 1907 | } |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1908 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1909 | converted_items = PyList_New(itemcount); |
| 1910 | if (converted_items == NULL) |
| 1911 | return NULL; |
| 1912 | for (i = 0; i < itemcount; i++) { |
| 1913 | PyObject *pylong; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1915 | pylong = _PyLong_FromByteArray( |
| 1916 | &memstr[i * mf_descr.size], |
| 1917 | mf_descr.size, |
| 1918 | !mf_descr.is_big_endian, |
| 1919 | mf_descr.is_signed); |
| 1920 | if (pylong == NULL) { |
| 1921 | Py_DECREF(converted_items); |
| 1922 | return NULL; |
| 1923 | } |
| 1924 | PyList_SET_ITEM(converted_items, i, pylong); |
| 1925 | } |
| 1926 | break; |
| 1927 | } |
| 1928 | case UNKNOWN_FORMAT: |
| 1929 | /* Impossible, but needed to shut up GCC about the unhandled |
| 1930 | * enumeration value. |
| 1931 | */ |
| 1932 | default: |
| 1933 | PyErr_BadArgument(); |
| 1934 | return NULL; |
| 1935 | } |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1936 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1937 | result = make_array(arraytype, typecode, converted_items); |
| 1938 | Py_DECREF(converted_items); |
| 1939 | return result; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | static PyObject * |
| 1943 | array_reduce_ex(arrayobject *array, PyObject *value) |
| 1944 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1945 | PyObject *dict; |
| 1946 | PyObject *result; |
| 1947 | PyObject *array_str; |
| 1948 | int typecode = array->ob_descr->typecode; |
| 1949 | int mformat_code; |
| 1950 | static PyObject *array_reconstructor = NULL; |
| 1951 | long protocol; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1952 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1953 | if (array_reconstructor == NULL) { |
| 1954 | PyObject *array_module = PyImport_ImportModule("array"); |
| 1955 | if (array_module == NULL) |
| 1956 | return NULL; |
| 1957 | array_reconstructor = PyObject_GetAttrString( |
| 1958 | array_module, |
| 1959 | "_array_reconstructor"); |
| 1960 | Py_DECREF(array_module); |
| 1961 | if (array_reconstructor == NULL) |
| 1962 | return NULL; |
| 1963 | } |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1964 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1965 | if (!PyLong_Check(value)) { |
| 1966 | PyErr_SetString(PyExc_TypeError, |
| 1967 | "__reduce_ex__ argument should an integer"); |
| 1968 | return NULL; |
| 1969 | } |
| 1970 | protocol = PyLong_AsLong(value); |
| 1971 | if (protocol == -1 && PyErr_Occurred()) |
| 1972 | return NULL; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1973 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1974 | dict = PyObject_GetAttrString((PyObject *)array, "__dict__"); |
| 1975 | if (dict == NULL) { |
| 1976 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1977 | return NULL; |
| 1978 | PyErr_Clear(); |
| 1979 | dict = Py_None; |
| 1980 | Py_INCREF(dict); |
| 1981 | } |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1982 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1983 | mformat_code = typecode_to_mformat_code(typecode); |
| 1984 | if (mformat_code == UNKNOWN_FORMAT || protocol < 3) { |
| 1985 | /* Convert the array to a list if we got something weird |
| 1986 | * (e.g., non-IEEE floats), or we are pickling the array using |
| 1987 | * a Python 2.x compatible protocol. |
| 1988 | * |
| 1989 | * It is necessary to use a list representation for Python 2.x |
| 1990 | * compatible pickle protocol, since Python 2's str objects |
| 1991 | * are unpickled as unicode by Python 3. Thus it is impossible |
| 1992 | * to make arrays unpicklable by Python 3 by using their memory |
| 1993 | * representation, unless we resort to ugly hacks such as |
| 1994 | * coercing unicode objects to bytes in array_reconstructor. |
| 1995 | */ |
| 1996 | PyObject *list; |
| 1997 | list = array_tolist(array, NULL); |
| 1998 | if (list == NULL) { |
| 1999 | Py_DECREF(dict); |
| 2000 | return NULL; |
| 2001 | } |
| 2002 | result = Py_BuildValue( |
| 2003 | "O(CO)O", Py_TYPE(array), typecode, list, dict); |
| 2004 | Py_DECREF(list); |
| 2005 | Py_DECREF(dict); |
| 2006 | return result; |
| 2007 | } |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 2008 | |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 2009 | array_str = array_tobytes(array, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2010 | if (array_str == NULL) { |
| 2011 | Py_DECREF(dict); |
| 2012 | return NULL; |
| 2013 | } |
| 2014 | result = Py_BuildValue( |
| 2015 | "O(OCiN)O", array_reconstructor, Py_TYPE(array), typecode, |
| 2016 | mformat_code, array_str, dict); |
| 2017 | Py_DECREF(dict); |
| 2018 | return result; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 2022 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2023 | static PyObject * |
| 2024 | array_get_typecode(arrayobject *a, void *closure) |
| 2025 | { |
Amaury Forgeot d'Arc | 24aa26b | 2010-11-25 08:13:35 +0000 | [diff] [blame] | 2026 | Py_UNICODE tc = a->ob_descr->typecode; |
| 2027 | return PyUnicode_FromUnicode(&tc, 1); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | static PyObject * |
| 2031 | array_get_itemsize(arrayobject *a, void *closure) |
| 2032 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2033 | return PyLong_FromLong((long)a->ob_descr->itemsize); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | static PyGetSetDef array_getsets [] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2037 | {"typecode", (getter) array_get_typecode, NULL, |
| 2038 | "the typecode character used to create the array"}, |
| 2039 | {"itemsize", (getter) array_get_itemsize, NULL, |
| 2040 | "the size, in bytes, of one array item"}, |
| 2041 | {NULL} |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2042 | }; |
| 2043 | |
Martin v. Löwis | 59683e8 | 2008-06-13 07:50:45 +0000 | [diff] [blame] | 2044 | static PyMethodDef array_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2045 | {"append", (PyCFunction)array_append, METH_O, |
| 2046 | append_doc}, |
| 2047 | {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS, |
| 2048 | buffer_info_doc}, |
| 2049 | {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS, |
| 2050 | byteswap_doc}, |
| 2051 | {"__copy__", (PyCFunction)array_copy, METH_NOARGS, |
| 2052 | copy_doc}, |
| 2053 | {"count", (PyCFunction)array_count, METH_O, |
| 2054 | count_doc}, |
| 2055 | {"__deepcopy__",(PyCFunction)array_copy, METH_O, |
| 2056 | copy_doc}, |
| 2057 | {"extend", (PyCFunction)array_extend, METH_O, |
| 2058 | extend_doc}, |
| 2059 | {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS, |
| 2060 | fromfile_doc}, |
| 2061 | {"fromlist", (PyCFunction)array_fromlist, METH_O, |
| 2062 | fromlist_doc}, |
| 2063 | {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS, |
| 2064 | fromstring_doc}, |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 2065 | {"frombytes", (PyCFunction)array_frombytes, METH_VARARGS, |
| 2066 | frombytes_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2067 | {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS, |
| 2068 | fromunicode_doc}, |
| 2069 | {"index", (PyCFunction)array_index, METH_O, |
| 2070 | index_doc}, |
| 2071 | {"insert", (PyCFunction)array_insert, METH_VARARGS, |
| 2072 | insert_doc}, |
| 2073 | {"pop", (PyCFunction)array_pop, METH_VARARGS, |
| 2074 | pop_doc}, |
| 2075 | {"__reduce_ex__", (PyCFunction)array_reduce_ex, METH_O, |
| 2076 | reduce_doc}, |
| 2077 | {"remove", (PyCFunction)array_remove, METH_O, |
| 2078 | remove_doc}, |
| 2079 | {"reverse", (PyCFunction)array_reverse, METH_NOARGS, |
| 2080 | reverse_doc}, |
| 2081 | /* {"sort", (PyCFunction)array_sort, METH_VARARGS, |
| 2082 | sort_doc},*/ |
| 2083 | {"tofile", (PyCFunction)array_tofile, METH_O, |
| 2084 | tofile_doc}, |
| 2085 | {"tolist", (PyCFunction)array_tolist, METH_NOARGS, |
| 2086 | tolist_doc}, |
| 2087 | {"tostring", (PyCFunction)array_tostring, METH_NOARGS, |
| 2088 | tostring_doc}, |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 2089 | {"tobytes", (PyCFunction)array_tobytes, METH_NOARGS, |
| 2090 | tobytes_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2091 | {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS, |
| 2092 | tounicode_doc}, |
Meador Inge | 03b4d50 | 2012-08-10 22:35:45 -0500 | [diff] [blame] | 2093 | {"__sizeof__", (PyCFunction)array_sizeof, METH_NOARGS, |
| 2094 | sizeof_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2095 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 2096 | }; |
| 2097 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 2098 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 2099 | array_repr(arrayobject *a) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 2100 | { |
Amaury Forgeot d'Arc | 24aa26b | 2010-11-25 08:13:35 +0000 | [diff] [blame] | 2101 | Py_UNICODE typecode; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2102 | PyObject *s, *v = NULL; |
| 2103 | Py_ssize_t len; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2104 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2105 | len = Py_SIZE(a); |
| 2106 | typecode = a->ob_descr->typecode; |
| 2107 | if (len == 0) { |
Amaury Forgeot d'Arc | 24aa26b | 2010-11-25 08:13:35 +0000 | [diff] [blame] | 2108 | return PyUnicode_FromFormat("array('%c')", (int)typecode); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2109 | } |
Gregory P. Smith | 9504b13 | 2012-12-10 20:20:20 -0800 | [diff] [blame] | 2110 | if (typecode == 'u') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2111 | v = array_tounicode(a, NULL); |
Gregory P. Smith | 9504b13 | 2012-12-10 20:20:20 -0800 | [diff] [blame] | 2112 | } else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2113 | v = array_tolist(a, NULL); |
Gregory P. Smith | 9504b13 | 2012-12-10 20:20:20 -0800 | [diff] [blame] | 2114 | } |
Raymond Hettinger | 88ba1e3 | 2003-04-23 17:27:00 +0000 | [diff] [blame] | 2115 | |
Amaury Forgeot d'Arc | 24aa26b | 2010-11-25 08:13:35 +0000 | [diff] [blame] | 2116 | s = PyUnicode_FromFormat("array('%c', %R)", (int)typecode, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2117 | Py_DECREF(v); |
| 2118 | return s; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 2119 | } |
| 2120 | |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 2121 | static PyObject* |
| 2122 | array_subscr(arrayobject* self, PyObject* item) |
| 2123 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2124 | if (PyIndex_Check(item)) { |
| 2125 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 2126 | if (i==-1 && PyErr_Occurred()) { |
| 2127 | return NULL; |
| 2128 | } |
| 2129 | if (i < 0) |
| 2130 | i += Py_SIZE(self); |
| 2131 | return array_item(self, i); |
| 2132 | } |
| 2133 | else if (PySlice_Check(item)) { |
| 2134 | Py_ssize_t start, stop, step, slicelength, cur, i; |
| 2135 | PyObject* result; |
| 2136 | arrayobject* ar; |
| 2137 | int itemsize = self->ob_descr->itemsize; |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 2138 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2139 | if (PySlice_GetIndicesEx(item, Py_SIZE(self), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2140 | &start, &stop, &step, &slicelength) < 0) { |
| 2141 | return NULL; |
| 2142 | } |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 2143 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2144 | if (slicelength <= 0) { |
| 2145 | return newarrayobject(&Arraytype, 0, self->ob_descr); |
| 2146 | } |
| 2147 | else if (step == 1) { |
| 2148 | PyObject *result = newarrayobject(&Arraytype, |
| 2149 | slicelength, self->ob_descr); |
| 2150 | if (result == NULL) |
| 2151 | return NULL; |
| 2152 | memcpy(((arrayobject *)result)->ob_item, |
| 2153 | self->ob_item + start * itemsize, |
| 2154 | slicelength * itemsize); |
| 2155 | return result; |
| 2156 | } |
| 2157 | else { |
| 2158 | result = newarrayobject(&Arraytype, slicelength, self->ob_descr); |
| 2159 | if (!result) return NULL; |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 2160 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2161 | ar = (arrayobject*)result; |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 2162 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2163 | for (cur = start, i = 0; i < slicelength; |
| 2164 | cur += step, i++) { |
| 2165 | memcpy(ar->ob_item + i*itemsize, |
| 2166 | self->ob_item + cur*itemsize, |
| 2167 | itemsize); |
| 2168 | } |
| 2169 | |
| 2170 | return result; |
| 2171 | } |
| 2172 | } |
| 2173 | else { |
| 2174 | PyErr_SetString(PyExc_TypeError, |
| 2175 | "array indices must be integers"); |
| 2176 | return NULL; |
| 2177 | } |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
| 2180 | static int |
| 2181 | array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value) |
| 2182 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2183 | Py_ssize_t start, stop, step, slicelength, needed; |
| 2184 | arrayobject* other; |
| 2185 | int itemsize; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 2186 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2187 | if (PyIndex_Check(item)) { |
| 2188 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Alexandre Vassalotti | 4713725 | 2009-07-05 19:57:00 +0000 | [diff] [blame] | 2189 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2190 | if (i == -1 && PyErr_Occurred()) |
| 2191 | return -1; |
| 2192 | if (i < 0) |
| 2193 | i += Py_SIZE(self); |
| 2194 | if (i < 0 || i >= Py_SIZE(self)) { |
| 2195 | PyErr_SetString(PyExc_IndexError, |
| 2196 | "array assignment index out of range"); |
| 2197 | return -1; |
| 2198 | } |
| 2199 | if (value == NULL) { |
| 2200 | /* Fall through to slice assignment */ |
| 2201 | start = i; |
| 2202 | stop = i + 1; |
| 2203 | step = 1; |
| 2204 | slicelength = 1; |
| 2205 | } |
| 2206 | else |
| 2207 | return (*self->ob_descr->setitem)(self, i, value); |
| 2208 | } |
| 2209 | else if (PySlice_Check(item)) { |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2210 | if (PySlice_GetIndicesEx(item, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2211 | Py_SIZE(self), &start, &stop, |
| 2212 | &step, &slicelength) < 0) { |
| 2213 | return -1; |
| 2214 | } |
| 2215 | } |
| 2216 | else { |
| 2217 | PyErr_SetString(PyExc_TypeError, |
| 2218 | "array indices must be integer"); |
| 2219 | return -1; |
| 2220 | } |
| 2221 | if (value == NULL) { |
| 2222 | other = NULL; |
| 2223 | needed = 0; |
| 2224 | } |
| 2225 | else if (array_Check(value)) { |
| 2226 | other = (arrayobject *)value; |
| 2227 | needed = Py_SIZE(other); |
| 2228 | if (self == other) { |
| 2229 | /* Special case "self[i:j] = self" -- copy self first */ |
| 2230 | int ret; |
| 2231 | value = array_slice(other, 0, needed); |
| 2232 | if (value == NULL) |
| 2233 | return -1; |
| 2234 | ret = array_ass_subscr(self, item, value); |
| 2235 | Py_DECREF(value); |
| 2236 | return ret; |
| 2237 | } |
| 2238 | if (other->ob_descr != self->ob_descr) { |
| 2239 | PyErr_BadArgument(); |
| 2240 | return -1; |
| 2241 | } |
| 2242 | } |
| 2243 | else { |
| 2244 | PyErr_Format(PyExc_TypeError, |
| 2245 | "can only assign array (not \"%.200s\") to array slice", |
| 2246 | Py_TYPE(value)->tp_name); |
| 2247 | return -1; |
| 2248 | } |
| 2249 | itemsize = self->ob_descr->itemsize; |
| 2250 | /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */ |
| 2251 | if ((step > 0 && stop < start) || |
| 2252 | (step < 0 && stop > start)) |
| 2253 | stop = start; |
Alexandre Vassalotti | 4713725 | 2009-07-05 19:57:00 +0000 | [diff] [blame] | 2254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2255 | /* Issue #4509: If the array has exported buffers and the slice |
| 2256 | assignment would change the size of the array, fail early to make |
| 2257 | sure we don't modify it. */ |
| 2258 | if ((needed == 0 || slicelength != needed) && self->ob_exports > 0) { |
| 2259 | PyErr_SetString(PyExc_BufferError, |
| 2260 | "cannot resize an array that is exporting buffers"); |
| 2261 | return -1; |
| 2262 | } |
Mark Dickinson | bc09964 | 2010-01-29 17:27:24 +0000 | [diff] [blame] | 2263 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2264 | if (step == 1) { |
| 2265 | if (slicelength > needed) { |
| 2266 | memmove(self->ob_item + (start + needed) * itemsize, |
| 2267 | self->ob_item + stop * itemsize, |
| 2268 | (Py_SIZE(self) - stop) * itemsize); |
| 2269 | if (array_resize(self, Py_SIZE(self) + |
| 2270 | needed - slicelength) < 0) |
| 2271 | return -1; |
| 2272 | } |
| 2273 | else if (slicelength < needed) { |
| 2274 | if (array_resize(self, Py_SIZE(self) + |
| 2275 | needed - slicelength) < 0) |
| 2276 | return -1; |
| 2277 | memmove(self->ob_item + (start + needed) * itemsize, |
| 2278 | self->ob_item + stop * itemsize, |
| 2279 | (Py_SIZE(self) - start - needed) * itemsize); |
| 2280 | } |
| 2281 | if (needed > 0) |
| 2282 | memcpy(self->ob_item + start * itemsize, |
| 2283 | other->ob_item, needed * itemsize); |
| 2284 | return 0; |
| 2285 | } |
| 2286 | else if (needed == 0) { |
| 2287 | /* Delete slice */ |
| 2288 | size_t cur; |
| 2289 | Py_ssize_t i; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 2290 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2291 | if (step < 0) { |
| 2292 | stop = start + 1; |
| 2293 | start = stop + step * (slicelength - 1) - 1; |
| 2294 | step = -step; |
| 2295 | } |
| 2296 | for (cur = start, i = 0; i < slicelength; |
| 2297 | cur += step, i++) { |
| 2298 | Py_ssize_t lim = step - 1; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 2299 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2300 | if (cur + step >= (size_t)Py_SIZE(self)) |
| 2301 | lim = Py_SIZE(self) - cur - 1; |
| 2302 | memmove(self->ob_item + (cur - i) * itemsize, |
| 2303 | self->ob_item + (cur + 1) * itemsize, |
| 2304 | lim * itemsize); |
| 2305 | } |
| 2306 | cur = start + slicelength * step; |
| 2307 | if (cur < (size_t)Py_SIZE(self)) { |
| 2308 | memmove(self->ob_item + (cur-slicelength) * itemsize, |
| 2309 | self->ob_item + cur * itemsize, |
| 2310 | (Py_SIZE(self) - cur) * itemsize); |
| 2311 | } |
| 2312 | if (array_resize(self, Py_SIZE(self) - slicelength) < 0) |
| 2313 | return -1; |
| 2314 | return 0; |
| 2315 | } |
| 2316 | else { |
| 2317 | Py_ssize_t cur, i; |
| 2318 | |
| 2319 | if (needed != slicelength) { |
| 2320 | PyErr_Format(PyExc_ValueError, |
| 2321 | "attempt to assign array of size %zd " |
| 2322 | "to extended slice of size %zd", |
| 2323 | needed, slicelength); |
| 2324 | return -1; |
| 2325 | } |
| 2326 | for (cur = start, i = 0; i < slicelength; |
| 2327 | cur += step, i++) { |
| 2328 | memcpy(self->ob_item + cur * itemsize, |
| 2329 | other->ob_item + i * itemsize, |
| 2330 | itemsize); |
| 2331 | } |
| 2332 | return 0; |
| 2333 | } |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | static PyMappingMethods array_as_mapping = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2337 | (lenfunc)array_length, |
| 2338 | (binaryfunc)array_subscr, |
| 2339 | (objobjargproc)array_ass_subscr |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 2340 | }; |
| 2341 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2342 | static const void *emptybuf = ""; |
| 2343 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 2344 | |
| 2345 | static int |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 2346 | array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 2347 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2348 | if (view==NULL) goto finish; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 2349 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2350 | view->buf = (void *)self->ob_item; |
| 2351 | view->obj = (PyObject*)self; |
| 2352 | Py_INCREF(self); |
| 2353 | if (view->buf == NULL) |
| 2354 | view->buf = (void *)emptybuf; |
| 2355 | view->len = (Py_SIZE(self)) * self->ob_descr->itemsize; |
| 2356 | view->readonly = 0; |
| 2357 | view->ndim = 1; |
| 2358 | view->itemsize = self->ob_descr->itemsize; |
| 2359 | view->suboffsets = NULL; |
| 2360 | view->shape = NULL; |
| 2361 | if ((flags & PyBUF_ND)==PyBUF_ND) { |
| 2362 | view->shape = &((Py_SIZE(self))); |
| 2363 | } |
| 2364 | view->strides = NULL; |
| 2365 | if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES) |
| 2366 | view->strides = &(view->itemsize); |
| 2367 | view->format = NULL; |
| 2368 | view->internal = NULL; |
| 2369 | if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) { |
| 2370 | view->format = self->ob_descr->formats; |
Travis E. Oliphant | ddacf96 | 2007-10-13 21:03:27 +0000 | [diff] [blame] | 2371 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2372 | if (self->ob_descr->typecode == 'u') { |
| 2373 | view->format = "w"; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 2374 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2375 | #endif |
| 2376 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 2377 | |
| 2378 | finish: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2379 | self->ob_exports++; |
| 2380 | return 0; |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 2383 | static void |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 2384 | array_buffer_relbuf(arrayobject *self, Py_buffer *view) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 2385 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2386 | self->ob_exports--; |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 2387 | } |
| 2388 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 2389 | static PySequenceMethods array_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2390 | (lenfunc)array_length, /*sq_length*/ |
| 2391 | (binaryfunc)array_concat, /*sq_concat*/ |
| 2392 | (ssizeargfunc)array_repeat, /*sq_repeat*/ |
| 2393 | (ssizeargfunc)array_item, /*sq_item*/ |
| 2394 | 0, /*sq_slice*/ |
| 2395 | (ssizeobjargproc)array_ass_item, /*sq_ass_item*/ |
| 2396 | 0, /*sq_ass_slice*/ |
| 2397 | (objobjproc)array_contains, /*sq_contains*/ |
| 2398 | (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/ |
| 2399 | (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 2400 | }; |
| 2401 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 2402 | static PyBufferProcs array_as_buffer = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2403 | (getbufferproc)array_buffer_getbuf, |
| 2404 | (releasebufferproc)array_buffer_relbuf |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 2405 | }; |
| 2406 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 2407 | static PyObject * |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2408 | array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 2409 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2410 | int c; |
| 2411 | PyObject *initial = NULL, *it = NULL; |
| 2412 | struct arraydescr *descr; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2413 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2414 | if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds)) |
| 2415 | return NULL; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2416 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2417 | if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial)) |
| 2418 | return NULL; |
Raymond Hettinger | 84fc9aa | 2003-04-24 10:41:55 +0000 | [diff] [blame] | 2419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2420 | if (!(initial == NULL || PyList_Check(initial) |
| 2421 | || PyByteArray_Check(initial) |
| 2422 | || PyBytes_Check(initial) |
| 2423 | || PyTuple_Check(initial) |
Alexander Belopolsky | ef4a03f | 2011-01-11 21:44:00 +0000 | [diff] [blame] | 2424 | || ((c=='u') && PyUnicode_Check(initial)) |
| 2425 | || (array_Check(initial) |
| 2426 | && c == ((arrayobject*)initial)->ob_descr->typecode))) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2427 | it = PyObject_GetIter(initial); |
| 2428 | if (it == NULL) |
| 2429 | return NULL; |
| 2430 | /* We set initial to NULL so that the subsequent code |
| 2431 | will create an empty array of the appropriate type |
| 2432 | and afterwards we can use array_iter_extend to populate |
| 2433 | the array. |
| 2434 | */ |
| 2435 | initial = NULL; |
| 2436 | } |
| 2437 | for (descr = descriptors; descr->typecode != '\0'; descr++) { |
| 2438 | if (descr->typecode == c) { |
| 2439 | PyObject *a; |
| 2440 | Py_ssize_t len; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2441 | |
Alexander Belopolsky | ef4a03f | 2011-01-11 21:44:00 +0000 | [diff] [blame] | 2442 | if (initial == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2443 | len = 0; |
Alexander Belopolsky | ef4a03f | 2011-01-11 21:44:00 +0000 | [diff] [blame] | 2444 | else if (PyList_Check(initial)) |
| 2445 | len = PyList_GET_SIZE(initial); |
| 2446 | else if (PyTuple_Check(initial) || array_Check(initial)) |
| 2447 | len = Py_SIZE(initial); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2448 | else |
Alexander Belopolsky | ef4a03f | 2011-01-11 21:44:00 +0000 | [diff] [blame] | 2449 | len = 0; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2450 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2451 | a = newarrayobject(type, len, descr); |
| 2452 | if (a == NULL) |
| 2453 | return NULL; |
| 2454 | |
Alexander Belopolsky | ef4a03f | 2011-01-11 21:44:00 +0000 | [diff] [blame] | 2455 | if (len > 0 && !array_Check(initial)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2456 | Py_ssize_t i; |
| 2457 | for (i = 0; i < len; i++) { |
| 2458 | PyObject *v = |
| 2459 | PySequence_GetItem(initial, i); |
| 2460 | if (v == NULL) { |
| 2461 | Py_DECREF(a); |
| 2462 | return NULL; |
| 2463 | } |
| 2464 | if (setarrayitem(a, i, v) != 0) { |
| 2465 | Py_DECREF(v); |
| 2466 | Py_DECREF(a); |
| 2467 | return NULL; |
| 2468 | } |
| 2469 | Py_DECREF(v); |
| 2470 | } |
| 2471 | } |
| 2472 | else if (initial != NULL && (PyByteArray_Check(initial) || |
| 2473 | PyBytes_Check(initial))) { |
| 2474 | PyObject *t_initial, *v; |
| 2475 | t_initial = PyTuple_Pack(1, initial); |
| 2476 | if (t_initial == NULL) { |
| 2477 | Py_DECREF(a); |
| 2478 | return NULL; |
| 2479 | } |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 2480 | v = array_frombytes((arrayobject *)a, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2481 | t_initial); |
| 2482 | Py_DECREF(t_initial); |
| 2483 | if (v == NULL) { |
| 2484 | Py_DECREF(a); |
| 2485 | return NULL; |
| 2486 | } |
| 2487 | Py_DECREF(v); |
| 2488 | } |
| 2489 | else if (initial != NULL && PyUnicode_Check(initial)) { |
| 2490 | Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial); |
| 2491 | if (n > 0) { |
| 2492 | arrayobject *self = (arrayobject *)a; |
| 2493 | char *item = self->ob_item; |
| 2494 | item = (char *)PyMem_Realloc(item, n); |
| 2495 | if (item == NULL) { |
| 2496 | PyErr_NoMemory(); |
| 2497 | Py_DECREF(a); |
| 2498 | return NULL; |
| 2499 | } |
| 2500 | self->ob_item = item; |
| 2501 | Py_SIZE(self) = n / sizeof(Py_UNICODE); |
| 2502 | memcpy(item, PyUnicode_AS_DATA(initial), n); |
| 2503 | self->allocated = Py_SIZE(self); |
| 2504 | } |
| 2505 | } |
Alexander Belopolsky | ef4a03f | 2011-01-11 21:44:00 +0000 | [diff] [blame] | 2506 | else if (initial != NULL && array_Check(initial)) { |
| 2507 | arrayobject *self = (arrayobject *)a; |
| 2508 | arrayobject *other = (arrayobject *)initial; |
| 2509 | memcpy(self->ob_item, other->ob_item, len * other->ob_descr->itemsize); |
| 2510 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2511 | if (it != NULL) { |
| 2512 | if (array_iter_extend((arrayobject *)a, it) == -1) { |
| 2513 | Py_DECREF(it); |
| 2514 | Py_DECREF(a); |
| 2515 | return NULL; |
| 2516 | } |
| 2517 | Py_DECREF(it); |
| 2518 | } |
| 2519 | return a; |
| 2520 | } |
| 2521 | } |
| 2522 | PyErr_SetString(PyExc_ValueError, |
| 2523 | "bad typecode (must be b, B, u, h, H, i, I, l, L, f or d)"); |
| 2524 | return NULL; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 2525 | } |
| 2526 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 2527 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2528 | PyDoc_STRVAR(module_doc, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2529 | "This module defines an object type which can efficiently represent\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2530 | an array of basic values: characters, integers, floating point\n\ |
| 2531 | numbers. Arrays are sequence types and behave very much like lists,\n\ |
| 2532 | except that the type of objects stored in them is constrained. The\n\ |
| 2533 | type is specified at object creation time by using a type code, which\n\ |
| 2534 | is a single character. The following type codes are defined:\n\ |
| 2535 | \n\ |
| 2536 | Type code C Type Minimum size in bytes \n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2537 | 'b' signed integer 1 \n\ |
| 2538 | 'B' unsigned integer 1 \n\ |
Travis E. Oliphant | d5c0add | 2007-10-12 22:05:15 +0000 | [diff] [blame] | 2539 | 'u' Unicode character 2 (see note) \n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2540 | 'h' signed integer 2 \n\ |
| 2541 | 'H' unsigned integer 2 \n\ |
| 2542 | 'i' signed integer 2 \n\ |
| 2543 | 'I' unsigned integer 2 \n\ |
| 2544 | 'l' signed integer 4 \n\ |
| 2545 | 'L' unsigned integer 4 \n\ |
| 2546 | 'f' floating point 4 \n\ |
| 2547 | 'd' floating point 8 \n\ |
| 2548 | \n\ |
Travis E. Oliphant | d5c0add | 2007-10-12 22:05:15 +0000 | [diff] [blame] | 2549 | NOTE: The 'u' typecode corresponds to Python's unicode character. On \n\ |
| 2550 | narrow builds this is 2-bytes on wide builds this is 4-bytes.\n\ |
| 2551 | \n\ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2552 | The constructor is:\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2553 | \n\ |
| 2554 | array(typecode [, initializer]) -- create a new array\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2555 | "); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2556 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2557 | PyDoc_STRVAR(arraytype_doc, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2558 | "array(typecode [, initializer]) -> array\n\ |
| 2559 | \n\ |
| 2560 | Return a new array whose items are restricted by typecode, and\n\ |
Raymond Hettinger | 6ab78cd | 2004-08-29 07:50:43 +0000 | [diff] [blame] | 2561 | initialized from the optional initializer value, which must be a list,\n\ |
Florent Xicluna | 0e686cb | 2011-12-09 23:41:19 +0100 | [diff] [blame] | 2562 | string or iterable over elements of the appropriate type.\n\ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2563 | \n\ |
| 2564 | 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] | 2565 | the type of objects stored in them is constrained.\n\ |
| 2566 | \n\ |
| 2567 | Methods:\n\ |
| 2568 | \n\ |
| 2569 | append() -- append a new item to the end of the array\n\ |
| 2570 | buffer_info() -- return information giving the current memory info\n\ |
| 2571 | byteswap() -- byteswap all the items of the array\n\ |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 2572 | count() -- return number of occurrences of an object\n\ |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 2573 | extend() -- extend array by appending multiple elements from an iterable\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2574 | fromfile() -- read items from a file object\n\ |
| 2575 | fromlist() -- append items from the list\n\ |
Florent Xicluna | c45fb25 | 2011-10-24 13:14:55 +0200 | [diff] [blame] | 2576 | frombytes() -- append items from the string\n\ |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 2577 | index() -- return index of first occurrence of an object\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2578 | 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] | 2579 | pop() -- remove and return item (default last)\n\ |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 2580 | remove() -- remove first occurrence of an object\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2581 | reverse() -- reverse the order of the items in the array\n\ |
| 2582 | tofile() -- write all items to a file object\n\ |
| 2583 | tolist() -- return the array converted to an ordinary list\n\ |
Florent Xicluna | c45fb25 | 2011-10-24 13:14:55 +0200 | [diff] [blame] | 2584 | tobytes() -- return the array converted to a string\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2585 | \n\ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2586 | Attributes:\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2587 | \n\ |
| 2588 | typecode -- the typecode character used to create the array\n\ |
| 2589 | itemsize -- the length in bytes of one array item\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2590 | "); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2591 | |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2592 | static PyObject *array_iter(arrayobject *ao); |
| 2593 | |
Tim Peters | 0c32279 | 2002-07-17 16:49:03 +0000 | [diff] [blame] | 2594 | static PyTypeObject Arraytype = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2595 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2596 | "array.array", |
| 2597 | sizeof(arrayobject), |
| 2598 | 0, |
| 2599 | (destructor)array_dealloc, /* tp_dealloc */ |
| 2600 | 0, /* tp_print */ |
| 2601 | 0, /* tp_getattr */ |
| 2602 | 0, /* tp_setattr */ |
| 2603 | 0, /* tp_reserved */ |
| 2604 | (reprfunc)array_repr, /* tp_repr */ |
| 2605 | 0, /* tp_as_number*/ |
| 2606 | &array_as_sequence, /* tp_as_sequence*/ |
| 2607 | &array_as_mapping, /* tp_as_mapping*/ |
| 2608 | 0, /* tp_hash */ |
| 2609 | 0, /* tp_call */ |
| 2610 | 0, /* tp_str */ |
| 2611 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2612 | 0, /* tp_setattro */ |
| 2613 | &array_as_buffer, /* tp_as_buffer*/ |
| 2614 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2615 | arraytype_doc, /* tp_doc */ |
| 2616 | 0, /* tp_traverse */ |
| 2617 | 0, /* tp_clear */ |
| 2618 | array_richcompare, /* tp_richcompare */ |
| 2619 | offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */ |
| 2620 | (getiterfunc)array_iter, /* tp_iter */ |
| 2621 | 0, /* tp_iternext */ |
| 2622 | array_methods, /* tp_methods */ |
| 2623 | 0, /* tp_members */ |
| 2624 | array_getsets, /* tp_getset */ |
| 2625 | 0, /* tp_base */ |
| 2626 | 0, /* tp_dict */ |
| 2627 | 0, /* tp_descr_get */ |
| 2628 | 0, /* tp_descr_set */ |
| 2629 | 0, /* tp_dictoffset */ |
| 2630 | 0, /* tp_init */ |
| 2631 | PyType_GenericAlloc, /* tp_alloc */ |
| 2632 | array_new, /* tp_new */ |
| 2633 | PyObject_Del, /* tp_free */ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2634 | }; |
| 2635 | |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2636 | |
| 2637 | /*********************** Array Iterator **************************/ |
| 2638 | |
| 2639 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2640 | PyObject_HEAD |
| 2641 | Py_ssize_t index; |
| 2642 | arrayobject *ao; |
| 2643 | PyObject * (*getitem)(struct arrayobject *, Py_ssize_t); |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2644 | } arrayiterobject; |
| 2645 | |
| 2646 | static PyTypeObject PyArrayIter_Type; |
| 2647 | |
| 2648 | #define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type) |
| 2649 | |
| 2650 | static PyObject * |
| 2651 | array_iter(arrayobject *ao) |
| 2652 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2653 | arrayiterobject *it; |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2654 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2655 | if (!array_Check(ao)) { |
| 2656 | PyErr_BadInternalCall(); |
| 2657 | return NULL; |
| 2658 | } |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2659 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2660 | it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type); |
| 2661 | if (it == NULL) |
| 2662 | return NULL; |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2663 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2664 | Py_INCREF(ao); |
| 2665 | it->ao = ao; |
| 2666 | it->index = 0; |
| 2667 | it->getitem = ao->ob_descr->getitem; |
| 2668 | PyObject_GC_Track(it); |
| 2669 | return (PyObject *)it; |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
| 2672 | static PyObject * |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2673 | arrayiter_next(arrayiterobject *it) |
| 2674 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2675 | assert(PyArrayIter_Check(it)); |
| 2676 | if (it->index < Py_SIZE(it->ao)) |
| 2677 | return (*it->getitem)(it->ao, it->index++); |
| 2678 | return NULL; |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2679 | } |
| 2680 | |
| 2681 | static void |
| 2682 | arrayiter_dealloc(arrayiterobject *it) |
| 2683 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2684 | PyObject_GC_UnTrack(it); |
| 2685 | Py_XDECREF(it->ao); |
| 2686 | PyObject_GC_Del(it); |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2687 | } |
| 2688 | |
| 2689 | static int |
| 2690 | arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg) |
| 2691 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2692 | Py_VISIT(it->ao); |
| 2693 | return 0; |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
| 2696 | static PyTypeObject PyArrayIter_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2697 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2698 | "arrayiterator", /* tp_name */ |
| 2699 | sizeof(arrayiterobject), /* tp_basicsize */ |
| 2700 | 0, /* tp_itemsize */ |
| 2701 | /* methods */ |
| 2702 | (destructor)arrayiter_dealloc, /* tp_dealloc */ |
| 2703 | 0, /* tp_print */ |
| 2704 | 0, /* tp_getattr */ |
| 2705 | 0, /* tp_setattr */ |
| 2706 | 0, /* tp_reserved */ |
| 2707 | 0, /* tp_repr */ |
| 2708 | 0, /* tp_as_number */ |
| 2709 | 0, /* tp_as_sequence */ |
| 2710 | 0, /* tp_as_mapping */ |
| 2711 | 0, /* tp_hash */ |
| 2712 | 0, /* tp_call */ |
| 2713 | 0, /* tp_str */ |
| 2714 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2715 | 0, /* tp_setattro */ |
| 2716 | 0, /* tp_as_buffer */ |
| 2717 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 2718 | 0, /* tp_doc */ |
| 2719 | (traverseproc)arrayiter_traverse, /* tp_traverse */ |
| 2720 | 0, /* tp_clear */ |
| 2721 | 0, /* tp_richcompare */ |
| 2722 | 0, /* tp_weaklistoffset */ |
| 2723 | PyObject_SelfIter, /* tp_iter */ |
| 2724 | (iternextfunc)arrayiter_next, /* tp_iternext */ |
| 2725 | 0, /* tp_methods */ |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2726 | }; |
| 2727 | |
| 2728 | |
| 2729 | /*********************** Install Module **************************/ |
| 2730 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2731 | /* No functions in array module. */ |
| 2732 | static PyMethodDef a_methods[] = { |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 2733 | {"_array_reconstructor", array_reconstructor, METH_VARARGS, |
| 2734 | PyDoc_STR("Internal. Used for pickling support.")}, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2735 | {NULL, NULL, 0, NULL} /* Sentinel */ |
| 2736 | }; |
| 2737 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2738 | static struct PyModuleDef arraymodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2739 | PyModuleDef_HEAD_INIT, |
| 2740 | "array", |
| 2741 | module_doc, |
| 2742 | -1, |
| 2743 | a_methods, |
| 2744 | NULL, |
| 2745 | NULL, |
| 2746 | NULL, |
| 2747 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2748 | }; |
| 2749 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2750 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 2751 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2752 | PyInit_array(void) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 2753 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2754 | PyObject *m; |
| 2755 | PyObject *typecodes; |
| 2756 | Py_ssize_t size = 0; |
| 2757 | register Py_UNICODE *p; |
| 2758 | struct arraydescr *descr; |
Fred Drake | 0d40ba4 | 2000-02-04 20:33:49 +0000 | [diff] [blame] | 2759 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2760 | if (PyType_Ready(&Arraytype) < 0) |
| 2761 | return NULL; |
| 2762 | Py_TYPE(&PyArrayIter_Type) = &PyType_Type; |
| 2763 | m = PyModule_Create(&arraymodule); |
| 2764 | if (m == NULL) |
| 2765 | return NULL; |
Fred Drake | f4e3484 | 2002-04-01 03:45:06 +0000 | [diff] [blame] | 2766 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2767 | Py_INCREF((PyObject *)&Arraytype); |
| 2768 | PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype); |
| 2769 | Py_INCREF((PyObject *)&Arraytype); |
| 2770 | PyModule_AddObject(m, "array", (PyObject *)&Arraytype); |
Travis E. Oliphant | d5c0add | 2007-10-12 22:05:15 +0000 | [diff] [blame] | 2771 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2772 | for (descr=descriptors; descr->typecode != '\0'; descr++) { |
| 2773 | size++; |
| 2774 | } |
Travis E. Oliphant | d5c0add | 2007-10-12 22:05:15 +0000 | [diff] [blame] | 2775 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2776 | typecodes = PyUnicode_FromStringAndSize(NULL, size); |
| 2777 | p = PyUnicode_AS_UNICODE(typecodes); |
| 2778 | for (descr = descriptors; descr->typecode != '\0'; descr++) { |
| 2779 | *p++ = (char)descr->typecode; |
| 2780 | } |
| 2781 | |
| 2782 | PyModule_AddObject(m, "typecodes", (PyObject *)typecodes); |
| 2783 | |
| 2784 | if (PyErr_Occurred()) { |
| 2785 | Py_DECREF(m); |
| 2786 | m = NULL; |
| 2787 | } |
| 2788 | return m; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 2789 | } |