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