blob: 523afb99e8ae6eb718d72ea56e2e1cb172eb5fad [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"
Raymond Hettingercb87bc82004-05-31 00:35:52 +00008#include "structmember.h"
Roger E. Masse5817f8f1996-12-09 22:24:19 +00009
Guido van Rossum0c709541994-08-19 12:01:32 +000010#ifdef STDC_HEADERS
11#include <stddef.h>
Guido van Rossum7f1de831999-08-27 20:33:52 +000012#else /* !STDC_HEADERS */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000013#ifdef HAVE_SYS_TYPES_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000014#include <sys/types.h> /* For size_t */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000015#endif /* HAVE_SYS_TYPES_H */
Guido van Rossum7f1de831999-08-27 20:33:52 +000016#endif /* !STDC_HEADERS */
Guido van Rossum778983b1993-02-19 15:55:02 +000017
Brett Cannon1eb32c22014-10-10 16:26:45 -040018/*[clinic input]
Brett Cannon1eb32c22014-10-10 16:26:45 -040019module array
20[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030021/*[clinic end generated code: output=da39a3ee5e6b4b0d input=7d1b8d7f5958fd83]*/
Brett Cannon1eb32c22014-10-10 16:26:45 -040022
Guido van Rossum778983b1993-02-19 15:55:02 +000023struct arrayobject; /* Forward */
24
Tim Petersbb307342000-09-10 05:22:54 +000025/* All possible arraydescr values are defined in the vector "descriptors"
26 * below. That's defined later because the appropriate get and set
27 * functions aren't visible yet.
28 */
Guido van Rossum778983b1993-02-19 15:55:02 +000029struct arraydescr {
Victor Stinnerf8bb7d02011-09-30 00:03:59 +020030 char typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 int itemsize;
32 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
33 int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *);
Adrian Wielgosik7c17e232017-08-17 12:46:06 +000034 int (*compareitems)(const void *, const void *, Py_ssize_t);
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020035 const char *formats;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 int is_integer_type;
37 int is_signed;
Guido van Rossum778983b1993-02-19 15:55:02 +000038};
39
40typedef struct arrayobject {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 PyObject_VAR_HEAD
42 char *ob_item;
43 Py_ssize_t allocated;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020044 const struct arraydescr *ob_descr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 PyObject *weakreflist; /* List of weak references */
46 int ob_exports; /* Number of exported buffers */
Guido van Rossum778983b1993-02-19 15:55:02 +000047} arrayobject;
48
Jeremy Hylton938ace62002-07-17 16:30:39 +000049static PyTypeObject Arraytype;
Guido van Rossum778983b1993-02-19 15:55:02 +000050
Brett Cannon1eb32c22014-10-10 16:26:45 -040051typedef struct {
52 PyObject_HEAD
53 Py_ssize_t index;
54 arrayobject *ao;
55 PyObject* (*getitem)(struct arrayobject *, Py_ssize_t);
56} arrayiterobject;
57
58static PyTypeObject PyArrayIter_Type;
59
60#define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
61
Larry Hastingsdfbeb162014-10-13 10:39:41 +010062enum machine_format_code {
63 UNKNOWN_FORMAT = -1,
64 /* UNKNOWN_FORMAT is used to indicate that the machine format for an
65 * array type code cannot be interpreted. When this occurs, a list of
66 * Python objects is used to represent the content of the array
67 * instead of using the memory content of the array directly. In that
68 * case, the array_reconstructor mechanism is bypassed completely, and
69 * the standard array constructor is used instead.
70 *
71 * This is will most likely occur when the machine doesn't use IEEE
72 * floating-point numbers.
73 */
74
75 UNSIGNED_INT8 = 0,
76 SIGNED_INT8 = 1,
77 UNSIGNED_INT16_LE = 2,
78 UNSIGNED_INT16_BE = 3,
79 SIGNED_INT16_LE = 4,
80 SIGNED_INT16_BE = 5,
81 UNSIGNED_INT32_LE = 6,
82 UNSIGNED_INT32_BE = 7,
83 SIGNED_INT32_LE = 8,
84 SIGNED_INT32_BE = 9,
85 UNSIGNED_INT64_LE = 10,
86 UNSIGNED_INT64_BE = 11,
87 SIGNED_INT64_LE = 12,
88 SIGNED_INT64_BE = 13,
89 IEEE_754_FLOAT_LE = 14,
90 IEEE_754_FLOAT_BE = 15,
91 IEEE_754_DOUBLE_LE = 16,
92 IEEE_754_DOUBLE_BE = 17,
93 UTF16_LE = 18,
94 UTF16_BE = 19,
95 UTF32_LE = 20,
96 UTF32_BE = 21
97};
98#define MACHINE_FORMAT_CODE_MIN 0
99#define MACHINE_FORMAT_CODE_MAX 21
100
101
102/*
103 * Must come after arrayobject, arrayiterobject,
104 * and enum machine_code_type definitions.
105 */
Brett Cannon1eb32c22014-10-10 16:26:45 -0400106#include "clinic/arraymodule.c.h"
107
Martin v. Löwis99866332002-03-01 10:27:01 +0000108#define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
Christian Heimes90aa7642007-12-19 02:45:37 +0000109#define array_CheckExact(op) (Py_TYPE(op) == &Arraytype)
Guido van Rossum778983b1993-02-19 15:55:02 +0000110
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000111static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000112array_resize(arrayobject *self, Py_ssize_t newsize)
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 char *items;
115 size_t _new_size;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 if (self->ob_exports > 0 && newsize != Py_SIZE(self)) {
118 PyErr_SetString(PyExc_BufferError,
119 "cannot resize an array that is exporting buffers");
120 return -1;
121 }
Antoine Pitrou3ad3a0d2008-12-18 17:08:32 +0000122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 /* Bypass realloc() when a previous overallocation is large enough
124 to accommodate the newsize. If the newsize is 16 smaller than the
125 current size, then proceed with the realloc() to shrink the array.
126 */
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 if (self->allocated >= newsize &&
129 Py_SIZE(self) < newsize + 16 &&
130 self->ob_item != NULL) {
131 Py_SIZE(self) = newsize;
132 return 0;
133 }
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 if (newsize == 0) {
136 PyMem_FREE(self->ob_item);
137 self->ob_item = NULL;
138 Py_SIZE(self) = 0;
139 self->allocated = 0;
140 return 0;
141 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 /* This over-allocates proportional to the array size, making room
144 * for additional growth. The over-allocation is mild, but is
145 * enough to give linear-time amortized behavior over a long
146 * sequence of appends() in the presence of a poorly-performing
147 * system realloc().
148 * The growth pattern is: 0, 4, 8, 16, 25, 34, 46, 56, 67, 79, ...
149 * Note, the pattern starts out the same as for lists but then
150 * grows at a smaller rate so that larger arrays only overallocate
151 * by about 1/16th -- this is done because arrays are presumed to be more
152 * memory critical.
153 */
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 _new_size = (newsize >> 4) + (Py_SIZE(self) < 8 ? 3 : 7) + newsize;
156 items = self->ob_item;
157 /* XXX The following multiplication and division does not optimize away
158 like it does for lists since the size is not known at compile time */
159 if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize))
160 PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize));
161 else
162 items = NULL;
163 if (items == NULL) {
164 PyErr_NoMemory();
165 return -1;
166 }
167 self->ob_item = items;
168 Py_SIZE(self) = newsize;
169 self->allocated = _new_size;
170 return 0;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000171}
172
Tim Petersbb307342000-09-10 05:22:54 +0000173/****************************************************************************
174Get and Set functions for each type.
175A Get function takes an arrayobject* and an integer index, returning the
176array value at that index wrapped in an appropriate PyObject*.
177A Set function takes an arrayobject, integer index, and PyObject*; sets
178the array value at that index to the raw C data extracted from the PyObject*,
179and returns 0 if successful, else nonzero on failure (PyObject* not of an
180appropriate type or value).
181Note that the basic Get and Set functions do NOT check that the index is
182in bounds; that's the responsibility of the caller.
183****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +0000184
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000185static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000186b_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 long x = ((char *)ap->ob_item)[i];
189 if (x >= 128)
190 x -= 256;
191 return PyLong_FromLong(x);
Guido van Rossum778983b1993-02-19 15:55:02 +0000192}
193
194static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000195b_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 short x;
198 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
199 must use the next size up that is signed ('h') and manually do
200 the overflow checking */
201 if (!PyArg_Parse(v, "h;array item must be integer", &x))
202 return -1;
203 else if (x < -128) {
204 PyErr_SetString(PyExc_OverflowError,
205 "signed char is less than minimum");
206 return -1;
207 }
208 else if (x > 127) {
209 PyErr_SetString(PyExc_OverflowError,
210 "signed char is greater than maximum");
211 return -1;
212 }
213 if (i >= 0)
214 ((char *)ap->ob_item)[i] = (char)x;
215 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000216}
217
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000218static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000219BB_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 long x = ((unsigned char *)ap->ob_item)[i];
222 return PyLong_FromLong(x);
Guido van Rossum549ab711997-01-03 19:09:47 +0000223}
224
Fred Drake541dc3b2000-06-28 17:49:30 +0000225static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000226BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000227{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 unsigned char x;
229 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
230 if (!PyArg_Parse(v, "b;array item must be integer", &x))
231 return -1;
232 if (i >= 0)
233 ((char *)ap->ob_item)[i] = x;
234 return 0;
Fred Drake541dc3b2000-06-28 17:49:30 +0000235}
Guido van Rossum549ab711997-01-03 19:09:47 +0000236
Martin v. Löwis99866332002-03-01 10:27:01 +0000237static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000238u_getitem(arrayobject *ap, Py_ssize_t i)
Martin v. Löwis99866332002-03-01 10:27:01 +0000239{
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200240 return PyUnicode_FromOrdinal(((Py_UNICODE *) ap->ob_item)[i]);
Martin v. Löwis99866332002-03-01 10:27:01 +0000241}
242
243static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000244u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Martin v. Löwis99866332002-03-01 10:27:01 +0000245{
Victor Stinner62bb3942012-08-06 00:46:05 +0200246 Py_UNICODE *p;
247 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +0000248
Victor Stinner62bb3942012-08-06 00:46:05 +0200249 if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 return -1;
Victor Stinner62bb3942012-08-06 00:46:05 +0200251 if (len != 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 PyErr_SetString(PyExc_TypeError,
253 "array item must be unicode character");
254 return -1;
255 }
256 if (i >= 0)
Victor Stinner62bb3942012-08-06 00:46:05 +0200257 ((Py_UNICODE *)ap->ob_item)[i] = p[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 return 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000259}
Martin v. Löwis99866332002-03-01 10:27:01 +0000260
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +0000261
Guido van Rossum549ab711997-01-03 19:09:47 +0000262static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000263h_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 return PyLong_FromLong((long) ((short *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000266}
267
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +0000268
Guido van Rossum778983b1993-02-19 15:55:02 +0000269static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000270h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 short x;
273 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
274 if (!PyArg_Parse(v, "h;array item must be integer", &x))
275 return -1;
276 if (i >= 0)
277 ((short *)ap->ob_item)[i] = x;
278 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000279}
280
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000281static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000282HH_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 return PyLong_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000285}
286
Fred Drake541dc3b2000-06-28 17:49:30 +0000287static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000288HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 int x;
291 /* PyArg_Parse's 'h' formatter is for a signed short, therefore
292 must use the next size up and manually do the overflow checking */
293 if (!PyArg_Parse(v, "i;array item must be integer", &x))
294 return -1;
295 else if (x < 0) {
296 PyErr_SetString(PyExc_OverflowError,
297 "unsigned short is less than minimum");
298 return -1;
299 }
300 else if (x > USHRT_MAX) {
301 PyErr_SetString(PyExc_OverflowError,
302 "unsigned short is greater than maximum");
303 return -1;
304 }
305 if (i >= 0)
306 ((short *)ap->ob_item)[i] = (short)x;
307 return 0;
Fred Drake541dc3b2000-06-28 17:49:30 +0000308}
Guido van Rossum549ab711997-01-03 19:09:47 +0000309
310static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000311i_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossume77a7571993-11-03 15:01:26 +0000312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 return PyLong_FromLong((long) ((int *)ap->ob_item)[i]);
Guido van Rossume77a7571993-11-03 15:01:26 +0000314}
315
316static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000317i_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossume77a7571993-11-03 15:01:26 +0000318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 int x;
320 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
321 if (!PyArg_Parse(v, "i;array item must be integer", &x))
322 return -1;
323 if (i >= 0)
324 ((int *)ap->ob_item)[i] = x;
325 return 0;
Guido van Rossume77a7571993-11-03 15:01:26 +0000326}
327
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000328static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000329II_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 return PyLong_FromUnsignedLong(
332 (unsigned long) ((unsigned int *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000333}
334
orenmn964281a2017-03-09 11:35:28 +0200335static PyObject *
336get_int_unless_float(PyObject *v)
337{
338 if (PyFloat_Check(v)) {
339 PyErr_SetString(PyExc_TypeError,
340 "array item must be integer");
341 return NULL;
342 }
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200343 return _PyLong_FromNbIndexOrNbInt(v);
orenmn964281a2017-03-09 11:35:28 +0200344}
345
Guido van Rossum549ab711997-01-03 19:09:47 +0000346static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000347II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000348{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 unsigned long x;
orenmn964281a2017-03-09 11:35:28 +0200350 int do_decref = 0; /* if nb_int was called */
351
352 if (!PyLong_Check(v)) {
353 v = get_int_unless_float(v);
354 if (NULL == v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 return -1;
356 }
orenmn964281a2017-03-09 11:35:28 +0200357 do_decref = 1;
358 }
359 x = PyLong_AsUnsignedLong(v);
360 if (x == (unsigned long)-1 && PyErr_Occurred()) {
361 if (do_decref) {
362 Py_DECREF(v);
363 }
364 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 }
366 if (x > UINT_MAX) {
367 PyErr_SetString(PyExc_OverflowError,
orenmn964281a2017-03-09 11:35:28 +0200368 "unsigned int is greater than maximum");
369 if (do_decref) {
370 Py_DECREF(v);
371 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 return -1;
373 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 if (i >= 0)
375 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
orenmn964281a2017-03-09 11:35:28 +0200376
377 if (do_decref) {
378 Py_DECREF(v);
379 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 return 0;
Guido van Rossum549ab711997-01-03 19:09:47 +0000381}
382
383static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000384l_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 return PyLong_FromLong(((long *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000387}
388
389static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000390l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000391{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 long x;
393 if (!PyArg_Parse(v, "l;array item must be integer", &x))
394 return -1;
395 if (i >= 0)
396 ((long *)ap->ob_item)[i] = x;
397 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000398}
399
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000400static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000401LL_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000404}
405
406static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000407LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 unsigned long x;
orenmn964281a2017-03-09 11:35:28 +0200410 int do_decref = 0; /* if nb_int was called */
411
412 if (!PyLong_Check(v)) {
413 v = get_int_unless_float(v);
414 if (NULL == v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 return -1;
416 }
orenmn964281a2017-03-09 11:35:28 +0200417 do_decref = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 }
orenmn964281a2017-03-09 11:35:28 +0200419 x = PyLong_AsUnsignedLong(v);
420 if (x == (unsigned long)-1 && PyErr_Occurred()) {
421 if (do_decref) {
422 Py_DECREF(v);
423 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 return -1;
425 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 if (i >= 0)
427 ((unsigned long *)ap->ob_item)[i] = x;
orenmn964281a2017-03-09 11:35:28 +0200428
429 if (do_decref) {
430 Py_DECREF(v);
431 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 return 0;
Guido van Rossum549ab711997-01-03 19:09:47 +0000433}
434
Meador Inge1c9f0c92011-09-20 19:55:51 -0500435static PyObject *
436q_getitem(arrayobject *ap, Py_ssize_t i)
437{
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700438 return PyLong_FromLongLong(((long long *)ap->ob_item)[i]);
Meador Inge1c9f0c92011-09-20 19:55:51 -0500439}
440
441static int
442q_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
443{
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700444 long long x;
Meador Inge1c9f0c92011-09-20 19:55:51 -0500445 if (!PyArg_Parse(v, "L;array item must be integer", &x))
446 return -1;
447 if (i >= 0)
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700448 ((long long *)ap->ob_item)[i] = x;
Meador Inge1c9f0c92011-09-20 19:55:51 -0500449 return 0;
450}
451
452static PyObject *
453QQ_getitem(arrayobject *ap, Py_ssize_t i)
454{
455 return PyLong_FromUnsignedLongLong(
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700456 ((unsigned long long *)ap->ob_item)[i]);
Meador Inge1c9f0c92011-09-20 19:55:51 -0500457}
458
459static int
460QQ_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
461{
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700462 unsigned long long x;
orenmn964281a2017-03-09 11:35:28 +0200463 int do_decref = 0; /* if nb_int was called */
464
465 if (!PyLong_Check(v)) {
466 v = get_int_unless_float(v);
467 if (NULL == v) {
Meador Inge1c9f0c92011-09-20 19:55:51 -0500468 return -1;
469 }
orenmn964281a2017-03-09 11:35:28 +0200470 do_decref = 1;
Meador Inge1c9f0c92011-09-20 19:55:51 -0500471 }
orenmn964281a2017-03-09 11:35:28 +0200472 x = PyLong_AsUnsignedLongLong(v);
473 if (x == (unsigned long long)-1 && PyErr_Occurred()) {
474 if (do_decref) {
475 Py_DECREF(v);
476 }
477 return -1;
478 }
Meador Inge1c9f0c92011-09-20 19:55:51 -0500479 if (i >= 0)
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700480 ((unsigned long long *)ap->ob_item)[i] = x;
orenmn964281a2017-03-09 11:35:28 +0200481
482 if (do_decref) {
483 Py_DECREF(v);
484 }
Meador Inge1c9f0c92011-09-20 19:55:51 -0500485 return 0;
486}
Meador Inge1c9f0c92011-09-20 19:55:51 -0500487
Guido van Rossum549ab711997-01-03 19:09:47 +0000488static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000489f_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000490{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000492}
493
494static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000495f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 float x;
498 if (!PyArg_Parse(v, "f;array item must be float", &x))
499 return -1;
500 if (i >= 0)
501 ((float *)ap->ob_item)[i] = x;
502 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000503}
504
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000505static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000506d_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000509}
510
511static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000512d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 double x;
515 if (!PyArg_Parse(v, "d;array item must be float", &x))
516 return -1;
517 if (i >= 0)
518 ((double *)ap->ob_item)[i] = x;
519 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000520}
521
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000522#define DEFINE_COMPAREITEMS(code, type) \
523 static int \
524 code##_compareitems(const void *lhs, const void *rhs, Py_ssize_t length) \
525 { \
526 const type *a = lhs, *b = rhs; \
527 for (Py_ssize_t i = 0; i < length; ++i) \
528 if (a[i] != b[i]) \
529 return a[i] < b[i] ? -1 : 1; \
530 return 0; \
531 }
532
533DEFINE_COMPAREITEMS(b, signed char)
534DEFINE_COMPAREITEMS(BB, unsigned char)
535DEFINE_COMPAREITEMS(u, Py_UNICODE)
536DEFINE_COMPAREITEMS(h, short)
537DEFINE_COMPAREITEMS(HH, unsigned short)
538DEFINE_COMPAREITEMS(i, int)
539DEFINE_COMPAREITEMS(II, unsigned int)
540DEFINE_COMPAREITEMS(l, long)
541DEFINE_COMPAREITEMS(LL, unsigned long)
542DEFINE_COMPAREITEMS(q, long long)
543DEFINE_COMPAREITEMS(QQ, unsigned long long)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000544
Alexandre Vassalottiad077152009-07-15 17:49:23 +0000545/* Description of types.
546 *
547 * Don't forget to update typecode_to_mformat_code() if you add a new
548 * typecode.
549 */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200550static const struct arraydescr descriptors[] = {
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000551 {'b', 1, b_getitem, b_setitem, b_compareitems, "b", 1, 1},
552 {'B', 1, BB_getitem, BB_setitem, BB_compareitems, "B", 1, 0},
553 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem, u_compareitems, "u", 0, 0},
554 {'h', sizeof(short), h_getitem, h_setitem, h_compareitems, "h", 1, 1},
555 {'H', sizeof(short), HH_getitem, HH_setitem, HH_compareitems, "H", 1, 0},
556 {'i', sizeof(int), i_getitem, i_setitem, i_compareitems, "i", 1, 1},
557 {'I', sizeof(int), II_getitem, II_setitem, II_compareitems, "I", 1, 0},
558 {'l', sizeof(long), l_getitem, l_setitem, l_compareitems, "l", 1, 1},
559 {'L', sizeof(long), LL_getitem, LL_setitem, LL_compareitems, "L", 1, 0},
560 {'q', sizeof(long long), q_getitem, q_setitem, q_compareitems, "q", 1, 1},
561 {'Q', sizeof(long long), QQ_getitem, QQ_setitem, QQ_compareitems, "Q", 1, 0},
562 {'f', sizeof(float), f_getitem, f_setitem, NULL, "f", 0, 0},
563 {'d', sizeof(double), d_getitem, d_setitem, NULL, "d", 0, 0},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 {'\0', 0, 0, 0, 0, 0, 0} /* Sentinel */
Guido van Rossum778983b1993-02-19 15:55:02 +0000565};
Tim Petersbb307342000-09-10 05:22:54 +0000566
567/****************************************************************************
568Implementations of array object methods.
569****************************************************************************/
Brett Cannon1eb32c22014-10-10 16:26:45 -0400570/*[clinic input]
571class array.array "arrayobject *" "&Arraytype"
572[clinic start generated code]*/
573/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ad43d37e942a8854]*/
Guido van Rossum778983b1993-02-19 15:55:02 +0000574
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000575static PyObject *
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200576newarrayobject(PyTypeObject *type, Py_ssize_t size, const struct arraydescr *descr)
Guido van Rossum778983b1993-02-19 15:55:02 +0000577{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 arrayobject *op;
579 size_t nbytes;
Martin v. Löwis99866332002-03-01 10:27:01 +0000580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 if (size < 0) {
582 PyErr_BadInternalCall();
583 return NULL;
584 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 /* Check for overflow */
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100587 if (size > PY_SSIZE_T_MAX / descr->itemsize) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 return PyErr_NoMemory();
589 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100590 nbytes = size * descr->itemsize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 op = (arrayobject *) type->tp_alloc(type, 0);
592 if (op == NULL) {
593 return NULL;
594 }
595 op->ob_descr = descr;
596 op->allocated = size;
597 op->weakreflist = NULL;
598 Py_SIZE(op) = size;
599 if (size <= 0) {
600 op->ob_item = NULL;
601 }
602 else {
603 op->ob_item = PyMem_NEW(char, nbytes);
604 if (op->ob_item == NULL) {
605 Py_DECREF(op);
606 return PyErr_NoMemory();
607 }
608 }
609 op->ob_exports = 0;
610 return (PyObject *) op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000611}
612
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000613static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000614getarrayitem(PyObject *op, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000615{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200616 arrayobject *ap;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 assert(array_Check(op));
618 ap = (arrayobject *)op;
619 assert(i>=0 && i<Py_SIZE(ap));
620 return (*ap->ob_descr->getitem)(ap, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000621}
622
623static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000624ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000625{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 char *items;
627 Py_ssize_t n = Py_SIZE(self);
628 if (v == NULL) {
629 PyErr_BadInternalCall();
630 return -1;
631 }
632 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
633 return -1;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 if (array_resize(self, n+1) == -1)
636 return -1;
637 items = self->ob_item;
638 if (where < 0) {
639 where += n;
640 if (where < 0)
641 where = 0;
642 }
643 if (where > n)
644 where = n;
645 /* appends don't need to call memmove() */
646 if (where != n)
647 memmove(items + (where+1)*self->ob_descr->itemsize,
648 items + where*self->ob_descr->itemsize,
649 (n-where)*self->ob_descr->itemsize);
650 return (*self->ob_descr->setitem)(self, where, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000651}
652
Guido van Rossum778983b1993-02-19 15:55:02 +0000653/* Methods */
654
655static void
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000656array_dealloc(arrayobject *op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000657{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 if (op->weakreflist != NULL)
659 PyObject_ClearWeakRefs((PyObject *) op);
660 if (op->ob_item != NULL)
661 PyMem_DEL(op->ob_item);
662 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000663}
664
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000665static PyObject *
666array_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 arrayobject *va, *wa;
669 PyObject *vi = NULL;
670 PyObject *wi = NULL;
671 Py_ssize_t i, k;
672 PyObject *res;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000673
Brian Curtindfc80e32011-08-10 20:28:54 -0500674 if (!array_Check(v) || !array_Check(w))
675 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 va = (arrayobject *)v;
678 wa = (arrayobject *)w;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 if (Py_SIZE(va) != Py_SIZE(wa) && (op == Py_EQ || op == Py_NE)) {
681 /* Shortcut: if the lengths differ, the arrays differ */
682 if (op == Py_EQ)
683 res = Py_False;
684 else
685 res = Py_True;
686 Py_INCREF(res);
687 return res;
688 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000689
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000690 if (va->ob_descr == wa->ob_descr && va->ob_descr->compareitems != NULL) {
691 /* Fast path:
692 arrays with same types can have their buffers compared directly */
693 Py_ssize_t common_length = Py_MIN(Py_SIZE(va), Py_SIZE(wa));
694 int result = va->ob_descr->compareitems(va->ob_item, wa->ob_item,
695 common_length);
696 if (result == 0)
697 goto compare_sizes;
698
699 int cmp;
700 switch (op) {
701 case Py_LT: cmp = result < 0; break;
702 case Py_LE: cmp = result <= 0; break;
703 case Py_EQ: cmp = result == 0; break;
704 case Py_NE: cmp = result != 0; break;
705 case Py_GT: cmp = result > 0; break;
706 case Py_GE: cmp = result >= 0; break;
707 default: return NULL; /* cannot happen */
708 }
709 PyObject *res = cmp ? Py_True : Py_False;
710 Py_INCREF(res);
711 return res;
712 }
713
714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 /* Search for the first index where items are different */
716 k = 1;
717 for (i = 0; i < Py_SIZE(va) && i < Py_SIZE(wa); i++) {
718 vi = getarrayitem(v, i);
719 wi = getarrayitem(w, i);
720 if (vi == NULL || wi == NULL) {
721 Py_XDECREF(vi);
722 Py_XDECREF(wi);
723 return NULL;
724 }
725 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
726 if (k == 0)
727 break; /* Keeping vi and wi alive! */
728 Py_DECREF(vi);
729 Py_DECREF(wi);
730 if (k < 0)
731 return NULL;
732 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 if (k) {
735 /* No more items to compare -- compare sizes */
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000736 compare_sizes: ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 Py_ssize_t vs = Py_SIZE(va);
738 Py_ssize_t ws = Py_SIZE(wa);
739 int cmp;
740 switch (op) {
741 case Py_LT: cmp = vs < ws; break;
742 case Py_LE: cmp = vs <= ws; break;
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000743 /* If the lengths were not equal,
744 the earlier fast-path check would have caught that. */
745 case Py_EQ: assert(vs == ws); cmp = 1; break;
746 case Py_NE: assert(vs == ws); cmp = 0; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 case Py_GT: cmp = vs > ws; break;
748 case Py_GE: cmp = vs >= ws; break;
749 default: return NULL; /* cannot happen */
750 }
751 if (cmp)
752 res = Py_True;
753 else
754 res = Py_False;
755 Py_INCREF(res);
756 return res;
757 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 /* We have an item that differs. First, shortcuts for EQ/NE */
760 if (op == Py_EQ) {
761 Py_INCREF(Py_False);
762 res = Py_False;
763 }
764 else if (op == Py_NE) {
765 Py_INCREF(Py_True);
766 res = Py_True;
767 }
768 else {
769 /* Compare the final item again using the proper operator */
770 res = PyObject_RichCompare(vi, wi, op);
771 }
772 Py_DECREF(vi);
773 Py_DECREF(wi);
774 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +0000775}
776
Martin v. Löwis18e16552006-02-15 17:27:45 +0000777static Py_ssize_t
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000778array_length(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +0000779{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 return Py_SIZE(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000781}
782
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000783static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000784array_item(arrayobject *a, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000785{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 if (i < 0 || i >= Py_SIZE(a)) {
787 PyErr_SetString(PyExc_IndexError, "array index out of range");
788 return NULL;
789 }
790 return getarrayitem((PyObject *)a, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000791}
792
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000793static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000794array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000795{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 arrayobject *np;
797 if (ilow < 0)
798 ilow = 0;
799 else if (ilow > Py_SIZE(a))
800 ilow = Py_SIZE(a);
801 if (ihigh < 0)
802 ihigh = 0;
803 if (ihigh < ilow)
804 ihigh = ilow;
805 else if (ihigh > Py_SIZE(a))
806 ihigh = Py_SIZE(a);
807 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
808 if (np == NULL)
809 return NULL;
Martin Panterbe8da9c2016-09-07 11:04:41 +0000810 if (ihigh > ilow) {
811 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
812 (ihigh-ilow) * a->ob_descr->itemsize);
813 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000815}
816
Brett Cannon1eb32c22014-10-10 16:26:45 -0400817
818/*[clinic input]
819array.array.__copy__
820
821Return a copy of the array.
822[clinic start generated code]*/
823
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000824static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -0400825array_array___copy___impl(arrayobject *self)
826/*[clinic end generated code: output=dec7c3f925d9619e input=ad1ee5b086965f09]*/
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000827{
Brett Cannon1eb32c22014-10-10 16:26:45 -0400828 return array_slice(self, 0, Py_SIZE(self));
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000829}
830
Brett Cannon1eb32c22014-10-10 16:26:45 -0400831/*[clinic input]
832array.array.__deepcopy__
833
834 unused: object
835 /
836
837Return a copy of the array.
838[clinic start generated code]*/
839
840static PyObject *
841array_array___deepcopy__(arrayobject *self, PyObject *unused)
842/*[clinic end generated code: output=1ec748d8e14a9faa input=2405ecb4933748c4]*/
843{
844 return array_array___copy___impl(self);
845}
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000846
847static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000848array_concat(arrayobject *a, PyObject *bb)
Guido van Rossum778983b1993-02-19 15:55:02 +0000849{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 Py_ssize_t size;
851 arrayobject *np;
852 if (!array_Check(bb)) {
853 PyErr_Format(PyExc_TypeError,
854 "can only append array (not \"%.200s\") to array",
855 Py_TYPE(bb)->tp_name);
856 return NULL;
857 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000858#define b ((arrayobject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 if (a->ob_descr != b->ob_descr) {
860 PyErr_BadArgument();
861 return NULL;
862 }
863 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) {
864 return PyErr_NoMemory();
865 }
866 size = Py_SIZE(a) + Py_SIZE(b);
867 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
868 if (np == NULL) {
869 return NULL;
870 }
Martin Panterbe8da9c2016-09-07 11:04:41 +0000871 if (Py_SIZE(a) > 0) {
872 memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize);
873 }
874 if (Py_SIZE(b) > 0) {
875 memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize,
876 b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
877 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000879#undef b
880}
881
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000882static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000883array_repeat(arrayobject *a, Py_ssize_t n)
Guido van Rossum778983b1993-02-19 15:55:02 +0000884{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 Py_ssize_t size;
886 arrayobject *np;
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000887 Py_ssize_t oldbytes, newbytes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 if (n < 0)
889 n = 0;
890 if ((Py_SIZE(a) != 0) && (n > PY_SSIZE_T_MAX / Py_SIZE(a))) {
891 return PyErr_NoMemory();
892 }
893 size = Py_SIZE(a) * n;
894 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
895 if (np == NULL)
896 return NULL;
Martin Panterbe8da9c2016-09-07 11:04:41 +0000897 if (size == 0)
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000898 return (PyObject *)np;
899 oldbytes = Py_SIZE(a) * a->ob_descr->itemsize;
900 newbytes = oldbytes * n;
901 /* this follows the code in unicode_repeat */
902 if (oldbytes == 1) {
903 memset(np->ob_item, a->ob_item[0], newbytes);
904 } else {
905 Py_ssize_t done = oldbytes;
Christian Heimesf051e432016-09-13 20:22:02 +0200906 memcpy(np->ob_item, a->ob_item, oldbytes);
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000907 while (done < newbytes) {
908 Py_ssize_t ncopy = (done <= newbytes-done) ? done : newbytes-done;
Christian Heimesf051e432016-09-13 20:22:02 +0200909 memcpy(np->ob_item+done, np->ob_item, ncopy);
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000910 done += ncopy;
911 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 }
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000913 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000914}
915
916static int
Martin Panter996d72b2016-07-25 02:21:14 +0000917array_del_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000918{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 char *item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 Py_ssize_t d; /* Change in size */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 if (ilow < 0)
922 ilow = 0;
923 else if (ilow > Py_SIZE(a))
924 ilow = Py_SIZE(a);
925 if (ihigh < 0)
926 ihigh = 0;
927 if (ihigh < ilow)
928 ihigh = ilow;
929 else if (ihigh > Py_SIZE(a))
930 ihigh = Py_SIZE(a);
931 item = a->ob_item;
Martin Panter996d72b2016-07-25 02:21:14 +0000932 d = ihigh-ilow;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 /* Issue #4509: If the array has exported buffers and the slice
934 assignment would change the size of the array, fail early to make
935 sure we don't modify it. */
936 if (d != 0 && a->ob_exports > 0) {
937 PyErr_SetString(PyExc_BufferError,
938 "cannot resize an array that is exporting buffers");
939 return -1;
940 }
Martin Panter996d72b2016-07-25 02:21:14 +0000941 if (d > 0) { /* Delete d items */
942 memmove(item + (ihigh-d)*a->ob_descr->itemsize,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 item + ihigh*a->ob_descr->itemsize,
944 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
Martin Panter996d72b2016-07-25 02:21:14 +0000945 if (array_resize(a, Py_SIZE(a) - d) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 return -1;
947 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000949}
950
951static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000952array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000953{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 if (i < 0 || i >= Py_SIZE(a)) {
955 PyErr_SetString(PyExc_IndexError,
956 "array assignment index out of range");
957 return -1;
958 }
959 if (v == NULL)
Martin Panter996d72b2016-07-25 02:21:14 +0000960 return array_del_slice(a, i, i+1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 return (*a->ob_descr->setitem)(a, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000962}
963
964static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000965setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000966{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 assert(array_Check(a));
968 return array_ass_item((arrayobject *)a, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000969}
970
Martin v. Löwis99866332002-03-01 10:27:01 +0000971static int
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000972array_iter_extend(arrayobject *self, PyObject *bb)
973{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 PyObject *it, *v;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 it = PyObject_GetIter(bb);
977 if (it == NULL)
978 return -1;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 while ((v = PyIter_Next(it)) != NULL) {
Mark Dickinson346f0af2010-08-06 09:36:57 +0000981 if (ins1(self, Py_SIZE(self), v) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 Py_DECREF(v);
983 Py_DECREF(it);
984 return -1;
985 }
986 Py_DECREF(v);
987 }
988 Py_DECREF(it);
989 if (PyErr_Occurred())
990 return -1;
991 return 0;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000992}
993
994static int
Martin v. Löwis99866332002-03-01 10:27:01 +0000995array_do_extend(arrayobject *self, PyObject *bb)
996{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 Py_ssize_t size, oldsize, bbsize;
Martin v. Löwis99866332002-03-01 10:27:01 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 if (!array_Check(bb))
1000 return array_iter_extend(self, bb);
1001#define b ((arrayobject *)bb)
1002 if (self->ob_descr != b->ob_descr) {
1003 PyErr_SetString(PyExc_TypeError,
1004 "can only extend with array of same kind");
1005 return -1;
1006 }
1007 if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) ||
1008 ((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
1009 PyErr_NoMemory();
1010 return -1;
1011 }
1012 oldsize = Py_SIZE(self);
1013 /* Get the size of bb before resizing the array since bb could be self. */
1014 bbsize = Py_SIZE(bb);
1015 size = oldsize + Py_SIZE(b);
1016 if (array_resize(self, size) == -1)
1017 return -1;
Martin Panterbe8da9c2016-09-07 11:04:41 +00001018 if (bbsize > 0) {
1019 memcpy(self->ob_item + oldsize * self->ob_descr->itemsize,
1020 b->ob_item, bbsize * b->ob_descr->itemsize);
1021 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022
1023 return 0;
Martin v. Löwis99866332002-03-01 10:27:01 +00001024#undef b
1025}
1026
1027static PyObject *
1028array_inplace_concat(arrayobject *self, PyObject *bb)
1029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 if (!array_Check(bb)) {
1031 PyErr_Format(PyExc_TypeError,
1032 "can only extend array with array (not \"%.200s\")",
1033 Py_TYPE(bb)->tp_name);
1034 return NULL;
1035 }
1036 if (array_do_extend(self, bb) == -1)
1037 return NULL;
1038 Py_INCREF(self);
1039 return (PyObject *)self;
Martin v. Löwis99866332002-03-01 10:27:01 +00001040}
1041
1042static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001043array_inplace_repeat(arrayobject *self, Py_ssize_t n)
Martin v. Löwis99866332002-03-01 10:27:01 +00001044{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 char *items, *p;
1046 Py_ssize_t size, i;
Martin v. Löwis99866332002-03-01 10:27:01 +00001047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 if (Py_SIZE(self) > 0) {
1049 if (n < 0)
1050 n = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 if ((self->ob_descr->itemsize != 0) &&
1052 (Py_SIZE(self) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
1053 return PyErr_NoMemory();
1054 }
1055 size = Py_SIZE(self) * self->ob_descr->itemsize;
1056 if (n > 0 && size > PY_SSIZE_T_MAX / n) {
1057 return PyErr_NoMemory();
1058 }
1059 if (array_resize(self, n * Py_SIZE(self)) == -1)
1060 return NULL;
1061 items = p = self->ob_item;
1062 for (i = 1; i < n; i++) {
1063 p += size;
1064 memcpy(p, items, size);
1065 }
1066 }
1067 Py_INCREF(self);
1068 return (PyObject *)self;
Martin v. Löwis99866332002-03-01 10:27:01 +00001069}
1070
1071
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001072static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001073ins(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +00001074{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 if (ins1(self, where, v) != 0)
1076 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001077 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001078}
1079
Brett Cannon1eb32c22014-10-10 16:26:45 -04001080/*[clinic input]
1081array.array.count
1082
1083 v: object
1084 /
1085
1086Return number of occurrences of v in the array.
1087[clinic start generated code]*/
1088
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001089static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001090array_array_count(arrayobject *self, PyObject *v)
1091/*[clinic end generated code: output=3dd3624bf7135a3a input=d9bce9d65e39d1f5]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001092{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 Py_ssize_t count = 0;
1094 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 for (i = 0; i < Py_SIZE(self); i++) {
Victor Stinner0b142e22013-07-17 23:01:30 +02001097 PyObject *selfi;
1098 int cmp;
1099
1100 selfi = getarrayitem((PyObject *)self, i);
1101 if (selfi == NULL)
1102 return NULL;
1103 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 Py_DECREF(selfi);
1105 if (cmp > 0)
1106 count++;
1107 else if (cmp < 0)
1108 return NULL;
1109 }
1110 return PyLong_FromSsize_t(count);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001111}
1112
Brett Cannon1eb32c22014-10-10 16:26:45 -04001113
1114/*[clinic input]
1115array.array.index
1116
1117 v: object
1118 /
1119
1120Return index of first occurrence of v in the array.
1121[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001122
1123static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001124array_array_index(arrayobject *self, PyObject *v)
1125/*[clinic end generated code: output=d48498d325602167 input=cf619898c6649d08]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 for (i = 0; i < Py_SIZE(self); i++) {
Victor Stinner0b142e22013-07-17 23:01:30 +02001130 PyObject *selfi;
1131 int cmp;
1132
1133 selfi = getarrayitem((PyObject *)self, i);
1134 if (selfi == NULL)
1135 return NULL;
1136 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 Py_DECREF(selfi);
1138 if (cmp > 0) {
1139 return PyLong_FromLong((long)i);
1140 }
1141 else if (cmp < 0)
1142 return NULL;
1143 }
Jim Fasarakis-Hilliarda4095ef2017-05-29 20:43:39 +03001144 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in array");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 return NULL;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001146}
1147
Raymond Hettinger625812f2003-01-07 01:58:52 +00001148static int
1149array_contains(arrayobject *self, PyObject *v)
1150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 Py_ssize_t i;
1152 int cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +00001153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) {
1155 PyObject *selfi = getarrayitem((PyObject *)self, i);
Victor Stinner0b142e22013-07-17 23:01:30 +02001156 if (selfi == NULL)
Victor Stinner4755bea2013-07-18 01:12:35 +02001157 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
1159 Py_DECREF(selfi);
1160 }
1161 return cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +00001162}
1163
Brett Cannon1eb32c22014-10-10 16:26:45 -04001164/*[clinic input]
1165array.array.remove
1166
1167 v: object
1168 /
1169
1170Remove the first occurrence of v in the array.
1171[clinic start generated code]*/
1172
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001173static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001174array_array_remove(arrayobject *self, PyObject *v)
1175/*[clinic end generated code: output=bef06be9fdf9dceb input=0b1e5aed25590027]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001176{
sthaa3ecb82019-03-20 20:49:39 +01001177 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 for (i = 0; i < Py_SIZE(self); i++) {
Victor Stinner0b142e22013-07-17 23:01:30 +02001180 PyObject *selfi;
1181 int cmp;
1182
1183 selfi = getarrayitem((PyObject *)self,i);
1184 if (selfi == NULL)
1185 return NULL;
1186 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 Py_DECREF(selfi);
1188 if (cmp > 0) {
Martin Panter996d72b2016-07-25 02:21:14 +00001189 if (array_del_slice(self, i, i+1) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001191 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 }
1193 else if (cmp < 0)
1194 return NULL;
1195 }
Jim Fasarakis-Hilliarda4095ef2017-05-29 20:43:39 +03001196 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in array");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 return NULL;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001198}
1199
Brett Cannon1eb32c22014-10-10 16:26:45 -04001200/*[clinic input]
1201array.array.pop
1202
1203 i: Py_ssize_t = -1
1204 /
1205
1206Return the i-th element and delete it from the array.
1207
1208i defaults to -1.
1209[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001210
1211static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001212array_array_pop_impl(arrayobject *self, Py_ssize_t i)
1213/*[clinic end generated code: output=bc1f0c54fe5308e4 input=8e5feb4c1a11cd44]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 PyObject *v;
Brett Cannon1eb32c22014-10-10 16:26:45 -04001216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 if (Py_SIZE(self) == 0) {
1218 /* Special-case most common failure cause */
1219 PyErr_SetString(PyExc_IndexError, "pop from empty array");
1220 return NULL;
1221 }
1222 if (i < 0)
1223 i += Py_SIZE(self);
1224 if (i < 0 || i >= Py_SIZE(self)) {
1225 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1226 return NULL;
1227 }
Victor Stinner0b142e22013-07-17 23:01:30 +02001228 v = getarrayitem((PyObject *)self, i);
1229 if (v == NULL)
1230 return NULL;
Martin Panter996d72b2016-07-25 02:21:14 +00001231 if (array_del_slice(self, i, i+1) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 Py_DECREF(v);
1233 return NULL;
1234 }
1235 return v;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001236}
1237
Brett Cannon1eb32c22014-10-10 16:26:45 -04001238/*[clinic input]
1239array.array.extend
1240
1241 bb: object
1242 /
1243
1244Append items to the end of the array.
1245[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001246
1247static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001248array_array_extend(arrayobject *self, PyObject *bb)
1249/*[clinic end generated code: output=bbddbc8e8bef871d input=43be86aba5c31e44]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 if (array_do_extend(self, bb) == -1)
1252 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001253 Py_RETURN_NONE;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001254}
1255
Brett Cannon1eb32c22014-10-10 16:26:45 -04001256/*[clinic input]
1257array.array.insert
1258
1259 i: Py_ssize_t
1260 v: object
1261 /
1262
1263Insert a new item v into the array before position i.
1264[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001265
1266static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001267array_array_insert_impl(arrayobject *self, Py_ssize_t i, PyObject *v)
1268/*[clinic end generated code: output=5a3648e278348564 input=5577d1b4383e9313]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 return ins(self, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001271}
1272
Brett Cannon1eb32c22014-10-10 16:26:45 -04001273/*[clinic input]
1274array.array.buffer_info
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001275
Brett Cannon1eb32c22014-10-10 16:26:45 -04001276Return a tuple (address, length) giving the current memory address and the length in items of the buffer used to hold array's contents.
1277
1278The length should be multiplied by the itemsize attribute to calculate
1279the buffer length in bytes.
1280[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001281
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001282static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001283array_array_buffer_info_impl(arrayobject *self)
1284/*[clinic end generated code: output=9b2a4ec3ae7e98e7 input=a58bae5c6e1ac6a6]*/
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001285{
Victor Stinner541067a2013-11-14 01:27:12 +01001286 PyObject *retval = NULL, *v;
1287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 retval = PyTuple_New(2);
1289 if (!retval)
1290 return NULL;
Fred Drake541dc3b2000-06-28 17:49:30 +00001291
Victor Stinner541067a2013-11-14 01:27:12 +01001292 v = PyLong_FromVoidPtr(self->ob_item);
1293 if (v == NULL) {
1294 Py_DECREF(retval);
1295 return NULL;
1296 }
1297 PyTuple_SET_ITEM(retval, 0, v);
1298
Serhiy Storchaka9e941d62016-06-23 23:55:34 +03001299 v = PyLong_FromSsize_t(Py_SIZE(self));
Victor Stinner541067a2013-11-14 01:27:12 +01001300 if (v == NULL) {
1301 Py_DECREF(retval);
1302 return NULL;
1303 }
1304 PyTuple_SET_ITEM(retval, 1, v);
Fred Drake541dc3b2000-06-28 17:49:30 +00001305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 return retval;
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001307}
1308
Brett Cannon1eb32c22014-10-10 16:26:45 -04001309/*[clinic input]
1310array.array.append
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001311
Brett Cannon1eb32c22014-10-10 16:26:45 -04001312 v: object
1313 /
1314
1315Append new value v to the end of the array.
1316[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001317
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001318static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001319array_array_append(arrayobject *self, PyObject *v)
1320/*[clinic end generated code: output=745a0669bf8db0e2 input=0b98d9d78e78f0fa]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001321{
Mark Dickinson346f0af2010-08-06 09:36:57 +00001322 return ins(self, Py_SIZE(self), v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001323}
1324
Brett Cannon1eb32c22014-10-10 16:26:45 -04001325/*[clinic input]
1326array.array.byteswap
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001327
Brett Cannon1eb32c22014-10-10 16:26:45 -04001328Byteswap all items of the array.
1329
1330If the items in the array are not 1, 2, 4, or 8 bytes in size, RuntimeError is
1331raised.
1332[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001333
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001334static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001335array_array_byteswap_impl(arrayobject *self)
1336/*[clinic end generated code: output=5f8236cbdf0d90b5 input=6a85591b950a0186]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 char *p;
1339 Py_ssize_t i;
Fred Drakebf272981999-12-03 17:15:30 +00001340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 switch (self->ob_descr->itemsize) {
1342 case 1:
1343 break;
1344 case 2:
1345 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) {
1346 char p0 = p[0];
1347 p[0] = p[1];
1348 p[1] = p0;
1349 }
1350 break;
1351 case 4:
1352 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 4) {
1353 char p0 = p[0];
1354 char p1 = p[1];
1355 p[0] = p[3];
1356 p[1] = p[2];
1357 p[2] = p1;
1358 p[3] = p0;
1359 }
1360 break;
1361 case 8:
1362 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) {
1363 char p0 = p[0];
1364 char p1 = p[1];
1365 char p2 = p[2];
1366 char p3 = p[3];
1367 p[0] = p[7];
1368 p[1] = p[6];
1369 p[2] = p[5];
1370 p[3] = p[4];
1371 p[4] = p3;
1372 p[5] = p2;
1373 p[6] = p1;
1374 p[7] = p0;
1375 }
1376 break;
1377 default:
1378 PyErr_SetString(PyExc_RuntimeError,
1379 "don't know how to byteswap this array type");
1380 return NULL;
1381 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001382 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001383}
1384
Brett Cannon1eb32c22014-10-10 16:26:45 -04001385/*[clinic input]
1386array.array.reverse
1387
1388Reverse the order of the items in the array.
1389[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001390
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001391static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001392array_array_reverse_impl(arrayobject *self)
1393/*[clinic end generated code: output=c04868b36f6f4089 input=cd904f01b27d966a]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001394{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001395 Py_ssize_t itemsize = self->ob_descr->itemsize;
1396 char *p, *q;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 /* little buffer to hold items while swapping */
1398 char tmp[256]; /* 8 is probably enough -- but why skimp */
1399 assert((size_t)itemsize <= sizeof(tmp));
Guido van Rossume77a7571993-11-03 15:01:26 +00001400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 if (Py_SIZE(self) > 1) {
1402 for (p = self->ob_item,
1403 q = self->ob_item + (Py_SIZE(self) - 1)*itemsize;
1404 p < q;
1405 p += itemsize, q -= itemsize) {
1406 /* memory areas guaranteed disjoint, so memcpy
1407 * is safe (& memmove may be slower).
1408 */
1409 memcpy(tmp, p, itemsize);
1410 memcpy(p, q, itemsize);
1411 memcpy(q, tmp, itemsize);
1412 }
1413 }
Tim Petersbb307342000-09-10 05:22:54 +00001414
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001415 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001416}
Guido van Rossume77a7571993-11-03 15:01:26 +00001417
Brett Cannon1eb32c22014-10-10 16:26:45 -04001418/*[clinic input]
1419array.array.fromfile
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001420
Brett Cannon1eb32c22014-10-10 16:26:45 -04001421 f: object
1422 n: Py_ssize_t
1423 /
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001424
Brett Cannon1eb32c22014-10-10 16:26:45 -04001425Read n objects from the file object f and append them to the end of the array.
1426[clinic start generated code]*/
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001427
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001428static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001429array_array_fromfile_impl(arrayobject *self, PyObject *f, Py_ssize_t n)
1430/*[clinic end generated code: output=ec9f600e10f53510 input=e188afe8e58adf40]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001431{
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03001432 PyObject *b, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 Py_ssize_t itemsize = self->ob_descr->itemsize;
Brett Cannon1eb32c22014-10-10 16:26:45 -04001434 Py_ssize_t nbytes;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001435 _Py_IDENTIFIER(read);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 int not_enough_bytes;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001437
Mark Dickinsonc04ddff2012-10-06 18:04:49 +01001438 if (n < 0) {
1439 PyErr_SetString(PyExc_ValueError, "negative count");
1440 return NULL;
1441 }
1442 if (n > PY_SSIZE_T_MAX / itemsize) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 PyErr_NoMemory();
1444 return NULL;
1445 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +01001446 nbytes = n * itemsize;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001447
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001448 b = _PyObject_CallMethodId(f, &PyId_read, "n", nbytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 if (b == NULL)
1450 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 if (!PyBytes_Check(b)) {
1453 PyErr_SetString(PyExc_TypeError,
1454 "read() didn't return bytes");
1455 Py_DECREF(b);
1456 return NULL;
1457 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 not_enough_bytes = (PyBytes_GET_SIZE(b) != nbytes);
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001460
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03001461 res = array_array_frombytes(self, b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 Py_DECREF(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 if (res == NULL)
1464 return NULL;
Hirokazu Yamamoto54d0df62009-03-06 03:04:07 +00001465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 if (not_enough_bytes) {
1467 PyErr_SetString(PyExc_EOFError,
1468 "read() didn't return enough bytes");
1469 Py_DECREF(res);
1470 return NULL;
1471 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +00001474}
1475
Brett Cannon1eb32c22014-10-10 16:26:45 -04001476/*[clinic input]
1477array.array.tofile
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001478
Brett Cannon1eb32c22014-10-10 16:26:45 -04001479 f: object
1480 /
1481
1482Write all items (as machine values) to the file object f.
1483[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001484
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001485static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001486array_array_tofile(arrayobject *self, PyObject *f)
1487/*[clinic end generated code: output=3a2cfa8128df0777 input=b0669a484aab0831]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize;
1490 /* Write 64K blocks at a time */
1491 /* XXX Make the block size settable */
1492 int BLOCKSIZE = 64*1024;
1493 Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE;
1494 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 if (Py_SIZE(self) == 0)
1497 goto done;
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 for (i = 0; i < nblocks; i++) {
1500 char* ptr = self->ob_item + i*BLOCKSIZE;
1501 Py_ssize_t size = BLOCKSIZE;
1502 PyObject *bytes, *res;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001503 _Py_IDENTIFIER(write);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 if (i*BLOCKSIZE + size > nbytes)
1506 size = nbytes - i*BLOCKSIZE;
1507 bytes = PyBytes_FromStringAndSize(ptr, size);
1508 if (bytes == NULL)
1509 return NULL;
Victor Stinner55ba38a2016-12-09 16:09:30 +01001510 res = _PyObject_CallMethodIdObjArgs(f, &PyId_write, bytes, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 Py_DECREF(bytes);
1512 if (res == NULL)
1513 return NULL;
1514 Py_DECREF(res); /* drop write result */
1515 }
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001516
1517 done:
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001518 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001519}
1520
Brett Cannon1eb32c22014-10-10 16:26:45 -04001521/*[clinic input]
1522array.array.fromlist
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001523
Brett Cannon1eb32c22014-10-10 16:26:45 -04001524 list: object
1525 /
1526
1527Append items to array from list.
1528[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001529
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001530static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001531array_array_fromlist(arrayobject *self, PyObject *list)
1532/*[clinic end generated code: output=26411c2d228a3e3f input=be2605a96c49680f]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 Py_ssize_t n;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 if (!PyList_Check(list)) {
1537 PyErr_SetString(PyExc_TypeError, "arg must be list");
1538 return NULL;
1539 }
1540 n = PyList_Size(list);
1541 if (n > 0) {
1542 Py_ssize_t i, old_size;
1543 old_size = Py_SIZE(self);
1544 if (array_resize(self, old_size + n) == -1)
1545 return NULL;
1546 for (i = 0; i < n; i++) {
Zackery Spytz99d56b52018-12-08 07:16:55 -07001547 PyObject *v = PyList_GET_ITEM(list, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 if ((*self->ob_descr->setitem)(self,
1549 Py_SIZE(self) - n + i, v) != 0) {
1550 array_resize(self, old_size);
1551 return NULL;
1552 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07001553 if (n != PyList_GET_SIZE(list)) {
1554 PyErr_SetString(PyExc_RuntimeError,
1555 "list changed size during iteration");
1556 array_resize(self, old_size);
1557 return NULL;
1558 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 }
1560 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001561 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001562}
1563
Brett Cannon1eb32c22014-10-10 16:26:45 -04001564/*[clinic input]
1565array.array.tolist
1566
1567Convert array to an ordinary list with the same items.
1568[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001569
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001570static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001571array_array_tolist_impl(arrayobject *self)
1572/*[clinic end generated code: output=00b60cc9eab8ef89 input=a8d7784a94f86b53]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001573{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 PyObject *list = PyList_New(Py_SIZE(self));
1575 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 if (list == NULL)
1578 return NULL;
1579 for (i = 0; i < Py_SIZE(self); i++) {
1580 PyObject *v = getarrayitem((PyObject *)self, i);
Victor Stinner4755bea2013-07-18 01:12:35 +02001581 if (v == NULL)
1582 goto error;
Zackery Spytz99d56b52018-12-08 07:16:55 -07001583 PyList_SET_ITEM(list, i, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 }
1585 return list;
Victor Stinner4755bea2013-07-18 01:12:35 +02001586
1587error:
1588 Py_DECREF(list);
1589 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001590}
1591
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001592static PyObject *
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001593frombytes(arrayobject *self, Py_buffer *buffer)
Guido van Rossum778983b1993-02-19 15:55:02 +00001594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 int itemsize = self->ob_descr->itemsize;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001596 Py_ssize_t n;
1597 if (buffer->itemsize != 1) {
1598 PyBuffer_Release(buffer);
Serhiy Storchakab757c832014-12-05 22:25:22 +02001599 PyErr_SetString(PyExc_TypeError, "a bytes-like object is required");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 return NULL;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001601 }
1602 n = buffer->len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 if (n % itemsize != 0) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001604 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 PyErr_SetString(PyExc_ValueError,
Serhiy Storchakab757c832014-12-05 22:25:22 +02001606 "bytes length not a multiple of item size");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 return NULL;
1608 }
1609 n = n / itemsize;
1610 if (n > 0) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001611 Py_ssize_t old_size = Py_SIZE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 if ((n > PY_SSIZE_T_MAX - old_size) ||
1613 ((old_size + n) > PY_SSIZE_T_MAX / itemsize)) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001614 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 return PyErr_NoMemory();
1616 }
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001617 if (array_resize(self, old_size + n) == -1) {
1618 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 return NULL;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001620 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 memcpy(self->ob_item + old_size * itemsize,
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001622 buffer->buf, n * itemsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 }
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001624 PyBuffer_Release(buffer);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001625 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001626}
1627
Brett Cannon1eb32c22014-10-10 16:26:45 -04001628/*[clinic input]
1629array.array.fromstring
1630
Larry Hastingsdbfdc382015-05-04 06:59:46 -07001631 buffer: Py_buffer(accept={str, buffer})
Brett Cannon1eb32c22014-10-10 16:26:45 -04001632 /
1633
1634Appends 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).
1635
1636This method is deprecated. Use frombytes instead.
1637[clinic start generated code]*/
1638
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001639static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001640array_array_fromstring_impl(arrayobject *self, Py_buffer *buffer)
Larry Hastingsdbfdc382015-05-04 06:59:46 -07001641/*[clinic end generated code: output=31c4baa779df84ce input=a3341a512e11d773]*/
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001642{
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001643 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1644 "fromstring() is deprecated. Use frombytes() instead.", 2) != 0)
1645 return NULL;
Brett Cannon1eb32c22014-10-10 16:26:45 -04001646 return frombytes(self, buffer);
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001647}
1648
Brett Cannon1eb32c22014-10-10 16:26:45 -04001649/*[clinic input]
1650array.array.frombytes
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001651
Brett Cannon1eb32c22014-10-10 16:26:45 -04001652 buffer: Py_buffer
1653 /
1654
1655Appends 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).
1656[clinic start generated code]*/
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001657
1658static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001659array_array_frombytes_impl(arrayobject *self, Py_buffer *buffer)
1660/*[clinic end generated code: output=d9842c8f7510a516 input=2bbf2b53ebfcc988]*/
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001661{
Brett Cannon1eb32c22014-10-10 16:26:45 -04001662 return frombytes(self, buffer);
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001663}
1664
Brett Cannon1eb32c22014-10-10 16:26:45 -04001665/*[clinic input]
1666array.array.tobytes
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001667
Brett Cannon1eb32c22014-10-10 16:26:45 -04001668Convert the array to an array of machine values and return the bytes representation.
1669[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001670
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001671static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001672array_array_tobytes_impl(arrayobject *self)
1673/*[clinic end generated code: output=87318e4edcdc2bb6 input=90ee495f96de34f5]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
1676 return PyBytes_FromStringAndSize(self->ob_item,
1677 Py_SIZE(self) * self->ob_descr->itemsize);
1678 } else {
1679 return PyErr_NoMemory();
1680 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001681}
1682
Brett Cannon1eb32c22014-10-10 16:26:45 -04001683/*[clinic input]
1684array.array.tostring
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001685
Brett Cannon1eb32c22014-10-10 16:26:45 -04001686Convert the array to an array of machine values and return the bytes representation.
1687
1688This method is deprecated. Use tobytes instead.
1689[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001690
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001691static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001692array_array_tostring_impl(arrayobject *self)
1693/*[clinic end generated code: output=7d6bd92745a2c8f3 input=b6c0ddee7b30457e]*/
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001694{
Victor Stinner9f0b51e2010-11-09 09:38:30 +00001695 if (PyErr_WarnEx(PyExc_DeprecationWarning,
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001696 "tostring() is deprecated. Use tobytes() instead.", 2) != 0)
1697 return NULL;
Brett Cannon1eb32c22014-10-10 16:26:45 -04001698 return array_array_tobytes_impl(self);
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001699}
1700
Brett Cannon1eb32c22014-10-10 16:26:45 -04001701/*[clinic input]
1702array.array.fromunicode
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001703
Larry Hastings38337d12015-05-07 23:30:09 -07001704 ustr: Py_UNICODE(zeroes=True)
Brett Cannon1eb32c22014-10-10 16:26:45 -04001705 /
1706
1707Extends this array with data from the unicode string ustr.
1708
1709The array must be a unicode type array; otherwise a ValueError is raised.
1710Use array.frombytes(ustr.encode(...)) to append Unicode data to an array of
1711some other type.
1712[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001713
Martin v. Löwis99866332002-03-01 10:27:01 +00001714static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001715array_array_fromunicode_impl(arrayobject *self, const Py_UNICODE *ustr,
Larry Hastings89964c42015-04-14 18:07:59 -04001716 Py_ssize_clean_t ustr_length)
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001717/*[clinic end generated code: output=cf2f662908e2befc input=150f00566ffbca6e]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001718{
Victor Stinner62bb3942012-08-06 00:46:05 +02001719 char typecode;
Martin v. Löwis99866332002-03-01 10:27:01 +00001720
Victor Stinner62bb3942012-08-06 00:46:05 +02001721 typecode = self->ob_descr->typecode;
Gregory P. Smith9504b132012-12-10 20:20:20 -08001722 if (typecode != 'u') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 PyErr_SetString(PyExc_ValueError,
1724 "fromunicode() may only be called on "
1725 "unicode type arrays");
1726 return NULL;
1727 }
Brett Cannon1eb32c22014-10-10 16:26:45 -04001728 if (ustr_length > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 Py_ssize_t old_size = Py_SIZE(self);
Brett Cannon1eb32c22014-10-10 16:26:45 -04001730 if (array_resize(self, old_size + ustr_length) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 return NULL;
Victor Stinner62bb3942012-08-06 00:46:05 +02001732 memcpy(self->ob_item + old_size * sizeof(Py_UNICODE),
Brett Cannon1eb32c22014-10-10 16:26:45 -04001733 ustr, ustr_length * sizeof(Py_UNICODE));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 }
Martin v. Löwis99866332002-03-01 10:27:01 +00001735
Brett Cannon1eb32c22014-10-10 16:26:45 -04001736 Py_RETURN_NONE;
Martin v. Löwis99866332002-03-01 10:27:01 +00001737}
1738
Brett Cannon1eb32c22014-10-10 16:26:45 -04001739/*[clinic input]
1740array.array.tounicode
Martin v. Löwis99866332002-03-01 10:27:01 +00001741
Brett Cannon1eb32c22014-10-10 16:26:45 -04001742Extends this array with data from the unicode string ustr.
1743
1744Convert the array to a unicode string. The array must be a unicode type array;
1745otherwise a ValueError is raised. Use array.tobytes().decode() to obtain a
1746unicode string from an array of some other type.
1747[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001748
1749static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001750array_array_tounicode_impl(arrayobject *self)
1751/*[clinic end generated code: output=08e442378336e1ef input=127242eebe70b66d]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001752{
Victor Stinner62bb3942012-08-06 00:46:05 +02001753 char typecode;
1754 typecode = self->ob_descr->typecode;
Gregory P. Smith9504b132012-12-10 20:20:20 -08001755 if (typecode != 'u') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 PyErr_SetString(PyExc_ValueError,
1757 "tounicode() may only be called on unicode type arrays");
1758 return NULL;
1759 }
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02001760 return PyUnicode_FromWideChar((Py_UNICODE *) self->ob_item, Py_SIZE(self));
Martin v. Löwis99866332002-03-01 10:27:01 +00001761}
1762
Brett Cannon1eb32c22014-10-10 16:26:45 -04001763/*[clinic input]
1764array.array.__sizeof__
Martin v. Löwis99866332002-03-01 10:27:01 +00001765
Brett Cannon1eb32c22014-10-10 16:26:45 -04001766Size of the array in memory, in bytes.
1767[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001768
Meador Inge03b4d502012-08-10 22:35:45 -05001769static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001770array_array___sizeof___impl(arrayobject *self)
1771/*[clinic end generated code: output=d8e1c61ebbe3eaed input=805586565bf2b3c6]*/
Meador Inge03b4d502012-08-10 22:35:45 -05001772{
1773 Py_ssize_t res;
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02001774 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * self->ob_descr->itemsize;
Meador Inge03b4d502012-08-10 22:35:45 -05001775 return PyLong_FromSsize_t(res);
1776}
1777
Martin v. Löwis99866332002-03-01 10:27:01 +00001778
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001779/*********************** Pickling support ************************/
1780
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001781static const struct mformatdescr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 size_t size;
1783 int is_signed;
1784 int is_big_endian;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001785} mformat_descriptors[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 {1, 0, 0}, /* 0: UNSIGNED_INT8 */
1787 {1, 1, 0}, /* 1: SIGNED_INT8 */
1788 {2, 0, 0}, /* 2: UNSIGNED_INT16_LE */
1789 {2, 0, 1}, /* 3: UNSIGNED_INT16_BE */
1790 {2, 1, 0}, /* 4: SIGNED_INT16_LE */
1791 {2, 1, 1}, /* 5: SIGNED_INT16_BE */
1792 {4, 0, 0}, /* 6: UNSIGNED_INT32_LE */
1793 {4, 0, 1}, /* 7: UNSIGNED_INT32_BE */
1794 {4, 1, 0}, /* 8: SIGNED_INT32_LE */
1795 {4, 1, 1}, /* 9: SIGNED_INT32_BE */
1796 {8, 0, 0}, /* 10: UNSIGNED_INT64_LE */
1797 {8, 0, 1}, /* 11: UNSIGNED_INT64_BE */
1798 {8, 1, 0}, /* 12: SIGNED_INT64_LE */
1799 {8, 1, 1}, /* 13: SIGNED_INT64_BE */
1800 {4, 0, 0}, /* 14: IEEE_754_FLOAT_LE */
1801 {4, 0, 1}, /* 15: IEEE_754_FLOAT_BE */
1802 {8, 0, 0}, /* 16: IEEE_754_DOUBLE_LE */
1803 {8, 0, 1}, /* 17: IEEE_754_DOUBLE_BE */
1804 {4, 0, 0}, /* 18: UTF16_LE */
1805 {4, 0, 1}, /* 19: UTF16_BE */
1806 {8, 0, 0}, /* 20: UTF32_LE */
1807 {8, 0, 1} /* 21: UTF32_BE */
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001808};
1809
1810
1811/*
1812 * Internal: This function is used to find the machine format of a given
1813 * array type code. This returns UNKNOWN_FORMAT when the machine format cannot
1814 * be found.
1815 */
1816static enum machine_format_code
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001817typecode_to_mformat_code(char typecode)
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001818{
Christian Heimes743e0cd2012-10-17 23:52:17 +02001819 const int is_big_endian = PY_BIG_ENDIAN;
1820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 size_t intsize;
1822 int is_signed;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 switch (typecode) {
1825 case 'b':
1826 return SIGNED_INT8;
1827 case 'B':
1828 return UNSIGNED_INT8;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 case 'u':
Victor Stinner62bb3942012-08-06 00:46:05 +02001831 if (sizeof(Py_UNICODE) == 2) {
1832 return UTF16_LE + is_big_endian;
1833 }
1834 if (sizeof(Py_UNICODE) == 4) {
1835 return UTF32_LE + is_big_endian;
1836 }
1837 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 case 'f':
1840 if (sizeof(float) == 4) {
1841 const float y = 16711938.0;
1842 if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0)
1843 return IEEE_754_FLOAT_BE;
1844 if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0)
1845 return IEEE_754_FLOAT_LE;
1846 }
1847 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 case 'd':
1850 if (sizeof(double) == 8) {
1851 const double x = 9006104071832581.0;
1852 if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0)
1853 return IEEE_754_DOUBLE_BE;
1854 if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
1855 return IEEE_754_DOUBLE_LE;
1856 }
1857 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 /* Integers */
1860 case 'h':
1861 intsize = sizeof(short);
1862 is_signed = 1;
1863 break;
1864 case 'H':
1865 intsize = sizeof(short);
1866 is_signed = 0;
1867 break;
1868 case 'i':
1869 intsize = sizeof(int);
1870 is_signed = 1;
1871 break;
1872 case 'I':
1873 intsize = sizeof(int);
1874 is_signed = 0;
1875 break;
1876 case 'l':
1877 intsize = sizeof(long);
1878 is_signed = 1;
1879 break;
1880 case 'L':
1881 intsize = sizeof(long);
1882 is_signed = 0;
1883 break;
Meador Inge1c9f0c92011-09-20 19:55:51 -05001884 case 'q':
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001885 intsize = sizeof(long long);
Meador Inge1c9f0c92011-09-20 19:55:51 -05001886 is_signed = 1;
1887 break;
1888 case 'Q':
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001889 intsize = sizeof(long long);
Meador Inge1c9f0c92011-09-20 19:55:51 -05001890 is_signed = 0;
1891 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 default:
1893 return UNKNOWN_FORMAT;
1894 }
1895 switch (intsize) {
1896 case 2:
1897 return UNSIGNED_INT16_LE + is_big_endian + (2 * is_signed);
1898 case 4:
1899 return UNSIGNED_INT32_LE + is_big_endian + (2 * is_signed);
1900 case 8:
1901 return UNSIGNED_INT64_LE + is_big_endian + (2 * is_signed);
1902 default:
1903 return UNKNOWN_FORMAT;
1904 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001905}
1906
1907/* Forward declaration. */
1908static PyObject *array_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
1909
1910/*
1911 * Internal: This function wraps the array constructor--i.e., array_new()--to
1912 * allow the creation of array objects from C code without having to deal
1913 * directly the tuple argument of array_new(). The typecode argument is a
1914 * Unicode character value, like 'i' or 'f' for example, representing an array
1915 * type code. The items argument is a bytes or a list object from which
1916 * contains the initial value of the array.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 *
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001918 * On success, this functions returns the array object created. Otherwise,
1919 * NULL is returned to indicate a failure.
1920 */
1921static PyObject *
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001922make_array(PyTypeObject *arraytype, char typecode, PyObject *items)
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001923{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 PyObject *new_args;
1925 PyObject *array_obj;
1926 PyObject *typecode_obj;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 assert(arraytype != NULL);
1929 assert(items != NULL);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001930
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001931 typecode_obj = PyUnicode_FromOrdinal(typecode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 if (typecode_obj == NULL)
1933 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 new_args = PyTuple_New(2);
Mat M56935a52017-11-14 01:00:54 -05001936 if (new_args == NULL) {
1937 Py_DECREF(typecode_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 return NULL;
Mat M56935a52017-11-14 01:00:54 -05001939 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 Py_INCREF(items);
1941 PyTuple_SET_ITEM(new_args, 0, typecode_obj);
1942 PyTuple_SET_ITEM(new_args, 1, items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 array_obj = array_new(arraytype, new_args, NULL);
1945 Py_DECREF(new_args);
1946 if (array_obj == NULL)
1947 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 return array_obj;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001950}
1951
1952/*
1953 * This functions is a special constructor used when unpickling an array. It
1954 * provides a portable way to rebuild an array from its memory representation.
1955 */
Brett Cannon1eb32c22014-10-10 16:26:45 -04001956/*[clinic input]
1957array._array_reconstructor
1958
1959 arraytype: object(type="PyTypeObject *")
Larry Hastingsdbfdc382015-05-04 06:59:46 -07001960 typecode: int(accept={str})
Brett Cannon1eb32c22014-10-10 16:26:45 -04001961 mformat_code: int(type="enum machine_format_code")
1962 items: object
1963 /
1964
1965Internal. Used for pickling support.
1966[clinic start generated code]*/
1967
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001968static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001969array__array_reconstructor_impl(PyObject *module, PyTypeObject *arraytype,
Larry Hastings89964c42015-04-14 18:07:59 -04001970 int typecode,
1971 enum machine_format_code mformat_code,
1972 PyObject *items)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001973/*[clinic end generated code: output=e05263141ba28365 input=2464dc8f4c7736b5]*/
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001974{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 PyObject *converted_items;
1976 PyObject *result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001977 const struct arraydescr *descr;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 if (!PyType_Check(arraytype)) {
1980 PyErr_Format(PyExc_TypeError,
Sylvaina90e64b2017-03-29 20:09:22 +02001981 "first argument must be a type object, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 Py_TYPE(arraytype)->tp_name);
1983 return NULL;
1984 }
1985 if (!PyType_IsSubtype(arraytype, &Arraytype)) {
1986 PyErr_Format(PyExc_TypeError,
1987 "%.200s is not a subtype of %.200s",
1988 arraytype->tp_name, Arraytype.tp_name);
1989 return NULL;
1990 }
1991 for (descr = descriptors; descr->typecode != '\0'; descr++) {
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001992 if ((int)descr->typecode == typecode)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001993 break;
1994 }
1995 if (descr->typecode == '\0') {
1996 PyErr_SetString(PyExc_ValueError,
1997 "second argument must be a valid type code");
1998 return NULL;
1999 }
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002000 if (mformat_code < MACHINE_FORMAT_CODE_MIN ||
2001 mformat_code > MACHINE_FORMAT_CODE_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002 PyErr_SetString(PyExc_ValueError,
2003 "third argument must be a valid machine format code.");
2004 return NULL;
2005 }
2006 if (!PyBytes_Check(items)) {
2007 PyErr_Format(PyExc_TypeError,
2008 "fourth argument should be bytes, not %.200s",
2009 Py_TYPE(items)->tp_name);
2010 return NULL;
2011 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 /* Fast path: No decoding has to be done. */
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002014 if (mformat_code == typecode_to_mformat_code((char)typecode) ||
2015 mformat_code == UNKNOWN_FORMAT) {
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002016 return make_array(arraytype, (char)typecode, items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 /* Slow path: Decode the byte string according to the given machine
2020 * format code. This occurs when the computer unpickling the array
2021 * object is architecturally different from the one that pickled the
2022 * array.
2023 */
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002024 if (Py_SIZE(items) % mformat_descriptors[mformat_code].size != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 PyErr_SetString(PyExc_ValueError,
2026 "string length not a multiple of item size");
2027 return NULL;
2028 }
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002029 switch (mformat_code) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 case IEEE_754_FLOAT_LE:
2031 case IEEE_754_FLOAT_BE: {
sthaa3ecb82019-03-20 20:49:39 +01002032 Py_ssize_t i;
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002033 int le = (mformat_code == IEEE_754_FLOAT_LE) ? 1 : 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 Py_ssize_t itemcount = Py_SIZE(items) / 4;
2035 const unsigned char *memstr =
2036 (unsigned char *)PyBytes_AS_STRING(items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 converted_items = PyList_New(itemcount);
2039 if (converted_items == NULL)
2040 return NULL;
2041 for (i = 0; i < itemcount; i++) {
2042 PyObject *pyfloat = PyFloat_FromDouble(
2043 _PyFloat_Unpack4(&memstr[i * 4], le));
2044 if (pyfloat == NULL) {
2045 Py_DECREF(converted_items);
2046 return NULL;
2047 }
2048 PyList_SET_ITEM(converted_items, i, pyfloat);
2049 }
2050 break;
2051 }
2052 case IEEE_754_DOUBLE_LE:
2053 case IEEE_754_DOUBLE_BE: {
sthaa3ecb82019-03-20 20:49:39 +01002054 Py_ssize_t i;
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002055 int le = (mformat_code == IEEE_754_DOUBLE_LE) ? 1 : 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 Py_ssize_t itemcount = Py_SIZE(items) / 8;
2057 const unsigned char *memstr =
2058 (unsigned char *)PyBytes_AS_STRING(items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 converted_items = PyList_New(itemcount);
2061 if (converted_items == NULL)
2062 return NULL;
2063 for (i = 0; i < itemcount; i++) {
2064 PyObject *pyfloat = PyFloat_FromDouble(
2065 _PyFloat_Unpack8(&memstr[i * 8], le));
2066 if (pyfloat == NULL) {
2067 Py_DECREF(converted_items);
2068 return NULL;
2069 }
2070 PyList_SET_ITEM(converted_items, i, pyfloat);
2071 }
2072 break;
2073 }
2074 case UTF16_LE:
2075 case UTF16_BE: {
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002076 int byteorder = (mformat_code == UTF16_LE) ? -1 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 converted_items = PyUnicode_DecodeUTF16(
2078 PyBytes_AS_STRING(items), Py_SIZE(items),
2079 "strict", &byteorder);
2080 if (converted_items == NULL)
2081 return NULL;
2082 break;
2083 }
2084 case UTF32_LE:
2085 case UTF32_BE: {
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002086 int byteorder = (mformat_code == UTF32_LE) ? -1 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 converted_items = PyUnicode_DecodeUTF32(
2088 PyBytes_AS_STRING(items), Py_SIZE(items),
2089 "strict", &byteorder);
2090 if (converted_items == NULL)
2091 return NULL;
2092 break;
2093 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 case UNSIGNED_INT8:
2096 case SIGNED_INT8:
2097 case UNSIGNED_INT16_LE:
2098 case UNSIGNED_INT16_BE:
2099 case SIGNED_INT16_LE:
2100 case SIGNED_INT16_BE:
2101 case UNSIGNED_INT32_LE:
2102 case UNSIGNED_INT32_BE:
2103 case SIGNED_INT32_LE:
2104 case SIGNED_INT32_BE:
2105 case UNSIGNED_INT64_LE:
2106 case UNSIGNED_INT64_BE:
2107 case SIGNED_INT64_LE:
2108 case SIGNED_INT64_BE: {
sthaa3ecb82019-03-20 20:49:39 +01002109 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 const struct mformatdescr mf_descr =
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002111 mformat_descriptors[mformat_code];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 Py_ssize_t itemcount = Py_SIZE(items) / mf_descr.size;
2113 const unsigned char *memstr =
2114 (unsigned char *)PyBytes_AS_STRING(items);
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002115 const struct arraydescr *descr;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002117 /* If possible, try to pack array's items using a data type
2118 * that fits better. This may result in an array with narrower
2119 * or wider elements.
2120 *
Martin Panter4c359642016-05-08 13:53:41 +00002121 * For example, if a 32-bit machine pickles an L-code array of
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 * unsigned longs, then the array will be unpickled by 64-bit
2123 * machine as an I-code array of unsigned ints.
2124 *
2125 * XXX: Is it possible to write a unit test for this?
2126 */
2127 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2128 if (descr->is_integer_type &&
Victor Stinner706768c2014-08-16 01:03:39 +02002129 (size_t)descr->itemsize == mf_descr.size &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 descr->is_signed == mf_descr.is_signed)
2131 typecode = descr->typecode;
2132 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002134 converted_items = PyList_New(itemcount);
2135 if (converted_items == NULL)
2136 return NULL;
2137 for (i = 0; i < itemcount; i++) {
2138 PyObject *pylong;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 pylong = _PyLong_FromByteArray(
2141 &memstr[i * mf_descr.size],
2142 mf_descr.size,
2143 !mf_descr.is_big_endian,
2144 mf_descr.is_signed);
2145 if (pylong == NULL) {
2146 Py_DECREF(converted_items);
2147 return NULL;
2148 }
2149 PyList_SET_ITEM(converted_items, i, pylong);
2150 }
2151 break;
2152 }
2153 case UNKNOWN_FORMAT:
2154 /* Impossible, but needed to shut up GCC about the unhandled
2155 * enumeration value.
2156 */
2157 default:
2158 PyErr_BadArgument();
2159 return NULL;
2160 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002161
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002162 result = make_array(arraytype, (char)typecode, converted_items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002163 Py_DECREF(converted_items);
2164 return result;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002165}
2166
Brett Cannon1eb32c22014-10-10 16:26:45 -04002167/*[clinic input]
2168array.array.__reduce_ex__
2169
2170 value: object
2171 /
2172
2173Return state information for pickling.
2174[clinic start generated code]*/
2175
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002176static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04002177array_array___reduce_ex__(arrayobject *self, PyObject *value)
2178/*[clinic end generated code: output=051e0a6175d0eddb input=c36c3f85de7df6cd]*/
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 PyObject *dict;
2181 PyObject *result;
2182 PyObject *array_str;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002183 int typecode = self->ob_descr->typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 int mformat_code;
2185 static PyObject *array_reconstructor = NULL;
2186 long protocol;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002187 _Py_IDENTIFIER(_array_reconstructor);
2188 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 if (array_reconstructor == NULL) {
2191 PyObject *array_module = PyImport_ImportModule("array");
2192 if (array_module == NULL)
2193 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002194 array_reconstructor = _PyObject_GetAttrId(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 array_module,
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002196 &PyId__array_reconstructor);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 Py_DECREF(array_module);
2198 if (array_reconstructor == NULL)
2199 return NULL;
2200 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 if (!PyLong_Check(value)) {
2203 PyErr_SetString(PyExc_TypeError,
Sylvaina90e64b2017-03-29 20:09:22 +02002204 "__reduce_ex__ argument should be an integer");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 return NULL;
2206 }
2207 protocol = PyLong_AsLong(value);
2208 if (protocol == -1 && PyErr_Occurred())
2209 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002210
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002211 if (_PyObject_LookupAttrId((PyObject *)self, &PyId___dict__, &dict) < 0) {
2212 return NULL;
2213 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215 dict = Py_None;
2216 Py_INCREF(dict);
2217 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 mformat_code = typecode_to_mformat_code(typecode);
2220 if (mformat_code == UNKNOWN_FORMAT || protocol < 3) {
2221 /* Convert the array to a list if we got something weird
2222 * (e.g., non-IEEE floats), or we are pickling the array using
2223 * a Python 2.x compatible protocol.
2224 *
2225 * It is necessary to use a list representation for Python 2.x
2226 * compatible pickle protocol, since Python 2's str objects
2227 * are unpickled as unicode by Python 3. Thus it is impossible
2228 * to make arrays unpicklable by Python 3 by using their memory
2229 * representation, unless we resort to ugly hacks such as
2230 * coercing unicode objects to bytes in array_reconstructor.
2231 */
2232 PyObject *list;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002233 list = array_array_tolist_impl(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 if (list == NULL) {
2235 Py_DECREF(dict);
2236 return NULL;
2237 }
2238 result = Py_BuildValue(
Brett Cannon1eb32c22014-10-10 16:26:45 -04002239 "O(CO)O", Py_TYPE(self), typecode, list, dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002240 Py_DECREF(list);
2241 Py_DECREF(dict);
2242 return result;
2243 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002244
Brett Cannon1eb32c22014-10-10 16:26:45 -04002245 array_str = array_array_tobytes_impl(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002246 if (array_str == NULL) {
2247 Py_DECREF(dict);
2248 return NULL;
2249 }
2250 result = Py_BuildValue(
Brett Cannon1eb32c22014-10-10 16:26:45 -04002251 "O(OCiN)O", array_reconstructor, Py_TYPE(self), typecode,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 mformat_code, array_str, dict);
2253 Py_DECREF(dict);
2254 return result;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002255}
2256
Martin v. Löwis99866332002-03-01 10:27:01 +00002257static PyObject *
2258array_get_typecode(arrayobject *a, void *closure)
2259{
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002260 char typecode = a->ob_descr->typecode;
2261 return PyUnicode_FromOrdinal(typecode);
Martin v. Löwis99866332002-03-01 10:27:01 +00002262}
2263
2264static PyObject *
2265array_get_itemsize(arrayobject *a, void *closure)
2266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 return PyLong_FromLong((long)a->ob_descr->itemsize);
Martin v. Löwis99866332002-03-01 10:27:01 +00002268}
2269
2270static PyGetSetDef array_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 {"typecode", (getter) array_get_typecode, NULL,
2272 "the typecode character used to create the array"},
2273 {"itemsize", (getter) array_get_itemsize, NULL,
2274 "the size, in bytes, of one array item"},
2275 {NULL}
Martin v. Löwis99866332002-03-01 10:27:01 +00002276};
2277
Martin v. Löwis59683e82008-06-13 07:50:45 +00002278static PyMethodDef array_methods[] = {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002279 ARRAY_ARRAY_APPEND_METHODDEF
2280 ARRAY_ARRAY_BUFFER_INFO_METHODDEF
2281 ARRAY_ARRAY_BYTESWAP_METHODDEF
2282 ARRAY_ARRAY___COPY___METHODDEF
2283 ARRAY_ARRAY_COUNT_METHODDEF
2284 ARRAY_ARRAY___DEEPCOPY___METHODDEF
2285 ARRAY_ARRAY_EXTEND_METHODDEF
2286 ARRAY_ARRAY_FROMFILE_METHODDEF
2287 ARRAY_ARRAY_FROMLIST_METHODDEF
2288 ARRAY_ARRAY_FROMSTRING_METHODDEF
2289 ARRAY_ARRAY_FROMBYTES_METHODDEF
2290 ARRAY_ARRAY_FROMUNICODE_METHODDEF
2291 ARRAY_ARRAY_INDEX_METHODDEF
2292 ARRAY_ARRAY_INSERT_METHODDEF
2293 ARRAY_ARRAY_POP_METHODDEF
2294 ARRAY_ARRAY___REDUCE_EX___METHODDEF
2295 ARRAY_ARRAY_REMOVE_METHODDEF
2296 ARRAY_ARRAY_REVERSE_METHODDEF
2297 ARRAY_ARRAY_TOFILE_METHODDEF
2298 ARRAY_ARRAY_TOLIST_METHODDEF
2299 ARRAY_ARRAY_TOSTRING_METHODDEF
2300 ARRAY_ARRAY_TOBYTES_METHODDEF
2301 ARRAY_ARRAY_TOUNICODE_METHODDEF
2302 ARRAY_ARRAY___SIZEOF___METHODDEF
2303 {NULL, NULL} /* sentinel */
Guido van Rossum778983b1993-02-19 15:55:02 +00002304};
2305
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002306static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00002307array_repr(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +00002308{
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002309 char typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 PyObject *s, *v = NULL;
2311 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00002312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 len = Py_SIZE(a);
2314 typecode = a->ob_descr->typecode;
2315 if (len == 0) {
Serhiy Storchakab3a77962017-09-21 14:24:13 +03002316 return PyUnicode_FromFormat("%s('%c')",
2317 _PyType_Name(Py_TYPE(a)), (int)typecode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 }
Gregory P. Smith9504b132012-12-10 20:20:20 -08002319 if (typecode == 'u') {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002320 v = array_array_tounicode_impl(a);
Gregory P. Smith9504b132012-12-10 20:20:20 -08002321 } else {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002322 v = array_array_tolist_impl(a);
Gregory P. Smith9504b132012-12-10 20:20:20 -08002323 }
Victor Stinner29ec5952013-02-26 00:27:38 +01002324 if (v == NULL)
2325 return NULL;
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00002326
Serhiy Storchakab3a77962017-09-21 14:24:13 +03002327 s = PyUnicode_FromFormat("%s('%c', %R)",
2328 _PyType_Name(Py_TYPE(a)), (int)typecode, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002329 Py_DECREF(v);
2330 return s;
Guido van Rossum778983b1993-02-19 15:55:02 +00002331}
2332
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002333static PyObject*
2334array_subscr(arrayobject* self, PyObject* item)
2335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 if (PyIndex_Check(item)) {
2337 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2338 if (i==-1 && PyErr_Occurred()) {
2339 return NULL;
2340 }
2341 if (i < 0)
2342 i += Py_SIZE(self);
2343 return array_item(self, i);
2344 }
2345 else if (PySlice_Check(item)) {
Zackery Spytz14514d92019-05-17 01:13:03 -06002346 Py_ssize_t start, stop, step, slicelength, i;
2347 size_t cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 PyObject* result;
2349 arrayobject* ar;
2350 int itemsize = self->ob_descr->itemsize;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002351
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002352 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 return NULL;
2354 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002355 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2356 step);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 if (slicelength <= 0) {
2359 return newarrayobject(&Arraytype, 0, self->ob_descr);
2360 }
2361 else if (step == 1) {
2362 PyObject *result = newarrayobject(&Arraytype,
2363 slicelength, self->ob_descr);
2364 if (result == NULL)
2365 return NULL;
2366 memcpy(((arrayobject *)result)->ob_item,
2367 self->ob_item + start * itemsize,
2368 slicelength * itemsize);
2369 return result;
2370 }
2371 else {
2372 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
2373 if (!result) return NULL;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 ar = (arrayobject*)result;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 for (cur = start, i = 0; i < slicelength;
2378 cur += step, i++) {
2379 memcpy(ar->ob_item + i*itemsize,
2380 self->ob_item + cur*itemsize,
2381 itemsize);
2382 }
2383
2384 return result;
2385 }
2386 }
2387 else {
2388 PyErr_SetString(PyExc_TypeError,
2389 "array indices must be integers");
2390 return NULL;
2391 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002392}
2393
2394static int
2395array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
2396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397 Py_ssize_t start, stop, step, slicelength, needed;
2398 arrayobject* other;
2399 int itemsize;
Thomas Woutersed03b412007-08-28 21:37:11 +00002400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 if (PyIndex_Check(item)) {
2402 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Alexandre Vassalotti47137252009-07-05 19:57:00 +00002403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 if (i == -1 && PyErr_Occurred())
2405 return -1;
2406 if (i < 0)
2407 i += Py_SIZE(self);
2408 if (i < 0 || i >= Py_SIZE(self)) {
2409 PyErr_SetString(PyExc_IndexError,
2410 "array assignment index out of range");
2411 return -1;
2412 }
2413 if (value == NULL) {
2414 /* Fall through to slice assignment */
2415 start = i;
2416 stop = i + 1;
2417 step = 1;
2418 slicelength = 1;
2419 }
2420 else
2421 return (*self->ob_descr->setitem)(self, i, value);
2422 }
2423 else if (PySlice_Check(item)) {
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002424 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 return -1;
2426 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002427 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2428 step);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 }
2430 else {
2431 PyErr_SetString(PyExc_TypeError,
Sylvaina90e64b2017-03-29 20:09:22 +02002432 "array indices must be integers");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 return -1;
2434 }
2435 if (value == NULL) {
2436 other = NULL;
2437 needed = 0;
2438 }
2439 else if (array_Check(value)) {
2440 other = (arrayobject *)value;
2441 needed = Py_SIZE(other);
2442 if (self == other) {
2443 /* Special case "self[i:j] = self" -- copy self first */
2444 int ret;
2445 value = array_slice(other, 0, needed);
2446 if (value == NULL)
2447 return -1;
2448 ret = array_ass_subscr(self, item, value);
2449 Py_DECREF(value);
2450 return ret;
2451 }
2452 if (other->ob_descr != self->ob_descr) {
2453 PyErr_BadArgument();
2454 return -1;
2455 }
2456 }
2457 else {
2458 PyErr_Format(PyExc_TypeError,
2459 "can only assign array (not \"%.200s\") to array slice",
2460 Py_TYPE(value)->tp_name);
2461 return -1;
2462 }
2463 itemsize = self->ob_descr->itemsize;
2464 /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
2465 if ((step > 0 && stop < start) ||
2466 (step < 0 && stop > start))
2467 stop = start;
Alexandre Vassalotti47137252009-07-05 19:57:00 +00002468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 /* Issue #4509: If the array has exported buffers and the slice
2470 assignment would change the size of the array, fail early to make
2471 sure we don't modify it. */
2472 if ((needed == 0 || slicelength != needed) && self->ob_exports > 0) {
2473 PyErr_SetString(PyExc_BufferError,
2474 "cannot resize an array that is exporting buffers");
2475 return -1;
2476 }
Mark Dickinsonbc099642010-01-29 17:27:24 +00002477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 if (step == 1) {
2479 if (slicelength > needed) {
2480 memmove(self->ob_item + (start + needed) * itemsize,
2481 self->ob_item + stop * itemsize,
2482 (Py_SIZE(self) - stop) * itemsize);
2483 if (array_resize(self, Py_SIZE(self) +
2484 needed - slicelength) < 0)
2485 return -1;
2486 }
2487 else if (slicelength < needed) {
2488 if (array_resize(self, Py_SIZE(self) +
2489 needed - slicelength) < 0)
2490 return -1;
2491 memmove(self->ob_item + (start + needed) * itemsize,
2492 self->ob_item + stop * itemsize,
2493 (Py_SIZE(self) - start - needed) * itemsize);
2494 }
2495 if (needed > 0)
2496 memcpy(self->ob_item + start * itemsize,
2497 other->ob_item, needed * itemsize);
2498 return 0;
2499 }
2500 else if (needed == 0) {
2501 /* Delete slice */
2502 size_t cur;
2503 Py_ssize_t i;
Thomas Woutersed03b412007-08-28 21:37:11 +00002504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 if (step < 0) {
2506 stop = start + 1;
2507 start = stop + step * (slicelength - 1) - 1;
2508 step = -step;
2509 }
2510 for (cur = start, i = 0; i < slicelength;
2511 cur += step, i++) {
2512 Py_ssize_t lim = step - 1;
Thomas Woutersed03b412007-08-28 21:37:11 +00002513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 if (cur + step >= (size_t)Py_SIZE(self))
2515 lim = Py_SIZE(self) - cur - 1;
2516 memmove(self->ob_item + (cur - i) * itemsize,
2517 self->ob_item + (cur + 1) * itemsize,
2518 lim * itemsize);
2519 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002520 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002521 if (cur < (size_t)Py_SIZE(self)) {
2522 memmove(self->ob_item + (cur-slicelength) * itemsize,
2523 self->ob_item + cur * itemsize,
2524 (Py_SIZE(self) - cur) * itemsize);
2525 }
2526 if (array_resize(self, Py_SIZE(self) - slicelength) < 0)
2527 return -1;
2528 return 0;
2529 }
2530 else {
Zackery Spytz14514d92019-05-17 01:13:03 -06002531 size_t cur;
2532 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533
2534 if (needed != slicelength) {
2535 PyErr_Format(PyExc_ValueError,
2536 "attempt to assign array of size %zd "
2537 "to extended slice of size %zd",
2538 needed, slicelength);
2539 return -1;
2540 }
2541 for (cur = start, i = 0; i < slicelength;
2542 cur += step, i++) {
2543 memcpy(self->ob_item + cur * itemsize,
2544 other->ob_item + i * itemsize,
2545 itemsize);
2546 }
2547 return 0;
2548 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002549}
2550
2551static PyMappingMethods array_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 (lenfunc)array_length,
2553 (binaryfunc)array_subscr,
2554 (objobjargproc)array_ass_subscr
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002555};
2556
Guido van Rossumd8faa362007-04-27 19:54:29 +00002557static const void *emptybuf = "";
2558
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002559
2560static int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00002561array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002562{
Stefan Krah650c1e82015-02-03 21:43:23 +01002563 if (view == NULL) {
2564 PyErr_SetString(PyExc_BufferError,
2565 "array_buffer_getbuf: view==NULL argument is obsolete");
2566 return -1;
2567 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002569 view->buf = (void *)self->ob_item;
2570 view->obj = (PyObject*)self;
2571 Py_INCREF(self);
2572 if (view->buf == NULL)
2573 view->buf = (void *)emptybuf;
2574 view->len = (Py_SIZE(self)) * self->ob_descr->itemsize;
2575 view->readonly = 0;
2576 view->ndim = 1;
2577 view->itemsize = self->ob_descr->itemsize;
2578 view->suboffsets = NULL;
2579 view->shape = NULL;
2580 if ((flags & PyBUF_ND)==PyBUF_ND) {
2581 view->shape = &((Py_SIZE(self)));
2582 }
2583 view->strides = NULL;
2584 if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES)
2585 view->strides = &(view->itemsize);
2586 view->format = NULL;
2587 view->internal = NULL;
Victor Stinner62bb3942012-08-06 00:46:05 +02002588 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002589 view->format = (char *)self->ob_descr->formats;
Victor Stinner62bb3942012-08-06 00:46:05 +02002590#ifdef Py_UNICODE_WIDE
2591 if (self->ob_descr->typecode == 'u') {
2592 view->format = "w";
2593 }
2594#endif
2595 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002597 self->ob_exports++;
2598 return 0;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002599}
2600
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002601static void
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00002602array_buffer_relbuf(arrayobject *self, Py_buffer *view)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002603{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002604 self->ob_exports--;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002605}
2606
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002607static PySequenceMethods array_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002608 (lenfunc)array_length, /*sq_length*/
2609 (binaryfunc)array_concat, /*sq_concat*/
2610 (ssizeargfunc)array_repeat, /*sq_repeat*/
2611 (ssizeargfunc)array_item, /*sq_item*/
2612 0, /*sq_slice*/
2613 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
2614 0, /*sq_ass_slice*/
2615 (objobjproc)array_contains, /*sq_contains*/
2616 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
2617 (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/
Guido van Rossum778983b1993-02-19 15:55:02 +00002618};
2619
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002620static PyBufferProcs array_as_buffer = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 (getbufferproc)array_buffer_getbuf,
2622 (releasebufferproc)array_buffer_relbuf
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002623};
2624
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002625static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +00002626array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum778983b1993-02-19 15:55:02 +00002627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002628 int c;
2629 PyObject *initial = NULL, *it = NULL;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002630 const struct arraydescr *descr;
Martin v. Löwis99866332002-03-01 10:27:01 +00002631
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03002632 if (type == &Arraytype && !_PyArg_NoKeywords("array.array", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002633 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00002634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002635 if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
2636 return NULL;
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00002637
Alexandre Vassalotti9730e332013-11-29 20:47:15 -08002638 if (initial && c != 'u') {
2639 if (PyUnicode_Check(initial)) {
2640 PyErr_Format(PyExc_TypeError, "cannot use a str to initialize "
2641 "an array with typecode '%c'", c);
2642 return NULL;
2643 }
2644 else if (array_Check(initial) &&
2645 ((arrayobject*)initial)->ob_descr->typecode == 'u') {
2646 PyErr_Format(PyExc_TypeError, "cannot use a unicode array to "
2647 "initialize an array with typecode '%c'", c);
2648 return NULL;
2649 }
2650 }
2651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002652 if (!(initial == NULL || PyList_Check(initial)
2653 || PyByteArray_Check(initial)
2654 || PyBytes_Check(initial)
2655 || PyTuple_Check(initial)
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002656 || ((c=='u') && PyUnicode_Check(initial))
2657 || (array_Check(initial)
2658 && c == ((arrayobject*)initial)->ob_descr->typecode))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002659 it = PyObject_GetIter(initial);
2660 if (it == NULL)
2661 return NULL;
2662 /* We set initial to NULL so that the subsequent code
2663 will create an empty array of the appropriate type
2664 and afterwards we can use array_iter_extend to populate
2665 the array.
2666 */
2667 initial = NULL;
2668 }
2669 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2670 if (descr->typecode == c) {
2671 PyObject *a;
2672 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00002673
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002674 if (initial == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 len = 0;
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002676 else if (PyList_Check(initial))
2677 len = PyList_GET_SIZE(initial);
2678 else if (PyTuple_Check(initial) || array_Check(initial))
2679 len = Py_SIZE(initial);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002680 else
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002681 len = 0;
Martin v. Löwis99866332002-03-01 10:27:01 +00002682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 a = newarrayobject(type, len, descr);
2684 if (a == NULL)
2685 return NULL;
2686
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002687 if (len > 0 && !array_Check(initial)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 Py_ssize_t i;
2689 for (i = 0; i < len; i++) {
2690 PyObject *v =
2691 PySequence_GetItem(initial, i);
2692 if (v == NULL) {
2693 Py_DECREF(a);
2694 return NULL;
2695 }
2696 if (setarrayitem(a, i, v) != 0) {
2697 Py_DECREF(v);
2698 Py_DECREF(a);
2699 return NULL;
2700 }
2701 Py_DECREF(v);
2702 }
2703 }
2704 else if (initial != NULL && (PyByteArray_Check(initial) ||
2705 PyBytes_Check(initial))) {
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03002706 PyObject *v;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002707 v = array_array_frombytes((arrayobject *)a,
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03002708 initial);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002709 if (v == NULL) {
2710 Py_DECREF(a);
2711 return NULL;
2712 }
2713 Py_DECREF(v);
2714 }
2715 else if (initial != NULL && PyUnicode_Check(initial)) {
Victor Stinner62bb3942012-08-06 00:46:05 +02002716 Py_UNICODE *ustr;
Victor Stinner1fbcaef2011-09-30 01:54:04 +02002717 Py_ssize_t n;
Victor Stinner62bb3942012-08-06 00:46:05 +02002718
2719 ustr = PyUnicode_AsUnicode(initial);
2720 if (ustr == NULL) {
2721 PyErr_NoMemory();
Victor Stinner1fbcaef2011-09-30 01:54:04 +02002722 Py_DECREF(a);
2723 return NULL;
2724 }
Victor Stinner62bb3942012-08-06 00:46:05 +02002725
2726 n = PyUnicode_GET_DATA_SIZE(initial);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002727 if (n > 0) {
2728 arrayobject *self = (arrayobject *)a;
Victor Stinner62bb3942012-08-06 00:46:05 +02002729 char *item = self->ob_item;
2730 item = (char *)PyMem_Realloc(item, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002731 if (item == NULL) {
2732 PyErr_NoMemory();
2733 Py_DECREF(a);
2734 return NULL;
2735 }
Victor Stinner62bb3942012-08-06 00:46:05 +02002736 self->ob_item = item;
2737 Py_SIZE(self) = n / sizeof(Py_UNICODE);
2738 memcpy(item, ustr, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002739 self->allocated = Py_SIZE(self);
2740 }
2741 }
Benjamin Peterson682124c2014-10-10 20:58:30 -04002742 else if (initial != NULL && array_Check(initial) && len > 0) {
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002743 arrayobject *self = (arrayobject *)a;
2744 arrayobject *other = (arrayobject *)initial;
2745 memcpy(self->ob_item, other->ob_item, len * other->ob_descr->itemsize);
2746 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 if (it != NULL) {
2748 if (array_iter_extend((arrayobject *)a, it) == -1) {
2749 Py_DECREF(it);
2750 Py_DECREF(a);
2751 return NULL;
2752 }
2753 Py_DECREF(it);
2754 }
2755 return a;
2756 }
2757 }
2758 PyErr_SetString(PyExc_ValueError,
Meador Inge1c9f0c92011-09-20 19:55:51 -05002759 "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 +00002760 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00002761}
2762
Guido van Rossum778983b1993-02-19 15:55:02 +00002763
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002764PyDoc_STRVAR(module_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00002765"This module defines an object type which can efficiently represent\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002766an array of basic values: characters, integers, floating point\n\
2767numbers. Arrays are sequence types and behave very much like lists,\n\
Alexandre Vassalotti9730e332013-11-29 20:47:15 -08002768except that the type of objects stored in them is constrained.\n");
2769
2770PyDoc_STRVAR(arraytype_doc,
2771"array(typecode [, initializer]) -> array\n\
2772\n\
2773Return a new array whose items are restricted by typecode, and\n\
2774initialized from the optional initializer value, which must be a list,\n\
2775string or iterable over elements of the appropriate type.\n\
2776\n\
2777Arrays represent basic values and behave very much like lists, except\n\
2778the type of objects stored in them is constrained. The type is specified\n\
2779at object creation time by using a type code, which is a single character.\n\
2780The following type codes are defined:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002781\n\
oldkaa0735f2018-02-02 16:52:55 +08002782 Type code C Type Minimum size in bytes\n\
2783 'b' signed integer 1\n\
2784 'B' unsigned integer 1\n\
2785 'u' Unicode character 2 (see note)\n\
2786 'h' signed integer 2\n\
2787 'H' unsigned integer 2\n\
2788 'i' signed integer 2\n\
2789 'I' unsigned integer 2\n\
2790 'l' signed integer 4\n\
2791 'L' unsigned integer 4\n\
2792 'q' signed integer 8 (see note)\n\
2793 'Q' unsigned integer 8 (see note)\n\
2794 'f' floating point 4\n\
2795 'd' floating point 8\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002796\n\
oldkaa0735f2018-02-02 16:52:55 +08002797NOTE: The 'u' typecode corresponds to Python's unicode character. On\n\
Victor Stinner62bb3942012-08-06 00:46:05 +02002798narrow builds this is 2-bytes on wide builds this is 4-bytes.\n\
2799\n\
oldkaa0735f2018-02-02 16:52:55 +08002800NOTE: The 'q' and 'Q' type codes are only available if the platform\n\
2801C compiler used to build Python supports 'long long', or, on Windows,\n\
Meador Inge1c9f0c92011-09-20 19:55:51 -05002802'__int64'.\n\
2803\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002804Methods:\n\
2805\n\
2806append() -- append a new item to the end of the array\n\
2807buffer_info() -- return information giving the current memory info\n\
2808byteswap() -- byteswap all the items of the array\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002809count() -- return number of occurrences of an object\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00002810extend() -- extend array by appending multiple elements from an iterable\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002811fromfile() -- read items from a file object\n\
2812fromlist() -- append items from the list\n\
Florent Xiclunac45fb252011-10-24 13:14:55 +02002813frombytes() -- append items from the string\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002814index() -- return index of first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002815insert() -- insert a new item into the array at a provided position\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00002816pop() -- remove and return item (default last)\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002817remove() -- remove first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002818reverse() -- reverse the order of the items in the array\n\
2819tofile() -- write all items to a file object\n\
2820tolist() -- return the array converted to an ordinary list\n\
Florent Xiclunac45fb252011-10-24 13:14:55 +02002821tobytes() -- return the array converted to a string\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002822\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002823Attributes:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002824\n\
2825typecode -- the typecode character used to create the array\n\
2826itemsize -- the length in bytes of one array item\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002827");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002828
Raymond Hettinger625812f2003-01-07 01:58:52 +00002829static PyObject *array_iter(arrayobject *ao);
2830
Tim Peters0c322792002-07-17 16:49:03 +00002831static PyTypeObject Arraytype = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 PyVarObject_HEAD_INIT(NULL, 0)
2833 "array.array",
2834 sizeof(arrayobject),
2835 0,
2836 (destructor)array_dealloc, /* tp_dealloc */
2837 0, /* tp_print */
2838 0, /* tp_getattr */
2839 0, /* tp_setattr */
2840 0, /* tp_reserved */
2841 (reprfunc)array_repr, /* tp_repr */
2842 0, /* tp_as_number*/
2843 &array_as_sequence, /* tp_as_sequence*/
2844 &array_as_mapping, /* tp_as_mapping*/
2845 0, /* tp_hash */
2846 0, /* tp_call */
2847 0, /* tp_str */
2848 PyObject_GenericGetAttr, /* tp_getattro */
2849 0, /* tp_setattro */
2850 &array_as_buffer, /* tp_as_buffer*/
2851 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2852 arraytype_doc, /* tp_doc */
2853 0, /* tp_traverse */
2854 0, /* tp_clear */
2855 array_richcompare, /* tp_richcompare */
2856 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
2857 (getiterfunc)array_iter, /* tp_iter */
2858 0, /* tp_iternext */
2859 array_methods, /* tp_methods */
2860 0, /* tp_members */
2861 array_getsets, /* tp_getset */
2862 0, /* tp_base */
2863 0, /* tp_dict */
2864 0, /* tp_descr_get */
2865 0, /* tp_descr_set */
2866 0, /* tp_dictoffset */
2867 0, /* tp_init */
2868 PyType_GenericAlloc, /* tp_alloc */
2869 array_new, /* tp_new */
2870 PyObject_Del, /* tp_free */
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002871};
2872
Raymond Hettinger625812f2003-01-07 01:58:52 +00002873
2874/*********************** Array Iterator **************************/
2875
Brett Cannon1eb32c22014-10-10 16:26:45 -04002876/*[clinic input]
2877class array.arrayiterator "arrayiterobject *" "&PyArrayIter_Type"
2878[clinic start generated code]*/
2879/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5aefd2d74d8c8e30]*/
Raymond Hettinger625812f2003-01-07 01:58:52 +00002880
2881static PyObject *
2882array_iter(arrayobject *ao)
2883{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 arrayiterobject *it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 if (!array_Check(ao)) {
2887 PyErr_BadInternalCall();
2888 return NULL;
2889 }
Raymond Hettinger625812f2003-01-07 01:58:52 +00002890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
2892 if (it == NULL)
2893 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 Py_INCREF(ao);
2896 it->ao = ao;
2897 it->index = 0;
2898 it->getitem = ao->ob_descr->getitem;
2899 PyObject_GC_Track(it);
2900 return (PyObject *)it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002901}
2902
2903static PyObject *
Raymond Hettinger625812f2003-01-07 01:58:52 +00002904arrayiter_next(arrayiterobject *it)
2905{
Serhiy Storchakaab0d1982016-03-30 21:11:16 +03002906 arrayobject *ao;
2907
2908 assert(it != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 assert(PyArrayIter_Check(it));
Serhiy Storchakaab0d1982016-03-30 21:11:16 +03002910 ao = it->ao;
2911 if (ao == NULL) {
2912 return NULL;
2913 }
2914 assert(array_Check(ao));
2915 if (it->index < Py_SIZE(ao)) {
2916 return (*it->getitem)(ao, it->index++);
2917 }
2918 it->ao = NULL;
2919 Py_DECREF(ao);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002921}
2922
2923static void
2924arrayiter_dealloc(arrayiterobject *it)
2925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 PyObject_GC_UnTrack(it);
2927 Py_XDECREF(it->ao);
2928 PyObject_GC_Del(it);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002929}
2930
2931static int
2932arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2933{
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
Raymond Hettinger625812f2003-01-07 01:58:52 +00002986static PyTypeObject PyArrayIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987 PyVarObject_HEAD_INIT(NULL, 0)
2988 "arrayiterator", /* tp_name */
2989 sizeof(arrayiterobject), /* tp_basicsize */
2990 0, /* tp_itemsize */
2991 /* methods */
2992 (destructor)arrayiter_dealloc, /* tp_dealloc */
2993 0, /* tp_print */
2994 0, /* tp_getattr */
2995 0, /* tp_setattr */
2996 0, /* tp_reserved */
2997 0, /* tp_repr */
2998 0, /* tp_as_number */
2999 0, /* tp_as_sequence */
3000 0, /* tp_as_mapping */
3001 0, /* tp_hash */
3002 0, /* tp_call */
3003 0, /* tp_str */
3004 PyObject_GenericGetAttr, /* tp_getattro */
3005 0, /* tp_setattro */
3006 0, /* tp_as_buffer */
3007 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3008 0, /* tp_doc */
3009 (traverseproc)arrayiter_traverse, /* tp_traverse */
3010 0, /* tp_clear */
3011 0, /* tp_richcompare */
3012 0, /* tp_weaklistoffset */
3013 PyObject_SelfIter, /* tp_iter */
3014 (iternextfunc)arrayiter_next, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003015 arrayiter_methods, /* tp_methods */
Raymond Hettinger625812f2003-01-07 01:58:52 +00003016};
3017
3018
3019/*********************** Install Module **************************/
3020
Martin v. Löwis99866332002-03-01 10:27:01 +00003021/* No functions in array module. */
3022static PyMethodDef a_methods[] = {
Brett Cannon1eb32c22014-10-10 16:26:45 -04003023 ARRAY__ARRAY_RECONSTRUCTOR_METHODDEF
Martin v. Löwis99866332002-03-01 10:27:01 +00003024 {NULL, NULL, 0, NULL} /* Sentinel */
3025};
3026
Nick Coghland5cacbb2015-05-23 22:24:10 +10003027static int
3028array_modexec(PyObject *m)
Guido van Rossum778983b1993-02-19 15:55:02 +00003029{
Georg Brandl4cb0de22011-09-28 21:49:49 +02003030 char buffer[Py_ARRAY_LENGTH(descriptors)], *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 PyObject *typecodes;
3032 Py_ssize_t size = 0;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02003033 const struct arraydescr *descr;
Fred Drake0d40ba42000-02-04 20:33:49 +00003034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003035 if (PyType_Ready(&Arraytype) < 0)
Nick Coghland5cacbb2015-05-23 22:24:10 +10003036 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 Py_TYPE(&PyArrayIter_Type) = &PyType_Type;
Fred Drakef4e34842002-04-01 03:45:06 +00003038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003039 Py_INCREF((PyObject *)&Arraytype);
3040 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
3041 Py_INCREF((PyObject *)&Arraytype);
3042 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00003043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003044 for (descr=descriptors; descr->typecode != '\0'; descr++) {
3045 size++;
3046 }
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00003047
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003048 p = buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 for (descr = descriptors; descr->typecode != '\0'; descr++) {
3050 *p++ = (char)descr->typecode;
3051 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003052 typecodes = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003053
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003054 PyModule_AddObject(m, "typecodes", typecodes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055
3056 if (PyErr_Occurred()) {
3057 Py_DECREF(m);
3058 m = NULL;
3059 }
Nick Coghland5cacbb2015-05-23 22:24:10 +10003060 return 0;
3061}
3062
3063static PyModuleDef_Slot arrayslots[] = {
3064 {Py_mod_exec, array_modexec},
3065 {0, NULL}
3066};
3067
3068
3069static struct PyModuleDef arraymodule = {
3070 PyModuleDef_HEAD_INIT,
3071 "array",
3072 module_doc,
3073 0,
3074 a_methods,
3075 arrayslots,
3076 NULL,
3077 NULL,
3078 NULL
3079};
3080
3081
3082PyMODINIT_FUNC
3083PyInit_array(void)
3084{
3085 return PyModuleDef_Init(&arraymodule);
Guido van Rossum778983b1993-02-19 15:55:02 +00003086}