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