blob: 64e0f172fd7d821305911d15b9825a05adc7e134 [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{
Victor Stinner62bb3942012-08-06 00:46:05 +0200239 return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1);
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
orenmn26d013e2017-03-09 16:06:47 +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;
orenmn26d013e2017-03-09 16:06:47 +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 }
orenmn26d013e2017-03-09 16:06:47 +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,
orenmn26d013e2017-03-09 16:06:47 +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;
orenmn26d013e2017-03-09 16:06:47 +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;
orenmn26d013e2017-03-09 16:06:47 +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 }
orenmn26d013e2017-03-09 16:06:47 +0200416 do_decref = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 }
orenmn26d013e2017-03-09 16:06:47 +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;
orenmn26d013e2017-03-09 16:06:47 +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;
orenmn26d013e2017-03-09 16:06:47 +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 }
orenmn26d013e2017-03-09 16:06:47 +0200469 do_decref = 1;
Meador Inge1c9f0c92011-09-20 19:55:51 -0500470 }
orenmn26d013e2017-03-09 16:06:47 +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;
orenmn26d013e2017-03-09 16:06:47 +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;
1026 Py_INCREF(Py_None);
1027 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001028}
1029
Brett Cannon1eb32c22014-10-10 16:26:45 -04001030/*[clinic input]
1031array.array.count
1032
1033 v: object
1034 /
1035
1036Return number of occurrences of v in the array.
1037[clinic start generated code]*/
1038
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001039static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001040array_array_count(arrayobject *self, PyObject *v)
1041/*[clinic end generated code: output=3dd3624bf7135a3a input=d9bce9d65e39d1f5]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001042{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 Py_ssize_t count = 0;
1044 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 for (i = 0; i < Py_SIZE(self); i++) {
Victor Stinner0b142e22013-07-17 23:01:30 +02001047 PyObject *selfi;
1048 int cmp;
1049
1050 selfi = getarrayitem((PyObject *)self, i);
1051 if (selfi == NULL)
1052 return NULL;
1053 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 Py_DECREF(selfi);
1055 if (cmp > 0)
1056 count++;
1057 else if (cmp < 0)
1058 return NULL;
1059 }
1060 return PyLong_FromSsize_t(count);
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001061}
1062
Brett Cannon1eb32c22014-10-10 16:26:45 -04001063
1064/*[clinic input]
1065array.array.index
1066
1067 v: object
1068 /
1069
1070Return index of first occurrence of v in the array.
1071[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001072
1073static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001074array_array_index(arrayobject *self, PyObject *v)
1075/*[clinic end generated code: output=d48498d325602167 input=cf619898c6649d08]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001076{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 Py_ssize_t i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 for (i = 0; i < Py_SIZE(self); i++) {
Victor Stinner0b142e22013-07-17 23:01:30 +02001080 PyObject *selfi;
1081 int cmp;
1082
1083 selfi = getarrayitem((PyObject *)self, i);
1084 if (selfi == NULL)
1085 return NULL;
1086 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 Py_DECREF(selfi);
1088 if (cmp > 0) {
1089 return PyLong_FromLong((long)i);
1090 }
1091 else if (cmp < 0)
1092 return NULL;
1093 }
1094 PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
1095 return NULL;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001096}
1097
Raymond Hettinger625812f2003-01-07 01:58:52 +00001098static int
1099array_contains(arrayobject *self, PyObject *v)
1100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 Py_ssize_t i;
1102 int cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +00001103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) {
1105 PyObject *selfi = getarrayitem((PyObject *)self, i);
Victor Stinner0b142e22013-07-17 23:01:30 +02001106 if (selfi == NULL)
Victor Stinner4755bea2013-07-18 01:12:35 +02001107 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
1109 Py_DECREF(selfi);
1110 }
1111 return cmp;
Raymond Hettinger625812f2003-01-07 01:58:52 +00001112}
1113
Brett Cannon1eb32c22014-10-10 16:26:45 -04001114/*[clinic input]
1115array.array.remove
1116
1117 v: object
1118 /
1119
1120Remove the first occurrence of v in the array.
1121[clinic start generated code]*/
1122
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001123static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001124array_array_remove(arrayobject *self, PyObject *v)
1125/*[clinic end generated code: output=bef06be9fdf9dceb input=0b1e5aed25590027]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 int i;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 for (i = 0; i < Py_SIZE(self); i++) {
Victor Stinner0b142e22013-07-17 23:01:30 +02001130 PyObject *selfi;
1131 int cmp;
1132
1133 selfi = getarrayitem((PyObject *)self,i);
1134 if (selfi == NULL)
1135 return NULL;
1136 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 Py_DECREF(selfi);
1138 if (cmp > 0) {
Martin Panter996d72b2016-07-25 02:21:14 +00001139 if (array_del_slice(self, i, i+1) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 return NULL;
1141 Py_INCREF(Py_None);
1142 return Py_None;
1143 }
1144 else if (cmp < 0)
1145 return NULL;
1146 }
1147 PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
1148 return NULL;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001149}
1150
Brett Cannon1eb32c22014-10-10 16:26:45 -04001151/*[clinic input]
1152array.array.pop
1153
1154 i: Py_ssize_t = -1
1155 /
1156
1157Return the i-th element and delete it from the array.
1158
1159i defaults to -1.
1160[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001161
1162static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001163array_array_pop_impl(arrayobject *self, Py_ssize_t i)
1164/*[clinic end generated code: output=bc1f0c54fe5308e4 input=8e5feb4c1a11cd44]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 PyObject *v;
Brett Cannon1eb32c22014-10-10 16:26:45 -04001167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 if (Py_SIZE(self) == 0) {
1169 /* Special-case most common failure cause */
1170 PyErr_SetString(PyExc_IndexError, "pop from empty array");
1171 return NULL;
1172 }
1173 if (i < 0)
1174 i += Py_SIZE(self);
1175 if (i < 0 || i >= Py_SIZE(self)) {
1176 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1177 return NULL;
1178 }
Victor Stinner0b142e22013-07-17 23:01:30 +02001179 v = getarrayitem((PyObject *)self, i);
1180 if (v == NULL)
1181 return NULL;
Martin Panter996d72b2016-07-25 02:21:14 +00001182 if (array_del_slice(self, i, i+1) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 Py_DECREF(v);
1184 return NULL;
1185 }
1186 return v;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001187}
1188
Brett Cannon1eb32c22014-10-10 16:26:45 -04001189/*[clinic input]
1190array.array.extend
1191
1192 bb: object
1193 /
1194
1195Append items to the end of the array.
1196[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001197
1198static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001199array_array_extend(arrayobject *self, PyObject *bb)
1200/*[clinic end generated code: output=bbddbc8e8bef871d input=43be86aba5c31e44]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 if (array_do_extend(self, bb) == -1)
1203 return NULL;
1204 Py_INCREF(Py_None);
1205 return Py_None;
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001206}
1207
Brett Cannon1eb32c22014-10-10 16:26:45 -04001208/*[clinic input]
1209array.array.insert
1210
1211 i: Py_ssize_t
1212 v: object
1213 /
1214
1215Insert a new item v into the array before position i.
1216[clinic start generated code]*/
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00001217
1218static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001219array_array_insert_impl(arrayobject *self, Py_ssize_t i, PyObject *v)
1220/*[clinic end generated code: output=5a3648e278348564 input=5577d1b4383e9313]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001221{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 return ins(self, i, v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001223}
1224
Brett Cannon1eb32c22014-10-10 16:26:45 -04001225/*[clinic input]
1226array.array.buffer_info
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001227
Brett Cannon1eb32c22014-10-10 16:26:45 -04001228Return a tuple (address, length) giving the current memory address and the length in items of the buffer used to hold array's contents.
1229
1230The length should be multiplied by the itemsize attribute to calculate
1231the buffer length in bytes.
1232[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001233
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001234static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001235array_array_buffer_info_impl(arrayobject *self)
1236/*[clinic end generated code: output=9b2a4ec3ae7e98e7 input=a58bae5c6e1ac6a6]*/
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001237{
Victor Stinner541067a2013-11-14 01:27:12 +01001238 PyObject *retval = NULL, *v;
1239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 retval = PyTuple_New(2);
1241 if (!retval)
1242 return NULL;
Fred Drake541dc3b2000-06-28 17:49:30 +00001243
Victor Stinner541067a2013-11-14 01:27:12 +01001244 v = PyLong_FromVoidPtr(self->ob_item);
1245 if (v == NULL) {
1246 Py_DECREF(retval);
1247 return NULL;
1248 }
1249 PyTuple_SET_ITEM(retval, 0, v);
1250
Serhiy Storchaka9e941d62016-06-23 23:55:34 +03001251 v = PyLong_FromSsize_t(Py_SIZE(self));
Victor Stinner541067a2013-11-14 01:27:12 +01001252 if (v == NULL) {
1253 Py_DECREF(retval);
1254 return NULL;
1255 }
1256 PyTuple_SET_ITEM(retval, 1, v);
Fred Drake541dc3b2000-06-28 17:49:30 +00001257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 return retval;
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001259}
1260
Brett Cannon1eb32c22014-10-10 16:26:45 -04001261/*[clinic input]
1262array.array.append
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001263
Brett Cannon1eb32c22014-10-10 16:26:45 -04001264 v: object
1265 /
1266
1267Append new value v to the end of the array.
1268[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001269
Guido van Rossumde4a4ca1997-08-12 14:55:56 +00001270static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001271array_array_append(arrayobject *self, PyObject *v)
1272/*[clinic end generated code: output=745a0669bf8db0e2 input=0b98d9d78e78f0fa]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001273{
Mark Dickinson346f0af2010-08-06 09:36:57 +00001274 return ins(self, Py_SIZE(self), v);
Guido van Rossum778983b1993-02-19 15:55:02 +00001275}
1276
Brett Cannon1eb32c22014-10-10 16:26:45 -04001277/*[clinic input]
1278array.array.byteswap
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001279
Brett Cannon1eb32c22014-10-10 16:26:45 -04001280Byteswap all items of the array.
1281
1282If the items in the array are not 1, 2, 4, or 8 bytes in size, RuntimeError is
1283raised.
1284[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001285
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001286static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001287array_array_byteswap_impl(arrayobject *self)
1288/*[clinic end generated code: output=5f8236cbdf0d90b5 input=6a85591b950a0186]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 char *p;
1291 Py_ssize_t i;
Fred Drakebf272981999-12-03 17:15:30 +00001292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 switch (self->ob_descr->itemsize) {
1294 case 1:
1295 break;
1296 case 2:
1297 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) {
1298 char p0 = p[0];
1299 p[0] = p[1];
1300 p[1] = p0;
1301 }
1302 break;
1303 case 4:
1304 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 4) {
1305 char p0 = p[0];
1306 char p1 = p[1];
1307 p[0] = p[3];
1308 p[1] = p[2];
1309 p[2] = p1;
1310 p[3] = p0;
1311 }
1312 break;
1313 case 8:
1314 for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) {
1315 char p0 = p[0];
1316 char p1 = p[1];
1317 char p2 = p[2];
1318 char p3 = p[3];
1319 p[0] = p[7];
1320 p[1] = p[6];
1321 p[2] = p[5];
1322 p[3] = p[4];
1323 p[4] = p3;
1324 p[5] = p2;
1325 p[6] = p1;
1326 p[7] = p0;
1327 }
1328 break;
1329 default:
1330 PyErr_SetString(PyExc_RuntimeError,
1331 "don't know how to byteswap this array type");
1332 return NULL;
1333 }
1334 Py_INCREF(Py_None);
1335 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001336}
1337
Brett Cannon1eb32c22014-10-10 16:26:45 -04001338/*[clinic input]
1339array.array.reverse
1340
1341Reverse the order of the items in the array.
1342[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001343
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001344static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001345array_array_reverse_impl(arrayobject *self)
1346/*[clinic end generated code: output=c04868b36f6f4089 input=cd904f01b27d966a]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001347{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001348 Py_ssize_t itemsize = self->ob_descr->itemsize;
1349 char *p, *q;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 /* little buffer to hold items while swapping */
1351 char tmp[256]; /* 8 is probably enough -- but why skimp */
1352 assert((size_t)itemsize <= sizeof(tmp));
Guido van Rossume77a7571993-11-03 15:01:26 +00001353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 if (Py_SIZE(self) > 1) {
1355 for (p = self->ob_item,
1356 q = self->ob_item + (Py_SIZE(self) - 1)*itemsize;
1357 p < q;
1358 p += itemsize, q -= itemsize) {
1359 /* memory areas guaranteed disjoint, so memcpy
1360 * is safe (& memmove may be slower).
1361 */
1362 memcpy(tmp, p, itemsize);
1363 memcpy(p, q, itemsize);
1364 memcpy(q, tmp, itemsize);
1365 }
1366 }
Tim Petersbb307342000-09-10 05:22:54 +00001367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 Py_INCREF(Py_None);
1369 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001370}
Guido van Rossume77a7571993-11-03 15:01:26 +00001371
Brett Cannon1eb32c22014-10-10 16:26:45 -04001372/*[clinic input]
1373array.array.fromfile
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001374
Brett Cannon1eb32c22014-10-10 16:26:45 -04001375 f: object
1376 n: Py_ssize_t
1377 /
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001378
Brett Cannon1eb32c22014-10-10 16:26:45 -04001379Read n objects from the file object f and append them to the end of the array.
1380[clinic start generated code]*/
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001381
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001382static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001383array_array_fromfile_impl(arrayobject *self, PyObject *f, Py_ssize_t n)
1384/*[clinic end generated code: output=ec9f600e10f53510 input=e188afe8e58adf40]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001385{
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03001386 PyObject *b, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 Py_ssize_t itemsize = self->ob_descr->itemsize;
Brett Cannon1eb32c22014-10-10 16:26:45 -04001388 Py_ssize_t nbytes;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001389 _Py_IDENTIFIER(read);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 int not_enough_bytes;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001391
Mark Dickinsonc04ddff2012-10-06 18:04:49 +01001392 if (n < 0) {
1393 PyErr_SetString(PyExc_ValueError, "negative count");
1394 return NULL;
1395 }
1396 if (n > PY_SSIZE_T_MAX / itemsize) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 PyErr_NoMemory();
1398 return NULL;
1399 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +01001400 nbytes = n * itemsize;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001401
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001402 b = _PyObject_CallMethodId(f, &PyId_read, "n", nbytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 if (b == NULL)
1404 return NULL;
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 if (!PyBytes_Check(b)) {
1407 PyErr_SetString(PyExc_TypeError,
1408 "read() didn't return bytes");
1409 Py_DECREF(b);
1410 return NULL;
1411 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 not_enough_bytes = (PyBytes_GET_SIZE(b) != nbytes);
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001414
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03001415 res = array_array_frombytes(self, b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 Py_DECREF(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 if (res == NULL)
1418 return NULL;
Hirokazu Yamamoto54d0df62009-03-06 03:04:07 +00001419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 if (not_enough_bytes) {
1421 PyErr_SetString(PyExc_EOFError,
1422 "read() didn't return enough bytes");
1423 Py_DECREF(res);
1424 return NULL;
1425 }
Guido van Rossum2c94aa52007-05-24 19:02:32 +00001426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 return res;
Guido van Rossum778983b1993-02-19 15:55:02 +00001428}
1429
Brett Cannon1eb32c22014-10-10 16:26:45 -04001430/*[clinic input]
1431array.array.tofile
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001432
Brett Cannon1eb32c22014-10-10 16:26:45 -04001433 f: object
1434 /
1435
1436Write all items (as machine values) to the file object f.
1437[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001438
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001439static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001440array_array_tofile(arrayobject *self, PyObject *f)
1441/*[clinic end generated code: output=3a2cfa8128df0777 input=b0669a484aab0831]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize;
1444 /* Write 64K blocks at a time */
1445 /* XXX Make the block size settable */
1446 int BLOCKSIZE = 64*1024;
1447 Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE;
1448 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 if (Py_SIZE(self) == 0)
1451 goto done;
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 for (i = 0; i < nblocks; i++) {
1454 char* ptr = self->ob_item + i*BLOCKSIZE;
1455 Py_ssize_t size = BLOCKSIZE;
1456 PyObject *bytes, *res;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001457 _Py_IDENTIFIER(write);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 if (i*BLOCKSIZE + size > nbytes)
1460 size = nbytes - i*BLOCKSIZE;
1461 bytes = PyBytes_FromStringAndSize(ptr, size);
1462 if (bytes == NULL)
1463 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001464 res = _PyObject_CallMethodId(f, &PyId_write, "O", bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 Py_DECREF(bytes);
1466 if (res == NULL)
1467 return NULL;
1468 Py_DECREF(res); /* drop write result */
1469 }
Guido van Rossumb5ddcfd2007-04-11 17:08:28 +00001470
1471 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 Py_INCREF(Py_None);
1473 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001474}
1475
Brett Cannon1eb32c22014-10-10 16:26:45 -04001476/*[clinic input]
1477array.array.fromlist
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001478
Brett Cannon1eb32c22014-10-10 16:26:45 -04001479 list: object
1480 /
1481
1482Append items to array from list.
1483[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001484
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001485static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001486array_array_fromlist(arrayobject *self, PyObject *list)
1487/*[clinic end generated code: output=26411c2d228a3e3f input=be2605a96c49680f]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 Py_ssize_t n;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 if (!PyList_Check(list)) {
1492 PyErr_SetString(PyExc_TypeError, "arg must be list");
1493 return NULL;
1494 }
1495 n = PyList_Size(list);
1496 if (n > 0) {
1497 Py_ssize_t i, old_size;
1498 old_size = Py_SIZE(self);
1499 if (array_resize(self, old_size + n) == -1)
1500 return NULL;
1501 for (i = 0; i < n; i++) {
1502 PyObject *v = PyList_GetItem(list, i);
1503 if ((*self->ob_descr->setitem)(self,
1504 Py_SIZE(self) - n + i, v) != 0) {
1505 array_resize(self, old_size);
1506 return NULL;
1507 }
1508 }
1509 }
1510 Py_INCREF(Py_None);
1511 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001512}
1513
Brett Cannon1eb32c22014-10-10 16:26:45 -04001514/*[clinic input]
1515array.array.tolist
1516
1517Convert array to an ordinary list with the same items.
1518[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001519
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001520static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001521array_array_tolist_impl(arrayobject *self)
1522/*[clinic end generated code: output=00b60cc9eab8ef89 input=a8d7784a94f86b53]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 PyObject *list = PyList_New(Py_SIZE(self));
1525 Py_ssize_t i;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +00001526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 if (list == NULL)
1528 return NULL;
1529 for (i = 0; i < Py_SIZE(self); i++) {
1530 PyObject *v = getarrayitem((PyObject *)self, i);
Victor Stinner4755bea2013-07-18 01:12:35 +02001531 if (v == NULL)
1532 goto error;
1533 if (PyList_SetItem(list, i, v) < 0)
1534 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 }
1536 return list;
Victor Stinner4755bea2013-07-18 01:12:35 +02001537
1538error:
1539 Py_DECREF(list);
1540 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00001541}
1542
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001543static PyObject *
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001544frombytes(arrayobject *self, Py_buffer *buffer)
Guido van Rossum778983b1993-02-19 15:55:02 +00001545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 int itemsize = self->ob_descr->itemsize;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001547 Py_ssize_t n;
1548 if (buffer->itemsize != 1) {
1549 PyBuffer_Release(buffer);
Serhiy Storchakab757c832014-12-05 22:25:22 +02001550 PyErr_SetString(PyExc_TypeError, "a bytes-like object is required");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 return NULL;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001552 }
1553 n = buffer->len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 if (n % itemsize != 0) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001555 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 PyErr_SetString(PyExc_ValueError,
Serhiy Storchakab757c832014-12-05 22:25:22 +02001557 "bytes length not a multiple of item size");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 return NULL;
1559 }
1560 n = n / itemsize;
1561 if (n > 0) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001562 Py_ssize_t old_size = Py_SIZE(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 if ((n > PY_SSIZE_T_MAX - old_size) ||
1564 ((old_size + n) > PY_SSIZE_T_MAX / itemsize)) {
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001565 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 return PyErr_NoMemory();
1567 }
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001568 if (array_resize(self, old_size + n) == -1) {
1569 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 return NULL;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001571 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 memcpy(self->ob_item + old_size * itemsize,
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001573 buffer->buf, n * itemsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 }
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001575 PyBuffer_Release(buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 Py_INCREF(Py_None);
1577 return Py_None;
Guido van Rossum778983b1993-02-19 15:55:02 +00001578}
1579
Brett Cannon1eb32c22014-10-10 16:26:45 -04001580/*[clinic input]
1581array.array.fromstring
1582
Larry Hastingsdbfdc382015-05-04 06:59:46 -07001583 buffer: Py_buffer(accept={str, buffer})
Brett Cannon1eb32c22014-10-10 16:26:45 -04001584 /
1585
1586Appends 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).
1587
1588This method is deprecated. Use frombytes instead.
1589[clinic start generated code]*/
1590
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001591static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001592array_array_fromstring_impl(arrayobject *self, Py_buffer *buffer)
Larry Hastingsdbfdc382015-05-04 06:59:46 -07001593/*[clinic end generated code: output=31c4baa779df84ce input=a3341a512e11d773]*/
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001594{
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001595 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1596 "fromstring() is deprecated. Use frombytes() instead.", 2) != 0)
1597 return NULL;
Brett Cannon1eb32c22014-10-10 16:26:45 -04001598 return frombytes(self, buffer);
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001599}
1600
Brett Cannon1eb32c22014-10-10 16:26:45 -04001601/*[clinic input]
1602array.array.frombytes
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001603
Brett Cannon1eb32c22014-10-10 16:26:45 -04001604 buffer: Py_buffer
1605 /
1606
1607Appends 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).
1608[clinic start generated code]*/
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001609
1610static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001611array_array_frombytes_impl(arrayobject *self, Py_buffer *buffer)
1612/*[clinic end generated code: output=d9842c8f7510a516 input=2bbf2b53ebfcc988]*/
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001613{
Brett Cannon1eb32c22014-10-10 16:26:45 -04001614 return frombytes(self, buffer);
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001615}
1616
Brett Cannon1eb32c22014-10-10 16:26:45 -04001617/*[clinic input]
1618array.array.tobytes
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001619
Brett Cannon1eb32c22014-10-10 16:26:45 -04001620Convert the array to an array of machine values and return the bytes representation.
1621[clinic start generated code]*/
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001622
Roger E. Masse2919eaa1996-12-09 20:10:36 +00001623static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001624array_array_tobytes_impl(arrayobject *self)
1625/*[clinic end generated code: output=87318e4edcdc2bb6 input=90ee495f96de34f5]*/
Guido van Rossum778983b1993-02-19 15:55:02 +00001626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
1628 return PyBytes_FromStringAndSize(self->ob_item,
1629 Py_SIZE(self) * self->ob_descr->itemsize);
1630 } else {
1631 return PyErr_NoMemory();
1632 }
Guido van Rossum778983b1993-02-19 15:55:02 +00001633}
1634
Brett Cannon1eb32c22014-10-10 16:26:45 -04001635/*[clinic input]
1636array.array.tostring
Guido van Rossumb39b90d1998-10-13 14:27:22 +00001637
Brett Cannon1eb32c22014-10-10 16:26:45 -04001638Convert the array to an array of machine values and return the bytes representation.
1639
1640This method is deprecated. Use tobytes instead.
1641[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001642
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001643static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001644array_array_tostring_impl(arrayobject *self)
1645/*[clinic end generated code: output=7d6bd92745a2c8f3 input=b6c0ddee7b30457e]*/
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001646{
Victor Stinner9f0b51e2010-11-09 09:38:30 +00001647 if (PyErr_WarnEx(PyExc_DeprecationWarning,
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001648 "tostring() is deprecated. Use tobytes() instead.", 2) != 0)
1649 return NULL;
Brett Cannon1eb32c22014-10-10 16:26:45 -04001650 return array_array_tobytes_impl(self);
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001651}
1652
Brett Cannon1eb32c22014-10-10 16:26:45 -04001653/*[clinic input]
1654array.array.fromunicode
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +00001655
Larry Hastings38337d12015-05-07 23:30:09 -07001656 ustr: Py_UNICODE(zeroes=True)
Brett Cannon1eb32c22014-10-10 16:26:45 -04001657 /
1658
1659Extends this array with data from the unicode string ustr.
1660
1661The array must be a unicode type array; otherwise a ValueError is raised.
1662Use array.frombytes(ustr.encode(...)) to append Unicode data to an array of
1663some other type.
1664[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001665
Martin v. Löwis99866332002-03-01 10:27:01 +00001666static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001667array_array_fromunicode_impl(arrayobject *self, Py_UNICODE *ustr,
1668 Py_ssize_clean_t ustr_length)
Larry Hastings38337d12015-05-07 23:30:09 -07001669/*[clinic end generated code: output=ebb72fc16975e06d input=150f00566ffbca6e]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001670{
Victor Stinner62bb3942012-08-06 00:46:05 +02001671 char typecode;
Martin v. Löwis99866332002-03-01 10:27:01 +00001672
Victor Stinner62bb3942012-08-06 00:46:05 +02001673 typecode = self->ob_descr->typecode;
Gregory P. Smith9504b132012-12-10 20:20:20 -08001674 if (typecode != 'u') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 PyErr_SetString(PyExc_ValueError,
1676 "fromunicode() may only be called on "
1677 "unicode type arrays");
1678 return NULL;
1679 }
Brett Cannon1eb32c22014-10-10 16:26:45 -04001680 if (ustr_length > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 Py_ssize_t old_size = Py_SIZE(self);
Brett Cannon1eb32c22014-10-10 16:26:45 -04001682 if (array_resize(self, old_size + ustr_length) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 return NULL;
Victor Stinner62bb3942012-08-06 00:46:05 +02001684 memcpy(self->ob_item + old_size * sizeof(Py_UNICODE),
Brett Cannon1eb32c22014-10-10 16:26:45 -04001685 ustr, ustr_length * sizeof(Py_UNICODE));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 }
Martin v. Löwis99866332002-03-01 10:27:01 +00001687
Brett Cannon1eb32c22014-10-10 16:26:45 -04001688 Py_RETURN_NONE;
Martin v. Löwis99866332002-03-01 10:27:01 +00001689}
1690
Brett Cannon1eb32c22014-10-10 16:26:45 -04001691/*[clinic input]
1692array.array.tounicode
Martin v. Löwis99866332002-03-01 10:27:01 +00001693
Brett Cannon1eb32c22014-10-10 16:26:45 -04001694Extends this array with data from the unicode string ustr.
1695
1696Convert the array to a unicode string. The array must be a unicode type array;
1697otherwise a ValueError is raised. Use array.tobytes().decode() to obtain a
1698unicode string from an array of some other type.
1699[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001700
1701static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001702array_array_tounicode_impl(arrayobject *self)
1703/*[clinic end generated code: output=08e442378336e1ef input=127242eebe70b66d]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001704{
Victor Stinner62bb3942012-08-06 00:46:05 +02001705 char typecode;
1706 typecode = self->ob_descr->typecode;
Gregory P. Smith9504b132012-12-10 20:20:20 -08001707 if (typecode != 'u') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 PyErr_SetString(PyExc_ValueError,
1709 "tounicode() may only be called on unicode type arrays");
1710 return NULL;
1711 }
Victor Stinner62bb3942012-08-06 00:46:05 +02001712 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_SIZE(self));
Martin v. Löwis99866332002-03-01 10:27:01 +00001713}
1714
Brett Cannon1eb32c22014-10-10 16:26:45 -04001715/*[clinic input]
1716array.array.__sizeof__
Martin v. Löwis99866332002-03-01 10:27:01 +00001717
Brett Cannon1eb32c22014-10-10 16:26:45 -04001718Size of the array in memory, in bytes.
1719[clinic start generated code]*/
Martin v. Löwis99866332002-03-01 10:27:01 +00001720
Meador Inge03b4d502012-08-10 22:35:45 -05001721static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04001722array_array___sizeof___impl(arrayobject *self)
1723/*[clinic end generated code: output=d8e1c61ebbe3eaed input=805586565bf2b3c6]*/
Meador Inge03b4d502012-08-10 22:35:45 -05001724{
1725 Py_ssize_t res;
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02001726 res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * self->ob_descr->itemsize;
Meador Inge03b4d502012-08-10 22:35:45 -05001727 return PyLong_FromSsize_t(res);
1728}
1729
Martin v. Löwis99866332002-03-01 10:27:01 +00001730
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001731/*********************** Pickling support ************************/
1732
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001733static const struct mformatdescr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 size_t size;
1735 int is_signed;
1736 int is_big_endian;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001737} mformat_descriptors[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 {1, 0, 0}, /* 0: UNSIGNED_INT8 */
1739 {1, 1, 0}, /* 1: SIGNED_INT8 */
1740 {2, 0, 0}, /* 2: UNSIGNED_INT16_LE */
1741 {2, 0, 1}, /* 3: UNSIGNED_INT16_BE */
1742 {2, 1, 0}, /* 4: SIGNED_INT16_LE */
1743 {2, 1, 1}, /* 5: SIGNED_INT16_BE */
1744 {4, 0, 0}, /* 6: UNSIGNED_INT32_LE */
1745 {4, 0, 1}, /* 7: UNSIGNED_INT32_BE */
1746 {4, 1, 0}, /* 8: SIGNED_INT32_LE */
1747 {4, 1, 1}, /* 9: SIGNED_INT32_BE */
1748 {8, 0, 0}, /* 10: UNSIGNED_INT64_LE */
1749 {8, 0, 1}, /* 11: UNSIGNED_INT64_BE */
1750 {8, 1, 0}, /* 12: SIGNED_INT64_LE */
1751 {8, 1, 1}, /* 13: SIGNED_INT64_BE */
1752 {4, 0, 0}, /* 14: IEEE_754_FLOAT_LE */
1753 {4, 0, 1}, /* 15: IEEE_754_FLOAT_BE */
1754 {8, 0, 0}, /* 16: IEEE_754_DOUBLE_LE */
1755 {8, 0, 1}, /* 17: IEEE_754_DOUBLE_BE */
1756 {4, 0, 0}, /* 18: UTF16_LE */
1757 {4, 0, 1}, /* 19: UTF16_BE */
1758 {8, 0, 0}, /* 20: UTF32_LE */
1759 {8, 0, 1} /* 21: UTF32_BE */
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001760};
1761
1762
1763/*
1764 * Internal: This function is used to find the machine format of a given
1765 * array type code. This returns UNKNOWN_FORMAT when the machine format cannot
1766 * be found.
1767 */
1768static enum machine_format_code
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001769typecode_to_mformat_code(char typecode)
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001770{
Christian Heimes743e0cd2012-10-17 23:52:17 +02001771 const int is_big_endian = PY_BIG_ENDIAN;
1772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 size_t intsize;
1774 int is_signed;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 switch (typecode) {
1777 case 'b':
1778 return SIGNED_INT8;
1779 case 'B':
1780 return UNSIGNED_INT8;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 case 'u':
Victor Stinner62bb3942012-08-06 00:46:05 +02001783 if (sizeof(Py_UNICODE) == 2) {
1784 return UTF16_LE + is_big_endian;
1785 }
1786 if (sizeof(Py_UNICODE) == 4) {
1787 return UTF32_LE + is_big_endian;
1788 }
1789 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 case 'f':
1792 if (sizeof(float) == 4) {
1793 const float y = 16711938.0;
1794 if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0)
1795 return IEEE_754_FLOAT_BE;
1796 if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0)
1797 return IEEE_754_FLOAT_LE;
1798 }
1799 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 case 'd':
1802 if (sizeof(double) == 8) {
1803 const double x = 9006104071832581.0;
1804 if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0)
1805 return IEEE_754_DOUBLE_BE;
1806 if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
1807 return IEEE_754_DOUBLE_LE;
1808 }
1809 return UNKNOWN_FORMAT;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 /* Integers */
1812 case 'h':
1813 intsize = sizeof(short);
1814 is_signed = 1;
1815 break;
1816 case 'H':
1817 intsize = sizeof(short);
1818 is_signed = 0;
1819 break;
1820 case 'i':
1821 intsize = sizeof(int);
1822 is_signed = 1;
1823 break;
1824 case 'I':
1825 intsize = sizeof(int);
1826 is_signed = 0;
1827 break;
1828 case 'l':
1829 intsize = sizeof(long);
1830 is_signed = 1;
1831 break;
1832 case 'L':
1833 intsize = sizeof(long);
1834 is_signed = 0;
1835 break;
Meador Inge1c9f0c92011-09-20 19:55:51 -05001836 case 'q':
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001837 intsize = sizeof(long long);
Meador Inge1c9f0c92011-09-20 19:55:51 -05001838 is_signed = 1;
1839 break;
1840 case 'Q':
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001841 intsize = sizeof(long long);
Meador Inge1c9f0c92011-09-20 19:55:51 -05001842 is_signed = 0;
1843 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 default:
1845 return UNKNOWN_FORMAT;
1846 }
1847 switch (intsize) {
1848 case 2:
1849 return UNSIGNED_INT16_LE + is_big_endian + (2 * is_signed);
1850 case 4:
1851 return UNSIGNED_INT32_LE + is_big_endian + (2 * is_signed);
1852 case 8:
1853 return UNSIGNED_INT64_LE + is_big_endian + (2 * is_signed);
1854 default:
1855 return UNKNOWN_FORMAT;
1856 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001857}
1858
1859/* Forward declaration. */
1860static PyObject *array_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
1861
1862/*
1863 * Internal: This function wraps the array constructor--i.e., array_new()--to
1864 * allow the creation of array objects from C code without having to deal
1865 * directly the tuple argument of array_new(). The typecode argument is a
1866 * Unicode character value, like 'i' or 'f' for example, representing an array
1867 * type code. The items argument is a bytes or a list object from which
1868 * contains the initial value of the array.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 *
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001870 * On success, this functions returns the array object created. Otherwise,
1871 * NULL is returned to indicate a failure.
1872 */
1873static PyObject *
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001874make_array(PyTypeObject *arraytype, char typecode, PyObject *items)
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 PyObject *new_args;
1877 PyObject *array_obj;
1878 PyObject *typecode_obj;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 assert(arraytype != NULL);
1881 assert(items != NULL);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001882
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001883 typecode_obj = PyUnicode_FromOrdinal(typecode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 if (typecode_obj == NULL)
1885 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 new_args = PyTuple_New(2);
1888 if (new_args == NULL)
1889 return NULL;
1890 Py_INCREF(items);
1891 PyTuple_SET_ITEM(new_args, 0, typecode_obj);
1892 PyTuple_SET_ITEM(new_args, 1, items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 array_obj = array_new(arraytype, new_args, NULL);
1895 Py_DECREF(new_args);
1896 if (array_obj == NULL)
1897 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 return array_obj;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001900}
1901
1902/*
1903 * This functions is a special constructor used when unpickling an array. It
1904 * provides a portable way to rebuild an array from its memory representation.
1905 */
Brett Cannon1eb32c22014-10-10 16:26:45 -04001906/*[clinic input]
1907array._array_reconstructor
1908
1909 arraytype: object(type="PyTypeObject *")
Larry Hastingsdbfdc382015-05-04 06:59:46 -07001910 typecode: int(accept={str})
Brett Cannon1eb32c22014-10-10 16:26:45 -04001911 mformat_code: int(type="enum machine_format_code")
1912 items: object
1913 /
1914
1915Internal. Used for pickling support.
1916[clinic start generated code]*/
1917
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001918static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001919array__array_reconstructor_impl(PyObject *module, PyTypeObject *arraytype,
Larry Hastings89964c42015-04-14 18:07:59 -04001920 int typecode,
1921 enum machine_format_code mformat_code,
1922 PyObject *items)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001923/*[clinic end generated code: output=e05263141ba28365 input=2464dc8f4c7736b5]*/
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001924{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 PyObject *converted_items;
1926 PyObject *result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001927 const struct arraydescr *descr;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 if (!PyType_Check(arraytype)) {
1930 PyErr_Format(PyExc_TypeError,
1931 "first argument must a type object, not %.200s",
1932 Py_TYPE(arraytype)->tp_name);
1933 return NULL;
1934 }
1935 if (!PyType_IsSubtype(arraytype, &Arraytype)) {
1936 PyErr_Format(PyExc_TypeError,
1937 "%.200s is not a subtype of %.200s",
1938 arraytype->tp_name, Arraytype.tp_name);
1939 return NULL;
1940 }
1941 for (descr = descriptors; descr->typecode != '\0'; descr++) {
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001942 if ((int)descr->typecode == typecode)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 break;
1944 }
1945 if (descr->typecode == '\0') {
1946 PyErr_SetString(PyExc_ValueError,
1947 "second argument must be a valid type code");
1948 return NULL;
1949 }
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001950 if (mformat_code < MACHINE_FORMAT_CODE_MIN ||
1951 mformat_code > MACHINE_FORMAT_CODE_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 PyErr_SetString(PyExc_ValueError,
1953 "third argument must be a valid machine format code.");
1954 return NULL;
1955 }
1956 if (!PyBytes_Check(items)) {
1957 PyErr_Format(PyExc_TypeError,
1958 "fourth argument should be bytes, not %.200s",
1959 Py_TYPE(items)->tp_name);
1960 return NULL;
1961 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 /* Fast path: No decoding has to be done. */
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001964 if (mformat_code == typecode_to_mformat_code((char)typecode) ||
1965 mformat_code == UNKNOWN_FORMAT) {
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02001966 return make_array(arraytype, (char)typecode, items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 /* Slow path: Decode the byte string according to the given machine
1970 * format code. This occurs when the computer unpickling the array
1971 * object is architecturally different from the one that pickled the
1972 * array.
1973 */
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001974 if (Py_SIZE(items) % mformat_descriptors[mformat_code].size != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 PyErr_SetString(PyExc_ValueError,
1976 "string length not a multiple of item size");
1977 return NULL;
1978 }
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001979 switch (mformat_code) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 case IEEE_754_FLOAT_LE:
1981 case IEEE_754_FLOAT_BE: {
1982 int i;
Larry Hastingsdfbeb162014-10-13 10:39:41 +01001983 int le = (mformat_code == IEEE_754_FLOAT_LE) ? 1 : 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 Py_ssize_t itemcount = Py_SIZE(items) / 4;
1985 const unsigned char *memstr =
1986 (unsigned char *)PyBytes_AS_STRING(items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00001987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 converted_items = PyList_New(itemcount);
1989 if (converted_items == NULL)
1990 return NULL;
1991 for (i = 0; i < itemcount; i++) {
1992 PyObject *pyfloat = PyFloat_FromDouble(
1993 _PyFloat_Unpack4(&memstr[i * 4], le));
1994 if (pyfloat == NULL) {
1995 Py_DECREF(converted_items);
1996 return NULL;
1997 }
1998 PyList_SET_ITEM(converted_items, i, pyfloat);
1999 }
2000 break;
2001 }
2002 case IEEE_754_DOUBLE_LE:
2003 case IEEE_754_DOUBLE_BE: {
2004 int i;
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002005 int le = (mformat_code == IEEE_754_DOUBLE_LE) ? 1 : 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 Py_ssize_t itemcount = Py_SIZE(items) / 8;
2007 const unsigned char *memstr =
2008 (unsigned char *)PyBytes_AS_STRING(items);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 converted_items = PyList_New(itemcount);
2011 if (converted_items == NULL)
2012 return NULL;
2013 for (i = 0; i < itemcount; i++) {
2014 PyObject *pyfloat = PyFloat_FromDouble(
2015 _PyFloat_Unpack8(&memstr[i * 8], le));
2016 if (pyfloat == NULL) {
2017 Py_DECREF(converted_items);
2018 return NULL;
2019 }
2020 PyList_SET_ITEM(converted_items, i, pyfloat);
2021 }
2022 break;
2023 }
2024 case UTF16_LE:
2025 case UTF16_BE: {
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002026 int byteorder = (mformat_code == UTF16_LE) ? -1 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 converted_items = PyUnicode_DecodeUTF16(
2028 PyBytes_AS_STRING(items), Py_SIZE(items),
2029 "strict", &byteorder);
2030 if (converted_items == NULL)
2031 return NULL;
2032 break;
2033 }
2034 case UTF32_LE:
2035 case UTF32_BE: {
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002036 int byteorder = (mformat_code == UTF32_LE) ? -1 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 converted_items = PyUnicode_DecodeUTF32(
2038 PyBytes_AS_STRING(items), Py_SIZE(items),
2039 "strict", &byteorder);
2040 if (converted_items == NULL)
2041 return NULL;
2042 break;
2043 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 case UNSIGNED_INT8:
2046 case SIGNED_INT8:
2047 case UNSIGNED_INT16_LE:
2048 case UNSIGNED_INT16_BE:
2049 case SIGNED_INT16_LE:
2050 case SIGNED_INT16_BE:
2051 case UNSIGNED_INT32_LE:
2052 case UNSIGNED_INT32_BE:
2053 case SIGNED_INT32_LE:
2054 case SIGNED_INT32_BE:
2055 case UNSIGNED_INT64_LE:
2056 case UNSIGNED_INT64_BE:
2057 case SIGNED_INT64_LE:
2058 case SIGNED_INT64_BE: {
2059 int i;
2060 const struct mformatdescr mf_descr =
Larry Hastingsdfbeb162014-10-13 10:39:41 +01002061 mformat_descriptors[mformat_code];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 Py_ssize_t itemcount = Py_SIZE(items) / mf_descr.size;
2063 const unsigned char *memstr =
2064 (unsigned char *)PyBytes_AS_STRING(items);
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002065 const struct arraydescr *descr;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 /* If possible, try to pack array's items using a data type
2068 * that fits better. This may result in an array with narrower
2069 * or wider elements.
2070 *
Martin Panter4c359642016-05-08 13:53:41 +00002071 * For example, if a 32-bit machine pickles an L-code array of
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 * unsigned longs, then the array will be unpickled by 64-bit
2073 * machine as an I-code array of unsigned ints.
2074 *
2075 * XXX: Is it possible to write a unit test for this?
2076 */
2077 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2078 if (descr->is_integer_type &&
Victor Stinner706768c2014-08-16 01:03:39 +02002079 (size_t)descr->itemsize == mf_descr.size &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 descr->is_signed == mf_descr.is_signed)
2081 typecode = descr->typecode;
2082 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 converted_items = PyList_New(itemcount);
2085 if (converted_items == NULL)
2086 return NULL;
2087 for (i = 0; i < itemcount; i++) {
2088 PyObject *pylong;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 pylong = _PyLong_FromByteArray(
2091 &memstr[i * mf_descr.size],
2092 mf_descr.size,
2093 !mf_descr.is_big_endian,
2094 mf_descr.is_signed);
2095 if (pylong == NULL) {
2096 Py_DECREF(converted_items);
2097 return NULL;
2098 }
2099 PyList_SET_ITEM(converted_items, i, pylong);
2100 }
2101 break;
2102 }
2103 case UNKNOWN_FORMAT:
2104 /* Impossible, but needed to shut up GCC about the unhandled
2105 * enumeration value.
2106 */
2107 default:
2108 PyErr_BadArgument();
2109 return NULL;
2110 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002111
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002112 result = make_array(arraytype, (char)typecode, converted_items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113 Py_DECREF(converted_items);
2114 return result;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002115}
2116
Brett Cannon1eb32c22014-10-10 16:26:45 -04002117/*[clinic input]
2118array.array.__reduce_ex__
2119
2120 value: object
2121 /
2122
2123Return state information for pickling.
2124[clinic start generated code]*/
2125
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002126static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04002127array_array___reduce_ex__(arrayobject *self, PyObject *value)
2128/*[clinic end generated code: output=051e0a6175d0eddb input=c36c3f85de7df6cd]*/
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 PyObject *dict;
2131 PyObject *result;
2132 PyObject *array_str;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002133 int typecode = self->ob_descr->typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002134 int mformat_code;
2135 static PyObject *array_reconstructor = NULL;
2136 long protocol;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002137 _Py_IDENTIFIER(_array_reconstructor);
2138 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 if (array_reconstructor == NULL) {
2141 PyObject *array_module = PyImport_ImportModule("array");
2142 if (array_module == NULL)
2143 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002144 array_reconstructor = _PyObject_GetAttrId(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145 array_module,
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002146 &PyId__array_reconstructor);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 Py_DECREF(array_module);
2148 if (array_reconstructor == NULL)
2149 return NULL;
2150 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 if (!PyLong_Check(value)) {
2153 PyErr_SetString(PyExc_TypeError,
2154 "__reduce_ex__ argument should an integer");
2155 return NULL;
2156 }
2157 protocol = PyLong_AsLong(value);
2158 if (protocol == -1 && PyErr_Occurred())
2159 return NULL;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002160
Brett Cannon1eb32c22014-10-10 16:26:45 -04002161 dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 if (dict == NULL) {
2163 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
2164 return NULL;
2165 PyErr_Clear();
2166 dict = Py_None;
2167 Py_INCREF(dict);
2168 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 mformat_code = typecode_to_mformat_code(typecode);
2171 if (mformat_code == UNKNOWN_FORMAT || protocol < 3) {
2172 /* Convert the array to a list if we got something weird
2173 * (e.g., non-IEEE floats), or we are pickling the array using
2174 * a Python 2.x compatible protocol.
2175 *
2176 * It is necessary to use a list representation for Python 2.x
2177 * compatible pickle protocol, since Python 2's str objects
2178 * are unpickled as unicode by Python 3. Thus it is impossible
2179 * to make arrays unpicklable by Python 3 by using their memory
2180 * representation, unless we resort to ugly hacks such as
2181 * coercing unicode objects to bytes in array_reconstructor.
2182 */
2183 PyObject *list;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002184 list = array_array_tolist_impl(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 if (list == NULL) {
2186 Py_DECREF(dict);
2187 return NULL;
2188 }
2189 result = Py_BuildValue(
Brett Cannon1eb32c22014-10-10 16:26:45 -04002190 "O(CO)O", Py_TYPE(self), typecode, list, dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 Py_DECREF(list);
2192 Py_DECREF(dict);
2193 return result;
2194 }
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002195
Brett Cannon1eb32c22014-10-10 16:26:45 -04002196 array_str = array_array_tobytes_impl(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 if (array_str == NULL) {
2198 Py_DECREF(dict);
2199 return NULL;
2200 }
2201 result = Py_BuildValue(
Brett Cannon1eb32c22014-10-10 16:26:45 -04002202 "O(OCiN)O", array_reconstructor, Py_TYPE(self), typecode,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203 mformat_code, array_str, dict);
2204 Py_DECREF(dict);
2205 return result;
Alexandre Vassalottiad077152009-07-15 17:49:23 +00002206}
2207
Martin v. Löwis99866332002-03-01 10:27:01 +00002208static PyObject *
2209array_get_typecode(arrayobject *a, void *closure)
2210{
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002211 char typecode = a->ob_descr->typecode;
2212 return PyUnicode_FromOrdinal(typecode);
Martin v. Löwis99866332002-03-01 10:27:01 +00002213}
2214
2215static PyObject *
2216array_get_itemsize(arrayobject *a, void *closure)
2217{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 return PyLong_FromLong((long)a->ob_descr->itemsize);
Martin v. Löwis99866332002-03-01 10:27:01 +00002219}
2220
2221static PyGetSetDef array_getsets [] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 {"typecode", (getter) array_get_typecode, NULL,
2223 "the typecode character used to create the array"},
2224 {"itemsize", (getter) array_get_itemsize, NULL,
2225 "the size, in bytes, of one array item"},
2226 {NULL}
Martin v. Löwis99866332002-03-01 10:27:01 +00002227};
2228
Martin v. Löwis59683e82008-06-13 07:50:45 +00002229static PyMethodDef array_methods[] = {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002230 ARRAY_ARRAY_APPEND_METHODDEF
2231 ARRAY_ARRAY_BUFFER_INFO_METHODDEF
2232 ARRAY_ARRAY_BYTESWAP_METHODDEF
2233 ARRAY_ARRAY___COPY___METHODDEF
2234 ARRAY_ARRAY_COUNT_METHODDEF
2235 ARRAY_ARRAY___DEEPCOPY___METHODDEF
2236 ARRAY_ARRAY_EXTEND_METHODDEF
2237 ARRAY_ARRAY_FROMFILE_METHODDEF
2238 ARRAY_ARRAY_FROMLIST_METHODDEF
2239 ARRAY_ARRAY_FROMSTRING_METHODDEF
2240 ARRAY_ARRAY_FROMBYTES_METHODDEF
2241 ARRAY_ARRAY_FROMUNICODE_METHODDEF
2242 ARRAY_ARRAY_INDEX_METHODDEF
2243 ARRAY_ARRAY_INSERT_METHODDEF
2244 ARRAY_ARRAY_POP_METHODDEF
2245 ARRAY_ARRAY___REDUCE_EX___METHODDEF
2246 ARRAY_ARRAY_REMOVE_METHODDEF
2247 ARRAY_ARRAY_REVERSE_METHODDEF
2248 ARRAY_ARRAY_TOFILE_METHODDEF
2249 ARRAY_ARRAY_TOLIST_METHODDEF
2250 ARRAY_ARRAY_TOSTRING_METHODDEF
2251 ARRAY_ARRAY_TOBYTES_METHODDEF
2252 ARRAY_ARRAY_TOUNICODE_METHODDEF
2253 ARRAY_ARRAY___SIZEOF___METHODDEF
2254 {NULL, NULL} /* sentinel */
Guido van Rossum778983b1993-02-19 15:55:02 +00002255};
2256
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002257static PyObject *
Peter Schneider-Kamp9656abd2000-07-13 21:10:57 +00002258array_repr(arrayobject *a)
Guido van Rossum778983b1993-02-19 15:55:02 +00002259{
Victor Stinnerf8bb7d02011-09-30 00:03:59 +02002260 char typecode;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 PyObject *s, *v = NULL;
2262 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00002263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 len = Py_SIZE(a);
2265 typecode = a->ob_descr->typecode;
2266 if (len == 0) {
Amaury Forgeot d'Arc24aa26b2010-11-25 08:13:35 +00002267 return PyUnicode_FromFormat("array('%c')", (int)typecode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002268 }
Gregory P. Smith9504b132012-12-10 20:20:20 -08002269 if (typecode == 'u') {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002270 v = array_array_tounicode_impl(a);
Gregory P. Smith9504b132012-12-10 20:20:20 -08002271 } else {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002272 v = array_array_tolist_impl(a);
Gregory P. Smith9504b132012-12-10 20:20:20 -08002273 }
Victor Stinner29ec5952013-02-26 00:27:38 +01002274 if (v == NULL)
2275 return NULL;
Raymond Hettinger88ba1e32003-04-23 17:27:00 +00002276
Amaury Forgeot d'Arc24aa26b2010-11-25 08:13:35 +00002277 s = PyUnicode_FromFormat("array('%c', %R)", (int)typecode, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 Py_DECREF(v);
2279 return s;
Guido van Rossum778983b1993-02-19 15:55:02 +00002280}
2281
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002282static PyObject*
2283array_subscr(arrayobject* self, PyObject* item)
2284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285 if (PyIndex_Check(item)) {
2286 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2287 if (i==-1 && PyErr_Occurred()) {
2288 return NULL;
2289 }
2290 if (i < 0)
2291 i += Py_SIZE(self);
2292 return array_item(self, i);
2293 }
2294 else if (PySlice_Check(item)) {
2295 Py_ssize_t start, stop, step, slicelength, cur, i;
2296 PyObject* result;
2297 arrayobject* ar;
2298 int itemsize = self->ob_descr->itemsize;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002299
Serhiy Storchakac26b19d2017-04-08 11:18:14 +03002300 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 return NULL;
2302 }
Serhiy Storchakac26b19d2017-04-08 11:18:14 +03002303 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2304 step);
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 if (slicelength <= 0) {
2307 return newarrayobject(&Arraytype, 0, self->ob_descr);
2308 }
2309 else if (step == 1) {
2310 PyObject *result = newarrayobject(&Arraytype,
2311 slicelength, self->ob_descr);
2312 if (result == NULL)
2313 return NULL;
2314 memcpy(((arrayobject *)result)->ob_item,
2315 self->ob_item + start * itemsize,
2316 slicelength * itemsize);
2317 return result;
2318 }
2319 else {
2320 result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
2321 if (!result) return NULL;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 ar = (arrayobject*)result;
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325 for (cur = start, i = 0; i < slicelength;
2326 cur += step, i++) {
2327 memcpy(ar->ob_item + i*itemsize,
2328 self->ob_item + cur*itemsize,
2329 itemsize);
2330 }
2331
2332 return result;
2333 }
2334 }
2335 else {
2336 PyErr_SetString(PyExc_TypeError,
2337 "array indices must be integers");
2338 return NULL;
2339 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002340}
2341
2342static int
2343array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
2344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 Py_ssize_t start, stop, step, slicelength, needed;
2346 arrayobject* other;
2347 int itemsize;
Thomas Woutersed03b412007-08-28 21:37:11 +00002348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349 if (PyIndex_Check(item)) {
2350 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
Alexandre Vassalotti47137252009-07-05 19:57:00 +00002351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 if (i == -1 && PyErr_Occurred())
2353 return -1;
2354 if (i < 0)
2355 i += Py_SIZE(self);
2356 if (i < 0 || i >= Py_SIZE(self)) {
2357 PyErr_SetString(PyExc_IndexError,
2358 "array assignment index out of range");
2359 return -1;
2360 }
2361 if (value == NULL) {
2362 /* Fall through to slice assignment */
2363 start = i;
2364 stop = i + 1;
2365 step = 1;
2366 slicelength = 1;
2367 }
2368 else
2369 return (*self->ob_descr->setitem)(self, i, value);
2370 }
2371 else if (PySlice_Check(item)) {
Serhiy Storchakac26b19d2017-04-08 11:18:14 +03002372 if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 return -1;
2374 }
Serhiy Storchakac26b19d2017-04-08 11:18:14 +03002375 slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2376 step);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 }
2378 else {
2379 PyErr_SetString(PyExc_TypeError,
2380 "array indices must be integer");
2381 return -1;
2382 }
2383 if (value == NULL) {
2384 other = NULL;
2385 needed = 0;
2386 }
2387 else if (array_Check(value)) {
2388 other = (arrayobject *)value;
2389 needed = Py_SIZE(other);
2390 if (self == other) {
2391 /* Special case "self[i:j] = self" -- copy self first */
2392 int ret;
2393 value = array_slice(other, 0, needed);
2394 if (value == NULL)
2395 return -1;
2396 ret = array_ass_subscr(self, item, value);
2397 Py_DECREF(value);
2398 return ret;
2399 }
2400 if (other->ob_descr != self->ob_descr) {
2401 PyErr_BadArgument();
2402 return -1;
2403 }
2404 }
2405 else {
2406 PyErr_Format(PyExc_TypeError,
2407 "can only assign array (not \"%.200s\") to array slice",
2408 Py_TYPE(value)->tp_name);
2409 return -1;
2410 }
2411 itemsize = self->ob_descr->itemsize;
2412 /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
2413 if ((step > 0 && stop < start) ||
2414 (step < 0 && stop > start))
2415 stop = start;
Alexandre Vassalotti47137252009-07-05 19:57:00 +00002416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 /* Issue #4509: If the array has exported buffers and the slice
2418 assignment would change the size of the array, fail early to make
2419 sure we don't modify it. */
2420 if ((needed == 0 || slicelength != needed) && self->ob_exports > 0) {
2421 PyErr_SetString(PyExc_BufferError,
2422 "cannot resize an array that is exporting buffers");
2423 return -1;
2424 }
Mark Dickinsonbc099642010-01-29 17:27:24 +00002425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002426 if (step == 1) {
2427 if (slicelength > needed) {
2428 memmove(self->ob_item + (start + needed) * itemsize,
2429 self->ob_item + stop * itemsize,
2430 (Py_SIZE(self) - stop) * itemsize);
2431 if (array_resize(self, Py_SIZE(self) +
2432 needed - slicelength) < 0)
2433 return -1;
2434 }
2435 else if (slicelength < needed) {
2436 if (array_resize(self, Py_SIZE(self) +
2437 needed - slicelength) < 0)
2438 return -1;
2439 memmove(self->ob_item + (start + needed) * itemsize,
2440 self->ob_item + stop * itemsize,
2441 (Py_SIZE(self) - start - needed) * itemsize);
2442 }
2443 if (needed > 0)
2444 memcpy(self->ob_item + start * itemsize,
2445 other->ob_item, needed * itemsize);
2446 return 0;
2447 }
2448 else if (needed == 0) {
2449 /* Delete slice */
2450 size_t cur;
2451 Py_ssize_t i;
Thomas Woutersed03b412007-08-28 21:37:11 +00002452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002453 if (step < 0) {
2454 stop = start + 1;
2455 start = stop + step * (slicelength - 1) - 1;
2456 step = -step;
2457 }
2458 for (cur = start, i = 0; i < slicelength;
2459 cur += step, i++) {
2460 Py_ssize_t lim = step - 1;
Thomas Woutersed03b412007-08-28 21:37:11 +00002461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 if (cur + step >= (size_t)Py_SIZE(self))
2463 lim = Py_SIZE(self) - cur - 1;
2464 memmove(self->ob_item + (cur - i) * itemsize,
2465 self->ob_item + (cur + 1) * itemsize,
2466 lim * itemsize);
2467 }
Mark Dickinsonc7d93b72011-09-25 15:34:32 +01002468 cur = start + (size_t)slicelength * step;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 if (cur < (size_t)Py_SIZE(self)) {
2470 memmove(self->ob_item + (cur-slicelength) * itemsize,
2471 self->ob_item + cur * itemsize,
2472 (Py_SIZE(self) - cur) * itemsize);
2473 }
2474 if (array_resize(self, Py_SIZE(self) - slicelength) < 0)
2475 return -1;
2476 return 0;
2477 }
2478 else {
2479 Py_ssize_t cur, i;
2480
2481 if (needed != slicelength) {
2482 PyErr_Format(PyExc_ValueError,
2483 "attempt to assign array of size %zd "
2484 "to extended slice of size %zd",
2485 needed, slicelength);
2486 return -1;
2487 }
2488 for (cur = start, i = 0; i < slicelength;
2489 cur += step, i++) {
2490 memcpy(self->ob_item + cur * itemsize,
2491 other->ob_item + i * itemsize,
2492 itemsize);
2493 }
2494 return 0;
2495 }
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002496}
2497
2498static PyMappingMethods array_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 (lenfunc)array_length,
2500 (binaryfunc)array_subscr,
2501 (objobjargproc)array_ass_subscr
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00002502};
2503
Guido van Rossumd8faa362007-04-27 19:54:29 +00002504static const void *emptybuf = "";
2505
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002506
2507static int
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00002508array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002509{
Stefan Krah650c1e82015-02-03 21:43:23 +01002510 if (view == NULL) {
2511 PyErr_SetString(PyExc_BufferError,
2512 "array_buffer_getbuf: view==NULL argument is obsolete");
2513 return -1;
2514 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 view->buf = (void *)self->ob_item;
2517 view->obj = (PyObject*)self;
2518 Py_INCREF(self);
2519 if (view->buf == NULL)
2520 view->buf = (void *)emptybuf;
2521 view->len = (Py_SIZE(self)) * self->ob_descr->itemsize;
2522 view->readonly = 0;
2523 view->ndim = 1;
2524 view->itemsize = self->ob_descr->itemsize;
2525 view->suboffsets = NULL;
2526 view->shape = NULL;
2527 if ((flags & PyBUF_ND)==PyBUF_ND) {
2528 view->shape = &((Py_SIZE(self)));
2529 }
2530 view->strides = NULL;
2531 if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES)
2532 view->strides = &(view->itemsize);
2533 view->format = NULL;
2534 view->internal = NULL;
Victor Stinner62bb3942012-08-06 00:46:05 +02002535 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002536 view->format = (char *)self->ob_descr->formats;
Victor Stinner62bb3942012-08-06 00:46:05 +02002537#ifdef Py_UNICODE_WIDE
2538 if (self->ob_descr->typecode == 'u') {
2539 view->format = "w";
2540 }
2541#endif
2542 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002544 self->ob_exports++;
2545 return 0;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002546}
2547
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002548static void
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00002549array_buffer_relbuf(arrayobject *self, Py_buffer *view)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 self->ob_exports--;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002552}
2553
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002554static PySequenceMethods array_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555 (lenfunc)array_length, /*sq_length*/
2556 (binaryfunc)array_concat, /*sq_concat*/
2557 (ssizeargfunc)array_repeat, /*sq_repeat*/
2558 (ssizeargfunc)array_item, /*sq_item*/
2559 0, /*sq_slice*/
2560 (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
2561 0, /*sq_ass_slice*/
2562 (objobjproc)array_contains, /*sq_contains*/
2563 (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
2564 (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/
Guido van Rossum778983b1993-02-19 15:55:02 +00002565};
2566
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002567static PyBufferProcs array_as_buffer = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002568 (getbufferproc)array_buffer_getbuf,
2569 (releasebufferproc)array_buffer_relbuf
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002570};
2571
Roger E. Masse2919eaa1996-12-09 20:10:36 +00002572static PyObject *
Martin v. Löwis99866332002-03-01 10:27:01 +00002573array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum778983b1993-02-19 15:55:02 +00002574{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002575 int c;
2576 PyObject *initial = NULL, *it = NULL;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002577 const struct arraydescr *descr;
Martin v. Löwis99866332002-03-01 10:27:01 +00002578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds))
2580 return NULL;
Martin v. Löwis99866332002-03-01 10:27:01 +00002581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
2583 return NULL;
Raymond Hettinger84fc9aa2003-04-24 10:41:55 +00002584
Alexandre Vassalotti9730e332013-11-29 20:47:15 -08002585 if (initial && c != 'u') {
2586 if (PyUnicode_Check(initial)) {
2587 PyErr_Format(PyExc_TypeError, "cannot use a str to initialize "
2588 "an array with typecode '%c'", c);
2589 return NULL;
2590 }
2591 else if (array_Check(initial) &&
2592 ((arrayobject*)initial)->ob_descr->typecode == 'u') {
2593 PyErr_Format(PyExc_TypeError, "cannot use a unicode array to "
2594 "initialize an array with typecode '%c'", c);
2595 return NULL;
2596 }
2597 }
2598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002599 if (!(initial == NULL || PyList_Check(initial)
2600 || PyByteArray_Check(initial)
2601 || PyBytes_Check(initial)
2602 || PyTuple_Check(initial)
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002603 || ((c=='u') && PyUnicode_Check(initial))
2604 || (array_Check(initial)
2605 && c == ((arrayobject*)initial)->ob_descr->typecode))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002606 it = PyObject_GetIter(initial);
2607 if (it == NULL)
2608 return NULL;
2609 /* We set initial to NULL so that the subsequent code
2610 will create an empty array of the appropriate type
2611 and afterwards we can use array_iter_extend to populate
2612 the array.
2613 */
2614 initial = NULL;
2615 }
2616 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2617 if (descr->typecode == c) {
2618 PyObject *a;
2619 Py_ssize_t len;
Martin v. Löwis99866332002-03-01 10:27:01 +00002620
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002621 if (initial == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002622 len = 0;
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002623 else if (PyList_Check(initial))
2624 len = PyList_GET_SIZE(initial);
2625 else if (PyTuple_Check(initial) || array_Check(initial))
2626 len = Py_SIZE(initial);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002627 else
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002628 len = 0;
Martin v. Löwis99866332002-03-01 10:27:01 +00002629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002630 a = newarrayobject(type, len, descr);
2631 if (a == NULL)
2632 return NULL;
2633
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002634 if (len > 0 && !array_Check(initial)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002635 Py_ssize_t i;
2636 for (i = 0; i < len; i++) {
2637 PyObject *v =
2638 PySequence_GetItem(initial, i);
2639 if (v == NULL) {
2640 Py_DECREF(a);
2641 return NULL;
2642 }
2643 if (setarrayitem(a, i, v) != 0) {
2644 Py_DECREF(v);
2645 Py_DECREF(a);
2646 return NULL;
2647 }
2648 Py_DECREF(v);
2649 }
2650 }
2651 else if (initial != NULL && (PyByteArray_Check(initial) ||
2652 PyBytes_Check(initial))) {
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03002653 PyObject *v;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002654 v = array_array_frombytes((arrayobject *)a,
Serhiy Storchaka04e6dba2015-04-04 17:06:55 +03002655 initial);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656 if (v == NULL) {
2657 Py_DECREF(a);
2658 return NULL;
2659 }
2660 Py_DECREF(v);
2661 }
2662 else if (initial != NULL && PyUnicode_Check(initial)) {
Victor Stinner62bb3942012-08-06 00:46:05 +02002663 Py_UNICODE *ustr;
Victor Stinner1fbcaef2011-09-30 01:54:04 +02002664 Py_ssize_t n;
Victor Stinner62bb3942012-08-06 00:46:05 +02002665
2666 ustr = PyUnicode_AsUnicode(initial);
2667 if (ustr == NULL) {
2668 PyErr_NoMemory();
Victor Stinner1fbcaef2011-09-30 01:54:04 +02002669 Py_DECREF(a);
2670 return NULL;
2671 }
Victor Stinner62bb3942012-08-06 00:46:05 +02002672
2673 n = PyUnicode_GET_DATA_SIZE(initial);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 if (n > 0) {
2675 arrayobject *self = (arrayobject *)a;
Victor Stinner62bb3942012-08-06 00:46:05 +02002676 char *item = self->ob_item;
2677 item = (char *)PyMem_Realloc(item, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002678 if (item == NULL) {
2679 PyErr_NoMemory();
2680 Py_DECREF(a);
2681 return NULL;
2682 }
Victor Stinner62bb3942012-08-06 00:46:05 +02002683 self->ob_item = item;
2684 Py_SIZE(self) = n / sizeof(Py_UNICODE);
2685 memcpy(item, ustr, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 self->allocated = Py_SIZE(self);
2687 }
2688 }
Benjamin Peterson682124c2014-10-10 20:58:30 -04002689 else if (initial != NULL && array_Check(initial) && len > 0) {
Alexander Belopolskyef4a03f2011-01-11 21:44:00 +00002690 arrayobject *self = (arrayobject *)a;
2691 arrayobject *other = (arrayobject *)initial;
2692 memcpy(self->ob_item, other->ob_item, len * other->ob_descr->itemsize);
2693 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002694 if (it != NULL) {
2695 if (array_iter_extend((arrayobject *)a, it) == -1) {
2696 Py_DECREF(it);
2697 Py_DECREF(a);
2698 return NULL;
2699 }
2700 Py_DECREF(it);
2701 }
2702 return a;
2703 }
2704 }
2705 PyErr_SetString(PyExc_ValueError,
Meador Inge1c9f0c92011-09-20 19:55:51 -05002706 "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 +00002707 return NULL;
Guido van Rossum778983b1993-02-19 15:55:02 +00002708}
2709
Guido van Rossum778983b1993-02-19 15:55:02 +00002710
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002711PyDoc_STRVAR(module_doc,
Martin v. Löwis99866332002-03-01 10:27:01 +00002712"This module defines an object type which can efficiently represent\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002713an array of basic values: characters, integers, floating point\n\
2714numbers. Arrays are sequence types and behave very much like lists,\n\
Alexandre Vassalotti9730e332013-11-29 20:47:15 -08002715except that the type of objects stored in them is constrained.\n");
2716
2717PyDoc_STRVAR(arraytype_doc,
2718"array(typecode [, initializer]) -> array\n\
2719\n\
2720Return a new array whose items are restricted by typecode, and\n\
2721initialized from the optional initializer value, which must be a list,\n\
2722string or iterable over elements of the appropriate type.\n\
2723\n\
2724Arrays represent basic values and behave very much like lists, except\n\
2725the type of objects stored in them is constrained. The type is specified\n\
2726at object creation time by using a type code, which is a single character.\n\
2727The following type codes are defined:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002728\n\
2729 Type code C Type Minimum size in bytes \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002730 'b' signed integer 1 \n\
2731 'B' unsigned integer 1 \n\
Victor Stinner62bb3942012-08-06 00:46:05 +02002732 'u' Unicode character 2 (see note) \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002733 'h' signed integer 2 \n\
2734 'H' unsigned integer 2 \n\
2735 'i' signed integer 2 \n\
2736 'I' unsigned integer 2 \n\
2737 'l' signed integer 4 \n\
2738 'L' unsigned integer 4 \n\
Meador Inge1c9f0c92011-09-20 19:55:51 -05002739 'q' signed integer 8 (see note) \n\
2740 'Q' unsigned integer 8 (see note) \n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002741 'f' floating point 4 \n\
2742 'd' floating point 8 \n\
2743\n\
Victor Stinner62bb3942012-08-06 00:46:05 +02002744NOTE: The 'u' typecode corresponds to Python's unicode character. On \n\
2745narrow builds this is 2-bytes on wide builds this is 4-bytes.\n\
2746\n\
Meador Inge1c9f0c92011-09-20 19:55:51 -05002747NOTE: The 'q' and 'Q' type codes are only available if the platform \n\
2748C compiler used to build Python supports 'long long', or, on Windows, \n\
2749'__int64'.\n\
2750\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002751Methods:\n\
2752\n\
2753append() -- append a new item to the end of the array\n\
2754buffer_info() -- return information giving the current memory info\n\
2755byteswap() -- byteswap all the items of the array\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002756count() -- return number of occurrences of an object\n\
Raymond Hettinger49f9bd12004-03-14 05:43:59 +00002757extend() -- extend array by appending multiple elements from an iterable\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002758fromfile() -- read items from a file object\n\
2759fromlist() -- append items from the list\n\
Florent Xiclunac45fb252011-10-24 13:14:55 +02002760frombytes() -- append items from the string\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002761index() -- return index of first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002762insert() -- insert a new item into the array at a provided position\n\
Peter Schneider-Kamp5a65c2d2000-07-31 20:52:21 +00002763pop() -- remove and return item (default last)\n\
Mark Dickinson934896d2009-02-21 20:59:32 +00002764remove() -- remove first occurrence of an object\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002765reverse() -- reverse the order of the items in the array\n\
2766tofile() -- write all items to a file object\n\
2767tolist() -- return the array converted to an ordinary list\n\
Florent Xiclunac45fb252011-10-24 13:14:55 +02002768tobytes() -- return the array converted to a string\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002769\n\
Martin v. Löwis99866332002-03-01 10:27:01 +00002770Attributes:\n\
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002771\n\
2772typecode -- the typecode character used to create the array\n\
2773itemsize -- the length in bytes of one array item\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002774");
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002775
Raymond Hettinger625812f2003-01-07 01:58:52 +00002776static PyObject *array_iter(arrayobject *ao);
2777
Tim Peters0c322792002-07-17 16:49:03 +00002778static PyTypeObject Arraytype = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002779 PyVarObject_HEAD_INIT(NULL, 0)
2780 "array.array",
2781 sizeof(arrayobject),
2782 0,
2783 (destructor)array_dealloc, /* tp_dealloc */
2784 0, /* tp_print */
2785 0, /* tp_getattr */
2786 0, /* tp_setattr */
2787 0, /* tp_reserved */
2788 (reprfunc)array_repr, /* tp_repr */
2789 0, /* tp_as_number*/
2790 &array_as_sequence, /* tp_as_sequence*/
2791 &array_as_mapping, /* tp_as_mapping*/
2792 0, /* tp_hash */
2793 0, /* tp_call */
2794 0, /* tp_str */
2795 PyObject_GenericGetAttr, /* tp_getattro */
2796 0, /* tp_setattro */
2797 &array_as_buffer, /* tp_as_buffer*/
2798 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2799 arraytype_doc, /* tp_doc */
2800 0, /* tp_traverse */
2801 0, /* tp_clear */
2802 array_richcompare, /* tp_richcompare */
2803 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
2804 (getiterfunc)array_iter, /* tp_iter */
2805 0, /* tp_iternext */
2806 array_methods, /* tp_methods */
2807 0, /* tp_members */
2808 array_getsets, /* tp_getset */
2809 0, /* tp_base */
2810 0, /* tp_dict */
2811 0, /* tp_descr_get */
2812 0, /* tp_descr_set */
2813 0, /* tp_dictoffset */
2814 0, /* tp_init */
2815 PyType_GenericAlloc, /* tp_alloc */
2816 array_new, /* tp_new */
2817 PyObject_Del, /* tp_free */
Guido van Rossumb39b90d1998-10-13 14:27:22 +00002818};
2819
Raymond Hettinger625812f2003-01-07 01:58:52 +00002820
2821/*********************** Array Iterator **************************/
2822
Brett Cannon1eb32c22014-10-10 16:26:45 -04002823/*[clinic input]
2824class array.arrayiterator "arrayiterobject *" "&PyArrayIter_Type"
2825[clinic start generated code]*/
2826/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5aefd2d74d8c8e30]*/
Raymond Hettinger625812f2003-01-07 01:58:52 +00002827
2828static PyObject *
2829array_iter(arrayobject *ao)
2830{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 arrayiterobject *it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 if (!array_Check(ao)) {
2834 PyErr_BadInternalCall();
2835 return NULL;
2836 }
Raymond Hettinger625812f2003-01-07 01:58:52 +00002837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838 it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
2839 if (it == NULL)
2840 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 Py_INCREF(ao);
2843 it->ao = ao;
2844 it->index = 0;
2845 it->getitem = ao->ob_descr->getitem;
2846 PyObject_GC_Track(it);
2847 return (PyObject *)it;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002848}
2849
2850static PyObject *
Raymond Hettinger625812f2003-01-07 01:58:52 +00002851arrayiter_next(arrayiterobject *it)
2852{
Serhiy Storchakaab0d1982016-03-30 21:11:16 +03002853 arrayobject *ao;
2854
2855 assert(it != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 assert(PyArrayIter_Check(it));
Serhiy Storchakaab0d1982016-03-30 21:11:16 +03002857 ao = it->ao;
2858 if (ao == NULL) {
2859 return NULL;
2860 }
2861 assert(array_Check(ao));
2862 if (it->index < Py_SIZE(ao)) {
2863 return (*it->getitem)(ao, it->index++);
2864 }
2865 it->ao = NULL;
2866 Py_DECREF(ao);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 return NULL;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002868}
2869
2870static void
2871arrayiter_dealloc(arrayiterobject *it)
2872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 PyObject_GC_UnTrack(it);
2874 Py_XDECREF(it->ao);
2875 PyObject_GC_Del(it);
Raymond Hettinger625812f2003-01-07 01:58:52 +00002876}
2877
2878static int
2879arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 Py_VISIT(it->ao);
2882 return 0;
Raymond Hettinger625812f2003-01-07 01:58:52 +00002883}
2884
Brett Cannon1eb32c22014-10-10 16:26:45 -04002885/*[clinic input]
2886array.arrayiterator.__reduce__
2887
2888Return state information for pickling.
2889[clinic start generated code]*/
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002890
2891static PyObject *
Brett Cannon1eb32c22014-10-10 16:26:45 -04002892array_arrayiterator___reduce___impl(arrayiterobject *self)
2893/*[clinic end generated code: output=7898a52e8e66e016 input=a062ea1e9951417a]*/
2894{
Serhiy Storchakaab0d1982016-03-30 21:11:16 +03002895 PyObject *func = _PyObject_GetBuiltin("iter");
2896 if (self->ao == NULL) {
2897 return Py_BuildValue("N(())", func);
2898 }
2899 return Py_BuildValue("N(O)n", func, self->ao, self->index);
Brett Cannon1eb32c22014-10-10 16:26:45 -04002900}
2901
2902/*[clinic input]
2903array.arrayiterator.__setstate__
2904
2905 state: object
2906 /
2907
2908Set state information for unpickling.
2909[clinic start generated code]*/
2910
2911static PyObject *
2912array_arrayiterator___setstate__(arrayiterobject *self, PyObject *state)
2913/*[clinic end generated code: output=397da9904e443cbe input=f47d5ceda19e787b]*/
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002914{
2915 Py_ssize_t index = PyLong_AsSsize_t(state);
2916 if (index == -1 && PyErr_Occurred())
2917 return NULL;
2918 if (index < 0)
2919 index = 0;
Brett Cannon1eb32c22014-10-10 16:26:45 -04002920 else if (index > Py_SIZE(self->ao))
2921 index = Py_SIZE(self->ao); /* iterator exhausted */
2922 self->index = index;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002923 Py_RETURN_NONE;
2924}
2925
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002926static PyMethodDef arrayiter_methods[] = {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002927 ARRAY_ARRAYITERATOR___REDUCE___METHODDEF
2928 ARRAY_ARRAYITERATOR___SETSTATE___METHODDEF
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002929 {NULL, NULL} /* sentinel */
2930};
2931
Raymond Hettinger625812f2003-01-07 01:58:52 +00002932static PyTypeObject PyArrayIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002933 PyVarObject_HEAD_INIT(NULL, 0)
2934 "arrayiterator", /* tp_name */
2935 sizeof(arrayiterobject), /* tp_basicsize */
2936 0, /* tp_itemsize */
2937 /* methods */
2938 (destructor)arrayiter_dealloc, /* tp_dealloc */
2939 0, /* tp_print */
2940 0, /* tp_getattr */
2941 0, /* tp_setattr */
2942 0, /* tp_reserved */
2943 0, /* tp_repr */
2944 0, /* tp_as_number */
2945 0, /* tp_as_sequence */
2946 0, /* tp_as_mapping */
2947 0, /* tp_hash */
2948 0, /* tp_call */
2949 0, /* tp_str */
2950 PyObject_GenericGetAttr, /* tp_getattro */
2951 0, /* tp_setattro */
2952 0, /* tp_as_buffer */
2953 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2954 0, /* tp_doc */
2955 (traverseproc)arrayiter_traverse, /* tp_traverse */
2956 0, /* tp_clear */
2957 0, /* tp_richcompare */
2958 0, /* tp_weaklistoffset */
2959 PyObject_SelfIter, /* tp_iter */
2960 (iternextfunc)arrayiter_next, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002961 arrayiter_methods, /* tp_methods */
Raymond Hettinger625812f2003-01-07 01:58:52 +00002962};
2963
2964
2965/*********************** Install Module **************************/
2966
Martin v. Löwis99866332002-03-01 10:27:01 +00002967/* No functions in array module. */
2968static PyMethodDef a_methods[] = {
Brett Cannon1eb32c22014-10-10 16:26:45 -04002969 ARRAY__ARRAY_RECONSTRUCTOR_METHODDEF
Martin v. Löwis99866332002-03-01 10:27:01 +00002970 {NULL, NULL, 0, NULL} /* Sentinel */
2971};
2972
Nick Coghland5cacbb2015-05-23 22:24:10 +10002973static int
2974array_modexec(PyObject *m)
Guido van Rossum778983b1993-02-19 15:55:02 +00002975{
Georg Brandl4cb0de22011-09-28 21:49:49 +02002976 char buffer[Py_ARRAY_LENGTH(descriptors)], *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 PyObject *typecodes;
2978 Py_ssize_t size = 0;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02002979 const struct arraydescr *descr;
Fred Drake0d40ba42000-02-04 20:33:49 +00002980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 if (PyType_Ready(&Arraytype) < 0)
Nick Coghland5cacbb2015-05-23 22:24:10 +10002982 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002983 Py_TYPE(&PyArrayIter_Type) = &PyType_Type;
Fred Drakef4e34842002-04-01 03:45:06 +00002984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 Py_INCREF((PyObject *)&Arraytype);
2986 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
2987 Py_INCREF((PyObject *)&Arraytype);
2988 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00002989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 for (descr=descriptors; descr->typecode != '\0'; descr++) {
2991 size++;
2992 }
Travis E. Oliphantd5c0add2007-10-12 22:05:15 +00002993
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002994 p = buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002995 for (descr = descriptors; descr->typecode != '\0'; descr++) {
2996 *p++ = (char)descr->typecode;
2997 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002998 typecodes = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002999
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003000 PyModule_AddObject(m, "typecodes", typecodes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001
3002 if (PyErr_Occurred()) {
3003 Py_DECREF(m);
3004 m = NULL;
3005 }
Nick Coghland5cacbb2015-05-23 22:24:10 +10003006 return 0;
3007}
3008
3009static PyModuleDef_Slot arrayslots[] = {
3010 {Py_mod_exec, array_modexec},
3011 {0, NULL}
3012};
3013
3014
3015static struct PyModuleDef arraymodule = {
3016 PyModuleDef_HEAD_INIT,
3017 "array",
3018 module_doc,
3019 0,
3020 a_methods,
3021 arrayslots,
3022 NULL,
3023 NULL,
3024 NULL
3025};
3026
3027
3028PyMODINIT_FUNC
3029PyInit_array(void)
3030{
3031 return PyModuleDef_Init(&arraymodule);
Guido van Rossum778983b1993-02-19 15:55:02 +00003032}