Mercurial > cpython-lru_cache-weakref
comparison Modules/arraymodule.c @ 42334:e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
................
r56477 | martin.v.loewis | 2007-07-21 09:04:38 +0200 (Sa, 21 Jul 2007) | 11 lines
Merged revisions 56466-56476 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r56476 | martin.v.loewis | 2007-07-21 08:55:02 +0200 (Sa, 21 Jul 2007) | 4 lines
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
backwards compatibility. Add Py_Refcnt, Py_Type, Py_Size, and
PyVarObject_HEAD_INIT.
........
................
r56478 | martin.v.loewis | 2007-07-21 09:47:23 +0200 (Sa, 21 Jul 2007) | 2 lines
PEP 3123: Use proper C inheritance for PyObject.
................
r56479 | martin.v.loewis | 2007-07-21 10:06:55 +0200 (Sa, 21 Jul 2007) | 3 lines
Add longintrepr.h to Python.h, so that the compiler can
see that PyFalse is really some kind of PyObject*.
................
r56480 | martin.v.loewis | 2007-07-21 10:47:18 +0200 (Sa, 21 Jul 2007) | 2 lines
Qualify SHIFT, MASK, BASE.
................
r56482 | martin.v.loewis | 2007-07-21 19:10:57 +0200 (Sa, 21 Jul 2007) | 2 lines
Correctly refer to _ob_next.
................
| author | Martin v. Löwis <martin@v.loewis.de> |
|---|---|
| date | Sat, 21 Jul 2007 17:22:18 +0000 |
| parents | d0719406b2c6 |
| children | c8b850b5a066 |
comparison
equal
deleted
inserted
replaced
| 42332:2be251dd2cbd | 42334:e70cdce0e8b2 |
|---|---|
| 37 } arrayobject; | 37 } arrayobject; |
| 38 | 38 |
| 39 static PyTypeObject Arraytype; | 39 static PyTypeObject Arraytype; |
| 40 | 40 |
| 41 #define array_Check(op) PyObject_TypeCheck(op, &Arraytype) | 41 #define array_Check(op) PyObject_TypeCheck(op, &Arraytype) |
| 42 #define array_CheckExact(op) ((op)->ob_type == &Arraytype) | 42 #define array_CheckExact(op) (Py_Type(op) == &Arraytype) |
| 43 | 43 |
| 44 static int | 44 static int |
| 45 array_resize(arrayobject *self, Py_ssize_t newsize) | 45 array_resize(arrayobject *self, Py_ssize_t newsize) |
| 46 { | 46 { |
| 47 char *items; | 47 char *items; |
| 51 to accommodate the newsize. If the newsize is 16 smaller than the | 51 to accommodate the newsize. If the newsize is 16 smaller than the |
| 52 current size, then proceed with the realloc() to shrink the list. | 52 current size, then proceed with the realloc() to shrink the list. |
| 53 */ | 53 */ |
| 54 | 54 |
| 55 if (self->allocated >= newsize && | 55 if (self->allocated >= newsize && |
| 56 self->ob_size < newsize + 16 && | 56 Py_Size(self) < newsize + 16 && |
| 57 self->ob_item != NULL) { | 57 self->ob_item != NULL) { |
| 58 self->ob_size = newsize; | 58 Py_Size(self) = newsize; |
| 59 return 0; | 59 return 0; |
| 60 } | 60 } |
| 61 | 61 |
| 62 /* This over-allocates proportional to the array size, making room | 62 /* This over-allocates proportional to the array size, making room |
| 63 * for additional growth. The over-allocation is mild, but is | 63 * for additional growth. The over-allocation is mild, but is |
| 69 * grows at a smaller rate so that larger arrays only overallocate | 69 * grows at a smaller rate so that larger arrays only overallocate |
| 70 * by about 1/16th -- this is done because arrays are presumed to be more | 70 * by about 1/16th -- this is done because arrays are presumed to be more |
| 71 * memory critical. | 71 * memory critical. |
| 72 */ | 72 */ |
| 73 | 73 |
| 74 _new_size = (newsize >> 4) + (self->ob_size < 8 ? 3 : 7) + newsize; | 74 _new_size = (newsize >> 4) + (Py_Size(self) < 8 ? 3 : 7) + newsize; |
| 75 items = self->ob_item; | 75 items = self->ob_item; |
| 76 /* XXX The following multiplication and division does not optimize away | 76 /* XXX The following multiplication and division does not optimize away |
| 77 like it does for lists since the size is not known at compile time */ | 77 like it does for lists since the size is not known at compile time */ |
| 78 if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize)) | 78 if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize)) |
| 79 PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize)); | 79 PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize)); |
| 82 if (items == NULL) { | 82 if (items == NULL) { |
| 83 PyErr_NoMemory(); | 83 PyErr_NoMemory(); |
| 84 return -1; | 84 return -1; |
| 85 } | 85 } |
| 86 self->ob_item = items; | 86 self->ob_item = items; |
| 87 self->ob_size = newsize; | 87 Py_Size(self) = newsize; |
| 88 self->allocated = _new_size; | 88 self->allocated = _new_size; |
| 89 return 0; | 89 return 0; |
| 90 } | 90 } |
| 91 | 91 |
| 92 /**************************************************************************** | 92 /**************************************************************************** |
| 408 } | 408 } |
| 409 op = (arrayobject *) type->tp_alloc(type, 0); | 409 op = (arrayobject *) type->tp_alloc(type, 0); |
| 410 if (op == NULL) { | 410 if (op == NULL) { |
| 411 return NULL; | 411 return NULL; |
| 412 } | 412 } |
| 413 op->ob_size = size; | 413 Py_Size(op) = size; |
| 414 if (size <= 0) { | 414 if (size <= 0) { |
| 415 op->ob_item = NULL; | 415 op->ob_item = NULL; |
| 416 } | 416 } |
| 417 else { | 417 else { |
| 418 op->ob_item = PyMem_NEW(char, nbytes); | 418 op->ob_item = PyMem_NEW(char, nbytes); |
| 431 getarrayitem(PyObject *op, Py_ssize_t i) | 431 getarrayitem(PyObject *op, Py_ssize_t i) |
| 432 { | 432 { |
| 433 register arrayobject *ap; | 433 register arrayobject *ap; |
| 434 assert(array_Check(op)); | 434 assert(array_Check(op)); |
| 435 ap = (arrayobject *)op; | 435 ap = (arrayobject *)op; |
| 436 assert(i>=0 && i<ap->ob_size); | 436 assert(i>=0 && i<Py_Size(ap)); |
| 437 return (*ap->ob_descr->getitem)(ap, i); | 437 return (*ap->ob_descr->getitem)(ap, i); |
| 438 } | 438 } |
| 439 | 439 |
| 440 static int | 440 static int |
| 441 ins1(arrayobject *self, Py_ssize_t where, PyObject *v) | 441 ins1(arrayobject *self, Py_ssize_t where, PyObject *v) |
| 442 { | 442 { |
| 443 char *items; | 443 char *items; |
| 444 Py_ssize_t n = self->ob_size; | 444 Py_ssize_t n = Py_Size(self); |
| 445 if (v == NULL) { | 445 if (v == NULL) { |
| 446 PyErr_BadInternalCall(); | 446 PyErr_BadInternalCall(); |
| 447 return -1; | 447 return -1; |
| 448 } | 448 } |
| 449 if ((*self->ob_descr->setitem)(self, -1, v) < 0) | 449 if ((*self->ob_descr->setitem)(self, -1, v) < 0) |
| 474 { | 474 { |
| 475 if (op->weakreflist != NULL) | 475 if (op->weakreflist != NULL) |
| 476 PyObject_ClearWeakRefs((PyObject *) op); | 476 PyObject_ClearWeakRefs((PyObject *) op); |
| 477 if (op->ob_item != NULL) | 477 if (op->ob_item != NULL) |
| 478 PyMem_DEL(op->ob_item); | 478 PyMem_DEL(op->ob_item); |
| 479 op->ob_type->tp_free((PyObject *)op); | 479 Py_Type(op)->tp_free((PyObject *)op); |
| 480 } | 480 } |
| 481 | 481 |
| 482 static PyObject * | 482 static PyObject * |
| 483 array_richcompare(PyObject *v, PyObject *w, int op) | 483 array_richcompare(PyObject *v, PyObject *w, int op) |
| 484 { | 484 { |
| 494 } | 494 } |
| 495 | 495 |
| 496 va = (arrayobject *)v; | 496 va = (arrayobject *)v; |
| 497 wa = (arrayobject *)w; | 497 wa = (arrayobject *)w; |
| 498 | 498 |
| 499 if (va->ob_size != wa->ob_size && (op == Py_EQ || op == Py_NE)) { | 499 if (Py_Size(va) != Py_Size(wa) && (op == Py_EQ || op == Py_NE)) { |
| 500 /* Shortcut: if the lengths differ, the arrays differ */ | 500 /* Shortcut: if the lengths differ, the arrays differ */ |
| 501 if (op == Py_EQ) | 501 if (op == Py_EQ) |
| 502 res = Py_False; | 502 res = Py_False; |
| 503 else | 503 else |
| 504 res = Py_True; | 504 res = Py_True; |
| 506 return res; | 506 return res; |
| 507 } | 507 } |
| 508 | 508 |
| 509 /* Search for the first index where items are different */ | 509 /* Search for the first index where items are different */ |
| 510 k = 1; | 510 k = 1; |
| 511 for (i = 0; i < va->ob_size && i < wa->ob_size; i++) { | 511 for (i = 0; i < Py_Size(va) && i < Py_Size(wa); i++) { |
| 512 vi = getarrayitem(v, i); | 512 vi = getarrayitem(v, i); |
| 513 wi = getarrayitem(w, i); | 513 wi = getarrayitem(w, i); |
| 514 if (vi == NULL || wi == NULL) { | 514 if (vi == NULL || wi == NULL) { |
| 515 Py_XDECREF(vi); | 515 Py_XDECREF(vi); |
| 516 Py_XDECREF(wi); | 516 Py_XDECREF(wi); |
| 525 return NULL; | 525 return NULL; |
| 526 } | 526 } |
| 527 | 527 |
| 528 if (k) { | 528 if (k) { |
| 529 /* No more items to compare -- compare sizes */ | 529 /* No more items to compare -- compare sizes */ |
| 530 Py_ssize_t vs = va->ob_size; | 530 Py_ssize_t vs = Py_Size(va); |
| 531 Py_ssize_t ws = wa->ob_size; | 531 Py_ssize_t ws = Py_Size(wa); |
| 532 int cmp; | 532 int cmp; |
| 533 switch (op) { | 533 switch (op) { |
| 534 case Py_LT: cmp = vs < ws; break; | 534 case Py_LT: cmp = vs < ws; break; |
| 535 case Py_LE: cmp = vs <= ws; break; | 535 case Py_LE: cmp = vs <= ws; break; |
| 536 case Py_EQ: cmp = vs == ws; break; | 536 case Py_EQ: cmp = vs == ws; break; |
| 566 } | 566 } |
| 567 | 567 |
| 568 static Py_ssize_t | 568 static Py_ssize_t |
| 569 array_length(arrayobject *a) | 569 array_length(arrayobject *a) |
| 570 { | 570 { |
| 571 return a->ob_size; | 571 return Py_Size(a); |
| 572 } | 572 } |
| 573 | 573 |
| 574 static PyObject * | 574 static PyObject * |
| 575 array_item(arrayobject *a, Py_ssize_t i) | 575 array_item(arrayobject *a, Py_ssize_t i) |
| 576 { | 576 { |
| 577 if (i < 0 || i >= a->ob_size) { | 577 if (i < 0 || i >= Py_Size(a)) { |
| 578 PyErr_SetString(PyExc_IndexError, "array index out of range"); | 578 PyErr_SetString(PyExc_IndexError, "array index out of range"); |
| 579 return NULL; | 579 return NULL; |
| 580 } | 580 } |
| 581 return getarrayitem((PyObject *)a, i); | 581 return getarrayitem((PyObject *)a, i); |
| 582 } | 582 } |
| 585 array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh) | 585 array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh) |
| 586 { | 586 { |
| 587 arrayobject *np; | 587 arrayobject *np; |
| 588 if (ilow < 0) | 588 if (ilow < 0) |
| 589 ilow = 0; | 589 ilow = 0; |
| 590 else if (ilow > a->ob_size) | 590 else if (ilow > Py_Size(a)) |
| 591 ilow = a->ob_size; | 591 ilow = Py_Size(a); |
| 592 if (ihigh < 0) | 592 if (ihigh < 0) |
| 593 ihigh = 0; | 593 ihigh = 0; |
| 594 if (ihigh < ilow) | 594 if (ihigh < ilow) |
| 595 ihigh = ilow; | 595 ihigh = ilow; |
| 596 else if (ihigh > a->ob_size) | 596 else if (ihigh > Py_Size(a)) |
| 597 ihigh = a->ob_size; | 597 ihigh = Py_Size(a); |
| 598 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr); | 598 np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr); |
| 599 if (np == NULL) | 599 if (np == NULL) |
| 600 return NULL; | 600 return NULL; |
| 601 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize, | 601 memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize, |
| 602 (ihigh-ilow) * a->ob_descr->itemsize); | 602 (ihigh-ilow) * a->ob_descr->itemsize); |
| 604 } | 604 } |
| 605 | 605 |
| 606 static PyObject * | 606 static PyObject * |
| 607 array_copy(arrayobject *a, PyObject *unused) | 607 array_copy(arrayobject *a, PyObject *unused) |
| 608 { | 608 { |
| 609 return array_slice(a, 0, a->ob_size); | 609 return array_slice(a, 0, Py_Size(a)); |
| 610 } | 610 } |
| 611 | 611 |
| 612 PyDoc_STRVAR(copy_doc, | 612 PyDoc_STRVAR(copy_doc, |
| 613 "copy(array)\n\ | 613 "copy(array)\n\ |
| 614 \n\ | 614 \n\ |
| 620 Py_ssize_t size; | 620 Py_ssize_t size; |
| 621 arrayobject *np; | 621 arrayobject *np; |
| 622 if (!array_Check(bb)) { | 622 if (!array_Check(bb)) { |
| 623 PyErr_Format(PyExc_TypeError, | 623 PyErr_Format(PyExc_TypeError, |
| 624 "can only append array (not \"%.200s\") to array", | 624 "can only append array (not \"%.200s\") to array", |
| 625 bb->ob_type->tp_name); | 625 Py_Type(bb)->tp_name); |
| 626 return NULL; | 626 return NULL; |
| 627 } | 627 } |
| 628 #define b ((arrayobject *)bb) | 628 #define b ((arrayobject *)bb) |
| 629 if (a->ob_descr != b->ob_descr) { | 629 if (a->ob_descr != b->ob_descr) { |
| 630 PyErr_BadArgument(); | 630 PyErr_BadArgument(); |
| 631 return NULL; | 631 return NULL; |
| 632 } | 632 } |
| 633 size = a->ob_size + b->ob_size; | 633 size = Py_Size(a) + Py_Size(b); |
| 634 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr); | 634 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr); |
| 635 if (np == NULL) { | 635 if (np == NULL) { |
| 636 return NULL; | 636 return NULL; |
| 637 } | 637 } |
| 638 memcpy(np->ob_item, a->ob_item, a->ob_size*a->ob_descr->itemsize); | 638 memcpy(np->ob_item, a->ob_item, Py_Size(a)*a->ob_descr->itemsize); |
| 639 memcpy(np->ob_item + a->ob_size*a->ob_descr->itemsize, | 639 memcpy(np->ob_item + Py_Size(a)*a->ob_descr->itemsize, |
| 640 b->ob_item, b->ob_size*b->ob_descr->itemsize); | 640 b->ob_item, Py_Size(b)*b->ob_descr->itemsize); |
| 641 return (PyObject *)np; | 641 return (PyObject *)np; |
| 642 #undef b | 642 #undef b |
| 643 } | 643 } |
| 644 | 644 |
| 645 static PyObject * | 645 static PyObject * |
| 650 arrayobject *np; | 650 arrayobject *np; |
| 651 char *p; | 651 char *p; |
| 652 Py_ssize_t nbytes; | 652 Py_ssize_t nbytes; |
| 653 if (n < 0) | 653 if (n < 0) |
| 654 n = 0; | 654 n = 0; |
| 655 size = a->ob_size * n; | 655 size = Py_Size(a) * n; |
| 656 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr); | 656 np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr); |
| 657 if (np == NULL) | 657 if (np == NULL) |
| 658 return NULL; | 658 return NULL; |
| 659 p = np->ob_item; | 659 p = np->ob_item; |
| 660 nbytes = a->ob_size * a->ob_descr->itemsize; | 660 nbytes = Py_Size(a) * a->ob_descr->itemsize; |
| 661 for (i = 0; i < n; i++) { | 661 for (i = 0; i < n; i++) { |
| 662 memcpy(p, a->ob_item, nbytes); | 662 memcpy(p, a->ob_item, nbytes); |
| 663 p += nbytes; | 663 p += nbytes; |
| 664 } | 664 } |
| 665 return (PyObject *) np; | 665 return (PyObject *) np; |
| 673 Py_ssize_t d; /* Change in size */ | 673 Py_ssize_t d; /* Change in size */ |
| 674 #define b ((arrayobject *)v) | 674 #define b ((arrayobject *)v) |
| 675 if (v == NULL) | 675 if (v == NULL) |
| 676 n = 0; | 676 n = 0; |
| 677 else if (array_Check(v)) { | 677 else if (array_Check(v)) { |
| 678 n = b->ob_size; | 678 n = Py_Size(b); |
| 679 if (a == b) { | 679 if (a == b) { |
| 680 /* Special case "a[i:j] = a" -- copy b first */ | 680 /* Special case "a[i:j] = a" -- copy b first */ |
| 681 int ret; | 681 int ret; |
| 682 v = array_slice(b, 0, n); | 682 v = array_slice(b, 0, n); |
| 683 if (!v) | 683 if (!v) |
| 692 } | 692 } |
| 693 } | 693 } |
| 694 else { | 694 else { |
| 695 PyErr_Format(PyExc_TypeError, | 695 PyErr_Format(PyExc_TypeError, |
| 696 "can only assign array (not \"%.200s\") to array slice", | 696 "can only assign array (not \"%.200s\") to array slice", |
| 697 v->ob_type->tp_name); | 697 Py_Type(v)->tp_name); |
| 698 return -1; | 698 return -1; |
| 699 } | 699 } |
| 700 if (ilow < 0) | 700 if (ilow < 0) |
| 701 ilow = 0; | 701 ilow = 0; |
| 702 else if (ilow > a->ob_size) | 702 else if (ilow > Py_Size(a)) |
| 703 ilow = a->ob_size; | 703 ilow = Py_Size(a); |
| 704 if (ihigh < 0) | 704 if (ihigh < 0) |
| 705 ihigh = 0; | 705 ihigh = 0; |
| 706 if (ihigh < ilow) | 706 if (ihigh < ilow) |
| 707 ihigh = ilow; | 707 ihigh = ilow; |
| 708 else if (ihigh > a->ob_size) | 708 else if (ihigh > Py_Size(a)) |
| 709 ihigh = a->ob_size; | 709 ihigh = Py_Size(a); |
| 710 item = a->ob_item; | 710 item = a->ob_item; |
| 711 d = n - (ihigh-ilow); | 711 d = n - (ihigh-ilow); |
| 712 if (d < 0) { /* Delete -d items */ | 712 if (d < 0) { /* Delete -d items */ |
| 713 memmove(item + (ihigh+d)*a->ob_descr->itemsize, | 713 memmove(item + (ihigh+d)*a->ob_descr->itemsize, |
| 714 item + ihigh*a->ob_descr->itemsize, | 714 item + ihigh*a->ob_descr->itemsize, |
| 715 (a->ob_size-ihigh)*a->ob_descr->itemsize); | 715 (Py_Size(a)-ihigh)*a->ob_descr->itemsize); |
| 716 a->ob_size += d; | 716 Py_Size(a) += d; |
| 717 PyMem_RESIZE(item, char, a->ob_size*a->ob_descr->itemsize); | 717 PyMem_RESIZE(item, char, Py_Size(a)*a->ob_descr->itemsize); |
| 718 /* Can't fail */ | 718 /* Can't fail */ |
| 719 a->ob_item = item; | 719 a->ob_item = item; |
| 720 a->allocated = a->ob_size; | 720 a->allocated = Py_Size(a); |
| 721 } | 721 } |
| 722 else if (d > 0) { /* Insert d items */ | 722 else if (d > 0) { /* Insert d items */ |
| 723 PyMem_RESIZE(item, char, | 723 PyMem_RESIZE(item, char, |
| 724 (a->ob_size + d)*a->ob_descr->itemsize); | 724 (Py_Size(a) + d)*a->ob_descr->itemsize); |
| 725 if (item == NULL) { | 725 if (item == NULL) { |
| 726 PyErr_NoMemory(); | 726 PyErr_NoMemory(); |
| 727 return -1; | 727 return -1; |
| 728 } | 728 } |
| 729 memmove(item + (ihigh+d)*a->ob_descr->itemsize, | 729 memmove(item + (ihigh+d)*a->ob_descr->itemsize, |
| 730 item + ihigh*a->ob_descr->itemsize, | 730 item + ihigh*a->ob_descr->itemsize, |
| 731 (a->ob_size-ihigh)*a->ob_descr->itemsize); | 731 (Py_Size(a)-ihigh)*a->ob_descr->itemsize); |
| 732 a->ob_item = item; | 732 a->ob_item = item; |
| 733 a->ob_size += d; | 733 Py_Size(a) += d; |
| 734 a->allocated = a->ob_size; | 734 a->allocated = Py_Size(a); |
| 735 } | 735 } |
| 736 if (n > 0) | 736 if (n > 0) |
| 737 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item, | 737 memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item, |
| 738 n*b->ob_descr->itemsize); | 738 n*b->ob_descr->itemsize); |
| 739 return 0; | 739 return 0; |
| 741 } | 741 } |
| 742 | 742 |
| 743 static int | 743 static int |
| 744 array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v) | 744 array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v) |
| 745 { | 745 { |
| 746 if (i < 0 || i >= a->ob_size) { | 746 if (i < 0 || i >= Py_Size(a)) { |
| 747 PyErr_SetString(PyExc_IndexError, | 747 PyErr_SetString(PyExc_IndexError, |
| 748 "array assignment index out of range"); | 748 "array assignment index out of range"); |
| 749 return -1; | 749 return -1; |
| 750 } | 750 } |
| 751 if (v == NULL) | 751 if (v == NULL) |
| 768 it = PyObject_GetIter(bb); | 768 it = PyObject_GetIter(bb); |
| 769 if (it == NULL) | 769 if (it == NULL) |
| 770 return -1; | 770 return -1; |
| 771 | 771 |
| 772 while ((v = PyIter_Next(it)) != NULL) { | 772 while ((v = PyIter_Next(it)) != NULL) { |
| 773 if (ins1(self, (int) self->ob_size, v) != 0) { | 773 if (ins1(self, (int) Py_Size(self), v) != 0) { |
| 774 Py_DECREF(v); | 774 Py_DECREF(v); |
| 775 Py_DECREF(it); | 775 Py_DECREF(it); |
| 776 return -1; | 776 return -1; |
| 777 } | 777 } |
| 778 Py_DECREF(v); | 778 Py_DECREF(v); |
| 794 if (self->ob_descr != b->ob_descr) { | 794 if (self->ob_descr != b->ob_descr) { |
| 795 PyErr_SetString(PyExc_TypeError, | 795 PyErr_SetString(PyExc_TypeError, |
| 796 "can only extend with array of same kind"); | 796 "can only extend with array of same kind"); |
| 797 return -1; | 797 return -1; |
| 798 } | 798 } |
| 799 size = self->ob_size + b->ob_size; | 799 size = Py_Size(self) + Py_Size(b); |
| 800 PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize); | 800 PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize); |
| 801 if (self->ob_item == NULL) { | 801 if (self->ob_item == NULL) { |
| 802 PyObject_Del(self); | 802 PyObject_Del(self); |
| 803 PyErr_NoMemory(); | 803 PyErr_NoMemory(); |
| 804 return -1; | 804 return -1; |
| 805 } | 805 } |
| 806 memcpy(self->ob_item + self->ob_size*self->ob_descr->itemsize, | 806 memcpy(self->ob_item + Py_Size(self)*self->ob_descr->itemsize, |
| 807 b->ob_item, b->ob_size*b->ob_descr->itemsize); | 807 b->ob_item, Py_Size(b)*b->ob_descr->itemsize); |
| 808 self->ob_size = size; | 808 Py_Size(self) = size; |
| 809 self->allocated = size; | 809 self->allocated = size; |
| 810 | 810 |
| 811 return 0; | 811 return 0; |
| 812 #undef b | 812 #undef b |
| 813 } | 813 } |
| 816 array_inplace_concat(arrayobject *self, PyObject *bb) | 816 array_inplace_concat(arrayobject *self, PyObject *bb) |
| 817 { | 817 { |
| 818 if (!array_Check(bb)) { | 818 if (!array_Check(bb)) { |
| 819 PyErr_Format(PyExc_TypeError, | 819 PyErr_Format(PyExc_TypeError, |
| 820 "can only extend array with array (not \"%.200s\")", | 820 "can only extend array with array (not \"%.200s\")", |
| 821 bb->ob_type->tp_name); | 821 Py_Type(bb)->tp_name); |
| 822 return NULL; | 822 return NULL; |
| 823 } | 823 } |
| 824 if (array_do_extend(self, bb) == -1) | 824 if (array_do_extend(self, bb) == -1) |
| 825 return NULL; | 825 return NULL; |
| 826 Py_INCREF(self); | 826 Py_INCREF(self); |
| 831 array_inplace_repeat(arrayobject *self, Py_ssize_t n) | 831 array_inplace_repeat(arrayobject *self, Py_ssize_t n) |
| 832 { | 832 { |
| 833 char *items, *p; | 833 char *items, *p; |
| 834 Py_ssize_t size, i; | 834 Py_ssize_t size, i; |
| 835 | 835 |
| 836 if (self->ob_size > 0) { | 836 if (Py_Size(self) > 0) { |
| 837 if (n < 0) | 837 if (n < 0) |
| 838 n = 0; | 838 n = 0; |
| 839 items = self->ob_item; | 839 items = self->ob_item; |
| 840 size = self->ob_size * self->ob_descr->itemsize; | 840 size = Py_Size(self) * self->ob_descr->itemsize; |
| 841 if (n == 0) { | 841 if (n == 0) { |
| 842 PyMem_FREE(items); | 842 PyMem_FREE(items); |
| 843 self->ob_item = NULL; | 843 self->ob_item = NULL; |
| 844 self->ob_size = 0; | 844 Py_Size(self) = 0; |
| 845 self->allocated = 0; | 845 self->allocated = 0; |
| 846 } | 846 } |
| 847 else { | 847 else { |
| 848 PyMem_Resize(items, char, n * size); | 848 PyMem_Resize(items, char, n * size); |
| 849 if (items == NULL) | 849 if (items == NULL) |
| 852 for (i = 1; i < n; i++) { | 852 for (i = 1; i < n; i++) { |
| 853 p += size; | 853 p += size; |
| 854 memcpy(p, items, size); | 854 memcpy(p, items, size); |
| 855 } | 855 } |
| 856 self->ob_item = items; | 856 self->ob_item = items; |
| 857 self->ob_size *= n; | 857 Py_Size(self) *= n; |
| 858 self->allocated = self->ob_size; | 858 self->allocated = Py_Size(self); |
| 859 } | 859 } |
| 860 } | 860 } |
| 861 Py_INCREF(self); | 861 Py_INCREF(self); |
| 862 return (PyObject *)self; | 862 return (PyObject *)self; |
| 863 } | 863 } |
| 876 array_count(arrayobject *self, PyObject *v) | 876 array_count(arrayobject *self, PyObject *v) |
| 877 { | 877 { |
| 878 Py_ssize_t count = 0; | 878 Py_ssize_t count = 0; |
| 879 Py_ssize_t i; | 879 Py_ssize_t i; |
| 880 | 880 |
| 881 for (i = 0; i < self->ob_size; i++) { | 881 for (i = 0; i < Py_Size(self); i++) { |
| 882 PyObject *selfi = getarrayitem((PyObject *)self, i); | 882 PyObject *selfi = getarrayitem((PyObject *)self, i); |
| 883 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); | 883 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
| 884 Py_DECREF(selfi); | 884 Py_DECREF(selfi); |
| 885 if (cmp > 0) | 885 if (cmp > 0) |
| 886 count++; | 886 count++; |
| 898 static PyObject * | 898 static PyObject * |
| 899 array_index(arrayobject *self, PyObject *v) | 899 array_index(arrayobject *self, PyObject *v) |
| 900 { | 900 { |
| 901 Py_ssize_t i; | 901 Py_ssize_t i; |
| 902 | 902 |
| 903 for (i = 0; i < self->ob_size; i++) { | 903 for (i = 0; i < Py_Size(self); i++) { |
| 904 PyObject *selfi = getarrayitem((PyObject *)self, i); | 904 PyObject *selfi = getarrayitem((PyObject *)self, i); |
| 905 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); | 905 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
| 906 Py_DECREF(selfi); | 906 Py_DECREF(selfi); |
| 907 if (cmp > 0) { | 907 if (cmp > 0) { |
| 908 return PyInt_FromLong((long)i); | 908 return PyInt_FromLong((long)i); |
| 923 array_contains(arrayobject *self, PyObject *v) | 923 array_contains(arrayobject *self, PyObject *v) |
| 924 { | 924 { |
| 925 Py_ssize_t i; | 925 Py_ssize_t i; |
| 926 int cmp; | 926 int cmp; |
| 927 | 927 |
| 928 for (i = 0, cmp = 0 ; cmp == 0 && i < self->ob_size; i++) { | 928 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_Size(self); i++) { |
| 929 PyObject *selfi = getarrayitem((PyObject *)self, i); | 929 PyObject *selfi = getarrayitem((PyObject *)self, i); |
| 930 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); | 930 cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
| 931 Py_DECREF(selfi); | 931 Py_DECREF(selfi); |
| 932 } | 932 } |
| 933 return cmp; | 933 return cmp; |
| 936 static PyObject * | 936 static PyObject * |
| 937 array_remove(arrayobject *self, PyObject *v) | 937 array_remove(arrayobject *self, PyObject *v) |
| 938 { | 938 { |
| 939 int i; | 939 int i; |
| 940 | 940 |
| 941 for (i = 0; i < self->ob_size; i++) { | 941 for (i = 0; i < Py_Size(self); i++) { |
| 942 PyObject *selfi = getarrayitem((PyObject *)self,i); | 942 PyObject *selfi = getarrayitem((PyObject *)self,i); |
| 943 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); | 943 int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
| 944 Py_DECREF(selfi); | 944 Py_DECREF(selfi); |
| 945 if (cmp > 0) { | 945 if (cmp > 0) { |
| 946 if (array_ass_slice(self, i, i+1, | 946 if (array_ass_slice(self, i, i+1, |
| 966 { | 966 { |
| 967 Py_ssize_t i = -1; | 967 Py_ssize_t i = -1; |
| 968 PyObject *v; | 968 PyObject *v; |
| 969 if (!PyArg_ParseTuple(args, "|n:pop", &i)) | 969 if (!PyArg_ParseTuple(args, "|n:pop", &i)) |
| 970 return NULL; | 970 return NULL; |
| 971 if (self->ob_size == 0) { | 971 if (Py_Size(self) == 0) { |
| 972 /* Special-case most common failure cause */ | 972 /* Special-case most common failure cause */ |
| 973 PyErr_SetString(PyExc_IndexError, "pop from empty array"); | 973 PyErr_SetString(PyExc_IndexError, "pop from empty array"); |
| 974 return NULL; | 974 return NULL; |
| 975 } | 975 } |
| 976 if (i < 0) | 976 if (i < 0) |
| 977 i += self->ob_size; | 977 i += Py_Size(self); |
| 978 if (i < 0 || i >= self->ob_size) { | 978 if (i < 0 || i >= Py_Size(self)) { |
| 979 PyErr_SetString(PyExc_IndexError, "pop index out of range"); | 979 PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
| 980 return NULL; | 980 return NULL; |
| 981 } | 981 } |
| 982 v = getarrayitem((PyObject *)self,i); | 982 v = getarrayitem((PyObject *)self,i); |
| 983 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) { | 983 if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) { |
| 1029 retval = PyTuple_New(2); | 1029 retval = PyTuple_New(2); |
| 1030 if (!retval) | 1030 if (!retval) |
| 1031 return NULL; | 1031 return NULL; |
| 1032 | 1032 |
| 1033 PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item)); | 1033 PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item)); |
| 1034 PyTuple_SET_ITEM(retval, 1, PyInt_FromLong((long)(self->ob_size))); | 1034 PyTuple_SET_ITEM(retval, 1, PyInt_FromLong((long)(Py_Size(self)))); |
| 1035 | 1035 |
| 1036 return retval; | 1036 return retval; |
| 1037 } | 1037 } |
| 1038 | 1038 |
| 1039 PyDoc_STRVAR(buffer_info_doc, | 1039 PyDoc_STRVAR(buffer_info_doc, |
| 1046 | 1046 |
| 1047 | 1047 |
| 1048 static PyObject * | 1048 static PyObject * |
| 1049 array_append(arrayobject *self, PyObject *v) | 1049 array_append(arrayobject *self, PyObject *v) |
| 1050 { | 1050 { |
| 1051 return ins(self, (int) self->ob_size, v); | 1051 return ins(self, (int) Py_Size(self), v); |
| 1052 } | 1052 } |
| 1053 | 1053 |
| 1054 PyDoc_STRVAR(append_doc, | 1054 PyDoc_STRVAR(append_doc, |
| 1055 "append(x)\n\ | 1055 "append(x)\n\ |
| 1056 \n\ | 1056 \n\ |
| 1065 | 1065 |
| 1066 switch (self->ob_descr->itemsize) { | 1066 switch (self->ob_descr->itemsize) { |
| 1067 case 1: | 1067 case 1: |
| 1068 break; | 1068 break; |
| 1069 case 2: | 1069 case 2: |
| 1070 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 2) { | 1070 for (p = self->ob_item, i = Py_Size(self); --i >= 0; p += 2) { |
| 1071 char p0 = p[0]; | 1071 char p0 = p[0]; |
| 1072 p[0] = p[1]; | 1072 p[0] = p[1]; |
| 1073 p[1] = p0; | 1073 p[1] = p0; |
| 1074 } | 1074 } |
| 1075 break; | 1075 break; |
| 1076 case 4: | 1076 case 4: |
| 1077 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 4) { | 1077 for (p = self->ob_item, i = Py_Size(self); --i >= 0; p += 4) { |
| 1078 char p0 = p[0]; | 1078 char p0 = p[0]; |
| 1079 char p1 = p[1]; | 1079 char p1 = p[1]; |
| 1080 p[0] = p[3]; | 1080 p[0] = p[3]; |
| 1081 p[1] = p[2]; | 1081 p[1] = p[2]; |
| 1082 p[2] = p1; | 1082 p[2] = p1; |
| 1083 p[3] = p0; | 1083 p[3] = p0; |
| 1084 } | 1084 } |
| 1085 break; | 1085 break; |
| 1086 case 8: | 1086 case 8: |
| 1087 for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 8) { | 1087 for (p = self->ob_item, i = Py_Size(self); --i >= 0; p += 8) { |
| 1088 char p0 = p[0]; | 1088 char p0 = p[0]; |
| 1089 char p1 = p[1]; | 1089 char p1 = p[1]; |
| 1090 char p2 = p[2]; | 1090 char p2 = p[2]; |
| 1091 char p3 = p[3]; | 1091 char p3 = p[3]; |
| 1092 p[0] = p[7]; | 1092 p[0] = p[7]; |
| 1123 if (dict == NULL) { | 1123 if (dict == NULL) { |
| 1124 PyErr_Clear(); | 1124 PyErr_Clear(); |
| 1125 dict = Py_None; | 1125 dict = Py_None; |
| 1126 Py_INCREF(dict); | 1126 Py_INCREF(dict); |
| 1127 } | 1127 } |
| 1128 if (array->ob_size > 0) { | 1128 if (Py_Size(array) > 0) { |
| 1129 result = Py_BuildValue("O(cy#)O", | 1129 result = Py_BuildValue("O(cy#)O", |
| 1130 array->ob_type, | 1130 Py_Type(array), |
| 1131 array->ob_descr->typecode, | 1131 array->ob_descr->typecode, |
| 1132 array->ob_item, | 1132 array->ob_item, |
| 1133 array->ob_size * array->ob_descr->itemsize, | 1133 Py_Size(array) * array->ob_descr->itemsize, |
| 1134 dict); | 1134 dict); |
| 1135 } else { | 1135 } else { |
| 1136 result = Py_BuildValue("O(c)O", | 1136 result = Py_BuildValue("O(c)O", |
| 1137 array->ob_type, | 1137 Py_Type(array), |
| 1138 array->ob_descr->typecode, | 1138 array->ob_descr->typecode, |
| 1139 dict); | 1139 dict); |
| 1140 } | 1140 } |
| 1141 Py_DECREF(dict); | 1141 Py_DECREF(dict); |
| 1142 return result; | 1142 return result; |
| 1151 register char *p, *q; | 1151 register char *p, *q; |
| 1152 /* little buffer to hold items while swapping */ | 1152 /* little buffer to hold items while swapping */ |
| 1153 char tmp[256]; /* 8 is probably enough -- but why skimp */ | 1153 char tmp[256]; /* 8 is probably enough -- but why skimp */ |
| 1154 assert((size_t)itemsize <= sizeof(tmp)); | 1154 assert((size_t)itemsize <= sizeof(tmp)); |
| 1155 | 1155 |
| 1156 if (self->ob_size > 1) { | 1156 if (Py_Size(self) > 1) { |
| 1157 for (p = self->ob_item, | 1157 for (p = self->ob_item, |
| 1158 q = self->ob_item + (self->ob_size - 1)*itemsize; | 1158 q = self->ob_item + (Py_Size(self) - 1)*itemsize; |
| 1159 p < q; | 1159 p < q; |
| 1160 p += itemsize, q -= itemsize) { | 1160 p += itemsize, q -= itemsize) { |
| 1161 /* memory areas guaranteed disjoint, so memcpy | 1161 /* memory areas guaranteed disjoint, so memcpy |
| 1162 * is safe (& memmove may be slower). | 1162 * is safe (& memmove may be slower). |
| 1163 */ | 1163 */ |
| 1240 /* XXX Make the block size settable */ | 1240 /* XXX Make the block size settable */ |
| 1241 int BLOCKSIZE = 64*1024; | 1241 int BLOCKSIZE = 64*1024; |
| 1242 Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE; | 1242 Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE; |
| 1243 Py_ssize_t i; | 1243 Py_ssize_t i; |
| 1244 | 1244 |
| 1245 if (self->ob_size == 0) | 1245 if (Py_Size(self) == 0) |
| 1246 goto done; | 1246 goto done; |
| 1247 | 1247 |
| 1248 for (i = 0; i < nblocks; i++) { | 1248 for (i = 0; i < nblocks; i++) { |
| 1249 char* ptr = self->ob_item + i*BLOCKSIZE; | 1249 char* ptr = self->ob_item + i*BLOCKSIZE; |
| 1250 Py_ssize_t size = BLOCKSIZE; | 1250 Py_ssize_t size = BLOCKSIZE; |
| 1284 } | 1284 } |
| 1285 n = PyList_Size(list); | 1285 n = PyList_Size(list); |
| 1286 if (n > 0) { | 1286 if (n > 0) { |
| 1287 char *item = self->ob_item; | 1287 char *item = self->ob_item; |
| 1288 Py_ssize_t i; | 1288 Py_ssize_t i; |
| 1289 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize); | 1289 PyMem_RESIZE(item, char, (Py_Size(self) + n) * itemsize); |
| 1290 if (item == NULL) { | 1290 if (item == NULL) { |
| 1291 PyErr_NoMemory(); | 1291 PyErr_NoMemory(); |
| 1292 return NULL; | 1292 return NULL; |
| 1293 } | 1293 } |
| 1294 self->ob_item = item; | 1294 self->ob_item = item; |
| 1295 self->ob_size += n; | 1295 Py_Size(self) += n; |
| 1296 self->allocated = self->ob_size; | 1296 self->allocated = Py_Size(self); |
| 1297 for (i = 0; i < n; i++) { | 1297 for (i = 0; i < n; i++) { |
| 1298 PyObject *v = PyList_GetItem(list, i); | 1298 PyObject *v = PyList_GetItem(list, i); |
| 1299 if ((*self->ob_descr->setitem)(self, | 1299 if ((*self->ob_descr->setitem)(self, |
| 1300 self->ob_size - n + i, v) != 0) { | 1300 Py_Size(self) - n + i, v) != 0) { |
| 1301 self->ob_size -= n; | 1301 Py_Size(self) -= n; |
| 1302 PyMem_RESIZE(item, char, | 1302 PyMem_RESIZE(item, char, |
| 1303 self->ob_size * itemsize); | 1303 Py_Size(self) * itemsize); |
| 1304 self->ob_item = item; | 1304 self->ob_item = item; |
| 1305 self->allocated = self->ob_size; | 1305 self->allocated = Py_Size(self); |
| 1306 return NULL; | 1306 return NULL; |
| 1307 } | 1307 } |
| 1308 } | 1308 } |
| 1309 } | 1309 } |
| 1310 Py_INCREF(Py_None); | 1310 Py_INCREF(Py_None); |
| 1317 Append items to array from list."); | 1317 Append items to array from list."); |
| 1318 | 1318 |
| 1319 static PyObject * | 1319 static PyObject * |
| 1320 array_tolist(arrayobject *self, PyObject *unused) | 1320 array_tolist(arrayobject *self, PyObject *unused) |
| 1321 { | 1321 { |
| 1322 PyObject *list = PyList_New(self->ob_size); | 1322 PyObject *list = PyList_New(Py_Size(self)); |
| 1323 Py_ssize_t i; | 1323 Py_ssize_t i; |
| 1324 | 1324 |
| 1325 if (list == NULL) | 1325 if (list == NULL) |
| 1326 return NULL; | 1326 return NULL; |
| 1327 for (i = 0; i < self->ob_size; i++) { | 1327 for (i = 0; i < Py_Size(self); i++) { |
| 1328 PyObject *v = getarrayitem((PyObject *)self, i); | 1328 PyObject *v = getarrayitem((PyObject *)self, i); |
| 1329 if (v == NULL) { | 1329 if (v == NULL) { |
| 1330 Py_DECREF(list); | 1330 Py_DECREF(list); |
| 1331 return NULL; | 1331 return NULL; |
| 1332 } | 1332 } |
| 1355 return NULL; | 1355 return NULL; |
| 1356 } | 1356 } |
| 1357 n = n / itemsize; | 1357 n = n / itemsize; |
| 1358 if (n > 0) { | 1358 if (n > 0) { |
| 1359 char *item = self->ob_item; | 1359 char *item = self->ob_item; |
| 1360 PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize); | 1360 PyMem_RESIZE(item, char, (Py_Size(self) + n) * itemsize); |
| 1361 if (item == NULL) { | 1361 if (item == NULL) { |
| 1362 PyErr_NoMemory(); | 1362 PyErr_NoMemory(); |
| 1363 return NULL; | 1363 return NULL; |
| 1364 } | 1364 } |
| 1365 self->ob_item = item; | 1365 self->ob_item = item; |
| 1366 self->ob_size += n; | 1366 Py_Size(self) += n; |
| 1367 self->allocated = self->ob_size; | 1367 self->allocated = Py_Size(self); |
| 1368 memcpy(item + (self->ob_size - n) * itemsize, | 1368 memcpy(item + (Py_Size(self) - n) * itemsize, |
| 1369 str, itemsize*n); | 1369 str, itemsize*n); |
| 1370 } | 1370 } |
| 1371 Py_INCREF(Py_None); | 1371 Py_INCREF(Py_None); |
| 1372 return Py_None; | 1372 return Py_None; |
| 1373 } | 1373 } |
| 1408 "type 'u' arrays"); | 1408 "type 'u' arrays"); |
| 1409 return NULL; | 1409 return NULL; |
| 1410 } | 1410 } |
| 1411 if (n > 0) { | 1411 if (n > 0) { |
| 1412 Py_UNICODE *item = (Py_UNICODE *) self->ob_item; | 1412 Py_UNICODE *item = (Py_UNICODE *) self->ob_item; |
| 1413 PyMem_RESIZE(item, Py_UNICODE, self->ob_size + n); | 1413 PyMem_RESIZE(item, Py_UNICODE, Py_Size(self) + n); |
| 1414 if (item == NULL) { | 1414 if (item == NULL) { |
| 1415 PyErr_NoMemory(); | 1415 PyErr_NoMemory(); |
| 1416 return NULL; | 1416 return NULL; |
| 1417 } | 1417 } |
| 1418 self->ob_item = (char *) item; | 1418 self->ob_item = (char *) item; |
| 1419 self->ob_size += n; | 1419 Py_Size(self) += n; |
| 1420 self->allocated = self->ob_size; | 1420 self->allocated = Py_Size(self); |
| 1421 memcpy(item + self->ob_size - n, | 1421 memcpy(item + Py_Size(self) - n, |
| 1422 ustr, n * sizeof(Py_UNICODE)); | 1422 ustr, n * sizeof(Py_UNICODE)); |
| 1423 } | 1423 } |
| 1424 | 1424 |
| 1425 Py_INCREF(Py_None); | 1425 Py_INCREF(Py_None); |
| 1426 return Py_None; | 1426 return Py_None; |
| 1441 if (self->ob_descr->typecode != 'u') { | 1441 if (self->ob_descr->typecode != 'u') { |
| 1442 PyErr_SetString(PyExc_ValueError, | 1442 PyErr_SetString(PyExc_ValueError, |
| 1443 "tounicode() may only be called on type 'u' arrays"); | 1443 "tounicode() may only be called on type 'u' arrays"); |
| 1444 return NULL; | 1444 return NULL; |
| 1445 } | 1445 } |
| 1446 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, self->ob_size); | 1446 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_Size(self)); |
| 1447 } | 1447 } |
| 1448 | 1448 |
| 1449 PyDoc_STRVAR(tounicode_doc, | 1449 PyDoc_STRVAR(tounicode_doc, |
| 1450 "tounicode() -> unicode\n\ | 1450 "tounicode() -> unicode\n\ |
| 1451 \n\ | 1451 \n\ |
| 1534 { | 1534 { |
| 1535 char typecode; | 1535 char typecode; |
| 1536 PyObject *s, *v = NULL; | 1536 PyObject *s, *v = NULL; |
| 1537 Py_ssize_t len; | 1537 Py_ssize_t len; |
| 1538 | 1538 |
| 1539 len = a->ob_size; | 1539 len = Py_Size(a); |
| 1540 typecode = a->ob_descr->typecode; | 1540 typecode = a->ob_descr->typecode; |
| 1541 if (len == 0) { | 1541 if (len == 0) { |
| 1542 return PyUnicode_FromFormat("array('%c')", typecode); | 1542 return PyUnicode_FromFormat("array('%c')", typecode); |
| 1543 } | 1543 } |
| 1544 if (typecode == 'u') | 1544 if (typecode == 'u') |
| 1558 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); | 1558 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 1559 if (i==-1 && PyErr_Occurred()) { | 1559 if (i==-1 && PyErr_Occurred()) { |
| 1560 return NULL; | 1560 return NULL; |
| 1561 } | 1561 } |
| 1562 if (i < 0) | 1562 if (i < 0) |
| 1563 i += self->ob_size; | 1563 i += Py_Size(self); |
| 1564 return array_item(self, i); | 1564 return array_item(self, i); |
| 1565 } | 1565 } |
| 1566 else if (PySlice_Check(item)) { | 1566 else if (PySlice_Check(item)) { |
| 1567 Py_ssize_t start, stop, step, slicelength, cur, i; | 1567 Py_ssize_t start, stop, step, slicelength, cur, i; |
| 1568 PyObject* result; | 1568 PyObject* result; |
| 1569 arrayobject* ar; | 1569 arrayobject* ar; |
| 1570 int itemsize = self->ob_descr->itemsize; | 1570 int itemsize = self->ob_descr->itemsize; |
| 1571 | 1571 |
| 1572 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size, | 1572 if (PySlice_GetIndicesEx((PySliceObject*)item, Py_Size(self), |
| 1573 &start, &stop, &step, &slicelength) < 0) { | 1573 &start, &stop, &step, &slicelength) < 0) { |
| 1574 return NULL; | 1574 return NULL; |
| 1575 } | 1575 } |
| 1576 | 1576 |
| 1577 if (slicelength <= 0) { | 1577 if (slicelength <= 0) { |
| 1606 if (PyIndex_Check(item)) { | 1606 if (PyIndex_Check(item)) { |
| 1607 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); | 1607 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 1608 if (i==-1 && PyErr_Occurred()) | 1608 if (i==-1 && PyErr_Occurred()) |
| 1609 return -1; | 1609 return -1; |
| 1610 if (i < 0) | 1610 if (i < 0) |
| 1611 i += self->ob_size; | 1611 i += Py_Size(self); |
| 1612 return array_ass_item(self, i, value); | 1612 return array_ass_item(self, i, value); |
| 1613 } | 1613 } |
| 1614 else if (PySlice_Check(item)) { | 1614 else if (PySlice_Check(item)) { |
| 1615 Py_ssize_t start, stop, step, slicelength; | 1615 Py_ssize_t start, stop, step, slicelength; |
| 1616 int itemsize = self->ob_descr->itemsize; | 1616 int itemsize = self->ob_descr->itemsize; |
| 1617 | 1617 |
| 1618 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size, | 1618 if (PySlice_GetIndicesEx((PySliceObject*)item, Py_Size(self), |
| 1619 &start, &stop, &step, &slicelength) < 0) { | 1619 &start, &stop, &step, &slicelength) < 0) { |
| 1620 return -1; | 1620 return -1; |
| 1621 } | 1621 } |
| 1622 | 1622 |
| 1623 /* treat A[slice(a,b)] = v _exactly_ like A[a:b] = v */ | 1623 /* treat A[slice(a,b)] = v _exactly_ like A[a:b] = v */ |
| 1641 cur += step, i++) { | 1641 cur += step, i++) { |
| 1642 memmove(self->ob_item + (cur - i)*itemsize, | 1642 memmove(self->ob_item + (cur - i)*itemsize, |
| 1643 self->ob_item + (cur + 1)*itemsize, | 1643 self->ob_item + (cur + 1)*itemsize, |
| 1644 (step - 1) * itemsize); | 1644 (step - 1) * itemsize); |
| 1645 } | 1645 } |
| 1646 extra = self->ob_size - (cur + 1); | 1646 extra = Py_Size(self) - (cur + 1); |
| 1647 if (extra > 0) { | 1647 if (extra > 0) { |
| 1648 memmove(self->ob_item + (cur - i)*itemsize, | 1648 memmove(self->ob_item + (cur - i)*itemsize, |
| 1649 self->ob_item + (cur + 1)*itemsize, | 1649 self->ob_item + (cur + 1)*itemsize, |
| 1650 extra*itemsize); | 1650 extra*itemsize); |
| 1651 } | 1651 } |
| 1652 | 1652 |
| 1653 self->ob_size -= slicelength; | 1653 Py_Size(self) -= slicelength; |
| 1654 self->ob_item = (char *)PyMem_REALLOC(self->ob_item, | 1654 self->ob_item = (char *)PyMem_REALLOC(self->ob_item, |
| 1655 itemsize*self->ob_size); | 1655 itemsize*Py_Size(self)); |
| 1656 self->allocated = self->ob_size; | 1656 self->allocated = Py_Size(self); |
| 1657 | 1657 |
| 1658 return 0; | 1658 return 0; |
| 1659 } | 1659 } |
| 1660 else { | 1660 else { |
| 1661 /* assign slice */ | 1661 /* assign slice */ |
| 1663 arrayobject* av; | 1663 arrayobject* av; |
| 1664 | 1664 |
| 1665 if (!array_Check(value)) { | 1665 if (!array_Check(value)) { |
| 1666 PyErr_Format(PyExc_TypeError, | 1666 PyErr_Format(PyExc_TypeError, |
| 1667 "must assign array (not \"%.200s\") to slice", | 1667 "must assign array (not \"%.200s\") to slice", |
| 1668 value->ob_type->tp_name); | 1668 Py_Type(value)->tp_name); |
| 1669 return -1; | 1669 return -1; |
| 1670 } | 1670 } |
| 1671 | 1671 |
| 1672 av = (arrayobject*)value; | 1672 av = (arrayobject*)value; |
| 1673 | 1673 |
| 1674 if (av->ob_size != slicelength) { | 1674 if (Py_Size(av) != slicelength) { |
| 1675 PyErr_Format(PyExc_ValueError, | 1675 PyErr_Format(PyExc_ValueError, |
| 1676 "attempt to assign array of size %ld to extended slice of size %ld", | 1676 "attempt to assign array of size %ld to extended slice of size %ld", |
| 1677 /*XXX*/(long)av->ob_size, /*XXX*/(long)slicelength); | 1677 /*XXX*/(long)Py_Size(av), /*XXX*/(long)slicelength); |
| 1678 return -1; | 1678 return -1; |
| 1679 } | 1679 } |
| 1680 | 1680 |
| 1681 if (!slicelength) | 1681 if (!slicelength) |
| 1682 return 0; | 1682 return 0; |
| 1683 | 1683 |
| 1684 /* protect against a[::-1] = a */ | 1684 /* protect against a[::-1] = a */ |
| 1685 if (self == av) { | 1685 if (self == av) { |
| 1686 value = array_slice(av, 0, av->ob_size); | 1686 value = array_slice(av, 0, Py_Size(av)); |
| 1687 av = (arrayobject*)value; | 1687 av = (arrayobject*)value; |
| 1688 if (!av) | 1688 if (!av) |
| 1689 return -1; | 1689 return -1; |
| 1690 } | 1690 } |
| 1691 else { | 1691 else { |
| 1728 return -1; | 1728 return -1; |
| 1729 } | 1729 } |
| 1730 *ptr = (void *)self->ob_item; | 1730 *ptr = (void *)self->ob_item; |
| 1731 if (*ptr == NULL) | 1731 if (*ptr == NULL) |
| 1732 *ptr = emptybuf; | 1732 *ptr = emptybuf; |
| 1733 return self->ob_size*self->ob_descr->itemsize; | 1733 return Py_Size(self)*self->ob_descr->itemsize; |
| 1734 } | 1734 } |
| 1735 | 1735 |
| 1736 static Py_ssize_t | 1736 static Py_ssize_t |
| 1737 array_buffer_getwritebuf(arrayobject *self, Py_ssize_t index, const void **ptr) | 1737 array_buffer_getwritebuf(arrayobject *self, Py_ssize_t index, const void **ptr) |
| 1738 { | 1738 { |
| 1742 return -1; | 1742 return -1; |
| 1743 } | 1743 } |
| 1744 *ptr = (void *)self->ob_item; | 1744 *ptr = (void *)self->ob_item; |
| 1745 if (*ptr == NULL) | 1745 if (*ptr == NULL) |
| 1746 *ptr = emptybuf; | 1746 *ptr = emptybuf; |
| 1747 return self->ob_size*self->ob_descr->itemsize; | 1747 return Py_Size(self)*self->ob_descr->itemsize; |
| 1748 } | 1748 } |
| 1749 | 1749 |
| 1750 static Py_ssize_t | 1750 static Py_ssize_t |
| 1751 array_buffer_getsegcount(arrayobject *self, Py_ssize_t *lenp) | 1751 array_buffer_getsegcount(arrayobject *self, Py_ssize_t *lenp) |
| 1752 { | 1752 { |
| 1753 if ( lenp ) | 1753 if ( lenp ) |
| 1754 *lenp = self->ob_size*self->ob_descr->itemsize; | 1754 *lenp = Py_Size(self)*self->ob_descr->itemsize; |
| 1755 return 1; | 1755 return 1; |
| 1756 } | 1756 } |
| 1757 | 1757 |
| 1758 static PySequenceMethods array_as_sequence = { | 1758 static PySequenceMethods array_as_sequence = { |
| 1759 (lenfunc)array_length, /*sq_length*/ | 1759 (lenfunc)array_length, /*sq_length*/ |
| 1859 PyErr_NoMemory(); | 1859 PyErr_NoMemory(); |
| 1860 Py_DECREF(a); | 1860 Py_DECREF(a); |
| 1861 return NULL; | 1861 return NULL; |
| 1862 } | 1862 } |
| 1863 self->ob_item = item; | 1863 self->ob_item = item; |
| 1864 self->ob_size = n / sizeof(Py_UNICODE); | 1864 Py_Size(self) = n / sizeof(Py_UNICODE); |
| 1865 memcpy(item, PyUnicode_AS_DATA(initial), n); | 1865 memcpy(item, PyUnicode_AS_DATA(initial), n); |
| 1866 self->allocated = self->ob_size; | 1866 self->allocated = Py_Size(self); |
| 1867 } | 1867 } |
| 1868 } | 1868 } |
| 1869 if (it != NULL) { | 1869 if (it != NULL) { |
| 1870 if (array_iter_extend((arrayobject *)a, it) == -1) { | 1870 if (array_iter_extend((arrayobject *)a, it) == -1) { |
| 1871 Py_DECREF(it); | 1871 Py_DECREF(it); |
| 1947 "); | 1947 "); |
| 1948 | 1948 |
| 1949 static PyObject *array_iter(arrayobject *ao); | 1949 static PyObject *array_iter(arrayobject *ao); |
| 1950 | 1950 |
| 1951 static PyTypeObject Arraytype = { | 1951 static PyTypeObject Arraytype = { |
| 1952 PyObject_HEAD_INIT(NULL) | 1952 PyVarObject_HEAD_INIT(NULL, 0) |
| 1953 0, | |
| 1954 "array.array", | 1953 "array.array", |
| 1955 sizeof(arrayobject), | 1954 sizeof(arrayobject), |
| 1956 0, | 1955 0, |
| 1957 (destructor)array_dealloc, /* tp_dealloc */ | 1956 (destructor)array_dealloc, /* tp_dealloc */ |
| 1958 0, /* tp_print */ | 1957 0, /* tp_print */ |
| 2029 | 2028 |
| 2030 static PyObject * | 2029 static PyObject * |
| 2031 arrayiter_next(arrayiterobject *it) | 2030 arrayiter_next(arrayiterobject *it) |
| 2032 { | 2031 { |
| 2033 assert(PyArrayIter_Check(it)); | 2032 assert(PyArrayIter_Check(it)); |
| 2034 if (it->index < it->ao->ob_size) | 2033 if (it->index < Py_Size(it->ao)) |
| 2035 return (*it->getitem)(it->ao, it->index++); | 2034 return (*it->getitem)(it->ao, it->index++); |
| 2036 return NULL; | 2035 return NULL; |
| 2037 } | 2036 } |
| 2038 | 2037 |
| 2039 static void | 2038 static void |
| 2050 Py_VISIT(it->ao); | 2049 Py_VISIT(it->ao); |
| 2051 return 0; | 2050 return 0; |
| 2052 } | 2051 } |
| 2053 | 2052 |
| 2054 static PyTypeObject PyArrayIter_Type = { | 2053 static PyTypeObject PyArrayIter_Type = { |
| 2055 PyObject_HEAD_INIT(NULL) | 2054 PyVarObject_HEAD_INIT(NULL, 0) |
| 2056 0, /* ob_size */ | |
| 2057 "arrayiterator", /* tp_name */ | 2055 "arrayiterator", /* tp_name */ |
| 2058 sizeof(arrayiterobject), /* tp_basicsize */ | 2056 sizeof(arrayiterobject), /* tp_basicsize */ |
| 2059 0, /* tp_itemsize */ | 2057 0, /* tp_itemsize */ |
| 2060 /* methods */ | 2058 /* methods */ |
| 2061 (destructor)arrayiter_dealloc, /* tp_dealloc */ | 2059 (destructor)arrayiter_dealloc, /* tp_dealloc */ |
| 2098 { | 2096 { |
| 2099 PyObject *m; | 2097 PyObject *m; |
| 2100 | 2098 |
| 2101 if (PyType_Ready(&Arraytype) < 0) | 2099 if (PyType_Ready(&Arraytype) < 0) |
| 2102 return; | 2100 return; |
| 2103 PyArrayIter_Type.ob_type = &PyType_Type; | 2101 Py_Type(&PyArrayIter_Type) = &PyType_Type; |
| 2104 m = Py_InitModule3("array", a_methods, module_doc); | 2102 m = Py_InitModule3("array", a_methods, module_doc); |
| 2105 if (m == NULL) | 2103 if (m == NULL) |
| 2106 return; | 2104 return; |
| 2107 | 2105 |
| 2108 Py_INCREF((PyObject *)&Arraytype); | 2106 Py_INCREF((PyObject *)&Arraytype); |
