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