Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
| 11 | /* Array object implementation */ |
| 12 | |
| 13 | /* An array is a uniform list -- all items have the same type. |
| 14 | The item type is restricted to simple C types like int or float */ |
| 15 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 16 | #include "Python.h" |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | 0c70954 | 1994-08-19 12:01:32 +0000 | [diff] [blame] | 18 | #ifdef STDC_HEADERS |
| 19 | #include <stddef.h> |
Guido van Rossum | 7f1de83 | 1999-08-27 20:33:52 +0000 | [diff] [blame] | 20 | #else /* !STDC_HEADERS */ |
| 21 | #ifndef DONT_HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 22 | #include <sys/types.h> /* For size_t */ |
Guido van Rossum | 7f1de83 | 1999-08-27 20:33:52 +0000 | [diff] [blame] | 23 | #endif /* DONT_HAVE_SYS_TYPES_H */ |
| 24 | #endif /* !STDC_HEADERS */ |
Guido van Rossum | db67739 | 2000-07-01 01:09:43 +0000 | [diff] [blame] | 25 | #ifdef HAVE_LIMITS_H |
| 26 | #include <limits.h> |
| 27 | #endif /* HAVE_LIMITS_H */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 28 | |
| 29 | struct arrayobject; /* Forward */ |
| 30 | |
| 31 | struct arraydescr { |
| 32 | int typecode; |
| 33 | int itemsize; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 34 | PyObject * (*getitem) Py_FPROTO((struct arrayobject *, int)); |
| 35 | int (*setitem) Py_FPROTO((struct arrayobject *, int, PyObject *)); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | typedef struct arrayobject { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 39 | PyObject_VAR_HEAD |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 40 | char *ob_item; |
| 41 | struct arraydescr *ob_descr; |
| 42 | } arrayobject; |
| 43 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 44 | staticforward PyTypeObject Arraytype; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 45 | |
| 46 | #define is_arrayobject(op) ((op)->ob_type == &Arraytype) |
| 47 | |
Guido van Rossum | b73cc04 | 1993-11-01 16:28:59 +0000 | [diff] [blame] | 48 | /* Forward */ |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 49 | static PyObject *newarrayobject Py_PROTO((int, struct arraydescr *)); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 50 | #if 0 |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 51 | static int getarraysize Py_PROTO((PyObject *)); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 52 | #endif |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 53 | static PyObject *getarrayitem Py_PROTO((PyObject *, int)); |
| 54 | static int setarrayitem Py_PROTO((PyObject *, int, PyObject *)); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 55 | #if 0 |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 56 | static int insarrayitem Py_PROTO((PyObject *, int, PyObject *)); |
| 57 | static int addarrayitem Py_PROTO((PyObject *, PyObject *)); |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 58 | #endif |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 59 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 60 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 61 | c_getitem(ap, i) |
| 62 | arrayobject *ap; |
| 63 | int i; |
| 64 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 65 | return PyString_FromStringAndSize(&((char *)ap->ob_item)[i], 1); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static int |
| 69 | c_setitem(ap, i, v) |
| 70 | arrayobject *ap; |
| 71 | int i; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 72 | PyObject *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 73 | { |
| 74 | char x; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 75 | if (!PyArg_Parse(v, "c;array item must be char", &x)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 76 | return -1; |
| 77 | if (i >= 0) |
| 78 | ((char *)ap->ob_item)[i] = x; |
| 79 | return 0; |
| 80 | } |
| 81 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 82 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 83 | b_getitem(ap, i) |
| 84 | arrayobject *ap; |
| 85 | int i; |
| 86 | { |
| 87 | long x = ((char *)ap->ob_item)[i]; |
| 88 | if (x >= 128) |
| 89 | x -= 256; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 90 | return PyInt_FromLong(x); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static int |
| 94 | b_setitem(ap, i, v) |
| 95 | arrayobject *ap; |
| 96 | int i; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 97 | PyObject *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 98 | { |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 99 | short x; |
| 100 | /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore |
| 101 | must use the next size up that is signed ('h') and manually do |
| 102 | the overflow checking */ |
| 103 | if (!PyArg_Parse(v, "h;array item must be integer", &x)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 104 | return -1; |
Guido van Rossum | 9f754e0 | 2000-07-01 00:38:19 +0000 | [diff] [blame] | 105 | else if (x < -128) { |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 106 | PyErr_SetString(PyExc_OverflowError, |
| 107 | "signed char is less than minimum"); |
| 108 | return -1; |
| 109 | } |
Guido van Rossum | 9f754e0 | 2000-07-01 00:38:19 +0000 | [diff] [blame] | 110 | else if (x > 127) { |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 111 | PyErr_SetString(PyExc_OverflowError, |
| 112 | "signed char is greater than maximum"); |
| 113 | return -1; |
| 114 | } |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 115 | if (i >= 0) |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 116 | ((char *)ap->ob_item)[i] = (char)x; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 117 | return 0; |
| 118 | } |
| 119 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 120 | static PyObject * |
Guido van Rossum | 1a747f8 | 1997-05-16 16:21:38 +0000 | [diff] [blame] | 121 | BB_getitem(ap, i) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 122 | arrayobject *ap; |
| 123 | int i; |
| 124 | { |
| 125 | long x = ((unsigned char *)ap->ob_item)[i]; |
| 126 | return PyInt_FromLong(x); |
| 127 | } |
| 128 | |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 129 | static int |
| 130 | BB_setitem(ap, i, v) |
| 131 | arrayobject *ap; |
| 132 | int i; |
| 133 | PyObject *v; |
| 134 | { |
| 135 | unsigned char x; |
| 136 | /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */ |
| 137 | if (!PyArg_Parse(v, "b;array item must be integer", &x)) |
| 138 | return -1; |
| 139 | if (i >= 0) |
| 140 | ((char *)ap->ob_item)[i] = x; |
| 141 | return 0; |
| 142 | } |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 143 | |
| 144 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 145 | h_getitem(ap, i) |
| 146 | arrayobject *ap; |
| 147 | int i; |
| 148 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 149 | return PyInt_FromLong((long) ((short *)ap->ob_item)[i]); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static int |
| 153 | h_setitem(ap, i, v) |
| 154 | arrayobject *ap; |
| 155 | int i; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 156 | PyObject *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 157 | { |
| 158 | short x; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 159 | /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */ |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 160 | if (!PyArg_Parse(v, "h;array item must be integer", &x)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 161 | return -1; |
| 162 | if (i >= 0) |
| 163 | ((short *)ap->ob_item)[i] = x; |
| 164 | return 0; |
| 165 | } |
| 166 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 167 | static PyObject * |
Guido van Rossum | 1a747f8 | 1997-05-16 16:21:38 +0000 | [diff] [blame] | 168 | HH_getitem(ap, i) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 169 | arrayobject *ap; |
| 170 | int i; |
| 171 | { |
| 172 | return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[i]); |
| 173 | } |
| 174 | |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 175 | static int |
| 176 | HH_setitem(ap, i, v) |
| 177 | arrayobject *ap; |
| 178 | int i; |
| 179 | PyObject *v; |
| 180 | { |
| 181 | int x; |
| 182 | /* PyArg_Parse's 'h' formatter is for a signed short, therefore |
| 183 | must use the next size up and manually do the overflow checking */ |
| 184 | if (!PyArg_Parse(v, "i;array item must be integer", &x)) |
| 185 | return -1; |
| 186 | else if (x < 0) { |
| 187 | PyErr_SetString(PyExc_OverflowError, |
| 188 | "unsigned short is less than minimum"); |
| 189 | return -1; |
| 190 | } |
| 191 | else if (x > USHRT_MAX) { |
| 192 | PyErr_SetString(PyExc_OverflowError, |
| 193 | "unsigned short is greater than maximum"); |
| 194 | return -1; |
| 195 | } |
| 196 | if (i >= 0) |
| 197 | ((short *)ap->ob_item)[i] = (short)x; |
| 198 | return 0; |
| 199 | } |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 200 | |
| 201 | static PyObject * |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 202 | i_getitem(ap, i) |
| 203 | arrayobject *ap; |
| 204 | int i; |
| 205 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 206 | return PyInt_FromLong((long) ((int *)ap->ob_item)[i]); |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static int |
| 210 | i_setitem(ap, i, v) |
| 211 | arrayobject *ap; |
| 212 | int i; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 213 | PyObject *v; |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 214 | { |
| 215 | int x; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 216 | /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */ |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 217 | if (!PyArg_Parse(v, "i;array item must be integer", &x)) |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 218 | return -1; |
| 219 | if (i >= 0) |
| 220 | ((int *)ap->ob_item)[i] = x; |
| 221 | return 0; |
| 222 | } |
| 223 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 224 | static PyObject * |
Guido van Rossum | 1a747f8 | 1997-05-16 16:21:38 +0000 | [diff] [blame] | 225 | II_getitem(ap, i) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 226 | arrayobject *ap; |
| 227 | int i; |
| 228 | { |
| 229 | return PyLong_FromUnsignedLong( |
| 230 | (unsigned long) ((unsigned int *)ap->ob_item)[i]); |
| 231 | } |
| 232 | |
| 233 | static int |
Guido van Rossum | 1a747f8 | 1997-05-16 16:21:38 +0000 | [diff] [blame] | 234 | II_setitem(ap, i, v) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 235 | arrayobject *ap; |
| 236 | int i; |
| 237 | PyObject *v; |
| 238 | { |
| 239 | unsigned long x; |
| 240 | if (PyLong_Check(v)) { |
| 241 | x = PyLong_AsUnsignedLong(v); |
| 242 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 243 | return -1; |
| 244 | } |
| 245 | else { |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 246 | long y; |
| 247 | if (!PyArg_Parse(v, "l;array item must be integer", &y)) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 248 | return -1; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 249 | if (y < 0) { |
| 250 | PyErr_SetString(PyExc_OverflowError, |
| 251 | "unsigned int is less than minimum"); |
| 252 | return -1; |
| 253 | } |
| 254 | x = (unsigned long)y; |
| 255 | |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 256 | } |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 257 | if (x > UINT_MAX) { |
| 258 | PyErr_SetString(PyExc_OverflowError, |
| 259 | "unsigned int is greater than maximum"); |
| 260 | return -1; |
| 261 | } |
| 262 | |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 263 | if (i >= 0) |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 264 | ((unsigned int *)ap->ob_item)[i] = (unsigned int)x; |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 269 | l_getitem(ap, i) |
| 270 | arrayobject *ap; |
| 271 | int i; |
| 272 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 273 | return PyInt_FromLong(((long *)ap->ob_item)[i]); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static int |
| 277 | l_setitem(ap, i, v) |
| 278 | arrayobject *ap; |
| 279 | int i; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 280 | PyObject *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 281 | { |
| 282 | long x; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 283 | if (!PyArg_Parse(v, "l;array item must be integer", &x)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 284 | return -1; |
| 285 | if (i >= 0) |
| 286 | ((long *)ap->ob_item)[i] = x; |
| 287 | return 0; |
| 288 | } |
| 289 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 290 | static PyObject * |
Guido van Rossum | 1a747f8 | 1997-05-16 16:21:38 +0000 | [diff] [blame] | 291 | LL_getitem(ap, i) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 292 | arrayobject *ap; |
| 293 | int i; |
| 294 | { |
| 295 | return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]); |
| 296 | } |
| 297 | |
| 298 | static int |
Guido van Rossum | 1a747f8 | 1997-05-16 16:21:38 +0000 | [diff] [blame] | 299 | LL_setitem(ap, i, v) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 300 | arrayobject *ap; |
| 301 | int i; |
| 302 | PyObject *v; |
| 303 | { |
| 304 | unsigned long x; |
| 305 | if (PyLong_Check(v)) { |
| 306 | x = PyLong_AsUnsignedLong(v); |
| 307 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 308 | return -1; |
| 309 | } |
| 310 | else { |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 311 | long y; |
| 312 | if (!PyArg_Parse(v, "l;array item must be integer", &y)) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 313 | return -1; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 314 | if (y < 0) { |
| 315 | PyErr_SetString(PyExc_OverflowError, |
| 316 | "unsigned long is less than minimum"); |
| 317 | return -1; |
| 318 | } |
| 319 | x = (unsigned long)y; |
| 320 | |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 321 | } |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 322 | if (x > ULONG_MAX) { |
| 323 | PyErr_SetString(PyExc_OverflowError, |
| 324 | "unsigned long is greater than maximum"); |
| 325 | return -1; |
| 326 | } |
| 327 | |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 328 | if (i >= 0) |
| 329 | ((unsigned long *)ap->ob_item)[i] = x; |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 334 | f_getitem(ap, i) |
| 335 | arrayobject *ap; |
| 336 | int i; |
| 337 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 338 | return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | static int |
| 342 | f_setitem(ap, i, v) |
| 343 | arrayobject *ap; |
| 344 | int i; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 345 | PyObject *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 346 | { |
| 347 | float x; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 348 | if (!PyArg_Parse(v, "f;array item must be float", &x)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 349 | return -1; |
| 350 | if (i >= 0) |
| 351 | ((float *)ap->ob_item)[i] = x; |
| 352 | return 0; |
| 353 | } |
| 354 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 355 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 356 | d_getitem(ap, i) |
| 357 | arrayobject *ap; |
| 358 | int i; |
| 359 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 360 | return PyFloat_FromDouble(((double *)ap->ob_item)[i]); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | static int |
| 364 | d_setitem(ap, i, v) |
| 365 | arrayobject *ap; |
| 366 | int i; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 367 | PyObject *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 368 | { |
| 369 | double x; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 370 | if (!PyArg_Parse(v, "d;array item must be float", &x)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 371 | return -1; |
| 372 | if (i >= 0) |
| 373 | ((double *)ap->ob_item)[i] = x; |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | /* Description of types */ |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 378 | static struct arraydescr descriptors[] = { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 379 | {'c', sizeof(char), c_getitem, c_setitem}, |
| 380 | {'b', sizeof(char), b_getitem, b_setitem}, |
Guido van Rossum | 1a747f8 | 1997-05-16 16:21:38 +0000 | [diff] [blame] | 381 | {'B', sizeof(char), BB_getitem, BB_setitem}, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 382 | {'h', sizeof(short), h_getitem, h_setitem}, |
Guido van Rossum | 1a747f8 | 1997-05-16 16:21:38 +0000 | [diff] [blame] | 383 | {'H', sizeof(short), HH_getitem, HH_setitem}, |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 384 | {'i', sizeof(int), i_getitem, i_setitem}, |
Guido van Rossum | 1a747f8 | 1997-05-16 16:21:38 +0000 | [diff] [blame] | 385 | {'I', sizeof(int), II_getitem, II_setitem}, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 386 | {'l', sizeof(long), l_getitem, l_setitem}, |
Guido van Rossum | 1a747f8 | 1997-05-16 16:21:38 +0000 | [diff] [blame] | 387 | {'L', sizeof(long), LL_getitem, LL_setitem}, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 388 | {'f', sizeof(float), f_getitem, f_setitem}, |
| 389 | {'d', sizeof(double), d_getitem, d_setitem}, |
| 390 | {'\0', 0, 0, 0} /* Sentinel */ |
| 391 | }; |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 392 | /* If we ever allow items larger than double, we must change reverse()! */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 393 | |
| 394 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 395 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 396 | newarrayobject(size, descr) |
| 397 | int size; |
| 398 | struct arraydescr *descr; |
| 399 | { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 400 | arrayobject *op; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 401 | size_t nbytes; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 402 | if (size < 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 403 | PyErr_BadInternalCall(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 404 | return NULL; |
| 405 | } |
| 406 | nbytes = size * descr->itemsize; |
| 407 | /* Check for overflow */ |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 408 | if (nbytes / descr->itemsize != (size_t)size) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 409 | return PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 410 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 411 | op = PyObject_NewVar(arrayobject, &Arraytype, size); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 412 | if (op == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 413 | return PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 414 | } |
| 415 | if (size <= 0) { |
| 416 | op->ob_item = NULL; |
| 417 | } |
| 418 | else { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 419 | op->ob_item = PyMem_NEW(char, nbytes); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 420 | if (op->ob_item == NULL) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 421 | PyObject_Del(op); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 422 | return PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 423 | } |
| 424 | } |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 425 | op->ob_descr = descr; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 426 | return (PyObject *) op; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 429 | #if 0 |
Guido van Rossum | 62de97f | 1995-01-22 00:48:41 +0000 | [diff] [blame] | 430 | static int |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 431 | getarraysize(op) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 432 | PyObject *op; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 433 | { |
| 434 | if (!is_arrayobject(op)) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 435 | PyErr_BadInternalCall(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 436 | return -1; |
| 437 | } |
| 438 | return ((arrayobject *)op) -> ob_size; |
| 439 | } |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 440 | #endif |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 441 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 442 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 443 | getarrayitem(op, i) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 444 | PyObject *op; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 445 | int i; |
| 446 | { |
| 447 | register arrayobject *ap; |
| 448 | if (!is_arrayobject(op)) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 449 | PyErr_BadInternalCall(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 450 | return NULL; |
| 451 | } |
| 452 | ap = (arrayobject *)op; |
| 453 | if (i < 0 || i >= ap->ob_size) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 454 | PyErr_SetString(PyExc_IndexError, "array index out of range"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 455 | return NULL; |
| 456 | } |
| 457 | return (*ap->ob_descr->getitem)(ap, i); |
| 458 | } |
| 459 | |
| 460 | static int |
| 461 | ins1(self, where, v) |
| 462 | arrayobject *self; |
| 463 | int where; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 464 | PyObject *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 465 | { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 466 | char *items; |
| 467 | if (v == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 468 | PyErr_BadInternalCall(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 469 | return -1; |
| 470 | } |
| 471 | if ((*self->ob_descr->setitem)(self, -1, v) < 0) |
| 472 | return -1; |
| 473 | items = self->ob_item; |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 474 | PyMem_RESIZE(items, char, |
| 475 | (self->ob_size+1) * self->ob_descr->itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 476 | if (items == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 477 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 478 | return -1; |
| 479 | } |
| 480 | if (where < 0) |
| 481 | where = 0; |
| 482 | if (where > self->ob_size) |
| 483 | where = self->ob_size; |
| 484 | memmove(items + (where+1)*self->ob_descr->itemsize, |
| 485 | items + where*self->ob_descr->itemsize, |
| 486 | (self->ob_size-where)*self->ob_descr->itemsize); |
| 487 | self->ob_item = items; |
| 488 | self->ob_size++; |
| 489 | return (*self->ob_descr->setitem)(self, where, v); |
| 490 | } |
| 491 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 492 | #if 0 |
Guido van Rossum | 62de97f | 1995-01-22 00:48:41 +0000 | [diff] [blame] | 493 | static int |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 494 | insarrayitem(op, where, newitem) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 495 | PyObject *op; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 496 | int where; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 497 | PyObject *newitem; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 498 | { |
| 499 | if (!is_arrayobject(op)) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 500 | PyErr_BadInternalCall(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 501 | return -1; |
| 502 | } |
| 503 | return ins1((arrayobject *)op, where, newitem); |
| 504 | } |
| 505 | |
Guido van Rossum | 62de97f | 1995-01-22 00:48:41 +0000 | [diff] [blame] | 506 | static int |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 507 | addarrayitem(op, newitem) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 508 | PyObject *op; |
| 509 | PyObject *newitem; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 510 | { |
| 511 | if (!is_arrayobject(op)) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 512 | PyErr_BadInternalCall(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 513 | return -1; |
| 514 | } |
| 515 | return ins1((arrayobject *)op, |
| 516 | (int) ((arrayobject *)op)->ob_size, newitem); |
| 517 | } |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 518 | #endif |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 519 | |
| 520 | /* Methods */ |
| 521 | |
| 522 | static void |
| 523 | array_dealloc(op) |
| 524 | arrayobject *op; |
| 525 | { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 526 | if (op->ob_item != NULL) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 527 | PyMem_DEL(op->ob_item); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 528 | PyObject_Del(op); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | static int |
| 532 | array_compare(v, w) |
| 533 | arrayobject *v, *w; |
| 534 | { |
| 535 | int len = (v->ob_size < w->ob_size) ? v->ob_size : w->ob_size; |
| 536 | int i; |
| 537 | for (i = 0; i < len; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 538 | PyObject *ai, *bi; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 539 | int cmp; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 540 | ai = getarrayitem((PyObject *)v, i); |
| 541 | bi = getarrayitem((PyObject *)w, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 542 | if (ai && bi) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 543 | cmp = PyObject_Compare(ai, bi); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 544 | else |
| 545 | cmp = -1; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 546 | Py_XDECREF(ai); |
| 547 | Py_XDECREF(bi); |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 548 | if (cmp != 0) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 549 | return cmp; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 550 | } |
| 551 | return v->ob_size - w->ob_size; |
| 552 | } |
| 553 | |
| 554 | static int |
| 555 | array_length(a) |
| 556 | arrayobject *a; |
| 557 | { |
| 558 | return a->ob_size; |
| 559 | } |
| 560 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 561 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 562 | array_item(a, i) |
| 563 | arrayobject *a; |
| 564 | int i; |
| 565 | { |
| 566 | if (i < 0 || i >= a->ob_size) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 567 | PyErr_SetString(PyExc_IndexError, "array index out of range"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 568 | return NULL; |
| 569 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 570 | return getarrayitem((PyObject *)a, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 573 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 574 | array_slice(a, ilow, ihigh) |
| 575 | arrayobject *a; |
| 576 | int ilow, ihigh; |
| 577 | { |
| 578 | arrayobject *np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 579 | if (ilow < 0) |
| 580 | ilow = 0; |
| 581 | else if (ilow > a->ob_size) |
| 582 | ilow = a->ob_size; |
| 583 | if (ihigh < 0) |
| 584 | ihigh = 0; |
| 585 | if (ihigh < ilow) |
| 586 | ihigh = ilow; |
| 587 | else if (ihigh > a->ob_size) |
| 588 | ihigh = a->ob_size; |
| 589 | np = (arrayobject *) newarrayobject(ihigh - ilow, a->ob_descr); |
| 590 | if (np == NULL) |
| 591 | return NULL; |
| 592 | memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize, |
| 593 | (ihigh-ilow) * a->ob_descr->itemsize); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 594 | return (PyObject *)np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 597 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 598 | array_concat(a, bb) |
| 599 | arrayobject *a; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 600 | PyObject *bb; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 601 | { |
| 602 | int size; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 603 | arrayobject *np; |
| 604 | if (!is_arrayobject(bb)) { |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 605 | PyErr_Format(PyExc_TypeError, |
| 606 | "can only append array (not \"%.200s\") to array", |
| 607 | bb->ob_type->tp_name); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 608 | return NULL; |
| 609 | } |
| 610 | #define b ((arrayobject *)bb) |
| 611 | if (a->ob_descr != b->ob_descr) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 612 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 613 | return NULL; |
| 614 | } |
| 615 | size = a->ob_size + b->ob_size; |
| 616 | np = (arrayobject *) newarrayobject(size, a->ob_descr); |
| 617 | if (np == NULL) { |
| 618 | return NULL; |
| 619 | } |
| 620 | memcpy(np->ob_item, a->ob_item, a->ob_size*a->ob_descr->itemsize); |
| 621 | memcpy(np->ob_item + a->ob_size*a->ob_descr->itemsize, |
Guido van Rossum | 32be3a7 | 1993-11-05 10:16:27 +0000 | [diff] [blame] | 622 | b->ob_item, b->ob_size*b->ob_descr->itemsize); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 623 | return (PyObject *)np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 624 | #undef b |
| 625 | } |
| 626 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 627 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 628 | array_repeat(a, n) |
| 629 | arrayobject *a; |
| 630 | int n; |
| 631 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 632 | int i; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 633 | int size; |
| 634 | arrayobject *np; |
| 635 | char *p; |
| 636 | int nbytes; |
| 637 | if (n < 0) |
| 638 | n = 0; |
| 639 | size = a->ob_size * n; |
| 640 | np = (arrayobject *) newarrayobject(size, a->ob_descr); |
| 641 | if (np == NULL) |
| 642 | return NULL; |
| 643 | p = np->ob_item; |
| 644 | nbytes = a->ob_size * a->ob_descr->itemsize; |
| 645 | for (i = 0; i < n; i++) { |
| 646 | memcpy(p, a->ob_item, nbytes); |
| 647 | p += nbytes; |
| 648 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 649 | return (PyObject *) np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | static int |
| 653 | array_ass_slice(a, ilow, ihigh, v) |
| 654 | arrayobject *a; |
| 655 | int ilow, ihigh; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 656 | PyObject *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 657 | { |
| 658 | char *item; |
| 659 | int n; /* Size of replacement array */ |
| 660 | int d; /* Change in size */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 661 | #define b ((arrayobject *)v) |
| 662 | if (v == NULL) |
| 663 | n = 0; |
| 664 | else if (is_arrayobject(v)) { |
| 665 | n = b->ob_size; |
| 666 | if (a == b) { |
| 667 | /* Special case "a[i:j] = a" -- copy b first */ |
| 668 | int ret; |
| 669 | v = array_slice(b, 0, n); |
| 670 | ret = array_ass_slice(a, ilow, ihigh, v); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 671 | Py_DECREF(v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 672 | return ret; |
| 673 | } |
| 674 | if (b->ob_descr != a->ob_descr) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 675 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 676 | return -1; |
| 677 | } |
| 678 | } |
| 679 | else { |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 680 | PyErr_Format(PyExc_TypeError, |
| 681 | "can only assign array (not \"%.200s\") to array slice", |
| 682 | v->ob_type->tp_name); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 683 | return -1; |
| 684 | } |
| 685 | if (ilow < 0) |
| 686 | ilow = 0; |
| 687 | else if (ilow > a->ob_size) |
| 688 | ilow = a->ob_size; |
| 689 | if (ihigh < 0) |
| 690 | ihigh = 0; |
| 691 | if (ihigh < ilow) |
| 692 | ihigh = ilow; |
| 693 | else if (ihigh > a->ob_size) |
| 694 | ihigh = a->ob_size; |
| 695 | item = a->ob_item; |
| 696 | d = n - (ihigh-ilow); |
| 697 | if (d < 0) { /* Delete -d items */ |
| 698 | memmove(item + (ihigh+d)*a->ob_descr->itemsize, |
| 699 | item + ihigh*a->ob_descr->itemsize, |
| 700 | (a->ob_size-ihigh)*a->ob_descr->itemsize); |
| 701 | a->ob_size += d; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 702 | PyMem_RESIZE(item, char, a->ob_size*a->ob_descr->itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 703 | /* Can't fail */ |
| 704 | a->ob_item = item; |
| 705 | } |
| 706 | else if (d > 0) { /* Insert d items */ |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 707 | PyMem_RESIZE(item, char, |
| 708 | (a->ob_size + d)*a->ob_descr->itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 709 | if (item == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 710 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 711 | return -1; |
| 712 | } |
| 713 | memmove(item + (ihigh+d)*a->ob_descr->itemsize, |
| 714 | item + ihigh*a->ob_descr->itemsize, |
| 715 | (a->ob_size-ihigh)*a->ob_descr->itemsize); |
| 716 | a->ob_item = item; |
| 717 | a->ob_size += d; |
| 718 | } |
| 719 | if (n > 0) |
| 720 | memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item, |
| 721 | n*b->ob_descr->itemsize); |
| 722 | return 0; |
| 723 | #undef b |
| 724 | } |
| 725 | |
| 726 | static int |
| 727 | array_ass_item(a, i, v) |
| 728 | arrayobject *a; |
| 729 | int i; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 730 | PyObject *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 731 | { |
| 732 | if (i < 0 || i >= a->ob_size) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 733 | PyErr_SetString(PyExc_IndexError, |
| 734 | "array assignment index out of range"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 735 | return -1; |
| 736 | } |
| 737 | if (v == NULL) |
| 738 | return array_ass_slice(a, i, i+1, v); |
| 739 | return (*a->ob_descr->setitem)(a, i, v); |
| 740 | } |
| 741 | |
| 742 | static int |
| 743 | setarrayitem(a, i, v) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 744 | PyObject *a; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 745 | int i; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 746 | PyObject *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 747 | { |
| 748 | if (!is_arrayobject(a)) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 749 | PyErr_BadInternalCall(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 750 | return -1; |
| 751 | } |
| 752 | return array_ass_item((arrayobject *)a, i, v); |
| 753 | } |
| 754 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 755 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 756 | ins(self, where, v) |
| 757 | arrayobject *self; |
| 758 | int where; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 759 | PyObject *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 760 | { |
| 761 | if (ins1(self, where, v) != 0) |
| 762 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 763 | Py_INCREF(Py_None); |
| 764 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 767 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 768 | array_insert(self, args) |
| 769 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 770 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 771 | { |
| 772 | int i; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 773 | PyObject *v; |
| 774 | if (!PyArg_Parse(args, "(iO)", &i, &v)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 775 | return NULL; |
| 776 | return ins(self, i, v); |
| 777 | } |
| 778 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 779 | static char insert_doc [] = |
| 780 | "insert (i,x)\n\ |
| 781 | \n\ |
| 782 | Insert a new item x into the array before position i."; |
| 783 | |
| 784 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 785 | static PyObject * |
Guido van Rossum | de4a4ca | 1997-08-12 14:55:56 +0000 | [diff] [blame] | 786 | array_buffer_info(self, args) |
| 787 | arrayobject *self; |
| 788 | PyObject *args; |
| 789 | { |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 790 | PyObject* retval = PyTuple_New(2); |
| 791 | if (!retval) return NULL; |
| 792 | |
| 793 | PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item)); |
| 794 | PyTuple_SET_ITEM(retval, 1, PyInt_FromLong((long)(self->ob_size))); |
| 795 | |
| 796 | return retval; |
Guido van Rossum | de4a4ca | 1997-08-12 14:55:56 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 799 | static char buffer_info_doc [] = |
| 800 | "buffer_info -> (address, length)\n\ |
| 801 | \n\ |
| 802 | Return a tuple (address, length) giving the current memory address and\n\ |
| 803 | the length in bytes of the buffer used to hold array's contents."; |
| 804 | |
| 805 | |
Guido van Rossum | de4a4ca | 1997-08-12 14:55:56 +0000 | [diff] [blame] | 806 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 807 | array_append(self, args) |
| 808 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 809 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 810 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 811 | PyObject *v; |
| 812 | if (!PyArg_Parse(args, "O", &v)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 813 | return NULL; |
| 814 | return ins(self, (int) self->ob_size, v); |
| 815 | } |
| 816 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 817 | static char append_doc [] = |
| 818 | "append(x)\n\ |
| 819 | \n\ |
| 820 | Append new value x to the end of the array."; |
| 821 | |
| 822 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 823 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 824 | array_byteswap(self, args) |
| 825 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 826 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 827 | { |
| 828 | char *p; |
| 829 | int i; |
Fred Drake | bf27298 | 1999-12-03 17:15:30 +0000 | [diff] [blame] | 830 | |
| 831 | if (!PyArg_ParseTuple(args, ":byteswap")) |
| 832 | return NULL; |
| 833 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 834 | switch (self->ob_descr->itemsize) { |
| 835 | case 1: |
| 836 | break; |
| 837 | case 2: |
| 838 | for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 2) { |
| 839 | char p0 = p[0]; |
| 840 | p[0] = p[1]; |
| 841 | p[1] = p0; |
| 842 | } |
| 843 | break; |
| 844 | case 4: |
| 845 | for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 4) { |
| 846 | char p0 = p[0]; |
| 847 | char p1 = p[1]; |
| 848 | p[0] = p[3]; |
| 849 | p[1] = p[2]; |
| 850 | p[2] = p1; |
| 851 | p[3] = p0; |
| 852 | } |
| 853 | break; |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 854 | case 8: |
| 855 | for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 8) { |
| 856 | char p0 = p[0]; |
| 857 | char p1 = p[1]; |
| 858 | char p2 = p[2]; |
| 859 | char p3 = p[3]; |
| 860 | p[0] = p[7]; |
| 861 | p[1] = p[6]; |
| 862 | p[2] = p[5]; |
| 863 | p[3] = p[4]; |
| 864 | p[4] = p3; |
| 865 | p[5] = p2; |
| 866 | p[6] = p1; |
| 867 | p[7] = p0; |
| 868 | } |
| 869 | break; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 870 | default: |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 871 | PyErr_SetString(PyExc_RuntimeError, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 872 | "don't know how to byteswap this array type"); |
| 873 | return NULL; |
| 874 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 875 | Py_INCREF(Py_None); |
| 876 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 879 | static char byteswap_doc [] = |
Fred Drake | bf27298 | 1999-12-03 17:15:30 +0000 | [diff] [blame] | 880 | "byteswap()\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 881 | \n\ |
Fred Drake | bf27298 | 1999-12-03 17:15:30 +0000 | [diff] [blame] | 882 | Byteswap all items of the array. If the items in the array are not 1, 2,\n\ |
| 883 | 4, or 8 bytes in size, RuntimeError is raised."; |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 884 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 885 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 886 | array_reverse(self, args) |
| 887 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 888 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 889 | { |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 890 | register int itemsize = self->ob_descr->itemsize; |
| 891 | register char *p, *q; |
| 892 | char tmp[sizeof(double)]; /* Assume that's the max item size */ |
| 893 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 894 | if (args != NULL) { |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 895 | PyErr_SetString(PyExc_TypeError, |
| 896 | "<array>.reverse requires exactly 0 arguments"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 897 | return NULL; |
| 898 | } |
| 899 | |
| 900 | if (self->ob_size > 1) { |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 901 | for (p = self->ob_item, |
| 902 | q = self->ob_item + (self->ob_size - 1)*itemsize; |
| 903 | p < q; |
| 904 | p += itemsize, q -= itemsize) { |
| 905 | memmove(tmp, p, itemsize); |
| 906 | memmove(p, q, itemsize); |
| 907 | memmove(q, tmp, itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 908 | } |
| 909 | } |
| 910 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 911 | Py_INCREF(Py_None); |
| 912 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 913 | } |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 914 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 915 | static char reverse_doc [] = |
| 916 | "reverse()\n\ |
| 917 | \n\ |
| 918 | Reverse the order of the items in the array."; |
| 919 | |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 920 | /* The following routines were adapted from listobject.c but not converted. |
| 921 | To make them work you will have to work! */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 922 | |
| 923 | #if 0 |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 924 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 925 | array_index(self, args) |
| 926 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 927 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 928 | { |
| 929 | int i; |
| 930 | |
| 931 | if (args == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 932 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 933 | return NULL; |
| 934 | } |
| 935 | for (i = 0; i < self->ob_size; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 936 | if (PyObject_Compare(self->ob_item[i], args) == 0) |
| 937 | return PyInt_FromLong((long)i); |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 938 | /* XXX PyErr_Occurred */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 939 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 940 | PyErr_SetString(PyExc_ValueError, "array.index(x): x not in array"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 941 | return NULL; |
| 942 | } |
| 943 | #endif |
| 944 | |
| 945 | #if 0 |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 946 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 947 | array_count(self, args) |
| 948 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 949 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 950 | { |
| 951 | int count = 0; |
| 952 | int i; |
| 953 | |
| 954 | if (args == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 955 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 956 | return NULL; |
| 957 | } |
| 958 | for (i = 0; i < self->ob_size; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 959 | if (PyObject_Compare(self->ob_item[i], args) == 0) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 960 | count++; |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 961 | /* XXX PyErr_Occurred */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 962 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 963 | return PyInt_FromLong((long)count); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 964 | } |
| 965 | #endif |
| 966 | |
| 967 | #if 0 |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 968 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 969 | array_remove(self, args) |
| 970 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 971 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 972 | { |
| 973 | int i; |
| 974 | |
| 975 | if (args == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 976 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 977 | return NULL; |
| 978 | } |
| 979 | for (i = 0; i < self->ob_size; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 980 | if (PyObject_Compare(self->ob_item[i], args) == 0) { |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 981 | if (array_ass_slice(self, i, i+1, |
| 982 | (PyObject *)NULL) != 0) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 983 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 984 | Py_INCREF(Py_None); |
| 985 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 986 | } |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 987 | /* XXX PyErr_Occurred */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 988 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 989 | PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in array"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 990 | return NULL; |
| 991 | } |
| 992 | #endif |
| 993 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 994 | static PyObject * |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 995 | array_fromfile(self, args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 996 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 997 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 998 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 999 | PyObject *f; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1000 | int n; |
| 1001 | FILE *fp; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1002 | if (!PyArg_Parse(args, "(Oi)", &f, &n)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1003 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1004 | fp = PyFile_AsFile(f); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1005 | if (fp == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1006 | PyErr_SetString(PyExc_TypeError, "arg1 must be open file"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1007 | return NULL; |
| 1008 | } |
| 1009 | if (n > 0) { |
| 1010 | char *item = self->ob_item; |
| 1011 | int itemsize = self->ob_descr->itemsize; |
Guido van Rossum | 7d0ae5e | 2000-06-28 21:27:21 +0000 | [diff] [blame] | 1012 | size_t nread; |
Guido van Rossum | 3791b0d | 1999-02-23 18:05:22 +0000 | [diff] [blame] | 1013 | int newlength; |
| 1014 | size_t newbytes; |
| 1015 | /* Be careful here about overflow */ |
| 1016 | if ((newlength = self->ob_size + n) <= 0 || |
Guido van Rossum | 481ac88 | 1999-03-19 21:50:11 +0000 | [diff] [blame] | 1017 | (newbytes = newlength * itemsize) / itemsize != |
| 1018 | (size_t)newlength) |
Guido van Rossum | 3791b0d | 1999-02-23 18:05:22 +0000 | [diff] [blame] | 1019 | goto nomem; |
| 1020 | PyMem_RESIZE(item, char, newbytes); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1021 | if (item == NULL) { |
Guido van Rossum | 3791b0d | 1999-02-23 18:05:22 +0000 | [diff] [blame] | 1022 | nomem: |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1023 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1024 | return NULL; |
| 1025 | } |
| 1026 | self->ob_item = item; |
| 1027 | self->ob_size += n; |
| 1028 | nread = fread(item + (self->ob_size - n) * itemsize, |
| 1029 | itemsize, n, fp); |
Guido van Rossum | 7d0ae5e | 2000-06-28 21:27:21 +0000 | [diff] [blame] | 1030 | if (nread < (size_t)n) { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1031 | self->ob_size -= (n - nread); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1032 | PyMem_RESIZE(item, char, self->ob_size*itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1033 | self->ob_item = item; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1034 | PyErr_SetString(PyExc_EOFError, |
| 1035 | "not enough items in file"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1036 | return NULL; |
| 1037 | } |
| 1038 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1039 | Py_INCREF(Py_None); |
| 1040 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1043 | static char fromfile_doc [] = |
| 1044 | "fromfile(f, n)\n\ |
| 1045 | \n\ |
| 1046 | Read n objects from the file object f and append them to the end of the\n\ |
| 1047 | array. Also called as read."; |
| 1048 | |
| 1049 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1050 | static PyObject * |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 1051 | array_tofile(self, args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1052 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1053 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1054 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1055 | PyObject *f; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1056 | FILE *fp; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1057 | if (!PyArg_Parse(args, "O", &f)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1058 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1059 | fp = PyFile_AsFile(f); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1060 | if (fp == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1061 | PyErr_SetString(PyExc_TypeError, "arg must be open file"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1062 | return NULL; |
| 1063 | } |
| 1064 | if (self->ob_size > 0) { |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 1065 | if ((int)fwrite(self->ob_item, self->ob_descr->itemsize, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1066 | self->ob_size, fp) != self->ob_size) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1067 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1068 | clearerr(fp); |
| 1069 | return NULL; |
| 1070 | } |
| 1071 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1072 | Py_INCREF(Py_None); |
| 1073 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1076 | static char tofile_doc [] = |
| 1077 | "tofile(f)\n\ |
| 1078 | \n\ |
| 1079 | Write all items (as machine values) to the file object f. Also called as\n\ |
| 1080 | write."; |
| 1081 | |
| 1082 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1083 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1084 | array_fromlist(self, args) |
| 1085 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1086 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1087 | { |
| 1088 | int n; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1089 | PyObject *list; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1090 | int itemsize = self->ob_descr->itemsize; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1091 | if (!PyArg_Parse(args, "O", &list)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1092 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1093 | if (!PyList_Check(list)) { |
| 1094 | PyErr_SetString(PyExc_TypeError, "arg must be list"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1095 | return NULL; |
| 1096 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1097 | n = PyList_Size(list); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1098 | if (n > 0) { |
| 1099 | char *item = self->ob_item; |
| 1100 | int i; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1101 | PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1102 | if (item == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1103 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1104 | return NULL; |
| 1105 | } |
| 1106 | self->ob_item = item; |
| 1107 | self->ob_size += n; |
| 1108 | for (i = 0; i < n; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1109 | PyObject *v = PyList_GetItem(list, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1110 | if ((*self->ob_descr->setitem)(self, |
| 1111 | self->ob_size - n + i, v) != 0) { |
| 1112 | self->ob_size -= n; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1113 | PyMem_RESIZE(item, char, |
| 1114 | self->ob_size * itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1115 | self->ob_item = item; |
| 1116 | return NULL; |
| 1117 | } |
| 1118 | } |
| 1119 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1120 | Py_INCREF(Py_None); |
| 1121 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1124 | static char fromlist_doc [] = |
| 1125 | "fromlist(list)\n\ |
| 1126 | \n\ |
| 1127 | Append items to array from list."; |
| 1128 | |
| 1129 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1130 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1131 | array_tolist(self, args) |
| 1132 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1133 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1134 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1135 | PyObject *list = PyList_New(self->ob_size); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1136 | int i; |
| 1137 | if (list == NULL) |
| 1138 | return NULL; |
| 1139 | for (i = 0; i < self->ob_size; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1140 | PyObject *v = getarrayitem((PyObject *)self, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1141 | if (v == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1142 | Py_DECREF(list); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1143 | return NULL; |
| 1144 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1145 | PyList_SetItem(list, i, v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1146 | } |
| 1147 | return list; |
| 1148 | } |
| 1149 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1150 | static char tolist_doc [] = |
Guido van Rossum | fc6aba5 | 1998-10-14 02:52:31 +0000 | [diff] [blame] | 1151 | "tolist() -> list\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1152 | \n\ |
| 1153 | Convert array to an ordinary list with the same items."; |
| 1154 | |
| 1155 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1156 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1157 | array_fromstring(self, args) |
| 1158 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1159 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1160 | { |
| 1161 | char *str; |
| 1162 | int n; |
| 1163 | int itemsize = self->ob_descr->itemsize; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1164 | if (!PyArg_Parse(args, "s#", &str, &n)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1165 | return NULL; |
| 1166 | if (n % itemsize != 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1167 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1168 | "string length not a multiple of item size"); |
| 1169 | return NULL; |
| 1170 | } |
| 1171 | n = n / itemsize; |
| 1172 | if (n > 0) { |
| 1173 | char *item = self->ob_item; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1174 | PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1175 | if (item == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1176 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1177 | return NULL; |
| 1178 | } |
| 1179 | self->ob_item = item; |
| 1180 | self->ob_size += n; |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1181 | memcpy(item + (self->ob_size - n) * itemsize, |
| 1182 | str, itemsize*n); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1183 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1184 | Py_INCREF(Py_None); |
| 1185 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1188 | static char fromstring_doc [] = |
| 1189 | "fromstring(string)\n\ |
| 1190 | \n\ |
| 1191 | Appends items from the string, interpreting it as an array of machine\n\ |
| 1192 | values,as if it had been read from a file using the fromfile() method)."; |
| 1193 | |
| 1194 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1195 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1196 | array_tostring(self, args) |
| 1197 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1198 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1199 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1200 | if (!PyArg_Parse(args, "")) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1201 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1202 | return PyString_FromStringAndSize(self->ob_item, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1203 | self->ob_size * self->ob_descr->itemsize); |
| 1204 | } |
| 1205 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1206 | static char tostring_doc [] = |
| 1207 | "tostring() -> string\n\ |
| 1208 | \n\ |
| 1209 | Convert the array to an array of machine values and return the string\n\ |
| 1210 | representation."; |
| 1211 | |
| 1212 | PyMethodDef array_methods[] = { |
| 1213 | {"append", (PyCFunction)array_append, 0, append_doc}, |
| 1214 | {"buffer_info", (PyCFunction)array_buffer_info, 0, buffer_info_doc}, |
Fred Drake | bf27298 | 1999-12-03 17:15:30 +0000 | [diff] [blame] | 1215 | {"byteswap", (PyCFunction)array_byteswap, METH_VARARGS, |
| 1216 | byteswap_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1217 | /* {"count", (method)array_count},*/ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1218 | {"fromfile", (PyCFunction)array_fromfile, 0, fromfile_doc}, |
| 1219 | {"fromlist", (PyCFunction)array_fromlist, 0, fromlist_doc}, |
| 1220 | {"fromstring", (PyCFunction)array_fromstring, 0, fromstring_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1221 | /* {"index", (method)array_index},*/ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1222 | {"insert", (PyCFunction)array_insert, 0, insert_doc}, |
| 1223 | {"read", (PyCFunction)array_fromfile, 0, fromfile_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1224 | /* {"remove", (method)array_remove},*/ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1225 | {"reverse", (PyCFunction)array_reverse, 0, reverse_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1226 | /* {"sort", (method)array_sort},*/ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1227 | {"tofile", (PyCFunction)array_tofile, 0, tofile_doc}, |
| 1228 | {"tolist", (PyCFunction)array_tolist, 0, tolist_doc}, |
| 1229 | {"tostring", (PyCFunction)array_tostring, 0, tostring_doc}, |
| 1230 | {"write", (PyCFunction)array_tofile, 0, tofile_doc}, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1231 | {NULL, NULL} /* sentinel */ |
| 1232 | }; |
| 1233 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1234 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1235 | array_getattr(a, name) |
| 1236 | arrayobject *a; |
| 1237 | char *name; |
| 1238 | { |
| 1239 | if (strcmp(name, "typecode") == 0) { |
| 1240 | char tc = a->ob_descr->typecode; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1241 | return PyString_FromStringAndSize(&tc, 1); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1242 | } |
| 1243 | if (strcmp(name, "itemsize") == 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1244 | return PyInt_FromLong((long)a->ob_descr->itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1245 | } |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1246 | if (strcmp(name, "__members__") == 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1247 | PyObject *list = PyList_New(2); |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1248 | if (list) { |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1249 | PyList_SetItem(list, 0, |
| 1250 | PyString_FromString("typecode")); |
| 1251 | PyList_SetItem(list, 1, |
| 1252 | PyString_FromString("itemsize")); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1253 | if (PyErr_Occurred()) { |
| 1254 | Py_DECREF(list); |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1255 | list = NULL; |
| 1256 | } |
| 1257 | } |
| 1258 | return list; |
| 1259 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1260 | return Py_FindMethod(array_methods, (PyObject *)a, name); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | static int |
| 1264 | array_print(a, fp, flags) |
| 1265 | arrayobject *a; |
| 1266 | FILE *fp; |
| 1267 | int flags; |
| 1268 | { |
| 1269 | int ok = 0; |
| 1270 | int i, len; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1271 | PyObject *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1272 | len = a->ob_size; |
| 1273 | if (len == 0) { |
| 1274 | fprintf(fp, "array('%c')", a->ob_descr->typecode); |
| 1275 | return ok; |
| 1276 | } |
| 1277 | if (a->ob_descr->typecode == 'c') { |
| 1278 | fprintf(fp, "array('c', "); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1279 | v = array_tostring(a, (PyObject *)NULL); |
| 1280 | ok = PyObject_Print(v, fp, 0); |
| 1281 | Py_XDECREF(v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1282 | fprintf(fp, ")"); |
| 1283 | return ok; |
| 1284 | } |
| 1285 | fprintf(fp, "array('%c', [", a->ob_descr->typecode); |
| 1286 | for (i = 0; i < len && ok == 0; i++) { |
| 1287 | if (i > 0) |
| 1288 | fprintf(fp, ", "); |
| 1289 | v = (a->ob_descr->getitem)(a, i); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1290 | ok = PyObject_Print(v, fp, 0); |
| 1291 | Py_XDECREF(v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1292 | } |
| 1293 | fprintf(fp, "])"); |
| 1294 | return ok; |
| 1295 | } |
| 1296 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1297 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1298 | array_repr(a) |
| 1299 | arrayobject *a; |
| 1300 | { |
| 1301 | char buf[256]; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1302 | PyObject *s, *t, *comma, *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1303 | int i, len; |
| 1304 | len = a->ob_size; |
| 1305 | if (len == 0) { |
| 1306 | sprintf(buf, "array('%c')", a->ob_descr->typecode); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1307 | return PyString_FromString(buf); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1308 | } |
| 1309 | if (a->ob_descr->typecode == 'c') { |
| 1310 | sprintf(buf, "array('c', "); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1311 | s = PyString_FromString(buf); |
| 1312 | v = array_tostring(a, (PyObject *)NULL); |
| 1313 | t = PyObject_Repr(v); |
| 1314 | Py_XDECREF(v); |
| 1315 | PyString_ConcatAndDel(&s, t); |
| 1316 | PyString_ConcatAndDel(&s, PyString_FromString(")")); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1317 | return s; |
| 1318 | } |
| 1319 | sprintf(buf, "array('%c', [", a->ob_descr->typecode); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1320 | s = PyString_FromString(buf); |
| 1321 | comma = PyString_FromString(", "); |
| 1322 | for (i = 0; i < len && !PyErr_Occurred(); i++) { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1323 | if (i > 0) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1324 | PyString_Concat(&s, comma); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1325 | v = (a->ob_descr->getitem)(a, i); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1326 | t = PyObject_Repr(v); |
| 1327 | Py_XDECREF(v); |
| 1328 | PyString_ConcatAndDel(&s, t); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1329 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1330 | Py_XDECREF(comma); |
| 1331 | PyString_ConcatAndDel(&s, PyString_FromString("])")); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1332 | return s; |
| 1333 | } |
| 1334 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1335 | static int |
| 1336 | array_buffer_getreadbuf(self, index, ptr) |
| 1337 | arrayobject *self; |
| 1338 | int index; |
| 1339 | const void **ptr; |
| 1340 | { |
| 1341 | if ( index != 0 ) { |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1342 | PyErr_SetString(PyExc_SystemError, |
| 1343 | "Accessing non-existent array segment"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1344 | return -1; |
| 1345 | } |
| 1346 | *ptr = (void *)self->ob_item; |
| 1347 | return self->ob_size*self->ob_descr->itemsize; |
| 1348 | } |
| 1349 | |
| 1350 | static int |
| 1351 | array_buffer_getwritebuf(self, index, ptr) |
| 1352 | arrayobject *self; |
| 1353 | int index; |
| 1354 | const void **ptr; |
| 1355 | { |
| 1356 | if ( index != 0 ) { |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1357 | PyErr_SetString(PyExc_SystemError, |
| 1358 | "Accessing non-existent array segment"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1359 | return -1; |
| 1360 | } |
| 1361 | *ptr = (void *)self->ob_item; |
| 1362 | return self->ob_size*self->ob_descr->itemsize; |
| 1363 | } |
| 1364 | |
| 1365 | static int |
| 1366 | array_buffer_getsegcount(self, lenp) |
| 1367 | arrayobject *self; |
| 1368 | int *lenp; |
| 1369 | { |
| 1370 | if ( lenp ) |
| 1371 | *lenp = self->ob_size*self->ob_descr->itemsize; |
| 1372 | return 1; |
| 1373 | } |
| 1374 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1375 | static PySequenceMethods array_as_sequence = { |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1376 | (inquiry)array_length, /*sq_length*/ |
| 1377 | (binaryfunc)array_concat, /*sq_concat*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1378 | (intargfunc)array_repeat, /*sq_repeat*/ |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1379 | (intargfunc)array_item, /*sq_item*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1380 | (intintargfunc)array_slice, /*sq_slice*/ |
| 1381 | (intobjargproc)array_ass_item, /*sq_ass_item*/ |
| 1382 | (intintobjargproc)array_ass_slice, /*sq_ass_slice*/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1383 | }; |
| 1384 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1385 | static PyBufferProcs array_as_buffer = { |
| 1386 | (getreadbufferproc)array_buffer_getreadbuf, |
| 1387 | (getwritebufferproc)array_buffer_getwritebuf, |
| 1388 | (getsegcountproc)array_buffer_getsegcount, |
| 1389 | }; |
| 1390 | |
| 1391 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1392 | |
| 1393 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1394 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1395 | a_array(self, args) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1396 | PyObject *self; |
| 1397 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1398 | { |
| 1399 | char c; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1400 | PyObject *initial = NULL; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1401 | struct arraydescr *descr; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1402 | if (!PyArg_Parse(args, "c", &c)) { |
| 1403 | PyErr_Clear(); |
| 1404 | if (!PyArg_Parse(args, "(cO)", &c, &initial)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1405 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1406 | if (!PyList_Check(initial) && !PyString_Check(initial)) { |
| 1407 | PyErr_SetString(PyExc_TypeError, |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1408 | "array initializer must be list or string"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1409 | return NULL; |
| 1410 | } |
| 1411 | } |
| 1412 | for (descr = descriptors; descr->typecode != '\0'; descr++) { |
| 1413 | if (descr->typecode == c) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1414 | PyObject *a; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1415 | int len; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1416 | if (initial == NULL || !PyList_Check(initial)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1417 | len = 0; |
| 1418 | else |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1419 | len = PyList_Size(initial); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1420 | a = newarrayobject(len, descr); |
| 1421 | if (a == NULL) |
| 1422 | return NULL; |
| 1423 | if (len > 0) { |
| 1424 | int i; |
| 1425 | for (i = 0; i < len; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1426 | PyObject *v = |
| 1427 | PyList_GetItem(initial, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1428 | if (setarrayitem(a, i, v) != 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1429 | Py_DECREF(a); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1430 | return NULL; |
| 1431 | } |
| 1432 | } |
| 1433 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1434 | if (initial != NULL && PyString_Check(initial)) { |
| 1435 | PyObject *v = |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1436 | array_fromstring((arrayobject *)a, initial); |
| 1437 | if (v == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1438 | Py_DECREF(a); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1439 | return NULL; |
| 1440 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1441 | Py_DECREF(v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1442 | } |
| 1443 | return a; |
| 1444 | } |
| 1445 | } |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1446 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 1447 | "bad typecode (must be c, b, B, h, H, i, I, l, L, f or d)"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1448 | return NULL; |
| 1449 | } |
| 1450 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1451 | static char a_array_doc [] = |
| 1452 | "array(typecode [, initializer]) -> array\n\ |
| 1453 | \n\ |
| 1454 | Return a new array whose items are restricted by typecode, and\n\ |
| 1455 | initialized from the optional initializer value, which must be a list\n\ |
| 1456 | or a string."; |
| 1457 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1458 | static PyMethodDef a_methods[] = { |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1459 | {"array", a_array, 0, a_array_doc}, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1460 | {NULL, NULL} /* sentinel */ |
| 1461 | }; |
| 1462 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1463 | static char module_doc [] = |
| 1464 | "This module defines a new object type which can efficiently represent\n\ |
| 1465 | an array of basic values: characters, integers, floating point\n\ |
| 1466 | numbers. Arrays are sequence types and behave very much like lists,\n\ |
| 1467 | except that the type of objects stored in them is constrained. The\n\ |
| 1468 | type is specified at object creation time by using a type code, which\n\ |
| 1469 | is a single character. The following type codes are defined:\n\ |
| 1470 | \n\ |
| 1471 | Type code C Type Minimum size in bytes \n\ |
| 1472 | 'c' character 1 \n\ |
| 1473 | 'b' signed integer 1 \n\ |
| 1474 | 'B' unsigned integer 1 \n\ |
| 1475 | 'h' signed integer 2 \n\ |
| 1476 | 'H' unsigned integer 2 \n\ |
| 1477 | 'i' signed integer 2 \n\ |
| 1478 | 'I' unsigned integer 2 \n\ |
| 1479 | 'l' signed integer 4 \n\ |
| 1480 | 'L' unsigned integer 4 \n\ |
| 1481 | 'f' floating point 4 \n\ |
| 1482 | 'd' floating point 8 \n\ |
| 1483 | \n\ |
| 1484 | Functions:\n\ |
| 1485 | \n\ |
| 1486 | array(typecode [, initializer]) -- create a new array\n\ |
| 1487 | \n\ |
| 1488 | Special Objects:\n\ |
| 1489 | \n\ |
| 1490 | ArrayType -- type object for array objects\n\ |
| 1491 | "; |
| 1492 | |
| 1493 | static char arraytype_doc [] = |
| 1494 | "An array represents basic values and behave very much like lists, except\n\ |
| 1495 | the type of objects stored in them is constrained.\n\ |
| 1496 | \n\ |
| 1497 | Methods:\n\ |
| 1498 | \n\ |
| 1499 | append() -- append a new item to the end of the array\n\ |
| 1500 | buffer_info() -- return information giving the current memory info\n\ |
| 1501 | byteswap() -- byteswap all the items of the array\n\ |
| 1502 | fromfile() -- read items from a file object\n\ |
| 1503 | fromlist() -- append items from the list\n\ |
| 1504 | fromstring() -- append items from the string\n\ |
| 1505 | insert() -- insert a new item into the array at a provided position\n\ |
| 1506 | read() -- DEPRECATED, use fromfile()\n\ |
| 1507 | reverse() -- reverse the order of the items in the array\n\ |
| 1508 | tofile() -- write all items to a file object\n\ |
| 1509 | tolist() -- return the array converted to an ordinary list\n\ |
| 1510 | tostring() -- return the array converted to a string\n\ |
| 1511 | write() -- DEPRECATED, use tofile()\n\ |
| 1512 | \n\ |
| 1513 | Variables:\n\ |
| 1514 | \n\ |
| 1515 | typecode -- the typecode character used to create the array\n\ |
| 1516 | itemsize -- the length in bytes of one array item\n\ |
| 1517 | "; |
| 1518 | |
| 1519 | statichere PyTypeObject Arraytype = { |
Fred Drake | 0d40ba4 | 2000-02-04 20:33:49 +0000 | [diff] [blame] | 1520 | PyObject_HEAD_INIT(NULL) |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1521 | 0, |
| 1522 | "array", |
| 1523 | sizeof(arrayobject), |
| 1524 | 0, |
| 1525 | (destructor)array_dealloc, /*tp_dealloc*/ |
| 1526 | (printfunc)array_print, /*tp_print*/ |
| 1527 | (getattrfunc)array_getattr, /*tp_getattr*/ |
| 1528 | 0, /*tp_setattr*/ |
| 1529 | (cmpfunc)array_compare, /*tp_compare*/ |
| 1530 | (reprfunc)array_repr, /*tp_repr*/ |
| 1531 | 0, /*tp_as_number*/ |
| 1532 | &array_as_sequence, /*tp_as_sequence*/ |
| 1533 | 0, /*tp_as_mapping*/ |
| 1534 | 0, /*tp_hash*/ |
| 1535 | 0, /*tp_call*/ |
| 1536 | 0, /*tp_str*/ |
| 1537 | 0, /*tp_getattro*/ |
| 1538 | 0, /*tp_setattro*/ |
| 1539 | &array_as_buffer, /*tp_as_buffer*/ |
| 1540 | 0, /*tp_xxx4*/ |
| 1541 | arraytype_doc, /*tp_doc*/ |
| 1542 | }; |
| 1543 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 1544 | DL_EXPORT(void) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1545 | initarray() |
| 1546 | { |
Guido van Rossum | b6190d3 | 1997-05-22 14:56:36 +0000 | [diff] [blame] | 1547 | PyObject *m, *d; |
Fred Drake | 0d40ba4 | 2000-02-04 20:33:49 +0000 | [diff] [blame] | 1548 | |
| 1549 | Arraytype.ob_type = &PyType_Type; |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1550 | m = Py_InitModule3("array", a_methods, module_doc); |
Guido van Rossum | b6190d3 | 1997-05-22 14:56:36 +0000 | [diff] [blame] | 1551 | d = PyModule_GetDict(m); |
Guido van Rossum | a0deb64 | 1998-10-14 13:45:06 +0000 | [diff] [blame] | 1552 | PyDict_SetItemString(d, "ArrayType", (PyObject *)&Arraytype); |
| 1553 | /* 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] | 1554 | } |