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