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