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