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