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 | |
Martin v. Löwis | b9132a2 | 2002-05-02 20:34:12 +0000 | [diff] [blame] | 16 | #define DELAYED(X) 0 |
| 17 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 18 | struct arrayobject; /* Forward */ |
| 19 | |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 20 | /* All possible arraydescr values are defined in the vector "descriptors" |
| 21 | * below. That's defined later because the appropriate get and set |
| 22 | * functions aren't visible yet. |
| 23 | */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 24 | struct arraydescr { |
| 25 | int typecode; |
| 26 | int itemsize; |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 27 | PyObject * (*getitem)(struct arrayobject *, int); |
| 28 | int (*setitem)(struct arrayobject *, int, PyObject *); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | typedef struct arrayobject { |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 32 | PyObject_HEAD |
| 33 | int ob_size; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 34 | char *ob_item; |
| 35 | struct arraydescr *ob_descr; |
| 36 | } arrayobject; |
| 37 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 38 | staticforward PyTypeObject Arraytype; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 39 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 40 | #define array_Check(op) PyObject_TypeCheck(op, &Arraytype) |
| 41 | #define array_CheckExact(op) ((op)->ob_type == &Arraytype) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 42 | |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 43 | /**************************************************************************** |
| 44 | Get and Set functions for each type. |
| 45 | A Get function takes an arrayobject* and an integer index, returning the |
| 46 | array value at that index wrapped in an appropriate PyObject*. |
| 47 | A Set function takes an arrayobject, integer index, and PyObject*; sets |
| 48 | the array value at that index to the raw C data extracted from the PyObject*, |
| 49 | and returns 0 if successful, else nonzero on failure (PyObject* not of an |
| 50 | appropriate type or value). |
| 51 | Note that the basic Get and Set functions do NOT check that the index is |
| 52 | in bounds; that's the responsibility of the caller. |
| 53 | ****************************************************************************/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 54 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 55 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 56 | c_getitem(arrayobject *ap, int i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 57 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 58 | return PyString_FromStringAndSize(&((char *)ap->ob_item)[i], 1); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 62 | c_setitem(arrayobject *ap, int i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 63 | { |
| 64 | char x; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 65 | if (!PyArg_Parse(v, "c;array item must be char", &x)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 66 | return -1; |
| 67 | if (i >= 0) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 68 | ((char *)ap->ob_item)[i] = x; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 69 | return 0; |
| 70 | } |
| 71 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 72 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 73 | b_getitem(arrayobject *ap, int i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 74 | { |
| 75 | long x = ((char *)ap->ob_item)[i]; |
| 76 | if (x >= 128) |
| 77 | x -= 256; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 78 | return PyInt_FromLong(x); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 82 | b_setitem(arrayobject *ap, int i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 83 | { |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 84 | short x; |
| 85 | /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore |
| 86 | must use the next size up that is signed ('h') and manually do |
| 87 | the overflow checking */ |
| 88 | if (!PyArg_Parse(v, "h;array item must be integer", &x)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 89 | return -1; |
Guido van Rossum | 9f754e0 | 2000-07-01 00:38:19 +0000 | [diff] [blame] | 90 | else if (x < -128) { |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 91 | PyErr_SetString(PyExc_OverflowError, |
| 92 | "signed char is less than minimum"); |
| 93 | return -1; |
| 94 | } |
Guido van Rossum | 9f754e0 | 2000-07-01 00:38:19 +0000 | [diff] [blame] | 95 | else if (x > 127) { |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 96 | PyErr_SetString(PyExc_OverflowError, |
| 97 | "signed char is greater than maximum"); |
| 98 | return -1; |
| 99 | } |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 100 | if (i >= 0) |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 101 | ((char *)ap->ob_item)[i] = (char)x; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 105 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 106 | BB_getitem(arrayobject *ap, int i) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 107 | { |
| 108 | long x = ((unsigned char *)ap->ob_item)[i]; |
| 109 | return PyInt_FromLong(x); |
| 110 | } |
| 111 | |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 112 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 113 | BB_setitem(arrayobject *ap, int i, PyObject *v) |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 114 | { |
| 115 | unsigned char x; |
| 116 | /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */ |
| 117 | if (!PyArg_Parse(v, "b;array item must be integer", &x)) |
| 118 | return -1; |
| 119 | if (i >= 0) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 120 | ((char *)ap->ob_item)[i] = x; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 121 | return 0; |
| 122 | } |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 123 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 124 | #ifdef Py_USING_UNICODE |
| 125 | static PyObject * |
| 126 | u_getitem(arrayobject *ap, int i) |
| 127 | { |
| 128 | return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1); |
| 129 | } |
| 130 | |
| 131 | static int |
| 132 | u_setitem(arrayobject *ap, int i, PyObject *v) |
| 133 | { |
| 134 | Py_UNICODE *p; |
| 135 | int len; |
| 136 | |
| 137 | if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len)) |
| 138 | return -1; |
| 139 | if (len != 1) { |
| 140 | PyErr_SetString(PyExc_TypeError, "array item must be unicode character"); |
| 141 | return -1; |
| 142 | } |
| 143 | if (i >= 0) |
| 144 | ((Py_UNICODE *)ap->ob_item)[i] = p[0]; |
| 145 | return 0; |
| 146 | } |
| 147 | #endif |
| 148 | |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 149 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 150 | h_getitem(arrayobject *ap, int i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 151 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 152 | return PyInt_FromLong((long) ((short *)ap->ob_item)[i]); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 156 | h_setitem(arrayobject *ap, int i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 157 | { |
| 158 | short x; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 159 | /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */ |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 160 | if (!PyArg_Parse(v, "h;array item must be integer", &x)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 161 | return -1; |
| 162 | if (i >= 0) |
| 163 | ((short *)ap->ob_item)[i] = x; |
| 164 | return 0; |
| 165 | } |
| 166 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 167 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 168 | HH_getitem(arrayobject *ap, int i) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 169 | { |
| 170 | return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[i]); |
| 171 | } |
| 172 | |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 173 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 174 | HH_setitem(arrayobject *ap, int i, PyObject *v) |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 175 | { |
| 176 | int x; |
| 177 | /* PyArg_Parse's 'h' formatter is for a signed short, therefore |
| 178 | must use the next size up and manually do the overflow checking */ |
| 179 | if (!PyArg_Parse(v, "i;array item must be integer", &x)) |
| 180 | return -1; |
| 181 | else if (x < 0) { |
| 182 | PyErr_SetString(PyExc_OverflowError, |
| 183 | "unsigned short is less than minimum"); |
| 184 | return -1; |
| 185 | } |
| 186 | else if (x > USHRT_MAX) { |
| 187 | PyErr_SetString(PyExc_OverflowError, |
| 188 | "unsigned short is greater than maximum"); |
| 189 | return -1; |
| 190 | } |
| 191 | if (i >= 0) |
| 192 | ((short *)ap->ob_item)[i] = (short)x; |
| 193 | return 0; |
| 194 | } |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 195 | |
| 196 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 197 | i_getitem(arrayobject *ap, int i) |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 198 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 199 | return PyInt_FromLong((long) ((int *)ap->ob_item)[i]); |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 203 | i_setitem(arrayobject *ap, int i, PyObject *v) |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 204 | { |
| 205 | int x; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 206 | /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */ |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 207 | if (!PyArg_Parse(v, "i;array item must be integer", &x)) |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 208 | return -1; |
| 209 | if (i >= 0) |
| 210 | ((int *)ap->ob_item)[i] = x; |
| 211 | return 0; |
| 212 | } |
| 213 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 214 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 215 | II_getitem(arrayobject *ap, int i) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 216 | { |
| 217 | return PyLong_FromUnsignedLong( |
| 218 | (unsigned long) ((unsigned int *)ap->ob_item)[i]); |
| 219 | } |
| 220 | |
| 221 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 222 | II_setitem(arrayobject *ap, int i, PyObject *v) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 223 | { |
| 224 | unsigned long x; |
| 225 | if (PyLong_Check(v)) { |
| 226 | x = PyLong_AsUnsignedLong(v); |
| 227 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 228 | return -1; |
| 229 | } |
| 230 | else { |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 231 | long y; |
| 232 | if (!PyArg_Parse(v, "l;array item must be integer", &y)) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 233 | return -1; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 234 | if (y < 0) { |
| 235 | PyErr_SetString(PyExc_OverflowError, |
| 236 | "unsigned int is less than minimum"); |
| 237 | return -1; |
| 238 | } |
| 239 | x = (unsigned long)y; |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 240 | |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 241 | } |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 242 | if (x > UINT_MAX) { |
| 243 | PyErr_SetString(PyExc_OverflowError, |
| 244 | "unsigned int is greater than maximum"); |
| 245 | return -1; |
| 246 | } |
| 247 | |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 248 | if (i >= 0) |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 249 | ((unsigned int *)ap->ob_item)[i] = (unsigned int)x; |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 254 | l_getitem(arrayobject *ap, int i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 255 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 256 | return PyInt_FromLong(((long *)ap->ob_item)[i]); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 260 | l_setitem(arrayobject *ap, int i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 261 | { |
| 262 | long x; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 263 | if (!PyArg_Parse(v, "l;array item must be integer", &x)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 264 | return -1; |
| 265 | if (i >= 0) |
| 266 | ((long *)ap->ob_item)[i] = x; |
| 267 | return 0; |
| 268 | } |
| 269 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 270 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 271 | LL_getitem(arrayobject *ap, int i) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 272 | { |
| 273 | return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]); |
| 274 | } |
| 275 | |
| 276 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 277 | LL_setitem(arrayobject *ap, int i, PyObject *v) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 278 | { |
| 279 | unsigned long x; |
| 280 | if (PyLong_Check(v)) { |
| 281 | x = PyLong_AsUnsignedLong(v); |
| 282 | if (x == (unsigned long) -1 && PyErr_Occurred()) |
| 283 | return -1; |
| 284 | } |
| 285 | else { |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 286 | long y; |
| 287 | if (!PyArg_Parse(v, "l;array item must be integer", &y)) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 288 | return -1; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 289 | if (y < 0) { |
| 290 | PyErr_SetString(PyExc_OverflowError, |
| 291 | "unsigned long is less than minimum"); |
| 292 | return -1; |
| 293 | } |
| 294 | x = (unsigned long)y; |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 295 | |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 296 | } |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 297 | if (x > ULONG_MAX) { |
| 298 | PyErr_SetString(PyExc_OverflowError, |
| 299 | "unsigned long is greater than maximum"); |
| 300 | return -1; |
| 301 | } |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 302 | |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 303 | if (i >= 0) |
| 304 | ((unsigned long *)ap->ob_item)[i] = x; |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 309 | f_getitem(arrayobject *ap, int i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 310 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 311 | return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 315 | f_setitem(arrayobject *ap, int i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 316 | { |
| 317 | float x; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 318 | if (!PyArg_Parse(v, "f;array item must be float", &x)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 319 | return -1; |
| 320 | if (i >= 0) |
| 321 | ((float *)ap->ob_item)[i] = x; |
| 322 | return 0; |
| 323 | } |
| 324 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 325 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 326 | d_getitem(arrayobject *ap, int i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 327 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 328 | return PyFloat_FromDouble(((double *)ap->ob_item)[i]); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 332 | d_setitem(arrayobject *ap, int i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 333 | { |
| 334 | double x; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 335 | if (!PyArg_Parse(v, "d;array item must be float", &x)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 336 | return -1; |
| 337 | if (i >= 0) |
| 338 | ((double *)ap->ob_item)[i] = x; |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | /* Description of types */ |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 343 | static struct arraydescr descriptors[] = { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 344 | {'c', sizeof(char), c_getitem, c_setitem}, |
| 345 | {'b', sizeof(char), b_getitem, b_setitem}, |
Guido van Rossum | 1a747f8 | 1997-05-16 16:21:38 +0000 | [diff] [blame] | 346 | {'B', sizeof(char), BB_getitem, BB_setitem}, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 347 | #ifdef Py_USING_UNICODE |
| 348 | {'u', sizeof(Py_UNICODE), u_getitem, u_setitem}, |
| 349 | #endif |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 350 | {'h', sizeof(short), h_getitem, h_setitem}, |
Guido van Rossum | 1a747f8 | 1997-05-16 16:21:38 +0000 | [diff] [blame] | 351 | {'H', sizeof(short), HH_getitem, HH_setitem}, |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 352 | {'i', sizeof(int), i_getitem, i_setitem}, |
Guido van Rossum | 1a747f8 | 1997-05-16 16:21:38 +0000 | [diff] [blame] | 353 | {'I', sizeof(int), II_getitem, II_setitem}, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 354 | {'l', sizeof(long), l_getitem, l_setitem}, |
Guido van Rossum | 1a747f8 | 1997-05-16 16:21:38 +0000 | [diff] [blame] | 355 | {'L', sizeof(long), LL_getitem, LL_setitem}, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 356 | {'f', sizeof(float), f_getitem, f_setitem}, |
| 357 | {'d', sizeof(double), d_getitem, d_setitem}, |
| 358 | {'\0', 0, 0, 0} /* Sentinel */ |
| 359 | }; |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 360 | |
| 361 | /**************************************************************************** |
| 362 | Implementations of array object methods. |
| 363 | ****************************************************************************/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 364 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 365 | static PyObject * |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 366 | newarrayobject(PyTypeObject *type, int size, struct arraydescr *descr) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 367 | { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 368 | arrayobject *op; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 369 | size_t nbytes; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 370 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 371 | if (size < 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 372 | PyErr_BadInternalCall(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 373 | return NULL; |
| 374 | } |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 375 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 376 | nbytes = size * descr->itemsize; |
| 377 | /* Check for overflow */ |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 378 | if (nbytes / descr->itemsize != (size_t)size) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 379 | return PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 380 | } |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 381 | op = (arrayobject *) type->tp_alloc(type, 0); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 382 | if (op == NULL) { |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 383 | return NULL; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 384 | } |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 385 | op->ob_size = size; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 386 | if (size <= 0) { |
| 387 | op->ob_item = NULL; |
| 388 | } |
| 389 | else { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 390 | op->ob_item = PyMem_NEW(char, nbytes); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 391 | if (op->ob_item == NULL) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 392 | PyObject_Del(op); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 393 | return PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 394 | } |
| 395 | } |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 396 | op->ob_descr = descr; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 397 | return (PyObject *) op; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 400 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 401 | getarrayitem(PyObject *op, int i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 402 | { |
| 403 | register arrayobject *ap; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 404 | assert(array_Check(op)); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 405 | ap = (arrayobject *)op; |
| 406 | if (i < 0 || i >= ap->ob_size) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 407 | PyErr_SetString(PyExc_IndexError, "array index out of range"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 408 | return NULL; |
| 409 | } |
| 410 | return (*ap->ob_descr->getitem)(ap, i); |
| 411 | } |
| 412 | |
| 413 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 414 | ins1(arrayobject *self, int where, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 415 | { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 416 | char *items; |
| 417 | if (v == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 418 | PyErr_BadInternalCall(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 419 | return -1; |
| 420 | } |
| 421 | if ((*self->ob_descr->setitem)(self, -1, v) < 0) |
| 422 | return -1; |
| 423 | items = self->ob_item; |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 424 | PyMem_RESIZE(items, char, |
| 425 | (self->ob_size+1) * self->ob_descr->itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 426 | if (items == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 427 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 428 | return -1; |
| 429 | } |
| 430 | if (where < 0) |
| 431 | where = 0; |
| 432 | if (where > self->ob_size) |
| 433 | where = self->ob_size; |
| 434 | memmove(items + (where+1)*self->ob_descr->itemsize, |
| 435 | items + where*self->ob_descr->itemsize, |
| 436 | (self->ob_size-where)*self->ob_descr->itemsize); |
| 437 | self->ob_item = items; |
| 438 | self->ob_size++; |
| 439 | return (*self->ob_descr->setitem)(self, where, v); |
| 440 | } |
| 441 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 442 | /* Methods */ |
| 443 | |
| 444 | static void |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 445 | array_dealloc(arrayobject *op) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 446 | { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 447 | if (op->ob_item != NULL) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 448 | PyMem_DEL(op->ob_item); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 449 | op->ob_type->tp_free((PyObject *)op); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 452 | static PyObject * |
| 453 | array_richcompare(PyObject *v, PyObject *w, int op) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 454 | { |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 455 | arrayobject *va, *wa; |
| 456 | PyObject *vi = NULL; |
| 457 | PyObject *wi = NULL; |
| 458 | int i, k; |
| 459 | PyObject *res; |
| 460 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 461 | if (!array_Check(v) || !array_Check(w)) { |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 462 | Py_INCREF(Py_NotImplemented); |
| 463 | return Py_NotImplemented; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 464 | } |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 465 | |
| 466 | va = (arrayobject *)v; |
| 467 | wa = (arrayobject *)w; |
| 468 | |
| 469 | if (va->ob_size != wa->ob_size && (op == Py_EQ || op == Py_NE)) { |
| 470 | /* Shortcut: if the lengths differ, the arrays differ */ |
| 471 | if (op == Py_EQ) |
| 472 | res = Py_False; |
| 473 | else |
| 474 | res = Py_True; |
| 475 | Py_INCREF(res); |
| 476 | return res; |
| 477 | } |
| 478 | |
| 479 | /* Search for the first index where items are different */ |
| 480 | k = 1; |
| 481 | for (i = 0; i < va->ob_size && i < wa->ob_size; i++) { |
| 482 | vi = getarrayitem(v, i); |
| 483 | wi = getarrayitem(w, i); |
| 484 | if (vi == NULL || wi == NULL) { |
| 485 | Py_XDECREF(vi); |
| 486 | Py_XDECREF(wi); |
| 487 | return NULL; |
| 488 | } |
| 489 | k = PyObject_RichCompareBool(vi, wi, Py_EQ); |
| 490 | if (k == 0) |
| 491 | break; /* Keeping vi and wi alive! */ |
| 492 | Py_DECREF(vi); |
| 493 | Py_DECREF(wi); |
| 494 | if (k < 0) |
| 495 | return NULL; |
| 496 | } |
| 497 | |
| 498 | if (k) { |
| 499 | /* No more items to compare -- compare sizes */ |
| 500 | int vs = va->ob_size; |
| 501 | int ws = wa->ob_size; |
| 502 | int cmp; |
| 503 | switch (op) { |
| 504 | case Py_LT: cmp = vs < ws; break; |
Guido van Rossum | 2b597e4 | 2001-01-25 22:12:43 +0000 | [diff] [blame] | 505 | case Py_LE: cmp = vs <= ws; break; |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 506 | case Py_EQ: cmp = vs == ws; break; |
| 507 | case Py_NE: cmp = vs != ws; break; |
| 508 | case Py_GT: cmp = vs > ws; break; |
| 509 | case Py_GE: cmp = vs >= ws; break; |
| 510 | default: return NULL; /* cannot happen */ |
| 511 | } |
| 512 | if (cmp) |
| 513 | res = Py_True; |
| 514 | else |
| 515 | res = Py_False; |
| 516 | Py_INCREF(res); |
| 517 | return res; |
| 518 | } |
| 519 | |
| 520 | /* We have an item that differs. First, shortcuts for EQ/NE */ |
| 521 | if (op == Py_EQ) { |
| 522 | Py_INCREF(Py_False); |
| 523 | res = Py_False; |
| 524 | } |
| 525 | else if (op == Py_NE) { |
| 526 | Py_INCREF(Py_True); |
| 527 | res = Py_True; |
| 528 | } |
| 529 | else { |
| 530 | /* Compare the final item again using the proper operator */ |
| 531 | res = PyObject_RichCompare(vi, wi, op); |
| 532 | } |
| 533 | Py_DECREF(vi); |
| 534 | Py_DECREF(wi); |
| 535 | return res; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 539 | array_length(arrayobject *a) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 540 | { |
| 541 | return a->ob_size; |
| 542 | } |
| 543 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 544 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 545 | array_item(arrayobject *a, int i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 546 | { |
| 547 | if (i < 0 || i >= a->ob_size) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 548 | PyErr_SetString(PyExc_IndexError, "array index out of range"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 549 | return NULL; |
| 550 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 551 | return getarrayitem((PyObject *)a, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 554 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 555 | array_slice(arrayobject *a, int ilow, int ihigh) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 556 | { |
| 557 | arrayobject *np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 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; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 568 | np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 569 | if (np == NULL) |
| 570 | return NULL; |
| 571 | memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize, |
| 572 | (ihigh-ilow) * a->ob_descr->itemsize); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 573 | return (PyObject *)np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 576 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 577 | array_concat(arrayobject *a, PyObject *bb) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 578 | { |
| 579 | int size; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 580 | arrayobject *np; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 581 | if (!array_Check(bb)) { |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 582 | PyErr_Format(PyExc_TypeError, |
| 583 | "can only append array (not \"%.200s\") to array", |
| 584 | bb->ob_type->tp_name); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 585 | return NULL; |
| 586 | } |
| 587 | #define b ((arrayobject *)bb) |
| 588 | if (a->ob_descr != b->ob_descr) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 589 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 590 | return NULL; |
| 591 | } |
| 592 | size = a->ob_size + b->ob_size; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 593 | np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 594 | if (np == NULL) { |
| 595 | return NULL; |
| 596 | } |
| 597 | memcpy(np->ob_item, a->ob_item, a->ob_size*a->ob_descr->itemsize); |
| 598 | memcpy(np->ob_item + a->ob_size*a->ob_descr->itemsize, |
Guido van Rossum | 32be3a7 | 1993-11-05 10:16:27 +0000 | [diff] [blame] | 599 | b->ob_item, b->ob_size*b->ob_descr->itemsize); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 600 | return (PyObject *)np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 601 | #undef b |
| 602 | } |
| 603 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 604 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 605 | array_repeat(arrayobject *a, int n) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 606 | { |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 607 | int i; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 608 | int size; |
| 609 | arrayobject *np; |
| 610 | char *p; |
| 611 | int nbytes; |
| 612 | if (n < 0) |
| 613 | n = 0; |
| 614 | size = a->ob_size * n; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 615 | np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 616 | if (np == NULL) |
| 617 | return NULL; |
| 618 | p = np->ob_item; |
| 619 | nbytes = a->ob_size * a->ob_descr->itemsize; |
| 620 | for (i = 0; i < n; i++) { |
| 621 | memcpy(p, a->ob_item, nbytes); |
| 622 | p += nbytes; |
| 623 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 624 | return (PyObject *) np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 628 | array_ass_slice(arrayobject *a, int ilow, int ihigh, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 629 | { |
| 630 | char *item; |
| 631 | int n; /* Size of replacement array */ |
| 632 | int d; /* Change in size */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 633 | #define b ((arrayobject *)v) |
| 634 | if (v == NULL) |
| 635 | n = 0; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 636 | else if (array_Check(v)) { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 637 | n = b->ob_size; |
| 638 | if (a == b) { |
| 639 | /* Special case "a[i:j] = a" -- copy b first */ |
| 640 | int ret; |
| 641 | v = array_slice(b, 0, n); |
| 642 | ret = array_ass_slice(a, ilow, ihigh, v); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 643 | Py_DECREF(v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 644 | return ret; |
| 645 | } |
| 646 | if (b->ob_descr != a->ob_descr) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 647 | PyErr_BadArgument(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 648 | return -1; |
| 649 | } |
| 650 | } |
| 651 | else { |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 652 | PyErr_Format(PyExc_TypeError, |
| 653 | "can only assign array (not \"%.200s\") to array slice", |
| 654 | v->ob_type->tp_name); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 655 | return -1; |
| 656 | } |
| 657 | if (ilow < 0) |
| 658 | ilow = 0; |
| 659 | else if (ilow > a->ob_size) |
| 660 | ilow = a->ob_size; |
| 661 | if (ihigh < 0) |
| 662 | ihigh = 0; |
| 663 | if (ihigh < ilow) |
| 664 | ihigh = ilow; |
| 665 | else if (ihigh > a->ob_size) |
| 666 | ihigh = a->ob_size; |
| 667 | item = a->ob_item; |
| 668 | d = n - (ihigh-ilow); |
| 669 | if (d < 0) { /* Delete -d items */ |
| 670 | memmove(item + (ihigh+d)*a->ob_descr->itemsize, |
| 671 | item + ihigh*a->ob_descr->itemsize, |
| 672 | (a->ob_size-ihigh)*a->ob_descr->itemsize); |
| 673 | a->ob_size += d; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 674 | PyMem_RESIZE(item, char, a->ob_size*a->ob_descr->itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 675 | /* Can't fail */ |
| 676 | a->ob_item = item; |
| 677 | } |
| 678 | else if (d > 0) { /* Insert d items */ |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 679 | PyMem_RESIZE(item, char, |
| 680 | (a->ob_size + d)*a->ob_descr->itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 681 | if (item == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 682 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 683 | return -1; |
| 684 | } |
| 685 | memmove(item + (ihigh+d)*a->ob_descr->itemsize, |
| 686 | item + ihigh*a->ob_descr->itemsize, |
| 687 | (a->ob_size-ihigh)*a->ob_descr->itemsize); |
| 688 | a->ob_item = item; |
| 689 | a->ob_size += d; |
| 690 | } |
| 691 | if (n > 0) |
| 692 | memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item, |
| 693 | n*b->ob_descr->itemsize); |
| 694 | return 0; |
| 695 | #undef b |
| 696 | } |
| 697 | |
| 698 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 699 | array_ass_item(arrayobject *a, int i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 700 | { |
| 701 | if (i < 0 || i >= a->ob_size) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 702 | PyErr_SetString(PyExc_IndexError, |
| 703 | "array assignment index out of range"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 704 | return -1; |
| 705 | } |
| 706 | if (v == NULL) |
| 707 | return array_ass_slice(a, i, i+1, v); |
| 708 | return (*a->ob_descr->setitem)(a, i, v); |
| 709 | } |
| 710 | |
| 711 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 712 | setarrayitem(PyObject *a, int i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 713 | { |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 714 | assert(array_Check(a)); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 715 | return array_ass_item((arrayobject *)a, i, v); |
| 716 | } |
| 717 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 718 | static int |
| 719 | array_do_extend(arrayobject *self, PyObject *bb) |
| 720 | { |
| 721 | int size; |
| 722 | |
| 723 | if (!array_Check(bb)) { |
| 724 | PyErr_Format(PyExc_TypeError, |
| 725 | "can only extend array with array (not \"%.200s\")", |
| 726 | bb->ob_type->tp_name); |
| 727 | return -1; |
| 728 | } |
| 729 | #define b ((arrayobject *)bb) |
| 730 | if (self->ob_descr != b->ob_descr) { |
| 731 | PyErr_SetString(PyExc_TypeError, |
| 732 | "can only extend with array of same kind"); |
| 733 | return -1; |
| 734 | } |
| 735 | size = self->ob_size + b->ob_size; |
| 736 | PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize); |
| 737 | if (self->ob_item == NULL) { |
| 738 | PyObject_Del(self); |
| 739 | PyErr_NoMemory(); |
| 740 | return -1; |
| 741 | } |
| 742 | memcpy(self->ob_item + self->ob_size*self->ob_descr->itemsize, |
| 743 | b->ob_item, b->ob_size*b->ob_descr->itemsize); |
| 744 | self->ob_size = size; |
| 745 | |
| 746 | return 0; |
| 747 | #undef b |
| 748 | } |
| 749 | |
| 750 | static PyObject * |
| 751 | array_inplace_concat(arrayobject *self, PyObject *bb) |
| 752 | { |
| 753 | if (array_do_extend(self, bb) == -1) |
| 754 | return NULL; |
| 755 | Py_INCREF(self); |
| 756 | return (PyObject *)self; |
| 757 | } |
| 758 | |
| 759 | static PyObject * |
| 760 | array_inplace_repeat(arrayobject *self, int n) |
| 761 | { |
| 762 | char *items, *p; |
| 763 | int size, i; |
| 764 | |
| 765 | if (self->ob_size > 0) { |
| 766 | if (n < 0) |
| 767 | n = 0; |
| 768 | items = self->ob_item; |
| 769 | size = self->ob_size * self->ob_descr->itemsize; |
| 770 | if (n == 0) { |
| 771 | PyMem_FREE(items); |
| 772 | self->ob_item = NULL; |
| 773 | self->ob_size = 0; |
| 774 | } |
| 775 | else { |
| 776 | PyMem_Resize(items, char, n * size); |
| 777 | if (items == NULL) |
| 778 | return PyErr_NoMemory(); |
| 779 | p = items; |
| 780 | for (i = 1; i < n; i++) { |
| 781 | p += size; |
| 782 | memcpy(p, items, size); |
| 783 | } |
| 784 | self->ob_item = items; |
| 785 | self->ob_size *= n; |
| 786 | } |
| 787 | } |
| 788 | Py_INCREF(self); |
| 789 | return (PyObject *)self; |
| 790 | } |
| 791 | |
| 792 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 793 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 794 | ins(arrayobject *self, int where, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 795 | { |
| 796 | if (ins1(self, where, v) != 0) |
| 797 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 798 | Py_INCREF(Py_None); |
| 799 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 802 | static PyObject * |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 803 | array_count(arrayobject *self, PyObject *args) |
| 804 | { |
| 805 | int count = 0; |
| 806 | int i; |
| 807 | PyObject *v; |
| 808 | |
| 809 | if (!PyArg_ParseTuple(args, "O:count", &v)) |
| 810 | return NULL; |
| 811 | for (i = 0; i < self->ob_size; i++) { |
| 812 | PyObject *selfi = getarrayitem((PyObject *)self, i); |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 813 | int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 814 | Py_DECREF(selfi); |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 815 | if (cmp > 0) |
| 816 | count++; |
| 817 | else if (cmp < 0) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 818 | return NULL; |
| 819 | } |
| 820 | return PyInt_FromLong((long)count); |
| 821 | } |
| 822 | |
| 823 | static char count_doc [] = |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 824 | "count(x)\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 825 | \n\ |
| 826 | Return number of occurences of x in the array."; |
| 827 | |
| 828 | static PyObject * |
| 829 | array_index(arrayobject *self, PyObject *args) |
| 830 | { |
| 831 | int i; |
| 832 | PyObject *v; |
| 833 | |
| 834 | if (!PyArg_ParseTuple(args, "O:index", &v)) |
| 835 | return NULL; |
| 836 | for (i = 0; i < self->ob_size; i++) { |
| 837 | PyObject *selfi = getarrayitem((PyObject *)self, i); |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 838 | int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
| 839 | Py_DECREF(selfi); |
| 840 | if (cmp > 0) { |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 841 | return PyInt_FromLong((long)i); |
| 842 | } |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 843 | else if (cmp < 0) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 844 | return NULL; |
| 845 | } |
| 846 | PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list"); |
| 847 | return NULL; |
| 848 | } |
| 849 | |
| 850 | static char index_doc [] = |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 851 | "index(x)\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 852 | \n\ |
| 853 | Return index of first occurence of x in the array."; |
| 854 | |
| 855 | static PyObject * |
| 856 | array_remove(arrayobject *self, PyObject *args) |
| 857 | { |
| 858 | int i; |
| 859 | PyObject *v; |
| 860 | |
| 861 | if (!PyArg_ParseTuple(args, "O:remove", &v)) |
| 862 | return NULL; |
| 863 | for (i = 0; i < self->ob_size; i++) { |
| 864 | PyObject *selfi = getarrayitem((PyObject *)self,i); |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 865 | int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
| 866 | Py_DECREF(selfi); |
| 867 | if (cmp > 0) { |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 868 | if (array_ass_slice(self, i, i+1, |
| 869 | (PyObject *)NULL) != 0) |
| 870 | return NULL; |
| 871 | Py_INCREF(Py_None); |
| 872 | return Py_None; |
| 873 | } |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 874 | else if (cmp < 0) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 875 | return NULL; |
| 876 | } |
| 877 | PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list"); |
| 878 | return NULL; |
| 879 | } |
| 880 | |
| 881 | static char remove_doc [] = |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 882 | "remove(x)\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 883 | \n\ |
| 884 | Remove the first occurence of x in the array."; |
| 885 | |
| 886 | static PyObject * |
| 887 | array_pop(arrayobject *self, PyObject *args) |
| 888 | { |
| 889 | int i = -1; |
| 890 | PyObject *v; |
| 891 | if (!PyArg_ParseTuple(args, "|i:pop", &i)) |
| 892 | return NULL; |
| 893 | if (self->ob_size == 0) { |
| 894 | /* Special-case most common failure cause */ |
| 895 | PyErr_SetString(PyExc_IndexError, "pop from empty array"); |
| 896 | return NULL; |
| 897 | } |
| 898 | if (i < 0) |
| 899 | i += self->ob_size; |
| 900 | if (i < 0 || i >= self->ob_size) { |
| 901 | PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
| 902 | return NULL; |
| 903 | } |
| 904 | v = getarrayitem((PyObject *)self,i); |
| 905 | if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) { |
| 906 | Py_DECREF(v); |
| 907 | return NULL; |
| 908 | } |
| 909 | return v; |
| 910 | } |
| 911 | |
| 912 | static char pop_doc [] = |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 913 | "pop([i])\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 914 | \n\ |
| 915 | Return the i-th element and delete it from the array. i defaults to -1."; |
| 916 | |
| 917 | static PyObject * |
Fred Drake | 8ce159a | 2000-08-31 05:18:54 +0000 | [diff] [blame] | 918 | array_extend(arrayobject *self, PyObject *args) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 919 | { |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 920 | PyObject *bb; |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 921 | |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 922 | if (!PyArg_ParseTuple(args, "O:extend", &bb)) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 923 | return NULL; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 924 | if (array_do_extend(self, bb) == -1) |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 925 | return NULL; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 926 | Py_INCREF(Py_None); |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 927 | return Py_None; |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | static char extend_doc [] = |
| 931 | "extend(array)\n\ |
| 932 | \n\ |
| 933 | Append array items to the end of the array."; |
| 934 | |
| 935 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 936 | array_insert(arrayobject *self, PyObject *args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 937 | { |
| 938 | int i; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 939 | PyObject *v; |
Peter Schneider-Kamp | dc71cac | 2000-07-31 21:57:30 +0000 | [diff] [blame] | 940 | if (!PyArg_ParseTuple(args, "iO:insert", &i, &v)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 941 | return NULL; |
| 942 | return ins(self, i, v); |
| 943 | } |
| 944 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 945 | static char insert_doc [] = |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 946 | "insert(i,x)\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 947 | \n\ |
| 948 | Insert a new item x into the array before position i."; |
| 949 | |
| 950 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 951 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 952 | array_buffer_info(arrayobject *self, PyObject *args) |
Guido van Rossum | de4a4ca | 1997-08-12 14:55:56 +0000 | [diff] [blame] | 953 | { |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 954 | PyObject* retval = NULL; |
| 955 | if (!PyArg_ParseTuple(args, ":buffer_info")) |
| 956 | return NULL; |
| 957 | retval = PyTuple_New(2); |
| 958 | if (!retval) |
| 959 | return NULL; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 960 | |
| 961 | PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item)); |
| 962 | PyTuple_SET_ITEM(retval, 1, PyInt_FromLong((long)(self->ob_size))); |
| 963 | |
| 964 | return retval; |
Guido van Rossum | de4a4ca | 1997-08-12 14:55:56 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 967 | static char buffer_info_doc [] = |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 968 | "buffer_info() -> (address, length)\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 969 | \n\ |
| 970 | Return a tuple (address, length) giving the current memory address and\n\ |
Guido van Rossum | 702d08e | 2001-07-27 16:05:32 +0000 | [diff] [blame] | 971 | the length in items of the buffer used to hold array's contents\n\ |
| 972 | The length should be multiplied by the itemsize attribute to calculate\n\ |
| 973 | the buffer length in bytes."; |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 974 | |
| 975 | |
Guido van Rossum | de4a4ca | 1997-08-12 14:55:56 +0000 | [diff] [blame] | 976 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 977 | array_append(arrayobject *self, PyObject *args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 978 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 979 | PyObject *v; |
Peter Schneider-Kamp | dc71cac | 2000-07-31 21:57:30 +0000 | [diff] [blame] | 980 | if (!PyArg_ParseTuple(args, "O:append", &v)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 981 | return NULL; |
| 982 | return ins(self, (int) self->ob_size, v); |
| 983 | } |
| 984 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 985 | static char append_doc [] = |
| 986 | "append(x)\n\ |
| 987 | \n\ |
| 988 | Append new value x to the end of the array."; |
| 989 | |
| 990 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 991 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 992 | array_byteswap(arrayobject *self, PyObject *args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 993 | { |
| 994 | char *p; |
| 995 | int i; |
Fred Drake | bf27298 | 1999-12-03 17:15:30 +0000 | [diff] [blame] | 996 | |
| 997 | if (!PyArg_ParseTuple(args, ":byteswap")) |
| 998 | return NULL; |
| 999 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1000 | switch (self->ob_descr->itemsize) { |
| 1001 | case 1: |
| 1002 | break; |
| 1003 | case 2: |
| 1004 | for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 2) { |
| 1005 | char p0 = p[0]; |
| 1006 | p[0] = p[1]; |
| 1007 | p[1] = p0; |
| 1008 | } |
| 1009 | break; |
| 1010 | case 4: |
| 1011 | for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 4) { |
| 1012 | char p0 = p[0]; |
| 1013 | char p1 = p[1]; |
| 1014 | p[0] = p[3]; |
| 1015 | p[1] = p[2]; |
| 1016 | p[2] = p1; |
| 1017 | p[3] = p0; |
| 1018 | } |
| 1019 | break; |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 1020 | case 8: |
| 1021 | for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 8) { |
| 1022 | char p0 = p[0]; |
| 1023 | char p1 = p[1]; |
| 1024 | char p2 = p[2]; |
| 1025 | char p3 = p[3]; |
| 1026 | p[0] = p[7]; |
| 1027 | p[1] = p[6]; |
| 1028 | p[2] = p[5]; |
| 1029 | p[3] = p[4]; |
| 1030 | p[4] = p3; |
| 1031 | p[5] = p2; |
| 1032 | p[6] = p1; |
| 1033 | p[7] = p0; |
| 1034 | } |
| 1035 | break; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1036 | default: |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1037 | PyErr_SetString(PyExc_RuntimeError, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1038 | "don't know how to byteswap this array type"); |
| 1039 | return NULL; |
| 1040 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1041 | Py_INCREF(Py_None); |
| 1042 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1045 | static char byteswap_doc [] = |
Fred Drake | bf27298 | 1999-12-03 17:15:30 +0000 | [diff] [blame] | 1046 | "byteswap()\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1047 | \n\ |
Fred Drake | bf27298 | 1999-12-03 17:15:30 +0000 | [diff] [blame] | 1048 | Byteswap all items of the array. If the items in the array are not 1, 2,\n\ |
| 1049 | 4, or 8 bytes in size, RuntimeError is raised."; |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1050 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1051 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 1052 | array_reverse(arrayobject *self, PyObject *args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1053 | { |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 1054 | register int itemsize = self->ob_descr->itemsize; |
| 1055 | register char *p, *q; |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 1056 | /* little buffer to hold items while swapping */ |
| 1057 | char tmp[256]; /* 8 is probably enough -- but why skimp */ |
| 1058 | assert(itemsize <= sizeof(tmp)); |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 1059 | |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 1060 | if (!PyArg_ParseTuple(args, ":reverse")) |
| 1061 | return NULL; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1062 | |
| 1063 | if (self->ob_size > 1) { |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 1064 | for (p = self->ob_item, |
| 1065 | q = self->ob_item + (self->ob_size - 1)*itemsize; |
| 1066 | p < q; |
| 1067 | p += itemsize, q -= itemsize) { |
Tim Peters | 077a11d | 2000-09-16 22:31:29 +0000 | [diff] [blame] | 1068 | /* memory areas guaranteed disjoint, so memcpy |
| 1069 | * is safe (& memmove may be slower). |
| 1070 | */ |
| 1071 | memcpy(tmp, p, itemsize); |
| 1072 | memcpy(p, q, itemsize); |
| 1073 | memcpy(q, tmp, itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1074 | } |
| 1075 | } |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 1076 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1077 | Py_INCREF(Py_None); |
| 1078 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1079 | } |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 1080 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1081 | static char reverse_doc [] = |
| 1082 | "reverse()\n\ |
| 1083 | \n\ |
| 1084 | Reverse the order of the items in the array."; |
| 1085 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1086 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 1087 | array_fromfile(arrayobject *self, PyObject *args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1088 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1089 | PyObject *f; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1090 | int n; |
| 1091 | FILE *fp; |
Peter Schneider-Kamp | dc71cac | 2000-07-31 21:57:30 +0000 | [diff] [blame] | 1092 | if (!PyArg_ParseTuple(args, "Oi:fromfile", &f, &n)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1093 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1094 | fp = PyFile_AsFile(f); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1095 | if (fp == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1096 | PyErr_SetString(PyExc_TypeError, "arg1 must be open file"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1097 | return NULL; |
| 1098 | } |
| 1099 | if (n > 0) { |
| 1100 | char *item = self->ob_item; |
| 1101 | int itemsize = self->ob_descr->itemsize; |
Guido van Rossum | 7d0ae5e | 2000-06-28 21:27:21 +0000 | [diff] [blame] | 1102 | size_t nread; |
Guido van Rossum | 3791b0d | 1999-02-23 18:05:22 +0000 | [diff] [blame] | 1103 | int newlength; |
| 1104 | size_t newbytes; |
| 1105 | /* Be careful here about overflow */ |
| 1106 | if ((newlength = self->ob_size + n) <= 0 || |
Guido van Rossum | 481ac88 | 1999-03-19 21:50:11 +0000 | [diff] [blame] | 1107 | (newbytes = newlength * itemsize) / itemsize != |
| 1108 | (size_t)newlength) |
Guido van Rossum | 3791b0d | 1999-02-23 18:05:22 +0000 | [diff] [blame] | 1109 | goto nomem; |
| 1110 | PyMem_RESIZE(item, char, newbytes); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1111 | if (item == NULL) { |
Guido van Rossum | 3791b0d | 1999-02-23 18:05:22 +0000 | [diff] [blame] | 1112 | nomem: |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1113 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1114 | return NULL; |
| 1115 | } |
| 1116 | self->ob_item = item; |
| 1117 | self->ob_size += n; |
| 1118 | nread = fread(item + (self->ob_size - n) * itemsize, |
| 1119 | itemsize, n, fp); |
Guido van Rossum | 7d0ae5e | 2000-06-28 21:27:21 +0000 | [diff] [blame] | 1120 | if (nread < (size_t)n) { |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1121 | self->ob_size -= (n - nread); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1122 | PyMem_RESIZE(item, char, self->ob_size*itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1123 | self->ob_item = item; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1124 | PyErr_SetString(PyExc_EOFError, |
| 1125 | "not enough items in file"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1126 | return NULL; |
| 1127 | } |
| 1128 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1129 | Py_INCREF(Py_None); |
| 1130 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1133 | static char fromfile_doc [] = |
| 1134 | "fromfile(f, n)\n\ |
| 1135 | \n\ |
| 1136 | Read n objects from the file object f and append them to the end of the\n\ |
| 1137 | array. Also called as read."; |
| 1138 | |
| 1139 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1140 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 1141 | array_tofile(arrayobject *self, PyObject *args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1142 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1143 | PyObject *f; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1144 | FILE *fp; |
Peter Schneider-Kamp | dc71cac | 2000-07-31 21:57:30 +0000 | [diff] [blame] | 1145 | if (!PyArg_ParseTuple(args, "O:tofile", &f)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1146 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1147 | fp = PyFile_AsFile(f); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1148 | if (fp == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1149 | PyErr_SetString(PyExc_TypeError, "arg must be open file"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1150 | return NULL; |
| 1151 | } |
| 1152 | if (self->ob_size > 0) { |
Trent Mick | 6c116dd | 2000-08-12 20:58:11 +0000 | [diff] [blame] | 1153 | if (fwrite(self->ob_item, self->ob_descr->itemsize, |
| 1154 | self->ob_size, fp) != (size_t)self->ob_size) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1155 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1156 | clearerr(fp); |
| 1157 | return NULL; |
| 1158 | } |
| 1159 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1160 | Py_INCREF(Py_None); |
| 1161 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1164 | static char tofile_doc [] = |
| 1165 | "tofile(f)\n\ |
| 1166 | \n\ |
| 1167 | Write all items (as machine values) to the file object f. Also called as\n\ |
| 1168 | write."; |
| 1169 | |
| 1170 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1171 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 1172 | array_fromlist(arrayobject *self, PyObject *args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1173 | { |
| 1174 | int n; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1175 | PyObject *list; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1176 | int itemsize = self->ob_descr->itemsize; |
Peter Schneider-Kamp | dc71cac | 2000-07-31 21:57:30 +0000 | [diff] [blame] | 1177 | if (!PyArg_ParseTuple(args, "O:fromlist", &list)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1178 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1179 | if (!PyList_Check(list)) { |
| 1180 | PyErr_SetString(PyExc_TypeError, "arg must be list"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1181 | return NULL; |
| 1182 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1183 | n = PyList_Size(list); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1184 | if (n > 0) { |
| 1185 | char *item = self->ob_item; |
| 1186 | int i; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1187 | PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1188 | if (item == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1189 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1190 | return NULL; |
| 1191 | } |
| 1192 | self->ob_item = item; |
| 1193 | self->ob_size += n; |
| 1194 | for (i = 0; i < n; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1195 | PyObject *v = PyList_GetItem(list, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1196 | if ((*self->ob_descr->setitem)(self, |
| 1197 | self->ob_size - n + i, v) != 0) { |
| 1198 | self->ob_size -= n; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1199 | PyMem_RESIZE(item, char, |
| 1200 | self->ob_size * itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1201 | self->ob_item = item; |
| 1202 | return NULL; |
| 1203 | } |
| 1204 | } |
| 1205 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1206 | Py_INCREF(Py_None); |
| 1207 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1210 | static char fromlist_doc [] = |
| 1211 | "fromlist(list)\n\ |
| 1212 | \n\ |
| 1213 | Append items to array from list."; |
| 1214 | |
| 1215 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1216 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 1217 | array_tolist(arrayobject *self, PyObject *args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1218 | { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1219 | PyObject *list = PyList_New(self->ob_size); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1220 | int i; |
Peter Schneider-Kamp | dc71cac | 2000-07-31 21:57:30 +0000 | [diff] [blame] | 1221 | if (!PyArg_ParseTuple(args, ":tolist")) |
| 1222 | return NULL; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1223 | if (list == NULL) |
| 1224 | return NULL; |
| 1225 | for (i = 0; i < self->ob_size; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1226 | PyObject *v = getarrayitem((PyObject *)self, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1227 | if (v == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1228 | Py_DECREF(list); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1229 | return NULL; |
| 1230 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1231 | PyList_SetItem(list, i, v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1232 | } |
| 1233 | return list; |
| 1234 | } |
| 1235 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1236 | static char tolist_doc [] = |
Guido van Rossum | fc6aba5 | 1998-10-14 02:52:31 +0000 | [diff] [blame] | 1237 | "tolist() -> list\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1238 | \n\ |
| 1239 | Convert array to an ordinary list with the same items."; |
| 1240 | |
| 1241 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1242 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 1243 | array_fromstring(arrayobject *self, PyObject *args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1244 | { |
| 1245 | char *str; |
| 1246 | int n; |
| 1247 | int itemsize = self->ob_descr->itemsize; |
Peter Schneider-Kamp | dc71cac | 2000-07-31 21:57:30 +0000 | [diff] [blame] | 1248 | if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1249 | return NULL; |
| 1250 | if (n % itemsize != 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1251 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1252 | "string length not a multiple of item size"); |
| 1253 | return NULL; |
| 1254 | } |
| 1255 | n = n / itemsize; |
| 1256 | if (n > 0) { |
| 1257 | char *item = self->ob_item; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1258 | PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1259 | if (item == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1260 | PyErr_NoMemory(); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1261 | return NULL; |
| 1262 | } |
| 1263 | self->ob_item = item; |
| 1264 | self->ob_size += n; |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1265 | memcpy(item + (self->ob_size - n) * itemsize, |
| 1266 | str, itemsize*n); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1267 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1268 | Py_INCREF(Py_None); |
| 1269 | return Py_None; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1272 | static char fromstring_doc [] = |
| 1273 | "fromstring(string)\n\ |
| 1274 | \n\ |
| 1275 | Appends items from the string, interpreting it as an array of machine\n\ |
| 1276 | values,as if it had been read from a file using the fromfile() method)."; |
| 1277 | |
| 1278 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1279 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 1280 | array_tostring(arrayobject *self, PyObject *args) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1281 | { |
Peter Schneider-Kamp | dc71cac | 2000-07-31 21:57:30 +0000 | [diff] [blame] | 1282 | if (!PyArg_ParseTuple(args, ":tostring")) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1283 | return NULL; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1284 | return PyString_FromStringAndSize(self->ob_item, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1285 | self->ob_size * self->ob_descr->itemsize); |
| 1286 | } |
| 1287 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1288 | static char tostring_doc [] = |
| 1289 | "tostring() -> string\n\ |
| 1290 | \n\ |
| 1291 | Convert the array to an array of machine values and return the string\n\ |
| 1292 | representation."; |
| 1293 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1294 | |
| 1295 | |
| 1296 | #ifdef Py_USING_UNICODE |
| 1297 | static PyObject * |
| 1298 | array_fromunicode(arrayobject *self, PyObject *args) |
| 1299 | { |
| 1300 | Py_UNICODE *ustr; |
| 1301 | int n; |
| 1302 | |
| 1303 | if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n)) |
| 1304 | return NULL; |
| 1305 | if (self->ob_descr->typecode != 'u') { |
| 1306 | PyErr_SetString(PyExc_ValueError, |
| 1307 | "fromunicode() may only be called on " |
| 1308 | "type 'u' arrays"); |
| 1309 | return NULL; |
| 1310 | } |
| 1311 | if (n > 0) { |
| 1312 | Py_UNICODE *item = (Py_UNICODE *) self->ob_item; |
| 1313 | PyMem_RESIZE(item, Py_UNICODE, self->ob_size + n); |
| 1314 | if (item == NULL) { |
| 1315 | PyErr_NoMemory(); |
| 1316 | return NULL; |
| 1317 | } |
| 1318 | self->ob_item = (char *) item; |
| 1319 | self->ob_size += n; |
| 1320 | memcpy(item + self->ob_size - n, |
| 1321 | ustr, n * sizeof(Py_UNICODE)); |
| 1322 | } |
| 1323 | |
| 1324 | Py_INCREF(Py_None); |
| 1325 | return Py_None; |
| 1326 | } |
| 1327 | |
| 1328 | static char fromunicode_doc[] = |
| 1329 | "fromunicode(ustr)\n\ |
| 1330 | \n\ |
| 1331 | Extends this array with data from the unicode string ustr.\n\ |
| 1332 | The array must be a type 'u' array; otherwise a ValueError\n\ |
| 1333 | is raised. Use array.fromstring(ustr.decode(...)) to\n\ |
| 1334 | append Unicode data to an array of some other type."; |
| 1335 | |
| 1336 | |
| 1337 | static PyObject * |
| 1338 | array_tounicode(arrayobject *self, PyObject *args) |
| 1339 | { |
| 1340 | if (!PyArg_ParseTuple(args, ":tounicode")) |
| 1341 | return NULL; |
| 1342 | if (self->ob_descr->typecode != 'u') { |
| 1343 | PyErr_SetString(PyExc_ValueError, |
| 1344 | "tounicode() may only be called on type 'u' arrays"); |
| 1345 | return NULL; |
| 1346 | } |
| 1347 | return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, self->ob_size); |
| 1348 | } |
| 1349 | |
| 1350 | static char tounicode_doc [] = |
| 1351 | "tounicode() -> unicode\n\ |
| 1352 | \n\ |
| 1353 | Convert the array to a unicode string. The array must be\n\ |
| 1354 | a type 'u' array; otherwise a ValueError is raised. Use\n\ |
| 1355 | array.tostring().decode() to obtain a unicode string from\n\ |
| 1356 | an array of some other type."; |
| 1357 | |
| 1358 | #endif /* Py_USING_UNICODE */ |
| 1359 | |
| 1360 | |
| 1361 | static PyObject * |
| 1362 | array_get_typecode(arrayobject *a, void *closure) |
| 1363 | { |
| 1364 | char tc = a->ob_descr->typecode; |
| 1365 | return PyString_FromStringAndSize(&tc, 1); |
| 1366 | } |
| 1367 | |
| 1368 | static PyObject * |
| 1369 | array_get_itemsize(arrayobject *a, void *closure) |
| 1370 | { |
| 1371 | return PyInt_FromLong((long)a->ob_descr->itemsize); |
| 1372 | } |
| 1373 | |
| 1374 | static PyGetSetDef array_getsets [] = { |
| 1375 | {"typecode", (getter) array_get_typecode, NULL, |
| 1376 | "the typecode character used to create the array"}, |
| 1377 | {"itemsize", (getter) array_get_itemsize, NULL, |
| 1378 | "the size, in bytes, of one array item"}, |
| 1379 | {NULL} |
| 1380 | }; |
| 1381 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1382 | PyMethodDef array_methods[] = { |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1383 | {"append", (PyCFunction)array_append, METH_VARARGS, |
| 1384 | append_doc}, |
| 1385 | {"buffer_info", (PyCFunction)array_buffer_info, METH_VARARGS, |
| 1386 | buffer_info_doc}, |
| 1387 | {"byteswap", (PyCFunction)array_byteswap, METH_VARARGS, |
| 1388 | byteswap_doc}, |
| 1389 | {"count", (PyCFunction)array_count, METH_VARARGS, |
| 1390 | count_doc}, |
| 1391 | {"extend", (PyCFunction)array_extend, METH_VARARGS, |
| 1392 | extend_doc}, |
| 1393 | {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS, |
| 1394 | fromfile_doc}, |
| 1395 | {"fromlist", (PyCFunction)array_fromlist, METH_VARARGS, |
| 1396 | fromlist_doc}, |
| 1397 | {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS, |
| 1398 | fromstring_doc}, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1399 | #ifdef Py_USING_UNICODE |
| 1400 | {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS, |
| 1401 | fromunicode_doc}, |
| 1402 | #endif |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1403 | {"index", (PyCFunction)array_index, METH_VARARGS, |
| 1404 | index_doc}, |
| 1405 | {"insert", (PyCFunction)array_insert, METH_VARARGS, |
| 1406 | insert_doc}, |
| 1407 | {"pop", (PyCFunction)array_pop, METH_VARARGS, |
| 1408 | pop_doc}, |
| 1409 | {"read", (PyCFunction)array_fromfile, METH_VARARGS, |
| 1410 | fromfile_doc}, |
| 1411 | {"remove", (PyCFunction)array_remove, METH_VARARGS, |
| 1412 | remove_doc}, |
| 1413 | {"reverse", (PyCFunction)array_reverse, METH_VARARGS, |
| 1414 | reverse_doc}, |
| 1415 | /* {"sort", (PyCFunction)array_sort, METH_VARARGS, |
| 1416 | sort_doc},*/ |
| 1417 | {"tofile", (PyCFunction)array_tofile, METH_VARARGS, |
| 1418 | tofile_doc}, |
| 1419 | {"tolist", (PyCFunction)array_tolist, METH_VARARGS, |
| 1420 | tolist_doc}, |
| 1421 | {"tostring", (PyCFunction)array_tostring, METH_VARARGS, |
| 1422 | tostring_doc}, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1423 | #ifdef Py_USING_UNICODE |
| 1424 | {"tounicode", (PyCFunction)array_tounicode, METH_VARARGS, |
| 1425 | tounicode_doc}, |
| 1426 | #endif |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1427 | {"write", (PyCFunction)array_tofile, METH_VARARGS, |
| 1428 | tofile_doc}, |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1429 | {NULL, NULL} /* sentinel */ |
| 1430 | }; |
| 1431 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1432 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 1433 | array_repr(arrayobject *a) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1434 | { |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1435 | char buf[256], typecode; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1436 | PyObject *s, *t, *comma, *v; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1437 | int i, len; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1438 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1439 | len = a->ob_size; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1440 | typecode = a->ob_descr->typecode; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1441 | if (len == 0) { |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1442 | PyOS_snprintf(buf, sizeof(buf), "array('%c')", typecode); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1443 | return PyString_FromString(buf); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1444 | } |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1445 | |
| 1446 | if (typecode == 'c' || typecode == 'u') { |
Tim Peters | 7c1cb46 | 2000-11-10 19:04:19 +0000 | [diff] [blame] | 1447 | PyObject *t_empty = PyTuple_New(0); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1448 | PyOS_snprintf(buf, sizeof(buf), "array('%c', ", typecode); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1449 | s = PyString_FromString(buf); |
Michael W. Hudson | 1755ad9 | 2002-05-13 10:14:59 +0000 | [diff] [blame] | 1450 | #ifdef Py_USING_UNICODE |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1451 | if (typecode == 'c') |
Michael W. Hudson | 1755ad9 | 2002-05-13 10:14:59 +0000 | [diff] [blame] | 1452 | #endif |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1453 | v = array_tostring(a, t_empty); |
Michael W. Hudson | 1755ad9 | 2002-05-13 10:14:59 +0000 | [diff] [blame] | 1454 | #ifdef Py_USING_UNICODE |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1455 | else |
| 1456 | v = array_tounicode(a, t_empty); |
Michael W. Hudson | 1755ad9 | 2002-05-13 10:14:59 +0000 | [diff] [blame] | 1457 | #endif |
Tim Peters | 7c1cb46 | 2000-11-10 19:04:19 +0000 | [diff] [blame] | 1458 | Py_DECREF(t_empty); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1459 | t = PyObject_Repr(v); |
| 1460 | Py_XDECREF(v); |
| 1461 | PyString_ConcatAndDel(&s, t); |
| 1462 | PyString_ConcatAndDel(&s, PyString_FromString(")")); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1463 | return s; |
| 1464 | } |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1465 | PyOS_snprintf(buf, sizeof(buf), "array('%c', [", typecode); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1466 | s = PyString_FromString(buf); |
| 1467 | comma = PyString_FromString(", "); |
| 1468 | for (i = 0; i < len && !PyErr_Occurred(); i++) { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1469 | if (i > 0) |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1470 | PyString_Concat(&s, comma); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1471 | v = (a->ob_descr->getitem)(a, i); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1472 | t = PyObject_Repr(v); |
| 1473 | Py_XDECREF(v); |
| 1474 | PyString_ConcatAndDel(&s, t); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1475 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1476 | Py_XDECREF(comma); |
| 1477 | PyString_ConcatAndDel(&s, PyString_FromString("])")); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1478 | return s; |
| 1479 | } |
| 1480 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1481 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 1482 | array_buffer_getreadbuf(arrayobject *self, int index, const void **ptr) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1483 | { |
| 1484 | if ( index != 0 ) { |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1485 | PyErr_SetString(PyExc_SystemError, |
| 1486 | "Accessing non-existent array segment"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1487 | return -1; |
| 1488 | } |
| 1489 | *ptr = (void *)self->ob_item; |
| 1490 | return self->ob_size*self->ob_descr->itemsize; |
| 1491 | } |
| 1492 | |
| 1493 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 1494 | array_buffer_getwritebuf(arrayobject *self, int index, const void **ptr) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1495 | { |
| 1496 | if ( index != 0 ) { |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1497 | PyErr_SetString(PyExc_SystemError, |
| 1498 | "Accessing non-existent array segment"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1499 | return -1; |
| 1500 | } |
| 1501 | *ptr = (void *)self->ob_item; |
| 1502 | return self->ob_size*self->ob_descr->itemsize; |
| 1503 | } |
| 1504 | |
| 1505 | static int |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 1506 | array_buffer_getsegcount(arrayobject *self, int *lenp) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1507 | { |
| 1508 | if ( lenp ) |
| 1509 | *lenp = self->ob_size*self->ob_descr->itemsize; |
| 1510 | return 1; |
| 1511 | } |
| 1512 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1513 | static PySequenceMethods array_as_sequence = { |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1514 | (inquiry)array_length, /*sq_length*/ |
| 1515 | (binaryfunc)array_concat, /*sq_concat*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1516 | (intargfunc)array_repeat, /*sq_repeat*/ |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1517 | (intargfunc)array_item, /*sq_item*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1518 | (intintargfunc)array_slice, /*sq_slice*/ |
| 1519 | (intobjargproc)array_ass_item, /*sq_ass_item*/ |
| 1520 | (intintobjargproc)array_ass_slice, /*sq_ass_slice*/ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1521 | NULL, /*sq_contains*/ |
| 1522 | (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/ |
| 1523 | (intargfunc)array_inplace_repeat /*sq_inplace_repeat*/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1524 | }; |
| 1525 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1526 | static PyBufferProcs array_as_buffer = { |
| 1527 | (getreadbufferproc)array_buffer_getreadbuf, |
| 1528 | (getwritebufferproc)array_buffer_getwritebuf, |
| 1529 | (getsegcountproc)array_buffer_getsegcount, |
| 1530 | }; |
| 1531 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1532 | static PyObject * |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1533 | array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1534 | { |
| 1535 | char c; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1536 | PyObject *initial = NULL; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1537 | struct arraydescr *descr; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1538 | |
| 1539 | if (kwds != NULL) { |
| 1540 | int i = PyObject_Length(kwds); |
| 1541 | if (i < 0) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1542 | return NULL; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1543 | else if (i > 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1544 | PyErr_SetString(PyExc_TypeError, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1545 | "array.array constructor takes " |
| 1546 | "no keyword arguments"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1547 | return NULL; |
| 1548 | } |
| 1549 | } |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1550 | |
| 1551 | if (!PyArg_ParseTuple(args, "c|O:array", &c, &initial)) |
| 1552 | return NULL; |
| 1553 | |
| 1554 | if (!(initial == NULL || PyList_Check(initial) |
| 1555 | || PyString_Check(initial) |
| 1556 | || (c == 'u' && PyUnicode_Check(initial)))) { |
| 1557 | PyErr_SetString(PyExc_TypeError, |
| 1558 | "array initializer must be list or string"); |
| 1559 | return NULL; |
| 1560 | } |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1561 | for (descr = descriptors; descr->typecode != '\0'; descr++) { |
| 1562 | if (descr->typecode == c) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1563 | PyObject *a; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1564 | int len; |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1565 | if (initial == NULL || !PyList_Check(initial)) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1566 | len = 0; |
| 1567 | else |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1568 | len = PyList_Size(initial); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1569 | |
| 1570 | a = newarrayobject(type, len, descr); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1571 | if (a == NULL) |
| 1572 | return NULL; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1573 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1574 | if (len > 0) { |
| 1575 | int i; |
| 1576 | for (i = 0; i < len; i++) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1577 | PyObject *v = |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1578 | PyList_GetItem(initial, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1579 | if (setarrayitem(a, i, v) != 0) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1580 | Py_DECREF(a); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1581 | return NULL; |
| 1582 | } |
| 1583 | } |
| 1584 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1585 | if (initial != NULL && PyString_Check(initial)) { |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1586 | PyObject *t_initial = Py_BuildValue("(O)", |
| 1587 | initial); |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1588 | PyObject *v = |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1589 | array_fromstring((arrayobject *)a, |
| 1590 | t_initial); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1591 | Py_DECREF(t_initial); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1592 | if (v == NULL) { |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1593 | Py_DECREF(a); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1594 | return NULL; |
| 1595 | } |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1596 | Py_DECREF(v); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1597 | #ifdef Py_USING_UNICODE |
| 1598 | } else if (initial != NULL && PyUnicode_Check(initial)) { |
| 1599 | int n = PyUnicode_GET_DATA_SIZE(initial); |
| 1600 | if (n > 0) { |
| 1601 | arrayobject *self = (arrayobject *)a; |
| 1602 | char *item = self->ob_item; |
| 1603 | item = PyMem_Realloc(item, n); |
| 1604 | if (item == NULL) { |
| 1605 | PyErr_NoMemory(); |
| 1606 | Py_DECREF(a); |
| 1607 | return NULL; |
| 1608 | } |
| 1609 | self->ob_item = item; |
| 1610 | self->ob_size = n / sizeof(Py_UNICODE); |
| 1611 | memcpy(item, PyUnicode_AS_DATA(initial), n); |
| 1612 | } |
| 1613 | #endif |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1614 | } |
| 1615 | return a; |
| 1616 | } |
| 1617 | } |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 1618 | PyErr_SetString(PyExc_ValueError, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1619 | "bad typecode (must be c, b, B, u, h, H, i, I, l, L, f or d)"); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1620 | return NULL; |
| 1621 | } |
| 1622 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1623 | |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1624 | static char module_doc [] = |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1625 | "This module defines an object type which can efficiently represent\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1626 | an array of basic values: characters, integers, floating point\n\ |
| 1627 | numbers. Arrays are sequence types and behave very much like lists,\n\ |
| 1628 | except that the type of objects stored in them is constrained. The\n\ |
| 1629 | type is specified at object creation time by using a type code, which\n\ |
| 1630 | is a single character. The following type codes are defined:\n\ |
| 1631 | \n\ |
| 1632 | Type code C Type Minimum size in bytes \n\ |
| 1633 | 'c' character 1 \n\ |
| 1634 | 'b' signed integer 1 \n\ |
| 1635 | 'B' unsigned integer 1 \n\ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1636 | 'u' Unicode character 2 \n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1637 | 'h' signed integer 2 \n\ |
| 1638 | 'H' unsigned integer 2 \n\ |
| 1639 | 'i' signed integer 2 \n\ |
| 1640 | 'I' unsigned integer 2 \n\ |
| 1641 | 'l' signed integer 4 \n\ |
| 1642 | 'L' unsigned integer 4 \n\ |
| 1643 | 'f' floating point 4 \n\ |
| 1644 | 'd' floating point 8 \n\ |
| 1645 | \n\ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1646 | The constructor is:\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1647 | \n\ |
| 1648 | array(typecode [, initializer]) -- create a new array\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1649 | "; |
| 1650 | |
| 1651 | static char arraytype_doc [] = |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1652 | "array(typecode [, initializer]) -> array\n\ |
| 1653 | \n\ |
| 1654 | Return a new array whose items are restricted by typecode, and\n\ |
| 1655 | initialized from the optional initializer value, which must be a list\n\ |
| 1656 | or a string.\n\ |
| 1657 | \n\ |
| 1658 | Arrays represent basic values and behave very much like lists, except\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1659 | the type of objects stored in them is constrained.\n\ |
| 1660 | \n\ |
| 1661 | Methods:\n\ |
| 1662 | \n\ |
| 1663 | append() -- append a new item to the end of the array\n\ |
| 1664 | buffer_info() -- return information giving the current memory info\n\ |
| 1665 | byteswap() -- byteswap all the items of the array\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1666 | count() -- return number of occurences of an object\n\ |
| 1667 | extend() -- extend array by appending array elements\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1668 | fromfile() -- read items from a file object\n\ |
| 1669 | fromlist() -- append items from the list\n\ |
| 1670 | fromstring() -- append items from the string\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1671 | index() -- return index of first occurence of an object\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1672 | 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] | 1673 | pop() -- remove and return item (default last)\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1674 | read() -- DEPRECATED, use fromfile()\n\ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1675 | remove() -- remove first occurence of an object\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1676 | reverse() -- reverse the order of the items in the array\n\ |
| 1677 | tofile() -- write all items to a file object\n\ |
| 1678 | tolist() -- return the array converted to an ordinary list\n\ |
| 1679 | tostring() -- return the array converted to a string\n\ |
| 1680 | write() -- DEPRECATED, use tofile()\n\ |
| 1681 | \n\ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1682 | Attributes:\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1683 | \n\ |
| 1684 | typecode -- the typecode character used to create the array\n\ |
| 1685 | itemsize -- the length in bytes of one array item\n\ |
| 1686 | "; |
| 1687 | |
| 1688 | statichere PyTypeObject Arraytype = { |
Fred Drake | 0d40ba4 | 2000-02-04 20:33:49 +0000 | [diff] [blame] | 1689 | PyObject_HEAD_INIT(NULL) |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1690 | 0, |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 1691 | "array.array", |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1692 | sizeof(arrayobject), |
| 1693 | 0, |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1694 | (destructor)array_dealloc, /* tp_dealloc */ |
Martin v. Löwis | aa158be | 2002-03-04 09:38:52 +0000 | [diff] [blame] | 1695 | 0, /* tp_print */ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1696 | 0, /* tp_getattr */ |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1697 | 0, /* tp_setattr */ |
| 1698 | 0, /* tp_compare */ |
| 1699 | (reprfunc)array_repr, /* tp_repr */ |
| 1700 | 0, /* tp_as _number*/ |
| 1701 | &array_as_sequence, /* tp_as _sequence*/ |
| 1702 | 0, /* tp_as _mapping*/ |
| 1703 | 0, /* tp_hash */ |
| 1704 | 0, /* tp_call */ |
| 1705 | 0, /* tp_str */ |
Martin v. Löwis | b9132a2 | 2002-05-02 20:34:12 +0000 | [diff] [blame] | 1706 | DELAYED(PyObject_GenericGetAttr), /* tp_getattro */ |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1707 | 0, /* tp_setattro */ |
Tim Peters | b870c75 | 2001-06-05 04:43:26 +0000 | [diff] [blame] | 1708 | &array_as_buffer, /* tp_as_buffer*/ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1709 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 1710 | arraytype_doc, /* tp_doc */ |
| 1711 | 0, /* tp_traverse */ |
| 1712 | 0, /* tp_clear */ |
| 1713 | array_richcompare, /* tp_richcompare */ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1714 | 0, /* tp_weaklistoffset */ |
| 1715 | 0, /* tp_iter */ |
| 1716 | 0, /* tp_iternext */ |
| 1717 | array_methods, /* tp_methods */ |
| 1718 | 0, /* tp_members */ |
| 1719 | array_getsets, /* tp_getset */ |
| 1720 | 0, /* tp_base */ |
| 1721 | 0, /* tp_dict */ |
| 1722 | 0, /* tp_descr_get */ |
| 1723 | 0, /* tp_descr_set */ |
| 1724 | 0, /* tp_dictoffset */ |
| 1725 | 0, /* tp_init */ |
Martin v. Löwis | b9132a2 | 2002-05-02 20:34:12 +0000 | [diff] [blame] | 1726 | DELAYED(PyType_GenericAlloc), /* tp_alloc */ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1727 | array_new, /* tp_new */ |
Martin v. Löwis | b9132a2 | 2002-05-02 20:34:12 +0000 | [diff] [blame] | 1728 | DELAYED(PyObject_Del), /* tp_free */ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1729 | }; |
| 1730 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1731 | /* No functions in array module. */ |
| 1732 | static PyMethodDef a_methods[] = { |
| 1733 | {NULL, NULL, 0, NULL} /* Sentinel */ |
| 1734 | }; |
| 1735 | |
| 1736 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 1737 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1738 | initarray(void) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1739 | { |
Fred Drake | f4e3484 | 2002-04-01 03:45:06 +0000 | [diff] [blame] | 1740 | PyObject *m; |
Fred Drake | 0d40ba4 | 2000-02-04 20:33:49 +0000 | [diff] [blame] | 1741 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1742 | Arraytype.ob_type = &PyType_Type; |
Martin v. Löwis | edbffc1 | 2002-05-02 20:09:59 +0000 | [diff] [blame] | 1743 | Arraytype.tp_getattro = PyObject_GenericGetAttr; |
| 1744 | Arraytype.tp_alloc = PyType_GenericAlloc; |
| 1745 | Arraytype.tp_free = PyObject_Del; |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1746 | m = Py_InitModule3("array", a_methods, module_doc); |
Fred Drake | f4e3484 | 2002-04-01 03:45:06 +0000 | [diff] [blame] | 1747 | |
| 1748 | Py_INCREF((PyObject *)&Arraytype); |
| 1749 | PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype); |
| 1750 | Py_INCREF((PyObject *)&Arraytype); |
| 1751 | PyModule_AddObject(m, "array", (PyObject *)&Arraytype); |
Guido van Rossum | a0deb64 | 1998-10-14 13:45:06 +0000 | [diff] [blame] | 1752 | /* 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] | 1753 | } |