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 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 714 | static PyObject * |
Guido van Rossum | de4a4ca | 1997-08-12 14:55:56 +0000 | [diff] [blame] | 715 | array_buffer_info(self, args) |
| 716 | arrayobject *self; |
| 717 | PyObject *args; |
| 718 | { |
| 719 | return Py_BuildValue("ll", |
| 720 | (long)(self->ob_item), (long)(self->ob_size)); |
| 721 | } |
| 722 | |
| 723 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 724 | array_append(self, args) |
| 725 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 726 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 727 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 728 | PyObject *v; |
| 729 | if (!PyArg_Parse(args, "O", &v)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 730 | return NULL; |
| 731 | return ins(self, (int) self->ob_size, v); |
| 732 | } |
| 733 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 734 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 735 | array_byteswap(self, args) |
| 736 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 737 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 738 | { |
| 739 | char *p; |
| 740 | int i; |
| 741 | switch (self->ob_descr->itemsize) { |
| 742 | case 1: |
| 743 | break; |
| 744 | case 2: |
| 745 | for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 2) { |
| 746 | char p0 = p[0]; |
| 747 | p[0] = p[1]; |
| 748 | p[1] = p0; |
| 749 | } |
| 750 | break; |
| 751 | case 4: |
| 752 | for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 4) { |
| 753 | char p0 = p[0]; |
| 754 | char p1 = p[1]; |
| 755 | p[0] = p[3]; |
| 756 | p[1] = p[2]; |
| 757 | p[2] = p1; |
| 758 | p[3] = p0; |
| 759 | } |
| 760 | break; |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 761 | case 8: |
| 762 | for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 8) { |
| 763 | char p0 = p[0]; |
| 764 | char p1 = p[1]; |
| 765 | char p2 = p[2]; |
| 766 | char p3 = p[3]; |
| 767 | p[0] = p[7]; |
| 768 | p[1] = p[6]; |
| 769 | p[2] = p[5]; |
| 770 | p[3] = p[4]; |
| 771 | p[4] = p3; |
| 772 | p[5] = p2; |
| 773 | p[6] = p1; |
| 774 | p[7] = p0; |
| 775 | } |
| 776 | break; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 777 | default: |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 778 | PyErr_SetString(PyExc_RuntimeError, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 779 | "don't know how to byteswap this array type"); |
| 780 | return NULL; |
| 781 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 782 | Py_INCREF(Py_None); |
| 783 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 786 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 787 | array_reverse(self, args) |
| 788 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 789 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 790 | { |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 791 | register int itemsize = self->ob_descr->itemsize; |
| 792 | register char *p, *q; |
| 793 | char tmp[sizeof(double)]; /* Assume that's the max item size */ |
| 794 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 795 | if (args != NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 796 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 797 | return NULL; |
| 798 | } |
| 799 | |
| 800 | if (self->ob_size > 1) { |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 801 | for (p = self->ob_item, |
| 802 | q = self->ob_item + (self->ob_size - 1)*itemsize; |
| 803 | p < q; |
| 804 | p += itemsize, q -= itemsize) { |
| 805 | memmove(tmp, p, itemsize); |
| 806 | memmove(p, q, itemsize); |
| 807 | memmove(q, tmp, itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 811 | Py_INCREF(Py_None); |
| 812 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 813 | } |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 814 | |
| 815 | /* The following routines were adapted from listobject.c but not converted. |
| 816 | To make them work you will have to work! */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 817 | |
| 818 | #if 0 |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 819 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 820 | array_index(self, args) |
| 821 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 822 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 823 | { |
| 824 | int i; |
| 825 | |
| 826 | if (args == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 827 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 828 | return NULL; |
| 829 | } |
| 830 | for (i = 0; i < self->ob_size; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 831 | if (PyObject_Compare(self->ob_item[i], args) == 0) |
| 832 | return PyInt_FromLong((long)i); |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 833 | /* XXX PyErr_Occurred */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 834 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 835 | PyErr_SetString(PyExc_ValueError, "array.index(x): x not in array"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 836 | return NULL; |
| 837 | } |
| 838 | #endif |
| 839 | |
| 840 | #if 0 |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 841 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 842 | array_count(self, args) |
| 843 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 844 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 845 | { |
| 846 | int count = 0; |
| 847 | int i; |
| 848 | |
| 849 | if (args == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 850 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 851 | return NULL; |
| 852 | } |
| 853 | for (i = 0; i < self->ob_size; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 854 | if (PyObject_Compare(self->ob_item[i], args) == 0) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 855 | count++; |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 856 | /* XXX PyErr_Occurred */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 857 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 858 | return PyInt_FromLong((long)count); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 859 | } |
| 860 | #endif |
| 861 | |
| 862 | #if 0 |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 863 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 864 | array_remove(self, args) |
| 865 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 866 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 867 | { |
| 868 | int i; |
| 869 | |
| 870 | if (args == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 871 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 872 | return NULL; |
| 873 | } |
| 874 | for (i = 0; i < self->ob_size; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 875 | if (PyObject_Compare(self->ob_item[i], args) == 0) { |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 876 | if (array_ass_slice(self, i, i+1, |
| 877 | (PyObject *)NULL) != 0) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 878 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 879 | Py_INCREF(Py_None); |
| 880 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 881 | } |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 882 | /* XXX PyErr_Occurred */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 883 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 884 | PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in array"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 885 | return NULL; |
| 886 | } |
| 887 | #endif |
| 888 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 889 | static PyObject * |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 890 | array_fromfile(self, args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 891 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 892 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 893 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 894 | PyObject *f; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 895 | int n; |
| 896 | FILE *fp; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 897 | if (!PyArg_Parse(args, "(Oi)", &f, &n)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 898 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 899 | fp = PyFile_AsFile(f); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 900 | if (fp == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 901 | PyErr_SetString(PyExc_TypeError, "arg1 must be open file"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 902 | return NULL; |
| 903 | } |
| 904 | if (n > 0) { |
| 905 | char *item = self->ob_item; |
| 906 | int itemsize = self->ob_descr->itemsize; |
| 907 | int nread; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 908 | PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 909 | if (item == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 910 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 911 | return NULL; |
| 912 | } |
| 913 | self->ob_item = item; |
| 914 | self->ob_size += n; |
| 915 | nread = fread(item + (self->ob_size - n) * itemsize, |
| 916 | itemsize, n, fp); |
| 917 | if (nread < n) { |
| 918 | self->ob_size -= (n - nread); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 919 | PyMem_RESIZE(item, char, self->ob_size*itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 920 | self->ob_item = item; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 921 | PyErr_SetString(PyExc_EOFError, |
| 922 | "not enough items in file"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 923 | return NULL; |
| 924 | } |
| 925 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 926 | Py_INCREF(Py_None); |
| 927 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 930 | static PyObject * |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 931 | array_tofile(self, args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 932 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 933 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 934 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 935 | PyObject *f; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 936 | FILE *fp; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 937 | if (!PyArg_Parse(args, "O", &f)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 938 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 939 | fp = PyFile_AsFile(f); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 940 | if (fp == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 941 | PyErr_SetString(PyExc_TypeError, "arg must be open file"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 942 | return NULL; |
| 943 | } |
| 944 | if (self->ob_size > 0) { |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 945 | if ((int)fwrite(self->ob_item, self->ob_descr->itemsize, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 946 | self->ob_size, fp) != self->ob_size) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 947 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 948 | clearerr(fp); |
| 949 | return NULL; |
| 950 | } |
| 951 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 952 | Py_INCREF(Py_None); |
| 953 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 956 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 957 | array_fromlist(self, args) |
| 958 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 959 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 960 | { |
| 961 | int n; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 962 | PyObject *list; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 963 | int itemsize = self->ob_descr->itemsize; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 964 | if (!PyArg_Parse(args, "O", &list)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 965 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 966 | if (!PyList_Check(list)) { |
| 967 | PyErr_SetString(PyExc_TypeError, "arg must be list"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 968 | return NULL; |
| 969 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 970 | n = PyList_Size(list); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 971 | if (n > 0) { |
| 972 | char *item = self->ob_item; |
| 973 | int i; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 974 | PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 975 | if (item == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 976 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 977 | return NULL; |
| 978 | } |
| 979 | self->ob_item = item; |
| 980 | self->ob_size += n; |
| 981 | for (i = 0; i < n; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 982 | PyObject *v = PyList_GetItem(list, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 983 | if ((*self->ob_descr->setitem)(self, |
| 984 | self->ob_size - n + i, v) != 0) { |
| 985 | self->ob_size -= n; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 986 | PyMem_RESIZE(item, char, |
| 987 | self->ob_size * itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 988 | self->ob_item = item; |
| 989 | return NULL; |
| 990 | } |
| 991 | } |
| 992 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 993 | Py_INCREF(Py_None); |
| 994 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 995 | } |
| 996 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 997 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 998 | array_tolist(self, args) |
| 999 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1000 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1001 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1002 | PyObject *list = PyList_New(self->ob_size); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1003 | int i; |
| 1004 | if (list == NULL) |
| 1005 | return NULL; |
| 1006 | for (i = 0; i < self->ob_size; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1007 | PyObject *v = getarrayitem((PyObject *)self, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1008 | if (v == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1009 | Py_DECREF(list); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1010 | return NULL; |
| 1011 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1012 | PyList_SetItem(list, i, v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1013 | } |
| 1014 | return list; |
| 1015 | } |
| 1016 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1017 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1018 | array_fromstring(self, args) |
| 1019 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1020 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1021 | { |
| 1022 | char *str; |
| 1023 | int n; |
| 1024 | int itemsize = self->ob_descr->itemsize; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1025 | if (!PyArg_Parse(args, "s#", &str, &n)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1026 | return NULL; |
| 1027 | if (n % itemsize != 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1028 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1029 | "string length not a multiple of item size"); |
| 1030 | return NULL; |
| 1031 | } |
| 1032 | n = n / itemsize; |
| 1033 | if (n > 0) { |
| 1034 | char *item = self->ob_item; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1035 | PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1036 | if (item == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1037 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1038 | return NULL; |
| 1039 | } |
| 1040 | self->ob_item = item; |
| 1041 | self->ob_size += n; |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1042 | memcpy(item + (self->ob_size - n) * itemsize, |
| 1043 | str, itemsize*n); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1044 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1045 | Py_INCREF(Py_None); |
| 1046 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1049 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1050 | array_tostring(self, args) |
| 1051 | arrayobject *self; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1052 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1053 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1054 | if (!PyArg_Parse(args, "")) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1055 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1056 | return PyString_FromStringAndSize(self->ob_item, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1057 | self->ob_size * self->ob_descr->itemsize); |
| 1058 | } |
| 1059 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1060 | static PyMethodDef array_methods[] = { |
| 1061 | {"append", (PyCFunction)array_append}, |
Guido van Rossum | de4a4ca | 1997-08-12 14:55:56 +0000 | [diff] [blame] | 1062 | {"buffer_info", (PyCFunction)array_buffer_info}, |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1063 | {"byteswap", (PyCFunction)array_byteswap}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1064 | /* {"count", (method)array_count},*/ |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1065 | {"fromfile", (PyCFunction)array_fromfile}, |
| 1066 | {"fromlist", (PyCFunction)array_fromlist}, |
| 1067 | {"fromstring", (PyCFunction)array_fromstring}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1068 | /* {"index", (method)array_index},*/ |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1069 | {"insert", (PyCFunction)array_insert}, |
| 1070 | {"read", (PyCFunction)array_fromfile}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1071 | /* {"remove", (method)array_remove},*/ |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1072 | {"reverse", (PyCFunction)array_reverse}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1073 | /* {"sort", (method)array_sort},*/ |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1074 | {"tofile", (PyCFunction)array_tofile}, |
| 1075 | {"tolist", (PyCFunction)array_tolist}, |
| 1076 | {"tostring", (PyCFunction)array_tostring}, |
| 1077 | {"write", (PyCFunction)array_tofile}, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1078 | {NULL, NULL} /* sentinel */ |
| 1079 | }; |
| 1080 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1081 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1082 | array_getattr(a, name) |
| 1083 | arrayobject *a; |
| 1084 | char *name; |
| 1085 | { |
| 1086 | if (strcmp(name, "typecode") == 0) { |
| 1087 | char tc = a->ob_descr->typecode; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1088 | return PyString_FromStringAndSize(&tc, 1); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1089 | } |
| 1090 | if (strcmp(name, "itemsize") == 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1091 | return PyInt_FromLong((long)a->ob_descr->itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1092 | } |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1093 | if (strcmp(name, "__members__") == 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1094 | PyObject *list = PyList_New(2); |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1095 | if (list) { |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1096 | PyList_SetItem(list, 0, |
| 1097 | PyString_FromString("typecode")); |
| 1098 | PyList_SetItem(list, 1, |
| 1099 | PyString_FromString("itemsize")); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1100 | if (PyErr_Occurred()) { |
| 1101 | Py_DECREF(list); |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 1102 | list = NULL; |
| 1103 | } |
| 1104 | } |
| 1105 | return list; |
| 1106 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1107 | return Py_FindMethod(array_methods, (PyObject *)a, name); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | static int |
| 1111 | array_print(a, fp, flags) |
| 1112 | arrayobject *a; |
| 1113 | FILE *fp; |
| 1114 | int flags; |
| 1115 | { |
| 1116 | int ok = 0; |
| 1117 | int i, len; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1118 | PyObject *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1119 | len = a->ob_size; |
| 1120 | if (len == 0) { |
| 1121 | fprintf(fp, "array('%c')", a->ob_descr->typecode); |
| 1122 | return ok; |
| 1123 | } |
| 1124 | if (a->ob_descr->typecode == 'c') { |
| 1125 | fprintf(fp, "array('c', "); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1126 | v = array_tostring(a, (PyObject *)NULL); |
| 1127 | ok = PyObject_Print(v, fp, 0); |
| 1128 | Py_XDECREF(v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1129 | fprintf(fp, ")"); |
| 1130 | return ok; |
| 1131 | } |
| 1132 | fprintf(fp, "array('%c', [", a->ob_descr->typecode); |
| 1133 | for (i = 0; i < len && ok == 0; i++) { |
| 1134 | if (i > 0) |
| 1135 | fprintf(fp, ", "); |
| 1136 | v = (a->ob_descr->getitem)(a, i); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1137 | ok = PyObject_Print(v, fp, 0); |
| 1138 | Py_XDECREF(v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1139 | } |
| 1140 | fprintf(fp, "])"); |
| 1141 | return ok; |
| 1142 | } |
| 1143 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1144 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1145 | array_repr(a) |
| 1146 | arrayobject *a; |
| 1147 | { |
| 1148 | char buf[256]; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1149 | PyObject *s, *t, *comma, *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1150 | int i, len; |
| 1151 | len = a->ob_size; |
| 1152 | if (len == 0) { |
| 1153 | sprintf(buf, "array('%c')", a->ob_descr->typecode); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1154 | return PyString_FromString(buf); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1155 | } |
| 1156 | if (a->ob_descr->typecode == 'c') { |
| 1157 | sprintf(buf, "array('c', "); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1158 | s = PyString_FromString(buf); |
| 1159 | v = array_tostring(a, (PyObject *)NULL); |
| 1160 | t = PyObject_Repr(v); |
| 1161 | Py_XDECREF(v); |
| 1162 | PyString_ConcatAndDel(&s, t); |
| 1163 | PyString_ConcatAndDel(&s, PyString_FromString(")")); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1164 | return s; |
| 1165 | } |
| 1166 | sprintf(buf, "array('%c', [", a->ob_descr->typecode); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1167 | s = PyString_FromString(buf); |
| 1168 | comma = PyString_FromString(", "); |
| 1169 | for (i = 0; i < len && !PyErr_Occurred(); i++) { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1170 | if (i > 0) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1171 | PyString_Concat(&s, comma); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1172 | v = (a->ob_descr->getitem)(a, i); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1173 | t = PyObject_Repr(v); |
| 1174 | Py_XDECREF(v); |
| 1175 | PyString_ConcatAndDel(&s, t); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1176 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1177 | Py_XDECREF(comma); |
| 1178 | PyString_ConcatAndDel(&s, PyString_FromString("])")); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1179 | return s; |
| 1180 | } |
| 1181 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1182 | static int |
| 1183 | array_buffer_getreadbuf(self, index, ptr) |
| 1184 | arrayobject *self; |
| 1185 | int index; |
| 1186 | const void **ptr; |
| 1187 | { |
| 1188 | if ( index != 0 ) { |
| 1189 | PyErr_SetString(PyExc_SystemError, "Accessing non-existent array segment"); |
| 1190 | return -1; |
| 1191 | } |
| 1192 | *ptr = (void *)self->ob_item; |
| 1193 | return self->ob_size*self->ob_descr->itemsize; |
| 1194 | } |
| 1195 | |
| 1196 | static int |
| 1197 | array_buffer_getwritebuf(self, index, ptr) |
| 1198 | arrayobject *self; |
| 1199 | int index; |
| 1200 | const void **ptr; |
| 1201 | { |
| 1202 | if ( index != 0 ) { |
| 1203 | PyErr_SetString(PyExc_SystemError, "Accessing non-existent array segment"); |
| 1204 | return -1; |
| 1205 | } |
| 1206 | *ptr = (void *)self->ob_item; |
| 1207 | return self->ob_size*self->ob_descr->itemsize; |
| 1208 | } |
| 1209 | |
| 1210 | static int |
| 1211 | array_buffer_getsegcount(self, lenp) |
| 1212 | arrayobject *self; |
| 1213 | int *lenp; |
| 1214 | { |
| 1215 | if ( lenp ) |
| 1216 | *lenp = self->ob_size*self->ob_descr->itemsize; |
| 1217 | return 1; |
| 1218 | } |
| 1219 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1220 | static PySequenceMethods array_as_sequence = { |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1221 | (inquiry)array_length, /*sq_length*/ |
| 1222 | (binaryfunc)array_concat, /*sq_concat*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1223 | (intargfunc)array_repeat, /*sq_repeat*/ |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1224 | (intargfunc)array_item, /*sq_item*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1225 | (intintargfunc)array_slice, /*sq_slice*/ |
| 1226 | (intobjargproc)array_ass_item, /*sq_ass_item*/ |
| 1227 | (intintobjargproc)array_ass_slice, /*sq_ass_slice*/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1228 | }; |
| 1229 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1230 | static PyBufferProcs array_as_buffer = { |
| 1231 | (getreadbufferproc)array_buffer_getreadbuf, |
| 1232 | (getwritebufferproc)array_buffer_getwritebuf, |
| 1233 | (getsegcountproc)array_buffer_getsegcount, |
| 1234 | }; |
| 1235 | |
| 1236 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1237 | statichere PyTypeObject Arraytype = { |
| 1238 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1239 | 0, |
| 1240 | "array", |
| 1241 | sizeof(arrayobject), |
| 1242 | 0, |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1243 | (destructor)array_dealloc, /*tp_dealloc*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1244 | (printfunc)array_print, /*tp_print*/ |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1245 | (getattrfunc)array_getattr, /*tp_getattr*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1246 | 0, /*tp_setattr*/ |
| 1247 | (cmpfunc)array_compare, /*tp_compare*/ |
| 1248 | (reprfunc)array_repr, /*tp_repr*/ |
| 1249 | 0, /*tp_as_number*/ |
| 1250 | &array_as_sequence, /*tp_as_sequence*/ |
| 1251 | 0, /*tp_as_mapping*/ |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1252 | 0, /*tp_hash*/ |
| 1253 | 0, /*tp_call*/ |
| 1254 | 0, /*tp_str*/ |
| 1255 | 0, /*tp_getattro*/ |
| 1256 | 0, /*tp_setattro*/ |
| 1257 | &array_as_buffer, /*tp_as_buffer*/ |
| 1258 | 0, /*tp_xxx4*/ |
| 1259 | 0, /*tp_doc*/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1260 | }; |
| 1261 | |
| 1262 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1263 | static PyObject * |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1264 | a_array(self, args) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1265 | PyObject *self; |
| 1266 | PyObject *args; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1267 | { |
| 1268 | char c; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1269 | PyObject *initial = NULL; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1270 | struct arraydescr *descr; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1271 | if (!PyArg_Parse(args, "c", &c)) { |
| 1272 | PyErr_Clear(); |
| 1273 | if (!PyArg_Parse(args, "(cO)", &c, &initial)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1274 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1275 | if (!PyList_Check(initial) && !PyString_Check(initial)) { |
| 1276 | PyErr_SetString(PyExc_TypeError, |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1277 | "array initializer must be list or string"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1278 | return NULL; |
| 1279 | } |
| 1280 | } |
| 1281 | for (descr = descriptors; descr->typecode != '\0'; descr++) { |
| 1282 | if (descr->typecode == c) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1283 | PyObject *a; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1284 | int len; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1285 | if (initial == NULL || !PyList_Check(initial)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1286 | len = 0; |
| 1287 | else |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1288 | len = PyList_Size(initial); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1289 | a = newarrayobject(len, descr); |
| 1290 | if (a == NULL) |
| 1291 | return NULL; |
| 1292 | if (len > 0) { |
| 1293 | int i; |
| 1294 | for (i = 0; i < len; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1295 | PyObject *v = |
| 1296 | PyList_GetItem(initial, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1297 | if (setarrayitem(a, i, v) != 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1298 | Py_DECREF(a); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1299 | return NULL; |
| 1300 | } |
| 1301 | } |
| 1302 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1303 | if (initial != NULL && PyString_Check(initial)) { |
| 1304 | PyObject *v = |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1305 | array_fromstring((arrayobject *)a, initial); |
| 1306 | if (v == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1307 | Py_DECREF(a); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1308 | return NULL; |
| 1309 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1310 | Py_DECREF(v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1311 | } |
| 1312 | return a; |
| 1313 | } |
| 1314 | } |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1315 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 1316 | "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] | 1317 | return NULL; |
| 1318 | } |
| 1319 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1320 | static PyMethodDef a_methods[] = { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1321 | {"array", a_array}, |
| 1322 | {NULL, NULL} /* sentinel */ |
| 1323 | }; |
| 1324 | |
| 1325 | void |
| 1326 | initarray() |
| 1327 | { |
Guido van Rossum | b6190d3 | 1997-05-22 14:56:36 +0000 | [diff] [blame] | 1328 | PyObject *m, *d; |
| 1329 | m = Py_InitModule("array", a_methods); |
| 1330 | d = PyModule_GetDict(m); |
Guido van Rossum | 24995b9 | 1997-06-02 22:17:49 +0000 | [diff] [blame] | 1331 | if (PyDict_SetItemString(d, "ArrayType", |
Guido van Rossum | b6190d3 | 1997-05-22 14:56:36 +0000 | [diff] [blame] | 1332 | (PyObject *)&Arraytype) != 0) |
Guido van Rossum | 24995b9 | 1997-06-02 22:17:49 +0000 | [diff] [blame] | 1333 | Py_FatalError("can't define array.ArrayType"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1334 | } |