blob: 5ba261819db2cef374c92db5d5208c8d25cabba7 [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 */
Hai Shi06cd5b62019-10-21 14:31:46 +080046 Py_ssize_t 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{
Disconnect3d13ab5702019-07-11 23:57:42 +0200188 long x = ((signed char *)ap->ob_item)[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 return PyLong_FromLong(x);
Guido van Rossum778983b1993-02-19 15:55:02 +0000190}
191
192static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000193b_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 short x;
196 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
197 must use the next size up that is signed ('h') and manually do
198 the overflow checking */
199 if (!PyArg_Parse(v, "h;array item must be integer", &x))
200 return -1;
201 else if (x < -128) {
202 PyErr_SetString(PyExc_OverflowError,
203 "signed char is less than minimum");
204 return -1;
205 }
206 else if (x > 127) {
207 PyErr_SetString(PyExc_OverflowError,
208 "signed char is greater than maximum");
209 return -1;
210 }
211 if (i >= 0)
212 ((char *)ap->ob_item)[i] = (char)x;
213 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000214}
215
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000216static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000217BB_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 long x = ((unsigned char *)ap->ob_item)[i];
220 return PyLong_FromLong(x);
Guido van Rossum549ab711997-01-03 19:09:47 +0000221}
222
Fred Drake541dc3b2000-06-28 17:49:30 +0000223static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000224BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 unsigned char x;
227 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
228 if (!PyArg_Parse(v, "b;array item must be integer", &x))
229 return -1;
230 if (i >= 0)
231 ((char *)ap->ob_item)[i] = x;
232 return 0;
Fred Drake541dc3b2000-06-28 17:49:30 +0000233}
Guido van Rossum549ab711997-01-03 19:09:47 +0000234
Martin v. Löwis99866332002-03-01 10:27:01 +0000235static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000236u_getitem(arrayobject *ap, Py_ssize_t i)
Martin v. Löwis99866332002-03-01 10:27:01 +0000237{
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200238 return PyUnicode_FromOrdinal(((Py_UNICODE *) ap->ob_item)[i]);
Martin v. Löwis99866332002-03-01 10:27:01 +0000239}
240
241static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000242u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Martin v. Löwis99866332002-03-01 10:27:01 +0000243{
Victor Stinner62bb3942012-08-06 00:46:05 +0200244 Py_UNICODE *p;
245 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +0000246
Victor Stinner62bb3942012-08-06 00:46:05 +0200247 if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 return -1;
Victor Stinner62bb3942012-08-06 00:46:05 +0200249 if (len != 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 PyErr_SetString(PyExc_TypeError,
251 "array item must be unicode character");
252 return -1;
253 }
254 if (i >= 0)
Victor Stinner62bb3942012-08-06 00:46:05 +0200255 ((Py_UNICODE *)ap->ob_item)[i] = p[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 return 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000257}
Martin v. Löwis99866332002-03-01 10:27:01 +0000258
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +0000259
Guido van Rossum549ab711997-01-03 19:09:47 +0000260static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000261h_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 return PyLong_FromLong((long) ((short *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000264}
265
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +0000266
Guido van Rossum778983b1993-02-19 15:55:02 +0000267static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000268h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 short x;
271 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
272 if (!PyArg_Parse(v, "h;array item must be integer", &x))
273 return -1;
274 if (i >= 0)
275 ((short *)ap->ob_item)[i] = x;
276 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000277}
278
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000279static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000280HH_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 return PyLong_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000283}
284
Fred Drake541dc3b2000-06-28 17:49:30 +0000285static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000286HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 int x;
289 /* PyArg_Parse's 'h' formatter is for a signed short, therefore
290 must use the next size up and manually do the overflow checking */
291 if (!PyArg_Parse(v, "i;array item must be integer", &x))
292 return -1;
293 else if (x < 0) {
294 PyErr_SetString(PyExc_OverflowError,
295 "unsigned short is less than minimum");
296 return -1;
297 }
298 else if (x > USHRT_MAX) {
299 PyErr_SetString(PyExc_OverflowError,
300 "unsigned short is greater than maximum");
301 return -1;
302 }
303 if (i >= 0)
304 ((short *)ap->ob_item)[i] = (short)x;
305 return 0;
Fred Drake541dc3b2000-06-28 17:49:30 +0000306}
Guido van Rossum549ab711997-01-03 19:09:47 +0000307
308static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000309i_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossume77a7571993-11-03 15:01:26 +0000310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 return PyLong_FromLong((long) ((int *)ap->ob_item)[i]);
Guido van Rossume77a7571993-11-03 15:01:26 +0000312}
313
314static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000315i_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossume77a7571993-11-03 15:01:26 +0000316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 int x;
318 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
319 if (!PyArg_Parse(v, "i;array item must be integer", &x))
320 return -1;
321 if (i >= 0)
322 ((int *)ap->ob_item)[i] = x;
323 return 0;
Guido van Rossume77a7571993-11-03 15:01:26 +0000324}
325
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000326static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000327II_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 return PyLong_FromUnsignedLong(
330 (unsigned long) ((unsigned int *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000331}
332
orenmn964281a2017-03-09 11:35:28 +0200333static PyObject *
334get_int_unless_float(PyObject *v)
335{
336 if (PyFloat_Check(v)) {
337 PyErr_SetString(PyExc_TypeError,
338 "array item must be integer");
339 return NULL;
340 }
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200341 return _PyLong_FromNbIndexOrNbInt(v);
orenmn964281a2017-03-09 11:35:28 +0200342}
343
Guido van Rossum549ab711997-01-03 19:09:47 +0000344static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000345II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 unsigned long x;
orenmn964281a2017-03-09 11:35:28 +0200348 int do_decref = 0; /* if nb_int was called */
349
350 if (!PyLong_Check(v)) {
351 v = get_int_unless_float(v);
352 if (NULL == v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 return -1;
354 }
orenmn964281a2017-03-09 11:35:28 +0200355 do_decref = 1;
356 }
357 x = PyLong_AsUnsignedLong(v);
358 if (x == (unsigned long)-1 && PyErr_Occurred()) {
359 if (do_decref) {
360 Py_DECREF(v);
361 }
362 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 }
364 if (x > UINT_MAX) {
365 PyErr_SetString(PyExc_OverflowError,
orenmn964281a2017-03-09 11:35:28 +0200366 "unsigned int is greater than maximum");
367 if (do_decref) {
368 Py_DECREF(v);
369 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 return -1;
371 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 if (i >= 0)
373 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
orenmn964281a2017-03-09 11:35:28 +0200374
375 if (do_decref) {
376 Py_DECREF(v);
377 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 return 0;
Guido van Rossum549ab711997-01-03 19:09:47 +0000379}
380
381static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000382l_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000383{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 return PyLong_FromLong(((long *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000385}
386
387static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000388l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000389{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 long x;
391 if (!PyArg_Parse(v, "l;array item must be integer", &x))
392 return -1;
393 if (i >= 0)
394 ((long *)ap->ob_item)[i] = x;
395 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000396}
397
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000398static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000399LL_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000402}
403
404static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000405LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000406{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 unsigned long x;
orenmn964281a2017-03-09 11:35:28 +0200408 int do_decref = 0; /* if nb_int was called */
409
410 if (!PyLong_Check(v)) {
411 v = get_int_unless_float(v);
412 if (NULL == v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 return -1;
414 }
orenmn964281a2017-03-09 11:35:28 +0200415 do_decref = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 }
orenmn964281a2017-03-09 11:35:28 +0200417 x = PyLong_AsUnsignedLong(v);
418 if (x == (unsigned long)-1 && PyErr_Occurred()) {
419 if (do_decref) {
420 Py_DECREF(v);
421 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 return -1;
423 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 if (i >= 0)
425 ((unsigned long *)ap->ob_item)[i] = x;
orenmn964281a2017-03-09 11:35:28 +0200426
427 if (do_decref) {
428 Py_DECREF(v);
429 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 return 0;
Guido van Rossum549ab711997-01-03 19:09:47 +0000431}
432
Meador Inge1c9f0c92011-09-20 19:55:51 -0500433static PyObject *
434q_getitem(arrayobject *ap, Py_ssize_t i)
435{
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700436 return PyLong_FromLongLong(((long long *)ap->ob_item)[i]);
Meador Inge1c9f0c92011-09-20 19:55:51 -0500437}
438
439static int
440q_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
441{
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700442 long long x;
Meador Inge1c9f0c92011-09-20 19:55:51 -0500443 if (!PyArg_Parse(v, "L;array item must be integer", &x))
444 return -1;
445 if (i >= 0)
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700446 ((long long *)ap->ob_item)[i] = x;
Meador Inge1c9f0c92011-09-20 19:55:51 -0500447 return 0;
448}
449
450static PyObject *
451QQ_getitem(arrayobject *ap, Py_ssize_t i)
452{
453 return PyLong_FromUnsignedLongLong(
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700454 ((unsigned long long *)ap->ob_item)[i]);
Meador Inge1c9f0c92011-09-20 19:55:51 -0500455}
456
457static int
458QQ_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
459{
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700460 unsigned long long x;
orenmn964281a2017-03-09 11:35:28 +0200461 int do_decref = 0; /* if nb_int was called */
462
463 if (!PyLong_Check(v)) {
464 v = get_int_unless_float(v);
465 if (NULL == v) {
Meador Inge1c9f0c92011-09-20 19:55:51 -0500466 return -1;
467 }
orenmn964281a2017-03-09 11:35:28 +0200468 do_decref = 1;
Meador Inge1c9f0c92011-09-20 19:55:51 -0500469 }
orenmn964281a2017-03-09 11:35:28 +0200470 x = PyLong_AsUnsignedLongLong(v);
471 if (x == (unsigned long long)-1 && PyErr_Occurred()) {
472 if (do_decref) {
473 Py_DECREF(v);
474 }
475 return -1;
476 }
Meador Inge1c9f0c92011-09-20 19:55:51 -0500477 if (i >= 0)
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700478 ((unsigned long long *)ap->ob_item)[i] = x;
orenmn964281a2017-03-09 11:35:28 +0200479
480 if (do_decref) {
481 Py_DECREF(v);
482 }
Meador Inge1c9f0c92011-09-20 19:55:51 -0500483 return 0;
484}
Meador Inge1c9f0c92011-09-20 19:55:51 -0500485
Guido van Rossum549ab711997-01-03 19:09:47 +0000486static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000487f_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000490}
491
492static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000493f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 float x;
496 if (!PyArg_Parse(v, "f;array item must be float", &x))
497 return -1;
498 if (i >= 0)
499 ((float *)ap->ob_item)[i] = x;
500 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000501}
502
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000503static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000504d_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000505{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000507}
508
509static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000510d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 double x;
513 if (!PyArg_Parse(v, "d;array item must be float", &x))
514 return -1;
515 if (i >= 0)
516 ((double *)ap->ob_item)[i] = x;
517 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000518}
519
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000520#define DEFINE_COMPAREITEMS(code, type) \
521 static int \
522 code##_compareitems(const void *lhs, const void *rhs, Py_ssize_t length) \
523 { \
524 const type *a = lhs, *b = rhs; \
525 for (Py_ssize_t i = 0; i < length; ++i) \
526 if (a[i] != b[i]) \
527 return a[i] < b[i] ? -1 : 1; \
528 return 0; \
529 }
530
531DEFINE_COMPAREITEMS(b, signed char)
532DEFINE_COMPAREITEMS(BB, unsigned char)
533DEFINE_COMPAREITEMS(u, Py_UNICODE)
534DEFINE_COMPAREITEMS(h, short)
535DEFINE_COMPAREITEMS(HH, unsigned short)
536DEFINE_COMPAREITEMS(i, int)
537DEFINE_COMPAREITEMS(II, unsigned int)
538DEFINE_COMPAREITEMS(l, long)
539DEFINE_COMPAREITEMS(LL, unsigned long)
540DEFINE_COMPAREITEMS(q, long long)
541DEFINE_COMPAREITEMS(QQ, unsigned long long)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000542
Alexandre Vassalottiad077152009-07-15 17:49:23 +0000543/* Description of types.
544 *
545 * Don't forget to update typecode_to_mformat_code() if you add a new
546 * typecode.
547 */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200548static const struct arraydescr descriptors[] = {
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000549 {'b', 1, b_getitem, b_setitem, b_compareitems, "b", 1, 1},
550 {'B', 1, BB_getitem, BB_setitem, BB_compareitems, "B", 1, 0},
551 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem, u_compareitems, "u", 0, 0},
552 {'h', sizeof(short), h_getitem, h_setitem, h_compareitems, "h", 1, 1},
553 {'H', sizeof(short), HH_getitem, HH_setitem, HH_compareitems, "H", 1, 0},
554 {'i', sizeof(int), i_getitem, i_setitem, i_compareitems, "i", 1, 1},
555 {'I', sizeof(int), II_getitem, II_setitem, II_compareitems, "I", 1, 0},
556 {'l', sizeof(long), l_getitem, l_setitem, l_compareitems, "l", 1, 1},
557 {'L', sizeof(long), LL_getitem, LL_setitem, LL_compareitems, "L", 1, 0},
558 {'q', sizeof(long long), q_getitem, q_setitem, q_compareitems, "q", 1, 1},
559 {'Q', sizeof(long long), QQ_getitem, QQ_setitem, QQ_compareitems, "Q", 1, 0},
560 {'f', sizeof(float), f_getitem, f_setitem, NULL, "f", 0, 0},
561 {'d', sizeof(double), d_getitem, d_setitem, NULL, "d", 0, 0},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 {'\0', 0, 0, 0, 0, 0, 0} /* Sentinel */
Guido van Rossum778983b1993-02-19 15:55:02 +0000563};
Tim Petersbb307342000-09-10 05:22:54 +0000564
565/****************************************************************************
566Implementations of array object methods.
567****************************************************************************/
Brett Cannon1eb32c22014-10-10 16:26:45 -0400568/*[clinic input]
569class array.array "arrayobject *" "&Arraytype"
570[clinic start generated code]*/
571/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ad43d37e942a8854]*/
Guido van Rossum778983b1993-02-19 15:55:02 +0000572
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000573static PyObject *
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200574newarrayobject(PyTypeObject *type, Py_ssize_t size, const struct arraydescr *descr)
Guido van Rossum778983b1993-02-19 15:55:02 +0000575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 arrayobject *op;
577 size_t nbytes;
Martin v. Löwis99866332002-03-01 10:27:01 +0000578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 if (size < 0) {
580 PyErr_BadInternalCall();
581 return NULL;
582 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 /* Check for overflow */
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100585 if (size > PY_SSIZE_T_MAX / descr->itemsize) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 return PyErr_NoMemory();
587 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100588 nbytes = size * descr->itemsize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 op = (arrayobject *) type->tp_alloc(type, 0);
590 if (op == NULL) {
591 return NULL;
592 }
593 op->ob_descr = descr;
594 op->allocated = size;
595 op->weakreflist = NULL;
596 Py_SIZE(op) = size;
597 if (size <= 0) {
598 op->ob_item = NULL;
599 }
600 else {
601 op->ob_item = PyMem_NEW(char, nbytes);
602 if (op->ob_item == NULL) {
603 Py_DECREF(op);
604 return PyErr_NoMemory();
605 }
606 }
607 op->ob_exports = 0;
608 return (PyObject *) op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000609}
610
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000611static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000612getarrayitem(PyObject *op, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000613{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200614 arrayobject *ap;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 assert(array_Check(op));
616 ap = (arrayobject *)op;
617 assert(i>=0 && i<Py_SIZE(ap));
618 return (*ap->ob_descr->getitem)(ap, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000619}
620
621static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000622ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000623{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 char *items;
625 Py_ssize_t n = Py_SIZE(self);
626 if (v == NULL) {
627 PyErr_BadInternalCall();
628 return -1;
629 }
630 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
631 return -1;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 if (array_resize(self, n+1) == -1)
634 return -1;
635 items = self->ob_item;
636 if (where < 0) {
637 where += n;
638 if (where < 0)
639 where = 0;
640 }
641 if (where > n)
642 where = n;
643 /* appends don't need to call memmove() */
644 if (where != n)
645 memmove(items + (where+1)*self->ob_descr->itemsize,
646 items + where*self->ob_descr->itemsize,
647 (n-where)*self->ob_descr->itemsize);
648 return (*self->ob_descr->setitem)(self, where, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000649}
650
Guido van Rossum778983b1993-02-19 15:55:02 +0000651/* Methods */
652
653static void
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000654array_dealloc(arrayobject *op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000655{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 if (op->weakreflist != NULL)
657 PyObject_ClearWeakRefs((PyObject *) op);
658 if (op->ob_item != NULL)
659 PyMem_DEL(op->ob_item);
660 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000661}
662
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000663static PyObject *
664array_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000665{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 arrayobject *va, *wa;
667 PyObject *vi = NULL;
668 PyObject *wi = NULL;
669 Py_ssize_t i, k;
670 PyObject *res;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000671
Brian Curtindfc80e32011-08-10 20:28:54 -0500672 if (!array_Check(v) || !array_Check(w))
673 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 va = (arrayobject *)v;
676 wa = (arrayobject *)w;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 if (Py_SIZE(va) != Py_SIZE(wa) && (op == Py_EQ || op == Py_NE)) {
679 /* Shortcut: if the lengths differ, the arrays differ */
680 if (op == Py_EQ)
681 res = Py_False;
682 else
683 res = Py_True;
684 Py_INCREF(res);
685 return res;
686 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000687
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000688 if (va->ob_descr == wa->ob_descr && va->ob_descr->compareitems != NULL) {
689 /* Fast path:
690 arrays with same types can have their buffers compared directly */
691 Py_ssize_t common_length = Py_MIN(Py_SIZE(va), Py_SIZE(wa));
692 int result = va->ob_descr->compareitems(va->ob_item, wa->ob_item,
693 common_length);
694 if (result == 0)
695 goto compare_sizes;
696
697 int cmp;
698 switch (op) {
699 case Py_LT: cmp = result < 0; break;
700 case Py_LE: cmp = result <= 0; break;
701 case Py_EQ: cmp = result == 0; break;
702 case Py_NE: cmp = result != 0; break;
703 case Py_GT: cmp = result > 0; break;
704 case Py_GE: cmp = result >= 0; break;
705 default: return NULL; /* cannot happen */
706 }
707 PyObject *res = cmp ? Py_True : Py_False;
708 Py_INCREF(res);
709 return res;
710 }
711
712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 /* Search for the first index where items are different */
714 k = 1;
715 for (i = 0; i < Py_SIZE(va) && i < Py_SIZE(wa); i++) {
716 vi = getarrayitem(v, i);
717 wi = getarrayitem(w, i);
718 if (vi == NULL || wi == NULL) {
719 Py_XDECREF(vi);
720 Py_XDECREF(wi);
721 return NULL;
722 }
723 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
724 if (k == 0)
725 break; /* Keeping vi and wi alive! */
726 Py_DECREF(vi);
727 Py_DECREF(wi);
728 if (k < 0)
729 return NULL;
730 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 if (k) {
733 /* No more items to compare -- compare sizes */
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000734 compare_sizes: ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 Py_ssize_t vs = Py_SIZE(va);
736 Py_ssize_t ws = Py_SIZE(wa);
737 int cmp;
738 switch (op) {
739 case Py_LT: cmp = vs < ws; break;
740 case Py_LE: cmp = vs <= ws; break;
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000741 /* If the lengths were not equal,
742 the earlier fast-path check would have caught that. */
743 case Py_EQ: assert(vs == ws); cmp = 1; break;
744 case Py_NE: assert(vs == ws); cmp = 0; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 case Py_GT: cmp = vs > ws; break;
746 case Py_GE: cmp = vs >= ws; break;
747 default: return NULL; /* cannot happen */
748 }
749 if (cmp)
750 res = Py_True;
751 else
752 res = Py_False;
753 Py_INCREF(res);
754 return res;
755 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 /* We have an item that differs. First, shortcuts for EQ/NE */
758 if (op == Py_EQ) {
759 Py_INCREF(Py_False);
760 res = Py_False;
761 }
762 else if (op == Py_NE) {
763 Py_INCREF(Py_True);
764 res = Py_True;
765 }
766 else {
767 /* Compare the final item again using the proper operator */
768 res = PyObject_RichCompare(vi, wi, op);
769 }
770 Py_DECREF(vi);
771 Py_DECREF(wi);
772 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +0000773}
774
Martin v. Löwis18e16552006-02-15 17:27:45 +0000775static Py_ssize_t
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000776array_length(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +0000777{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 return Py_SIZE(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000779}
780
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000781static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000782array_item(arrayobject *a, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 if (i < 0 || i >= Py_SIZE(a)) {
785 PyErr_SetString(PyExc_IndexError, "array index out of range");
786 return NULL;
787 }
788 return getarrayitem((PyObject *)a, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000789}
790
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000791static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000792array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 arrayobject *np;
795 if (ilow < 0)
796 ilow = 0;
797 else if (ilow > Py_SIZE(a))
798 ilow = Py_SIZE(a);
799 if (ihigh < 0)
800 ihigh = 0;
801 if (ihigh < ilow)
802 ihigh = ilow;
803 else if (ihigh > Py_SIZE(a))
804 ihigh = Py_SIZE(a);
805 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
806 if (np == NULL)
807 return NULL;
Martin Panterbe8da9c2016-09-07 11:04:41 +0000808 if (ihigh > ilow) {
809 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
810 (ihigh-ilow) * a->ob_descr->itemsize);
811 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000813}
814
Brett Cannon1eb32c22014-10-10 16:26:45 -0400815
816/*[clinic input]
817array.array.__copy__
818
819Return a copy of the array.
820[clinic start generated code]*/
821
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000822static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -0400823array_array___copy___impl(arrayobject *self)
824/*[clinic end generated code: output=dec7c3f925d9619e input=ad1ee5b086965f09]*/
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000825{
Brett Cannon1eb32c22014-10-10 16:26:45 -0400826 return array_slice(self, 0, Py_SIZE(self));
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000827}
828
Brett Cannon1eb32c22014-10-10 16:26:45 -0400829/*[clinic input]
830array.array.__deepcopy__
831
832 unused: object
833 /
834
835Return a copy of the array.
836[clinic start generated code]*/
837
838static PyObject *
839array_array___deepcopy__(arrayobject *self, PyObject *unused)
840/*[clinic end generated code: output=1ec748d8e14a9faa input=2405ecb4933748c4]*/
841{
842 return array_array___copy___impl(self);
843}
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000844
845static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000846array_concat(arrayobject *a, PyObject *bb)
Guido van Rossum778983b1993-02-19 15:55:02 +0000847{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 Py_ssize_t size;
849 arrayobject *np;
850 if (!array_Check(bb)) {
851 PyErr_Format(PyExc_TypeError,
852 "can only append array (not \"%.200s\") to array",
853 Py_TYPE(bb)->tp_name);
854 return NULL;
855 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000856#define b ((arrayobject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 if (a->ob_descr != b->ob_descr) {
858 PyErr_BadArgument();
859 return NULL;
860 }
861 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) {
862 return PyErr_NoMemory();
863 }
864 size = Py_SIZE(a) + Py_SIZE(b);
865 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
866 if (np == NULL) {
867 return NULL;
868 }
Martin Panterbe8da9c2016-09-07 11:04:41 +0000869 if (Py_SIZE(a) > 0) {
870 memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize);
871 }
872 if (Py_SIZE(b) > 0) {
873 memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize,
874 b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
875 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000877#undef b
878}
879
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000880static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000881array_repeat(arrayobject *a, Py_ssize_t n)
Guido van Rossum778983b1993-02-19 15:55:02 +0000882{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 Py_ssize_t size;
884 arrayobject *np;
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000885 Py_ssize_t oldbytes, newbytes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 if (n < 0)
887 n = 0;
888 if ((Py_SIZE(a) != 0) && (n > PY_SSIZE_T_MAX / Py_SIZE(a))) {
889 return PyErr_NoMemory();
890 }
891 size = Py_SIZE(a) * n;
892 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
893 if (np == NULL)
894 return NULL;
Martin Panterbe8da9c2016-09-07 11:04:41 +0000895 if (size == 0)
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000896 return (PyObject *)np;
897 oldbytes = Py_SIZE(a) * a->ob_descr->itemsize;
898 newbytes = oldbytes * n;
899 /* this follows the code in unicode_repeat */
900 if (oldbytes == 1) {
901 memset(np->ob_item, a->ob_item[0], newbytes);
902 } else {
903 Py_ssize_t done = oldbytes;
Christian Heimesf051e432016-09-13 20:22:02 +0200904 memcpy(np->ob_item, a->ob_item, oldbytes);
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000905 while (done < newbytes) {
906 Py_ssize_t ncopy = (done <= newbytes-done) ? done : newbytes-done;
Christian Heimesf051e432016-09-13 20:22:02 +0200907 memcpy(np->ob_item+done, np->ob_item, ncopy);
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000908 done += ncopy;
909 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 }
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000911 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000912}
913
914static int
Martin Panter996d72b2016-07-25 02:21:14 +0000915array_del_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 char *item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 Py_ssize_t d; /* Change in size */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 if (ilow < 0)
920 ilow = 0;
921 else if (ilow > Py_SIZE(a))
922 ilow = Py_SIZE(a);
923 if (ihigh < 0)
924 ihigh = 0;
925 if (ihigh < ilow)
926 ihigh = ilow;
927 else if (ihigh > Py_SIZE(a))
928 ihigh = Py_SIZE(a);
929 item = a->ob_item;
Martin Panter996d72b2016-07-25 02:21:14 +0000930 d = ihigh-ilow;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 /* Issue #4509: If the array has exported buffers and the slice
932 assignment would change the size of the array, fail early to make
933 sure we don't modify it. */
934 if (d != 0 && a->ob_exports > 0) {
935 PyErr_SetString(PyExc_BufferError,
936 "cannot resize an array that is exporting buffers");
937 return -1;
938 }
Martin Panter996d72b2016-07-25 02:21:14 +0000939 if (d > 0) { /* Delete d items */
940 memmove(item + (ihigh-d)*a->ob_descr->itemsize,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 item + ihigh*a->ob_descr->itemsize,
942 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
Martin Panter996d72b2016-07-25 02:21:14 +0000943 if (array_resize(a, Py_SIZE(a) - d) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 return -1;
945 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000947}
948
949static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000950array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000951{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 if (i < 0 || i >= Py_SIZE(a)) {
953 PyErr_SetString(PyExc_IndexError,
954 "array assignment index out of range");
955 return -1;
956 }
957 if (v == NULL)
Martin Panter996d72b2016-07-25 02:21:14 +0000958 return array_del_slice(a, i, i+1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 return (*a->ob_descr->setitem)(a, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000960}
961
962static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000963setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000964{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 assert(array_Check(a));
966 return array_ass_item((arrayobject *)a, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000967}
968
Martin v. Löwis99866332002-03-01 10:27:01 +0000969static int
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000970array_iter_extend(arrayobject *self, PyObject *bb)
971{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 PyObject *it, *v;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 it = PyObject_GetIter(bb);
975 if (it == NULL)
976 return -1;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 while ((v = PyIter_Next(it)) != NULL) {
Mark Dickinson346f0af2010-08-06 09:36:57 +0000979 if (ins1(self, Py_SIZE(self), v) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 Py_DECREF(v);
981 Py_DECREF(it);
982 return -1;
983 }
984 Py_DECREF(v);
985 }
986 Py_DECREF(it);
987 if (PyErr_Occurred())
988 return -1;
989 return 0;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000990}
991
992static int
Martin v. Löwis99866332002-03-01 10:27:01 +0000993array_do_extend(arrayobject *self, PyObject *bb)
994{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 Py_ssize_t size, oldsize, bbsize;
Martin v. Löwis99866332002-03-01 10:27:01 +0000996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 if (!array_Check(bb))
998 return array_iter_extend(self, bb);
999#define b ((arrayobject *)bb)
1000 if (self->ob_descr != b->ob_descr) {
1001 PyErr_SetString(PyExc_TypeError,
1002 "can only extend with array of same kind");
1003 return -1;
1004 }
1005 if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) ||
1006 ((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
1007 PyErr_NoMemory();
1008 return -1;
1009 }
1010 oldsize = Py_SIZE(self);
1011 /* Get the size of bb before resizing the array since bb could be self. */
1012 bbsize = Py_SIZE(bb);
1013 size = oldsize + Py_SIZE(b);
1014 if (array_resize(self, size) == -1)
1015 return -1;
Martin Panterbe8da9c2016-09-07 11:04:41 +00001016 if (bbsize > 0) {
1017 memcpy(self->ob_item + oldsize * self->ob_descr->itemsize,
1018 b->ob_item, bbsize * b->ob_descr->itemsize);
1019 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020
1021 return 0;
Martin v. Löwis99866332002-03-01 10:27:01 +00001022#undef b
1023}
1024
1025static PyObject *
1026array_inplace_concat(arrayobject *self, PyObject *bb)
1027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 if (!array_Check(bb)) {
1029 PyErr_Format(PyExc_TypeError,
1030 "can only extend array with array (not \"%.200s\")",
1031 Py_TYPE(bb)->tp_name);
1032 return NULL;
1033 }
1034 if (array_do_extend(self, bb) == -1)
1035 return NULL;
1036 Py_INCREF(self);
1037 return (PyObject *)self;
Martin v. Löwis99866332002-03-01 10:27:01 +00001038}
1039
1040static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001041array_inplace_repeat(arrayobject *self, Py_ssize_t n)
Martin v. Löwis99866332002-03-01 10:27:01 +00001042{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 char *items, *p;
1044 Py_ssize_t size, i;
Martin v. Löwis99866332002-03-01 10:27:01 +00001045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 if (Py_SIZE(self) > 0) {
1047 if (n < 0)
1048 n = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 if ((self->ob_descr->itemsize != 0) &&
1050 (Py_SIZE(self) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
1051 return PyErr_NoMemory();
1052 }
1053 size = Py_SIZE(self) * self->ob_descr->itemsize;
1054 if (n > 0 && size > PY_SSIZE_T_MAX / n) {
1055 return PyErr_NoMemory();
1056 }
1057 if (array_resize(self, n * Py_SIZE(self)) == -1)
1058 return NULL;
1059 items = p = self->ob_item;
1060 for (i = 1; i < n; i++) {
1061 p += size;
1062 memcpy(p, items, size);
1063 }
1064 }
1065 Py_INCREF(self);
1066 return (PyObject *)self;
Martin v. Löwis99866332002-03-01 10:27:01 +00001067}
1068
1069
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001070static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001071ins(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +00001072{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 if (ins1(self, where, v) != 0)
1074 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001075 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001076}
1077
Brett Cannon1eb32c22014-10-10 16:26:45 -04001078/*[clinic input]
1079array.array.count
1080
1081 v: object
1082 /
1083
1084Return number of occurrences of v in the array.
1085[clinic start generated code]*/
1086
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001087static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001088array_array_count(arrayobject *self, PyObject *v)
1089/*[clinic end generated code: output=3dd3624bf7135a3a input=d9bce9d65e39d1f5]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001090{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 Py_ssize_t count = 0;
1092 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 for (i = 0; i < Py_SIZE(self); i++) {
Victor Stinner0b142e22013-07-17 23:01:30 +02001095 PyObject *selfi;
1096 int cmp;
1097
1098 selfi = getarrayitem((PyObject *)self, i);
1099 if (selfi == NULL)
1100 return NULL;
1101 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 Py_DECREF(selfi);
1103 if (cmp > 0)
1104 count++;
1105 else if (cmp < 0)
1106 return NULL;
1107 }
1108 return PyLong_FromSsize_t(count);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001109}
1110
Brett Cannon1eb32c22014-10-10 16:26:45 -04001111
1112/*[clinic input]
1113array.array.index
1114
1115 v: object
1116 /
1117
1118Return index of first occurrence of v in the array.
1119[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001120
1121static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001122array_array_index(arrayobject *self, PyObject *v)
1123/*[clinic end generated code: output=d48498d325602167 input=cf619898c6649d08]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 for (i = 0; i < Py_SIZE(self); i++) {
Victor Stinner0b142e22013-07-17 23:01:30 +02001128 PyObject *selfi;
1129 int cmp;
1130
1131 selfi = getarrayitem((PyObject *)self, i);
1132 if (selfi == NULL)
1133 return NULL;
1134 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 Py_DECREF(selfi);
1136 if (cmp > 0) {
1137 return PyLong_FromLong((long)i);
1138 }
1139 else if (cmp < 0)
1140 return NULL;
1141 }
Jim Fasarakis-Hilliarda4095ef2017-05-29 20:43:39 +03001142 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in array");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 return NULL;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001144}
1145
Raymond Hettinger625812f2003-01-07 01:58:52 +00001146static int
1147array_contains(arrayobject *self, PyObject *v)
1148{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 Py_ssize_t i;
1150 int cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +00001151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) {
1153 PyObject *selfi = getarrayitem((PyObject *)self, i);
Victor Stinner0b142e22013-07-17 23:01:30 +02001154 if (selfi == NULL)
Victor Stinner4755bea2013-07-18 01:12:35 +02001155 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
1157 Py_DECREF(selfi);
1158 }
1159 return cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +00001160}
1161
Brett Cannon1eb32c22014-10-10 16:26:45 -04001162/*[clinic input]
1163array.array.remove
1164
1165 v: object
1166 /
1167
1168Remove the first occurrence of v in the array.
1169[clinic start generated code]*/
1170
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001171static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001172array_array_remove(arrayobject *self, PyObject *v)
1173/*[clinic end generated code: output=bef06be9fdf9dceb input=0b1e5aed25590027]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001174{
sthaa3ecb82019-03-20 20:49:39 +01001175 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 for (i = 0; i < Py_SIZE(self); i++) {
Victor Stinner0b142e22013-07-17 23:01:30 +02001178 PyObject *selfi;
1179 int cmp;
1180
1181 selfi = getarrayitem((PyObject *)self,i);
1182 if (selfi == NULL)
1183 return NULL;
1184 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 Py_DECREF(selfi);
1186 if (cmp > 0) {
Martin Panter996d72b2016-07-25 02:21:14 +00001187 if (array_del_slice(self, i, i+1) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001189 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 }
1191 else if (cmp < 0)
1192 return NULL;
1193 }
Jim Fasarakis-Hilliarda4095ef2017-05-29 20:43:39 +03001194 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in array");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 return NULL;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001196}
1197
Brett Cannon1eb32c22014-10-10 16:26:45 -04001198/*[clinic input]
1199array.array.pop
1200
1201 i: Py_ssize_t = -1
1202 /
1203
1204Return the i-th element and delete it from the array.
1205
1206i defaults to -1.
1207[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001208
1209static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001210array_array_pop_impl(arrayobject *self, Py_ssize_t i)
1211/*[clinic end generated code: output=bc1f0c54fe5308e4 input=8e5feb4c1a11cd44]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 PyObject *v;
Brett Cannon1eb32c22014-10-10 16:26:45 -04001214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 if (Py_SIZE(self) == 0) {
1216 /* Special-case most common failure cause */
1217 PyErr_SetString(PyExc_IndexError, "pop from empty array");
1218 return NULL;
1219 }
1220 if (i < 0)
1221 i += Py_SIZE(self);
1222 if (i < 0 || i >= Py_SIZE(self)) {
1223 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1224 return NULL;
1225 }
Victor Stinner0b142e22013-07-17 23:01:30 +02001226 v = getarrayitem((PyObject *)self, i);
1227 if (v == NULL)
1228 return NULL;
Martin Panter996d72b2016-07-25 02:21:14 +00001229 if (array_del_slice(self, i, i+1) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 Py_DECREF(v);
1231 return NULL;
1232 }
1233 return v;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001234}
1235
Brett Cannon1eb32c22014-10-10 16:26:45 -04001236/*[clinic input]
1237array.array.extend
1238
1239 bb: object
1240 /
1241
1242Append items to the end of the array.
1243[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001244
1245static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001246array_array_extend(arrayobject *self, PyObject *bb)
1247/*[clinic end generated code: output=bbddbc8e8bef871d input=43be86aba5c31e44]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 if (array_do_extend(self, bb) == -1)
1250 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001251 Py_RETURN_NONE;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001252}
1253
Brett Cannon1eb32c22014-10-10 16:26:45 -04001254/*[clinic input]
1255array.array.insert
1256
1257 i: Py_ssize_t
1258 v: object
1259 /
1260
1261Insert a new item v into the array before position i.
1262[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001263
1264static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001265array_array_insert_impl(arrayobject *self, Py_ssize_t i, PyObject *v)
1266/*[clinic end generated code: output=5a3648e278348564 input=5577d1b4383e9313]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 return ins(self, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001269}
1270
Brett Cannon1eb32c22014-10-10 16:26:45 -04001271/*[clinic input]
1272array.array.buffer_info
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001273
Brett Cannon1eb32c22014-10-10 16:26:45 -04001274Return a tuple (address, length) giving the current memory address and the length in items of the buffer used to hold array's contents.
1275
1276The length should be multiplied by the itemsize attribute to calculate
1277the buffer length in bytes.
1278[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001279
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001280static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001281array_array_buffer_info_impl(arrayobject *self)
1282/*[clinic end generated code: output=9b2a4ec3ae7e98e7 input=a58bae5c6e1ac6a6]*/
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001283{
Victor Stinner541067a2013-11-14 01:27:12 +01001284 PyObject *retval = NULL, *v;
1285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 retval = PyTuple_New(2);
1287 if (!retval)
1288 return NULL;
Fred Drake541dc3b2000-06-28 17:49:30 +00001289
Victor Stinner541067a2013-11-14 01:27:12 +01001290 v = PyLong_FromVoidPtr(self->ob_item);
1291 if (v == NULL) {
1292 Py_DECREF(retval);
1293 return NULL;
1294 }
1295 PyTuple_SET_ITEM(retval, 0, v);
1296
Serhiy Storchaka9e941d62016-06-23 23:55:34 +03001297 v = PyLong_FromSsize_t(Py_SIZE(self));
Victor Stinner541067a2013-11-14 01:27:12 +01001298 if (v == NULL) {
1299 Py_DECREF(retval);
1300 return NULL;
1301 }
1302 PyTuple_SET_ITEM(retval, 1, v);
Fred Drake541dc3b2000-06-28 17:49:30 +00001303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 return retval;
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001305}
1306
Brett Cannon1eb32c22014-10-10 16:26:45 -04001307/*[clinic input]
1308array.array.append
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001309
Brett Cannon1eb32c22014-10-10 16:26:45 -04001310 v: object
1311 /
1312
1313Append new value v to the end of the array.
1314[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001315
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001316static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001317array_array_append(arrayobject *self, PyObject *v)
1318/*[clinic end generated code: output=745a0669bf8db0e2 input=0b98d9d78e78f0fa]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001319{
Mark Dickinson346f0af2010-08-06 09:36:57 +00001320 return ins(self, Py_SIZE(self), v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001321}
1322
Brett Cannon1eb32c22014-10-10 16:26:45 -04001323/*[clinic input]
1324array.array.byteswap
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001325
Brett Cannon1eb32c22014-10-10 16:26:45 -04001326Byteswap all items of the array.
1327
1328If the items in the array are not 1, 2, 4, or 8 bytes in size, RuntimeError is
1329raised.
1330[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001331
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001332static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001333array_array_byteswap_impl(arrayobject *self)
1334/*[clinic end generated code: output=5f8236cbdf0d90b5 input=6a85591b950a0186]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 char *p;
1337 Py_ssize_t i;
Fred Drakebf272981999-12-03 17:15:30 +00001338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 switch (self->ob_descr->itemsize) {
1340 case 1:
1341 break;
1342 case 2:
1343 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) {
1344 char p0 = p[0];
1345 p[0] = p[1];
1346 p[1] = p0;
1347 }
1348 break;
1349 case 4:
1350 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 4) {
1351 char p0 = p[0];
1352 char p1 = p[1];
1353 p[0] = p[3];
1354 p[1] = p[2];
1355 p[2] = p1;
1356 p[3] = p0;
1357 }
1358 break;
1359 case 8:
1360 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) {
1361 char p0 = p[0];
1362 char p1 = p[1];
1363 char p2 = p[2];
1364 char p3 = p[3];
1365 p[0] = p[7];
1366 p[1] = p[6];
1367 p[2] = p[5];
1368 p[3] = p[4];
1369 p[4] = p3;
1370 p[5] = p2;
1371 p[6] = p1;
1372 p[7] = p0;
1373 }
1374 break;
1375 default:
1376 PyErr_SetString(PyExc_RuntimeError,
1377 "don't know how to byteswap this array type");
1378 return NULL;
1379 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001380 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001381}
1382
Brett Cannon1eb32c22014-10-10 16:26:45 -04001383/*[clinic input]
1384array.array.reverse
1385
1386Reverse the order of the items in the array.
1387[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001388
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001389static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001390array_array_reverse_impl(arrayobject *self)
1391/*[clinic end generated code: output=c04868b36f6f4089 input=cd904f01b27d966a]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001392{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001393 Py_ssize_t itemsize = self->ob_descr->itemsize;
1394 char *p, *q;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 /* little buffer to hold items while swapping */
1396 char tmp[256]; /* 8 is probably enough -- but why skimp */
1397 assert((size_t)itemsize <= sizeof(tmp));
Guido van Rossume77a7571993-11-03 15:01:26 +00001398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 if (Py_SIZE(self) > 1) {
1400 for (p = self->ob_item,
1401 q = self->ob_item + (Py_SIZE(self) - 1)*itemsize;
1402 p < q;
1403 p += itemsize, q -= itemsize) {
1404 /* memory areas guaranteed disjoint, so memcpy
1405 * is safe (& memmove may be slower).
1406 */
1407 memcpy(tmp, p, itemsize);
1408 memcpy(p, q, itemsize);
1409 memcpy(q, tmp, itemsize);
1410 }
1411 }
Tim Petersbb307342000-09-10 05:22:54 +00001412
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001413 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001414}
Guido van Rossume77a7571993-11-03 15:01:26 +00001415
Brett Cannon1eb32c22014-10-10 16:26:45 -04001416/*[clinic input]
1417array.array.fromfile
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001418
Brett Cannon1eb32c22014-10-10 16:26:45 -04001419 f: object
1420 n: Py_ssize_t
1421 /
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001422
Brett Cannon1eb32c22014-10-10 16:26:45 -04001423Read n objects from the file object f and append them to the end of the array.
1424[clinic start generated code]*/
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001425
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001426static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001427array_array_fromfile_impl(arrayobject *self, PyObject *f, Py_ssize_t n)
1428/*[clinic end generated code: output=ec9f600e10f53510 input=e188afe8e58adf40]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001429{
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03001430 PyObject *b, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 Py_ssize_t itemsize = self->ob_descr->itemsize;
Brett Cannon1eb32c22014-10-10 16:26:45 -04001432 Py_ssize_t nbytes;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001433 _Py_IDENTIFIER(read);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 int not_enough_bytes;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001435
Mark Dickinsonc04ddff2012-10-06 18:04:49 +01001436 if (n < 0) {
1437 PyErr_SetString(PyExc_ValueError, "negative count");
1438 return NULL;
1439 }
1440 if (n > PY_SSIZE_T_MAX / itemsize) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 PyErr_NoMemory();
1442 return NULL;
1443 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +01001444 nbytes = n * itemsize;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001445
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001446 b = _PyObject_CallMethodId(f, &PyId_read, "n", nbytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 if (b == NULL)
1448 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 if (!PyBytes_Check(b)) {
1451 PyErr_SetString(PyExc_TypeError,
1452 "read() didn't return bytes");
1453 Py_DECREF(b);
1454 return NULL;
1455 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 not_enough_bytes = (PyBytes_GET_SIZE(b) != nbytes);
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001458
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03001459 res = array_array_frombytes(self, b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 Py_DECREF(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 if (res == NULL)
1462 return NULL;
Hirokazu Yamamoto54d0df62009-03-06 03:04:07 +00001463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 if (not_enough_bytes) {
1465 PyErr_SetString(PyExc_EOFError,
1466 "read() didn't return enough bytes");
1467 Py_DECREF(res);
1468 return NULL;
1469 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +00001472}
1473
Brett Cannon1eb32c22014-10-10 16:26:45 -04001474/*[clinic input]
1475array.array.tofile
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001476
Brett Cannon1eb32c22014-10-10 16:26:45 -04001477 f: object
1478 /
1479
1480Write all items (as machine values) to the file object f.
1481[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001482
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001483static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001484array_array_tofile(arrayobject *self, PyObject *f)
1485/*[clinic end generated code: output=3a2cfa8128df0777 input=b0669a484aab0831]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize;
1488 /* Write 64K blocks at a time */
1489 /* XXX Make the block size settable */
1490 int BLOCKSIZE = 64*1024;
1491 Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE;
1492 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 if (Py_SIZE(self) == 0)
1495 goto done;
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 for (i = 0; i < nblocks; i++) {
1498 char* ptr = self->ob_item + i*BLOCKSIZE;
1499 Py_ssize_t size = BLOCKSIZE;
1500 PyObject *bytes, *res;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001501 _Py_IDENTIFIER(write);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 if (i*BLOCKSIZE + size > nbytes)
1504 size = nbytes - i*BLOCKSIZE;
1505 bytes = PyBytes_FromStringAndSize(ptr, size);
1506 if (bytes == NULL)
1507 return NULL;
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001508 res = _PyObject_CallMethodIdOneArg(f, &PyId_write, bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 Py_DECREF(bytes);
1510 if (res == NULL)
1511 return NULL;
1512 Py_DECREF(res); /* drop write result */
1513 }
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001514
1515 done:
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001516 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001517}
1518
Brett Cannon1eb32c22014-10-10 16:26:45 -04001519/*[clinic input]
1520array.array.fromlist
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001521
Brett Cannon1eb32c22014-10-10 16:26:45 -04001522 list: object
1523 /
1524
1525Append items to array from list.
1526[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001527
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001528static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001529array_array_fromlist(arrayobject *self, PyObject *list)
1530/*[clinic end generated code: output=26411c2d228a3e3f input=be2605a96c49680f]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001531{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 Py_ssize_t n;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 if (!PyList_Check(list)) {
1535 PyErr_SetString(PyExc_TypeError, "arg must be list");
1536 return NULL;
1537 }
1538 n = PyList_Size(list);
1539 if (n > 0) {
1540 Py_ssize_t i, old_size;
1541 old_size = Py_SIZE(self);
1542 if (array_resize(self, old_size + n) == -1)
1543 return NULL;
1544 for (i = 0; i < n; i++) {
Zackery Spytz99d56b52018-12-08 07:16:55 -07001545 PyObject *v = PyList_GET_ITEM(list, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 if ((*self->ob_descr->setitem)(self,
1547 Py_SIZE(self) - n + i, v) != 0) {
1548 array_resize(self, old_size);
1549 return NULL;
1550 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07001551 if (n != PyList_GET_SIZE(list)) {
1552 PyErr_SetString(PyExc_RuntimeError,
1553 "list changed size during iteration");
1554 array_resize(self, old_size);
1555 return NULL;
1556 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 }
1558 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001559 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001560}
1561
Brett Cannon1eb32c22014-10-10 16:26:45 -04001562/*[clinic input]
1563array.array.tolist
1564
1565Convert array to an ordinary list with the same items.
1566[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001567
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001568static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001569array_array_tolist_impl(arrayobject *self)
1570/*[clinic end generated code: output=00b60cc9eab8ef89 input=a8d7784a94f86b53]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001571{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 PyObject *list = PyList_New(Py_SIZE(self));
1573 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 if (list == NULL)
1576 return NULL;
1577 for (i = 0; i < Py_SIZE(self); i++) {
1578 PyObject *v = getarrayitem((PyObject *)self, i);
Victor Stinner4755bea2013-07-18 01:12:35 +02001579 if (v == NULL)
1580 goto error;
Zackery Spytz99d56b52018-12-08 07:16:55 -07001581 PyList_SET_ITEM(list, i, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 }
1583 return list;
Victor Stinner4755bea2013-07-18 01:12:35 +02001584
1585error:
1586 Py_DECREF(list);
1587 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001588}
1589
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001590static PyObject *
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001591frombytes(arrayobject *self, Py_buffer *buffer)
Guido van Rossum778983b1993-02-19 15:55:02 +00001592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 int itemsize = self->ob_descr->itemsize;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001594 Py_ssize_t n;
1595 if (buffer->itemsize != 1) {
1596 PyBuffer_Release(buffer);
Serhiy Storchakab757c832014-12-05 22:25:22 +02001597 PyErr_SetString(PyExc_TypeError, "a bytes-like object is required");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 return NULL;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001599 }
1600 n = buffer->len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 if (n % itemsize != 0) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001602 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 PyErr_SetString(PyExc_ValueError,
Serhiy Storchakab757c832014-12-05 22:25:22 +02001604 "bytes length not a multiple of item size");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 return NULL;
1606 }
1607 n = n / itemsize;
1608 if (n > 0) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001609 Py_ssize_t old_size = Py_SIZE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 if ((n > PY_SSIZE_T_MAX - old_size) ||
1611 ((old_size + n) > PY_SSIZE_T_MAX / itemsize)) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001612 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 return PyErr_NoMemory();
1614 }
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001615 if (array_resize(self, old_size + n) == -1) {
1616 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 return NULL;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001618 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 memcpy(self->ob_item + old_size * itemsize,
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001620 buffer->buf, n * itemsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 }
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001622 PyBuffer_Release(buffer);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001623 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001624}
1625
Brett Cannon1eb32c22014-10-10 16:26:45 -04001626/*[clinic input]
1627array.array.fromstring
1628
Larry Hastingsdbfdc382015-05-04 06:59:46 -07001629 buffer: Py_buffer(accept={str, buffer})
Brett Cannon1eb32c22014-10-10 16:26:45 -04001630 /
1631
1632Appends 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).
1633
1634This method is deprecated. Use frombytes instead.
1635[clinic start generated code]*/
1636
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001637static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001638array_array_fromstring_impl(arrayobject *self, Py_buffer *buffer)
Larry Hastingsdbfdc382015-05-04 06:59:46 -07001639/*[clinic end generated code: output=31c4baa779df84ce input=a3341a512e11d773]*/
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001640{
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001641 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1642 "fromstring() is deprecated. Use frombytes() instead.", 2) != 0)
1643 return NULL;
Brett Cannon1eb32c22014-10-10 16:26:45 -04001644 return frombytes(self, buffer);
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001645}
1646
Brett Cannon1eb32c22014-10-10 16:26:45 -04001647/*[clinic input]
1648array.array.frombytes
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001649
Brett Cannon1eb32c22014-10-10 16:26:45 -04001650 buffer: Py_buffer
1651 /
1652
1653Appends 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).
1654[clinic start generated code]*/
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001655
1656static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001657array_array_frombytes_impl(arrayobject *self, Py_buffer *buffer)
1658/*[clinic end generated code: output=d9842c8f7510a516 input=2bbf2b53ebfcc988]*/
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001659{
Brett Cannon1eb32c22014-10-10 16:26:45 -04001660 return frombytes(self, buffer);
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001661}
1662
Brett Cannon1eb32c22014-10-10 16:26:45 -04001663/*[clinic input]
1664array.array.tobytes
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001665
Brett Cannon1eb32c22014-10-10 16:26:45 -04001666Convert the array to an array of machine values and return the bytes representation.
1667[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001668
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001669static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001670array_array_tobytes_impl(arrayobject *self)
1671/*[clinic end generated code: output=87318e4edcdc2bb6 input=90ee495f96de34f5]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001672{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
1674 return PyBytes_FromStringAndSize(self->ob_item,
1675 Py_SIZE(self) * self->ob_descr->itemsize);
1676 } else {
1677 return PyErr_NoMemory();
1678 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001679}
1680
Brett Cannon1eb32c22014-10-10 16:26:45 -04001681/*[clinic input]
1682array.array.tostring
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001683
Brett Cannon1eb32c22014-10-10 16:26:45 -04001684Convert the array to an array of machine values and return the bytes representation.
1685
1686This method is deprecated. Use tobytes instead.
1687[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001688
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001689static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001690array_array_tostring_impl(arrayobject *self)
1691/*[clinic end generated code: output=7d6bd92745a2c8f3 input=b6c0ddee7b30457e]*/
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001692{
Victor Stinner9f0b51e2010-11-09 09:38:30 +00001693 if (PyErr_WarnEx(PyExc_DeprecationWarning,
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001694 "tostring() is deprecated. Use tobytes() instead.", 2) != 0)
1695 return NULL;
Brett Cannon1eb32c22014-10-10 16:26:45 -04001696 return array_array_tobytes_impl(self);
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001697}
1698
Brett Cannon1eb32c22014-10-10 16:26:45 -04001699/*[clinic input]
1700array.array.fromunicode
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001701
Larry Hastings38337d12015-05-07 23:30:09 -07001702 ustr: Py_UNICODE(zeroes=True)
Brett Cannon1eb32c22014-10-10 16:26:45 -04001703 /
1704
1705Extends this array with data from the unicode string ustr.
1706
1707The array must be a unicode type array; otherwise a ValueError is raised.
1708Use array.frombytes(ustr.encode(...)) to append Unicode data to an array of
1709some other type.
1710[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001711
Martin v. Löwis99866332002-03-01 10:27:01 +00001712static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001713array_array_fromunicode_impl(arrayobject *self, const Py_UNICODE *ustr,
Larry Hastings89964c42015-04-14 18:07:59 -04001714 Py_ssize_clean_t ustr_length)
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001715/*[clinic end generated code: output=cf2f662908e2befc input=150f00566ffbca6e]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001716{
Victor Stinner62bb3942012-08-06 00:46:05 +02001717 char typecode;
Martin v. Löwis99866332002-03-01 10:27:01 +00001718
Victor Stinner62bb3942012-08-06 00:46:05 +02001719 typecode = self->ob_descr->typecode;
Gregory P. Smith9504b132012-12-10 20:20:20 -08001720 if (typecode != 'u') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 PyErr_SetString(PyExc_ValueError,
1722 "fromunicode() may only be called on "
1723 "unicode type arrays");
1724 return NULL;
1725 }
Brett Cannon1eb32c22014-10-10 16:26:45 -04001726 if (ustr_length > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 Py_ssize_t old_size = Py_SIZE(self);
Brett Cannon1eb32c22014-10-10 16:26:45 -04001728 if (array_resize(self, old_size + ustr_length) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 return NULL;
Victor Stinner62bb3942012-08-06 00:46:05 +02001730 memcpy(self->ob_item + old_size * sizeof(Py_UNICODE),
Brett Cannon1eb32c22014-10-10 16:26:45 -04001731 ustr, ustr_length * sizeof(Py_UNICODE));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 }
Martin v. Löwis99866332002-03-01 10:27:01 +00001733
Brett Cannon1eb32c22014-10-10 16:26:45 -04001734 Py_RETURN_NONE;
Martin v. Löwis99866332002-03-01 10:27:01 +00001735}
1736
Brett Cannon1eb32c22014-10-10 16:26:45 -04001737/*[clinic input]
1738array.array.tounicode
Martin v. Löwis99866332002-03-01 10:27:01 +00001739
Brett Cannon1eb32c22014-10-10 16:26:45 -04001740Extends this array with data from the unicode string ustr.
1741
1742Convert the array to a unicode string. The array must be a unicode type array;
1743otherwise a ValueError is raised. Use array.tobytes().decode() to obtain a
1744unicode string from an array of some other type.
1745[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001746
1747static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001748array_array_tounicode_impl(arrayobject *self)
1749/*[clinic end generated code: output=08e442378336e1ef input=127242eebe70b66d]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001750{
Victor Stinner62bb3942012-08-06 00:46:05 +02001751 char typecode;
1752 typecode = self->ob_descr->typecode;
Gregory P. Smith9504b132012-12-10 20:20:20 -08001753 if (typecode != 'u') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 PyErr_SetString(PyExc_ValueError,
1755 "tounicode() may only be called on unicode type arrays");
1756 return NULL;
1757 }
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02001758 return PyUnicode_FromWideChar((Py_UNICODE *) self->ob_item, Py_SIZE(self));
Martin v. Löwis99866332002-03-01 10:27:01 +00001759}
1760
Brett Cannon1eb32c22014-10-10 16:26:45 -04001761/*[clinic input]
1762array.array.__sizeof__
Martin v. Löwis99866332002-03-01 10:27:01 +00001763
Brett Cannon1eb32c22014-10-10 16:26:45 -04001764Size of the array in memory, in bytes.
1765[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001766
Meador Inge03b4d502012-08-10 22:35:45 -05001767static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001768array_array___sizeof___impl(arrayobject *self)
1769/*[clinic end generated code: output=d8e1c61ebbe3eaed input=805586565bf2b3c6]*/
Meador Inge03b4d502012-08-10 22:35:45 -05001770{
1771 Py_ssize_t res;
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02001772 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * self->ob_descr->itemsize;
Meador Inge03b4d502012-08-10 22:35:45 -05001773 return PyLong_FromSsize_t(res);
1774}
1775
Martin v. Löwis99866332002-03-01 10:27:01 +00001776
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001777/*********************** Pickling support ************************/
1778
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001779static const struct mformatdescr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 size_t size;
1781 int is_signed;
1782 int is_big_endian;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001783} mformat_descriptors[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 {1, 0, 0}, /* 0: UNSIGNED_INT8 */
1785 {1, 1, 0}, /* 1: SIGNED_INT8 */
1786 {2, 0, 0}, /* 2: UNSIGNED_INT16_LE */
1787 {2, 0, 1}, /* 3: UNSIGNED_INT16_BE */
1788 {2, 1, 0}, /* 4: SIGNED_INT16_LE */
1789 {2, 1, 1}, /* 5: SIGNED_INT16_BE */
1790 {4, 0, 0}, /* 6: UNSIGNED_INT32_LE */
1791 {4, 0, 1}, /* 7: UNSIGNED_INT32_BE */
1792 {4, 1, 0}, /* 8: SIGNED_INT32_LE */
1793 {4, 1, 1}, /* 9: SIGNED_INT32_BE */
1794 {8, 0, 0}, /* 10: UNSIGNED_INT64_LE */
1795 {8, 0, 1}, /* 11: UNSIGNED_INT64_BE */
1796 {8, 1, 0}, /* 12: SIGNED_INT64_LE */
1797 {8, 1, 1}, /* 13: SIGNED_INT64_BE */
1798 {4, 0, 0}, /* 14: IEEE_754_FLOAT_LE */
1799 {4, 0, 1}, /* 15: IEEE_754_FLOAT_BE */
1800 {8, 0, 0}, /* 16: IEEE_754_DOUBLE_LE */
1801 {8, 0, 1}, /* 17: IEEE_754_DOUBLE_BE */
1802 {4, 0, 0}, /* 18: UTF16_LE */
1803 {4, 0, 1}, /* 19: UTF16_BE */
1804 {8, 0, 0}, /* 20: UTF32_LE */
1805 {8, 0, 1} /* 21: UTF32_BE */
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001806};
1807
1808
1809/*
1810 * Internal: This function is used to find the machine format of a given
1811 * array type code. This returns UNKNOWN_FORMAT when the machine format cannot
1812 * be found.
1813 */
1814static enum machine_format_code
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001815typecode_to_mformat_code(char typecode)
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001816{
Christian Heimes743e0cd2012-10-17 23:52:17 +02001817 const int is_big_endian = PY_BIG_ENDIAN;
1818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 size_t intsize;
1820 int is_signed;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 switch (typecode) {
1823 case 'b':
1824 return SIGNED_INT8;
1825 case 'B':
1826 return UNSIGNED_INT8;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 case 'u':
Victor Stinner62bb3942012-08-06 00:46:05 +02001829 if (sizeof(Py_UNICODE) == 2) {
1830 return UTF16_LE + is_big_endian;
1831 }
1832 if (sizeof(Py_UNICODE) == 4) {
1833 return UTF32_LE + is_big_endian;
1834 }
1835 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 case 'f':
1838 if (sizeof(float) == 4) {
1839 const float y = 16711938.0;
1840 if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0)
1841 return IEEE_754_FLOAT_BE;
1842 if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0)
1843 return IEEE_754_FLOAT_LE;
1844 }
1845 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 case 'd':
1848 if (sizeof(double) == 8) {
1849 const double x = 9006104071832581.0;
1850 if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0)
1851 return IEEE_754_DOUBLE_BE;
1852 if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
1853 return IEEE_754_DOUBLE_LE;
1854 }
1855 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 /* Integers */
1858 case 'h':
1859 intsize = sizeof(short);
1860 is_signed = 1;
1861 break;
1862 case 'H':
1863 intsize = sizeof(short);
1864 is_signed = 0;
1865 break;
1866 case 'i':
1867 intsize = sizeof(int);
1868 is_signed = 1;
1869 break;
1870 case 'I':
1871 intsize = sizeof(int);
1872 is_signed = 0;
1873 break;
1874 case 'l':
1875 intsize = sizeof(long);
1876 is_signed = 1;
1877 break;
1878 case 'L':
1879 intsize = sizeof(long);
1880 is_signed = 0;
1881 break;
Meador Inge1c9f0c92011-09-20 19:55:51 -05001882 case 'q':
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001883 intsize = sizeof(long long);
Meador Inge1c9f0c92011-09-20 19:55:51 -05001884 is_signed = 1;
1885 break;
1886 case 'Q':
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001887 intsize = sizeof(long long);
Meador Inge1c9f0c92011-09-20 19:55:51 -05001888 is_signed = 0;
1889 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 default:
1891 return UNKNOWN_FORMAT;
1892 }
1893 switch (intsize) {
1894 case 2:
1895 return UNSIGNED_INT16_LE + is_big_endian + (2 * is_signed);
1896 case 4:
1897 return UNSIGNED_INT32_LE + is_big_endian + (2 * is_signed);
1898 case 8:
1899 return UNSIGNED_INT64_LE + is_big_endian + (2 * is_signed);
1900 default:
1901 return UNKNOWN_FORMAT;
1902 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001903}
1904
1905/* Forward declaration. */
1906static PyObject *array_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
1907
1908/*
1909 * Internal: This function wraps the array constructor--i.e., array_new()--to
1910 * allow the creation of array objects from C code without having to deal
1911 * directly the tuple argument of array_new(). The typecode argument is a
1912 * Unicode character value, like 'i' or 'f' for example, representing an array
1913 * type code. The items argument is a bytes or a list object from which
1914 * contains the initial value of the array.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 *
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001916 * On success, this functions returns the array object created. Otherwise,
1917 * NULL is returned to indicate a failure.
1918 */
1919static PyObject *
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001920make_array(PyTypeObject *arraytype, char typecode, PyObject *items)
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001921{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 PyObject *new_args;
1923 PyObject *array_obj;
1924 PyObject *typecode_obj;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 assert(arraytype != NULL);
1927 assert(items != NULL);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001928
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001929 typecode_obj = PyUnicode_FromOrdinal(typecode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 if (typecode_obj == NULL)
1931 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 new_args = PyTuple_New(2);
Mat M56935a52017-11-14 01:00:54 -05001934 if (new_args == NULL) {
1935 Py_DECREF(typecode_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 return NULL;
Mat M56935a52017-11-14 01:00:54 -05001937 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 Py_INCREF(items);
1939 PyTuple_SET_ITEM(new_args, 0, typecode_obj);
1940 PyTuple_SET_ITEM(new_args, 1, items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 array_obj = array_new(arraytype, new_args, NULL);
1943 Py_DECREF(new_args);
1944 if (array_obj == NULL)
1945 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 return array_obj;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001948}
1949
1950/*
1951 * This functions is a special constructor used when unpickling an array. It
1952 * provides a portable way to rebuild an array from its memory representation.
1953 */
Brett Cannon1eb32c22014-10-10 16:26:45 -04001954/*[clinic input]
1955array._array_reconstructor
1956
1957 arraytype: object(type="PyTypeObject *")
Larry Hastingsdbfdc382015-05-04 06:59:46 -07001958 typecode: int(accept={str})
Brett Cannon1eb32c22014-10-10 16:26:45 -04001959 mformat_code: int(type="enum machine_format_code")
1960 items: object
1961 /
1962
1963Internal. Used for pickling support.
1964[clinic start generated code]*/
1965
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001966static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001967array__array_reconstructor_impl(PyObject *module, PyTypeObject *arraytype,
Larry Hastings89964c42015-04-14 18:07:59 -04001968 int typecode,
1969 enum machine_format_code mformat_code,
1970 PyObject *items)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001971/*[clinic end generated code: output=e05263141ba28365 input=2464dc8f4c7736b5]*/
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001972{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 PyObject *converted_items;
1974 PyObject *result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001975 const struct arraydescr *descr;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 if (!PyType_Check(arraytype)) {
1978 PyErr_Format(PyExc_TypeError,
Sylvaina90e64b2017-03-29 20:09:22 +02001979 "first argument must be a type object, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 Py_TYPE(arraytype)->tp_name);
1981 return NULL;
1982 }
1983 if (!PyType_IsSubtype(arraytype, &Arraytype)) {
1984 PyErr_Format(PyExc_TypeError,
1985 "%.200s is not a subtype of %.200s",
1986 arraytype->tp_name, Arraytype.tp_name);
1987 return NULL;
1988 }
1989 for (descr = descriptors; descr->typecode != '\0'; descr++) {
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001990 if ((int)descr->typecode == typecode)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001991 break;
1992 }
1993 if (descr->typecode == '\0') {
1994 PyErr_SetString(PyExc_ValueError,
1995 "second argument must be a valid type code");
1996 return NULL;
1997 }
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001998 if (mformat_code < MACHINE_FORMAT_CODE_MIN ||
1999 mformat_code > MACHINE_FORMAT_CODE_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 PyErr_SetString(PyExc_ValueError,
2001 "third argument must be a valid machine format code.");
2002 return NULL;
2003 }
2004 if (!PyBytes_Check(items)) {
2005 PyErr_Format(PyExc_TypeError,
2006 "fourth argument should be bytes, not %.200s",
2007 Py_TYPE(items)->tp_name);
2008 return NULL;
2009 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 /* Fast path: No decoding has to be done. */
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002012 if (mformat_code == typecode_to_mformat_code((char)typecode) ||
2013 mformat_code == UNKNOWN_FORMAT) {
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002014 return make_array(arraytype, (char)typecode, items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 /* Slow path: Decode the byte string according to the given machine
2018 * format code. This occurs when the computer unpickling the array
2019 * object is architecturally different from the one that pickled the
2020 * array.
2021 */
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002022 if (Py_SIZE(items) % mformat_descriptors[mformat_code].size != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 PyErr_SetString(PyExc_ValueError,
2024 "string length not a multiple of item size");
2025 return NULL;
2026 }
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002027 switch (mformat_code) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 case IEEE_754_FLOAT_LE:
2029 case IEEE_754_FLOAT_BE: {
sthaa3ecb82019-03-20 20:49:39 +01002030 Py_ssize_t i;
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002031 int le = (mformat_code == IEEE_754_FLOAT_LE) ? 1 : 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 Py_ssize_t itemcount = Py_SIZE(items) / 4;
2033 const unsigned char *memstr =
2034 (unsigned char *)PyBytes_AS_STRING(items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 converted_items = PyList_New(itemcount);
2037 if (converted_items == NULL)
2038 return NULL;
2039 for (i = 0; i < itemcount; i++) {
2040 PyObject *pyfloat = PyFloat_FromDouble(
2041 _PyFloat_Unpack4(&memstr[i * 4], le));
2042 if (pyfloat == NULL) {
2043 Py_DECREF(converted_items);
2044 return NULL;
2045 }
2046 PyList_SET_ITEM(converted_items, i, pyfloat);
2047 }
2048 break;
2049 }
2050 case IEEE_754_DOUBLE_LE:
2051 case IEEE_754_DOUBLE_BE: {
sthaa3ecb82019-03-20 20:49:39 +01002052 Py_ssize_t i;
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002053 int le = (mformat_code == IEEE_754_DOUBLE_LE) ? 1 : 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 Py_ssize_t itemcount = Py_SIZE(items) / 8;
2055 const unsigned char *memstr =
2056 (unsigned char *)PyBytes_AS_STRING(items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 converted_items = PyList_New(itemcount);
2059 if (converted_items == NULL)
2060 return NULL;
2061 for (i = 0; i < itemcount; i++) {
2062 PyObject *pyfloat = PyFloat_FromDouble(
2063 _PyFloat_Unpack8(&memstr[i * 8], le));
2064 if (pyfloat == NULL) {
2065 Py_DECREF(converted_items);
2066 return NULL;
2067 }
2068 PyList_SET_ITEM(converted_items, i, pyfloat);
2069 }
2070 break;
2071 }
2072 case UTF16_LE:
2073 case UTF16_BE: {
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002074 int byteorder = (mformat_code == UTF16_LE) ? -1 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 converted_items = PyUnicode_DecodeUTF16(
2076 PyBytes_AS_STRING(items), Py_SIZE(items),
2077 "strict", &byteorder);
2078 if (converted_items == NULL)
2079 return NULL;
2080 break;
2081 }
2082 case UTF32_LE:
2083 case UTF32_BE: {
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002084 int byteorder = (mformat_code == UTF32_LE) ? -1 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 converted_items = PyUnicode_DecodeUTF32(
2086 PyBytes_AS_STRING(items), Py_SIZE(items),
2087 "strict", &byteorder);
2088 if (converted_items == NULL)
2089 return NULL;
2090 break;
2091 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 case UNSIGNED_INT8:
2094 case SIGNED_INT8:
2095 case UNSIGNED_INT16_LE:
2096 case UNSIGNED_INT16_BE:
2097 case SIGNED_INT16_LE:
2098 case SIGNED_INT16_BE:
2099 case UNSIGNED_INT32_LE:
2100 case UNSIGNED_INT32_BE:
2101 case SIGNED_INT32_LE:
2102 case SIGNED_INT32_BE:
2103 case UNSIGNED_INT64_LE:
2104 case UNSIGNED_INT64_BE:
2105 case SIGNED_INT64_LE:
2106 case SIGNED_INT64_BE: {
sthaa3ecb82019-03-20 20:49:39 +01002107 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 const struct mformatdescr mf_descr =
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002109 mformat_descriptors[mformat_code];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 Py_ssize_t itemcount = Py_SIZE(items) / mf_descr.size;
2111 const unsigned char *memstr =
2112 (unsigned char *)PyBytes_AS_STRING(items);
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002113 const struct arraydescr *descr;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115 /* If possible, try to pack array's items using a data type
2116 * that fits better. This may result in an array with narrower
2117 * or wider elements.
2118 *
Martin Panter4c359642016-05-08 13:53:41 +00002119 * For example, if a 32-bit machine pickles an L-code array of
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002120 * unsigned longs, then the array will be unpickled by 64-bit
2121 * machine as an I-code array of unsigned ints.
2122 *
2123 * XXX: Is it possible to write a unit test for this?
2124 */
2125 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2126 if (descr->is_integer_type &&
Victor Stinner706768c2014-08-16 01:03:39 +02002127 (size_t)descr->itemsize == mf_descr.size &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002128 descr->is_signed == mf_descr.is_signed)
2129 typecode = descr->typecode;
2130 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132 converted_items = PyList_New(itemcount);
2133 if (converted_items == NULL)
2134 return NULL;
2135 for (i = 0; i < itemcount; i++) {
2136 PyObject *pylong;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 pylong = _PyLong_FromByteArray(
2139 &memstr[i * mf_descr.size],
2140 mf_descr.size,
2141 !mf_descr.is_big_endian,
2142 mf_descr.is_signed);
2143 if (pylong == NULL) {
2144 Py_DECREF(converted_items);
2145 return NULL;
2146 }
2147 PyList_SET_ITEM(converted_items, i, pylong);
2148 }
2149 break;
2150 }
2151 case UNKNOWN_FORMAT:
2152 /* Impossible, but needed to shut up GCC about the unhandled
2153 * enumeration value.
2154 */
2155 default:
2156 PyErr_BadArgument();
2157 return NULL;
2158 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002159
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002160 result = make_array(arraytype, (char)typecode, converted_items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 Py_DECREF(converted_items);
2162 return result;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002163}
2164
Brett Cannon1eb32c22014-10-10 16:26:45 -04002165/*[clinic input]
2166array.array.__reduce_ex__
2167
2168 value: object
2169 /
2170
2171Return state information for pickling.
2172[clinic start generated code]*/
2173
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002174static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04002175array_array___reduce_ex__(arrayobject *self, PyObject *value)
2176/*[clinic end generated code: output=051e0a6175d0eddb input=c36c3f85de7df6cd]*/
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002177{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 PyObject *dict;
2179 PyObject *result;
2180 PyObject *array_str;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002181 int typecode = self->ob_descr->typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 int mformat_code;
2183 static PyObject *array_reconstructor = NULL;
2184 long protocol;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002185 _Py_IDENTIFIER(_array_reconstructor);
2186 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188 if (array_reconstructor == NULL) {
2189 PyObject *array_module = PyImport_ImportModule("array");
2190 if (array_module == NULL)
2191 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002192 array_reconstructor = _PyObject_GetAttrId(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 array_module,
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002194 &PyId__array_reconstructor);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 Py_DECREF(array_module);
2196 if (array_reconstructor == NULL)
2197 return NULL;
2198 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 if (!PyLong_Check(value)) {
2201 PyErr_SetString(PyExc_TypeError,
Sylvaina90e64b2017-03-29 20:09:22 +02002202 "__reduce_ex__ argument should be an integer");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203 return NULL;
2204 }
2205 protocol = PyLong_AsLong(value);
2206 if (protocol == -1 && PyErr_Occurred())
2207 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002208
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002209 if (_PyObject_LookupAttrId((PyObject *)self, &PyId___dict__, &dict) < 0) {
2210 return NULL;
2211 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 dict = Py_None;
2214 Py_INCREF(dict);
2215 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217 mformat_code = typecode_to_mformat_code(typecode);
2218 if (mformat_code == UNKNOWN_FORMAT || protocol < 3) {
2219 /* Convert the array to a list if we got something weird
2220 * (e.g., non-IEEE floats), or we are pickling the array using
2221 * a Python 2.x compatible protocol.
2222 *
2223 * It is necessary to use a list representation for Python 2.x
2224 * compatible pickle protocol, since Python 2's str objects
2225 * are unpickled as unicode by Python 3. Thus it is impossible
2226 * to make arrays unpicklable by Python 3 by using their memory
2227 * representation, unless we resort to ugly hacks such as
2228 * coercing unicode objects to bytes in array_reconstructor.
2229 */
2230 PyObject *list;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002231 list = array_array_tolist_impl(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 if (list == NULL) {
2233 Py_DECREF(dict);
2234 return NULL;
2235 }
2236 result = Py_BuildValue(
Brett Cannon1eb32c22014-10-10 16:26:45 -04002237 "O(CO)O", Py_TYPE(self), typecode, list, dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 Py_DECREF(list);
2239 Py_DECREF(dict);
2240 return result;
2241 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002242
Brett Cannon1eb32c22014-10-10 16:26:45 -04002243 array_str = array_array_tobytes_impl(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 if (array_str == NULL) {
2245 Py_DECREF(dict);
2246 return NULL;
2247 }
2248 result = Py_BuildValue(
Brett Cannon1eb32c22014-10-10 16:26:45 -04002249 "O(OCiN)O", array_reconstructor, Py_TYPE(self), typecode,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 mformat_code, array_str, dict);
2251 Py_DECREF(dict);
2252 return result;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002253}
2254
Martin v. Löwis99866332002-03-01 10:27:01 +00002255static PyObject *
2256array_get_typecode(arrayobject *a, void *closure)
2257{
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002258 char typecode = a->ob_descr->typecode;
2259 return PyUnicode_FromOrdinal(typecode);
Martin v. Löwis99866332002-03-01 10:27:01 +00002260}
2261
2262static PyObject *
2263array_get_itemsize(arrayobject *a, void *closure)
2264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 return PyLong_FromLong((long)a->ob_descr->itemsize);
Martin v. Löwis99866332002-03-01 10:27:01 +00002266}
2267
2268static PyGetSetDef array_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 {"typecode", (getter) array_get_typecode, NULL,
2270 "the typecode character used to create the array"},
2271 {"itemsize", (getter) array_get_itemsize, NULL,
2272 "the size, in bytes, of one array item"},
2273 {NULL}
Martin v. Löwis99866332002-03-01 10:27:01 +00002274};
2275
Martin v. Löwis59683e82008-06-13 07:50:45 +00002276static PyMethodDef array_methods[] = {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002277 ARRAY_ARRAY_APPEND_METHODDEF
2278 ARRAY_ARRAY_BUFFER_INFO_METHODDEF
2279 ARRAY_ARRAY_BYTESWAP_METHODDEF
2280 ARRAY_ARRAY___COPY___METHODDEF
2281 ARRAY_ARRAY_COUNT_METHODDEF
2282 ARRAY_ARRAY___DEEPCOPY___METHODDEF
2283 ARRAY_ARRAY_EXTEND_METHODDEF
2284 ARRAY_ARRAY_FROMFILE_METHODDEF
2285 ARRAY_ARRAY_FROMLIST_METHODDEF
2286 ARRAY_ARRAY_FROMSTRING_METHODDEF
2287 ARRAY_ARRAY_FROMBYTES_METHODDEF
2288 ARRAY_ARRAY_FROMUNICODE_METHODDEF
2289 ARRAY_ARRAY_INDEX_METHODDEF
2290 ARRAY_ARRAY_INSERT_METHODDEF
2291 ARRAY_ARRAY_POP_METHODDEF
2292 ARRAY_ARRAY___REDUCE_EX___METHODDEF
2293 ARRAY_ARRAY_REMOVE_METHODDEF
2294 ARRAY_ARRAY_REVERSE_METHODDEF
2295 ARRAY_ARRAY_TOFILE_METHODDEF
2296 ARRAY_ARRAY_TOLIST_METHODDEF
2297 ARRAY_ARRAY_TOSTRING_METHODDEF
2298 ARRAY_ARRAY_TOBYTES_METHODDEF
2299 ARRAY_ARRAY_TOUNICODE_METHODDEF
2300 ARRAY_ARRAY___SIZEOF___METHODDEF
2301 {NULL, NULL} /* sentinel */
Guido van Rossum778983b1993-02-19 15:55:02 +00002302};
2303
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002304static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00002305array_repr(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +00002306{
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002307 char typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 PyObject *s, *v = NULL;
2309 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00002310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 len = Py_SIZE(a);
2312 typecode = a->ob_descr->typecode;
2313 if (len == 0) {
Serhiy Storchakab3a77962017-09-21 14:24:13 +03002314 return PyUnicode_FromFormat("%s('%c')",
2315 _PyType_Name(Py_TYPE(a)), (int)typecode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 }
Gregory P. Smith9504b132012-12-10 20:20:20 -08002317 if (typecode == 'u') {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002318 v = array_array_tounicode_impl(a);
Gregory P. Smith9504b132012-12-10 20:20:20 -08002319 } else {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002320 v = array_array_tolist_impl(a);
Gregory P. Smith9504b132012-12-10 20:20:20 -08002321 }
Victor Stinner29ec5952013-02-26 00:27:38 +01002322 if (v == NULL)
2323 return NULL;
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00002324
Serhiy Storchakab3a77962017-09-21 14:24:13 +03002325 s = PyUnicode_FromFormat("%s('%c', %R)",
2326 _PyType_Name(Py_TYPE(a)), (int)typecode, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 Py_DECREF(v);
2328 return s;
Guido van Rossum778983b1993-02-19 15:55:02 +00002329}
2330
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002331static PyObject*
2332array_subscr(arrayobject* self, PyObject* item)
2333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 if (PyIndex_Check(item)) {
2335 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2336 if (i==-1 && PyErr_Occurred()) {
2337 return NULL;
2338 }
2339 if (i < 0)
2340 i += Py_SIZE(self);
2341 return array_item(self, i);
2342 }
2343 else if (PySlice_Check(item)) {
Zackery Spytz14514d92019-05-17 01:13:03 -06002344 Py_ssize_t start, stop, step, slicelength, i;
2345 size_t cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 PyObject* result;
2347 arrayobject* ar;
2348 int itemsize = self->ob_descr->itemsize;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002349
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002350 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 return NULL;
2352 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002353 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2354 step);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 if (slicelength <= 0) {
2357 return newarrayobject(&Arraytype, 0, self->ob_descr);
2358 }
2359 else if (step == 1) {
2360 PyObject *result = newarrayobject(&Arraytype,
2361 slicelength, self->ob_descr);
2362 if (result == NULL)
2363 return NULL;
2364 memcpy(((arrayobject *)result)->ob_item,
2365 self->ob_item + start * itemsize,
2366 slicelength * itemsize);
2367 return result;
2368 }
2369 else {
2370 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
2371 if (!result) return NULL;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 ar = (arrayobject*)result;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 for (cur = start, i = 0; i < slicelength;
2376 cur += step, i++) {
2377 memcpy(ar->ob_item + i*itemsize,
2378 self->ob_item + cur*itemsize,
2379 itemsize);
2380 }
2381
2382 return result;
2383 }
2384 }
2385 else {
2386 PyErr_SetString(PyExc_TypeError,
2387 "array indices must be integers");
2388 return NULL;
2389 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002390}
2391
2392static int
2393array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
2394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 Py_ssize_t start, stop, step, slicelength, needed;
2396 arrayobject* other;
2397 int itemsize;
Thomas Woutersed03b412007-08-28 21:37:11 +00002398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 if (PyIndex_Check(item)) {
2400 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Alexandre Vassalotti47137252009-07-05 19:57:00 +00002401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 if (i == -1 && PyErr_Occurred())
2403 return -1;
2404 if (i < 0)
2405 i += Py_SIZE(self);
2406 if (i < 0 || i >= Py_SIZE(self)) {
2407 PyErr_SetString(PyExc_IndexError,
2408 "array assignment index out of range");
2409 return -1;
2410 }
2411 if (value == NULL) {
2412 /* Fall through to slice assignment */
2413 start = i;
2414 stop = i + 1;
2415 step = 1;
2416 slicelength = 1;
2417 }
2418 else
2419 return (*self->ob_descr->setitem)(self, i, value);
2420 }
2421 else if (PySlice_Check(item)) {
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002422 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 return -1;
2424 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002425 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2426 step);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 }
2428 else {
2429 PyErr_SetString(PyExc_TypeError,
Sylvaina90e64b2017-03-29 20:09:22 +02002430 "array indices must be integers");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 return -1;
2432 }
2433 if (value == NULL) {
2434 other = NULL;
2435 needed = 0;
2436 }
2437 else if (array_Check(value)) {
2438 other = (arrayobject *)value;
2439 needed = Py_SIZE(other);
2440 if (self == other) {
2441 /* Special case "self[i:j] = self" -- copy self first */
2442 int ret;
2443 value = array_slice(other, 0, needed);
2444 if (value == NULL)
2445 return -1;
2446 ret = array_ass_subscr(self, item, value);
2447 Py_DECREF(value);
2448 return ret;
2449 }
2450 if (other->ob_descr != self->ob_descr) {
2451 PyErr_BadArgument();
2452 return -1;
2453 }
2454 }
2455 else {
2456 PyErr_Format(PyExc_TypeError,
2457 "can only assign array (not \"%.200s\") to array slice",
2458 Py_TYPE(value)->tp_name);
2459 return -1;
2460 }
2461 itemsize = self->ob_descr->itemsize;
2462 /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
2463 if ((step > 0 && stop < start) ||
2464 (step < 0 && stop > start))
2465 stop = start;
Alexandre Vassalotti47137252009-07-05 19:57:00 +00002466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467 /* Issue #4509: If the array has exported buffers and the slice
2468 assignment would change the size of the array, fail early to make
2469 sure we don't modify it. */
2470 if ((needed == 0 || slicelength != needed) && self->ob_exports > 0) {
2471 PyErr_SetString(PyExc_BufferError,
2472 "cannot resize an array that is exporting buffers");
2473 return -1;
2474 }
Mark Dickinsonbc099642010-01-29 17:27:24 +00002475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 if (step == 1) {
2477 if (slicelength > needed) {
2478 memmove(self->ob_item + (start + needed) * itemsize,
2479 self->ob_item + stop * itemsize,
2480 (Py_SIZE(self) - stop) * itemsize);
2481 if (array_resize(self, Py_SIZE(self) +
2482 needed - slicelength) < 0)
2483 return -1;
2484 }
2485 else if (slicelength < needed) {
2486 if (array_resize(self, Py_SIZE(self) +
2487 needed - slicelength) < 0)
2488 return -1;
2489 memmove(self->ob_item + (start + needed) * itemsize,
2490 self->ob_item + stop * itemsize,
2491 (Py_SIZE(self) - start - needed) * itemsize);
2492 }
2493 if (needed > 0)
2494 memcpy(self->ob_item + start * itemsize,
2495 other->ob_item, needed * itemsize);
2496 return 0;
2497 }
2498 else if (needed == 0) {
2499 /* Delete slice */
2500 size_t cur;
2501 Py_ssize_t i;
Thomas Woutersed03b412007-08-28 21:37:11 +00002502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002503 if (step < 0) {
2504 stop = start + 1;
2505 start = stop + step * (slicelength - 1) - 1;
2506 step = -step;
2507 }
2508 for (cur = start, i = 0; i < slicelength;
2509 cur += step, i++) {
2510 Py_ssize_t lim = step - 1;
Thomas Woutersed03b412007-08-28 21:37:11 +00002511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002512 if (cur + step >= (size_t)Py_SIZE(self))
2513 lim = Py_SIZE(self) - cur - 1;
2514 memmove(self->ob_item + (cur - i) * itemsize,
2515 self->ob_item + (cur + 1) * itemsize,
2516 lim * itemsize);
2517 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002518 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 if (cur < (size_t)Py_SIZE(self)) {
2520 memmove(self->ob_item + (cur-slicelength) * itemsize,
2521 self->ob_item + cur * itemsize,
2522 (Py_SIZE(self) - cur) * itemsize);
2523 }
2524 if (array_resize(self, Py_SIZE(self) - slicelength) < 0)
2525 return -1;
2526 return 0;
2527 }
2528 else {
Zackery Spytz14514d92019-05-17 01:13:03 -06002529 size_t cur;
2530 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002531
2532 if (needed != slicelength) {
2533 PyErr_Format(PyExc_ValueError,
2534 "attempt to assign array of size %zd "
2535 "to extended slice of size %zd",
2536 needed, slicelength);
2537 return -1;
2538 }
2539 for (cur = start, i = 0; i < slicelength;
2540 cur += step, i++) {
2541 memcpy(self->ob_item + cur * itemsize,
2542 other->ob_item + i * itemsize,
2543 itemsize);
2544 }
2545 return 0;
2546 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002547}
2548
2549static PyMappingMethods array_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 (lenfunc)array_length,
2551 (binaryfunc)array_subscr,
2552 (objobjargproc)array_ass_subscr
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002553};
2554
Guido van Rossumd8faa362007-04-27 19:54:29 +00002555static const void *emptybuf = "";
2556
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002557
2558static int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00002559array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002560{
Stefan Krah650c1e82015-02-03 21:43:23 +01002561 if (view == NULL) {
2562 PyErr_SetString(PyExc_BufferError,
2563 "array_buffer_getbuf: view==NULL argument is obsolete");
2564 return -1;
2565 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002567 view->buf = (void *)self->ob_item;
2568 view->obj = (PyObject*)self;
2569 Py_INCREF(self);
2570 if (view->buf == NULL)
2571 view->buf = (void *)emptybuf;
2572 view->len = (Py_SIZE(self)) * self->ob_descr->itemsize;
2573 view->readonly = 0;
2574 view->ndim = 1;
2575 view->itemsize = self->ob_descr->itemsize;
2576 view->suboffsets = NULL;
2577 view->shape = NULL;
2578 if ((flags & PyBUF_ND)==PyBUF_ND) {
2579 view->shape = &((Py_SIZE(self)));
2580 }
2581 view->strides = NULL;
2582 if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES)
2583 view->strides = &(view->itemsize);
2584 view->format = NULL;
2585 view->internal = NULL;
Victor Stinner62bb3942012-08-06 00:46:05 +02002586 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002587 view->format = (char *)self->ob_descr->formats;
Victor Stinner62bb3942012-08-06 00:46:05 +02002588#ifdef Py_UNICODE_WIDE
2589 if (self->ob_descr->typecode == 'u') {
2590 view->format = "w";
2591 }
2592#endif
2593 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002595 self->ob_exports++;
2596 return 0;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002597}
2598
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002599static void
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00002600array_buffer_relbuf(arrayobject *self, Py_buffer *view)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002602 self->ob_exports--;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002603}
2604
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002605static PySequenceMethods array_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002606 (lenfunc)array_length, /*sq_length*/
2607 (binaryfunc)array_concat, /*sq_concat*/
2608 (ssizeargfunc)array_repeat, /*sq_repeat*/
2609 (ssizeargfunc)array_item, /*sq_item*/
2610 0, /*sq_slice*/
2611 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
2612 0, /*sq_ass_slice*/
2613 (objobjproc)array_contains, /*sq_contains*/
2614 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
2615 (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/
Guido van Rossum778983b1993-02-19 15:55:02 +00002616};
2617
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002618static PyBufferProcs array_as_buffer = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002619 (getbufferproc)array_buffer_getbuf,
2620 (releasebufferproc)array_buffer_relbuf
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002621};
2622
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002623static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +00002624array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum778983b1993-02-19 15:55:02 +00002625{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002626 int c;
2627 PyObject *initial = NULL, *it = NULL;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002628 const struct arraydescr *descr;
Martin v. Löwis99866332002-03-01 10:27:01 +00002629
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03002630 if (type == &Arraytype && !_PyArg_NoKeywords("array.array", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002631 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00002632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002633 if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
2634 return NULL;
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00002635
Steve Dowerb82e17e2019-05-23 08:45:22 -07002636 if (PySys_Audit("array.__new__", "CO",
2637 c, initial ? initial : Py_None) < 0) {
2638 return NULL;
2639 }
2640
Alexandre Vassalotti9730e332013-11-29 20:47:15 -08002641 if (initial && c != 'u') {
2642 if (PyUnicode_Check(initial)) {
2643 PyErr_Format(PyExc_TypeError, "cannot use a str to initialize "
2644 "an array with typecode '%c'", c);
2645 return NULL;
2646 }
2647 else if (array_Check(initial) &&
2648 ((arrayobject*)initial)->ob_descr->typecode == 'u') {
2649 PyErr_Format(PyExc_TypeError, "cannot use a unicode array to "
2650 "initialize an array with typecode '%c'", c);
2651 return NULL;
2652 }
2653 }
2654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 if (!(initial == NULL || PyList_Check(initial)
2656 || PyByteArray_Check(initial)
2657 || PyBytes_Check(initial)
2658 || PyTuple_Check(initial)
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002659 || ((c=='u') && PyUnicode_Check(initial))
2660 || (array_Check(initial)
2661 && c == ((arrayobject*)initial)->ob_descr->typecode))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002662 it = PyObject_GetIter(initial);
2663 if (it == NULL)
2664 return NULL;
2665 /* We set initial to NULL so that the subsequent code
2666 will create an empty array of the appropriate type
2667 and afterwards we can use array_iter_extend to populate
2668 the array.
2669 */
2670 initial = NULL;
2671 }
2672 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2673 if (descr->typecode == c) {
2674 PyObject *a;
2675 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00002676
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002677 if (initial == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002678 len = 0;
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002679 else if (PyList_Check(initial))
2680 len = PyList_GET_SIZE(initial);
2681 else if (PyTuple_Check(initial) || array_Check(initial))
2682 len = Py_SIZE(initial);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 else
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002684 len = 0;
Martin v. Löwis99866332002-03-01 10:27:01 +00002685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 a = newarrayobject(type, len, descr);
2687 if (a == NULL)
2688 return NULL;
2689
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002690 if (len > 0 && !array_Check(initial)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002691 Py_ssize_t i;
2692 for (i = 0; i < len; i++) {
2693 PyObject *v =
2694 PySequence_GetItem(initial, i);
2695 if (v == NULL) {
2696 Py_DECREF(a);
2697 return NULL;
2698 }
2699 if (setarrayitem(a, i, v) != 0) {
2700 Py_DECREF(v);
2701 Py_DECREF(a);
2702 return NULL;
2703 }
2704 Py_DECREF(v);
2705 }
2706 }
2707 else if (initial != NULL && (PyByteArray_Check(initial) ||
2708 PyBytes_Check(initial))) {
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03002709 PyObject *v;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002710 v = array_array_frombytes((arrayobject *)a,
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03002711 initial);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002712 if (v == NULL) {
2713 Py_DECREF(a);
2714 return NULL;
2715 }
2716 Py_DECREF(v);
2717 }
2718 else if (initial != NULL && PyUnicode_Check(initial)) {
Victor Stinner62bb3942012-08-06 00:46:05 +02002719 Py_UNICODE *ustr;
Victor Stinner1fbcaef2011-09-30 01:54:04 +02002720 Py_ssize_t n;
Victor Stinner62bb3942012-08-06 00:46:05 +02002721
2722 ustr = PyUnicode_AsUnicode(initial);
2723 if (ustr == NULL) {
2724 PyErr_NoMemory();
Victor Stinner1fbcaef2011-09-30 01:54:04 +02002725 Py_DECREF(a);
2726 return NULL;
2727 }
Victor Stinner62bb3942012-08-06 00:46:05 +02002728
2729 n = PyUnicode_GET_DATA_SIZE(initial);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002730 if (n > 0) {
2731 arrayobject *self = (arrayobject *)a;
Victor Stinner62bb3942012-08-06 00:46:05 +02002732 char *item = self->ob_item;
2733 item = (char *)PyMem_Realloc(item, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002734 if (item == NULL) {
2735 PyErr_NoMemory();
2736 Py_DECREF(a);
2737 return NULL;
2738 }
Victor Stinner62bb3942012-08-06 00:46:05 +02002739 self->ob_item = item;
2740 Py_SIZE(self) = n / sizeof(Py_UNICODE);
2741 memcpy(item, ustr, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 self->allocated = Py_SIZE(self);
2743 }
2744 }
Benjamin Peterson682124c2014-10-10 20:58:30 -04002745 else if (initial != NULL && array_Check(initial) && len > 0) {
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002746 arrayobject *self = (arrayobject *)a;
2747 arrayobject *other = (arrayobject *)initial;
2748 memcpy(self->ob_item, other->ob_item, len * other->ob_descr->itemsize);
2749 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 if (it != NULL) {
2751 if (array_iter_extend((arrayobject *)a, it) == -1) {
2752 Py_DECREF(it);
2753 Py_DECREF(a);
2754 return NULL;
2755 }
2756 Py_DECREF(it);
2757 }
2758 return a;
2759 }
2760 }
2761 PyErr_SetString(PyExc_ValueError,
Meador Inge1c9f0c92011-09-20 19:55:51 -05002762 "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 +00002763 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00002764}
2765
Guido van Rossum778983b1993-02-19 15:55:02 +00002766
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002767PyDoc_STRVAR(module_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00002768"This module defines an object type which can efficiently represent\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002769an array of basic values: characters, integers, floating point\n\
2770numbers. Arrays are sequence types and behave very much like lists,\n\
Alexandre Vassalotti9730e332013-11-29 20:47:15 -08002771except that the type of objects stored in them is constrained.\n");
2772
2773PyDoc_STRVAR(arraytype_doc,
2774"array(typecode [, initializer]) -> array\n\
2775\n\
2776Return a new array whose items are restricted by typecode, and\n\
2777initialized from the optional initializer value, which must be a list,\n\
2778string or iterable over elements of the appropriate type.\n\
2779\n\
2780Arrays represent basic values and behave very much like lists, except\n\
2781the type of objects stored in them is constrained. The type is specified\n\
2782at object creation time by using a type code, which is a single character.\n\
2783The following type codes are defined:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002784\n\
oldkaa0735f2018-02-02 16:52:55 +08002785 Type code C Type Minimum size in bytes\n\
2786 'b' signed integer 1\n\
2787 'B' unsigned integer 1\n\
2788 'u' Unicode character 2 (see note)\n\
2789 'h' signed integer 2\n\
2790 'H' unsigned integer 2\n\
2791 'i' signed integer 2\n\
2792 'I' unsigned integer 2\n\
2793 'l' signed integer 4\n\
2794 'L' unsigned integer 4\n\
2795 'q' signed integer 8 (see note)\n\
2796 'Q' unsigned integer 8 (see note)\n\
2797 'f' floating point 4\n\
2798 'd' floating point 8\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002799\n\
oldkaa0735f2018-02-02 16:52:55 +08002800NOTE: The 'u' typecode corresponds to Python's unicode character. On\n\
Victor Stinner62bb3942012-08-06 00:46:05 +02002801narrow builds this is 2-bytes on wide builds this is 4-bytes.\n\
2802\n\
oldkaa0735f2018-02-02 16:52:55 +08002803NOTE: The 'q' and 'Q' type codes are only available if the platform\n\
2804C compiler used to build Python supports 'long long', or, on Windows,\n\
Meador Inge1c9f0c92011-09-20 19:55:51 -05002805'__int64'.\n\
2806\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002807Methods:\n\
2808\n\
2809append() -- append a new item to the end of the array\n\
2810buffer_info() -- return information giving the current memory info\n\
2811byteswap() -- byteswap all the items of the array\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002812count() -- return number of occurrences of an object\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00002813extend() -- extend array by appending multiple elements from an iterable\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002814fromfile() -- read items from a file object\n\
2815fromlist() -- append items from the list\n\
Florent Xiclunac45fb252011-10-24 13:14:55 +02002816frombytes() -- append items from the string\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002817index() -- return index of first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002818insert() -- insert a new item into the array at a provided position\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00002819pop() -- remove and return item (default last)\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002820remove() -- remove first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002821reverse() -- reverse the order of the items in the array\n\
2822tofile() -- write all items to a file object\n\
2823tolist() -- return the array converted to an ordinary list\n\
Florent Xiclunac45fb252011-10-24 13:14:55 +02002824tobytes() -- return the array converted to a string\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002825\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002826Attributes:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002827\n\
2828typecode -- the typecode character used to create the array\n\
2829itemsize -- the length in bytes of one array item\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002830");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002831
Raymond Hettinger625812f2003-01-07 01:58:52 +00002832static PyObject *array_iter(arrayobject *ao);
2833
Tim Peters0c322792002-07-17 16:49:03 +00002834static PyTypeObject Arraytype = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 PyVarObject_HEAD_INIT(NULL, 0)
2836 "array.array",
2837 sizeof(arrayobject),
2838 0,
2839 (destructor)array_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002840 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002841 0, /* tp_getattr */
2842 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002843 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 (reprfunc)array_repr, /* tp_repr */
2845 0, /* tp_as_number*/
2846 &array_as_sequence, /* tp_as_sequence*/
2847 &array_as_mapping, /* tp_as_mapping*/
2848 0, /* tp_hash */
2849 0, /* tp_call */
2850 0, /* tp_str */
2851 PyObject_GenericGetAttr, /* tp_getattro */
2852 0, /* tp_setattro */
2853 &array_as_buffer, /* tp_as_buffer*/
2854 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2855 arraytype_doc, /* tp_doc */
2856 0, /* tp_traverse */
2857 0, /* tp_clear */
2858 array_richcompare, /* tp_richcompare */
2859 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
2860 (getiterfunc)array_iter, /* tp_iter */
2861 0, /* tp_iternext */
2862 array_methods, /* tp_methods */
2863 0, /* tp_members */
2864 array_getsets, /* tp_getset */
2865 0, /* tp_base */
2866 0, /* tp_dict */
2867 0, /* tp_descr_get */
2868 0, /* tp_descr_set */
2869 0, /* tp_dictoffset */
2870 0, /* tp_init */
2871 PyType_GenericAlloc, /* tp_alloc */
2872 array_new, /* tp_new */
2873 PyObject_Del, /* tp_free */
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002874};
2875
Raymond Hettinger625812f2003-01-07 01:58:52 +00002876
2877/*********************** Array Iterator **************************/
2878
Brett Cannon1eb32c22014-10-10 16:26:45 -04002879/*[clinic input]
2880class array.arrayiterator "arrayiterobject *" "&PyArrayIter_Type"
2881[clinic start generated code]*/
2882/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5aefd2d74d8c8e30]*/
Raymond Hettinger625812f2003-01-07 01:58:52 +00002883
2884static PyObject *
2885array_iter(arrayobject *ao)
2886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 arrayiterobject *it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002889 if (!array_Check(ao)) {
2890 PyErr_BadInternalCall();
2891 return NULL;
2892 }
Raymond Hettinger625812f2003-01-07 01:58:52 +00002893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002894 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
2895 if (it == NULL)
2896 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898 Py_INCREF(ao);
2899 it->ao = ao;
2900 it->index = 0;
2901 it->getitem = ao->ob_descr->getitem;
2902 PyObject_GC_Track(it);
2903 return (PyObject *)it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002904}
2905
2906static PyObject *
Raymond Hettinger625812f2003-01-07 01:58:52 +00002907arrayiter_next(arrayiterobject *it)
2908{
Serhiy Storchakaab0d1982016-03-30 21:11:16 +03002909 arrayobject *ao;
2910
2911 assert(it != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 assert(PyArrayIter_Check(it));
Serhiy Storchakaab0d1982016-03-30 21:11:16 +03002913 ao = it->ao;
2914 if (ao == NULL) {
2915 return NULL;
2916 }
2917 assert(array_Check(ao));
2918 if (it->index < Py_SIZE(ao)) {
2919 return (*it->getitem)(ao, it->index++);
2920 }
2921 it->ao = NULL;
2922 Py_DECREF(ao);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002924}
2925
2926static void
2927arrayiter_dealloc(arrayiterobject *it)
2928{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 PyObject_GC_UnTrack(it);
2930 Py_XDECREF(it->ao);
2931 PyObject_GC_Del(it);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002932}
2933
2934static int
2935arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002937 Py_VISIT(it->ao);
2938 return 0;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002939}
2940
Brett Cannon1eb32c22014-10-10 16:26:45 -04002941/*[clinic input]
2942array.arrayiterator.__reduce__
2943
2944Return state information for pickling.
2945[clinic start generated code]*/
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002946
2947static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04002948array_arrayiterator___reduce___impl(arrayiterobject *self)
2949/*[clinic end generated code: output=7898a52e8e66e016 input=a062ea1e9951417a]*/
2950{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002951 _Py_IDENTIFIER(iter);
2952 PyObject *func = _PyEval_GetBuiltinId(&PyId_iter);
Serhiy Storchakaab0d1982016-03-30 21:11:16 +03002953 if (self->ao == NULL) {
2954 return Py_BuildValue("N(())", func);
2955 }
2956 return Py_BuildValue("N(O)n", func, self->ao, self->index);
Brett Cannon1eb32c22014-10-10 16:26:45 -04002957}
2958
2959/*[clinic input]
2960array.arrayiterator.__setstate__
2961
2962 state: object
2963 /
2964
2965Set state information for unpickling.
2966[clinic start generated code]*/
2967
2968static PyObject *
2969array_arrayiterator___setstate__(arrayiterobject *self, PyObject *state)
2970/*[clinic end generated code: output=397da9904e443cbe input=f47d5ceda19e787b]*/
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002971{
2972 Py_ssize_t index = PyLong_AsSsize_t(state);
2973 if (index == -1 && PyErr_Occurred())
2974 return NULL;
2975 if (index < 0)
2976 index = 0;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002977 else if (index > Py_SIZE(self->ao))
2978 index = Py_SIZE(self->ao); /* iterator exhausted */
2979 self->index = index;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002980 Py_RETURN_NONE;
2981}
2982
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002983static PyMethodDef arrayiter_methods[] = {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002984 ARRAY_ARRAYITERATOR___REDUCE___METHODDEF
2985 ARRAY_ARRAYITERATOR___SETSTATE___METHODDEF
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002986 {NULL, NULL} /* sentinel */
2987};
2988
Raymond Hettinger625812f2003-01-07 01:58:52 +00002989static PyTypeObject PyArrayIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 PyVarObject_HEAD_INIT(NULL, 0)
2991 "arrayiterator", /* tp_name */
2992 sizeof(arrayiterobject), /* tp_basicsize */
2993 0, /* tp_itemsize */
2994 /* methods */
2995 (destructor)arrayiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002996 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002997 0, /* tp_getattr */
2998 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002999 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003000 0, /* tp_repr */
3001 0, /* tp_as_number */
3002 0, /* tp_as_sequence */
3003 0, /* tp_as_mapping */
3004 0, /* tp_hash */
3005 0, /* tp_call */
3006 0, /* tp_str */
3007 PyObject_GenericGetAttr, /* tp_getattro */
3008 0, /* tp_setattro */
3009 0, /* tp_as_buffer */
3010 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3011 0, /* tp_doc */
3012 (traverseproc)arrayiter_traverse, /* tp_traverse */
3013 0, /* tp_clear */
3014 0, /* tp_richcompare */
3015 0, /* tp_weaklistoffset */
3016 PyObject_SelfIter, /* tp_iter */
3017 (iternextfunc)arrayiter_next, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003018 arrayiter_methods, /* tp_methods */
Raymond Hettinger625812f2003-01-07 01:58:52 +00003019};
3020
3021
3022/*********************** Install Module **************************/
3023
Martin v. Löwis99866332002-03-01 10:27:01 +00003024/* No functions in array module. */
3025static PyMethodDef a_methods[] = {
Brett Cannon1eb32c22014-10-10 16:26:45 -04003026 ARRAY__ARRAY_RECONSTRUCTOR_METHODDEF
Martin v. Löwis99866332002-03-01 10:27:01 +00003027 {NULL, NULL, 0, NULL} /* Sentinel */
3028};
3029
Nick Coghland5cacbb2015-05-23 22:24:10 +10003030static int
3031array_modexec(PyObject *m)
Guido van Rossum778983b1993-02-19 15:55:02 +00003032{
Georg Brandl4cb0de22011-09-28 21:49:49 +02003033 char buffer[Py_ARRAY_LENGTH(descriptors)], *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003034 PyObject *typecodes;
3035 Py_ssize_t size = 0;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02003036 const struct arraydescr *descr;
Fred Drake0d40ba42000-02-04 20:33:49 +00003037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003038 if (PyType_Ready(&Arraytype) < 0)
Nick Coghland5cacbb2015-05-23 22:24:10 +10003039 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 Py_TYPE(&PyArrayIter_Type) = &PyType_Type;
Fred Drakef4e34842002-04-01 03:45:06 +00003041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003042 Py_INCREF((PyObject *)&Arraytype);
3043 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
3044 Py_INCREF((PyObject *)&Arraytype);
3045 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00003046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047 for (descr=descriptors; descr->typecode != '\0'; descr++) {
3048 size++;
3049 }
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00003050
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003051 p = buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003052 for (descr = descriptors; descr->typecode != '\0'; descr++) {
3053 *p++ = (char)descr->typecode;
3054 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003055 typecodes = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003056
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003057 PyModule_AddObject(m, "typecodes", typecodes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058
3059 if (PyErr_Occurred()) {
3060 Py_DECREF(m);
3061 m = NULL;
3062 }
Nick Coghland5cacbb2015-05-23 22:24:10 +10003063 return 0;
3064}
3065
3066static PyModuleDef_Slot arrayslots[] = {
3067 {Py_mod_exec, array_modexec},
3068 {0, NULL}
3069};
3070
3071
3072static struct PyModuleDef arraymodule = {
3073 PyModuleDef_HEAD_INIT,
3074 "array",
3075 module_doc,
3076 0,
3077 a_methods,
3078 arrayslots,
3079 NULL,
3080 NULL,
3081 NULL
3082};
3083
3084
3085PyMODINIT_FUNC
3086PyInit_array(void)
3087{
3088 return PyModuleDef_Init(&arraymodule);
Guido van Rossum778983b1993-02-19 15:55:02 +00003089}