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