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