blob: eeda714d6a935edc72973444665185d430b5e89e [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) {
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100131 Py_SET_SIZE(self, newsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 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;
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100138 Py_SET_SIZE(self, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 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;
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100168 Py_SET_SIZE(self, newsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 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;
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100596 Py_SET_SIZE(op, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 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]
Brett Cannon1eb32c22014-10-10 16:26:45 -04001627array.array.frombytes
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001628
Brett Cannon1eb32c22014-10-10 16:26:45 -04001629 buffer: Py_buffer
1630 /
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[clinic start generated code]*/
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001634
1635static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001636array_array_frombytes_impl(arrayobject *self, Py_buffer *buffer)
1637/*[clinic end generated code: output=d9842c8f7510a516 input=2bbf2b53ebfcc988]*/
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001638{
Brett Cannon1eb32c22014-10-10 16:26:45 -04001639 return frombytes(self, buffer);
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001640}
1641
Brett Cannon1eb32c22014-10-10 16:26:45 -04001642/*[clinic input]
1643array.array.tobytes
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001644
Brett Cannon1eb32c22014-10-10 16:26:45 -04001645Convert the array to an array of machine values and return the bytes representation.
1646[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001647
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001648static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001649array_array_tobytes_impl(arrayobject *self)
1650/*[clinic end generated code: output=87318e4edcdc2bb6 input=90ee495f96de34f5]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001651{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
1653 return PyBytes_FromStringAndSize(self->ob_item,
1654 Py_SIZE(self) * self->ob_descr->itemsize);
1655 } else {
1656 return PyErr_NoMemory();
1657 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001658}
1659
Brett Cannon1eb32c22014-10-10 16:26:45 -04001660/*[clinic input]
Brett Cannon1eb32c22014-10-10 16:26:45 -04001661array.array.fromunicode
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001662
Larry Hastings38337d12015-05-07 23:30:09 -07001663 ustr: Py_UNICODE(zeroes=True)
Brett Cannon1eb32c22014-10-10 16:26:45 -04001664 /
1665
1666Extends this array with data from the unicode string ustr.
1667
1668The array must be a unicode type array; otherwise a ValueError is raised.
1669Use array.frombytes(ustr.encode(...)) to append Unicode data to an array of
1670some other type.
1671[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001672
Martin v. Löwis99866332002-03-01 10:27:01 +00001673static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001674array_array_fromunicode_impl(arrayobject *self, const Py_UNICODE *ustr,
Larry Hastings89964c42015-04-14 18:07:59 -04001675 Py_ssize_clean_t ustr_length)
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001676/*[clinic end generated code: output=cf2f662908e2befc input=150f00566ffbca6e]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001677{
Victor Stinner62bb3942012-08-06 00:46:05 +02001678 char typecode;
Martin v. Löwis99866332002-03-01 10:27:01 +00001679
Victor Stinner62bb3942012-08-06 00:46:05 +02001680 typecode = self->ob_descr->typecode;
Gregory P. Smith9504b132012-12-10 20:20:20 -08001681 if (typecode != 'u') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 PyErr_SetString(PyExc_ValueError,
1683 "fromunicode() may only be called on "
1684 "unicode type arrays");
1685 return NULL;
1686 }
Brett Cannon1eb32c22014-10-10 16:26:45 -04001687 if (ustr_length > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 Py_ssize_t old_size = Py_SIZE(self);
Brett Cannon1eb32c22014-10-10 16:26:45 -04001689 if (array_resize(self, old_size + ustr_length) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 return NULL;
Victor Stinner62bb3942012-08-06 00:46:05 +02001691 memcpy(self->ob_item + old_size * sizeof(Py_UNICODE),
Brett Cannon1eb32c22014-10-10 16:26:45 -04001692 ustr, ustr_length * sizeof(Py_UNICODE));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 }
Martin v. Löwis99866332002-03-01 10:27:01 +00001694
Brett Cannon1eb32c22014-10-10 16:26:45 -04001695 Py_RETURN_NONE;
Martin v. Löwis99866332002-03-01 10:27:01 +00001696}
1697
Brett Cannon1eb32c22014-10-10 16:26:45 -04001698/*[clinic input]
1699array.array.tounicode
Martin v. Löwis99866332002-03-01 10:27:01 +00001700
Brett Cannon1eb32c22014-10-10 16:26:45 -04001701Extends this array with data from the unicode string ustr.
1702
1703Convert the array to a unicode string. The array must be a unicode type array;
1704otherwise a ValueError is raised. Use array.tobytes().decode() to obtain a
1705unicode string from an array of some other type.
1706[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001707
1708static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001709array_array_tounicode_impl(arrayobject *self)
1710/*[clinic end generated code: output=08e442378336e1ef input=127242eebe70b66d]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001711{
Victor Stinner62bb3942012-08-06 00:46:05 +02001712 char typecode;
1713 typecode = self->ob_descr->typecode;
Gregory P. Smith9504b132012-12-10 20:20:20 -08001714 if (typecode != 'u') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 PyErr_SetString(PyExc_ValueError,
1716 "tounicode() may only be called on unicode type arrays");
1717 return NULL;
1718 }
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02001719 return PyUnicode_FromWideChar((Py_UNICODE *) self->ob_item, Py_SIZE(self));
Martin v. Löwis99866332002-03-01 10:27:01 +00001720}
1721
Brett Cannon1eb32c22014-10-10 16:26:45 -04001722/*[clinic input]
1723array.array.__sizeof__
Martin v. Löwis99866332002-03-01 10:27:01 +00001724
Brett Cannon1eb32c22014-10-10 16:26:45 -04001725Size of the array in memory, in bytes.
1726[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001727
Meador Inge03b4d502012-08-10 22:35:45 -05001728static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001729array_array___sizeof___impl(arrayobject *self)
1730/*[clinic end generated code: output=d8e1c61ebbe3eaed input=805586565bf2b3c6]*/
Meador Inge03b4d502012-08-10 22:35:45 -05001731{
1732 Py_ssize_t res;
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02001733 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * self->ob_descr->itemsize;
Meador Inge03b4d502012-08-10 22:35:45 -05001734 return PyLong_FromSsize_t(res);
1735}
1736
Martin v. Löwis99866332002-03-01 10:27:01 +00001737
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001738/*********************** Pickling support ************************/
1739
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001740static const struct mformatdescr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 size_t size;
1742 int is_signed;
1743 int is_big_endian;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001744} mformat_descriptors[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 {1, 0, 0}, /* 0: UNSIGNED_INT8 */
1746 {1, 1, 0}, /* 1: SIGNED_INT8 */
1747 {2, 0, 0}, /* 2: UNSIGNED_INT16_LE */
1748 {2, 0, 1}, /* 3: UNSIGNED_INT16_BE */
1749 {2, 1, 0}, /* 4: SIGNED_INT16_LE */
1750 {2, 1, 1}, /* 5: SIGNED_INT16_BE */
1751 {4, 0, 0}, /* 6: UNSIGNED_INT32_LE */
1752 {4, 0, 1}, /* 7: UNSIGNED_INT32_BE */
1753 {4, 1, 0}, /* 8: SIGNED_INT32_LE */
1754 {4, 1, 1}, /* 9: SIGNED_INT32_BE */
1755 {8, 0, 0}, /* 10: UNSIGNED_INT64_LE */
1756 {8, 0, 1}, /* 11: UNSIGNED_INT64_BE */
1757 {8, 1, 0}, /* 12: SIGNED_INT64_LE */
1758 {8, 1, 1}, /* 13: SIGNED_INT64_BE */
1759 {4, 0, 0}, /* 14: IEEE_754_FLOAT_LE */
1760 {4, 0, 1}, /* 15: IEEE_754_FLOAT_BE */
1761 {8, 0, 0}, /* 16: IEEE_754_DOUBLE_LE */
1762 {8, 0, 1}, /* 17: IEEE_754_DOUBLE_BE */
1763 {4, 0, 0}, /* 18: UTF16_LE */
1764 {4, 0, 1}, /* 19: UTF16_BE */
1765 {8, 0, 0}, /* 20: UTF32_LE */
1766 {8, 0, 1} /* 21: UTF32_BE */
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001767};
1768
1769
1770/*
1771 * Internal: This function is used to find the machine format of a given
1772 * array type code. This returns UNKNOWN_FORMAT when the machine format cannot
1773 * be found.
1774 */
1775static enum machine_format_code
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001776typecode_to_mformat_code(char typecode)
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001777{
Christian Heimes743e0cd2012-10-17 23:52:17 +02001778 const int is_big_endian = PY_BIG_ENDIAN;
1779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 size_t intsize;
1781 int is_signed;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 switch (typecode) {
1784 case 'b':
1785 return SIGNED_INT8;
1786 case 'B':
1787 return UNSIGNED_INT8;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 case 'u':
Victor Stinner62bb3942012-08-06 00:46:05 +02001790 if (sizeof(Py_UNICODE) == 2) {
1791 return UTF16_LE + is_big_endian;
1792 }
1793 if (sizeof(Py_UNICODE) == 4) {
1794 return UTF32_LE + is_big_endian;
1795 }
1796 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 case 'f':
1799 if (sizeof(float) == 4) {
1800 const float y = 16711938.0;
1801 if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0)
1802 return IEEE_754_FLOAT_BE;
1803 if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0)
1804 return IEEE_754_FLOAT_LE;
1805 }
1806 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 case 'd':
1809 if (sizeof(double) == 8) {
1810 const double x = 9006104071832581.0;
1811 if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0)
1812 return IEEE_754_DOUBLE_BE;
1813 if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
1814 return IEEE_754_DOUBLE_LE;
1815 }
1816 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 /* Integers */
1819 case 'h':
1820 intsize = sizeof(short);
1821 is_signed = 1;
1822 break;
1823 case 'H':
1824 intsize = sizeof(short);
1825 is_signed = 0;
1826 break;
1827 case 'i':
1828 intsize = sizeof(int);
1829 is_signed = 1;
1830 break;
1831 case 'I':
1832 intsize = sizeof(int);
1833 is_signed = 0;
1834 break;
1835 case 'l':
1836 intsize = sizeof(long);
1837 is_signed = 1;
1838 break;
1839 case 'L':
1840 intsize = sizeof(long);
1841 is_signed = 0;
1842 break;
Meador Inge1c9f0c92011-09-20 19:55:51 -05001843 case 'q':
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001844 intsize = sizeof(long long);
Meador Inge1c9f0c92011-09-20 19:55:51 -05001845 is_signed = 1;
1846 break;
1847 case 'Q':
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001848 intsize = sizeof(long long);
Meador Inge1c9f0c92011-09-20 19:55:51 -05001849 is_signed = 0;
1850 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 default:
1852 return UNKNOWN_FORMAT;
1853 }
1854 switch (intsize) {
1855 case 2:
1856 return UNSIGNED_INT16_LE + is_big_endian + (2 * is_signed);
1857 case 4:
1858 return UNSIGNED_INT32_LE + is_big_endian + (2 * is_signed);
1859 case 8:
1860 return UNSIGNED_INT64_LE + is_big_endian + (2 * is_signed);
1861 default:
1862 return UNKNOWN_FORMAT;
1863 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001864}
1865
1866/* Forward declaration. */
1867static PyObject *array_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
1868
1869/*
1870 * Internal: This function wraps the array constructor--i.e., array_new()--to
1871 * allow the creation of array objects from C code without having to deal
1872 * directly the tuple argument of array_new(). The typecode argument is a
1873 * Unicode character value, like 'i' or 'f' for example, representing an array
1874 * type code. The items argument is a bytes or a list object from which
1875 * contains the initial value of the array.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 *
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001877 * On success, this functions returns the array object created. Otherwise,
1878 * NULL is returned to indicate a failure.
1879 */
1880static PyObject *
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001881make_array(PyTypeObject *arraytype, char typecode, PyObject *items)
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001882{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 PyObject *new_args;
1884 PyObject *array_obj;
1885 PyObject *typecode_obj;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 assert(arraytype != NULL);
1888 assert(items != NULL);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001889
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001890 typecode_obj = PyUnicode_FromOrdinal(typecode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 if (typecode_obj == NULL)
1892 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 new_args = PyTuple_New(2);
Mat M56935a52017-11-14 01:00:54 -05001895 if (new_args == NULL) {
1896 Py_DECREF(typecode_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 return NULL;
Mat M56935a52017-11-14 01:00:54 -05001898 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 Py_INCREF(items);
1900 PyTuple_SET_ITEM(new_args, 0, typecode_obj);
1901 PyTuple_SET_ITEM(new_args, 1, items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 array_obj = array_new(arraytype, new_args, NULL);
1904 Py_DECREF(new_args);
1905 if (array_obj == NULL)
1906 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 return array_obj;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001909}
1910
1911/*
1912 * This functions is a special constructor used when unpickling an array. It
1913 * provides a portable way to rebuild an array from its memory representation.
1914 */
Brett Cannon1eb32c22014-10-10 16:26:45 -04001915/*[clinic input]
1916array._array_reconstructor
1917
1918 arraytype: object(type="PyTypeObject *")
Larry Hastingsdbfdc382015-05-04 06:59:46 -07001919 typecode: int(accept={str})
Brett Cannon1eb32c22014-10-10 16:26:45 -04001920 mformat_code: int(type="enum machine_format_code")
1921 items: object
1922 /
1923
1924Internal. Used for pickling support.
1925[clinic start generated code]*/
1926
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001927static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001928array__array_reconstructor_impl(PyObject *module, PyTypeObject *arraytype,
Larry Hastings89964c42015-04-14 18:07:59 -04001929 int typecode,
1930 enum machine_format_code mformat_code,
1931 PyObject *items)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001932/*[clinic end generated code: output=e05263141ba28365 input=2464dc8f4c7736b5]*/
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001933{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 PyObject *converted_items;
1935 PyObject *result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001936 const struct arraydescr *descr;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 if (!PyType_Check(arraytype)) {
1939 PyErr_Format(PyExc_TypeError,
Sylvaina90e64b2017-03-29 20:09:22 +02001940 "first argument must be a type object, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 Py_TYPE(arraytype)->tp_name);
1942 return NULL;
1943 }
1944 if (!PyType_IsSubtype(arraytype, &Arraytype)) {
1945 PyErr_Format(PyExc_TypeError,
1946 "%.200s is not a subtype of %.200s",
1947 arraytype->tp_name, Arraytype.tp_name);
1948 return NULL;
1949 }
1950 for (descr = descriptors; descr->typecode != '\0'; descr++) {
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001951 if ((int)descr->typecode == typecode)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 break;
1953 }
1954 if (descr->typecode == '\0') {
1955 PyErr_SetString(PyExc_ValueError,
1956 "second argument must be a valid type code");
1957 return NULL;
1958 }
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001959 if (mformat_code < MACHINE_FORMAT_CODE_MIN ||
1960 mformat_code > MACHINE_FORMAT_CODE_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 PyErr_SetString(PyExc_ValueError,
1962 "third argument must be a valid machine format code.");
1963 return NULL;
1964 }
1965 if (!PyBytes_Check(items)) {
1966 PyErr_Format(PyExc_TypeError,
1967 "fourth argument should be bytes, not %.200s",
1968 Py_TYPE(items)->tp_name);
1969 return NULL;
1970 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 /* Fast path: No decoding has to be done. */
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001973 if (mformat_code == typecode_to_mformat_code((char)typecode) ||
1974 mformat_code == UNKNOWN_FORMAT) {
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001975 return make_array(arraytype, (char)typecode, items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 /* Slow path: Decode the byte string according to the given machine
1979 * format code. This occurs when the computer unpickling the array
1980 * object is architecturally different from the one that pickled the
1981 * array.
1982 */
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001983 if (Py_SIZE(items) % mformat_descriptors[mformat_code].size != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 PyErr_SetString(PyExc_ValueError,
1985 "string length not a multiple of item size");
1986 return NULL;
1987 }
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001988 switch (mformat_code) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 case IEEE_754_FLOAT_LE:
1990 case IEEE_754_FLOAT_BE: {
sthaa3ecb82019-03-20 20:49:39 +01001991 Py_ssize_t i;
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001992 int le = (mformat_code == IEEE_754_FLOAT_LE) ? 1 : 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001993 Py_ssize_t itemcount = Py_SIZE(items) / 4;
1994 const unsigned char *memstr =
1995 (unsigned char *)PyBytes_AS_STRING(items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 converted_items = PyList_New(itemcount);
1998 if (converted_items == NULL)
1999 return NULL;
2000 for (i = 0; i < itemcount; i++) {
2001 PyObject *pyfloat = PyFloat_FromDouble(
2002 _PyFloat_Unpack4(&memstr[i * 4], le));
2003 if (pyfloat == NULL) {
2004 Py_DECREF(converted_items);
2005 return NULL;
2006 }
2007 PyList_SET_ITEM(converted_items, i, pyfloat);
2008 }
2009 break;
2010 }
2011 case IEEE_754_DOUBLE_LE:
2012 case IEEE_754_DOUBLE_BE: {
sthaa3ecb82019-03-20 20:49:39 +01002013 Py_ssize_t i;
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002014 int le = (mformat_code == IEEE_754_DOUBLE_LE) ? 1 : 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 Py_ssize_t itemcount = Py_SIZE(items) / 8;
2016 const unsigned char *memstr =
2017 (unsigned char *)PyBytes_AS_STRING(items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 converted_items = PyList_New(itemcount);
2020 if (converted_items == NULL)
2021 return NULL;
2022 for (i = 0; i < itemcount; i++) {
2023 PyObject *pyfloat = PyFloat_FromDouble(
2024 _PyFloat_Unpack8(&memstr[i * 8], le));
2025 if (pyfloat == NULL) {
2026 Py_DECREF(converted_items);
2027 return NULL;
2028 }
2029 PyList_SET_ITEM(converted_items, i, pyfloat);
2030 }
2031 break;
2032 }
2033 case UTF16_LE:
2034 case UTF16_BE: {
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002035 int byteorder = (mformat_code == UTF16_LE) ? -1 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 converted_items = PyUnicode_DecodeUTF16(
2037 PyBytes_AS_STRING(items), Py_SIZE(items),
2038 "strict", &byteorder);
2039 if (converted_items == NULL)
2040 return NULL;
2041 break;
2042 }
2043 case UTF32_LE:
2044 case UTF32_BE: {
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002045 int byteorder = (mformat_code == UTF32_LE) ? -1 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 converted_items = PyUnicode_DecodeUTF32(
2047 PyBytes_AS_STRING(items), Py_SIZE(items),
2048 "strict", &byteorder);
2049 if (converted_items == NULL)
2050 return NULL;
2051 break;
2052 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 case UNSIGNED_INT8:
2055 case SIGNED_INT8:
2056 case UNSIGNED_INT16_LE:
2057 case UNSIGNED_INT16_BE:
2058 case SIGNED_INT16_LE:
2059 case SIGNED_INT16_BE:
2060 case UNSIGNED_INT32_LE:
2061 case UNSIGNED_INT32_BE:
2062 case SIGNED_INT32_LE:
2063 case SIGNED_INT32_BE:
2064 case UNSIGNED_INT64_LE:
2065 case UNSIGNED_INT64_BE:
2066 case SIGNED_INT64_LE:
2067 case SIGNED_INT64_BE: {
sthaa3ecb82019-03-20 20:49:39 +01002068 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 const struct mformatdescr mf_descr =
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002070 mformat_descriptors[mformat_code];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 Py_ssize_t itemcount = Py_SIZE(items) / mf_descr.size;
2072 const unsigned char *memstr =
2073 (unsigned char *)PyBytes_AS_STRING(items);
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002074 const struct arraydescr *descr;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 /* If possible, try to pack array's items using a data type
2077 * that fits better. This may result in an array with narrower
2078 * or wider elements.
2079 *
Martin Panter4c359642016-05-08 13:53:41 +00002080 * For example, if a 32-bit machine pickles an L-code array of
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 * unsigned longs, then the array will be unpickled by 64-bit
2082 * machine as an I-code array of unsigned ints.
2083 *
2084 * XXX: Is it possible to write a unit test for this?
2085 */
2086 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2087 if (descr->is_integer_type &&
Victor Stinner706768c2014-08-16 01:03:39 +02002088 (size_t)descr->itemsize == mf_descr.size &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 descr->is_signed == mf_descr.is_signed)
2090 typecode = descr->typecode;
2091 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 converted_items = PyList_New(itemcount);
2094 if (converted_items == NULL)
2095 return NULL;
2096 for (i = 0; i < itemcount; i++) {
2097 PyObject *pylong;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 pylong = _PyLong_FromByteArray(
2100 &memstr[i * mf_descr.size],
2101 mf_descr.size,
2102 !mf_descr.is_big_endian,
2103 mf_descr.is_signed);
2104 if (pylong == NULL) {
2105 Py_DECREF(converted_items);
2106 return NULL;
2107 }
2108 PyList_SET_ITEM(converted_items, i, pylong);
2109 }
2110 break;
2111 }
2112 case UNKNOWN_FORMAT:
2113 /* Impossible, but needed to shut up GCC about the unhandled
2114 * enumeration value.
2115 */
2116 default:
2117 PyErr_BadArgument();
2118 return NULL;
2119 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002120
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002121 result = make_array(arraytype, (char)typecode, converted_items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 Py_DECREF(converted_items);
2123 return result;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002124}
2125
Brett Cannon1eb32c22014-10-10 16:26:45 -04002126/*[clinic input]
2127array.array.__reduce_ex__
2128
2129 value: object
2130 /
2131
2132Return state information for pickling.
2133[clinic start generated code]*/
2134
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002135static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04002136array_array___reduce_ex__(arrayobject *self, PyObject *value)
2137/*[clinic end generated code: output=051e0a6175d0eddb input=c36c3f85de7df6cd]*/
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 PyObject *dict;
2140 PyObject *result;
2141 PyObject *array_str;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002142 int typecode = self->ob_descr->typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 int mformat_code;
2144 static PyObject *array_reconstructor = NULL;
2145 long protocol;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002146 _Py_IDENTIFIER(_array_reconstructor);
2147 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002149 if (array_reconstructor == NULL) {
2150 PyObject *array_module = PyImport_ImportModule("array");
2151 if (array_module == NULL)
2152 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002153 array_reconstructor = _PyObject_GetAttrId(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 array_module,
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002155 &PyId__array_reconstructor);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156 Py_DECREF(array_module);
2157 if (array_reconstructor == NULL)
2158 return NULL;
2159 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 if (!PyLong_Check(value)) {
2162 PyErr_SetString(PyExc_TypeError,
Sylvaina90e64b2017-03-29 20:09:22 +02002163 "__reduce_ex__ argument should be an integer");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 return NULL;
2165 }
2166 protocol = PyLong_AsLong(value);
2167 if (protocol == -1 && PyErr_Occurred())
2168 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002169
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002170 if (_PyObject_LookupAttrId((PyObject *)self, &PyId___dict__, &dict) < 0) {
2171 return NULL;
2172 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 dict = Py_None;
2175 Py_INCREF(dict);
2176 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 mformat_code = typecode_to_mformat_code(typecode);
2179 if (mformat_code == UNKNOWN_FORMAT || protocol < 3) {
2180 /* Convert the array to a list if we got something weird
2181 * (e.g., non-IEEE floats), or we are pickling the array using
2182 * a Python 2.x compatible protocol.
2183 *
2184 * It is necessary to use a list representation for Python 2.x
2185 * compatible pickle protocol, since Python 2's str objects
2186 * are unpickled as unicode by Python 3. Thus it is impossible
2187 * to make arrays unpicklable by Python 3 by using their memory
2188 * representation, unless we resort to ugly hacks such as
2189 * coercing unicode objects to bytes in array_reconstructor.
2190 */
2191 PyObject *list;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002192 list = array_array_tolist_impl(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 if (list == NULL) {
2194 Py_DECREF(dict);
2195 return NULL;
2196 }
2197 result = Py_BuildValue(
Brett Cannon1eb32c22014-10-10 16:26:45 -04002198 "O(CO)O", Py_TYPE(self), typecode, list, dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 Py_DECREF(list);
2200 Py_DECREF(dict);
2201 return result;
2202 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002203
Brett Cannon1eb32c22014-10-10 16:26:45 -04002204 array_str = array_array_tobytes_impl(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 if (array_str == NULL) {
2206 Py_DECREF(dict);
2207 return NULL;
2208 }
2209 result = Py_BuildValue(
Brett Cannon1eb32c22014-10-10 16:26:45 -04002210 "O(OCiN)O", array_reconstructor, Py_TYPE(self), typecode,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 mformat_code, array_str, dict);
2212 Py_DECREF(dict);
2213 return result;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002214}
2215
Martin v. Löwis99866332002-03-01 10:27:01 +00002216static PyObject *
2217array_get_typecode(arrayobject *a, void *closure)
2218{
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002219 char typecode = a->ob_descr->typecode;
2220 return PyUnicode_FromOrdinal(typecode);
Martin v. Löwis99866332002-03-01 10:27:01 +00002221}
2222
2223static PyObject *
2224array_get_itemsize(arrayobject *a, void *closure)
2225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 return PyLong_FromLong((long)a->ob_descr->itemsize);
Martin v. Löwis99866332002-03-01 10:27:01 +00002227}
2228
2229static PyGetSetDef array_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 {"typecode", (getter) array_get_typecode, NULL,
2231 "the typecode character used to create the array"},
2232 {"itemsize", (getter) array_get_itemsize, NULL,
2233 "the size, in bytes, of one array item"},
2234 {NULL}
Martin v. Löwis99866332002-03-01 10:27:01 +00002235};
2236
Martin v. Löwis59683e82008-06-13 07:50:45 +00002237static PyMethodDef array_methods[] = {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002238 ARRAY_ARRAY_APPEND_METHODDEF
2239 ARRAY_ARRAY_BUFFER_INFO_METHODDEF
2240 ARRAY_ARRAY_BYTESWAP_METHODDEF
2241 ARRAY_ARRAY___COPY___METHODDEF
2242 ARRAY_ARRAY_COUNT_METHODDEF
2243 ARRAY_ARRAY___DEEPCOPY___METHODDEF
2244 ARRAY_ARRAY_EXTEND_METHODDEF
2245 ARRAY_ARRAY_FROMFILE_METHODDEF
2246 ARRAY_ARRAY_FROMLIST_METHODDEF
Brett Cannon1eb32c22014-10-10 16:26:45 -04002247 ARRAY_ARRAY_FROMBYTES_METHODDEF
2248 ARRAY_ARRAY_FROMUNICODE_METHODDEF
2249 ARRAY_ARRAY_INDEX_METHODDEF
2250 ARRAY_ARRAY_INSERT_METHODDEF
2251 ARRAY_ARRAY_POP_METHODDEF
2252 ARRAY_ARRAY___REDUCE_EX___METHODDEF
2253 ARRAY_ARRAY_REMOVE_METHODDEF
2254 ARRAY_ARRAY_REVERSE_METHODDEF
2255 ARRAY_ARRAY_TOFILE_METHODDEF
2256 ARRAY_ARRAY_TOLIST_METHODDEF
Brett Cannon1eb32c22014-10-10 16:26:45 -04002257 ARRAY_ARRAY_TOBYTES_METHODDEF
2258 ARRAY_ARRAY_TOUNICODE_METHODDEF
2259 ARRAY_ARRAY___SIZEOF___METHODDEF
2260 {NULL, NULL} /* sentinel */
Guido van Rossum778983b1993-02-19 15:55:02 +00002261};
2262
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002263static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00002264array_repr(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +00002265{
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002266 char typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 PyObject *s, *v = NULL;
2268 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00002269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 len = Py_SIZE(a);
2271 typecode = a->ob_descr->typecode;
2272 if (len == 0) {
Serhiy Storchakab3a77962017-09-21 14:24:13 +03002273 return PyUnicode_FromFormat("%s('%c')",
2274 _PyType_Name(Py_TYPE(a)), (int)typecode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 }
Gregory P. Smith9504b132012-12-10 20:20:20 -08002276 if (typecode == 'u') {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002277 v = array_array_tounicode_impl(a);
Gregory P. Smith9504b132012-12-10 20:20:20 -08002278 } else {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002279 v = array_array_tolist_impl(a);
Gregory P. Smith9504b132012-12-10 20:20:20 -08002280 }
Victor Stinner29ec5952013-02-26 00:27:38 +01002281 if (v == NULL)
2282 return NULL;
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00002283
Serhiy Storchakab3a77962017-09-21 14:24:13 +03002284 s = PyUnicode_FromFormat("%s('%c', %R)",
2285 _PyType_Name(Py_TYPE(a)), (int)typecode, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 Py_DECREF(v);
2287 return s;
Guido van Rossum778983b1993-02-19 15:55:02 +00002288}
2289
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002290static PyObject*
2291array_subscr(arrayobject* self, PyObject* item)
2292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 if (PyIndex_Check(item)) {
2294 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2295 if (i==-1 && PyErr_Occurred()) {
2296 return NULL;
2297 }
2298 if (i < 0)
2299 i += Py_SIZE(self);
2300 return array_item(self, i);
2301 }
2302 else if (PySlice_Check(item)) {
Zackery Spytz14514d92019-05-17 01:13:03 -06002303 Py_ssize_t start, stop, step, slicelength, i;
2304 size_t cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 PyObject* result;
2306 arrayobject* ar;
2307 int itemsize = self->ob_descr->itemsize;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002308
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002309 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 return NULL;
2311 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002312 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2313 step);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 if (slicelength <= 0) {
2316 return newarrayobject(&Arraytype, 0, self->ob_descr);
2317 }
2318 else if (step == 1) {
2319 PyObject *result = newarrayobject(&Arraytype,
2320 slicelength, self->ob_descr);
2321 if (result == NULL)
2322 return NULL;
2323 memcpy(((arrayobject *)result)->ob_item,
2324 self->ob_item + start * itemsize,
2325 slicelength * itemsize);
2326 return result;
2327 }
2328 else {
2329 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
2330 if (!result) return NULL;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 ar = (arrayobject*)result;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 for (cur = start, i = 0; i < slicelength;
2335 cur += step, i++) {
2336 memcpy(ar->ob_item + i*itemsize,
2337 self->ob_item + cur*itemsize,
2338 itemsize);
2339 }
2340
2341 return result;
2342 }
2343 }
2344 else {
2345 PyErr_SetString(PyExc_TypeError,
2346 "array indices must be integers");
2347 return NULL;
2348 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002349}
2350
2351static int
2352array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
2353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 Py_ssize_t start, stop, step, slicelength, needed;
2355 arrayobject* other;
2356 int itemsize;
Thomas Woutersed03b412007-08-28 21:37:11 +00002357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 if (PyIndex_Check(item)) {
2359 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Alexandre Vassalotti47137252009-07-05 19:57:00 +00002360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 if (i == -1 && PyErr_Occurred())
2362 return -1;
2363 if (i < 0)
2364 i += Py_SIZE(self);
2365 if (i < 0 || i >= Py_SIZE(self)) {
2366 PyErr_SetString(PyExc_IndexError,
2367 "array assignment index out of range");
2368 return -1;
2369 }
2370 if (value == NULL) {
2371 /* Fall through to slice assignment */
2372 start = i;
2373 stop = i + 1;
2374 step = 1;
2375 slicelength = 1;
2376 }
2377 else
2378 return (*self->ob_descr->setitem)(self, i, value);
2379 }
2380 else if (PySlice_Check(item)) {
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002381 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 return -1;
2383 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002384 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2385 step);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 }
2387 else {
2388 PyErr_SetString(PyExc_TypeError,
Sylvaina90e64b2017-03-29 20:09:22 +02002389 "array indices must be integers");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 return -1;
2391 }
2392 if (value == NULL) {
2393 other = NULL;
2394 needed = 0;
2395 }
2396 else if (array_Check(value)) {
2397 other = (arrayobject *)value;
2398 needed = Py_SIZE(other);
2399 if (self == other) {
2400 /* Special case "self[i:j] = self" -- copy self first */
2401 int ret;
2402 value = array_slice(other, 0, needed);
2403 if (value == NULL)
2404 return -1;
2405 ret = array_ass_subscr(self, item, value);
2406 Py_DECREF(value);
2407 return ret;
2408 }
2409 if (other->ob_descr != self->ob_descr) {
2410 PyErr_BadArgument();
2411 return -1;
2412 }
2413 }
2414 else {
2415 PyErr_Format(PyExc_TypeError,
2416 "can only assign array (not \"%.200s\") to array slice",
2417 Py_TYPE(value)->tp_name);
2418 return -1;
2419 }
2420 itemsize = self->ob_descr->itemsize;
2421 /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
2422 if ((step > 0 && stop < start) ||
2423 (step < 0 && stop > start))
2424 stop = start;
Alexandre Vassalotti47137252009-07-05 19:57:00 +00002425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002426 /* Issue #4509: If the array has exported buffers and the slice
2427 assignment would change the size of the array, fail early to make
2428 sure we don't modify it. */
2429 if ((needed == 0 || slicelength != needed) && self->ob_exports > 0) {
2430 PyErr_SetString(PyExc_BufferError,
2431 "cannot resize an array that is exporting buffers");
2432 return -1;
2433 }
Mark Dickinsonbc099642010-01-29 17:27:24 +00002434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 if (step == 1) {
2436 if (slicelength > needed) {
2437 memmove(self->ob_item + (start + needed) * itemsize,
2438 self->ob_item + stop * itemsize,
2439 (Py_SIZE(self) - stop) * itemsize);
2440 if (array_resize(self, Py_SIZE(self) +
2441 needed - slicelength) < 0)
2442 return -1;
2443 }
2444 else if (slicelength < needed) {
2445 if (array_resize(self, Py_SIZE(self) +
2446 needed - slicelength) < 0)
2447 return -1;
2448 memmove(self->ob_item + (start + needed) * itemsize,
2449 self->ob_item + stop * itemsize,
2450 (Py_SIZE(self) - start - needed) * itemsize);
2451 }
2452 if (needed > 0)
2453 memcpy(self->ob_item + start * itemsize,
2454 other->ob_item, needed * itemsize);
2455 return 0;
2456 }
2457 else if (needed == 0) {
2458 /* Delete slice */
2459 size_t cur;
2460 Py_ssize_t i;
Thomas Woutersed03b412007-08-28 21:37:11 +00002461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 if (step < 0) {
2463 stop = start + 1;
2464 start = stop + step * (slicelength - 1) - 1;
2465 step = -step;
2466 }
2467 for (cur = start, i = 0; i < slicelength;
2468 cur += step, i++) {
2469 Py_ssize_t lim = step - 1;
Thomas Woutersed03b412007-08-28 21:37:11 +00002470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002471 if (cur + step >= (size_t)Py_SIZE(self))
2472 lim = Py_SIZE(self) - cur - 1;
2473 memmove(self->ob_item + (cur - i) * itemsize,
2474 self->ob_item + (cur + 1) * itemsize,
2475 lim * itemsize);
2476 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002477 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 if (cur < (size_t)Py_SIZE(self)) {
2479 memmove(self->ob_item + (cur-slicelength) * itemsize,
2480 self->ob_item + cur * itemsize,
2481 (Py_SIZE(self) - cur) * itemsize);
2482 }
2483 if (array_resize(self, Py_SIZE(self) - slicelength) < 0)
2484 return -1;
2485 return 0;
2486 }
2487 else {
Zackery Spytz14514d92019-05-17 01:13:03 -06002488 size_t cur;
2489 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490
2491 if (needed != slicelength) {
2492 PyErr_Format(PyExc_ValueError,
2493 "attempt to assign array of size %zd "
2494 "to extended slice of size %zd",
2495 needed, slicelength);
2496 return -1;
2497 }
2498 for (cur = start, i = 0; i < slicelength;
2499 cur += step, i++) {
2500 memcpy(self->ob_item + cur * itemsize,
2501 other->ob_item + i * itemsize,
2502 itemsize);
2503 }
2504 return 0;
2505 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002506}
2507
2508static PyMappingMethods array_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 (lenfunc)array_length,
2510 (binaryfunc)array_subscr,
2511 (objobjargproc)array_ass_subscr
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002512};
2513
Guido van Rossumd8faa362007-04-27 19:54:29 +00002514static const void *emptybuf = "";
2515
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002516
2517static int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00002518array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002519{
Stefan Krah650c1e82015-02-03 21:43:23 +01002520 if (view == NULL) {
2521 PyErr_SetString(PyExc_BufferError,
2522 "array_buffer_getbuf: view==NULL argument is obsolete");
2523 return -1;
2524 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002526 view->buf = (void *)self->ob_item;
2527 view->obj = (PyObject*)self;
2528 Py_INCREF(self);
2529 if (view->buf == NULL)
2530 view->buf = (void *)emptybuf;
2531 view->len = (Py_SIZE(self)) * self->ob_descr->itemsize;
2532 view->readonly = 0;
2533 view->ndim = 1;
2534 view->itemsize = self->ob_descr->itemsize;
2535 view->suboffsets = NULL;
2536 view->shape = NULL;
2537 if ((flags & PyBUF_ND)==PyBUF_ND) {
2538 view->shape = &((Py_SIZE(self)));
2539 }
2540 view->strides = NULL;
2541 if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES)
2542 view->strides = &(view->itemsize);
2543 view->format = NULL;
2544 view->internal = NULL;
Victor Stinner62bb3942012-08-06 00:46:05 +02002545 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002546 view->format = (char *)self->ob_descr->formats;
Victor Stinner62bb3942012-08-06 00:46:05 +02002547#ifdef Py_UNICODE_WIDE
2548 if (self->ob_descr->typecode == 'u') {
2549 view->format = "w";
2550 }
2551#endif
2552 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002554 self->ob_exports++;
2555 return 0;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002556}
2557
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002558static void
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00002559array_buffer_relbuf(arrayobject *self, Py_buffer *view)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002561 self->ob_exports--;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002562}
2563
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002564static PySequenceMethods array_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 (lenfunc)array_length, /*sq_length*/
2566 (binaryfunc)array_concat, /*sq_concat*/
2567 (ssizeargfunc)array_repeat, /*sq_repeat*/
2568 (ssizeargfunc)array_item, /*sq_item*/
2569 0, /*sq_slice*/
2570 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
2571 0, /*sq_ass_slice*/
2572 (objobjproc)array_contains, /*sq_contains*/
2573 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
2574 (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/
Guido van Rossum778983b1993-02-19 15:55:02 +00002575};
2576
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002577static PyBufferProcs array_as_buffer = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 (getbufferproc)array_buffer_getbuf,
2579 (releasebufferproc)array_buffer_relbuf
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002580};
2581
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002582static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +00002583array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum778983b1993-02-19 15:55:02 +00002584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 int c;
2586 PyObject *initial = NULL, *it = NULL;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002587 const struct arraydescr *descr;
Martin v. Löwis99866332002-03-01 10:27:01 +00002588
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03002589 if (type == &Arraytype && !_PyArg_NoKeywords("array.array", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002590 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00002591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002592 if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
2593 return NULL;
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00002594
Steve Dowerb82e17e2019-05-23 08:45:22 -07002595 if (PySys_Audit("array.__new__", "CO",
2596 c, initial ? initial : Py_None) < 0) {
2597 return NULL;
2598 }
2599
Alexandre Vassalotti9730e332013-11-29 20:47:15 -08002600 if (initial && c != 'u') {
2601 if (PyUnicode_Check(initial)) {
2602 PyErr_Format(PyExc_TypeError, "cannot use a str to initialize "
2603 "an array with typecode '%c'", c);
2604 return NULL;
2605 }
2606 else if (array_Check(initial) &&
2607 ((arrayobject*)initial)->ob_descr->typecode == 'u') {
2608 PyErr_Format(PyExc_TypeError, "cannot use a unicode array to "
2609 "initialize an array with typecode '%c'", c);
2610 return NULL;
2611 }
2612 }
2613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002614 if (!(initial == NULL || PyList_Check(initial)
2615 || PyByteArray_Check(initial)
2616 || PyBytes_Check(initial)
2617 || PyTuple_Check(initial)
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002618 || ((c=='u') && PyUnicode_Check(initial))
2619 || (array_Check(initial)
2620 && c == ((arrayobject*)initial)->ob_descr->typecode))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 it = PyObject_GetIter(initial);
2622 if (it == NULL)
2623 return NULL;
2624 /* We set initial to NULL so that the subsequent code
2625 will create an empty array of the appropriate type
2626 and afterwards we can use array_iter_extend to populate
2627 the array.
2628 */
2629 initial = NULL;
2630 }
2631 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2632 if (descr->typecode == c) {
2633 PyObject *a;
2634 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00002635
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002636 if (initial == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002637 len = 0;
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002638 else if (PyList_Check(initial))
2639 len = PyList_GET_SIZE(initial);
2640 else if (PyTuple_Check(initial) || array_Check(initial))
2641 len = Py_SIZE(initial);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002642 else
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002643 len = 0;
Martin v. Löwis99866332002-03-01 10:27:01 +00002644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 a = newarrayobject(type, len, descr);
2646 if (a == NULL)
2647 return NULL;
2648
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002649 if (len > 0 && !array_Check(initial)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 Py_ssize_t i;
2651 for (i = 0; i < len; i++) {
2652 PyObject *v =
2653 PySequence_GetItem(initial, i);
2654 if (v == NULL) {
2655 Py_DECREF(a);
2656 return NULL;
2657 }
2658 if (setarrayitem(a, i, v) != 0) {
2659 Py_DECREF(v);
2660 Py_DECREF(a);
2661 return NULL;
2662 }
2663 Py_DECREF(v);
2664 }
2665 }
2666 else if (initial != NULL && (PyByteArray_Check(initial) ||
2667 PyBytes_Check(initial))) {
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03002668 PyObject *v;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002669 v = array_array_frombytes((arrayobject *)a,
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03002670 initial);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 if (v == NULL) {
2672 Py_DECREF(a);
2673 return NULL;
2674 }
2675 Py_DECREF(v);
2676 }
2677 else if (initial != NULL && PyUnicode_Check(initial)) {
Victor Stinner62bb3942012-08-06 00:46:05 +02002678 Py_UNICODE *ustr;
Victor Stinner1fbcaef2011-09-30 01:54:04 +02002679 Py_ssize_t n;
Victor Stinner62bb3942012-08-06 00:46:05 +02002680
2681 ustr = PyUnicode_AsUnicode(initial);
2682 if (ustr == NULL) {
2683 PyErr_NoMemory();
Victor Stinner1fbcaef2011-09-30 01:54:04 +02002684 Py_DECREF(a);
2685 return NULL;
2686 }
Victor Stinner62bb3942012-08-06 00:46:05 +02002687
2688 n = PyUnicode_GET_DATA_SIZE(initial);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002689 if (n > 0) {
2690 arrayobject *self = (arrayobject *)a;
Victor Stinner62bb3942012-08-06 00:46:05 +02002691 char *item = self->ob_item;
2692 item = (char *)PyMem_Realloc(item, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002693 if (item == NULL) {
2694 PyErr_NoMemory();
2695 Py_DECREF(a);
2696 return NULL;
2697 }
Victor Stinner62bb3942012-08-06 00:46:05 +02002698 self->ob_item = item;
Victor Stinner60ac6ed2020-02-07 23:18:08 +01002699 Py_SET_SIZE(self, n / sizeof(Py_UNICODE));
Victor Stinner62bb3942012-08-06 00:46:05 +02002700 memcpy(item, ustr, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002701 self->allocated = Py_SIZE(self);
2702 }
2703 }
Benjamin Peterson682124c2014-10-10 20:58:30 -04002704 else if (initial != NULL && array_Check(initial) && len > 0) {
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002705 arrayobject *self = (arrayobject *)a;
2706 arrayobject *other = (arrayobject *)initial;
2707 memcpy(self->ob_item, other->ob_item, len * other->ob_descr->itemsize);
2708 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002709 if (it != NULL) {
2710 if (array_iter_extend((arrayobject *)a, it) == -1) {
2711 Py_DECREF(it);
2712 Py_DECREF(a);
2713 return NULL;
2714 }
2715 Py_DECREF(it);
2716 }
2717 return a;
2718 }
2719 }
2720 PyErr_SetString(PyExc_ValueError,
Meador Inge1c9f0c92011-09-20 19:55:51 -05002721 "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 +00002722 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00002723}
2724
Guido van Rossum778983b1993-02-19 15:55:02 +00002725
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002726PyDoc_STRVAR(module_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00002727"This module defines an object type which can efficiently represent\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002728an array of basic values: characters, integers, floating point\n\
2729numbers. Arrays are sequence types and behave very much like lists,\n\
Alexandre Vassalotti9730e332013-11-29 20:47:15 -08002730except that the type of objects stored in them is constrained.\n");
2731
2732PyDoc_STRVAR(arraytype_doc,
2733"array(typecode [, initializer]) -> array\n\
2734\n\
2735Return a new array whose items are restricted by typecode, and\n\
2736initialized from the optional initializer value, which must be a list,\n\
2737string or iterable over elements of the appropriate type.\n\
2738\n\
2739Arrays represent basic values and behave very much like lists, except\n\
2740the type of objects stored in them is constrained. The type is specified\n\
2741at object creation time by using a type code, which is a single character.\n\
2742The following type codes are defined:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002743\n\
oldkaa0735f2018-02-02 16:52:55 +08002744 Type code C Type Minimum size in bytes\n\
2745 'b' signed integer 1\n\
2746 'B' unsigned integer 1\n\
2747 'u' Unicode character 2 (see note)\n\
2748 'h' signed integer 2\n\
2749 'H' unsigned integer 2\n\
2750 'i' signed integer 2\n\
2751 'I' unsigned integer 2\n\
2752 'l' signed integer 4\n\
2753 'L' unsigned integer 4\n\
2754 'q' signed integer 8 (see note)\n\
2755 'Q' unsigned integer 8 (see note)\n\
2756 'f' floating point 4\n\
2757 'd' floating point 8\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002758\n\
oldkaa0735f2018-02-02 16:52:55 +08002759NOTE: The 'u' typecode corresponds to Python's unicode character. On\n\
Victor Stinner62bb3942012-08-06 00:46:05 +02002760narrow builds this is 2-bytes on wide builds this is 4-bytes.\n\
2761\n\
oldkaa0735f2018-02-02 16:52:55 +08002762NOTE: The 'q' and 'Q' type codes are only available if the platform\n\
2763C compiler used to build Python supports 'long long', or, on Windows,\n\
Meador Inge1c9f0c92011-09-20 19:55:51 -05002764'__int64'.\n\
2765\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002766Methods:\n\
2767\n\
2768append() -- append a new item to the end of the array\n\
2769buffer_info() -- return information giving the current memory info\n\
2770byteswap() -- byteswap all the items of the array\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002771count() -- return number of occurrences of an object\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00002772extend() -- extend array by appending multiple elements from an iterable\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002773fromfile() -- read items from a file object\n\
2774fromlist() -- append items from the list\n\
Florent Xiclunac45fb252011-10-24 13:14:55 +02002775frombytes() -- append items from the string\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002776index() -- return index of first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002777insert() -- insert a new item into the array at a provided position\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00002778pop() -- remove and return item (default last)\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002779remove() -- remove first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002780reverse() -- reverse the order of the items in the array\n\
2781tofile() -- write all items to a file object\n\
2782tolist() -- return the array converted to an ordinary list\n\
Florent Xiclunac45fb252011-10-24 13:14:55 +02002783tobytes() -- return the array converted to a string\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002784\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002785Attributes:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002786\n\
2787typecode -- the typecode character used to create the array\n\
2788itemsize -- the length in bytes of one array item\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002789");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002790
Raymond Hettinger625812f2003-01-07 01:58:52 +00002791static PyObject *array_iter(arrayobject *ao);
2792
Tim Peters0c322792002-07-17 16:49:03 +00002793static PyTypeObject Arraytype = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002794 PyVarObject_HEAD_INIT(NULL, 0)
2795 "array.array",
2796 sizeof(arrayobject),
2797 0,
2798 (destructor)array_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002799 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002800 0, /* tp_getattr */
2801 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002802 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803 (reprfunc)array_repr, /* tp_repr */
2804 0, /* tp_as_number*/
2805 &array_as_sequence, /* tp_as_sequence*/
2806 &array_as_mapping, /* tp_as_mapping*/
2807 0, /* tp_hash */
2808 0, /* tp_call */
2809 0, /* tp_str */
2810 PyObject_GenericGetAttr, /* tp_getattro */
2811 0, /* tp_setattro */
2812 &array_as_buffer, /* tp_as_buffer*/
2813 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2814 arraytype_doc, /* tp_doc */
2815 0, /* tp_traverse */
2816 0, /* tp_clear */
2817 array_richcompare, /* tp_richcompare */
2818 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
2819 (getiterfunc)array_iter, /* tp_iter */
2820 0, /* tp_iternext */
2821 array_methods, /* tp_methods */
2822 0, /* tp_members */
2823 array_getsets, /* tp_getset */
2824 0, /* tp_base */
2825 0, /* tp_dict */
2826 0, /* tp_descr_get */
2827 0, /* tp_descr_set */
2828 0, /* tp_dictoffset */
2829 0, /* tp_init */
2830 PyType_GenericAlloc, /* tp_alloc */
2831 array_new, /* tp_new */
2832 PyObject_Del, /* tp_free */
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002833};
2834
Raymond Hettinger625812f2003-01-07 01:58:52 +00002835
2836/*********************** Array Iterator **************************/
2837
Brett Cannon1eb32c22014-10-10 16:26:45 -04002838/*[clinic input]
2839class array.arrayiterator "arrayiterobject *" "&PyArrayIter_Type"
2840[clinic start generated code]*/
2841/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5aefd2d74d8c8e30]*/
Raymond Hettinger625812f2003-01-07 01:58:52 +00002842
2843static PyObject *
2844array_iter(arrayobject *ao)
2845{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 arrayiterobject *it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002848 if (!array_Check(ao)) {
2849 PyErr_BadInternalCall();
2850 return NULL;
2851 }
Raymond Hettinger625812f2003-01-07 01:58:52 +00002852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
2854 if (it == NULL)
2855 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 Py_INCREF(ao);
2858 it->ao = ao;
2859 it->index = 0;
2860 it->getitem = ao->ob_descr->getitem;
2861 PyObject_GC_Track(it);
2862 return (PyObject *)it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002863}
2864
2865static PyObject *
Raymond Hettinger625812f2003-01-07 01:58:52 +00002866arrayiter_next(arrayiterobject *it)
2867{
Serhiy Storchakaab0d1982016-03-30 21:11:16 +03002868 arrayobject *ao;
2869
2870 assert(it != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 assert(PyArrayIter_Check(it));
Serhiy Storchakaab0d1982016-03-30 21:11:16 +03002872 ao = it->ao;
2873 if (ao == NULL) {
2874 return NULL;
2875 }
2876 assert(array_Check(ao));
2877 if (it->index < Py_SIZE(ao)) {
2878 return (*it->getitem)(ao, it->index++);
2879 }
2880 it->ao = NULL;
2881 Py_DECREF(ao);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002883}
2884
2885static void
2886arrayiter_dealloc(arrayiterobject *it)
2887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 PyObject_GC_UnTrack(it);
2889 Py_XDECREF(it->ao);
2890 PyObject_GC_Del(it);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002891}
2892
2893static int
2894arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2895{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002896 Py_VISIT(it->ao);
2897 return 0;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002898}
2899
Brett Cannon1eb32c22014-10-10 16:26:45 -04002900/*[clinic input]
2901array.arrayiterator.__reduce__
2902
2903Return state information for pickling.
2904[clinic start generated code]*/
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002905
2906static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04002907array_arrayiterator___reduce___impl(arrayiterobject *self)
2908/*[clinic end generated code: output=7898a52e8e66e016 input=a062ea1e9951417a]*/
2909{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002910 _Py_IDENTIFIER(iter);
2911 PyObject *func = _PyEval_GetBuiltinId(&PyId_iter);
Serhiy Storchakaab0d1982016-03-30 21:11:16 +03002912 if (self->ao == NULL) {
2913 return Py_BuildValue("N(())", func);
2914 }
2915 return Py_BuildValue("N(O)n", func, self->ao, self->index);
Brett Cannon1eb32c22014-10-10 16:26:45 -04002916}
2917
2918/*[clinic input]
2919array.arrayiterator.__setstate__
2920
2921 state: object
2922 /
2923
2924Set state information for unpickling.
2925[clinic start generated code]*/
2926
2927static PyObject *
2928array_arrayiterator___setstate__(arrayiterobject *self, PyObject *state)
2929/*[clinic end generated code: output=397da9904e443cbe input=f47d5ceda19e787b]*/
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002930{
2931 Py_ssize_t index = PyLong_AsSsize_t(state);
2932 if (index == -1 && PyErr_Occurred())
2933 return NULL;
2934 if (index < 0)
2935 index = 0;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002936 else if (index > Py_SIZE(self->ao))
2937 index = Py_SIZE(self->ao); /* iterator exhausted */
2938 self->index = index;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002939 Py_RETURN_NONE;
2940}
2941
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002942static PyMethodDef arrayiter_methods[] = {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002943 ARRAY_ARRAYITERATOR___REDUCE___METHODDEF
2944 ARRAY_ARRAYITERATOR___SETSTATE___METHODDEF
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002945 {NULL, NULL} /* sentinel */
2946};
2947
Raymond Hettinger625812f2003-01-07 01:58:52 +00002948static PyTypeObject PyArrayIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002949 PyVarObject_HEAD_INIT(NULL, 0)
2950 "arrayiterator", /* tp_name */
2951 sizeof(arrayiterobject), /* tp_basicsize */
2952 0, /* tp_itemsize */
2953 /* methods */
2954 (destructor)arrayiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002955 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002956 0, /* tp_getattr */
2957 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002958 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 0, /* tp_repr */
2960 0, /* tp_as_number */
2961 0, /* tp_as_sequence */
2962 0, /* tp_as_mapping */
2963 0, /* tp_hash */
2964 0, /* tp_call */
2965 0, /* tp_str */
2966 PyObject_GenericGetAttr, /* tp_getattro */
2967 0, /* tp_setattro */
2968 0, /* tp_as_buffer */
2969 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2970 0, /* tp_doc */
2971 (traverseproc)arrayiter_traverse, /* tp_traverse */
2972 0, /* tp_clear */
2973 0, /* tp_richcompare */
2974 0, /* tp_weaklistoffset */
2975 PyObject_SelfIter, /* tp_iter */
2976 (iternextfunc)arrayiter_next, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002977 arrayiter_methods, /* tp_methods */
Raymond Hettinger625812f2003-01-07 01:58:52 +00002978};
2979
2980
2981/*********************** Install Module **************************/
2982
Martin v. Löwis99866332002-03-01 10:27:01 +00002983/* No functions in array module. */
2984static PyMethodDef a_methods[] = {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002985 ARRAY__ARRAY_RECONSTRUCTOR_METHODDEF
Martin v. Löwis99866332002-03-01 10:27:01 +00002986 {NULL, NULL, 0, NULL} /* Sentinel */
2987};
2988
Nick Coghland5cacbb2015-05-23 22:24:10 +10002989static int
2990array_modexec(PyObject *m)
Guido van Rossum778983b1993-02-19 15:55:02 +00002991{
Georg Brandl4cb0de22011-09-28 21:49:49 +02002992 char buffer[Py_ARRAY_LENGTH(descriptors)], *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002993 PyObject *typecodes;
2994 Py_ssize_t size = 0;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002995 const struct arraydescr *descr;
Fred Drake0d40ba42000-02-04 20:33:49 +00002996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002997 if (PyType_Ready(&Arraytype) < 0)
Nick Coghland5cacbb2015-05-23 22:24:10 +10002998 return -1;
Victor Stinnerd2ec81a2020-02-07 09:17:07 +01002999 Py_SET_TYPE(&PyArrayIter_Type, &PyType_Type);
Fred Drakef4e34842002-04-01 03:45:06 +00003000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 Py_INCREF((PyObject *)&Arraytype);
Marco Paolinib44ffc82019-11-15 08:42:51 +00003002 if (PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype) < 0) {
3003 Py_DECREF((PyObject *)&Arraytype);
3004 return -1;
3005 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 Py_INCREF((PyObject *)&Arraytype);
Marco Paolinib44ffc82019-11-15 08:42:51 +00003007 if (PyModule_AddObject(m, "array", (PyObject *)&Arraytype) < 0) {
3008 Py_DECREF((PyObject *)&Arraytype);
3009 return -1;
3010 }
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00003011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003012 for (descr=descriptors; descr->typecode != '\0'; descr++) {
3013 size++;
3014 }
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00003015
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003016 p = buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 for (descr = descriptors; descr->typecode != '\0'; descr++) {
3018 *p++ = (char)descr->typecode;
3019 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003020 typecodes = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
Marco Paolinib44ffc82019-11-15 08:42:51 +00003021 if (PyModule_AddObject(m, "typecodes", typecodes) < 0) {
3022 Py_XDECREF(typecodes);
3023 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 }
Marco Paolinib44ffc82019-11-15 08:42:51 +00003025
Nick Coghland5cacbb2015-05-23 22:24:10 +10003026 return 0;
3027}
3028
3029static PyModuleDef_Slot arrayslots[] = {
3030 {Py_mod_exec, array_modexec},
3031 {0, NULL}
3032};
3033
3034
3035static struct PyModuleDef arraymodule = {
3036 PyModuleDef_HEAD_INIT,
3037 "array",
3038 module_doc,
3039 0,
3040 a_methods,
3041 arrayslots,
3042 NULL,
3043 NULL,
3044 NULL
3045};
3046
3047
3048PyMODINIT_FUNC
3049PyInit_array(void)
3050{
3051 return PyModuleDef_Init(&arraymodule);
Guido van Rossum778983b1993-02-19 15:55:02 +00003052}