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