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