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 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 349 | op = PyObject_NewVar(arrayobject, &Arraytype, size); |
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) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 359 | PyObject_Del(op); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 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_descr = descr; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 364 | return (PyObject *) op; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 367 | #if 0 |
Guido van Rossum | 62de97f | 1995-01-22 00:48:41 +0000 | [diff] [blame] | 368 | static int |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 369 | getarraysize(op) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 370 | PyObject *op; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 371 | { |
| 372 | if (!is_arrayobject(op)) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 373 | PyErr_BadInternalCall(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 374 | return -1; |
| 375 | } |
| 376 | return ((arrayobject *)op) -> ob_size; |
| 377 | } |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 378 | #endif |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 379 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 380 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 381 | getarrayitem(op, i) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 382 | PyObject *op; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 383 | int i; |
| 384 | { |
| 385 | register arrayobject *ap; |
| 386 | if (!is_arrayobject(op)) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 387 | PyErr_BadInternalCall(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 388 | return NULL; |
| 389 | } |
| 390 | ap = (arrayobject *)op; |
| 391 | if (i < 0 || i >= ap->ob_size) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 392 | PyErr_SetString(PyExc_IndexError, "array index out of range"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 393 | return NULL; |
| 394 | } |
| 395 | return (*ap->ob_descr->getitem)(ap, i); |
| 396 | } |
| 397 | |
| 398 | static int |
| 399 | ins1(self, where, v) |
| 400 | arrayobject *self; |
| 401 | int where; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 402 | PyObject *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 403 | { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 404 | char *items; |
| 405 | if (v == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 406 | PyErr_BadInternalCall(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 407 | return -1; |
| 408 | } |
| 409 | if ((*self->ob_descr->setitem)(self, -1, v) < 0) |
| 410 | return -1; |
| 411 | items = self->ob_item; |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 412 | PyMem_RESIZE(items, char, |
| 413 | (self->ob_size+1) * self->ob_descr->itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 414 | if (items == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 415 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 416 | return -1; |
| 417 | } |
| 418 | if (where < 0) |
| 419 | where = 0; |
| 420 | if (where > self->ob_size) |
| 421 | where = self->ob_size; |
| 422 | memmove(items + (where+1)*self->ob_descr->itemsize, |
| 423 | items + where*self->ob_descr->itemsize, |
| 424 | (self->ob_size-where)*self->ob_descr->itemsize); |
| 425 | self->ob_item = items; |
| 426 | self->ob_size++; |
| 427 | return (*self->ob_descr->setitem)(self, where, v); |
| 428 | } |
| 429 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 430 | #if 0 |
Guido van Rossum | 62de97f | 1995-01-22 00:48:41 +0000 | [diff] [blame] | 431 | static int |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 432 | insarrayitem(op, where, newitem) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 433 | PyObject *op; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 434 | int where; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 435 | PyObject *newitem; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 436 | { |
| 437 | if (!is_arrayobject(op)) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 438 | PyErr_BadInternalCall(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 439 | return -1; |
| 440 | } |
| 441 | return ins1((arrayobject *)op, where, newitem); |
| 442 | } |
| 443 | |
Guido van Rossum | 62de97f | 1995-01-22 00:48:41 +0000 | [diff] [blame] | 444 | static int |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 445 | addarrayitem(op, newitem) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 446 | PyObject *op; |
| 447 | PyObject *newitem; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 448 | { |
| 449 | if (!is_arrayobject(op)) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 450 | PyErr_BadInternalCall(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 451 | return -1; |
| 452 | } |
| 453 | return ins1((arrayobject *)op, |
| 454 | (int) ((arrayobject *)op)->ob_size, newitem); |
| 455 | } |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 456 | #endif |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 457 | |
| 458 | /* Methods */ |
| 459 | |
| 460 | static void |
| 461 | array_dealloc(op) |
| 462 | arrayobject *op; |
| 463 | { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 464 | if (op->ob_item != NULL) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 465 | PyMem_DEL(op->ob_item); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 466 | PyObject_Del(op); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | static int |
| 470 | array_compare(v, w) |
| 471 | arrayobject *v, *w; |
| 472 | { |
| 473 | int len = (v->ob_size < w->ob_size) ? v->ob_size : w->ob_size; |
| 474 | int i; |
| 475 | for (i = 0; i < len; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 476 | PyObject *ai, *bi; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 477 | int cmp; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 478 | ai = getarrayitem((PyObject *)v, i); |
| 479 | bi = getarrayitem((PyObject *)w, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 480 | if (ai && bi) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 481 | cmp = PyObject_Compare(ai, bi); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 482 | else |
| 483 | cmp = -1; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 484 | Py_XDECREF(ai); |
| 485 | Py_XDECREF(bi); |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 486 | if (cmp != 0) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 487 | return cmp; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 488 | } |
| 489 | return v->ob_size - w->ob_size; |
| 490 | } |
| 491 | |
| 492 | static int |
| 493 | array_length(a) |
| 494 | arrayobject *a; |
| 495 | { |
| 496 | return a->ob_size; |
| 497 | } |
| 498 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 499 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 500 | array_item(a, i) |
| 501 | arrayobject *a; |
| 502 | int i; |
| 503 | { |
| 504 | if (i < 0 || i >= a->ob_size) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 505 | PyErr_SetString(PyExc_IndexError, "array index out of range"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 506 | return NULL; |
| 507 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 508 | return getarrayitem((PyObject *)a, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 511 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 512 | array_slice(a, ilow, ihigh) |
| 513 | arrayobject *a; |
| 514 | int ilow, ihigh; |
| 515 | { |
| 516 | arrayobject *np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 517 | if (ilow < 0) |
| 518 | ilow = 0; |
| 519 | else if (ilow > a->ob_size) |
| 520 | ilow = a->ob_size; |
| 521 | if (ihigh < 0) |
| 522 | ihigh = 0; |
| 523 | if (ihigh < ilow) |
| 524 | ihigh = ilow; |
| 525 | else if (ihigh > a->ob_size) |
| 526 | ihigh = a->ob_size; |
| 527 | np = (arrayobject *) newarrayobject(ihigh - ilow, a->ob_descr); |
| 528 | if (np == NULL) |
| 529 | return NULL; |
| 530 | memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize, |
| 531 | (ihigh-ilow) * a->ob_descr->itemsize); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 532 | return (PyObject *)np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 535 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 536 | array_concat(a, bb) |
| 537 | arrayobject *a; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 538 | PyObject *bb; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 539 | { |
| 540 | int size; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 541 | arrayobject *np; |
| 542 | if (!is_arrayobject(bb)) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 543 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 544 | return NULL; |
| 545 | } |
| 546 | #define b ((arrayobject *)bb) |
| 547 | if (a->ob_descr != b->ob_descr) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 548 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 549 | return NULL; |
| 550 | } |
| 551 | size = a->ob_size + b->ob_size; |
| 552 | np = (arrayobject *) newarrayobject(size, a->ob_descr); |
| 553 | if (np == NULL) { |
| 554 | return NULL; |
| 555 | } |
| 556 | memcpy(np->ob_item, a->ob_item, a->ob_size*a->ob_descr->itemsize); |
| 557 | memcpy(np->ob_item + a->ob_size*a->ob_descr->itemsize, |
Guido van Rossum | 32be3a7 | 1993-11-05 10:16:27 +0000 | [diff] [blame] | 558 | b->ob_item, b->ob_size*b->ob_descr->itemsize); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 559 | return (PyObject *)np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 560 | #undef b |
| 561 | } |
| 562 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 563 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 564 | array_repeat(a, n) |
| 565 | arrayobject *a; |
| 566 | int n; |
| 567 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 568 | int i; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 569 | int size; |
| 570 | arrayobject *np; |
| 571 | char *p; |
| 572 | int nbytes; |
| 573 | if (n < 0) |
| 574 | n = 0; |
| 575 | size = a->ob_size * n; |
| 576 | np = (arrayobject *) newarrayobject(size, a->ob_descr); |
| 577 | if (np == NULL) |
| 578 | return NULL; |
| 579 | p = np->ob_item; |
| 580 | nbytes = a->ob_size * a->ob_descr->itemsize; |
| 581 | for (i = 0; i < n; i++) { |
| 582 | memcpy(p, a->ob_item, nbytes); |
| 583 | p += nbytes; |
| 584 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 585 | return (PyObject *) np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | static int |
| 589 | array_ass_slice(a, ilow, ihigh, v) |
| 590 | arrayobject *a; |
| 591 | int ilow, ihigh; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 592 | PyObject *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 593 | { |
| 594 | char *item; |
| 595 | int n; /* Size of replacement array */ |
| 596 | int d; /* Change in size */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 597 | #define b ((arrayobject *)v) |
| 598 | if (v == NULL) |
| 599 | n = 0; |
| 600 | else if (is_arrayobject(v)) { |
| 601 | n = b->ob_size; |
| 602 | if (a == b) { |
| 603 | /* Special case "a[i:j] = a" -- copy b first */ |
| 604 | int ret; |
| 605 | v = array_slice(b, 0, n); |
| 606 | ret = array_ass_slice(a, ilow, ihigh, v); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 607 | Py_DECREF(v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 608 | return ret; |
| 609 | } |
| 610 | if (b->ob_descr != a->ob_descr) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 611 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 612 | return -1; |
| 613 | } |
| 614 | } |
| 615 | else { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 616 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 617 | return -1; |
| 618 | } |
| 619 | if (ilow < 0) |
| 620 | ilow = 0; |
| 621 | else if (ilow > a->ob_size) |
| 622 | ilow = a->ob_size; |
| 623 | if (ihigh < 0) |
| 624 | ihigh = 0; |
| 625 | if (ihigh < ilow) |
| 626 | ihigh = ilow; |
| 627 | else if (ihigh > a->ob_size) |
| 628 | ihigh = a->ob_size; |
| 629 | item = a->ob_item; |
| 630 | d = n - (ihigh-ilow); |
| 631 | if (d < 0) { /* Delete -d items */ |
| 632 | memmove(item + (ihigh+d)*a->ob_descr->itemsize, |
| 633 | item + ihigh*a->ob_descr->itemsize, |
| 634 | (a->ob_size-ihigh)*a->ob_descr->itemsize); |
| 635 | a->ob_size += d; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 636 | PyMem_RESIZE(item, char, a->ob_size*a->ob_descr->itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 637 | /* Can't fail */ |
| 638 | a->ob_item = item; |
| 639 | } |
| 640 | else if (d > 0) { /* Insert d items */ |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 641 | PyMem_RESIZE(item, char, |
| 642 | (a->ob_size + d)*a->ob_descr->itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 643 | if (item == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 644 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 645 | return -1; |
| 646 | } |
| 647 | memmove(item + (ihigh+d)*a->ob_descr->itemsize, |
| 648 | item + ihigh*a->ob_descr->itemsize, |
| 649 | (a->ob_size-ihigh)*a->ob_descr->itemsize); |
| 650 | a->ob_item = item; |
| 651 | a->ob_size += d; |
| 652 | } |
| 653 | if (n > 0) |
| 654 | memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item, |
| 655 | n*b->ob_descr->itemsize); |
| 656 | return 0; |
| 657 | #undef b |
| 658 | } |
| 659 | |
| 660 | static int |
| 661 | array_ass_item(a, i, v) |
| 662 | arrayobject *a; |
| 663 | int i; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 664 | PyObject *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 665 | { |
| 666 | if (i < 0 || i >= a->ob_size) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 667 | PyErr_SetString(PyExc_IndexError, |
| 668 | "array assignment index out of range"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 669 | return -1; |
| 670 | } |
| 671 | if (v == NULL) |
| 672 | return array_ass_slice(a, i, i+1, v); |
| 673 | return (*a->ob_descr->setitem)(a, i, v); |
| 674 | } |
| 675 | |
| 676 | static int |
| 677 | setarrayitem(a, i, v) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 678 | PyObject *a; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 679 | int i; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 680 | PyObject *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 681 | { |
| 682 | if (!is_arrayobject(a)) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 683 | PyErr_BadInternalCall(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 684 | return -1; |
| 685 | } |
| 686 | return array_ass_item((arrayobject *)a, i, v); |
| 687 | } |
| 688 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 689 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 690 | ins(self, where, v) |
| 691 | arrayobject *self; |
| 692 | int where; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 693 | PyObject *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 694 | { |
| 695 | if (ins1(self, where, v) != 0) |
| 696 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 697 | Py_INCREF(Py_None); |
| 698 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 701 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 702 | array_insert(self, args) |
| 703 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 704 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 705 | { |
| 706 | int i; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 707 | PyObject *v; |
| 708 | if (!PyArg_Parse(args, "(iO)", &i, &v)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 709 | return NULL; |
| 710 | return ins(self, i, v); |
| 711 | } |
| 712 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 713 | static char insert_doc [] = |
| 714 | "insert (i,x)\n\ |
| 715 | \n\ |
| 716 | Insert a new item x into the array before position i."; |
| 717 | |
| 718 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 719 | static PyObject * |
Guido van Rossum | de4a4ca | 1997-08-12 14:55:56 +0000 | [diff] [blame] | 720 | array_buffer_info(self, args) |
| 721 | arrayobject *self; |
| 722 | PyObject *args; |
| 723 | { |
| 724 | return Py_BuildValue("ll", |
| 725 | (long)(self->ob_item), (long)(self->ob_size)); |
| 726 | } |
| 727 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 728 | static char buffer_info_doc [] = |
| 729 | "buffer_info -> (address, length)\n\ |
| 730 | \n\ |
| 731 | Return a tuple (address, length) giving the current memory address and\n\ |
| 732 | the length in bytes of the buffer used to hold array's contents."; |
| 733 | |
| 734 | |
Guido van Rossum | de4a4ca | 1997-08-12 14:55:56 +0000 | [diff] [blame] | 735 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 736 | array_append(self, args) |
| 737 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 738 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 739 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 740 | PyObject *v; |
| 741 | if (!PyArg_Parse(args, "O", &v)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 742 | return NULL; |
| 743 | return ins(self, (int) self->ob_size, v); |
| 744 | } |
| 745 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 746 | static char append_doc [] = |
| 747 | "append(x)\n\ |
| 748 | \n\ |
| 749 | Append new value x to the end of the array."; |
| 750 | |
| 751 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 752 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 753 | array_byteswap(self, args) |
| 754 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 755 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 756 | { |
| 757 | char *p; |
| 758 | int i; |
Fred Drake | bf27298 | 1999-12-03 17:15:30 +0000 | [diff] [blame] | 759 | |
| 760 | if (!PyArg_ParseTuple(args, ":byteswap")) |
| 761 | return NULL; |
| 762 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 763 | switch (self->ob_descr->itemsize) { |
| 764 | case 1: |
| 765 | break; |
| 766 | case 2: |
| 767 | for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 2) { |
| 768 | char p0 = p[0]; |
| 769 | p[0] = p[1]; |
| 770 | p[1] = p0; |
| 771 | } |
| 772 | break; |
| 773 | case 4: |
| 774 | for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 4) { |
| 775 | char p0 = p[0]; |
| 776 | char p1 = p[1]; |
| 777 | p[0] = p[3]; |
| 778 | p[1] = p[2]; |
| 779 | p[2] = p1; |
| 780 | p[3] = p0; |
| 781 | } |
| 782 | break; |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 783 | case 8: |
| 784 | for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 8) { |
| 785 | char p0 = p[0]; |
| 786 | char p1 = p[1]; |
| 787 | char p2 = p[2]; |
| 788 | char p3 = p[3]; |
| 789 | p[0] = p[7]; |
| 790 | p[1] = p[6]; |
| 791 | p[2] = p[5]; |
| 792 | p[3] = p[4]; |
| 793 | p[4] = p3; |
| 794 | p[5] = p2; |
| 795 | p[6] = p1; |
| 796 | p[7] = p0; |
| 797 | } |
| 798 | break; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 799 | default: |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 800 | PyErr_SetString(PyExc_RuntimeError, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 801 | "don't know how to byteswap this array type"); |
| 802 | return NULL; |
| 803 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 804 | Py_INCREF(Py_None); |
| 805 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 808 | static char byteswap_doc [] = |
Fred Drake | bf27298 | 1999-12-03 17:15:30 +0000 | [diff] [blame] | 809 | "byteswap()\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 810 | \n\ |
Fred Drake | bf27298 | 1999-12-03 17:15:30 +0000 | [diff] [blame] | 811 | Byteswap all items of the array. If the items in the array are not 1, 2,\n\ |
| 812 | 4, or 8 bytes in size, RuntimeError is raised."; |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 813 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 814 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 815 | array_reverse(self, args) |
| 816 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 817 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 818 | { |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 819 | register int itemsize = self->ob_descr->itemsize; |
| 820 | register char *p, *q; |
| 821 | char tmp[sizeof(double)]; /* Assume that's the max item size */ |
| 822 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 823 | if (args != NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 824 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 825 | return NULL; |
| 826 | } |
| 827 | |
| 828 | if (self->ob_size > 1) { |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 829 | for (p = self->ob_item, |
| 830 | q = self->ob_item + (self->ob_size - 1)*itemsize; |
| 831 | p < q; |
| 832 | p += itemsize, q -= itemsize) { |
| 833 | memmove(tmp, p, itemsize); |
| 834 | memmove(p, q, itemsize); |
| 835 | memmove(q, tmp, itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 836 | } |
| 837 | } |
| 838 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 839 | Py_INCREF(Py_None); |
| 840 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 841 | } |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 842 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 843 | static char reverse_doc [] = |
| 844 | "reverse()\n\ |
| 845 | \n\ |
| 846 | Reverse the order of the items in the array."; |
| 847 | |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 848 | /* The following routines were adapted from listobject.c but not converted. |
| 849 | To make them work you will have to work! */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 850 | |
| 851 | #if 0 |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 852 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 853 | array_index(self, args) |
| 854 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 855 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 856 | { |
| 857 | int i; |
| 858 | |
| 859 | if (args == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 860 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 861 | return NULL; |
| 862 | } |
| 863 | for (i = 0; i < self->ob_size; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 864 | if (PyObject_Compare(self->ob_item[i], args) == 0) |
| 865 | return PyInt_FromLong((long)i); |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 866 | /* XXX PyErr_Occurred */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 867 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 868 | PyErr_SetString(PyExc_ValueError, "array.index(x): x not in array"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 869 | return NULL; |
| 870 | } |
| 871 | #endif |
| 872 | |
| 873 | #if 0 |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 874 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 875 | array_count(self, args) |
| 876 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 877 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 878 | { |
| 879 | int count = 0; |
| 880 | int i; |
| 881 | |
| 882 | if (args == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 883 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 884 | return NULL; |
| 885 | } |
| 886 | for (i = 0; i < self->ob_size; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 887 | if (PyObject_Compare(self->ob_item[i], args) == 0) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 888 | count++; |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 889 | /* XXX PyErr_Occurred */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 890 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 891 | return PyInt_FromLong((long)count); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 892 | } |
| 893 | #endif |
| 894 | |
| 895 | #if 0 |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 896 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 897 | array_remove(self, args) |
| 898 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 899 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 900 | { |
| 901 | int i; |
| 902 | |
| 903 | if (args == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 904 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 905 | return NULL; |
| 906 | } |
| 907 | for (i = 0; i < self->ob_size; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 908 | if (PyObject_Compare(self->ob_item[i], args) == 0) { |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 909 | if (array_ass_slice(self, i, i+1, |
| 910 | (PyObject *)NULL) != 0) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 911 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 912 | Py_INCREF(Py_None); |
| 913 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 914 | } |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 915 | /* XXX PyErr_Occurred */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 916 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 917 | PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in array"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 918 | return NULL; |
| 919 | } |
| 920 | #endif |
| 921 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 922 | static PyObject * |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 923 | array_fromfile(self, args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 924 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 925 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 926 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 927 | PyObject *f; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 928 | int n; |
| 929 | FILE *fp; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 930 | if (!PyArg_Parse(args, "(Oi)", &f, &n)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 931 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 932 | fp = PyFile_AsFile(f); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 933 | if (fp == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 934 | PyErr_SetString(PyExc_TypeError, "arg1 must be open file"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 935 | return NULL; |
| 936 | } |
| 937 | if (n > 0) { |
| 938 | char *item = self->ob_item; |
| 939 | int itemsize = self->ob_descr->itemsize; |
| 940 | int nread; |
Guido van Rossum | 3791b0d | 1999-02-23 18:05:22 +0000 | [diff] [blame] | 941 | int newlength; |
| 942 | size_t newbytes; |
| 943 | /* Be careful here about overflow */ |
| 944 | if ((newlength = self->ob_size + n) <= 0 || |
Guido van Rossum | 481ac88 | 1999-03-19 21:50:11 +0000 | [diff] [blame] | 945 | (newbytes = newlength * itemsize) / itemsize != |
| 946 | (size_t)newlength) |
Guido van Rossum | 3791b0d | 1999-02-23 18:05:22 +0000 | [diff] [blame] | 947 | goto nomem; |
| 948 | PyMem_RESIZE(item, char, newbytes); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 949 | if (item == NULL) { |
Guido van Rossum | 3791b0d | 1999-02-23 18:05:22 +0000 | [diff] [blame] | 950 | nomem: |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 951 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 952 | return NULL; |
| 953 | } |
| 954 | self->ob_item = item; |
| 955 | self->ob_size += n; |
| 956 | nread = fread(item + (self->ob_size - n) * itemsize, |
| 957 | itemsize, n, fp); |
| 958 | if (nread < n) { |
| 959 | self->ob_size -= (n - nread); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 960 | PyMem_RESIZE(item, char, self->ob_size*itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 961 | self->ob_item = item; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 962 | PyErr_SetString(PyExc_EOFError, |
| 963 | "not enough items in file"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 964 | return NULL; |
| 965 | } |
| 966 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 967 | Py_INCREF(Py_None); |
| 968 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 971 | static char fromfile_doc [] = |
| 972 | "fromfile(f, n)\n\ |
| 973 | \n\ |
| 974 | Read n objects from the file object f and append them to the end of the\n\ |
| 975 | array. Also called as read."; |
| 976 | |
| 977 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 978 | static PyObject * |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 979 | array_tofile(self, args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 980 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 981 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 982 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 983 | PyObject *f; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 984 | FILE *fp; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 985 | if (!PyArg_Parse(args, "O", &f)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 986 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 987 | fp = PyFile_AsFile(f); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 988 | if (fp == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 989 | PyErr_SetString(PyExc_TypeError, "arg must be open file"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 990 | return NULL; |
| 991 | } |
| 992 | if (self->ob_size > 0) { |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 993 | if ((int)fwrite(self->ob_item, self->ob_descr->itemsize, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 994 | self->ob_size, fp) != self->ob_size) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 995 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 996 | clearerr(fp); |
| 997 | return NULL; |
| 998 | } |
| 999 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1000 | Py_INCREF(Py_None); |
| 1001 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1004 | static char tofile_doc [] = |
| 1005 | "tofile(f)\n\ |
| 1006 | \n\ |
| 1007 | Write all items (as machine values) to the file object f. Also called as\n\ |
| 1008 | write."; |
| 1009 | |
| 1010 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1011 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1012 | array_fromlist(self, args) |
| 1013 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1014 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1015 | { |
| 1016 | int n; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1017 | PyObject *list; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1018 | int itemsize = self->ob_descr->itemsize; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1019 | if (!PyArg_Parse(args, "O", &list)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1020 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1021 | if (!PyList_Check(list)) { |
| 1022 | PyErr_SetString(PyExc_TypeError, "arg must be list"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1023 | return NULL; |
| 1024 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1025 | n = PyList_Size(list); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1026 | if (n > 0) { |
| 1027 | char *item = self->ob_item; |
| 1028 | int i; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1029 | PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1030 | if (item == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1031 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1032 | return NULL; |
| 1033 | } |
| 1034 | self->ob_item = item; |
| 1035 | self->ob_size += n; |
| 1036 | for (i = 0; i < n; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1037 | PyObject *v = PyList_GetItem(list, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1038 | if ((*self->ob_descr->setitem)(self, |
| 1039 | self->ob_size - n + i, v) != 0) { |
| 1040 | self->ob_size -= n; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1041 | PyMem_RESIZE(item, char, |
| 1042 | self->ob_size * itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1043 | self->ob_item = item; |
| 1044 | return NULL; |
| 1045 | } |
| 1046 | } |
| 1047 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1048 | Py_INCREF(Py_None); |
| 1049 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1052 | static char fromlist_doc [] = |
| 1053 | "fromlist(list)\n\ |
| 1054 | \n\ |
| 1055 | Append items to array from list."; |
| 1056 | |
| 1057 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1058 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1059 | array_tolist(self, args) |
| 1060 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1061 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1062 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1063 | PyObject *list = PyList_New(self->ob_size); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1064 | int i; |
| 1065 | if (list == NULL) |
| 1066 | return NULL; |
| 1067 | for (i = 0; i < self->ob_size; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1068 | PyObject *v = getarrayitem((PyObject *)self, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1069 | if (v == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1070 | Py_DECREF(list); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1071 | return NULL; |
| 1072 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1073 | PyList_SetItem(list, i, v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1074 | } |
| 1075 | return list; |
| 1076 | } |
| 1077 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1078 | static char tolist_doc [] = |
Guido van Rossum | fc6aba5 | 1998-10-14 02:52:31 +0000 | [diff] [blame] | 1079 | "tolist() -> list\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1080 | \n\ |
| 1081 | Convert array to an ordinary list with the same items."; |
| 1082 | |
| 1083 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1084 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1085 | array_fromstring(self, args) |
| 1086 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1087 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1088 | { |
| 1089 | char *str; |
| 1090 | int n; |
| 1091 | int itemsize = self->ob_descr->itemsize; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1092 | if (!PyArg_Parse(args, "s#", &str, &n)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1093 | return NULL; |
| 1094 | if (n % itemsize != 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1095 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1096 | "string length not a multiple of item size"); |
| 1097 | return NULL; |
| 1098 | } |
| 1099 | n = n / itemsize; |
| 1100 | if (n > 0) { |
| 1101 | char *item = self->ob_item; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1102 | PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1103 | if (item == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1104 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1105 | return NULL; |
| 1106 | } |
| 1107 | self->ob_item = item; |
| 1108 | self->ob_size += n; |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1109 | memcpy(item + (self->ob_size - n) * itemsize, |
| 1110 | str, itemsize*n); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1111 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1112 | Py_INCREF(Py_None); |
| 1113 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1116 | static char fromstring_doc [] = |
| 1117 | "fromstring(string)\n\ |
| 1118 | \n\ |
| 1119 | Appends items from the string, interpreting it as an array of machine\n\ |
| 1120 | values,as if it had been read from a file using the fromfile() method)."; |
| 1121 | |
| 1122 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1123 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1124 | array_tostring(self, args) |
| 1125 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1126 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1127 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1128 | if (!PyArg_Parse(args, "")) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1129 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1130 | return PyString_FromStringAndSize(self->ob_item, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1131 | self->ob_size * self->ob_descr->itemsize); |
| 1132 | } |
| 1133 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1134 | static char tostring_doc [] = |
| 1135 | "tostring() -> string\n\ |
| 1136 | \n\ |
| 1137 | Convert the array to an array of machine values and return the string\n\ |
| 1138 | representation."; |
| 1139 | |
| 1140 | PyMethodDef array_methods[] = { |
| 1141 | {"append", (PyCFunction)array_append, 0, append_doc}, |
| 1142 | {"buffer_info", (PyCFunction)array_buffer_info, 0, buffer_info_doc}, |
Fred Drake | bf27298 | 1999-12-03 17:15:30 +0000 | [diff] [blame] | 1143 | {"byteswap", (PyCFunction)array_byteswap, METH_VARARGS, |
| 1144 | byteswap_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1145 | /* {"count", (method)array_count},*/ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1146 | {"fromfile", (PyCFunction)array_fromfile, 0, fromfile_doc}, |
| 1147 | {"fromlist", (PyCFunction)array_fromlist, 0, fromlist_doc}, |
| 1148 | {"fromstring", (PyCFunction)array_fromstring, 0, fromstring_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1149 | /* {"index", (method)array_index},*/ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1150 | {"insert", (PyCFunction)array_insert, 0, insert_doc}, |
| 1151 | {"read", (PyCFunction)array_fromfile, 0, fromfile_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1152 | /* {"remove", (method)array_remove},*/ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1153 | {"reverse", (PyCFunction)array_reverse, 0, reverse_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1154 | /* {"sort", (method)array_sort},*/ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1155 | {"tofile", (PyCFunction)array_tofile, 0, tofile_doc}, |
| 1156 | {"tolist", (PyCFunction)array_tolist, 0, tolist_doc}, |
| 1157 | {"tostring", (PyCFunction)array_tostring, 0, tostring_doc}, |
| 1158 | {"write", (PyCFunction)array_tofile, 0, tofile_doc}, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1159 | {NULL, NULL} /* sentinel */ |
| 1160 | }; |
| 1161 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1162 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1163 | array_getattr(a, name) |
| 1164 | arrayobject *a; |
| 1165 | char *name; |
| 1166 | { |
| 1167 | if (strcmp(name, "typecode") == 0) { |
| 1168 | char tc = a->ob_descr->typecode; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1169 | return PyString_FromStringAndSize(&tc, 1); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1170 | } |
| 1171 | if (strcmp(name, "itemsize") == 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1172 | return PyInt_FromLong((long)a->ob_descr->itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1173 | } |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1174 | if (strcmp(name, "__members__") == 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1175 | PyObject *list = PyList_New(2); |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1176 | if (list) { |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1177 | PyList_SetItem(list, 0, |
| 1178 | PyString_FromString("typecode")); |
| 1179 | PyList_SetItem(list, 1, |
| 1180 | PyString_FromString("itemsize")); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1181 | if (PyErr_Occurred()) { |
| 1182 | Py_DECREF(list); |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1183 | list = NULL; |
| 1184 | } |
| 1185 | } |
| 1186 | return list; |
| 1187 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1188 | return Py_FindMethod(array_methods, (PyObject *)a, name); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | static int |
| 1192 | array_print(a, fp, flags) |
| 1193 | arrayobject *a; |
| 1194 | FILE *fp; |
| 1195 | int flags; |
| 1196 | { |
| 1197 | int ok = 0; |
| 1198 | int i, len; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1199 | PyObject *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1200 | len = a->ob_size; |
| 1201 | if (len == 0) { |
| 1202 | fprintf(fp, "array('%c')", a->ob_descr->typecode); |
| 1203 | return ok; |
| 1204 | } |
| 1205 | if (a->ob_descr->typecode == 'c') { |
| 1206 | fprintf(fp, "array('c', "); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1207 | v = array_tostring(a, (PyObject *)NULL); |
| 1208 | ok = PyObject_Print(v, fp, 0); |
| 1209 | Py_XDECREF(v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1210 | fprintf(fp, ")"); |
| 1211 | return ok; |
| 1212 | } |
| 1213 | fprintf(fp, "array('%c', [", a->ob_descr->typecode); |
| 1214 | for (i = 0; i < len && ok == 0; i++) { |
| 1215 | if (i > 0) |
| 1216 | fprintf(fp, ", "); |
| 1217 | v = (a->ob_descr->getitem)(a, i); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1218 | ok = PyObject_Print(v, fp, 0); |
| 1219 | Py_XDECREF(v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1220 | } |
| 1221 | fprintf(fp, "])"); |
| 1222 | return ok; |
| 1223 | } |
| 1224 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1225 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1226 | array_repr(a) |
| 1227 | arrayobject *a; |
| 1228 | { |
| 1229 | char buf[256]; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1230 | PyObject *s, *t, *comma, *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1231 | int i, len; |
| 1232 | len = a->ob_size; |
| 1233 | if (len == 0) { |
| 1234 | sprintf(buf, "array('%c')", a->ob_descr->typecode); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1235 | return PyString_FromString(buf); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1236 | } |
| 1237 | if (a->ob_descr->typecode == 'c') { |
| 1238 | sprintf(buf, "array('c', "); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1239 | s = PyString_FromString(buf); |
| 1240 | v = array_tostring(a, (PyObject *)NULL); |
| 1241 | t = PyObject_Repr(v); |
| 1242 | Py_XDECREF(v); |
| 1243 | PyString_ConcatAndDel(&s, t); |
| 1244 | PyString_ConcatAndDel(&s, PyString_FromString(")")); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1245 | return s; |
| 1246 | } |
| 1247 | sprintf(buf, "array('%c', [", a->ob_descr->typecode); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1248 | s = PyString_FromString(buf); |
| 1249 | comma = PyString_FromString(", "); |
| 1250 | for (i = 0; i < len && !PyErr_Occurred(); i++) { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1251 | if (i > 0) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1252 | PyString_Concat(&s, comma); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1253 | v = (a->ob_descr->getitem)(a, i); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1254 | t = PyObject_Repr(v); |
| 1255 | Py_XDECREF(v); |
| 1256 | PyString_ConcatAndDel(&s, t); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1257 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1258 | Py_XDECREF(comma); |
| 1259 | PyString_ConcatAndDel(&s, PyString_FromString("])")); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1260 | return s; |
| 1261 | } |
| 1262 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1263 | static int |
| 1264 | array_buffer_getreadbuf(self, index, ptr) |
| 1265 | arrayobject *self; |
| 1266 | int index; |
| 1267 | const void **ptr; |
| 1268 | { |
| 1269 | if ( index != 0 ) { |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1270 | PyErr_SetString(PyExc_SystemError, |
| 1271 | "Accessing non-existent array segment"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1272 | return -1; |
| 1273 | } |
| 1274 | *ptr = (void *)self->ob_item; |
| 1275 | return self->ob_size*self->ob_descr->itemsize; |
| 1276 | } |
| 1277 | |
| 1278 | static int |
| 1279 | array_buffer_getwritebuf(self, index, ptr) |
| 1280 | arrayobject *self; |
| 1281 | int index; |
| 1282 | const void **ptr; |
| 1283 | { |
| 1284 | if ( index != 0 ) { |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1285 | PyErr_SetString(PyExc_SystemError, |
| 1286 | "Accessing non-existent array segment"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1287 | return -1; |
| 1288 | } |
| 1289 | *ptr = (void *)self->ob_item; |
| 1290 | return self->ob_size*self->ob_descr->itemsize; |
| 1291 | } |
| 1292 | |
| 1293 | static int |
| 1294 | array_buffer_getsegcount(self, lenp) |
| 1295 | arrayobject *self; |
| 1296 | int *lenp; |
| 1297 | { |
| 1298 | if ( lenp ) |
| 1299 | *lenp = self->ob_size*self->ob_descr->itemsize; |
| 1300 | return 1; |
| 1301 | } |
| 1302 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1303 | static PySequenceMethods array_as_sequence = { |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1304 | (inquiry)array_length, /*sq_length*/ |
| 1305 | (binaryfunc)array_concat, /*sq_concat*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1306 | (intargfunc)array_repeat, /*sq_repeat*/ |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1307 | (intargfunc)array_item, /*sq_item*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1308 | (intintargfunc)array_slice, /*sq_slice*/ |
| 1309 | (intobjargproc)array_ass_item, /*sq_ass_item*/ |
| 1310 | (intintobjargproc)array_ass_slice, /*sq_ass_slice*/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1311 | }; |
| 1312 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1313 | static PyBufferProcs array_as_buffer = { |
| 1314 | (getreadbufferproc)array_buffer_getreadbuf, |
| 1315 | (getwritebufferproc)array_buffer_getwritebuf, |
| 1316 | (getsegcountproc)array_buffer_getsegcount, |
| 1317 | }; |
| 1318 | |
| 1319 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1320 | |
| 1321 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1322 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1323 | a_array(self, args) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1324 | PyObject *self; |
| 1325 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1326 | { |
| 1327 | char c; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1328 | PyObject *initial = NULL; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1329 | struct arraydescr *descr; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1330 | if (!PyArg_Parse(args, "c", &c)) { |
| 1331 | PyErr_Clear(); |
| 1332 | if (!PyArg_Parse(args, "(cO)", &c, &initial)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1333 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1334 | if (!PyList_Check(initial) && !PyString_Check(initial)) { |
| 1335 | PyErr_SetString(PyExc_TypeError, |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1336 | "array initializer must be list or string"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1337 | return NULL; |
| 1338 | } |
| 1339 | } |
| 1340 | for (descr = descriptors; descr->typecode != '\0'; descr++) { |
| 1341 | if (descr->typecode == c) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1342 | PyObject *a; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1343 | int len; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1344 | if (initial == NULL || !PyList_Check(initial)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1345 | len = 0; |
| 1346 | else |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1347 | len = PyList_Size(initial); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1348 | a = newarrayobject(len, descr); |
| 1349 | if (a == NULL) |
| 1350 | return NULL; |
| 1351 | if (len > 0) { |
| 1352 | int i; |
| 1353 | for (i = 0; i < len; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1354 | PyObject *v = |
| 1355 | PyList_GetItem(initial, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1356 | if (setarrayitem(a, i, v) != 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1357 | Py_DECREF(a); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1358 | return NULL; |
| 1359 | } |
| 1360 | } |
| 1361 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1362 | if (initial != NULL && PyString_Check(initial)) { |
| 1363 | PyObject *v = |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1364 | array_fromstring((arrayobject *)a, initial); |
| 1365 | if (v == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1366 | Py_DECREF(a); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1367 | return NULL; |
| 1368 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1369 | Py_DECREF(v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1370 | } |
| 1371 | return a; |
| 1372 | } |
| 1373 | } |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1374 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 1375 | "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] | 1376 | return NULL; |
| 1377 | } |
| 1378 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1379 | static char a_array_doc [] = |
| 1380 | "array(typecode [, initializer]) -> array\n\ |
| 1381 | \n\ |
| 1382 | Return a new array whose items are restricted by typecode, and\n\ |
| 1383 | initialized from the optional initializer value, which must be a list\n\ |
| 1384 | or a string."; |
| 1385 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1386 | static PyMethodDef a_methods[] = { |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1387 | {"array", a_array, 0, a_array_doc}, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1388 | {NULL, NULL} /* sentinel */ |
| 1389 | }; |
| 1390 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1391 | static char module_doc [] = |
| 1392 | "This module defines a new object type which can efficiently represent\n\ |
| 1393 | an array of basic values: characters, integers, floating point\n\ |
| 1394 | numbers. Arrays are sequence types and behave very much like lists,\n\ |
| 1395 | except that the type of objects stored in them is constrained. The\n\ |
| 1396 | type is specified at object creation time by using a type code, which\n\ |
| 1397 | is a single character. The following type codes are defined:\n\ |
| 1398 | \n\ |
| 1399 | Type code C Type Minimum size in bytes \n\ |
| 1400 | 'c' character 1 \n\ |
| 1401 | 'b' signed integer 1 \n\ |
| 1402 | 'B' unsigned integer 1 \n\ |
| 1403 | 'h' signed integer 2 \n\ |
| 1404 | 'H' unsigned integer 2 \n\ |
| 1405 | 'i' signed integer 2 \n\ |
| 1406 | 'I' unsigned integer 2 \n\ |
| 1407 | 'l' signed integer 4 \n\ |
| 1408 | 'L' unsigned integer 4 \n\ |
| 1409 | 'f' floating point 4 \n\ |
| 1410 | 'd' floating point 8 \n\ |
| 1411 | \n\ |
| 1412 | Functions:\n\ |
| 1413 | \n\ |
| 1414 | array(typecode [, initializer]) -- create a new array\n\ |
| 1415 | \n\ |
| 1416 | Special Objects:\n\ |
| 1417 | \n\ |
| 1418 | ArrayType -- type object for array objects\n\ |
| 1419 | "; |
| 1420 | |
| 1421 | static char arraytype_doc [] = |
| 1422 | "An array represents basic values and behave very much like lists, except\n\ |
| 1423 | the type of objects stored in them is constrained.\n\ |
| 1424 | \n\ |
| 1425 | Methods:\n\ |
| 1426 | \n\ |
| 1427 | append() -- append a new item to the end of the array\n\ |
| 1428 | buffer_info() -- return information giving the current memory info\n\ |
| 1429 | byteswap() -- byteswap all the items of the array\n\ |
| 1430 | fromfile() -- read items from a file object\n\ |
| 1431 | fromlist() -- append items from the list\n\ |
| 1432 | fromstring() -- append items from the string\n\ |
| 1433 | insert() -- insert a new item into the array at a provided position\n\ |
| 1434 | read() -- DEPRECATED, use fromfile()\n\ |
| 1435 | reverse() -- reverse the order of the items in the array\n\ |
| 1436 | tofile() -- write all items to a file object\n\ |
| 1437 | tolist() -- return the array converted to an ordinary list\n\ |
| 1438 | tostring() -- return the array converted to a string\n\ |
| 1439 | write() -- DEPRECATED, use tofile()\n\ |
| 1440 | \n\ |
| 1441 | Variables:\n\ |
| 1442 | \n\ |
| 1443 | typecode -- the typecode character used to create the array\n\ |
| 1444 | itemsize -- the length in bytes of one array item\n\ |
| 1445 | "; |
| 1446 | |
| 1447 | statichere PyTypeObject Arraytype = { |
Fred Drake | 0d40ba4 | 2000-02-04 20:33:49 +0000 | [diff] [blame] | 1448 | PyObject_HEAD_INIT(NULL) |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1449 | 0, |
| 1450 | "array", |
| 1451 | sizeof(arrayobject), |
| 1452 | 0, |
| 1453 | (destructor)array_dealloc, /*tp_dealloc*/ |
| 1454 | (printfunc)array_print, /*tp_print*/ |
| 1455 | (getattrfunc)array_getattr, /*tp_getattr*/ |
| 1456 | 0, /*tp_setattr*/ |
| 1457 | (cmpfunc)array_compare, /*tp_compare*/ |
| 1458 | (reprfunc)array_repr, /*tp_repr*/ |
| 1459 | 0, /*tp_as_number*/ |
| 1460 | &array_as_sequence, /*tp_as_sequence*/ |
| 1461 | 0, /*tp_as_mapping*/ |
| 1462 | 0, /*tp_hash*/ |
| 1463 | 0, /*tp_call*/ |
| 1464 | 0, /*tp_str*/ |
| 1465 | 0, /*tp_getattro*/ |
| 1466 | 0, /*tp_setattro*/ |
| 1467 | &array_as_buffer, /*tp_as_buffer*/ |
| 1468 | 0, /*tp_xxx4*/ |
| 1469 | arraytype_doc, /*tp_doc*/ |
| 1470 | }; |
| 1471 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 1472 | DL_EXPORT(void) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1473 | initarray() |
| 1474 | { |
Guido van Rossum | b6190d3 | 1997-05-22 14:56:36 +0000 | [diff] [blame] | 1475 | PyObject *m, *d; |
Fred Drake | 0d40ba4 | 2000-02-04 20:33:49 +0000 | [diff] [blame] | 1476 | |
| 1477 | Arraytype.ob_type = &PyType_Type; |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1478 | m = Py_InitModule3("array", a_methods, module_doc); |
Guido van Rossum | b6190d3 | 1997-05-22 14:56:36 +0000 | [diff] [blame] | 1479 | d = PyModule_GetDict(m); |
Guido van Rossum | a0deb64 | 1998-10-14 13:45:06 +0000 | [diff] [blame] | 1480 | PyDict_SetItemString(d, "ArrayType", (PyObject *)&Arraytype); |
| 1481 | /* 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] | 1482 | } |