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