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