blob: 06461c380365bf2d372a1e3909f9c9b7fa25a25b [file] [log] [blame]
Guido van Rossum778983b1993-02-19 15:55:02 +00001/* Array object implementation */
2
3/* An array is a uniform list -- all items have the same type.
4 The item type is restricted to simple C types like int or float */
5
Martin v. Löwis18e16552006-02-15 17:27:45 +00006#define PY_SSIZE_T_CLEAN
Roger E. Masse2919eaa1996-12-09 20:10:36 +00007#include "Python.h"
Raymond Hettingercb87bc82004-05-31 00:35:52 +00008#include "structmember.h"
Roger E. Masse5817f8f1996-12-09 22:24:19 +00009
Guido van Rossum0c709541994-08-19 12:01:32 +000010#ifdef STDC_HEADERS
11#include <stddef.h>
Guido van Rossum7f1de831999-08-27 20:33:52 +000012#else /* !STDC_HEADERS */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000013#ifdef HAVE_SYS_TYPES_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000014#include <sys/types.h> /* For size_t */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000015#endif /* HAVE_SYS_TYPES_H */
Guido van Rossum7f1de831999-08-27 20:33:52 +000016#endif /* !STDC_HEADERS */
Guido van Rossum778983b1993-02-19 15:55:02 +000017
Brett Cannon1eb32c22014-10-10 16:26:45 -040018/*[clinic input]
Brett Cannon1eb32c22014-10-10 16:26:45 -040019module array
20[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030021/*[clinic end generated code: output=da39a3ee5e6b4b0d input=7d1b8d7f5958fd83]*/
Brett Cannon1eb32c22014-10-10 16:26:45 -040022
Guido van Rossum778983b1993-02-19 15:55:02 +000023struct arrayobject; /* Forward */
24
Tim Petersbb307342000-09-10 05:22:54 +000025/* All possible arraydescr values are defined in the vector "descriptors"
26 * below. That's defined later because the appropriate get and set
27 * functions aren't visible yet.
28 */
Guido van Rossum778983b1993-02-19 15:55:02 +000029struct arraydescr {
Victor Stinnerf8bb7d02011-09-30 00:03:59 +020030 char typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 int itemsize;
32 PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
33 int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *);
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020034 const char *formats;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 int is_integer_type;
36 int is_signed;
Guido van Rossum778983b1993-02-19 15:55:02 +000037};
38
39typedef struct arrayobject {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 PyObject_VAR_HEAD
41 char *ob_item;
42 Py_ssize_t allocated;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020043 const struct arraydescr *ob_descr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044 PyObject *weakreflist; /* List of weak references */
45 int ob_exports; /* Number of exported buffers */
Guido van Rossum778983b1993-02-19 15:55:02 +000046} arrayobject;
47
Jeremy Hylton938ace62002-07-17 16:30:39 +000048static PyTypeObject Arraytype;
Guido van Rossum778983b1993-02-19 15:55:02 +000049
Brett Cannon1eb32c22014-10-10 16:26:45 -040050typedef struct {
51 PyObject_HEAD
52 Py_ssize_t index;
53 arrayobject *ao;
54 PyObject* (*getitem)(struct arrayobject *, Py_ssize_t);
55} arrayiterobject;
56
57static PyTypeObject PyArrayIter_Type;
58
59#define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
60
Larry Hastingsdfbeb162014-10-13 10:39:41 +010061enum machine_format_code {
62 UNKNOWN_FORMAT = -1,
63 /* UNKNOWN_FORMAT is used to indicate that the machine format for an
64 * array type code cannot be interpreted. When this occurs, a list of
65 * Python objects is used to represent the content of the array
66 * instead of using the memory content of the array directly. In that
67 * case, the array_reconstructor mechanism is bypassed completely, and
68 * the standard array constructor is used instead.
69 *
70 * This is will most likely occur when the machine doesn't use IEEE
71 * floating-point numbers.
72 */
73
74 UNSIGNED_INT8 = 0,
75 SIGNED_INT8 = 1,
76 UNSIGNED_INT16_LE = 2,
77 UNSIGNED_INT16_BE = 3,
78 SIGNED_INT16_LE = 4,
79 SIGNED_INT16_BE = 5,
80 UNSIGNED_INT32_LE = 6,
81 UNSIGNED_INT32_BE = 7,
82 SIGNED_INT32_LE = 8,
83 SIGNED_INT32_BE = 9,
84 UNSIGNED_INT64_LE = 10,
85 UNSIGNED_INT64_BE = 11,
86 SIGNED_INT64_LE = 12,
87 SIGNED_INT64_BE = 13,
88 IEEE_754_FLOAT_LE = 14,
89 IEEE_754_FLOAT_BE = 15,
90 IEEE_754_DOUBLE_LE = 16,
91 IEEE_754_DOUBLE_BE = 17,
92 UTF16_LE = 18,
93 UTF16_BE = 19,
94 UTF32_LE = 20,
95 UTF32_BE = 21
96};
97#define MACHINE_FORMAT_CODE_MIN 0
98#define MACHINE_FORMAT_CODE_MAX 21
99
100
101/*
102 * Must come after arrayobject, arrayiterobject,
103 * and enum machine_code_type definitions.
104 */
Brett Cannon1eb32c22014-10-10 16:26:45 -0400105#include "clinic/arraymodule.c.h"
106
Martin v. Löwis99866332002-03-01 10:27:01 +0000107#define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
Christian Heimes90aa7642007-12-19 02:45:37 +0000108#define array_CheckExact(op) (Py_TYPE(op) == &Arraytype)
Guido van Rossum778983b1993-02-19 15:55:02 +0000109
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000110static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000111array_resize(arrayobject *self, Py_ssize_t newsize)
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 char *items;
114 size_t _new_size;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 if (self->ob_exports > 0 && newsize != Py_SIZE(self)) {
117 PyErr_SetString(PyExc_BufferError,
118 "cannot resize an array that is exporting buffers");
119 return -1;
120 }
Antoine Pitrou3ad3a0d2008-12-18 17:08:32 +0000121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 /* Bypass realloc() when a previous overallocation is large enough
123 to accommodate the newsize. If the newsize is 16 smaller than the
124 current size, then proceed with the realloc() to shrink the array.
125 */
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 if (self->allocated >= newsize &&
128 Py_SIZE(self) < newsize + 16 &&
129 self->ob_item != NULL) {
130 Py_SIZE(self) = newsize;
131 return 0;
132 }
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 if (newsize == 0) {
135 PyMem_FREE(self->ob_item);
136 self->ob_item = NULL;
137 Py_SIZE(self) = 0;
138 self->allocated = 0;
139 return 0;
140 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 /* This over-allocates proportional to the array size, making room
143 * for additional growth. The over-allocation is mild, but is
144 * enough to give linear-time amortized behavior over a long
145 * sequence of appends() in the presence of a poorly-performing
146 * system realloc().
147 * The growth pattern is: 0, 4, 8, 16, 25, 34, 46, 56, 67, 79, ...
148 * Note, the pattern starts out the same as for lists but then
149 * grows at a smaller rate so that larger arrays only overallocate
150 * by about 1/16th -- this is done because arrays are presumed to be more
151 * memory critical.
152 */
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 _new_size = (newsize >> 4) + (Py_SIZE(self) < 8 ? 3 : 7) + newsize;
155 items = self->ob_item;
156 /* XXX The following multiplication and division does not optimize away
157 like it does for lists since the size is not known at compile time */
158 if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize))
159 PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize));
160 else
161 items = NULL;
162 if (items == NULL) {
163 PyErr_NoMemory();
164 return -1;
165 }
166 self->ob_item = items;
167 Py_SIZE(self) = newsize;
168 self->allocated = _new_size;
169 return 0;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000170}
171
Tim Petersbb307342000-09-10 05:22:54 +0000172/****************************************************************************
173Get and Set functions for each type.
174A Get function takes an arrayobject* and an integer index, returning the
175array value at that index wrapped in an appropriate PyObject*.
176A Set function takes an arrayobject, integer index, and PyObject*; sets
177the array value at that index to the raw C data extracted from the PyObject*,
178and returns 0 if successful, else nonzero on failure (PyObject* not of an
179appropriate type or value).
180Note that the basic Get and Set functions do NOT check that the index is
181in bounds; that's the responsibility of the caller.
182****************************************************************************/
Guido van Rossum778983b1993-02-19 15:55:02 +0000183
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000184static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000185b_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 long x = ((char *)ap->ob_item)[i];
188 if (x >= 128)
189 x -= 256;
190 return PyLong_FromLong(x);
Guido van Rossum778983b1993-02-19 15:55:02 +0000191}
192
193static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000194b_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 short x;
197 /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
198 must use the next size up that is signed ('h') and manually do
199 the overflow checking */
200 if (!PyArg_Parse(v, "h;array item must be integer", &x))
201 return -1;
202 else if (x < -128) {
203 PyErr_SetString(PyExc_OverflowError,
204 "signed char is less than minimum");
205 return -1;
206 }
207 else if (x > 127) {
208 PyErr_SetString(PyExc_OverflowError,
209 "signed char is greater than maximum");
210 return -1;
211 }
212 if (i >= 0)
213 ((char *)ap->ob_item)[i] = (char)x;
214 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000215}
216
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000217static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000218BB_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000219{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 long x = ((unsigned char *)ap->ob_item)[i];
221 return PyLong_FromLong(x);
Guido van Rossum549ab711997-01-03 19:09:47 +0000222}
223
Fred Drake541dc3b2000-06-28 17:49:30 +0000224static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000225BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 unsigned char x;
228 /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
229 if (!PyArg_Parse(v, "b;array item must be integer", &x))
230 return -1;
231 if (i >= 0)
232 ((char *)ap->ob_item)[i] = x;
233 return 0;
Fred Drake541dc3b2000-06-28 17:49:30 +0000234}
Guido van Rossum549ab711997-01-03 19:09:47 +0000235
Martin v. Löwis99866332002-03-01 10:27:01 +0000236static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000237u_getitem(arrayobject *ap, Py_ssize_t i)
Martin v. Löwis99866332002-03-01 10:27:01 +0000238{
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200239 return PyUnicode_FromOrdinal(((Py_UNICODE *) ap->ob_item)[i]);
Martin v. Löwis99866332002-03-01 10:27:01 +0000240}
241
242static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000243u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Martin v. Löwis99866332002-03-01 10:27:01 +0000244{
Victor Stinner62bb3942012-08-06 00:46:05 +0200245 Py_UNICODE *p;
246 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +0000247
Victor Stinner62bb3942012-08-06 00:46:05 +0200248 if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 return -1;
Victor Stinner62bb3942012-08-06 00:46:05 +0200250 if (len != 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 PyErr_SetString(PyExc_TypeError,
252 "array item must be unicode character");
253 return -1;
254 }
255 if (i >= 0)
Victor Stinner62bb3942012-08-06 00:46:05 +0200256 ((Py_UNICODE *)ap->ob_item)[i] = p[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 return 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000258}
Martin v. Löwis99866332002-03-01 10:27:01 +0000259
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +0000260
Guido van Rossum549ab711997-01-03 19:09:47 +0000261static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000262h_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 return PyLong_FromLong((long) ((short *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000265}
266
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +0000267
Guido van Rossum778983b1993-02-19 15:55:02 +0000268static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000269h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 short x;
272 /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
273 if (!PyArg_Parse(v, "h;array item must be integer", &x))
274 return -1;
275 if (i >= 0)
276 ((short *)ap->ob_item)[i] = x;
277 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000278}
279
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000280static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000281HH_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 return PyLong_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000284}
285
Fred Drake541dc3b2000-06-28 17:49:30 +0000286static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000287HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Fred Drake541dc3b2000-06-28 17:49:30 +0000288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 int x;
290 /* PyArg_Parse's 'h' formatter is for a signed short, therefore
291 must use the next size up and manually do the overflow checking */
292 if (!PyArg_Parse(v, "i;array item must be integer", &x))
293 return -1;
294 else if (x < 0) {
295 PyErr_SetString(PyExc_OverflowError,
296 "unsigned short is less than minimum");
297 return -1;
298 }
299 else if (x > USHRT_MAX) {
300 PyErr_SetString(PyExc_OverflowError,
301 "unsigned short is greater than maximum");
302 return -1;
303 }
304 if (i >= 0)
305 ((short *)ap->ob_item)[i] = (short)x;
306 return 0;
Fred Drake541dc3b2000-06-28 17:49:30 +0000307}
Guido van Rossum549ab711997-01-03 19:09:47 +0000308
309static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000310i_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossume77a7571993-11-03 15:01:26 +0000311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 return PyLong_FromLong((long) ((int *)ap->ob_item)[i]);
Guido van Rossume77a7571993-11-03 15:01:26 +0000313}
314
315static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000316i_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossume77a7571993-11-03 15:01:26 +0000317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 int x;
319 /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
320 if (!PyArg_Parse(v, "i;array item must be integer", &x))
321 return -1;
322 if (i >= 0)
323 ((int *)ap->ob_item)[i] = x;
324 return 0;
Guido van Rossume77a7571993-11-03 15:01:26 +0000325}
326
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000327static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000328II_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 return PyLong_FromUnsignedLong(
331 (unsigned long) ((unsigned int *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000332}
333
orenmn964281a2017-03-09 11:35:28 +0200334static PyObject *
335get_int_unless_float(PyObject *v)
336{
337 if (PyFloat_Check(v)) {
338 PyErr_SetString(PyExc_TypeError,
339 "array item must be integer");
340 return NULL;
341 }
342 return (PyObject *)_PyLong_FromNbInt(v);
343}
344
Guido van Rossum549ab711997-01-03 19:09:47 +0000345static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000346II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000347{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 unsigned long x;
orenmn964281a2017-03-09 11:35:28 +0200349 int do_decref = 0; /* if nb_int was called */
350
351 if (!PyLong_Check(v)) {
352 v = get_int_unless_float(v);
353 if (NULL == v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 return -1;
355 }
orenmn964281a2017-03-09 11:35:28 +0200356 do_decref = 1;
357 }
358 x = PyLong_AsUnsignedLong(v);
359 if (x == (unsigned long)-1 && PyErr_Occurred()) {
360 if (do_decref) {
361 Py_DECREF(v);
362 }
363 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 }
365 if (x > UINT_MAX) {
366 PyErr_SetString(PyExc_OverflowError,
orenmn964281a2017-03-09 11:35:28 +0200367 "unsigned int is greater than maximum");
368 if (do_decref) {
369 Py_DECREF(v);
370 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 return -1;
372 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 if (i >= 0)
374 ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
orenmn964281a2017-03-09 11:35:28 +0200375
376 if (do_decref) {
377 Py_DECREF(v);
378 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 return 0;
Guido van Rossum549ab711997-01-03 19:09:47 +0000380}
381
382static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000383l_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 return PyLong_FromLong(((long *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000386}
387
388static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000389l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 long x;
392 if (!PyArg_Parse(v, "l;array item must be integer", &x))
393 return -1;
394 if (i >= 0)
395 ((long *)ap->ob_item)[i] = x;
396 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000397}
398
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000399static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000400LL_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum549ab711997-01-03 19:09:47 +0000401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
Guido van Rossum549ab711997-01-03 19:09:47 +0000403}
404
405static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000406LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum549ab711997-01-03 19:09:47 +0000407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 unsigned long x;
orenmn964281a2017-03-09 11:35:28 +0200409 int do_decref = 0; /* if nb_int was called */
410
411 if (!PyLong_Check(v)) {
412 v = get_int_unless_float(v);
413 if (NULL == v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 return -1;
415 }
orenmn964281a2017-03-09 11:35:28 +0200416 do_decref = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 }
orenmn964281a2017-03-09 11:35:28 +0200418 x = PyLong_AsUnsignedLong(v);
419 if (x == (unsigned long)-1 && PyErr_Occurred()) {
420 if (do_decref) {
421 Py_DECREF(v);
422 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 return -1;
424 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 if (i >= 0)
426 ((unsigned long *)ap->ob_item)[i] = x;
orenmn964281a2017-03-09 11:35:28 +0200427
428 if (do_decref) {
429 Py_DECREF(v);
430 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 return 0;
Guido van Rossum549ab711997-01-03 19:09:47 +0000432}
433
Meador Inge1c9f0c92011-09-20 19:55:51 -0500434static PyObject *
435q_getitem(arrayobject *ap, Py_ssize_t i)
436{
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700437 return PyLong_FromLongLong(((long long *)ap->ob_item)[i]);
Meador Inge1c9f0c92011-09-20 19:55:51 -0500438}
439
440static int
441q_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
442{
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700443 long long x;
Meador Inge1c9f0c92011-09-20 19:55:51 -0500444 if (!PyArg_Parse(v, "L;array item must be integer", &x))
445 return -1;
446 if (i >= 0)
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700447 ((long long *)ap->ob_item)[i] = x;
Meador Inge1c9f0c92011-09-20 19:55:51 -0500448 return 0;
449}
450
451static PyObject *
452QQ_getitem(arrayobject *ap, Py_ssize_t i)
453{
454 return PyLong_FromUnsignedLongLong(
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700455 ((unsigned long long *)ap->ob_item)[i]);
Meador Inge1c9f0c92011-09-20 19:55:51 -0500456}
457
458static int
459QQ_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
460{
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700461 unsigned long long x;
orenmn964281a2017-03-09 11:35:28 +0200462 int do_decref = 0; /* if nb_int was called */
463
464 if (!PyLong_Check(v)) {
465 v = get_int_unless_float(v);
466 if (NULL == v) {
Meador Inge1c9f0c92011-09-20 19:55:51 -0500467 return -1;
468 }
orenmn964281a2017-03-09 11:35:28 +0200469 do_decref = 1;
Meador Inge1c9f0c92011-09-20 19:55:51 -0500470 }
orenmn964281a2017-03-09 11:35:28 +0200471 x = PyLong_AsUnsignedLongLong(v);
472 if (x == (unsigned long long)-1 && PyErr_Occurred()) {
473 if (do_decref) {
474 Py_DECREF(v);
475 }
476 return -1;
477 }
Meador Inge1c9f0c92011-09-20 19:55:51 -0500478 if (i >= 0)
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700479 ((unsigned long long *)ap->ob_item)[i] = x;
orenmn964281a2017-03-09 11:35:28 +0200480
481 if (do_decref) {
482 Py_DECREF(v);
483 }
Meador Inge1c9f0c92011-09-20 19:55:51 -0500484 return 0;
485}
Meador Inge1c9f0c92011-09-20 19:55:51 -0500486
Guido van Rossum549ab711997-01-03 19:09:47 +0000487static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000488f_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000489{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000491}
492
493static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000494f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000495{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 float x;
497 if (!PyArg_Parse(v, "f;array item must be float", &x))
498 return -1;
499 if (i >= 0)
500 ((float *)ap->ob_item)[i] = x;
501 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000502}
503
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000504static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000505d_getitem(arrayobject *ap, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
Guido van Rossum778983b1993-02-19 15:55:02 +0000508}
509
510static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000511d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 double x;
514 if (!PyArg_Parse(v, "d;array item must be float", &x))
515 return -1;
516 if (i >= 0)
517 ((double *)ap->ob_item)[i] = x;
518 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000519}
520
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000521
Alexandre Vassalottiad077152009-07-15 17:49:23 +0000522/* Description of types.
523 *
524 * Don't forget to update typecode_to_mformat_code() if you add a new
525 * typecode.
526 */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200527static const struct arraydescr descriptors[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 {'b', 1, b_getitem, b_setitem, "b", 1, 1},
529 {'B', 1, BB_getitem, BB_setitem, "B", 1, 0},
Victor Stinner62bb3942012-08-06 00:46:05 +0200530 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem, "u", 0, 0},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 {'h', sizeof(short), h_getitem, h_setitem, "h", 1, 1},
532 {'H', sizeof(short), HH_getitem, HH_setitem, "H", 1, 0},
533 {'i', sizeof(int), i_getitem, i_setitem, "i", 1, 1},
534 {'I', sizeof(int), II_getitem, II_setitem, "I", 1, 0},
535 {'l', sizeof(long), l_getitem, l_setitem, "l", 1, 1},
536 {'L', sizeof(long), LL_getitem, LL_setitem, "L", 1, 0},
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700537 {'q', sizeof(long long), q_getitem, q_setitem, "q", 1, 1},
538 {'Q', sizeof(long long), QQ_getitem, QQ_setitem, "Q", 1, 0},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 {'f', sizeof(float), f_getitem, f_setitem, "f", 0, 0},
540 {'d', sizeof(double), d_getitem, d_setitem, "d", 0, 0},
541 {'\0', 0, 0, 0, 0, 0, 0} /* Sentinel */
Guido van Rossum778983b1993-02-19 15:55:02 +0000542};
Tim Petersbb307342000-09-10 05:22:54 +0000543
544/****************************************************************************
545Implementations of array object methods.
546****************************************************************************/
Brett Cannon1eb32c22014-10-10 16:26:45 -0400547/*[clinic input]
548class array.array "arrayobject *" "&Arraytype"
549[clinic start generated code]*/
550/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ad43d37e942a8854]*/
Guido van Rossum778983b1993-02-19 15:55:02 +0000551
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000552static PyObject *
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200553newarrayobject(PyTypeObject *type, Py_ssize_t size, const struct arraydescr *descr)
Guido van Rossum778983b1993-02-19 15:55:02 +0000554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 arrayobject *op;
556 size_t nbytes;
Martin v. Löwis99866332002-03-01 10:27:01 +0000557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 if (size < 0) {
559 PyErr_BadInternalCall();
560 return NULL;
561 }
Martin v. Löwis99866332002-03-01 10:27:01 +0000562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 /* Check for overflow */
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100564 if (size > PY_SSIZE_T_MAX / descr->itemsize) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 return PyErr_NoMemory();
566 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100567 nbytes = size * descr->itemsize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 op = (arrayobject *) type->tp_alloc(type, 0);
569 if (op == NULL) {
570 return NULL;
571 }
572 op->ob_descr = descr;
573 op->allocated = size;
574 op->weakreflist = NULL;
575 Py_SIZE(op) = size;
576 if (size <= 0) {
577 op->ob_item = NULL;
578 }
579 else {
580 op->ob_item = PyMem_NEW(char, nbytes);
581 if (op->ob_item == NULL) {
582 Py_DECREF(op);
583 return PyErr_NoMemory();
584 }
585 }
586 op->ob_exports = 0;
587 return (PyObject *) op;
Guido van Rossum778983b1993-02-19 15:55:02 +0000588}
589
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000590static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000591getarrayitem(PyObject *op, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000592{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200593 arrayobject *ap;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 assert(array_Check(op));
595 ap = (arrayobject *)op;
596 assert(i>=0 && i<Py_SIZE(ap));
597 return (*ap->ob_descr->getitem)(ap, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000598}
599
600static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000601ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 char *items;
604 Py_ssize_t n = Py_SIZE(self);
605 if (v == NULL) {
606 PyErr_BadInternalCall();
607 return -1;
608 }
609 if ((*self->ob_descr->setitem)(self, -1, v) < 0)
610 return -1;
Raymond Hettinger6e2ee862004-03-14 04:37:50 +0000611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 if (array_resize(self, n+1) == -1)
613 return -1;
614 items = self->ob_item;
615 if (where < 0) {
616 where += n;
617 if (where < 0)
618 where = 0;
619 }
620 if (where > n)
621 where = n;
622 /* appends don't need to call memmove() */
623 if (where != n)
624 memmove(items + (where+1)*self->ob_descr->itemsize,
625 items + where*self->ob_descr->itemsize,
626 (n-where)*self->ob_descr->itemsize);
627 return (*self->ob_descr->setitem)(self, where, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000628}
629
Guido van Rossum778983b1993-02-19 15:55:02 +0000630/* Methods */
631
632static void
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000633array_dealloc(arrayobject *op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 if (op->weakreflist != NULL)
636 PyObject_ClearWeakRefs((PyObject *) op);
637 if (op->ob_item != NULL)
638 PyMem_DEL(op->ob_item);
639 Py_TYPE(op)->tp_free((PyObject *)op);
Guido van Rossum778983b1993-02-19 15:55:02 +0000640}
641
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000642static PyObject *
643array_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossum778983b1993-02-19 15:55:02 +0000644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 arrayobject *va, *wa;
646 PyObject *vi = NULL;
647 PyObject *wi = NULL;
648 Py_ssize_t i, k;
649 PyObject *res;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000650
Brian Curtindfc80e32011-08-10 20:28:54 -0500651 if (!array_Check(v) || !array_Check(w))
652 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 va = (arrayobject *)v;
655 wa = (arrayobject *)w;
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 if (Py_SIZE(va) != Py_SIZE(wa) && (op == Py_EQ || op == Py_NE)) {
658 /* Shortcut: if the lengths differ, the arrays differ */
659 if (op == Py_EQ)
660 res = Py_False;
661 else
662 res = Py_True;
663 Py_INCREF(res);
664 return res;
665 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 /* Search for the first index where items are different */
668 k = 1;
669 for (i = 0; i < Py_SIZE(va) && i < Py_SIZE(wa); i++) {
670 vi = getarrayitem(v, i);
671 wi = getarrayitem(w, i);
672 if (vi == NULL || wi == NULL) {
673 Py_XDECREF(vi);
674 Py_XDECREF(wi);
675 return NULL;
676 }
677 k = PyObject_RichCompareBool(vi, wi, Py_EQ);
678 if (k == 0)
679 break; /* Keeping vi and wi alive! */
680 Py_DECREF(vi);
681 Py_DECREF(wi);
682 if (k < 0)
683 return NULL;
684 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 if (k) {
687 /* No more items to compare -- compare sizes */
688 Py_ssize_t vs = Py_SIZE(va);
689 Py_ssize_t ws = Py_SIZE(wa);
690 int cmp;
691 switch (op) {
692 case Py_LT: cmp = vs < ws; break;
693 case Py_LE: cmp = vs <= ws; break;
694 case Py_EQ: cmp = vs == ws; break;
695 case Py_NE: cmp = vs != ws; break;
696 case Py_GT: cmp = vs > ws; break;
697 case Py_GE: cmp = vs >= ws; break;
698 default: return NULL; /* cannot happen */
699 }
700 if (cmp)
701 res = Py_True;
702 else
703 res = Py_False;
704 Py_INCREF(res);
705 return res;
706 }
Guido van Rossum9d19cb82001-01-18 01:02:55 +0000707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 /* We have an item that differs. First, shortcuts for EQ/NE */
709 if (op == Py_EQ) {
710 Py_INCREF(Py_False);
711 res = Py_False;
712 }
713 else if (op == Py_NE) {
714 Py_INCREF(Py_True);
715 res = Py_True;
716 }
717 else {
718 /* Compare the final item again using the proper operator */
719 res = PyObject_RichCompare(vi, wi, op);
720 }
721 Py_DECREF(vi);
722 Py_DECREF(wi);
723 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +0000724}
725
Martin v. Löwis18e16552006-02-15 17:27:45 +0000726static Py_ssize_t
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000727array_length(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +0000728{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 return Py_SIZE(a);
Guido van Rossum778983b1993-02-19 15:55:02 +0000730}
731
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000732static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000733array_item(arrayobject *a, Py_ssize_t i)
Guido van Rossum778983b1993-02-19 15:55:02 +0000734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 if (i < 0 || i >= Py_SIZE(a)) {
736 PyErr_SetString(PyExc_IndexError, "array index out of range");
737 return NULL;
738 }
739 return getarrayitem((PyObject *)a, i);
Guido van Rossum778983b1993-02-19 15:55:02 +0000740}
741
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000742static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000743array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 arrayobject *np;
746 if (ilow < 0)
747 ilow = 0;
748 else if (ilow > Py_SIZE(a))
749 ilow = Py_SIZE(a);
750 if (ihigh < 0)
751 ihigh = 0;
752 if (ihigh < ilow)
753 ihigh = ilow;
754 else if (ihigh > Py_SIZE(a))
755 ihigh = Py_SIZE(a);
756 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
757 if (np == NULL)
758 return NULL;
Martin Panterbe8da9c2016-09-07 11:04:41 +0000759 if (ihigh > ilow) {
760 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
761 (ihigh-ilow) * a->ob_descr->itemsize);
762 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000764}
765
Brett Cannon1eb32c22014-10-10 16:26:45 -0400766
767/*[clinic input]
768array.array.__copy__
769
770Return a copy of the array.
771[clinic start generated code]*/
772
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000773static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -0400774array_array___copy___impl(arrayobject *self)
775/*[clinic end generated code: output=dec7c3f925d9619e input=ad1ee5b086965f09]*/
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000776{
Brett Cannon1eb32c22014-10-10 16:26:45 -0400777 return array_slice(self, 0, Py_SIZE(self));
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000778}
779
Brett Cannon1eb32c22014-10-10 16:26:45 -0400780/*[clinic input]
781array.array.__deepcopy__
782
783 unused: object
784 /
785
786Return a copy of the array.
787[clinic start generated code]*/
788
789static PyObject *
790array_array___deepcopy__(arrayobject *self, PyObject *unused)
791/*[clinic end generated code: output=1ec748d8e14a9faa input=2405ecb4933748c4]*/
792{
793 return array_array___copy___impl(self);
794}
Raymond Hettinger3aa82c02004-03-13 18:18:51 +0000795
796static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +0000797array_concat(arrayobject *a, PyObject *bb)
Guido van Rossum778983b1993-02-19 15:55:02 +0000798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 Py_ssize_t size;
800 arrayobject *np;
801 if (!array_Check(bb)) {
802 PyErr_Format(PyExc_TypeError,
803 "can only append array (not \"%.200s\") to array",
804 Py_TYPE(bb)->tp_name);
805 return NULL;
806 }
Guido van Rossum778983b1993-02-19 15:55:02 +0000807#define b ((arrayobject *)bb)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 if (a->ob_descr != b->ob_descr) {
809 PyErr_BadArgument();
810 return NULL;
811 }
812 if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) {
813 return PyErr_NoMemory();
814 }
815 size = Py_SIZE(a) + Py_SIZE(b);
816 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
817 if (np == NULL) {
818 return NULL;
819 }
Martin Panterbe8da9c2016-09-07 11:04:41 +0000820 if (Py_SIZE(a) > 0) {
821 memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize);
822 }
823 if (Py_SIZE(b) > 0) {
824 memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize,
825 b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
826 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000828#undef b
829}
830
Roger E. Masse2919eaa1996-12-09 20:10:36 +0000831static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000832array_repeat(arrayobject *a, Py_ssize_t n)
Guido van Rossum778983b1993-02-19 15:55:02 +0000833{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 Py_ssize_t size;
835 arrayobject *np;
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000836 Py_ssize_t oldbytes, newbytes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 if (n < 0)
838 n = 0;
839 if ((Py_SIZE(a) != 0) && (n > PY_SSIZE_T_MAX / Py_SIZE(a))) {
840 return PyErr_NoMemory();
841 }
842 size = Py_SIZE(a) * n;
843 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
844 if (np == NULL)
845 return NULL;
Martin Panterbe8da9c2016-09-07 11:04:41 +0000846 if (size == 0)
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000847 return (PyObject *)np;
848 oldbytes = Py_SIZE(a) * a->ob_descr->itemsize;
849 newbytes = oldbytes * n;
850 /* this follows the code in unicode_repeat */
851 if (oldbytes == 1) {
852 memset(np->ob_item, a->ob_item[0], newbytes);
853 } else {
854 Py_ssize_t done = oldbytes;
Christian Heimesf051e432016-09-13 20:22:02 +0200855 memcpy(np->ob_item, a->ob_item, oldbytes);
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000856 while (done < newbytes) {
857 Py_ssize_t ncopy = (done <= newbytes-done) ? done : newbytes-done;
Christian Heimesf051e432016-09-13 20:22:02 +0200858 memcpy(np->ob_item+done, np->ob_item, ncopy);
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000859 done += ncopy;
860 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 }
Georg Brandlc29cc6a2010-12-04 11:02:04 +0000862 return (PyObject *)np;
Guido van Rossum778983b1993-02-19 15:55:02 +0000863}
864
865static int
Martin Panter996d72b2016-07-25 02:21:14 +0000866array_del_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Guido van Rossum778983b1993-02-19 15:55:02 +0000867{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 char *item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 Py_ssize_t d; /* Change in size */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 if (ilow < 0)
871 ilow = 0;
872 else if (ilow > Py_SIZE(a))
873 ilow = Py_SIZE(a);
874 if (ihigh < 0)
875 ihigh = 0;
876 if (ihigh < ilow)
877 ihigh = ilow;
878 else if (ihigh > Py_SIZE(a))
879 ihigh = Py_SIZE(a);
880 item = a->ob_item;
Martin Panter996d72b2016-07-25 02:21:14 +0000881 d = ihigh-ilow;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 /* Issue #4509: If the array has exported buffers and the slice
883 assignment would change the size of the array, fail early to make
884 sure we don't modify it. */
885 if (d != 0 && a->ob_exports > 0) {
886 PyErr_SetString(PyExc_BufferError,
887 "cannot resize an array that is exporting buffers");
888 return -1;
889 }
Martin Panter996d72b2016-07-25 02:21:14 +0000890 if (d > 0) { /* Delete d items */
891 memmove(item + (ihigh-d)*a->ob_descr->itemsize,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 item + ihigh*a->ob_descr->itemsize,
893 (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
Martin Panter996d72b2016-07-25 02:21:14 +0000894 if (array_resize(a, Py_SIZE(a) - d) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 return -1;
896 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 return 0;
Guido van Rossum778983b1993-02-19 15:55:02 +0000898}
899
900static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000901array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000902{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 if (i < 0 || i >= Py_SIZE(a)) {
904 PyErr_SetString(PyExc_IndexError,
905 "array assignment index out of range");
906 return -1;
907 }
908 if (v == NULL)
Martin Panter996d72b2016-07-25 02:21:14 +0000909 return array_del_slice(a, i, i+1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 return (*a->ob_descr->setitem)(a, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000911}
912
913static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000914setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +0000915{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 assert(array_Check(a));
917 return array_ass_item((arrayobject *)a, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +0000918}
919
Martin v. Löwis99866332002-03-01 10:27:01 +0000920static int
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000921array_iter_extend(arrayobject *self, PyObject *bb)
922{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 PyObject *it, *v;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 it = PyObject_GetIter(bb);
926 if (it == NULL)
927 return -1;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 while ((v = PyIter_Next(it)) != NULL) {
Mark Dickinson346f0af2010-08-06 09:36:57 +0000930 if (ins1(self, Py_SIZE(self), v) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 Py_DECREF(v);
932 Py_DECREF(it);
933 return -1;
934 }
935 Py_DECREF(v);
936 }
937 Py_DECREF(it);
938 if (PyErr_Occurred())
939 return -1;
940 return 0;
Raymond Hettinger49f9bd12004-03-14 05:43:59 +0000941}
942
943static int
Martin v. Löwis99866332002-03-01 10:27:01 +0000944array_do_extend(arrayobject *self, PyObject *bb)
945{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 Py_ssize_t size, oldsize, bbsize;
Martin v. Löwis99866332002-03-01 10:27:01 +0000947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 if (!array_Check(bb))
949 return array_iter_extend(self, bb);
950#define b ((arrayobject *)bb)
951 if (self->ob_descr != b->ob_descr) {
952 PyErr_SetString(PyExc_TypeError,
953 "can only extend with array of same kind");
954 return -1;
955 }
956 if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) ||
957 ((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
958 PyErr_NoMemory();
959 return -1;
960 }
961 oldsize = Py_SIZE(self);
962 /* Get the size of bb before resizing the array since bb could be self. */
963 bbsize = Py_SIZE(bb);
964 size = oldsize + Py_SIZE(b);
965 if (array_resize(self, size) == -1)
966 return -1;
Martin Panterbe8da9c2016-09-07 11:04:41 +0000967 if (bbsize > 0) {
968 memcpy(self->ob_item + oldsize * self->ob_descr->itemsize,
969 b->ob_item, bbsize * b->ob_descr->itemsize);
970 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971
972 return 0;
Martin v. Löwis99866332002-03-01 10:27:01 +0000973#undef b
974}
975
976static PyObject *
977array_inplace_concat(arrayobject *self, PyObject *bb)
978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 if (!array_Check(bb)) {
980 PyErr_Format(PyExc_TypeError,
981 "can only extend array with array (not \"%.200s\")",
982 Py_TYPE(bb)->tp_name);
983 return NULL;
984 }
985 if (array_do_extend(self, bb) == -1)
986 return NULL;
987 Py_INCREF(self);
988 return (PyObject *)self;
Martin v. Löwis99866332002-03-01 10:27:01 +0000989}
990
991static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000992array_inplace_repeat(arrayobject *self, Py_ssize_t n)
Martin v. Löwis99866332002-03-01 10:27:01 +0000993{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 char *items, *p;
995 Py_ssize_t size, i;
Martin v. Löwis99866332002-03-01 10:27:01 +0000996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 if (Py_SIZE(self) > 0) {
998 if (n < 0)
999 n = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 if ((self->ob_descr->itemsize != 0) &&
1001 (Py_SIZE(self) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
1002 return PyErr_NoMemory();
1003 }
1004 size = Py_SIZE(self) * self->ob_descr->itemsize;
1005 if (n > 0 && size > PY_SSIZE_T_MAX / n) {
1006 return PyErr_NoMemory();
1007 }
1008 if (array_resize(self, n * Py_SIZE(self)) == -1)
1009 return NULL;
1010 items = p = self->ob_item;
1011 for (i = 1; i < n; i++) {
1012 p += size;
1013 memcpy(p, items, size);
1014 }
1015 }
1016 Py_INCREF(self);
1017 return (PyObject *)self;
Martin v. Löwis99866332002-03-01 10:27:01 +00001018}
1019
1020
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001021static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001022ins(arrayobject *self, Py_ssize_t where, PyObject *v)
Guido van Rossum778983b1993-02-19 15:55:02 +00001023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 if (ins1(self, where, v) != 0)
1025 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001026 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001027}
1028
Brett Cannon1eb32c22014-10-10 16:26:45 -04001029/*[clinic input]
1030array.array.count
1031
1032 v: object
1033 /
1034
1035Return number of occurrences of v in the array.
1036[clinic start generated code]*/
1037
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001038static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001039array_array_count(arrayobject *self, PyObject *v)
1040/*[clinic end generated code: output=3dd3624bf7135a3a input=d9bce9d65e39d1f5]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001041{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 Py_ssize_t count = 0;
1043 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 for (i = 0; i < Py_SIZE(self); i++) {
Victor Stinner0b142e22013-07-17 23:01:30 +02001046 PyObject *selfi;
1047 int cmp;
1048
1049 selfi = getarrayitem((PyObject *)self, i);
1050 if (selfi == NULL)
1051 return NULL;
1052 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 Py_DECREF(selfi);
1054 if (cmp > 0)
1055 count++;
1056 else if (cmp < 0)
1057 return NULL;
1058 }
1059 return PyLong_FromSsize_t(count);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001060}
1061
Brett Cannon1eb32c22014-10-10 16:26:45 -04001062
1063/*[clinic input]
1064array.array.index
1065
1066 v: object
1067 /
1068
1069Return index of first occurrence of v in the array.
1070[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001071
1072static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001073array_array_index(arrayobject *self, PyObject *v)
1074/*[clinic end generated code: output=d48498d325602167 input=cf619898c6649d08]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001075{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 for (i = 0; i < Py_SIZE(self); i++) {
Victor Stinner0b142e22013-07-17 23:01:30 +02001079 PyObject *selfi;
1080 int cmp;
1081
1082 selfi = getarrayitem((PyObject *)self, i);
1083 if (selfi == NULL)
1084 return NULL;
1085 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 Py_DECREF(selfi);
1087 if (cmp > 0) {
1088 return PyLong_FromLong((long)i);
1089 }
1090 else if (cmp < 0)
1091 return NULL;
1092 }
Jim Fasarakis-Hilliarda4095ef2017-05-29 20:43:39 +03001093 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in array");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 return NULL;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001095}
1096
Raymond Hettinger625812f2003-01-07 01:58:52 +00001097static int
1098array_contains(arrayobject *self, PyObject *v)
1099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 Py_ssize_t i;
1101 int cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +00001102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) {
1104 PyObject *selfi = getarrayitem((PyObject *)self, i);
Victor Stinner0b142e22013-07-17 23:01:30 +02001105 if (selfi == NULL)
Victor Stinner4755bea2013-07-18 01:12:35 +02001106 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
1108 Py_DECREF(selfi);
1109 }
1110 return cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +00001111}
1112
Brett Cannon1eb32c22014-10-10 16:26:45 -04001113/*[clinic input]
1114array.array.remove
1115
1116 v: object
1117 /
1118
1119Remove the first occurrence of v in the array.
1120[clinic start generated code]*/
1121
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001122static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001123array_array_remove(arrayobject *self, PyObject *v)
1124/*[clinic end generated code: output=bef06be9fdf9dceb input=0b1e5aed25590027]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 int i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 for (i = 0; i < Py_SIZE(self); i++) {
Victor Stinner0b142e22013-07-17 23:01:30 +02001129 PyObject *selfi;
1130 int cmp;
1131
1132 selfi = getarrayitem((PyObject *)self,i);
1133 if (selfi == NULL)
1134 return NULL;
1135 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 Py_DECREF(selfi);
1137 if (cmp > 0) {
Martin Panter996d72b2016-07-25 02:21:14 +00001138 if (array_del_slice(self, i, i+1) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001140 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 }
1142 else if (cmp < 0)
1143 return NULL;
1144 }
Jim Fasarakis-Hilliarda4095ef2017-05-29 20:43:39 +03001145 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in array");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 return NULL;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001147}
1148
Brett Cannon1eb32c22014-10-10 16:26:45 -04001149/*[clinic input]
1150array.array.pop
1151
1152 i: Py_ssize_t = -1
1153 /
1154
1155Return the i-th element and delete it from the array.
1156
1157i defaults to -1.
1158[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001159
1160static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001161array_array_pop_impl(arrayobject *self, Py_ssize_t i)
1162/*[clinic end generated code: output=bc1f0c54fe5308e4 input=8e5feb4c1a11cd44]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001163{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 PyObject *v;
Brett Cannon1eb32c22014-10-10 16:26:45 -04001165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 if (Py_SIZE(self) == 0) {
1167 /* Special-case most common failure cause */
1168 PyErr_SetString(PyExc_IndexError, "pop from empty array");
1169 return NULL;
1170 }
1171 if (i < 0)
1172 i += Py_SIZE(self);
1173 if (i < 0 || i >= Py_SIZE(self)) {
1174 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1175 return NULL;
1176 }
Victor Stinner0b142e22013-07-17 23:01:30 +02001177 v = getarrayitem((PyObject *)self, i);
1178 if (v == NULL)
1179 return NULL;
Martin Panter996d72b2016-07-25 02:21:14 +00001180 if (array_del_slice(self, i, i+1) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 Py_DECREF(v);
1182 return NULL;
1183 }
1184 return v;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001185}
1186
Brett Cannon1eb32c22014-10-10 16:26:45 -04001187/*[clinic input]
1188array.array.extend
1189
1190 bb: object
1191 /
1192
1193Append items to the end of the array.
1194[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001195
1196static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001197array_array_extend(arrayobject *self, PyObject *bb)
1198/*[clinic end generated code: output=bbddbc8e8bef871d input=43be86aba5c31e44]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 if (array_do_extend(self, bb) == -1)
1201 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001202 Py_RETURN_NONE;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001203}
1204
Brett Cannon1eb32c22014-10-10 16:26:45 -04001205/*[clinic input]
1206array.array.insert
1207
1208 i: Py_ssize_t
1209 v: object
1210 /
1211
1212Insert a new item v into the array before position i.
1213[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001214
1215static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001216array_array_insert_impl(arrayobject *self, Py_ssize_t i, PyObject *v)
1217/*[clinic end generated code: output=5a3648e278348564 input=5577d1b4383e9313]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 return ins(self, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001220}
1221
Brett Cannon1eb32c22014-10-10 16:26:45 -04001222/*[clinic input]
1223array.array.buffer_info
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001224
Brett Cannon1eb32c22014-10-10 16:26:45 -04001225Return a tuple (address, length) giving the current memory address and the length in items of the buffer used to hold array's contents.
1226
1227The length should be multiplied by the itemsize attribute to calculate
1228the buffer length in bytes.
1229[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001230
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001231static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001232array_array_buffer_info_impl(arrayobject *self)
1233/*[clinic end generated code: output=9b2a4ec3ae7e98e7 input=a58bae5c6e1ac6a6]*/
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001234{
Victor Stinner541067a2013-11-14 01:27:12 +01001235 PyObject *retval = NULL, *v;
1236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 retval = PyTuple_New(2);
1238 if (!retval)
1239 return NULL;
Fred Drake541dc3b2000-06-28 17:49:30 +00001240
Victor Stinner541067a2013-11-14 01:27:12 +01001241 v = PyLong_FromVoidPtr(self->ob_item);
1242 if (v == NULL) {
1243 Py_DECREF(retval);
1244 return NULL;
1245 }
1246 PyTuple_SET_ITEM(retval, 0, v);
1247
Serhiy Storchaka9e941d62016-06-23 23:55:34 +03001248 v = PyLong_FromSsize_t(Py_SIZE(self));
Victor Stinner541067a2013-11-14 01:27:12 +01001249 if (v == NULL) {
1250 Py_DECREF(retval);
1251 return NULL;
1252 }
1253 PyTuple_SET_ITEM(retval, 1, v);
Fred Drake541dc3b2000-06-28 17:49:30 +00001254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 return retval;
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001256}
1257
Brett Cannon1eb32c22014-10-10 16:26:45 -04001258/*[clinic input]
1259array.array.append
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001260
Brett Cannon1eb32c22014-10-10 16:26:45 -04001261 v: object
1262 /
1263
1264Append new value v to the end of the array.
1265[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001266
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001267static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001268array_array_append(arrayobject *self, PyObject *v)
1269/*[clinic end generated code: output=745a0669bf8db0e2 input=0b98d9d78e78f0fa]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001270{
Mark Dickinson346f0af2010-08-06 09:36:57 +00001271 return ins(self, Py_SIZE(self), v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001272}
1273
Brett Cannon1eb32c22014-10-10 16:26:45 -04001274/*[clinic input]
1275array.array.byteswap
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001276
Brett Cannon1eb32c22014-10-10 16:26:45 -04001277Byteswap all items of the array.
1278
1279If the items in the array are not 1, 2, 4, or 8 bytes in size, RuntimeError is
1280raised.
1281[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001282
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001283static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001284array_array_byteswap_impl(arrayobject *self)
1285/*[clinic end generated code: output=5f8236cbdf0d90b5 input=6a85591b950a0186]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 char *p;
1288 Py_ssize_t i;
Fred Drakebf272981999-12-03 17:15:30 +00001289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 switch (self->ob_descr->itemsize) {
1291 case 1:
1292 break;
1293 case 2:
1294 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) {
1295 char p0 = p[0];
1296 p[0] = p[1];
1297 p[1] = p0;
1298 }
1299 break;
1300 case 4:
1301 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 4) {
1302 char p0 = p[0];
1303 char p1 = p[1];
1304 p[0] = p[3];
1305 p[1] = p[2];
1306 p[2] = p1;
1307 p[3] = p0;
1308 }
1309 break;
1310 case 8:
1311 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) {
1312 char p0 = p[0];
1313 char p1 = p[1];
1314 char p2 = p[2];
1315 char p3 = p[3];
1316 p[0] = p[7];
1317 p[1] = p[6];
1318 p[2] = p[5];
1319 p[3] = p[4];
1320 p[4] = p3;
1321 p[5] = p2;
1322 p[6] = p1;
1323 p[7] = p0;
1324 }
1325 break;
1326 default:
1327 PyErr_SetString(PyExc_RuntimeError,
1328 "don't know how to byteswap this array type");
1329 return NULL;
1330 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001331 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001332}
1333
Brett Cannon1eb32c22014-10-10 16:26:45 -04001334/*[clinic input]
1335array.array.reverse
1336
1337Reverse the order of the items in the array.
1338[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001339
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001340static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001341array_array_reverse_impl(arrayobject *self)
1342/*[clinic end generated code: output=c04868b36f6f4089 input=cd904f01b27d966a]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001343{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001344 Py_ssize_t itemsize = self->ob_descr->itemsize;
1345 char *p, *q;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 /* little buffer to hold items while swapping */
1347 char tmp[256]; /* 8 is probably enough -- but why skimp */
1348 assert((size_t)itemsize <= sizeof(tmp));
Guido van Rossume77a7571993-11-03 15:01:26 +00001349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 if (Py_SIZE(self) > 1) {
1351 for (p = self->ob_item,
1352 q = self->ob_item + (Py_SIZE(self) - 1)*itemsize;
1353 p < q;
1354 p += itemsize, q -= itemsize) {
1355 /* memory areas guaranteed disjoint, so memcpy
1356 * is safe (& memmove may be slower).
1357 */
1358 memcpy(tmp, p, itemsize);
1359 memcpy(p, q, itemsize);
1360 memcpy(q, tmp, itemsize);
1361 }
1362 }
Tim Petersbb307342000-09-10 05:22:54 +00001363
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001364 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001365}
Guido van Rossume77a7571993-11-03 15:01:26 +00001366
Brett Cannon1eb32c22014-10-10 16:26:45 -04001367/*[clinic input]
1368array.array.fromfile
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001369
Brett Cannon1eb32c22014-10-10 16:26:45 -04001370 f: object
1371 n: Py_ssize_t
1372 /
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001373
Brett Cannon1eb32c22014-10-10 16:26:45 -04001374Read n objects from the file object f and append them to the end of the array.
1375[clinic start generated code]*/
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001376
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001377static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001378array_array_fromfile_impl(arrayobject *self, PyObject *f, Py_ssize_t n)
1379/*[clinic end generated code: output=ec9f600e10f53510 input=e188afe8e58adf40]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001380{
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03001381 PyObject *b, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 Py_ssize_t itemsize = self->ob_descr->itemsize;
Brett Cannon1eb32c22014-10-10 16:26:45 -04001383 Py_ssize_t nbytes;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001384 _Py_IDENTIFIER(read);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 int not_enough_bytes;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001386
Mark Dickinsonc04ddff2012-10-06 18:04:49 +01001387 if (n < 0) {
1388 PyErr_SetString(PyExc_ValueError, "negative count");
1389 return NULL;
1390 }
1391 if (n > PY_SSIZE_T_MAX / itemsize) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 PyErr_NoMemory();
1393 return NULL;
1394 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +01001395 nbytes = n * itemsize;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001396
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001397 b = _PyObject_CallMethodId(f, &PyId_read, "n", nbytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 if (b == NULL)
1399 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 if (!PyBytes_Check(b)) {
1402 PyErr_SetString(PyExc_TypeError,
1403 "read() didn't return bytes");
1404 Py_DECREF(b);
1405 return NULL;
1406 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 not_enough_bytes = (PyBytes_GET_SIZE(b) != nbytes);
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001409
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03001410 res = array_array_frombytes(self, b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 Py_DECREF(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 if (res == NULL)
1413 return NULL;
Hirokazu Yamamoto54d0df62009-03-06 03:04:07 +00001414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 if (not_enough_bytes) {
1416 PyErr_SetString(PyExc_EOFError,
1417 "read() didn't return enough bytes");
1418 Py_DECREF(res);
1419 return NULL;
1420 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +00001423}
1424
Brett Cannon1eb32c22014-10-10 16:26:45 -04001425/*[clinic input]
1426array.array.tofile
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001427
Brett Cannon1eb32c22014-10-10 16:26:45 -04001428 f: object
1429 /
1430
1431Write all items (as machine values) to the file object f.
1432[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001433
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001434static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001435array_array_tofile(arrayobject *self, PyObject *f)
1436/*[clinic end generated code: output=3a2cfa8128df0777 input=b0669a484aab0831]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize;
1439 /* Write 64K blocks at a time */
1440 /* XXX Make the block size settable */
1441 int BLOCKSIZE = 64*1024;
1442 Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE;
1443 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 if (Py_SIZE(self) == 0)
1446 goto done;
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 for (i = 0; i < nblocks; i++) {
1449 char* ptr = self->ob_item + i*BLOCKSIZE;
1450 Py_ssize_t size = BLOCKSIZE;
1451 PyObject *bytes, *res;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001452 _Py_IDENTIFIER(write);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 if (i*BLOCKSIZE + size > nbytes)
1455 size = nbytes - i*BLOCKSIZE;
1456 bytes = PyBytes_FromStringAndSize(ptr, size);
1457 if (bytes == NULL)
1458 return NULL;
Victor Stinner55ba38a2016-12-09 16:09:30 +01001459 res = _PyObject_CallMethodIdObjArgs(f, &PyId_write, bytes, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 Py_DECREF(bytes);
1461 if (res == NULL)
1462 return NULL;
1463 Py_DECREF(res); /* drop write result */
1464 }
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001465
1466 done:
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001467 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001468}
1469
Brett Cannon1eb32c22014-10-10 16:26:45 -04001470/*[clinic input]
1471array.array.fromlist
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001472
Brett Cannon1eb32c22014-10-10 16:26:45 -04001473 list: object
1474 /
1475
1476Append items to array from list.
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_fromlist(arrayobject *self, PyObject *list)
1481/*[clinic end generated code: output=26411c2d228a3e3f input=be2605a96c49680f]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001482{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 Py_ssize_t n;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 if (!PyList_Check(list)) {
1486 PyErr_SetString(PyExc_TypeError, "arg must be list");
1487 return NULL;
1488 }
1489 n = PyList_Size(list);
1490 if (n > 0) {
1491 Py_ssize_t i, old_size;
1492 old_size = Py_SIZE(self);
1493 if (array_resize(self, old_size + n) == -1)
1494 return NULL;
1495 for (i = 0; i < n; i++) {
1496 PyObject *v = PyList_GetItem(list, i);
1497 if ((*self->ob_descr->setitem)(self,
1498 Py_SIZE(self) - n + i, v) != 0) {
1499 array_resize(self, old_size);
1500 return NULL;
1501 }
1502 }
1503 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001504 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001505}
1506
Brett Cannon1eb32c22014-10-10 16:26:45 -04001507/*[clinic input]
1508array.array.tolist
1509
1510Convert array to an ordinary list with the same items.
1511[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001512
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001513static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001514array_array_tolist_impl(arrayobject *self)
1515/*[clinic end generated code: output=00b60cc9eab8ef89 input=a8d7784a94f86b53]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 PyObject *list = PyList_New(Py_SIZE(self));
1518 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 if (list == NULL)
1521 return NULL;
1522 for (i = 0; i < Py_SIZE(self); i++) {
1523 PyObject *v = getarrayitem((PyObject *)self, i);
Victor Stinner4755bea2013-07-18 01:12:35 +02001524 if (v == NULL)
1525 goto error;
1526 if (PyList_SetItem(list, i, v) < 0)
1527 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 }
1529 return list;
Victor Stinner4755bea2013-07-18 01:12:35 +02001530
1531error:
1532 Py_DECREF(list);
1533 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001534}
1535
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001536static PyObject *
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001537frombytes(arrayobject *self, Py_buffer *buffer)
Guido van Rossum778983b1993-02-19 15:55:02 +00001538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 int itemsize = self->ob_descr->itemsize;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001540 Py_ssize_t n;
1541 if (buffer->itemsize != 1) {
1542 PyBuffer_Release(buffer);
Serhiy Storchakab757c832014-12-05 22:25:22 +02001543 PyErr_SetString(PyExc_TypeError, "a bytes-like object is required");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 return NULL;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001545 }
1546 n = buffer->len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 if (n % itemsize != 0) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001548 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 PyErr_SetString(PyExc_ValueError,
Serhiy Storchakab757c832014-12-05 22:25:22 +02001550 "bytes length not a multiple of item size");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 return NULL;
1552 }
1553 n = n / itemsize;
1554 if (n > 0) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001555 Py_ssize_t old_size = Py_SIZE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 if ((n > PY_SSIZE_T_MAX - old_size) ||
1557 ((old_size + n) > PY_SSIZE_T_MAX / itemsize)) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001558 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 return PyErr_NoMemory();
1560 }
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001561 if (array_resize(self, old_size + n) == -1) {
1562 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 return NULL;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001564 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 memcpy(self->ob_item + old_size * itemsize,
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001566 buffer->buf, n * itemsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 }
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001568 PyBuffer_Release(buffer);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001569 Py_RETURN_NONE;
Guido van Rossum778983b1993-02-19 15:55:02 +00001570}
1571
Brett Cannon1eb32c22014-10-10 16:26:45 -04001572/*[clinic input]
1573array.array.fromstring
1574
Larry Hastingsdbfdc382015-05-04 06:59:46 -07001575 buffer: Py_buffer(accept={str, buffer})
Brett Cannon1eb32c22014-10-10 16:26:45 -04001576 /
1577
1578Appends 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).
1579
1580This method is deprecated. Use frombytes instead.
1581[clinic start generated code]*/
1582
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001583static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001584array_array_fromstring_impl(arrayobject *self, Py_buffer *buffer)
Larry Hastingsdbfdc382015-05-04 06:59:46 -07001585/*[clinic end generated code: output=31c4baa779df84ce input=a3341a512e11d773]*/
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001586{
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001587 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1588 "fromstring() is deprecated. Use frombytes() instead.", 2) != 0)
1589 return NULL;
Brett Cannon1eb32c22014-10-10 16:26:45 -04001590 return frombytes(self, buffer);
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001591}
1592
Brett Cannon1eb32c22014-10-10 16:26:45 -04001593/*[clinic input]
1594array.array.frombytes
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001595
Brett Cannon1eb32c22014-10-10 16:26:45 -04001596 buffer: Py_buffer
1597 /
1598
1599Appends 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).
1600[clinic start generated code]*/
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001601
1602static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001603array_array_frombytes_impl(arrayobject *self, Py_buffer *buffer)
1604/*[clinic end generated code: output=d9842c8f7510a516 input=2bbf2b53ebfcc988]*/
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001605{
Brett Cannon1eb32c22014-10-10 16:26:45 -04001606 return frombytes(self, buffer);
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001607}
1608
Brett Cannon1eb32c22014-10-10 16:26:45 -04001609/*[clinic input]
1610array.array.tobytes
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001611
Brett Cannon1eb32c22014-10-10 16:26:45 -04001612Convert the array to an array of machine values and return the bytes representation.
1613[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001614
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001615static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001616array_array_tobytes_impl(arrayobject *self)
1617/*[clinic end generated code: output=87318e4edcdc2bb6 input=90ee495f96de34f5]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
1620 return PyBytes_FromStringAndSize(self->ob_item,
1621 Py_SIZE(self) * self->ob_descr->itemsize);
1622 } else {
1623 return PyErr_NoMemory();
1624 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001625}
1626
Brett Cannon1eb32c22014-10-10 16:26:45 -04001627/*[clinic input]
1628array.array.tostring
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001629
Brett Cannon1eb32c22014-10-10 16:26:45 -04001630Convert the array to an array of machine values and return the bytes representation.
1631
1632This method is deprecated. Use tobytes instead.
1633[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001634
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001635static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001636array_array_tostring_impl(arrayobject *self)
1637/*[clinic end generated code: output=7d6bd92745a2c8f3 input=b6c0ddee7b30457e]*/
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001638{
Victor Stinner9f0b51e2010-11-09 09:38:30 +00001639 if (PyErr_WarnEx(PyExc_DeprecationWarning,
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001640 "tostring() is deprecated. Use tobytes() instead.", 2) != 0)
1641 return NULL;
Brett Cannon1eb32c22014-10-10 16:26:45 -04001642 return array_array_tobytes_impl(self);
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001643}
1644
Brett Cannon1eb32c22014-10-10 16:26:45 -04001645/*[clinic input]
1646array.array.fromunicode
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001647
Larry Hastings38337d12015-05-07 23:30:09 -07001648 ustr: Py_UNICODE(zeroes=True)
Brett Cannon1eb32c22014-10-10 16:26:45 -04001649 /
1650
1651Extends this array with data from the unicode string ustr.
1652
1653The array must be a unicode type array; otherwise a ValueError is raised.
1654Use array.frombytes(ustr.encode(...)) to append Unicode data to an array of
1655some other type.
1656[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001657
Martin v. Löwis99866332002-03-01 10:27:01 +00001658static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001659array_array_fromunicode_impl(arrayobject *self, Py_UNICODE *ustr,
1660 Py_ssize_clean_t ustr_length)
Larry Hastings38337d12015-05-07 23:30:09 -07001661/*[clinic end generated code: output=ebb72fc16975e06d input=150f00566ffbca6e]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001662{
Victor Stinner62bb3942012-08-06 00:46:05 +02001663 char typecode;
Martin v. Löwis99866332002-03-01 10:27:01 +00001664
Victor Stinner62bb3942012-08-06 00:46:05 +02001665 typecode = self->ob_descr->typecode;
Gregory P. Smith9504b132012-12-10 20:20:20 -08001666 if (typecode != 'u') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 PyErr_SetString(PyExc_ValueError,
1668 "fromunicode() may only be called on "
1669 "unicode type arrays");
1670 return NULL;
1671 }
Brett Cannon1eb32c22014-10-10 16:26:45 -04001672 if (ustr_length > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 Py_ssize_t old_size = Py_SIZE(self);
Brett Cannon1eb32c22014-10-10 16:26:45 -04001674 if (array_resize(self, old_size + ustr_length) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 return NULL;
Victor Stinner62bb3942012-08-06 00:46:05 +02001676 memcpy(self->ob_item + old_size * sizeof(Py_UNICODE),
Brett Cannon1eb32c22014-10-10 16:26:45 -04001677 ustr, ustr_length * sizeof(Py_UNICODE));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 }
Martin v. Löwis99866332002-03-01 10:27:01 +00001679
Brett Cannon1eb32c22014-10-10 16:26:45 -04001680 Py_RETURN_NONE;
Martin v. Löwis99866332002-03-01 10:27:01 +00001681}
1682
Brett Cannon1eb32c22014-10-10 16:26:45 -04001683/*[clinic input]
1684array.array.tounicode
Martin v. Löwis99866332002-03-01 10:27:01 +00001685
Brett Cannon1eb32c22014-10-10 16:26:45 -04001686Extends this array with data from the unicode string ustr.
1687
1688Convert the array to a unicode string. The array must be a unicode type array;
1689otherwise a ValueError is raised. Use array.tobytes().decode() to obtain a
1690unicode string from an array of some other type.
1691[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001692
1693static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001694array_array_tounicode_impl(arrayobject *self)
1695/*[clinic end generated code: output=08e442378336e1ef input=127242eebe70b66d]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001696{
Victor Stinner62bb3942012-08-06 00:46:05 +02001697 char typecode;
1698 typecode = self->ob_descr->typecode;
Gregory P. Smith9504b132012-12-10 20:20:20 -08001699 if (typecode != 'u') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 PyErr_SetString(PyExc_ValueError,
1701 "tounicode() may only be called on unicode type arrays");
1702 return NULL;
1703 }
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02001704 return PyUnicode_FromWideChar((Py_UNICODE *) self->ob_item, Py_SIZE(self));
Martin v. Löwis99866332002-03-01 10:27:01 +00001705}
1706
Brett Cannon1eb32c22014-10-10 16:26:45 -04001707/*[clinic input]
1708array.array.__sizeof__
Martin v. Löwis99866332002-03-01 10:27:01 +00001709
Brett Cannon1eb32c22014-10-10 16:26:45 -04001710Size of the array in memory, in bytes.
1711[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001712
Meador Inge03b4d502012-08-10 22:35:45 -05001713static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001714array_array___sizeof___impl(arrayobject *self)
1715/*[clinic end generated code: output=d8e1c61ebbe3eaed input=805586565bf2b3c6]*/
Meador Inge03b4d502012-08-10 22:35:45 -05001716{
1717 Py_ssize_t res;
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02001718 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * self->ob_descr->itemsize;
Meador Inge03b4d502012-08-10 22:35:45 -05001719 return PyLong_FromSsize_t(res);
1720}
1721
Martin v. Löwis99866332002-03-01 10:27:01 +00001722
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001723/*********************** Pickling support ************************/
1724
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001725static const struct mformatdescr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 size_t size;
1727 int is_signed;
1728 int is_big_endian;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001729} mformat_descriptors[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 {1, 0, 0}, /* 0: UNSIGNED_INT8 */
1731 {1, 1, 0}, /* 1: SIGNED_INT8 */
1732 {2, 0, 0}, /* 2: UNSIGNED_INT16_LE */
1733 {2, 0, 1}, /* 3: UNSIGNED_INT16_BE */
1734 {2, 1, 0}, /* 4: SIGNED_INT16_LE */
1735 {2, 1, 1}, /* 5: SIGNED_INT16_BE */
1736 {4, 0, 0}, /* 6: UNSIGNED_INT32_LE */
1737 {4, 0, 1}, /* 7: UNSIGNED_INT32_BE */
1738 {4, 1, 0}, /* 8: SIGNED_INT32_LE */
1739 {4, 1, 1}, /* 9: SIGNED_INT32_BE */
1740 {8, 0, 0}, /* 10: UNSIGNED_INT64_LE */
1741 {8, 0, 1}, /* 11: UNSIGNED_INT64_BE */
1742 {8, 1, 0}, /* 12: SIGNED_INT64_LE */
1743 {8, 1, 1}, /* 13: SIGNED_INT64_BE */
1744 {4, 0, 0}, /* 14: IEEE_754_FLOAT_LE */
1745 {4, 0, 1}, /* 15: IEEE_754_FLOAT_BE */
1746 {8, 0, 0}, /* 16: IEEE_754_DOUBLE_LE */
1747 {8, 0, 1}, /* 17: IEEE_754_DOUBLE_BE */
1748 {4, 0, 0}, /* 18: UTF16_LE */
1749 {4, 0, 1}, /* 19: UTF16_BE */
1750 {8, 0, 0}, /* 20: UTF32_LE */
1751 {8, 0, 1} /* 21: UTF32_BE */
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001752};
1753
1754
1755/*
1756 * Internal: This function is used to find the machine format of a given
1757 * array type code. This returns UNKNOWN_FORMAT when the machine format cannot
1758 * be found.
1759 */
1760static enum machine_format_code
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001761typecode_to_mformat_code(char typecode)
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001762{
Christian Heimes743e0cd2012-10-17 23:52:17 +02001763 const int is_big_endian = PY_BIG_ENDIAN;
1764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 size_t intsize;
1766 int is_signed;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 switch (typecode) {
1769 case 'b':
1770 return SIGNED_INT8;
1771 case 'B':
1772 return UNSIGNED_INT8;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 case 'u':
Victor Stinner62bb3942012-08-06 00:46:05 +02001775 if (sizeof(Py_UNICODE) == 2) {
1776 return UTF16_LE + is_big_endian;
1777 }
1778 if (sizeof(Py_UNICODE) == 4) {
1779 return UTF32_LE + is_big_endian;
1780 }
1781 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 case 'f':
1784 if (sizeof(float) == 4) {
1785 const float y = 16711938.0;
1786 if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0)
1787 return IEEE_754_FLOAT_BE;
1788 if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0)
1789 return IEEE_754_FLOAT_LE;
1790 }
1791 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 case 'd':
1794 if (sizeof(double) == 8) {
1795 const double x = 9006104071832581.0;
1796 if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0)
1797 return IEEE_754_DOUBLE_BE;
1798 if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
1799 return IEEE_754_DOUBLE_LE;
1800 }
1801 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 /* Integers */
1804 case 'h':
1805 intsize = sizeof(short);
1806 is_signed = 1;
1807 break;
1808 case 'H':
1809 intsize = sizeof(short);
1810 is_signed = 0;
1811 break;
1812 case 'i':
1813 intsize = sizeof(int);
1814 is_signed = 1;
1815 break;
1816 case 'I':
1817 intsize = sizeof(int);
1818 is_signed = 0;
1819 break;
1820 case 'l':
1821 intsize = sizeof(long);
1822 is_signed = 1;
1823 break;
1824 case 'L':
1825 intsize = sizeof(long);
1826 is_signed = 0;
1827 break;
Meador Inge1c9f0c92011-09-20 19:55:51 -05001828 case 'q':
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001829 intsize = sizeof(long long);
Meador Inge1c9f0c92011-09-20 19:55:51 -05001830 is_signed = 1;
1831 break;
1832 case 'Q':
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001833 intsize = sizeof(long long);
Meador Inge1c9f0c92011-09-20 19:55:51 -05001834 is_signed = 0;
1835 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 default:
1837 return UNKNOWN_FORMAT;
1838 }
1839 switch (intsize) {
1840 case 2:
1841 return UNSIGNED_INT16_LE + is_big_endian + (2 * is_signed);
1842 case 4:
1843 return UNSIGNED_INT32_LE + is_big_endian + (2 * is_signed);
1844 case 8:
1845 return UNSIGNED_INT64_LE + is_big_endian + (2 * is_signed);
1846 default:
1847 return UNKNOWN_FORMAT;
1848 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001849}
1850
1851/* Forward declaration. */
1852static PyObject *array_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
1853
1854/*
1855 * Internal: This function wraps the array constructor--i.e., array_new()--to
1856 * allow the creation of array objects from C code without having to deal
1857 * directly the tuple argument of array_new(). The typecode argument is a
1858 * Unicode character value, like 'i' or 'f' for example, representing an array
1859 * type code. The items argument is a bytes or a list object from which
1860 * contains the initial value of the array.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 *
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001862 * On success, this functions returns the array object created. Otherwise,
1863 * NULL is returned to indicate a failure.
1864 */
1865static PyObject *
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001866make_array(PyTypeObject *arraytype, char typecode, PyObject *items)
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001867{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 PyObject *new_args;
1869 PyObject *array_obj;
1870 PyObject *typecode_obj;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 assert(arraytype != NULL);
1873 assert(items != NULL);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001874
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001875 typecode_obj = PyUnicode_FromOrdinal(typecode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 if (typecode_obj == NULL)
1877 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 new_args = PyTuple_New(2);
1880 if (new_args == NULL)
1881 return NULL;
1882 Py_INCREF(items);
1883 PyTuple_SET_ITEM(new_args, 0, typecode_obj);
1884 PyTuple_SET_ITEM(new_args, 1, items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 array_obj = array_new(arraytype, new_args, NULL);
1887 Py_DECREF(new_args);
1888 if (array_obj == NULL)
1889 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 return array_obj;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001892}
1893
1894/*
1895 * This functions is a special constructor used when unpickling an array. It
1896 * provides a portable way to rebuild an array from its memory representation.
1897 */
Brett Cannon1eb32c22014-10-10 16:26:45 -04001898/*[clinic input]
1899array._array_reconstructor
1900
1901 arraytype: object(type="PyTypeObject *")
Larry Hastingsdbfdc382015-05-04 06:59:46 -07001902 typecode: int(accept={str})
Brett Cannon1eb32c22014-10-10 16:26:45 -04001903 mformat_code: int(type="enum machine_format_code")
1904 items: object
1905 /
1906
1907Internal. Used for pickling support.
1908[clinic start generated code]*/
1909
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001910static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001911array__array_reconstructor_impl(PyObject *module, PyTypeObject *arraytype,
Larry Hastings89964c42015-04-14 18:07:59 -04001912 int typecode,
1913 enum machine_format_code mformat_code,
1914 PyObject *items)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001915/*[clinic end generated code: output=e05263141ba28365 input=2464dc8f4c7736b5]*/
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 PyObject *converted_items;
1918 PyObject *result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001919 const struct arraydescr *descr;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 if (!PyType_Check(arraytype)) {
1922 PyErr_Format(PyExc_TypeError,
Sylvaina90e64b2017-03-29 20:09:22 +02001923 "first argument must be a type object, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 Py_TYPE(arraytype)->tp_name);
1925 return NULL;
1926 }
1927 if (!PyType_IsSubtype(arraytype, &Arraytype)) {
1928 PyErr_Format(PyExc_TypeError,
1929 "%.200s is not a subtype of %.200s",
1930 arraytype->tp_name, Arraytype.tp_name);
1931 return NULL;
1932 }
1933 for (descr = descriptors; descr->typecode != '\0'; descr++) {
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001934 if ((int)descr->typecode == typecode)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 break;
1936 }
1937 if (descr->typecode == '\0') {
1938 PyErr_SetString(PyExc_ValueError,
1939 "second argument must be a valid type code");
1940 return NULL;
1941 }
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001942 if (mformat_code < MACHINE_FORMAT_CODE_MIN ||
1943 mformat_code > MACHINE_FORMAT_CODE_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 PyErr_SetString(PyExc_ValueError,
1945 "third argument must be a valid machine format code.");
1946 return NULL;
1947 }
1948 if (!PyBytes_Check(items)) {
1949 PyErr_Format(PyExc_TypeError,
1950 "fourth argument should be bytes, not %.200s",
1951 Py_TYPE(items)->tp_name);
1952 return NULL;
1953 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 /* Fast path: No decoding has to be done. */
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001956 if (mformat_code == typecode_to_mformat_code((char)typecode) ||
1957 mformat_code == UNKNOWN_FORMAT) {
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001958 return make_array(arraytype, (char)typecode, items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 /* Slow path: Decode the byte string according to the given machine
1962 * format code. This occurs when the computer unpickling the array
1963 * object is architecturally different from the one that pickled the
1964 * array.
1965 */
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001966 if (Py_SIZE(items) % mformat_descriptors[mformat_code].size != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 PyErr_SetString(PyExc_ValueError,
1968 "string length not a multiple of item size");
1969 return NULL;
1970 }
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001971 switch (mformat_code) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 case IEEE_754_FLOAT_LE:
1973 case IEEE_754_FLOAT_BE: {
1974 int i;
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001975 int le = (mformat_code == IEEE_754_FLOAT_LE) ? 1 : 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 Py_ssize_t itemcount = Py_SIZE(items) / 4;
1977 const unsigned char *memstr =
1978 (unsigned char *)PyBytes_AS_STRING(items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 converted_items = PyList_New(itemcount);
1981 if (converted_items == NULL)
1982 return NULL;
1983 for (i = 0; i < itemcount; i++) {
1984 PyObject *pyfloat = PyFloat_FromDouble(
1985 _PyFloat_Unpack4(&memstr[i * 4], le));
1986 if (pyfloat == NULL) {
1987 Py_DECREF(converted_items);
1988 return NULL;
1989 }
1990 PyList_SET_ITEM(converted_items, i, pyfloat);
1991 }
1992 break;
1993 }
1994 case IEEE_754_DOUBLE_LE:
1995 case IEEE_754_DOUBLE_BE: {
1996 int i;
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001997 int le = (mformat_code == IEEE_754_DOUBLE_LE) ? 1 : 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998 Py_ssize_t itemcount = Py_SIZE(items) / 8;
1999 const unsigned char *memstr =
2000 (unsigned char *)PyBytes_AS_STRING(items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002 converted_items = PyList_New(itemcount);
2003 if (converted_items == NULL)
2004 return NULL;
2005 for (i = 0; i < itemcount; i++) {
2006 PyObject *pyfloat = PyFloat_FromDouble(
2007 _PyFloat_Unpack8(&memstr[i * 8], le));
2008 if (pyfloat == NULL) {
2009 Py_DECREF(converted_items);
2010 return NULL;
2011 }
2012 PyList_SET_ITEM(converted_items, i, pyfloat);
2013 }
2014 break;
2015 }
2016 case UTF16_LE:
2017 case UTF16_BE: {
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002018 int byteorder = (mformat_code == UTF16_LE) ? -1 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 converted_items = PyUnicode_DecodeUTF16(
2020 PyBytes_AS_STRING(items), Py_SIZE(items),
2021 "strict", &byteorder);
2022 if (converted_items == NULL)
2023 return NULL;
2024 break;
2025 }
2026 case UTF32_LE:
2027 case UTF32_BE: {
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002028 int byteorder = (mformat_code == UTF32_LE) ? -1 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 converted_items = PyUnicode_DecodeUTF32(
2030 PyBytes_AS_STRING(items), Py_SIZE(items),
2031 "strict", &byteorder);
2032 if (converted_items == NULL)
2033 return NULL;
2034 break;
2035 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 case UNSIGNED_INT8:
2038 case SIGNED_INT8:
2039 case UNSIGNED_INT16_LE:
2040 case UNSIGNED_INT16_BE:
2041 case SIGNED_INT16_LE:
2042 case SIGNED_INT16_BE:
2043 case UNSIGNED_INT32_LE:
2044 case UNSIGNED_INT32_BE:
2045 case SIGNED_INT32_LE:
2046 case SIGNED_INT32_BE:
2047 case UNSIGNED_INT64_LE:
2048 case UNSIGNED_INT64_BE:
2049 case SIGNED_INT64_LE:
2050 case SIGNED_INT64_BE: {
2051 int i;
2052 const struct mformatdescr mf_descr =
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002053 mformat_descriptors[mformat_code];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 Py_ssize_t itemcount = Py_SIZE(items) / mf_descr.size;
2055 const unsigned char *memstr =
2056 (unsigned char *)PyBytes_AS_STRING(items);
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002057 const struct arraydescr *descr;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 /* If possible, try to pack array's items using a data type
2060 * that fits better. This may result in an array with narrower
2061 * or wider elements.
2062 *
Martin Panter4c359642016-05-08 13:53:41 +00002063 * For example, if a 32-bit machine pickles an L-code array of
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 * unsigned longs, then the array will be unpickled by 64-bit
2065 * machine as an I-code array of unsigned ints.
2066 *
2067 * XXX: Is it possible to write a unit test for this?
2068 */
2069 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2070 if (descr->is_integer_type &&
Victor Stinner706768c2014-08-16 01:03:39 +02002071 (size_t)descr->itemsize == mf_descr.size &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 descr->is_signed == mf_descr.is_signed)
2073 typecode = descr->typecode;
2074 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 converted_items = PyList_New(itemcount);
2077 if (converted_items == NULL)
2078 return NULL;
2079 for (i = 0; i < itemcount; i++) {
2080 PyObject *pylong;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 pylong = _PyLong_FromByteArray(
2083 &memstr[i * mf_descr.size],
2084 mf_descr.size,
2085 !mf_descr.is_big_endian,
2086 mf_descr.is_signed);
2087 if (pylong == NULL) {
2088 Py_DECREF(converted_items);
2089 return NULL;
2090 }
2091 PyList_SET_ITEM(converted_items, i, pylong);
2092 }
2093 break;
2094 }
2095 case UNKNOWN_FORMAT:
2096 /* Impossible, but needed to shut up GCC about the unhandled
2097 * enumeration value.
2098 */
2099 default:
2100 PyErr_BadArgument();
2101 return NULL;
2102 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002103
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002104 result = make_array(arraytype, (char)typecode, converted_items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 Py_DECREF(converted_items);
2106 return result;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002107}
2108
Brett Cannon1eb32c22014-10-10 16:26:45 -04002109/*[clinic input]
2110array.array.__reduce_ex__
2111
2112 value: object
2113 /
2114
2115Return state information for pickling.
2116[clinic start generated code]*/
2117
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002118static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04002119array_array___reduce_ex__(arrayobject *self, PyObject *value)
2120/*[clinic end generated code: output=051e0a6175d0eddb input=c36c3f85de7df6cd]*/
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002121{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 PyObject *dict;
2123 PyObject *result;
2124 PyObject *array_str;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002125 int typecode = self->ob_descr->typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002126 int mformat_code;
2127 static PyObject *array_reconstructor = NULL;
2128 long protocol;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002129 _Py_IDENTIFIER(_array_reconstructor);
2130 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132 if (array_reconstructor == NULL) {
2133 PyObject *array_module = PyImport_ImportModule("array");
2134 if (array_module == NULL)
2135 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002136 array_reconstructor = _PyObject_GetAttrId(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 array_module,
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002138 &PyId__array_reconstructor);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 Py_DECREF(array_module);
2140 if (array_reconstructor == NULL)
2141 return NULL;
2142 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 if (!PyLong_Check(value)) {
2145 PyErr_SetString(PyExc_TypeError,
Sylvaina90e64b2017-03-29 20:09:22 +02002146 "__reduce_ex__ argument should be an integer");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 return NULL;
2148 }
2149 protocol = PyLong_AsLong(value);
2150 if (protocol == -1 && PyErr_Occurred())
2151 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002152
Brett Cannon1eb32c22014-10-10 16:26:45 -04002153 dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 if (dict == NULL) {
2155 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
2156 return NULL;
2157 PyErr_Clear();
2158 dict = Py_None;
2159 Py_INCREF(dict);
2160 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 mformat_code = typecode_to_mformat_code(typecode);
2163 if (mformat_code == UNKNOWN_FORMAT || protocol < 3) {
2164 /* Convert the array to a list if we got something weird
2165 * (e.g., non-IEEE floats), or we are pickling the array using
2166 * a Python 2.x compatible protocol.
2167 *
2168 * It is necessary to use a list representation for Python 2.x
2169 * compatible pickle protocol, since Python 2's str objects
2170 * are unpickled as unicode by Python 3. Thus it is impossible
2171 * to make arrays unpicklable by Python 3 by using their memory
2172 * representation, unless we resort to ugly hacks such as
2173 * coercing unicode objects to bytes in array_reconstructor.
2174 */
2175 PyObject *list;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002176 list = array_array_tolist_impl(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 if (list == NULL) {
2178 Py_DECREF(dict);
2179 return NULL;
2180 }
2181 result = Py_BuildValue(
Brett Cannon1eb32c22014-10-10 16:26:45 -04002182 "O(CO)O", Py_TYPE(self), typecode, list, dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 Py_DECREF(list);
2184 Py_DECREF(dict);
2185 return result;
2186 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002187
Brett Cannon1eb32c22014-10-10 16:26:45 -04002188 array_str = array_array_tobytes_impl(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 if (array_str == NULL) {
2190 Py_DECREF(dict);
2191 return NULL;
2192 }
2193 result = Py_BuildValue(
Brett Cannon1eb32c22014-10-10 16:26:45 -04002194 "O(OCiN)O", array_reconstructor, Py_TYPE(self), typecode,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 mformat_code, array_str, dict);
2196 Py_DECREF(dict);
2197 return result;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002198}
2199
Martin v. Löwis99866332002-03-01 10:27:01 +00002200static PyObject *
2201array_get_typecode(arrayobject *a, void *closure)
2202{
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002203 char typecode = a->ob_descr->typecode;
2204 return PyUnicode_FromOrdinal(typecode);
Martin v. Löwis99866332002-03-01 10:27:01 +00002205}
2206
2207static PyObject *
2208array_get_itemsize(arrayobject *a, void *closure)
2209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002210 return PyLong_FromLong((long)a->ob_descr->itemsize);
Martin v. Löwis99866332002-03-01 10:27:01 +00002211}
2212
2213static PyGetSetDef array_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 {"typecode", (getter) array_get_typecode, NULL,
2215 "the typecode character used to create the array"},
2216 {"itemsize", (getter) array_get_itemsize, NULL,
2217 "the size, in bytes, of one array item"},
2218 {NULL}
Martin v. Löwis99866332002-03-01 10:27:01 +00002219};
2220
Martin v. Löwis59683e82008-06-13 07:50:45 +00002221static PyMethodDef array_methods[] = {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002222 ARRAY_ARRAY_APPEND_METHODDEF
2223 ARRAY_ARRAY_BUFFER_INFO_METHODDEF
2224 ARRAY_ARRAY_BYTESWAP_METHODDEF
2225 ARRAY_ARRAY___COPY___METHODDEF
2226 ARRAY_ARRAY_COUNT_METHODDEF
2227 ARRAY_ARRAY___DEEPCOPY___METHODDEF
2228 ARRAY_ARRAY_EXTEND_METHODDEF
2229 ARRAY_ARRAY_FROMFILE_METHODDEF
2230 ARRAY_ARRAY_FROMLIST_METHODDEF
2231 ARRAY_ARRAY_FROMSTRING_METHODDEF
2232 ARRAY_ARRAY_FROMBYTES_METHODDEF
2233 ARRAY_ARRAY_FROMUNICODE_METHODDEF
2234 ARRAY_ARRAY_INDEX_METHODDEF
2235 ARRAY_ARRAY_INSERT_METHODDEF
2236 ARRAY_ARRAY_POP_METHODDEF
2237 ARRAY_ARRAY___REDUCE_EX___METHODDEF
2238 ARRAY_ARRAY_REMOVE_METHODDEF
2239 ARRAY_ARRAY_REVERSE_METHODDEF
2240 ARRAY_ARRAY_TOFILE_METHODDEF
2241 ARRAY_ARRAY_TOLIST_METHODDEF
2242 ARRAY_ARRAY_TOSTRING_METHODDEF
2243 ARRAY_ARRAY_TOBYTES_METHODDEF
2244 ARRAY_ARRAY_TOUNICODE_METHODDEF
2245 ARRAY_ARRAY___SIZEOF___METHODDEF
2246 {NULL, NULL} /* sentinel */
Guido van Rossum778983b1993-02-19 15:55:02 +00002247};
2248
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002249static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00002250array_repr(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +00002251{
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002252 char typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 PyObject *s, *v = NULL;
2254 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00002255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 len = Py_SIZE(a);
2257 typecode = a->ob_descr->typecode;
2258 if (len == 0) {
Amaury Forgeot d'Arc24aa26b2010-11-25 08:13:35 +00002259 return PyUnicode_FromFormat("array('%c')", (int)typecode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 }
Gregory P. Smith9504b132012-12-10 20:20:20 -08002261 if (typecode == 'u') {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002262 v = array_array_tounicode_impl(a);
Gregory P. Smith9504b132012-12-10 20:20:20 -08002263 } else {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002264 v = array_array_tolist_impl(a);
Gregory P. Smith9504b132012-12-10 20:20:20 -08002265 }
Victor Stinner29ec5952013-02-26 00:27:38 +01002266 if (v == NULL)
2267 return NULL;
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00002268
Amaury Forgeot d'Arc24aa26b2010-11-25 08:13:35 +00002269 s = PyUnicode_FromFormat("array('%c', %R)", (int)typecode, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 Py_DECREF(v);
2271 return s;
Guido van Rossum778983b1993-02-19 15:55:02 +00002272}
2273
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002274static PyObject*
2275array_subscr(arrayobject* self, PyObject* item)
2276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 if (PyIndex_Check(item)) {
2278 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2279 if (i==-1 && PyErr_Occurred()) {
2280 return NULL;
2281 }
2282 if (i < 0)
2283 i += Py_SIZE(self);
2284 return array_item(self, i);
2285 }
2286 else if (PySlice_Check(item)) {
2287 Py_ssize_t start, stop, step, slicelength, cur, i;
2288 PyObject* result;
2289 arrayobject* ar;
2290 int itemsize = self->ob_descr->itemsize;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002291
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002292 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 return NULL;
2294 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002295 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2296 step);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 if (slicelength <= 0) {
2299 return newarrayobject(&Arraytype, 0, self->ob_descr);
2300 }
2301 else if (step == 1) {
2302 PyObject *result = newarrayobject(&Arraytype,
2303 slicelength, self->ob_descr);
2304 if (result == NULL)
2305 return NULL;
2306 memcpy(((arrayobject *)result)->ob_item,
2307 self->ob_item + start * itemsize,
2308 slicelength * itemsize);
2309 return result;
2310 }
2311 else {
2312 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
2313 if (!result) return NULL;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 ar = (arrayobject*)result;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 for (cur = start, i = 0; i < slicelength;
2318 cur += step, i++) {
2319 memcpy(ar->ob_item + i*itemsize,
2320 self->ob_item + cur*itemsize,
2321 itemsize);
2322 }
2323
2324 return result;
2325 }
2326 }
2327 else {
2328 PyErr_SetString(PyExc_TypeError,
2329 "array indices must be integers");
2330 return NULL;
2331 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002332}
2333
2334static int
2335array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
2336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 Py_ssize_t start, stop, step, slicelength, needed;
2338 arrayobject* other;
2339 int itemsize;
Thomas Woutersed03b412007-08-28 21:37:11 +00002340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002341 if (PyIndex_Check(item)) {
2342 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Alexandre Vassalotti47137252009-07-05 19:57:00 +00002343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 if (i == -1 && PyErr_Occurred())
2345 return -1;
2346 if (i < 0)
2347 i += Py_SIZE(self);
2348 if (i < 0 || i >= Py_SIZE(self)) {
2349 PyErr_SetString(PyExc_IndexError,
2350 "array assignment index out of range");
2351 return -1;
2352 }
2353 if (value == NULL) {
2354 /* Fall through to slice assignment */
2355 start = i;
2356 stop = i + 1;
2357 step = 1;
2358 slicelength = 1;
2359 }
2360 else
2361 return (*self->ob_descr->setitem)(self, i, value);
2362 }
2363 else if (PySlice_Check(item)) {
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002364 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 return -1;
2366 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +03002367 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2368 step);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 }
2370 else {
2371 PyErr_SetString(PyExc_TypeError,
Sylvaina90e64b2017-03-29 20:09:22 +02002372 "array indices must be integers");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 return -1;
2374 }
2375 if (value == NULL) {
2376 other = NULL;
2377 needed = 0;
2378 }
2379 else if (array_Check(value)) {
2380 other = (arrayobject *)value;
2381 needed = Py_SIZE(other);
2382 if (self == other) {
2383 /* Special case "self[i:j] = self" -- copy self first */
2384 int ret;
2385 value = array_slice(other, 0, needed);
2386 if (value == NULL)
2387 return -1;
2388 ret = array_ass_subscr(self, item, value);
2389 Py_DECREF(value);
2390 return ret;
2391 }
2392 if (other->ob_descr != self->ob_descr) {
2393 PyErr_BadArgument();
2394 return -1;
2395 }
2396 }
2397 else {
2398 PyErr_Format(PyExc_TypeError,
2399 "can only assign array (not \"%.200s\") to array slice",
2400 Py_TYPE(value)->tp_name);
2401 return -1;
2402 }
2403 itemsize = self->ob_descr->itemsize;
2404 /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
2405 if ((step > 0 && stop < start) ||
2406 (step < 0 && stop > start))
2407 stop = start;
Alexandre Vassalotti47137252009-07-05 19:57:00 +00002408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 /* Issue #4509: If the array has exported buffers and the slice
2410 assignment would change the size of the array, fail early to make
2411 sure we don't modify it. */
2412 if ((needed == 0 || slicelength != needed) && self->ob_exports > 0) {
2413 PyErr_SetString(PyExc_BufferError,
2414 "cannot resize an array that is exporting buffers");
2415 return -1;
2416 }
Mark Dickinsonbc099642010-01-29 17:27:24 +00002417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 if (step == 1) {
2419 if (slicelength > needed) {
2420 memmove(self->ob_item + (start + needed) * itemsize,
2421 self->ob_item + stop * itemsize,
2422 (Py_SIZE(self) - stop) * itemsize);
2423 if (array_resize(self, Py_SIZE(self) +
2424 needed - slicelength) < 0)
2425 return -1;
2426 }
2427 else if (slicelength < needed) {
2428 if (array_resize(self, Py_SIZE(self) +
2429 needed - slicelength) < 0)
2430 return -1;
2431 memmove(self->ob_item + (start + needed) * itemsize,
2432 self->ob_item + stop * itemsize,
2433 (Py_SIZE(self) - start - needed) * itemsize);
2434 }
2435 if (needed > 0)
2436 memcpy(self->ob_item + start * itemsize,
2437 other->ob_item, needed * itemsize);
2438 return 0;
2439 }
2440 else if (needed == 0) {
2441 /* Delete slice */
2442 size_t cur;
2443 Py_ssize_t i;
Thomas Woutersed03b412007-08-28 21:37:11 +00002444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 if (step < 0) {
2446 stop = start + 1;
2447 start = stop + step * (slicelength - 1) - 1;
2448 step = -step;
2449 }
2450 for (cur = start, i = 0; i < slicelength;
2451 cur += step, i++) {
2452 Py_ssize_t lim = step - 1;
Thomas Woutersed03b412007-08-28 21:37:11 +00002453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 if (cur + step >= (size_t)Py_SIZE(self))
2455 lim = Py_SIZE(self) - cur - 1;
2456 memmove(self->ob_item + (cur - i) * itemsize,
2457 self->ob_item + (cur + 1) * itemsize,
2458 lim * itemsize);
2459 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002460 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 if (cur < (size_t)Py_SIZE(self)) {
2462 memmove(self->ob_item + (cur-slicelength) * itemsize,
2463 self->ob_item + cur * itemsize,
2464 (Py_SIZE(self) - cur) * itemsize);
2465 }
2466 if (array_resize(self, Py_SIZE(self) - slicelength) < 0)
2467 return -1;
2468 return 0;
2469 }
2470 else {
2471 Py_ssize_t cur, i;
2472
2473 if (needed != slicelength) {
2474 PyErr_Format(PyExc_ValueError,
2475 "attempt to assign array of size %zd "
2476 "to extended slice of size %zd",
2477 needed, slicelength);
2478 return -1;
2479 }
2480 for (cur = start, i = 0; i < slicelength;
2481 cur += step, i++) {
2482 memcpy(self->ob_item + cur * itemsize,
2483 other->ob_item + i * itemsize,
2484 itemsize);
2485 }
2486 return 0;
2487 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002488}
2489
2490static PyMappingMethods array_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002491 (lenfunc)array_length,
2492 (binaryfunc)array_subscr,
2493 (objobjargproc)array_ass_subscr
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002494};
2495
Guido van Rossumd8faa362007-04-27 19:54:29 +00002496static const void *emptybuf = "";
2497
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002498
2499static int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00002500array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002501{
Stefan Krah650c1e82015-02-03 21:43:23 +01002502 if (view == NULL) {
2503 PyErr_SetString(PyExc_BufferError,
2504 "array_buffer_getbuf: view==NULL argument is obsolete");
2505 return -1;
2506 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508 view->buf = (void *)self->ob_item;
2509 view->obj = (PyObject*)self;
2510 Py_INCREF(self);
2511 if (view->buf == NULL)
2512 view->buf = (void *)emptybuf;
2513 view->len = (Py_SIZE(self)) * self->ob_descr->itemsize;
2514 view->readonly = 0;
2515 view->ndim = 1;
2516 view->itemsize = self->ob_descr->itemsize;
2517 view->suboffsets = NULL;
2518 view->shape = NULL;
2519 if ((flags & PyBUF_ND)==PyBUF_ND) {
2520 view->shape = &((Py_SIZE(self)));
2521 }
2522 view->strides = NULL;
2523 if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES)
2524 view->strides = &(view->itemsize);
2525 view->format = NULL;
2526 view->internal = NULL;
Victor Stinner62bb3942012-08-06 00:46:05 +02002527 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002528 view->format = (char *)self->ob_descr->formats;
Victor Stinner62bb3942012-08-06 00:46:05 +02002529#ifdef Py_UNICODE_WIDE
2530 if (self->ob_descr->typecode == 'u') {
2531 view->format = "w";
2532 }
2533#endif
2534 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002536 self->ob_exports++;
2537 return 0;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002538}
2539
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002540static void
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00002541array_buffer_relbuf(arrayobject *self, Py_buffer *view)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 self->ob_exports--;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002544}
2545
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002546static PySequenceMethods array_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 (lenfunc)array_length, /*sq_length*/
2548 (binaryfunc)array_concat, /*sq_concat*/
2549 (ssizeargfunc)array_repeat, /*sq_repeat*/
2550 (ssizeargfunc)array_item, /*sq_item*/
2551 0, /*sq_slice*/
2552 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
2553 0, /*sq_ass_slice*/
2554 (objobjproc)array_contains, /*sq_contains*/
2555 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
2556 (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/
Guido van Rossum778983b1993-02-19 15:55:02 +00002557};
2558
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002559static PyBufferProcs array_as_buffer = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002560 (getbufferproc)array_buffer_getbuf,
2561 (releasebufferproc)array_buffer_relbuf
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002562};
2563
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002564static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +00002565array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum778983b1993-02-19 15:55:02 +00002566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002567 int c;
2568 PyObject *initial = NULL, *it = NULL;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002569 const struct arraydescr *descr;
Martin v. Löwis99866332002-03-01 10:27:01 +00002570
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03002571 if (type == &Arraytype && !_PyArg_NoKeywords("array.array", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00002573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002574 if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
2575 return NULL;
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00002576
Alexandre Vassalotti9730e332013-11-29 20:47:15 -08002577 if (initial && c != 'u') {
2578 if (PyUnicode_Check(initial)) {
2579 PyErr_Format(PyExc_TypeError, "cannot use a str to initialize "
2580 "an array with typecode '%c'", c);
2581 return NULL;
2582 }
2583 else if (array_Check(initial) &&
2584 ((arrayobject*)initial)->ob_descr->typecode == 'u') {
2585 PyErr_Format(PyExc_TypeError, "cannot use a unicode array to "
2586 "initialize an array with typecode '%c'", c);
2587 return NULL;
2588 }
2589 }
2590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002591 if (!(initial == NULL || PyList_Check(initial)
2592 || PyByteArray_Check(initial)
2593 || PyBytes_Check(initial)
2594 || PyTuple_Check(initial)
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002595 || ((c=='u') && PyUnicode_Check(initial))
2596 || (array_Check(initial)
2597 && c == ((arrayobject*)initial)->ob_descr->typecode))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002598 it = PyObject_GetIter(initial);
2599 if (it == NULL)
2600 return NULL;
2601 /* We set initial to NULL so that the subsequent code
2602 will create an empty array of the appropriate type
2603 and afterwards we can use array_iter_extend to populate
2604 the array.
2605 */
2606 initial = NULL;
2607 }
2608 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2609 if (descr->typecode == c) {
2610 PyObject *a;
2611 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00002612
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002613 if (initial == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002614 len = 0;
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002615 else if (PyList_Check(initial))
2616 len = PyList_GET_SIZE(initial);
2617 else if (PyTuple_Check(initial) || array_Check(initial))
2618 len = Py_SIZE(initial);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002619 else
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002620 len = 0;
Martin v. Löwis99866332002-03-01 10:27:01 +00002621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002622 a = newarrayobject(type, len, descr);
2623 if (a == NULL)
2624 return NULL;
2625
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002626 if (len > 0 && !array_Check(initial)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002627 Py_ssize_t i;
2628 for (i = 0; i < len; i++) {
2629 PyObject *v =
2630 PySequence_GetItem(initial, i);
2631 if (v == NULL) {
2632 Py_DECREF(a);
2633 return NULL;
2634 }
2635 if (setarrayitem(a, i, v) != 0) {
2636 Py_DECREF(v);
2637 Py_DECREF(a);
2638 return NULL;
2639 }
2640 Py_DECREF(v);
2641 }
2642 }
2643 else if (initial != NULL && (PyByteArray_Check(initial) ||
2644 PyBytes_Check(initial))) {
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03002645 PyObject *v;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002646 v = array_array_frombytes((arrayobject *)a,
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03002647 initial);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002648 if (v == NULL) {
2649 Py_DECREF(a);
2650 return NULL;
2651 }
2652 Py_DECREF(v);
2653 }
2654 else if (initial != NULL && PyUnicode_Check(initial)) {
Victor Stinner62bb3942012-08-06 00:46:05 +02002655 Py_UNICODE *ustr;
Victor Stinner1fbcaef2011-09-30 01:54:04 +02002656 Py_ssize_t n;
Victor Stinner62bb3942012-08-06 00:46:05 +02002657
2658 ustr = PyUnicode_AsUnicode(initial);
2659 if (ustr == NULL) {
2660 PyErr_NoMemory();
Victor Stinner1fbcaef2011-09-30 01:54:04 +02002661 Py_DECREF(a);
2662 return NULL;
2663 }
Victor Stinner62bb3942012-08-06 00:46:05 +02002664
2665 n = PyUnicode_GET_DATA_SIZE(initial);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 if (n > 0) {
2667 arrayobject *self = (arrayobject *)a;
Victor Stinner62bb3942012-08-06 00:46:05 +02002668 char *item = self->ob_item;
2669 item = (char *)PyMem_Realloc(item, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002670 if (item == NULL) {
2671 PyErr_NoMemory();
2672 Py_DECREF(a);
2673 return NULL;
2674 }
Victor Stinner62bb3942012-08-06 00:46:05 +02002675 self->ob_item = item;
2676 Py_SIZE(self) = n / sizeof(Py_UNICODE);
2677 memcpy(item, ustr, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002678 self->allocated = Py_SIZE(self);
2679 }
2680 }
Benjamin Peterson682124c2014-10-10 20:58:30 -04002681 else if (initial != NULL && array_Check(initial) && len > 0) {
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002682 arrayobject *self = (arrayobject *)a;
2683 arrayobject *other = (arrayobject *)initial;
2684 memcpy(self->ob_item, other->ob_item, len * other->ob_descr->itemsize);
2685 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 if (it != NULL) {
2687 if (array_iter_extend((arrayobject *)a, it) == -1) {
2688 Py_DECREF(it);
2689 Py_DECREF(a);
2690 return NULL;
2691 }
2692 Py_DECREF(it);
2693 }
2694 return a;
2695 }
2696 }
2697 PyErr_SetString(PyExc_ValueError,
Meador Inge1c9f0c92011-09-20 19:55:51 -05002698 "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 +00002699 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00002700}
2701
Guido van Rossum778983b1993-02-19 15:55:02 +00002702
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002703PyDoc_STRVAR(module_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00002704"This module defines an object type which can efficiently represent\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002705an array of basic values: characters, integers, floating point\n\
2706numbers. Arrays are sequence types and behave very much like lists,\n\
Alexandre Vassalotti9730e332013-11-29 20:47:15 -08002707except that the type of objects stored in them is constrained.\n");
2708
2709PyDoc_STRVAR(arraytype_doc,
2710"array(typecode [, initializer]) -> array\n\
2711\n\
2712Return a new array whose items are restricted by typecode, and\n\
2713initialized from the optional initializer value, which must be a list,\n\
2714string or iterable over elements of the appropriate type.\n\
2715\n\
2716Arrays represent basic values and behave very much like lists, except\n\
2717the type of objects stored in them is constrained. The type is specified\n\
2718at object creation time by using a type code, which is a single character.\n\
2719The following type codes are defined:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002720\n\
2721 Type code C Type Minimum size in bytes \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002722 'b' signed integer 1 \n\
2723 'B' unsigned integer 1 \n\
Victor Stinner62bb3942012-08-06 00:46:05 +02002724 'u' Unicode character 2 (see note) \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002725 'h' signed integer 2 \n\
2726 'H' unsigned integer 2 \n\
2727 'i' signed integer 2 \n\
2728 'I' unsigned integer 2 \n\
2729 'l' signed integer 4 \n\
2730 'L' unsigned integer 4 \n\
Meador Inge1c9f0c92011-09-20 19:55:51 -05002731 'q' signed integer 8 (see note) \n\
2732 'Q' unsigned integer 8 (see note) \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002733 'f' floating point 4 \n\
2734 'd' floating point 8 \n\
2735\n\
Victor Stinner62bb3942012-08-06 00:46:05 +02002736NOTE: The 'u' typecode corresponds to Python's unicode character. On \n\
2737narrow builds this is 2-bytes on wide builds this is 4-bytes.\n\
2738\n\
Meador Inge1c9f0c92011-09-20 19:55:51 -05002739NOTE: The 'q' and 'Q' type codes are only available if the platform \n\
2740C compiler used to build Python supports 'long long', or, on Windows, \n\
2741'__int64'.\n\
2742\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002743Methods:\n\
2744\n\
2745append() -- append a new item to the end of the array\n\
2746buffer_info() -- return information giving the current memory info\n\
2747byteswap() -- byteswap all the items of the array\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002748count() -- return number of occurrences of an object\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00002749extend() -- extend array by appending multiple elements from an iterable\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002750fromfile() -- read items from a file object\n\
2751fromlist() -- append items from the list\n\
Florent Xiclunac45fb252011-10-24 13:14:55 +02002752frombytes() -- append items from the string\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002753index() -- return index of first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002754insert() -- insert a new item into the array at a provided position\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00002755pop() -- remove and return item (default last)\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002756remove() -- remove first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002757reverse() -- reverse the order of the items in the array\n\
2758tofile() -- write all items to a file object\n\
2759tolist() -- return the array converted to an ordinary list\n\
Florent Xiclunac45fb252011-10-24 13:14:55 +02002760tobytes() -- return the array converted to a string\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002761\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002762Attributes:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002763\n\
2764typecode -- the typecode character used to create the array\n\
2765itemsize -- the length in bytes of one array item\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002766");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002767
Raymond Hettinger625812f2003-01-07 01:58:52 +00002768static PyObject *array_iter(arrayobject *ao);
2769
Tim Peters0c322792002-07-17 16:49:03 +00002770static PyTypeObject Arraytype = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002771 PyVarObject_HEAD_INIT(NULL, 0)
2772 "array.array",
2773 sizeof(arrayobject),
2774 0,
2775 (destructor)array_dealloc, /* tp_dealloc */
2776 0, /* tp_print */
2777 0, /* tp_getattr */
2778 0, /* tp_setattr */
2779 0, /* tp_reserved */
2780 (reprfunc)array_repr, /* tp_repr */
2781 0, /* tp_as_number*/
2782 &array_as_sequence, /* tp_as_sequence*/
2783 &array_as_mapping, /* tp_as_mapping*/
2784 0, /* tp_hash */
2785 0, /* tp_call */
2786 0, /* tp_str */
2787 PyObject_GenericGetAttr, /* tp_getattro */
2788 0, /* tp_setattro */
2789 &array_as_buffer, /* tp_as_buffer*/
2790 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2791 arraytype_doc, /* tp_doc */
2792 0, /* tp_traverse */
2793 0, /* tp_clear */
2794 array_richcompare, /* tp_richcompare */
2795 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
2796 (getiterfunc)array_iter, /* tp_iter */
2797 0, /* tp_iternext */
2798 array_methods, /* tp_methods */
2799 0, /* tp_members */
2800 array_getsets, /* tp_getset */
2801 0, /* tp_base */
2802 0, /* tp_dict */
2803 0, /* tp_descr_get */
2804 0, /* tp_descr_set */
2805 0, /* tp_dictoffset */
2806 0, /* tp_init */
2807 PyType_GenericAlloc, /* tp_alloc */
2808 array_new, /* tp_new */
2809 PyObject_Del, /* tp_free */
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002810};
2811
Raymond Hettinger625812f2003-01-07 01:58:52 +00002812
2813/*********************** Array Iterator **************************/
2814
Brett Cannon1eb32c22014-10-10 16:26:45 -04002815/*[clinic input]
2816class array.arrayiterator "arrayiterobject *" "&PyArrayIter_Type"
2817[clinic start generated code]*/
2818/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5aefd2d74d8c8e30]*/
Raymond Hettinger625812f2003-01-07 01:58:52 +00002819
2820static PyObject *
2821array_iter(arrayobject *ao)
2822{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 arrayiterobject *it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002825 if (!array_Check(ao)) {
2826 PyErr_BadInternalCall();
2827 return NULL;
2828 }
Raymond Hettinger625812f2003-01-07 01:58:52 +00002829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
2831 if (it == NULL)
2832 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 Py_INCREF(ao);
2835 it->ao = ao;
2836 it->index = 0;
2837 it->getitem = ao->ob_descr->getitem;
2838 PyObject_GC_Track(it);
2839 return (PyObject *)it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002840}
2841
2842static PyObject *
Raymond Hettinger625812f2003-01-07 01:58:52 +00002843arrayiter_next(arrayiterobject *it)
2844{
Serhiy Storchakaab0d1982016-03-30 21:11:16 +03002845 arrayobject *ao;
2846
2847 assert(it != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002848 assert(PyArrayIter_Check(it));
Serhiy Storchakaab0d1982016-03-30 21:11:16 +03002849 ao = it->ao;
2850 if (ao == NULL) {
2851 return NULL;
2852 }
2853 assert(array_Check(ao));
2854 if (it->index < Py_SIZE(ao)) {
2855 return (*it->getitem)(ao, it->index++);
2856 }
2857 it->ao = NULL;
2858 Py_DECREF(ao);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002860}
2861
2862static void
2863arrayiter_dealloc(arrayiterobject *it)
2864{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 PyObject_GC_UnTrack(it);
2866 Py_XDECREF(it->ao);
2867 PyObject_GC_Del(it);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002868}
2869
2870static int
2871arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 Py_VISIT(it->ao);
2874 return 0;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002875}
2876
Brett Cannon1eb32c22014-10-10 16:26:45 -04002877/*[clinic input]
2878array.arrayiterator.__reduce__
2879
2880Return state information for pickling.
2881[clinic start generated code]*/
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002882
2883static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04002884array_arrayiterator___reduce___impl(arrayiterobject *self)
2885/*[clinic end generated code: output=7898a52e8e66e016 input=a062ea1e9951417a]*/
2886{
Serhiy Storchakaab0d1982016-03-30 21:11:16 +03002887 PyObject *func = _PyObject_GetBuiltin("iter");
2888 if (self->ao == NULL) {
2889 return Py_BuildValue("N(())", func);
2890 }
2891 return Py_BuildValue("N(O)n", func, self->ao, self->index);
Brett Cannon1eb32c22014-10-10 16:26:45 -04002892}
2893
2894/*[clinic input]
2895array.arrayiterator.__setstate__
2896
2897 state: object
2898 /
2899
2900Set state information for unpickling.
2901[clinic start generated code]*/
2902
2903static PyObject *
2904array_arrayiterator___setstate__(arrayiterobject *self, PyObject *state)
2905/*[clinic end generated code: output=397da9904e443cbe input=f47d5ceda19e787b]*/
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002906{
2907 Py_ssize_t index = PyLong_AsSsize_t(state);
2908 if (index == -1 && PyErr_Occurred())
2909 return NULL;
2910 if (index < 0)
2911 index = 0;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002912 else if (index > Py_SIZE(self->ao))
2913 index = Py_SIZE(self->ao); /* iterator exhausted */
2914 self->index = index;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002915 Py_RETURN_NONE;
2916}
2917
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002918static PyMethodDef arrayiter_methods[] = {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002919 ARRAY_ARRAYITERATOR___REDUCE___METHODDEF
2920 ARRAY_ARRAYITERATOR___SETSTATE___METHODDEF
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002921 {NULL, NULL} /* sentinel */
2922};
2923
Raymond Hettinger625812f2003-01-07 01:58:52 +00002924static PyTypeObject PyArrayIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 PyVarObject_HEAD_INIT(NULL, 0)
2926 "arrayiterator", /* tp_name */
2927 sizeof(arrayiterobject), /* tp_basicsize */
2928 0, /* tp_itemsize */
2929 /* methods */
2930 (destructor)arrayiter_dealloc, /* tp_dealloc */
2931 0, /* tp_print */
2932 0, /* tp_getattr */
2933 0, /* tp_setattr */
2934 0, /* tp_reserved */
2935 0, /* tp_repr */
2936 0, /* tp_as_number */
2937 0, /* tp_as_sequence */
2938 0, /* tp_as_mapping */
2939 0, /* tp_hash */
2940 0, /* tp_call */
2941 0, /* tp_str */
2942 PyObject_GenericGetAttr, /* tp_getattro */
2943 0, /* tp_setattro */
2944 0, /* tp_as_buffer */
2945 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2946 0, /* tp_doc */
2947 (traverseproc)arrayiter_traverse, /* tp_traverse */
2948 0, /* tp_clear */
2949 0, /* tp_richcompare */
2950 0, /* tp_weaklistoffset */
2951 PyObject_SelfIter, /* tp_iter */
2952 (iternextfunc)arrayiter_next, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002953 arrayiter_methods, /* tp_methods */
Raymond Hettinger625812f2003-01-07 01:58:52 +00002954};
2955
2956
2957/*********************** Install Module **************************/
2958
Martin v. Löwis99866332002-03-01 10:27:01 +00002959/* No functions in array module. */
2960static PyMethodDef a_methods[] = {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002961 ARRAY__ARRAY_RECONSTRUCTOR_METHODDEF
Martin v. Löwis99866332002-03-01 10:27:01 +00002962 {NULL, NULL, 0, NULL} /* Sentinel */
2963};
2964
Nick Coghland5cacbb2015-05-23 22:24:10 +10002965static int
2966array_modexec(PyObject *m)
Guido van Rossum778983b1993-02-19 15:55:02 +00002967{
Georg Brandl4cb0de22011-09-28 21:49:49 +02002968 char buffer[Py_ARRAY_LENGTH(descriptors)], *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002969 PyObject *typecodes;
2970 Py_ssize_t size = 0;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002971 const struct arraydescr *descr;
Fred Drake0d40ba42000-02-04 20:33:49 +00002972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002973 if (PyType_Ready(&Arraytype) < 0)
Nick Coghland5cacbb2015-05-23 22:24:10 +10002974 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975 Py_TYPE(&PyArrayIter_Type) = &PyType_Type;
Fred Drakef4e34842002-04-01 03:45:06 +00002976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 Py_INCREF((PyObject *)&Arraytype);
2978 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
2979 Py_INCREF((PyObject *)&Arraytype);
2980 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00002981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002982 for (descr=descriptors; descr->typecode != '\0'; descr++) {
2983 size++;
2984 }
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00002985
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002986 p = buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2988 *p++ = (char)descr->typecode;
2989 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002990 typecodes = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002991
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002992 PyModule_AddObject(m, "typecodes", typecodes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002993
2994 if (PyErr_Occurred()) {
2995 Py_DECREF(m);
2996 m = NULL;
2997 }
Nick Coghland5cacbb2015-05-23 22:24:10 +10002998 return 0;
2999}
3000
3001static PyModuleDef_Slot arrayslots[] = {
3002 {Py_mod_exec, array_modexec},
3003 {0, NULL}
3004};
3005
3006
3007static struct PyModuleDef arraymodule = {
3008 PyModuleDef_HEAD_INIT,
3009 "array",
3010 module_doc,
3011 0,
3012 a_methods,
3013 arrayslots,
3014 NULL,
3015 NULL,
3016 NULL
3017};
3018
3019
3020PyMODINIT_FUNC
3021PyInit_array(void)
3022{
3023 return PyModuleDef_Init(&arraymodule);
Guido van Rossum778983b1993-02-19 15:55:02 +00003024}