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