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