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