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