blob: 72b90111a837353e9a14a7a5de4679accd857286 [file] [log] [blame]
Guido van Rossum778983b1993-02-19 15:55:02 +00001/* 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öwis18e16552006-02-15 17:27:45 +00006#define PY_SSIZE_T_CLEAN
Roger E. Masse2919eaa1996-12-09 20:10:36 +00007#include "Python.h"
Victor Stinnercdad2722021-04-22 00:52:52 +02008#include "pycore_moduleobject.h" // _PyModule_GetState()
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01009#include "structmember.h" // PyMemberDef
Victor Stinner4a21e572020-04-15 02:35:41 +020010#include <stddef.h> // offsetof()
Roger E. Masse5817f8f1996-12-09 22:24:19 +000011
Guido van Rossum0c709541994-08-19 12:01:32 +000012#ifdef STDC_HEADERS
13#include <stddef.h>
Guido van Rossum7f1de831999-08-27 20:33:52 +000014#else /* !STDC_HEADERS */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000015#ifdef HAVE_SYS_TYPES_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000016#include <sys/types.h> /* For size_t */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000017#endif /* HAVE_SYS_TYPES_H */
Guido van Rossum7f1de831999-08-27 20:33:52 +000018#endif /* !STDC_HEADERS */
Guido van Rossum778983b1993-02-19 15:55:02 +000019
Brett Cannon1eb32c22014-10-10 16:26:45 -040020/*[clinic input]
Brett Cannon1eb32c22014-10-10 16:26:45 -040021module array
22[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030023/*[clinic end generated code: output=da39a3ee5e6b4b0d input=7d1b8d7f5958fd83]*/
Brett Cannon1eb32c22014-10-10 16:26:45 -040024
Guido van Rossum778983b1993-02-19 15:55:02 +000025struct arrayobject; /* Forward */
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +010026static struct PyModuleDef arraymodule;
Guido van Rossum778983b1993-02-19 15:55:02 +000027
Tim Petersbb307342000-09-10 05:22:54 +000028/* 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 Rossum778983b1993-02-19 15:55:02 +000032struct arraydescr {
Victor Stinnerf8bb7d02011-09-30 00:03:59 +020033 char typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000034 int itemsize;
35 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
36 int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *);
Adrian Wielgosik7c17e232017-08-17 12:46:06 +000037 int (*compareitems)(const void *, const void *, Py_ssize_t);
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020038 const char *formats;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039 int is_integer_type;
40 int is_signed;
Guido van Rossum778983b1993-02-19 15:55:02 +000041};
42
43typedef struct arrayobject {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044 PyObject_VAR_HEAD
45 char *ob_item;
46 Py_ssize_t allocated;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020047 const struct arraydescr *ob_descr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 PyObject *weakreflist; /* List of weak references */
Hai Shi06cd5b62019-10-21 14:31:46 +080049 Py_ssize_t ob_exports; /* Number of exported buffers */
Guido van Rossum778983b1993-02-19 15:55:02 +000050} arrayobject;
51
Brett Cannon1eb32c22014-10-10 16:26:45 -040052typedef 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 Aasland75bf1072021-01-02 17:38:47 +010059typedef struct {
60 PyTypeObject *ArrayType;
61 PyTypeObject *ArrayIterType;
62} array_state;
Brett Cannon1eb32c22014-10-10 16:26:45 -040063
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +010064static array_state *
65get_array_state(PyObject *module)
66{
Victor Stinnercdad2722021-04-22 00:52:52 +020067 return (array_state *)_PyModule_GetState(module);
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +010068}
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 Cannon1eb32c22014-10-10 16:26:45 -040074
Larry Hastingsdfbeb162014-10-13 10:39:41 +010075enum 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 Cannon1eb32c22014-10-10 16:26:45 -0400119#include "clinic/arraymodule.c.h"
120
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +0100121#define array_Check(op, state) PyObject_TypeCheck(op, state->ArrayType)
Guido van Rossum778983b1993-02-19 15:55:02 +0000122
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000123static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000124array_resize(arrayobject *self, Py_ssize_t newsize)
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 char *items;
127 size_t _new_size;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 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 Pitrou3ad3a0d2008-12-18 17:08:32 +0000134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 /* 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 Hettinger6e2ee862004-03-14 04:37:50 +0000139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 if (self->allocated >= newsize &&
141 Py_SIZE(self) < newsize + 16 &&
142 self->ob_item != NULL) {
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100143 Py_SET_SIZE(self, newsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 return 0;
145 }
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 if (newsize == 0) {
Victor Stinner00d7abd2020-12-01 09:56:42 +0100148 PyMem_Free(self->ob_item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 self->ob_item = NULL;
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100150 Py_SET_SIZE(self, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 self->allocated = 0;
152 return 0;
153 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 /* 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 Hettinger6e2ee862004-03-14 04:37:50 +0000166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 _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 Stinner60ac6ed2020-02-07 23:18:08 +0100180 Py_SET_SIZE(self, newsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 self->allocated = _new_size;
182 return 0;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000183}
184
Tim Petersbb307342000-09-10 05:22:54 +0000185/****************************************************************************
186Get and Set functions for each type.
187A Get function takes an arrayobject* and an integer index, returning the
188array value at that index wrapped in an appropriate PyObject*.
189A Set function takes an arrayobject, integer index, and PyObject*; sets
190the array value at that index to the raw C data extracted from the PyObject*,
191and returns 0 if successful, else nonzero on failure (PyObject* not of an
192appropriate type or value).
193Note that the basic Get and Set functions do NOT check that the index is
194in bounds; that's the responsibility of the caller.
195****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +0000196
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000197static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000198b_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000199{
Disconnect3d13ab5702019-07-11 23:57:42 +0200200 long x = ((signed char *)ap->ob_item)[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 return PyLong_FromLong(x);
Guido van Rossum778983b1993-02-19 15:55:02 +0000202}
203
204static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000205b_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000206{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 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 Rossum778983b1993-02-19 15:55:02 +0000226}
227
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000228static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000229BB_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000230{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 long x = ((unsigned char *)ap->ob_item)[i];
232 return PyLong_FromLong(x);
Guido van Rossum549ab711997-01-03 19:09:47 +0000233}
234
Fred Drake541dc3b2000-06-28 17:49:30 +0000235static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000236BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000237{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 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 Drake541dc3b2000-06-28 17:49:30 +0000245}
Guido van Rossum549ab711997-01-03 19:09:47 +0000246
Martin v. Löwis99866332002-03-01 10:27:01 +0000247static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000248u_getitem(arrayobject *ap, Py_ssize_t i)
Martin v. Löwis99866332002-03-01 10:27:01 +0000249{
Inada Naokid5d9a712020-05-11 15:37:25 +0900250 return PyUnicode_FromOrdinal(((wchar_t *) ap->ob_item)[i]);
Martin v. Löwis99866332002-03-01 10:27:01 +0000251}
252
253static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000254u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Martin v. Löwis99866332002-03-01 10:27:01 +0000255{
Inada Naokid5d9a712020-05-11 15:37:25 +0900256 PyObject *u;
257 if (!PyArg_Parse(v, "U;array item must be unicode character", &u)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 return -1;
Inada Naokid5d9a712020-05-11 15:37:25 +0900259 }
260
261 Py_ssize_t len = PyUnicode_AsWideChar(u, NULL, 0);
262 if (len != 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 PyErr_SetString(PyExc_TypeError,
264 "array item must be unicode character");
265 return -1;
266 }
Inada Naokid5d9a712020-05-11 15:37:25 +0900267
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 Pitrouf95a1b32010-05-09 15:52:27 +0000275 return 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000276}
Martin v. Löwis99866332002-03-01 10:27:01 +0000277
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +0000278
Guido van Rossum549ab711997-01-03 19:09:47 +0000279static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000280h_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 return PyLong_FromLong((long) ((short *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000283}
284
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +0000285
Guido van Rossum778983b1993-02-19 15:55:02 +0000286static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000287h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 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 Rossum778983b1993-02-19 15:55:02 +0000296}
297
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000298static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000299HH_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 return PyLong_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000302}
303
Fred Drake541dc3b2000-06-28 17:49:30 +0000304static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000305HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 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 Drake541dc3b2000-06-28 17:49:30 +0000325}
Guido van Rossum549ab711997-01-03 19:09:47 +0000326
327static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000328i_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossume77a7571993-11-03 15:01:26 +0000329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 return PyLong_FromLong((long) ((int *)ap->ob_item)[i]);
Guido van Rossume77a7571993-11-03 15:01:26 +0000331}
332
333static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000334i_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossume77a7571993-11-03 15:01:26 +0000335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 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 Rossume77a7571993-11-03 15:01:26 +0000343}
344
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000345static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000346II_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000347{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 return PyLong_FromUnsignedLong(
349 (unsigned long) ((unsigned int *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000350}
351
352static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000353II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000354{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 unsigned long x;
orenmn964281a2017-03-09 11:35:28 +0200356 int do_decref = 0; /* if nb_int was called */
357
358 if (!PyLong_Check(v)) {
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300359 v = _PyNumber_Index(v);
orenmn964281a2017-03-09 11:35:28 +0200360 if (NULL == v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 return -1;
362 }
orenmn964281a2017-03-09 11:35:28 +0200363 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 Pitrouf95a1b32010-05-09 15:52:27 +0000371 }
372 if (x > UINT_MAX) {
373 PyErr_SetString(PyExc_OverflowError,
orenmn964281a2017-03-09 11:35:28 +0200374 "unsigned int is greater than maximum");
375 if (do_decref) {
376 Py_DECREF(v);
377 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 return -1;
379 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 if (i >= 0)
381 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
orenmn964281a2017-03-09 11:35:28 +0200382
383 if (do_decref) {
384 Py_DECREF(v);
385 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 return 0;
Guido van Rossum549ab711997-01-03 19:09:47 +0000387}
388
389static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000390l_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000391{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 return PyLong_FromLong(((long *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000393}
394
395static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000396l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 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 Rossum778983b1993-02-19 15:55:02 +0000404}
405
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000406static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000407LL_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000410}
411
412static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000413LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 unsigned long x;
orenmn964281a2017-03-09 11:35:28 +0200416 int do_decref = 0; /* if nb_int was called */
417
418 if (!PyLong_Check(v)) {
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300419 v = _PyNumber_Index(v);
orenmn964281a2017-03-09 11:35:28 +0200420 if (NULL == v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 return -1;
422 }
orenmn964281a2017-03-09 11:35:28 +0200423 do_decref = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 }
orenmn964281a2017-03-09 11:35:28 +0200425 x = PyLong_AsUnsignedLong(v);
426 if (x == (unsigned long)-1 && PyErr_Occurred()) {
427 if (do_decref) {
428 Py_DECREF(v);
429 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 return -1;
431 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 if (i >= 0)
433 ((unsigned long *)ap->ob_item)[i] = x;
orenmn964281a2017-03-09 11:35:28 +0200434
435 if (do_decref) {
436 Py_DECREF(v);
437 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 return 0;
Guido van Rossum549ab711997-01-03 19:09:47 +0000439}
440
Meador Inge1c9f0c92011-09-20 19:55:51 -0500441static PyObject *
442q_getitem(arrayobject *ap, Py_ssize_t i)
443{
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700444 return PyLong_FromLongLong(((long long *)ap->ob_item)[i]);
Meador Inge1c9f0c92011-09-20 19:55:51 -0500445}
446
447static int
448q_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
449{
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700450 long long x;
Meador Inge1c9f0c92011-09-20 19:55:51 -0500451 if (!PyArg_Parse(v, "L;array item must be integer", &x))
452 return -1;
453 if (i >= 0)
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700454 ((long long *)ap->ob_item)[i] = x;
Meador Inge1c9f0c92011-09-20 19:55:51 -0500455 return 0;
456}
457
458static PyObject *
459QQ_getitem(arrayobject *ap, Py_ssize_t i)
460{
461 return PyLong_FromUnsignedLongLong(
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700462 ((unsigned long long *)ap->ob_item)[i]);
Meador Inge1c9f0c92011-09-20 19:55:51 -0500463}
464
465static int
466QQ_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
467{
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700468 unsigned long long x;
orenmn964281a2017-03-09 11:35:28 +0200469 int do_decref = 0; /* if nb_int was called */
470
471 if (!PyLong_Check(v)) {
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300472 v = _PyNumber_Index(v);
orenmn964281a2017-03-09 11:35:28 +0200473 if (NULL == v) {
Meador Inge1c9f0c92011-09-20 19:55:51 -0500474 return -1;
475 }
orenmn964281a2017-03-09 11:35:28 +0200476 do_decref = 1;
Meador Inge1c9f0c92011-09-20 19:55:51 -0500477 }
orenmn964281a2017-03-09 11:35:28 +0200478 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 Inge1c9f0c92011-09-20 19:55:51 -0500485 if (i >= 0)
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700486 ((unsigned long long *)ap->ob_item)[i] = x;
orenmn964281a2017-03-09 11:35:28 +0200487
488 if (do_decref) {
489 Py_DECREF(v);
490 }
Meador Inge1c9f0c92011-09-20 19:55:51 -0500491 return 0;
492}
Meador Inge1c9f0c92011-09-20 19:55:51 -0500493
Guido van Rossum549ab711997-01-03 19:09:47 +0000494static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000495f_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000498}
499
500static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000501f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 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 Rossum778983b1993-02-19 15:55:02 +0000509}
510
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000511static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000512d_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000515}
516
517static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000518d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000519{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 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 Rossum778983b1993-02-19 15:55:02 +0000526}
527
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000528#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
539DEFINE_COMPAREITEMS(b, signed char)
540DEFINE_COMPAREITEMS(BB, unsigned char)
Inada Naokid5d9a712020-05-11 15:37:25 +0900541DEFINE_COMPAREITEMS(u, wchar_t)
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000542DEFINE_COMPAREITEMS(h, short)
543DEFINE_COMPAREITEMS(HH, unsigned short)
544DEFINE_COMPAREITEMS(i, int)
545DEFINE_COMPAREITEMS(II, unsigned int)
546DEFINE_COMPAREITEMS(l, long)
547DEFINE_COMPAREITEMS(LL, unsigned long)
548DEFINE_COMPAREITEMS(q, long long)
549DEFINE_COMPAREITEMS(QQ, unsigned long long)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000550
Alexandre Vassalottiad077152009-07-15 17:49:23 +0000551/* Description of types.
552 *
553 * Don't forget to update typecode_to_mformat_code() if you add a new
554 * typecode.
555 */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200556static const struct arraydescr descriptors[] = {
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000557 {'b', 1, b_getitem, b_setitem, b_compareitems, "b", 1, 1},
558 {'B', 1, BB_getitem, BB_setitem, BB_compareitems, "B", 1, 0},
Inada Naokid5d9a712020-05-11 15:37:25 +0900559 {'u', sizeof(wchar_t), u_getitem, u_setitem, u_compareitems, "u", 0, 0},
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000560 {'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 Pitrouf95a1b32010-05-09 15:52:27 +0000570 {'\0', 0, 0, 0, 0, 0, 0} /* Sentinel */
Guido van Rossum778983b1993-02-19 15:55:02 +0000571};
Tim Petersbb307342000-09-10 05:22:54 +0000572
573/****************************************************************************
574Implementations of array object methods.
575****************************************************************************/
Brett Cannon1eb32c22014-10-10 16:26:45 -0400576/*[clinic input]
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +0100577class array.array "arrayobject *" "ArrayType"
Brett Cannon1eb32c22014-10-10 16:26:45 -0400578[clinic start generated code]*/
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +0100579/*[clinic end generated code: output=da39a3ee5e6b4b0d input=a5c29edf59f176a3]*/
Guido van Rossum778983b1993-02-19 15:55:02 +0000580
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000581static PyObject *
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200582newarrayobject(PyTypeObject *type, Py_ssize_t size, const struct arraydescr *descr)
Guido van Rossum778983b1993-02-19 15:55:02 +0000583{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 arrayobject *op;
585 size_t nbytes;
Martin v. Löwis99866332002-03-01 10:27:01 +0000586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 if (size < 0) {
588 PyErr_BadInternalCall();
589 return NULL;
590 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 /* Check for overflow */
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100593 if (size > PY_SSIZE_T_MAX / descr->itemsize) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 return PyErr_NoMemory();
595 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100596 nbytes = size * descr->itemsize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 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 Stinner60ac6ed2020-02-07 23:18:08 +0100604 Py_SET_SIZE(op, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 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 Rossum778983b1993-02-19 15:55:02 +0000617}
618
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000619static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000620getarrayitem(PyObject *op, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000621{
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +0100622#ifndef NDEBUG
623 array_state *state = find_array_state_by_type(Py_TYPE(op));
624 assert(array_Check(op, state));
625#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200626 arrayobject *ap;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 ap = (arrayobject *)op;
628 assert(i>=0 && i<Py_SIZE(ap));
629 return (*ap->ob_descr->getitem)(ap, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000630}
631
632static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000633ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 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 Hettinger6e2ee862004-03-14 04:37:50 +0000643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 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 Rossum778983b1993-02-19 15:55:02 +0000660}
661
Guido van Rossum778983b1993-02-19 15:55:02 +0000662/* Methods */
663
Miss Islington (bot)534da742021-05-25 11:49:19 -0700664static int
665array_tp_traverse(arrayobject *op, visitproc visit, void *arg)
666{
667 Py_VISIT(Py_TYPE(op));
668 return 0;
669}
670
Guido van Rossum778983b1993-02-19 15:55:02 +0000671static void
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000672array_dealloc(arrayobject *op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000673{
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +0100674 PyTypeObject *tp = Py_TYPE(op);
Miss Islington (bot)534da742021-05-25 11:49:19 -0700675 PyObject_GC_UnTrack(op);
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +0100676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 if (op->weakreflist != NULL)
678 PyObject_ClearWeakRefs((PyObject *) op);
679 if (op->ob_item != NULL)
Victor Stinner00d7abd2020-12-01 09:56:42 +0100680 PyMem_Free(op->ob_item);
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +0100681 tp->tp_free(op);
682 Py_DECREF(tp);
Guido van Rossum778983b1993-02-19 15:55:02 +0000683}
684
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000685static PyObject *
686array_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000687{
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +0100688 array_state *state = find_array_state_by_type(Py_TYPE(v));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 arrayobject *va, *wa;
690 PyObject *vi = NULL;
691 PyObject *wi = NULL;
692 Py_ssize_t i, k;
693 PyObject *res;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000694
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +0100695 if (!array_Check(v, state) || !array_Check(w, state))
Brian Curtindfc80e32011-08-10 20:28:54 -0500696 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 va = (arrayobject *)v;
699 wa = (arrayobject *)w;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 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 Rossum9d19cb82001-01-18 01:02:55 +0000710
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000711 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 Pitrouf95a1b32010-05-09 15:52:27 +0000736 /* 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 Rossum9d19cb82001-01-18 01:02:55 +0000754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 if (k) {
756 /* No more items to compare -- compare sizes */
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000757 compare_sizes: ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 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 Wielgosik7c17e232017-08-17 12:46:06 +0000764 /* 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 Pitrouf95a1b32010-05-09 15:52:27 +0000768 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 Rossum9d19cb82001-01-18 01:02:55 +0000779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 /* 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 Rossum778983b1993-02-19 15:55:02 +0000796}
797
Martin v. Löwis18e16552006-02-15 17:27:45 +0000798static Py_ssize_t
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000799array_length(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +0000800{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 return Py_SIZE(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000802}
803
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000804static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000805array_item(arrayobject *a, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000806{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 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 Rossum778983b1993-02-19 15:55:02 +0000812}
813
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000814static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000815array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000816{
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +0100817 array_state *state = find_array_state_by_type(Py_TYPE(a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 arrayobject *np;
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +0100819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 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 Aasland75bf1072021-01-02 17:38:47 +0100830 np = (arrayobject *) newarrayobject(state->ArrayType, ihigh - ilow, a->ob_descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 if (np == NULL)
832 return NULL;
Martin Panterbe8da9c2016-09-07 11:04:41 +0000833 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 Pitrouf95a1b32010-05-09 15:52:27 +0000837 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000838}
839
Brett Cannon1eb32c22014-10-10 16:26:45 -0400840
841/*[clinic input]
842array.array.__copy__
843
844Return a copy of the array.
845[clinic start generated code]*/
846
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000847static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -0400848array_array___copy___impl(arrayobject *self)
849/*[clinic end generated code: output=dec7c3f925d9619e input=ad1ee5b086965f09]*/
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000850{
Brett Cannon1eb32c22014-10-10 16:26:45 -0400851 return array_slice(self, 0, Py_SIZE(self));
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000852}
853
Brett Cannon1eb32c22014-10-10 16:26:45 -0400854/*[clinic input]
855array.array.__deepcopy__
856
857 unused: object
858 /
859
860Return a copy of the array.
861[clinic start generated code]*/
862
863static PyObject *
864array_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 Hettinger3aa82c02004-03-13 18:18:51 +0000869
870static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000871array_concat(arrayobject *a, PyObject *bb)
Guido van Rossum778983b1993-02-19 15:55:02 +0000872{
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +0100873 array_state *state = find_array_state_by_type(Py_TYPE(a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 Py_ssize_t size;
875 arrayobject *np;
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +0100876 if (!array_Check(bb, state)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 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 Rossum778983b1993-02-19 15:55:02 +0000882#define b ((arrayobject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 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 Aasland75bf1072021-01-02 17:38:47 +0100891 np = (arrayobject *) newarrayobject(state->ArrayType, size, a->ob_descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 if (np == NULL) {
893 return NULL;
894 }
Martin Panterbe8da9c2016-09-07 11:04:41 +0000895 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 Pitrouf95a1b32010-05-09 15:52:27 +0000902 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000903#undef b
904}
905
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000906static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000907array_repeat(arrayobject *a, Py_ssize_t n)
Guido van Rossum778983b1993-02-19 15:55:02 +0000908{
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +0100909 array_state *state = find_array_state_by_type(Py_TYPE(a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 Py_ssize_t size;
911 arrayobject *np;
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000912 Py_ssize_t oldbytes, newbytes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 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 Aasland75bf1072021-01-02 17:38:47 +0100919 np = (arrayobject *) newarrayobject(state->ArrayType, size, a->ob_descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 if (np == NULL)
921 return NULL;
Martin Panterbe8da9c2016-09-07 11:04:41 +0000922 if (size == 0)
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000923 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 Heimesf051e432016-09-13 20:22:02 +0200931 memcpy(np->ob_item, a->ob_item, oldbytes);
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000932 while (done < newbytes) {
933 Py_ssize_t ncopy = (done <= newbytes-done) ? done : newbytes-done;
Christian Heimesf051e432016-09-13 20:22:02 +0200934 memcpy(np->ob_item+done, np->ob_item, ncopy);
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000935 done += ncopy;
936 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 }
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000938 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000939}
940
941static int
Martin Panter996d72b2016-07-25 02:21:14 +0000942array_del_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 char *item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 Py_ssize_t d; /* Change in size */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 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 Panter996d72b2016-07-25 02:21:14 +0000957 d = ihigh-ilow;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 /* 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 Panter996d72b2016-07-25 02:21:14 +0000966 if (d > 0) { /* Delete d items */
967 memmove(item + (ihigh-d)*a->ob_descr->itemsize,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 item + ihigh*a->ob_descr->itemsize,
969 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
Martin Panter996d72b2016-07-25 02:21:14 +0000970 if (array_resize(a, Py_SIZE(a) - d) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 return -1;
972 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000974}
975
976static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000977array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 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 Panter996d72b2016-07-25 02:21:14 +0000985 return array_del_slice(a, i, i+1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 return (*a->ob_descr->setitem)(a, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000987}
988
989static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000990setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000991{
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +0100992#ifndef NDEBUG
993 array_state *state = find_array_state_by_type(Py_TYPE(a));
994 assert(array_Check(a, state));
995#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 return array_ass_item((arrayobject *)a, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000997}
998
Martin v. Löwis99866332002-03-01 10:27:01 +0000999static int
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001000array_iter_extend(arrayobject *self, PyObject *bb)
1001{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 PyObject *it, *v;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 it = PyObject_GetIter(bb);
1005 if (it == NULL)
1006 return -1;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00001007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 while ((v = PyIter_Next(it)) != NULL) {
Mark Dickinson346f0af2010-08-06 09:36:57 +00001009 if (ins1(self, Py_SIZE(self), v) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 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 Hettinger49f9bd12004-03-14 05:43:59 +00001020}
1021
1022static int
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01001023array_do_extend(array_state *state, arrayobject *self, PyObject *bb)
Martin v. Löwis99866332002-03-01 10:27:01 +00001024{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 Py_ssize_t size, oldsize, bbsize;
Martin v. Löwis99866332002-03-01 10:27:01 +00001026
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01001027 if (!array_Check(bb, state))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 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 Panterbe8da9c2016-09-07 11:04:41 +00001046 if (bbsize > 0) {
1047 memcpy(self->ob_item + oldsize * self->ob_descr->itemsize,
1048 b->ob_item, bbsize * b->ob_descr->itemsize);
1049 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050
1051 return 0;
Martin v. Löwis99866332002-03-01 10:27:01 +00001052#undef b
1053}
1054
1055static PyObject *
1056array_inplace_concat(arrayobject *self, PyObject *bb)
1057{
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01001058 array_state *state = find_array_state_by_type(Py_TYPE(self));
1059
1060 if (!array_Check(bb, state)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 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 Aasland75bf1072021-01-02 17:38:47 +01001066 if (array_do_extend(state, self, bb) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 return NULL;
1068 Py_INCREF(self);
1069 return (PyObject *)self;
Martin v. Löwis99866332002-03-01 10:27:01 +00001070}
1071
1072static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001073array_inplace_repeat(arrayobject *self, Py_ssize_t n)
Martin v. Löwis99866332002-03-01 10:27:01 +00001074{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 char *items, *p;
1076 Py_ssize_t size, i;
Martin v. Löwis99866332002-03-01 10:27:01 +00001077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 if (Py_SIZE(self) > 0) {
1079 if (n < 0)
1080 n = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 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öwis99866332002-03-01 10:27:01 +00001099}
1100
1101
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001102static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001103ins(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +00001104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 if (ins1(self, where, v) != 0)
1106 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001107 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001108}
1109
Brett Cannon1eb32c22014-10-10 16:26:45 -04001110/*[clinic input]
1111array.array.count
1112
1113 v: object
1114 /
1115
1116Return number of occurrences of v in the array.
1117[clinic start generated code]*/
1118
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001119static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001120array_array_count(arrayobject *self, PyObject *v)
1121/*[clinic end generated code: output=3dd3624bf7135a3a input=d9bce9d65e39d1f5]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001122{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 Py_ssize_t count = 0;
1124 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 for (i = 0; i < Py_SIZE(self); i++) {
Victor Stinner0b142e22013-07-17 23:01:30 +02001127 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 Pitrouf95a1b32010-05-09 15:52:27 +00001134 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-Kamp5a65c2d2000-07-31 20:52:21 +00001141}
1142
Brett Cannon1eb32c22014-10-10 16:26:45 -04001143
1144/*[clinic input]
1145array.array.index
1146
1147 v: object
Zackery Spytzafd12652021-04-02 09:28:35 -06001148 start: slice_index(accept={int}) = 0
1149 stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
Brett Cannon1eb32c22014-10-10 16:26:45 -04001150 /
1151
1152Return index of first occurrence of v in the array.
Zackery Spytzafd12652021-04-02 09:28:35 -06001153
1154Raise ValueError if the value is not present.
Brett Cannon1eb32c22014-10-10 16:26:45 -04001155[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001156
1157static PyObject *
Zackery Spytzafd12652021-04-02 09:28:35 -06001158array_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-Kamp5a65c2d2000-07-31 20:52:21 +00001161{
Zackery Spytzafd12652021-04-02 09:28:35 -06001162 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 Stinner0b142e22013-07-17 23:01:30 +02001174 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 Pitrouf95a1b32010-05-09 15:52:27 +00001181 Py_DECREF(selfi);
1182 if (cmp > 0) {
WildCard651d3dad52020-06-23 09:21:16 -04001183 return PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 }
1185 else if (cmp < 0)
1186 return NULL;
1187 }
Jim Fasarakis-Hilliarda4095ef2017-05-29 20:43:39 +03001188 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in array");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 return NULL;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001190}
1191
Raymond Hettinger625812f2003-01-07 01:58:52 +00001192static int
1193array_contains(arrayobject *self, PyObject *v)
1194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 Py_ssize_t i;
1196 int cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +00001197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) {
1199 PyObject *selfi = getarrayitem((PyObject *)self, i);
Victor Stinner0b142e22013-07-17 23:01:30 +02001200 if (selfi == NULL)
Victor Stinner4755bea2013-07-18 01:12:35 +02001201 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
1203 Py_DECREF(selfi);
1204 }
1205 return cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +00001206}
1207
Brett Cannon1eb32c22014-10-10 16:26:45 -04001208/*[clinic input]
1209array.array.remove
1210
1211 v: object
1212 /
1213
1214Remove the first occurrence of v in the array.
1215[clinic start generated code]*/
1216
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001217static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001218array_array_remove(arrayobject *self, PyObject *v)
1219/*[clinic end generated code: output=bef06be9fdf9dceb input=0b1e5aed25590027]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001220{
sthaa3ecb82019-03-20 20:49:39 +01001221 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 for (i = 0; i < Py_SIZE(self); i++) {
Victor Stinner0b142e22013-07-17 23:01:30 +02001224 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 Pitrouf95a1b32010-05-09 15:52:27 +00001231 Py_DECREF(selfi);
1232 if (cmp > 0) {
Martin Panter996d72b2016-07-25 02:21:14 +00001233 if (array_del_slice(self, i, i+1) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001235 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 }
1237 else if (cmp < 0)
1238 return NULL;
1239 }
Jim Fasarakis-Hilliarda4095ef2017-05-29 20:43:39 +03001240 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in array");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 return NULL;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001242}
1243
Brett Cannon1eb32c22014-10-10 16:26:45 -04001244/*[clinic input]
1245array.array.pop
1246
1247 i: Py_ssize_t = -1
1248 /
1249
1250Return the i-th element and delete it from the array.
1251
1252i defaults to -1.
1253[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001254
1255static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001256array_array_pop_impl(arrayobject *self, Py_ssize_t i)
1257/*[clinic end generated code: output=bc1f0c54fe5308e4 input=8e5feb4c1a11cd44]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001258{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 PyObject *v;
Brett Cannon1eb32c22014-10-10 16:26:45 -04001260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 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 Stinner0b142e22013-07-17 23:01:30 +02001272 v = getarrayitem((PyObject *)self, i);
1273 if (v == NULL)
1274 return NULL;
Martin Panter996d72b2016-07-25 02:21:14 +00001275 if (array_del_slice(self, i, i+1) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 Py_DECREF(v);
1277 return NULL;
1278 }
1279 return v;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001280}
1281
Brett Cannon1eb32c22014-10-10 16:26:45 -04001282/*[clinic input]
1283array.array.extend
1284
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01001285 cls: defining_class
Brett Cannon1eb32c22014-10-10 16:26:45 -04001286 bb: object
1287 /
1288
1289Append items to the end of the array.
1290[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001291
1292static PyObject *
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01001293array_array_extend_impl(arrayobject *self, PyTypeObject *cls, PyObject *bb)
1294/*[clinic end generated code: output=e65eb7588f0bc266 input=8eb6817ec4d2cb62]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001295{
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01001296 array_state *state = get_array_state_by_class(cls);
1297
1298 if (array_do_extend(state, self, bb) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001300 Py_RETURN_NONE;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001301}
1302
Brett Cannon1eb32c22014-10-10 16:26:45 -04001303/*[clinic input]
1304array.array.insert
1305
1306 i: Py_ssize_t
1307 v: object
1308 /
1309
1310Insert a new item v into the array before position i.
1311[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001312
1313static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001314array_array_insert_impl(arrayobject *self, Py_ssize_t i, PyObject *v)
1315/*[clinic end generated code: output=5a3648e278348564 input=5577d1b4383e9313]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 return ins(self, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001318}
1319
Brett Cannon1eb32c22014-10-10 16:26:45 -04001320/*[clinic input]
1321array.array.buffer_info
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001322
Brett Cannon1eb32c22014-10-10 16:26:45 -04001323Return a tuple (address, length) giving the current memory address and the length in items of the buffer used to hold array's contents.
1324
1325The length should be multiplied by the itemsize attribute to calculate
1326the buffer length in bytes.
1327[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001328
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001329static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001330array_array_buffer_info_impl(arrayobject *self)
1331/*[clinic end generated code: output=9b2a4ec3ae7e98e7 input=a58bae5c6e1ac6a6]*/
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001332{
Victor Stinner541067a2013-11-14 01:27:12 +01001333 PyObject *retval = NULL, *v;
1334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 retval = PyTuple_New(2);
1336 if (!retval)
1337 return NULL;
Fred Drake541dc3b2000-06-28 17:49:30 +00001338
Victor Stinner541067a2013-11-14 01:27:12 +01001339 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 Storchaka9e941d62016-06-23 23:55:34 +03001346 v = PyLong_FromSsize_t(Py_SIZE(self));
Victor Stinner541067a2013-11-14 01:27:12 +01001347 if (v == NULL) {
1348 Py_DECREF(retval);
1349 return NULL;
1350 }
1351 PyTuple_SET_ITEM(retval, 1, v);
Fred Drake541dc3b2000-06-28 17:49:30 +00001352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 return retval;
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001354}
1355
Brett Cannon1eb32c22014-10-10 16:26:45 -04001356/*[clinic input]
1357array.array.append
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001358
Brett Cannon1eb32c22014-10-10 16:26:45 -04001359 v: object
1360 /
1361
1362Append new value v to the end of the array.
1363[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001364
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001365static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001366array_array_append(arrayobject *self, PyObject *v)
1367/*[clinic end generated code: output=745a0669bf8db0e2 input=0b98d9d78e78f0fa]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001368{
Mark Dickinson346f0af2010-08-06 09:36:57 +00001369 return ins(self, Py_SIZE(self), v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001370}
1371
Brett Cannon1eb32c22014-10-10 16:26:45 -04001372/*[clinic input]
1373array.array.byteswap
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001374
Brett Cannon1eb32c22014-10-10 16:26:45 -04001375Byteswap all items of the array.
1376
1377If the items in the array are not 1, 2, 4, or 8 bytes in size, RuntimeError is
1378raised.
1379[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001380
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001381static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001382array_array_byteswap_impl(arrayobject *self)
1383/*[clinic end generated code: output=5f8236cbdf0d90b5 input=6a85591b950a0186]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 char *p;
1386 Py_ssize_t i;
Fred Drakebf272981999-12-03 17:15:30 +00001387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 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 Storchaka228b12e2017-01-23 09:47:21 +02001429 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001430}
1431
Brett Cannon1eb32c22014-10-10 16:26:45 -04001432/*[clinic input]
1433array.array.reverse
1434
1435Reverse the order of the items in the array.
1436[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001437
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001438static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001439array_array_reverse_impl(arrayobject *self)
1440/*[clinic end generated code: output=c04868b36f6f4089 input=cd904f01b27d966a]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001441{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001442 Py_ssize_t itemsize = self->ob_descr->itemsize;
1443 char *p, *q;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 /* 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 Rossume77a7571993-11-03 15:01:26 +00001447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 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 Petersbb307342000-09-10 05:22:54 +00001461
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001462 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001463}
Guido van Rossume77a7571993-11-03 15:01:26 +00001464
Brett Cannon1eb32c22014-10-10 16:26:45 -04001465/*[clinic input]
1466array.array.fromfile
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001467
Brett Cannon1eb32c22014-10-10 16:26:45 -04001468 f: object
1469 n: Py_ssize_t
1470 /
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001471
Brett Cannon1eb32c22014-10-10 16:26:45 -04001472Read n objects from the file object f and append them to the end of the array.
1473[clinic start generated code]*/
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001474
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001475static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001476array_array_fromfile_impl(arrayobject *self, PyObject *f, Py_ssize_t n)
1477/*[clinic end generated code: output=ec9f600e10f53510 input=e188afe8e58adf40]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001478{
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03001479 PyObject *b, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 Py_ssize_t itemsize = self->ob_descr->itemsize;
Brett Cannon1eb32c22014-10-10 16:26:45 -04001481 Py_ssize_t nbytes;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001482 _Py_IDENTIFIER(read);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 int not_enough_bytes;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001484
Mark Dickinsonc04ddff2012-10-06 18:04:49 +01001485 if (n < 0) {
1486 PyErr_SetString(PyExc_ValueError, "negative count");
1487 return NULL;
1488 }
1489 if (n > PY_SSIZE_T_MAX / itemsize) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 PyErr_NoMemory();
1491 return NULL;
1492 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +01001493 nbytes = n * itemsize;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001494
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001495 b = _PyObject_CallMethodId(f, &PyId_read, "n", nbytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 if (b == NULL)
1497 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 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 Rossum2c94aa52007-05-24 19:02:32 +00001505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 not_enough_bytes = (PyBytes_GET_SIZE(b) != nbytes);
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001507
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03001508 res = array_array_frombytes(self, b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 Py_DECREF(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 if (res == NULL)
1511 return NULL;
Hirokazu Yamamoto54d0df62009-03-06 03:04:07 +00001512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 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 Rossum2c94aa52007-05-24 19:02:32 +00001519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +00001521}
1522
Brett Cannon1eb32c22014-10-10 16:26:45 -04001523/*[clinic input]
1524array.array.tofile
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001525
Brett Cannon1eb32c22014-10-10 16:26:45 -04001526 f: object
1527 /
1528
1529Write all items (as machine values) to the file object f.
1530[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001531
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001532static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001533array_array_tofile(arrayobject *self, PyObject *f)
1534/*[clinic end generated code: output=3a2cfa8128df0777 input=b0669a484aab0831]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 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 Hettinger36cd2bf2003-01-03 08:24:58 +00001542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 if (Py_SIZE(self) == 0)
1544 goto done;
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 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öwisbd928fe2011-10-14 10:20:37 +02001550 _Py_IDENTIFIER(write);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 if (i*BLOCKSIZE + size > nbytes)
1553 size = nbytes - i*BLOCKSIZE;
1554 bytes = PyBytes_FromStringAndSize(ptr, size);
1555 if (bytes == NULL)
1556 return NULL;
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001557 res = _PyObject_CallMethodIdOneArg(f, &PyId_write, bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 Py_DECREF(bytes);
1559 if (res == NULL)
1560 return NULL;
1561 Py_DECREF(res); /* drop write result */
1562 }
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001563
1564 done:
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001565 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001566}
1567
Brett Cannon1eb32c22014-10-10 16:26:45 -04001568/*[clinic input]
1569array.array.fromlist
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001570
Brett Cannon1eb32c22014-10-10 16:26:45 -04001571 list: object
1572 /
1573
1574Append items to array from list.
1575[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001576
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001577static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001578array_array_fromlist(arrayobject *self, PyObject *list)
1579/*[clinic end generated code: output=26411c2d228a3e3f input=be2605a96c49680f]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 Py_ssize_t n;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 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 Spytz99d56b52018-12-08 07:16:55 -07001594 PyObject *v = PyList_GET_ITEM(list, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 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 Spytz99d56b52018-12-08 07:16:55 -07001600 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 Pitrouf95a1b32010-05-09 15:52:27 +00001606 }
1607 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001608 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001609}
1610
Brett Cannon1eb32c22014-10-10 16:26:45 -04001611/*[clinic input]
1612array.array.tolist
1613
1614Convert array to an ordinary list with the same items.
1615[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001616
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001617static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001618array_array_tolist_impl(arrayobject *self)
1619/*[clinic end generated code: output=00b60cc9eab8ef89 input=a8d7784a94f86b53]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001620{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 PyObject *list = PyList_New(Py_SIZE(self));
1622 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 if (list == NULL)
1625 return NULL;
1626 for (i = 0; i < Py_SIZE(self); i++) {
1627 PyObject *v = getarrayitem((PyObject *)self, i);
Victor Stinner4755bea2013-07-18 01:12:35 +02001628 if (v == NULL)
1629 goto error;
Zackery Spytz99d56b52018-12-08 07:16:55 -07001630 PyList_SET_ITEM(list, i, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 }
1632 return list;
Victor Stinner4755bea2013-07-18 01:12:35 +02001633
1634error:
1635 Py_DECREF(list);
1636 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001637}
1638
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001639static PyObject *
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001640frombytes(arrayobject *self, Py_buffer *buffer)
Guido van Rossum778983b1993-02-19 15:55:02 +00001641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 int itemsize = self->ob_descr->itemsize;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001643 Py_ssize_t n;
1644 if (buffer->itemsize != 1) {
1645 PyBuffer_Release(buffer);
Serhiy Storchakab757c832014-12-05 22:25:22 +02001646 PyErr_SetString(PyExc_TypeError, "a bytes-like object is required");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 return NULL;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001648 }
1649 n = buffer->len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 if (n % itemsize != 0) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001651 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 PyErr_SetString(PyExc_ValueError,
Serhiy Storchakab757c832014-12-05 22:25:22 +02001653 "bytes length not a multiple of item size");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 return NULL;
1655 }
1656 n = n / itemsize;
1657 if (n > 0) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001658 Py_ssize_t old_size = Py_SIZE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 if ((n > PY_SSIZE_T_MAX - old_size) ||
1660 ((old_size + n) > PY_SSIZE_T_MAX / itemsize)) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001661 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 return PyErr_NoMemory();
1663 }
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001664 if (array_resize(self, old_size + n) == -1) {
1665 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 return NULL;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001667 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 memcpy(self->ob_item + old_size * itemsize,
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001669 buffer->buf, n * itemsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 }
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001671 PyBuffer_Release(buffer);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001672 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001673}
1674
Brett Cannon1eb32c22014-10-10 16:26:45 -04001675/*[clinic input]
Brett Cannon1eb32c22014-10-10 16:26:45 -04001676array.array.frombytes
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001677
Brett Cannon1eb32c22014-10-10 16:26:45 -04001678 buffer: Py_buffer
1679 /
1680
Łukasz Langa8c1e1da2021-09-22 01:33:59 +02001681Appends 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.
Brett Cannon1eb32c22014-10-10 16:26:45 -04001682[clinic start generated code]*/
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001683
1684static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001685array_array_frombytes_impl(arrayobject *self, Py_buffer *buffer)
Łukasz Langa8c1e1da2021-09-22 01:33:59 +02001686/*[clinic end generated code: output=d9842c8f7510a516 input=378db226dfac949e]*/
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001687{
Brett Cannon1eb32c22014-10-10 16:26:45 -04001688 return frombytes(self, buffer);
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001689}
1690
Brett Cannon1eb32c22014-10-10 16:26:45 -04001691/*[clinic input]
1692array.array.tobytes
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001693
Brett Cannon1eb32c22014-10-10 16:26:45 -04001694Convert the array to an array of machine values and return the bytes representation.
1695[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001696
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001697static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001698array_array_tobytes_impl(arrayobject *self)
1699/*[clinic end generated code: output=87318e4edcdc2bb6 input=90ee495f96de34f5]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 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 Rossum778983b1993-02-19 15:55:02 +00001707}
1708
Brett Cannon1eb32c22014-10-10 16:26:45 -04001709/*[clinic input]
Brett Cannon1eb32c22014-10-10 16:26:45 -04001710array.array.fromunicode
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001711
Inada Naokid5d9a712020-05-11 15:37:25 +09001712 ustr: unicode
Brett Cannon1eb32c22014-10-10 16:26:45 -04001713 /
1714
1715Extends this array with data from the unicode string ustr.
1716
1717The array must be a unicode type array; otherwise a ValueError is raised.
1718Use array.frombytes(ustr.encode(...)) to append Unicode data to an array of
1719some other type.
1720[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001721
Martin v. Löwis99866332002-03-01 10:27:01 +00001722static PyObject *
Inada Naokid5d9a712020-05-11 15:37:25 +09001723array_array_fromunicode_impl(arrayobject *self, PyObject *ustr)
1724/*[clinic end generated code: output=24359f5e001a7f2b input=025db1fdade7a4ce]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001725{
Inada Naokid5d9a712020-05-11 15:37:25 +09001726 if (self->ob_descr->typecode != 'u') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 PyErr_SetString(PyExc_ValueError,
1728 "fromunicode() may only be called on "
1729 "unicode type arrays");
1730 return NULL;
1731 }
Inada Naokid5d9a712020-05-11 15:37:25 +09001732
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 Pitrouf95a1b32010-05-09 15:52:27 +00001737 Py_ssize_t old_size = Py_SIZE(self);
Inada Naokid5d9a712020-05-11 15:37:25 +09001738 if (array_resize(self, old_size + ustr_length) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 return NULL;
Inada Naokid5d9a712020-05-11 15:37:25 +09001740 }
1741
1742 // must not fail
1743 PyUnicode_AsWideChar(
1744 ustr, ((wchar_t *)self->ob_item) + old_size, ustr_length);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 }
Martin v. Löwis99866332002-03-01 10:27:01 +00001746
Brett Cannon1eb32c22014-10-10 16:26:45 -04001747 Py_RETURN_NONE;
Martin v. Löwis99866332002-03-01 10:27:01 +00001748}
1749
Brett Cannon1eb32c22014-10-10 16:26:45 -04001750/*[clinic input]
1751array.array.tounicode
Martin v. Löwis99866332002-03-01 10:27:01 +00001752
Brett Cannon1eb32c22014-10-10 16:26:45 -04001753Extends this array with data from the unicode string ustr.
1754
1755Convert the array to a unicode string. The array must be a unicode type array;
1756otherwise a ValueError is raised. Use array.tobytes().decode() to obtain a
1757unicode string from an array of some other type.
1758[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001759
1760static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001761array_array_tounicode_impl(arrayobject *self)
1762/*[clinic end generated code: output=08e442378336e1ef input=127242eebe70b66d]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001763{
Inada Naokid5d9a712020-05-11 15:37:25 +09001764 if (self->ob_descr->typecode != 'u') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 PyErr_SetString(PyExc_ValueError,
1766 "tounicode() may only be called on unicode type arrays");
1767 return NULL;
1768 }
Inada Naokid5d9a712020-05-11 15:37:25 +09001769 return PyUnicode_FromWideChar((wchar_t *) self->ob_item, Py_SIZE(self));
Martin v. Löwis99866332002-03-01 10:27:01 +00001770}
1771
Brett Cannon1eb32c22014-10-10 16:26:45 -04001772/*[clinic input]
1773array.array.__sizeof__
Martin v. Löwis99866332002-03-01 10:27:01 +00001774
Brett Cannon1eb32c22014-10-10 16:26:45 -04001775Size of the array in memory, in bytes.
1776[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001777
Meador Inge03b4d502012-08-10 22:35:45 -05001778static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001779array_array___sizeof___impl(arrayobject *self)
1780/*[clinic end generated code: output=d8e1c61ebbe3eaed input=805586565bf2b3c6]*/
Meador Inge03b4d502012-08-10 22:35:45 -05001781{
1782 Py_ssize_t res;
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02001783 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * self->ob_descr->itemsize;
Meador Inge03b4d502012-08-10 22:35:45 -05001784 return PyLong_FromSsize_t(res);
1785}
1786
Martin v. Löwis99866332002-03-01 10:27:01 +00001787
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001788/*********************** Pickling support ************************/
1789
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001790static const struct mformatdescr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 size_t size;
1792 int is_signed;
1793 int is_big_endian;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001794} mformat_descriptors[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 {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 Vassalottiad077152009-07-15 17:49:23 +00001817};
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 */
1825static enum machine_format_code
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001826typecode_to_mformat_code(char typecode)
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001827{
Christian Heimes743e0cd2012-10-17 23:52:17 +02001828 const int is_big_endian = PY_BIG_ENDIAN;
1829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 size_t intsize;
1831 int is_signed;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 switch (typecode) {
1834 case 'b':
1835 return SIGNED_INT8;
1836 case 'B':
1837 return UNSIGNED_INT8;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 case 'u':
Victor Stinner62bb3942012-08-06 00:46:05 +02001840 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 Vassalottiad077152009-07-15 17:49:23 +00001847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 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 Vassalottiad077152009-07-15 17:49:23 +00001857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 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 Vassalottiad077152009-07-15 17:49:23 +00001867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 /* 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 Inge1c9f0c92011-09-20 19:55:51 -05001893 case 'q':
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001894 intsize = sizeof(long long);
Meador Inge1c9f0c92011-09-20 19:55:51 -05001895 is_signed = 1;
1896 break;
1897 case 'Q':
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001898 intsize = sizeof(long long);
Meador Inge1c9f0c92011-09-20 19:55:51 -05001899 is_signed = 0;
1900 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 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 Vassalottiad077152009-07-15 17:49:23 +00001914}
1915
1916/* Forward declaration. */
1917static 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 Pitrouf95a1b32010-05-09 15:52:27 +00001926 *
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001927 * On success, this functions returns the array object created. Otherwise,
1928 * NULL is returned to indicate a failure.
1929 */
1930static PyObject *
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001931make_array(PyTypeObject *arraytype, char typecode, PyObject *items)
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001932{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 PyObject *new_args;
1934 PyObject *array_obj;
1935 PyObject *typecode_obj;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 assert(arraytype != NULL);
1938 assert(items != NULL);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001939
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001940 typecode_obj = PyUnicode_FromOrdinal(typecode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 if (typecode_obj == NULL)
1942 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 new_args = PyTuple_New(2);
Mat M56935a52017-11-14 01:00:54 -05001945 if (new_args == NULL) {
1946 Py_DECREF(typecode_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 return NULL;
Mat M56935a52017-11-14 01:00:54 -05001948 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 Py_INCREF(items);
1950 PyTuple_SET_ITEM(new_args, 0, typecode_obj);
1951 PyTuple_SET_ITEM(new_args, 1, items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 array_obj = array_new(arraytype, new_args, NULL);
1954 Py_DECREF(new_args);
1955 if (array_obj == NULL)
1956 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 return array_obj;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001959}
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 Cannon1eb32c22014-10-10 16:26:45 -04001965/*[clinic input]
1966array._array_reconstructor
1967
1968 arraytype: object(type="PyTypeObject *")
Larry Hastingsdbfdc382015-05-04 06:59:46 -07001969 typecode: int(accept={str})
Brett Cannon1eb32c22014-10-10 16:26:45 -04001970 mformat_code: int(type="enum machine_format_code")
1971 items: object
1972 /
1973
1974Internal. Used for pickling support.
1975[clinic start generated code]*/
1976
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001977static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001978array__array_reconstructor_impl(PyObject *module, PyTypeObject *arraytype,
Larry Hastings89964c42015-04-14 18:07:59 -04001979 int typecode,
1980 enum machine_format_code mformat_code,
1981 PyObject *items)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001982/*[clinic end generated code: output=e05263141ba28365 input=2464dc8f4c7736b5]*/
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001983{
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01001984 array_state *state = get_array_state(module);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 PyObject *converted_items;
1986 PyObject *result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001987 const struct arraydescr *descr;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 if (!PyType_Check(arraytype)) {
1990 PyErr_Format(PyExc_TypeError,
Sylvaina90e64b2017-03-29 20:09:22 +02001991 "first argument must be a type object, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 Py_TYPE(arraytype)->tp_name);
1993 return NULL;
1994 }
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01001995 if (!PyType_IsSubtype(arraytype, state->ArrayType)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 PyErr_Format(PyExc_TypeError,
1997 "%.200s is not a subtype of %.200s",
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01001998 arraytype->tp_name, state->ArrayType->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 return NULL;
2000 }
2001 for (descr = descriptors; descr->typecode != '\0'; descr++) {
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002002 if ((int)descr->typecode == typecode)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 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 Hastingsdfbeb162014-10-13 10:39:41 +01002010 if (mformat_code < MACHINE_FORMAT_CODE_MIN ||
2011 mformat_code > MACHINE_FORMAT_CODE_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 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 Vassalottiad077152009-07-15 17:49:23 +00002022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 /* Fast path: No decoding has to be done. */
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002024 if (mformat_code == typecode_to_mformat_code((char)typecode) ||
2025 mformat_code == UNKNOWN_FORMAT) {
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002026 return make_array(arraytype, (char)typecode, items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 /* 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 Hastingsdfbeb162014-10-13 10:39:41 +01002034 if (Py_SIZE(items) % mformat_descriptors[mformat_code].size != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 PyErr_SetString(PyExc_ValueError,
2036 "string length not a multiple of item size");
2037 return NULL;
2038 }
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002039 switch (mformat_code) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002040 case IEEE_754_FLOAT_LE:
2041 case IEEE_754_FLOAT_BE: {
sthaa3ecb82019-03-20 20:49:39 +01002042 Py_ssize_t i;
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002043 int le = (mformat_code == IEEE_754_FLOAT_LE) ? 1 : 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 Py_ssize_t itemcount = Py_SIZE(items) / 4;
2045 const unsigned char *memstr =
2046 (unsigned char *)PyBytes_AS_STRING(items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 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: {
sthaa3ecb82019-03-20 20:49:39 +01002064 Py_ssize_t i;
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002065 int le = (mformat_code == IEEE_754_DOUBLE_LE) ? 1 : 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 Py_ssize_t itemcount = Py_SIZE(items) / 8;
2067 const unsigned char *memstr =
2068 (unsigned char *)PyBytes_AS_STRING(items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070 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 Hastingsdfbeb162014-10-13 10:39:41 +01002086 int byteorder = (mformat_code == UTF16_LE) ? -1 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 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 Hastingsdfbeb162014-10-13 10:39:41 +01002096 int byteorder = (mformat_code == UTF32_LE) ? -1 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 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 Vassalottiad077152009-07-15 17:49:23 +00002104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 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: {
sthaa3ecb82019-03-20 20:49:39 +01002119 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002120 const struct mformatdescr mf_descr =
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002121 mformat_descriptors[mformat_code];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 Py_ssize_t itemcount = Py_SIZE(items) / mf_descr.size;
2123 const unsigned char *memstr =
2124 (unsigned char *)PyBytes_AS_STRING(items);
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002125 const struct arraydescr *descr;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 /* 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 Panter4c359642016-05-08 13:53:41 +00002131 * For example, if a 32-bit machine pickles an L-code array of
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132 * 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 Stinner706768c2014-08-16 01:03:39 +02002139 (size_t)descr->itemsize == mf_descr.size &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 descr->is_signed == mf_descr.is_signed)
2141 typecode = descr->typecode;
2142 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 converted_items = PyList_New(itemcount);
2145 if (converted_items == NULL)
2146 return NULL;
2147 for (i = 0; i < itemcount; i++) {
2148 PyObject *pylong;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 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 Vassalottiad077152009-07-15 17:49:23 +00002171
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002172 result = make_array(arraytype, (char)typecode, converted_items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 Py_DECREF(converted_items);
2174 return result;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002175}
2176
Brett Cannon1eb32c22014-10-10 16:26:45 -04002177/*[clinic input]
2178array.array.__reduce_ex__
2179
2180 value: object
2181 /
2182
2183Return state information for pickling.
2184[clinic start generated code]*/
2185
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002186static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04002187array_array___reduce_ex__(arrayobject *self, PyObject *value)
2188/*[clinic end generated code: output=051e0a6175d0eddb input=c36c3f85de7df6cd]*/
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002189{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 PyObject *dict;
2191 PyObject *result;
2192 PyObject *array_str;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002193 int typecode = self->ob_descr->typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 int mformat_code;
2195 static PyObject *array_reconstructor = NULL;
2196 long protocol;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002197 _Py_IDENTIFIER(_array_reconstructor);
2198 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 if (array_reconstructor == NULL) {
2201 PyObject *array_module = PyImport_ImportModule("array");
2202 if (array_module == NULL)
2203 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002204 array_reconstructor = _PyObject_GetAttrId(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 array_module,
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002206 &PyId__array_reconstructor);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002207 Py_DECREF(array_module);
2208 if (array_reconstructor == NULL)
2209 return NULL;
2210 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 if (!PyLong_Check(value)) {
2213 PyErr_SetString(PyExc_TypeError,
Sylvaina90e64b2017-03-29 20:09:22 +02002214 "__reduce_ex__ argument should be an integer");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215 return NULL;
2216 }
2217 protocol = PyLong_AsLong(value);
2218 if (protocol == -1 && PyErr_Occurred())
2219 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002220
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002221 if (_PyObject_LookupAttrId((PyObject *)self, &PyId___dict__, &dict) < 0) {
2222 return NULL;
2223 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 dict = Py_None;
2226 Py_INCREF(dict);
2227 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 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 Cannon1eb32c22014-10-10 16:26:45 -04002243 list = array_array_tolist_impl(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 if (list == NULL) {
2245 Py_DECREF(dict);
2246 return NULL;
2247 }
2248 result = Py_BuildValue(
Brett Cannon1eb32c22014-10-10 16:26:45 -04002249 "O(CO)O", Py_TYPE(self), typecode, list, dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 Py_DECREF(list);
2251 Py_DECREF(dict);
2252 return result;
2253 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002254
Brett Cannon1eb32c22014-10-10 16:26:45 -04002255 array_str = array_array_tobytes_impl(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 if (array_str == NULL) {
2257 Py_DECREF(dict);
2258 return NULL;
2259 }
2260 result = Py_BuildValue(
Brett Cannon1eb32c22014-10-10 16:26:45 -04002261 "O(OCiN)O", array_reconstructor, Py_TYPE(self), typecode,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 mformat_code, array_str, dict);
2263 Py_DECREF(dict);
2264 return result;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002265}
2266
Martin v. Löwis99866332002-03-01 10:27:01 +00002267static PyObject *
2268array_get_typecode(arrayobject *a, void *closure)
2269{
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002270 char typecode = a->ob_descr->typecode;
2271 return PyUnicode_FromOrdinal(typecode);
Martin v. Löwis99866332002-03-01 10:27:01 +00002272}
2273
2274static PyObject *
2275array_get_itemsize(arrayobject *a, void *closure)
2276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 return PyLong_FromLong((long)a->ob_descr->itemsize);
Martin v. Löwis99866332002-03-01 10:27:01 +00002278}
2279
2280static PyGetSetDef array_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 {"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öwis99866332002-03-01 10:27:01 +00002286};
2287
Martin v. Löwis59683e82008-06-13 07:50:45 +00002288static PyMethodDef array_methods[] = {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002289 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 Cannon1eb32c22014-10-10 16:26:45 -04002298 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 Cannon1eb32c22014-10-10 16:26:45 -04002308 ARRAY_ARRAY_TOBYTES_METHODDEF
2309 ARRAY_ARRAY_TOUNICODE_METHODDEF
2310 ARRAY_ARRAY___SIZEOF___METHODDEF
2311 {NULL, NULL} /* sentinel */
Guido van Rossum778983b1993-02-19 15:55:02 +00002312};
2313
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002314static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00002315array_repr(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +00002316{
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002317 char typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 PyObject *s, *v = NULL;
2319 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00002320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 len = Py_SIZE(a);
2322 typecode = a->ob_descr->typecode;
2323 if (len == 0) {
Serhiy Storchakab3a77962017-09-21 14:24:13 +03002324 return PyUnicode_FromFormat("%s('%c')",
2325 _PyType_Name(Py_TYPE(a)), (int)typecode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 }
Gregory P. Smith9504b132012-12-10 20:20:20 -08002327 if (typecode == 'u') {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002328 v = array_array_tounicode_impl(a);
Gregory P. Smith9504b132012-12-10 20:20:20 -08002329 } else {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002330 v = array_array_tolist_impl(a);
Gregory P. Smith9504b132012-12-10 20:20:20 -08002331 }
Victor Stinner29ec5952013-02-26 00:27:38 +01002332 if (v == NULL)
2333 return NULL;
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00002334
Serhiy Storchakab3a77962017-09-21 14:24:13 +03002335 s = PyUnicode_FromFormat("%s('%c', %R)",
2336 _PyType_Name(Py_TYPE(a)), (int)typecode, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 Py_DECREF(v);
2338 return s;
Guido van Rossum778983b1993-02-19 15:55:02 +00002339}
2340
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002341static PyObject*
2342array_subscr(arrayobject* self, PyObject* item)
2343{
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002344 array_state *state = find_array_state_by_type(Py_TYPE(self));
2345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 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 Spytz14514d92019-05-17 01:13:03 -06002356 Py_ssize_t start, stop, step, slicelength, i;
2357 size_t cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 PyObject* result;
2359 arrayobject* ar;
2360 int itemsize = self->ob_descr->itemsize;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002361
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002362 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 return NULL;
2364 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002365 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2366 step);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 if (slicelength <= 0) {
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002369 return newarrayobject(state->ArrayType, 0, self->ob_descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 }
2371 else if (step == 1) {
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002372 PyObject *result = newarrayobject(state->ArrayType,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 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 Aasland75bf1072021-01-02 17:38:47 +01002382 result = newarrayobject(state->ArrayType, slicelength, self->ob_descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 if (!result) return NULL;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 ar = (arrayobject*)result;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 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. Hudson9c14bad2002-06-19 15:44:15 +00002402}
2403
2404static int
2405array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
2406{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 Py_ssize_t start, stop, step, slicelength, needed;
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002408 array_state* state = find_array_state_by_type(Py_TYPE(self));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 arrayobject* other;
2410 int itemsize;
Thomas Woutersed03b412007-08-28 21:37:11 +00002411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 if (PyIndex_Check(item)) {
2413 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Alexandre Vassalotti47137252009-07-05 19:57:00 +00002414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 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 Storchakab879fe82017-04-08 09:53:51 +03002435 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 return -1;
2437 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002438 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2439 step);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 }
2441 else {
2442 PyErr_SetString(PyExc_TypeError,
Sylvaina90e64b2017-03-29 20:09:22 +02002443 "array indices must be integers");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 return -1;
2445 }
2446 if (value == NULL) {
2447 other = NULL;
2448 needed = 0;
2449 }
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002450 else if (array_Check(value, state)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 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 Vassalotti47137252009-07-05 19:57:00 +00002479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 /* 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 Dickinsonbc099642010-01-29 17:27:24 +00002488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002489 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 Woutersed03b412007-08-28 21:37:11 +00002515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 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 Woutersed03b412007-08-28 21:37:11 +00002524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002525 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 Dickinsonc7d93b72011-09-25 15:34:32 +01002531 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002532 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 Spytz14514d92019-05-17 01:13:03 -06002542 size_t cur;
2543 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002544
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. Hudson9c14bad2002-06-19 15:44:15 +00002560}
2561
Guido van Rossumd8faa362007-04-27 19:54:29 +00002562static const void *emptybuf = "";
2563
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002564
2565static int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00002566array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002567{
Stefan Krah650c1e82015-02-03 21:43:23 +01002568 if (view == NULL) {
2569 PyErr_SetString(PyExc_BufferError,
2570 "array_buffer_getbuf: view==NULL argument is obsolete");
2571 return -1;
2572 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002574 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 Stinnerfe2978b2020-05-27 14:55:10 +02002579 view->len = Py_SIZE(self) * self->ob_descr->itemsize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 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 Stinnerfe2978b2020-05-27 14:55:10 +02002586 view->shape = &((PyVarObject*)self)->ob_size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002587 }
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 Stinner62bb3942012-08-06 00:46:05 +02002593 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002594 view->format = (char *)self->ob_descr->formats;
Victor Stinner62bb3942012-08-06 00:46:05 +02002595#ifdef Py_UNICODE_WIDE
2596 if (self->ob_descr->typecode == 'u') {
2597 view->format = "w";
2598 }
2599#endif
2600 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002602 self->ob_exports++;
2603 return 0;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002604}
2605
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002606static void
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00002607array_buffer_relbuf(arrayobject *self, Py_buffer *view)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002608{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002609 self->ob_exports--;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002610}
2611
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002612static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +00002613array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum778983b1993-02-19 15:55:02 +00002614{
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002615 array_state *state = find_array_state_by_type(type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002616 int c;
2617 PyObject *initial = NULL, *it = NULL;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002618 const struct arraydescr *descr;
Martin v. Löwis99866332002-03-01 10:27:01 +00002619
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002620 if (type == state->ArrayType && !_PyArg_NoKeywords("array.array", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00002622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002623 if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
2624 return NULL;
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00002625
Steve Dowerb82e17e2019-05-23 08:45:22 -07002626 if (PySys_Audit("array.__new__", "CO",
2627 c, initial ? initial : Py_None) < 0) {
2628 return NULL;
2629 }
2630
Alexandre Vassalotti9730e332013-11-29 20:47:15 -08002631 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 Aasland75bf1072021-01-02 17:38:47 +01002637 else if (array_Check(initial, state) &&
Alexandre Vassalotti9730e332013-11-29 20:47:15 -08002638 ((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 Pitrouf95a1b32010-05-09 15:52:27 +00002645 if (!(initial == NULL || PyList_Check(initial)
2646 || PyByteArray_Check(initial)
2647 || PyBytes_Check(initial)
2648 || PyTuple_Check(initial)
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002649 || ((c=='u') && PyUnicode_Check(initial))
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002650 || (array_Check(initial, state)
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002651 && c == ((arrayobject*)initial)->ob_descr->typecode))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002652 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öwis99866332002-03-01 10:27:01 +00002666
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002667 if (initial == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 len = 0;
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002669 else if (PyList_Check(initial))
2670 len = PyList_GET_SIZE(initial);
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002671 else if (PyTuple_Check(initial) || array_Check(initial, state))
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002672 len = Py_SIZE(initial);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 else
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002674 len = 0;
Martin v. Löwis99866332002-03-01 10:27:01 +00002675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002676 a = newarrayobject(type, len, descr);
2677 if (a == NULL)
2678 return NULL;
2679
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002680 if (len > 0 && !array_Check(initial, state)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002681 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 Storchaka04e6dba2015-04-04 17:06:55 +03002699 PyObject *v;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002700 v = array_array_frombytes((arrayobject *)a,
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03002701 initial);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002702 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 Stinner1fbcaef2011-09-30 01:54:04 +02002709 Py_ssize_t n;
Inada Naokid5d9a712020-05-11 15:37:25 +09002710 wchar_t *ustr = PyUnicode_AsWideCharString(initial, &n);
Victor Stinner62bb3942012-08-06 00:46:05 +02002711 if (ustr == NULL) {
Victor Stinner1fbcaef2011-09-30 01:54:04 +02002712 Py_DECREF(a);
2713 return NULL;
2714 }
Victor Stinner62bb3942012-08-06 00:46:05 +02002715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 if (n > 0) {
2717 arrayobject *self = (arrayobject *)a;
Inada Naokid5d9a712020-05-11 15:37:25 +09002718 // 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 Pitrouf95a1b32010-05-09 15:52:27 +00002723 }
2724 }
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002725 else if (initial != NULL && array_Check(initial, state) && len > 0) {
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002726 arrayobject *self = (arrayobject *)a;
2727 arrayobject *other = (arrayobject *)initial;
2728 memcpy(self->ob_item, other->ob_item, len * other->ob_descr->itemsize);
2729 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002730 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 Inge1c9f0c92011-09-20 19:55:51 -05002742 "bad typecode (must be b, B, u, h, H, i, I, l, L, q, Q, f or d)");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00002744}
2745
Guido van Rossum778983b1993-02-19 15:55:02 +00002746
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002747PyDoc_STRVAR(module_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00002748"This module defines an object type which can efficiently represent\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002749an array of basic values: characters, integers, floating point\n\
2750numbers. Arrays are sequence types and behave very much like lists,\n\
Alexandre Vassalotti9730e332013-11-29 20:47:15 -08002751except that the type of objects stored in them is constrained.\n");
2752
2753PyDoc_STRVAR(arraytype_doc,
2754"array(typecode [, initializer]) -> array\n\
2755\n\
2756Return a new array whose items are restricted by typecode, and\n\
2757initialized from the optional initializer value, which must be a list,\n\
2758string or iterable over elements of the appropriate type.\n\
2759\n\
2760Arrays represent basic values and behave very much like lists, except\n\
2761the type of objects stored in them is constrained. The type is specified\n\
2762at object creation time by using a type code, which is a single character.\n\
2763The following type codes are defined:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002764\n\
oldkaa0735f2018-02-02 16:52:55 +08002765 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 Rossumb39b90d1998-10-13 14:27:22 +00002779\n\
oldkaa0735f2018-02-02 16:52:55 +08002780NOTE: The 'u' typecode corresponds to Python's unicode character. On\n\
Victor Stinner62bb3942012-08-06 00:46:05 +02002781narrow builds this is 2-bytes on wide builds this is 4-bytes.\n\
2782\n\
oldkaa0735f2018-02-02 16:52:55 +08002783NOTE: The 'q' and 'Q' type codes are only available if the platform\n\
2784C compiler used to build Python supports 'long long', or, on Windows,\n\
Meador Inge1c9f0c92011-09-20 19:55:51 -05002785'__int64'.\n\
2786\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002787Methods:\n\
2788\n\
2789append() -- append a new item to the end of the array\n\
2790buffer_info() -- return information giving the current memory info\n\
2791byteswap() -- byteswap all the items of the array\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002792count() -- return number of occurrences of an object\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00002793extend() -- extend array by appending multiple elements from an iterable\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002794fromfile() -- read items from a file object\n\
2795fromlist() -- append items from the list\n\
Florent Xiclunac45fb252011-10-24 13:14:55 +02002796frombytes() -- append items from the string\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002797index() -- return index of first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002798insert() -- insert a new item into the array at a provided position\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00002799pop() -- remove and return item (default last)\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002800remove() -- remove first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002801reverse() -- reverse the order of the items in the array\n\
2802tofile() -- write all items to a file object\n\
2803tolist() -- return the array converted to an ordinary list\n\
Florent Xiclunac45fb252011-10-24 13:14:55 +02002804tobytes() -- return the array converted to a string\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002805\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002806Attributes:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002807\n\
2808typecode -- the typecode character used to create the array\n\
2809itemsize -- the length in bytes of one array item\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002810");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002811
Raymond Hettinger625812f2003-01-07 01:58:52 +00002812static PyObject *array_iter(arrayobject *ao);
2813
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002814static struct PyMemberDef array_members[] = {
2815 {"__weaklistoffset__", T_PYSSIZET, offsetof(arrayobject, weakreflist), READONLY},
2816 {NULL},
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002817};
2818
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002819static 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)534da742021-05-25 11:49:19 -07002831 {Py_tp_traverse, array_tp_traverse},
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002832
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
2855static PyType_Spec array_spec = {
2856 .name = "array.array",
2857 .basicsize = sizeof(arrayobject),
Erlend Egeberg Aaslandc6ad03f2021-04-29 08:47:48 +02002858 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
Miss Islington (bot)534da742021-05-25 11:49:19 -07002859 Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC |
Mark Shannon069e81a2021-04-30 09:50:28 +01002860 Py_TPFLAGS_SEQUENCE),
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002861 .slots = array_slots,
2862};
Raymond Hettinger625812f2003-01-07 01:58:52 +00002863
2864/*********************** Array Iterator **************************/
2865
Brett Cannon1eb32c22014-10-10 16:26:45 -04002866/*[clinic input]
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002867class array.arrayiterator "arrayiterobject *" "find_array_state_by_type(type)->ArrayIterType"
Brett Cannon1eb32c22014-10-10 16:26:45 -04002868[clinic start generated code]*/
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002869/*[clinic end generated code: output=da39a3ee5e6b4b0d input=fb46d5ef98dd95ff]*/
Raymond Hettinger625812f2003-01-07 01:58:52 +00002870
2871static PyObject *
2872array_iter(arrayobject *ao)
2873{
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002874 array_state *state = find_array_state_by_type(Py_TYPE(ao));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 arrayiterobject *it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002876
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002877 if (!array_Check(ao, state)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 PyErr_BadInternalCall();
2879 return NULL;
2880 }
Raymond Hettinger625812f2003-01-07 01:58:52 +00002881
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002882 it = PyObject_GC_New(arrayiterobject, state->ArrayIterType);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 if (it == NULL)
2884 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 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 Hettinger625812f2003-01-07 01:58:52 +00002892}
2893
2894static PyObject *
Raymond Hettinger625812f2003-01-07 01:58:52 +00002895arrayiter_next(arrayiterobject *it)
2896{
Serhiy Storchakaab0d1982016-03-30 21:11:16 +03002897 arrayobject *ao;
2898
2899 assert(it != NULL);
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002900#ifndef NDEBUG
2901 array_state *state = find_array_state_by_type(Py_TYPE(it));
2902 assert(PyObject_TypeCheck(it, state->ArrayIterType));
2903#endif
Serhiy Storchakaab0d1982016-03-30 21:11:16 +03002904 ao = it->ao;
2905 if (ao == NULL) {
2906 return NULL;
2907 }
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002908#ifndef NDEBUG
2909 assert(array_Check(ao, state));
2910#endif
Serhiy Storchakaab0d1982016-03-30 21:11:16 +03002911 if (it->index < Py_SIZE(ao)) {
2912 return (*it->getitem)(ao, it->index++);
2913 }
2914 it->ao = NULL;
2915 Py_DECREF(ao);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002916 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002917}
2918
2919static void
2920arrayiter_dealloc(arrayiterobject *it)
2921{
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002922 PyTypeObject *tp = Py_TYPE(it);
2923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 PyObject_GC_UnTrack(it);
2925 Py_XDECREF(it->ao);
2926 PyObject_GC_Del(it);
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002927 Py_DECREF(tp);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002928}
2929
2930static int
2931arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2932{
Miss Islington (bot)534da742021-05-25 11:49:19 -07002933 Py_VISIT(Py_TYPE(it));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 Py_VISIT(it->ao);
2935 return 0;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002936}
2937
Brett Cannon1eb32c22014-10-10 16:26:45 -04002938/*[clinic input]
2939array.arrayiterator.__reduce__
2940
2941Return state information for pickling.
2942[clinic start generated code]*/
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002943
2944static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04002945array_arrayiterator___reduce___impl(arrayiterobject *self)
2946/*[clinic end generated code: output=7898a52e8e66e016 input=a062ea1e9951417a]*/
2947{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002948 _Py_IDENTIFIER(iter);
2949 PyObject *func = _PyEval_GetBuiltinId(&PyId_iter);
Serhiy Storchakaab0d1982016-03-30 21:11:16 +03002950 if (self->ao == NULL) {
2951 return Py_BuildValue("N(())", func);
2952 }
2953 return Py_BuildValue("N(O)n", func, self->ao, self->index);
Brett Cannon1eb32c22014-10-10 16:26:45 -04002954}
2955
2956/*[clinic input]
2957array.arrayiterator.__setstate__
2958
2959 state: object
2960 /
2961
2962Set state information for unpickling.
2963[clinic start generated code]*/
2964
2965static PyObject *
2966array_arrayiterator___setstate__(arrayiterobject *self, PyObject *state)
2967/*[clinic end generated code: output=397da9904e443cbe input=f47d5ceda19e787b]*/
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002968{
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 Cannon1eb32c22014-10-10 16:26:45 -04002974 else if (index > Py_SIZE(self->ao))
2975 index = Py_SIZE(self->ao); /* iterator exhausted */
2976 self->index = index;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002977 Py_RETURN_NONE;
2978}
2979
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002980static PyMethodDef arrayiter_methods[] = {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002981 ARRAY_ARRAYITERATOR___REDUCE___METHODDEF
2982 ARRAY_ARRAYITERATOR___SETSTATE___METHODDEF
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002983 {NULL, NULL} /* sentinel */
2984};
2985
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01002986static 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
2996static PyType_Spec arrayiter_spec = {
2997 .name = "array.arrayiterator",
2998 .basicsize = sizeof(arrayiterobject),
Erlend Egeberg Aasland9746cda2021-04-30 16:04:57 +02002999 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Miss Islington (bot)7297d742021-06-17 03:19:44 -07003000 Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_IMMUTABLETYPE),
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01003001 .slots = arrayiter_slots,
Raymond Hettinger625812f2003-01-07 01:58:52 +00003002};
3003
3004
3005/*********************** Install Module **************************/
3006
Erlend Egeberg Aaslandb8eb3762021-01-03 14:11:15 +01003007static int
3008array_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
3016static int
3017array_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
3025static void
3026array_free(void *module)
3027{
3028 array_clear((PyObject *)module);
3029}
3030
Martin v. Löwis99866332002-03-01 10:27:01 +00003031/* No functions in array module. */
3032static PyMethodDef a_methods[] = {
Brett Cannon1eb32c22014-10-10 16:26:45 -04003033 ARRAY__ARRAY_RECONSTRUCTOR_METHODDEF
Martin v. Löwis99866332002-03-01 10:27:01 +00003034 {NULL, NULL, 0, NULL} /* Sentinel */
3035};
3036
Erlend Egeberg Aaslandb8eb3762021-01-03 14:11:15 +01003037#define CREATE_TYPE(module, type, spec) \
3038do { \
3039 type = (PyTypeObject *)PyType_FromModuleAndSpec(module, spec, NULL); \
3040 if (type == NULL) { \
3041 return -1; \
3042 } \
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01003043} while (0)
3044
Nick Coghland5cacbb2015-05-23 22:24:10 +10003045static int
3046array_modexec(PyObject *m)
Guido van Rossum778983b1993-02-19 15:55:02 +00003047{
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01003048 array_state *state = get_array_state(m);
Georg Brandl4cb0de22011-09-28 21:49:49 +02003049 char buffer[Py_ARRAY_LENGTH(descriptors)], *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003050 PyObject *typecodes;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02003051 const struct arraydescr *descr;
Fred Drake0d40ba42000-02-04 20:33:49 +00003052
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01003053 CREATE_TYPE(m, state->ArrayType, &array_spec);
3054 CREATE_TYPE(m, state->ArrayIterType, &arrayiter_spec);
3055 Py_SET_TYPE(state->ArrayIterType, &PyType_Type);
Fred Drakef4e34842002-04-01 03:45:06 +00003056
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01003057 Py_INCREF((PyObject *)state->ArrayType);
3058 if (PyModule_AddObject(m, "ArrayType", (PyObject *)state->ArrayType) < 0) {
3059 Py_DECREF((PyObject *)state->ArrayType);
Marco Paolinib44ffc82019-11-15 08:42:51 +00003060 return -1;
3061 }
Pablo Galindoe51dd9d2020-07-05 22:43:14 +01003062
3063 PyObject *abc_mod = PyImport_ImportModule("collections.abc");
3064 if (!abc_mod) {
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01003065 Py_DECREF((PyObject *)state->ArrayType);
Pablo Galindoe51dd9d2020-07-05 22:43:14 +01003066 return -1;
3067 }
3068 PyObject *mutablesequence = PyObject_GetAttrString(abc_mod, "MutableSequence");
3069 Py_DECREF(abc_mod);
3070 if (!mutablesequence) {
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01003071 Py_DECREF((PyObject *)state->ArrayType);
Pablo Galindoe51dd9d2020-07-05 22:43:14 +01003072 return -1;
3073 }
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01003074 PyObject *res = PyObject_CallMethod(mutablesequence, "register", "O",
3075 (PyObject *)state->ArrayType);
Pablo Galindoe51dd9d2020-07-05 22:43:14 +01003076 Py_DECREF(mutablesequence);
3077 if (!res) {
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01003078 Py_DECREF((PyObject *)state->ArrayType);
Pablo Galindoe51dd9d2020-07-05 22:43:14 +01003079 return -1;
3080 }
3081 Py_DECREF(res);
3082
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01003083 if (PyModule_AddType(m, state->ArrayType) < 0) {
Marco Paolinib44ffc82019-11-15 08:42:51 +00003084 return -1;
3085 }
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00003086
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003087 p = buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 for (descr = descriptors; descr->typecode != '\0'; descr++) {
3089 *p++ = (char)descr->typecode;
3090 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003091 typecodes = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
Marco Paolinib44ffc82019-11-15 08:42:51 +00003092 if (PyModule_AddObject(m, "typecodes", typecodes) < 0) {
3093 Py_XDECREF(typecodes);
3094 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003095 }
Marco Paolinib44ffc82019-11-15 08:42:51 +00003096
Nick Coghland5cacbb2015-05-23 22:24:10 +10003097 return 0;
3098}
3099
3100static PyModuleDef_Slot arrayslots[] = {
3101 {Py_mod_exec, array_modexec},
3102 {0, NULL}
3103};
3104
3105
3106static struct PyModuleDef arraymodule = {
Erlend Egeberg Aasland75bf1072021-01-02 17:38:47 +01003107 .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 Aaslandb8eb3762021-01-03 14:11:15 +01003113 .m_traverse = array_traverse,
3114 .m_clear = array_clear,
3115 .m_free = array_free,
Nick Coghland5cacbb2015-05-23 22:24:10 +10003116};
3117
3118
3119PyMODINIT_FUNC
3120PyInit_array(void)
3121{
3122 return PyModuleDef_Init(&arraymodule);
Guido van Rossum778983b1993-02-19 15:55:02 +00003123}