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