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