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 */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 14 | #include <sys/types.h> /* For size_t */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +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 { |
| 25 | int typecode; |
| 26 | int itemsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 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 { |
Georg Brandl | 1b67267 | 2006-02-17 08:56:33 +0000 | [diff] [blame] | 32 | PyObject_VAR_HEAD |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 33 | char *ob_item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 34 | Py_ssize_t allocated; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 35 | struct arraydescr *ob_descr; |
Raymond Hettinger | cb87bc8 | 2004-05-31 00:35:52 +0000 | [diff] [blame] | 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) |
| 42 | #define array_CheckExact(op) ((op)->ob_type == &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 | { |
| 47 | char *items; |
| 48 | size_t _new_size; |
| 49 | |
| 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 | */ |
| 54 | |
| 55 | if (self->allocated >= newsize && |
| 56 | self->ob_size < newsize + 16 && |
| 57 | self->ob_item != NULL) { |
| 58 | self->ob_size = newsize; |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 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 | */ |
| 73 | |
| 74 | _new_size = (newsize >> 4) + (self->ob_size < 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 | self->ob_size = newsize; |
| 88 | self->allocated = _new_size; |
| 89 | return 0; |
| 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 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +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 | { |
| 113 | char x; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 114 | if (!PyArg_Parse(v, "c;array item must be char", &x)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 115 | return -1; |
| 116 | if (i >= 0) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 117 | ((char *)ap->ob_item)[i] = x; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 118 | return 0; |
| 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 | { |
| 124 | long x = ((char *)ap->ob_item)[i]; |
| 125 | if (x >= 128) |
| 126 | x -= 256; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 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 | { |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +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)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 138 | return -1; |
Guido van Rossum | 9f754e0 | 2000-07-01 00:38:19 +0000 | [diff] [blame] | 139 | else if (x < -128) { |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 140 | PyErr_SetString(PyExc_OverflowError, |
| 141 | "signed char is less than minimum"); |
| 142 | return -1; |
| 143 | } |
Guido van Rossum | 9f754e0 | 2000-07-01 00:38:19 +0000 | [diff] [blame] | 144 | else if (x > 127) { |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 145 | PyErr_SetString(PyExc_OverflowError, |
| 146 | "signed char is greater than maximum"); |
| 147 | return -1; |
| 148 | } |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 149 | if (i >= 0) |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 150 | ((char *)ap->ob_item)[i] = (char)x; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 151 | return 0; |
| 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 | { |
| 157 | long x = ((unsigned char *)ap->ob_item)[i]; |
| 158 | return PyInt_FromLong(x); |
| 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 | { |
| 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) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 169 | ((char *)ap->ob_item)[i] = x; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 170 | return 0; |
| 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 | { |
| 177 | return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1); |
| 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 | { |
| 183 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 184 | Py_ssize_t len; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 185 | |
| 186 | if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len)) |
| 187 | return -1; |
| 188 | if (len != 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 189 | PyErr_SetString(PyExc_TypeError, |
| 190 | "array item must be unicode character"); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 191 | return -1; |
| 192 | } |
| 193 | if (i >= 0) |
| 194 | ((Py_UNICODE *)ap->ob_item)[i] = p[0]; |
| 195 | return 0; |
| 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 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +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 | { |
| 208 | short x; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 209 | /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */ |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 210 | if (!PyArg_Parse(v, "h;array item must be integer", &x)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 211 | return -1; |
| 212 | if (i >= 0) |
| 213 | ((short *)ap->ob_item)[i] = x; |
| 214 | return 0; |
| 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 | { |
| 220 | return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[i]); |
| 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 | { |
| 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; |
| 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 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +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 | { |
| 255 | int x; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 256 | /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */ |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 257 | if (!PyArg_Parse(v, "i;array item must be integer", &x)) |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 258 | return -1; |
| 259 | if (i >= 0) |
| 260 | ((int *)ap->ob_item)[i] = x; |
| 261 | return 0; |
| 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 | { |
| 267 | return PyLong_FromUnsignedLong( |
| 268 | (unsigned long) ((unsigned int *)ap->ob_item)[i]); |
| 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 | { |
| 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 { |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 281 | long y; |
| 282 | if (!PyArg_Parse(v, "l;array item must be integer", &y)) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 283 | return -1; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 291 | } |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 292 | if (x > UINT_MAX) { |
| 293 | PyErr_SetString(PyExc_OverflowError, |
| 294 | "unsigned int is greater than maximum"); |
| 295 | return -1; |
| 296 | } |
| 297 | |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 298 | if (i >= 0) |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 299 | ((unsigned int *)ap->ob_item)[i] = (unsigned int)x; |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 300 | return 0; |
| 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 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +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 | { |
| 312 | long x; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 313 | if (!PyArg_Parse(v, "l;array item must be integer", &x)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 314 | return -1; |
| 315 | if (i >= 0) |
| 316 | ((long *)ap->ob_item)[i] = x; |
| 317 | return 0; |
| 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 | { |
| 323 | return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]); |
| 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 | { |
| 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 { |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 336 | long y; |
| 337 | if (!PyArg_Parse(v, "l;array item must be integer", &y)) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 338 | return -1; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 346 | } |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 353 | if (i >= 0) |
| 354 | ((unsigned long *)ap->ob_item)[i] = x; |
| 355 | return 0; |
| 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 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +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 | { |
| 367 | float x; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 368 | if (!PyArg_Parse(v, "f;array item must be float", &x)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 369 | return -1; |
| 370 | if (i >= 0) |
| 371 | ((float *)ap->ob_item)[i] = x; |
| 372 | return 0; |
| 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 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +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 | { |
| 384 | double x; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 385 | if (!PyArg_Parse(v, "d;array item must be float", &x)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 386 | return -1; |
| 387 | if (i >= 0) |
| 388 | ((double *)ap->ob_item)[i] = x; |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | /* Description of types */ |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 393 | static struct arraydescr descriptors[] = { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 394 | {'c', sizeof(char), c_getitem, c_setitem}, |
| 395 | {'b', sizeof(char), b_getitem, b_setitem}, |
Guido van Rossum | 1a747f8 | 1997-05-16 16:21:38 +0000 | [diff] [blame] | 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 |
| 398 | {'u', sizeof(Py_UNICODE), u_getitem, u_setitem}, |
| 399 | #endif |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 400 | {'h', sizeof(short), h_getitem, h_setitem}, |
Guido van Rossum | 1a747f8 | 1997-05-16 16:21:38 +0000 | [diff] [blame] | 401 | {'H', sizeof(short), HH_getitem, HH_setitem}, |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 402 | {'i', sizeof(int), i_getitem, i_setitem}, |
Guido van Rossum | 1a747f8 | 1997-05-16 16:21:38 +0000 | [diff] [blame] | 403 | {'I', sizeof(int), II_getitem, II_setitem}, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 404 | {'l', sizeof(long), l_getitem, l_setitem}, |
Guido van Rossum | 1a747f8 | 1997-05-16 16:21:38 +0000 | [diff] [blame] | 405 | {'L', sizeof(long), LL_getitem, LL_setitem}, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 406 | {'f', sizeof(float), f_getitem, f_setitem}, |
| 407 | {'d', sizeof(double), d_getitem, d_setitem}, |
| 408 | {'\0', 0, 0, 0} /* Sentinel */ |
| 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 | { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 418 | arrayobject *op; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 419 | size_t nbytes; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 420 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 421 | if (size < 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 422 | PyErr_BadInternalCall(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 423 | return NULL; |
| 424 | } |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 425 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 426 | nbytes = size * descr->itemsize; |
| 427 | /* Check for overflow */ |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 428 | if (nbytes / descr->itemsize != (size_t)size) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 429 | return PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 430 | } |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 431 | op = (arrayobject *) type->tp_alloc(type, 0); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 432 | if (op == NULL) { |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 433 | return NULL; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 434 | } |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 435 | op->ob_size = size; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 436 | if (size <= 0) { |
| 437 | op->ob_item = NULL; |
| 438 | } |
| 439 | else { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 440 | op->ob_item = PyMem_NEW(char, nbytes); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 441 | if (op->ob_item == NULL) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 442 | PyObject_Del(op); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 443 | return PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 444 | } |
| 445 | } |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 446 | op->ob_descr = descr; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 447 | op->allocated = size; |
Raymond Hettinger | cb87bc8 | 2004-05-31 00:35:52 +0000 | [diff] [blame] | 448 | op->weakreflist = NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 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 | { |
| 455 | register arrayobject *ap; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 456 | assert(array_Check(op)); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 457 | ap = (arrayobject *)op; |
Walter Dörwald | 8bb1ae9 | 2003-05-23 10:01:07 +0000 | [diff] [blame] | 458 | assert(i>=0 && i<ap->ob_size); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 459 | return (*ap->ob_descr->getitem)(ap, i); |
| 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 | { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 465 | char *items; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 466 | Py_ssize_t n = self->ob_size; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 467 | if (v == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 468 | PyErr_BadInternalCall(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 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 | |
| 474 | if (array_resize(self, n+1) == -1) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 475 | return -1; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 476 | items = self->ob_item; |
Walter Dörwald | 9e46abe | 2003-05-18 03:15:10 +0000 | [diff] [blame] | 477 | if (where < 0) { |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 478 | where += n; |
Walter Dörwald | 9e46abe | 2003-05-18 03:15:10 +0000 | [diff] [blame] | 479 | if (where < 0) |
| 480 | where = 0; |
| 481 | } |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 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); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 489 | return (*self->ob_descr->setitem)(self, where, v); |
| 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 | { |
Raymond Hettinger | cb87bc8 | 2004-05-31 00:35:52 +0000 | [diff] [blame] | 497 | if (op->weakreflist != NULL) |
| 498 | PyObject_ClearWeakRefs((PyObject *) op); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 499 | if (op->ob_item != NULL) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 500 | PyMem_DEL(op->ob_item); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 501 | op->ob_type->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 | { |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 507 | arrayobject *va, *wa; |
| 508 | PyObject *vi = NULL; |
| 509 | PyObject *wi = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 510 | Py_ssize_t i, k; |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 511 | PyObject *res; |
| 512 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 513 | if (!array_Check(v) || !array_Check(w)) { |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 514 | Py_INCREF(Py_NotImplemented); |
| 515 | return Py_NotImplemented; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 516 | } |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 517 | |
| 518 | va = (arrayobject *)v; |
| 519 | wa = (arrayobject *)w; |
| 520 | |
| 521 | if (va->ob_size != wa->ob_size && (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 | } |
| 530 | |
| 531 | /* Search for the first index where items are different */ |
| 532 | k = 1; |
| 533 | for (i = 0; i < va->ob_size && i < wa->ob_size; 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 | } |
| 549 | |
| 550 | if (k) { |
| 551 | /* No more items to compare -- compare sizes */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 552 | Py_ssize_t vs = va->ob_size; |
| 553 | Py_ssize_t ws = wa->ob_size; |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 554 | int cmp; |
| 555 | switch (op) { |
| 556 | case Py_LT: cmp = vs < ws; break; |
Guido van Rossum | 2b597e4 | 2001-01-25 22:12:43 +0000 | [diff] [blame] | 557 | case Py_LE: cmp = vs <= ws; break; |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 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 | } |
| 571 | |
| 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 | { |
| 593 | return a->ob_size; |
| 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 | { |
| 599 | if (i < 0 || i >= a->ob_size) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 600 | PyErr_SetString(PyExc_IndexError, "array index out of range"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 601 | return NULL; |
| 602 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 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 | { |
| 609 | arrayobject *np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 610 | if (ilow < 0) |
| 611 | ilow = 0; |
| 612 | else if (ilow > a->ob_size) |
| 613 | ilow = a->ob_size; |
| 614 | if (ihigh < 0) |
| 615 | ihigh = 0; |
| 616 | if (ihigh < ilow) |
| 617 | ihigh = ilow; |
| 618 | else if (ihigh > a->ob_size) |
| 619 | ihigh = a->ob_size; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 620 | np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 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); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 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 | { |
| 631 | return array_slice(a, 0, a->ob_size); |
| 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 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 642 | Py_ssize_t size; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 643 | arrayobject *np; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 644 | if (!array_Check(bb)) { |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 645 | PyErr_Format(PyExc_TypeError, |
| 646 | "can only append array (not \"%.200s\") to array", |
| 647 | bb->ob_type->tp_name); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 648 | return NULL; |
| 649 | } |
| 650 | #define b ((arrayobject *)bb) |
| 651 | if (a->ob_descr != b->ob_descr) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 652 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 653 | return NULL; |
| 654 | } |
| 655 | size = a->ob_size + b->ob_size; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 656 | np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 657 | if (np == NULL) { |
| 658 | return NULL; |
| 659 | } |
| 660 | memcpy(np->ob_item, a->ob_item, a->ob_size*a->ob_descr->itemsize); |
| 661 | memcpy(np->ob_item + a->ob_size*a->ob_descr->itemsize, |
Guido van Rossum | 32be3a7 | 1993-11-05 10:16:27 +0000 | [diff] [blame] | 662 | b->ob_item, b->ob_size*b->ob_descr->itemsize); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 663 | return (PyObject *)np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 664 | #undef b |
| 665 | } |
| 666 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 667 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 668 | array_repeat(arrayobject *a, Py_ssize_t n) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 669 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 670 | Py_ssize_t i; |
| 671 | Py_ssize_t size; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 672 | arrayobject *np; |
| 673 | char *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 674 | Py_ssize_t nbytes; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 675 | if (n < 0) |
| 676 | n = 0; |
| 677 | size = a->ob_size * n; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 678 | np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 679 | if (np == NULL) |
| 680 | return NULL; |
| 681 | p = np->ob_item; |
| 682 | nbytes = a->ob_size * a->ob_descr->itemsize; |
| 683 | for (i = 0; i < n; i++) { |
| 684 | memcpy(p, a->ob_item, nbytes); |
| 685 | p += nbytes; |
| 686 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 687 | return (PyObject *) np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | static int |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 691 | 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] | 692 | { |
| 693 | char *item; |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 694 | Py_ssize_t n; /* Size of replacement array */ |
| 695 | Py_ssize_t d; /* Change in size */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 696 | #define b ((arrayobject *)v) |
| 697 | if (v == NULL) |
| 698 | n = 0; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 699 | else if (array_Check(v)) { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 700 | n = b->ob_size; |
| 701 | if (a == b) { |
| 702 | /* Special case "a[i:j] = a" -- copy b first */ |
| 703 | int ret; |
| 704 | v = array_slice(b, 0, n); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 705 | if (!v) |
| 706 | return -1; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 707 | ret = array_ass_slice(a, ilow, ihigh, v); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 708 | Py_DECREF(v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 709 | return ret; |
| 710 | } |
| 711 | if (b->ob_descr != a->ob_descr) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 712 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 713 | return -1; |
| 714 | } |
| 715 | } |
| 716 | else { |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 717 | PyErr_Format(PyExc_TypeError, |
| 718 | "can only assign array (not \"%.200s\") to array slice", |
| 719 | v->ob_type->tp_name); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 720 | return -1; |
| 721 | } |
| 722 | if (ilow < 0) |
| 723 | ilow = 0; |
| 724 | else if (ilow > a->ob_size) |
| 725 | ilow = a->ob_size; |
| 726 | if (ihigh < 0) |
| 727 | ihigh = 0; |
| 728 | if (ihigh < ilow) |
| 729 | ihigh = ilow; |
| 730 | else if (ihigh > a->ob_size) |
| 731 | ihigh = a->ob_size; |
| 732 | item = a->ob_item; |
| 733 | d = n - (ihigh-ilow); |
| 734 | if (d < 0) { /* Delete -d items */ |
| 735 | memmove(item + (ihigh+d)*a->ob_descr->itemsize, |
| 736 | item + ihigh*a->ob_descr->itemsize, |
| 737 | (a->ob_size-ihigh)*a->ob_descr->itemsize); |
| 738 | a->ob_size += d; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 739 | PyMem_RESIZE(item, char, a->ob_size*a->ob_descr->itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 740 | /* Can't fail */ |
| 741 | a->ob_item = item; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 742 | a->allocated = a->ob_size; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 743 | } |
| 744 | else if (d > 0) { /* Insert d items */ |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 745 | PyMem_RESIZE(item, char, |
| 746 | (a->ob_size + d)*a->ob_descr->itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 747 | if (item == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 748 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 749 | return -1; |
| 750 | } |
| 751 | memmove(item + (ihigh+d)*a->ob_descr->itemsize, |
| 752 | item + ihigh*a->ob_descr->itemsize, |
| 753 | (a->ob_size-ihigh)*a->ob_descr->itemsize); |
| 754 | a->ob_item = item; |
| 755 | a->ob_size += d; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 756 | a->allocated = a->ob_size; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 757 | } |
| 758 | if (n > 0) |
| 759 | memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item, |
| 760 | n*b->ob_descr->itemsize); |
| 761 | return 0; |
| 762 | #undef b |
| 763 | } |
| 764 | |
| 765 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 766 | array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 767 | { |
| 768 | if (i < 0 || i >= a->ob_size) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 769 | PyErr_SetString(PyExc_IndexError, |
| 770 | "array assignment index out of range"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 771 | return -1; |
| 772 | } |
| 773 | if (v == NULL) |
| 774 | return array_ass_slice(a, i, i+1, v); |
| 775 | return (*a->ob_descr->setitem)(a, i, v); |
| 776 | } |
| 777 | |
| 778 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 779 | setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 780 | { |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 781 | assert(array_Check(a)); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 782 | return array_ass_item((arrayobject *)a, i, v); |
| 783 | } |
| 784 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 785 | static int |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 786 | array_iter_extend(arrayobject *self, PyObject *bb) |
| 787 | { |
| 788 | PyObject *it, *v; |
| 789 | |
| 790 | it = PyObject_GetIter(bb); |
| 791 | if (it == NULL) |
| 792 | return -1; |
| 793 | |
| 794 | while ((v = PyIter_Next(it)) != NULL) { |
| 795 | if (ins1(self, (int) self->ob_size, v) != 0) { |
| 796 | Py_DECREF(v); |
| 797 | Py_DECREF(it); |
| 798 | return -1; |
| 799 | } |
| 800 | Py_DECREF(v); |
| 801 | } |
| 802 | Py_DECREF(it); |
| 803 | if (PyErr_Occurred()) |
| 804 | return -1; |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | static int |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 809 | array_do_extend(arrayobject *self, PyObject *bb) |
| 810 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 811 | Py_ssize_t size; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 812 | |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 813 | if (!array_Check(bb)) |
| 814 | return array_iter_extend(self, bb); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 815 | #define b ((arrayobject *)bb) |
| 816 | if (self->ob_descr != b->ob_descr) { |
| 817 | PyErr_SetString(PyExc_TypeError, |
| 818 | "can only extend with array of same kind"); |
| 819 | return -1; |
| 820 | } |
| 821 | size = self->ob_size + b->ob_size; |
| 822 | PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize); |
| 823 | if (self->ob_item == NULL) { |
| 824 | PyObject_Del(self); |
| 825 | PyErr_NoMemory(); |
| 826 | return -1; |
| 827 | } |
| 828 | memcpy(self->ob_item + self->ob_size*self->ob_descr->itemsize, |
| 829 | b->ob_item, b->ob_size*b->ob_descr->itemsize); |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 830 | self->ob_size = size; |
| 831 | self->allocated = size; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 832 | |
| 833 | return 0; |
| 834 | #undef b |
| 835 | } |
| 836 | |
| 837 | static PyObject * |
| 838 | array_inplace_concat(arrayobject *self, PyObject *bb) |
| 839 | { |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 840 | if (!array_Check(bb)) { |
| 841 | PyErr_Format(PyExc_TypeError, |
| 842 | "can only extend array with array (not \"%.200s\")", |
| 843 | bb->ob_type->tp_name); |
| 844 | return NULL; |
| 845 | } |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 846 | if (array_do_extend(self, bb) == -1) |
| 847 | return NULL; |
| 848 | Py_INCREF(self); |
| 849 | return (PyObject *)self; |
| 850 | } |
| 851 | |
| 852 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 853 | array_inplace_repeat(arrayobject *self, Py_ssize_t n) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 854 | { |
| 855 | char *items, *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 856 | Py_ssize_t size, i; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 857 | |
| 858 | if (self->ob_size > 0) { |
| 859 | if (n < 0) |
| 860 | n = 0; |
| 861 | items = self->ob_item; |
| 862 | size = self->ob_size * self->ob_descr->itemsize; |
| 863 | if (n == 0) { |
| 864 | PyMem_FREE(items); |
| 865 | self->ob_item = NULL; |
| 866 | self->ob_size = 0; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 867 | self->allocated = 0; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 868 | } |
| 869 | else { |
| 870 | PyMem_Resize(items, char, n * size); |
| 871 | if (items == NULL) |
| 872 | return PyErr_NoMemory(); |
| 873 | p = items; |
| 874 | for (i = 1; i < n; i++) { |
| 875 | p += size; |
| 876 | memcpy(p, items, size); |
| 877 | } |
| 878 | self->ob_item = items; |
| 879 | self->ob_size *= n; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 880 | self->allocated = self->ob_size; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | Py_INCREF(self); |
| 884 | return (PyObject *)self; |
| 885 | } |
| 886 | |
| 887 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 888 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 889 | ins(arrayobject *self, Py_ssize_t where, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 890 | { |
| 891 | if (ins1(self, where, v) != 0) |
| 892 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 893 | Py_INCREF(Py_None); |
| 894 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 897 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 898 | array_count(arrayobject *self, PyObject *v) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 899 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 900 | Py_ssize_t count = 0; |
| 901 | Py_ssize_t i; |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 902 | |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 903 | for (i = 0; i < self->ob_size; i++) { |
| 904 | PyObject *selfi = getarrayitem((PyObject *)self, i); |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 905 | int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 906 | Py_DECREF(selfi); |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 907 | if (cmp > 0) |
| 908 | count++; |
| 909 | else if (cmp < 0) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 910 | return NULL; |
| 911 | } |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 912 | return PyInt_FromSsize_t(count); |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 913 | } |
| 914 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 915 | PyDoc_STRVAR(count_doc, |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 916 | "count(x)\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 917 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 918 | Return number of occurences of x in the array."); |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 919 | |
| 920 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 921 | array_index(arrayobject *self, PyObject *v) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 922 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 923 | Py_ssize_t i; |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 924 | |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 925 | for (i = 0; i < self->ob_size; i++) { |
| 926 | PyObject *selfi = getarrayitem((PyObject *)self, i); |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 927 | int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
| 928 | Py_DECREF(selfi); |
| 929 | if (cmp > 0) { |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 930 | return PyInt_FromLong((long)i); |
| 931 | } |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 932 | else if (cmp < 0) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 933 | return NULL; |
| 934 | } |
| 935 | PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list"); |
| 936 | return NULL; |
| 937 | } |
| 938 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 939 | PyDoc_STRVAR(index_doc, |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 940 | "index(x)\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 941 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 942 | Return index of first occurence of x in the array."); |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 943 | |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 944 | static int |
| 945 | array_contains(arrayobject *self, PyObject *v) |
| 946 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 947 | Py_ssize_t i; |
| 948 | int cmp; |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 949 | |
| 950 | for (i = 0, cmp = 0 ; cmp == 0 && i < self->ob_size; i++) { |
| 951 | PyObject *selfi = getarrayitem((PyObject *)self, i); |
| 952 | cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
| 953 | Py_DECREF(selfi); |
| 954 | } |
| 955 | return cmp; |
| 956 | } |
| 957 | |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 958 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 959 | array_remove(arrayobject *self, PyObject *v) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 960 | { |
| 961 | int i; |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 962 | |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 963 | for (i = 0; i < self->ob_size; i++) { |
| 964 | PyObject *selfi = getarrayitem((PyObject *)self,i); |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 965 | int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
| 966 | Py_DECREF(selfi); |
| 967 | if (cmp > 0) { |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 968 | if (array_ass_slice(self, i, i+1, |
| 969 | (PyObject *)NULL) != 0) |
| 970 | return NULL; |
| 971 | Py_INCREF(Py_None); |
| 972 | return Py_None; |
| 973 | } |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 974 | else if (cmp < 0) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 975 | return NULL; |
| 976 | } |
| 977 | PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list"); |
| 978 | return NULL; |
| 979 | } |
| 980 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 981 | PyDoc_STRVAR(remove_doc, |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 982 | "remove(x)\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 983 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 984 | Remove the first occurence of x in the array."); |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 985 | |
| 986 | static PyObject * |
| 987 | array_pop(arrayobject *self, PyObject *args) |
| 988 | { |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 989 | Py_ssize_t i = -1; |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 990 | PyObject *v; |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 991 | if (!PyArg_ParseTuple(args, "|n:pop", &i)) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 992 | return NULL; |
| 993 | if (self->ob_size == 0) { |
| 994 | /* Special-case most common failure cause */ |
| 995 | PyErr_SetString(PyExc_IndexError, "pop from empty array"); |
| 996 | return NULL; |
| 997 | } |
| 998 | if (i < 0) |
| 999 | i += self->ob_size; |
| 1000 | if (i < 0 || i >= self->ob_size) { |
| 1001 | PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
| 1002 | return NULL; |
| 1003 | } |
| 1004 | v = getarrayitem((PyObject *)self,i); |
| 1005 | if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) { |
| 1006 | Py_DECREF(v); |
| 1007 | return NULL; |
| 1008 | } |
| 1009 | return v; |
| 1010 | } |
| 1011 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1012 | PyDoc_STRVAR(pop_doc, |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 1013 | "pop([i])\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1014 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1015 | 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] | 1016 | |
| 1017 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1018 | array_extend(arrayobject *self, PyObject *bb) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1019 | { |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1020 | if (array_do_extend(self, bb) == -1) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1021 | return NULL; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1022 | Py_INCREF(Py_None); |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1023 | return Py_None; |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1026 | PyDoc_STRVAR(extend_doc, |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 1027 | "extend(array or iterable)\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1028 | \n\ |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 1029 | Append items to the end of the array."); |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1030 | |
| 1031 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 1032 | array_insert(arrayobject *self, PyObject *args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1033 | { |
Thomas Wouters | b9eb551 | 2006-02-27 19:44:56 +0000 | [diff] [blame] | 1034 | Py_ssize_t i; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1035 | PyObject *v; |
Thomas Wouters | b9eb551 | 2006-02-27 19:44:56 +0000 | [diff] [blame] | 1036 | if (!PyArg_ParseTuple(args, "nO:insert", &i, &v)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1037 | return NULL; |
| 1038 | return ins(self, i, v); |
| 1039 | } |
| 1040 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1041 | PyDoc_STRVAR(insert_doc, |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 1042 | "insert(i,x)\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1043 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1044 | Insert a new item x into the array before position i."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1045 | |
| 1046 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1047 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1048 | array_buffer_info(arrayobject *self, PyObject *unused) |
Guido van Rossum | de4a4ca | 1997-08-12 14:55:56 +0000 | [diff] [blame] | 1049 | { |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 1050 | PyObject* retval = NULL; |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 1051 | retval = PyTuple_New(2); |
| 1052 | if (!retval) |
| 1053 | return NULL; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 1054 | |
| 1055 | PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item)); |
| 1056 | PyTuple_SET_ITEM(retval, 1, PyInt_FromLong((long)(self->ob_size))); |
| 1057 | |
| 1058 | return retval; |
Guido van Rossum | de4a4ca | 1997-08-12 14:55:56 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1061 | PyDoc_STRVAR(buffer_info_doc, |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 1062 | "buffer_info() -> (address, length)\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1063 | \n\ |
| 1064 | 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] | 1065 | the length in items of the buffer used to hold array's contents\n\ |
| 1066 | 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] | 1067 | the buffer length in bytes."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1068 | |
| 1069 | |
Guido van Rossum | de4a4ca | 1997-08-12 14:55:56 +0000 | [diff] [blame] | 1070 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1071 | array_append(arrayobject *self, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1072 | { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1073 | return ins(self, (int) self->ob_size, v); |
| 1074 | } |
| 1075 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1076 | PyDoc_STRVAR(append_doc, |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1077 | "append(x)\n\ |
| 1078 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1079 | Append new value x to the end of the array."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1080 | |
| 1081 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1082 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1083 | array_byteswap(arrayobject *self, PyObject *unused) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1084 | { |
| 1085 | char *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1086 | Py_ssize_t i; |
Fred Drake | bf27298 | 1999-12-03 17:15:30 +0000 | [diff] [blame] | 1087 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1088 | switch (self->ob_descr->itemsize) { |
| 1089 | case 1: |
| 1090 | break; |
| 1091 | case 2: |
| 1092 | for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 2) { |
| 1093 | char p0 = p[0]; |
| 1094 | p[0] = p[1]; |
| 1095 | p[1] = p0; |
| 1096 | } |
| 1097 | break; |
| 1098 | case 4: |
| 1099 | for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 4) { |
| 1100 | char p0 = p[0]; |
| 1101 | char p1 = p[1]; |
| 1102 | p[0] = p[3]; |
| 1103 | p[1] = p[2]; |
| 1104 | p[2] = p1; |
| 1105 | p[3] = p0; |
| 1106 | } |
| 1107 | break; |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 1108 | case 8: |
| 1109 | for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 8) { |
| 1110 | char p0 = p[0]; |
| 1111 | char p1 = p[1]; |
| 1112 | char p2 = p[2]; |
| 1113 | char p3 = p[3]; |
| 1114 | p[0] = p[7]; |
| 1115 | p[1] = p[6]; |
| 1116 | p[2] = p[5]; |
| 1117 | p[3] = p[4]; |
| 1118 | p[4] = p3; |
| 1119 | p[5] = p2; |
| 1120 | p[6] = p1; |
| 1121 | p[7] = p0; |
| 1122 | } |
| 1123 | break; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1124 | default: |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1125 | PyErr_SetString(PyExc_RuntimeError, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1126 | "don't know how to byteswap this array type"); |
| 1127 | return NULL; |
| 1128 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1129 | Py_INCREF(Py_None); |
| 1130 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1133 | PyDoc_STRVAR(byteswap_doc, |
Fred Drake | bf27298 | 1999-12-03 17:15:30 +0000 | [diff] [blame] | 1134 | "byteswap()\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1135 | \n\ |
Fred Drake | bf27298 | 1999-12-03 17:15:30 +0000 | [diff] [blame] | 1136 | 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] | 1137 | 4, or 8 bytes in size, RuntimeError is raised."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1138 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1139 | static PyObject * |
Raymond Hettinger | b0900e6 | 2004-12-16 16:23:40 +0000 | [diff] [blame] | 1140 | array_reduce(arrayobject *array) |
| 1141 | { |
| 1142 | PyObject *dict, *result; |
| 1143 | |
| 1144 | dict = PyObject_GetAttrString((PyObject *)array, "__dict__"); |
| 1145 | if (dict == NULL) { |
| 1146 | PyErr_Clear(); |
| 1147 | dict = Py_None; |
| 1148 | Py_INCREF(dict); |
| 1149 | } |
| 1150 | result = Py_BuildValue("O(cs#)O", |
| 1151 | array->ob_type, |
| 1152 | array->ob_descr->typecode, |
| 1153 | array->ob_item, |
| 1154 | array->ob_size * array->ob_descr->itemsize, |
| 1155 | dict); |
| 1156 | Py_DECREF(dict); |
| 1157 | return result; |
| 1158 | } |
| 1159 | |
| 1160 | PyDoc_STRVAR(array_doc, "Return state information for pickling."); |
| 1161 | |
| 1162 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1163 | array_reverse(arrayobject *self, PyObject *unused) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1164 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1165 | register Py_ssize_t itemsize = self->ob_descr->itemsize; |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 1166 | register char *p, *q; |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 1167 | /* little buffer to hold items while swapping */ |
| 1168 | char tmp[256]; /* 8 is probably enough -- but why skimp */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1169 | assert((size_t)itemsize <= sizeof(tmp)); |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 1170 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1171 | if (self->ob_size > 1) { |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 1172 | for (p = self->ob_item, |
| 1173 | q = self->ob_item + (self->ob_size - 1)*itemsize; |
| 1174 | p < q; |
| 1175 | p += itemsize, q -= itemsize) { |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 1176 | /* memory areas guaranteed disjoint, so memcpy |
| 1177 | * is safe (& memmove may be slower). |
| 1178 | */ |
| 1179 | memcpy(tmp, p, itemsize); |
| 1180 | memcpy(p, q, itemsize); |
| 1181 | memcpy(q, tmp, itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1182 | } |
| 1183 | } |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 1184 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1185 | Py_INCREF(Py_None); |
| 1186 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1187 | } |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 1188 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1189 | PyDoc_STRVAR(reverse_doc, |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1190 | "reverse()\n\ |
| 1191 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1192 | Reverse the order of the items in the array."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1193 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1194 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 1195 | array_fromfile(arrayobject *self, PyObject *args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1196 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1197 | PyObject *f; |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 1198 | Py_ssize_t n; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1199 | FILE *fp; |
Thomas Wouters | 7a2f83b | 2006-02-16 17:07:41 +0000 | [diff] [blame] | 1200 | if (!PyArg_ParseTuple(args, "On:fromfile", &f, &n)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1201 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1202 | fp = PyFile_AsFile(f); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1203 | if (fp == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1204 | PyErr_SetString(PyExc_TypeError, "arg1 must be open file"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1205 | return NULL; |
| 1206 | } |
| 1207 | if (n > 0) { |
| 1208 | char *item = self->ob_item; |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 1209 | Py_ssize_t itemsize = self->ob_descr->itemsize; |
Guido van Rossum | 7d0ae5e | 2000-06-28 21:27:21 +0000 | [diff] [blame] | 1210 | size_t nread; |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 1211 | Py_ssize_t newlength; |
Guido van Rossum | 3791b0d | 1999-02-23 18:05:22 +0000 | [diff] [blame] | 1212 | size_t newbytes; |
| 1213 | /* Be careful here about overflow */ |
| 1214 | if ((newlength = self->ob_size + n) <= 0 || |
Guido van Rossum | 481ac88 | 1999-03-19 21:50:11 +0000 | [diff] [blame] | 1215 | (newbytes = newlength * itemsize) / itemsize != |
| 1216 | (size_t)newlength) |
Guido van Rossum | 3791b0d | 1999-02-23 18:05:22 +0000 | [diff] [blame] | 1217 | goto nomem; |
| 1218 | PyMem_RESIZE(item, char, newbytes); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1219 | if (item == NULL) { |
Guido van Rossum | 3791b0d | 1999-02-23 18:05:22 +0000 | [diff] [blame] | 1220 | nomem: |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1221 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1222 | return NULL; |
| 1223 | } |
| 1224 | self->ob_item = item; |
| 1225 | self->ob_size += n; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 1226 | self->allocated = self->ob_size; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1227 | nread = fread(item + (self->ob_size - n) * itemsize, |
| 1228 | itemsize, n, fp); |
Guido van Rossum | 7d0ae5e | 2000-06-28 21:27:21 +0000 | [diff] [blame] | 1229 | if (nread < (size_t)n) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1230 | self->ob_size -= (n - nread); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1231 | PyMem_RESIZE(item, char, self->ob_size*itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1232 | self->ob_item = item; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 1233 | self->allocated = self->ob_size; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1234 | PyErr_SetString(PyExc_EOFError, |
| 1235 | "not enough items in file"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1236 | return NULL; |
| 1237 | } |
| 1238 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1239 | Py_INCREF(Py_None); |
| 1240 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1243 | PyDoc_STRVAR(fromfile_doc, |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1244 | "fromfile(f, n)\n\ |
| 1245 | \n\ |
| 1246 | 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] | 1247 | array. Also called as read."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1248 | |
| 1249 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1250 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1251 | array_tofile(arrayobject *self, PyObject *f) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1252 | { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1253 | FILE *fp; |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1254 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1255 | fp = PyFile_AsFile(f); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1256 | if (fp == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1257 | PyErr_SetString(PyExc_TypeError, "arg must be open file"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1258 | return NULL; |
| 1259 | } |
| 1260 | if (self->ob_size > 0) { |
Trent Mick | 6c116dd | 2000-08-12 20:58:11 +0000 | [diff] [blame] | 1261 | if (fwrite(self->ob_item, self->ob_descr->itemsize, |
| 1262 | self->ob_size, fp) != (size_t)self->ob_size) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1263 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1264 | clearerr(fp); |
| 1265 | return NULL; |
| 1266 | } |
| 1267 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1268 | Py_INCREF(Py_None); |
| 1269 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1272 | PyDoc_STRVAR(tofile_doc, |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1273 | "tofile(f)\n\ |
| 1274 | \n\ |
| 1275 | 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] | 1276 | write."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1277 | |
| 1278 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1279 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1280 | array_fromlist(arrayobject *self, PyObject *list) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1281 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1282 | Py_ssize_t n; |
| 1283 | Py_ssize_t itemsize = self->ob_descr->itemsize; |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1284 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1285 | if (!PyList_Check(list)) { |
| 1286 | PyErr_SetString(PyExc_TypeError, "arg must be list"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1287 | return NULL; |
| 1288 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1289 | n = PyList_Size(list); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1290 | if (n > 0) { |
| 1291 | char *item = self->ob_item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1292 | Py_ssize_t i; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1293 | PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1294 | if (item == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1295 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1296 | return NULL; |
| 1297 | } |
| 1298 | self->ob_item = item; |
| 1299 | self->ob_size += n; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 1300 | self->allocated = self->ob_size; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1301 | for (i = 0; i < n; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1302 | PyObject *v = PyList_GetItem(list, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1303 | if ((*self->ob_descr->setitem)(self, |
| 1304 | self->ob_size - n + i, v) != 0) { |
| 1305 | self->ob_size -= n; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1306 | PyMem_RESIZE(item, char, |
| 1307 | self->ob_size * itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1308 | self->ob_item = item; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 1309 | self->allocated = self->ob_size; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1310 | return NULL; |
| 1311 | } |
| 1312 | } |
| 1313 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1314 | Py_INCREF(Py_None); |
| 1315 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1318 | PyDoc_STRVAR(fromlist_doc, |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1319 | "fromlist(list)\n\ |
| 1320 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1321 | Append items to array from list."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1322 | |
| 1323 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1324 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1325 | array_tolist(arrayobject *self, PyObject *unused) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1326 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1327 | PyObject *list = PyList_New(self->ob_size); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1328 | Py_ssize_t i; |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1329 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1330 | if (list == NULL) |
| 1331 | return NULL; |
| 1332 | for (i = 0; i < self->ob_size; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1333 | PyObject *v = getarrayitem((PyObject *)self, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1334 | if (v == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1335 | Py_DECREF(list); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1336 | return NULL; |
| 1337 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1338 | PyList_SetItem(list, i, v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1339 | } |
| 1340 | return list; |
| 1341 | } |
| 1342 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1343 | PyDoc_STRVAR(tolist_doc, |
Guido van Rossum | fc6aba5 | 1998-10-14 02:52:31 +0000 | [diff] [blame] | 1344 | "tolist() -> list\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1345 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1346 | Convert array to an ordinary list with the same items."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1347 | |
| 1348 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1349 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 1350 | array_fromstring(arrayobject *self, PyObject *args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1351 | { |
| 1352 | char *str; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1353 | Py_ssize_t n; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1354 | int itemsize = self->ob_descr->itemsize; |
Peter Schneider-Kamp | dc71cac | 2000-07-31 21:57:30 +0000 | [diff] [blame] | 1355 | if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1356 | return NULL; |
| 1357 | if (n % itemsize != 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1358 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1359 | "string length not a multiple of item size"); |
| 1360 | return NULL; |
| 1361 | } |
| 1362 | n = n / itemsize; |
| 1363 | if (n > 0) { |
| 1364 | char *item = self->ob_item; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1365 | PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1366 | if (item == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1367 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1368 | return NULL; |
| 1369 | } |
| 1370 | self->ob_item = item; |
| 1371 | self->ob_size += n; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 1372 | self->allocated = self->ob_size; |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1373 | memcpy(item + (self->ob_size - n) * itemsize, |
| 1374 | str, itemsize*n); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1375 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1376 | Py_INCREF(Py_None); |
| 1377 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1380 | PyDoc_STRVAR(fromstring_doc, |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1381 | "fromstring(string)\n\ |
| 1382 | \n\ |
| 1383 | 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] | 1384 | 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] | 1385 | |
| 1386 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1387 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1388 | array_tostring(arrayobject *self, PyObject *unused) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1389 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1390 | return PyString_FromStringAndSize(self->ob_item, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1391 | self->ob_size * self->ob_descr->itemsize); |
| 1392 | } |
| 1393 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1394 | PyDoc_STRVAR(tostring_doc, |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1395 | "tostring() -> string\n\ |
| 1396 | \n\ |
| 1397 | 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] | 1398 | representation."); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1399 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1400 | |
| 1401 | |
| 1402 | #ifdef Py_USING_UNICODE |
| 1403 | static PyObject * |
| 1404 | array_fromunicode(arrayobject *self, PyObject *args) |
| 1405 | { |
| 1406 | Py_UNICODE *ustr; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1407 | Py_ssize_t n; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1408 | |
| 1409 | if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n)) |
| 1410 | return NULL; |
| 1411 | if (self->ob_descr->typecode != 'u') { |
| 1412 | PyErr_SetString(PyExc_ValueError, |
| 1413 | "fromunicode() may only be called on " |
| 1414 | "type 'u' arrays"); |
| 1415 | return NULL; |
| 1416 | } |
| 1417 | if (n > 0) { |
| 1418 | Py_UNICODE *item = (Py_UNICODE *) self->ob_item; |
| 1419 | PyMem_RESIZE(item, Py_UNICODE, self->ob_size + n); |
| 1420 | if (item == NULL) { |
| 1421 | PyErr_NoMemory(); |
| 1422 | return NULL; |
| 1423 | } |
| 1424 | self->ob_item = (char *) item; |
| 1425 | self->ob_size += n; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 1426 | self->allocated = self->ob_size; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1427 | memcpy(item + self->ob_size - n, |
| 1428 | ustr, n * sizeof(Py_UNICODE)); |
| 1429 | } |
| 1430 | |
| 1431 | Py_INCREF(Py_None); |
| 1432 | return Py_None; |
| 1433 | } |
| 1434 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1435 | PyDoc_STRVAR(fromunicode_doc, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1436 | "fromunicode(ustr)\n\ |
| 1437 | \n\ |
| 1438 | Extends this array with data from the unicode string ustr.\n\ |
| 1439 | The array must be a type 'u' array; otherwise a ValueError\n\ |
| 1440 | is raised. Use array.fromstring(ustr.decode(...)) to\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1441 | append Unicode data to an array of some other type."); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1442 | |
| 1443 | |
| 1444 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1445 | array_tounicode(arrayobject *self, PyObject *unused) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1446 | { |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1447 | if (self->ob_descr->typecode != 'u') { |
| 1448 | PyErr_SetString(PyExc_ValueError, |
| 1449 | "tounicode() may only be called on type 'u' arrays"); |
| 1450 | return NULL; |
| 1451 | } |
| 1452 | return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, self->ob_size); |
| 1453 | } |
| 1454 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1455 | PyDoc_STRVAR(tounicode_doc, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1456 | "tounicode() -> unicode\n\ |
| 1457 | \n\ |
| 1458 | Convert the array to a unicode string. The array must be\n\ |
| 1459 | a type 'u' array; otherwise a ValueError is raised. Use\n\ |
| 1460 | array.tostring().decode() to obtain a unicode string from\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1461 | an array of some other type."); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1462 | |
| 1463 | #endif /* Py_USING_UNICODE */ |
| 1464 | |
| 1465 | |
| 1466 | static PyObject * |
| 1467 | array_get_typecode(arrayobject *a, void *closure) |
| 1468 | { |
| 1469 | char tc = a->ob_descr->typecode; |
| 1470 | return PyString_FromStringAndSize(&tc, 1); |
| 1471 | } |
| 1472 | |
| 1473 | static PyObject * |
| 1474 | array_get_itemsize(arrayobject *a, void *closure) |
| 1475 | { |
| 1476 | return PyInt_FromLong((long)a->ob_descr->itemsize); |
| 1477 | } |
| 1478 | |
| 1479 | static PyGetSetDef array_getsets [] = { |
| 1480 | {"typecode", (getter) array_get_typecode, NULL, |
| 1481 | "the typecode character used to create the array"}, |
| 1482 | {"itemsize", (getter) array_get_itemsize, NULL, |
| 1483 | "the size, in bytes, of one array item"}, |
| 1484 | {NULL} |
| 1485 | }; |
| 1486 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1487 | PyMethodDef array_methods[] = { |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1488 | {"append", (PyCFunction)array_append, METH_O, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1489 | append_doc}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1490 | {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1491 | buffer_info_doc}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1492 | {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1493 | byteswap_doc}, |
Raymond Hettinger | 3aa82c0 | 2004-03-13 18:18:51 +0000 | [diff] [blame] | 1494 | {"__copy__", (PyCFunction)array_copy, METH_NOARGS, |
| 1495 | copy_doc}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1496 | {"count", (PyCFunction)array_count, METH_O, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1497 | count_doc}, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1498 | {"__deepcopy__",(PyCFunction)array_copy, METH_O, |
Raymond Hettinger | 3aa82c0 | 2004-03-13 18:18:51 +0000 | [diff] [blame] | 1499 | copy_doc}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1500 | {"extend", (PyCFunction)array_extend, METH_O, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1501 | extend_doc}, |
| 1502 | {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS, |
| 1503 | fromfile_doc}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1504 | {"fromlist", (PyCFunction)array_fromlist, METH_O, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1505 | fromlist_doc}, |
| 1506 | {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS, |
| 1507 | fromstring_doc}, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1508 | #ifdef Py_USING_UNICODE |
| 1509 | {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS, |
| 1510 | fromunicode_doc}, |
| 1511 | #endif |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1512 | {"index", (PyCFunction)array_index, METH_O, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1513 | index_doc}, |
| 1514 | {"insert", (PyCFunction)array_insert, METH_VARARGS, |
| 1515 | insert_doc}, |
| 1516 | {"pop", (PyCFunction)array_pop, METH_VARARGS, |
| 1517 | pop_doc}, |
| 1518 | {"read", (PyCFunction)array_fromfile, METH_VARARGS, |
| 1519 | fromfile_doc}, |
Raymond Hettinger | b0900e6 | 2004-12-16 16:23:40 +0000 | [diff] [blame] | 1520 | {"__reduce__", (PyCFunction)array_reduce, METH_NOARGS, |
| 1521 | array_doc}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1522 | {"remove", (PyCFunction)array_remove, METH_O, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1523 | remove_doc}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1524 | {"reverse", (PyCFunction)array_reverse, METH_NOARGS, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1525 | reverse_doc}, |
| 1526 | /* {"sort", (PyCFunction)array_sort, METH_VARARGS, |
| 1527 | sort_doc},*/ |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1528 | {"tofile", (PyCFunction)array_tofile, METH_O, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1529 | tofile_doc}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1530 | {"tolist", (PyCFunction)array_tolist, METH_NOARGS, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1531 | tolist_doc}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1532 | {"tostring", (PyCFunction)array_tostring, METH_NOARGS, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1533 | tostring_doc}, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1534 | #ifdef Py_USING_UNICODE |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1535 | {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1536 | tounicode_doc}, |
| 1537 | #endif |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1538 | {"write", (PyCFunction)array_tofile, METH_O, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1539 | tofile_doc}, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1540 | {NULL, NULL} /* sentinel */ |
| 1541 | }; |
| 1542 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1543 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 1544 | array_repr(arrayobject *a) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1545 | { |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1546 | char buf[256], typecode; |
Raymond Hettinger | 88ba1e3 | 2003-04-23 17:27:00 +0000 | [diff] [blame] | 1547 | PyObject *s, *t, *v = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1548 | Py_ssize_t len; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1549 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1550 | len = a->ob_size; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1551 | typecode = a->ob_descr->typecode; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1552 | if (len == 0) { |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1553 | PyOS_snprintf(buf, sizeof(buf), "array('%c')", typecode); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1554 | return PyString_FromString(buf); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1555 | } |
Raymond Hettinger | 88ba1e3 | 2003-04-23 17:27:00 +0000 | [diff] [blame] | 1556 | |
| 1557 | if (typecode == 'c') |
| 1558 | v = array_tostring(a, NULL); |
Michael W. Hudson | 1755ad9 | 2002-05-13 10:14:59 +0000 | [diff] [blame] | 1559 | #ifdef Py_USING_UNICODE |
Raymond Hettinger | 88ba1e3 | 2003-04-23 17:27:00 +0000 | [diff] [blame] | 1560 | else if (typecode == 'u') |
| 1561 | v = array_tounicode(a, NULL); |
Michael W. Hudson | 1755ad9 | 2002-05-13 10:14:59 +0000 | [diff] [blame] | 1562 | #endif |
Raymond Hettinger | 88ba1e3 | 2003-04-23 17:27:00 +0000 | [diff] [blame] | 1563 | else |
| 1564 | v = array_tolist(a, NULL); |
| 1565 | t = PyObject_Repr(v); |
| 1566 | Py_XDECREF(v); |
| 1567 | |
| 1568 | PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1569 | s = PyString_FromString(buf); |
Raymond Hettinger | 88ba1e3 | 2003-04-23 17:27:00 +0000 | [diff] [blame] | 1570 | PyString_ConcatAndDel(&s, t); |
| 1571 | PyString_ConcatAndDel(&s, PyString_FromString(")")); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1572 | return s; |
| 1573 | } |
| 1574 | |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1575 | static PyObject* |
| 1576 | array_subscr(arrayobject* self, PyObject* item) |
| 1577 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1578 | if (PyIndex_Check(item)) { |
| 1579 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1580 | if (i==-1 && PyErr_Occurred()) { |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1581 | return NULL; |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1582 | } |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1583 | if (i < 0) |
| 1584 | i += self->ob_size; |
| 1585 | return array_item(self, i); |
| 1586 | } |
| 1587 | else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1588 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1589 | PyObject* result; |
| 1590 | arrayobject* ar; |
| 1591 | int itemsize = self->ob_descr->itemsize; |
| 1592 | |
| 1593 | if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size, |
| 1594 | &start, &stop, &step, &slicelength) < 0) { |
| 1595 | return NULL; |
| 1596 | } |
| 1597 | |
| 1598 | if (slicelength <= 0) { |
| 1599 | return newarrayobject(&Arraytype, 0, self->ob_descr); |
| 1600 | } |
| 1601 | else { |
| 1602 | result = newarrayobject(&Arraytype, slicelength, self->ob_descr); |
| 1603 | if (!result) return NULL; |
| 1604 | |
| 1605 | ar = (arrayobject*)result; |
| 1606 | |
| 1607 | for (cur = start, i = 0; i < slicelength; |
| 1608 | cur += step, i++) { |
| 1609 | memcpy(ar->ob_item + i*itemsize, |
| 1610 | self->ob_item + cur*itemsize, |
| 1611 | itemsize); |
| 1612 | } |
| 1613 | |
| 1614 | return result; |
| 1615 | } |
| 1616 | } |
| 1617 | else { |
| 1618 | PyErr_SetString(PyExc_TypeError, |
| 1619 | "list indices must be integers"); |
| 1620 | return NULL; |
| 1621 | } |
| 1622 | } |
| 1623 | |
| 1624 | static int |
| 1625 | array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value) |
| 1626 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1627 | if (PyIndex_Check(item)) { |
| 1628 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1629 | if (i==-1 && PyErr_Occurred()) |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1630 | return -1; |
| 1631 | if (i < 0) |
| 1632 | i += self->ob_size; |
| 1633 | return array_ass_item(self, i, value); |
| 1634 | } |
| 1635 | else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1636 | Py_ssize_t start, stop, step, slicelength; |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1637 | int itemsize = self->ob_descr->itemsize; |
| 1638 | |
| 1639 | if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size, |
| 1640 | &start, &stop, &step, &slicelength) < 0) { |
| 1641 | return -1; |
| 1642 | } |
| 1643 | |
| 1644 | /* treat A[slice(a,b)] = v _exactly_ like A[a:b] = v */ |
| 1645 | if (step == 1 && ((PySliceObject*)item)->step == Py_None) |
| 1646 | return array_ass_slice(self, start, stop, value); |
| 1647 | |
| 1648 | if (value == NULL) { |
| 1649 | /* delete slice */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1650 | Py_ssize_t cur, i, extra; |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1651 | |
| 1652 | if (slicelength <= 0) |
| 1653 | return 0; |
| 1654 | |
| 1655 | if (step < 0) { |
| 1656 | stop = start + 1; |
| 1657 | start = stop + step*(slicelength - 1) - 1; |
| 1658 | step = -step; |
| 1659 | } |
| 1660 | |
Michael W. Hudson | 56796f6 | 2002-07-29 14:35:04 +0000 | [diff] [blame] | 1661 | for (cur = start, i = 0; i < slicelength - 1; |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1662 | cur += step, i++) { |
| 1663 | memmove(self->ob_item + (cur - i)*itemsize, |
| 1664 | self->ob_item + (cur + 1)*itemsize, |
| 1665 | (step - 1) * itemsize); |
| 1666 | } |
Michael W. Hudson | 56796f6 | 2002-07-29 14:35:04 +0000 | [diff] [blame] | 1667 | extra = self->ob_size - (cur + 1); |
| 1668 | if (extra > 0) { |
| 1669 | memmove(self->ob_item + (cur - i)*itemsize, |
| 1670 | self->ob_item + (cur + 1)*itemsize, |
| 1671 | extra*itemsize); |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | self->ob_size -= slicelength; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1675 | self->ob_item = (char *)PyMem_REALLOC(self->ob_item, |
| 1676 | itemsize*self->ob_size); |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 1677 | self->allocated = self->ob_size; |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1678 | |
| 1679 | return 0; |
| 1680 | } |
| 1681 | else { |
| 1682 | /* assign slice */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1683 | Py_ssize_t cur, i; |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1684 | arrayobject* av; |
| 1685 | |
| 1686 | if (!array_Check(value)) { |
| 1687 | PyErr_Format(PyExc_TypeError, |
| 1688 | "must assign array (not \"%.200s\") to slice", |
| 1689 | value->ob_type->tp_name); |
| 1690 | return -1; |
| 1691 | } |
| 1692 | |
| 1693 | av = (arrayobject*)value; |
| 1694 | |
| 1695 | if (av->ob_size != slicelength) { |
| 1696 | PyErr_Format(PyExc_ValueError, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1697 | "attempt to assign array of size %ld to extended slice of size %ld", |
| 1698 | /*XXX*/(long)av->ob_size, /*XXX*/(long)slicelength); |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1699 | return -1; |
| 1700 | } |
| 1701 | |
| 1702 | if (!slicelength) |
| 1703 | return 0; |
| 1704 | |
| 1705 | /* protect against a[::-1] = a */ |
| 1706 | if (self == av) { |
| 1707 | value = array_slice(av, 0, av->ob_size); |
| 1708 | av = (arrayobject*)value; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1709 | if (!av) |
| 1710 | return -1; |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1711 | } |
| 1712 | else { |
| 1713 | Py_INCREF(value); |
| 1714 | } |
| 1715 | |
| 1716 | for (cur = start, i = 0; i < slicelength; |
| 1717 | cur += step, i++) { |
| 1718 | memcpy(self->ob_item + cur*itemsize, |
| 1719 | av->ob_item + i*itemsize, |
| 1720 | itemsize); |
| 1721 | } |
| 1722 | |
| 1723 | Py_DECREF(value); |
| 1724 | |
| 1725 | return 0; |
| 1726 | } |
| 1727 | } |
| 1728 | else { |
| 1729 | PyErr_SetString(PyExc_TypeError, |
| 1730 | "list indices must be integers"); |
| 1731 | return -1; |
| 1732 | } |
| 1733 | } |
| 1734 | |
| 1735 | static PyMappingMethods array_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1736 | (lenfunc)array_length, |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 1737 | (binaryfunc)array_subscr, |
| 1738 | (objobjargproc)array_ass_subscr |
| 1739 | }; |
| 1740 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1741 | static Py_ssize_t |
| 1742 | 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] | 1743 | { |
| 1744 | if ( index != 0 ) { |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1745 | PyErr_SetString(PyExc_SystemError, |
| 1746 | "Accessing non-existent array segment"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1747 | return -1; |
| 1748 | } |
| 1749 | *ptr = (void *)self->ob_item; |
| 1750 | return self->ob_size*self->ob_descr->itemsize; |
| 1751 | } |
| 1752 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1753 | static Py_ssize_t |
| 1754 | 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] | 1755 | { |
| 1756 | if ( index != 0 ) { |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1757 | PyErr_SetString(PyExc_SystemError, |
| 1758 | "Accessing non-existent array segment"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1759 | return -1; |
| 1760 | } |
| 1761 | *ptr = (void *)self->ob_item; |
| 1762 | return self->ob_size*self->ob_descr->itemsize; |
| 1763 | } |
| 1764 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1765 | static Py_ssize_t |
| 1766 | array_buffer_getsegcount(arrayobject *self, Py_ssize_t *lenp) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1767 | { |
| 1768 | if ( lenp ) |
| 1769 | *lenp = self->ob_size*self->ob_descr->itemsize; |
| 1770 | return 1; |
| 1771 | } |
| 1772 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1773 | static PySequenceMethods array_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1774 | (lenfunc)array_length, /*sq_length*/ |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1775 | (binaryfunc)array_concat, /*sq_concat*/ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1776 | (ssizeargfunc)array_repeat, /*sq_repeat*/ |
| 1777 | (ssizeargfunc)array_item, /*sq_item*/ |
| 1778 | (ssizessizeargfunc)array_slice, /*sq_slice*/ |
| 1779 | (ssizeobjargproc)array_ass_item, /*sq_ass_item*/ |
| 1780 | (ssizessizeobjargproc)array_ass_slice, /*sq_ass_slice*/ |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 1781 | (objobjproc)array_contains, /*sq_contains*/ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1782 | (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1783 | (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1784 | }; |
| 1785 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1786 | static PyBufferProcs array_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1787 | (readbufferproc)array_buffer_getreadbuf, |
| 1788 | (writebufferproc)array_buffer_getwritebuf, |
| 1789 | (segcountproc)array_buffer_getsegcount, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1790 | NULL, |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1791 | }; |
| 1792 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1793 | static PyObject * |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1794 | array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1795 | { |
| 1796 | char c; |
Raymond Hettinger | 6ab78cd | 2004-08-29 07:50:43 +0000 | [diff] [blame] | 1797 | PyObject *initial = NULL, *it = NULL; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1798 | struct arraydescr *descr; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1799 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame^] | 1800 | if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds)) |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1801 | return NULL; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1802 | |
| 1803 | if (!PyArg_ParseTuple(args, "c|O:array", &c, &initial)) |
| 1804 | return NULL; |
| 1805 | |
| 1806 | if (!(initial == NULL || PyList_Check(initial) |
Raymond Hettinger | 84fc9aa | 2003-04-24 10:41:55 +0000 | [diff] [blame] | 1807 | || PyString_Check(initial) || PyTuple_Check(initial) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1808 | || (c == 'u' && PyUnicode_Check(initial)))) { |
Raymond Hettinger | 6ab78cd | 2004-08-29 07:50:43 +0000 | [diff] [blame] | 1809 | it = PyObject_GetIter(initial); |
| 1810 | if (it == NULL) |
| 1811 | return NULL; |
| 1812 | /* We set initial to NULL so that the subsequent code |
| 1813 | will create an empty array of the appropriate type |
| 1814 | and afterwards we can use array_iter_extend to populate |
| 1815 | the array. |
| 1816 | */ |
| 1817 | initial = NULL; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1818 | } |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1819 | for (descr = descriptors; descr->typecode != '\0'; descr++) { |
| 1820 | if (descr->typecode == c) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1821 | PyObject *a; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1822 | Py_ssize_t len; |
Raymond Hettinger | 84fc9aa | 2003-04-24 10:41:55 +0000 | [diff] [blame] | 1823 | |
| 1824 | if (initial == NULL || !(PyList_Check(initial) |
| 1825 | || PyTuple_Check(initial))) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1826 | len = 0; |
| 1827 | else |
Raymond Hettinger | 84fc9aa | 2003-04-24 10:41:55 +0000 | [diff] [blame] | 1828 | len = PySequence_Size(initial); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1829 | |
| 1830 | a = newarrayobject(type, len, descr); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1831 | if (a == NULL) |
| 1832 | return NULL; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1833 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1834 | if (len > 0) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1835 | Py_ssize_t i; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1836 | for (i = 0; i < len; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1837 | PyObject *v = |
Raymond Hettinger | 84fc9aa | 2003-04-24 10:41:55 +0000 | [diff] [blame] | 1838 | PySequence_GetItem(initial, i); |
Raymond Hettinger | 85004cc | 2003-08-05 11:23:59 +0000 | [diff] [blame] | 1839 | if (v == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1840 | Py_DECREF(a); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1841 | return NULL; |
| 1842 | } |
Raymond Hettinger | 85004cc | 2003-08-05 11:23:59 +0000 | [diff] [blame] | 1843 | if (setarrayitem(a, i, v) != 0) { |
| 1844 | Py_DECREF(v); |
| 1845 | Py_DECREF(a); |
| 1846 | return NULL; |
| 1847 | } |
| 1848 | Py_DECREF(v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1849 | } |
Raymond Hettinger | 85004cc | 2003-08-05 11:23:59 +0000 | [diff] [blame] | 1850 | } else if (initial != NULL && PyString_Check(initial)) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1851 | PyObject *t_initial, *v; |
| 1852 | t_initial = PyTuple_Pack(1, initial); |
| 1853 | if (t_initial == NULL) { |
| 1854 | Py_DECREF(a); |
| 1855 | return NULL; |
| 1856 | } |
| 1857 | v = array_fromstring((arrayobject *)a, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1858 | t_initial); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1859 | Py_DECREF(t_initial); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1860 | if (v == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1861 | Py_DECREF(a); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1862 | return NULL; |
| 1863 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1864 | Py_DECREF(v); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1865 | #ifdef Py_USING_UNICODE |
| 1866 | } else if (initial != NULL && PyUnicode_Check(initial)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1867 | Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1868 | if (n > 0) { |
| 1869 | arrayobject *self = (arrayobject *)a; |
| 1870 | char *item = self->ob_item; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1871 | item = (char *)PyMem_Realloc(item, n); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1872 | if (item == NULL) { |
| 1873 | PyErr_NoMemory(); |
| 1874 | Py_DECREF(a); |
| 1875 | return NULL; |
| 1876 | } |
| 1877 | self->ob_item = item; |
| 1878 | self->ob_size = n / sizeof(Py_UNICODE); |
| 1879 | memcpy(item, PyUnicode_AS_DATA(initial), n); |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 1880 | self->allocated = self->ob_size; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1881 | } |
| 1882 | #endif |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1883 | } |
Raymond Hettinger | 6ab78cd | 2004-08-29 07:50:43 +0000 | [diff] [blame] | 1884 | if (it != NULL) { |
| 1885 | if (array_iter_extend((arrayobject *)a, it) == -1) { |
| 1886 | Py_DECREF(it); |
| 1887 | Py_DECREF(a); |
| 1888 | return NULL; |
| 1889 | } |
| 1890 | Py_DECREF(it); |
| 1891 | } |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1892 | return a; |
| 1893 | } |
| 1894 | } |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1895 | PyErr_SetString(PyExc_ValueError, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1896 | "bad typecode (must be c, b, B, u, h, H, i, I, l, L, f or d)"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1897 | return NULL; |
| 1898 | } |
| 1899 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1900 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1901 | PyDoc_STRVAR(module_doc, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1902 | "This module defines an object type which can efficiently represent\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1903 | an array of basic values: characters, integers, floating point\n\ |
| 1904 | numbers. Arrays are sequence types and behave very much like lists,\n\ |
| 1905 | except that the type of objects stored in them is constrained. The\n\ |
| 1906 | type is specified at object creation time by using a type code, which\n\ |
| 1907 | is a single character. The following type codes are defined:\n\ |
| 1908 | \n\ |
| 1909 | Type code C Type Minimum size in bytes \n\ |
| 1910 | 'c' character 1 \n\ |
| 1911 | 'b' signed integer 1 \n\ |
| 1912 | 'B' unsigned integer 1 \n\ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1913 | 'u' Unicode character 2 \n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1914 | 'h' signed integer 2 \n\ |
| 1915 | 'H' unsigned integer 2 \n\ |
| 1916 | 'i' signed integer 2 \n\ |
| 1917 | 'I' unsigned integer 2 \n\ |
| 1918 | 'l' signed integer 4 \n\ |
| 1919 | 'L' unsigned integer 4 \n\ |
| 1920 | 'f' floating point 4 \n\ |
| 1921 | 'd' floating point 8 \n\ |
| 1922 | \n\ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1923 | The constructor is:\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1924 | \n\ |
| 1925 | array(typecode [, initializer]) -- create a new array\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1926 | "); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1927 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1928 | PyDoc_STRVAR(arraytype_doc, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1929 | "array(typecode [, initializer]) -> array\n\ |
| 1930 | \n\ |
| 1931 | Return a new array whose items are restricted by typecode, and\n\ |
Raymond Hettinger | 6ab78cd | 2004-08-29 07:50:43 +0000 | [diff] [blame] | 1932 | initialized from the optional initializer value, which must be a list,\n\ |
| 1933 | string. or iterable over elements of the appropriate type.\n\ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1934 | \n\ |
| 1935 | 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] | 1936 | the type of objects stored in them is constrained.\n\ |
| 1937 | \n\ |
| 1938 | Methods:\n\ |
| 1939 | \n\ |
| 1940 | append() -- append a new item to the end of the array\n\ |
| 1941 | buffer_info() -- return information giving the current memory info\n\ |
| 1942 | byteswap() -- byteswap all the items of the array\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1943 | count() -- return number of occurences of an object\n\ |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 1944 | extend() -- extend array by appending multiple elements from an iterable\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1945 | fromfile() -- read items from a file object\n\ |
| 1946 | fromlist() -- append items from the list\n\ |
| 1947 | fromstring() -- append items from the string\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1948 | index() -- return index of first occurence of an object\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1949 | 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] | 1950 | pop() -- remove and return item (default last)\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1951 | read() -- DEPRECATED, use fromfile()\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1952 | remove() -- remove first occurence of an object\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1953 | reverse() -- reverse the order of the items in the array\n\ |
| 1954 | tofile() -- write all items to a file object\n\ |
| 1955 | tolist() -- return the array converted to an ordinary list\n\ |
| 1956 | tostring() -- return the array converted to a string\n\ |
| 1957 | write() -- DEPRECATED, use tofile()\n\ |
| 1958 | \n\ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1959 | Attributes:\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1960 | \n\ |
| 1961 | typecode -- the typecode character used to create the array\n\ |
| 1962 | itemsize -- the length in bytes of one array item\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1963 | "); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1964 | |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 1965 | static PyObject *array_iter(arrayobject *ao); |
| 1966 | |
Tim Peters | 0c32279 | 2002-07-17 16:49:03 +0000 | [diff] [blame] | 1967 | static PyTypeObject Arraytype = { |
Fred Drake | 0d40ba4 | 2000-02-04 20:33:49 +0000 | [diff] [blame] | 1968 | PyObject_HEAD_INIT(NULL) |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1969 | 0, |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 1970 | "array.array", |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1971 | sizeof(arrayobject), |
| 1972 | 0, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1973 | (destructor)array_dealloc, /* tp_dealloc */ |
Martin v. Löwis | aa158be | 2002-03-04 09:38:52 +0000 | [diff] [blame] | 1974 | 0, /* tp_print */ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1975 | 0, /* tp_getattr */ |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1976 | 0, /* tp_setattr */ |
| 1977 | 0, /* tp_compare */ |
| 1978 | (reprfunc)array_repr, /* tp_repr */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1979 | 0, /* tp_as_number*/ |
| 1980 | &array_as_sequence, /* tp_as_sequence*/ |
| 1981 | &array_as_mapping, /* tp_as_mapping*/ |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1982 | 0, /* tp_hash */ |
| 1983 | 0, /* tp_call */ |
| 1984 | 0, /* tp_str */ |
Jason Tishler | fb8595d | 2003-01-06 12:41:26 +0000 | [diff] [blame] | 1985 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1986 | 0, /* tp_setattro */ |
Tim Peters | b870c75 | 2001-06-05 04:43:26 +0000 | [diff] [blame] | 1987 | &array_as_buffer, /* tp_as_buffer*/ |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 1988 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1989 | arraytype_doc, /* tp_doc */ |
| 1990 | 0, /* tp_traverse */ |
| 1991 | 0, /* tp_clear */ |
| 1992 | array_richcompare, /* tp_richcompare */ |
Raymond Hettinger | cb87bc8 | 2004-05-31 00:35:52 +0000 | [diff] [blame] | 1993 | offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */ |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 1994 | (getiterfunc)array_iter, /* tp_iter */ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1995 | 0, /* tp_iternext */ |
| 1996 | array_methods, /* tp_methods */ |
| 1997 | 0, /* tp_members */ |
| 1998 | array_getsets, /* tp_getset */ |
| 1999 | 0, /* tp_base */ |
| 2000 | 0, /* tp_dict */ |
| 2001 | 0, /* tp_descr_get */ |
| 2002 | 0, /* tp_descr_set */ |
| 2003 | 0, /* tp_dictoffset */ |
| 2004 | 0, /* tp_init */ |
Jason Tishler | fb8595d | 2003-01-06 12:41:26 +0000 | [diff] [blame] | 2005 | PyType_GenericAlloc, /* tp_alloc */ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2006 | array_new, /* tp_new */ |
Jason Tishler | fb8595d | 2003-01-06 12:41:26 +0000 | [diff] [blame] | 2007 | PyObject_Del, /* tp_free */ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2008 | }; |
| 2009 | |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2010 | |
| 2011 | /*********************** Array Iterator **************************/ |
| 2012 | |
| 2013 | typedef struct { |
| 2014 | PyObject_HEAD |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2015 | Py_ssize_t index; |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2016 | arrayobject *ao; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2017 | PyObject * (*getitem)(struct arrayobject *, Py_ssize_t); |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2018 | } arrayiterobject; |
| 2019 | |
| 2020 | static PyTypeObject PyArrayIter_Type; |
| 2021 | |
| 2022 | #define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type) |
| 2023 | |
| 2024 | static PyObject * |
| 2025 | array_iter(arrayobject *ao) |
| 2026 | { |
| 2027 | arrayiterobject *it; |
| 2028 | |
| 2029 | if (!array_Check(ao)) { |
| 2030 | PyErr_BadInternalCall(); |
| 2031 | return NULL; |
| 2032 | } |
| 2033 | |
| 2034 | it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type); |
| 2035 | if (it == NULL) |
| 2036 | return NULL; |
| 2037 | |
| 2038 | Py_INCREF(ao); |
| 2039 | it->ao = ao; |
| 2040 | it->index = 0; |
| 2041 | it->getitem = ao->ob_descr->getitem; |
| 2042 | PyObject_GC_Track(it); |
| 2043 | return (PyObject *)it; |
| 2044 | } |
| 2045 | |
| 2046 | static PyObject * |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2047 | arrayiter_next(arrayiterobject *it) |
| 2048 | { |
| 2049 | assert(PyArrayIter_Check(it)); |
| 2050 | if (it->index < it->ao->ob_size) |
| 2051 | return (*it->getitem)(it->ao, it->index++); |
| 2052 | return NULL; |
| 2053 | } |
| 2054 | |
| 2055 | static void |
| 2056 | arrayiter_dealloc(arrayiterobject *it) |
| 2057 | { |
| 2058 | PyObject_GC_UnTrack(it); |
| 2059 | Py_XDECREF(it->ao); |
| 2060 | PyObject_GC_Del(it); |
| 2061 | } |
| 2062 | |
| 2063 | static int |
| 2064 | arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg) |
| 2065 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2066 | Py_VISIT(it->ao); |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2067 | return 0; |
| 2068 | } |
| 2069 | |
| 2070 | static PyTypeObject PyArrayIter_Type = { |
Jason Tishler | 8437570 | 2003-02-10 20:45:47 +0000 | [diff] [blame] | 2071 | PyObject_HEAD_INIT(NULL) |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2072 | 0, /* ob_size */ |
| 2073 | "arrayiterator", /* tp_name */ |
| 2074 | sizeof(arrayiterobject), /* tp_basicsize */ |
| 2075 | 0, /* tp_itemsize */ |
| 2076 | /* methods */ |
| 2077 | (destructor)arrayiter_dealloc, /* tp_dealloc */ |
| 2078 | 0, /* tp_print */ |
| 2079 | 0, /* tp_getattr */ |
| 2080 | 0, /* tp_setattr */ |
| 2081 | 0, /* tp_compare */ |
| 2082 | 0, /* tp_repr */ |
| 2083 | 0, /* tp_as_number */ |
| 2084 | 0, /* tp_as_sequence */ |
| 2085 | 0, /* tp_as_mapping */ |
| 2086 | 0, /* tp_hash */ |
| 2087 | 0, /* tp_call */ |
| 2088 | 0, /* tp_str */ |
| 2089 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2090 | 0, /* tp_setattro */ |
| 2091 | 0, /* tp_as_buffer */ |
| 2092 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 2093 | 0, /* tp_doc */ |
| 2094 | (traverseproc)arrayiter_traverse, /* tp_traverse */ |
| 2095 | 0, /* tp_clear */ |
| 2096 | 0, /* tp_richcompare */ |
| 2097 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 2098 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2099 | (iternextfunc)arrayiter_next, /* tp_iternext */ |
| 2100 | 0, /* tp_methods */ |
| 2101 | }; |
| 2102 | |
| 2103 | |
| 2104 | /*********************** Install Module **************************/ |
| 2105 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2106 | /* No functions in array module. */ |
| 2107 | static PyMethodDef a_methods[] = { |
| 2108 | {NULL, NULL, 0, NULL} /* Sentinel */ |
| 2109 | }; |
| 2110 | |
| 2111 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 2112 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 2113 | initarray(void) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 2114 | { |
Fred Drake | f4e3484 | 2002-04-01 03:45:06 +0000 | [diff] [blame] | 2115 | PyObject *m; |
Fred Drake | 0d40ba4 | 2000-02-04 20:33:49 +0000 | [diff] [blame] | 2116 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 2117 | if (PyType_Ready(&Arraytype) < 0) |
| 2118 | return; |
Jason Tishler | 8437570 | 2003-02-10 20:45:47 +0000 | [diff] [blame] | 2119 | PyArrayIter_Type.ob_type = &PyType_Type; |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2120 | m = Py_InitModule3("array", a_methods, module_doc); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 2121 | if (m == NULL) |
| 2122 | return; |
Fred Drake | f4e3484 | 2002-04-01 03:45:06 +0000 | [diff] [blame] | 2123 | |
| 2124 | Py_INCREF((PyObject *)&Arraytype); |
| 2125 | PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype); |
| 2126 | Py_INCREF((PyObject *)&Arraytype); |
| 2127 | PyModule_AddObject(m, "array", (PyObject *)&Arraytype); |
Guido van Rossum | a0deb64 | 1998-10-14 13:45:06 +0000 | [diff] [blame] | 2128 | /* 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] | 2129 | } |