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