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