]>
Commit | Line | Data |
---|---|---|
7eb75bcc DM |
1 | /* Array object implementation */\r |
2 | \r | |
3 | /* An array is a uniform list -- all items have the same type.\r | |
4 | The item type is restricted to simple C types like int or float */\r | |
5 | \r | |
6 | #define PY_SSIZE_T_CLEAN\r | |
7 | #include "Python.h"\r | |
8 | #include "structmember.h"\r | |
9 | \r | |
10 | #ifdef STDC_HEADERS\r | |
11 | #include <stddef.h>\r | |
12 | #else /* !STDC_HEADERS */\r | |
13 | #ifdef HAVE_SYS_TYPES_H\r | |
14 | #include <sys/types.h> /* For size_t */\r | |
15 | #endif /* HAVE_SYS_TYPES_H */\r | |
16 | #endif /* !STDC_HEADERS */\r | |
17 | \r | |
18 | struct arrayobject; /* Forward */\r | |
19 | \r | |
20 | /* All possible arraydescr values are defined in the vector "descriptors"\r | |
21 | * below. That's defined later because the appropriate get and set\r | |
22 | * functions aren't visible yet.\r | |
23 | */\r | |
24 | struct arraydescr {\r | |
25 | int typecode;\r | |
26 | int itemsize;\r | |
27 | PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);\r | |
28 | int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *);\r | |
29 | };\r | |
30 | \r | |
31 | typedef struct arrayobject {\r | |
32 | PyObject_VAR_HEAD\r | |
33 | char *ob_item;\r | |
34 | Py_ssize_t allocated;\r | |
35 | struct arraydescr *ob_descr;\r | |
36 | PyObject *weakreflist; /* List of weak references */\r | |
37 | } arrayobject;\r | |
38 | \r | |
39 | static PyTypeObject Arraytype;\r | |
40 | \r | |
41 | #define array_Check(op) PyObject_TypeCheck(op, &Arraytype)\r | |
42 | #define array_CheckExact(op) (Py_TYPE(op) == &Arraytype)\r | |
43 | \r | |
44 | static int\r | |
45 | array_resize(arrayobject *self, Py_ssize_t newsize)\r | |
46 | {\r | |
47 | char *items;\r | |
48 | size_t _new_size;\r | |
49 | \r | |
50 | /* Bypass realloc() when a previous overallocation is large enough\r | |
51 | to accommodate the newsize. If the newsize is 16 smaller than the\r | |
52 | current size, then proceed with the realloc() to shrink the list.\r | |
53 | */\r | |
54 | \r | |
55 | if (self->allocated >= newsize &&\r | |
56 | Py_SIZE(self) < newsize + 16 &&\r | |
57 | self->ob_item != NULL) {\r | |
58 | Py_SIZE(self) = newsize;\r | |
59 | return 0;\r | |
60 | }\r | |
61 | \r | |
62 | /* This over-allocates proportional to the array size, making room\r | |
63 | * for additional growth. The over-allocation is mild, but is\r | |
64 | * enough to give linear-time amortized behavior over a long\r | |
65 | * sequence of appends() in the presence of a poorly-performing\r | |
66 | * system realloc().\r | |
67 | * The growth pattern is: 0, 4, 8, 16, 25, 34, 46, 56, 67, 79, ...\r | |
68 | * Note, the pattern starts out the same as for lists but then\r | |
69 | * grows at a smaller rate so that larger arrays only overallocate\r | |
70 | * by about 1/16th -- this is done because arrays are presumed to be more\r | |
71 | * memory critical.\r | |
72 | */\r | |
73 | \r | |
74 | _new_size = (newsize >> 4) + (Py_SIZE(self) < 8 ? 3 : 7) + newsize;\r | |
75 | items = self->ob_item;\r | |
76 | /* XXX The following multiplication and division does not optimize away\r | |
77 | like it does for lists since the size is not known at compile time */\r | |
78 | if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize))\r | |
79 | PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize));\r | |
80 | else\r | |
81 | items = NULL;\r | |
82 | if (items == NULL) {\r | |
83 | PyErr_NoMemory();\r | |
84 | return -1;\r | |
85 | }\r | |
86 | self->ob_item = items;\r | |
87 | Py_SIZE(self) = newsize;\r | |
88 | self->allocated = _new_size;\r | |
89 | return 0;\r | |
90 | }\r | |
91 | \r | |
92 | /****************************************************************************\r | |
93 | Get and Set functions for each type.\r | |
94 | A Get function takes an arrayobject* and an integer index, returning the\r | |
95 | array value at that index wrapped in an appropriate PyObject*.\r | |
96 | A Set function takes an arrayobject, integer index, and PyObject*; sets\r | |
97 | the array value at that index to the raw C data extracted from the PyObject*,\r | |
98 | and returns 0 if successful, else nonzero on failure (PyObject* not of an\r | |
99 | appropriate type or value).\r | |
100 | Note that the basic Get and Set functions do NOT check that the index is\r | |
101 | in bounds; that's the responsibility of the caller.\r | |
102 | ****************************************************************************/\r | |
103 | \r | |
104 | static PyObject *\r | |
105 | c_getitem(arrayobject *ap, Py_ssize_t i)\r | |
106 | {\r | |
107 | return PyString_FromStringAndSize(&((char *)ap->ob_item)[i], 1);\r | |
108 | }\r | |
109 | \r | |
110 | static int\r | |
111 | c_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)\r | |
112 | {\r | |
113 | char x;\r | |
114 | if (!PyArg_Parse(v, "c;array item must be char", &x))\r | |
115 | return -1;\r | |
116 | if (i >= 0)\r | |
117 | ((char *)ap->ob_item)[i] = x;\r | |
118 | return 0;\r | |
119 | }\r | |
120 | \r | |
121 | static PyObject *\r | |
122 | b_getitem(arrayobject *ap, Py_ssize_t i)\r | |
123 | {\r | |
124 | long x = ((char *)ap->ob_item)[i];\r | |
125 | if (x >= 128)\r | |
126 | x -= 256;\r | |
127 | return PyInt_FromLong(x);\r | |
128 | }\r | |
129 | \r | |
130 | static int\r | |
131 | b_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)\r | |
132 | {\r | |
133 | short x;\r | |
134 | /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore\r | |
135 | must use the next size up that is signed ('h') and manually do\r | |
136 | the overflow checking */\r | |
137 | if (!PyArg_Parse(v, "h;array item must be integer", &x))\r | |
138 | return -1;\r | |
139 | else if (x < -128) {\r | |
140 | PyErr_SetString(PyExc_OverflowError,\r | |
141 | "signed char is less than minimum");\r | |
142 | return -1;\r | |
143 | }\r | |
144 | else if (x > 127) {\r | |
145 | PyErr_SetString(PyExc_OverflowError,\r | |
146 | "signed char is greater than maximum");\r | |
147 | return -1;\r | |
148 | }\r | |
149 | if (i >= 0)\r | |
150 | ((char *)ap->ob_item)[i] = (char)x;\r | |
151 | return 0;\r | |
152 | }\r | |
153 | \r | |
154 | static PyObject *\r | |
155 | BB_getitem(arrayobject *ap, Py_ssize_t i)\r | |
156 | {\r | |
157 | long x = ((unsigned char *)ap->ob_item)[i];\r | |
158 | return PyInt_FromLong(x);\r | |
159 | }\r | |
160 | \r | |
161 | static int\r | |
162 | BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)\r | |
163 | {\r | |
164 | unsigned char x;\r | |
165 | /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */\r | |
166 | if (!PyArg_Parse(v, "b;array item must be integer", &x))\r | |
167 | return -1;\r | |
168 | if (i >= 0)\r | |
169 | ((char *)ap->ob_item)[i] = x;\r | |
170 | return 0;\r | |
171 | }\r | |
172 | \r | |
173 | #ifdef Py_USING_UNICODE\r | |
174 | static PyObject *\r | |
175 | u_getitem(arrayobject *ap, Py_ssize_t i)\r | |
176 | {\r | |
177 | return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1);\r | |
178 | }\r | |
179 | \r | |
180 | static int\r | |
181 | u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)\r | |
182 | {\r | |
183 | Py_UNICODE *p;\r | |
184 | Py_ssize_t len;\r | |
185 | \r | |
186 | if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))\r | |
187 | return -1;\r | |
188 | if (len != 1) {\r | |
189 | PyErr_SetString(PyExc_TypeError,\r | |
190 | "array item must be unicode character");\r | |
191 | return -1;\r | |
192 | }\r | |
193 | if (i >= 0)\r | |
194 | ((Py_UNICODE *)ap->ob_item)[i] = p[0];\r | |
195 | return 0;\r | |
196 | }\r | |
197 | #endif\r | |
198 | \r | |
199 | static PyObject *\r | |
200 | h_getitem(arrayobject *ap, Py_ssize_t i)\r | |
201 | {\r | |
202 | return PyInt_FromLong((long) ((short *)ap->ob_item)[i]);\r | |
203 | }\r | |
204 | \r | |
205 | static int\r | |
206 | h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)\r | |
207 | {\r | |
208 | short x;\r | |
209 | /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */\r | |
210 | if (!PyArg_Parse(v, "h;array item must be integer", &x))\r | |
211 | return -1;\r | |
212 | if (i >= 0)\r | |
213 | ((short *)ap->ob_item)[i] = x;\r | |
214 | return 0;\r | |
215 | }\r | |
216 | \r | |
217 | static PyObject *\r | |
218 | HH_getitem(arrayobject *ap, Py_ssize_t i)\r | |
219 | {\r | |
220 | return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[i]);\r | |
221 | }\r | |
222 | \r | |
223 | static int\r | |
224 | HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)\r | |
225 | {\r | |
226 | int x;\r | |
227 | /* PyArg_Parse's 'h' formatter is for a signed short, therefore\r | |
228 | must use the next size up and manually do the overflow checking */\r | |
229 | if (!PyArg_Parse(v, "i;array item must be integer", &x))\r | |
230 | return -1;\r | |
231 | else if (x < 0) {\r | |
232 | PyErr_SetString(PyExc_OverflowError,\r | |
233 | "unsigned short is less than minimum");\r | |
234 | return -1;\r | |
235 | }\r | |
236 | else if (x > USHRT_MAX) {\r | |
237 | PyErr_SetString(PyExc_OverflowError,\r | |
238 | "unsigned short is greater than maximum");\r | |
239 | return -1;\r | |
240 | }\r | |
241 | if (i >= 0)\r | |
242 | ((short *)ap->ob_item)[i] = (short)x;\r | |
243 | return 0;\r | |
244 | }\r | |
245 | \r | |
246 | static PyObject *\r | |
247 | i_getitem(arrayobject *ap, Py_ssize_t i)\r | |
248 | {\r | |
249 | return PyInt_FromLong((long) ((int *)ap->ob_item)[i]);\r | |
250 | }\r | |
251 | \r | |
252 | static int\r | |
253 | i_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)\r | |
254 | {\r | |
255 | int x;\r | |
256 | /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */\r | |
257 | if (!PyArg_Parse(v, "i;array item must be integer", &x))\r | |
258 | return -1;\r | |
259 | if (i >= 0)\r | |
260 | ((int *)ap->ob_item)[i] = x;\r | |
261 | return 0;\r | |
262 | }\r | |
263 | \r | |
264 | static PyObject *\r | |
265 | II_getitem(arrayobject *ap, Py_ssize_t i)\r | |
266 | {\r | |
267 | return PyLong_FromUnsignedLong(\r | |
268 | (unsigned long) ((unsigned int *)ap->ob_item)[i]);\r | |
269 | }\r | |
270 | \r | |
271 | static int\r | |
272 | II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)\r | |
273 | {\r | |
274 | unsigned long x;\r | |
275 | if (PyLong_Check(v)) {\r | |
276 | x = PyLong_AsUnsignedLong(v);\r | |
277 | if (x == (unsigned long) -1 && PyErr_Occurred())\r | |
278 | return -1;\r | |
279 | }\r | |
280 | else {\r | |
281 | long y;\r | |
282 | if (!PyArg_Parse(v, "l;array item must be integer", &y))\r | |
283 | return -1;\r | |
284 | if (y < 0) {\r | |
285 | PyErr_SetString(PyExc_OverflowError,\r | |
286 | "unsigned int is less than minimum");\r | |
287 | return -1;\r | |
288 | }\r | |
289 | x = (unsigned long)y;\r | |
290 | \r | |
291 | }\r | |
292 | if (x > UINT_MAX) {\r | |
293 | PyErr_SetString(PyExc_OverflowError,\r | |
294 | "unsigned int is greater than maximum");\r | |
295 | return -1;\r | |
296 | }\r | |
297 | \r | |
298 | if (i >= 0)\r | |
299 | ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;\r | |
300 | return 0;\r | |
301 | }\r | |
302 | \r | |
303 | static PyObject *\r | |
304 | l_getitem(arrayobject *ap, Py_ssize_t i)\r | |
305 | {\r | |
306 | return PyInt_FromLong(((long *)ap->ob_item)[i]);\r | |
307 | }\r | |
308 | \r | |
309 | static int\r | |
310 | l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)\r | |
311 | {\r | |
312 | long x;\r | |
313 | if (!PyArg_Parse(v, "l;array item must be integer", &x))\r | |
314 | return -1;\r | |
315 | if (i >= 0)\r | |
316 | ((long *)ap->ob_item)[i] = x;\r | |
317 | return 0;\r | |
318 | }\r | |
319 | \r | |
320 | static PyObject *\r | |
321 | LL_getitem(arrayobject *ap, Py_ssize_t i)\r | |
322 | {\r | |
323 | return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);\r | |
324 | }\r | |
325 | \r | |
326 | static int\r | |
327 | LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)\r | |
328 | {\r | |
329 | unsigned long x;\r | |
330 | if (PyLong_Check(v)) {\r | |
331 | x = PyLong_AsUnsignedLong(v);\r | |
332 | if (x == (unsigned long) -1 && PyErr_Occurred())\r | |
333 | return -1;\r | |
334 | }\r | |
335 | else {\r | |
336 | long y;\r | |
337 | if (!PyArg_Parse(v, "l;array item must be integer", &y))\r | |
338 | return -1;\r | |
339 | if (y < 0) {\r | |
340 | PyErr_SetString(PyExc_OverflowError,\r | |
341 | "unsigned long is less than minimum");\r | |
342 | return -1;\r | |
343 | }\r | |
344 | x = (unsigned long)y;\r | |
345 | \r | |
346 | }\r | |
347 | if (x > ULONG_MAX) {\r | |
348 | PyErr_SetString(PyExc_OverflowError,\r | |
349 | "unsigned long is greater than maximum");\r | |
350 | return -1;\r | |
351 | }\r | |
352 | \r | |
353 | if (i >= 0)\r | |
354 | ((unsigned long *)ap->ob_item)[i] = x;\r | |
355 | return 0;\r | |
356 | }\r | |
357 | \r | |
358 | static PyObject *\r | |
359 | f_getitem(arrayobject *ap, Py_ssize_t i)\r | |
360 | {\r | |
361 | return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);\r | |
362 | }\r | |
363 | \r | |
364 | static int\r | |
365 | f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)\r | |
366 | {\r | |
367 | float x;\r | |
368 | if (!PyArg_Parse(v, "f;array item must be float", &x))\r | |
369 | return -1;\r | |
370 | if (i >= 0)\r | |
371 | ((float *)ap->ob_item)[i] = x;\r | |
372 | return 0;\r | |
373 | }\r | |
374 | \r | |
375 | static PyObject *\r | |
376 | d_getitem(arrayobject *ap, Py_ssize_t i)\r | |
377 | {\r | |
378 | return PyFloat_FromDouble(((double *)ap->ob_item)[i]);\r | |
379 | }\r | |
380 | \r | |
381 | static int\r | |
382 | d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)\r | |
383 | {\r | |
384 | double x;\r | |
385 | if (!PyArg_Parse(v, "d;array item must be float", &x))\r | |
386 | return -1;\r | |
387 | if (i >= 0)\r | |
388 | ((double *)ap->ob_item)[i] = x;\r | |
389 | return 0;\r | |
390 | }\r | |
391 | \r | |
392 | /* Description of types */\r | |
393 | static struct arraydescr descriptors[] = {\r | |
394 | {'c', sizeof(char), c_getitem, c_setitem},\r | |
395 | {'b', sizeof(char), b_getitem, b_setitem},\r | |
396 | {'B', sizeof(char), BB_getitem, BB_setitem},\r | |
397 | #ifdef Py_USING_UNICODE\r | |
398 | {'u', sizeof(Py_UNICODE), u_getitem, u_setitem},\r | |
399 | #endif\r | |
400 | {'h', sizeof(short), h_getitem, h_setitem},\r | |
401 | {'H', sizeof(short), HH_getitem, HH_setitem},\r | |
402 | {'i', sizeof(int), i_getitem, i_setitem},\r | |
403 | {'I', sizeof(int), II_getitem, II_setitem},\r | |
404 | {'l', sizeof(long), l_getitem, l_setitem},\r | |
405 | {'L', sizeof(long), LL_getitem, LL_setitem},\r | |
406 | {'f', sizeof(float), f_getitem, f_setitem},\r | |
407 | {'d', sizeof(double), d_getitem, d_setitem},\r | |
408 | {'\0', 0, 0, 0} /* Sentinel */\r | |
409 | };\r | |
410 | \r | |
411 | /****************************************************************************\r | |
412 | Implementations of array object methods.\r | |
413 | ****************************************************************************/\r | |
414 | \r | |
415 | static PyObject *\r | |
416 | newarrayobject(PyTypeObject *type, Py_ssize_t size, struct arraydescr *descr)\r | |
417 | {\r | |
418 | arrayobject *op;\r | |
419 | size_t nbytes;\r | |
420 | \r | |
421 | if (size < 0) {\r | |
422 | PyErr_BadInternalCall();\r | |
423 | return NULL;\r | |
424 | }\r | |
425 | \r | |
426 | nbytes = size * descr->itemsize;\r | |
427 | /* Check for overflow */\r | |
428 | if (nbytes / descr->itemsize != (size_t)size) {\r | |
429 | return PyErr_NoMemory();\r | |
430 | }\r | |
431 | op = (arrayobject *) type->tp_alloc(type, 0);\r | |
432 | if (op == NULL) {\r | |
433 | return NULL;\r | |
434 | }\r | |
435 | op->ob_descr = descr;\r | |
436 | op->allocated = size;\r | |
437 | op->weakreflist = NULL;\r | |
438 | Py_SIZE(op) = size;\r | |
439 | if (size <= 0) {\r | |
440 | op->ob_item = NULL;\r | |
441 | }\r | |
442 | else {\r | |
443 | op->ob_item = PyMem_NEW(char, nbytes);\r | |
444 | if (op->ob_item == NULL) {\r | |
445 | Py_DECREF(op);\r | |
446 | return PyErr_NoMemory();\r | |
447 | }\r | |
448 | }\r | |
449 | return (PyObject *) op;\r | |
450 | }\r | |
451 | \r | |
452 | static PyObject *\r | |
453 | getarrayitem(PyObject *op, Py_ssize_t i)\r | |
454 | {\r | |
455 | register arrayobject *ap;\r | |
456 | assert(array_Check(op));\r | |
457 | ap = (arrayobject *)op;\r | |
458 | assert(i>=0 && i<Py_SIZE(ap));\r | |
459 | return (*ap->ob_descr->getitem)(ap, i);\r | |
460 | }\r | |
461 | \r | |
462 | static int\r | |
463 | ins1(arrayobject *self, Py_ssize_t where, PyObject *v)\r | |
464 | {\r | |
465 | char *items;\r | |
466 | Py_ssize_t n = Py_SIZE(self);\r | |
467 | if (v == NULL) {\r | |
468 | PyErr_BadInternalCall();\r | |
469 | return -1;\r | |
470 | }\r | |
471 | if ((*self->ob_descr->setitem)(self, -1, v) < 0)\r | |
472 | return -1;\r | |
473 | \r | |
474 | if (array_resize(self, n+1) == -1)\r | |
475 | return -1;\r | |
476 | items = self->ob_item;\r | |
477 | if (where < 0) {\r | |
478 | where += n;\r | |
479 | if (where < 0)\r | |
480 | where = 0;\r | |
481 | }\r | |
482 | if (where > n)\r | |
483 | where = n;\r | |
484 | /* appends don't need to call memmove() */\r | |
485 | if (where != n)\r | |
486 | memmove(items + (where+1)*self->ob_descr->itemsize,\r | |
487 | items + where*self->ob_descr->itemsize,\r | |
488 | (n-where)*self->ob_descr->itemsize);\r | |
489 | return (*self->ob_descr->setitem)(self, where, v);\r | |
490 | }\r | |
491 | \r | |
492 | /* Methods */\r | |
493 | \r | |
494 | static void\r | |
495 | array_dealloc(arrayobject *op)\r | |
496 | {\r | |
497 | if (op->weakreflist != NULL)\r | |
498 | PyObject_ClearWeakRefs((PyObject *) op);\r | |
499 | if (op->ob_item != NULL)\r | |
500 | PyMem_DEL(op->ob_item);\r | |
501 | Py_TYPE(op)->tp_free((PyObject *)op);\r | |
502 | }\r | |
503 | \r | |
504 | static PyObject *\r | |
505 | array_richcompare(PyObject *v, PyObject *w, int op)\r | |
506 | {\r | |
507 | arrayobject *va, *wa;\r | |
508 | PyObject *vi = NULL;\r | |
509 | PyObject *wi = NULL;\r | |
510 | Py_ssize_t i, k;\r | |
511 | PyObject *res;\r | |
512 | \r | |
513 | if (!array_Check(v) || !array_Check(w)) {\r | |
514 | Py_INCREF(Py_NotImplemented);\r | |
515 | return Py_NotImplemented;\r | |
516 | }\r | |
517 | \r | |
518 | va = (arrayobject *)v;\r | |
519 | wa = (arrayobject *)w;\r | |
520 | \r | |
521 | if (Py_SIZE(va) != Py_SIZE(wa) && (op == Py_EQ || op == Py_NE)) {\r | |
522 | /* Shortcut: if the lengths differ, the arrays differ */\r | |
523 | if (op == Py_EQ)\r | |
524 | res = Py_False;\r | |
525 | else\r | |
526 | res = Py_True;\r | |
527 | Py_INCREF(res);\r | |
528 | return res;\r | |
529 | }\r | |
530 | \r | |
531 | /* Search for the first index where items are different */\r | |
532 | k = 1;\r | |
533 | for (i = 0; i < Py_SIZE(va) && i < Py_SIZE(wa); i++) {\r | |
534 | vi = getarrayitem(v, i);\r | |
535 | wi = getarrayitem(w, i);\r | |
536 | if (vi == NULL || wi == NULL) {\r | |
537 | Py_XDECREF(vi);\r | |
538 | Py_XDECREF(wi);\r | |
539 | return NULL;\r | |
540 | }\r | |
541 | k = PyObject_RichCompareBool(vi, wi, Py_EQ);\r | |
542 | if (k == 0)\r | |
543 | break; /* Keeping vi and wi alive! */\r | |
544 | Py_DECREF(vi);\r | |
545 | Py_DECREF(wi);\r | |
546 | if (k < 0)\r | |
547 | return NULL;\r | |
548 | }\r | |
549 | \r | |
550 | if (k) {\r | |
551 | /* No more items to compare -- compare sizes */\r | |
552 | Py_ssize_t vs = Py_SIZE(va);\r | |
553 | Py_ssize_t ws = Py_SIZE(wa);\r | |
554 | int cmp;\r | |
555 | switch (op) {\r | |
556 | case Py_LT: cmp = vs < ws; break;\r | |
557 | case Py_LE: cmp = vs <= ws; break;\r | |
558 | case Py_EQ: cmp = vs == ws; break;\r | |
559 | case Py_NE: cmp = vs != ws; break;\r | |
560 | case Py_GT: cmp = vs > ws; break;\r | |
561 | case Py_GE: cmp = vs >= ws; break;\r | |
562 | default: return NULL; /* cannot happen */\r | |
563 | }\r | |
564 | if (cmp)\r | |
565 | res = Py_True;\r | |
566 | else\r | |
567 | res = Py_False;\r | |
568 | Py_INCREF(res);\r | |
569 | return res;\r | |
570 | }\r | |
571 | \r | |
572 | /* We have an item that differs. First, shortcuts for EQ/NE */\r | |
573 | if (op == Py_EQ) {\r | |
574 | Py_INCREF(Py_False);\r | |
575 | res = Py_False;\r | |
576 | }\r | |
577 | else if (op == Py_NE) {\r | |
578 | Py_INCREF(Py_True);\r | |
579 | res = Py_True;\r | |
580 | }\r | |
581 | else {\r | |
582 | /* Compare the final item again using the proper operator */\r | |
583 | res = PyObject_RichCompare(vi, wi, op);\r | |
584 | }\r | |
585 | Py_DECREF(vi);\r | |
586 | Py_DECREF(wi);\r | |
587 | return res;\r | |
588 | }\r | |
589 | \r | |
590 | static Py_ssize_t\r | |
591 | array_length(arrayobject *a)\r | |
592 | {\r | |
593 | return Py_SIZE(a);\r | |
594 | }\r | |
595 | \r | |
596 | static PyObject *\r | |
597 | array_item(arrayobject *a, Py_ssize_t i)\r | |
598 | {\r | |
599 | if (i < 0 || i >= Py_SIZE(a)) {\r | |
600 | PyErr_SetString(PyExc_IndexError, "array index out of range");\r | |
601 | return NULL;\r | |
602 | }\r | |
603 | return getarrayitem((PyObject *)a, i);\r | |
604 | }\r | |
605 | \r | |
606 | static PyObject *\r | |
607 | array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)\r | |
608 | {\r | |
609 | arrayobject *np;\r | |
610 | if (ilow < 0)\r | |
611 | ilow = 0;\r | |
612 | else if (ilow > Py_SIZE(a))\r | |
613 | ilow = Py_SIZE(a);\r | |
614 | if (ihigh < 0)\r | |
615 | ihigh = 0;\r | |
616 | if (ihigh < ilow)\r | |
617 | ihigh = ilow;\r | |
618 | else if (ihigh > Py_SIZE(a))\r | |
619 | ihigh = Py_SIZE(a);\r | |
620 | np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);\r | |
621 | if (np == NULL)\r | |
622 | return NULL;\r | |
623 | memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,\r | |
624 | (ihigh-ilow) * a->ob_descr->itemsize);\r | |
625 | return (PyObject *)np;\r | |
626 | }\r | |
627 | \r | |
628 | static PyObject *\r | |
629 | array_copy(arrayobject *a, PyObject *unused)\r | |
630 | {\r | |
631 | return array_slice(a, 0, Py_SIZE(a));\r | |
632 | }\r | |
633 | \r | |
634 | PyDoc_STRVAR(copy_doc,\r | |
635 | "copy(array)\n\\r | |
636 | \n\\r | |
637 | Return a copy of the array.");\r | |
638 | \r | |
639 | static PyObject *\r | |
640 | array_concat(arrayobject *a, PyObject *bb)\r | |
641 | {\r | |
642 | Py_ssize_t size;\r | |
643 | arrayobject *np;\r | |
644 | if (!array_Check(bb)) {\r | |
645 | PyErr_Format(PyExc_TypeError,\r | |
646 | "can only append array (not \"%.200s\") to array",\r | |
647 | Py_TYPE(bb)->tp_name);\r | |
648 | return NULL;\r | |
649 | }\r | |
650 | #define b ((arrayobject *)bb)\r | |
651 | if (a->ob_descr != b->ob_descr) {\r | |
652 | PyErr_BadArgument();\r | |
653 | return NULL;\r | |
654 | }\r | |
655 | if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) {\r | |
656 | return PyErr_NoMemory();\r | |
657 | }\r | |
658 | size = Py_SIZE(a) + Py_SIZE(b);\r | |
659 | np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);\r | |
660 | if (np == NULL) {\r | |
661 | return NULL;\r | |
662 | }\r | |
663 | memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize);\r | |
664 | memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize,\r | |
665 | b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);\r | |
666 | return (PyObject *)np;\r | |
667 | #undef b\r | |
668 | }\r | |
669 | \r | |
670 | static PyObject *\r | |
671 | array_repeat(arrayobject *a, Py_ssize_t n)\r | |
672 | {\r | |
673 | Py_ssize_t i;\r | |
674 | Py_ssize_t size;\r | |
675 | arrayobject *np;\r | |
676 | char *p;\r | |
677 | Py_ssize_t nbytes;\r | |
678 | if (n < 0)\r | |
679 | n = 0;\r | |
680 | if ((Py_SIZE(a) != 0) && (n > PY_SSIZE_T_MAX / Py_SIZE(a))) {\r | |
681 | return PyErr_NoMemory();\r | |
682 | }\r | |
683 | size = Py_SIZE(a) * n;\r | |
684 | np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);\r | |
685 | if (np == NULL)\r | |
686 | return NULL;\r | |
687 | p = np->ob_item;\r | |
688 | nbytes = Py_SIZE(a) * a->ob_descr->itemsize;\r | |
689 | for (i = 0; i < n; i++) {\r | |
690 | memcpy(p, a->ob_item, nbytes);\r | |
691 | p += nbytes;\r | |
692 | }\r | |
693 | return (PyObject *) np;\r | |
694 | }\r | |
695 | \r | |
696 | static int\r | |
697 | array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)\r | |
698 | {\r | |
699 | char *item;\r | |
700 | Py_ssize_t n; /* Size of replacement array */\r | |
701 | Py_ssize_t d; /* Change in size */\r | |
702 | #define b ((arrayobject *)v)\r | |
703 | if (v == NULL)\r | |
704 | n = 0;\r | |
705 | else if (array_Check(v)) {\r | |
706 | n = Py_SIZE(b);\r | |
707 | if (a == b) {\r | |
708 | /* Special case "a[i:j] = a" -- copy b first */\r | |
709 | int ret;\r | |
710 | v = array_slice(b, 0, n);\r | |
711 | if (!v)\r | |
712 | return -1;\r | |
713 | ret = array_ass_slice(a, ilow, ihigh, v);\r | |
714 | Py_DECREF(v);\r | |
715 | return ret;\r | |
716 | }\r | |
717 | if (b->ob_descr != a->ob_descr) {\r | |
718 | PyErr_BadArgument();\r | |
719 | return -1;\r | |
720 | }\r | |
721 | }\r | |
722 | else {\r | |
723 | PyErr_Format(PyExc_TypeError,\r | |
724 | "can only assign array (not \"%.200s\") to array slice",\r | |
725 | Py_TYPE(v)->tp_name);\r | |
726 | return -1;\r | |
727 | }\r | |
728 | if (ilow < 0)\r | |
729 | ilow = 0;\r | |
730 | else if (ilow > Py_SIZE(a))\r | |
731 | ilow = Py_SIZE(a);\r | |
732 | if (ihigh < 0)\r | |
733 | ihigh = 0;\r | |
734 | if (ihigh < ilow)\r | |
735 | ihigh = ilow;\r | |
736 | else if (ihigh > Py_SIZE(a))\r | |
737 | ihigh = Py_SIZE(a);\r | |
738 | item = a->ob_item;\r | |
739 | d = n - (ihigh-ilow);\r | |
740 | if (d < 0) { /* Delete -d items */\r | |
741 | memmove(item + (ihigh+d)*a->ob_descr->itemsize,\r | |
742 | item + ihigh*a->ob_descr->itemsize,\r | |
743 | (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);\r | |
744 | Py_SIZE(a) += d;\r | |
745 | PyMem_RESIZE(item, char, Py_SIZE(a)*a->ob_descr->itemsize);\r | |
746 | /* Can't fail */\r | |
747 | a->ob_item = item;\r | |
748 | a->allocated = Py_SIZE(a);\r | |
749 | }\r | |
750 | else if (d > 0) { /* Insert d items */\r | |
751 | PyMem_RESIZE(item, char,\r | |
752 | (Py_SIZE(a) + d)*a->ob_descr->itemsize);\r | |
753 | if (item == NULL) {\r | |
754 | PyErr_NoMemory();\r | |
755 | return -1;\r | |
756 | }\r | |
757 | memmove(item + (ihigh+d)*a->ob_descr->itemsize,\r | |
758 | item + ihigh*a->ob_descr->itemsize,\r | |
759 | (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);\r | |
760 | a->ob_item = item;\r | |
761 | Py_SIZE(a) += d;\r | |
762 | a->allocated = Py_SIZE(a);\r | |
763 | }\r | |
764 | if (n > 0)\r | |
765 | memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,\r | |
766 | n*b->ob_descr->itemsize);\r | |
767 | return 0;\r | |
768 | #undef b\r | |
769 | }\r | |
770 | \r | |
771 | static int\r | |
772 | array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v)\r | |
773 | {\r | |
774 | if (i < 0 || i >= Py_SIZE(a)) {\r | |
775 | PyErr_SetString(PyExc_IndexError,\r | |
776 | "array assignment index out of range");\r | |
777 | return -1;\r | |
778 | }\r | |
779 | if (v == NULL)\r | |
780 | return array_ass_slice(a, i, i+1, v);\r | |
781 | return (*a->ob_descr->setitem)(a, i, v);\r | |
782 | }\r | |
783 | \r | |
784 | static int\r | |
785 | setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v)\r | |
786 | {\r | |
787 | assert(array_Check(a));\r | |
788 | return array_ass_item((arrayobject *)a, i, v);\r | |
789 | }\r | |
790 | \r | |
791 | static int\r | |
792 | array_iter_extend(arrayobject *self, PyObject *bb)\r | |
793 | {\r | |
794 | PyObject *it, *v;\r | |
795 | \r | |
796 | it = PyObject_GetIter(bb);\r | |
797 | if (it == NULL)\r | |
798 | return -1;\r | |
799 | \r | |
800 | while ((v = PyIter_Next(it)) != NULL) {\r | |
801 | if (ins1(self, Py_SIZE(self), v) != 0) {\r | |
802 | Py_DECREF(v);\r | |
803 | Py_DECREF(it);\r | |
804 | return -1;\r | |
805 | }\r | |
806 | Py_DECREF(v);\r | |
807 | }\r | |
808 | Py_DECREF(it);\r | |
809 | if (PyErr_Occurred())\r | |
810 | return -1;\r | |
811 | return 0;\r | |
812 | }\r | |
813 | \r | |
814 | static int\r | |
815 | array_do_extend(arrayobject *self, PyObject *bb)\r | |
816 | {\r | |
817 | Py_ssize_t size;\r | |
818 | char *old_item;\r | |
819 | \r | |
820 | if (!array_Check(bb))\r | |
821 | return array_iter_extend(self, bb);\r | |
822 | #define b ((arrayobject *)bb)\r | |
823 | if (self->ob_descr != b->ob_descr) {\r | |
824 | PyErr_SetString(PyExc_TypeError,\r | |
825 | "can only extend with array of same kind");\r | |
826 | return -1;\r | |
827 | }\r | |
828 | if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) ||\r | |
829 | ((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {\r | |
830 | PyErr_NoMemory();\r | |
831 | return -1;\r | |
832 | }\r | |
833 | size = Py_SIZE(self) + Py_SIZE(b);\r | |
834 | old_item = self->ob_item;\r | |
835 | PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize);\r | |
836 | if (self->ob_item == NULL) {\r | |
837 | self->ob_item = old_item;\r | |
838 | PyErr_NoMemory();\r | |
839 | return -1;\r | |
840 | }\r | |
841 | memcpy(self->ob_item + Py_SIZE(self)*self->ob_descr->itemsize,\r | |
842 | b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);\r | |
843 | Py_SIZE(self) = size;\r | |
844 | self->allocated = size;\r | |
845 | \r | |
846 | return 0;\r | |
847 | #undef b\r | |
848 | }\r | |
849 | \r | |
850 | static PyObject *\r | |
851 | array_inplace_concat(arrayobject *self, PyObject *bb)\r | |
852 | {\r | |
853 | if (!array_Check(bb)) {\r | |
854 | PyErr_Format(PyExc_TypeError,\r | |
855 | "can only extend array with array (not \"%.200s\")",\r | |
856 | Py_TYPE(bb)->tp_name);\r | |
857 | return NULL;\r | |
858 | }\r | |
859 | if (array_do_extend(self, bb) == -1)\r | |
860 | return NULL;\r | |
861 | Py_INCREF(self);\r | |
862 | return (PyObject *)self;\r | |
863 | }\r | |
864 | \r | |
865 | static PyObject *\r | |
866 | array_inplace_repeat(arrayobject *self, Py_ssize_t n)\r | |
867 | {\r | |
868 | char *items, *p;\r | |
869 | Py_ssize_t size, i;\r | |
870 | \r | |
871 | if (Py_SIZE(self) > 0) {\r | |
872 | if (n < 0)\r | |
873 | n = 0;\r | |
874 | items = self->ob_item;\r | |
875 | if ((self->ob_descr->itemsize != 0) &&\r | |
876 | (Py_SIZE(self) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {\r | |
877 | return PyErr_NoMemory();\r | |
878 | }\r | |
879 | size = Py_SIZE(self) * self->ob_descr->itemsize;\r | |
880 | if (n == 0) {\r | |
881 | PyMem_FREE(items);\r | |
882 | self->ob_item = NULL;\r | |
883 | Py_SIZE(self) = 0;\r | |
884 | self->allocated = 0;\r | |
885 | }\r | |
886 | else {\r | |
887 | if (size > PY_SSIZE_T_MAX / n) {\r | |
888 | return PyErr_NoMemory();\r | |
889 | }\r | |
890 | PyMem_RESIZE(items, char, n * size);\r | |
891 | if (items == NULL)\r | |
892 | return PyErr_NoMemory();\r | |
893 | p = items;\r | |
894 | for (i = 1; i < n; i++) {\r | |
895 | p += size;\r | |
896 | memcpy(p, items, size);\r | |
897 | }\r | |
898 | self->ob_item = items;\r | |
899 | Py_SIZE(self) *= n;\r | |
900 | self->allocated = Py_SIZE(self);\r | |
901 | }\r | |
902 | }\r | |
903 | Py_INCREF(self);\r | |
904 | return (PyObject *)self;\r | |
905 | }\r | |
906 | \r | |
907 | \r | |
908 | static PyObject *\r | |
909 | ins(arrayobject *self, Py_ssize_t where, PyObject *v)\r | |
910 | {\r | |
911 | if (ins1(self, where, v) != 0)\r | |
912 | return NULL;\r | |
913 | Py_INCREF(Py_None);\r | |
914 | return Py_None;\r | |
915 | }\r | |
916 | \r | |
917 | static PyObject *\r | |
918 | array_count(arrayobject *self, PyObject *v)\r | |
919 | {\r | |
920 | Py_ssize_t count = 0;\r | |
921 | Py_ssize_t i;\r | |
922 | \r | |
923 | for (i = 0; i < Py_SIZE(self); i++) {\r | |
924 | PyObject *selfi = getarrayitem((PyObject *)self, i);\r | |
925 | int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);\r | |
926 | Py_DECREF(selfi);\r | |
927 | if (cmp > 0)\r | |
928 | count++;\r | |
929 | else if (cmp < 0)\r | |
930 | return NULL;\r | |
931 | }\r | |
932 | return PyInt_FromSsize_t(count);\r | |
933 | }\r | |
934 | \r | |
935 | PyDoc_STRVAR(count_doc,\r | |
936 | "count(x)\n\\r | |
937 | \n\\r | |
938 | Return number of occurrences of x in the array.");\r | |
939 | \r | |
940 | static PyObject *\r | |
941 | array_index(arrayobject *self, PyObject *v)\r | |
942 | {\r | |
943 | Py_ssize_t i;\r | |
944 | \r | |
945 | for (i = 0; i < Py_SIZE(self); i++) {\r | |
946 | PyObject *selfi = getarrayitem((PyObject *)self, i);\r | |
947 | int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);\r | |
948 | Py_DECREF(selfi);\r | |
949 | if (cmp > 0) {\r | |
950 | return PyInt_FromLong((long)i);\r | |
951 | }\r | |
952 | else if (cmp < 0)\r | |
953 | return NULL;\r | |
954 | }\r | |
955 | PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");\r | |
956 | return NULL;\r | |
957 | }\r | |
958 | \r | |
959 | PyDoc_STRVAR(index_doc,\r | |
960 | "index(x)\n\\r | |
961 | \n\\r | |
962 | Return index of first occurrence of x in the array.");\r | |
963 | \r | |
964 | static int\r | |
965 | array_contains(arrayobject *self, PyObject *v)\r | |
966 | {\r | |
967 | Py_ssize_t i;\r | |
968 | int cmp;\r | |
969 | \r | |
970 | for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) {\r | |
971 | PyObject *selfi = getarrayitem((PyObject *)self, i);\r | |
972 | cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);\r | |
973 | Py_DECREF(selfi);\r | |
974 | }\r | |
975 | return cmp;\r | |
976 | }\r | |
977 | \r | |
978 | static PyObject *\r | |
979 | array_remove(arrayobject *self, PyObject *v)\r | |
980 | {\r | |
981 | int i;\r | |
982 | \r | |
983 | for (i = 0; i < Py_SIZE(self); i++) {\r | |
984 | PyObject *selfi = getarrayitem((PyObject *)self,i);\r | |
985 | int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);\r | |
986 | Py_DECREF(selfi);\r | |
987 | if (cmp > 0) {\r | |
988 | if (array_ass_slice(self, i, i+1,\r | |
989 | (PyObject *)NULL) != 0)\r | |
990 | return NULL;\r | |
991 | Py_INCREF(Py_None);\r | |
992 | return Py_None;\r | |
993 | }\r | |
994 | else if (cmp < 0)\r | |
995 | return NULL;\r | |
996 | }\r | |
997 | PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");\r | |
998 | return NULL;\r | |
999 | }\r | |
1000 | \r | |
1001 | PyDoc_STRVAR(remove_doc,\r | |
1002 | "remove(x)\n\\r | |
1003 | \n\\r | |
1004 | Remove the first occurrence of x in the array.");\r | |
1005 | \r | |
1006 | static PyObject *\r | |
1007 | array_pop(arrayobject *self, PyObject *args)\r | |
1008 | {\r | |
1009 | Py_ssize_t i = -1;\r | |
1010 | PyObject *v;\r | |
1011 | if (!PyArg_ParseTuple(args, "|n:pop", &i))\r | |
1012 | return NULL;\r | |
1013 | if (Py_SIZE(self) == 0) {\r | |
1014 | /* Special-case most common failure cause */\r | |
1015 | PyErr_SetString(PyExc_IndexError, "pop from empty array");\r | |
1016 | return NULL;\r | |
1017 | }\r | |
1018 | if (i < 0)\r | |
1019 | i += Py_SIZE(self);\r | |
1020 | if (i < 0 || i >= Py_SIZE(self)) {\r | |
1021 | PyErr_SetString(PyExc_IndexError, "pop index out of range");\r | |
1022 | return NULL;\r | |
1023 | }\r | |
1024 | v = getarrayitem((PyObject *)self,i);\r | |
1025 | if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {\r | |
1026 | Py_DECREF(v);\r | |
1027 | return NULL;\r | |
1028 | }\r | |
1029 | return v;\r | |
1030 | }\r | |
1031 | \r | |
1032 | PyDoc_STRVAR(pop_doc,\r | |
1033 | "pop([i])\n\\r | |
1034 | \n\\r | |
1035 | Return the i-th element and delete it from the array. i defaults to -1.");\r | |
1036 | \r | |
1037 | static PyObject *\r | |
1038 | array_extend(arrayobject *self, PyObject *bb)\r | |
1039 | {\r | |
1040 | if (array_do_extend(self, bb) == -1)\r | |
1041 | return NULL;\r | |
1042 | Py_INCREF(Py_None);\r | |
1043 | return Py_None;\r | |
1044 | }\r | |
1045 | \r | |
1046 | PyDoc_STRVAR(extend_doc,\r | |
1047 | "extend(array or iterable)\n\\r | |
1048 | \n\\r | |
1049 | Append items to the end of the array.");\r | |
1050 | \r | |
1051 | static PyObject *\r | |
1052 | array_insert(arrayobject *self, PyObject *args)\r | |
1053 | {\r | |
1054 | Py_ssize_t i;\r | |
1055 | PyObject *v;\r | |
1056 | if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))\r | |
1057 | return NULL;\r | |
1058 | return ins(self, i, v);\r | |
1059 | }\r | |
1060 | \r | |
1061 | PyDoc_STRVAR(insert_doc,\r | |
1062 | "insert(i,x)\n\\r | |
1063 | \n\\r | |
1064 | Insert a new item x into the array before position i.");\r | |
1065 | \r | |
1066 | \r | |
1067 | static PyObject *\r | |
1068 | array_buffer_info(arrayobject *self, PyObject *unused)\r | |
1069 | {\r | |
1070 | PyObject* retval = NULL;\r | |
1071 | retval = PyTuple_New(2);\r | |
1072 | if (!retval)\r | |
1073 | return NULL;\r | |
1074 | \r | |
1075 | PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item));\r | |
1076 | PyTuple_SET_ITEM(retval, 1, PyInt_FromLong((long)(Py_SIZE(self))));\r | |
1077 | \r | |
1078 | return retval;\r | |
1079 | }\r | |
1080 | \r | |
1081 | PyDoc_STRVAR(buffer_info_doc,\r | |
1082 | "buffer_info() -> (address, length)\n\\r | |
1083 | \n\\r | |
1084 | Return a tuple (address, length) giving the current memory address and\n\\r | |
1085 | the length in items of the buffer used to hold array's contents\n\\r | |
1086 | The length should be multiplied by the itemsize attribute to calculate\n\\r | |
1087 | the buffer length in bytes.");\r | |
1088 | \r | |
1089 | \r | |
1090 | static PyObject *\r | |
1091 | array_append(arrayobject *self, PyObject *v)\r | |
1092 | {\r | |
1093 | return ins(self, Py_SIZE(self), v);\r | |
1094 | }\r | |
1095 | \r | |
1096 | PyDoc_STRVAR(append_doc,\r | |
1097 | "append(x)\n\\r | |
1098 | \n\\r | |
1099 | Append new value x to the end of the array.");\r | |
1100 | \r | |
1101 | \r | |
1102 | static PyObject *\r | |
1103 | array_byteswap(arrayobject *self, PyObject *unused)\r | |
1104 | {\r | |
1105 | char *p;\r | |
1106 | Py_ssize_t i;\r | |
1107 | \r | |
1108 | switch (self->ob_descr->itemsize) {\r | |
1109 | case 1:\r | |
1110 | break;\r | |
1111 | case 2:\r | |
1112 | for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) {\r | |
1113 | char p0 = p[0];\r | |
1114 | p[0] = p[1];\r | |
1115 | p[1] = p0;\r | |
1116 | }\r | |
1117 | break;\r | |
1118 | case 4:\r | |
1119 | for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 4) {\r | |
1120 | char p0 = p[0];\r | |
1121 | char p1 = p[1];\r | |
1122 | p[0] = p[3];\r | |
1123 | p[1] = p[2];\r | |
1124 | p[2] = p1;\r | |
1125 | p[3] = p0;\r | |
1126 | }\r | |
1127 | break;\r | |
1128 | case 8:\r | |
1129 | for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) {\r | |
1130 | char p0 = p[0];\r | |
1131 | char p1 = p[1];\r | |
1132 | char p2 = p[2];\r | |
1133 | char p3 = p[3];\r | |
1134 | p[0] = p[7];\r | |
1135 | p[1] = p[6];\r | |
1136 | p[2] = p[5];\r | |
1137 | p[3] = p[4];\r | |
1138 | p[4] = p3;\r | |
1139 | p[5] = p2;\r | |
1140 | p[6] = p1;\r | |
1141 | p[7] = p0;\r | |
1142 | }\r | |
1143 | break;\r | |
1144 | default:\r | |
1145 | PyErr_SetString(PyExc_RuntimeError,\r | |
1146 | "don't know how to byteswap this array type");\r | |
1147 | return NULL;\r | |
1148 | }\r | |
1149 | Py_INCREF(Py_None);\r | |
1150 | return Py_None;\r | |
1151 | }\r | |
1152 | \r | |
1153 | PyDoc_STRVAR(byteswap_doc,\r | |
1154 | "byteswap()\n\\r | |
1155 | \n\\r | |
1156 | Byteswap all items of the array. If the items in the array are not 1, 2,\n\\r | |
1157 | 4, or 8 bytes in size, RuntimeError is raised.");\r | |
1158 | \r | |
1159 | static PyObject *\r | |
1160 | array_reverse(arrayobject *self, PyObject *unused)\r | |
1161 | {\r | |
1162 | register Py_ssize_t itemsize = self->ob_descr->itemsize;\r | |
1163 | register char *p, *q;\r | |
1164 | /* little buffer to hold items while swapping */\r | |
1165 | char tmp[256]; /* 8 is probably enough -- but why skimp */\r | |
1166 | assert((size_t)itemsize <= sizeof(tmp));\r | |
1167 | \r | |
1168 | if (Py_SIZE(self) > 1) {\r | |
1169 | for (p = self->ob_item,\r | |
1170 | q = self->ob_item + (Py_SIZE(self) - 1)*itemsize;\r | |
1171 | p < q;\r | |
1172 | p += itemsize, q -= itemsize) {\r | |
1173 | /* memory areas guaranteed disjoint, so memcpy\r | |
1174 | * is safe (& memmove may be slower).\r | |
1175 | */\r | |
1176 | memcpy(tmp, p, itemsize);\r | |
1177 | memcpy(p, q, itemsize);\r | |
1178 | memcpy(q, tmp, itemsize);\r | |
1179 | }\r | |
1180 | }\r | |
1181 | \r | |
1182 | Py_INCREF(Py_None);\r | |
1183 | return Py_None;\r | |
1184 | }\r | |
1185 | \r | |
1186 | PyDoc_STRVAR(reverse_doc,\r | |
1187 | "reverse()\n\\r | |
1188 | \n\\r | |
1189 | Reverse the order of the items in the array.");\r | |
1190 | \r | |
1191 | static PyObject *\r | |
1192 | array_fromfile(arrayobject *self, PyObject *args)\r | |
1193 | {\r | |
1194 | PyObject *f;\r | |
1195 | Py_ssize_t n;\r | |
1196 | FILE *fp;\r | |
1197 | if (!PyArg_ParseTuple(args, "On:fromfile", &f, &n))\r | |
1198 | return NULL;\r | |
1199 | fp = PyFile_AsFile(f);\r | |
1200 | if (fp == NULL) {\r | |
1201 | PyErr_SetString(PyExc_TypeError, "arg1 must be open file");\r | |
1202 | return NULL;\r | |
1203 | }\r | |
1204 | if (n > 0) {\r | |
1205 | char *item = self->ob_item;\r | |
1206 | Py_ssize_t itemsize = self->ob_descr->itemsize;\r | |
1207 | size_t nread;\r | |
1208 | Py_ssize_t newlength;\r | |
1209 | size_t newbytes;\r | |
1210 | /* Be careful here about overflow */\r | |
1211 | if ((newlength = Py_SIZE(self) + n) <= 0 ||\r | |
1212 | (newbytes = newlength * itemsize) / itemsize !=\r | |
1213 | (size_t)newlength)\r | |
1214 | goto nomem;\r | |
1215 | PyMem_RESIZE(item, char, newbytes);\r | |
1216 | if (item == NULL) {\r | |
1217 | nomem:\r | |
1218 | PyErr_NoMemory();\r | |
1219 | return NULL;\r | |
1220 | }\r | |
1221 | self->ob_item = item;\r | |
1222 | Py_SIZE(self) += n;\r | |
1223 | self->allocated = Py_SIZE(self);\r | |
1224 | nread = fread(item + (Py_SIZE(self) - n) * itemsize,\r | |
1225 | itemsize, n, fp);\r | |
1226 | if (nread < (size_t)n) {\r | |
1227 | Py_SIZE(self) -= (n - nread);\r | |
1228 | PyMem_RESIZE(item, char, Py_SIZE(self)*itemsize);\r | |
1229 | self->ob_item = item;\r | |
1230 | self->allocated = Py_SIZE(self);\r | |
1231 | if (ferror(fp)) {\r | |
1232 | PyErr_SetFromErrno(PyExc_IOError);\r | |
1233 | clearerr(fp);\r | |
1234 | }\r | |
1235 | else {\r | |
1236 | PyErr_SetString(PyExc_EOFError,\r | |
1237 | "not enough items in file");\r | |
1238 | }\r | |
1239 | return NULL;\r | |
1240 | }\r | |
1241 | }\r | |
1242 | Py_INCREF(Py_None);\r | |
1243 | return Py_None;\r | |
1244 | }\r | |
1245 | \r | |
1246 | PyDoc_STRVAR(fromfile_doc,\r | |
1247 | "fromfile(f, n)\n\\r | |
1248 | \n\\r | |
1249 | Read n objects from the file object f and append them to the end of the\n\\r | |
1250 | array. Also called as read.");\r | |
1251 | \r | |
1252 | \r | |
1253 | static PyObject *\r | |
1254 | array_fromfile_as_read(arrayobject *self, PyObject *args)\r | |
1255 | {\r | |
1256 | if (PyErr_WarnPy3k("array.read() not supported in 3.x; "\r | |
1257 | "use array.fromfile()", 1) < 0)\r | |
1258 | return NULL;\r | |
1259 | return array_fromfile(self, args);\r | |
1260 | }\r | |
1261 | \r | |
1262 | \r | |
1263 | static PyObject *\r | |
1264 | array_tofile(arrayobject *self, PyObject *f)\r | |
1265 | {\r | |
1266 | FILE *fp;\r | |
1267 | \r | |
1268 | fp = PyFile_AsFile(f);\r | |
1269 | if (fp == NULL) {\r | |
1270 | PyErr_SetString(PyExc_TypeError, "arg must be open file");\r | |
1271 | return NULL;\r | |
1272 | }\r | |
1273 | if (self->ob_size > 0) {\r | |
1274 | if (fwrite(self->ob_item, self->ob_descr->itemsize,\r | |
1275 | self->ob_size, fp) != (size_t)self->ob_size) {\r | |
1276 | PyErr_SetFromErrno(PyExc_IOError);\r | |
1277 | clearerr(fp);\r | |
1278 | return NULL;\r | |
1279 | }\r | |
1280 | }\r | |
1281 | Py_INCREF(Py_None);\r | |
1282 | return Py_None;\r | |
1283 | }\r | |
1284 | \r | |
1285 | PyDoc_STRVAR(tofile_doc,\r | |
1286 | "tofile(f)\n\\r | |
1287 | \n\\r | |
1288 | Write all items (as machine values) to the file object f. Also called as\n\\r | |
1289 | write.");\r | |
1290 | \r | |
1291 | \r | |
1292 | static PyObject *\r | |
1293 | array_tofile_as_write(arrayobject *self, PyObject *f)\r | |
1294 | {\r | |
1295 | if (PyErr_WarnPy3k("array.write() not supported in 3.x; "\r | |
1296 | "use array.tofile()", 1) < 0)\r | |
1297 | return NULL;\r | |
1298 | return array_tofile(self, f);\r | |
1299 | }\r | |
1300 | \r | |
1301 | \r | |
1302 | static PyObject *\r | |
1303 | array_fromlist(arrayobject *self, PyObject *list)\r | |
1304 | {\r | |
1305 | Py_ssize_t n;\r | |
1306 | Py_ssize_t itemsize = self->ob_descr->itemsize;\r | |
1307 | \r | |
1308 | if (!PyList_Check(list)) {\r | |
1309 | PyErr_SetString(PyExc_TypeError, "arg must be list");\r | |
1310 | return NULL;\r | |
1311 | }\r | |
1312 | n = PyList_Size(list);\r | |
1313 | if (n > 0) {\r | |
1314 | char *item = self->ob_item;\r | |
1315 | Py_ssize_t i;\r | |
1316 | PyMem_RESIZE(item, char, (Py_SIZE(self) + n) * itemsize);\r | |
1317 | if (item == NULL) {\r | |
1318 | PyErr_NoMemory();\r | |
1319 | return NULL;\r | |
1320 | }\r | |
1321 | self->ob_item = item;\r | |
1322 | Py_SIZE(self) += n;\r | |
1323 | self->allocated = Py_SIZE(self);\r | |
1324 | for (i = 0; i < n; i++) {\r | |
1325 | PyObject *v = PyList_GetItem(list, i);\r | |
1326 | if ((*self->ob_descr->setitem)(self,\r | |
1327 | Py_SIZE(self) - n + i, v) != 0) {\r | |
1328 | Py_SIZE(self) -= n;\r | |
1329 | if (itemsize && (self->ob_size > PY_SSIZE_T_MAX / itemsize)) {\r | |
1330 | return PyErr_NoMemory();\r | |
1331 | }\r | |
1332 | PyMem_RESIZE(item, char,\r | |
1333 | Py_SIZE(self) * itemsize);\r | |
1334 | self->ob_item = item;\r | |
1335 | self->allocated = Py_SIZE(self);\r | |
1336 | return NULL;\r | |
1337 | }\r | |
1338 | }\r | |
1339 | }\r | |
1340 | Py_INCREF(Py_None);\r | |
1341 | return Py_None;\r | |
1342 | }\r | |
1343 | \r | |
1344 | PyDoc_STRVAR(fromlist_doc,\r | |
1345 | "fromlist(list)\n\\r | |
1346 | \n\\r | |
1347 | Append items to array from list.");\r | |
1348 | \r | |
1349 | \r | |
1350 | static PyObject *\r | |
1351 | array_tolist(arrayobject *self, PyObject *unused)\r | |
1352 | {\r | |
1353 | PyObject *list = PyList_New(Py_SIZE(self));\r | |
1354 | Py_ssize_t i;\r | |
1355 | \r | |
1356 | if (list == NULL)\r | |
1357 | return NULL;\r | |
1358 | for (i = 0; i < Py_SIZE(self); i++) {\r | |
1359 | PyObject *v = getarrayitem((PyObject *)self, i);\r | |
1360 | if (v == NULL) {\r | |
1361 | Py_DECREF(list);\r | |
1362 | return NULL;\r | |
1363 | }\r | |
1364 | PyList_SetItem(list, i, v);\r | |
1365 | }\r | |
1366 | return list;\r | |
1367 | }\r | |
1368 | \r | |
1369 | PyDoc_STRVAR(tolist_doc,\r | |
1370 | "tolist() -> list\n\\r | |
1371 | \n\\r | |
1372 | Convert array to an ordinary list with the same items.");\r | |
1373 | \r | |
1374 | \r | |
1375 | static PyObject *\r | |
1376 | array_fromstring(arrayobject *self, PyObject *args)\r | |
1377 | {\r | |
1378 | char *str;\r | |
1379 | Py_ssize_t n;\r | |
1380 | int itemsize = self->ob_descr->itemsize;\r | |
1381 | if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n))\r | |
1382 | return NULL;\r | |
1383 | if (n % itemsize != 0) {\r | |
1384 | PyErr_SetString(PyExc_ValueError,\r | |
1385 | "string length not a multiple of item size");\r | |
1386 | return NULL;\r | |
1387 | }\r | |
1388 | n = n / itemsize;\r | |
1389 | if (n > 0) {\r | |
1390 | char *item = self->ob_item;\r | |
1391 | if ((n > PY_SSIZE_T_MAX - Py_SIZE(self)) ||\r | |
1392 | ((Py_SIZE(self) + n) > PY_SSIZE_T_MAX / itemsize)) {\r | |
1393 | return PyErr_NoMemory();\r | |
1394 | }\r | |
1395 | PyMem_RESIZE(item, char, (Py_SIZE(self) + n) * itemsize);\r | |
1396 | if (item == NULL) {\r | |
1397 | PyErr_NoMemory();\r | |
1398 | return NULL;\r | |
1399 | }\r | |
1400 | self->ob_item = item;\r | |
1401 | Py_SIZE(self) += n;\r | |
1402 | self->allocated = Py_SIZE(self);\r | |
1403 | memcpy(item + (Py_SIZE(self) - n) * itemsize,\r | |
1404 | str, itemsize*n);\r | |
1405 | }\r | |
1406 | Py_INCREF(Py_None);\r | |
1407 | return Py_None;\r | |
1408 | }\r | |
1409 | \r | |
1410 | PyDoc_STRVAR(fromstring_doc,\r | |
1411 | "fromstring(string)\n\\r | |
1412 | \n\\r | |
1413 | Appends items from the string, interpreting it as an array of machine\n\\r | |
1414 | values,as if it had been read from a file using the fromfile() method).");\r | |
1415 | \r | |
1416 | \r | |
1417 | static PyObject *\r | |
1418 | array_tostring(arrayobject *self, PyObject *unused)\r | |
1419 | {\r | |
1420 | if (self->ob_size <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {\r | |
1421 | return PyString_FromStringAndSize(self->ob_item,\r | |
1422 | Py_SIZE(self) * self->ob_descr->itemsize);\r | |
1423 | } else {\r | |
1424 | return PyErr_NoMemory();\r | |
1425 | }\r | |
1426 | }\r | |
1427 | \r | |
1428 | PyDoc_STRVAR(tostring_doc,\r | |
1429 | "tostring() -> string\n\\r | |
1430 | \n\\r | |
1431 | Convert the array to an array of machine values and return the string\n\\r | |
1432 | representation.");\r | |
1433 | \r | |
1434 | \r | |
1435 | \r | |
1436 | #ifdef Py_USING_UNICODE\r | |
1437 | static PyObject *\r | |
1438 | array_fromunicode(arrayobject *self, PyObject *args)\r | |
1439 | {\r | |
1440 | Py_UNICODE *ustr;\r | |
1441 | Py_ssize_t n;\r | |
1442 | \r | |
1443 | if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))\r | |
1444 | return NULL;\r | |
1445 | if (self->ob_descr->typecode != 'u') {\r | |
1446 | PyErr_SetString(PyExc_ValueError,\r | |
1447 | "fromunicode() may only be called on "\r | |
1448 | "type 'u' arrays");\r | |
1449 | return NULL;\r | |
1450 | }\r | |
1451 | if (n > 0) {\r | |
1452 | Py_UNICODE *item = (Py_UNICODE *) self->ob_item;\r | |
1453 | if (Py_SIZE(self) > PY_SSIZE_T_MAX - n) {\r | |
1454 | return PyErr_NoMemory();\r | |
1455 | }\r | |
1456 | PyMem_RESIZE(item, Py_UNICODE, Py_SIZE(self) + n);\r | |
1457 | if (item == NULL) {\r | |
1458 | PyErr_NoMemory();\r | |
1459 | return NULL;\r | |
1460 | }\r | |
1461 | self->ob_item = (char *) item;\r | |
1462 | Py_SIZE(self) += n;\r | |
1463 | self->allocated = Py_SIZE(self);\r | |
1464 | memcpy(item + Py_SIZE(self) - n,\r | |
1465 | ustr, n * sizeof(Py_UNICODE));\r | |
1466 | }\r | |
1467 | \r | |
1468 | Py_INCREF(Py_None);\r | |
1469 | return Py_None;\r | |
1470 | }\r | |
1471 | \r | |
1472 | PyDoc_STRVAR(fromunicode_doc,\r | |
1473 | "fromunicode(ustr)\n\\r | |
1474 | \n\\r | |
1475 | Extends this array with data from the unicode string ustr.\n\\r | |
1476 | The array must be a type 'u' array; otherwise a ValueError\n\\r | |
1477 | is raised. Use array.fromstring(ustr.decode(...)) to\n\\r | |
1478 | append Unicode data to an array of some other type.");\r | |
1479 | \r | |
1480 | \r | |
1481 | static PyObject *\r | |
1482 | array_tounicode(arrayobject *self, PyObject *unused)\r | |
1483 | {\r | |
1484 | if (self->ob_descr->typecode != 'u') {\r | |
1485 | PyErr_SetString(PyExc_ValueError,\r | |
1486 | "tounicode() may only be called on type 'u' arrays");\r | |
1487 | return NULL;\r | |
1488 | }\r | |
1489 | return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_SIZE(self));\r | |
1490 | }\r | |
1491 | \r | |
1492 | PyDoc_STRVAR(tounicode_doc,\r | |
1493 | "tounicode() -> unicode\n\\r | |
1494 | \n\\r | |
1495 | Convert the array to a unicode string. The array must be\n\\r | |
1496 | a type 'u' array; otherwise a ValueError is raised. Use\n\\r | |
1497 | array.tostring().decode() to obtain a unicode string from\n\\r | |
1498 | an array of some other type.");\r | |
1499 | \r | |
1500 | #endif /* Py_USING_UNICODE */\r | |
1501 | \r | |
1502 | static PyObject *\r | |
1503 | array_reduce(arrayobject *array)\r | |
1504 | {\r | |
1505 | PyObject *dict, *result, *list;\r | |
1506 | \r | |
1507 | dict = PyObject_GetAttrString((PyObject *)array, "__dict__");\r | |
1508 | if (dict == NULL) {\r | |
1509 | if (!PyErr_ExceptionMatches(PyExc_AttributeError))\r | |
1510 | return NULL;\r | |
1511 | PyErr_Clear();\r | |
1512 | dict = Py_None;\r | |
1513 | Py_INCREF(dict);\r | |
1514 | }\r | |
1515 | /* Unlike in Python 3.x, we never use the more efficient memory\r | |
1516 | * representation of an array for pickling. This is unfortunately\r | |
1517 | * necessary to allow array objects to be unpickled by Python 3.x,\r | |
1518 | * since str objects from 2.x are always decoded to unicode in\r | |
1519 | * Python 3.x.\r | |
1520 | */\r | |
1521 | list = array_tolist(array, NULL);\r | |
1522 | if (list == NULL) {\r | |
1523 | Py_DECREF(dict);\r | |
1524 | return NULL;\r | |
1525 | }\r | |
1526 | result = Py_BuildValue(\r | |
1527 | "O(cO)O", Py_TYPE(array), array->ob_descr->typecode, list, dict);\r | |
1528 | Py_DECREF(list);\r | |
1529 | Py_DECREF(dict);\r | |
1530 | return result;\r | |
1531 | }\r | |
1532 | \r | |
1533 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");\r | |
1534 | \r | |
1535 | static PyObject *\r | |
1536 | array_sizeof(arrayobject *self, PyObject *unused)\r | |
1537 | {\r | |
1538 | Py_ssize_t res;\r | |
1539 | res = sizeof(arrayobject) + self->allocated * self->ob_descr->itemsize;\r | |
1540 | return PyLong_FromSsize_t(res);\r | |
1541 | }\r | |
1542 | \r | |
1543 | PyDoc_STRVAR(sizeof_doc,\r | |
1544 | "__sizeof__() -> int\n\\r | |
1545 | \n\\r | |
1546 | Size of the array in memory, in bytes.");\r | |
1547 | \r | |
1548 | static PyObject *\r | |
1549 | array_get_typecode(arrayobject *a, void *closure)\r | |
1550 | {\r | |
1551 | char tc = a->ob_descr->typecode;\r | |
1552 | return PyString_FromStringAndSize(&tc, 1);\r | |
1553 | }\r | |
1554 | \r | |
1555 | static PyObject *\r | |
1556 | array_get_itemsize(arrayobject *a, void *closure)\r | |
1557 | {\r | |
1558 | return PyInt_FromLong((long)a->ob_descr->itemsize);\r | |
1559 | }\r | |
1560 | \r | |
1561 | static PyGetSetDef array_getsets [] = {\r | |
1562 | {"typecode", (getter) array_get_typecode, NULL,\r | |
1563 | "the typecode character used to create the array"},\r | |
1564 | {"itemsize", (getter) array_get_itemsize, NULL,\r | |
1565 | "the size, in bytes, of one array item"},\r | |
1566 | {NULL}\r | |
1567 | };\r | |
1568 | \r | |
1569 | static PyMethodDef array_methods[] = {\r | |
1570 | {"append", (PyCFunction)array_append, METH_O,\r | |
1571 | append_doc},\r | |
1572 | {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS,\r | |
1573 | buffer_info_doc},\r | |
1574 | {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,\r | |
1575 | byteswap_doc},\r | |
1576 | {"__copy__", (PyCFunction)array_copy, METH_NOARGS,\r | |
1577 | copy_doc},\r | |
1578 | {"count", (PyCFunction)array_count, METH_O,\r | |
1579 | count_doc},\r | |
1580 | {"__deepcopy__",(PyCFunction)array_copy, METH_O,\r | |
1581 | copy_doc},\r | |
1582 | {"extend", (PyCFunction)array_extend, METH_O,\r | |
1583 | extend_doc},\r | |
1584 | {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,\r | |
1585 | fromfile_doc},\r | |
1586 | {"fromlist", (PyCFunction)array_fromlist, METH_O,\r | |
1587 | fromlist_doc},\r | |
1588 | {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,\r | |
1589 | fromstring_doc},\r | |
1590 | #ifdef Py_USING_UNICODE\r | |
1591 | {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,\r | |
1592 | fromunicode_doc},\r | |
1593 | #endif\r | |
1594 | {"index", (PyCFunction)array_index, METH_O,\r | |
1595 | index_doc},\r | |
1596 | {"insert", (PyCFunction)array_insert, METH_VARARGS,\r | |
1597 | insert_doc},\r | |
1598 | {"pop", (PyCFunction)array_pop, METH_VARARGS,\r | |
1599 | pop_doc},\r | |
1600 | {"read", (PyCFunction)array_fromfile_as_read, METH_VARARGS,\r | |
1601 | fromfile_doc},\r | |
1602 | {"__reduce__", (PyCFunction)array_reduce, METH_NOARGS,\r | |
1603 | reduce_doc},\r | |
1604 | {"remove", (PyCFunction)array_remove, METH_O,\r | |
1605 | remove_doc},\r | |
1606 | {"reverse", (PyCFunction)array_reverse, METH_NOARGS,\r | |
1607 | reverse_doc},\r | |
1608 | /* {"sort", (PyCFunction)array_sort, METH_VARARGS,\r | |
1609 | sort_doc},*/\r | |
1610 | {"tofile", (PyCFunction)array_tofile, METH_O,\r | |
1611 | tofile_doc},\r | |
1612 | {"tolist", (PyCFunction)array_tolist, METH_NOARGS,\r | |
1613 | tolist_doc},\r | |
1614 | {"tostring", (PyCFunction)array_tostring, METH_NOARGS,\r | |
1615 | tostring_doc},\r | |
1616 | #ifdef Py_USING_UNICODE\r | |
1617 | {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,\r | |
1618 | tounicode_doc},\r | |
1619 | #endif\r | |
1620 | {"write", (PyCFunction)array_tofile_as_write, METH_O,\r | |
1621 | tofile_doc},\r | |
1622 | {"__sizeof__", (PyCFunction)array_sizeof, METH_NOARGS,\r | |
1623 | sizeof_doc},\r | |
1624 | {NULL, NULL} /* sentinel */\r | |
1625 | };\r | |
1626 | \r | |
1627 | static PyObject *\r | |
1628 | array_repr(arrayobject *a)\r | |
1629 | {\r | |
1630 | char buf[256], typecode;\r | |
1631 | PyObject *s, *t, *v = NULL;\r | |
1632 | Py_ssize_t len;\r | |
1633 | \r | |
1634 | len = Py_SIZE(a);\r | |
1635 | typecode = a->ob_descr->typecode;\r | |
1636 | if (len == 0) {\r | |
1637 | PyOS_snprintf(buf, sizeof(buf), "array('%c')", typecode);\r | |
1638 | return PyString_FromString(buf);\r | |
1639 | }\r | |
1640 | \r | |
1641 | if (typecode == 'c')\r | |
1642 | v = array_tostring(a, NULL);\r | |
1643 | #ifdef Py_USING_UNICODE\r | |
1644 | else if (typecode == 'u')\r | |
1645 | v = array_tounicode(a, NULL);\r | |
1646 | #endif\r | |
1647 | else\r | |
1648 | v = array_tolist(a, NULL);\r | |
1649 | t = PyObject_Repr(v);\r | |
1650 | Py_XDECREF(v);\r | |
1651 | \r | |
1652 | PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode);\r | |
1653 | s = PyString_FromString(buf);\r | |
1654 | PyString_ConcatAndDel(&s, t);\r | |
1655 | PyString_ConcatAndDel(&s, PyString_FromString(")"));\r | |
1656 | return s;\r | |
1657 | }\r | |
1658 | \r | |
1659 | static PyObject*\r | |
1660 | array_subscr(arrayobject* self, PyObject* item)\r | |
1661 | {\r | |
1662 | if (PyIndex_Check(item)) {\r | |
1663 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);\r | |
1664 | if (i==-1 && PyErr_Occurred()) {\r | |
1665 | return NULL;\r | |
1666 | }\r | |
1667 | if (i < 0)\r | |
1668 | i += Py_SIZE(self);\r | |
1669 | return array_item(self, i);\r | |
1670 | }\r | |
1671 | else if (PySlice_Check(item)) {\r | |
1672 | Py_ssize_t start, stop, step, slicelength, cur, i;\r | |
1673 | PyObject* result;\r | |
1674 | arrayobject* ar;\r | |
1675 | int itemsize = self->ob_descr->itemsize;\r | |
1676 | \r | |
1677 | if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self),\r | |
1678 | &start, &stop, &step, &slicelength) < 0) {\r | |
1679 | return NULL;\r | |
1680 | }\r | |
1681 | \r | |
1682 | if (slicelength <= 0) {\r | |
1683 | return newarrayobject(&Arraytype, 0, self->ob_descr);\r | |
1684 | }\r | |
1685 | else if (step == 1) {\r | |
1686 | PyObject *result = newarrayobject(&Arraytype,\r | |
1687 | slicelength, self->ob_descr);\r | |
1688 | if (result == NULL)\r | |
1689 | return NULL;\r | |
1690 | memcpy(((arrayobject *)result)->ob_item,\r | |
1691 | self->ob_item + start * itemsize,\r | |
1692 | slicelength * itemsize);\r | |
1693 | return result;\r | |
1694 | }\r | |
1695 | else {\r | |
1696 | result = newarrayobject(&Arraytype, slicelength, self->ob_descr);\r | |
1697 | if (!result) return NULL;\r | |
1698 | \r | |
1699 | ar = (arrayobject*)result;\r | |
1700 | \r | |
1701 | for (cur = start, i = 0; i < slicelength;\r | |
1702 | cur += step, i++) {\r | |
1703 | memcpy(ar->ob_item + i*itemsize,\r | |
1704 | self->ob_item + cur*itemsize,\r | |
1705 | itemsize);\r | |
1706 | }\r | |
1707 | \r | |
1708 | return result;\r | |
1709 | }\r | |
1710 | }\r | |
1711 | else {\r | |
1712 | PyErr_SetString(PyExc_TypeError,\r | |
1713 | "array indices must be integers");\r | |
1714 | return NULL;\r | |
1715 | }\r | |
1716 | }\r | |
1717 | \r | |
1718 | static int\r | |
1719 | array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)\r | |
1720 | {\r | |
1721 | Py_ssize_t start, stop, step, slicelength, needed;\r | |
1722 | arrayobject* other;\r | |
1723 | int itemsize;\r | |
1724 | \r | |
1725 | if (PyIndex_Check(item)) {\r | |
1726 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);\r | |
1727 | \r | |
1728 | if (i == -1 && PyErr_Occurred())\r | |
1729 | return -1;\r | |
1730 | if (i < 0)\r | |
1731 | i += Py_SIZE(self);\r | |
1732 | if (i < 0 || i >= Py_SIZE(self)) {\r | |
1733 | PyErr_SetString(PyExc_IndexError,\r | |
1734 | "array assignment index out of range");\r | |
1735 | return -1;\r | |
1736 | }\r | |
1737 | if (value == NULL) {\r | |
1738 | /* Fall through to slice assignment */\r | |
1739 | start = i;\r | |
1740 | stop = i + 1;\r | |
1741 | step = 1;\r | |
1742 | slicelength = 1;\r | |
1743 | }\r | |
1744 | else\r | |
1745 | return (*self->ob_descr->setitem)(self, i, value);\r | |
1746 | }\r | |
1747 | else if (PySlice_Check(item)) {\r | |
1748 | if (PySlice_GetIndicesEx((PySliceObject *)item,\r | |
1749 | Py_SIZE(self), &start, &stop,\r | |
1750 | &step, &slicelength) < 0) {\r | |
1751 | return -1;\r | |
1752 | }\r | |
1753 | }\r | |
1754 | else {\r | |
1755 | PyErr_SetString(PyExc_TypeError,\r | |
1756 | "array indices must be integer");\r | |
1757 | return -1;\r | |
1758 | }\r | |
1759 | if (value == NULL) {\r | |
1760 | other = NULL;\r | |
1761 | needed = 0;\r | |
1762 | }\r | |
1763 | else if (array_Check(value)) {\r | |
1764 | other = (arrayobject *)value;\r | |
1765 | needed = Py_SIZE(other);\r | |
1766 | if (self == other) {\r | |
1767 | /* Special case "self[i:j] = self" -- copy self first */\r | |
1768 | int ret;\r | |
1769 | value = array_slice(other, 0, needed);\r | |
1770 | if (value == NULL)\r | |
1771 | return -1;\r | |
1772 | ret = array_ass_subscr(self, item, value);\r | |
1773 | Py_DECREF(value);\r | |
1774 | return ret;\r | |
1775 | }\r | |
1776 | if (other->ob_descr != self->ob_descr) {\r | |
1777 | PyErr_BadArgument();\r | |
1778 | return -1;\r | |
1779 | }\r | |
1780 | }\r | |
1781 | else {\r | |
1782 | PyErr_Format(PyExc_TypeError,\r | |
1783 | "can only assign array (not \"%.200s\") to array slice",\r | |
1784 | Py_TYPE(value)->tp_name);\r | |
1785 | return -1;\r | |
1786 | }\r | |
1787 | itemsize = self->ob_descr->itemsize;\r | |
1788 | /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */\r | |
1789 | if ((step > 0 && stop < start) ||\r | |
1790 | (step < 0 && stop > start))\r | |
1791 | stop = start;\r | |
1792 | if (step == 1) {\r | |
1793 | if (slicelength > needed) {\r | |
1794 | memmove(self->ob_item + (start + needed) * itemsize,\r | |
1795 | self->ob_item + stop * itemsize,\r | |
1796 | (Py_SIZE(self) - stop) * itemsize);\r | |
1797 | if (array_resize(self, Py_SIZE(self) +\r | |
1798 | needed - slicelength) < 0)\r | |
1799 | return -1;\r | |
1800 | }\r | |
1801 | else if (slicelength < needed) {\r | |
1802 | if (array_resize(self, Py_SIZE(self) +\r | |
1803 | needed - slicelength) < 0)\r | |
1804 | return -1;\r | |
1805 | memmove(self->ob_item + (start + needed) * itemsize,\r | |
1806 | self->ob_item + stop * itemsize,\r | |
1807 | (Py_SIZE(self) - start - needed) * itemsize);\r | |
1808 | }\r | |
1809 | if (needed > 0)\r | |
1810 | memcpy(self->ob_item + start * itemsize,\r | |
1811 | other->ob_item, needed * itemsize);\r | |
1812 | return 0;\r | |
1813 | }\r | |
1814 | else if (needed == 0) {\r | |
1815 | /* Delete slice */\r | |
1816 | size_t cur;\r | |
1817 | Py_ssize_t i;\r | |
1818 | \r | |
1819 | if (step < 0) {\r | |
1820 | stop = start + 1;\r | |
1821 | start = stop + step * (slicelength - 1) - 1;\r | |
1822 | step = -step;\r | |
1823 | }\r | |
1824 | for (cur = start, i = 0; i < slicelength;\r | |
1825 | cur += step, i++) {\r | |
1826 | Py_ssize_t lim = step - 1;\r | |
1827 | \r | |
1828 | if (cur + step >= (size_t)Py_SIZE(self))\r | |
1829 | lim = Py_SIZE(self) - cur - 1;\r | |
1830 | memmove(self->ob_item + (cur - i) * itemsize,\r | |
1831 | self->ob_item + (cur + 1) * itemsize,\r | |
1832 | lim * itemsize);\r | |
1833 | }\r | |
1834 | cur = start + slicelength * step;\r | |
1835 | if (cur < (size_t)Py_SIZE(self)) {\r | |
1836 | memmove(self->ob_item + (cur-slicelength) * itemsize,\r | |
1837 | self->ob_item + cur * itemsize,\r | |
1838 | (Py_SIZE(self) - cur) * itemsize);\r | |
1839 | }\r | |
1840 | if (array_resize(self, Py_SIZE(self) - slicelength) < 0)\r | |
1841 | return -1;\r | |
1842 | return 0;\r | |
1843 | }\r | |
1844 | else {\r | |
1845 | Py_ssize_t cur, i;\r | |
1846 | \r | |
1847 | if (needed != slicelength) {\r | |
1848 | PyErr_Format(PyExc_ValueError,\r | |
1849 | "attempt to assign array of size %zd "\r | |
1850 | "to extended slice of size %zd",\r | |
1851 | needed, slicelength);\r | |
1852 | return -1;\r | |
1853 | }\r | |
1854 | for (cur = start, i = 0; i < slicelength;\r | |
1855 | cur += step, i++) {\r | |
1856 | memcpy(self->ob_item + cur * itemsize,\r | |
1857 | other->ob_item + i * itemsize,\r | |
1858 | itemsize);\r | |
1859 | }\r | |
1860 | return 0;\r | |
1861 | }\r | |
1862 | }\r | |
1863 | \r | |
1864 | static PyMappingMethods array_as_mapping = {\r | |
1865 | (lenfunc)array_length,\r | |
1866 | (binaryfunc)array_subscr,\r | |
1867 | (objobjargproc)array_ass_subscr\r | |
1868 | };\r | |
1869 | \r | |
1870 | static const void *emptybuf = "";\r | |
1871 | \r | |
1872 | static Py_ssize_t\r | |
1873 | array_buffer_getreadbuf(arrayobject *self, Py_ssize_t index, const void **ptr)\r | |
1874 | {\r | |
1875 | if ( index != 0 ) {\r | |
1876 | PyErr_SetString(PyExc_SystemError,\r | |
1877 | "Accessing non-existent array segment");\r | |
1878 | return -1;\r | |
1879 | }\r | |
1880 | *ptr = (void *)self->ob_item;\r | |
1881 | if (*ptr == NULL)\r | |
1882 | *ptr = emptybuf;\r | |
1883 | return Py_SIZE(self)*self->ob_descr->itemsize;\r | |
1884 | }\r | |
1885 | \r | |
1886 | static Py_ssize_t\r | |
1887 | array_buffer_getwritebuf(arrayobject *self, Py_ssize_t index, const void **ptr)\r | |
1888 | {\r | |
1889 | if ( index != 0 ) {\r | |
1890 | PyErr_SetString(PyExc_SystemError,\r | |
1891 | "Accessing non-existent array segment");\r | |
1892 | return -1;\r | |
1893 | }\r | |
1894 | *ptr = (void *)self->ob_item;\r | |
1895 | if (*ptr == NULL)\r | |
1896 | *ptr = emptybuf;\r | |
1897 | return Py_SIZE(self)*self->ob_descr->itemsize;\r | |
1898 | }\r | |
1899 | \r | |
1900 | static Py_ssize_t\r | |
1901 | array_buffer_getsegcount(arrayobject *self, Py_ssize_t *lenp)\r | |
1902 | {\r | |
1903 | if ( lenp )\r | |
1904 | *lenp = Py_SIZE(self)*self->ob_descr->itemsize;\r | |
1905 | return 1;\r | |
1906 | }\r | |
1907 | \r | |
1908 | static PySequenceMethods array_as_sequence = {\r | |
1909 | (lenfunc)array_length, /*sq_length*/\r | |
1910 | (binaryfunc)array_concat, /*sq_concat*/\r | |
1911 | (ssizeargfunc)array_repeat, /*sq_repeat*/\r | |
1912 | (ssizeargfunc)array_item, /*sq_item*/\r | |
1913 | (ssizessizeargfunc)array_slice, /*sq_slice*/\r | |
1914 | (ssizeobjargproc)array_ass_item, /*sq_ass_item*/\r | |
1915 | (ssizessizeobjargproc)array_ass_slice, /*sq_ass_slice*/\r | |
1916 | (objobjproc)array_contains, /*sq_contains*/\r | |
1917 | (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/\r | |
1918 | (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/\r | |
1919 | };\r | |
1920 | \r | |
1921 | static PyBufferProcs array_as_buffer = {\r | |
1922 | (readbufferproc)array_buffer_getreadbuf,\r | |
1923 | (writebufferproc)array_buffer_getwritebuf,\r | |
1924 | (segcountproc)array_buffer_getsegcount,\r | |
1925 | NULL,\r | |
1926 | };\r | |
1927 | \r | |
1928 | static PyObject *\r | |
1929 | array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)\r | |
1930 | {\r | |
1931 | char c;\r | |
1932 | PyObject *initial = NULL, *it = NULL;\r | |
1933 | struct arraydescr *descr;\r | |
1934 | \r | |
1935 | if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds))\r | |
1936 | return NULL;\r | |
1937 | \r | |
1938 | if (!PyArg_ParseTuple(args, "c|O:array", &c, &initial))\r | |
1939 | return NULL;\r | |
1940 | \r | |
1941 | if (!(initial == NULL || PyList_Check(initial)\r | |
1942 | || PyString_Check(initial) || PyTuple_Check(initial)\r | |
1943 | || (c == 'u' && PyUnicode_Check(initial)))) {\r | |
1944 | it = PyObject_GetIter(initial);\r | |
1945 | if (it == NULL)\r | |
1946 | return NULL;\r | |
1947 | /* We set initial to NULL so that the subsequent code\r | |
1948 | will create an empty array of the appropriate type\r | |
1949 | and afterwards we can use array_iter_extend to populate\r | |
1950 | the array.\r | |
1951 | */\r | |
1952 | initial = NULL;\r | |
1953 | }\r | |
1954 | for (descr = descriptors; descr->typecode != '\0'; descr++) {\r | |
1955 | if (descr->typecode == c) {\r | |
1956 | PyObject *a;\r | |
1957 | Py_ssize_t len;\r | |
1958 | \r | |
1959 | if (initial == NULL || !(PyList_Check(initial)\r | |
1960 | || PyTuple_Check(initial)))\r | |
1961 | len = 0;\r | |
1962 | else\r | |
1963 | len = PySequence_Size(initial);\r | |
1964 | \r | |
1965 | a = newarrayobject(type, len, descr);\r | |
1966 | if (a == NULL)\r | |
1967 | return NULL;\r | |
1968 | \r | |
1969 | if (len > 0) {\r | |
1970 | Py_ssize_t i;\r | |
1971 | for (i = 0; i < len; i++) {\r | |
1972 | PyObject *v =\r | |
1973 | PySequence_GetItem(initial, i);\r | |
1974 | if (v == NULL) {\r | |
1975 | Py_DECREF(a);\r | |
1976 | return NULL;\r | |
1977 | }\r | |
1978 | if (setarrayitem(a, i, v) != 0) {\r | |
1979 | Py_DECREF(v);\r | |
1980 | Py_DECREF(a);\r | |
1981 | return NULL;\r | |
1982 | }\r | |
1983 | Py_DECREF(v);\r | |
1984 | }\r | |
1985 | } else if (initial != NULL && PyString_Check(initial)) {\r | |
1986 | PyObject *t_initial, *v;\r | |
1987 | t_initial = PyTuple_Pack(1, initial);\r | |
1988 | if (t_initial == NULL) {\r | |
1989 | Py_DECREF(a);\r | |
1990 | return NULL;\r | |
1991 | }\r | |
1992 | v = array_fromstring((arrayobject *)a,\r | |
1993 | t_initial);\r | |
1994 | Py_DECREF(t_initial);\r | |
1995 | if (v == NULL) {\r | |
1996 | Py_DECREF(a);\r | |
1997 | return NULL;\r | |
1998 | }\r | |
1999 | Py_DECREF(v);\r | |
2000 | #ifdef Py_USING_UNICODE\r | |
2001 | } else if (initial != NULL && PyUnicode_Check(initial)) {\r | |
2002 | Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial);\r | |
2003 | if (n > 0) {\r | |
2004 | arrayobject *self = (arrayobject *)a;\r | |
2005 | char *item = self->ob_item;\r | |
2006 | item = (char *)PyMem_Realloc(item, n);\r | |
2007 | if (item == NULL) {\r | |
2008 | PyErr_NoMemory();\r | |
2009 | Py_DECREF(a);\r | |
2010 | return NULL;\r | |
2011 | }\r | |
2012 | self->ob_item = item;\r | |
2013 | Py_SIZE(self) = n / sizeof(Py_UNICODE);\r | |
2014 | memcpy(item, PyUnicode_AS_DATA(initial), n);\r | |
2015 | self->allocated = Py_SIZE(self);\r | |
2016 | }\r | |
2017 | #endif\r | |
2018 | }\r | |
2019 | if (it != NULL) {\r | |
2020 | if (array_iter_extend((arrayobject *)a, it) == -1) {\r | |
2021 | Py_DECREF(it);\r | |
2022 | Py_DECREF(a);\r | |
2023 | return NULL;\r | |
2024 | }\r | |
2025 | Py_DECREF(it);\r | |
2026 | }\r | |
2027 | return a;\r | |
2028 | }\r | |
2029 | }\r | |
2030 | PyErr_SetString(PyExc_ValueError,\r | |
2031 | "bad typecode (must be c, b, B, u, h, H, i, I, l, L, f or d)");\r | |
2032 | return NULL;\r | |
2033 | }\r | |
2034 | \r | |
2035 | \r | |
2036 | PyDoc_STRVAR(module_doc,\r | |
2037 | "This module defines an object type which can efficiently represent\n\\r | |
2038 | an array of basic values: characters, integers, floating point\n\\r | |
2039 | numbers. Arrays are sequence types and behave very much like lists,\n\\r | |
2040 | except that the type of objects stored in them is constrained. The\n\\r | |
2041 | type is specified at object creation time by using a type code, which\n\\r | |
2042 | is a single character. The following type codes are defined:\n\\r | |
2043 | \n\\r | |
2044 | Type code C Type Minimum size in bytes \n\\r | |
2045 | 'c' character 1 \n\\r | |
2046 | 'b' signed integer 1 \n\\r | |
2047 | 'B' unsigned integer 1 \n\\r | |
2048 | 'u' Unicode character 2 \n\\r | |
2049 | 'h' signed integer 2 \n\\r | |
2050 | 'H' unsigned integer 2 \n\\r | |
2051 | 'i' signed integer 2 \n\\r | |
2052 | 'I' unsigned integer 2 \n\\r | |
2053 | 'l' signed integer 4 \n\\r | |
2054 | 'L' unsigned integer 4 \n\\r | |
2055 | 'f' floating point 4 \n\\r | |
2056 | 'd' floating point 8 \n\\r | |
2057 | \n\\r | |
2058 | The constructor is:\n\\r | |
2059 | \n\\r | |
2060 | array(typecode [, initializer]) -- create a new array\n\\r | |
2061 | ");\r | |
2062 | \r | |
2063 | PyDoc_STRVAR(arraytype_doc,\r | |
2064 | "array(typecode [, initializer]) -> array\n\\r | |
2065 | \n\\r | |
2066 | Return a new array whose items are restricted by typecode, and\n\\r | |
2067 | initialized from the optional initializer value, which must be a list,\n\\r | |
2068 | string or iterable over elements of the appropriate type.\n\\r | |
2069 | \n\\r | |
2070 | Arrays represent basic values and behave very much like lists, except\n\\r | |
2071 | the type of objects stored in them is constrained.\n\\r | |
2072 | \n\\r | |
2073 | Methods:\n\\r | |
2074 | \n\\r | |
2075 | append() -- append a new item to the end of the array\n\\r | |
2076 | buffer_info() -- return information giving the current memory info\n\\r | |
2077 | byteswap() -- byteswap all the items of the array\n\\r | |
2078 | count() -- return number of occurrences of an object\n\\r | |
2079 | extend() -- extend array by appending multiple elements from an iterable\n\\r | |
2080 | fromfile() -- read items from a file object\n\\r | |
2081 | fromlist() -- append items from the list\n\\r | |
2082 | fromstring() -- append items from the string\n\\r | |
2083 | index() -- return index of first occurrence of an object\n\\r | |
2084 | insert() -- insert a new item into the array at a provided position\n\\r | |
2085 | pop() -- remove and return item (default last)\n\\r | |
2086 | read() -- DEPRECATED, use fromfile()\n\\r | |
2087 | remove() -- remove first occurrence of an object\n\\r | |
2088 | reverse() -- reverse the order of the items in the array\n\\r | |
2089 | tofile() -- write all items to a file object\n\\r | |
2090 | tolist() -- return the array converted to an ordinary list\n\\r | |
2091 | tostring() -- return the array converted to a string\n\\r | |
2092 | write() -- DEPRECATED, use tofile()\n\\r | |
2093 | \n\\r | |
2094 | Attributes:\n\\r | |
2095 | \n\\r | |
2096 | typecode -- the typecode character used to create the array\n\\r | |
2097 | itemsize -- the length in bytes of one array item\n\\r | |
2098 | ");\r | |
2099 | \r | |
2100 | static PyObject *array_iter(arrayobject *ao);\r | |
2101 | \r | |
2102 | static PyTypeObject Arraytype = {\r | |
2103 | PyVarObject_HEAD_INIT(NULL, 0)\r | |
2104 | "array.array",\r | |
2105 | sizeof(arrayobject),\r | |
2106 | 0,\r | |
2107 | (destructor)array_dealloc, /* tp_dealloc */\r | |
2108 | 0, /* tp_print */\r | |
2109 | 0, /* tp_getattr */\r | |
2110 | 0, /* tp_setattr */\r | |
2111 | 0, /* tp_compare */\r | |
2112 | (reprfunc)array_repr, /* tp_repr */\r | |
2113 | 0, /* tp_as_number*/\r | |
2114 | &array_as_sequence, /* tp_as_sequence*/\r | |
2115 | &array_as_mapping, /* tp_as_mapping*/\r | |
2116 | 0, /* tp_hash */\r | |
2117 | 0, /* tp_call */\r | |
2118 | 0, /* tp_str */\r | |
2119 | PyObject_GenericGetAttr, /* tp_getattro */\r | |
2120 | 0, /* tp_setattro */\r | |
2121 | &array_as_buffer, /* tp_as_buffer*/\r | |
2122 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */\r | |
2123 | arraytype_doc, /* tp_doc */\r | |
2124 | 0, /* tp_traverse */\r | |
2125 | 0, /* tp_clear */\r | |
2126 | array_richcompare, /* tp_richcompare */\r | |
2127 | offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */\r | |
2128 | (getiterfunc)array_iter, /* tp_iter */\r | |
2129 | 0, /* tp_iternext */\r | |
2130 | array_methods, /* tp_methods */\r | |
2131 | 0, /* tp_members */\r | |
2132 | array_getsets, /* tp_getset */\r | |
2133 | 0, /* tp_base */\r | |
2134 | 0, /* tp_dict */\r | |
2135 | 0, /* tp_descr_get */\r | |
2136 | 0, /* tp_descr_set */\r | |
2137 | 0, /* tp_dictoffset */\r | |
2138 | 0, /* tp_init */\r | |
2139 | PyType_GenericAlloc, /* tp_alloc */\r | |
2140 | array_new, /* tp_new */\r | |
2141 | PyObject_Del, /* tp_free */\r | |
2142 | };\r | |
2143 | \r | |
2144 | \r | |
2145 | /*********************** Array Iterator **************************/\r | |
2146 | \r | |
2147 | typedef struct {\r | |
2148 | PyObject_HEAD\r | |
2149 | Py_ssize_t index;\r | |
2150 | arrayobject *ao;\r | |
2151 | PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);\r | |
2152 | } arrayiterobject;\r | |
2153 | \r | |
2154 | static PyTypeObject PyArrayIter_Type;\r | |
2155 | \r | |
2156 | #define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)\r | |
2157 | \r | |
2158 | static PyObject *\r | |
2159 | array_iter(arrayobject *ao)\r | |
2160 | {\r | |
2161 | arrayiterobject *it;\r | |
2162 | \r | |
2163 | if (!array_Check(ao)) {\r | |
2164 | PyErr_BadInternalCall();\r | |
2165 | return NULL;\r | |
2166 | }\r | |
2167 | \r | |
2168 | it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);\r | |
2169 | if (it == NULL)\r | |
2170 | return NULL;\r | |
2171 | \r | |
2172 | Py_INCREF(ao);\r | |
2173 | it->ao = ao;\r | |
2174 | it->index = 0;\r | |
2175 | it->getitem = ao->ob_descr->getitem;\r | |
2176 | PyObject_GC_Track(it);\r | |
2177 | return (PyObject *)it;\r | |
2178 | }\r | |
2179 | \r | |
2180 | static PyObject *\r | |
2181 | arrayiter_next(arrayiterobject *it)\r | |
2182 | {\r | |
2183 | assert(PyArrayIter_Check(it));\r | |
2184 | if (it->index < Py_SIZE(it->ao))\r | |
2185 | return (*it->getitem)(it->ao, it->index++);\r | |
2186 | return NULL;\r | |
2187 | }\r | |
2188 | \r | |
2189 | static void\r | |
2190 | arrayiter_dealloc(arrayiterobject *it)\r | |
2191 | {\r | |
2192 | PyObject_GC_UnTrack(it);\r | |
2193 | Py_XDECREF(it->ao);\r | |
2194 | PyObject_GC_Del(it);\r | |
2195 | }\r | |
2196 | \r | |
2197 | static int\r | |
2198 | arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)\r | |
2199 | {\r | |
2200 | Py_VISIT(it->ao);\r | |
2201 | return 0;\r | |
2202 | }\r | |
2203 | \r | |
2204 | static PyTypeObject PyArrayIter_Type = {\r | |
2205 | PyVarObject_HEAD_INIT(NULL, 0)\r | |
2206 | "arrayiterator", /* tp_name */\r | |
2207 | sizeof(arrayiterobject), /* tp_basicsize */\r | |
2208 | 0, /* tp_itemsize */\r | |
2209 | /* methods */\r | |
2210 | (destructor)arrayiter_dealloc, /* tp_dealloc */\r | |
2211 | 0, /* tp_print */\r | |
2212 | 0, /* tp_getattr */\r | |
2213 | 0, /* tp_setattr */\r | |
2214 | 0, /* tp_compare */\r | |
2215 | 0, /* tp_repr */\r | |
2216 | 0, /* tp_as_number */\r | |
2217 | 0, /* tp_as_sequence */\r | |
2218 | 0, /* tp_as_mapping */\r | |
2219 | 0, /* tp_hash */\r | |
2220 | 0, /* tp_call */\r | |
2221 | 0, /* tp_str */\r | |
2222 | PyObject_GenericGetAttr, /* tp_getattro */\r | |
2223 | 0, /* tp_setattro */\r | |
2224 | 0, /* tp_as_buffer */\r | |
2225 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */\r | |
2226 | 0, /* tp_doc */\r | |
2227 | (traverseproc)arrayiter_traverse, /* tp_traverse */\r | |
2228 | 0, /* tp_clear */\r | |
2229 | 0, /* tp_richcompare */\r | |
2230 | 0, /* tp_weaklistoffset */\r | |
2231 | PyObject_SelfIter, /* tp_iter */\r | |
2232 | (iternextfunc)arrayiter_next, /* tp_iternext */\r | |
2233 | 0, /* tp_methods */\r | |
2234 | };\r | |
2235 | \r | |
2236 | \r | |
2237 | /*********************** Install Module **************************/\r | |
2238 | \r | |
2239 | /* No functions in array module. */\r | |
2240 | static PyMethodDef a_methods[] = {\r | |
2241 | {NULL, NULL, 0, NULL} /* Sentinel */\r | |
2242 | };\r | |
2243 | \r | |
2244 | \r | |
2245 | PyMODINIT_FUNC\r | |
2246 | initarray(void)\r | |
2247 | {\r | |
2248 | PyObject *m;\r | |
2249 | \r | |
2250 | Arraytype.ob_type = &PyType_Type;\r | |
2251 | PyArrayIter_Type.ob_type = &PyType_Type;\r | |
2252 | m = Py_InitModule3("array", a_methods, module_doc);\r | |
2253 | if (m == NULL)\r | |
2254 | return;\r | |
2255 | \r | |
2256 | Py_INCREF((PyObject *)&Arraytype);\r | |
2257 | PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);\r | |
2258 | Py_INCREF((PyObject *)&Arraytype);\r | |
2259 | PyModule_AddObject(m, "array", (PyObject *)&Arraytype);\r | |
2260 | /* No need to check the error here, the caller will do that */\r | |
2261 | }\r |