blob: 8f12c61646335608d8bdc8c53209b0c38b119c83 [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
340static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000341II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 unsigned long x;
orenmn964281a2017-03-09 11:35:28 +0200344 int do_decref = 0; /* if nb_int was called */
345
346 if (!PyLong_Check(v)) {
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300347 v = _PyNumber_Index(v);
orenmn964281a2017-03-09 11:35:28 +0200348 if (NULL == v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 return -1;
350 }
orenmn964281a2017-03-09 11:35:28 +0200351 do_decref = 1;
352 }
353 x = PyLong_AsUnsignedLong(v);
354 if (x == (unsigned long)-1 && PyErr_Occurred()) {
355 if (do_decref) {
356 Py_DECREF(v);
357 }
358 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 }
360 if (x > UINT_MAX) {
361 PyErr_SetString(PyExc_OverflowError,
orenmn964281a2017-03-09 11:35:28 +0200362 "unsigned int is greater than maximum");
363 if (do_decref) {
364 Py_DECREF(v);
365 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 return -1;
367 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 if (i >= 0)
369 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
orenmn964281a2017-03-09 11:35:28 +0200370
371 if (do_decref) {
372 Py_DECREF(v);
373 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 return 0;
Guido van Rossum549ab711997-01-03 19:09:47 +0000375}
376
377static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000378l_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 return PyLong_FromLong(((long *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000381}
382
383static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000384l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 long x;
387 if (!PyArg_Parse(v, "l;array item must be integer", &x))
388 return -1;
389 if (i >= 0)
390 ((long *)ap->ob_item)[i] = x;
391 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000392}
393
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000394static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000395LL_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000398}
399
400static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000401LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 unsigned long x;
orenmn964281a2017-03-09 11:35:28 +0200404 int do_decref = 0; /* if nb_int was called */
405
406 if (!PyLong_Check(v)) {
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300407 v = _PyNumber_Index(v);
orenmn964281a2017-03-09 11:35:28 +0200408 if (NULL == v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 return -1;
410 }
orenmn964281a2017-03-09 11:35:28 +0200411 do_decref = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 }
orenmn964281a2017-03-09 11:35:28 +0200413 x = PyLong_AsUnsignedLong(v);
414 if (x == (unsigned long)-1 && PyErr_Occurred()) {
415 if (do_decref) {
416 Py_DECREF(v);
417 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 return -1;
419 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 if (i >= 0)
421 ((unsigned long *)ap->ob_item)[i] = x;
orenmn964281a2017-03-09 11:35:28 +0200422
423 if (do_decref) {
424 Py_DECREF(v);
425 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 return 0;
Guido van Rossum549ab711997-01-03 19:09:47 +0000427}
428
Meador Inge1c9f0c92011-09-20 19:55:51 -0500429static PyObject *
430q_getitem(arrayobject *ap, Py_ssize_t i)
431{
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700432 return PyLong_FromLongLong(((long long *)ap->ob_item)[i]);
Meador Inge1c9f0c92011-09-20 19:55:51 -0500433}
434
435static int
436q_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
437{
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700438 long long x;
Meador Inge1c9f0c92011-09-20 19:55:51 -0500439 if (!PyArg_Parse(v, "L;array item must be integer", &x))
440 return -1;
441 if (i >= 0)
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700442 ((long long *)ap->ob_item)[i] = x;
Meador Inge1c9f0c92011-09-20 19:55:51 -0500443 return 0;
444}
445
446static PyObject *
447QQ_getitem(arrayobject *ap, Py_ssize_t i)
448{
449 return PyLong_FromUnsignedLongLong(
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700450 ((unsigned long long *)ap->ob_item)[i]);
Meador Inge1c9f0c92011-09-20 19:55:51 -0500451}
452
453static int
454QQ_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
455{
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700456 unsigned long long x;
orenmn964281a2017-03-09 11:35:28 +0200457 int do_decref = 0; /* if nb_int was called */
458
459 if (!PyLong_Check(v)) {
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300460 v = _PyNumber_Index(v);
orenmn964281a2017-03-09 11:35:28 +0200461 if (NULL == v) {
Meador Inge1c9f0c92011-09-20 19:55:51 -0500462 return -1;
463 }
orenmn964281a2017-03-09 11:35:28 +0200464 do_decref = 1;
Meador Inge1c9f0c92011-09-20 19:55:51 -0500465 }
orenmn964281a2017-03-09 11:35:28 +0200466 x = PyLong_AsUnsignedLongLong(v);
467 if (x == (unsigned long long)-1 && PyErr_Occurred()) {
468 if (do_decref) {
469 Py_DECREF(v);
470 }
471 return -1;
472 }
Meador Inge1c9f0c92011-09-20 19:55:51 -0500473 if (i >= 0)
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700474 ((unsigned long long *)ap->ob_item)[i] = x;
orenmn964281a2017-03-09 11:35:28 +0200475
476 if (do_decref) {
477 Py_DECREF(v);
478 }
Meador Inge1c9f0c92011-09-20 19:55:51 -0500479 return 0;
480}
Meador Inge1c9f0c92011-09-20 19:55:51 -0500481
Guido van Rossum549ab711997-01-03 19:09:47 +0000482static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000483f_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000484{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000486}
487
488static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000489f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000490{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 float x;
492 if (!PyArg_Parse(v, "f;array item must be float", &x))
493 return -1;
494 if (i >= 0)
495 ((float *)ap->ob_item)[i] = x;
496 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000497}
498
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000499static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000500d_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000503}
504
505static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000506d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 double x;
509 if (!PyArg_Parse(v, "d;array item must be float", &x))
510 return -1;
511 if (i >= 0)
512 ((double *)ap->ob_item)[i] = x;
513 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000514}
515
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000516#define DEFINE_COMPAREITEMS(code, type) \
517 static int \
518 code##_compareitems(const void *lhs, const void *rhs, Py_ssize_t length) \
519 { \
520 const type *a = lhs, *b = rhs; \
521 for (Py_ssize_t i = 0; i < length; ++i) \
522 if (a[i] != b[i]) \
523 return a[i] < b[i] ? -1 : 1; \
524 return 0; \
525 }
526
527DEFINE_COMPAREITEMS(b, signed char)
528DEFINE_COMPAREITEMS(BB, unsigned char)
Inada Naokid5d9a712020-05-11 15:37:25 +0900529DEFINE_COMPAREITEMS(u, wchar_t)
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000530DEFINE_COMPAREITEMS(h, short)
531DEFINE_COMPAREITEMS(HH, unsigned short)
532DEFINE_COMPAREITEMS(i, int)
533DEFINE_COMPAREITEMS(II, unsigned int)
534DEFINE_COMPAREITEMS(l, long)
535DEFINE_COMPAREITEMS(LL, unsigned long)
536DEFINE_COMPAREITEMS(q, long long)
537DEFINE_COMPAREITEMS(QQ, unsigned long long)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000538
Alexandre Vassalottiad077152009-07-15 17:49:23 +0000539/* Description of types.
540 *
541 * Don't forget to update typecode_to_mformat_code() if you add a new
542 * typecode.
543 */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200544static const struct arraydescr descriptors[] = {
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000545 {'b', 1, b_getitem, b_setitem, b_compareitems, "b", 1, 1},
546 {'B', 1, BB_getitem, BB_setitem, BB_compareitems, "B", 1, 0},
Inada Naokid5d9a712020-05-11 15:37:25 +0900547 {'u', sizeof(wchar_t), u_getitem, u_setitem, u_compareitems, "u", 0, 0},
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000548 {'h', sizeof(short), h_getitem, h_setitem, h_compareitems, "h", 1, 1},
549 {'H', sizeof(short), HH_getitem, HH_setitem, HH_compareitems, "H", 1, 0},
550 {'i', sizeof(int), i_getitem, i_setitem, i_compareitems, "i", 1, 1},
551 {'I', sizeof(int), II_getitem, II_setitem, II_compareitems, "I", 1, 0},
552 {'l', sizeof(long), l_getitem, l_setitem, l_compareitems, "l", 1, 1},
553 {'L', sizeof(long), LL_getitem, LL_setitem, LL_compareitems, "L", 1, 0},
554 {'q', sizeof(long long), q_getitem, q_setitem, q_compareitems, "q", 1, 1},
555 {'Q', sizeof(long long), QQ_getitem, QQ_setitem, QQ_compareitems, "Q", 1, 0},
556 {'f', sizeof(float), f_getitem, f_setitem, NULL, "f", 0, 0},
557 {'d', sizeof(double), d_getitem, d_setitem, NULL, "d", 0, 0},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 {'\0', 0, 0, 0, 0, 0, 0} /* Sentinel */
Guido van Rossum778983b1993-02-19 15:55:02 +0000559};
Tim Petersbb307342000-09-10 05:22:54 +0000560
561/****************************************************************************
562Implementations of array object methods.
563****************************************************************************/
Brett Cannon1eb32c22014-10-10 16:26:45 -0400564/*[clinic input]
565class array.array "arrayobject *" "&Arraytype"
566[clinic start generated code]*/
567/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ad43d37e942a8854]*/
Guido van Rossum778983b1993-02-19 15:55:02 +0000568
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000569static PyObject *
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200570newarrayobject(PyTypeObject *type, Py_ssize_t size, const struct arraydescr *descr)
Guido van Rossum778983b1993-02-19 15:55:02 +0000571{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 arrayobject *op;
573 size_t nbytes;
Martin v. Löwis99866332002-03-01 10:27:01 +0000574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 if (size < 0) {
576 PyErr_BadInternalCall();
577 return NULL;
578 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 /* Check for overflow */
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100581 if (size > PY_SSIZE_T_MAX / descr->itemsize) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 return PyErr_NoMemory();
583 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100584 nbytes = size * descr->itemsize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 op = (arrayobject *) type->tp_alloc(type, 0);
586 if (op == NULL) {
587 return NULL;
588 }
589 op->ob_descr = descr;
590 op->allocated = size;
591 op->weakreflist = NULL;
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100592 Py_SET_SIZE(op, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 if (size <= 0) {
594 op->ob_item = NULL;
595 }
596 else {
597 op->ob_item = PyMem_NEW(char, nbytes);
598 if (op->ob_item == NULL) {
599 Py_DECREF(op);
600 return PyErr_NoMemory();
601 }
602 }
603 op->ob_exports = 0;
604 return (PyObject *) op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000605}
606
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000607static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000608getarrayitem(PyObject *op, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000609{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200610 arrayobject *ap;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 assert(array_Check(op));
612 ap = (arrayobject *)op;
613 assert(i>=0 && i<Py_SIZE(ap));
614 return (*ap->ob_descr->getitem)(ap, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000615}
616
617static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000618ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000619{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 char *items;
621 Py_ssize_t n = Py_SIZE(self);
622 if (v == NULL) {
623 PyErr_BadInternalCall();
624 return -1;
625 }
626 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
627 return -1;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 if (array_resize(self, n+1) == -1)
630 return -1;
631 items = self->ob_item;
632 if (where < 0) {
633 where += n;
634 if (where < 0)
635 where = 0;
636 }
637 if (where > n)
638 where = n;
639 /* appends don't need to call memmove() */
640 if (where != n)
641 memmove(items + (where+1)*self->ob_descr->itemsize,
642 items + where*self->ob_descr->itemsize,
643 (n-where)*self->ob_descr->itemsize);
644 return (*self->ob_descr->setitem)(self, where, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000645}
646
Guido van Rossum778983b1993-02-19 15:55:02 +0000647/* Methods */
648
649static void
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000650array_dealloc(arrayobject *op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000651{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 if (op->weakreflist != NULL)
653 PyObject_ClearWeakRefs((PyObject *) op);
654 if (op->ob_item != NULL)
655 PyMem_DEL(op->ob_item);
656 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000657}
658
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000659static PyObject *
660array_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000661{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 arrayobject *va, *wa;
663 PyObject *vi = NULL;
664 PyObject *wi = NULL;
665 Py_ssize_t i, k;
666 PyObject *res;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000667
Brian Curtindfc80e32011-08-10 20:28:54 -0500668 if (!array_Check(v) || !array_Check(w))
669 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 va = (arrayobject *)v;
672 wa = (arrayobject *)w;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 if (Py_SIZE(va) != Py_SIZE(wa) && (op == Py_EQ || op == Py_NE)) {
675 /* Shortcut: if the lengths differ, the arrays differ */
676 if (op == Py_EQ)
677 res = Py_False;
678 else
679 res = Py_True;
680 Py_INCREF(res);
681 return res;
682 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000683
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000684 if (va->ob_descr == wa->ob_descr && va->ob_descr->compareitems != NULL) {
685 /* Fast path:
686 arrays with same types can have their buffers compared directly */
687 Py_ssize_t common_length = Py_MIN(Py_SIZE(va), Py_SIZE(wa));
688 int result = va->ob_descr->compareitems(va->ob_item, wa->ob_item,
689 common_length);
690 if (result == 0)
691 goto compare_sizes;
692
693 int cmp;
694 switch (op) {
695 case Py_LT: cmp = result < 0; break;
696 case Py_LE: cmp = result <= 0; break;
697 case Py_EQ: cmp = result == 0; break;
698 case Py_NE: cmp = result != 0; break;
699 case Py_GT: cmp = result > 0; break;
700 case Py_GE: cmp = result >= 0; break;
701 default: return NULL; /* cannot happen */
702 }
703 PyObject *res = cmp ? Py_True : Py_False;
704 Py_INCREF(res);
705 return res;
706 }
707
708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 /* Search for the first index where items are different */
710 k = 1;
711 for (i = 0; i < Py_SIZE(va) && i < Py_SIZE(wa); i++) {
712 vi = getarrayitem(v, i);
713 wi = getarrayitem(w, i);
714 if (vi == NULL || wi == NULL) {
715 Py_XDECREF(vi);
716 Py_XDECREF(wi);
717 return NULL;
718 }
719 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
720 if (k == 0)
721 break; /* Keeping vi and wi alive! */
722 Py_DECREF(vi);
723 Py_DECREF(wi);
724 if (k < 0)
725 return NULL;
726 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 if (k) {
729 /* No more items to compare -- compare sizes */
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000730 compare_sizes: ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 Py_ssize_t vs = Py_SIZE(va);
732 Py_ssize_t ws = Py_SIZE(wa);
733 int cmp;
734 switch (op) {
735 case Py_LT: cmp = vs < ws; break;
736 case Py_LE: cmp = vs <= ws; break;
Adrian Wielgosik7c17e232017-08-17 12:46:06 +0000737 /* If the lengths were not equal,
738 the earlier fast-path check would have caught that. */
739 case Py_EQ: assert(vs == ws); cmp = 1; break;
740 case Py_NE: assert(vs == ws); cmp = 0; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 case Py_GT: cmp = vs > ws; break;
742 case Py_GE: cmp = vs >= ws; break;
743 default: return NULL; /* cannot happen */
744 }
745 if (cmp)
746 res = Py_True;
747 else
748 res = Py_False;
749 Py_INCREF(res);
750 return res;
751 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 /* We have an item that differs. First, shortcuts for EQ/NE */
754 if (op == Py_EQ) {
755 Py_INCREF(Py_False);
756 res = Py_False;
757 }
758 else if (op == Py_NE) {
759 Py_INCREF(Py_True);
760 res = Py_True;
761 }
762 else {
763 /* Compare the final item again using the proper operator */
764 res = PyObject_RichCompare(vi, wi, op);
765 }
766 Py_DECREF(vi);
767 Py_DECREF(wi);
768 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +0000769}
770
Martin v. Löwis18e16552006-02-15 17:27:45 +0000771static Py_ssize_t
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000772array_length(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +0000773{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 return Py_SIZE(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000775}
776
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000777static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000778array_item(arrayobject *a, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000779{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 if (i < 0 || i >= Py_SIZE(a)) {
781 PyErr_SetString(PyExc_IndexError, "array index out of range");
782 return NULL;
783 }
784 return getarrayitem((PyObject *)a, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000785}
786
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000787static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000788array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000789{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 arrayobject *np;
791 if (ilow < 0)
792 ilow = 0;
793 else if (ilow > Py_SIZE(a))
794 ilow = Py_SIZE(a);
795 if (ihigh < 0)
796 ihigh = 0;
797 if (ihigh < ilow)
798 ihigh = ilow;
799 else if (ihigh > Py_SIZE(a))
800 ihigh = Py_SIZE(a);
801 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
802 if (np == NULL)
803 return NULL;
Martin Panterbe8da9c2016-09-07 11:04:41 +0000804 if (ihigh > ilow) {
805 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
806 (ihigh-ilow) * a->ob_descr->itemsize);
807 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000809}
810
Brett Cannon1eb32c22014-10-10 16:26:45 -0400811
812/*[clinic input]
813array.array.__copy__
814
815Return a copy of the array.
816[clinic start generated code]*/
817
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000818static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -0400819array_array___copy___impl(arrayobject *self)
820/*[clinic end generated code: output=dec7c3f925d9619e input=ad1ee5b086965f09]*/
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000821{
Brett Cannon1eb32c22014-10-10 16:26:45 -0400822 return array_slice(self, 0, Py_SIZE(self));
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000823}
824
Brett Cannon1eb32c22014-10-10 16:26:45 -0400825/*[clinic input]
826array.array.__deepcopy__
827
828 unused: object
829 /
830
831Return a copy of the array.
832[clinic start generated code]*/
833
834static PyObject *
835array_array___deepcopy__(arrayobject *self, PyObject *unused)
836/*[clinic end generated code: output=1ec748d8e14a9faa input=2405ecb4933748c4]*/
837{
838 return array_array___copy___impl(self);
839}
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000840
841static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000842array_concat(arrayobject *a, PyObject *bb)
Guido van Rossum778983b1993-02-19 15:55:02 +0000843{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 Py_ssize_t size;
845 arrayobject *np;
846 if (!array_Check(bb)) {
847 PyErr_Format(PyExc_TypeError,
848 "can only append array (not \"%.200s\") to array",
849 Py_TYPE(bb)->tp_name);
850 return NULL;
851 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000852#define b ((arrayobject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 if (a->ob_descr != b->ob_descr) {
854 PyErr_BadArgument();
855 return NULL;
856 }
857 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) {
858 return PyErr_NoMemory();
859 }
860 size = Py_SIZE(a) + Py_SIZE(b);
861 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
862 if (np == NULL) {
863 return NULL;
864 }
Martin Panterbe8da9c2016-09-07 11:04:41 +0000865 if (Py_SIZE(a) > 0) {
866 memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize);
867 }
868 if (Py_SIZE(b) > 0) {
869 memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize,
870 b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
871 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000873#undef b
874}
875
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000876static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000877array_repeat(arrayobject *a, Py_ssize_t n)
Guido van Rossum778983b1993-02-19 15:55:02 +0000878{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 Py_ssize_t size;
880 arrayobject *np;
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000881 Py_ssize_t oldbytes, newbytes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 if (n < 0)
883 n = 0;
884 if ((Py_SIZE(a) != 0) && (n > PY_SSIZE_T_MAX / Py_SIZE(a))) {
885 return PyErr_NoMemory();
886 }
887 size = Py_SIZE(a) * n;
888 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
889 if (np == NULL)
890 return NULL;
Martin Panterbe8da9c2016-09-07 11:04:41 +0000891 if (size == 0)
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000892 return (PyObject *)np;
893 oldbytes = Py_SIZE(a) * a->ob_descr->itemsize;
894 newbytes = oldbytes * n;
895 /* this follows the code in unicode_repeat */
896 if (oldbytes == 1) {
897 memset(np->ob_item, a->ob_item[0], newbytes);
898 } else {
899 Py_ssize_t done = oldbytes;
Christian Heimesf051e432016-09-13 20:22:02 +0200900 memcpy(np->ob_item, a->ob_item, oldbytes);
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000901 while (done < newbytes) {
902 Py_ssize_t ncopy = (done <= newbytes-done) ? done : newbytes-done;
Christian Heimesf051e432016-09-13 20:22:02 +0200903 memcpy(np->ob_item+done, np->ob_item, ncopy);
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000904 done += ncopy;
905 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 }
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000907 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000908}
909
910static int
Martin Panter996d72b2016-07-25 02:21:14 +0000911array_del_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 char *item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 Py_ssize_t d; /* Change in size */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 if (ilow < 0)
916 ilow = 0;
917 else if (ilow > Py_SIZE(a))
918 ilow = Py_SIZE(a);
919 if (ihigh < 0)
920 ihigh = 0;
921 if (ihigh < ilow)
922 ihigh = ilow;
923 else if (ihigh > Py_SIZE(a))
924 ihigh = Py_SIZE(a);
925 item = a->ob_item;
Martin Panter996d72b2016-07-25 02:21:14 +0000926 d = ihigh-ilow;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 /* Issue #4509: If the array has exported buffers and the slice
928 assignment would change the size of the array, fail early to make
929 sure we don't modify it. */
930 if (d != 0 && a->ob_exports > 0) {
931 PyErr_SetString(PyExc_BufferError,
932 "cannot resize an array that is exporting buffers");
933 return -1;
934 }
Martin Panter996d72b2016-07-25 02:21:14 +0000935 if (d > 0) { /* Delete d items */
936 memmove(item + (ihigh-d)*a->ob_descr->itemsize,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 item + ihigh*a->ob_descr->itemsize,
938 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
Martin Panter996d72b2016-07-25 02:21:14 +0000939 if (array_resize(a, Py_SIZE(a) - d) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 return -1;
941 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000943}
944
945static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000946array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 if (i < 0 || i >= Py_SIZE(a)) {
949 PyErr_SetString(PyExc_IndexError,
950 "array assignment index out of range");
951 return -1;
952 }
953 if (v == NULL)
Martin Panter996d72b2016-07-25 02:21:14 +0000954 return array_del_slice(a, i, i+1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 return (*a->ob_descr->setitem)(a, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000956}
957
958static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000959setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000960{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 assert(array_Check(a));
962 return array_ass_item((arrayobject *)a, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000963}
964
Martin v. Löwis99866332002-03-01 10:27:01 +0000965static int
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000966array_iter_extend(arrayobject *self, PyObject *bb)
967{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 PyObject *it, *v;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 it = PyObject_GetIter(bb);
971 if (it == NULL)
972 return -1;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 while ((v = PyIter_Next(it)) != NULL) {
Mark Dickinson346f0af2010-08-06 09:36:57 +0000975 if (ins1(self, Py_SIZE(self), v) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 Py_DECREF(v);
977 Py_DECREF(it);
978 return -1;
979 }
980 Py_DECREF(v);
981 }
982 Py_DECREF(it);
983 if (PyErr_Occurred())
984 return -1;
985 return 0;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000986}
987
988static int
Martin v. Löwis99866332002-03-01 10:27:01 +0000989array_do_extend(arrayobject *self, PyObject *bb)
990{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 Py_ssize_t size, oldsize, bbsize;
Martin v. Löwis99866332002-03-01 10:27:01 +0000992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 if (!array_Check(bb))
994 return array_iter_extend(self, bb);
995#define b ((arrayobject *)bb)
996 if (self->ob_descr != b->ob_descr) {
997 PyErr_SetString(PyExc_TypeError,
998 "can only extend with array of same kind");
999 return -1;
1000 }
1001 if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) ||
1002 ((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
1003 PyErr_NoMemory();
1004 return -1;
1005 }
1006 oldsize = Py_SIZE(self);
1007 /* Get the size of bb before resizing the array since bb could be self. */
1008 bbsize = Py_SIZE(bb);
1009 size = oldsize + Py_SIZE(b);
1010 if (array_resize(self, size) == -1)
1011 return -1;
Martin Panterbe8da9c2016-09-07 11:04:41 +00001012 if (bbsize > 0) {
1013 memcpy(self->ob_item + oldsize * self->ob_descr->itemsize,
1014 b->ob_item, bbsize * b->ob_descr->itemsize);
1015 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016
1017 return 0;
Martin v. Löwis99866332002-03-01 10:27:01 +00001018#undef b
1019}
1020
1021static PyObject *
1022array_inplace_concat(arrayobject *self, PyObject *bb)
1023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 if (!array_Check(bb)) {
1025 PyErr_Format(PyExc_TypeError,
1026 "can only extend array with array (not \"%.200s\")",
1027 Py_TYPE(bb)->tp_name);
1028 return NULL;
1029 }
1030 if (array_do_extend(self, bb) == -1)
1031 return NULL;
1032 Py_INCREF(self);
1033 return (PyObject *)self;
Martin v. Löwis99866332002-03-01 10:27:01 +00001034}
1035
1036static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001037array_inplace_repeat(arrayobject *self, Py_ssize_t n)
Martin v. Löwis99866332002-03-01 10:27:01 +00001038{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 char *items, *p;
1040 Py_ssize_t size, i;
Martin v. Löwis99866332002-03-01 10:27:01 +00001041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 if (Py_SIZE(self) > 0) {
1043 if (n < 0)
1044 n = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 if ((self->ob_descr->itemsize != 0) &&
1046 (Py_SIZE(self) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
1047 return PyErr_NoMemory();
1048 }
1049 size = Py_SIZE(self) * self->ob_descr->itemsize;
1050 if (n > 0 && size > PY_SSIZE_T_MAX / n) {
1051 return PyErr_NoMemory();
1052 }
1053 if (array_resize(self, n * Py_SIZE(self)) == -1)
1054 return NULL;
1055 items = p = self->ob_item;
1056 for (i = 1; i < n; i++) {
1057 p += size;
1058 memcpy(p, items, size);
1059 }
1060 }
1061 Py_INCREF(self);
1062 return (PyObject *)self;
Martin v. Löwis99866332002-03-01 10:27:01 +00001063}
1064
1065
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001066static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001067ins(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +00001068{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 if (ins1(self, where, v) != 0)
1070 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001071 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001072}
1073
Brett Cannon1eb32c22014-10-10 16:26:45 -04001074/*[clinic input]
1075array.array.count
1076
1077 v: object
1078 /
1079
1080Return number of occurrences of v in the array.
1081[clinic start generated code]*/
1082
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001083static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001084array_array_count(arrayobject *self, PyObject *v)
1085/*[clinic end generated code: output=3dd3624bf7135a3a input=d9bce9d65e39d1f5]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001086{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 Py_ssize_t count = 0;
1088 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 for (i = 0; i < Py_SIZE(self); i++) {
Victor Stinner0b142e22013-07-17 23:01:30 +02001091 PyObject *selfi;
1092 int cmp;
1093
1094 selfi = getarrayitem((PyObject *)self, i);
1095 if (selfi == NULL)
1096 return NULL;
1097 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 Py_DECREF(selfi);
1099 if (cmp > 0)
1100 count++;
1101 else if (cmp < 0)
1102 return NULL;
1103 }
1104 return PyLong_FromSsize_t(count);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001105}
1106
Brett Cannon1eb32c22014-10-10 16:26:45 -04001107
1108/*[clinic input]
1109array.array.index
1110
1111 v: object
1112 /
1113
1114Return index of first occurrence of v in the array.
1115[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001116
1117static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001118array_array_index(arrayobject *self, PyObject *v)
1119/*[clinic end generated code: output=d48498d325602167 input=cf619898c6649d08]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 for (i = 0; i < Py_SIZE(self); i++) {
Victor Stinner0b142e22013-07-17 23:01:30 +02001124 PyObject *selfi;
1125 int cmp;
1126
1127 selfi = getarrayitem((PyObject *)self, i);
1128 if (selfi == NULL)
1129 return NULL;
1130 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 Py_DECREF(selfi);
1132 if (cmp > 0) {
1133 return PyLong_FromLong((long)i);
1134 }
1135 else if (cmp < 0)
1136 return NULL;
1137 }
Jim Fasarakis-Hilliarda4095ef2017-05-29 20:43:39 +03001138 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in array");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 return NULL;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001140}
1141
Raymond Hettinger625812f2003-01-07 01:58:52 +00001142static int
1143array_contains(arrayobject *self, PyObject *v)
1144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 Py_ssize_t i;
1146 int cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +00001147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) {
1149 PyObject *selfi = getarrayitem((PyObject *)self, i);
Victor Stinner0b142e22013-07-17 23:01:30 +02001150 if (selfi == NULL)
Victor Stinner4755bea2013-07-18 01:12:35 +02001151 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
1153 Py_DECREF(selfi);
1154 }
1155 return cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +00001156}
1157
Brett Cannon1eb32c22014-10-10 16:26:45 -04001158/*[clinic input]
1159array.array.remove
1160
1161 v: object
1162 /
1163
1164Remove the first occurrence of v in the array.
1165[clinic start generated code]*/
1166
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001167static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001168array_array_remove(arrayobject *self, PyObject *v)
1169/*[clinic end generated code: output=bef06be9fdf9dceb input=0b1e5aed25590027]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001170{
sthaa3ecb82019-03-20 20:49:39 +01001171 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 for (i = 0; i < Py_SIZE(self); i++) {
Victor Stinner0b142e22013-07-17 23:01:30 +02001174 PyObject *selfi;
1175 int cmp;
1176
1177 selfi = getarrayitem((PyObject *)self,i);
1178 if (selfi == NULL)
1179 return NULL;
1180 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 Py_DECREF(selfi);
1182 if (cmp > 0) {
Martin Panter996d72b2016-07-25 02:21:14 +00001183 if (array_del_slice(self, i, i+1) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001185 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 }
1187 else if (cmp < 0)
1188 return NULL;
1189 }
Jim Fasarakis-Hilliarda4095ef2017-05-29 20:43:39 +03001190 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in array");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 return NULL;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001192}
1193
Brett Cannon1eb32c22014-10-10 16:26:45 -04001194/*[clinic input]
1195array.array.pop
1196
1197 i: Py_ssize_t = -1
1198 /
1199
1200Return the i-th element and delete it from the array.
1201
1202i defaults to -1.
1203[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001204
1205static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001206array_array_pop_impl(arrayobject *self, Py_ssize_t i)
1207/*[clinic end generated code: output=bc1f0c54fe5308e4 input=8e5feb4c1a11cd44]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 PyObject *v;
Brett Cannon1eb32c22014-10-10 16:26:45 -04001210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 if (Py_SIZE(self) == 0) {
1212 /* Special-case most common failure cause */
1213 PyErr_SetString(PyExc_IndexError, "pop from empty array");
1214 return NULL;
1215 }
1216 if (i < 0)
1217 i += Py_SIZE(self);
1218 if (i < 0 || i >= Py_SIZE(self)) {
1219 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1220 return NULL;
1221 }
Victor Stinner0b142e22013-07-17 23:01:30 +02001222 v = getarrayitem((PyObject *)self, i);
1223 if (v == NULL)
1224 return NULL;
Martin Panter996d72b2016-07-25 02:21:14 +00001225 if (array_del_slice(self, i, i+1) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 Py_DECREF(v);
1227 return NULL;
1228 }
1229 return v;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001230}
1231
Brett Cannon1eb32c22014-10-10 16:26:45 -04001232/*[clinic input]
1233array.array.extend
1234
1235 bb: object
1236 /
1237
1238Append items to the end of the array.
1239[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001240
1241static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001242array_array_extend(arrayobject *self, PyObject *bb)
1243/*[clinic end generated code: output=bbddbc8e8bef871d input=43be86aba5c31e44]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 if (array_do_extend(self, bb) == -1)
1246 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001247 Py_RETURN_NONE;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001248}
1249
Brett Cannon1eb32c22014-10-10 16:26:45 -04001250/*[clinic input]
1251array.array.insert
1252
1253 i: Py_ssize_t
1254 v: object
1255 /
1256
1257Insert a new item v into the array before position i.
1258[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001259
1260static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001261array_array_insert_impl(arrayobject *self, Py_ssize_t i, PyObject *v)
1262/*[clinic end generated code: output=5a3648e278348564 input=5577d1b4383e9313]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 return ins(self, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001265}
1266
Brett Cannon1eb32c22014-10-10 16:26:45 -04001267/*[clinic input]
1268array.array.buffer_info
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001269
Brett Cannon1eb32c22014-10-10 16:26:45 -04001270Return a tuple (address, length) giving the current memory address and the length in items of the buffer used to hold array's contents.
1271
1272The length should be multiplied by the itemsize attribute to calculate
1273the buffer length in bytes.
1274[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001275
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001276static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001277array_array_buffer_info_impl(arrayobject *self)
1278/*[clinic end generated code: output=9b2a4ec3ae7e98e7 input=a58bae5c6e1ac6a6]*/
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001279{
Victor Stinner541067a2013-11-14 01:27:12 +01001280 PyObject *retval = NULL, *v;
1281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 retval = PyTuple_New(2);
1283 if (!retval)
1284 return NULL;
Fred Drake541dc3b2000-06-28 17:49:30 +00001285
Victor Stinner541067a2013-11-14 01:27:12 +01001286 v = PyLong_FromVoidPtr(self->ob_item);
1287 if (v == NULL) {
1288 Py_DECREF(retval);
1289 return NULL;
1290 }
1291 PyTuple_SET_ITEM(retval, 0, v);
1292
Serhiy Storchaka9e941d62016-06-23 23:55:34 +03001293 v = PyLong_FromSsize_t(Py_SIZE(self));
Victor Stinner541067a2013-11-14 01:27:12 +01001294 if (v == NULL) {
1295 Py_DECREF(retval);
1296 return NULL;
1297 }
1298 PyTuple_SET_ITEM(retval, 1, v);
Fred Drake541dc3b2000-06-28 17:49:30 +00001299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 return retval;
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001301}
1302
Brett Cannon1eb32c22014-10-10 16:26:45 -04001303/*[clinic input]
1304array.array.append
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001305
Brett Cannon1eb32c22014-10-10 16:26:45 -04001306 v: object
1307 /
1308
1309Append new value v to the end of the array.
1310[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001311
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001312static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001313array_array_append(arrayobject *self, PyObject *v)
1314/*[clinic end generated code: output=745a0669bf8db0e2 input=0b98d9d78e78f0fa]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001315{
Mark Dickinson346f0af2010-08-06 09:36:57 +00001316 return ins(self, Py_SIZE(self), v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001317}
1318
Brett Cannon1eb32c22014-10-10 16:26:45 -04001319/*[clinic input]
1320array.array.byteswap
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001321
Brett Cannon1eb32c22014-10-10 16:26:45 -04001322Byteswap all items of the array.
1323
1324If the items in the array are not 1, 2, 4, or 8 bytes in size, RuntimeError is
1325raised.
1326[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001327
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001328static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001329array_array_byteswap_impl(arrayobject *self)
1330/*[clinic end generated code: output=5f8236cbdf0d90b5 input=6a85591b950a0186]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 char *p;
1333 Py_ssize_t i;
Fred Drakebf272981999-12-03 17:15:30 +00001334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 switch (self->ob_descr->itemsize) {
1336 case 1:
1337 break;
1338 case 2:
1339 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) {
1340 char p0 = p[0];
1341 p[0] = p[1];
1342 p[1] = p0;
1343 }
1344 break;
1345 case 4:
1346 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 4) {
1347 char p0 = p[0];
1348 char p1 = p[1];
1349 p[0] = p[3];
1350 p[1] = p[2];
1351 p[2] = p1;
1352 p[3] = p0;
1353 }
1354 break;
1355 case 8:
1356 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) {
1357 char p0 = p[0];
1358 char p1 = p[1];
1359 char p2 = p[2];
1360 char p3 = p[3];
1361 p[0] = p[7];
1362 p[1] = p[6];
1363 p[2] = p[5];
1364 p[3] = p[4];
1365 p[4] = p3;
1366 p[5] = p2;
1367 p[6] = p1;
1368 p[7] = p0;
1369 }
1370 break;
1371 default:
1372 PyErr_SetString(PyExc_RuntimeError,
1373 "don't know how to byteswap this array type");
1374 return NULL;
1375 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001376 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001377}
1378
Brett Cannon1eb32c22014-10-10 16:26:45 -04001379/*[clinic input]
1380array.array.reverse
1381
1382Reverse the order of the items in the array.
1383[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001384
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001385static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001386array_array_reverse_impl(arrayobject *self)
1387/*[clinic end generated code: output=c04868b36f6f4089 input=cd904f01b27d966a]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001388{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001389 Py_ssize_t itemsize = self->ob_descr->itemsize;
1390 char *p, *q;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 /* little buffer to hold items while swapping */
1392 char tmp[256]; /* 8 is probably enough -- but why skimp */
1393 assert((size_t)itemsize <= sizeof(tmp));
Guido van Rossume77a7571993-11-03 15:01:26 +00001394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 if (Py_SIZE(self) > 1) {
1396 for (p = self->ob_item,
1397 q = self->ob_item + (Py_SIZE(self) - 1)*itemsize;
1398 p < q;
1399 p += itemsize, q -= itemsize) {
1400 /* memory areas guaranteed disjoint, so memcpy
1401 * is safe (& memmove may be slower).
1402 */
1403 memcpy(tmp, p, itemsize);
1404 memcpy(p, q, itemsize);
1405 memcpy(q, tmp, itemsize);
1406 }
1407 }
Tim Petersbb307342000-09-10 05:22:54 +00001408
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001409 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001410}
Guido van Rossume77a7571993-11-03 15:01:26 +00001411
Brett Cannon1eb32c22014-10-10 16:26:45 -04001412/*[clinic input]
1413array.array.fromfile
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001414
Brett Cannon1eb32c22014-10-10 16:26:45 -04001415 f: object
1416 n: Py_ssize_t
1417 /
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001418
Brett Cannon1eb32c22014-10-10 16:26:45 -04001419Read n objects from the file object f and append them to the end of the array.
1420[clinic start generated code]*/
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001421
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001422static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001423array_array_fromfile_impl(arrayobject *self, PyObject *f, Py_ssize_t n)
1424/*[clinic end generated code: output=ec9f600e10f53510 input=e188afe8e58adf40]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001425{
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03001426 PyObject *b, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 Py_ssize_t itemsize = self->ob_descr->itemsize;
Brett Cannon1eb32c22014-10-10 16:26:45 -04001428 Py_ssize_t nbytes;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001429 _Py_IDENTIFIER(read);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 int not_enough_bytes;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001431
Mark Dickinsonc04ddff2012-10-06 18:04:49 +01001432 if (n < 0) {
1433 PyErr_SetString(PyExc_ValueError, "negative count");
1434 return NULL;
1435 }
1436 if (n > PY_SSIZE_T_MAX / itemsize) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 PyErr_NoMemory();
1438 return NULL;
1439 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +01001440 nbytes = n * itemsize;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001441
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001442 b = _PyObject_CallMethodId(f, &PyId_read, "n", nbytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 if (b == NULL)
1444 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 if (!PyBytes_Check(b)) {
1447 PyErr_SetString(PyExc_TypeError,
1448 "read() didn't return bytes");
1449 Py_DECREF(b);
1450 return NULL;
1451 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 not_enough_bytes = (PyBytes_GET_SIZE(b) != nbytes);
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001454
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03001455 res = array_array_frombytes(self, b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 Py_DECREF(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 if (res == NULL)
1458 return NULL;
Hirokazu Yamamoto54d0df62009-03-06 03:04:07 +00001459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 if (not_enough_bytes) {
1461 PyErr_SetString(PyExc_EOFError,
1462 "read() didn't return enough bytes");
1463 Py_DECREF(res);
1464 return NULL;
1465 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +00001468}
1469
Brett Cannon1eb32c22014-10-10 16:26:45 -04001470/*[clinic input]
1471array.array.tofile
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001472
Brett Cannon1eb32c22014-10-10 16:26:45 -04001473 f: object
1474 /
1475
1476Write all items (as machine values) to the file object f.
1477[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001478
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001479static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001480array_array_tofile(arrayobject *self, PyObject *f)
1481/*[clinic end generated code: output=3a2cfa8128df0777 input=b0669a484aab0831]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001482{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize;
1484 /* Write 64K blocks at a time */
1485 /* XXX Make the block size settable */
1486 int BLOCKSIZE = 64*1024;
1487 Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE;
1488 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 if (Py_SIZE(self) == 0)
1491 goto done;
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 for (i = 0; i < nblocks; i++) {
1494 char* ptr = self->ob_item + i*BLOCKSIZE;
1495 Py_ssize_t size = BLOCKSIZE;
1496 PyObject *bytes, *res;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001497 _Py_IDENTIFIER(write);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 if (i*BLOCKSIZE + size > nbytes)
1500 size = nbytes - i*BLOCKSIZE;
1501 bytes = PyBytes_FromStringAndSize(ptr, size);
1502 if (bytes == NULL)
1503 return NULL;
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001504 res = _PyObject_CallMethodIdOneArg(f, &PyId_write, bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 Py_DECREF(bytes);
1506 if (res == NULL)
1507 return NULL;
1508 Py_DECREF(res); /* drop write result */
1509 }
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001510
1511 done:
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001512 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001513}
1514
Brett Cannon1eb32c22014-10-10 16:26:45 -04001515/*[clinic input]
1516array.array.fromlist
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001517
Brett Cannon1eb32c22014-10-10 16:26:45 -04001518 list: object
1519 /
1520
1521Append items to array from list.
1522[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001523
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001524static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001525array_array_fromlist(arrayobject *self, PyObject *list)
1526/*[clinic end generated code: output=26411c2d228a3e3f input=be2605a96c49680f]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 Py_ssize_t n;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 if (!PyList_Check(list)) {
1531 PyErr_SetString(PyExc_TypeError, "arg must be list");
1532 return NULL;
1533 }
1534 n = PyList_Size(list);
1535 if (n > 0) {
1536 Py_ssize_t i, old_size;
1537 old_size = Py_SIZE(self);
1538 if (array_resize(self, old_size + n) == -1)
1539 return NULL;
1540 for (i = 0; i < n; i++) {
Zackery Spytz99d56b52018-12-08 07:16:55 -07001541 PyObject *v = PyList_GET_ITEM(list, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 if ((*self->ob_descr->setitem)(self,
1543 Py_SIZE(self) - n + i, v) != 0) {
1544 array_resize(self, old_size);
1545 return NULL;
1546 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07001547 if (n != PyList_GET_SIZE(list)) {
1548 PyErr_SetString(PyExc_RuntimeError,
1549 "list changed size during iteration");
1550 array_resize(self, old_size);
1551 return NULL;
1552 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 }
1554 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001555 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001556}
1557
Brett Cannon1eb32c22014-10-10 16:26:45 -04001558/*[clinic input]
1559array.array.tolist
1560
1561Convert array to an ordinary list with the same items.
1562[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001563
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001564static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001565array_array_tolist_impl(arrayobject *self)
1566/*[clinic end generated code: output=00b60cc9eab8ef89 input=a8d7784a94f86b53]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 PyObject *list = PyList_New(Py_SIZE(self));
1569 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 if (list == NULL)
1572 return NULL;
1573 for (i = 0; i < Py_SIZE(self); i++) {
1574 PyObject *v = getarrayitem((PyObject *)self, i);
Victor Stinner4755bea2013-07-18 01:12:35 +02001575 if (v == NULL)
1576 goto error;
Zackery Spytz99d56b52018-12-08 07:16:55 -07001577 PyList_SET_ITEM(list, i, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 }
1579 return list;
Victor Stinner4755bea2013-07-18 01:12:35 +02001580
1581error:
1582 Py_DECREF(list);
1583 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001584}
1585
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001586static PyObject *
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001587frombytes(arrayobject *self, Py_buffer *buffer)
Guido van Rossum778983b1993-02-19 15:55:02 +00001588{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 int itemsize = self->ob_descr->itemsize;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001590 Py_ssize_t n;
1591 if (buffer->itemsize != 1) {
1592 PyBuffer_Release(buffer);
Serhiy Storchakab757c832014-12-05 22:25:22 +02001593 PyErr_SetString(PyExc_TypeError, "a bytes-like object is required");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 return NULL;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001595 }
1596 n = buffer->len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 if (n % itemsize != 0) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001598 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 PyErr_SetString(PyExc_ValueError,
Serhiy Storchakab757c832014-12-05 22:25:22 +02001600 "bytes length not a multiple of item size");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 return NULL;
1602 }
1603 n = n / itemsize;
1604 if (n > 0) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001605 Py_ssize_t old_size = Py_SIZE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 if ((n > PY_SSIZE_T_MAX - old_size) ||
1607 ((old_size + n) > PY_SSIZE_T_MAX / itemsize)) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001608 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 return PyErr_NoMemory();
1610 }
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001611 if (array_resize(self, old_size + n) == -1) {
1612 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 return NULL;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001614 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 memcpy(self->ob_item + old_size * itemsize,
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001616 buffer->buf, n * itemsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 }
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001618 PyBuffer_Release(buffer);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001619 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001620}
1621
Brett Cannon1eb32c22014-10-10 16:26:45 -04001622/*[clinic input]
Brett Cannon1eb32c22014-10-10 16:26:45 -04001623array.array.frombytes
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001624
Brett Cannon1eb32c22014-10-10 16:26:45 -04001625 buffer: Py_buffer
1626 /
1627
1628Appends 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).
1629[clinic start generated code]*/
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001630
1631static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001632array_array_frombytes_impl(arrayobject *self, Py_buffer *buffer)
1633/*[clinic end generated code: output=d9842c8f7510a516 input=2bbf2b53ebfcc988]*/
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001634{
Brett Cannon1eb32c22014-10-10 16:26:45 -04001635 return frombytes(self, buffer);
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001636}
1637
Brett Cannon1eb32c22014-10-10 16:26:45 -04001638/*[clinic input]
1639array.array.tobytes
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001640
Brett Cannon1eb32c22014-10-10 16:26:45 -04001641Convert the array to an array of machine values and return the bytes representation.
1642[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001643
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001644static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001645array_array_tobytes_impl(arrayobject *self)
1646/*[clinic end generated code: output=87318e4edcdc2bb6 input=90ee495f96de34f5]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001647{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
1649 return PyBytes_FromStringAndSize(self->ob_item,
1650 Py_SIZE(self) * self->ob_descr->itemsize);
1651 } else {
1652 return PyErr_NoMemory();
1653 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001654}
1655
Brett Cannon1eb32c22014-10-10 16:26:45 -04001656/*[clinic input]
Brett Cannon1eb32c22014-10-10 16:26:45 -04001657array.array.fromunicode
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001658
Inada Naokid5d9a712020-05-11 15:37:25 +09001659 ustr: unicode
Brett Cannon1eb32c22014-10-10 16:26:45 -04001660 /
1661
1662Extends this array with data from the unicode string ustr.
1663
1664The array must be a unicode type array; otherwise a ValueError is raised.
1665Use array.frombytes(ustr.encode(...)) to append Unicode data to an array of
1666some other type.
1667[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001668
Martin v. Löwis99866332002-03-01 10:27:01 +00001669static PyObject *
Inada Naokid5d9a712020-05-11 15:37:25 +09001670array_array_fromunicode_impl(arrayobject *self, PyObject *ustr)
1671/*[clinic end generated code: output=24359f5e001a7f2b input=025db1fdade7a4ce]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001672{
Inada Naokid5d9a712020-05-11 15:37:25 +09001673 if (self->ob_descr->typecode != 'u') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 PyErr_SetString(PyExc_ValueError,
1675 "fromunicode() may only be called on "
1676 "unicode type arrays");
1677 return NULL;
1678 }
Inada Naokid5d9a712020-05-11 15:37:25 +09001679
1680 Py_ssize_t ustr_length = PyUnicode_AsWideChar(ustr, NULL, 0);
1681 assert(ustr_length > 0);
1682 if (ustr_length > 1) {
1683 ustr_length--; /* trim trailing NUL character */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 Py_ssize_t old_size = Py_SIZE(self);
Inada Naokid5d9a712020-05-11 15:37:25 +09001685 if (array_resize(self, old_size + ustr_length) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 return NULL;
Inada Naokid5d9a712020-05-11 15:37:25 +09001687 }
1688
1689 // must not fail
1690 PyUnicode_AsWideChar(
1691 ustr, ((wchar_t *)self->ob_item) + old_size, ustr_length);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 }
Martin v. Löwis99866332002-03-01 10:27:01 +00001693
Brett Cannon1eb32c22014-10-10 16:26:45 -04001694 Py_RETURN_NONE;
Martin v. Löwis99866332002-03-01 10:27:01 +00001695}
1696
Brett Cannon1eb32c22014-10-10 16:26:45 -04001697/*[clinic input]
1698array.array.tounicode
Martin v. Löwis99866332002-03-01 10:27:01 +00001699
Brett Cannon1eb32c22014-10-10 16:26:45 -04001700Extends this array with data from the unicode string ustr.
1701
1702Convert the array to a unicode string. The array must be a unicode type array;
1703otherwise a ValueError is raised. Use array.tobytes().decode() to obtain a
1704unicode string from an array of some other type.
1705[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001706
1707static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001708array_array_tounicode_impl(arrayobject *self)
1709/*[clinic end generated code: output=08e442378336e1ef input=127242eebe70b66d]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001710{
Inada Naokid5d9a712020-05-11 15:37:25 +09001711 if (self->ob_descr->typecode != 'u') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 PyErr_SetString(PyExc_ValueError,
1713 "tounicode() may only be called on unicode type arrays");
1714 return NULL;
1715 }
Inada Naokid5d9a712020-05-11 15:37:25 +09001716 return PyUnicode_FromWideChar((wchar_t *) self->ob_item, Py_SIZE(self));
Martin v. Löwis99866332002-03-01 10:27:01 +00001717}
1718
Brett Cannon1eb32c22014-10-10 16:26:45 -04001719/*[clinic input]
1720array.array.__sizeof__
Martin v. Löwis99866332002-03-01 10:27:01 +00001721
Brett Cannon1eb32c22014-10-10 16:26:45 -04001722Size of the array in memory, in bytes.
1723[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001724
Meador Inge03b4d502012-08-10 22:35:45 -05001725static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001726array_array___sizeof___impl(arrayobject *self)
1727/*[clinic end generated code: output=d8e1c61ebbe3eaed input=805586565bf2b3c6]*/
Meador Inge03b4d502012-08-10 22:35:45 -05001728{
1729 Py_ssize_t res;
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02001730 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * self->ob_descr->itemsize;
Meador Inge03b4d502012-08-10 22:35:45 -05001731 return PyLong_FromSsize_t(res);
1732}
1733
Martin v. Löwis99866332002-03-01 10:27:01 +00001734
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001735/*********************** Pickling support ************************/
1736
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001737static const struct mformatdescr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 size_t size;
1739 int is_signed;
1740 int is_big_endian;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001741} mformat_descriptors[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 {1, 0, 0}, /* 0: UNSIGNED_INT8 */
1743 {1, 1, 0}, /* 1: SIGNED_INT8 */
1744 {2, 0, 0}, /* 2: UNSIGNED_INT16_LE */
1745 {2, 0, 1}, /* 3: UNSIGNED_INT16_BE */
1746 {2, 1, 0}, /* 4: SIGNED_INT16_LE */
1747 {2, 1, 1}, /* 5: SIGNED_INT16_BE */
1748 {4, 0, 0}, /* 6: UNSIGNED_INT32_LE */
1749 {4, 0, 1}, /* 7: UNSIGNED_INT32_BE */
1750 {4, 1, 0}, /* 8: SIGNED_INT32_LE */
1751 {4, 1, 1}, /* 9: SIGNED_INT32_BE */
1752 {8, 0, 0}, /* 10: UNSIGNED_INT64_LE */
1753 {8, 0, 1}, /* 11: UNSIGNED_INT64_BE */
1754 {8, 1, 0}, /* 12: SIGNED_INT64_LE */
1755 {8, 1, 1}, /* 13: SIGNED_INT64_BE */
1756 {4, 0, 0}, /* 14: IEEE_754_FLOAT_LE */
1757 {4, 0, 1}, /* 15: IEEE_754_FLOAT_BE */
1758 {8, 0, 0}, /* 16: IEEE_754_DOUBLE_LE */
1759 {8, 0, 1}, /* 17: IEEE_754_DOUBLE_BE */
1760 {4, 0, 0}, /* 18: UTF16_LE */
1761 {4, 0, 1}, /* 19: UTF16_BE */
1762 {8, 0, 0}, /* 20: UTF32_LE */
1763 {8, 0, 1} /* 21: UTF32_BE */
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001764};
1765
1766
1767/*
1768 * Internal: This function is used to find the machine format of a given
1769 * array type code. This returns UNKNOWN_FORMAT when the machine format cannot
1770 * be found.
1771 */
1772static enum machine_format_code
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001773typecode_to_mformat_code(char typecode)
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001774{
Christian Heimes743e0cd2012-10-17 23:52:17 +02001775 const int is_big_endian = PY_BIG_ENDIAN;
1776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 size_t intsize;
1778 int is_signed;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 switch (typecode) {
1781 case 'b':
1782 return SIGNED_INT8;
1783 case 'B':
1784 return UNSIGNED_INT8;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 case 'u':
Victor Stinner62bb3942012-08-06 00:46:05 +02001787 if (sizeof(Py_UNICODE) == 2) {
1788 return UTF16_LE + is_big_endian;
1789 }
1790 if (sizeof(Py_UNICODE) == 4) {
1791 return UTF32_LE + is_big_endian;
1792 }
1793 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 case 'f':
1796 if (sizeof(float) == 4) {
1797 const float y = 16711938.0;
1798 if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0)
1799 return IEEE_754_FLOAT_BE;
1800 if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0)
1801 return IEEE_754_FLOAT_LE;
1802 }
1803 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 case 'd':
1806 if (sizeof(double) == 8) {
1807 const double x = 9006104071832581.0;
1808 if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0)
1809 return IEEE_754_DOUBLE_BE;
1810 if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
1811 return IEEE_754_DOUBLE_LE;
1812 }
1813 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 /* Integers */
1816 case 'h':
1817 intsize = sizeof(short);
1818 is_signed = 1;
1819 break;
1820 case 'H':
1821 intsize = sizeof(short);
1822 is_signed = 0;
1823 break;
1824 case 'i':
1825 intsize = sizeof(int);
1826 is_signed = 1;
1827 break;
1828 case 'I':
1829 intsize = sizeof(int);
1830 is_signed = 0;
1831 break;
1832 case 'l':
1833 intsize = sizeof(long);
1834 is_signed = 1;
1835 break;
1836 case 'L':
1837 intsize = sizeof(long);
1838 is_signed = 0;
1839 break;
Meador Inge1c9f0c92011-09-20 19:55:51 -05001840 case 'q':
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001841 intsize = sizeof(long long);
Meador Inge1c9f0c92011-09-20 19:55:51 -05001842 is_signed = 1;
1843 break;
1844 case 'Q':
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001845 intsize = sizeof(long long);
Meador Inge1c9f0c92011-09-20 19:55:51 -05001846 is_signed = 0;
1847 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 default:
1849 return UNKNOWN_FORMAT;
1850 }
1851 switch (intsize) {
1852 case 2:
1853 return UNSIGNED_INT16_LE + is_big_endian + (2 * is_signed);
1854 case 4:
1855 return UNSIGNED_INT32_LE + is_big_endian + (2 * is_signed);
1856 case 8:
1857 return UNSIGNED_INT64_LE + is_big_endian + (2 * is_signed);
1858 default:
1859 return UNKNOWN_FORMAT;
1860 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001861}
1862
1863/* Forward declaration. */
1864static PyObject *array_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
1865
1866/*
1867 * Internal: This function wraps the array constructor--i.e., array_new()--to
1868 * allow the creation of array objects from C code without having to deal
1869 * directly the tuple argument of array_new(). The typecode argument is a
1870 * Unicode character value, like 'i' or 'f' for example, representing an array
1871 * type code. The items argument is a bytes or a list object from which
1872 * contains the initial value of the array.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 *
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001874 * On success, this functions returns the array object created. Otherwise,
1875 * NULL is returned to indicate a failure.
1876 */
1877static PyObject *
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001878make_array(PyTypeObject *arraytype, char typecode, PyObject *items)
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 PyObject *new_args;
1881 PyObject *array_obj;
1882 PyObject *typecode_obj;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 assert(arraytype != NULL);
1885 assert(items != NULL);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001886
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001887 typecode_obj = PyUnicode_FromOrdinal(typecode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 if (typecode_obj == NULL)
1889 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 new_args = PyTuple_New(2);
Mat M56935a52017-11-14 01:00:54 -05001892 if (new_args == NULL) {
1893 Py_DECREF(typecode_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 return NULL;
Mat M56935a52017-11-14 01:00:54 -05001895 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 Py_INCREF(items);
1897 PyTuple_SET_ITEM(new_args, 0, typecode_obj);
1898 PyTuple_SET_ITEM(new_args, 1, items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 array_obj = array_new(arraytype, new_args, NULL);
1901 Py_DECREF(new_args);
1902 if (array_obj == NULL)
1903 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 return array_obj;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001906}
1907
1908/*
1909 * This functions is a special constructor used when unpickling an array. It
1910 * provides a portable way to rebuild an array from its memory representation.
1911 */
Brett Cannon1eb32c22014-10-10 16:26:45 -04001912/*[clinic input]
1913array._array_reconstructor
1914
1915 arraytype: object(type="PyTypeObject *")
Larry Hastingsdbfdc382015-05-04 06:59:46 -07001916 typecode: int(accept={str})
Brett Cannon1eb32c22014-10-10 16:26:45 -04001917 mformat_code: int(type="enum machine_format_code")
1918 items: object
1919 /
1920
1921Internal. Used for pickling support.
1922[clinic start generated code]*/
1923
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001924static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001925array__array_reconstructor_impl(PyObject *module, PyTypeObject *arraytype,
Larry Hastings89964c42015-04-14 18:07:59 -04001926 int typecode,
1927 enum machine_format_code mformat_code,
1928 PyObject *items)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001929/*[clinic end generated code: output=e05263141ba28365 input=2464dc8f4c7736b5]*/
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001930{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 PyObject *converted_items;
1932 PyObject *result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001933 const struct arraydescr *descr;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 if (!PyType_Check(arraytype)) {
1936 PyErr_Format(PyExc_TypeError,
Sylvaina90e64b2017-03-29 20:09:22 +02001937 "first argument must be a type object, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 Py_TYPE(arraytype)->tp_name);
1939 return NULL;
1940 }
1941 if (!PyType_IsSubtype(arraytype, &Arraytype)) {
1942 PyErr_Format(PyExc_TypeError,
1943 "%.200s is not a subtype of %.200s",
1944 arraytype->tp_name, Arraytype.tp_name);
1945 return NULL;
1946 }
1947 for (descr = descriptors; descr->typecode != '\0'; descr++) {
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001948 if ((int)descr->typecode == typecode)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 break;
1950 }
1951 if (descr->typecode == '\0') {
1952 PyErr_SetString(PyExc_ValueError,
1953 "second argument must be a valid type code");
1954 return NULL;
1955 }
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001956 if (mformat_code < MACHINE_FORMAT_CODE_MIN ||
1957 mformat_code > MACHINE_FORMAT_CODE_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 PyErr_SetString(PyExc_ValueError,
1959 "third argument must be a valid machine format code.");
1960 return NULL;
1961 }
1962 if (!PyBytes_Check(items)) {
1963 PyErr_Format(PyExc_TypeError,
1964 "fourth argument should be bytes, not %.200s",
1965 Py_TYPE(items)->tp_name);
1966 return NULL;
1967 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 /* Fast path: No decoding has to be done. */
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001970 if (mformat_code == typecode_to_mformat_code((char)typecode) ||
1971 mformat_code == UNKNOWN_FORMAT) {
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001972 return make_array(arraytype, (char)typecode, items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 /* Slow path: Decode the byte string according to the given machine
1976 * format code. This occurs when the computer unpickling the array
1977 * object is architecturally different from the one that pickled the
1978 * array.
1979 */
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001980 if (Py_SIZE(items) % mformat_descriptors[mformat_code].size != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 PyErr_SetString(PyExc_ValueError,
1982 "string length not a multiple of item size");
1983 return NULL;
1984 }
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001985 switch (mformat_code) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001986 case IEEE_754_FLOAT_LE:
1987 case IEEE_754_FLOAT_BE: {
sthaa3ecb82019-03-20 20:49:39 +01001988 Py_ssize_t i;
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001989 int le = (mformat_code == IEEE_754_FLOAT_LE) ? 1 : 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 Py_ssize_t itemcount = Py_SIZE(items) / 4;
1991 const unsigned char *memstr =
1992 (unsigned char *)PyBytes_AS_STRING(items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 converted_items = PyList_New(itemcount);
1995 if (converted_items == NULL)
1996 return NULL;
1997 for (i = 0; i < itemcount; i++) {
1998 PyObject *pyfloat = PyFloat_FromDouble(
1999 _PyFloat_Unpack4(&memstr[i * 4], le));
2000 if (pyfloat == NULL) {
2001 Py_DECREF(converted_items);
2002 return NULL;
2003 }
2004 PyList_SET_ITEM(converted_items, i, pyfloat);
2005 }
2006 break;
2007 }
2008 case IEEE_754_DOUBLE_LE:
2009 case IEEE_754_DOUBLE_BE: {
sthaa3ecb82019-03-20 20:49:39 +01002010 Py_ssize_t i;
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002011 int le = (mformat_code == IEEE_754_DOUBLE_LE) ? 1 : 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 Py_ssize_t itemcount = Py_SIZE(items) / 8;
2013 const unsigned char *memstr =
2014 (unsigned char *)PyBytes_AS_STRING(items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 converted_items = PyList_New(itemcount);
2017 if (converted_items == NULL)
2018 return NULL;
2019 for (i = 0; i < itemcount; i++) {
2020 PyObject *pyfloat = PyFloat_FromDouble(
2021 _PyFloat_Unpack8(&memstr[i * 8], le));
2022 if (pyfloat == NULL) {
2023 Py_DECREF(converted_items);
2024 return NULL;
2025 }
2026 PyList_SET_ITEM(converted_items, i, pyfloat);
2027 }
2028 break;
2029 }
2030 case UTF16_LE:
2031 case UTF16_BE: {
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002032 int byteorder = (mformat_code == UTF16_LE) ? -1 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 converted_items = PyUnicode_DecodeUTF16(
2034 PyBytes_AS_STRING(items), Py_SIZE(items),
2035 "strict", &byteorder);
2036 if (converted_items == NULL)
2037 return NULL;
2038 break;
2039 }
2040 case UTF32_LE:
2041 case UTF32_BE: {
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002042 int byteorder = (mformat_code == UTF32_LE) ? -1 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 converted_items = PyUnicode_DecodeUTF32(
2044 PyBytes_AS_STRING(items), Py_SIZE(items),
2045 "strict", &byteorder);
2046 if (converted_items == NULL)
2047 return NULL;
2048 break;
2049 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 case UNSIGNED_INT8:
2052 case SIGNED_INT8:
2053 case UNSIGNED_INT16_LE:
2054 case UNSIGNED_INT16_BE:
2055 case SIGNED_INT16_LE:
2056 case SIGNED_INT16_BE:
2057 case UNSIGNED_INT32_LE:
2058 case UNSIGNED_INT32_BE:
2059 case SIGNED_INT32_LE:
2060 case SIGNED_INT32_BE:
2061 case UNSIGNED_INT64_LE:
2062 case UNSIGNED_INT64_BE:
2063 case SIGNED_INT64_LE:
2064 case SIGNED_INT64_BE: {
sthaa3ecb82019-03-20 20:49:39 +01002065 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 const struct mformatdescr mf_descr =
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002067 mformat_descriptors[mformat_code];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 Py_ssize_t itemcount = Py_SIZE(items) / mf_descr.size;
2069 const unsigned char *memstr =
2070 (unsigned char *)PyBytes_AS_STRING(items);
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002071 const struct arraydescr *descr;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 /* If possible, try to pack array's items using a data type
2074 * that fits better. This may result in an array with narrower
2075 * or wider elements.
2076 *
Martin Panter4c359642016-05-08 13:53:41 +00002077 * For example, if a 32-bit machine pickles an L-code array of
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002078 * unsigned longs, then the array will be unpickled by 64-bit
2079 * machine as an I-code array of unsigned ints.
2080 *
2081 * XXX: Is it possible to write a unit test for this?
2082 */
2083 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2084 if (descr->is_integer_type &&
Victor Stinner706768c2014-08-16 01:03:39 +02002085 (size_t)descr->itemsize == mf_descr.size &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086 descr->is_signed == mf_descr.is_signed)
2087 typecode = descr->typecode;
2088 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 converted_items = PyList_New(itemcount);
2091 if (converted_items == NULL)
2092 return NULL;
2093 for (i = 0; i < itemcount; i++) {
2094 PyObject *pylong;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 pylong = _PyLong_FromByteArray(
2097 &memstr[i * mf_descr.size],
2098 mf_descr.size,
2099 !mf_descr.is_big_endian,
2100 mf_descr.is_signed);
2101 if (pylong == NULL) {
2102 Py_DECREF(converted_items);
2103 return NULL;
2104 }
2105 PyList_SET_ITEM(converted_items, i, pylong);
2106 }
2107 break;
2108 }
2109 case UNKNOWN_FORMAT:
2110 /* Impossible, but needed to shut up GCC about the unhandled
2111 * enumeration value.
2112 */
2113 default:
2114 PyErr_BadArgument();
2115 return NULL;
2116 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002117
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002118 result = make_array(arraytype, (char)typecode, converted_items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002119 Py_DECREF(converted_items);
2120 return result;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002121}
2122
Brett Cannon1eb32c22014-10-10 16:26:45 -04002123/*[clinic input]
2124array.array.__reduce_ex__
2125
2126 value: object
2127 /
2128
2129Return state information for pickling.
2130[clinic start generated code]*/
2131
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002132static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04002133array_array___reduce_ex__(arrayobject *self, PyObject *value)
2134/*[clinic end generated code: output=051e0a6175d0eddb input=c36c3f85de7df6cd]*/
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 PyObject *dict;
2137 PyObject *result;
2138 PyObject *array_str;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002139 int typecode = self->ob_descr->typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 int mformat_code;
2141 static PyObject *array_reconstructor = NULL;
2142 long protocol;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002143 _Py_IDENTIFIER(_array_reconstructor);
2144 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 if (array_reconstructor == NULL) {
2147 PyObject *array_module = PyImport_ImportModule("array");
2148 if (array_module == NULL)
2149 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002150 array_reconstructor = _PyObject_GetAttrId(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 array_module,
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002152 &PyId__array_reconstructor);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153 Py_DECREF(array_module);
2154 if (array_reconstructor == NULL)
2155 return NULL;
2156 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002158 if (!PyLong_Check(value)) {
2159 PyErr_SetString(PyExc_TypeError,
Sylvaina90e64b2017-03-29 20:09:22 +02002160 "__reduce_ex__ argument should be an integer");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 return NULL;
2162 }
2163 protocol = PyLong_AsLong(value);
2164 if (protocol == -1 && PyErr_Occurred())
2165 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002166
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002167 if (_PyObject_LookupAttrId((PyObject *)self, &PyId___dict__, &dict) < 0) {
2168 return NULL;
2169 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 dict = Py_None;
2172 Py_INCREF(dict);
2173 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 mformat_code = typecode_to_mformat_code(typecode);
2176 if (mformat_code == UNKNOWN_FORMAT || protocol < 3) {
2177 /* Convert the array to a list if we got something weird
2178 * (e.g., non-IEEE floats), or we are pickling the array using
2179 * a Python 2.x compatible protocol.
2180 *
2181 * It is necessary to use a list representation for Python 2.x
2182 * compatible pickle protocol, since Python 2's str objects
2183 * are unpickled as unicode by Python 3. Thus it is impossible
2184 * to make arrays unpicklable by Python 3 by using their memory
2185 * representation, unless we resort to ugly hacks such as
2186 * coercing unicode objects to bytes in array_reconstructor.
2187 */
2188 PyObject *list;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002189 list = array_array_tolist_impl(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 if (list == NULL) {
2191 Py_DECREF(dict);
2192 return NULL;
2193 }
2194 result = Py_BuildValue(
Brett Cannon1eb32c22014-10-10 16:26:45 -04002195 "O(CO)O", Py_TYPE(self), typecode, list, dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196 Py_DECREF(list);
2197 Py_DECREF(dict);
2198 return result;
2199 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002200
Brett Cannon1eb32c22014-10-10 16:26:45 -04002201 array_str = array_array_tobytes_impl(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 if (array_str == NULL) {
2203 Py_DECREF(dict);
2204 return NULL;
2205 }
2206 result = Py_BuildValue(
Brett Cannon1eb32c22014-10-10 16:26:45 -04002207 "O(OCiN)O", array_reconstructor, Py_TYPE(self), typecode,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 mformat_code, array_str, dict);
2209 Py_DECREF(dict);
2210 return result;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002211}
2212
Martin v. Löwis99866332002-03-01 10:27:01 +00002213static PyObject *
2214array_get_typecode(arrayobject *a, void *closure)
2215{
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002216 char typecode = a->ob_descr->typecode;
2217 return PyUnicode_FromOrdinal(typecode);
Martin v. Löwis99866332002-03-01 10:27:01 +00002218}
2219
2220static PyObject *
2221array_get_itemsize(arrayobject *a, void *closure)
2222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 return PyLong_FromLong((long)a->ob_descr->itemsize);
Martin v. Löwis99866332002-03-01 10:27:01 +00002224}
2225
2226static PyGetSetDef array_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 {"typecode", (getter) array_get_typecode, NULL,
2228 "the typecode character used to create the array"},
2229 {"itemsize", (getter) array_get_itemsize, NULL,
2230 "the size, in bytes, of one array item"},
2231 {NULL}
Martin v. Löwis99866332002-03-01 10:27:01 +00002232};
2233
Martin v. Löwis59683e82008-06-13 07:50:45 +00002234static PyMethodDef array_methods[] = {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002235 ARRAY_ARRAY_APPEND_METHODDEF
2236 ARRAY_ARRAY_BUFFER_INFO_METHODDEF
2237 ARRAY_ARRAY_BYTESWAP_METHODDEF
2238 ARRAY_ARRAY___COPY___METHODDEF
2239 ARRAY_ARRAY_COUNT_METHODDEF
2240 ARRAY_ARRAY___DEEPCOPY___METHODDEF
2241 ARRAY_ARRAY_EXTEND_METHODDEF
2242 ARRAY_ARRAY_FROMFILE_METHODDEF
2243 ARRAY_ARRAY_FROMLIST_METHODDEF
Brett Cannon1eb32c22014-10-10 16:26:45 -04002244 ARRAY_ARRAY_FROMBYTES_METHODDEF
2245 ARRAY_ARRAY_FROMUNICODE_METHODDEF
2246 ARRAY_ARRAY_INDEX_METHODDEF
2247 ARRAY_ARRAY_INSERT_METHODDEF
2248 ARRAY_ARRAY_POP_METHODDEF
2249 ARRAY_ARRAY___REDUCE_EX___METHODDEF
2250 ARRAY_ARRAY_REMOVE_METHODDEF
2251 ARRAY_ARRAY_REVERSE_METHODDEF
2252 ARRAY_ARRAY_TOFILE_METHODDEF
2253 ARRAY_ARRAY_TOLIST_METHODDEF
Brett Cannon1eb32c22014-10-10 16:26:45 -04002254 ARRAY_ARRAY_TOBYTES_METHODDEF
2255 ARRAY_ARRAY_TOUNICODE_METHODDEF
2256 ARRAY_ARRAY___SIZEOF___METHODDEF
2257 {NULL, NULL} /* sentinel */
Guido van Rossum778983b1993-02-19 15:55:02 +00002258};
2259
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002260static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00002261array_repr(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +00002262{
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002263 char typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 PyObject *s, *v = NULL;
2265 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00002266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 len = Py_SIZE(a);
2268 typecode = a->ob_descr->typecode;
2269 if (len == 0) {
Serhiy Storchakab3a77962017-09-21 14:24:13 +03002270 return PyUnicode_FromFormat("%s('%c')",
2271 _PyType_Name(Py_TYPE(a)), (int)typecode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 }
Gregory P. Smith9504b132012-12-10 20:20:20 -08002273 if (typecode == 'u') {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002274 v = array_array_tounicode_impl(a);
Gregory P. Smith9504b132012-12-10 20:20:20 -08002275 } else {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002276 v = array_array_tolist_impl(a);
Gregory P. Smith9504b132012-12-10 20:20:20 -08002277 }
Victor Stinner29ec5952013-02-26 00:27:38 +01002278 if (v == NULL)
2279 return NULL;
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00002280
Serhiy Storchakab3a77962017-09-21 14:24:13 +03002281 s = PyUnicode_FromFormat("%s('%c', %R)",
2282 _PyType_Name(Py_TYPE(a)), (int)typecode, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 Py_DECREF(v);
2284 return s;
Guido van Rossum778983b1993-02-19 15:55:02 +00002285}
2286
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002287static PyObject*
2288array_subscr(arrayobject* self, PyObject* item)
2289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 if (PyIndex_Check(item)) {
2291 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2292 if (i==-1 && PyErr_Occurred()) {
2293 return NULL;
2294 }
2295 if (i < 0)
2296 i += Py_SIZE(self);
2297 return array_item(self, i);
2298 }
2299 else if (PySlice_Check(item)) {
Zackery Spytz14514d92019-05-17 01:13:03 -06002300 Py_ssize_t start, stop, step, slicelength, i;
2301 size_t cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 PyObject* result;
2303 arrayobject* ar;
2304 int itemsize = self->ob_descr->itemsize;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002305
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002306 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 return NULL;
2308 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002309 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2310 step);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002312 if (slicelength <= 0) {
2313 return newarrayobject(&Arraytype, 0, self->ob_descr);
2314 }
2315 else if (step == 1) {
2316 PyObject *result = newarrayobject(&Arraytype,
2317 slicelength, self->ob_descr);
2318 if (result == NULL)
2319 return NULL;
2320 memcpy(((arrayobject *)result)->ob_item,
2321 self->ob_item + start * itemsize,
2322 slicelength * itemsize);
2323 return result;
2324 }
2325 else {
2326 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
2327 if (!result) return NULL;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002329 ar = (arrayobject*)result;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 for (cur = start, i = 0; i < slicelength;
2332 cur += step, i++) {
2333 memcpy(ar->ob_item + i*itemsize,
2334 self->ob_item + cur*itemsize,
2335 itemsize);
2336 }
2337
2338 return result;
2339 }
2340 }
2341 else {
2342 PyErr_SetString(PyExc_TypeError,
2343 "array indices must be integers");
2344 return NULL;
2345 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002346}
2347
2348static int
2349array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
2350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 Py_ssize_t start, stop, step, slicelength, needed;
2352 arrayobject* other;
2353 int itemsize;
Thomas Woutersed03b412007-08-28 21:37:11 +00002354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 if (PyIndex_Check(item)) {
2356 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Alexandre Vassalotti47137252009-07-05 19:57:00 +00002357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 if (i == -1 && PyErr_Occurred())
2359 return -1;
2360 if (i < 0)
2361 i += Py_SIZE(self);
2362 if (i < 0 || i >= Py_SIZE(self)) {
2363 PyErr_SetString(PyExc_IndexError,
2364 "array assignment index out of range");
2365 return -1;
2366 }
2367 if (value == NULL) {
2368 /* Fall through to slice assignment */
2369 start = i;
2370 stop = i + 1;
2371 step = 1;
2372 slicelength = 1;
2373 }
2374 else
2375 return (*self->ob_descr->setitem)(self, i, value);
2376 }
2377 else if (PySlice_Check(item)) {
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002378 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 return -1;
2380 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002381 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2382 step);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 }
2384 else {
2385 PyErr_SetString(PyExc_TypeError,
Sylvaina90e64b2017-03-29 20:09:22 +02002386 "array indices must be integers");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 return -1;
2388 }
2389 if (value == NULL) {
2390 other = NULL;
2391 needed = 0;
2392 }
2393 else if (array_Check(value)) {
2394 other = (arrayobject *)value;
2395 needed = Py_SIZE(other);
2396 if (self == other) {
2397 /* Special case "self[i:j] = self" -- copy self first */
2398 int ret;
2399 value = array_slice(other, 0, needed);
2400 if (value == NULL)
2401 return -1;
2402 ret = array_ass_subscr(self, item, value);
2403 Py_DECREF(value);
2404 return ret;
2405 }
2406 if (other->ob_descr != self->ob_descr) {
2407 PyErr_BadArgument();
2408 return -1;
2409 }
2410 }
2411 else {
2412 PyErr_Format(PyExc_TypeError,
2413 "can only assign array (not \"%.200s\") to array slice",
2414 Py_TYPE(value)->tp_name);
2415 return -1;
2416 }
2417 itemsize = self->ob_descr->itemsize;
2418 /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
2419 if ((step > 0 && stop < start) ||
2420 (step < 0 && stop > start))
2421 stop = start;
Alexandre Vassalotti47137252009-07-05 19:57:00 +00002422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 /* Issue #4509: If the array has exported buffers and the slice
2424 assignment would change the size of the array, fail early to make
2425 sure we don't modify it. */
2426 if ((needed == 0 || slicelength != needed) && self->ob_exports > 0) {
2427 PyErr_SetString(PyExc_BufferError,
2428 "cannot resize an array that is exporting buffers");
2429 return -1;
2430 }
Mark Dickinsonbc099642010-01-29 17:27:24 +00002431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 if (step == 1) {
2433 if (slicelength > needed) {
2434 memmove(self->ob_item + (start + needed) * itemsize,
2435 self->ob_item + stop * itemsize,
2436 (Py_SIZE(self) - stop) * itemsize);
2437 if (array_resize(self, Py_SIZE(self) +
2438 needed - slicelength) < 0)
2439 return -1;
2440 }
2441 else if (slicelength < needed) {
2442 if (array_resize(self, Py_SIZE(self) +
2443 needed - slicelength) < 0)
2444 return -1;
2445 memmove(self->ob_item + (start + needed) * itemsize,
2446 self->ob_item + stop * itemsize,
2447 (Py_SIZE(self) - start - needed) * itemsize);
2448 }
2449 if (needed > 0)
2450 memcpy(self->ob_item + start * itemsize,
2451 other->ob_item, needed * itemsize);
2452 return 0;
2453 }
2454 else if (needed == 0) {
2455 /* Delete slice */
2456 size_t cur;
2457 Py_ssize_t i;
Thomas Woutersed03b412007-08-28 21:37:11 +00002458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 if (step < 0) {
2460 stop = start + 1;
2461 start = stop + step * (slicelength - 1) - 1;
2462 step = -step;
2463 }
2464 for (cur = start, i = 0; i < slicelength;
2465 cur += step, i++) {
2466 Py_ssize_t lim = step - 1;
Thomas Woutersed03b412007-08-28 21:37:11 +00002467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 if (cur + step >= (size_t)Py_SIZE(self))
2469 lim = Py_SIZE(self) - cur - 1;
2470 memmove(self->ob_item + (cur - i) * itemsize,
2471 self->ob_item + (cur + 1) * itemsize,
2472 lim * itemsize);
2473 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002474 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 if (cur < (size_t)Py_SIZE(self)) {
2476 memmove(self->ob_item + (cur-slicelength) * itemsize,
2477 self->ob_item + cur * itemsize,
2478 (Py_SIZE(self) - cur) * itemsize);
2479 }
2480 if (array_resize(self, Py_SIZE(self) - slicelength) < 0)
2481 return -1;
2482 return 0;
2483 }
2484 else {
Zackery Spytz14514d92019-05-17 01:13:03 -06002485 size_t cur;
2486 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487
2488 if (needed != slicelength) {
2489 PyErr_Format(PyExc_ValueError,
2490 "attempt to assign array of size %zd "
2491 "to extended slice of size %zd",
2492 needed, slicelength);
2493 return -1;
2494 }
2495 for (cur = start, i = 0; i < slicelength;
2496 cur += step, i++) {
2497 memcpy(self->ob_item + cur * itemsize,
2498 other->ob_item + i * itemsize,
2499 itemsize);
2500 }
2501 return 0;
2502 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002503}
2504
2505static PyMappingMethods array_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002506 (lenfunc)array_length,
2507 (binaryfunc)array_subscr,
2508 (objobjargproc)array_ass_subscr
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002509};
2510
Guido van Rossumd8faa362007-04-27 19:54:29 +00002511static const void *emptybuf = "";
2512
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002513
2514static int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00002515array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002516{
Stefan Krah650c1e82015-02-03 21:43:23 +01002517 if (view == NULL) {
2518 PyErr_SetString(PyExc_BufferError,
2519 "array_buffer_getbuf: view==NULL argument is obsolete");
2520 return -1;
2521 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 view->buf = (void *)self->ob_item;
2524 view->obj = (PyObject*)self;
2525 Py_INCREF(self);
2526 if (view->buf == NULL)
2527 view->buf = (void *)emptybuf;
Victor Stinnerfe2978b2020-05-27 14:55:10 +02002528 view->len = Py_SIZE(self) * self->ob_descr->itemsize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 view->readonly = 0;
2530 view->ndim = 1;
2531 view->itemsize = self->ob_descr->itemsize;
2532 view->suboffsets = NULL;
2533 view->shape = NULL;
2534 if ((flags & PyBUF_ND)==PyBUF_ND) {
Victor Stinnerfe2978b2020-05-27 14:55:10 +02002535 view->shape = &((PyVarObject*)self)->ob_size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002536 }
2537 view->strides = NULL;
2538 if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES)
2539 view->strides = &(view->itemsize);
2540 view->format = NULL;
2541 view->internal = NULL;
Victor Stinner62bb3942012-08-06 00:46:05 +02002542 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002543 view->format = (char *)self->ob_descr->formats;
Victor Stinner62bb3942012-08-06 00:46:05 +02002544#ifdef Py_UNICODE_WIDE
2545 if (self->ob_descr->typecode == 'u') {
2546 view->format = "w";
2547 }
2548#endif
2549 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 self->ob_exports++;
2552 return 0;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002553}
2554
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002555static void
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00002556array_buffer_relbuf(arrayobject *self, Py_buffer *view)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002557{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002558 self->ob_exports--;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002559}
2560
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002561static PySequenceMethods array_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002562 (lenfunc)array_length, /*sq_length*/
2563 (binaryfunc)array_concat, /*sq_concat*/
2564 (ssizeargfunc)array_repeat, /*sq_repeat*/
2565 (ssizeargfunc)array_item, /*sq_item*/
2566 0, /*sq_slice*/
2567 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
2568 0, /*sq_ass_slice*/
2569 (objobjproc)array_contains, /*sq_contains*/
2570 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
2571 (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/
Guido van Rossum778983b1993-02-19 15:55:02 +00002572};
2573
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002574static PyBufferProcs array_as_buffer = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002575 (getbufferproc)array_buffer_getbuf,
2576 (releasebufferproc)array_buffer_relbuf
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002577};
2578
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002579static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +00002580array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum778983b1993-02-19 15:55:02 +00002581{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 int c;
2583 PyObject *initial = NULL, *it = NULL;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002584 const struct arraydescr *descr;
Martin v. Löwis99866332002-03-01 10:27:01 +00002585
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03002586 if (type == &Arraytype && !_PyArg_NoKeywords("array.array", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002587 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00002588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
2590 return NULL;
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00002591
Steve Dowerb82e17e2019-05-23 08:45:22 -07002592 if (PySys_Audit("array.__new__", "CO",
2593 c, initial ? initial : Py_None) < 0) {
2594 return NULL;
2595 }
2596
Alexandre Vassalotti9730e332013-11-29 20:47:15 -08002597 if (initial && c != 'u') {
2598 if (PyUnicode_Check(initial)) {
2599 PyErr_Format(PyExc_TypeError, "cannot use a str to initialize "
2600 "an array with typecode '%c'", c);
2601 return NULL;
2602 }
2603 else if (array_Check(initial) &&
2604 ((arrayobject*)initial)->ob_descr->typecode == 'u') {
2605 PyErr_Format(PyExc_TypeError, "cannot use a unicode array to "
2606 "initialize an array with typecode '%c'", c);
2607 return NULL;
2608 }
2609 }
2610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002611 if (!(initial == NULL || PyList_Check(initial)
2612 || PyByteArray_Check(initial)
2613 || PyBytes_Check(initial)
2614 || PyTuple_Check(initial)
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002615 || ((c=='u') && PyUnicode_Check(initial))
2616 || (array_Check(initial)
2617 && c == ((arrayobject*)initial)->ob_descr->typecode))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002618 it = PyObject_GetIter(initial);
2619 if (it == NULL)
2620 return NULL;
2621 /* We set initial to NULL so that the subsequent code
2622 will create an empty array of the appropriate type
2623 and afterwards we can use array_iter_extend to populate
2624 the array.
2625 */
2626 initial = NULL;
2627 }
2628 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2629 if (descr->typecode == c) {
2630 PyObject *a;
2631 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00002632
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002633 if (initial == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002634 len = 0;
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002635 else if (PyList_Check(initial))
2636 len = PyList_GET_SIZE(initial);
2637 else if (PyTuple_Check(initial) || array_Check(initial))
2638 len = Py_SIZE(initial);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002639 else
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002640 len = 0;
Martin v. Löwis99866332002-03-01 10:27:01 +00002641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002642 a = newarrayobject(type, len, descr);
2643 if (a == NULL)
2644 return NULL;
2645
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002646 if (len > 0 && !array_Check(initial)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 Py_ssize_t i;
2648 for (i = 0; i < len; i++) {
2649 PyObject *v =
2650 PySequence_GetItem(initial, i);
2651 if (v == NULL) {
2652 Py_DECREF(a);
2653 return NULL;
2654 }
2655 if (setarrayitem(a, i, v) != 0) {
2656 Py_DECREF(v);
2657 Py_DECREF(a);
2658 return NULL;
2659 }
2660 Py_DECREF(v);
2661 }
2662 }
2663 else if (initial != NULL && (PyByteArray_Check(initial) ||
2664 PyBytes_Check(initial))) {
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03002665 PyObject *v;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002666 v = array_array_frombytes((arrayobject *)a,
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03002667 initial);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 if (v == NULL) {
2669 Py_DECREF(a);
2670 return NULL;
2671 }
2672 Py_DECREF(v);
2673 }
2674 else if (initial != NULL && PyUnicode_Check(initial)) {
Victor Stinner1fbcaef2011-09-30 01:54:04 +02002675 Py_ssize_t n;
Inada Naokid5d9a712020-05-11 15:37:25 +09002676 wchar_t *ustr = PyUnicode_AsWideCharString(initial, &n);
Victor Stinner62bb3942012-08-06 00:46:05 +02002677 if (ustr == NULL) {
Victor Stinner1fbcaef2011-09-30 01:54:04 +02002678 Py_DECREF(a);
2679 return NULL;
2680 }
Victor Stinner62bb3942012-08-06 00:46:05 +02002681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002682 if (n > 0) {
2683 arrayobject *self = (arrayobject *)a;
Inada Naokid5d9a712020-05-11 15:37:25 +09002684 // self->ob_item may be NULL but it is safe.
2685 PyMem_Free(self->ob_item);
2686 self->ob_item = (char *)ustr;
2687 Py_SET_SIZE(self, n);
2688 self->allocated = n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002689 }
2690 }
Benjamin Peterson682124c2014-10-10 20:58:30 -04002691 else if (initial != NULL && array_Check(initial) && len > 0) {
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002692 arrayobject *self = (arrayobject *)a;
2693 arrayobject *other = (arrayobject *)initial;
2694 memcpy(self->ob_item, other->ob_item, len * other->ob_descr->itemsize);
2695 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002696 if (it != NULL) {
2697 if (array_iter_extend((arrayobject *)a, it) == -1) {
2698 Py_DECREF(it);
2699 Py_DECREF(a);
2700 return NULL;
2701 }
2702 Py_DECREF(it);
2703 }
2704 return a;
2705 }
2706 }
2707 PyErr_SetString(PyExc_ValueError,
Meador Inge1c9f0c92011-09-20 19:55:51 -05002708 "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 +00002709 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00002710}
2711
Guido van Rossum778983b1993-02-19 15:55:02 +00002712
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002713PyDoc_STRVAR(module_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00002714"This module defines an object type which can efficiently represent\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002715an array of basic values: characters, integers, floating point\n\
2716numbers. Arrays are sequence types and behave very much like lists,\n\
Alexandre Vassalotti9730e332013-11-29 20:47:15 -08002717except that the type of objects stored in them is constrained.\n");
2718
2719PyDoc_STRVAR(arraytype_doc,
2720"array(typecode [, initializer]) -> array\n\
2721\n\
2722Return a new array whose items are restricted by typecode, and\n\
2723initialized from the optional initializer value, which must be a list,\n\
2724string or iterable over elements of the appropriate type.\n\
2725\n\
2726Arrays represent basic values and behave very much like lists, except\n\
2727the type of objects stored in them is constrained. The type is specified\n\
2728at object creation time by using a type code, which is a single character.\n\
2729The following type codes are defined:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002730\n\
oldkaa0735f2018-02-02 16:52:55 +08002731 Type code C Type Minimum size in bytes\n\
2732 'b' signed integer 1\n\
2733 'B' unsigned integer 1\n\
2734 'u' Unicode character 2 (see note)\n\
2735 'h' signed integer 2\n\
2736 'H' unsigned integer 2\n\
2737 'i' signed integer 2\n\
2738 'I' unsigned integer 2\n\
2739 'l' signed integer 4\n\
2740 'L' unsigned integer 4\n\
2741 'q' signed integer 8 (see note)\n\
2742 'Q' unsigned integer 8 (see note)\n\
2743 'f' floating point 4\n\
2744 'd' floating point 8\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002745\n\
oldkaa0735f2018-02-02 16:52:55 +08002746NOTE: The 'u' typecode corresponds to Python's unicode character. On\n\
Victor Stinner62bb3942012-08-06 00:46:05 +02002747narrow builds this is 2-bytes on wide builds this is 4-bytes.\n\
2748\n\
oldkaa0735f2018-02-02 16:52:55 +08002749NOTE: The 'q' and 'Q' type codes are only available if the platform\n\
2750C compiler used to build Python supports 'long long', or, on Windows,\n\
Meador Inge1c9f0c92011-09-20 19:55:51 -05002751'__int64'.\n\
2752\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002753Methods:\n\
2754\n\
2755append() -- append a new item to the end of the array\n\
2756buffer_info() -- return information giving the current memory info\n\
2757byteswap() -- byteswap all the items of the array\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002758count() -- return number of occurrences of an object\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00002759extend() -- extend array by appending multiple elements from an iterable\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002760fromfile() -- read items from a file object\n\
2761fromlist() -- append items from the list\n\
Florent Xiclunac45fb252011-10-24 13:14:55 +02002762frombytes() -- append items from the string\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002763index() -- return index of first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002764insert() -- insert a new item into the array at a provided position\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00002765pop() -- remove and return item (default last)\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002766remove() -- remove first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002767reverse() -- reverse the order of the items in the array\n\
2768tofile() -- write all items to a file object\n\
2769tolist() -- return the array converted to an ordinary list\n\
Florent Xiclunac45fb252011-10-24 13:14:55 +02002770tobytes() -- return the array converted to a string\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002771\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002772Attributes:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002773\n\
2774typecode -- the typecode character used to create the array\n\
2775itemsize -- the length in bytes of one array item\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002776");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002777
Raymond Hettinger625812f2003-01-07 01:58:52 +00002778static PyObject *array_iter(arrayobject *ao);
2779
Tim Peters0c322792002-07-17 16:49:03 +00002780static PyTypeObject Arraytype = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 PyVarObject_HEAD_INIT(NULL, 0)
2782 "array.array",
2783 sizeof(arrayobject),
2784 0,
2785 (destructor)array_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002786 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002787 0, /* tp_getattr */
2788 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002789 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002790 (reprfunc)array_repr, /* tp_repr */
2791 0, /* tp_as_number*/
2792 &array_as_sequence, /* tp_as_sequence*/
2793 &array_as_mapping, /* tp_as_mapping*/
2794 0, /* tp_hash */
2795 0, /* tp_call */
2796 0, /* tp_str */
2797 PyObject_GenericGetAttr, /* tp_getattro */
2798 0, /* tp_setattro */
2799 &array_as_buffer, /* tp_as_buffer*/
2800 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2801 arraytype_doc, /* tp_doc */
2802 0, /* tp_traverse */
2803 0, /* tp_clear */
2804 array_richcompare, /* tp_richcompare */
2805 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
2806 (getiterfunc)array_iter, /* tp_iter */
2807 0, /* tp_iternext */
2808 array_methods, /* tp_methods */
2809 0, /* tp_members */
2810 array_getsets, /* tp_getset */
2811 0, /* tp_base */
2812 0, /* tp_dict */
2813 0, /* tp_descr_get */
2814 0, /* tp_descr_set */
2815 0, /* tp_dictoffset */
2816 0, /* tp_init */
2817 PyType_GenericAlloc, /* tp_alloc */
2818 array_new, /* tp_new */
2819 PyObject_Del, /* tp_free */
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002820};
2821
Raymond Hettinger625812f2003-01-07 01:58:52 +00002822
2823/*********************** Array Iterator **************************/
2824
Brett Cannon1eb32c22014-10-10 16:26:45 -04002825/*[clinic input]
2826class array.arrayiterator "arrayiterobject *" "&PyArrayIter_Type"
2827[clinic start generated code]*/
2828/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5aefd2d74d8c8e30]*/
Raymond Hettinger625812f2003-01-07 01:58:52 +00002829
2830static PyObject *
2831array_iter(arrayobject *ao)
2832{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 arrayiterobject *it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 if (!array_Check(ao)) {
2836 PyErr_BadInternalCall();
2837 return NULL;
2838 }
Raymond Hettinger625812f2003-01-07 01:58:52 +00002839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
2841 if (it == NULL)
2842 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 Py_INCREF(ao);
2845 it->ao = ao;
2846 it->index = 0;
2847 it->getitem = ao->ob_descr->getitem;
2848 PyObject_GC_Track(it);
2849 return (PyObject *)it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002850}
2851
2852static PyObject *
Raymond Hettinger625812f2003-01-07 01:58:52 +00002853arrayiter_next(arrayiterobject *it)
2854{
Serhiy Storchakaab0d1982016-03-30 21:11:16 +03002855 arrayobject *ao;
2856
2857 assert(it != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002858 assert(PyArrayIter_Check(it));
Serhiy Storchakaab0d1982016-03-30 21:11:16 +03002859 ao = it->ao;
2860 if (ao == NULL) {
2861 return NULL;
2862 }
2863 assert(array_Check(ao));
2864 if (it->index < Py_SIZE(ao)) {
2865 return (*it->getitem)(ao, it->index++);
2866 }
2867 it->ao = NULL;
2868 Py_DECREF(ao);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002870}
2871
2872static void
2873arrayiter_dealloc(arrayiterobject *it)
2874{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 PyObject_GC_UnTrack(it);
2876 Py_XDECREF(it->ao);
2877 PyObject_GC_Del(it);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002878}
2879
2880static int
2881arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2882{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 Py_VISIT(it->ao);
2884 return 0;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002885}
2886
Brett Cannon1eb32c22014-10-10 16:26:45 -04002887/*[clinic input]
2888array.arrayiterator.__reduce__
2889
2890Return state information for pickling.
2891[clinic start generated code]*/
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002892
2893static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04002894array_arrayiterator___reduce___impl(arrayiterobject *self)
2895/*[clinic end generated code: output=7898a52e8e66e016 input=a062ea1e9951417a]*/
2896{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002897 _Py_IDENTIFIER(iter);
2898 PyObject *func = _PyEval_GetBuiltinId(&PyId_iter);
Serhiy Storchakaab0d1982016-03-30 21:11:16 +03002899 if (self->ao == NULL) {
2900 return Py_BuildValue("N(())", func);
2901 }
2902 return Py_BuildValue("N(O)n", func, self->ao, self->index);
Brett Cannon1eb32c22014-10-10 16:26:45 -04002903}
2904
2905/*[clinic input]
2906array.arrayiterator.__setstate__
2907
2908 state: object
2909 /
2910
2911Set state information for unpickling.
2912[clinic start generated code]*/
2913
2914static PyObject *
2915array_arrayiterator___setstate__(arrayiterobject *self, PyObject *state)
2916/*[clinic end generated code: output=397da9904e443cbe input=f47d5ceda19e787b]*/
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002917{
2918 Py_ssize_t index = PyLong_AsSsize_t(state);
2919 if (index == -1 && PyErr_Occurred())
2920 return NULL;
2921 if (index < 0)
2922 index = 0;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002923 else if (index > Py_SIZE(self->ao))
2924 index = Py_SIZE(self->ao); /* iterator exhausted */
2925 self->index = index;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002926 Py_RETURN_NONE;
2927}
2928
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002929static PyMethodDef arrayiter_methods[] = {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002930 ARRAY_ARRAYITERATOR___REDUCE___METHODDEF
2931 ARRAY_ARRAYITERATOR___SETSTATE___METHODDEF
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002932 {NULL, NULL} /* sentinel */
2933};
2934
Raymond Hettinger625812f2003-01-07 01:58:52 +00002935static PyTypeObject PyArrayIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 PyVarObject_HEAD_INIT(NULL, 0)
2937 "arrayiterator", /* tp_name */
2938 sizeof(arrayiterobject), /* tp_basicsize */
2939 0, /* tp_itemsize */
2940 /* methods */
2941 (destructor)arrayiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002942 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 0, /* tp_getattr */
2944 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002945 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 0, /* tp_repr */
2947 0, /* tp_as_number */
2948 0, /* tp_as_sequence */
2949 0, /* tp_as_mapping */
2950 0, /* tp_hash */
2951 0, /* tp_call */
2952 0, /* tp_str */
2953 PyObject_GenericGetAttr, /* tp_getattro */
2954 0, /* tp_setattro */
2955 0, /* tp_as_buffer */
2956 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2957 0, /* tp_doc */
2958 (traverseproc)arrayiter_traverse, /* tp_traverse */
2959 0, /* tp_clear */
2960 0, /* tp_richcompare */
2961 0, /* tp_weaklistoffset */
2962 PyObject_SelfIter, /* tp_iter */
2963 (iternextfunc)arrayiter_next, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002964 arrayiter_methods, /* tp_methods */
Raymond Hettinger625812f2003-01-07 01:58:52 +00002965};
2966
2967
2968/*********************** Install Module **************************/
2969
Martin v. Löwis99866332002-03-01 10:27:01 +00002970/* No functions in array module. */
2971static PyMethodDef a_methods[] = {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002972 ARRAY__ARRAY_RECONSTRUCTOR_METHODDEF
Martin v. Löwis99866332002-03-01 10:27:01 +00002973 {NULL, NULL, 0, NULL} /* Sentinel */
2974};
2975
Nick Coghland5cacbb2015-05-23 22:24:10 +10002976static int
2977array_modexec(PyObject *m)
Guido van Rossum778983b1993-02-19 15:55:02 +00002978{
Georg Brandl4cb0de22011-09-28 21:49:49 +02002979 char buffer[Py_ARRAY_LENGTH(descriptors)], *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002980 PyObject *typecodes;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002981 const struct arraydescr *descr;
Fred Drake0d40ba42000-02-04 20:33:49 +00002982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002983 if (PyType_Ready(&Arraytype) < 0)
Nick Coghland5cacbb2015-05-23 22:24:10 +10002984 return -1;
Victor Stinnerd2ec81a2020-02-07 09:17:07 +01002985 Py_SET_TYPE(&PyArrayIter_Type, &PyType_Type);
Fred Drakef4e34842002-04-01 03:45:06 +00002986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987 Py_INCREF((PyObject *)&Arraytype);
Marco Paolinib44ffc82019-11-15 08:42:51 +00002988 if (PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype) < 0) {
2989 Py_DECREF((PyObject *)&Arraytype);
2990 return -1;
2991 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 Py_INCREF((PyObject *)&Arraytype);
Marco Paolinib44ffc82019-11-15 08:42:51 +00002993 if (PyModule_AddObject(m, "array", (PyObject *)&Arraytype) < 0) {
2994 Py_DECREF((PyObject *)&Arraytype);
2995 return -1;
2996 }
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00002997
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002998 p = buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002999 for (descr = descriptors; descr->typecode != '\0'; descr++) {
3000 *p++ = (char)descr->typecode;
3001 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003002 typecodes = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
Marco Paolinib44ffc82019-11-15 08:42:51 +00003003 if (PyModule_AddObject(m, "typecodes", typecodes) < 0) {
3004 Py_XDECREF(typecodes);
3005 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 }
Marco Paolinib44ffc82019-11-15 08:42:51 +00003007
Nick Coghland5cacbb2015-05-23 22:24:10 +10003008 return 0;
3009}
3010
3011static PyModuleDef_Slot arrayslots[] = {
3012 {Py_mod_exec, array_modexec},
3013 {0, NULL}
3014};
3015
3016
3017static struct PyModuleDef arraymodule = {
3018 PyModuleDef_HEAD_INIT,
3019 "array",
3020 module_doc,
3021 0,
3022 a_methods,
3023 arrayslots,
3024 NULL,
3025 NULL,
3026 NULL
3027};
3028
3029
3030PyMODINIT_FUNC
3031PyInit_array(void)
3032{
3033 return PyModuleDef_Init(&arraymodule);
Guido van Rossum778983b1993-02-19 15:55:02 +00003034}