• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* 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 
6 #define PY_SSIZE_T_CLEAN
7 #include "Python.h"
8 #include <stddef.h>               // offsetof()
9 
10 #ifdef STDC_HEADERS
11 #include <stddef.h>
12 #else /* !STDC_HEADERS */
13 #ifdef HAVE_SYS_TYPES_H
14 #include <sys/types.h>          /* For size_t */
15 #endif /* HAVE_SYS_TYPES_H */
16 #endif /* !STDC_HEADERS */
17 
18 /*[clinic input]
19 module array
20 [clinic start generated code]*/
21 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=7d1b8d7f5958fd83]*/
22 
23 struct arrayobject; /* Forward */
24 
25 /* 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  */
29 struct arraydescr {
30     char typecode;
31     int itemsize;
32     PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
33     int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *);
34     int (*compareitems)(const void *, const void *, Py_ssize_t);
35     const char *formats;
36     int is_integer_type;
37     int is_signed;
38 };
39 
40 typedef struct arrayobject {
41     PyObject_VAR_HEAD
42     char *ob_item;
43     Py_ssize_t allocated;
44     const struct arraydescr *ob_descr;
45     PyObject *weakreflist; /* List of weak references */
46     Py_ssize_t ob_exports;  /* Number of exported buffers */
47 } arrayobject;
48 
49 static PyTypeObject Arraytype;
50 
51 typedef struct {
52     PyObject_HEAD
53     Py_ssize_t index;
54     arrayobject *ao;
55     PyObject* (*getitem)(struct arrayobject *, Py_ssize_t);
56 } arrayiterobject;
57 
58 static PyTypeObject PyArrayIter_Type;
59 
60 #define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
61 
62 enum machine_format_code {
63     UNKNOWN_FORMAT = -1,
64     /* UNKNOWN_FORMAT is used to indicate that the machine format for an
65      * array type code cannot be interpreted. When this occurs, a list of
66      * Python objects is used to represent the content of the array
67      * instead of using the memory content of the array directly. In that
68      * case, the array_reconstructor mechanism is bypassed completely, and
69      * the standard array constructor is used instead.
70      *
71      * This is will most likely occur when the machine doesn't use IEEE
72      * floating-point numbers.
73      */
74 
75     UNSIGNED_INT8 = 0,
76     SIGNED_INT8 = 1,
77     UNSIGNED_INT16_LE = 2,
78     UNSIGNED_INT16_BE = 3,
79     SIGNED_INT16_LE = 4,
80     SIGNED_INT16_BE = 5,
81     UNSIGNED_INT32_LE = 6,
82     UNSIGNED_INT32_BE = 7,
83     SIGNED_INT32_LE = 8,
84     SIGNED_INT32_BE = 9,
85     UNSIGNED_INT64_LE = 10,
86     UNSIGNED_INT64_BE = 11,
87     SIGNED_INT64_LE = 12,
88     SIGNED_INT64_BE = 13,
89     IEEE_754_FLOAT_LE = 14,
90     IEEE_754_FLOAT_BE = 15,
91     IEEE_754_DOUBLE_LE = 16,
92     IEEE_754_DOUBLE_BE = 17,
93     UTF16_LE = 18,
94     UTF16_BE = 19,
95     UTF32_LE = 20,
96     UTF32_BE = 21
97 };
98 #define MACHINE_FORMAT_CODE_MIN 0
99 #define MACHINE_FORMAT_CODE_MAX 21
100 
101 
102 /*
103  * Must come after arrayobject, arrayiterobject,
104  * and enum machine_code_type definitions.
105  */
106 #include "clinic/arraymodule.c.h"
107 
108 #define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
109 #define array_CheckExact(op) Py_IS_TYPE(op, &Arraytype)
110 
111 static int
array_resize(arrayobject * self,Py_ssize_t newsize)112 array_resize(arrayobject *self, Py_ssize_t newsize)
113 {
114     char *items;
115     size_t _new_size;
116 
117     if (self->ob_exports > 0 && newsize != Py_SIZE(self)) {
118         PyErr_SetString(PyExc_BufferError,
119             "cannot resize an array that is exporting buffers");
120         return -1;
121     }
122 
123     /* Bypass realloc() when a previous overallocation is large enough
124        to accommodate the newsize.  If the newsize is 16 smaller than the
125        current size, then proceed with the realloc() to shrink the array.
126     */
127 
128     if (self->allocated >= newsize &&
129         Py_SIZE(self) < newsize + 16 &&
130         self->ob_item != NULL) {
131         Py_SET_SIZE(self, newsize);
132         return 0;
133     }
134 
135     if (newsize == 0) {
136         PyMem_FREE(self->ob_item);
137         self->ob_item = NULL;
138         Py_SET_SIZE(self, 0);
139         self->allocated = 0;
140         return 0;
141     }
142 
143     /* This over-allocates proportional to the array size, making room
144      * for additional growth.  The over-allocation is mild, but is
145      * enough to give linear-time amortized behavior over a long
146      * sequence of appends() in the presence of a poorly-performing
147      * system realloc().
148      * The growth pattern is:  0, 4, 8, 16, 25, 34, 46, 56, 67, 79, ...
149      * Note, the pattern starts out the same as for lists but then
150      * grows at a smaller rate so that larger arrays only overallocate
151      * by about 1/16th -- this is done because arrays are presumed to be more
152      * memory critical.
153      */
154 
155     _new_size = (newsize >> 4) + (Py_SIZE(self) < 8 ? 3 : 7) + newsize;
156     items = self->ob_item;
157     /* XXX The following multiplication and division does not optimize away
158        like it does for lists since the size is not known at compile time */
159     if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize))
160         PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize));
161     else
162         items = NULL;
163     if (items == NULL) {
164         PyErr_NoMemory();
165         return -1;
166     }
167     self->ob_item = items;
168     Py_SET_SIZE(self, newsize);
169     self->allocated = _new_size;
170     return 0;
171 }
172 
173 /****************************************************************************
174 Get and Set functions for each type.
175 A Get function takes an arrayobject* and an integer index, returning the
176 array value at that index wrapped in an appropriate PyObject*.
177 A Set function takes an arrayobject, integer index, and PyObject*; sets
178 the array value at that index to the raw C data extracted from the PyObject*,
179 and returns 0 if successful, else nonzero on failure (PyObject* not of an
180 appropriate type or value).
181 Note that the basic Get and Set functions do NOT check that the index is
182 in bounds; that's the responsibility of the caller.
183 ****************************************************************************/
184 
185 static PyObject *
b_getitem(arrayobject * ap,Py_ssize_t i)186 b_getitem(arrayobject *ap, Py_ssize_t i)
187 {
188     long x = ((signed char *)ap->ob_item)[i];
189     return PyLong_FromLong(x);
190 }
191 
192 static int
b_setitem(arrayobject * ap,Py_ssize_t i,PyObject * v)193 b_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
194 {
195     short x;
196     /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
197        must use the next size up that is signed ('h') and manually do
198        the overflow checking */
199     if (!PyArg_Parse(v, "h;array item must be integer", &x))
200         return -1;
201     else if (x < -128) {
202         PyErr_SetString(PyExc_OverflowError,
203             "signed char is less than minimum");
204         return -1;
205     }
206     else if (x > 127) {
207         PyErr_SetString(PyExc_OverflowError,
208             "signed char is greater than maximum");
209         return -1;
210     }
211     if (i >= 0)
212         ((char *)ap->ob_item)[i] = (char)x;
213     return 0;
214 }
215 
216 static PyObject *
BB_getitem(arrayobject * ap,Py_ssize_t i)217 BB_getitem(arrayobject *ap, Py_ssize_t i)
218 {
219     long x = ((unsigned char *)ap->ob_item)[i];
220     return PyLong_FromLong(x);
221 }
222 
223 static int
BB_setitem(arrayobject * ap,Py_ssize_t i,PyObject * v)224 BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
225 {
226     unsigned char x;
227     /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
228     if (!PyArg_Parse(v, "b;array item must be integer", &x))
229         return -1;
230     if (i >= 0)
231         ((char *)ap->ob_item)[i] = x;
232     return 0;
233 }
234 
235 static PyObject *
u_getitem(arrayobject * ap,Py_ssize_t i)236 u_getitem(arrayobject *ap, Py_ssize_t i)
237 {
238     return PyUnicode_FromOrdinal(((wchar_t *) ap->ob_item)[i]);
239 }
240 
241 static int
u_setitem(arrayobject * ap,Py_ssize_t i,PyObject * v)242 u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
243 {
244     PyObject *u;
245     if (!PyArg_Parse(v, "U;array item must be unicode character", &u)) {
246         return -1;
247     }
248 
249     Py_ssize_t len = PyUnicode_AsWideChar(u, NULL, 0);
250     if (len != 2) {
251         PyErr_SetString(PyExc_TypeError,
252                         "array item must be unicode character");
253         return -1;
254     }
255 
256     wchar_t w;
257     len = PyUnicode_AsWideChar(u, &w, 1);
258     assert(len == 1);
259 
260     if (i >= 0) {
261         ((wchar_t *)ap->ob_item)[i] = w;
262     }
263     return 0;
264 }
265 
266 
267 static PyObject *
h_getitem(arrayobject * ap,Py_ssize_t i)268 h_getitem(arrayobject *ap, Py_ssize_t i)
269 {
270     return PyLong_FromLong((long) ((short *)ap->ob_item)[i]);
271 }
272 
273 
274 static int
h_setitem(arrayobject * ap,Py_ssize_t i,PyObject * v)275 h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
276 {
277     short x;
278     /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
279     if (!PyArg_Parse(v, "h;array item must be integer", &x))
280         return -1;
281     if (i >= 0)
282                  ((short *)ap->ob_item)[i] = x;
283     return 0;
284 }
285 
286 static PyObject *
HH_getitem(arrayobject * ap,Py_ssize_t i)287 HH_getitem(arrayobject *ap, Py_ssize_t i)
288 {
289     return PyLong_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
290 }
291 
292 static int
HH_setitem(arrayobject * ap,Py_ssize_t i,PyObject * v)293 HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
294 {
295     int x;
296     /* PyArg_Parse's 'h' formatter is for a signed short, therefore
297        must use the next size up and manually do the overflow checking */
298     if (!PyArg_Parse(v, "i;array item must be integer", &x))
299         return -1;
300     else if (x < 0) {
301         PyErr_SetString(PyExc_OverflowError,
302             "unsigned short is less than minimum");
303         return -1;
304     }
305     else if (x > USHRT_MAX) {
306         PyErr_SetString(PyExc_OverflowError,
307             "unsigned short is greater than maximum");
308         return -1;
309     }
310     if (i >= 0)
311         ((short *)ap->ob_item)[i] = (short)x;
312     return 0;
313 }
314 
315 static PyObject *
i_getitem(arrayobject * ap,Py_ssize_t i)316 i_getitem(arrayobject *ap, Py_ssize_t i)
317 {
318     return PyLong_FromLong((long) ((int *)ap->ob_item)[i]);
319 }
320 
321 static int
i_setitem(arrayobject * ap,Py_ssize_t i,PyObject * v)322 i_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
323 {
324     int x;
325     /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
326     if (!PyArg_Parse(v, "i;array item must be integer", &x))
327         return -1;
328     if (i >= 0)
329                  ((int *)ap->ob_item)[i] = x;
330     return 0;
331 }
332 
333 static PyObject *
II_getitem(arrayobject * ap,Py_ssize_t i)334 II_getitem(arrayobject *ap, Py_ssize_t i)
335 {
336     return PyLong_FromUnsignedLong(
337         (unsigned long) ((unsigned int *)ap->ob_item)[i]);
338 }
339 
340 static PyObject *
get_int_unless_float(PyObject * v)341 get_int_unless_float(PyObject *v)
342 {
343     if (PyFloat_Check(v)) {
344         PyErr_SetString(PyExc_TypeError,
345                         "array item must be integer");
346         return NULL;
347     }
348     return _PyLong_FromNbIndexOrNbInt(v);
349 }
350 
351 static int
II_setitem(arrayobject * ap,Py_ssize_t i,PyObject * v)352 II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
353 {
354     unsigned long x;
355     int do_decref = 0; /* if nb_int was called */
356 
357     if (!PyLong_Check(v)) {
358         v = get_int_unless_float(v);
359         if (NULL == v) {
360             return -1;
361         }
362         do_decref = 1;
363     }
364     x = PyLong_AsUnsignedLong(v);
365     if (x == (unsigned long)-1 && PyErr_Occurred()) {
366         if (do_decref) {
367             Py_DECREF(v);
368         }
369         return -1;
370     }
371     if (x > UINT_MAX) {
372         PyErr_SetString(PyExc_OverflowError,
373                         "unsigned int is greater than maximum");
374         if (do_decref) {
375             Py_DECREF(v);
376         }
377         return -1;
378     }
379     if (i >= 0)
380         ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
381 
382     if (do_decref) {
383         Py_DECREF(v);
384     }
385     return 0;
386 }
387 
388 static PyObject *
l_getitem(arrayobject * ap,Py_ssize_t i)389 l_getitem(arrayobject *ap, Py_ssize_t i)
390 {
391     return PyLong_FromLong(((long *)ap->ob_item)[i]);
392 }
393 
394 static int
l_setitem(arrayobject * ap,Py_ssize_t i,PyObject * v)395 l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
396 {
397     long x;
398     if (!PyArg_Parse(v, "l;array item must be integer", &x))
399         return -1;
400     if (i >= 0)
401                  ((long *)ap->ob_item)[i] = x;
402     return 0;
403 }
404 
405 static PyObject *
LL_getitem(arrayobject * ap,Py_ssize_t i)406 LL_getitem(arrayobject *ap, Py_ssize_t i)
407 {
408     return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
409 }
410 
411 static int
LL_setitem(arrayobject * ap,Py_ssize_t i,PyObject * v)412 LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
413 {
414     unsigned long x;
415     int do_decref = 0; /* if nb_int was called */
416 
417     if (!PyLong_Check(v)) {
418         v = get_int_unless_float(v);
419         if (NULL == v) {
420             return -1;
421         }
422         do_decref = 1;
423     }
424     x = PyLong_AsUnsignedLong(v);
425     if (x == (unsigned long)-1 && PyErr_Occurred()) {
426         if (do_decref) {
427             Py_DECREF(v);
428         }
429         return -1;
430     }
431     if (i >= 0)
432         ((unsigned long *)ap->ob_item)[i] = x;
433 
434     if (do_decref) {
435         Py_DECREF(v);
436     }
437     return 0;
438 }
439 
440 static PyObject *
q_getitem(arrayobject * ap,Py_ssize_t i)441 q_getitem(arrayobject *ap, Py_ssize_t i)
442 {
443     return PyLong_FromLongLong(((long long *)ap->ob_item)[i]);
444 }
445 
446 static int
q_setitem(arrayobject * ap,Py_ssize_t i,PyObject * v)447 q_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
448 {
449     long long x;
450     if (!PyArg_Parse(v, "L;array item must be integer", &x))
451         return -1;
452     if (i >= 0)
453         ((long long *)ap->ob_item)[i] = x;
454     return 0;
455 }
456 
457 static PyObject *
QQ_getitem(arrayobject * ap,Py_ssize_t i)458 QQ_getitem(arrayobject *ap, Py_ssize_t i)
459 {
460     return PyLong_FromUnsignedLongLong(
461         ((unsigned long long *)ap->ob_item)[i]);
462 }
463 
464 static int
QQ_setitem(arrayobject * ap,Py_ssize_t i,PyObject * v)465 QQ_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
466 {
467     unsigned long long x;
468     int do_decref = 0; /* if nb_int was called */
469 
470     if (!PyLong_Check(v)) {
471         v = get_int_unless_float(v);
472         if (NULL == v) {
473             return -1;
474         }
475         do_decref = 1;
476     }
477     x = PyLong_AsUnsignedLongLong(v);
478     if (x == (unsigned long long)-1 && PyErr_Occurred()) {
479         if (do_decref) {
480             Py_DECREF(v);
481         }
482         return -1;
483     }
484     if (i >= 0)
485         ((unsigned long long *)ap->ob_item)[i] = x;
486 
487     if (do_decref) {
488         Py_DECREF(v);
489     }
490     return 0;
491 }
492 
493 static PyObject *
f_getitem(arrayobject * ap,Py_ssize_t i)494 f_getitem(arrayobject *ap, Py_ssize_t i)
495 {
496     return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
497 }
498 
499 static int
f_setitem(arrayobject * ap,Py_ssize_t i,PyObject * v)500 f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
501 {
502     float x;
503     if (!PyArg_Parse(v, "f;array item must be float", &x))
504         return -1;
505     if (i >= 0)
506                  ((float *)ap->ob_item)[i] = x;
507     return 0;
508 }
509 
510 static PyObject *
d_getitem(arrayobject * ap,Py_ssize_t i)511 d_getitem(arrayobject *ap, Py_ssize_t i)
512 {
513     return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
514 }
515 
516 static int
d_setitem(arrayobject * ap,Py_ssize_t i,PyObject * v)517 d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
518 {
519     double x;
520     if (!PyArg_Parse(v, "d;array item must be float", &x))
521         return -1;
522     if (i >= 0)
523                  ((double *)ap->ob_item)[i] = x;
524     return 0;
525 }
526 
527 #define DEFINE_COMPAREITEMS(code, type) \
528     static int \
529     code##_compareitems(const void *lhs, const void *rhs, Py_ssize_t length) \
530     { \
531         const type *a = lhs, *b = rhs; \
532         for (Py_ssize_t i = 0; i < length; ++i) \
533             if (a[i] != b[i]) \
534                 return a[i] < b[i] ? -1 : 1; \
535         return 0; \
536     }
537 
538 DEFINE_COMPAREITEMS(b, signed char)
539 DEFINE_COMPAREITEMS(BB, unsigned char)
540 DEFINE_COMPAREITEMS(u, wchar_t)
541 DEFINE_COMPAREITEMS(h, short)
542 DEFINE_COMPAREITEMS(HH, unsigned short)
543 DEFINE_COMPAREITEMS(i, int)
544 DEFINE_COMPAREITEMS(II, unsigned int)
545 DEFINE_COMPAREITEMS(l, long)
546 DEFINE_COMPAREITEMS(LL, unsigned long)
547 DEFINE_COMPAREITEMS(q, long long)
548 DEFINE_COMPAREITEMS(QQ, unsigned long long)
549 
550 /* Description of types.
551  *
552  * Don't forget to update typecode_to_mformat_code() if you add a new
553  * typecode.
554  */
555 static const struct arraydescr descriptors[] = {
556     {'b', 1, b_getitem, b_setitem, b_compareitems, "b", 1, 1},
557     {'B', 1, BB_getitem, BB_setitem, BB_compareitems, "B", 1, 0},
558     {'u', sizeof(wchar_t), u_getitem, u_setitem, u_compareitems, "u", 0, 0},
559     {'h', sizeof(short), h_getitem, h_setitem, h_compareitems, "h", 1, 1},
560     {'H', sizeof(short), HH_getitem, HH_setitem, HH_compareitems, "H", 1, 0},
561     {'i', sizeof(int), i_getitem, i_setitem, i_compareitems, "i", 1, 1},
562     {'I', sizeof(int), II_getitem, II_setitem, II_compareitems, "I", 1, 0},
563     {'l', sizeof(long), l_getitem, l_setitem, l_compareitems, "l", 1, 1},
564     {'L', sizeof(long), LL_getitem, LL_setitem, LL_compareitems, "L", 1, 0},
565     {'q', sizeof(long long), q_getitem, q_setitem, q_compareitems, "q", 1, 1},
566     {'Q', sizeof(long long), QQ_getitem, QQ_setitem, QQ_compareitems, "Q", 1, 0},
567     {'f', sizeof(float), f_getitem, f_setitem, NULL, "f", 0, 0},
568     {'d', sizeof(double), d_getitem, d_setitem, NULL, "d", 0, 0},
569     {'\0', 0, 0, 0, 0, 0, 0} /* Sentinel */
570 };
571 
572 /****************************************************************************
573 Implementations of array object methods.
574 ****************************************************************************/
575 /*[clinic input]
576 class array.array "arrayobject *" "&Arraytype"
577 [clinic start generated code]*/
578 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=ad43d37e942a8854]*/
579 
580 static PyObject *
newarrayobject(PyTypeObject * type,Py_ssize_t size,const struct arraydescr * descr)581 newarrayobject(PyTypeObject *type, Py_ssize_t size, const struct arraydescr *descr)
582 {
583     arrayobject *op;
584     size_t nbytes;
585 
586     if (size < 0) {
587         PyErr_BadInternalCall();
588         return NULL;
589     }
590 
591     /* Check for overflow */
592     if (size > PY_SSIZE_T_MAX / descr->itemsize) {
593         return PyErr_NoMemory();
594     }
595     nbytes = size * descr->itemsize;
596     op = (arrayobject *) type->tp_alloc(type, 0);
597     if (op == NULL) {
598         return NULL;
599     }
600     op->ob_descr = descr;
601     op->allocated = size;
602     op->weakreflist = NULL;
603     Py_SET_SIZE(op, size);
604     if (size <= 0) {
605         op->ob_item = NULL;
606     }
607     else {
608         op->ob_item = PyMem_NEW(char, nbytes);
609         if (op->ob_item == NULL) {
610             Py_DECREF(op);
611             return PyErr_NoMemory();
612         }
613     }
614     op->ob_exports = 0;
615     return (PyObject *) op;
616 }
617 
618 static PyObject *
getarrayitem(PyObject * op,Py_ssize_t i)619 getarrayitem(PyObject *op, Py_ssize_t i)
620 {
621     arrayobject *ap;
622     assert(array_Check(op));
623     ap = (arrayobject *)op;
624     assert(i>=0 && i<Py_SIZE(ap));
625     return (*ap->ob_descr->getitem)(ap, i);
626 }
627 
628 static int
ins1(arrayobject * self,Py_ssize_t where,PyObject * v)629 ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
630 {
631     char *items;
632     Py_ssize_t n = Py_SIZE(self);
633     if (v == NULL) {
634         PyErr_BadInternalCall();
635         return -1;
636     }
637     if ((*self->ob_descr->setitem)(self, -1, v) < 0)
638         return -1;
639 
640     if (array_resize(self, n+1) == -1)
641         return -1;
642     items = self->ob_item;
643     if (where < 0) {
644         where += n;
645         if (where < 0)
646             where = 0;
647     }
648     if (where > n)
649         where = n;
650     /* appends don't need to call memmove() */
651     if (where != n)
652         memmove(items + (where+1)*self->ob_descr->itemsize,
653             items + where*self->ob_descr->itemsize,
654             (n-where)*self->ob_descr->itemsize);
655     return (*self->ob_descr->setitem)(self, where, v);
656 }
657 
658 /* Methods */
659 
660 static void
array_dealloc(arrayobject * op)661 array_dealloc(arrayobject *op)
662 {
663     if (op->weakreflist != NULL)
664         PyObject_ClearWeakRefs((PyObject *) op);
665     if (op->ob_item != NULL)
666         PyMem_DEL(op->ob_item);
667     Py_TYPE(op)->tp_free((PyObject *)op);
668 }
669 
670 static PyObject *
array_richcompare(PyObject * v,PyObject * w,int op)671 array_richcompare(PyObject *v, PyObject *w, int op)
672 {
673     arrayobject *va, *wa;
674     PyObject *vi = NULL;
675     PyObject *wi = NULL;
676     Py_ssize_t i, k;
677     PyObject *res;
678 
679     if (!array_Check(v) || !array_Check(w))
680         Py_RETURN_NOTIMPLEMENTED;
681 
682     va = (arrayobject *)v;
683     wa = (arrayobject *)w;
684 
685     if (Py_SIZE(va) != Py_SIZE(wa) && (op == Py_EQ || op == Py_NE)) {
686         /* Shortcut: if the lengths differ, the arrays differ */
687         if (op == Py_EQ)
688             res = Py_False;
689         else
690             res = Py_True;
691         Py_INCREF(res);
692         return res;
693     }
694 
695     if (va->ob_descr == wa->ob_descr && va->ob_descr->compareitems != NULL) {
696         /* Fast path:
697            arrays with same types can have their buffers compared directly */
698         Py_ssize_t common_length = Py_MIN(Py_SIZE(va), Py_SIZE(wa));
699         int result = va->ob_descr->compareitems(va->ob_item, wa->ob_item,
700                                                 common_length);
701         if (result == 0)
702             goto compare_sizes;
703 
704         int cmp;
705         switch (op) {
706         case Py_LT: cmp = result < 0; break;
707         case Py_LE: cmp = result <= 0; break;
708         case Py_EQ: cmp = result == 0; break;
709         case Py_NE: cmp = result != 0; break;
710         case Py_GT: cmp = result > 0; break;
711         case Py_GE: cmp = result >= 0; break;
712         default: return NULL; /* cannot happen */
713         }
714         PyObject *res = cmp ? Py_True : Py_False;
715         Py_INCREF(res);
716         return res;
717     }
718 
719 
720     /* Search for the first index where items are different */
721     k = 1;
722     for (i = 0; i < Py_SIZE(va) && i < Py_SIZE(wa); i++) {
723         vi = getarrayitem(v, i);
724         wi = getarrayitem(w, i);
725         if (vi == NULL || wi == NULL) {
726             Py_XDECREF(vi);
727             Py_XDECREF(wi);
728             return NULL;
729         }
730         k = PyObject_RichCompareBool(vi, wi, Py_EQ);
731         if (k == 0)
732             break; /* Keeping vi and wi alive! */
733         Py_DECREF(vi);
734         Py_DECREF(wi);
735         if (k < 0)
736             return NULL;
737     }
738 
739     if (k) {
740         /* No more items to compare -- compare sizes */
741         compare_sizes: ;
742         Py_ssize_t vs = Py_SIZE(va);
743         Py_ssize_t ws = Py_SIZE(wa);
744         int cmp;
745         switch (op) {
746         case Py_LT: cmp = vs <  ws; break;
747         case Py_LE: cmp = vs <= ws; break;
748         /* If the lengths were not equal,
749            the earlier fast-path check would have caught that. */
750         case Py_EQ: assert(vs == ws); cmp = 1; break;
751         case Py_NE: assert(vs == ws); cmp = 0; break;
752         case Py_GT: cmp = vs >  ws; break;
753         case Py_GE: cmp = vs >= ws; break;
754         default: return NULL; /* cannot happen */
755         }
756         if (cmp)
757             res = Py_True;
758         else
759             res = Py_False;
760         Py_INCREF(res);
761         return res;
762     }
763 
764     /* We have an item that differs.  First, shortcuts for EQ/NE */
765     if (op == Py_EQ) {
766         Py_INCREF(Py_False);
767         res = Py_False;
768     }
769     else if (op == Py_NE) {
770         Py_INCREF(Py_True);
771         res = Py_True;
772     }
773     else {
774         /* Compare the final item again using the proper operator */
775         res = PyObject_RichCompare(vi, wi, op);
776     }
777     Py_DECREF(vi);
778     Py_DECREF(wi);
779     return res;
780 }
781 
782 static Py_ssize_t
array_length(arrayobject * a)783 array_length(arrayobject *a)
784 {
785     return Py_SIZE(a);
786 }
787 
788 static PyObject *
array_item(arrayobject * a,Py_ssize_t i)789 array_item(arrayobject *a, Py_ssize_t i)
790 {
791     if (i < 0 || i >= Py_SIZE(a)) {
792         PyErr_SetString(PyExc_IndexError, "array index out of range");
793         return NULL;
794     }
795     return getarrayitem((PyObject *)a, i);
796 }
797 
798 static PyObject *
array_slice(arrayobject * a,Py_ssize_t ilow,Py_ssize_t ihigh)799 array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
800 {
801     arrayobject *np;
802     if (ilow < 0)
803         ilow = 0;
804     else if (ilow > Py_SIZE(a))
805         ilow = Py_SIZE(a);
806     if (ihigh < 0)
807         ihigh = 0;
808     if (ihigh < ilow)
809         ihigh = ilow;
810     else if (ihigh > Py_SIZE(a))
811         ihigh = Py_SIZE(a);
812     np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
813     if (np == NULL)
814         return NULL;
815     if (ihigh > ilow) {
816         memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
817                (ihigh-ilow) * a->ob_descr->itemsize);
818     }
819     return (PyObject *)np;
820 }
821 
822 
823 /*[clinic input]
824 array.array.__copy__
825 
826 Return a copy of the array.
827 [clinic start generated code]*/
828 
829 static PyObject *
array_array___copy___impl(arrayobject * self)830 array_array___copy___impl(arrayobject *self)
831 /*[clinic end generated code: output=dec7c3f925d9619e input=ad1ee5b086965f09]*/
832 {
833     return array_slice(self, 0, Py_SIZE(self));
834 }
835 
836 /*[clinic input]
837 array.array.__deepcopy__
838 
839     unused: object
840     /
841 
842 Return a copy of the array.
843 [clinic start generated code]*/
844 
845 static PyObject *
array_array___deepcopy__(arrayobject * self,PyObject * unused)846 array_array___deepcopy__(arrayobject *self, PyObject *unused)
847 /*[clinic end generated code: output=1ec748d8e14a9faa input=2405ecb4933748c4]*/
848 {
849     return array_array___copy___impl(self);
850 }
851 
852 static PyObject *
array_concat(arrayobject * a,PyObject * bb)853 array_concat(arrayobject *a, PyObject *bb)
854 {
855     Py_ssize_t size;
856     arrayobject *np;
857     if (!array_Check(bb)) {
858         PyErr_Format(PyExc_TypeError,
859              "can only append array (not \"%.200s\") to array",
860                  Py_TYPE(bb)->tp_name);
861         return NULL;
862     }
863 #define b ((arrayobject *)bb)
864     if (a->ob_descr != b->ob_descr) {
865         PyErr_BadArgument();
866         return NULL;
867     }
868     if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) {
869         return PyErr_NoMemory();
870     }
871     size = Py_SIZE(a) + Py_SIZE(b);
872     np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
873     if (np == NULL) {
874         return NULL;
875     }
876     if (Py_SIZE(a) > 0) {
877         memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize);
878     }
879     if (Py_SIZE(b) > 0) {
880         memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize,
881                b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
882     }
883     return (PyObject *)np;
884 #undef b
885 }
886 
887 static PyObject *
array_repeat(arrayobject * a,Py_ssize_t n)888 array_repeat(arrayobject *a, Py_ssize_t n)
889 {
890     Py_ssize_t size;
891     arrayobject *np;
892     Py_ssize_t oldbytes, newbytes;
893     if (n < 0)
894         n = 0;
895     if ((Py_SIZE(a) != 0) && (n > PY_SSIZE_T_MAX / Py_SIZE(a))) {
896         return PyErr_NoMemory();
897     }
898     size = Py_SIZE(a) * n;
899     np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
900     if (np == NULL)
901         return NULL;
902     if (size == 0)
903         return (PyObject *)np;
904     oldbytes = Py_SIZE(a) * a->ob_descr->itemsize;
905     newbytes = oldbytes * n;
906     /* this follows the code in unicode_repeat */
907     if (oldbytes == 1) {
908         memset(np->ob_item, a->ob_item[0], newbytes);
909     } else {
910         Py_ssize_t done = oldbytes;
911         memcpy(np->ob_item, a->ob_item, oldbytes);
912         while (done < newbytes) {
913             Py_ssize_t ncopy = (done <= newbytes-done) ? done : newbytes-done;
914             memcpy(np->ob_item+done, np->ob_item, ncopy);
915             done += ncopy;
916         }
917     }
918     return (PyObject *)np;
919 }
920 
921 static int
array_del_slice(arrayobject * a,Py_ssize_t ilow,Py_ssize_t ihigh)922 array_del_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
923 {
924     char *item;
925     Py_ssize_t d; /* Change in size */
926     if (ilow < 0)
927         ilow = 0;
928     else if (ilow > Py_SIZE(a))
929         ilow = Py_SIZE(a);
930     if (ihigh < 0)
931         ihigh = 0;
932     if (ihigh < ilow)
933         ihigh = ilow;
934     else if (ihigh > Py_SIZE(a))
935         ihigh = Py_SIZE(a);
936     item = a->ob_item;
937     d = ihigh-ilow;
938     /* Issue #4509: If the array has exported buffers and the slice
939        assignment would change the size of the array, fail early to make
940        sure we don't modify it. */
941     if (d != 0 && a->ob_exports > 0) {
942         PyErr_SetString(PyExc_BufferError,
943             "cannot resize an array that is exporting buffers");
944         return -1;
945     }
946     if (d > 0) { /* Delete d items */
947         memmove(item + (ihigh-d)*a->ob_descr->itemsize,
948             item + ihigh*a->ob_descr->itemsize,
949             (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
950         if (array_resize(a, Py_SIZE(a) - d) == -1)
951             return -1;
952     }
953     return 0;
954 }
955 
956 static int
array_ass_item(arrayobject * a,Py_ssize_t i,PyObject * v)957 array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v)
958 {
959     if (i < 0 || i >= Py_SIZE(a)) {
960         PyErr_SetString(PyExc_IndexError,
961                          "array assignment index out of range");
962         return -1;
963     }
964     if (v == NULL)
965         return array_del_slice(a, i, i+1);
966     return (*a->ob_descr->setitem)(a, i, v);
967 }
968 
969 static int
setarrayitem(PyObject * a,Py_ssize_t i,PyObject * v)970 setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v)
971 {
972     assert(array_Check(a));
973     return array_ass_item((arrayobject *)a, i, v);
974 }
975 
976 static int
array_iter_extend(arrayobject * self,PyObject * bb)977 array_iter_extend(arrayobject *self, PyObject *bb)
978 {
979     PyObject *it, *v;
980 
981     it = PyObject_GetIter(bb);
982     if (it == NULL)
983         return -1;
984 
985     while ((v = PyIter_Next(it)) != NULL) {
986         if (ins1(self, Py_SIZE(self), v) != 0) {
987             Py_DECREF(v);
988             Py_DECREF(it);
989             return -1;
990         }
991         Py_DECREF(v);
992     }
993     Py_DECREF(it);
994     if (PyErr_Occurred())
995         return -1;
996     return 0;
997 }
998 
999 static int
array_do_extend(arrayobject * self,PyObject * bb)1000 array_do_extend(arrayobject *self, PyObject *bb)
1001 {
1002     Py_ssize_t size, oldsize, bbsize;
1003 
1004     if (!array_Check(bb))
1005         return array_iter_extend(self, bb);
1006 #define b ((arrayobject *)bb)
1007     if (self->ob_descr != b->ob_descr) {
1008         PyErr_SetString(PyExc_TypeError,
1009                      "can only extend with array of same kind");
1010         return -1;
1011     }
1012     if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) ||
1013         ((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
1014         PyErr_NoMemory();
1015         return -1;
1016     }
1017     oldsize = Py_SIZE(self);
1018     /* Get the size of bb before resizing the array since bb could be self. */
1019     bbsize = Py_SIZE(bb);
1020     size = oldsize + Py_SIZE(b);
1021     if (array_resize(self, size) == -1)
1022         return -1;
1023     if (bbsize > 0) {
1024         memcpy(self->ob_item + oldsize * self->ob_descr->itemsize,
1025             b->ob_item, bbsize * b->ob_descr->itemsize);
1026     }
1027 
1028     return 0;
1029 #undef b
1030 }
1031 
1032 static PyObject *
array_inplace_concat(arrayobject * self,PyObject * bb)1033 array_inplace_concat(arrayobject *self, PyObject *bb)
1034 {
1035     if (!array_Check(bb)) {
1036         PyErr_Format(PyExc_TypeError,
1037             "can only extend array with array (not \"%.200s\")",
1038             Py_TYPE(bb)->tp_name);
1039         return NULL;
1040     }
1041     if (array_do_extend(self, bb) == -1)
1042         return NULL;
1043     Py_INCREF(self);
1044     return (PyObject *)self;
1045 }
1046 
1047 static PyObject *
array_inplace_repeat(arrayobject * self,Py_ssize_t n)1048 array_inplace_repeat(arrayobject *self, Py_ssize_t n)
1049 {
1050     char *items, *p;
1051     Py_ssize_t size, i;
1052 
1053     if (Py_SIZE(self) > 0) {
1054         if (n < 0)
1055             n = 0;
1056         if ((self->ob_descr->itemsize != 0) &&
1057             (Py_SIZE(self) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
1058             return PyErr_NoMemory();
1059         }
1060         size = Py_SIZE(self) * self->ob_descr->itemsize;
1061         if (n > 0 && size > PY_SSIZE_T_MAX / n) {
1062             return PyErr_NoMemory();
1063         }
1064         if (array_resize(self, n * Py_SIZE(self)) == -1)
1065             return NULL;
1066         items = p = self->ob_item;
1067         for (i = 1; i < n; i++) {
1068             p += size;
1069             memcpy(p, items, size);
1070         }
1071     }
1072     Py_INCREF(self);
1073     return (PyObject *)self;
1074 }
1075 
1076 
1077 static PyObject *
ins(arrayobject * self,Py_ssize_t where,PyObject * v)1078 ins(arrayobject *self, Py_ssize_t where, PyObject *v)
1079 {
1080     if (ins1(self, where, v) != 0)
1081         return NULL;
1082     Py_RETURN_NONE;
1083 }
1084 
1085 /*[clinic input]
1086 array.array.count
1087 
1088     v: object
1089     /
1090 
1091 Return number of occurrences of v in the array.
1092 [clinic start generated code]*/
1093 
1094 static PyObject *
array_array_count(arrayobject * self,PyObject * v)1095 array_array_count(arrayobject *self, PyObject *v)
1096 /*[clinic end generated code: output=3dd3624bf7135a3a input=d9bce9d65e39d1f5]*/
1097 {
1098     Py_ssize_t count = 0;
1099     Py_ssize_t i;
1100 
1101     for (i = 0; i < Py_SIZE(self); i++) {
1102         PyObject *selfi;
1103         int cmp;
1104 
1105         selfi = getarrayitem((PyObject *)self, i);
1106         if (selfi == NULL)
1107             return NULL;
1108         cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
1109         Py_DECREF(selfi);
1110         if (cmp > 0)
1111             count++;
1112         else if (cmp < 0)
1113             return NULL;
1114     }
1115     return PyLong_FromSsize_t(count);
1116 }
1117 
1118 
1119 /*[clinic input]
1120 array.array.index
1121 
1122     v: object
1123     /
1124 
1125 Return index of first occurrence of v in the array.
1126 [clinic start generated code]*/
1127 
1128 static PyObject *
array_array_index(arrayobject * self,PyObject * v)1129 array_array_index(arrayobject *self, PyObject *v)
1130 /*[clinic end generated code: output=d48498d325602167 input=cf619898c6649d08]*/
1131 {
1132     Py_ssize_t i;
1133 
1134     for (i = 0; i < Py_SIZE(self); i++) {
1135         PyObject *selfi;
1136         int cmp;
1137 
1138         selfi = getarrayitem((PyObject *)self, i);
1139         if (selfi == NULL)
1140             return NULL;
1141         cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
1142         Py_DECREF(selfi);
1143         if (cmp > 0) {
1144             return PyLong_FromSsize_t(i);
1145         }
1146         else if (cmp < 0)
1147             return NULL;
1148     }
1149     PyErr_SetString(PyExc_ValueError, "array.index(x): x not in array");
1150     return NULL;
1151 }
1152 
1153 static int
array_contains(arrayobject * self,PyObject * v)1154 array_contains(arrayobject *self, PyObject *v)
1155 {
1156     Py_ssize_t i;
1157     int cmp;
1158 
1159     for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) {
1160         PyObject *selfi = getarrayitem((PyObject *)self, i);
1161         if (selfi == NULL)
1162             return -1;
1163         cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
1164         Py_DECREF(selfi);
1165     }
1166     return cmp;
1167 }
1168 
1169 /*[clinic input]
1170 array.array.remove
1171 
1172     v: object
1173     /
1174 
1175 Remove the first occurrence of v in the array.
1176 [clinic start generated code]*/
1177 
1178 static PyObject *
array_array_remove(arrayobject * self,PyObject * v)1179 array_array_remove(arrayobject *self, PyObject *v)
1180 /*[clinic end generated code: output=bef06be9fdf9dceb input=0b1e5aed25590027]*/
1181 {
1182     Py_ssize_t i;
1183 
1184     for (i = 0; i < Py_SIZE(self); i++) {
1185         PyObject *selfi;
1186         int cmp;
1187 
1188         selfi = getarrayitem((PyObject *)self,i);
1189         if (selfi == NULL)
1190             return NULL;
1191         cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
1192         Py_DECREF(selfi);
1193         if (cmp > 0) {
1194             if (array_del_slice(self, i, i+1) != 0)
1195                 return NULL;
1196             Py_RETURN_NONE;
1197         }
1198         else if (cmp < 0)
1199             return NULL;
1200     }
1201     PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in array");
1202     return NULL;
1203 }
1204 
1205 /*[clinic input]
1206 array.array.pop
1207 
1208     i: Py_ssize_t = -1
1209     /
1210 
1211 Return the i-th element and delete it from the array.
1212 
1213 i defaults to -1.
1214 [clinic start generated code]*/
1215 
1216 static PyObject *
array_array_pop_impl(arrayobject * self,Py_ssize_t i)1217 array_array_pop_impl(arrayobject *self, Py_ssize_t i)
1218 /*[clinic end generated code: output=bc1f0c54fe5308e4 input=8e5feb4c1a11cd44]*/
1219 {
1220     PyObject *v;
1221 
1222     if (Py_SIZE(self) == 0) {
1223         /* Special-case most common failure cause */
1224         PyErr_SetString(PyExc_IndexError, "pop from empty array");
1225         return NULL;
1226     }
1227     if (i < 0)
1228         i += Py_SIZE(self);
1229     if (i < 0 || i >= Py_SIZE(self)) {
1230         PyErr_SetString(PyExc_IndexError, "pop index out of range");
1231         return NULL;
1232     }
1233     v = getarrayitem((PyObject *)self, i);
1234     if (v == NULL)
1235         return NULL;
1236     if (array_del_slice(self, i, i+1) != 0) {
1237         Py_DECREF(v);
1238         return NULL;
1239     }
1240     return v;
1241 }
1242 
1243 /*[clinic input]
1244 array.array.extend
1245 
1246     bb: object
1247     /
1248 
1249 Append items to the end of the array.
1250 [clinic start generated code]*/
1251 
1252 static PyObject *
array_array_extend(arrayobject * self,PyObject * bb)1253 array_array_extend(arrayobject *self, PyObject *bb)
1254 /*[clinic end generated code: output=bbddbc8e8bef871d input=43be86aba5c31e44]*/
1255 {
1256     if (array_do_extend(self, bb) == -1)
1257         return NULL;
1258     Py_RETURN_NONE;
1259 }
1260 
1261 /*[clinic input]
1262 array.array.insert
1263 
1264     i: Py_ssize_t
1265     v: object
1266     /
1267 
1268 Insert a new item v into the array before position i.
1269 [clinic start generated code]*/
1270 
1271 static PyObject *
array_array_insert_impl(arrayobject * self,Py_ssize_t i,PyObject * v)1272 array_array_insert_impl(arrayobject *self, Py_ssize_t i, PyObject *v)
1273 /*[clinic end generated code: output=5a3648e278348564 input=5577d1b4383e9313]*/
1274 {
1275     return ins(self, i, v);
1276 }
1277 
1278 /*[clinic input]
1279 array.array.buffer_info
1280 
1281 Return a tuple (address, length) giving the current memory address and the length in items of the buffer used to hold array's contents.
1282 
1283 The length should be multiplied by the itemsize attribute to calculate
1284 the buffer length in bytes.
1285 [clinic start generated code]*/
1286 
1287 static PyObject *
array_array_buffer_info_impl(arrayobject * self)1288 array_array_buffer_info_impl(arrayobject *self)
1289 /*[clinic end generated code: output=9b2a4ec3ae7e98e7 input=a58bae5c6e1ac6a6]*/
1290 {
1291     PyObject *retval = NULL, *v;
1292 
1293     retval = PyTuple_New(2);
1294     if (!retval)
1295         return NULL;
1296 
1297     v = PyLong_FromVoidPtr(self->ob_item);
1298     if (v == NULL) {
1299         Py_DECREF(retval);
1300         return NULL;
1301     }
1302     PyTuple_SET_ITEM(retval, 0, v);
1303 
1304     v = PyLong_FromSsize_t(Py_SIZE(self));
1305     if (v == NULL) {
1306         Py_DECREF(retval);
1307         return NULL;
1308     }
1309     PyTuple_SET_ITEM(retval, 1, v);
1310 
1311     return retval;
1312 }
1313 
1314 /*[clinic input]
1315 array.array.append
1316 
1317     v: object
1318     /
1319 
1320 Append new value v to the end of the array.
1321 [clinic start generated code]*/
1322 
1323 static PyObject *
array_array_append(arrayobject * self,PyObject * v)1324 array_array_append(arrayobject *self, PyObject *v)
1325 /*[clinic end generated code: output=745a0669bf8db0e2 input=0b98d9d78e78f0fa]*/
1326 {
1327     return ins(self, Py_SIZE(self), v);
1328 }
1329 
1330 /*[clinic input]
1331 array.array.byteswap
1332 
1333 Byteswap all items of the array.
1334 
1335 If the items in the array are not 1, 2, 4, or 8 bytes in size, RuntimeError is
1336 raised.
1337 [clinic start generated code]*/
1338 
1339 static PyObject *
array_array_byteswap_impl(arrayobject * self)1340 array_array_byteswap_impl(arrayobject *self)
1341 /*[clinic end generated code: output=5f8236cbdf0d90b5 input=6a85591b950a0186]*/
1342 {
1343     char *p;
1344     Py_ssize_t i;
1345 
1346     switch (self->ob_descr->itemsize) {
1347     case 1:
1348         break;
1349     case 2:
1350         for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) {
1351             char p0 = p[0];
1352             p[0] = p[1];
1353             p[1] = p0;
1354         }
1355         break;
1356     case 4:
1357         for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 4) {
1358             char p0 = p[0];
1359             char p1 = p[1];
1360             p[0] = p[3];
1361             p[1] = p[2];
1362             p[2] = p1;
1363             p[3] = p0;
1364         }
1365         break;
1366     case 8:
1367         for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) {
1368             char p0 = p[0];
1369             char p1 = p[1];
1370             char p2 = p[2];
1371             char p3 = p[3];
1372             p[0] = p[7];
1373             p[1] = p[6];
1374             p[2] = p[5];
1375             p[3] = p[4];
1376             p[4] = p3;
1377             p[5] = p2;
1378             p[6] = p1;
1379             p[7] = p0;
1380         }
1381         break;
1382     default:
1383         PyErr_SetString(PyExc_RuntimeError,
1384                    "don't know how to byteswap this array type");
1385         return NULL;
1386     }
1387     Py_RETURN_NONE;
1388 }
1389 
1390 /*[clinic input]
1391 array.array.reverse
1392 
1393 Reverse the order of the items in the array.
1394 [clinic start generated code]*/
1395 
1396 static PyObject *
array_array_reverse_impl(arrayobject * self)1397 array_array_reverse_impl(arrayobject *self)
1398 /*[clinic end generated code: output=c04868b36f6f4089 input=cd904f01b27d966a]*/
1399 {
1400     Py_ssize_t itemsize = self->ob_descr->itemsize;
1401     char *p, *q;
1402     /* little buffer to hold items while swapping */
1403     char tmp[256];      /* 8 is probably enough -- but why skimp */
1404     assert((size_t)itemsize <= sizeof(tmp));
1405 
1406     if (Py_SIZE(self) > 1) {
1407         for (p = self->ob_item,
1408              q = self->ob_item + (Py_SIZE(self) - 1)*itemsize;
1409              p < q;
1410              p += itemsize, q -= itemsize) {
1411             /* memory areas guaranteed disjoint, so memcpy
1412              * is safe (& memmove may be slower).
1413              */
1414             memcpy(tmp, p, itemsize);
1415             memcpy(p, q, itemsize);
1416             memcpy(q, tmp, itemsize);
1417         }
1418     }
1419 
1420     Py_RETURN_NONE;
1421 }
1422 
1423 /*[clinic input]
1424 array.array.fromfile
1425 
1426     f: object
1427     n: Py_ssize_t
1428     /
1429 
1430 Read n objects from the file object f and append them to the end of the array.
1431 [clinic start generated code]*/
1432 
1433 static PyObject *
array_array_fromfile_impl(arrayobject * self,PyObject * f,Py_ssize_t n)1434 array_array_fromfile_impl(arrayobject *self, PyObject *f, Py_ssize_t n)
1435 /*[clinic end generated code: output=ec9f600e10f53510 input=e188afe8e58adf40]*/
1436 {
1437     PyObject *b, *res;
1438     Py_ssize_t itemsize = self->ob_descr->itemsize;
1439     Py_ssize_t nbytes;
1440     _Py_IDENTIFIER(read);
1441     int not_enough_bytes;
1442 
1443     if (n < 0) {
1444         PyErr_SetString(PyExc_ValueError, "negative count");
1445         return NULL;
1446     }
1447     if (n > PY_SSIZE_T_MAX / itemsize) {
1448         PyErr_NoMemory();
1449         return NULL;
1450     }
1451     nbytes = n * itemsize;
1452 
1453     b = _PyObject_CallMethodId(f, &PyId_read, "n", nbytes);
1454     if (b == NULL)
1455         return NULL;
1456 
1457     if (!PyBytes_Check(b)) {
1458         PyErr_SetString(PyExc_TypeError,
1459                         "read() didn't return bytes");
1460         Py_DECREF(b);
1461         return NULL;
1462     }
1463 
1464     not_enough_bytes = (PyBytes_GET_SIZE(b) != nbytes);
1465 
1466     res = array_array_frombytes(self, b);
1467     Py_DECREF(b);
1468     if (res == NULL)
1469         return NULL;
1470 
1471     if (not_enough_bytes) {
1472         PyErr_SetString(PyExc_EOFError,
1473                         "read() didn't return enough bytes");
1474         Py_DECREF(res);
1475         return NULL;
1476     }
1477 
1478     return res;
1479 }
1480 
1481 /*[clinic input]
1482 array.array.tofile
1483 
1484     f: object
1485     /
1486 
1487 Write all items (as machine values) to the file object f.
1488 [clinic start generated code]*/
1489 
1490 static PyObject *
array_array_tofile(arrayobject * self,PyObject * f)1491 array_array_tofile(arrayobject *self, PyObject *f)
1492 /*[clinic end generated code: output=3a2cfa8128df0777 input=b0669a484aab0831]*/
1493 {
1494     Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize;
1495     /* Write 64K blocks at a time */
1496     /* XXX Make the block size settable */
1497     int BLOCKSIZE = 64*1024;
1498     Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE;
1499     Py_ssize_t i;
1500 
1501     if (Py_SIZE(self) == 0)
1502         goto done;
1503 
1504     for (i = 0; i < nblocks; i++) {
1505         char* ptr = self->ob_item + i*BLOCKSIZE;
1506         Py_ssize_t size = BLOCKSIZE;
1507         PyObject *bytes, *res;
1508         _Py_IDENTIFIER(write);
1509 
1510         if (i*BLOCKSIZE + size > nbytes)
1511             size = nbytes - i*BLOCKSIZE;
1512         bytes = PyBytes_FromStringAndSize(ptr, size);
1513         if (bytes == NULL)
1514             return NULL;
1515         res = _PyObject_CallMethodIdOneArg(f, &PyId_write, bytes);
1516         Py_DECREF(bytes);
1517         if (res == NULL)
1518             return NULL;
1519         Py_DECREF(res); /* drop write result */
1520     }
1521 
1522   done:
1523     Py_RETURN_NONE;
1524 }
1525 
1526 /*[clinic input]
1527 array.array.fromlist
1528 
1529     list: object
1530     /
1531 
1532 Append items to array from list.
1533 [clinic start generated code]*/
1534 
1535 static PyObject *
array_array_fromlist(arrayobject * self,PyObject * list)1536 array_array_fromlist(arrayobject *self, PyObject *list)
1537 /*[clinic end generated code: output=26411c2d228a3e3f input=be2605a96c49680f]*/
1538 {
1539     Py_ssize_t n;
1540 
1541     if (!PyList_Check(list)) {
1542         PyErr_SetString(PyExc_TypeError, "arg must be list");
1543         return NULL;
1544     }
1545     n = PyList_Size(list);
1546     if (n > 0) {
1547         Py_ssize_t i, old_size;
1548         old_size = Py_SIZE(self);
1549         if (array_resize(self, old_size + n) == -1)
1550             return NULL;
1551         for (i = 0; i < n; i++) {
1552             PyObject *v = PyList_GET_ITEM(list, i);
1553             if ((*self->ob_descr->setitem)(self,
1554                             Py_SIZE(self) - n + i, v) != 0) {
1555                 array_resize(self, old_size);
1556                 return NULL;
1557             }
1558             if (n != PyList_GET_SIZE(list)) {
1559                 PyErr_SetString(PyExc_RuntimeError,
1560                                 "list changed size during iteration");
1561                 array_resize(self, old_size);
1562                 return NULL;
1563             }
1564         }
1565     }
1566     Py_RETURN_NONE;
1567 }
1568 
1569 /*[clinic input]
1570 array.array.tolist
1571 
1572 Convert array to an ordinary list with the same items.
1573 [clinic start generated code]*/
1574 
1575 static PyObject *
array_array_tolist_impl(arrayobject * self)1576 array_array_tolist_impl(arrayobject *self)
1577 /*[clinic end generated code: output=00b60cc9eab8ef89 input=a8d7784a94f86b53]*/
1578 {
1579     PyObject *list = PyList_New(Py_SIZE(self));
1580     Py_ssize_t i;
1581 
1582     if (list == NULL)
1583         return NULL;
1584     for (i = 0; i < Py_SIZE(self); i++) {
1585         PyObject *v = getarrayitem((PyObject *)self, i);
1586         if (v == NULL)
1587             goto error;
1588         PyList_SET_ITEM(list, i, v);
1589     }
1590     return list;
1591 
1592 error:
1593     Py_DECREF(list);
1594     return NULL;
1595 }
1596 
1597 static PyObject *
frombytes(arrayobject * self,Py_buffer * buffer)1598 frombytes(arrayobject *self, Py_buffer *buffer)
1599 {
1600     int itemsize = self->ob_descr->itemsize;
1601     Py_ssize_t n;
1602     if (buffer->itemsize != 1) {
1603         PyBuffer_Release(buffer);
1604         PyErr_SetString(PyExc_TypeError, "a bytes-like object is required");
1605         return NULL;
1606     }
1607     n = buffer->len;
1608     if (n % itemsize != 0) {
1609         PyBuffer_Release(buffer);
1610         PyErr_SetString(PyExc_ValueError,
1611                    "bytes length not a multiple of item size");
1612         return NULL;
1613     }
1614     n = n / itemsize;
1615     if (n > 0) {
1616         Py_ssize_t old_size = Py_SIZE(self);
1617         if ((n > PY_SSIZE_T_MAX - old_size) ||
1618             ((old_size + n) > PY_SSIZE_T_MAX / itemsize)) {
1619                 PyBuffer_Release(buffer);
1620                 return PyErr_NoMemory();
1621         }
1622         if (array_resize(self, old_size + n) == -1) {
1623             PyBuffer_Release(buffer);
1624             return NULL;
1625         }
1626         memcpy(self->ob_item + old_size * itemsize,
1627             buffer->buf, n * itemsize);
1628     }
1629     PyBuffer_Release(buffer);
1630     Py_RETURN_NONE;
1631 }
1632 
1633 /*[clinic input]
1634 array.array.frombytes
1635 
1636     buffer: Py_buffer
1637     /
1638 
1639 Appends items from the string, interpreting it as an array of machine values, as if it had been read from a file using the fromfile() method).
1640 [clinic start generated code]*/
1641 
1642 static PyObject *
array_array_frombytes_impl(arrayobject * self,Py_buffer * buffer)1643 array_array_frombytes_impl(arrayobject *self, Py_buffer *buffer)
1644 /*[clinic end generated code: output=d9842c8f7510a516 input=2bbf2b53ebfcc988]*/
1645 {
1646     return frombytes(self, buffer);
1647 }
1648 
1649 /*[clinic input]
1650 array.array.tobytes
1651 
1652 Convert the array to an array of machine values and return the bytes representation.
1653 [clinic start generated code]*/
1654 
1655 static PyObject *
array_array_tobytes_impl(arrayobject * self)1656 array_array_tobytes_impl(arrayobject *self)
1657 /*[clinic end generated code: output=87318e4edcdc2bb6 input=90ee495f96de34f5]*/
1658 {
1659     if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
1660         return PyBytes_FromStringAndSize(self->ob_item,
1661                             Py_SIZE(self) * self->ob_descr->itemsize);
1662     } else {
1663         return PyErr_NoMemory();
1664     }
1665 }
1666 
1667 /*[clinic input]
1668 array.array.fromunicode
1669 
1670     ustr: unicode
1671     /
1672 
1673 Extends this array with data from the unicode string ustr.
1674 
1675 The array must be a unicode type array; otherwise a ValueError is raised.
1676 Use array.frombytes(ustr.encode(...)) to append Unicode data to an array of
1677 some other type.
1678 [clinic start generated code]*/
1679 
1680 static PyObject *
array_array_fromunicode_impl(arrayobject * self,PyObject * ustr)1681 array_array_fromunicode_impl(arrayobject *self, PyObject *ustr)
1682 /*[clinic end generated code: output=24359f5e001a7f2b input=025db1fdade7a4ce]*/
1683 {
1684     if (self->ob_descr->typecode != 'u') {
1685         PyErr_SetString(PyExc_ValueError,
1686             "fromunicode() may only be called on "
1687             "unicode type arrays");
1688         return NULL;
1689     }
1690 
1691     Py_ssize_t ustr_length = PyUnicode_AsWideChar(ustr, NULL, 0);
1692     assert(ustr_length > 0);
1693     if (ustr_length > 1) {
1694         ustr_length--; /* trim trailing NUL character */
1695         Py_ssize_t old_size = Py_SIZE(self);
1696         if (array_resize(self, old_size + ustr_length) == -1) {
1697             return NULL;
1698         }
1699 
1700         // must not fail
1701         PyUnicode_AsWideChar(
1702             ustr, ((wchar_t *)self->ob_item) + old_size, ustr_length);
1703     }
1704 
1705     Py_RETURN_NONE;
1706 }
1707 
1708 /*[clinic input]
1709 array.array.tounicode
1710 
1711 Extends this array with data from the unicode string ustr.
1712 
1713 Convert the array to a unicode string.  The array must be a unicode type array;
1714 otherwise a ValueError is raised.  Use array.tobytes().decode() to obtain a
1715 unicode string from an array of some other type.
1716 [clinic start generated code]*/
1717 
1718 static PyObject *
array_array_tounicode_impl(arrayobject * self)1719 array_array_tounicode_impl(arrayobject *self)
1720 /*[clinic end generated code: output=08e442378336e1ef input=127242eebe70b66d]*/
1721 {
1722     if (self->ob_descr->typecode != 'u') {
1723         PyErr_SetString(PyExc_ValueError,
1724              "tounicode() may only be called on unicode type arrays");
1725         return NULL;
1726     }
1727     return PyUnicode_FromWideChar((wchar_t *) self->ob_item, Py_SIZE(self));
1728 }
1729 
1730 /*[clinic input]
1731 array.array.__sizeof__
1732 
1733 Size of the array in memory, in bytes.
1734 [clinic start generated code]*/
1735 
1736 static PyObject *
array_array___sizeof___impl(arrayobject * self)1737 array_array___sizeof___impl(arrayobject *self)
1738 /*[clinic end generated code: output=d8e1c61ebbe3eaed input=805586565bf2b3c6]*/
1739 {
1740     Py_ssize_t res;
1741     res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * self->ob_descr->itemsize;
1742     return PyLong_FromSsize_t(res);
1743 }
1744 
1745 
1746 /*********************** Pickling support ************************/
1747 
1748 static const struct mformatdescr {
1749     size_t size;
1750     int is_signed;
1751     int is_big_endian;
1752 } mformat_descriptors[] = {
1753     {1, 0, 0},                  /* 0: UNSIGNED_INT8 */
1754     {1, 1, 0},                  /* 1: SIGNED_INT8 */
1755     {2, 0, 0},                  /* 2: UNSIGNED_INT16_LE */
1756     {2, 0, 1},                  /* 3: UNSIGNED_INT16_BE */
1757     {2, 1, 0},                  /* 4: SIGNED_INT16_LE */
1758     {2, 1, 1},                  /* 5: SIGNED_INT16_BE */
1759     {4, 0, 0},                  /* 6: UNSIGNED_INT32_LE */
1760     {4, 0, 1},                  /* 7: UNSIGNED_INT32_BE */
1761     {4, 1, 0},                  /* 8: SIGNED_INT32_LE */
1762     {4, 1, 1},                  /* 9: SIGNED_INT32_BE */
1763     {8, 0, 0},                  /* 10: UNSIGNED_INT64_LE */
1764     {8, 0, 1},                  /* 11: UNSIGNED_INT64_BE */
1765     {8, 1, 0},                  /* 12: SIGNED_INT64_LE */
1766     {8, 1, 1},                  /* 13: SIGNED_INT64_BE */
1767     {4, 0, 0},                  /* 14: IEEE_754_FLOAT_LE */
1768     {4, 0, 1},                  /* 15: IEEE_754_FLOAT_BE */
1769     {8, 0, 0},                  /* 16: IEEE_754_DOUBLE_LE */
1770     {8, 0, 1},                  /* 17: IEEE_754_DOUBLE_BE */
1771     {4, 0, 0},                  /* 18: UTF16_LE */
1772     {4, 0, 1},                  /* 19: UTF16_BE */
1773     {8, 0, 0},                  /* 20: UTF32_LE */
1774     {8, 0, 1}                   /* 21: UTF32_BE */
1775 };
1776 
1777 
1778 /*
1779  * Internal: This function is used to find the machine format of a given
1780  * array type code. This returns UNKNOWN_FORMAT when the machine format cannot
1781  * be found.
1782  */
1783 static enum machine_format_code
typecode_to_mformat_code(char typecode)1784 typecode_to_mformat_code(char typecode)
1785 {
1786     const int is_big_endian = PY_BIG_ENDIAN;
1787 
1788     size_t intsize;
1789     int is_signed;
1790 
1791     switch (typecode) {
1792     case 'b':
1793         return SIGNED_INT8;
1794     case 'B':
1795         return UNSIGNED_INT8;
1796 
1797     case 'u':
1798         if (sizeof(Py_UNICODE) == 2) {
1799             return UTF16_LE + is_big_endian;
1800         }
1801         if (sizeof(Py_UNICODE) == 4) {
1802             return UTF32_LE + is_big_endian;
1803         }
1804         return UNKNOWN_FORMAT;
1805 
1806     case 'f':
1807         if (sizeof(float) == 4) {
1808             const float y = 16711938.0;
1809             if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0)
1810                 return IEEE_754_FLOAT_BE;
1811             if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0)
1812                 return IEEE_754_FLOAT_LE;
1813         }
1814         return UNKNOWN_FORMAT;
1815 
1816     case 'd':
1817         if (sizeof(double) == 8) {
1818             const double x = 9006104071832581.0;
1819             if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0)
1820                 return IEEE_754_DOUBLE_BE;
1821             if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
1822                 return IEEE_754_DOUBLE_LE;
1823         }
1824         return UNKNOWN_FORMAT;
1825 
1826     /* Integers */
1827     case 'h':
1828         intsize = sizeof(short);
1829         is_signed = 1;
1830         break;
1831     case 'H':
1832         intsize = sizeof(short);
1833         is_signed = 0;
1834         break;
1835     case 'i':
1836         intsize = sizeof(int);
1837         is_signed = 1;
1838         break;
1839     case 'I':
1840         intsize = sizeof(int);
1841         is_signed = 0;
1842         break;
1843     case 'l':
1844         intsize = sizeof(long);
1845         is_signed = 1;
1846         break;
1847     case 'L':
1848         intsize = sizeof(long);
1849         is_signed = 0;
1850         break;
1851     case 'q':
1852         intsize = sizeof(long long);
1853         is_signed = 1;
1854         break;
1855     case 'Q':
1856         intsize = sizeof(long long);
1857         is_signed = 0;
1858         break;
1859     default:
1860         return UNKNOWN_FORMAT;
1861     }
1862     switch (intsize) {
1863     case 2:
1864         return UNSIGNED_INT16_LE + is_big_endian + (2 * is_signed);
1865     case 4:
1866         return UNSIGNED_INT32_LE + is_big_endian + (2 * is_signed);
1867     case 8:
1868         return UNSIGNED_INT64_LE + is_big_endian + (2 * is_signed);
1869     default:
1870         return UNKNOWN_FORMAT;
1871     }
1872 }
1873 
1874 /* Forward declaration. */
1875 static PyObject *array_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
1876 
1877 /*
1878  * Internal: This function wraps the array constructor--i.e., array_new()--to
1879  * allow the creation of array objects from C code without having to deal
1880  * directly the tuple argument of array_new(). The typecode argument is a
1881  * Unicode character value, like 'i' or 'f' for example, representing an array
1882  * type code. The items argument is a bytes or a list object from which
1883  * contains the initial value of the array.
1884  *
1885  * On success, this functions returns the array object created. Otherwise,
1886  * NULL is returned to indicate a failure.
1887  */
1888 static PyObject *
make_array(PyTypeObject * arraytype,char typecode,PyObject * items)1889 make_array(PyTypeObject *arraytype, char typecode, PyObject *items)
1890 {
1891     PyObject *new_args;
1892     PyObject *array_obj;
1893     PyObject *typecode_obj;
1894 
1895     assert(arraytype != NULL);
1896     assert(items != NULL);
1897 
1898     typecode_obj = PyUnicode_FromOrdinal(typecode);
1899     if (typecode_obj == NULL)
1900         return NULL;
1901 
1902     new_args = PyTuple_New(2);
1903     if (new_args == NULL) {
1904         Py_DECREF(typecode_obj);
1905         return NULL;
1906     }
1907     Py_INCREF(items);
1908     PyTuple_SET_ITEM(new_args, 0, typecode_obj);
1909     PyTuple_SET_ITEM(new_args, 1, items);
1910 
1911     array_obj = array_new(arraytype, new_args, NULL);
1912     Py_DECREF(new_args);
1913     if (array_obj == NULL)
1914         return NULL;
1915 
1916     return array_obj;
1917 }
1918 
1919 /*
1920  * This functions is a special constructor used when unpickling an array. It
1921  * provides a portable way to rebuild an array from its memory representation.
1922  */
1923 /*[clinic input]
1924 array._array_reconstructor
1925 
1926     arraytype: object(type="PyTypeObject *")
1927     typecode: int(accept={str})
1928     mformat_code: int(type="enum machine_format_code")
1929     items: object
1930     /
1931 
1932 Internal. Used for pickling support.
1933 [clinic start generated code]*/
1934 
1935 static PyObject *
array__array_reconstructor_impl(PyObject * module,PyTypeObject * arraytype,int typecode,enum machine_format_code mformat_code,PyObject * items)1936 array__array_reconstructor_impl(PyObject *module, PyTypeObject *arraytype,
1937                                 int typecode,
1938                                 enum machine_format_code mformat_code,
1939                                 PyObject *items)
1940 /*[clinic end generated code: output=e05263141ba28365 input=2464dc8f4c7736b5]*/
1941 {
1942     PyObject *converted_items;
1943     PyObject *result;
1944     const struct arraydescr *descr;
1945 
1946     if (!PyType_Check(arraytype)) {
1947         PyErr_Format(PyExc_TypeError,
1948             "first argument must be a type object, not %.200s",
1949             Py_TYPE(arraytype)->tp_name);
1950         return NULL;
1951     }
1952     if (!PyType_IsSubtype(arraytype, &Arraytype)) {
1953         PyErr_Format(PyExc_TypeError,
1954             "%.200s is not a subtype of %.200s",
1955             arraytype->tp_name, Arraytype.tp_name);
1956         return NULL;
1957     }
1958     for (descr = descriptors; descr->typecode != '\0'; descr++) {
1959         if ((int)descr->typecode == typecode)
1960             break;
1961     }
1962     if (descr->typecode == '\0') {
1963         PyErr_SetString(PyExc_ValueError,
1964                         "second argument must be a valid type code");
1965         return NULL;
1966     }
1967     if (mformat_code < MACHINE_FORMAT_CODE_MIN ||
1968         mformat_code > MACHINE_FORMAT_CODE_MAX) {
1969         PyErr_SetString(PyExc_ValueError,
1970             "third argument must be a valid machine format code.");
1971         return NULL;
1972     }
1973     if (!PyBytes_Check(items)) {
1974         PyErr_Format(PyExc_TypeError,
1975             "fourth argument should be bytes, not %.200s",
1976             Py_TYPE(items)->tp_name);
1977         return NULL;
1978     }
1979 
1980     /* Fast path: No decoding has to be done. */
1981     if (mformat_code == typecode_to_mformat_code((char)typecode) ||
1982         mformat_code == UNKNOWN_FORMAT) {
1983         return make_array(arraytype, (char)typecode, items);
1984     }
1985 
1986     /* Slow path: Decode the byte string according to the given machine
1987      * format code. This occurs when the computer unpickling the array
1988      * object is architecturally different from the one that pickled the
1989      * array.
1990      */
1991     if (Py_SIZE(items) % mformat_descriptors[mformat_code].size != 0) {
1992         PyErr_SetString(PyExc_ValueError,
1993                         "string length not a multiple of item size");
1994         return NULL;
1995     }
1996     switch (mformat_code) {
1997     case IEEE_754_FLOAT_LE:
1998     case IEEE_754_FLOAT_BE: {
1999         Py_ssize_t i;
2000         int le = (mformat_code == IEEE_754_FLOAT_LE) ? 1 : 0;
2001         Py_ssize_t itemcount = Py_SIZE(items) / 4;
2002         const unsigned char *memstr =
2003             (unsigned char *)PyBytes_AS_STRING(items);
2004 
2005         converted_items = PyList_New(itemcount);
2006         if (converted_items == NULL)
2007             return NULL;
2008         for (i = 0; i < itemcount; i++) {
2009             PyObject *pyfloat = PyFloat_FromDouble(
2010                 _PyFloat_Unpack4(&memstr[i * 4], le));
2011             if (pyfloat == NULL) {
2012                 Py_DECREF(converted_items);
2013                 return NULL;
2014             }
2015             PyList_SET_ITEM(converted_items, i, pyfloat);
2016         }
2017         break;
2018     }
2019     case IEEE_754_DOUBLE_LE:
2020     case IEEE_754_DOUBLE_BE: {
2021         Py_ssize_t i;
2022         int le = (mformat_code == IEEE_754_DOUBLE_LE) ? 1 : 0;
2023         Py_ssize_t itemcount = Py_SIZE(items) / 8;
2024         const unsigned char *memstr =
2025             (unsigned char *)PyBytes_AS_STRING(items);
2026 
2027         converted_items = PyList_New(itemcount);
2028         if (converted_items == NULL)
2029             return NULL;
2030         for (i = 0; i < itemcount; i++) {
2031             PyObject *pyfloat = PyFloat_FromDouble(
2032                 _PyFloat_Unpack8(&memstr[i * 8], le));
2033             if (pyfloat == NULL) {
2034                 Py_DECREF(converted_items);
2035                 return NULL;
2036             }
2037             PyList_SET_ITEM(converted_items, i, pyfloat);
2038         }
2039         break;
2040     }
2041     case UTF16_LE:
2042     case UTF16_BE: {
2043         int byteorder = (mformat_code == UTF16_LE) ? -1 : 1;
2044         converted_items = PyUnicode_DecodeUTF16(
2045             PyBytes_AS_STRING(items), Py_SIZE(items),
2046             "strict", &byteorder);
2047         if (converted_items == NULL)
2048             return NULL;
2049         break;
2050     }
2051     case UTF32_LE:
2052     case UTF32_BE: {
2053         int byteorder = (mformat_code == UTF32_LE) ? -1 : 1;
2054         converted_items = PyUnicode_DecodeUTF32(
2055             PyBytes_AS_STRING(items), Py_SIZE(items),
2056             "strict", &byteorder);
2057         if (converted_items == NULL)
2058             return NULL;
2059         break;
2060     }
2061 
2062     case UNSIGNED_INT8:
2063     case SIGNED_INT8:
2064     case UNSIGNED_INT16_LE:
2065     case UNSIGNED_INT16_BE:
2066     case SIGNED_INT16_LE:
2067     case SIGNED_INT16_BE:
2068     case UNSIGNED_INT32_LE:
2069     case UNSIGNED_INT32_BE:
2070     case SIGNED_INT32_LE:
2071     case SIGNED_INT32_BE:
2072     case UNSIGNED_INT64_LE:
2073     case UNSIGNED_INT64_BE:
2074     case SIGNED_INT64_LE:
2075     case SIGNED_INT64_BE: {
2076         Py_ssize_t i;
2077         const struct mformatdescr mf_descr =
2078             mformat_descriptors[mformat_code];
2079         Py_ssize_t itemcount = Py_SIZE(items) / mf_descr.size;
2080         const unsigned char *memstr =
2081             (unsigned char *)PyBytes_AS_STRING(items);
2082         const struct arraydescr *descr;
2083 
2084         /* If possible, try to pack array's items using a data type
2085          * that fits better. This may result in an array with narrower
2086          * or wider elements.
2087          *
2088          * For example, if a 32-bit machine pickles an L-code array of
2089          * unsigned longs, then the array will be unpickled by 64-bit
2090          * machine as an I-code array of unsigned ints.
2091          *
2092          * XXX: Is it possible to write a unit test for this?
2093          */
2094         for (descr = descriptors; descr->typecode != '\0'; descr++) {
2095             if (descr->is_integer_type &&
2096                 (size_t)descr->itemsize == mf_descr.size &&
2097                 descr->is_signed == mf_descr.is_signed)
2098                 typecode = descr->typecode;
2099         }
2100 
2101         converted_items = PyList_New(itemcount);
2102         if (converted_items == NULL)
2103             return NULL;
2104         for (i = 0; i < itemcount; i++) {
2105             PyObject *pylong;
2106 
2107             pylong = _PyLong_FromByteArray(
2108                 &memstr[i * mf_descr.size],
2109                 mf_descr.size,
2110                 !mf_descr.is_big_endian,
2111                 mf_descr.is_signed);
2112             if (pylong == NULL) {
2113                 Py_DECREF(converted_items);
2114                 return NULL;
2115             }
2116             PyList_SET_ITEM(converted_items, i, pylong);
2117         }
2118         break;
2119     }
2120     case UNKNOWN_FORMAT:
2121         /* Impossible, but needed to shut up GCC about the unhandled
2122          * enumeration value.
2123          */
2124     default:
2125         PyErr_BadArgument();
2126         return NULL;
2127     }
2128 
2129     result = make_array(arraytype, (char)typecode, converted_items);
2130     Py_DECREF(converted_items);
2131     return result;
2132 }
2133 
2134 /*[clinic input]
2135 array.array.__reduce_ex__
2136 
2137     value: object
2138     /
2139 
2140 Return state information for pickling.
2141 [clinic start generated code]*/
2142 
2143 static PyObject *
array_array___reduce_ex__(arrayobject * self,PyObject * value)2144 array_array___reduce_ex__(arrayobject *self, PyObject *value)
2145 /*[clinic end generated code: output=051e0a6175d0eddb input=c36c3f85de7df6cd]*/
2146 {
2147     PyObject *dict;
2148     PyObject *result;
2149     PyObject *array_str;
2150     int typecode = self->ob_descr->typecode;
2151     int mformat_code;
2152     static PyObject *array_reconstructor = NULL;
2153     long protocol;
2154     _Py_IDENTIFIER(_array_reconstructor);
2155     _Py_IDENTIFIER(__dict__);
2156 
2157     if (array_reconstructor == NULL) {
2158         PyObject *array_module = PyImport_ImportModule("array");
2159         if (array_module == NULL)
2160             return NULL;
2161         array_reconstructor = _PyObject_GetAttrId(
2162             array_module,
2163             &PyId__array_reconstructor);
2164         Py_DECREF(array_module);
2165         if (array_reconstructor == NULL)
2166             return NULL;
2167     }
2168 
2169     if (!PyLong_Check(value)) {
2170         PyErr_SetString(PyExc_TypeError,
2171                         "__reduce_ex__ argument should be an integer");
2172         return NULL;
2173     }
2174     protocol = PyLong_AsLong(value);
2175     if (protocol == -1 && PyErr_Occurred())
2176         return NULL;
2177 
2178     if (_PyObject_LookupAttrId((PyObject *)self, &PyId___dict__, &dict) < 0) {
2179         return NULL;
2180     }
2181     if (dict == NULL) {
2182         dict = Py_None;
2183         Py_INCREF(dict);
2184     }
2185 
2186     mformat_code = typecode_to_mformat_code(typecode);
2187     if (mformat_code == UNKNOWN_FORMAT || protocol < 3) {
2188         /* Convert the array to a list if we got something weird
2189          * (e.g., non-IEEE floats), or we are pickling the array using
2190          * a Python 2.x compatible protocol.
2191          *
2192          * It is necessary to use a list representation for Python 2.x
2193          * compatible pickle protocol, since Python 2's str objects
2194          * are unpickled as unicode by Python 3. Thus it is impossible
2195          * to make arrays unpicklable by Python 3 by using their memory
2196          * representation, unless we resort to ugly hacks such as
2197          * coercing unicode objects to bytes in array_reconstructor.
2198          */
2199         PyObject *list;
2200         list = array_array_tolist_impl(self);
2201         if (list == NULL) {
2202             Py_DECREF(dict);
2203             return NULL;
2204         }
2205         result = Py_BuildValue(
2206             "O(CO)O", Py_TYPE(self), typecode, list, dict);
2207         Py_DECREF(list);
2208         Py_DECREF(dict);
2209         return result;
2210     }
2211 
2212     array_str = array_array_tobytes_impl(self);
2213     if (array_str == NULL) {
2214         Py_DECREF(dict);
2215         return NULL;
2216     }
2217     result = Py_BuildValue(
2218         "O(OCiN)O", array_reconstructor, Py_TYPE(self), typecode,
2219         mformat_code, array_str, dict);
2220     Py_DECREF(dict);
2221     return result;
2222 }
2223 
2224 static PyObject *
array_get_typecode(arrayobject * a,void * closure)2225 array_get_typecode(arrayobject *a, void *closure)
2226 {
2227     char typecode = a->ob_descr->typecode;
2228     return PyUnicode_FromOrdinal(typecode);
2229 }
2230 
2231 static PyObject *
array_get_itemsize(arrayobject * a,void * closure)2232 array_get_itemsize(arrayobject *a, void *closure)
2233 {
2234     return PyLong_FromLong((long)a->ob_descr->itemsize);
2235 }
2236 
2237 static PyGetSetDef array_getsets [] = {
2238     {"typecode", (getter) array_get_typecode, NULL,
2239      "the typecode character used to create the array"},
2240     {"itemsize", (getter) array_get_itemsize, NULL,
2241      "the size, in bytes, of one array item"},
2242     {NULL}
2243 };
2244 
2245 static PyMethodDef array_methods[] = {
2246     ARRAY_ARRAY_APPEND_METHODDEF
2247     ARRAY_ARRAY_BUFFER_INFO_METHODDEF
2248     ARRAY_ARRAY_BYTESWAP_METHODDEF
2249     ARRAY_ARRAY___COPY___METHODDEF
2250     ARRAY_ARRAY_COUNT_METHODDEF
2251     ARRAY_ARRAY___DEEPCOPY___METHODDEF
2252     ARRAY_ARRAY_EXTEND_METHODDEF
2253     ARRAY_ARRAY_FROMFILE_METHODDEF
2254     ARRAY_ARRAY_FROMLIST_METHODDEF
2255     ARRAY_ARRAY_FROMBYTES_METHODDEF
2256     ARRAY_ARRAY_FROMUNICODE_METHODDEF
2257     ARRAY_ARRAY_INDEX_METHODDEF
2258     ARRAY_ARRAY_INSERT_METHODDEF
2259     ARRAY_ARRAY_POP_METHODDEF
2260     ARRAY_ARRAY___REDUCE_EX___METHODDEF
2261     ARRAY_ARRAY_REMOVE_METHODDEF
2262     ARRAY_ARRAY_REVERSE_METHODDEF
2263     ARRAY_ARRAY_TOFILE_METHODDEF
2264     ARRAY_ARRAY_TOLIST_METHODDEF
2265     ARRAY_ARRAY_TOBYTES_METHODDEF
2266     ARRAY_ARRAY_TOUNICODE_METHODDEF
2267     ARRAY_ARRAY___SIZEOF___METHODDEF
2268     {NULL, NULL}  /* sentinel */
2269 };
2270 
2271 static PyObject *
array_repr(arrayobject * a)2272 array_repr(arrayobject *a)
2273 {
2274     char typecode;
2275     PyObject *s, *v = NULL;
2276     Py_ssize_t len;
2277 
2278     len = Py_SIZE(a);
2279     typecode = a->ob_descr->typecode;
2280     if (len == 0) {
2281         return PyUnicode_FromFormat("%s('%c')",
2282                                     _PyType_Name(Py_TYPE(a)), (int)typecode);
2283     }
2284     if (typecode == 'u') {
2285         v = array_array_tounicode_impl(a);
2286     } else {
2287         v = array_array_tolist_impl(a);
2288     }
2289     if (v == NULL)
2290         return NULL;
2291 
2292     s = PyUnicode_FromFormat("%s('%c', %R)",
2293                              _PyType_Name(Py_TYPE(a)), (int)typecode, v);
2294     Py_DECREF(v);
2295     return s;
2296 }
2297 
2298 static PyObject*
array_subscr(arrayobject * self,PyObject * item)2299 array_subscr(arrayobject* self, PyObject* item)
2300 {
2301     if (PyIndex_Check(item)) {
2302         Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2303         if (i==-1 && PyErr_Occurred()) {
2304             return NULL;
2305         }
2306         if (i < 0)
2307             i += Py_SIZE(self);
2308         return array_item(self, i);
2309     }
2310     else if (PySlice_Check(item)) {
2311         Py_ssize_t start, stop, step, slicelength, i;
2312         size_t cur;
2313         PyObject* result;
2314         arrayobject* ar;
2315         int itemsize = self->ob_descr->itemsize;
2316 
2317         if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
2318             return NULL;
2319         }
2320         slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2321                                             step);
2322 
2323         if (slicelength <= 0) {
2324             return newarrayobject(&Arraytype, 0, self->ob_descr);
2325         }
2326         else if (step == 1) {
2327             PyObject *result = newarrayobject(&Arraytype,
2328                                     slicelength, self->ob_descr);
2329             if (result == NULL)
2330                 return NULL;
2331             memcpy(((arrayobject *)result)->ob_item,
2332                    self->ob_item + start * itemsize,
2333                    slicelength * itemsize);
2334             return result;
2335         }
2336         else {
2337             result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
2338             if (!result) return NULL;
2339 
2340             ar = (arrayobject*)result;
2341 
2342             for (cur = start, i = 0; i < slicelength;
2343                  cur += step, i++) {
2344                 memcpy(ar->ob_item + i*itemsize,
2345                        self->ob_item + cur*itemsize,
2346                        itemsize);
2347             }
2348 
2349             return result;
2350         }
2351     }
2352     else {
2353         PyErr_SetString(PyExc_TypeError,
2354                         "array indices must be integers");
2355         return NULL;
2356     }
2357 }
2358 
2359 static int
array_ass_subscr(arrayobject * self,PyObject * item,PyObject * value)2360 array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
2361 {
2362     Py_ssize_t start, stop, step, slicelength, needed;
2363     arrayobject* other;
2364     int itemsize;
2365 
2366     if (PyIndex_Check(item)) {
2367         Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2368 
2369         if (i == -1 && PyErr_Occurred())
2370             return -1;
2371         if (i < 0)
2372             i += Py_SIZE(self);
2373         if (i < 0 || i >= Py_SIZE(self)) {
2374             PyErr_SetString(PyExc_IndexError,
2375                 "array assignment index out of range");
2376             return -1;
2377         }
2378         if (value == NULL) {
2379             /* Fall through to slice assignment */
2380             start = i;
2381             stop = i + 1;
2382             step = 1;
2383             slicelength = 1;
2384         }
2385         else
2386             return (*self->ob_descr->setitem)(self, i, value);
2387     }
2388     else if (PySlice_Check(item)) {
2389         if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
2390             return -1;
2391         }
2392         slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
2393                                             step);
2394     }
2395     else {
2396         PyErr_SetString(PyExc_TypeError,
2397                         "array indices must be integers");
2398         return -1;
2399     }
2400     if (value == NULL) {
2401         other = NULL;
2402         needed = 0;
2403     }
2404     else if (array_Check(value)) {
2405         other = (arrayobject *)value;
2406         needed = Py_SIZE(other);
2407         if (self == other) {
2408             /* Special case "self[i:j] = self" -- copy self first */
2409             int ret;
2410             value = array_slice(other, 0, needed);
2411             if (value == NULL)
2412                 return -1;
2413             ret = array_ass_subscr(self, item, value);
2414             Py_DECREF(value);
2415             return ret;
2416         }
2417         if (other->ob_descr != self->ob_descr) {
2418             PyErr_BadArgument();
2419             return -1;
2420         }
2421     }
2422     else {
2423         PyErr_Format(PyExc_TypeError,
2424          "can only assign array (not \"%.200s\") to array slice",
2425                          Py_TYPE(value)->tp_name);
2426         return -1;
2427     }
2428     itemsize = self->ob_descr->itemsize;
2429     /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
2430     if ((step > 0 && stop < start) ||
2431         (step < 0 && stop > start))
2432         stop = start;
2433 
2434     /* Issue #4509: If the array has exported buffers and the slice
2435        assignment would change the size of the array, fail early to make
2436        sure we don't modify it. */
2437     if ((needed == 0 || slicelength != needed) && self->ob_exports > 0) {
2438         PyErr_SetString(PyExc_BufferError,
2439             "cannot resize an array that is exporting buffers");
2440         return -1;
2441     }
2442 
2443     if (step == 1) {
2444         if (slicelength > needed) {
2445             memmove(self->ob_item + (start + needed) * itemsize,
2446                 self->ob_item + stop * itemsize,
2447                 (Py_SIZE(self) - stop) * itemsize);
2448             if (array_resize(self, Py_SIZE(self) +
2449                 needed - slicelength) < 0)
2450                 return -1;
2451         }
2452         else if (slicelength < needed) {
2453             if (array_resize(self, Py_SIZE(self) +
2454                 needed - slicelength) < 0)
2455                 return -1;
2456             memmove(self->ob_item + (start + needed) * itemsize,
2457                 self->ob_item + stop * itemsize,
2458                 (Py_SIZE(self) - start - needed) * itemsize);
2459         }
2460         if (needed > 0)
2461             memcpy(self->ob_item + start * itemsize,
2462                    other->ob_item, needed * itemsize);
2463         return 0;
2464     }
2465     else if (needed == 0) {
2466         /* Delete slice */
2467         size_t cur;
2468         Py_ssize_t i;
2469 
2470         if (step < 0) {
2471             stop = start + 1;
2472             start = stop + step * (slicelength - 1) - 1;
2473             step = -step;
2474         }
2475         for (cur = start, i = 0; i < slicelength;
2476              cur += step, i++) {
2477             Py_ssize_t lim = step - 1;
2478 
2479             if (cur + step >= (size_t)Py_SIZE(self))
2480                 lim = Py_SIZE(self) - cur - 1;
2481             memmove(self->ob_item + (cur - i) * itemsize,
2482                 self->ob_item + (cur + 1) * itemsize,
2483                 lim * itemsize);
2484         }
2485         cur = start + (size_t)slicelength * step;
2486         if (cur < (size_t)Py_SIZE(self)) {
2487             memmove(self->ob_item + (cur-slicelength) * itemsize,
2488                 self->ob_item + cur * itemsize,
2489                 (Py_SIZE(self) - cur) * itemsize);
2490         }
2491         if (array_resize(self, Py_SIZE(self) - slicelength) < 0)
2492             return -1;
2493         return 0;
2494     }
2495     else {
2496         size_t cur;
2497         Py_ssize_t i;
2498 
2499         if (needed != slicelength) {
2500             PyErr_Format(PyExc_ValueError,
2501                 "attempt to assign array of size %zd "
2502                 "to extended slice of size %zd",
2503                 needed, slicelength);
2504             return -1;
2505         }
2506         for (cur = start, i = 0; i < slicelength;
2507              cur += step, i++) {
2508             memcpy(self->ob_item + cur * itemsize,
2509                    other->ob_item + i * itemsize,
2510                    itemsize);
2511         }
2512         return 0;
2513     }
2514 }
2515 
2516 static PyMappingMethods array_as_mapping = {
2517     (lenfunc)array_length,
2518     (binaryfunc)array_subscr,
2519     (objobjargproc)array_ass_subscr
2520 };
2521 
2522 static const void *emptybuf = "";
2523 
2524 
2525 static int
array_buffer_getbuf(arrayobject * self,Py_buffer * view,int flags)2526 array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
2527 {
2528     if (view == NULL) {
2529         PyErr_SetString(PyExc_BufferError,
2530             "array_buffer_getbuf: view==NULL argument is obsolete");
2531         return -1;
2532     }
2533 
2534     view->buf = (void *)self->ob_item;
2535     view->obj = (PyObject*)self;
2536     Py_INCREF(self);
2537     if (view->buf == NULL)
2538         view->buf = (void *)emptybuf;
2539     view->len = (Py_SIZE(self)) * self->ob_descr->itemsize;
2540     view->readonly = 0;
2541     view->ndim = 1;
2542     view->itemsize = self->ob_descr->itemsize;
2543     view->suboffsets = NULL;
2544     view->shape = NULL;
2545     if ((flags & PyBUF_ND)==PyBUF_ND) {
2546         view->shape = &((Py_SIZE(self)));
2547     }
2548     view->strides = NULL;
2549     if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES)
2550         view->strides = &(view->itemsize);
2551     view->format = NULL;
2552     view->internal = NULL;
2553     if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
2554         view->format = (char *)self->ob_descr->formats;
2555 #ifdef Py_UNICODE_WIDE
2556         if (self->ob_descr->typecode == 'u') {
2557             view->format = "w";
2558         }
2559 #endif
2560     }
2561 
2562     self->ob_exports++;
2563     return 0;
2564 }
2565 
2566 static void
array_buffer_relbuf(arrayobject * self,Py_buffer * view)2567 array_buffer_relbuf(arrayobject *self, Py_buffer *view)
2568 {
2569     self->ob_exports--;
2570 }
2571 
2572 static PySequenceMethods array_as_sequence = {
2573     (lenfunc)array_length,                      /*sq_length*/
2574     (binaryfunc)array_concat,               /*sq_concat*/
2575     (ssizeargfunc)array_repeat,                 /*sq_repeat*/
2576     (ssizeargfunc)array_item,                           /*sq_item*/
2577     0,                                          /*sq_slice*/
2578     (ssizeobjargproc)array_ass_item,                    /*sq_ass_item*/
2579     0,                                          /*sq_ass_slice*/
2580     (objobjproc)array_contains,                 /*sq_contains*/
2581     (binaryfunc)array_inplace_concat,           /*sq_inplace_concat*/
2582     (ssizeargfunc)array_inplace_repeat          /*sq_inplace_repeat*/
2583 };
2584 
2585 static PyBufferProcs array_as_buffer = {
2586     (getbufferproc)array_buffer_getbuf,
2587     (releasebufferproc)array_buffer_relbuf
2588 };
2589 
2590 static PyObject *
array_new(PyTypeObject * type,PyObject * args,PyObject * kwds)2591 array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2592 {
2593     int c;
2594     PyObject *initial = NULL, *it = NULL;
2595     const struct arraydescr *descr;
2596 
2597     if (type == &Arraytype && !_PyArg_NoKeywords("array.array", kwds))
2598         return NULL;
2599 
2600     if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
2601         return NULL;
2602 
2603     if (PySys_Audit("array.__new__", "CO",
2604                     c, initial ? initial : Py_None) < 0) {
2605         return NULL;
2606     }
2607 
2608     if (initial && c != 'u') {
2609         if (PyUnicode_Check(initial)) {
2610             PyErr_Format(PyExc_TypeError, "cannot use a str to initialize "
2611                          "an array with typecode '%c'", c);
2612             return NULL;
2613         }
2614         else if (array_Check(initial) &&
2615                  ((arrayobject*)initial)->ob_descr->typecode == 'u') {
2616             PyErr_Format(PyExc_TypeError, "cannot use a unicode array to "
2617                          "initialize an array with typecode '%c'", c);
2618             return NULL;
2619         }
2620     }
2621 
2622     if (!(initial == NULL || PyList_Check(initial)
2623           || PyByteArray_Check(initial)
2624           || PyBytes_Check(initial)
2625           || PyTuple_Check(initial)
2626           || ((c=='u') && PyUnicode_Check(initial))
2627           || (array_Check(initial)
2628               && c == ((arrayobject*)initial)->ob_descr->typecode))) {
2629         it = PyObject_GetIter(initial);
2630         if (it == NULL)
2631             return NULL;
2632         /* We set initial to NULL so that the subsequent code
2633            will create an empty array of the appropriate type
2634            and afterwards we can use array_iter_extend to populate
2635            the array.
2636         */
2637         initial = NULL;
2638     }
2639     for (descr = descriptors; descr->typecode != '\0'; descr++) {
2640         if (descr->typecode == c) {
2641             PyObject *a;
2642             Py_ssize_t len;
2643 
2644             if (initial == NULL)
2645                 len = 0;
2646             else if (PyList_Check(initial))
2647                 len = PyList_GET_SIZE(initial);
2648             else if (PyTuple_Check(initial) || array_Check(initial))
2649                 len = Py_SIZE(initial);
2650             else
2651                 len = 0;
2652 
2653             a = newarrayobject(type, len, descr);
2654             if (a == NULL)
2655                 return NULL;
2656 
2657             if (len > 0 && !array_Check(initial)) {
2658                 Py_ssize_t i;
2659                 for (i = 0; i < len; i++) {
2660                     PyObject *v =
2661                         PySequence_GetItem(initial, i);
2662                     if (v == NULL) {
2663                         Py_DECREF(a);
2664                         return NULL;
2665                     }
2666                     if (setarrayitem(a, i, v) != 0) {
2667                         Py_DECREF(v);
2668                         Py_DECREF(a);
2669                         return NULL;
2670                     }
2671                     Py_DECREF(v);
2672                 }
2673             }
2674             else if (initial != NULL && (PyByteArray_Check(initial) ||
2675                                PyBytes_Check(initial))) {
2676                 PyObject *v;
2677                 v = array_array_frombytes((arrayobject *)a,
2678                                           initial);
2679                 if (v == NULL) {
2680                     Py_DECREF(a);
2681                     return NULL;
2682                 }
2683                 Py_DECREF(v);
2684             }
2685             else if (initial != NULL && PyUnicode_Check(initial))  {
2686                 Py_ssize_t n;
2687                 wchar_t *ustr = PyUnicode_AsWideCharString(initial, &n);
2688                 if (ustr == NULL) {
2689                     Py_DECREF(a);
2690                     return NULL;
2691                 }
2692 
2693                 if (n > 0) {
2694                     arrayobject *self = (arrayobject *)a;
2695                     // self->ob_item may be NULL but it is safe.
2696                     PyMem_Free(self->ob_item);
2697                     self->ob_item = (char *)ustr;
2698                     Py_SET_SIZE(self, n);
2699                     self->allocated = n;
2700                 }
2701             }
2702             else if (initial != NULL && array_Check(initial) && len > 0) {
2703                 arrayobject *self = (arrayobject *)a;
2704                 arrayobject *other = (arrayobject *)initial;
2705                 memcpy(self->ob_item, other->ob_item, len * other->ob_descr->itemsize);
2706             }
2707             if (it != NULL) {
2708                 if (array_iter_extend((arrayobject *)a, it) == -1) {
2709                     Py_DECREF(it);
2710                     Py_DECREF(a);
2711                     return NULL;
2712                 }
2713                 Py_DECREF(it);
2714             }
2715             return a;
2716         }
2717     }
2718     PyErr_SetString(PyExc_ValueError,
2719         "bad typecode (must be b, B, u, h, H, i, I, l, L, q, Q, f or d)");
2720     return NULL;
2721 }
2722 
2723 
2724 PyDoc_STRVAR(module_doc,
2725 "This module defines an object type which can efficiently represent\n\
2726 an array of basic values: characters, integers, floating point\n\
2727 numbers.  Arrays are sequence types and behave very much like lists,\n\
2728 except that the type of objects stored in them is constrained.\n");
2729 
2730 PyDoc_STRVAR(arraytype_doc,
2731 "array(typecode [, initializer]) -> array\n\
2732 \n\
2733 Return a new array whose items are restricted by typecode, and\n\
2734 initialized from the optional initializer value, which must be a list,\n\
2735 string or iterable over elements of the appropriate type.\n\
2736 \n\
2737 Arrays represent basic values and behave very much like lists, except\n\
2738 the type of objects stored in them is constrained. The type is specified\n\
2739 at object creation time by using a type code, which is a single character.\n\
2740 The following type codes are defined:\n\
2741 \n\
2742     Type code   C Type             Minimum size in bytes\n\
2743     'b'         signed integer     1\n\
2744     'B'         unsigned integer   1\n\
2745     'u'         Unicode character  2 (see note)\n\
2746     'h'         signed integer     2\n\
2747     'H'         unsigned integer   2\n\
2748     'i'         signed integer     2\n\
2749     'I'         unsigned integer   2\n\
2750     'l'         signed integer     4\n\
2751     'L'         unsigned integer   4\n\
2752     'q'         signed integer     8 (see note)\n\
2753     'Q'         unsigned integer   8 (see note)\n\
2754     'f'         floating point     4\n\
2755     'd'         floating point     8\n\
2756 \n\
2757 NOTE: The 'u' typecode corresponds to Python's unicode character. On\n\
2758 narrow builds this is 2-bytes on wide builds this is 4-bytes.\n\
2759 \n\
2760 NOTE: The 'q' and 'Q' type codes are only available if the platform\n\
2761 C compiler used to build Python supports 'long long', or, on Windows,\n\
2762 '__int64'.\n\
2763 \n\
2764 Methods:\n\
2765 \n\
2766 append() -- append a new item to the end of the array\n\
2767 buffer_info() -- return information giving the current memory info\n\
2768 byteswap() -- byteswap all the items of the array\n\
2769 count() -- return number of occurrences of an object\n\
2770 extend() -- extend array by appending multiple elements from an iterable\n\
2771 fromfile() -- read items from a file object\n\
2772 fromlist() -- append items from the list\n\
2773 frombytes() -- append items from the string\n\
2774 index() -- return index of first occurrence of an object\n\
2775 insert() -- insert a new item into the array at a provided position\n\
2776 pop() -- remove and return item (default last)\n\
2777 remove() -- remove first occurrence of an object\n\
2778 reverse() -- reverse the order of the items in the array\n\
2779 tofile() -- write all items to a file object\n\
2780 tolist() -- return the array converted to an ordinary list\n\
2781 tobytes() -- return the array converted to a string\n\
2782 \n\
2783 Attributes:\n\
2784 \n\
2785 typecode -- the typecode character used to create the array\n\
2786 itemsize -- the length in bytes of one array item\n\
2787 ");
2788 
2789 static PyObject *array_iter(arrayobject *ao);
2790 
2791 static PyTypeObject Arraytype = {
2792     PyVarObject_HEAD_INIT(NULL, 0)
2793     "array.array",
2794     sizeof(arrayobject),
2795     0,
2796     (destructor)array_dealloc,                  /* tp_dealloc */
2797     0,                                          /* tp_vectorcall_offset */
2798     0,                                          /* tp_getattr */
2799     0,                                          /* tp_setattr */
2800     0,                                          /* tp_as_async */
2801     (reprfunc)array_repr,                       /* tp_repr */
2802     0,                                          /* tp_as_number*/
2803     &array_as_sequence,                         /* tp_as_sequence*/
2804     &array_as_mapping,                          /* tp_as_mapping*/
2805     0,                                          /* tp_hash */
2806     0,                                          /* tp_call */
2807     0,                                          /* tp_str */
2808     PyObject_GenericGetAttr,                    /* tp_getattro */
2809     0,                                          /* tp_setattro */
2810     &array_as_buffer,                           /* tp_as_buffer*/
2811     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2812     arraytype_doc,                              /* tp_doc */
2813     0,                                          /* tp_traverse */
2814     0,                                          /* tp_clear */
2815     array_richcompare,                          /* tp_richcompare */
2816     offsetof(arrayobject, weakreflist),         /* tp_weaklistoffset */
2817     (getiterfunc)array_iter,                    /* tp_iter */
2818     0,                                          /* tp_iternext */
2819     array_methods,                              /* tp_methods */
2820     0,                                          /* tp_members */
2821     array_getsets,                              /* tp_getset */
2822     0,                                          /* tp_base */
2823     0,                                          /* tp_dict */
2824     0,                                          /* tp_descr_get */
2825     0,                                          /* tp_descr_set */
2826     0,                                          /* tp_dictoffset */
2827     0,                                          /* tp_init */
2828     PyType_GenericAlloc,                        /* tp_alloc */
2829     array_new,                                  /* tp_new */
2830     PyObject_Del,                               /* tp_free */
2831 };
2832 
2833 
2834 /*********************** Array Iterator **************************/
2835 
2836 /*[clinic input]
2837 class array.arrayiterator "arrayiterobject *" "&PyArrayIter_Type"
2838 [clinic start generated code]*/
2839 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=5aefd2d74d8c8e30]*/
2840 
2841 static PyObject *
array_iter(arrayobject * ao)2842 array_iter(arrayobject *ao)
2843 {
2844     arrayiterobject *it;
2845 
2846     if (!array_Check(ao)) {
2847         PyErr_BadInternalCall();
2848         return NULL;
2849     }
2850 
2851     it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
2852     if (it == NULL)
2853         return NULL;
2854 
2855     Py_INCREF(ao);
2856     it->ao = ao;
2857     it->index = 0;
2858     it->getitem = ao->ob_descr->getitem;
2859     PyObject_GC_Track(it);
2860     return (PyObject *)it;
2861 }
2862 
2863 static PyObject *
arrayiter_next(arrayiterobject * it)2864 arrayiter_next(arrayiterobject *it)
2865 {
2866     arrayobject *ao;
2867 
2868     assert(it != NULL);
2869     assert(PyArrayIter_Check(it));
2870     ao = it->ao;
2871     if (ao == NULL) {
2872         return NULL;
2873     }
2874     assert(array_Check(ao));
2875     if (it->index < Py_SIZE(ao)) {
2876         return (*it->getitem)(ao, it->index++);
2877     }
2878     it->ao = NULL;
2879     Py_DECREF(ao);
2880     return NULL;
2881 }
2882 
2883 static void
arrayiter_dealloc(arrayiterobject * it)2884 arrayiter_dealloc(arrayiterobject *it)
2885 {
2886     PyObject_GC_UnTrack(it);
2887     Py_XDECREF(it->ao);
2888     PyObject_GC_Del(it);
2889 }
2890 
2891 static int
arrayiter_traverse(arrayiterobject * it,visitproc visit,void * arg)2892 arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
2893 {
2894     Py_VISIT(it->ao);
2895     return 0;
2896 }
2897 
2898 /*[clinic input]
2899 array.arrayiterator.__reduce__
2900 
2901 Return state information for pickling.
2902 [clinic start generated code]*/
2903 
2904 static PyObject *
array_arrayiterator___reduce___impl(arrayiterobject * self)2905 array_arrayiterator___reduce___impl(arrayiterobject *self)
2906 /*[clinic end generated code: output=7898a52e8e66e016 input=a062ea1e9951417a]*/
2907 {
2908     _Py_IDENTIFIER(iter);
2909     PyObject *func = _PyEval_GetBuiltinId(&PyId_iter);
2910     if (self->ao == NULL) {
2911         return Py_BuildValue("N(())", func);
2912     }
2913     return Py_BuildValue("N(O)n", func, self->ao, self->index);
2914 }
2915 
2916 /*[clinic input]
2917 array.arrayiterator.__setstate__
2918 
2919     state: object
2920     /
2921 
2922 Set state information for unpickling.
2923 [clinic start generated code]*/
2924 
2925 static PyObject *
array_arrayiterator___setstate__(arrayiterobject * self,PyObject * state)2926 array_arrayiterator___setstate__(arrayiterobject *self, PyObject *state)
2927 /*[clinic end generated code: output=397da9904e443cbe input=f47d5ceda19e787b]*/
2928 {
2929     Py_ssize_t index = PyLong_AsSsize_t(state);
2930     if (index == -1 && PyErr_Occurred())
2931         return NULL;
2932     if (index < 0)
2933         index = 0;
2934     else if (index > Py_SIZE(self->ao))
2935         index = Py_SIZE(self->ao); /* iterator exhausted */
2936     self->index = index;
2937     Py_RETURN_NONE;
2938 }
2939 
2940 static PyMethodDef arrayiter_methods[] = {
2941     ARRAY_ARRAYITERATOR___REDUCE___METHODDEF
2942     ARRAY_ARRAYITERATOR___SETSTATE___METHODDEF
2943     {NULL, NULL} /* sentinel */
2944 };
2945 
2946 static PyTypeObject PyArrayIter_Type = {
2947     PyVarObject_HEAD_INIT(NULL, 0)
2948     "arrayiterator",                        /* tp_name */
2949     sizeof(arrayiterobject),                /* tp_basicsize */
2950     0,                                      /* tp_itemsize */
2951     /* methods */
2952     (destructor)arrayiter_dealloc,              /* tp_dealloc */
2953     0,                                      /* tp_vectorcall_offset */
2954     0,                                      /* tp_getattr */
2955     0,                                      /* tp_setattr */
2956     0,                                      /* tp_as_async */
2957     0,                                      /* tp_repr */
2958     0,                                      /* tp_as_number */
2959     0,                                      /* tp_as_sequence */
2960     0,                                      /* tp_as_mapping */
2961     0,                                      /* tp_hash */
2962     0,                                      /* tp_call */
2963     0,                                      /* tp_str */
2964     PyObject_GenericGetAttr,                /* tp_getattro */
2965     0,                                      /* tp_setattro */
2966     0,                                      /* tp_as_buffer */
2967     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2968     0,                                      /* tp_doc */
2969     (traverseproc)arrayiter_traverse,           /* tp_traverse */
2970     0,                                          /* tp_clear */
2971     0,                                      /* tp_richcompare */
2972     0,                                      /* tp_weaklistoffset */
2973     PyObject_SelfIter,                          /* tp_iter */
2974     (iternextfunc)arrayiter_next,               /* tp_iternext */
2975     arrayiter_methods,                      /* tp_methods */
2976 };
2977 
2978 
2979 /*********************** Install Module **************************/
2980 
2981 /* No functions in array module. */
2982 static PyMethodDef a_methods[] = {
2983     ARRAY__ARRAY_RECONSTRUCTOR_METHODDEF
2984     {NULL, NULL, 0, NULL}        /* Sentinel */
2985 };
2986 
2987 static int
array_modexec(PyObject * m)2988 array_modexec(PyObject *m)
2989 {
2990     char buffer[Py_ARRAY_LENGTH(descriptors)], *p;
2991     PyObject *typecodes;
2992     const struct arraydescr *descr;
2993 
2994     if (PyType_Ready(&Arraytype) < 0)
2995         return -1;
2996     Py_SET_TYPE(&PyArrayIter_Type, &PyType_Type);
2997 
2998     Py_INCREF((PyObject *)&Arraytype);
2999     if (PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype) < 0) {
3000         Py_DECREF((PyObject *)&Arraytype);
3001         return -1;
3002     }
3003     Py_INCREF((PyObject *)&Arraytype);
3004     if (PyModule_AddObject(m, "array", (PyObject *)&Arraytype) < 0) {
3005         Py_DECREF((PyObject *)&Arraytype);
3006         return -1;
3007     }
3008 
3009     p = buffer;
3010     for (descr = descriptors; descr->typecode != '\0'; descr++) {
3011         *p++ = (char)descr->typecode;
3012     }
3013     typecodes = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
3014     if (PyModule_AddObject(m, "typecodes", typecodes) < 0) {
3015         Py_XDECREF(typecodes);
3016         return -1;
3017     }
3018 
3019     return 0;
3020 }
3021 
3022 static PyModuleDef_Slot arrayslots[] = {
3023     {Py_mod_exec, array_modexec},
3024     {0, NULL}
3025 };
3026 
3027 
3028 static struct PyModuleDef arraymodule = {
3029     PyModuleDef_HEAD_INIT,
3030     "array",
3031     module_doc,
3032     0,
3033     a_methods,
3034     arrayslots,
3035     NULL,
3036     NULL,
3037     NULL
3038 };
3039 
3040 
3041 PyMODINIT_FUNC
PyInit_array(void)3042 PyInit_array(void)
3043 {
3044     return PyModuleDef_Init(&arraymodule);
3045 }
3046