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; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 366 | _Py_NewReference(op); |
| 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; |
| 762 | switch (self->ob_descr->itemsize) { |
| 763 | case 1: |
| 764 | break; |
| 765 | case 2: |
| 766 | for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 2) { |
| 767 | char p0 = p[0]; |
| 768 | p[0] = p[1]; |
| 769 | p[1] = p0; |
| 770 | } |
| 771 | break; |
| 772 | case 4: |
| 773 | for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 4) { |
| 774 | char p0 = p[0]; |
| 775 | char p1 = p[1]; |
| 776 | p[0] = p[3]; |
| 777 | p[1] = p[2]; |
| 778 | p[2] = p1; |
| 779 | p[3] = p0; |
| 780 | } |
| 781 | break; |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 782 | case 8: |
| 783 | for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 8) { |
| 784 | char p0 = p[0]; |
| 785 | char p1 = p[1]; |
| 786 | char p2 = p[2]; |
| 787 | char p3 = p[3]; |
| 788 | p[0] = p[7]; |
| 789 | p[1] = p[6]; |
| 790 | p[2] = p[5]; |
| 791 | p[3] = p[4]; |
| 792 | p[4] = p3; |
| 793 | p[5] = p2; |
| 794 | p[6] = p1; |
| 795 | p[7] = p0; |
| 796 | } |
| 797 | break; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 798 | default: |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 799 | PyErr_SetString(PyExc_RuntimeError, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 800 | "don't know how to byteswap this array type"); |
| 801 | return NULL; |
| 802 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 803 | Py_INCREF(Py_None); |
| 804 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 807 | static char byteswap_doc [] = |
| 808 | "byteswap(x)\n\ |
| 809 | \n\ |
| 810 | Byteswap all items of the array. This is only supported for integer\n\ |
| 811 | values of x, which determines the size of the blocks swapped."; |
| 812 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 813 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 814 | array_reverse(self, args) |
| 815 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 816 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 817 | { |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 818 | register int itemsize = self->ob_descr->itemsize; |
| 819 | register char *p, *q; |
| 820 | char tmp[sizeof(double)]; /* Assume that's the max item size */ |
| 821 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 822 | if (args != NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 823 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 824 | return NULL; |
| 825 | } |
| 826 | |
| 827 | if (self->ob_size > 1) { |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 828 | for (p = self->ob_item, |
| 829 | q = self->ob_item + (self->ob_size - 1)*itemsize; |
| 830 | p < q; |
| 831 | p += itemsize, q -= itemsize) { |
| 832 | memmove(tmp, p, itemsize); |
| 833 | memmove(p, q, itemsize); |
| 834 | memmove(q, tmp, itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 838 | Py_INCREF(Py_None); |
| 839 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 840 | } |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 841 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 842 | static char reverse_doc [] = |
| 843 | "reverse()\n\ |
| 844 | \n\ |
| 845 | Reverse the order of the items in the array."; |
| 846 | |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 847 | /* The following routines were adapted from listobject.c but not converted. |
| 848 | To make them work you will have to work! */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 849 | |
| 850 | #if 0 |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 851 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 852 | array_index(self, args) |
| 853 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 854 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 855 | { |
| 856 | int i; |
| 857 | |
| 858 | if (args == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 859 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 860 | return NULL; |
| 861 | } |
| 862 | for (i = 0; i < self->ob_size; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 863 | if (PyObject_Compare(self->ob_item[i], args) == 0) |
| 864 | return PyInt_FromLong((long)i); |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 865 | /* XXX PyErr_Occurred */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 866 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 867 | PyErr_SetString(PyExc_ValueError, "array.index(x): x not in array"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 868 | return NULL; |
| 869 | } |
| 870 | #endif |
| 871 | |
| 872 | #if 0 |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 873 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 874 | array_count(self, args) |
| 875 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 876 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 877 | { |
| 878 | int count = 0; |
| 879 | int i; |
| 880 | |
| 881 | if (args == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 882 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 883 | return NULL; |
| 884 | } |
| 885 | for (i = 0; i < self->ob_size; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 886 | if (PyObject_Compare(self->ob_item[i], args) == 0) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 887 | count++; |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 888 | /* XXX PyErr_Occurred */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 889 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 890 | return PyInt_FromLong((long)count); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 891 | } |
| 892 | #endif |
| 893 | |
| 894 | #if 0 |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 895 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 896 | array_remove(self, args) |
| 897 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 898 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 899 | { |
| 900 | int i; |
| 901 | |
| 902 | if (args == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 903 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 904 | return NULL; |
| 905 | } |
| 906 | for (i = 0; i < self->ob_size; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 907 | if (PyObject_Compare(self->ob_item[i], args) == 0) { |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 908 | if (array_ass_slice(self, i, i+1, |
| 909 | (PyObject *)NULL) != 0) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 910 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 911 | Py_INCREF(Py_None); |
| 912 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 913 | } |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 914 | /* XXX PyErr_Occurred */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 915 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 916 | PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in array"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 917 | return NULL; |
| 918 | } |
| 919 | #endif |
| 920 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 921 | static PyObject * |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 922 | array_fromfile(self, args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 923 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 924 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 925 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 926 | PyObject *f; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 927 | int n; |
| 928 | FILE *fp; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 929 | if (!PyArg_Parse(args, "(Oi)", &f, &n)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 930 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 931 | fp = PyFile_AsFile(f); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 932 | if (fp == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 933 | PyErr_SetString(PyExc_TypeError, "arg1 must be open file"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 934 | return NULL; |
| 935 | } |
| 936 | if (n > 0) { |
| 937 | char *item = self->ob_item; |
| 938 | int itemsize = self->ob_descr->itemsize; |
| 939 | int nread; |
Guido van Rossum | 3791b0d | 1999-02-23 18:05:22 +0000 | [diff] [blame] | 940 | int newlength; |
| 941 | size_t newbytes; |
| 942 | /* Be careful here about overflow */ |
| 943 | if ((newlength = self->ob_size + n) <= 0 || |
Guido van Rossum | 481ac88 | 1999-03-19 21:50:11 +0000 | [diff] [blame] | 944 | (newbytes = newlength * itemsize) / itemsize != |
| 945 | (size_t)newlength) |
Guido van Rossum | 3791b0d | 1999-02-23 18:05:22 +0000 | [diff] [blame] | 946 | goto nomem; |
| 947 | PyMem_RESIZE(item, char, newbytes); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 948 | if (item == NULL) { |
Guido van Rossum | 3791b0d | 1999-02-23 18:05:22 +0000 | [diff] [blame] | 949 | nomem: |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 950 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 951 | return NULL; |
| 952 | } |
| 953 | self->ob_item = item; |
| 954 | self->ob_size += n; |
| 955 | nread = fread(item + (self->ob_size - n) * itemsize, |
| 956 | itemsize, n, fp); |
| 957 | if (nread < n) { |
| 958 | self->ob_size -= (n - nread); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 959 | PyMem_RESIZE(item, char, self->ob_size*itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 960 | self->ob_item = item; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 961 | PyErr_SetString(PyExc_EOFError, |
| 962 | "not enough items in file"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 963 | return NULL; |
| 964 | } |
| 965 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 966 | Py_INCREF(Py_None); |
| 967 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 970 | static char fromfile_doc [] = |
| 971 | "fromfile(f, n)\n\ |
| 972 | \n\ |
| 973 | Read n objects from the file object f and append them to the end of the\n\ |
| 974 | array. Also called as read."; |
| 975 | |
| 976 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 977 | static PyObject * |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 978 | array_tofile(self, args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 979 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 980 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 981 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 982 | PyObject *f; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 983 | FILE *fp; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 984 | if (!PyArg_Parse(args, "O", &f)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 985 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 986 | fp = PyFile_AsFile(f); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 987 | if (fp == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 988 | PyErr_SetString(PyExc_TypeError, "arg must be open file"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 989 | return NULL; |
| 990 | } |
| 991 | if (self->ob_size > 0) { |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 992 | if ((int)fwrite(self->ob_item, self->ob_descr->itemsize, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 993 | self->ob_size, fp) != self->ob_size) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 994 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 995 | clearerr(fp); |
| 996 | return NULL; |
| 997 | } |
| 998 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 999 | Py_INCREF(Py_None); |
| 1000 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1003 | static char tofile_doc [] = |
| 1004 | "tofile(f)\n\ |
| 1005 | \n\ |
| 1006 | Write all items (as machine values) to the file object f. Also called as\n\ |
| 1007 | write."; |
| 1008 | |
| 1009 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1010 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1011 | array_fromlist(self, args) |
| 1012 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1013 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1014 | { |
| 1015 | int n; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1016 | PyObject *list; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1017 | int itemsize = self->ob_descr->itemsize; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1018 | if (!PyArg_Parse(args, "O", &list)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1019 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1020 | if (!PyList_Check(list)) { |
| 1021 | PyErr_SetString(PyExc_TypeError, "arg must be list"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1022 | return NULL; |
| 1023 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1024 | n = PyList_Size(list); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1025 | if (n > 0) { |
| 1026 | char *item = self->ob_item; |
| 1027 | int i; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1028 | PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1029 | if (item == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1030 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1031 | return NULL; |
| 1032 | } |
| 1033 | self->ob_item = item; |
| 1034 | self->ob_size += n; |
| 1035 | for (i = 0; i < n; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1036 | PyObject *v = PyList_GetItem(list, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1037 | if ((*self->ob_descr->setitem)(self, |
| 1038 | self->ob_size - n + i, v) != 0) { |
| 1039 | self->ob_size -= n; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1040 | PyMem_RESIZE(item, char, |
| 1041 | self->ob_size * itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1042 | self->ob_item = item; |
| 1043 | return NULL; |
| 1044 | } |
| 1045 | } |
| 1046 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1047 | Py_INCREF(Py_None); |
| 1048 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1051 | static char fromlist_doc [] = |
| 1052 | "fromlist(list)\n\ |
| 1053 | \n\ |
| 1054 | Append items to array from list."; |
| 1055 | |
| 1056 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1057 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1058 | array_tolist(self, args) |
| 1059 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1060 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1061 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1062 | PyObject *list = PyList_New(self->ob_size); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1063 | int i; |
| 1064 | if (list == NULL) |
| 1065 | return NULL; |
| 1066 | for (i = 0; i < self->ob_size; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1067 | PyObject *v = getarrayitem((PyObject *)self, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1068 | if (v == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1069 | Py_DECREF(list); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1070 | return NULL; |
| 1071 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1072 | PyList_SetItem(list, i, v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1073 | } |
| 1074 | return list; |
| 1075 | } |
| 1076 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1077 | static char tolist_doc [] = |
Guido van Rossum | fc6aba5 | 1998-10-14 02:52:31 +0000 | [diff] [blame] | 1078 | "tolist() -> list\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1079 | \n\ |
| 1080 | Convert array to an ordinary list with the same items."; |
| 1081 | |
| 1082 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1083 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1084 | array_fromstring(self, args) |
| 1085 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1086 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1087 | { |
| 1088 | char *str; |
| 1089 | int n; |
| 1090 | int itemsize = self->ob_descr->itemsize; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1091 | if (!PyArg_Parse(args, "s#", &str, &n)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1092 | return NULL; |
| 1093 | if (n % itemsize != 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1094 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1095 | "string length not a multiple of item size"); |
| 1096 | return NULL; |
| 1097 | } |
| 1098 | n = n / itemsize; |
| 1099 | if (n > 0) { |
| 1100 | char *item = self->ob_item; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1101 | PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1102 | if (item == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1103 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1104 | return NULL; |
| 1105 | } |
| 1106 | self->ob_item = item; |
| 1107 | self->ob_size += n; |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1108 | memcpy(item + (self->ob_size - n) * itemsize, |
| 1109 | str, itemsize*n); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1110 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1111 | Py_INCREF(Py_None); |
| 1112 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1115 | static char fromstring_doc [] = |
| 1116 | "fromstring(string)\n\ |
| 1117 | \n\ |
| 1118 | Appends items from the string, interpreting it as an array of machine\n\ |
| 1119 | values,as if it had been read from a file using the fromfile() method)."; |
| 1120 | |
| 1121 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1122 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1123 | array_tostring(self, args) |
| 1124 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1125 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1126 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1127 | if (!PyArg_Parse(args, "")) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1128 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1129 | return PyString_FromStringAndSize(self->ob_item, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1130 | self->ob_size * self->ob_descr->itemsize); |
| 1131 | } |
| 1132 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1133 | static char tostring_doc [] = |
| 1134 | "tostring() -> string\n\ |
| 1135 | \n\ |
| 1136 | Convert the array to an array of machine values and return the string\n\ |
| 1137 | representation."; |
| 1138 | |
| 1139 | PyMethodDef array_methods[] = { |
| 1140 | {"append", (PyCFunction)array_append, 0, append_doc}, |
| 1141 | {"buffer_info", (PyCFunction)array_buffer_info, 0, buffer_info_doc}, |
| 1142 | {"byteswap", (PyCFunction)array_byteswap, 0, byteswap_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1143 | /* {"count", (method)array_count},*/ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1144 | {"fromfile", (PyCFunction)array_fromfile, 0, fromfile_doc}, |
| 1145 | {"fromlist", (PyCFunction)array_fromlist, 0, fromlist_doc}, |
| 1146 | {"fromstring", (PyCFunction)array_fromstring, 0, fromstring_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1147 | /* {"index", (method)array_index},*/ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1148 | {"insert", (PyCFunction)array_insert, 0, insert_doc}, |
| 1149 | {"read", (PyCFunction)array_fromfile, 0, fromfile_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1150 | /* {"remove", (method)array_remove},*/ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1151 | {"reverse", (PyCFunction)array_reverse, 0, reverse_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1152 | /* {"sort", (method)array_sort},*/ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1153 | {"tofile", (PyCFunction)array_tofile, 0, tofile_doc}, |
| 1154 | {"tolist", (PyCFunction)array_tolist, 0, tolist_doc}, |
| 1155 | {"tostring", (PyCFunction)array_tostring, 0, tostring_doc}, |
| 1156 | {"write", (PyCFunction)array_tofile, 0, tofile_doc}, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1157 | {NULL, NULL} /* sentinel */ |
| 1158 | }; |
| 1159 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1160 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1161 | array_getattr(a, name) |
| 1162 | arrayobject *a; |
| 1163 | char *name; |
| 1164 | { |
| 1165 | if (strcmp(name, "typecode") == 0) { |
| 1166 | char tc = a->ob_descr->typecode; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1167 | return PyString_FromStringAndSize(&tc, 1); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1168 | } |
| 1169 | if (strcmp(name, "itemsize") == 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1170 | return PyInt_FromLong((long)a->ob_descr->itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1171 | } |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1172 | if (strcmp(name, "__members__") == 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1173 | PyObject *list = PyList_New(2); |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1174 | if (list) { |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1175 | PyList_SetItem(list, 0, |
| 1176 | PyString_FromString("typecode")); |
| 1177 | PyList_SetItem(list, 1, |
| 1178 | PyString_FromString("itemsize")); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1179 | if (PyErr_Occurred()) { |
| 1180 | Py_DECREF(list); |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1181 | list = NULL; |
| 1182 | } |
| 1183 | } |
| 1184 | return list; |
| 1185 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1186 | return Py_FindMethod(array_methods, (PyObject *)a, name); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | static int |
| 1190 | array_print(a, fp, flags) |
| 1191 | arrayobject *a; |
| 1192 | FILE *fp; |
| 1193 | int flags; |
| 1194 | { |
| 1195 | int ok = 0; |
| 1196 | int i, len; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1197 | PyObject *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1198 | len = a->ob_size; |
| 1199 | if (len == 0) { |
| 1200 | fprintf(fp, "array('%c')", a->ob_descr->typecode); |
| 1201 | return ok; |
| 1202 | } |
| 1203 | if (a->ob_descr->typecode == 'c') { |
| 1204 | fprintf(fp, "array('c', "); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1205 | v = array_tostring(a, (PyObject *)NULL); |
| 1206 | ok = PyObject_Print(v, fp, 0); |
| 1207 | Py_XDECREF(v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1208 | fprintf(fp, ")"); |
| 1209 | return ok; |
| 1210 | } |
| 1211 | fprintf(fp, "array('%c', [", a->ob_descr->typecode); |
| 1212 | for (i = 0; i < len && ok == 0; i++) { |
| 1213 | if (i > 0) |
| 1214 | fprintf(fp, ", "); |
| 1215 | v = (a->ob_descr->getitem)(a, i); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1216 | ok = PyObject_Print(v, fp, 0); |
| 1217 | Py_XDECREF(v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1218 | } |
| 1219 | fprintf(fp, "])"); |
| 1220 | return ok; |
| 1221 | } |
| 1222 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1223 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1224 | array_repr(a) |
| 1225 | arrayobject *a; |
| 1226 | { |
| 1227 | char buf[256]; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1228 | PyObject *s, *t, *comma, *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1229 | int i, len; |
| 1230 | len = a->ob_size; |
| 1231 | if (len == 0) { |
| 1232 | sprintf(buf, "array('%c')", a->ob_descr->typecode); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1233 | return PyString_FromString(buf); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1234 | } |
| 1235 | if (a->ob_descr->typecode == 'c') { |
| 1236 | sprintf(buf, "array('c', "); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1237 | s = PyString_FromString(buf); |
| 1238 | v = array_tostring(a, (PyObject *)NULL); |
| 1239 | t = PyObject_Repr(v); |
| 1240 | Py_XDECREF(v); |
| 1241 | PyString_ConcatAndDel(&s, t); |
| 1242 | PyString_ConcatAndDel(&s, PyString_FromString(")")); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1243 | return s; |
| 1244 | } |
| 1245 | sprintf(buf, "array('%c', [", a->ob_descr->typecode); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1246 | s = PyString_FromString(buf); |
| 1247 | comma = PyString_FromString(", "); |
| 1248 | for (i = 0; i < len && !PyErr_Occurred(); i++) { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1249 | if (i > 0) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1250 | PyString_Concat(&s, comma); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1251 | v = (a->ob_descr->getitem)(a, i); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1252 | t = PyObject_Repr(v); |
| 1253 | Py_XDECREF(v); |
| 1254 | PyString_ConcatAndDel(&s, t); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1255 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1256 | Py_XDECREF(comma); |
| 1257 | PyString_ConcatAndDel(&s, PyString_FromString("])")); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1258 | return s; |
| 1259 | } |
| 1260 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1261 | static int |
| 1262 | array_buffer_getreadbuf(self, index, ptr) |
| 1263 | arrayobject *self; |
| 1264 | int index; |
| 1265 | const void **ptr; |
| 1266 | { |
| 1267 | if ( index != 0 ) { |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1268 | PyErr_SetString(PyExc_SystemError, |
| 1269 | "Accessing non-existent array segment"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1270 | return -1; |
| 1271 | } |
| 1272 | *ptr = (void *)self->ob_item; |
| 1273 | return self->ob_size*self->ob_descr->itemsize; |
| 1274 | } |
| 1275 | |
| 1276 | static int |
| 1277 | array_buffer_getwritebuf(self, index, ptr) |
| 1278 | arrayobject *self; |
| 1279 | int index; |
| 1280 | const void **ptr; |
| 1281 | { |
| 1282 | if ( index != 0 ) { |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1283 | PyErr_SetString(PyExc_SystemError, |
| 1284 | "Accessing non-existent array segment"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1285 | return -1; |
| 1286 | } |
| 1287 | *ptr = (void *)self->ob_item; |
| 1288 | return self->ob_size*self->ob_descr->itemsize; |
| 1289 | } |
| 1290 | |
| 1291 | static int |
| 1292 | array_buffer_getsegcount(self, lenp) |
| 1293 | arrayobject *self; |
| 1294 | int *lenp; |
| 1295 | { |
| 1296 | if ( lenp ) |
| 1297 | *lenp = self->ob_size*self->ob_descr->itemsize; |
| 1298 | return 1; |
| 1299 | } |
| 1300 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1301 | static PySequenceMethods array_as_sequence = { |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1302 | (inquiry)array_length, /*sq_length*/ |
| 1303 | (binaryfunc)array_concat, /*sq_concat*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1304 | (intargfunc)array_repeat, /*sq_repeat*/ |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1305 | (intargfunc)array_item, /*sq_item*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1306 | (intintargfunc)array_slice, /*sq_slice*/ |
| 1307 | (intobjargproc)array_ass_item, /*sq_ass_item*/ |
| 1308 | (intintobjargproc)array_ass_slice, /*sq_ass_slice*/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1309 | }; |
| 1310 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1311 | static PyBufferProcs array_as_buffer = { |
| 1312 | (getreadbufferproc)array_buffer_getreadbuf, |
| 1313 | (getwritebufferproc)array_buffer_getwritebuf, |
| 1314 | (getsegcountproc)array_buffer_getsegcount, |
| 1315 | }; |
| 1316 | |
| 1317 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1318 | |
| 1319 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1320 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1321 | a_array(self, args) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1322 | PyObject *self; |
| 1323 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1324 | { |
| 1325 | char c; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1326 | PyObject *initial = NULL; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1327 | struct arraydescr *descr; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1328 | if (!PyArg_Parse(args, "c", &c)) { |
| 1329 | PyErr_Clear(); |
| 1330 | if (!PyArg_Parse(args, "(cO)", &c, &initial)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1331 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1332 | if (!PyList_Check(initial) && !PyString_Check(initial)) { |
| 1333 | PyErr_SetString(PyExc_TypeError, |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1334 | "array initializer must be list or string"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1335 | return NULL; |
| 1336 | } |
| 1337 | } |
| 1338 | for (descr = descriptors; descr->typecode != '\0'; descr++) { |
| 1339 | if (descr->typecode == c) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1340 | PyObject *a; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1341 | int len; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1342 | if (initial == NULL || !PyList_Check(initial)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1343 | len = 0; |
| 1344 | else |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1345 | len = PyList_Size(initial); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1346 | a = newarrayobject(len, descr); |
| 1347 | if (a == NULL) |
| 1348 | return NULL; |
| 1349 | if (len > 0) { |
| 1350 | int i; |
| 1351 | for (i = 0; i < len; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1352 | PyObject *v = |
| 1353 | PyList_GetItem(initial, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1354 | if (setarrayitem(a, i, v) != 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1355 | Py_DECREF(a); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1356 | return NULL; |
| 1357 | } |
| 1358 | } |
| 1359 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1360 | if (initial != NULL && PyString_Check(initial)) { |
| 1361 | PyObject *v = |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1362 | array_fromstring((arrayobject *)a, initial); |
| 1363 | if (v == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1364 | Py_DECREF(a); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1365 | return NULL; |
| 1366 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1367 | Py_DECREF(v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1368 | } |
| 1369 | return a; |
| 1370 | } |
| 1371 | } |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1372 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 1373 | "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] | 1374 | return NULL; |
| 1375 | } |
| 1376 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1377 | static char a_array_doc [] = |
| 1378 | "array(typecode [, initializer]) -> array\n\ |
| 1379 | \n\ |
| 1380 | Return a new array whose items are restricted by typecode, and\n\ |
| 1381 | initialized from the optional initializer value, which must be a list\n\ |
| 1382 | or a string."; |
| 1383 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1384 | static PyMethodDef a_methods[] = { |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1385 | {"array", a_array, 0, a_array_doc}, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1386 | {NULL, NULL} /* sentinel */ |
| 1387 | }; |
| 1388 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1389 | static char module_doc [] = |
| 1390 | "This module defines a new object type which can efficiently represent\n\ |
| 1391 | an array of basic values: characters, integers, floating point\n\ |
| 1392 | numbers. Arrays are sequence types and behave very much like lists,\n\ |
| 1393 | except that the type of objects stored in them is constrained. The\n\ |
| 1394 | type is specified at object creation time by using a type code, which\n\ |
| 1395 | is a single character. The following type codes are defined:\n\ |
| 1396 | \n\ |
| 1397 | Type code C Type Minimum size in bytes \n\ |
| 1398 | 'c' character 1 \n\ |
| 1399 | 'b' signed integer 1 \n\ |
| 1400 | 'B' unsigned integer 1 \n\ |
| 1401 | 'h' signed integer 2 \n\ |
| 1402 | 'H' unsigned integer 2 \n\ |
| 1403 | 'i' signed integer 2 \n\ |
| 1404 | 'I' unsigned integer 2 \n\ |
| 1405 | 'l' signed integer 4 \n\ |
| 1406 | 'L' unsigned integer 4 \n\ |
| 1407 | 'f' floating point 4 \n\ |
| 1408 | 'd' floating point 8 \n\ |
| 1409 | \n\ |
| 1410 | Functions:\n\ |
| 1411 | \n\ |
| 1412 | array(typecode [, initializer]) -- create a new array\n\ |
| 1413 | \n\ |
| 1414 | Special Objects:\n\ |
| 1415 | \n\ |
| 1416 | ArrayType -- type object for array objects\n\ |
| 1417 | "; |
| 1418 | |
| 1419 | static char arraytype_doc [] = |
| 1420 | "An array represents basic values and behave very much like lists, except\n\ |
| 1421 | the type of objects stored in them is constrained.\n\ |
| 1422 | \n\ |
| 1423 | Methods:\n\ |
| 1424 | \n\ |
| 1425 | append() -- append a new item to the end of the array\n\ |
| 1426 | buffer_info() -- return information giving the current memory info\n\ |
| 1427 | byteswap() -- byteswap all the items of the array\n\ |
| 1428 | fromfile() -- read items from a file object\n\ |
| 1429 | fromlist() -- append items from the list\n\ |
| 1430 | fromstring() -- append items from the string\n\ |
| 1431 | insert() -- insert a new item into the array at a provided position\n\ |
| 1432 | read() -- DEPRECATED, use fromfile()\n\ |
| 1433 | reverse() -- reverse the order of the items in the array\n\ |
| 1434 | tofile() -- write all items to a file object\n\ |
| 1435 | tolist() -- return the array converted to an ordinary list\n\ |
| 1436 | tostring() -- return the array converted to a string\n\ |
| 1437 | write() -- DEPRECATED, use tofile()\n\ |
| 1438 | \n\ |
| 1439 | Variables:\n\ |
| 1440 | \n\ |
| 1441 | typecode -- the typecode character used to create the array\n\ |
| 1442 | itemsize -- the length in bytes of one array item\n\ |
| 1443 | "; |
| 1444 | |
| 1445 | statichere PyTypeObject Arraytype = { |
| 1446 | PyObject_HEAD_INIT(&PyType_Type) |
| 1447 | 0, |
| 1448 | "array", |
| 1449 | sizeof(arrayobject), |
| 1450 | 0, |
| 1451 | (destructor)array_dealloc, /*tp_dealloc*/ |
| 1452 | (printfunc)array_print, /*tp_print*/ |
| 1453 | (getattrfunc)array_getattr, /*tp_getattr*/ |
| 1454 | 0, /*tp_setattr*/ |
| 1455 | (cmpfunc)array_compare, /*tp_compare*/ |
| 1456 | (reprfunc)array_repr, /*tp_repr*/ |
| 1457 | 0, /*tp_as_number*/ |
| 1458 | &array_as_sequence, /*tp_as_sequence*/ |
| 1459 | 0, /*tp_as_mapping*/ |
| 1460 | 0, /*tp_hash*/ |
| 1461 | 0, /*tp_call*/ |
| 1462 | 0, /*tp_str*/ |
| 1463 | 0, /*tp_getattro*/ |
| 1464 | 0, /*tp_setattro*/ |
| 1465 | &array_as_buffer, /*tp_as_buffer*/ |
| 1466 | 0, /*tp_xxx4*/ |
| 1467 | arraytype_doc, /*tp_doc*/ |
| 1468 | }; |
| 1469 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 1470 | DL_EXPORT(void) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1471 | initarray() |
| 1472 | { |
Guido van Rossum | b6190d3 | 1997-05-22 14:56:36 +0000 | [diff] [blame] | 1473 | PyObject *m, *d; |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1474 | m = Py_InitModule3("array", a_methods, module_doc); |
Guido van Rossum | b6190d3 | 1997-05-22 14:56:36 +0000 | [diff] [blame] | 1475 | d = PyModule_GetDict(m); |
Guido van Rossum | a0deb64 | 1998-10-14 13:45:06 +0000 | [diff] [blame] | 1476 | PyDict_SetItemString(d, "ArrayType", (PyObject *)&Arraytype); |
| 1477 | /* 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] | 1478 | } |