blob: 724c503eba289b3cb995859c2c21acb755e07fa5 [file] [log] [blame]
Guido van Rossum778983b1993-02-19 15:55:02 +00001/* Array object implementation */
2
3/* An array is a uniform list -- all items have the same type.
4 The item type is restricted to simple C types like int or float */
5
Martin v. Löwis18e16552006-02-15 17:27:45 +00006#define PY_SSIZE_T_CLEAN
Roger E. Masse2919eaa1996-12-09 20:10:36 +00007#include "Python.h"
Victor Stinner4a21e572020-04-15 02:35:41 +02008#include <stddef.h> // offsetof()
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)
Dong-hee Na1b55b652020-02-17 19:09:15 +0900109#define array_CheckExact(op) Py_IS_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{
Inada Naokid5d9a712020-05-11 15:37:25 +0900238 return PyUnicode_FromOrdinal(((wchar_t *) 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{
Inada Naokid5d9a712020-05-11 15:37:25 +0900244 PyObject *u;
245 if (!PyArg_Parse(v, "U;array item must be unicode character", &u)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 return -1;
Inada Naokid5d9a712020-05-11 15:37:25 +0900247 }
248
249 Py_ssize_t len = PyUnicode_AsWideChar(u, NULL, 0);
250 if (len != 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 PyErr_SetString(PyExc_TypeError,
252 "array item must be unicode character");
253 return -1;
254 }
Inada Naokid5d9a712020-05-11 15:37:25 +0900255
256 wchar_t w;
257 len = PyUnicode_AsWideChar(u, &w, 1);
258 assert(len == 1);
259
260 if (i >= 0) {
261 ((wchar_t *)ap->ob_item)[i] = w;
262 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 return 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000264}
Martin v. Löwis99866332002-03-01 10:27:01 +0000265
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +0000266
Guido van Rossum549ab711997-01-03 19:09:47 +0000267static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000268h_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 return PyLong_FromLong((long) ((short *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000271}
272
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +0000273
Guido van Rossum778983b1993-02-19 15:55:02 +0000274static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000275h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 short x;
278 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
279 if (!PyArg_Parse(v, "h;array item must be integer", &x))
280 return -1;
281 if (i >= 0)
282 ((short *)ap->ob_item)[i] = x;
283 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000284}
285
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000286static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000287HH_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 return PyLong_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000290}
291
Fred Drake541dc3b2000-06-28 17:49:30 +0000292static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000293HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 int x;
296 /* PyArg_Parse's 'h' formatter is for a signed short, therefore
297 must use the next size up and manually do the overflow checking */
298 if (!PyArg_Parse(v, "i;array item must be integer", &x))
299 return -1;
300 else if (x < 0) {
301 PyErr_SetString(PyExc_OverflowError,
302 "unsigned short is less than minimum");
303 return -1;
304 }
305 else if (x > USHRT_MAX) {
306 PyErr_SetString(PyExc_OverflowError,
307 "unsigned short is greater than maximum");
308 return -1;
309 }
310 if (i >= 0)
311 ((short *)ap->ob_item)[i] = (short)x;
312 return 0;
Fred Drake541dc3b2000-06-28 17:49:30 +0000313}
Guido van Rossum549ab711997-01-03 19:09:47 +0000314
315static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000316i_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossume77a7571993-11-03 15:01:26 +0000317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 return PyLong_FromLong((long) ((int *)ap->ob_item)[i]);
Guido van Rossume77a7571993-11-03 15:01:26 +0000319}
320
321static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000322i_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossume77a7571993-11-03 15:01:26 +0000323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 int x;
325 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
326 if (!PyArg_Parse(v, "i;array item must be integer", &x))
327 return -1;
328 if (i >= 0)
329 ((int *)ap->ob_item)[i] = x;
330 return 0;
Guido van Rossume77a7571993-11-03 15:01:26 +0000331}
332
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000333static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000334II_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 return PyLong_FromUnsignedLong(
337 (unsigned long) ((unsigned int *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000338}
339
orenmn964281a2017-03-09 11:35:28 +0200340static PyObject *
341get_int_unless_float(PyObject *v)
342{
343 if (PyFloat_Check(v)) {
344 PyErr_SetString(PyExc_TypeError,
345 "array item must be integer");
346 return NULL;
347 }
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200348 return _PyLong_FromNbIndexOrNbInt(v);
orenmn964281a2017-03-09 11:35:28 +0200349}
350
Guido van Rossum549ab711997-01-03 19:09:47 +0000351static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000352II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 unsigned long x;
orenmn964281a2017-03-09 11:35:28 +0200355 int do_decref = 0; /* if nb_int was called */
356
357 if (!PyLong_Check(v)) {
358 v = get_int_unless_float(v);
359 if (NULL == v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 return -1;
361 }
orenmn964281a2017-03-09 11:35:28 +0200362 do_decref = 1;
363 }
364 x = PyLong_AsUnsignedLong(v);
365 if (x == (unsigned long)-1 && PyErr_Occurred()) {
366 if (do_decref) {
367 Py_DECREF(v);
368 }
369 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 }
371 if (x > UINT_MAX) {
372 PyErr_SetString(PyExc_OverflowError,
orenmn964281a2017-03-09 11:35:28 +0200373 "unsigned int is greater than maximum");
374 if (do_decref) {
375 Py_DECREF(v);
376 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 return -1;
378 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 if (i >= 0)
380 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
orenmn964281a2017-03-09 11:35:28 +0200381
382 if (do_decref) {
383 Py_DECREF(v);
384 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 return 0;
Guido van Rossum549ab711997-01-03 19:09:47 +0000386}
387
388static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000389l_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 return PyLong_FromLong(((long *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000392}
393
394static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000395l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 long x;
398 if (!PyArg_Parse(v, "l;array item must be integer", &x))
399 return -1;
400 if (i >= 0)
401 ((long *)ap->ob_item)[i] = x;
402 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000403}
404
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000405static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000406LL_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000409}
410
411static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000412LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 unsigned long x;
orenmn964281a2017-03-09 11:35:28 +0200415 int do_decref = 0; /* if nb_int was called */
416
417 if (!PyLong_Check(v)) {
418 v = get_int_unless_float(v);
419 if (NULL == v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 return -1;
421 }
orenmn964281a2017-03-09 11:35:28 +0200422 do_decref = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 }
orenmn964281a2017-03-09 11:35:28 +0200424 x = PyLong_AsUnsignedLong(v);
425 if (x == (unsigned long)-1 && PyErr_Occurred()) {
426 if (do_decref) {
427 Py_DECREF(v);
428 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 return -1;
430 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 if (i >= 0)
432 ((unsigned long *)ap->ob_item)[i] = x;
orenmn964281a2017-03-09 11:35:28 +0200433
434 if (do_decref) {
435 Py_DECREF(v);
436 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 return 0;
Guido van Rossum549ab711997-01-03 19:09:47 +0000438}
439
Meador Inge1c9f0c92011-09-20 19:55:51 -0500440static PyObject *
441q_getitem(arrayobject *ap, Py_ssize_t i)
442{
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700443 return PyLong_FromLongLong(((long long *)ap->ob_item)[i]);
Meador Inge1c9f0c92011-09-20 19:55:51 -0500444}
445
446static int
447q_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
448{
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700449 long long x;
Meador Inge1c9f0c92011-09-20 19:55:51 -0500450 if (!PyArg_Parse(v, "L;array item must be integer", &x))
451 return -1;
452 if (i >= 0)
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700453 ((long long *)ap->ob_item)[i] = x;
Meador Inge1c9f0c92011-09-20 19:55:51 -0500454 return 0;
455}
456
457static PyObject *
458QQ_getitem(arrayobject *ap, Py_ssize_t i)
459{
460 return PyLong_FromUnsignedLongLong(
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700461 ((unsigned long long *)ap->ob_item)[i]);
Meador Inge1c9f0c92011-09-20 19:55:51 -0500462}
463
464static int
465QQ_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
466{
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700467 unsigned long long x;
orenmn964281a2017-03-09 11:35:28 +0200468 int do_decref = 0; /* if nb_int was called */
469
470 if (!PyLong_Check(v)) {
471 v = get_int_unless_float(v);
472 if (NULL == v) {
Meador Inge1c9f0c92011-09-20 19:55:51 -0500473 return -1;
474 }
orenmn964281a2017-03-09 11:35:28 +0200475 do_decref = 1;
Meador Inge1c9f0c92011-09-20 19:55:51 -0500476 }
orenmn964281a2017-03-09 11:35:28 +0200477 x = PyLong_AsUnsignedLongLong(v);
478 if (x == (unsigned long long)-1 && PyErr_Occurred()) {
479 if (do_decref) {
480 Py_DECREF(v);
481 }
482 return -1;
483 }
Meador Inge1c9f0c92011-09-20 19:55:51 -0500484 if (i >= 0)
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700485 ((unsigned long long *)ap->ob_item)[i] = x;
orenmn964281a2017-03-09 11:35:28 +0200486
487 if (do_decref) {
488 Py_DECREF(v);
489 }
Meador Inge1c9f0c92011-09-20 19:55:51 -0500490 return 0;
491}
Meador Inge1c9f0c92011-09-20 19:55:51 -0500492
Guido van Rossum549ab711997-01-03 19:09:47 +0000493static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000494f_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000495{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000497}
498
499static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000500f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 float x;
503 if (!PyArg_Parse(v, "f;array item must be float", &x))
504 return -1;
505 if (i >= 0)
506 ((float *)ap->ob_item)[i] = x;
507 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000508}
509
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000510static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000511d_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000514}
515
516static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000517d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 double x;
520 if (!PyArg_Parse(v, "d;array item must be float", &x))
521 return -1;
522 if (i >= 0)
523 ((double *)ap->ob_item)[i] = x;
524 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000525}
526
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000527#define DEFINE_COMPAREITEMS(code, type) \
528 static int \
529 code##_compareitems(const void *lhs, const void *rhs, Py_ssize_t length) \
530 { \
531 const type *a = lhs, *b = rhs; \
532 for (Py_ssize_t i = 0; i < length; ++i) \
533 if (a[i] != b[i]) \
534 return a[i] < b[i] ? -1 : 1; \
535 return 0; \
536 }
537
538DEFINE_COMPAREITEMS(b, signed char)
539DEFINE_COMPAREITEMS(BB, unsigned char)
Inada Naokid5d9a712020-05-11 15:37:25 +0900540DEFINE_COMPAREITEMS(u, wchar_t)
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000541DEFINE_COMPAREITEMS(h, short)
542DEFINE_COMPAREITEMS(HH, unsigned short)
543DEFINE_COMPAREITEMS(i, int)
544DEFINE_COMPAREITEMS(II, unsigned int)
545DEFINE_COMPAREITEMS(l, long)
546DEFINE_COMPAREITEMS(LL, unsigned long)
547DEFINE_COMPAREITEMS(q, long long)
548DEFINE_COMPAREITEMS(QQ, unsigned long long)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000549
Alexandre Vassalottiad077152009-07-15 17:49:23 +0000550/* Description of types.
551 *
552 * Don't forget to update typecode_to_mformat_code() if you add a new
553 * typecode.
554 */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200555static const struct arraydescr descriptors[] = {
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000556 {'b', 1, b_getitem, b_setitem, b_compareitems, "b", 1, 1},
557 {'B', 1, BB_getitem, BB_setitem, BB_compareitems, "B", 1, 0},
Inada Naokid5d9a712020-05-11 15:37:25 +0900558 {'u', sizeof(wchar_t), u_getitem, u_setitem, u_compareitems, "u", 0, 0},
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000559 {'h', sizeof(short), h_getitem, h_setitem, h_compareitems, "h", 1, 1},
560 {'H', sizeof(short), HH_getitem, HH_setitem, HH_compareitems, "H", 1, 0},
561 {'i', sizeof(int), i_getitem, i_setitem, i_compareitems, "i", 1, 1},
562 {'I', sizeof(int), II_getitem, II_setitem, II_compareitems, "I", 1, 0},
563 {'l', sizeof(long), l_getitem, l_setitem, l_compareitems, "l", 1, 1},
564 {'L', sizeof(long), LL_getitem, LL_setitem, LL_compareitems, "L", 1, 0},
565 {'q', sizeof(long long), q_getitem, q_setitem, q_compareitems, "q", 1, 1},
566 {'Q', sizeof(long long), QQ_getitem, QQ_setitem, QQ_compareitems, "Q", 1, 0},
567 {'f', sizeof(float), f_getitem, f_setitem, NULL, "f", 0, 0},
568 {'d', sizeof(double), d_getitem, d_setitem, NULL, "d", 0, 0},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 {'\0', 0, 0, 0, 0, 0, 0} /* Sentinel */
Guido van Rossum778983b1993-02-19 15:55:02 +0000570};
Tim Petersbb307342000-09-10 05:22:54 +0000571
572/****************************************************************************
573Implementations of array object methods.
574****************************************************************************/
Brett Cannon1eb32c22014-10-10 16:26:45 -0400575/*[clinic input]
576class array.array "arrayobject *" "&Arraytype"
577[clinic start generated code]*/
578/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ad43d37e942a8854]*/
Guido van Rossum778983b1993-02-19 15:55:02 +0000579
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000580static PyObject *
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200581newarrayobject(PyTypeObject *type, Py_ssize_t size, const struct arraydescr *descr)
Guido van Rossum778983b1993-02-19 15:55:02 +0000582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 arrayobject *op;
584 size_t nbytes;
Martin v. Löwis99866332002-03-01 10:27:01 +0000585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 if (size < 0) {
587 PyErr_BadInternalCall();
588 return NULL;
589 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 /* Check for overflow */
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100592 if (size > PY_SSIZE_T_MAX / descr->itemsize) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 return PyErr_NoMemory();
594 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100595 nbytes = size * descr->itemsize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 op = (arrayobject *) type->tp_alloc(type, 0);
597 if (op == NULL) {
598 return NULL;
599 }
600 op->ob_descr = descr;
601 op->allocated = size;
602 op->weakreflist = NULL;
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100603 Py_SET_SIZE(op, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 if (size <= 0) {
605 op->ob_item = NULL;
606 }
607 else {
608 op->ob_item = PyMem_NEW(char, nbytes);
609 if (op->ob_item == NULL) {
610 Py_DECREF(op);
611 return PyErr_NoMemory();
612 }
613 }
614 op->ob_exports = 0;
615 return (PyObject *) op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000616}
617
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000618static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000619getarrayitem(PyObject *op, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000620{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200621 arrayobject *ap;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 assert(array_Check(op));
623 ap = (arrayobject *)op;
624 assert(i>=0 && i<Py_SIZE(ap));
625 return (*ap->ob_descr->getitem)(ap, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000626}
627
628static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000629ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 char *items;
632 Py_ssize_t n = Py_SIZE(self);
633 if (v == NULL) {
634 PyErr_BadInternalCall();
635 return -1;
636 }
637 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
638 return -1;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 if (array_resize(self, n+1) == -1)
641 return -1;
642 items = self->ob_item;
643 if (where < 0) {
644 where += n;
645 if (where < 0)
646 where = 0;
647 }
648 if (where > n)
649 where = n;
650 /* appends don't need to call memmove() */
651 if (where != n)
652 memmove(items + (where+1)*self->ob_descr->itemsize,
653 items + where*self->ob_descr->itemsize,
654 (n-where)*self->ob_descr->itemsize);
655 return (*self->ob_descr->setitem)(self, where, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000656}
657
Guido van Rossum778983b1993-02-19 15:55:02 +0000658/* Methods */
659
660static void
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000661array_dealloc(arrayobject *op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 if (op->weakreflist != NULL)
664 PyObject_ClearWeakRefs((PyObject *) op);
665 if (op->ob_item != NULL)
666 PyMem_DEL(op->ob_item);
667 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000668}
669
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000670static PyObject *
671array_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000672{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 arrayobject *va, *wa;
674 PyObject *vi = NULL;
675 PyObject *wi = NULL;
676 Py_ssize_t i, k;
677 PyObject *res;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000678
Brian Curtindfc80e32011-08-10 20:28:54 -0500679 if (!array_Check(v) || !array_Check(w))
680 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 va = (arrayobject *)v;
683 wa = (arrayobject *)w;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 if (Py_SIZE(va) != Py_SIZE(wa) && (op == Py_EQ || op == Py_NE)) {
686 /* Shortcut: if the lengths differ, the arrays differ */
687 if (op == Py_EQ)
688 res = Py_False;
689 else
690 res = Py_True;
691 Py_INCREF(res);
692 return res;
693 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000694
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000695 if (va->ob_descr == wa->ob_descr && va->ob_descr->compareitems != NULL) {
696 /* Fast path:
697 arrays with same types can have their buffers compared directly */
698 Py_ssize_t common_length = Py_MIN(Py_SIZE(va), Py_SIZE(wa));
699 int result = va->ob_descr->compareitems(va->ob_item, wa->ob_item,
700 common_length);
701 if (result == 0)
702 goto compare_sizes;
703
704 int cmp;
705 switch (op) {
706 case Py_LT: cmp = result < 0; break;
707 case Py_LE: cmp = result <= 0; break;
708 case Py_EQ: cmp = result == 0; break;
709 case Py_NE: cmp = result != 0; break;
710 case Py_GT: cmp = result > 0; break;
711 case Py_GE: cmp = result >= 0; break;
712 default: return NULL; /* cannot happen */
713 }
714 PyObject *res = cmp ? Py_True : Py_False;
715 Py_INCREF(res);
716 return res;
717 }
718
719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 /* Search for the first index where items are different */
721 k = 1;
722 for (i = 0; i < Py_SIZE(va) && i < Py_SIZE(wa); i++) {
723 vi = getarrayitem(v, i);
724 wi = getarrayitem(w, i);
725 if (vi == NULL || wi == NULL) {
726 Py_XDECREF(vi);
727 Py_XDECREF(wi);
728 return NULL;
729 }
730 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
731 if (k == 0)
732 break; /* Keeping vi and wi alive! */
733 Py_DECREF(vi);
734 Py_DECREF(wi);
735 if (k < 0)
736 return NULL;
737 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 if (k) {
740 /* No more items to compare -- compare sizes */
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000741 compare_sizes: ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 Py_ssize_t vs = Py_SIZE(va);
743 Py_ssize_t ws = Py_SIZE(wa);
744 int cmp;
745 switch (op) {
746 case Py_LT: cmp = vs < ws; break;
747 case Py_LE: cmp = vs <= ws; break;
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000748 /* If the lengths were not equal,
749 the earlier fast-path check would have caught that. */
750 case Py_EQ: assert(vs == ws); cmp = 1; break;
751 case Py_NE: assert(vs == ws); cmp = 0; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 case Py_GT: cmp = vs > ws; break;
753 case Py_GE: cmp = vs >= ws; break;
754 default: return NULL; /* cannot happen */
755 }
756 if (cmp)
757 res = Py_True;
758 else
759 res = Py_False;
760 Py_INCREF(res);
761 return res;
762 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 /* We have an item that differs. First, shortcuts for EQ/NE */
765 if (op == Py_EQ) {
766 Py_INCREF(Py_False);
767 res = Py_False;
768 }
769 else if (op == Py_NE) {
770 Py_INCREF(Py_True);
771 res = Py_True;
772 }
773 else {
774 /* Compare the final item again using the proper operator */
775 res = PyObject_RichCompare(vi, wi, op);
776 }
777 Py_DECREF(vi);
778 Py_DECREF(wi);
779 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +0000780}
781
Martin v. Löwis18e16552006-02-15 17:27:45 +0000782static Py_ssize_t
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000783array_length(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +0000784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 return Py_SIZE(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000786}
787
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000788static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000789array_item(arrayobject *a, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000790{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 if (i < 0 || i >= Py_SIZE(a)) {
792 PyErr_SetString(PyExc_IndexError, "array index out of range");
793 return NULL;
794 }
795 return getarrayitem((PyObject *)a, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000796}
797
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000798static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000799array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000800{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 arrayobject *np;
802 if (ilow < 0)
803 ilow = 0;
804 else if (ilow > Py_SIZE(a))
805 ilow = Py_SIZE(a);
806 if (ihigh < 0)
807 ihigh = 0;
808 if (ihigh < ilow)
809 ihigh = ilow;
810 else if (ihigh > Py_SIZE(a))
811 ihigh = Py_SIZE(a);
812 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
813 if (np == NULL)
814 return NULL;
Martin Panterbe8da9c2016-09-07 11:04:41 +0000815 if (ihigh > ilow) {
816 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
817 (ihigh-ilow) * a->ob_descr->itemsize);
818 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000820}
821
Brett Cannon1eb32c22014-10-10 16:26:45 -0400822
823/*[clinic input]
824array.array.__copy__
825
826Return a copy of the array.
827[clinic start generated code]*/
828
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000829static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -0400830array_array___copy___impl(arrayobject *self)
831/*[clinic end generated code: output=dec7c3f925d9619e input=ad1ee5b086965f09]*/
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000832{
Brett Cannon1eb32c22014-10-10 16:26:45 -0400833 return array_slice(self, 0, Py_SIZE(self));
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000834}
835
Brett Cannon1eb32c22014-10-10 16:26:45 -0400836/*[clinic input]
837array.array.__deepcopy__
838
839 unused: object
840 /
841
842Return a copy of the array.
843[clinic start generated code]*/
844
845static PyObject *
846array_array___deepcopy__(arrayobject *self, PyObject *unused)
847/*[clinic end generated code: output=1ec748d8e14a9faa input=2405ecb4933748c4]*/
848{
849 return array_array___copy___impl(self);
850}
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000851
852static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000853array_concat(arrayobject *a, PyObject *bb)
Guido van Rossum778983b1993-02-19 15:55:02 +0000854{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 Py_ssize_t size;
856 arrayobject *np;
857 if (!array_Check(bb)) {
858 PyErr_Format(PyExc_TypeError,
859 "can only append array (not \"%.200s\") to array",
860 Py_TYPE(bb)->tp_name);
861 return NULL;
862 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000863#define b ((arrayobject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 if (a->ob_descr != b->ob_descr) {
865 PyErr_BadArgument();
866 return NULL;
867 }
868 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) {
869 return PyErr_NoMemory();
870 }
871 size = Py_SIZE(a) + Py_SIZE(b);
872 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
873 if (np == NULL) {
874 return NULL;
875 }
Martin Panterbe8da9c2016-09-07 11:04:41 +0000876 if (Py_SIZE(a) > 0) {
877 memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize);
878 }
879 if (Py_SIZE(b) > 0) {
880 memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize,
881 b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
882 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000884#undef b
885}
886
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000887static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000888array_repeat(arrayobject *a, Py_ssize_t n)
Guido van Rossum778983b1993-02-19 15:55:02 +0000889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 Py_ssize_t size;
891 arrayobject *np;
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000892 Py_ssize_t oldbytes, newbytes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 if (n < 0)
894 n = 0;
895 if ((Py_SIZE(a) != 0) && (n > PY_SSIZE_T_MAX / Py_SIZE(a))) {
896 return PyErr_NoMemory();
897 }
898 size = Py_SIZE(a) * n;
899 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
900 if (np == NULL)
901 return NULL;
Martin Panterbe8da9c2016-09-07 11:04:41 +0000902 if (size == 0)
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000903 return (PyObject *)np;
904 oldbytes = Py_SIZE(a) * a->ob_descr->itemsize;
905 newbytes = oldbytes * n;
906 /* this follows the code in unicode_repeat */
907 if (oldbytes == 1) {
908 memset(np->ob_item, a->ob_item[0], newbytes);
909 } else {
910 Py_ssize_t done = oldbytes;
Christian Heimesf051e432016-09-13 20:22:02 +0200911 memcpy(np->ob_item, a->ob_item, oldbytes);
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000912 while (done < newbytes) {
913 Py_ssize_t ncopy = (done <= newbytes-done) ? done : newbytes-done;
Christian Heimesf051e432016-09-13 20:22:02 +0200914 memcpy(np->ob_item+done, np->ob_item, ncopy);
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000915 done += ncopy;
916 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 }
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000918 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000919}
920
921static int
Martin Panter996d72b2016-07-25 02:21:14 +0000922array_del_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000923{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 char *item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 Py_ssize_t d; /* Change in size */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 if (ilow < 0)
927 ilow = 0;
928 else if (ilow > Py_SIZE(a))
929 ilow = Py_SIZE(a);
930 if (ihigh < 0)
931 ihigh = 0;
932 if (ihigh < ilow)
933 ihigh = ilow;
934 else if (ihigh > Py_SIZE(a))
935 ihigh = Py_SIZE(a);
936 item = a->ob_item;
Martin Panter996d72b2016-07-25 02:21:14 +0000937 d = ihigh-ilow;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 /* Issue #4509: If the array has exported buffers and the slice
939 assignment would change the size of the array, fail early to make
940 sure we don't modify it. */
941 if (d != 0 && a->ob_exports > 0) {
942 PyErr_SetString(PyExc_BufferError,
943 "cannot resize an array that is exporting buffers");
944 return -1;
945 }
Martin Panter996d72b2016-07-25 02:21:14 +0000946 if (d > 0) { /* Delete d items */
947 memmove(item + (ihigh-d)*a->ob_descr->itemsize,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 item + ihigh*a->ob_descr->itemsize,
949 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
Martin Panter996d72b2016-07-25 02:21:14 +0000950 if (array_resize(a, Py_SIZE(a) - d) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 return -1;
952 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000954}
955
956static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000957array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000958{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 if (i < 0 || i >= Py_SIZE(a)) {
960 PyErr_SetString(PyExc_IndexError,
961 "array assignment index out of range");
962 return -1;
963 }
964 if (v == NULL)
Martin Panter996d72b2016-07-25 02:21:14 +0000965 return array_del_slice(a, i, i+1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 return (*a->ob_descr->setitem)(a, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000967}
968
969static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000970setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000971{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 assert(array_Check(a));
973 return array_ass_item((arrayobject *)a, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000974}
975
Martin v. Löwis99866332002-03-01 10:27:01 +0000976static int
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000977array_iter_extend(arrayobject *self, PyObject *bb)
978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 PyObject *it, *v;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 it = PyObject_GetIter(bb);
982 if (it == NULL)
983 return -1;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 while ((v = PyIter_Next(it)) != NULL) {
Mark Dickinson346f0af2010-08-06 09:36:57 +0000986 if (ins1(self, Py_SIZE(self), v) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 Py_DECREF(v);
988 Py_DECREF(it);
989 return -1;
990 }
991 Py_DECREF(v);
992 }
993 Py_DECREF(it);
994 if (PyErr_Occurred())
995 return -1;
996 return 0;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000997}
998
999static int
Martin v. Löwis99866332002-03-01 10:27:01 +00001000array_do_extend(arrayobject *self, PyObject *bb)
1001{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 Py_ssize_t size, oldsize, bbsize;
Martin v. Löwis99866332002-03-01 10:27:01 +00001003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 if (!array_Check(bb))
1005 return array_iter_extend(self, bb);
1006#define b ((arrayobject *)bb)
1007 if (self->ob_descr != b->ob_descr) {
1008 PyErr_SetString(PyExc_TypeError,
1009 "can only extend with array of same kind");
1010 return -1;
1011 }
1012 if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) ||
1013 ((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
1014 PyErr_NoMemory();
1015 return -1;
1016 }
1017 oldsize = Py_SIZE(self);
1018 /* Get the size of bb before resizing the array since bb could be self. */
1019 bbsize = Py_SIZE(bb);
1020 size = oldsize + Py_SIZE(b);
1021 if (array_resize(self, size) == -1)
1022 return -1;
Martin Panterbe8da9c2016-09-07 11:04:41 +00001023 if (bbsize > 0) {
1024 memcpy(self->ob_item + oldsize * self->ob_descr->itemsize,
1025 b->ob_item, bbsize * b->ob_descr->itemsize);
1026 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027
1028 return 0;
Martin v. Löwis99866332002-03-01 10:27:01 +00001029#undef b
1030}
1031
1032static PyObject *
1033array_inplace_concat(arrayobject *self, PyObject *bb)
1034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 if (!array_Check(bb)) {
1036 PyErr_Format(PyExc_TypeError,
1037 "can only extend array with array (not \"%.200s\")",
1038 Py_TYPE(bb)->tp_name);
1039 return NULL;
1040 }
1041 if (array_do_extend(self, bb) == -1)
1042 return NULL;
1043 Py_INCREF(self);
1044 return (PyObject *)self;
Martin v. Löwis99866332002-03-01 10:27:01 +00001045}
1046
1047static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001048array_inplace_repeat(arrayobject *self, Py_ssize_t n)
Martin v. Löwis99866332002-03-01 10:27:01 +00001049{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 char *items, *p;
1051 Py_ssize_t size, i;
Martin v. Löwis99866332002-03-01 10:27:01 +00001052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 if (Py_SIZE(self) > 0) {
1054 if (n < 0)
1055 n = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 if ((self->ob_descr->itemsize != 0) &&
1057 (Py_SIZE(self) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
1058 return PyErr_NoMemory();
1059 }
1060 size = Py_SIZE(self) * self->ob_descr->itemsize;
1061 if (n > 0 && size > PY_SSIZE_T_MAX / n) {
1062 return PyErr_NoMemory();
1063 }
1064 if (array_resize(self, n * Py_SIZE(self)) == -1)
1065 return NULL;
1066 items = p = self->ob_item;
1067 for (i = 1; i < n; i++) {
1068 p += size;
1069 memcpy(p, items, size);
1070 }
1071 }
1072 Py_INCREF(self);
1073 return (PyObject *)self;
Martin v. Löwis99866332002-03-01 10:27:01 +00001074}
1075
1076
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001077static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001078ins(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +00001079{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 if (ins1(self, where, v) != 0)
1081 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001082 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001083}
1084
Brett Cannon1eb32c22014-10-10 16:26:45 -04001085/*[clinic input]
1086array.array.count
1087
1088 v: object
1089 /
1090
1091Return number of occurrences of v in the array.
1092[clinic start generated code]*/
1093
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001094static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001095array_array_count(arrayobject *self, PyObject *v)
1096/*[clinic end generated code: output=3dd3624bf7135a3a input=d9bce9d65e39d1f5]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001097{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 Py_ssize_t count = 0;
1099 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 for (i = 0; i < Py_SIZE(self); i++) {
Victor Stinner0b142e22013-07-17 23:01:30 +02001102 PyObject *selfi;
1103 int cmp;
1104
1105 selfi = getarrayitem((PyObject *)self, i);
1106 if (selfi == NULL)
1107 return NULL;
1108 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 Py_DECREF(selfi);
1110 if (cmp > 0)
1111 count++;
1112 else if (cmp < 0)
1113 return NULL;
1114 }
1115 return PyLong_FromSsize_t(count);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001116}
1117
Brett Cannon1eb32c22014-10-10 16:26:45 -04001118
1119/*[clinic input]
1120array.array.index
1121
1122 v: object
1123 /
1124
1125Return index of first occurrence of v in the array.
1126[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001127
1128static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001129array_array_index(arrayobject *self, PyObject *v)
1130/*[clinic end generated code: output=d48498d325602167 input=cf619898c6649d08]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001131{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 for (i = 0; i < Py_SIZE(self); i++) {
Victor Stinner0b142e22013-07-17 23:01:30 +02001135 PyObject *selfi;
1136 int cmp;
1137
1138 selfi = getarrayitem((PyObject *)self, i);
1139 if (selfi == NULL)
1140 return NULL;
1141 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 Py_DECREF(selfi);
1143 if (cmp > 0) {
Miss Islington (bot)92f8b482020-06-23 06:41:24 -07001144 return PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 }
1146 else if (cmp < 0)
1147 return NULL;
1148 }
Jim Fasarakis-Hilliarda4095ef2017-05-29 20:43:39 +03001149 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in array");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 return NULL;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001151}
1152
Raymond Hettinger625812f2003-01-07 01:58:52 +00001153static int
1154array_contains(arrayobject *self, PyObject *v)
1155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 Py_ssize_t i;
1157 int cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +00001158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) {
1160 PyObject *selfi = getarrayitem((PyObject *)self, i);
Victor Stinner0b142e22013-07-17 23:01:30 +02001161 if (selfi == NULL)
Victor Stinner4755bea2013-07-18 01:12:35 +02001162 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
1164 Py_DECREF(selfi);
1165 }
1166 return cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +00001167}
1168
Brett Cannon1eb32c22014-10-10 16:26:45 -04001169/*[clinic input]
1170array.array.remove
1171
1172 v: object
1173 /
1174
1175Remove the first occurrence of v in the array.
1176[clinic start generated code]*/
1177
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001178static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001179array_array_remove(arrayobject *self, PyObject *v)
1180/*[clinic end generated code: output=bef06be9fdf9dceb input=0b1e5aed25590027]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001181{
sthaa3ecb82019-03-20 20:49:39 +01001182 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 for (i = 0; i < Py_SIZE(self); i++) {
Victor Stinner0b142e22013-07-17 23:01:30 +02001185 PyObject *selfi;
1186 int cmp;
1187
1188 selfi = getarrayitem((PyObject *)self,i);
1189 if (selfi == NULL)
1190 return NULL;
1191 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 Py_DECREF(selfi);
1193 if (cmp > 0) {
Martin Panter996d72b2016-07-25 02:21:14 +00001194 if (array_del_slice(self, i, i+1) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001196 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 }
1198 else if (cmp < 0)
1199 return NULL;
1200 }
Jim Fasarakis-Hilliarda4095ef2017-05-29 20:43:39 +03001201 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in array");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 return NULL;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001203}
1204
Brett Cannon1eb32c22014-10-10 16:26:45 -04001205/*[clinic input]
1206array.array.pop
1207
1208 i: Py_ssize_t = -1
1209 /
1210
1211Return the i-th element and delete it from the array.
1212
1213i defaults to -1.
1214[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001215
1216static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001217array_array_pop_impl(arrayobject *self, Py_ssize_t i)
1218/*[clinic end generated code: output=bc1f0c54fe5308e4 input=8e5feb4c1a11cd44]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001219{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 PyObject *v;
Brett Cannon1eb32c22014-10-10 16:26:45 -04001221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 if (Py_SIZE(self) == 0) {
1223 /* Special-case most common failure cause */
1224 PyErr_SetString(PyExc_IndexError, "pop from empty array");
1225 return NULL;
1226 }
1227 if (i < 0)
1228 i += Py_SIZE(self);
1229 if (i < 0 || i >= Py_SIZE(self)) {
1230 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1231 return NULL;
1232 }
Victor Stinner0b142e22013-07-17 23:01:30 +02001233 v = getarrayitem((PyObject *)self, i);
1234 if (v == NULL)
1235 return NULL;
Martin Panter996d72b2016-07-25 02:21:14 +00001236 if (array_del_slice(self, i, i+1) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 Py_DECREF(v);
1238 return NULL;
1239 }
1240 return v;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001241}
1242
Brett Cannon1eb32c22014-10-10 16:26:45 -04001243/*[clinic input]
1244array.array.extend
1245
1246 bb: object
1247 /
1248
1249Append items to the end of the array.
1250[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001251
1252static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001253array_array_extend(arrayobject *self, PyObject *bb)
1254/*[clinic end generated code: output=bbddbc8e8bef871d input=43be86aba5c31e44]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 if (array_do_extend(self, bb) == -1)
1257 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001258 Py_RETURN_NONE;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001259}
1260
Brett Cannon1eb32c22014-10-10 16:26:45 -04001261/*[clinic input]
1262array.array.insert
1263
1264 i: Py_ssize_t
1265 v: object
1266 /
1267
1268Insert a new item v into the array before position i.
1269[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001270
1271static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001272array_array_insert_impl(arrayobject *self, Py_ssize_t i, PyObject *v)
1273/*[clinic end generated code: output=5a3648e278348564 input=5577d1b4383e9313]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 return ins(self, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001276}
1277
Brett Cannon1eb32c22014-10-10 16:26:45 -04001278/*[clinic input]
1279array.array.buffer_info
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001280
Brett Cannon1eb32c22014-10-10 16:26:45 -04001281Return a tuple (address, length) giving the current memory address and the length in items of the buffer used to hold array's contents.
1282
1283The length should be multiplied by the itemsize attribute to calculate
1284the buffer length in bytes.
1285[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001286
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001287static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001288array_array_buffer_info_impl(arrayobject *self)
1289/*[clinic end generated code: output=9b2a4ec3ae7e98e7 input=a58bae5c6e1ac6a6]*/
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001290{
Victor Stinner541067a2013-11-14 01:27:12 +01001291 PyObject *retval = NULL, *v;
1292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 retval = PyTuple_New(2);
1294 if (!retval)
1295 return NULL;
Fred Drake541dc3b2000-06-28 17:49:30 +00001296
Victor Stinner541067a2013-11-14 01:27:12 +01001297 v = PyLong_FromVoidPtr(self->ob_item);
1298 if (v == NULL) {
1299 Py_DECREF(retval);
1300 return NULL;
1301 }
1302 PyTuple_SET_ITEM(retval, 0, v);
1303
Serhiy Storchaka9e941d62016-06-23 23:55:34 +03001304 v = PyLong_FromSsize_t(Py_SIZE(self));
Victor Stinner541067a2013-11-14 01:27:12 +01001305 if (v == NULL) {
1306 Py_DECREF(retval);
1307 return NULL;
1308 }
1309 PyTuple_SET_ITEM(retval, 1, v);
Fred Drake541dc3b2000-06-28 17:49:30 +00001310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 return retval;
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001312}
1313
Brett Cannon1eb32c22014-10-10 16:26:45 -04001314/*[clinic input]
1315array.array.append
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001316
Brett Cannon1eb32c22014-10-10 16:26:45 -04001317 v: object
1318 /
1319
1320Append new value v to the end of the array.
1321[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001322
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001323static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001324array_array_append(arrayobject *self, PyObject *v)
1325/*[clinic end generated code: output=745a0669bf8db0e2 input=0b98d9d78e78f0fa]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001326{
Mark Dickinson346f0af2010-08-06 09:36:57 +00001327 return ins(self, Py_SIZE(self), v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001328}
1329
Brett Cannon1eb32c22014-10-10 16:26:45 -04001330/*[clinic input]
1331array.array.byteswap
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001332
Brett Cannon1eb32c22014-10-10 16:26:45 -04001333Byteswap all items of the array.
1334
1335If the items in the array are not 1, 2, 4, or 8 bytes in size, RuntimeError is
1336raised.
1337[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001338
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001339static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001340array_array_byteswap_impl(arrayobject *self)
1341/*[clinic end generated code: output=5f8236cbdf0d90b5 input=6a85591b950a0186]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 char *p;
1344 Py_ssize_t i;
Fred Drakebf272981999-12-03 17:15:30 +00001345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 switch (self->ob_descr->itemsize) {
1347 case 1:
1348 break;
1349 case 2:
1350 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) {
1351 char p0 = p[0];
1352 p[0] = p[1];
1353 p[1] = p0;
1354 }
1355 break;
1356 case 4:
1357 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 4) {
1358 char p0 = p[0];
1359 char p1 = p[1];
1360 p[0] = p[3];
1361 p[1] = p[2];
1362 p[2] = p1;
1363 p[3] = p0;
1364 }
1365 break;
1366 case 8:
1367 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) {
1368 char p0 = p[0];
1369 char p1 = p[1];
1370 char p2 = p[2];
1371 char p3 = p[3];
1372 p[0] = p[7];
1373 p[1] = p[6];
1374 p[2] = p[5];
1375 p[3] = p[4];
1376 p[4] = p3;
1377 p[5] = p2;
1378 p[6] = p1;
1379 p[7] = p0;
1380 }
1381 break;
1382 default:
1383 PyErr_SetString(PyExc_RuntimeError,
1384 "don't know how to byteswap this array type");
1385 return NULL;
1386 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001387 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001388}
1389
Brett Cannon1eb32c22014-10-10 16:26:45 -04001390/*[clinic input]
1391array.array.reverse
1392
1393Reverse the order of the items in the array.
1394[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001395
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001396static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001397array_array_reverse_impl(arrayobject *self)
1398/*[clinic end generated code: output=c04868b36f6f4089 input=cd904f01b27d966a]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001399{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001400 Py_ssize_t itemsize = self->ob_descr->itemsize;
1401 char *p, *q;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 /* little buffer to hold items while swapping */
1403 char tmp[256]; /* 8 is probably enough -- but why skimp */
1404 assert((size_t)itemsize <= sizeof(tmp));
Guido van Rossume77a7571993-11-03 15:01:26 +00001405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 if (Py_SIZE(self) > 1) {
1407 for (p = self->ob_item,
1408 q = self->ob_item + (Py_SIZE(self) - 1)*itemsize;
1409 p < q;
1410 p += itemsize, q -= itemsize) {
1411 /* memory areas guaranteed disjoint, so memcpy
1412 * is safe (& memmove may be slower).
1413 */
1414 memcpy(tmp, p, itemsize);
1415 memcpy(p, q, itemsize);
1416 memcpy(q, tmp, itemsize);
1417 }
1418 }
Tim Petersbb307342000-09-10 05:22:54 +00001419
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001420 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001421}
Guido van Rossume77a7571993-11-03 15:01:26 +00001422
Brett Cannon1eb32c22014-10-10 16:26:45 -04001423/*[clinic input]
1424array.array.fromfile
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001425
Brett Cannon1eb32c22014-10-10 16:26:45 -04001426 f: object
1427 n: Py_ssize_t
1428 /
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001429
Brett Cannon1eb32c22014-10-10 16:26:45 -04001430Read n objects from the file object f and append them to the end of the array.
1431[clinic start generated code]*/
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001432
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001433static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001434array_array_fromfile_impl(arrayobject *self, PyObject *f, Py_ssize_t n)
1435/*[clinic end generated code: output=ec9f600e10f53510 input=e188afe8e58adf40]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001436{
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03001437 PyObject *b, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 Py_ssize_t itemsize = self->ob_descr->itemsize;
Brett Cannon1eb32c22014-10-10 16:26:45 -04001439 Py_ssize_t nbytes;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001440 _Py_IDENTIFIER(read);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 int not_enough_bytes;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001442
Mark Dickinsonc04ddff2012-10-06 18:04:49 +01001443 if (n < 0) {
1444 PyErr_SetString(PyExc_ValueError, "negative count");
1445 return NULL;
1446 }
1447 if (n > PY_SSIZE_T_MAX / itemsize) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 PyErr_NoMemory();
1449 return NULL;
1450 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +01001451 nbytes = n * itemsize;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001452
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001453 b = _PyObject_CallMethodId(f, &PyId_read, "n", nbytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 if (b == NULL)
1455 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 if (!PyBytes_Check(b)) {
1458 PyErr_SetString(PyExc_TypeError,
1459 "read() didn't return bytes");
1460 Py_DECREF(b);
1461 return NULL;
1462 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 not_enough_bytes = (PyBytes_GET_SIZE(b) != nbytes);
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001465
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03001466 res = array_array_frombytes(self, b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 Py_DECREF(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 if (res == NULL)
1469 return NULL;
Hirokazu Yamamoto54d0df62009-03-06 03:04:07 +00001470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 if (not_enough_bytes) {
1472 PyErr_SetString(PyExc_EOFError,
1473 "read() didn't return enough bytes");
1474 Py_DECREF(res);
1475 return NULL;
1476 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +00001479}
1480
Brett Cannon1eb32c22014-10-10 16:26:45 -04001481/*[clinic input]
1482array.array.tofile
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001483
Brett Cannon1eb32c22014-10-10 16:26:45 -04001484 f: object
1485 /
1486
1487Write all items (as machine values) to the file object f.
1488[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001489
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001490static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001491array_array_tofile(arrayobject *self, PyObject *f)
1492/*[clinic end generated code: output=3a2cfa8128df0777 input=b0669a484aab0831]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize;
1495 /* Write 64K blocks at a time */
1496 /* XXX Make the block size settable */
1497 int BLOCKSIZE = 64*1024;
1498 Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE;
1499 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 if (Py_SIZE(self) == 0)
1502 goto done;
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 for (i = 0; i < nblocks; i++) {
1505 char* ptr = self->ob_item + i*BLOCKSIZE;
1506 Py_ssize_t size = BLOCKSIZE;
1507 PyObject *bytes, *res;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001508 _Py_IDENTIFIER(write);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 if (i*BLOCKSIZE + size > nbytes)
1511 size = nbytes - i*BLOCKSIZE;
1512 bytes = PyBytes_FromStringAndSize(ptr, size);
1513 if (bytes == NULL)
1514 return NULL;
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001515 res = _PyObject_CallMethodIdOneArg(f, &PyId_write, bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 Py_DECREF(bytes);
1517 if (res == NULL)
1518 return NULL;
1519 Py_DECREF(res); /* drop write result */
1520 }
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001521
1522 done:
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001523 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001524}
1525
Brett Cannon1eb32c22014-10-10 16:26:45 -04001526/*[clinic input]
1527array.array.fromlist
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001528
Brett Cannon1eb32c22014-10-10 16:26:45 -04001529 list: object
1530 /
1531
1532Append items to array from list.
1533[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001534
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001535static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001536array_array_fromlist(arrayobject *self, PyObject *list)
1537/*[clinic end generated code: output=26411c2d228a3e3f input=be2605a96c49680f]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 Py_ssize_t n;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 if (!PyList_Check(list)) {
1542 PyErr_SetString(PyExc_TypeError, "arg must be list");
1543 return NULL;
1544 }
1545 n = PyList_Size(list);
1546 if (n > 0) {
1547 Py_ssize_t i, old_size;
1548 old_size = Py_SIZE(self);
1549 if (array_resize(self, old_size + n) == -1)
1550 return NULL;
1551 for (i = 0; i < n; i++) {
Zackery Spytz99d56b52018-12-08 07:16:55 -07001552 PyObject *v = PyList_GET_ITEM(list, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 if ((*self->ob_descr->setitem)(self,
1554 Py_SIZE(self) - n + i, v) != 0) {
1555 array_resize(self, old_size);
1556 return NULL;
1557 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07001558 if (n != PyList_GET_SIZE(list)) {
1559 PyErr_SetString(PyExc_RuntimeError,
1560 "list changed size during iteration");
1561 array_resize(self, old_size);
1562 return NULL;
1563 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 }
1565 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001566 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001567}
1568
Brett Cannon1eb32c22014-10-10 16:26:45 -04001569/*[clinic input]
1570array.array.tolist
1571
1572Convert array to an ordinary list with the same items.
1573[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001574
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001575static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001576array_array_tolist_impl(arrayobject *self)
1577/*[clinic end generated code: output=00b60cc9eab8ef89 input=a8d7784a94f86b53]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001578{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 PyObject *list = PyList_New(Py_SIZE(self));
1580 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 if (list == NULL)
1583 return NULL;
1584 for (i = 0; i < Py_SIZE(self); i++) {
1585 PyObject *v = getarrayitem((PyObject *)self, i);
Victor Stinner4755bea2013-07-18 01:12:35 +02001586 if (v == NULL)
1587 goto error;
Zackery Spytz99d56b52018-12-08 07:16:55 -07001588 PyList_SET_ITEM(list, i, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 }
1590 return list;
Victor Stinner4755bea2013-07-18 01:12:35 +02001591
1592error:
1593 Py_DECREF(list);
1594 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001595}
1596
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001597static PyObject *
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001598frombytes(arrayobject *self, Py_buffer *buffer)
Guido van Rossum778983b1993-02-19 15:55:02 +00001599{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 int itemsize = self->ob_descr->itemsize;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001601 Py_ssize_t n;
1602 if (buffer->itemsize != 1) {
1603 PyBuffer_Release(buffer);
Serhiy Storchakab757c832014-12-05 22:25:22 +02001604 PyErr_SetString(PyExc_TypeError, "a bytes-like object is required");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 return NULL;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001606 }
1607 n = buffer->len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 if (n % itemsize != 0) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001609 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 PyErr_SetString(PyExc_ValueError,
Serhiy Storchakab757c832014-12-05 22:25:22 +02001611 "bytes length not a multiple of item size");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 return NULL;
1613 }
1614 n = n / itemsize;
1615 if (n > 0) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001616 Py_ssize_t old_size = Py_SIZE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 if ((n > PY_SSIZE_T_MAX - old_size) ||
1618 ((old_size + n) > PY_SSIZE_T_MAX / itemsize)) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001619 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 return PyErr_NoMemory();
1621 }
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001622 if (array_resize(self, old_size + n) == -1) {
1623 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 return NULL;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001625 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 memcpy(self->ob_item + old_size * itemsize,
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001627 buffer->buf, n * itemsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 }
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001629 PyBuffer_Release(buffer);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001630 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001631}
1632
Brett Cannon1eb32c22014-10-10 16:26:45 -04001633/*[clinic input]
Brett Cannon1eb32c22014-10-10 16:26:45 -04001634array.array.frombytes
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001635
Brett Cannon1eb32c22014-10-10 16:26:45 -04001636 buffer: Py_buffer
1637 /
1638
1639Appends 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).
1640[clinic start generated code]*/
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001641
1642static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001643array_array_frombytes_impl(arrayobject *self, Py_buffer *buffer)
1644/*[clinic end generated code: output=d9842c8f7510a516 input=2bbf2b53ebfcc988]*/
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001645{
Brett Cannon1eb32c22014-10-10 16:26:45 -04001646 return frombytes(self, buffer);
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001647}
1648
Brett Cannon1eb32c22014-10-10 16:26:45 -04001649/*[clinic input]
1650array.array.tobytes
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001651
Brett Cannon1eb32c22014-10-10 16:26:45 -04001652Convert the array to an array of machine values and return the bytes representation.
1653[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001654
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001655static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001656array_array_tobytes_impl(arrayobject *self)
1657/*[clinic end generated code: output=87318e4edcdc2bb6 input=90ee495f96de34f5]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001658{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
1660 return PyBytes_FromStringAndSize(self->ob_item,
1661 Py_SIZE(self) * self->ob_descr->itemsize);
1662 } else {
1663 return PyErr_NoMemory();
1664 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001665}
1666
Brett Cannon1eb32c22014-10-10 16:26:45 -04001667/*[clinic input]
Brett Cannon1eb32c22014-10-10 16:26:45 -04001668array.array.fromunicode
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001669
Inada Naokid5d9a712020-05-11 15:37:25 +09001670 ustr: unicode
Brett Cannon1eb32c22014-10-10 16:26:45 -04001671 /
1672
1673Extends this array with data from the unicode string ustr.
1674
1675The array must be a unicode type array; otherwise a ValueError is raised.
1676Use array.frombytes(ustr.encode(...)) to append Unicode data to an array of
1677some other type.
1678[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001679
Martin v. Löwis99866332002-03-01 10:27:01 +00001680static PyObject *
Inada Naokid5d9a712020-05-11 15:37:25 +09001681array_array_fromunicode_impl(arrayobject *self, PyObject *ustr)
1682/*[clinic end generated code: output=24359f5e001a7f2b input=025db1fdade7a4ce]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001683{
Inada Naokid5d9a712020-05-11 15:37:25 +09001684 if (self->ob_descr->typecode != 'u') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 PyErr_SetString(PyExc_ValueError,
1686 "fromunicode() may only be called on "
1687 "unicode type arrays");
1688 return NULL;
1689 }
Inada Naokid5d9a712020-05-11 15:37:25 +09001690
1691 Py_ssize_t ustr_length = PyUnicode_AsWideChar(ustr, NULL, 0);
1692 assert(ustr_length > 0);
1693 if (ustr_length > 1) {
1694 ustr_length--; /* trim trailing NUL character */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 Py_ssize_t old_size = Py_SIZE(self);
Inada Naokid5d9a712020-05-11 15:37:25 +09001696 if (array_resize(self, old_size + ustr_length) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 return NULL;
Inada Naokid5d9a712020-05-11 15:37:25 +09001698 }
1699
1700 // must not fail
1701 PyUnicode_AsWideChar(
1702 ustr, ((wchar_t *)self->ob_item) + old_size, ustr_length);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 }
Martin v. Löwis99866332002-03-01 10:27:01 +00001704
Brett Cannon1eb32c22014-10-10 16:26:45 -04001705 Py_RETURN_NONE;
Martin v. Löwis99866332002-03-01 10:27:01 +00001706}
1707
Brett Cannon1eb32c22014-10-10 16:26:45 -04001708/*[clinic input]
1709array.array.tounicode
Martin v. Löwis99866332002-03-01 10:27:01 +00001710
Brett Cannon1eb32c22014-10-10 16:26:45 -04001711Extends this array with data from the unicode string ustr.
1712
1713Convert the array to a unicode string. The array must be a unicode type array;
1714otherwise a ValueError is raised. Use array.tobytes().decode() to obtain a
1715unicode string from an array of some other type.
1716[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001717
1718static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001719array_array_tounicode_impl(arrayobject *self)
1720/*[clinic end generated code: output=08e442378336e1ef input=127242eebe70b66d]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001721{
Inada Naokid5d9a712020-05-11 15:37:25 +09001722 if (self->ob_descr->typecode != 'u') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 PyErr_SetString(PyExc_ValueError,
1724 "tounicode() may only be called on unicode type arrays");
1725 return NULL;
1726 }
Inada Naokid5d9a712020-05-11 15:37:25 +09001727 return PyUnicode_FromWideChar((wchar_t *) self->ob_item, Py_SIZE(self));
Martin v. Löwis99866332002-03-01 10:27:01 +00001728}
1729
Brett Cannon1eb32c22014-10-10 16:26:45 -04001730/*[clinic input]
1731array.array.__sizeof__
Martin v. Löwis99866332002-03-01 10:27:01 +00001732
Brett Cannon1eb32c22014-10-10 16:26:45 -04001733Size of the array in memory, in bytes.
1734[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001735
Meador Inge03b4d502012-08-10 22:35:45 -05001736static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001737array_array___sizeof___impl(arrayobject *self)
1738/*[clinic end generated code: output=d8e1c61ebbe3eaed input=805586565bf2b3c6]*/
Meador Inge03b4d502012-08-10 22:35:45 -05001739{
1740 Py_ssize_t res;
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02001741 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * self->ob_descr->itemsize;
Meador Inge03b4d502012-08-10 22:35:45 -05001742 return PyLong_FromSsize_t(res);
1743}
1744
Martin v. Löwis99866332002-03-01 10:27:01 +00001745
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001746/*********************** Pickling support ************************/
1747
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001748static const struct mformatdescr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 size_t size;
1750 int is_signed;
1751 int is_big_endian;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001752} mformat_descriptors[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 {1, 0, 0}, /* 0: UNSIGNED_INT8 */
1754 {1, 1, 0}, /* 1: SIGNED_INT8 */
1755 {2, 0, 0}, /* 2: UNSIGNED_INT16_LE */
1756 {2, 0, 1}, /* 3: UNSIGNED_INT16_BE */
1757 {2, 1, 0}, /* 4: SIGNED_INT16_LE */
1758 {2, 1, 1}, /* 5: SIGNED_INT16_BE */
1759 {4, 0, 0}, /* 6: UNSIGNED_INT32_LE */
1760 {4, 0, 1}, /* 7: UNSIGNED_INT32_BE */
1761 {4, 1, 0}, /* 8: SIGNED_INT32_LE */
1762 {4, 1, 1}, /* 9: SIGNED_INT32_BE */
1763 {8, 0, 0}, /* 10: UNSIGNED_INT64_LE */
1764 {8, 0, 1}, /* 11: UNSIGNED_INT64_BE */
1765 {8, 1, 0}, /* 12: SIGNED_INT64_LE */
1766 {8, 1, 1}, /* 13: SIGNED_INT64_BE */
1767 {4, 0, 0}, /* 14: IEEE_754_FLOAT_LE */
1768 {4, 0, 1}, /* 15: IEEE_754_FLOAT_BE */
1769 {8, 0, 0}, /* 16: IEEE_754_DOUBLE_LE */
1770 {8, 0, 1}, /* 17: IEEE_754_DOUBLE_BE */
1771 {4, 0, 0}, /* 18: UTF16_LE */
1772 {4, 0, 1}, /* 19: UTF16_BE */
1773 {8, 0, 0}, /* 20: UTF32_LE */
1774 {8, 0, 1} /* 21: UTF32_BE */
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001775};
1776
1777
1778/*
1779 * Internal: This function is used to find the machine format of a given
1780 * array type code. This returns UNKNOWN_FORMAT when the machine format cannot
1781 * be found.
1782 */
1783static enum machine_format_code
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001784typecode_to_mformat_code(char typecode)
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001785{
Christian Heimes743e0cd2012-10-17 23:52:17 +02001786 const int is_big_endian = PY_BIG_ENDIAN;
1787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 size_t intsize;
1789 int is_signed;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 switch (typecode) {
1792 case 'b':
1793 return SIGNED_INT8;
1794 case 'B':
1795 return UNSIGNED_INT8;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 case 'u':
Victor Stinner62bb3942012-08-06 00:46:05 +02001798 if (sizeof(Py_UNICODE) == 2) {
1799 return UTF16_LE + is_big_endian;
1800 }
1801 if (sizeof(Py_UNICODE) == 4) {
1802 return UTF32_LE + is_big_endian;
1803 }
1804 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 case 'f':
1807 if (sizeof(float) == 4) {
1808 const float y = 16711938.0;
1809 if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0)
1810 return IEEE_754_FLOAT_BE;
1811 if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0)
1812 return IEEE_754_FLOAT_LE;
1813 }
1814 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 case 'd':
1817 if (sizeof(double) == 8) {
1818 const double x = 9006104071832581.0;
1819 if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0)
1820 return IEEE_754_DOUBLE_BE;
1821 if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
1822 return IEEE_754_DOUBLE_LE;
1823 }
1824 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 /* Integers */
1827 case 'h':
1828 intsize = sizeof(short);
1829 is_signed = 1;
1830 break;
1831 case 'H':
1832 intsize = sizeof(short);
1833 is_signed = 0;
1834 break;
1835 case 'i':
1836 intsize = sizeof(int);
1837 is_signed = 1;
1838 break;
1839 case 'I':
1840 intsize = sizeof(int);
1841 is_signed = 0;
1842 break;
1843 case 'l':
1844 intsize = sizeof(long);
1845 is_signed = 1;
1846 break;
1847 case 'L':
1848 intsize = sizeof(long);
1849 is_signed = 0;
1850 break;
Meador Inge1c9f0c92011-09-20 19:55:51 -05001851 case 'q':
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001852 intsize = sizeof(long long);
Meador Inge1c9f0c92011-09-20 19:55:51 -05001853 is_signed = 1;
1854 break;
1855 case 'Q':
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001856 intsize = sizeof(long long);
Meador Inge1c9f0c92011-09-20 19:55:51 -05001857 is_signed = 0;
1858 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 default:
1860 return UNKNOWN_FORMAT;
1861 }
1862 switch (intsize) {
1863 case 2:
1864 return UNSIGNED_INT16_LE + is_big_endian + (2 * is_signed);
1865 case 4:
1866 return UNSIGNED_INT32_LE + is_big_endian + (2 * is_signed);
1867 case 8:
1868 return UNSIGNED_INT64_LE + is_big_endian + (2 * is_signed);
1869 default:
1870 return UNKNOWN_FORMAT;
1871 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001872}
1873
1874/* Forward declaration. */
1875static PyObject *array_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
1876
1877/*
1878 * Internal: This function wraps the array constructor--i.e., array_new()--to
1879 * allow the creation of array objects from C code without having to deal
1880 * directly the tuple argument of array_new(). The typecode argument is a
1881 * Unicode character value, like 'i' or 'f' for example, representing an array
1882 * type code. The items argument is a bytes or a list object from which
1883 * contains the initial value of the array.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 *
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001885 * On success, this functions returns the array object created. Otherwise,
1886 * NULL is returned to indicate a failure.
1887 */
1888static PyObject *
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001889make_array(PyTypeObject *arraytype, char typecode, PyObject *items)
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001890{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 PyObject *new_args;
1892 PyObject *array_obj;
1893 PyObject *typecode_obj;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 assert(arraytype != NULL);
1896 assert(items != NULL);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001897
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001898 typecode_obj = PyUnicode_FromOrdinal(typecode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 if (typecode_obj == NULL)
1900 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 new_args = PyTuple_New(2);
Mat M56935a52017-11-14 01:00:54 -05001903 if (new_args == NULL) {
1904 Py_DECREF(typecode_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 return NULL;
Mat M56935a52017-11-14 01:00:54 -05001906 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 Py_INCREF(items);
1908 PyTuple_SET_ITEM(new_args, 0, typecode_obj);
1909 PyTuple_SET_ITEM(new_args, 1, items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 array_obj = array_new(arraytype, new_args, NULL);
1912 Py_DECREF(new_args);
1913 if (array_obj == NULL)
1914 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 return array_obj;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001917}
1918
1919/*
1920 * This functions is a special constructor used when unpickling an array. It
1921 * provides a portable way to rebuild an array from its memory representation.
1922 */
Brett Cannon1eb32c22014-10-10 16:26:45 -04001923/*[clinic input]
1924array._array_reconstructor
1925
1926 arraytype: object(type="PyTypeObject *")
Larry Hastingsdbfdc382015-05-04 06:59:46 -07001927 typecode: int(accept={str})
Brett Cannon1eb32c22014-10-10 16:26:45 -04001928 mformat_code: int(type="enum machine_format_code")
1929 items: object
1930 /
1931
1932Internal. Used for pickling support.
1933[clinic start generated code]*/
1934
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001935static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001936array__array_reconstructor_impl(PyObject *module, PyTypeObject *arraytype,
Larry Hastings89964c42015-04-14 18:07:59 -04001937 int typecode,
1938 enum machine_format_code mformat_code,
1939 PyObject *items)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001940/*[clinic end generated code: output=e05263141ba28365 input=2464dc8f4c7736b5]*/
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001941{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 PyObject *converted_items;
1943 PyObject *result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001944 const struct arraydescr *descr;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 if (!PyType_Check(arraytype)) {
1947 PyErr_Format(PyExc_TypeError,
Sylvaina90e64b2017-03-29 20:09:22 +02001948 "first argument must be a type object, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 Py_TYPE(arraytype)->tp_name);
1950 return NULL;
1951 }
1952 if (!PyType_IsSubtype(arraytype, &Arraytype)) {
1953 PyErr_Format(PyExc_TypeError,
1954 "%.200s is not a subtype of %.200s",
1955 arraytype->tp_name, Arraytype.tp_name);
1956 return NULL;
1957 }
1958 for (descr = descriptors; descr->typecode != '\0'; descr++) {
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001959 if ((int)descr->typecode == typecode)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 break;
1961 }
1962 if (descr->typecode == '\0') {
1963 PyErr_SetString(PyExc_ValueError,
1964 "second argument must be a valid type code");
1965 return NULL;
1966 }
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001967 if (mformat_code < MACHINE_FORMAT_CODE_MIN ||
1968 mformat_code > MACHINE_FORMAT_CODE_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 PyErr_SetString(PyExc_ValueError,
1970 "third argument must be a valid machine format code.");
1971 return NULL;
1972 }
1973 if (!PyBytes_Check(items)) {
1974 PyErr_Format(PyExc_TypeError,
1975 "fourth argument should be bytes, not %.200s",
1976 Py_TYPE(items)->tp_name);
1977 return NULL;
1978 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 /* Fast path: No decoding has to be done. */
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001981 if (mformat_code == typecode_to_mformat_code((char)typecode) ||
1982 mformat_code == UNKNOWN_FORMAT) {
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001983 return make_array(arraytype, (char)typecode, items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001986 /* Slow path: Decode the byte string according to the given machine
1987 * format code. This occurs when the computer unpickling the array
1988 * object is architecturally different from the one that pickled the
1989 * array.
1990 */
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001991 if (Py_SIZE(items) % mformat_descriptors[mformat_code].size != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 PyErr_SetString(PyExc_ValueError,
1993 "string length not a multiple of item size");
1994 return NULL;
1995 }
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001996 switch (mformat_code) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 case IEEE_754_FLOAT_LE:
1998 case IEEE_754_FLOAT_BE: {
sthaa3ecb82019-03-20 20:49:39 +01001999 Py_ssize_t i;
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002000 int le = (mformat_code == IEEE_754_FLOAT_LE) ? 1 : 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001 Py_ssize_t itemcount = Py_SIZE(items) / 4;
2002 const unsigned char *memstr =
2003 (unsigned char *)PyBytes_AS_STRING(items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 converted_items = PyList_New(itemcount);
2006 if (converted_items == NULL)
2007 return NULL;
2008 for (i = 0; i < itemcount; i++) {
2009 PyObject *pyfloat = PyFloat_FromDouble(
2010 _PyFloat_Unpack4(&memstr[i * 4], le));
2011 if (pyfloat == NULL) {
2012 Py_DECREF(converted_items);
2013 return NULL;
2014 }
2015 PyList_SET_ITEM(converted_items, i, pyfloat);
2016 }
2017 break;
2018 }
2019 case IEEE_754_DOUBLE_LE:
2020 case IEEE_754_DOUBLE_BE: {
sthaa3ecb82019-03-20 20:49:39 +01002021 Py_ssize_t i;
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002022 int le = (mformat_code == IEEE_754_DOUBLE_LE) ? 1 : 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 Py_ssize_t itemcount = Py_SIZE(items) / 8;
2024 const unsigned char *memstr =
2025 (unsigned char *)PyBytes_AS_STRING(items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 converted_items = PyList_New(itemcount);
2028 if (converted_items == NULL)
2029 return NULL;
2030 for (i = 0; i < itemcount; i++) {
2031 PyObject *pyfloat = PyFloat_FromDouble(
2032 _PyFloat_Unpack8(&memstr[i * 8], le));
2033 if (pyfloat == NULL) {
2034 Py_DECREF(converted_items);
2035 return NULL;
2036 }
2037 PyList_SET_ITEM(converted_items, i, pyfloat);
2038 }
2039 break;
2040 }
2041 case UTF16_LE:
2042 case UTF16_BE: {
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002043 int byteorder = (mformat_code == UTF16_LE) ? -1 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 converted_items = PyUnicode_DecodeUTF16(
2045 PyBytes_AS_STRING(items), Py_SIZE(items),
2046 "strict", &byteorder);
2047 if (converted_items == NULL)
2048 return NULL;
2049 break;
2050 }
2051 case UTF32_LE:
2052 case UTF32_BE: {
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002053 int byteorder = (mformat_code == UTF32_LE) ? -1 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 converted_items = PyUnicode_DecodeUTF32(
2055 PyBytes_AS_STRING(items), Py_SIZE(items),
2056 "strict", &byteorder);
2057 if (converted_items == NULL)
2058 return NULL;
2059 break;
2060 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 case UNSIGNED_INT8:
2063 case SIGNED_INT8:
2064 case UNSIGNED_INT16_LE:
2065 case UNSIGNED_INT16_BE:
2066 case SIGNED_INT16_LE:
2067 case SIGNED_INT16_BE:
2068 case UNSIGNED_INT32_LE:
2069 case UNSIGNED_INT32_BE:
2070 case SIGNED_INT32_LE:
2071 case SIGNED_INT32_BE:
2072 case UNSIGNED_INT64_LE:
2073 case UNSIGNED_INT64_BE:
2074 case SIGNED_INT64_LE:
2075 case SIGNED_INT64_BE: {
sthaa3ecb82019-03-20 20:49:39 +01002076 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 const struct mformatdescr mf_descr =
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002078 mformat_descriptors[mformat_code];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002079 Py_ssize_t itemcount = Py_SIZE(items) / mf_descr.size;
2080 const unsigned char *memstr =
2081 (unsigned char *)PyBytes_AS_STRING(items);
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002082 const struct arraydescr *descr;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 /* If possible, try to pack array's items using a data type
2085 * that fits better. This may result in an array with narrower
2086 * or wider elements.
2087 *
Martin Panter4c359642016-05-08 13:53:41 +00002088 * For example, if a 32-bit machine pickles an L-code array of
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 * unsigned longs, then the array will be unpickled by 64-bit
2090 * machine as an I-code array of unsigned ints.
2091 *
2092 * XXX: Is it possible to write a unit test for this?
2093 */
2094 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2095 if (descr->is_integer_type &&
Victor Stinner706768c2014-08-16 01:03:39 +02002096 (size_t)descr->itemsize == mf_descr.size &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 descr->is_signed == mf_descr.is_signed)
2098 typecode = descr->typecode;
2099 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 converted_items = PyList_New(itemcount);
2102 if (converted_items == NULL)
2103 return NULL;
2104 for (i = 0; i < itemcount; i++) {
2105 PyObject *pylong;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 pylong = _PyLong_FromByteArray(
2108 &memstr[i * mf_descr.size],
2109 mf_descr.size,
2110 !mf_descr.is_big_endian,
2111 mf_descr.is_signed);
2112 if (pylong == NULL) {
2113 Py_DECREF(converted_items);
2114 return NULL;
2115 }
2116 PyList_SET_ITEM(converted_items, i, pylong);
2117 }
2118 break;
2119 }
2120 case UNKNOWN_FORMAT:
2121 /* Impossible, but needed to shut up GCC about the unhandled
2122 * enumeration value.
2123 */
2124 default:
2125 PyErr_BadArgument();
2126 return NULL;
2127 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002128
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002129 result = make_array(arraytype, (char)typecode, converted_items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 Py_DECREF(converted_items);
2131 return result;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002132}
2133
Brett Cannon1eb32c22014-10-10 16:26:45 -04002134/*[clinic input]
2135array.array.__reduce_ex__
2136
2137 value: object
2138 /
2139
2140Return state information for pickling.
2141[clinic start generated code]*/
2142
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002143static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04002144array_array___reduce_ex__(arrayobject *self, PyObject *value)
2145/*[clinic end generated code: output=051e0a6175d0eddb input=c36c3f85de7df6cd]*/
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 PyObject *dict;
2148 PyObject *result;
2149 PyObject *array_str;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002150 int typecode = self->ob_descr->typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 int mformat_code;
2152 static PyObject *array_reconstructor = NULL;
2153 long protocol;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002154 _Py_IDENTIFIER(_array_reconstructor);
2155 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 if (array_reconstructor == NULL) {
2158 PyObject *array_module = PyImport_ImportModule("array");
2159 if (array_module == NULL)
2160 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002161 array_reconstructor = _PyObject_GetAttrId(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 array_module,
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002163 &PyId__array_reconstructor);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 Py_DECREF(array_module);
2165 if (array_reconstructor == NULL)
2166 return NULL;
2167 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 if (!PyLong_Check(value)) {
2170 PyErr_SetString(PyExc_TypeError,
Sylvaina90e64b2017-03-29 20:09:22 +02002171 "__reduce_ex__ argument should be an integer");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172 return NULL;
2173 }
2174 protocol = PyLong_AsLong(value);
2175 if (protocol == -1 && PyErr_Occurred())
2176 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002177
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002178 if (_PyObject_LookupAttrId((PyObject *)self, &PyId___dict__, &dict) < 0) {
2179 return NULL;
2180 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 dict = Py_None;
2183 Py_INCREF(dict);
2184 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 mformat_code = typecode_to_mformat_code(typecode);
2187 if (mformat_code == UNKNOWN_FORMAT || protocol < 3) {
2188 /* Convert the array to a list if we got something weird
2189 * (e.g., non-IEEE floats), or we are pickling the array using
2190 * a Python 2.x compatible protocol.
2191 *
2192 * It is necessary to use a list representation for Python 2.x
2193 * compatible pickle protocol, since Python 2's str objects
2194 * are unpickled as unicode by Python 3. Thus it is impossible
2195 * to make arrays unpicklable by Python 3 by using their memory
2196 * representation, unless we resort to ugly hacks such as
2197 * coercing unicode objects to bytes in array_reconstructor.
2198 */
2199 PyObject *list;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002200 list = array_array_tolist_impl(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002201 if (list == NULL) {
2202 Py_DECREF(dict);
2203 return NULL;
2204 }
2205 result = Py_BuildValue(
Brett Cannon1eb32c22014-10-10 16:26:45 -04002206 "O(CO)O", Py_TYPE(self), typecode, list, dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002207 Py_DECREF(list);
2208 Py_DECREF(dict);
2209 return result;
2210 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002211
Brett Cannon1eb32c22014-10-10 16:26:45 -04002212 array_str = array_array_tobytes_impl(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 if (array_str == NULL) {
2214 Py_DECREF(dict);
2215 return NULL;
2216 }
2217 result = Py_BuildValue(
Brett Cannon1eb32c22014-10-10 16:26:45 -04002218 "O(OCiN)O", array_reconstructor, Py_TYPE(self), typecode,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 mformat_code, array_str, dict);
2220 Py_DECREF(dict);
2221 return result;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002222}
2223
Martin v. Löwis99866332002-03-01 10:27:01 +00002224static PyObject *
2225array_get_typecode(arrayobject *a, void *closure)
2226{
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002227 char typecode = a->ob_descr->typecode;
2228 return PyUnicode_FromOrdinal(typecode);
Martin v. Löwis99866332002-03-01 10:27:01 +00002229}
2230
2231static PyObject *
2232array_get_itemsize(arrayobject *a, void *closure)
2233{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 return PyLong_FromLong((long)a->ob_descr->itemsize);
Martin v. Löwis99866332002-03-01 10:27:01 +00002235}
2236
2237static PyGetSetDef array_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 {"typecode", (getter) array_get_typecode, NULL,
2239 "the typecode character used to create the array"},
2240 {"itemsize", (getter) array_get_itemsize, NULL,
2241 "the size, in bytes, of one array item"},
2242 {NULL}
Martin v. Löwis99866332002-03-01 10:27:01 +00002243};
2244
Martin v. Löwis59683e82008-06-13 07:50:45 +00002245static PyMethodDef array_methods[] = {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002246 ARRAY_ARRAY_APPEND_METHODDEF
2247 ARRAY_ARRAY_BUFFER_INFO_METHODDEF
2248 ARRAY_ARRAY_BYTESWAP_METHODDEF
2249 ARRAY_ARRAY___COPY___METHODDEF
2250 ARRAY_ARRAY_COUNT_METHODDEF
2251 ARRAY_ARRAY___DEEPCOPY___METHODDEF
2252 ARRAY_ARRAY_EXTEND_METHODDEF
2253 ARRAY_ARRAY_FROMFILE_METHODDEF
2254 ARRAY_ARRAY_FROMLIST_METHODDEF
Brett Cannon1eb32c22014-10-10 16:26:45 -04002255 ARRAY_ARRAY_FROMBYTES_METHODDEF
2256 ARRAY_ARRAY_FROMUNICODE_METHODDEF
2257 ARRAY_ARRAY_INDEX_METHODDEF
2258 ARRAY_ARRAY_INSERT_METHODDEF
2259 ARRAY_ARRAY_POP_METHODDEF
2260 ARRAY_ARRAY___REDUCE_EX___METHODDEF
2261 ARRAY_ARRAY_REMOVE_METHODDEF
2262 ARRAY_ARRAY_REVERSE_METHODDEF
2263 ARRAY_ARRAY_TOFILE_METHODDEF
2264 ARRAY_ARRAY_TOLIST_METHODDEF
Brett Cannon1eb32c22014-10-10 16:26:45 -04002265 ARRAY_ARRAY_TOBYTES_METHODDEF
2266 ARRAY_ARRAY_TOUNICODE_METHODDEF
2267 ARRAY_ARRAY___SIZEOF___METHODDEF
2268 {NULL, NULL} /* sentinel */
Guido van Rossum778983b1993-02-19 15:55:02 +00002269};
2270
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002271static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00002272array_repr(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +00002273{
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002274 char typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 PyObject *s, *v = NULL;
2276 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00002277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 len = Py_SIZE(a);
2279 typecode = a->ob_descr->typecode;
2280 if (len == 0) {
Serhiy Storchakab3a77962017-09-21 14:24:13 +03002281 return PyUnicode_FromFormat("%s('%c')",
2282 _PyType_Name(Py_TYPE(a)), (int)typecode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 }
Gregory P. Smith9504b132012-12-10 20:20:20 -08002284 if (typecode == 'u') {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002285 v = array_array_tounicode_impl(a);
Gregory P. Smith9504b132012-12-10 20:20:20 -08002286 } else {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002287 v = array_array_tolist_impl(a);
Gregory P. Smith9504b132012-12-10 20:20:20 -08002288 }
Victor Stinner29ec5952013-02-26 00:27:38 +01002289 if (v == NULL)
2290 return NULL;
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00002291
Serhiy Storchakab3a77962017-09-21 14:24:13 +03002292 s = PyUnicode_FromFormat("%s('%c', %R)",
2293 _PyType_Name(Py_TYPE(a)), (int)typecode, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 Py_DECREF(v);
2295 return s;
Guido van Rossum778983b1993-02-19 15:55:02 +00002296}
2297
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002298static PyObject*
2299array_subscr(arrayobject* self, PyObject* item)
2300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 if (PyIndex_Check(item)) {
2302 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2303 if (i==-1 && PyErr_Occurred()) {
2304 return NULL;
2305 }
2306 if (i < 0)
2307 i += Py_SIZE(self);
2308 return array_item(self, i);
2309 }
2310 else if (PySlice_Check(item)) {
Zackery Spytz14514d92019-05-17 01:13:03 -06002311 Py_ssize_t start, stop, step, slicelength, i;
2312 size_t cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 PyObject* result;
2314 arrayobject* ar;
2315 int itemsize = self->ob_descr->itemsize;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002316
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002317 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 return NULL;
2319 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002320 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2321 step);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 if (slicelength <= 0) {
2324 return newarrayobject(&Arraytype, 0, self->ob_descr);
2325 }
2326 else if (step == 1) {
2327 PyObject *result = newarrayobject(&Arraytype,
2328 slicelength, self->ob_descr);
2329 if (result == NULL)
2330 return NULL;
2331 memcpy(((arrayobject *)result)->ob_item,
2332 self->ob_item + start * itemsize,
2333 slicelength * itemsize);
2334 return result;
2335 }
2336 else {
2337 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
2338 if (!result) return NULL;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 ar = (arrayobject*)result;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 for (cur = start, i = 0; i < slicelength;
2343 cur += step, i++) {
2344 memcpy(ar->ob_item + i*itemsize,
2345 self->ob_item + cur*itemsize,
2346 itemsize);
2347 }
2348
2349 return result;
2350 }
2351 }
2352 else {
2353 PyErr_SetString(PyExc_TypeError,
2354 "array indices must be integers");
2355 return NULL;
2356 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002357}
2358
2359static int
2360array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
2361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 Py_ssize_t start, stop, step, slicelength, needed;
2363 arrayobject* other;
2364 int itemsize;
Thomas Woutersed03b412007-08-28 21:37:11 +00002365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 if (PyIndex_Check(item)) {
2367 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Alexandre Vassalotti47137252009-07-05 19:57:00 +00002368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 if (i == -1 && PyErr_Occurred())
2370 return -1;
2371 if (i < 0)
2372 i += Py_SIZE(self);
2373 if (i < 0 || i >= Py_SIZE(self)) {
2374 PyErr_SetString(PyExc_IndexError,
2375 "array assignment index out of range");
2376 return -1;
2377 }
2378 if (value == NULL) {
2379 /* Fall through to slice assignment */
2380 start = i;
2381 stop = i + 1;
2382 step = 1;
2383 slicelength = 1;
2384 }
2385 else
2386 return (*self->ob_descr->setitem)(self, i, value);
2387 }
2388 else if (PySlice_Check(item)) {
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002389 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 return -1;
2391 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002392 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2393 step);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 }
2395 else {
2396 PyErr_SetString(PyExc_TypeError,
Sylvaina90e64b2017-03-29 20:09:22 +02002397 "array indices must be integers");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 return -1;
2399 }
2400 if (value == NULL) {
2401 other = NULL;
2402 needed = 0;
2403 }
2404 else if (array_Check(value)) {
2405 other = (arrayobject *)value;
2406 needed = Py_SIZE(other);
2407 if (self == other) {
2408 /* Special case "self[i:j] = self" -- copy self first */
2409 int ret;
2410 value = array_slice(other, 0, needed);
2411 if (value == NULL)
2412 return -1;
2413 ret = array_ass_subscr(self, item, value);
2414 Py_DECREF(value);
2415 return ret;
2416 }
2417 if (other->ob_descr != self->ob_descr) {
2418 PyErr_BadArgument();
2419 return -1;
2420 }
2421 }
2422 else {
2423 PyErr_Format(PyExc_TypeError,
2424 "can only assign array (not \"%.200s\") to array slice",
2425 Py_TYPE(value)->tp_name);
2426 return -1;
2427 }
2428 itemsize = self->ob_descr->itemsize;
2429 /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
2430 if ((step > 0 && stop < start) ||
2431 (step < 0 && stop > start))
2432 stop = start;
Alexandre Vassalotti47137252009-07-05 19:57:00 +00002433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 /* Issue #4509: If the array has exported buffers and the slice
2435 assignment would change the size of the array, fail early to make
2436 sure we don't modify it. */
2437 if ((needed == 0 || slicelength != needed) && self->ob_exports > 0) {
2438 PyErr_SetString(PyExc_BufferError,
2439 "cannot resize an array that is exporting buffers");
2440 return -1;
2441 }
Mark Dickinsonbc099642010-01-29 17:27:24 +00002442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 if (step == 1) {
2444 if (slicelength > needed) {
2445 memmove(self->ob_item + (start + needed) * itemsize,
2446 self->ob_item + stop * itemsize,
2447 (Py_SIZE(self) - stop) * itemsize);
2448 if (array_resize(self, Py_SIZE(self) +
2449 needed - slicelength) < 0)
2450 return -1;
2451 }
2452 else if (slicelength < needed) {
2453 if (array_resize(self, Py_SIZE(self) +
2454 needed - slicelength) < 0)
2455 return -1;
2456 memmove(self->ob_item + (start + needed) * itemsize,
2457 self->ob_item + stop * itemsize,
2458 (Py_SIZE(self) - start - needed) * itemsize);
2459 }
2460 if (needed > 0)
2461 memcpy(self->ob_item + start * itemsize,
2462 other->ob_item, needed * itemsize);
2463 return 0;
2464 }
2465 else if (needed == 0) {
2466 /* Delete slice */
2467 size_t cur;
2468 Py_ssize_t i;
Thomas Woutersed03b412007-08-28 21:37:11 +00002469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 if (step < 0) {
2471 stop = start + 1;
2472 start = stop + step * (slicelength - 1) - 1;
2473 step = -step;
2474 }
2475 for (cur = start, i = 0; i < slicelength;
2476 cur += step, i++) {
2477 Py_ssize_t lim = step - 1;
Thomas Woutersed03b412007-08-28 21:37:11 +00002478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002479 if (cur + step >= (size_t)Py_SIZE(self))
2480 lim = Py_SIZE(self) - cur - 1;
2481 memmove(self->ob_item + (cur - i) * itemsize,
2482 self->ob_item + (cur + 1) * itemsize,
2483 lim * itemsize);
2484 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002485 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 if (cur < (size_t)Py_SIZE(self)) {
2487 memmove(self->ob_item + (cur-slicelength) * itemsize,
2488 self->ob_item + cur * itemsize,
2489 (Py_SIZE(self) - cur) * itemsize);
2490 }
2491 if (array_resize(self, Py_SIZE(self) - slicelength) < 0)
2492 return -1;
2493 return 0;
2494 }
2495 else {
Zackery Spytz14514d92019-05-17 01:13:03 -06002496 size_t cur;
2497 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002498
2499 if (needed != slicelength) {
2500 PyErr_Format(PyExc_ValueError,
2501 "attempt to assign array of size %zd "
2502 "to extended slice of size %zd",
2503 needed, slicelength);
2504 return -1;
2505 }
2506 for (cur = start, i = 0; i < slicelength;
2507 cur += step, i++) {
2508 memcpy(self->ob_item + cur * itemsize,
2509 other->ob_item + i * itemsize,
2510 itemsize);
2511 }
2512 return 0;
2513 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002514}
2515
2516static PyMappingMethods array_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002517 (lenfunc)array_length,
2518 (binaryfunc)array_subscr,
2519 (objobjargproc)array_ass_subscr
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002520};
2521
Guido van Rossumd8faa362007-04-27 19:54:29 +00002522static const void *emptybuf = "";
2523
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002524
2525static int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00002526array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002527{
Stefan Krah650c1e82015-02-03 21:43:23 +01002528 if (view == NULL) {
2529 PyErr_SetString(PyExc_BufferError,
2530 "array_buffer_getbuf: view==NULL argument is obsolete");
2531 return -1;
2532 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002534 view->buf = (void *)self->ob_item;
2535 view->obj = (PyObject*)self;
2536 Py_INCREF(self);
2537 if (view->buf == NULL)
2538 view->buf = (void *)emptybuf;
2539 view->len = (Py_SIZE(self)) * self->ob_descr->itemsize;
2540 view->readonly = 0;
2541 view->ndim = 1;
2542 view->itemsize = self->ob_descr->itemsize;
2543 view->suboffsets = NULL;
2544 view->shape = NULL;
2545 if ((flags & PyBUF_ND)==PyBUF_ND) {
2546 view->shape = &((Py_SIZE(self)));
2547 }
2548 view->strides = NULL;
2549 if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES)
2550 view->strides = &(view->itemsize);
2551 view->format = NULL;
2552 view->internal = NULL;
Victor Stinner62bb3942012-08-06 00:46:05 +02002553 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002554 view->format = (char *)self->ob_descr->formats;
Victor Stinner62bb3942012-08-06 00:46:05 +02002555#ifdef Py_UNICODE_WIDE
2556 if (self->ob_descr->typecode == 'u') {
2557 view->format = "w";
2558 }
2559#endif
2560 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002562 self->ob_exports++;
2563 return 0;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002564}
2565
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002566static void
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00002567array_buffer_relbuf(arrayobject *self, Py_buffer *view)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002569 self->ob_exports--;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002570}
2571
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002572static PySequenceMethods array_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002573 (lenfunc)array_length, /*sq_length*/
2574 (binaryfunc)array_concat, /*sq_concat*/
2575 (ssizeargfunc)array_repeat, /*sq_repeat*/
2576 (ssizeargfunc)array_item, /*sq_item*/
2577 0, /*sq_slice*/
2578 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
2579 0, /*sq_ass_slice*/
2580 (objobjproc)array_contains, /*sq_contains*/
2581 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
2582 (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/
Guido van Rossum778983b1993-02-19 15:55:02 +00002583};
2584
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002585static PyBufferProcs array_as_buffer = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002586 (getbufferproc)array_buffer_getbuf,
2587 (releasebufferproc)array_buffer_relbuf
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002588};
2589
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002590static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +00002591array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum778983b1993-02-19 15:55:02 +00002592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 int c;
2594 PyObject *initial = NULL, *it = NULL;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002595 const struct arraydescr *descr;
Martin v. Löwis99866332002-03-01 10:27:01 +00002596
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03002597 if (type == &Arraytype && !_PyArg_NoKeywords("array.array", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002598 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00002599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002600 if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
2601 return NULL;
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00002602
Steve Dowerb82e17e2019-05-23 08:45:22 -07002603 if (PySys_Audit("array.__new__", "CO",
2604 c, initial ? initial : Py_None) < 0) {
2605 return NULL;
2606 }
2607
Alexandre Vassalotti9730e332013-11-29 20:47:15 -08002608 if (initial && c != 'u') {
2609 if (PyUnicode_Check(initial)) {
2610 PyErr_Format(PyExc_TypeError, "cannot use a str to initialize "
2611 "an array with typecode '%c'", c);
2612 return NULL;
2613 }
2614 else if (array_Check(initial) &&
2615 ((arrayobject*)initial)->ob_descr->typecode == 'u') {
2616 PyErr_Format(PyExc_TypeError, "cannot use a unicode array to "
2617 "initialize an array with typecode '%c'", c);
2618 return NULL;
2619 }
2620 }
2621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002622 if (!(initial == NULL || PyList_Check(initial)
2623 || PyByteArray_Check(initial)
2624 || PyBytes_Check(initial)
2625 || PyTuple_Check(initial)
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002626 || ((c=='u') && PyUnicode_Check(initial))
2627 || (array_Check(initial)
2628 && c == ((arrayobject*)initial)->ob_descr->typecode))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629 it = PyObject_GetIter(initial);
2630 if (it == NULL)
2631 return NULL;
2632 /* We set initial to NULL so that the subsequent code
2633 will create an empty array of the appropriate type
2634 and afterwards we can use array_iter_extend to populate
2635 the array.
2636 */
2637 initial = NULL;
2638 }
2639 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2640 if (descr->typecode == c) {
2641 PyObject *a;
2642 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00002643
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002644 if (initial == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 len = 0;
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002646 else if (PyList_Check(initial))
2647 len = PyList_GET_SIZE(initial);
2648 else if (PyTuple_Check(initial) || array_Check(initial))
2649 len = Py_SIZE(initial);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 else
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002651 len = 0;
Martin v. Löwis99866332002-03-01 10:27:01 +00002652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 a = newarrayobject(type, len, descr);
2654 if (a == NULL)
2655 return NULL;
2656
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002657 if (len > 0 && !array_Check(initial)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002658 Py_ssize_t i;
2659 for (i = 0; i < len; i++) {
2660 PyObject *v =
2661 PySequence_GetItem(initial, i);
2662 if (v == NULL) {
2663 Py_DECREF(a);
2664 return NULL;
2665 }
2666 if (setarrayitem(a, i, v) != 0) {
2667 Py_DECREF(v);
2668 Py_DECREF(a);
2669 return NULL;
2670 }
2671 Py_DECREF(v);
2672 }
2673 }
2674 else if (initial != NULL && (PyByteArray_Check(initial) ||
2675 PyBytes_Check(initial))) {
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03002676 PyObject *v;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002677 v = array_array_frombytes((arrayobject *)a,
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03002678 initial);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 if (v == NULL) {
2680 Py_DECREF(a);
2681 return NULL;
2682 }
2683 Py_DECREF(v);
2684 }
2685 else if (initial != NULL && PyUnicode_Check(initial)) {
Victor Stinner1fbcaef2011-09-30 01:54:04 +02002686 Py_ssize_t n;
Inada Naokid5d9a712020-05-11 15:37:25 +09002687 wchar_t *ustr = PyUnicode_AsWideCharString(initial, &n);
Victor Stinner62bb3942012-08-06 00:46:05 +02002688 if (ustr == NULL) {
Victor Stinner1fbcaef2011-09-30 01:54:04 +02002689 Py_DECREF(a);
2690 return NULL;
2691 }
Victor Stinner62bb3942012-08-06 00:46:05 +02002692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002693 if (n > 0) {
2694 arrayobject *self = (arrayobject *)a;
Inada Naokid5d9a712020-05-11 15:37:25 +09002695 // self->ob_item may be NULL but it is safe.
2696 PyMem_Free(self->ob_item);
2697 self->ob_item = (char *)ustr;
2698 Py_SET_SIZE(self, n);
2699 self->allocated = n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 }
2701 }
Benjamin Peterson682124c2014-10-10 20:58:30 -04002702 else if (initial != NULL && array_Check(initial) && len > 0) {
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002703 arrayobject *self = (arrayobject *)a;
2704 arrayobject *other = (arrayobject *)initial;
2705 memcpy(self->ob_item, other->ob_item, len * other->ob_descr->itemsize);
2706 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002707 if (it != NULL) {
2708 if (array_iter_extend((arrayobject *)a, it) == -1) {
2709 Py_DECREF(it);
2710 Py_DECREF(a);
2711 return NULL;
2712 }
2713 Py_DECREF(it);
2714 }
2715 return a;
2716 }
2717 }
2718 PyErr_SetString(PyExc_ValueError,
Meador Inge1c9f0c92011-09-20 19:55:51 -05002719 "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 +00002720 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00002721}
2722
Guido van Rossum778983b1993-02-19 15:55:02 +00002723
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002724PyDoc_STRVAR(module_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00002725"This module defines an object type which can efficiently represent\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002726an array of basic values: characters, integers, floating point\n\
2727numbers. Arrays are sequence types and behave very much like lists,\n\
Alexandre Vassalotti9730e332013-11-29 20:47:15 -08002728except that the type of objects stored in them is constrained.\n");
2729
2730PyDoc_STRVAR(arraytype_doc,
2731"array(typecode [, initializer]) -> array\n\
2732\n\
2733Return a new array whose items are restricted by typecode, and\n\
2734initialized from the optional initializer value, which must be a list,\n\
2735string or iterable over elements of the appropriate type.\n\
2736\n\
2737Arrays represent basic values and behave very much like lists, except\n\
2738the type of objects stored in them is constrained. The type is specified\n\
2739at object creation time by using a type code, which is a single character.\n\
2740The following type codes are defined:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002741\n\
oldkaa0735f2018-02-02 16:52:55 +08002742 Type code C Type Minimum size in bytes\n\
2743 'b' signed integer 1\n\
2744 'B' unsigned integer 1\n\
2745 'u' Unicode character 2 (see note)\n\
2746 'h' signed integer 2\n\
2747 'H' unsigned integer 2\n\
2748 'i' signed integer 2\n\
2749 'I' unsigned integer 2\n\
2750 'l' signed integer 4\n\
2751 'L' unsigned integer 4\n\
2752 'q' signed integer 8 (see note)\n\
2753 'Q' unsigned integer 8 (see note)\n\
2754 'f' floating point 4\n\
2755 'd' floating point 8\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002756\n\
oldkaa0735f2018-02-02 16:52:55 +08002757NOTE: The 'u' typecode corresponds to Python's unicode character. On\n\
Victor Stinner62bb3942012-08-06 00:46:05 +02002758narrow builds this is 2-bytes on wide builds this is 4-bytes.\n\
2759\n\
oldkaa0735f2018-02-02 16:52:55 +08002760NOTE: The 'q' and 'Q' type codes are only available if the platform\n\
2761C compiler used to build Python supports 'long long', or, on Windows,\n\
Meador Inge1c9f0c92011-09-20 19:55:51 -05002762'__int64'.\n\
2763\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002764Methods:\n\
2765\n\
2766append() -- append a new item to the end of the array\n\
2767buffer_info() -- return information giving the current memory info\n\
2768byteswap() -- byteswap all the items of the array\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002769count() -- return number of occurrences of an object\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00002770extend() -- extend array by appending multiple elements from an iterable\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002771fromfile() -- read items from a file object\n\
2772fromlist() -- append items from the list\n\
Florent Xiclunac45fb252011-10-24 13:14:55 +02002773frombytes() -- append items from the string\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002774index() -- return index of first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002775insert() -- insert a new item into the array at a provided position\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00002776pop() -- remove and return item (default last)\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002777remove() -- remove first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002778reverse() -- reverse the order of the items in the array\n\
2779tofile() -- write all items to a file object\n\
2780tolist() -- return the array converted to an ordinary list\n\
Florent Xiclunac45fb252011-10-24 13:14:55 +02002781tobytes() -- return the array converted to a string\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002782\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002783Attributes:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002784\n\
2785typecode -- the typecode character used to create the array\n\
2786itemsize -- the length in bytes of one array item\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002787");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002788
Raymond Hettinger625812f2003-01-07 01:58:52 +00002789static PyObject *array_iter(arrayobject *ao);
2790
Tim Peters0c322792002-07-17 16:49:03 +00002791static PyTypeObject Arraytype = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 PyVarObject_HEAD_INIT(NULL, 0)
2793 "array.array",
2794 sizeof(arrayobject),
2795 0,
2796 (destructor)array_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002797 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 0, /* tp_getattr */
2799 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002800 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 (reprfunc)array_repr, /* tp_repr */
2802 0, /* tp_as_number*/
2803 &array_as_sequence, /* tp_as_sequence*/
2804 &array_as_mapping, /* tp_as_mapping*/
2805 0, /* tp_hash */
2806 0, /* tp_call */
2807 0, /* tp_str */
2808 PyObject_GenericGetAttr, /* tp_getattro */
2809 0, /* tp_setattro */
2810 &array_as_buffer, /* tp_as_buffer*/
2811 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2812 arraytype_doc, /* tp_doc */
2813 0, /* tp_traverse */
2814 0, /* tp_clear */
2815 array_richcompare, /* tp_richcompare */
2816 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
2817 (getiterfunc)array_iter, /* tp_iter */
2818 0, /* tp_iternext */
2819 array_methods, /* tp_methods */
2820 0, /* tp_members */
2821 array_getsets, /* tp_getset */
2822 0, /* tp_base */
2823 0, /* tp_dict */
2824 0, /* tp_descr_get */
2825 0, /* tp_descr_set */
2826 0, /* tp_dictoffset */
2827 0, /* tp_init */
2828 PyType_GenericAlloc, /* tp_alloc */
2829 array_new, /* tp_new */
2830 PyObject_Del, /* tp_free */
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002831};
2832
Raymond Hettinger625812f2003-01-07 01:58:52 +00002833
2834/*********************** Array Iterator **************************/
2835
Brett Cannon1eb32c22014-10-10 16:26:45 -04002836/*[clinic input]
2837class array.arrayiterator "arrayiterobject *" "&PyArrayIter_Type"
2838[clinic start generated code]*/
2839/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5aefd2d74d8c8e30]*/
Raymond Hettinger625812f2003-01-07 01:58:52 +00002840
2841static PyObject *
2842array_iter(arrayobject *ao)
2843{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 arrayiterobject *it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 if (!array_Check(ao)) {
2847 PyErr_BadInternalCall();
2848 return NULL;
2849 }
Raymond Hettinger625812f2003-01-07 01:58:52 +00002850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002851 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
2852 if (it == NULL)
2853 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 Py_INCREF(ao);
2856 it->ao = ao;
2857 it->index = 0;
2858 it->getitem = ao->ob_descr->getitem;
2859 PyObject_GC_Track(it);
2860 return (PyObject *)it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002861}
2862
2863static PyObject *
Raymond Hettinger625812f2003-01-07 01:58:52 +00002864arrayiter_next(arrayiterobject *it)
2865{
Serhiy Storchakaab0d1982016-03-30 21:11:16 +03002866 arrayobject *ao;
2867
2868 assert(it != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 assert(PyArrayIter_Check(it));
Serhiy Storchakaab0d1982016-03-30 21:11:16 +03002870 ao = it->ao;
2871 if (ao == NULL) {
2872 return NULL;
2873 }
2874 assert(array_Check(ao));
2875 if (it->index < Py_SIZE(ao)) {
2876 return (*it->getitem)(ao, it->index++);
2877 }
2878 it->ao = NULL;
2879 Py_DECREF(ao);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002881}
2882
2883static void
2884arrayiter_dealloc(arrayiterobject *it)
2885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 PyObject_GC_UnTrack(it);
2887 Py_XDECREF(it->ao);
2888 PyObject_GC_Del(it);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002889}
2890
2891static int
2892arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2893{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002894 Py_VISIT(it->ao);
2895 return 0;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002896}
2897
Brett Cannon1eb32c22014-10-10 16:26:45 -04002898/*[clinic input]
2899array.arrayiterator.__reduce__
2900
2901Return state information for pickling.
2902[clinic start generated code]*/
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002903
2904static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04002905array_arrayiterator___reduce___impl(arrayiterobject *self)
2906/*[clinic end generated code: output=7898a52e8e66e016 input=a062ea1e9951417a]*/
2907{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002908 _Py_IDENTIFIER(iter);
2909 PyObject *func = _PyEval_GetBuiltinId(&PyId_iter);
Serhiy Storchakaab0d1982016-03-30 21:11:16 +03002910 if (self->ao == NULL) {
2911 return Py_BuildValue("N(())", func);
2912 }
2913 return Py_BuildValue("N(O)n", func, self->ao, self->index);
Brett Cannon1eb32c22014-10-10 16:26:45 -04002914}
2915
2916/*[clinic input]
2917array.arrayiterator.__setstate__
2918
2919 state: object
2920 /
2921
2922Set state information for unpickling.
2923[clinic start generated code]*/
2924
2925static PyObject *
2926array_arrayiterator___setstate__(arrayiterobject *self, PyObject *state)
2927/*[clinic end generated code: output=397da9904e443cbe input=f47d5ceda19e787b]*/
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002928{
2929 Py_ssize_t index = PyLong_AsSsize_t(state);
2930 if (index == -1 && PyErr_Occurred())
2931 return NULL;
2932 if (index < 0)
2933 index = 0;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002934 else if (index > Py_SIZE(self->ao))
2935 index = Py_SIZE(self->ao); /* iterator exhausted */
2936 self->index = index;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002937 Py_RETURN_NONE;
2938}
2939
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002940static PyMethodDef arrayiter_methods[] = {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002941 ARRAY_ARRAYITERATOR___REDUCE___METHODDEF
2942 ARRAY_ARRAYITERATOR___SETSTATE___METHODDEF
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002943 {NULL, NULL} /* sentinel */
2944};
2945
Raymond Hettinger625812f2003-01-07 01:58:52 +00002946static PyTypeObject PyArrayIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002947 PyVarObject_HEAD_INIT(NULL, 0)
2948 "arrayiterator", /* tp_name */
2949 sizeof(arrayiterobject), /* tp_basicsize */
2950 0, /* tp_itemsize */
2951 /* methods */
2952 (destructor)arrayiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002953 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002954 0, /* tp_getattr */
2955 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002956 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002957 0, /* tp_repr */
2958 0, /* tp_as_number */
2959 0, /* tp_as_sequence */
2960 0, /* tp_as_mapping */
2961 0, /* tp_hash */
2962 0, /* tp_call */
2963 0, /* tp_str */
2964 PyObject_GenericGetAttr, /* tp_getattro */
2965 0, /* tp_setattro */
2966 0, /* tp_as_buffer */
2967 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2968 0, /* tp_doc */
2969 (traverseproc)arrayiter_traverse, /* tp_traverse */
2970 0, /* tp_clear */
2971 0, /* tp_richcompare */
2972 0, /* tp_weaklistoffset */
2973 PyObject_SelfIter, /* tp_iter */
2974 (iternextfunc)arrayiter_next, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002975 arrayiter_methods, /* tp_methods */
Raymond Hettinger625812f2003-01-07 01:58:52 +00002976};
2977
2978
2979/*********************** Install Module **************************/
2980
Martin v. Löwis99866332002-03-01 10:27:01 +00002981/* No functions in array module. */
2982static PyMethodDef a_methods[] = {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002983 ARRAY__ARRAY_RECONSTRUCTOR_METHODDEF
Martin v. Löwis99866332002-03-01 10:27:01 +00002984 {NULL, NULL, 0, NULL} /* Sentinel */
2985};
2986
Nick Coghland5cacbb2015-05-23 22:24:10 +10002987static int
2988array_modexec(PyObject *m)
Guido van Rossum778983b1993-02-19 15:55:02 +00002989{
Georg Brandl4cb0de22011-09-28 21:49:49 +02002990 char buffer[Py_ARRAY_LENGTH(descriptors)], *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002991 PyObject *typecodes;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002992 const struct arraydescr *descr;
Fred Drake0d40ba42000-02-04 20:33:49 +00002993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 if (PyType_Ready(&Arraytype) < 0)
Nick Coghland5cacbb2015-05-23 22:24:10 +10002995 return -1;
Victor Stinnerd2ec81a2020-02-07 09:17:07 +01002996 Py_SET_TYPE(&PyArrayIter_Type, &PyType_Type);
Fred Drakef4e34842002-04-01 03:45:06 +00002997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 Py_INCREF((PyObject *)&Arraytype);
Marco Paolinib44ffc82019-11-15 08:42:51 +00002999 if (PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype) < 0) {
3000 Py_DECREF((PyObject *)&Arraytype);
3001 return -1;
3002 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 Py_INCREF((PyObject *)&Arraytype);
Marco Paolinib44ffc82019-11-15 08:42:51 +00003004 if (PyModule_AddObject(m, "array", (PyObject *)&Arraytype) < 0) {
3005 Py_DECREF((PyObject *)&Arraytype);
3006 return -1;
3007 }
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00003008
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003009 p = buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 for (descr = descriptors; descr->typecode != '\0'; descr++) {
3011 *p++ = (char)descr->typecode;
3012 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003013 typecodes = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
Marco Paolinib44ffc82019-11-15 08:42:51 +00003014 if (PyModule_AddObject(m, "typecodes", typecodes) < 0) {
3015 Py_XDECREF(typecodes);
3016 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 }
Marco Paolinib44ffc82019-11-15 08:42:51 +00003018
Nick Coghland5cacbb2015-05-23 22:24:10 +10003019 return 0;
3020}
3021
3022static PyModuleDef_Slot arrayslots[] = {
3023 {Py_mod_exec, array_modexec},
3024 {0, NULL}
3025};
3026
3027
3028static struct PyModuleDef arraymodule = {
3029 PyModuleDef_HEAD_INIT,
3030 "array",
3031 module_doc,
3032 0,
3033 a_methods,
3034 arrayslots,
3035 NULL,
3036 NULL,
3037 NULL
3038};
3039
3040
3041PyMODINIT_FUNC
3042PyInit_array(void)
3043{
3044 return PyModuleDef_Init(&arraymodule);
Guido van Rossum778983b1993-02-19 15:55:02 +00003045}