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