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 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6 | #define PY_SSIZE_T_CLEAN |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 7 | #include "Python.h" |
Raymond Hettinger | cb87bc8 | 2004-05-31 00:35:52 +0000 | [diff] [blame] | 8 | #include "structmember.h" |
Roger E. Masse | 5817f8f | 1996-12-09 22:24:19 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 0c70954 | 1994-08-19 12:01:32 +0000 | [diff] [blame] | 10 | #ifdef STDC_HEADERS |
| 11 | #include <stddef.h> |
Guido van Rossum | 7f1de83 | 1999-08-27 20:33:52 +0000 | [diff] [blame] | 12 | #else /* !STDC_HEADERS */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13 | #ifdef HAVE_SYS_TYPES_H |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 14 | #include <sys/types.h> /* For size_t */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 15 | #endif /* HAVE_SYS_TYPES_H */ |
Guido van Rossum | 7f1de83 | 1999-08-27 20:33:52 +0000 | [diff] [blame] | 16 | #endif /* !STDC_HEADERS */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 17 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 18 | /*[clinic input] |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 19 | module array |
| 20 | [clinic start generated code]*/ |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 21 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=7d1b8d7f5958fd83]*/ |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 22 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 23 | struct arrayobject; /* Forward */ |
| 24 | |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 25 | /* All possible arraydescr values are defined in the vector "descriptors" |
| 26 | * below. That's defined later because the appropriate get and set |
| 27 | * functions aren't visible yet. |
| 28 | */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 29 | struct arraydescr { |
Victor Stinner | f8bb7d0 | 2011-09-30 00:03:59 +0200 | [diff] [blame] | 30 | char typecode; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 31 | int itemsize; |
| 32 | PyObject * (*getitem)(struct arrayobject *, Py_ssize_t); |
| 33 | int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *); |
Adrian Wielgosik | 7c17e23 | 2017-08-17 12:46:06 +0000 | [diff] [blame] | 34 | int (*compareitems)(const void *, const void *, Py_ssize_t); |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 35 | const char *formats; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 36 | int is_integer_type; |
| 37 | int is_signed; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | typedef struct arrayobject { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 41 | PyObject_VAR_HEAD |
| 42 | char *ob_item; |
| 43 | Py_ssize_t allocated; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 44 | const struct arraydescr *ob_descr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 45 | PyObject *weakreflist; /* List of weak references */ |
Hai Shi | 06cd5b6 | 2019-10-21 14:31:46 +0800 | [diff] [blame] | 46 | Py_ssize_t ob_exports; /* Number of exported buffers */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 47 | } arrayobject; |
| 48 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 49 | static PyTypeObject Arraytype; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 50 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 51 | typedef struct { |
| 52 | PyObject_HEAD |
| 53 | Py_ssize_t index; |
| 54 | arrayobject *ao; |
| 55 | PyObject* (*getitem)(struct arrayobject *, Py_ssize_t); |
| 56 | } arrayiterobject; |
| 57 | |
| 58 | static PyTypeObject PyArrayIter_Type; |
| 59 | |
| 60 | #define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type) |
| 61 | |
Larry Hastings | dfbeb16 | 2014-10-13 10:39:41 +0100 | [diff] [blame] | 62 | enum machine_format_code { |
| 63 | UNKNOWN_FORMAT = -1, |
| 64 | /* UNKNOWN_FORMAT is used to indicate that the machine format for an |
| 65 | * array type code cannot be interpreted. When this occurs, a list of |
| 66 | * Python objects is used to represent the content of the array |
| 67 | * instead of using the memory content of the array directly. In that |
| 68 | * case, the array_reconstructor mechanism is bypassed completely, and |
| 69 | * the standard array constructor is used instead. |
| 70 | * |
| 71 | * This is will most likely occur when the machine doesn't use IEEE |
| 72 | * floating-point numbers. |
| 73 | */ |
| 74 | |
| 75 | UNSIGNED_INT8 = 0, |
| 76 | SIGNED_INT8 = 1, |
| 77 | UNSIGNED_INT16_LE = 2, |
| 78 | UNSIGNED_INT16_BE = 3, |
| 79 | SIGNED_INT16_LE = 4, |
| 80 | SIGNED_INT16_BE = 5, |
| 81 | UNSIGNED_INT32_LE = 6, |
| 82 | UNSIGNED_INT32_BE = 7, |
| 83 | SIGNED_INT32_LE = 8, |
| 84 | SIGNED_INT32_BE = 9, |
| 85 | UNSIGNED_INT64_LE = 10, |
| 86 | UNSIGNED_INT64_BE = 11, |
| 87 | SIGNED_INT64_LE = 12, |
| 88 | SIGNED_INT64_BE = 13, |
| 89 | IEEE_754_FLOAT_LE = 14, |
| 90 | IEEE_754_FLOAT_BE = 15, |
| 91 | IEEE_754_DOUBLE_LE = 16, |
| 92 | IEEE_754_DOUBLE_BE = 17, |
| 93 | UTF16_LE = 18, |
| 94 | UTF16_BE = 19, |
| 95 | UTF32_LE = 20, |
| 96 | UTF32_BE = 21 |
| 97 | }; |
| 98 | #define MACHINE_FORMAT_CODE_MIN 0 |
| 99 | #define MACHINE_FORMAT_CODE_MAX 21 |
| 100 | |
| 101 | |
| 102 | /* |
| 103 | * Must come after arrayobject, arrayiterobject, |
| 104 | * and enum machine_code_type definitions. |
| 105 | */ |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 106 | #include "clinic/arraymodule.c.h" |
| 107 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 108 | #define array_Check(op) PyObject_TypeCheck(op, &Arraytype) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 109 | #define array_CheckExact(op) (Py_TYPE(op) == &Arraytype) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 110 | |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 111 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 112 | array_resize(arrayobject *self, Py_ssize_t newsize) |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 113 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 114 | char *items; |
| 115 | size_t _new_size; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 116 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | if (self->ob_exports > 0 && newsize != Py_SIZE(self)) { |
| 118 | PyErr_SetString(PyExc_BufferError, |
| 119 | "cannot resize an array that is exporting buffers"); |
| 120 | return -1; |
| 121 | } |
Antoine Pitrou | 3ad3a0d | 2008-12-18 17:08:32 +0000 | [diff] [blame] | 122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 123 | /* Bypass realloc() when a previous overallocation is large enough |
| 124 | to accommodate the newsize. If the newsize is 16 smaller than the |
| 125 | current size, then proceed with the realloc() to shrink the array. |
| 126 | */ |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 127 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 128 | if (self->allocated >= newsize && |
| 129 | Py_SIZE(self) < newsize + 16 && |
| 130 | self->ob_item != NULL) { |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 131 | Py_SET_SIZE(self, newsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 132 | return 0; |
| 133 | } |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 134 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | if (newsize == 0) { |
| 136 | PyMem_FREE(self->ob_item); |
| 137 | self->ob_item = NULL; |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 138 | Py_SET_SIZE(self, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | self->allocated = 0; |
| 140 | return 0; |
| 141 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 142 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 143 | /* This over-allocates proportional to the array size, making room |
| 144 | * for additional growth. The over-allocation is mild, but is |
| 145 | * enough to give linear-time amortized behavior over a long |
| 146 | * sequence of appends() in the presence of a poorly-performing |
| 147 | * system realloc(). |
| 148 | * The growth pattern is: 0, 4, 8, 16, 25, 34, 46, 56, 67, 79, ... |
| 149 | * Note, the pattern starts out the same as for lists but then |
| 150 | * grows at a smaller rate so that larger arrays only overallocate |
| 151 | * by about 1/16th -- this is done because arrays are presumed to be more |
| 152 | * memory critical. |
| 153 | */ |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 155 | _new_size = (newsize >> 4) + (Py_SIZE(self) < 8 ? 3 : 7) + newsize; |
| 156 | items = self->ob_item; |
| 157 | /* XXX The following multiplication and division does not optimize away |
| 158 | like it does for lists since the size is not known at compile time */ |
| 159 | if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize)) |
| 160 | PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize)); |
| 161 | else |
| 162 | items = NULL; |
| 163 | if (items == NULL) { |
| 164 | PyErr_NoMemory(); |
| 165 | return -1; |
| 166 | } |
| 167 | self->ob_item = items; |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 168 | Py_SET_SIZE(self, newsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 169 | self->allocated = _new_size; |
| 170 | return 0; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 173 | /**************************************************************************** |
| 174 | Get and Set functions for each type. |
| 175 | A Get function takes an arrayobject* and an integer index, returning the |
| 176 | array value at that index wrapped in an appropriate PyObject*. |
| 177 | A Set function takes an arrayobject, integer index, and PyObject*; sets |
| 178 | the array value at that index to the raw C data extracted from the PyObject*, |
| 179 | and returns 0 if successful, else nonzero on failure (PyObject* not of an |
| 180 | appropriate type or value). |
| 181 | Note that the basic Get and Set functions do NOT check that the index is |
| 182 | in bounds; that's the responsibility of the caller. |
| 183 | ****************************************************************************/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 184 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 185 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 186 | b_getitem(arrayobject *ap, Py_ssize_t i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 187 | { |
Disconnect3d | 13ab570 | 2019-07-11 23:57:42 +0200 | [diff] [blame] | 188 | long x = ((signed char *)ap->ob_item)[i]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | return PyLong_FromLong(x); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 193 | b_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 194 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 195 | short x; |
| 196 | /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore |
| 197 | must use the next size up that is signed ('h') and manually do |
| 198 | the overflow checking */ |
| 199 | if (!PyArg_Parse(v, "h;array item must be integer", &x)) |
| 200 | return -1; |
| 201 | else if (x < -128) { |
| 202 | PyErr_SetString(PyExc_OverflowError, |
| 203 | "signed char is less than minimum"); |
| 204 | return -1; |
| 205 | } |
| 206 | else if (x > 127) { |
| 207 | PyErr_SetString(PyExc_OverflowError, |
| 208 | "signed char is greater than maximum"); |
| 209 | return -1; |
| 210 | } |
| 211 | if (i >= 0) |
| 212 | ((char *)ap->ob_item)[i] = (char)x; |
| 213 | return 0; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 216 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 217 | BB_getitem(arrayobject *ap, Py_ssize_t i) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 218 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 219 | long x = ((unsigned char *)ap->ob_item)[i]; |
| 220 | return PyLong_FromLong(x); |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 223 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 224 | BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 225 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 226 | unsigned char x; |
| 227 | /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */ |
| 228 | if (!PyArg_Parse(v, "b;array item must be integer", &x)) |
| 229 | return -1; |
| 230 | if (i >= 0) |
| 231 | ((char *)ap->ob_item)[i] = x; |
| 232 | return 0; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 233 | } |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 234 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 235 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 236 | u_getitem(arrayobject *ap, Py_ssize_t i) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 237 | { |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 238 | return PyUnicode_FromOrdinal(((Py_UNICODE *) ap->ob_item)[i]); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 242 | u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 243 | { |
Victor Stinner | 62bb394 | 2012-08-06 00:46:05 +0200 | [diff] [blame] | 244 | Py_UNICODE *p; |
| 245 | Py_ssize_t len; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 246 | |
Victor Stinner | 62bb394 | 2012-08-06 00:46:05 +0200 | [diff] [blame] | 247 | if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 248 | return -1; |
Victor Stinner | 62bb394 | 2012-08-06 00:46:05 +0200 | [diff] [blame] | 249 | if (len != 1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 | PyErr_SetString(PyExc_TypeError, |
| 251 | "array item must be unicode character"); |
| 252 | return -1; |
| 253 | } |
| 254 | if (i >= 0) |
Victor Stinner | 62bb394 | 2012-08-06 00:46:05 +0200 | [diff] [blame] | 255 | ((Py_UNICODE *)ap->ob_item)[i] = p[0]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 256 | return 0; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 257 | } |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 258 | |
Travis E. Oliphant | d5c0add | 2007-10-12 22:05:15 +0000 | [diff] [blame] | 259 | |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 260 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 261 | h_getitem(arrayobject *ap, Py_ssize_t i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 262 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 263 | return PyLong_FromLong((long) ((short *)ap->ob_item)[i]); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Travis E. Oliphant | d5c0add | 2007-10-12 22:05:15 +0000 | [diff] [blame] | 266 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 267 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 268 | h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 269 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 270 | short x; |
| 271 | /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */ |
| 272 | if (!PyArg_Parse(v, "h;array item must be integer", &x)) |
| 273 | return -1; |
| 274 | if (i >= 0) |
| 275 | ((short *)ap->ob_item)[i] = x; |
| 276 | return 0; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 279 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 280 | HH_getitem(arrayobject *ap, Py_ssize_t i) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 281 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 282 | return PyLong_FromLong((long) ((unsigned short *)ap->ob_item)[i]); |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 285 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 286 | HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 287 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 288 | int x; |
| 289 | /* PyArg_Parse's 'h' formatter is for a signed short, therefore |
| 290 | must use the next size up and manually do the overflow checking */ |
| 291 | if (!PyArg_Parse(v, "i;array item must be integer", &x)) |
| 292 | return -1; |
| 293 | else if (x < 0) { |
| 294 | PyErr_SetString(PyExc_OverflowError, |
| 295 | "unsigned short is less than minimum"); |
| 296 | return -1; |
| 297 | } |
| 298 | else if (x > USHRT_MAX) { |
| 299 | PyErr_SetString(PyExc_OverflowError, |
| 300 | "unsigned short is greater than maximum"); |
| 301 | return -1; |
| 302 | } |
| 303 | if (i >= 0) |
| 304 | ((short *)ap->ob_item)[i] = (short)x; |
| 305 | return 0; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 306 | } |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 307 | |
| 308 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 309 | i_getitem(arrayobject *ap, Py_ssize_t i) |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 310 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 311 | return PyLong_FromLong((long) ((int *)ap->ob_item)[i]); |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 315 | i_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 316 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 317 | int x; |
| 318 | /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */ |
| 319 | if (!PyArg_Parse(v, "i;array item must be integer", &x)) |
| 320 | return -1; |
| 321 | if (i >= 0) |
| 322 | ((int *)ap->ob_item)[i] = x; |
| 323 | return 0; |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 326 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 327 | II_getitem(arrayobject *ap, Py_ssize_t i) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 328 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | return PyLong_FromUnsignedLong( |
| 330 | (unsigned long) ((unsigned int *)ap->ob_item)[i]); |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 331 | } |
| 332 | |
orenmn | 964281a | 2017-03-09 11:35:28 +0200 | [diff] [blame] | 333 | static PyObject * |
| 334 | get_int_unless_float(PyObject *v) |
| 335 | { |
| 336 | if (PyFloat_Check(v)) { |
| 337 | PyErr_SetString(PyExc_TypeError, |
| 338 | "array item must be integer"); |
| 339 | return NULL; |
| 340 | } |
Serhiy Storchaka | 6a44f6e | 2019-02-25 17:57:58 +0200 | [diff] [blame] | 341 | return _PyLong_FromNbIndexOrNbInt(v); |
orenmn | 964281a | 2017-03-09 11:35:28 +0200 | [diff] [blame] | 342 | } |
| 343 | |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 344 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 345 | II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 346 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 347 | unsigned long x; |
orenmn | 964281a | 2017-03-09 11:35:28 +0200 | [diff] [blame] | 348 | int do_decref = 0; /* if nb_int was called */ |
| 349 | |
| 350 | if (!PyLong_Check(v)) { |
| 351 | v = get_int_unless_float(v); |
| 352 | if (NULL == v) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 353 | return -1; |
| 354 | } |
orenmn | 964281a | 2017-03-09 11:35:28 +0200 | [diff] [blame] | 355 | do_decref = 1; |
| 356 | } |
| 357 | x = PyLong_AsUnsignedLong(v); |
| 358 | if (x == (unsigned long)-1 && PyErr_Occurred()) { |
| 359 | if (do_decref) { |
| 360 | Py_DECREF(v); |
| 361 | } |
| 362 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 363 | } |
| 364 | if (x > UINT_MAX) { |
| 365 | PyErr_SetString(PyExc_OverflowError, |
orenmn | 964281a | 2017-03-09 11:35:28 +0200 | [diff] [blame] | 366 | "unsigned int is greater than maximum"); |
| 367 | if (do_decref) { |
| 368 | Py_DECREF(v); |
| 369 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | return -1; |
| 371 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 372 | if (i >= 0) |
| 373 | ((unsigned int *)ap->ob_item)[i] = (unsigned int)x; |
orenmn | 964281a | 2017-03-09 11:35:28 +0200 | [diff] [blame] | 374 | |
| 375 | if (do_decref) { |
| 376 | Py_DECREF(v); |
| 377 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 378 | return 0; |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 382 | l_getitem(arrayobject *ap, Py_ssize_t i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 383 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 384 | return PyLong_FromLong(((long *)ap->ob_item)[i]); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 388 | l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 389 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 390 | long x; |
| 391 | if (!PyArg_Parse(v, "l;array item must be integer", &x)) |
| 392 | return -1; |
| 393 | if (i >= 0) |
| 394 | ((long *)ap->ob_item)[i] = x; |
| 395 | return 0; |
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 * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 399 | LL_getitem(arrayobject *ap, Py_ssize_t i) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 400 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 401 | return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]); |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 405 | LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 406 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 407 | unsigned long x; |
orenmn | 964281a | 2017-03-09 11:35:28 +0200 | [diff] [blame] | 408 | int do_decref = 0; /* if nb_int was called */ |
| 409 | |
| 410 | if (!PyLong_Check(v)) { |
| 411 | v = get_int_unless_float(v); |
| 412 | if (NULL == v) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 413 | return -1; |
| 414 | } |
orenmn | 964281a | 2017-03-09 11:35:28 +0200 | [diff] [blame] | 415 | do_decref = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 416 | } |
orenmn | 964281a | 2017-03-09 11:35:28 +0200 | [diff] [blame] | 417 | x = PyLong_AsUnsignedLong(v); |
| 418 | if (x == (unsigned long)-1 && PyErr_Occurred()) { |
| 419 | if (do_decref) { |
| 420 | Py_DECREF(v); |
| 421 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 422 | return -1; |
| 423 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 424 | if (i >= 0) |
| 425 | ((unsigned long *)ap->ob_item)[i] = x; |
orenmn | 964281a | 2017-03-09 11:35:28 +0200 | [diff] [blame] | 426 | |
| 427 | if (do_decref) { |
| 428 | Py_DECREF(v); |
| 429 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 430 | return 0; |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 433 | static PyObject * |
| 434 | q_getitem(arrayobject *ap, Py_ssize_t i) |
| 435 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 436 | return PyLong_FromLongLong(((long long *)ap->ob_item)[i]); |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | static int |
| 440 | q_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
| 441 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 442 | long long x; |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 443 | if (!PyArg_Parse(v, "L;array item must be integer", &x)) |
| 444 | return -1; |
| 445 | if (i >= 0) |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 446 | ((long long *)ap->ob_item)[i] = x; |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | static PyObject * |
| 451 | QQ_getitem(arrayobject *ap, Py_ssize_t i) |
| 452 | { |
| 453 | return PyLong_FromUnsignedLongLong( |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 454 | ((unsigned long long *)ap->ob_item)[i]); |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | static int |
| 458 | QQ_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
| 459 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 460 | unsigned long long x; |
orenmn | 964281a | 2017-03-09 11:35:28 +0200 | [diff] [blame] | 461 | int do_decref = 0; /* if nb_int was called */ |
| 462 | |
| 463 | if (!PyLong_Check(v)) { |
| 464 | v = get_int_unless_float(v); |
| 465 | if (NULL == v) { |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 466 | return -1; |
| 467 | } |
orenmn | 964281a | 2017-03-09 11:35:28 +0200 | [diff] [blame] | 468 | do_decref = 1; |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 469 | } |
orenmn | 964281a | 2017-03-09 11:35:28 +0200 | [diff] [blame] | 470 | x = PyLong_AsUnsignedLongLong(v); |
| 471 | if (x == (unsigned long long)-1 && PyErr_Occurred()) { |
| 472 | if (do_decref) { |
| 473 | Py_DECREF(v); |
| 474 | } |
| 475 | return -1; |
| 476 | } |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 477 | if (i >= 0) |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 478 | ((unsigned long long *)ap->ob_item)[i] = x; |
orenmn | 964281a | 2017-03-09 11:35:28 +0200 | [diff] [blame] | 479 | |
| 480 | if (do_decref) { |
| 481 | Py_DECREF(v); |
| 482 | } |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 483 | return 0; |
| 484 | } |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 485 | |
Guido van Rossum | 549ab71 | 1997-01-03 19:09:47 +0000 | [diff] [blame] | 486 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 487 | f_getitem(arrayobject *ap, Py_ssize_t i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 488 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 489 | return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 493 | f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 494 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 495 | float x; |
| 496 | if (!PyArg_Parse(v, "f;array item must be float", &x)) |
| 497 | return -1; |
| 498 | if (i >= 0) |
| 499 | ((float *)ap->ob_item)[i] = x; |
| 500 | return 0; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 503 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 504 | d_getitem(arrayobject *ap, Py_ssize_t i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 505 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 506 | return PyFloat_FromDouble(((double *)ap->ob_item)[i]); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 510 | d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 511 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 | double x; |
| 513 | if (!PyArg_Parse(v, "d;array item must be float", &x)) |
| 514 | return -1; |
| 515 | if (i >= 0) |
| 516 | ((double *)ap->ob_item)[i] = x; |
| 517 | return 0; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Adrian Wielgosik | 7c17e23 | 2017-08-17 12:46:06 +0000 | [diff] [blame] | 520 | #define DEFINE_COMPAREITEMS(code, type) \ |
| 521 | static int \ |
| 522 | code##_compareitems(const void *lhs, const void *rhs, Py_ssize_t length) \ |
| 523 | { \ |
| 524 | const type *a = lhs, *b = rhs; \ |
| 525 | for (Py_ssize_t i = 0; i < length; ++i) \ |
| 526 | if (a[i] != b[i]) \ |
| 527 | return a[i] < b[i] ? -1 : 1; \ |
| 528 | return 0; \ |
| 529 | } |
| 530 | |
| 531 | DEFINE_COMPAREITEMS(b, signed char) |
| 532 | DEFINE_COMPAREITEMS(BB, unsigned char) |
| 533 | DEFINE_COMPAREITEMS(u, Py_UNICODE) |
| 534 | DEFINE_COMPAREITEMS(h, short) |
| 535 | DEFINE_COMPAREITEMS(HH, unsigned short) |
| 536 | DEFINE_COMPAREITEMS(i, int) |
| 537 | DEFINE_COMPAREITEMS(II, unsigned int) |
| 538 | DEFINE_COMPAREITEMS(l, long) |
| 539 | DEFINE_COMPAREITEMS(LL, unsigned long) |
| 540 | DEFINE_COMPAREITEMS(q, long long) |
| 541 | DEFINE_COMPAREITEMS(QQ, unsigned long long) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 542 | |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 543 | /* Description of types. |
| 544 | * |
| 545 | * Don't forget to update typecode_to_mformat_code() if you add a new |
| 546 | * typecode. |
| 547 | */ |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 548 | static const struct arraydescr descriptors[] = { |
Adrian Wielgosik | 7c17e23 | 2017-08-17 12:46:06 +0000 | [diff] [blame] | 549 | {'b', 1, b_getitem, b_setitem, b_compareitems, "b", 1, 1}, |
| 550 | {'B', 1, BB_getitem, BB_setitem, BB_compareitems, "B", 1, 0}, |
| 551 | {'u', sizeof(Py_UNICODE), u_getitem, u_setitem, u_compareitems, "u", 0, 0}, |
| 552 | {'h', sizeof(short), h_getitem, h_setitem, h_compareitems, "h", 1, 1}, |
| 553 | {'H', sizeof(short), HH_getitem, HH_setitem, HH_compareitems, "H", 1, 0}, |
| 554 | {'i', sizeof(int), i_getitem, i_setitem, i_compareitems, "i", 1, 1}, |
| 555 | {'I', sizeof(int), II_getitem, II_setitem, II_compareitems, "I", 1, 0}, |
| 556 | {'l', sizeof(long), l_getitem, l_setitem, l_compareitems, "l", 1, 1}, |
| 557 | {'L', sizeof(long), LL_getitem, LL_setitem, LL_compareitems, "L", 1, 0}, |
| 558 | {'q', sizeof(long long), q_getitem, q_setitem, q_compareitems, "q", 1, 1}, |
| 559 | {'Q', sizeof(long long), QQ_getitem, QQ_setitem, QQ_compareitems, "Q", 1, 0}, |
| 560 | {'f', sizeof(float), f_getitem, f_setitem, NULL, "f", 0, 0}, |
| 561 | {'d', sizeof(double), d_getitem, d_setitem, NULL, "d", 0, 0}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 562 | {'\0', 0, 0, 0, 0, 0, 0} /* Sentinel */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 563 | }; |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 564 | |
| 565 | /**************************************************************************** |
| 566 | Implementations of array object methods. |
| 567 | ****************************************************************************/ |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 568 | /*[clinic input] |
| 569 | class array.array "arrayobject *" "&Arraytype" |
| 570 | [clinic start generated code]*/ |
| 571 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=ad43d37e942a8854]*/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 572 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 573 | static PyObject * |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 574 | newarrayobject(PyTypeObject *type, Py_ssize_t size, const struct arraydescr *descr) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 575 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 576 | arrayobject *op; |
| 577 | size_t nbytes; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 578 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 579 | if (size < 0) { |
| 580 | PyErr_BadInternalCall(); |
| 581 | return NULL; |
| 582 | } |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 583 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 584 | /* Check for overflow */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 585 | if (size > PY_SSIZE_T_MAX / descr->itemsize) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 586 | return PyErr_NoMemory(); |
| 587 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 588 | nbytes = size * descr->itemsize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | op = (arrayobject *) type->tp_alloc(type, 0); |
| 590 | if (op == NULL) { |
| 591 | return NULL; |
| 592 | } |
| 593 | op->ob_descr = descr; |
| 594 | op->allocated = size; |
| 595 | op->weakreflist = NULL; |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 596 | Py_SET_SIZE(op, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 597 | if (size <= 0) { |
| 598 | op->ob_item = NULL; |
| 599 | } |
| 600 | else { |
| 601 | op->ob_item = PyMem_NEW(char, nbytes); |
| 602 | if (op->ob_item == NULL) { |
| 603 | Py_DECREF(op); |
| 604 | return PyErr_NoMemory(); |
| 605 | } |
| 606 | } |
| 607 | op->ob_exports = 0; |
| 608 | return (PyObject *) op; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 611 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 612 | getarrayitem(PyObject *op, Py_ssize_t i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 613 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 614 | arrayobject *ap; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 615 | assert(array_Check(op)); |
| 616 | ap = (arrayobject *)op; |
| 617 | assert(i>=0 && i<Py_SIZE(ap)); |
| 618 | return (*ap->ob_descr->getitem)(ap, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 622 | ins1(arrayobject *self, Py_ssize_t where, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 623 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 624 | char *items; |
| 625 | Py_ssize_t n = Py_SIZE(self); |
| 626 | if (v == NULL) { |
| 627 | PyErr_BadInternalCall(); |
| 628 | return -1; |
| 629 | } |
| 630 | if ((*self->ob_descr->setitem)(self, -1, v) < 0) |
| 631 | return -1; |
Raymond Hettinger | 6e2ee86 | 2004-03-14 04:37:50 +0000 | [diff] [blame] | 632 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 633 | if (array_resize(self, n+1) == -1) |
| 634 | return -1; |
| 635 | items = self->ob_item; |
| 636 | if (where < 0) { |
| 637 | where += n; |
| 638 | if (where < 0) |
| 639 | where = 0; |
| 640 | } |
| 641 | if (where > n) |
| 642 | where = n; |
| 643 | /* appends don't need to call memmove() */ |
| 644 | if (where != n) |
| 645 | memmove(items + (where+1)*self->ob_descr->itemsize, |
| 646 | items + where*self->ob_descr->itemsize, |
| 647 | (n-where)*self->ob_descr->itemsize); |
| 648 | return (*self->ob_descr->setitem)(self, where, v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 651 | /* Methods */ |
| 652 | |
| 653 | static void |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 654 | array_dealloc(arrayobject *op) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 655 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 656 | if (op->weakreflist != NULL) |
| 657 | PyObject_ClearWeakRefs((PyObject *) op); |
| 658 | if (op->ob_item != NULL) |
| 659 | PyMem_DEL(op->ob_item); |
| 660 | Py_TYPE(op)->tp_free((PyObject *)op); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 663 | static PyObject * |
| 664 | array_richcompare(PyObject *v, PyObject *w, int op) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 665 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 666 | arrayobject *va, *wa; |
| 667 | PyObject *vi = NULL; |
| 668 | PyObject *wi = NULL; |
| 669 | Py_ssize_t i, k; |
| 670 | PyObject *res; |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 671 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 672 | if (!array_Check(v) || !array_Check(w)) |
| 673 | Py_RETURN_NOTIMPLEMENTED; |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 674 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 675 | va = (arrayobject *)v; |
| 676 | wa = (arrayobject *)w; |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 677 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 678 | if (Py_SIZE(va) != Py_SIZE(wa) && (op == Py_EQ || op == Py_NE)) { |
| 679 | /* Shortcut: if the lengths differ, the arrays differ */ |
| 680 | if (op == Py_EQ) |
| 681 | res = Py_False; |
| 682 | else |
| 683 | res = Py_True; |
| 684 | Py_INCREF(res); |
| 685 | return res; |
| 686 | } |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 687 | |
Adrian Wielgosik | 7c17e23 | 2017-08-17 12:46:06 +0000 | [diff] [blame] | 688 | if (va->ob_descr == wa->ob_descr && va->ob_descr->compareitems != NULL) { |
| 689 | /* Fast path: |
| 690 | arrays with same types can have their buffers compared directly */ |
| 691 | Py_ssize_t common_length = Py_MIN(Py_SIZE(va), Py_SIZE(wa)); |
| 692 | int result = va->ob_descr->compareitems(va->ob_item, wa->ob_item, |
| 693 | common_length); |
| 694 | if (result == 0) |
| 695 | goto compare_sizes; |
| 696 | |
| 697 | int cmp; |
| 698 | switch (op) { |
| 699 | case Py_LT: cmp = result < 0; break; |
| 700 | case Py_LE: cmp = result <= 0; break; |
| 701 | case Py_EQ: cmp = result == 0; break; |
| 702 | case Py_NE: cmp = result != 0; break; |
| 703 | case Py_GT: cmp = result > 0; break; |
| 704 | case Py_GE: cmp = result >= 0; break; |
| 705 | default: return NULL; /* cannot happen */ |
| 706 | } |
| 707 | PyObject *res = cmp ? Py_True : Py_False; |
| 708 | Py_INCREF(res); |
| 709 | return res; |
| 710 | } |
| 711 | |
| 712 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 713 | /* Search for the first index where items are different */ |
| 714 | k = 1; |
| 715 | for (i = 0; i < Py_SIZE(va) && i < Py_SIZE(wa); i++) { |
| 716 | vi = getarrayitem(v, i); |
| 717 | wi = getarrayitem(w, i); |
| 718 | if (vi == NULL || wi == NULL) { |
| 719 | Py_XDECREF(vi); |
| 720 | Py_XDECREF(wi); |
| 721 | return NULL; |
| 722 | } |
| 723 | k = PyObject_RichCompareBool(vi, wi, Py_EQ); |
| 724 | if (k == 0) |
| 725 | break; /* Keeping vi and wi alive! */ |
| 726 | Py_DECREF(vi); |
| 727 | Py_DECREF(wi); |
| 728 | if (k < 0) |
| 729 | return NULL; |
| 730 | } |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 731 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 732 | if (k) { |
| 733 | /* No more items to compare -- compare sizes */ |
Adrian Wielgosik | 7c17e23 | 2017-08-17 12:46:06 +0000 | [diff] [blame] | 734 | compare_sizes: ; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 735 | Py_ssize_t vs = Py_SIZE(va); |
| 736 | Py_ssize_t ws = Py_SIZE(wa); |
| 737 | int cmp; |
| 738 | switch (op) { |
| 739 | case Py_LT: cmp = vs < ws; break; |
| 740 | case Py_LE: cmp = vs <= ws; break; |
Adrian Wielgosik | 7c17e23 | 2017-08-17 12:46:06 +0000 | [diff] [blame] | 741 | /* If the lengths were not equal, |
| 742 | the earlier fast-path check would have caught that. */ |
| 743 | case Py_EQ: assert(vs == ws); cmp = 1; break; |
| 744 | case Py_NE: assert(vs == ws); cmp = 0; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 745 | case Py_GT: cmp = vs > ws; break; |
| 746 | case Py_GE: cmp = vs >= ws; break; |
| 747 | default: return NULL; /* cannot happen */ |
| 748 | } |
| 749 | if (cmp) |
| 750 | res = Py_True; |
| 751 | else |
| 752 | res = Py_False; |
| 753 | Py_INCREF(res); |
| 754 | return res; |
| 755 | } |
Guido van Rossum | 9d19cb8 | 2001-01-18 01:02:55 +0000 | [diff] [blame] | 756 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 757 | /* We have an item that differs. First, shortcuts for EQ/NE */ |
| 758 | if (op == Py_EQ) { |
| 759 | Py_INCREF(Py_False); |
| 760 | res = Py_False; |
| 761 | } |
| 762 | else if (op == Py_NE) { |
| 763 | Py_INCREF(Py_True); |
| 764 | res = Py_True; |
| 765 | } |
| 766 | else { |
| 767 | /* Compare the final item again using the proper operator */ |
| 768 | res = PyObject_RichCompare(vi, wi, op); |
| 769 | } |
| 770 | Py_DECREF(vi); |
| 771 | Py_DECREF(wi); |
| 772 | return res; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 775 | static Py_ssize_t |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 776 | array_length(arrayobject *a) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 777 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 778 | return Py_SIZE(a); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 781 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 782 | array_item(arrayobject *a, Py_ssize_t i) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 783 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 784 | if (i < 0 || i >= Py_SIZE(a)) { |
| 785 | PyErr_SetString(PyExc_IndexError, "array index out of range"); |
| 786 | return NULL; |
| 787 | } |
| 788 | return getarrayitem((PyObject *)a, i); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 791 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 792 | array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 793 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 794 | arrayobject *np; |
| 795 | if (ilow < 0) |
| 796 | ilow = 0; |
| 797 | else if (ilow > Py_SIZE(a)) |
| 798 | ilow = Py_SIZE(a); |
| 799 | if (ihigh < 0) |
| 800 | ihigh = 0; |
| 801 | if (ihigh < ilow) |
| 802 | ihigh = ilow; |
| 803 | else if (ihigh > Py_SIZE(a)) |
| 804 | ihigh = Py_SIZE(a); |
| 805 | np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr); |
| 806 | if (np == NULL) |
| 807 | return NULL; |
Martin Panter | be8da9c | 2016-09-07 11:04:41 +0000 | [diff] [blame] | 808 | if (ihigh > ilow) { |
| 809 | memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize, |
| 810 | (ihigh-ilow) * a->ob_descr->itemsize); |
| 811 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 812 | return (PyObject *)np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 815 | |
| 816 | /*[clinic input] |
| 817 | array.array.__copy__ |
| 818 | |
| 819 | Return a copy of the array. |
| 820 | [clinic start generated code]*/ |
| 821 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 822 | static PyObject * |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 823 | array_array___copy___impl(arrayobject *self) |
| 824 | /*[clinic end generated code: output=dec7c3f925d9619e input=ad1ee5b086965f09]*/ |
Raymond Hettinger | 3aa82c0 | 2004-03-13 18:18:51 +0000 | [diff] [blame] | 825 | { |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 826 | return array_slice(self, 0, Py_SIZE(self)); |
Raymond Hettinger | 3aa82c0 | 2004-03-13 18:18:51 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 829 | /*[clinic input] |
| 830 | array.array.__deepcopy__ |
| 831 | |
| 832 | unused: object |
| 833 | / |
| 834 | |
| 835 | Return a copy of the array. |
| 836 | [clinic start generated code]*/ |
| 837 | |
| 838 | static PyObject * |
| 839 | array_array___deepcopy__(arrayobject *self, PyObject *unused) |
| 840 | /*[clinic end generated code: output=1ec748d8e14a9faa input=2405ecb4933748c4]*/ |
| 841 | { |
| 842 | return array_array___copy___impl(self); |
| 843 | } |
Raymond Hettinger | 3aa82c0 | 2004-03-13 18:18:51 +0000 | [diff] [blame] | 844 | |
| 845 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 846 | array_concat(arrayobject *a, PyObject *bb) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 847 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 848 | Py_ssize_t size; |
| 849 | arrayobject *np; |
| 850 | if (!array_Check(bb)) { |
| 851 | PyErr_Format(PyExc_TypeError, |
| 852 | "can only append array (not \"%.200s\") to array", |
| 853 | Py_TYPE(bb)->tp_name); |
| 854 | return NULL; |
| 855 | } |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 856 | #define b ((arrayobject *)bb) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 857 | if (a->ob_descr != b->ob_descr) { |
| 858 | PyErr_BadArgument(); |
| 859 | return NULL; |
| 860 | } |
| 861 | if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) { |
| 862 | return PyErr_NoMemory(); |
| 863 | } |
| 864 | size = Py_SIZE(a) + Py_SIZE(b); |
| 865 | np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr); |
| 866 | if (np == NULL) { |
| 867 | return NULL; |
| 868 | } |
Martin Panter | be8da9c | 2016-09-07 11:04:41 +0000 | [diff] [blame] | 869 | if (Py_SIZE(a) > 0) { |
| 870 | memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize); |
| 871 | } |
| 872 | if (Py_SIZE(b) > 0) { |
| 873 | memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize, |
| 874 | b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize); |
| 875 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 876 | return (PyObject *)np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 877 | #undef b |
| 878 | } |
| 879 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 880 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 881 | array_repeat(arrayobject *a, Py_ssize_t n) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 882 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 883 | Py_ssize_t size; |
| 884 | arrayobject *np; |
Georg Brandl | c29cc6a | 2010-12-04 11:02:04 +0000 | [diff] [blame] | 885 | Py_ssize_t oldbytes, newbytes; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | if (n < 0) |
| 887 | n = 0; |
| 888 | if ((Py_SIZE(a) != 0) && (n > PY_SSIZE_T_MAX / Py_SIZE(a))) { |
| 889 | return PyErr_NoMemory(); |
| 890 | } |
| 891 | size = Py_SIZE(a) * n; |
| 892 | np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr); |
| 893 | if (np == NULL) |
| 894 | return NULL; |
Martin Panter | be8da9c | 2016-09-07 11:04:41 +0000 | [diff] [blame] | 895 | if (size == 0) |
Georg Brandl | c29cc6a | 2010-12-04 11:02:04 +0000 | [diff] [blame] | 896 | return (PyObject *)np; |
| 897 | oldbytes = Py_SIZE(a) * a->ob_descr->itemsize; |
| 898 | newbytes = oldbytes * n; |
| 899 | /* this follows the code in unicode_repeat */ |
| 900 | if (oldbytes == 1) { |
| 901 | memset(np->ob_item, a->ob_item[0], newbytes); |
| 902 | } else { |
| 903 | Py_ssize_t done = oldbytes; |
Christian Heimes | f051e43 | 2016-09-13 20:22:02 +0200 | [diff] [blame] | 904 | memcpy(np->ob_item, a->ob_item, oldbytes); |
Georg Brandl | c29cc6a | 2010-12-04 11:02:04 +0000 | [diff] [blame] | 905 | while (done < newbytes) { |
| 906 | Py_ssize_t ncopy = (done <= newbytes-done) ? done : newbytes-done; |
Christian Heimes | f051e43 | 2016-09-13 20:22:02 +0200 | [diff] [blame] | 907 | memcpy(np->ob_item+done, np->ob_item, ncopy); |
Georg Brandl | c29cc6a | 2010-12-04 11:02:04 +0000 | [diff] [blame] | 908 | done += ncopy; |
| 909 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 910 | } |
Georg Brandl | c29cc6a | 2010-12-04 11:02:04 +0000 | [diff] [blame] | 911 | return (PyObject *)np; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | static int |
Martin Panter | 996d72b | 2016-07-25 02:21:14 +0000 | [diff] [blame] | 915 | array_del_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 916 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 917 | char *item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 918 | Py_ssize_t d; /* Change in size */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 919 | if (ilow < 0) |
| 920 | ilow = 0; |
| 921 | else if (ilow > Py_SIZE(a)) |
| 922 | ilow = Py_SIZE(a); |
| 923 | if (ihigh < 0) |
| 924 | ihigh = 0; |
| 925 | if (ihigh < ilow) |
| 926 | ihigh = ilow; |
| 927 | else if (ihigh > Py_SIZE(a)) |
| 928 | ihigh = Py_SIZE(a); |
| 929 | item = a->ob_item; |
Martin Panter | 996d72b | 2016-07-25 02:21:14 +0000 | [diff] [blame] | 930 | d = ihigh-ilow; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 931 | /* Issue #4509: If the array has exported buffers and the slice |
| 932 | assignment would change the size of the array, fail early to make |
| 933 | sure we don't modify it. */ |
| 934 | if (d != 0 && a->ob_exports > 0) { |
| 935 | PyErr_SetString(PyExc_BufferError, |
| 936 | "cannot resize an array that is exporting buffers"); |
| 937 | return -1; |
| 938 | } |
Martin Panter | 996d72b | 2016-07-25 02:21:14 +0000 | [diff] [blame] | 939 | if (d > 0) { /* Delete d items */ |
| 940 | memmove(item + (ihigh-d)*a->ob_descr->itemsize, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 941 | item + ihigh*a->ob_descr->itemsize, |
| 942 | (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize); |
Martin Panter | 996d72b | 2016-07-25 02:21:14 +0000 | [diff] [blame] | 943 | if (array_resize(a, Py_SIZE(a) - d) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 944 | return -1; |
| 945 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 946 | return 0; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 950 | array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 951 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 952 | if (i < 0 || i >= Py_SIZE(a)) { |
| 953 | PyErr_SetString(PyExc_IndexError, |
| 954 | "array assignment index out of range"); |
| 955 | return -1; |
| 956 | } |
| 957 | if (v == NULL) |
Martin Panter | 996d72b | 2016-07-25 02:21:14 +0000 | [diff] [blame] | 958 | return array_del_slice(a, i, i+1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 959 | return (*a->ob_descr->setitem)(a, i, v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 963 | setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 964 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 965 | assert(array_Check(a)); |
| 966 | return array_ass_item((arrayobject *)a, i, v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 969 | static int |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 970 | array_iter_extend(arrayobject *self, PyObject *bb) |
| 971 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 972 | PyObject *it, *v; |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 973 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 974 | it = PyObject_GetIter(bb); |
| 975 | if (it == NULL) |
| 976 | return -1; |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 977 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 978 | while ((v = PyIter_Next(it)) != NULL) { |
Mark Dickinson | 346f0af | 2010-08-06 09:36:57 +0000 | [diff] [blame] | 979 | if (ins1(self, Py_SIZE(self), v) != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 980 | Py_DECREF(v); |
| 981 | Py_DECREF(it); |
| 982 | return -1; |
| 983 | } |
| 984 | Py_DECREF(v); |
| 985 | } |
| 986 | Py_DECREF(it); |
| 987 | if (PyErr_Occurred()) |
| 988 | return -1; |
| 989 | return 0; |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | static int |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 993 | array_do_extend(arrayobject *self, PyObject *bb) |
| 994 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 995 | Py_ssize_t size, oldsize, bbsize; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 996 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 997 | if (!array_Check(bb)) |
| 998 | return array_iter_extend(self, bb); |
| 999 | #define b ((arrayobject *)bb) |
| 1000 | if (self->ob_descr != b->ob_descr) { |
| 1001 | PyErr_SetString(PyExc_TypeError, |
| 1002 | "can only extend with array of same kind"); |
| 1003 | return -1; |
| 1004 | } |
| 1005 | if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) || |
| 1006 | ((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) { |
| 1007 | PyErr_NoMemory(); |
| 1008 | return -1; |
| 1009 | } |
| 1010 | oldsize = Py_SIZE(self); |
| 1011 | /* Get the size of bb before resizing the array since bb could be self. */ |
| 1012 | bbsize = Py_SIZE(bb); |
| 1013 | size = oldsize + Py_SIZE(b); |
| 1014 | if (array_resize(self, size) == -1) |
| 1015 | return -1; |
Martin Panter | be8da9c | 2016-09-07 11:04:41 +0000 | [diff] [blame] | 1016 | if (bbsize > 0) { |
| 1017 | memcpy(self->ob_item + oldsize * self->ob_descr->itemsize, |
| 1018 | b->ob_item, bbsize * b->ob_descr->itemsize); |
| 1019 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1020 | |
| 1021 | return 0; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1022 | #undef b |
| 1023 | } |
| 1024 | |
| 1025 | static PyObject * |
| 1026 | array_inplace_concat(arrayobject *self, PyObject *bb) |
| 1027 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1028 | if (!array_Check(bb)) { |
| 1029 | PyErr_Format(PyExc_TypeError, |
| 1030 | "can only extend array with array (not \"%.200s\")", |
| 1031 | Py_TYPE(bb)->tp_name); |
| 1032 | return NULL; |
| 1033 | } |
| 1034 | if (array_do_extend(self, bb) == -1) |
| 1035 | return NULL; |
| 1036 | Py_INCREF(self); |
| 1037 | return (PyObject *)self; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1041 | array_inplace_repeat(arrayobject *self, Py_ssize_t n) |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1042 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1043 | char *items, *p; |
| 1044 | Py_ssize_t size, i; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1045 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1046 | if (Py_SIZE(self) > 0) { |
| 1047 | if (n < 0) |
| 1048 | n = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | if ((self->ob_descr->itemsize != 0) && |
| 1050 | (Py_SIZE(self) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) { |
| 1051 | return PyErr_NoMemory(); |
| 1052 | } |
| 1053 | size = Py_SIZE(self) * self->ob_descr->itemsize; |
| 1054 | if (n > 0 && size > PY_SSIZE_T_MAX / n) { |
| 1055 | return PyErr_NoMemory(); |
| 1056 | } |
| 1057 | if (array_resize(self, n * Py_SIZE(self)) == -1) |
| 1058 | return NULL; |
| 1059 | items = p = self->ob_item; |
| 1060 | for (i = 1; i < n; i++) { |
| 1061 | p += size; |
| 1062 | memcpy(p, items, size); |
| 1063 | } |
| 1064 | } |
| 1065 | Py_INCREF(self); |
| 1066 | return (PyObject *)self; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1070 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1071 | ins(arrayobject *self, Py_ssize_t where, PyObject *v) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1072 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1073 | if (ins1(self, where, v) != 0) |
| 1074 | return NULL; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1075 | Py_RETURN_NONE; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1078 | /*[clinic input] |
| 1079 | array.array.count |
| 1080 | |
| 1081 | v: object |
| 1082 | / |
| 1083 | |
| 1084 | Return number of occurrences of v in the array. |
| 1085 | [clinic start generated code]*/ |
| 1086 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1087 | static PyObject * |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1088 | array_array_count(arrayobject *self, PyObject *v) |
| 1089 | /*[clinic end generated code: output=3dd3624bf7135a3a input=d9bce9d65e39d1f5]*/ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1090 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1091 | Py_ssize_t count = 0; |
| 1092 | Py_ssize_t i; |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1093 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1094 | for (i = 0; i < Py_SIZE(self); i++) { |
Victor Stinner | 0b142e2 | 2013-07-17 23:01:30 +0200 | [diff] [blame] | 1095 | PyObject *selfi; |
| 1096 | int cmp; |
| 1097 | |
| 1098 | selfi = getarrayitem((PyObject *)self, i); |
| 1099 | if (selfi == NULL) |
| 1100 | return NULL; |
| 1101 | cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1102 | Py_DECREF(selfi); |
| 1103 | if (cmp > 0) |
| 1104 | count++; |
| 1105 | else if (cmp < 0) |
| 1106 | return NULL; |
| 1107 | } |
| 1108 | return PyLong_FromSsize_t(count); |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1111 | |
| 1112 | /*[clinic input] |
| 1113 | array.array.index |
| 1114 | |
| 1115 | v: object |
| 1116 | / |
| 1117 | |
| 1118 | Return index of first occurrence of v in the array. |
| 1119 | [clinic start generated code]*/ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1120 | |
| 1121 | static PyObject * |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1122 | array_array_index(arrayobject *self, PyObject *v) |
| 1123 | /*[clinic end generated code: output=d48498d325602167 input=cf619898c6649d08]*/ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1124 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1125 | Py_ssize_t i; |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1126 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1127 | for (i = 0; i < Py_SIZE(self); i++) { |
Victor Stinner | 0b142e2 | 2013-07-17 23:01:30 +0200 | [diff] [blame] | 1128 | PyObject *selfi; |
| 1129 | int cmp; |
| 1130 | |
| 1131 | selfi = getarrayitem((PyObject *)self, i); |
| 1132 | if (selfi == NULL) |
| 1133 | return NULL; |
| 1134 | cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1135 | Py_DECREF(selfi); |
| 1136 | if (cmp > 0) { |
| 1137 | return PyLong_FromLong((long)i); |
| 1138 | } |
| 1139 | else if (cmp < 0) |
| 1140 | return NULL; |
| 1141 | } |
Jim Fasarakis-Hilliard | a4095ef | 2017-05-29 20:43:39 +0300 | [diff] [blame] | 1142 | PyErr_SetString(PyExc_ValueError, "array.index(x): x not in array"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1143 | return NULL; |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 1146 | static int |
| 1147 | array_contains(arrayobject *self, PyObject *v) |
| 1148 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1149 | Py_ssize_t i; |
| 1150 | int cmp; |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 1151 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1152 | for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) { |
| 1153 | PyObject *selfi = getarrayitem((PyObject *)self, i); |
Victor Stinner | 0b142e2 | 2013-07-17 23:01:30 +0200 | [diff] [blame] | 1154 | if (selfi == NULL) |
Victor Stinner | 4755bea | 2013-07-18 01:12:35 +0200 | [diff] [blame] | 1155 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1156 | cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
| 1157 | Py_DECREF(selfi); |
| 1158 | } |
| 1159 | return cmp; |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1162 | /*[clinic input] |
| 1163 | array.array.remove |
| 1164 | |
| 1165 | v: object |
| 1166 | / |
| 1167 | |
| 1168 | Remove the first occurrence of v in the array. |
| 1169 | [clinic start generated code]*/ |
| 1170 | |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1171 | static PyObject * |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1172 | array_array_remove(arrayobject *self, PyObject *v) |
| 1173 | /*[clinic end generated code: output=bef06be9fdf9dceb input=0b1e5aed25590027]*/ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1174 | { |
sth | aa3ecb8 | 2019-03-20 20:49:39 +0100 | [diff] [blame] | 1175 | Py_ssize_t i; |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1177 | for (i = 0; i < Py_SIZE(self); i++) { |
Victor Stinner | 0b142e2 | 2013-07-17 23:01:30 +0200 | [diff] [blame] | 1178 | PyObject *selfi; |
| 1179 | int cmp; |
| 1180 | |
| 1181 | selfi = getarrayitem((PyObject *)self,i); |
| 1182 | if (selfi == NULL) |
| 1183 | return NULL; |
| 1184 | cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1185 | Py_DECREF(selfi); |
| 1186 | if (cmp > 0) { |
Martin Panter | 996d72b | 2016-07-25 02:21:14 +0000 | [diff] [blame] | 1187 | if (array_del_slice(self, i, i+1) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1188 | return NULL; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1189 | Py_RETURN_NONE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1190 | } |
| 1191 | else if (cmp < 0) |
| 1192 | return NULL; |
| 1193 | } |
Jim Fasarakis-Hilliard | a4095ef | 2017-05-29 20:43:39 +0300 | [diff] [blame] | 1194 | PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in array"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1195 | return NULL; |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1198 | /*[clinic input] |
| 1199 | array.array.pop |
| 1200 | |
| 1201 | i: Py_ssize_t = -1 |
| 1202 | / |
| 1203 | |
| 1204 | Return the i-th element and delete it from the array. |
| 1205 | |
| 1206 | i defaults to -1. |
| 1207 | [clinic start generated code]*/ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1208 | |
| 1209 | static PyObject * |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1210 | array_array_pop_impl(arrayobject *self, Py_ssize_t i) |
| 1211 | /*[clinic end generated code: output=bc1f0c54fe5308e4 input=8e5feb4c1a11cd44]*/ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1212 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1213 | PyObject *v; |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1214 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1215 | if (Py_SIZE(self) == 0) { |
| 1216 | /* Special-case most common failure cause */ |
| 1217 | PyErr_SetString(PyExc_IndexError, "pop from empty array"); |
| 1218 | return NULL; |
| 1219 | } |
| 1220 | if (i < 0) |
| 1221 | i += Py_SIZE(self); |
| 1222 | if (i < 0 || i >= Py_SIZE(self)) { |
| 1223 | PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
| 1224 | return NULL; |
| 1225 | } |
Victor Stinner | 0b142e2 | 2013-07-17 23:01:30 +0200 | [diff] [blame] | 1226 | v = getarrayitem((PyObject *)self, i); |
| 1227 | if (v == NULL) |
| 1228 | return NULL; |
Martin Panter | 996d72b | 2016-07-25 02:21:14 +0000 | [diff] [blame] | 1229 | if (array_del_slice(self, i, i+1) != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | Py_DECREF(v); |
| 1231 | return NULL; |
| 1232 | } |
| 1233 | return v; |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1236 | /*[clinic input] |
| 1237 | array.array.extend |
| 1238 | |
| 1239 | bb: object |
| 1240 | / |
| 1241 | |
| 1242 | Append items to the end of the array. |
| 1243 | [clinic start generated code]*/ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1244 | |
| 1245 | static PyObject * |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1246 | array_array_extend(arrayobject *self, PyObject *bb) |
| 1247 | /*[clinic end generated code: output=bbddbc8e8bef871d input=43be86aba5c31e44]*/ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1248 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1249 | if (array_do_extend(self, bb) == -1) |
| 1250 | return NULL; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1251 | Py_RETURN_NONE; |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1254 | /*[clinic input] |
| 1255 | array.array.insert |
| 1256 | |
| 1257 | i: Py_ssize_t |
| 1258 | v: object |
| 1259 | / |
| 1260 | |
| 1261 | Insert a new item v into the array before position i. |
| 1262 | [clinic start generated code]*/ |
Peter Schneider-Kamp | 5a65c2d | 2000-07-31 20:52:21 +0000 | [diff] [blame] | 1263 | |
| 1264 | static PyObject * |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1265 | array_array_insert_impl(arrayobject *self, Py_ssize_t i, PyObject *v) |
| 1266 | /*[clinic end generated code: output=5a3648e278348564 input=5577d1b4383e9313]*/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1267 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1268 | return ins(self, i, v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1271 | /*[clinic input] |
| 1272 | array.array.buffer_info |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1273 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1274 | Return a tuple (address, length) giving the current memory address and the length in items of the buffer used to hold array's contents. |
| 1275 | |
| 1276 | The length should be multiplied by the itemsize attribute to calculate |
| 1277 | the buffer length in bytes. |
| 1278 | [clinic start generated code]*/ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1279 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1280 | static PyObject * |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1281 | array_array_buffer_info_impl(arrayobject *self) |
| 1282 | /*[clinic end generated code: output=9b2a4ec3ae7e98e7 input=a58bae5c6e1ac6a6]*/ |
Guido van Rossum | de4a4ca | 1997-08-12 14:55:56 +0000 | [diff] [blame] | 1283 | { |
Victor Stinner | 541067a | 2013-11-14 01:27:12 +0100 | [diff] [blame] | 1284 | PyObject *retval = NULL, *v; |
| 1285 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1286 | retval = PyTuple_New(2); |
| 1287 | if (!retval) |
| 1288 | return NULL; |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 1289 | |
Victor Stinner | 541067a | 2013-11-14 01:27:12 +0100 | [diff] [blame] | 1290 | v = PyLong_FromVoidPtr(self->ob_item); |
| 1291 | if (v == NULL) { |
| 1292 | Py_DECREF(retval); |
| 1293 | return NULL; |
| 1294 | } |
| 1295 | PyTuple_SET_ITEM(retval, 0, v); |
| 1296 | |
Serhiy Storchaka | 9e941d6 | 2016-06-23 23:55:34 +0300 | [diff] [blame] | 1297 | v = PyLong_FromSsize_t(Py_SIZE(self)); |
Victor Stinner | 541067a | 2013-11-14 01:27:12 +0100 | [diff] [blame] | 1298 | if (v == NULL) { |
| 1299 | Py_DECREF(retval); |
| 1300 | return NULL; |
| 1301 | } |
| 1302 | PyTuple_SET_ITEM(retval, 1, v); |
Fred Drake | 541dc3b | 2000-06-28 17:49:30 +0000 | [diff] [blame] | 1303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1304 | return retval; |
Guido van Rossum | de4a4ca | 1997-08-12 14:55:56 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1307 | /*[clinic input] |
| 1308 | array.array.append |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1309 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1310 | v: object |
| 1311 | / |
| 1312 | |
| 1313 | Append new value v to the end of the array. |
| 1314 | [clinic start generated code]*/ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1315 | |
Guido van Rossum | de4a4ca | 1997-08-12 14:55:56 +0000 | [diff] [blame] | 1316 | static PyObject * |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1317 | array_array_append(arrayobject *self, PyObject *v) |
| 1318 | /*[clinic end generated code: output=745a0669bf8db0e2 input=0b98d9d78e78f0fa]*/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1319 | { |
Mark Dickinson | 346f0af | 2010-08-06 09:36:57 +0000 | [diff] [blame] | 1320 | return ins(self, Py_SIZE(self), v); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1323 | /*[clinic input] |
| 1324 | array.array.byteswap |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1325 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1326 | Byteswap all items of the array. |
| 1327 | |
| 1328 | If the items in the array are not 1, 2, 4, or 8 bytes in size, RuntimeError is |
| 1329 | raised. |
| 1330 | [clinic start generated code]*/ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1331 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1332 | static PyObject * |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1333 | array_array_byteswap_impl(arrayobject *self) |
| 1334 | /*[clinic end generated code: output=5f8236cbdf0d90b5 input=6a85591b950a0186]*/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1335 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1336 | char *p; |
| 1337 | Py_ssize_t i; |
Fred Drake | bf27298 | 1999-12-03 17:15:30 +0000 | [diff] [blame] | 1338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1339 | switch (self->ob_descr->itemsize) { |
| 1340 | case 1: |
| 1341 | break; |
| 1342 | case 2: |
| 1343 | for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) { |
| 1344 | char p0 = p[0]; |
| 1345 | p[0] = p[1]; |
| 1346 | p[1] = p0; |
| 1347 | } |
| 1348 | break; |
| 1349 | case 4: |
| 1350 | for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 4) { |
| 1351 | char p0 = p[0]; |
| 1352 | char p1 = p[1]; |
| 1353 | p[0] = p[3]; |
| 1354 | p[1] = p[2]; |
| 1355 | p[2] = p1; |
| 1356 | p[3] = p0; |
| 1357 | } |
| 1358 | break; |
| 1359 | case 8: |
| 1360 | for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) { |
| 1361 | char p0 = p[0]; |
| 1362 | char p1 = p[1]; |
| 1363 | char p2 = p[2]; |
| 1364 | char p3 = p[3]; |
| 1365 | p[0] = p[7]; |
| 1366 | p[1] = p[6]; |
| 1367 | p[2] = p[5]; |
| 1368 | p[3] = p[4]; |
| 1369 | p[4] = p3; |
| 1370 | p[5] = p2; |
| 1371 | p[6] = p1; |
| 1372 | p[7] = p0; |
| 1373 | } |
| 1374 | break; |
| 1375 | default: |
| 1376 | PyErr_SetString(PyExc_RuntimeError, |
| 1377 | "don't know how to byteswap this array type"); |
| 1378 | return NULL; |
| 1379 | } |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1380 | Py_RETURN_NONE; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1383 | /*[clinic input] |
| 1384 | array.array.reverse |
| 1385 | |
| 1386 | Reverse the order of the items in the array. |
| 1387 | [clinic start generated code]*/ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1388 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1389 | static PyObject * |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1390 | array_array_reverse_impl(arrayobject *self) |
| 1391 | /*[clinic end generated code: output=c04868b36f6f4089 input=cd904f01b27d966a]*/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1392 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1393 | Py_ssize_t itemsize = self->ob_descr->itemsize; |
| 1394 | char *p, *q; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1395 | /* little buffer to hold items while swapping */ |
| 1396 | char tmp[256]; /* 8 is probably enough -- but why skimp */ |
| 1397 | assert((size_t)itemsize <= sizeof(tmp)); |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 1398 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1399 | if (Py_SIZE(self) > 1) { |
| 1400 | for (p = self->ob_item, |
| 1401 | q = self->ob_item + (Py_SIZE(self) - 1)*itemsize; |
| 1402 | p < q; |
| 1403 | p += itemsize, q -= itemsize) { |
| 1404 | /* memory areas guaranteed disjoint, so memcpy |
| 1405 | * is safe (& memmove may be slower). |
| 1406 | */ |
| 1407 | memcpy(tmp, p, itemsize); |
| 1408 | memcpy(p, q, itemsize); |
| 1409 | memcpy(q, tmp, itemsize); |
| 1410 | } |
| 1411 | } |
Tim Peters | bb30734 | 2000-09-10 05:22:54 +0000 | [diff] [blame] | 1412 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1413 | Py_RETURN_NONE; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1414 | } |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 1415 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1416 | /*[clinic input] |
| 1417 | array.array.fromfile |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1418 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1419 | f: object |
| 1420 | n: Py_ssize_t |
| 1421 | / |
Guido van Rossum | 2c94aa5 | 2007-05-24 19:02:32 +0000 | [diff] [blame] | 1422 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1423 | Read n objects from the file object f and append them to the end of the array. |
| 1424 | [clinic start generated code]*/ |
Guido van Rossum | 2c94aa5 | 2007-05-24 19:02:32 +0000 | [diff] [blame] | 1425 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1426 | static PyObject * |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1427 | array_array_fromfile_impl(arrayobject *self, PyObject *f, Py_ssize_t n) |
| 1428 | /*[clinic end generated code: output=ec9f600e10f53510 input=e188afe8e58adf40]*/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1429 | { |
Serhiy Storchaka | 04e6dba | 2015-04-04 17:06:55 +0300 | [diff] [blame] | 1430 | PyObject *b, *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1431 | Py_ssize_t itemsize = self->ob_descr->itemsize; |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1432 | Py_ssize_t nbytes; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1433 | _Py_IDENTIFIER(read); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1434 | int not_enough_bytes; |
Guido van Rossum | 2c94aa5 | 2007-05-24 19:02:32 +0000 | [diff] [blame] | 1435 | |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 1436 | if (n < 0) { |
| 1437 | PyErr_SetString(PyExc_ValueError, "negative count"); |
| 1438 | return NULL; |
| 1439 | } |
| 1440 | if (n > PY_SSIZE_T_MAX / itemsize) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1441 | PyErr_NoMemory(); |
| 1442 | return NULL; |
| 1443 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 1444 | nbytes = n * itemsize; |
Guido van Rossum | 2c94aa5 | 2007-05-24 19:02:32 +0000 | [diff] [blame] | 1445 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1446 | b = _PyObject_CallMethodId(f, &PyId_read, "n", nbytes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1447 | if (b == NULL) |
| 1448 | return NULL; |
Guido van Rossum | 2c94aa5 | 2007-05-24 19:02:32 +0000 | [diff] [blame] | 1449 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1450 | if (!PyBytes_Check(b)) { |
| 1451 | PyErr_SetString(PyExc_TypeError, |
| 1452 | "read() didn't return bytes"); |
| 1453 | Py_DECREF(b); |
| 1454 | return NULL; |
| 1455 | } |
Guido van Rossum | 2c94aa5 | 2007-05-24 19:02:32 +0000 | [diff] [blame] | 1456 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1457 | not_enough_bytes = (PyBytes_GET_SIZE(b) != nbytes); |
Guido van Rossum | 2c94aa5 | 2007-05-24 19:02:32 +0000 | [diff] [blame] | 1458 | |
Serhiy Storchaka | 04e6dba | 2015-04-04 17:06:55 +0300 | [diff] [blame] | 1459 | res = array_array_frombytes(self, b); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1460 | Py_DECREF(b); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1461 | if (res == NULL) |
| 1462 | return NULL; |
Hirokazu Yamamoto | 54d0df6 | 2009-03-06 03:04:07 +0000 | [diff] [blame] | 1463 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1464 | if (not_enough_bytes) { |
| 1465 | PyErr_SetString(PyExc_EOFError, |
| 1466 | "read() didn't return enough bytes"); |
| 1467 | Py_DECREF(res); |
| 1468 | return NULL; |
| 1469 | } |
Guido van Rossum | 2c94aa5 | 2007-05-24 19:02:32 +0000 | [diff] [blame] | 1470 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1471 | return res; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1474 | /*[clinic input] |
| 1475 | array.array.tofile |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1476 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1477 | f: object |
| 1478 | / |
| 1479 | |
| 1480 | Write all items (as machine values) to the file object f. |
| 1481 | [clinic start generated code]*/ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1482 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1483 | static PyObject * |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1484 | array_array_tofile(arrayobject *self, PyObject *f) |
| 1485 | /*[clinic end generated code: output=3a2cfa8128df0777 input=b0669a484aab0831]*/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1486 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1487 | Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize; |
| 1488 | /* Write 64K blocks at a time */ |
| 1489 | /* XXX Make the block size settable */ |
| 1490 | int BLOCKSIZE = 64*1024; |
| 1491 | Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE; |
| 1492 | Py_ssize_t i; |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1494 | if (Py_SIZE(self) == 0) |
| 1495 | goto done; |
Guido van Rossum | b5ddcfd | 2007-04-11 17:08:28 +0000 | [diff] [blame] | 1496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1497 | for (i = 0; i < nblocks; i++) { |
| 1498 | char* ptr = self->ob_item + i*BLOCKSIZE; |
| 1499 | Py_ssize_t size = BLOCKSIZE; |
| 1500 | PyObject *bytes, *res; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1501 | _Py_IDENTIFIER(write); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1502 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1503 | if (i*BLOCKSIZE + size > nbytes) |
| 1504 | size = nbytes - i*BLOCKSIZE; |
| 1505 | bytes = PyBytes_FromStringAndSize(ptr, size); |
| 1506 | if (bytes == NULL) |
| 1507 | return NULL; |
Jeroen Demeyer | 59ad110 | 2019-07-11 10:59:05 +0200 | [diff] [blame] | 1508 | res = _PyObject_CallMethodIdOneArg(f, &PyId_write, bytes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1509 | Py_DECREF(bytes); |
| 1510 | if (res == NULL) |
| 1511 | return NULL; |
| 1512 | Py_DECREF(res); /* drop write result */ |
| 1513 | } |
Guido van Rossum | b5ddcfd | 2007-04-11 17:08:28 +0000 | [diff] [blame] | 1514 | |
| 1515 | done: |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1516 | Py_RETURN_NONE; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1519 | /*[clinic input] |
| 1520 | array.array.fromlist |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1521 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1522 | list: object |
| 1523 | / |
| 1524 | |
| 1525 | Append items to array from list. |
| 1526 | [clinic start generated code]*/ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1527 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1528 | static PyObject * |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1529 | array_array_fromlist(arrayobject *self, PyObject *list) |
| 1530 | /*[clinic end generated code: output=26411c2d228a3e3f input=be2605a96c49680f]*/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1531 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1532 | Py_ssize_t n; |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1533 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1534 | if (!PyList_Check(list)) { |
| 1535 | PyErr_SetString(PyExc_TypeError, "arg must be list"); |
| 1536 | return NULL; |
| 1537 | } |
| 1538 | n = PyList_Size(list); |
| 1539 | if (n > 0) { |
| 1540 | Py_ssize_t i, old_size; |
| 1541 | old_size = Py_SIZE(self); |
| 1542 | if (array_resize(self, old_size + n) == -1) |
| 1543 | return NULL; |
| 1544 | for (i = 0; i < n; i++) { |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 1545 | PyObject *v = PyList_GET_ITEM(list, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1546 | if ((*self->ob_descr->setitem)(self, |
| 1547 | Py_SIZE(self) - n + i, v) != 0) { |
| 1548 | array_resize(self, old_size); |
| 1549 | return NULL; |
| 1550 | } |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 1551 | if (n != PyList_GET_SIZE(list)) { |
| 1552 | PyErr_SetString(PyExc_RuntimeError, |
| 1553 | "list changed size during iteration"); |
| 1554 | array_resize(self, old_size); |
| 1555 | return NULL; |
| 1556 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1557 | } |
| 1558 | } |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1559 | Py_RETURN_NONE; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1562 | /*[clinic input] |
| 1563 | array.array.tolist |
| 1564 | |
| 1565 | Convert array to an ordinary list with the same items. |
| 1566 | [clinic start generated code]*/ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1567 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1568 | static PyObject * |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1569 | array_array_tolist_impl(arrayobject *self) |
| 1570 | /*[clinic end generated code: output=00b60cc9eab8ef89 input=a8d7784a94f86b53]*/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1571 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1572 | PyObject *list = PyList_New(Py_SIZE(self)); |
| 1573 | Py_ssize_t i; |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 1574 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1575 | if (list == NULL) |
| 1576 | return NULL; |
| 1577 | for (i = 0; i < Py_SIZE(self); i++) { |
| 1578 | PyObject *v = getarrayitem((PyObject *)self, i); |
Victor Stinner | 4755bea | 2013-07-18 01:12:35 +0200 | [diff] [blame] | 1579 | if (v == NULL) |
| 1580 | goto error; |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 1581 | PyList_SET_ITEM(list, i, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1582 | } |
| 1583 | return list; |
Victor Stinner | 4755bea | 2013-07-18 01:12:35 +0200 | [diff] [blame] | 1584 | |
| 1585 | error: |
| 1586 | Py_DECREF(list); |
| 1587 | return NULL; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1590 | static PyObject * |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1591 | frombytes(arrayobject *self, Py_buffer *buffer) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1592 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1593 | int itemsize = self->ob_descr->itemsize; |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1594 | Py_ssize_t n; |
| 1595 | if (buffer->itemsize != 1) { |
| 1596 | PyBuffer_Release(buffer); |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 1597 | PyErr_SetString(PyExc_TypeError, "a bytes-like object is required"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1598 | return NULL; |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1599 | } |
| 1600 | n = buffer->len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1601 | if (n % itemsize != 0) { |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1602 | PyBuffer_Release(buffer); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1603 | PyErr_SetString(PyExc_ValueError, |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 1604 | "bytes length not a multiple of item size"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1605 | return NULL; |
| 1606 | } |
| 1607 | n = n / itemsize; |
| 1608 | if (n > 0) { |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1609 | Py_ssize_t old_size = Py_SIZE(self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1610 | if ((n > PY_SSIZE_T_MAX - old_size) || |
| 1611 | ((old_size + n) > PY_SSIZE_T_MAX / itemsize)) { |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1612 | PyBuffer_Release(buffer); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1613 | return PyErr_NoMemory(); |
| 1614 | } |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1615 | if (array_resize(self, old_size + n) == -1) { |
| 1616 | PyBuffer_Release(buffer); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1617 | return NULL; |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1618 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1619 | memcpy(self->ob_item + old_size * itemsize, |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1620 | buffer->buf, n * itemsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1621 | } |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1622 | PyBuffer_Release(buffer); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1623 | Py_RETURN_NONE; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1626 | /*[clinic input] |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1627 | array.array.frombytes |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1628 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1629 | buffer: Py_buffer |
| 1630 | / |
| 1631 | |
| 1632 | Appends items from the string, interpreting it as an array of machine values, as if it had been read from a file using the fromfile() method). |
| 1633 | [clinic start generated code]*/ |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1634 | |
| 1635 | static PyObject * |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1636 | array_array_frombytes_impl(arrayobject *self, Py_buffer *buffer) |
| 1637 | /*[clinic end generated code: output=d9842c8f7510a516 input=2bbf2b53ebfcc988]*/ |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1638 | { |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1639 | return frombytes(self, buffer); |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1642 | /*[clinic input] |
| 1643 | array.array.tobytes |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1644 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1645 | Convert the array to an array of machine values and return the bytes representation. |
| 1646 | [clinic start generated code]*/ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 1647 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 1648 | static PyObject * |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1649 | array_array_tobytes_impl(arrayobject *self) |
| 1650 | /*[clinic end generated code: output=87318e4edcdc2bb6 input=90ee495f96de34f5]*/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1651 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1652 | if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) { |
| 1653 | return PyBytes_FromStringAndSize(self->ob_item, |
| 1654 | Py_SIZE(self) * self->ob_descr->itemsize); |
| 1655 | } else { |
| 1656 | return PyErr_NoMemory(); |
| 1657 | } |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1660 | /*[clinic input] |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1661 | array.array.fromunicode |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 1662 | |
Larry Hastings | 38337d1 | 2015-05-07 23:30:09 -0700 | [diff] [blame] | 1663 | ustr: Py_UNICODE(zeroes=True) |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1664 | / |
| 1665 | |
| 1666 | Extends this array with data from the unicode string ustr. |
| 1667 | |
| 1668 | The array must be a unicode type array; otherwise a ValueError is raised. |
| 1669 | Use array.frombytes(ustr.encode(...)) to append Unicode data to an array of |
| 1670 | some other type. |
| 1671 | [clinic start generated code]*/ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1672 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1673 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1674 | array_array_fromunicode_impl(arrayobject *self, const Py_UNICODE *ustr, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1675 | Py_ssize_clean_t ustr_length) |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1676 | /*[clinic end generated code: output=cf2f662908e2befc input=150f00566ffbca6e]*/ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1677 | { |
Victor Stinner | 62bb394 | 2012-08-06 00:46:05 +0200 | [diff] [blame] | 1678 | char typecode; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1679 | |
Victor Stinner | 62bb394 | 2012-08-06 00:46:05 +0200 | [diff] [blame] | 1680 | typecode = self->ob_descr->typecode; |
Gregory P. Smith | 9504b13 | 2012-12-10 20:20:20 -0800 | [diff] [blame] | 1681 | if (typecode != 'u') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1682 | PyErr_SetString(PyExc_ValueError, |
| 1683 | "fromunicode() may only be called on " |
| 1684 | "unicode type arrays"); |
| 1685 | return NULL; |
| 1686 | } |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1687 | if (ustr_length > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1688 | Py_ssize_t old_size = Py_SIZE(self); |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1689 | if (array_resize(self, old_size + ustr_length) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1690 | return NULL; |
Victor Stinner | 62bb394 | 2012-08-06 00:46:05 +0200 | [diff] [blame] | 1691 | memcpy(self->ob_item + old_size * sizeof(Py_UNICODE), |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1692 | ustr, ustr_length * sizeof(Py_UNICODE)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1693 | } |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1694 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1695 | Py_RETURN_NONE; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1698 | /*[clinic input] |
| 1699 | array.array.tounicode |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1700 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1701 | Extends this array with data from the unicode string ustr. |
| 1702 | |
| 1703 | Convert the array to a unicode string. The array must be a unicode type array; |
| 1704 | otherwise a ValueError is raised. Use array.tobytes().decode() to obtain a |
| 1705 | unicode string from an array of some other type. |
| 1706 | [clinic start generated code]*/ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1707 | |
| 1708 | static PyObject * |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1709 | array_array_tounicode_impl(arrayobject *self) |
| 1710 | /*[clinic end generated code: output=08e442378336e1ef input=127242eebe70b66d]*/ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1711 | { |
Victor Stinner | 62bb394 | 2012-08-06 00:46:05 +0200 | [diff] [blame] | 1712 | char typecode; |
| 1713 | typecode = self->ob_descr->typecode; |
Gregory P. Smith | 9504b13 | 2012-12-10 20:20:20 -0800 | [diff] [blame] | 1714 | if (typecode != 'u') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1715 | PyErr_SetString(PyExc_ValueError, |
| 1716 | "tounicode() may only be called on unicode type arrays"); |
| 1717 | return NULL; |
| 1718 | } |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 1719 | return PyUnicode_FromWideChar((Py_UNICODE *) self->ob_item, Py_SIZE(self)); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1722 | /*[clinic input] |
| 1723 | array.array.__sizeof__ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1724 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1725 | Size of the array in memory, in bytes. |
| 1726 | [clinic start generated code]*/ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1727 | |
Meador Inge | 03b4d50 | 2012-08-10 22:35:45 -0500 | [diff] [blame] | 1728 | static PyObject * |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1729 | array_array___sizeof___impl(arrayobject *self) |
| 1730 | /*[clinic end generated code: output=d8e1c61ebbe3eaed input=805586565bf2b3c6]*/ |
Meador Inge | 03b4d50 | 2012-08-10 22:35:45 -0500 | [diff] [blame] | 1731 | { |
| 1732 | Py_ssize_t res; |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 1733 | res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * self->ob_descr->itemsize; |
Meador Inge | 03b4d50 | 2012-08-10 22:35:45 -0500 | [diff] [blame] | 1734 | return PyLong_FromSsize_t(res); |
| 1735 | } |
| 1736 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 1737 | |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1738 | /*********************** Pickling support ************************/ |
| 1739 | |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1740 | static const struct mformatdescr { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1741 | size_t size; |
| 1742 | int is_signed; |
| 1743 | int is_big_endian; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1744 | } mformat_descriptors[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1745 | {1, 0, 0}, /* 0: UNSIGNED_INT8 */ |
| 1746 | {1, 1, 0}, /* 1: SIGNED_INT8 */ |
| 1747 | {2, 0, 0}, /* 2: UNSIGNED_INT16_LE */ |
| 1748 | {2, 0, 1}, /* 3: UNSIGNED_INT16_BE */ |
| 1749 | {2, 1, 0}, /* 4: SIGNED_INT16_LE */ |
| 1750 | {2, 1, 1}, /* 5: SIGNED_INT16_BE */ |
| 1751 | {4, 0, 0}, /* 6: UNSIGNED_INT32_LE */ |
| 1752 | {4, 0, 1}, /* 7: UNSIGNED_INT32_BE */ |
| 1753 | {4, 1, 0}, /* 8: SIGNED_INT32_LE */ |
| 1754 | {4, 1, 1}, /* 9: SIGNED_INT32_BE */ |
| 1755 | {8, 0, 0}, /* 10: UNSIGNED_INT64_LE */ |
| 1756 | {8, 0, 1}, /* 11: UNSIGNED_INT64_BE */ |
| 1757 | {8, 1, 0}, /* 12: SIGNED_INT64_LE */ |
| 1758 | {8, 1, 1}, /* 13: SIGNED_INT64_BE */ |
| 1759 | {4, 0, 0}, /* 14: IEEE_754_FLOAT_LE */ |
| 1760 | {4, 0, 1}, /* 15: IEEE_754_FLOAT_BE */ |
| 1761 | {8, 0, 0}, /* 16: IEEE_754_DOUBLE_LE */ |
| 1762 | {8, 0, 1}, /* 17: IEEE_754_DOUBLE_BE */ |
| 1763 | {4, 0, 0}, /* 18: UTF16_LE */ |
| 1764 | {4, 0, 1}, /* 19: UTF16_BE */ |
| 1765 | {8, 0, 0}, /* 20: UTF32_LE */ |
| 1766 | {8, 0, 1} /* 21: UTF32_BE */ |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1767 | }; |
| 1768 | |
| 1769 | |
| 1770 | /* |
| 1771 | * Internal: This function is used to find the machine format of a given |
| 1772 | * array type code. This returns UNKNOWN_FORMAT when the machine format cannot |
| 1773 | * be found. |
| 1774 | */ |
| 1775 | static enum machine_format_code |
Victor Stinner | f8bb7d0 | 2011-09-30 00:03:59 +0200 | [diff] [blame] | 1776 | typecode_to_mformat_code(char typecode) |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1777 | { |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 1778 | const int is_big_endian = PY_BIG_ENDIAN; |
| 1779 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1780 | size_t intsize; |
| 1781 | int is_signed; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1782 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1783 | switch (typecode) { |
| 1784 | case 'b': |
| 1785 | return SIGNED_INT8; |
| 1786 | case 'B': |
| 1787 | return UNSIGNED_INT8; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1788 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1789 | case 'u': |
Victor Stinner | 62bb394 | 2012-08-06 00:46:05 +0200 | [diff] [blame] | 1790 | if (sizeof(Py_UNICODE) == 2) { |
| 1791 | return UTF16_LE + is_big_endian; |
| 1792 | } |
| 1793 | if (sizeof(Py_UNICODE) == 4) { |
| 1794 | return UTF32_LE + is_big_endian; |
| 1795 | } |
| 1796 | return UNKNOWN_FORMAT; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1797 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1798 | case 'f': |
| 1799 | if (sizeof(float) == 4) { |
| 1800 | const float y = 16711938.0; |
| 1801 | if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0) |
| 1802 | return IEEE_754_FLOAT_BE; |
| 1803 | if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0) |
| 1804 | return IEEE_754_FLOAT_LE; |
| 1805 | } |
| 1806 | return UNKNOWN_FORMAT; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1808 | case 'd': |
| 1809 | if (sizeof(double) == 8) { |
| 1810 | const double x = 9006104071832581.0; |
| 1811 | if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0) |
| 1812 | return IEEE_754_DOUBLE_BE; |
| 1813 | if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0) |
| 1814 | return IEEE_754_DOUBLE_LE; |
| 1815 | } |
| 1816 | return UNKNOWN_FORMAT; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1817 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1818 | /* Integers */ |
| 1819 | case 'h': |
| 1820 | intsize = sizeof(short); |
| 1821 | is_signed = 1; |
| 1822 | break; |
| 1823 | case 'H': |
| 1824 | intsize = sizeof(short); |
| 1825 | is_signed = 0; |
| 1826 | break; |
| 1827 | case 'i': |
| 1828 | intsize = sizeof(int); |
| 1829 | is_signed = 1; |
| 1830 | break; |
| 1831 | case 'I': |
| 1832 | intsize = sizeof(int); |
| 1833 | is_signed = 0; |
| 1834 | break; |
| 1835 | case 'l': |
| 1836 | intsize = sizeof(long); |
| 1837 | is_signed = 1; |
| 1838 | break; |
| 1839 | case 'L': |
| 1840 | intsize = sizeof(long); |
| 1841 | is_signed = 0; |
| 1842 | break; |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 1843 | case 'q': |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1844 | intsize = sizeof(long long); |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 1845 | is_signed = 1; |
| 1846 | break; |
| 1847 | case 'Q': |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1848 | intsize = sizeof(long long); |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 1849 | is_signed = 0; |
| 1850 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1851 | default: |
| 1852 | return UNKNOWN_FORMAT; |
| 1853 | } |
| 1854 | switch (intsize) { |
| 1855 | case 2: |
| 1856 | return UNSIGNED_INT16_LE + is_big_endian + (2 * is_signed); |
| 1857 | case 4: |
| 1858 | return UNSIGNED_INT32_LE + is_big_endian + (2 * is_signed); |
| 1859 | case 8: |
| 1860 | return UNSIGNED_INT64_LE + is_big_endian + (2 * is_signed); |
| 1861 | default: |
| 1862 | return UNKNOWN_FORMAT; |
| 1863 | } |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
| 1866 | /* Forward declaration. */ |
| 1867 | static PyObject *array_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 1868 | |
| 1869 | /* |
| 1870 | * Internal: This function wraps the array constructor--i.e., array_new()--to |
| 1871 | * allow the creation of array objects from C code without having to deal |
| 1872 | * directly the tuple argument of array_new(). The typecode argument is a |
| 1873 | * Unicode character value, like 'i' or 'f' for example, representing an array |
| 1874 | * type code. The items argument is a bytes or a list object from which |
| 1875 | * contains the initial value of the array. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1876 | * |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1877 | * On success, this functions returns the array object created. Otherwise, |
| 1878 | * NULL is returned to indicate a failure. |
| 1879 | */ |
| 1880 | static PyObject * |
Victor Stinner | f8bb7d0 | 2011-09-30 00:03:59 +0200 | [diff] [blame] | 1881 | make_array(PyTypeObject *arraytype, char typecode, PyObject *items) |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1882 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1883 | PyObject *new_args; |
| 1884 | PyObject *array_obj; |
| 1885 | PyObject *typecode_obj; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1886 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1887 | assert(arraytype != NULL); |
| 1888 | assert(items != NULL); |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1889 | |
Victor Stinner | f8bb7d0 | 2011-09-30 00:03:59 +0200 | [diff] [blame] | 1890 | typecode_obj = PyUnicode_FromOrdinal(typecode); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1891 | if (typecode_obj == NULL) |
| 1892 | return NULL; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1893 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1894 | new_args = PyTuple_New(2); |
Mat M | 56935a5 | 2017-11-14 01:00:54 -0500 | [diff] [blame] | 1895 | if (new_args == NULL) { |
| 1896 | Py_DECREF(typecode_obj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1897 | return NULL; |
Mat M | 56935a5 | 2017-11-14 01:00:54 -0500 | [diff] [blame] | 1898 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1899 | Py_INCREF(items); |
| 1900 | PyTuple_SET_ITEM(new_args, 0, typecode_obj); |
| 1901 | PyTuple_SET_ITEM(new_args, 1, items); |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1902 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1903 | array_obj = array_new(arraytype, new_args, NULL); |
| 1904 | Py_DECREF(new_args); |
| 1905 | if (array_obj == NULL) |
| 1906 | return NULL; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1907 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1908 | return array_obj; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | /* |
| 1912 | * This functions is a special constructor used when unpickling an array. It |
| 1913 | * provides a portable way to rebuild an array from its memory representation. |
| 1914 | */ |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1915 | /*[clinic input] |
| 1916 | array._array_reconstructor |
| 1917 | |
| 1918 | arraytype: object(type="PyTypeObject *") |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 1919 | typecode: int(accept={str}) |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 1920 | mformat_code: int(type="enum machine_format_code") |
| 1921 | items: object |
| 1922 | / |
| 1923 | |
| 1924 | Internal. Used for pickling support. |
| 1925 | [clinic start generated code]*/ |
| 1926 | |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1927 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1928 | array__array_reconstructor_impl(PyObject *module, PyTypeObject *arraytype, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1929 | int typecode, |
| 1930 | enum machine_format_code mformat_code, |
| 1931 | PyObject *items) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1932 | /*[clinic end generated code: output=e05263141ba28365 input=2464dc8f4c7736b5]*/ |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1933 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1934 | PyObject *converted_items; |
| 1935 | PyObject *result; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1936 | const struct arraydescr *descr; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1938 | if (!PyType_Check(arraytype)) { |
| 1939 | PyErr_Format(PyExc_TypeError, |
Sylvain | a90e64b | 2017-03-29 20:09:22 +0200 | [diff] [blame] | 1940 | "first argument must be a type object, not %.200s", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1941 | Py_TYPE(arraytype)->tp_name); |
| 1942 | return NULL; |
| 1943 | } |
| 1944 | if (!PyType_IsSubtype(arraytype, &Arraytype)) { |
| 1945 | PyErr_Format(PyExc_TypeError, |
| 1946 | "%.200s is not a subtype of %.200s", |
| 1947 | arraytype->tp_name, Arraytype.tp_name); |
| 1948 | return NULL; |
| 1949 | } |
| 1950 | for (descr = descriptors; descr->typecode != '\0'; descr++) { |
Victor Stinner | f8bb7d0 | 2011-09-30 00:03:59 +0200 | [diff] [blame] | 1951 | if ((int)descr->typecode == typecode) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1952 | break; |
| 1953 | } |
| 1954 | if (descr->typecode == '\0') { |
| 1955 | PyErr_SetString(PyExc_ValueError, |
| 1956 | "second argument must be a valid type code"); |
| 1957 | return NULL; |
| 1958 | } |
Larry Hastings | dfbeb16 | 2014-10-13 10:39:41 +0100 | [diff] [blame] | 1959 | if (mformat_code < MACHINE_FORMAT_CODE_MIN || |
| 1960 | mformat_code > MACHINE_FORMAT_CODE_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1961 | PyErr_SetString(PyExc_ValueError, |
| 1962 | "third argument must be a valid machine format code."); |
| 1963 | return NULL; |
| 1964 | } |
| 1965 | if (!PyBytes_Check(items)) { |
| 1966 | PyErr_Format(PyExc_TypeError, |
| 1967 | "fourth argument should be bytes, not %.200s", |
| 1968 | Py_TYPE(items)->tp_name); |
| 1969 | return NULL; |
| 1970 | } |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1971 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1972 | /* Fast path: No decoding has to be done. */ |
Larry Hastings | dfbeb16 | 2014-10-13 10:39:41 +0100 | [diff] [blame] | 1973 | if (mformat_code == typecode_to_mformat_code((char)typecode) || |
| 1974 | mformat_code == UNKNOWN_FORMAT) { |
Victor Stinner | f8bb7d0 | 2011-09-30 00:03:59 +0200 | [diff] [blame] | 1975 | return make_array(arraytype, (char)typecode, items); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1976 | } |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1977 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1978 | /* Slow path: Decode the byte string according to the given machine |
| 1979 | * format code. This occurs when the computer unpickling the array |
| 1980 | * object is architecturally different from the one that pickled the |
| 1981 | * array. |
| 1982 | */ |
Larry Hastings | dfbeb16 | 2014-10-13 10:39:41 +0100 | [diff] [blame] | 1983 | if (Py_SIZE(items) % mformat_descriptors[mformat_code].size != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1984 | PyErr_SetString(PyExc_ValueError, |
| 1985 | "string length not a multiple of item size"); |
| 1986 | return NULL; |
| 1987 | } |
Larry Hastings | dfbeb16 | 2014-10-13 10:39:41 +0100 | [diff] [blame] | 1988 | switch (mformat_code) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1989 | case IEEE_754_FLOAT_LE: |
| 1990 | case IEEE_754_FLOAT_BE: { |
sth | aa3ecb8 | 2019-03-20 20:49:39 +0100 | [diff] [blame] | 1991 | Py_ssize_t i; |
Larry Hastings | dfbeb16 | 2014-10-13 10:39:41 +0100 | [diff] [blame] | 1992 | int le = (mformat_code == IEEE_754_FLOAT_LE) ? 1 : 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1993 | Py_ssize_t itemcount = Py_SIZE(items) / 4; |
| 1994 | const unsigned char *memstr = |
| 1995 | (unsigned char *)PyBytes_AS_STRING(items); |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 1996 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1997 | converted_items = PyList_New(itemcount); |
| 1998 | if (converted_items == NULL) |
| 1999 | return NULL; |
| 2000 | for (i = 0; i < itemcount; i++) { |
| 2001 | PyObject *pyfloat = PyFloat_FromDouble( |
| 2002 | _PyFloat_Unpack4(&memstr[i * 4], le)); |
| 2003 | if (pyfloat == NULL) { |
| 2004 | Py_DECREF(converted_items); |
| 2005 | return NULL; |
| 2006 | } |
| 2007 | PyList_SET_ITEM(converted_items, i, pyfloat); |
| 2008 | } |
| 2009 | break; |
| 2010 | } |
| 2011 | case IEEE_754_DOUBLE_LE: |
| 2012 | case IEEE_754_DOUBLE_BE: { |
sth | aa3ecb8 | 2019-03-20 20:49:39 +0100 | [diff] [blame] | 2013 | Py_ssize_t i; |
Larry Hastings | dfbeb16 | 2014-10-13 10:39:41 +0100 | [diff] [blame] | 2014 | int le = (mformat_code == IEEE_754_DOUBLE_LE) ? 1 : 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2015 | Py_ssize_t itemcount = Py_SIZE(items) / 8; |
| 2016 | const unsigned char *memstr = |
| 2017 | (unsigned char *)PyBytes_AS_STRING(items); |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 2018 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2019 | converted_items = PyList_New(itemcount); |
| 2020 | if (converted_items == NULL) |
| 2021 | return NULL; |
| 2022 | for (i = 0; i < itemcount; i++) { |
| 2023 | PyObject *pyfloat = PyFloat_FromDouble( |
| 2024 | _PyFloat_Unpack8(&memstr[i * 8], le)); |
| 2025 | if (pyfloat == NULL) { |
| 2026 | Py_DECREF(converted_items); |
| 2027 | return NULL; |
| 2028 | } |
| 2029 | PyList_SET_ITEM(converted_items, i, pyfloat); |
| 2030 | } |
| 2031 | break; |
| 2032 | } |
| 2033 | case UTF16_LE: |
| 2034 | case UTF16_BE: { |
Larry Hastings | dfbeb16 | 2014-10-13 10:39:41 +0100 | [diff] [blame] | 2035 | int byteorder = (mformat_code == UTF16_LE) ? -1 : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2036 | converted_items = PyUnicode_DecodeUTF16( |
| 2037 | PyBytes_AS_STRING(items), Py_SIZE(items), |
| 2038 | "strict", &byteorder); |
| 2039 | if (converted_items == NULL) |
| 2040 | return NULL; |
| 2041 | break; |
| 2042 | } |
| 2043 | case UTF32_LE: |
| 2044 | case UTF32_BE: { |
Larry Hastings | dfbeb16 | 2014-10-13 10:39:41 +0100 | [diff] [blame] | 2045 | int byteorder = (mformat_code == UTF32_LE) ? -1 : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2046 | converted_items = PyUnicode_DecodeUTF32( |
| 2047 | PyBytes_AS_STRING(items), Py_SIZE(items), |
| 2048 | "strict", &byteorder); |
| 2049 | if (converted_items == NULL) |
| 2050 | return NULL; |
| 2051 | break; |
| 2052 | } |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 2053 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2054 | case UNSIGNED_INT8: |
| 2055 | case SIGNED_INT8: |
| 2056 | case UNSIGNED_INT16_LE: |
| 2057 | case UNSIGNED_INT16_BE: |
| 2058 | case SIGNED_INT16_LE: |
| 2059 | case SIGNED_INT16_BE: |
| 2060 | case UNSIGNED_INT32_LE: |
| 2061 | case UNSIGNED_INT32_BE: |
| 2062 | case SIGNED_INT32_LE: |
| 2063 | case SIGNED_INT32_BE: |
| 2064 | case UNSIGNED_INT64_LE: |
| 2065 | case UNSIGNED_INT64_BE: |
| 2066 | case SIGNED_INT64_LE: |
| 2067 | case SIGNED_INT64_BE: { |
sth | aa3ecb8 | 2019-03-20 20:49:39 +0100 | [diff] [blame] | 2068 | Py_ssize_t i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2069 | const struct mformatdescr mf_descr = |
Larry Hastings | dfbeb16 | 2014-10-13 10:39:41 +0100 | [diff] [blame] | 2070 | mformat_descriptors[mformat_code]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2071 | Py_ssize_t itemcount = Py_SIZE(items) / mf_descr.size; |
| 2072 | const unsigned char *memstr = |
| 2073 | (unsigned char *)PyBytes_AS_STRING(items); |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 2074 | const struct arraydescr *descr; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 2075 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2076 | /* If possible, try to pack array's items using a data type |
| 2077 | * that fits better. This may result in an array with narrower |
| 2078 | * or wider elements. |
| 2079 | * |
Martin Panter | 4c35964 | 2016-05-08 13:53:41 +0000 | [diff] [blame] | 2080 | * For example, if a 32-bit machine pickles an L-code array of |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2081 | * unsigned longs, then the array will be unpickled by 64-bit |
| 2082 | * machine as an I-code array of unsigned ints. |
| 2083 | * |
| 2084 | * XXX: Is it possible to write a unit test for this? |
| 2085 | */ |
| 2086 | for (descr = descriptors; descr->typecode != '\0'; descr++) { |
| 2087 | if (descr->is_integer_type && |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 2088 | (size_t)descr->itemsize == mf_descr.size && |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2089 | descr->is_signed == mf_descr.is_signed) |
| 2090 | typecode = descr->typecode; |
| 2091 | } |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 2092 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2093 | converted_items = PyList_New(itemcount); |
| 2094 | if (converted_items == NULL) |
| 2095 | return NULL; |
| 2096 | for (i = 0; i < itemcount; i++) { |
| 2097 | PyObject *pylong; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 2098 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2099 | pylong = _PyLong_FromByteArray( |
| 2100 | &memstr[i * mf_descr.size], |
| 2101 | mf_descr.size, |
| 2102 | !mf_descr.is_big_endian, |
| 2103 | mf_descr.is_signed); |
| 2104 | if (pylong == NULL) { |
| 2105 | Py_DECREF(converted_items); |
| 2106 | return NULL; |
| 2107 | } |
| 2108 | PyList_SET_ITEM(converted_items, i, pylong); |
| 2109 | } |
| 2110 | break; |
| 2111 | } |
| 2112 | case UNKNOWN_FORMAT: |
| 2113 | /* Impossible, but needed to shut up GCC about the unhandled |
| 2114 | * enumeration value. |
| 2115 | */ |
| 2116 | default: |
| 2117 | PyErr_BadArgument(); |
| 2118 | return NULL; |
| 2119 | } |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 2120 | |
Victor Stinner | f8bb7d0 | 2011-09-30 00:03:59 +0200 | [diff] [blame] | 2121 | result = make_array(arraytype, (char)typecode, converted_items); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2122 | Py_DECREF(converted_items); |
| 2123 | return result; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 2124 | } |
| 2125 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 2126 | /*[clinic input] |
| 2127 | array.array.__reduce_ex__ |
| 2128 | |
| 2129 | value: object |
| 2130 | / |
| 2131 | |
| 2132 | Return state information for pickling. |
| 2133 | [clinic start generated code]*/ |
| 2134 | |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 2135 | static PyObject * |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 2136 | array_array___reduce_ex__(arrayobject *self, PyObject *value) |
| 2137 | /*[clinic end generated code: output=051e0a6175d0eddb input=c36c3f85de7df6cd]*/ |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 2138 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2139 | PyObject *dict; |
| 2140 | PyObject *result; |
| 2141 | PyObject *array_str; |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 2142 | int typecode = self->ob_descr->typecode; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2143 | int mformat_code; |
| 2144 | static PyObject *array_reconstructor = NULL; |
| 2145 | long protocol; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2146 | _Py_IDENTIFIER(_array_reconstructor); |
| 2147 | _Py_IDENTIFIER(__dict__); |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 2148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2149 | if (array_reconstructor == NULL) { |
| 2150 | PyObject *array_module = PyImport_ImportModule("array"); |
| 2151 | if (array_module == NULL) |
| 2152 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2153 | array_reconstructor = _PyObject_GetAttrId( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2154 | array_module, |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2155 | &PyId__array_reconstructor); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2156 | Py_DECREF(array_module); |
| 2157 | if (array_reconstructor == NULL) |
| 2158 | return NULL; |
| 2159 | } |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 2160 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2161 | if (!PyLong_Check(value)) { |
| 2162 | PyErr_SetString(PyExc_TypeError, |
Sylvain | a90e64b | 2017-03-29 20:09:22 +0200 | [diff] [blame] | 2163 | "__reduce_ex__ argument should be an integer"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2164 | return NULL; |
| 2165 | } |
| 2166 | protocol = PyLong_AsLong(value); |
| 2167 | if (protocol == -1 && PyErr_Occurred()) |
| 2168 | return NULL; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 2169 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 2170 | if (_PyObject_LookupAttrId((PyObject *)self, &PyId___dict__, &dict) < 0) { |
| 2171 | return NULL; |
| 2172 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2173 | if (dict == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2174 | dict = Py_None; |
| 2175 | Py_INCREF(dict); |
| 2176 | } |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 2177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2178 | mformat_code = typecode_to_mformat_code(typecode); |
| 2179 | if (mformat_code == UNKNOWN_FORMAT || protocol < 3) { |
| 2180 | /* Convert the array to a list if we got something weird |
| 2181 | * (e.g., non-IEEE floats), or we are pickling the array using |
| 2182 | * a Python 2.x compatible protocol. |
| 2183 | * |
| 2184 | * It is necessary to use a list representation for Python 2.x |
| 2185 | * compatible pickle protocol, since Python 2's str objects |
| 2186 | * are unpickled as unicode by Python 3. Thus it is impossible |
| 2187 | * to make arrays unpicklable by Python 3 by using their memory |
| 2188 | * representation, unless we resort to ugly hacks such as |
| 2189 | * coercing unicode objects to bytes in array_reconstructor. |
| 2190 | */ |
| 2191 | PyObject *list; |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 2192 | list = array_array_tolist_impl(self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2193 | if (list == NULL) { |
| 2194 | Py_DECREF(dict); |
| 2195 | return NULL; |
| 2196 | } |
| 2197 | result = Py_BuildValue( |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 2198 | "O(CO)O", Py_TYPE(self), typecode, list, dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2199 | Py_DECREF(list); |
| 2200 | Py_DECREF(dict); |
| 2201 | return result; |
| 2202 | } |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 2203 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 2204 | array_str = array_array_tobytes_impl(self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2205 | if (array_str == NULL) { |
| 2206 | Py_DECREF(dict); |
| 2207 | return NULL; |
| 2208 | } |
| 2209 | result = Py_BuildValue( |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 2210 | "O(OCiN)O", array_reconstructor, Py_TYPE(self), typecode, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2211 | mformat_code, array_str, dict); |
| 2212 | Py_DECREF(dict); |
| 2213 | return result; |
Alexandre Vassalotti | ad07715 | 2009-07-15 17:49:23 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2216 | static PyObject * |
| 2217 | array_get_typecode(arrayobject *a, void *closure) |
| 2218 | { |
Victor Stinner | f8bb7d0 | 2011-09-30 00:03:59 +0200 | [diff] [blame] | 2219 | char typecode = a->ob_descr->typecode; |
| 2220 | return PyUnicode_FromOrdinal(typecode); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | static PyObject * |
| 2224 | array_get_itemsize(arrayobject *a, void *closure) |
| 2225 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2226 | return PyLong_FromLong((long)a->ob_descr->itemsize); |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
| 2229 | static PyGetSetDef array_getsets [] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2230 | {"typecode", (getter) array_get_typecode, NULL, |
| 2231 | "the typecode character used to create the array"}, |
| 2232 | {"itemsize", (getter) array_get_itemsize, NULL, |
| 2233 | "the size, in bytes, of one array item"}, |
| 2234 | {NULL} |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2235 | }; |
| 2236 | |
Martin v. Löwis | 59683e8 | 2008-06-13 07:50:45 +0000 | [diff] [blame] | 2237 | static PyMethodDef array_methods[] = { |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 2238 | ARRAY_ARRAY_APPEND_METHODDEF |
| 2239 | ARRAY_ARRAY_BUFFER_INFO_METHODDEF |
| 2240 | ARRAY_ARRAY_BYTESWAP_METHODDEF |
| 2241 | ARRAY_ARRAY___COPY___METHODDEF |
| 2242 | ARRAY_ARRAY_COUNT_METHODDEF |
| 2243 | ARRAY_ARRAY___DEEPCOPY___METHODDEF |
| 2244 | ARRAY_ARRAY_EXTEND_METHODDEF |
| 2245 | ARRAY_ARRAY_FROMFILE_METHODDEF |
| 2246 | ARRAY_ARRAY_FROMLIST_METHODDEF |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 2247 | ARRAY_ARRAY_FROMBYTES_METHODDEF |
| 2248 | ARRAY_ARRAY_FROMUNICODE_METHODDEF |
| 2249 | ARRAY_ARRAY_INDEX_METHODDEF |
| 2250 | ARRAY_ARRAY_INSERT_METHODDEF |
| 2251 | ARRAY_ARRAY_POP_METHODDEF |
| 2252 | ARRAY_ARRAY___REDUCE_EX___METHODDEF |
| 2253 | ARRAY_ARRAY_REMOVE_METHODDEF |
| 2254 | ARRAY_ARRAY_REVERSE_METHODDEF |
| 2255 | ARRAY_ARRAY_TOFILE_METHODDEF |
| 2256 | ARRAY_ARRAY_TOLIST_METHODDEF |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 2257 | ARRAY_ARRAY_TOBYTES_METHODDEF |
| 2258 | ARRAY_ARRAY_TOUNICODE_METHODDEF |
| 2259 | ARRAY_ARRAY___SIZEOF___METHODDEF |
| 2260 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 2261 | }; |
| 2262 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 2263 | static PyObject * |
Peter Schneider-Kamp | 9656abd | 2000-07-13 21:10:57 +0000 | [diff] [blame] | 2264 | array_repr(arrayobject *a) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 2265 | { |
Victor Stinner | f8bb7d0 | 2011-09-30 00:03:59 +0200 | [diff] [blame] | 2266 | char typecode; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2267 | PyObject *s, *v = NULL; |
| 2268 | Py_ssize_t len; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2269 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2270 | len = Py_SIZE(a); |
| 2271 | typecode = a->ob_descr->typecode; |
| 2272 | if (len == 0) { |
Serhiy Storchaka | b3a7796 | 2017-09-21 14:24:13 +0300 | [diff] [blame] | 2273 | return PyUnicode_FromFormat("%s('%c')", |
| 2274 | _PyType_Name(Py_TYPE(a)), (int)typecode); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2275 | } |
Gregory P. Smith | 9504b13 | 2012-12-10 20:20:20 -0800 | [diff] [blame] | 2276 | if (typecode == 'u') { |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 2277 | v = array_array_tounicode_impl(a); |
Gregory P. Smith | 9504b13 | 2012-12-10 20:20:20 -0800 | [diff] [blame] | 2278 | } else { |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 2279 | v = array_array_tolist_impl(a); |
Gregory P. Smith | 9504b13 | 2012-12-10 20:20:20 -0800 | [diff] [blame] | 2280 | } |
Victor Stinner | 29ec595 | 2013-02-26 00:27:38 +0100 | [diff] [blame] | 2281 | if (v == NULL) |
| 2282 | return NULL; |
Raymond Hettinger | 88ba1e3 | 2003-04-23 17:27:00 +0000 | [diff] [blame] | 2283 | |
Serhiy Storchaka | b3a7796 | 2017-09-21 14:24:13 +0300 | [diff] [blame] | 2284 | s = PyUnicode_FromFormat("%s('%c', %R)", |
| 2285 | _PyType_Name(Py_TYPE(a)), (int)typecode, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2286 | Py_DECREF(v); |
| 2287 | return s; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 2290 | static PyObject* |
| 2291 | array_subscr(arrayobject* self, PyObject* item) |
| 2292 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2293 | if (PyIndex_Check(item)) { |
| 2294 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 2295 | if (i==-1 && PyErr_Occurred()) { |
| 2296 | return NULL; |
| 2297 | } |
| 2298 | if (i < 0) |
| 2299 | i += Py_SIZE(self); |
| 2300 | return array_item(self, i); |
| 2301 | } |
| 2302 | else if (PySlice_Check(item)) { |
Zackery Spytz | 14514d9 | 2019-05-17 01:13:03 -0600 | [diff] [blame] | 2303 | Py_ssize_t start, stop, step, slicelength, i; |
| 2304 | size_t cur; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2305 | PyObject* result; |
| 2306 | arrayobject* ar; |
| 2307 | int itemsize = self->ob_descr->itemsize; |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 2308 | |
Serhiy Storchaka | b879fe8 | 2017-04-08 09:53:51 +0300 | [diff] [blame] | 2309 | if (PySlice_Unpack(item, &start, &stop, &step) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2310 | return NULL; |
| 2311 | } |
Serhiy Storchaka | b879fe8 | 2017-04-08 09:53:51 +0300 | [diff] [blame] | 2312 | slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop, |
| 2313 | step); |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 2314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2315 | if (slicelength <= 0) { |
| 2316 | return newarrayobject(&Arraytype, 0, self->ob_descr); |
| 2317 | } |
| 2318 | else if (step == 1) { |
| 2319 | PyObject *result = newarrayobject(&Arraytype, |
| 2320 | slicelength, self->ob_descr); |
| 2321 | if (result == NULL) |
| 2322 | return NULL; |
| 2323 | memcpy(((arrayobject *)result)->ob_item, |
| 2324 | self->ob_item + start * itemsize, |
| 2325 | slicelength * itemsize); |
| 2326 | return result; |
| 2327 | } |
| 2328 | else { |
| 2329 | result = newarrayobject(&Arraytype, slicelength, self->ob_descr); |
| 2330 | if (!result) return NULL; |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 2331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2332 | ar = (arrayobject*)result; |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 2333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2334 | for (cur = start, i = 0; i < slicelength; |
| 2335 | cur += step, i++) { |
| 2336 | memcpy(ar->ob_item + i*itemsize, |
| 2337 | self->ob_item + cur*itemsize, |
| 2338 | itemsize); |
| 2339 | } |
| 2340 | |
| 2341 | return result; |
| 2342 | } |
| 2343 | } |
| 2344 | else { |
| 2345 | PyErr_SetString(PyExc_TypeError, |
| 2346 | "array indices must be integers"); |
| 2347 | return NULL; |
| 2348 | } |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 2349 | } |
| 2350 | |
| 2351 | static int |
| 2352 | array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value) |
| 2353 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2354 | Py_ssize_t start, stop, step, slicelength, needed; |
| 2355 | arrayobject* other; |
| 2356 | int itemsize; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 2357 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2358 | if (PyIndex_Check(item)) { |
| 2359 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Alexandre Vassalotti | 4713725 | 2009-07-05 19:57:00 +0000 | [diff] [blame] | 2360 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2361 | if (i == -1 && PyErr_Occurred()) |
| 2362 | return -1; |
| 2363 | if (i < 0) |
| 2364 | i += Py_SIZE(self); |
| 2365 | if (i < 0 || i >= Py_SIZE(self)) { |
| 2366 | PyErr_SetString(PyExc_IndexError, |
| 2367 | "array assignment index out of range"); |
| 2368 | return -1; |
| 2369 | } |
| 2370 | if (value == NULL) { |
| 2371 | /* Fall through to slice assignment */ |
| 2372 | start = i; |
| 2373 | stop = i + 1; |
| 2374 | step = 1; |
| 2375 | slicelength = 1; |
| 2376 | } |
| 2377 | else |
| 2378 | return (*self->ob_descr->setitem)(self, i, value); |
| 2379 | } |
| 2380 | else if (PySlice_Check(item)) { |
Serhiy Storchaka | b879fe8 | 2017-04-08 09:53:51 +0300 | [diff] [blame] | 2381 | if (PySlice_Unpack(item, &start, &stop, &step) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2382 | return -1; |
| 2383 | } |
Serhiy Storchaka | b879fe8 | 2017-04-08 09:53:51 +0300 | [diff] [blame] | 2384 | slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop, |
| 2385 | step); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2386 | } |
| 2387 | else { |
| 2388 | PyErr_SetString(PyExc_TypeError, |
Sylvain | a90e64b | 2017-03-29 20:09:22 +0200 | [diff] [blame] | 2389 | "array indices must be integers"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2390 | return -1; |
| 2391 | } |
| 2392 | if (value == NULL) { |
| 2393 | other = NULL; |
| 2394 | needed = 0; |
| 2395 | } |
| 2396 | else if (array_Check(value)) { |
| 2397 | other = (arrayobject *)value; |
| 2398 | needed = Py_SIZE(other); |
| 2399 | if (self == other) { |
| 2400 | /* Special case "self[i:j] = self" -- copy self first */ |
| 2401 | int ret; |
| 2402 | value = array_slice(other, 0, needed); |
| 2403 | if (value == NULL) |
| 2404 | return -1; |
| 2405 | ret = array_ass_subscr(self, item, value); |
| 2406 | Py_DECREF(value); |
| 2407 | return ret; |
| 2408 | } |
| 2409 | if (other->ob_descr != self->ob_descr) { |
| 2410 | PyErr_BadArgument(); |
| 2411 | return -1; |
| 2412 | } |
| 2413 | } |
| 2414 | else { |
| 2415 | PyErr_Format(PyExc_TypeError, |
| 2416 | "can only assign array (not \"%.200s\") to array slice", |
| 2417 | Py_TYPE(value)->tp_name); |
| 2418 | return -1; |
| 2419 | } |
| 2420 | itemsize = self->ob_descr->itemsize; |
| 2421 | /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */ |
| 2422 | if ((step > 0 && stop < start) || |
| 2423 | (step < 0 && stop > start)) |
| 2424 | stop = start; |
Alexandre Vassalotti | 4713725 | 2009-07-05 19:57:00 +0000 | [diff] [blame] | 2425 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2426 | /* Issue #4509: If the array has exported buffers and the slice |
| 2427 | assignment would change the size of the array, fail early to make |
| 2428 | sure we don't modify it. */ |
| 2429 | if ((needed == 0 || slicelength != needed) && self->ob_exports > 0) { |
| 2430 | PyErr_SetString(PyExc_BufferError, |
| 2431 | "cannot resize an array that is exporting buffers"); |
| 2432 | return -1; |
| 2433 | } |
Mark Dickinson | bc09964 | 2010-01-29 17:27:24 +0000 | [diff] [blame] | 2434 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2435 | if (step == 1) { |
| 2436 | if (slicelength > needed) { |
| 2437 | memmove(self->ob_item + (start + needed) * itemsize, |
| 2438 | self->ob_item + stop * itemsize, |
| 2439 | (Py_SIZE(self) - stop) * itemsize); |
| 2440 | if (array_resize(self, Py_SIZE(self) + |
| 2441 | needed - slicelength) < 0) |
| 2442 | return -1; |
| 2443 | } |
| 2444 | else if (slicelength < needed) { |
| 2445 | if (array_resize(self, Py_SIZE(self) + |
| 2446 | needed - slicelength) < 0) |
| 2447 | return -1; |
| 2448 | memmove(self->ob_item + (start + needed) * itemsize, |
| 2449 | self->ob_item + stop * itemsize, |
| 2450 | (Py_SIZE(self) - start - needed) * itemsize); |
| 2451 | } |
| 2452 | if (needed > 0) |
| 2453 | memcpy(self->ob_item + start * itemsize, |
| 2454 | other->ob_item, needed * itemsize); |
| 2455 | return 0; |
| 2456 | } |
| 2457 | else if (needed == 0) { |
| 2458 | /* Delete slice */ |
| 2459 | size_t cur; |
| 2460 | Py_ssize_t i; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 2461 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2462 | if (step < 0) { |
| 2463 | stop = start + 1; |
| 2464 | start = stop + step * (slicelength - 1) - 1; |
| 2465 | step = -step; |
| 2466 | } |
| 2467 | for (cur = start, i = 0; i < slicelength; |
| 2468 | cur += step, i++) { |
| 2469 | Py_ssize_t lim = step - 1; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 2470 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2471 | if (cur + step >= (size_t)Py_SIZE(self)) |
| 2472 | lim = Py_SIZE(self) - cur - 1; |
| 2473 | memmove(self->ob_item + (cur - i) * itemsize, |
| 2474 | self->ob_item + (cur + 1) * itemsize, |
| 2475 | lim * itemsize); |
| 2476 | } |
Mark Dickinson | c7d93b7 | 2011-09-25 15:34:32 +0100 | [diff] [blame] | 2477 | cur = start + (size_t)slicelength * step; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2478 | if (cur < (size_t)Py_SIZE(self)) { |
| 2479 | memmove(self->ob_item + (cur-slicelength) * itemsize, |
| 2480 | self->ob_item + cur * itemsize, |
| 2481 | (Py_SIZE(self) - cur) * itemsize); |
| 2482 | } |
| 2483 | if (array_resize(self, Py_SIZE(self) - slicelength) < 0) |
| 2484 | return -1; |
| 2485 | return 0; |
| 2486 | } |
| 2487 | else { |
Zackery Spytz | 14514d9 | 2019-05-17 01:13:03 -0600 | [diff] [blame] | 2488 | size_t cur; |
| 2489 | Py_ssize_t i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2490 | |
| 2491 | if (needed != slicelength) { |
| 2492 | PyErr_Format(PyExc_ValueError, |
| 2493 | "attempt to assign array of size %zd " |
| 2494 | "to extended slice of size %zd", |
| 2495 | needed, slicelength); |
| 2496 | return -1; |
| 2497 | } |
| 2498 | for (cur = start, i = 0; i < slicelength; |
| 2499 | cur += step, i++) { |
| 2500 | memcpy(self->ob_item + cur * itemsize, |
| 2501 | other->ob_item + i * itemsize, |
| 2502 | itemsize); |
| 2503 | } |
| 2504 | return 0; |
| 2505 | } |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 2506 | } |
| 2507 | |
| 2508 | static PyMappingMethods array_as_mapping = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2509 | (lenfunc)array_length, |
| 2510 | (binaryfunc)array_subscr, |
| 2511 | (objobjargproc)array_ass_subscr |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 2512 | }; |
| 2513 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2514 | static const void *emptybuf = ""; |
| 2515 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 2516 | |
| 2517 | static int |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 2518 | array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 2519 | { |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 2520 | if (view == NULL) { |
| 2521 | PyErr_SetString(PyExc_BufferError, |
| 2522 | "array_buffer_getbuf: view==NULL argument is obsolete"); |
| 2523 | return -1; |
| 2524 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 2525 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2526 | view->buf = (void *)self->ob_item; |
| 2527 | view->obj = (PyObject*)self; |
| 2528 | Py_INCREF(self); |
| 2529 | if (view->buf == NULL) |
| 2530 | view->buf = (void *)emptybuf; |
| 2531 | view->len = (Py_SIZE(self)) * self->ob_descr->itemsize; |
| 2532 | view->readonly = 0; |
| 2533 | view->ndim = 1; |
| 2534 | view->itemsize = self->ob_descr->itemsize; |
| 2535 | view->suboffsets = NULL; |
| 2536 | view->shape = NULL; |
| 2537 | if ((flags & PyBUF_ND)==PyBUF_ND) { |
| 2538 | view->shape = &((Py_SIZE(self))); |
| 2539 | } |
| 2540 | view->strides = NULL; |
| 2541 | if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES) |
| 2542 | view->strides = &(view->itemsize); |
| 2543 | view->format = NULL; |
| 2544 | view->internal = NULL; |
Victor Stinner | 62bb394 | 2012-08-06 00:46:05 +0200 | [diff] [blame] | 2545 | if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 2546 | view->format = (char *)self->ob_descr->formats; |
Victor Stinner | 62bb394 | 2012-08-06 00:46:05 +0200 | [diff] [blame] | 2547 | #ifdef Py_UNICODE_WIDE |
| 2548 | if (self->ob_descr->typecode == 'u') { |
| 2549 | view->format = "w"; |
| 2550 | } |
| 2551 | #endif |
| 2552 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 2553 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2554 | self->ob_exports++; |
| 2555 | return 0; |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 2556 | } |
| 2557 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 2558 | static void |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 2559 | array_buffer_relbuf(arrayobject *self, Py_buffer *view) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 2560 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2561 | self->ob_exports--; |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 2562 | } |
| 2563 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 2564 | static PySequenceMethods array_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2565 | (lenfunc)array_length, /*sq_length*/ |
| 2566 | (binaryfunc)array_concat, /*sq_concat*/ |
| 2567 | (ssizeargfunc)array_repeat, /*sq_repeat*/ |
| 2568 | (ssizeargfunc)array_item, /*sq_item*/ |
| 2569 | 0, /*sq_slice*/ |
| 2570 | (ssizeobjargproc)array_ass_item, /*sq_ass_item*/ |
| 2571 | 0, /*sq_ass_slice*/ |
| 2572 | (objobjproc)array_contains, /*sq_contains*/ |
| 2573 | (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/ |
| 2574 | (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/ |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 2575 | }; |
| 2576 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 2577 | static PyBufferProcs array_as_buffer = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2578 | (getbufferproc)array_buffer_getbuf, |
| 2579 | (releasebufferproc)array_buffer_relbuf |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 2580 | }; |
| 2581 | |
Roger E. Masse | 2919eaa | 1996-12-09 20:10:36 +0000 | [diff] [blame] | 2582 | static PyObject * |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2583 | array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 2584 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2585 | int c; |
| 2586 | PyObject *initial = NULL, *it = NULL; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 2587 | const struct arraydescr *descr; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2588 | |
Serhiy Storchaka | 6cca5c8 | 2017-06-08 14:41:19 +0300 | [diff] [blame] | 2589 | if (type == &Arraytype && !_PyArg_NoKeywords("array.array", kwds)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2590 | return NULL; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2591 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2592 | if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial)) |
| 2593 | return NULL; |
Raymond Hettinger | 84fc9aa | 2003-04-24 10:41:55 +0000 | [diff] [blame] | 2594 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 2595 | if (PySys_Audit("array.__new__", "CO", |
| 2596 | c, initial ? initial : Py_None) < 0) { |
| 2597 | return NULL; |
| 2598 | } |
| 2599 | |
Alexandre Vassalotti | 9730e33 | 2013-11-29 20:47:15 -0800 | [diff] [blame] | 2600 | if (initial && c != 'u') { |
| 2601 | if (PyUnicode_Check(initial)) { |
| 2602 | PyErr_Format(PyExc_TypeError, "cannot use a str to initialize " |
| 2603 | "an array with typecode '%c'", c); |
| 2604 | return NULL; |
| 2605 | } |
| 2606 | else if (array_Check(initial) && |
| 2607 | ((arrayobject*)initial)->ob_descr->typecode == 'u') { |
| 2608 | PyErr_Format(PyExc_TypeError, "cannot use a unicode array to " |
| 2609 | "initialize an array with typecode '%c'", c); |
| 2610 | return NULL; |
| 2611 | } |
| 2612 | } |
| 2613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2614 | if (!(initial == NULL || PyList_Check(initial) |
| 2615 | || PyByteArray_Check(initial) |
| 2616 | || PyBytes_Check(initial) |
| 2617 | || PyTuple_Check(initial) |
Alexander Belopolsky | ef4a03f | 2011-01-11 21:44:00 +0000 | [diff] [blame] | 2618 | || ((c=='u') && PyUnicode_Check(initial)) |
| 2619 | || (array_Check(initial) |
| 2620 | && c == ((arrayobject*)initial)->ob_descr->typecode))) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2621 | it = PyObject_GetIter(initial); |
| 2622 | if (it == NULL) |
| 2623 | return NULL; |
| 2624 | /* We set initial to NULL so that the subsequent code |
| 2625 | will create an empty array of the appropriate type |
| 2626 | and afterwards we can use array_iter_extend to populate |
| 2627 | the array. |
| 2628 | */ |
| 2629 | initial = NULL; |
| 2630 | } |
| 2631 | for (descr = descriptors; descr->typecode != '\0'; descr++) { |
| 2632 | if (descr->typecode == c) { |
| 2633 | PyObject *a; |
| 2634 | Py_ssize_t len; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2635 | |
Alexander Belopolsky | ef4a03f | 2011-01-11 21:44:00 +0000 | [diff] [blame] | 2636 | if (initial == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2637 | len = 0; |
Alexander Belopolsky | ef4a03f | 2011-01-11 21:44:00 +0000 | [diff] [blame] | 2638 | else if (PyList_Check(initial)) |
| 2639 | len = PyList_GET_SIZE(initial); |
| 2640 | else if (PyTuple_Check(initial) || array_Check(initial)) |
| 2641 | len = Py_SIZE(initial); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2642 | else |
Alexander Belopolsky | ef4a03f | 2011-01-11 21:44:00 +0000 | [diff] [blame] | 2643 | len = 0; |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2645 | a = newarrayobject(type, len, descr); |
| 2646 | if (a == NULL) |
| 2647 | return NULL; |
| 2648 | |
Alexander Belopolsky | ef4a03f | 2011-01-11 21:44:00 +0000 | [diff] [blame] | 2649 | if (len > 0 && !array_Check(initial)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2650 | Py_ssize_t i; |
| 2651 | for (i = 0; i < len; i++) { |
| 2652 | PyObject *v = |
| 2653 | PySequence_GetItem(initial, i); |
| 2654 | if (v == NULL) { |
| 2655 | Py_DECREF(a); |
| 2656 | return NULL; |
| 2657 | } |
| 2658 | if (setarrayitem(a, i, v) != 0) { |
| 2659 | Py_DECREF(v); |
| 2660 | Py_DECREF(a); |
| 2661 | return NULL; |
| 2662 | } |
| 2663 | Py_DECREF(v); |
| 2664 | } |
| 2665 | } |
| 2666 | else if (initial != NULL && (PyByteArray_Check(initial) || |
| 2667 | PyBytes_Check(initial))) { |
Serhiy Storchaka | 04e6dba | 2015-04-04 17:06:55 +0300 | [diff] [blame] | 2668 | PyObject *v; |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 2669 | v = array_array_frombytes((arrayobject *)a, |
Serhiy Storchaka | 04e6dba | 2015-04-04 17:06:55 +0300 | [diff] [blame] | 2670 | initial); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2671 | if (v == NULL) { |
| 2672 | Py_DECREF(a); |
| 2673 | return NULL; |
| 2674 | } |
| 2675 | Py_DECREF(v); |
| 2676 | } |
| 2677 | else if (initial != NULL && PyUnicode_Check(initial)) { |
Victor Stinner | 62bb394 | 2012-08-06 00:46:05 +0200 | [diff] [blame] | 2678 | Py_UNICODE *ustr; |
Victor Stinner | 1fbcaef | 2011-09-30 01:54:04 +0200 | [diff] [blame] | 2679 | Py_ssize_t n; |
Victor Stinner | 62bb394 | 2012-08-06 00:46:05 +0200 | [diff] [blame] | 2680 | |
| 2681 | ustr = PyUnicode_AsUnicode(initial); |
| 2682 | if (ustr == NULL) { |
| 2683 | PyErr_NoMemory(); |
Victor Stinner | 1fbcaef | 2011-09-30 01:54:04 +0200 | [diff] [blame] | 2684 | Py_DECREF(a); |
| 2685 | return NULL; |
| 2686 | } |
Victor Stinner | 62bb394 | 2012-08-06 00:46:05 +0200 | [diff] [blame] | 2687 | |
| 2688 | n = PyUnicode_GET_DATA_SIZE(initial); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2689 | if (n > 0) { |
| 2690 | arrayobject *self = (arrayobject *)a; |
Victor Stinner | 62bb394 | 2012-08-06 00:46:05 +0200 | [diff] [blame] | 2691 | char *item = self->ob_item; |
| 2692 | item = (char *)PyMem_Realloc(item, n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2693 | if (item == NULL) { |
| 2694 | PyErr_NoMemory(); |
| 2695 | Py_DECREF(a); |
| 2696 | return NULL; |
| 2697 | } |
Victor Stinner | 62bb394 | 2012-08-06 00:46:05 +0200 | [diff] [blame] | 2698 | self->ob_item = item; |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 2699 | Py_SET_SIZE(self, n / sizeof(Py_UNICODE)); |
Victor Stinner | 62bb394 | 2012-08-06 00:46:05 +0200 | [diff] [blame] | 2700 | memcpy(item, ustr, n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2701 | self->allocated = Py_SIZE(self); |
| 2702 | } |
| 2703 | } |
Benjamin Peterson | 682124c | 2014-10-10 20:58:30 -0400 | [diff] [blame] | 2704 | else if (initial != NULL && array_Check(initial) && len > 0) { |
Alexander Belopolsky | ef4a03f | 2011-01-11 21:44:00 +0000 | [diff] [blame] | 2705 | arrayobject *self = (arrayobject *)a; |
| 2706 | arrayobject *other = (arrayobject *)initial; |
| 2707 | memcpy(self->ob_item, other->ob_item, len * other->ob_descr->itemsize); |
| 2708 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2709 | if (it != NULL) { |
| 2710 | if (array_iter_extend((arrayobject *)a, it) == -1) { |
| 2711 | Py_DECREF(it); |
| 2712 | Py_DECREF(a); |
| 2713 | return NULL; |
| 2714 | } |
| 2715 | Py_DECREF(it); |
| 2716 | } |
| 2717 | return a; |
| 2718 | } |
| 2719 | } |
| 2720 | PyErr_SetString(PyExc_ValueError, |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 2721 | "bad typecode (must be b, B, u, h, H, i, I, l, L, q, Q, f or d)"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2722 | return NULL; |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 2723 | } |
| 2724 | |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 2725 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2726 | PyDoc_STRVAR(module_doc, |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2727 | "This module defines an object type which can efficiently represent\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2728 | an array of basic values: characters, integers, floating point\n\ |
| 2729 | numbers. Arrays are sequence types and behave very much like lists,\n\ |
Alexandre Vassalotti | 9730e33 | 2013-11-29 20:47:15 -0800 | [diff] [blame] | 2730 | except that the type of objects stored in them is constrained.\n"); |
| 2731 | |
| 2732 | PyDoc_STRVAR(arraytype_doc, |
| 2733 | "array(typecode [, initializer]) -> array\n\ |
| 2734 | \n\ |
| 2735 | Return a new array whose items are restricted by typecode, and\n\ |
| 2736 | initialized from the optional initializer value, which must be a list,\n\ |
| 2737 | string or iterable over elements of the appropriate type.\n\ |
| 2738 | \n\ |
| 2739 | Arrays represent basic values and behave very much like lists, except\n\ |
| 2740 | the type of objects stored in them is constrained. The type is specified\n\ |
| 2741 | at object creation time by using a type code, which is a single character.\n\ |
| 2742 | The following type codes are defined:\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2743 | \n\ |
oldk | aa0735f | 2018-02-02 16:52:55 +0800 | [diff] [blame] | 2744 | Type code C Type Minimum size in bytes\n\ |
| 2745 | 'b' signed integer 1\n\ |
| 2746 | 'B' unsigned integer 1\n\ |
| 2747 | 'u' Unicode character 2 (see note)\n\ |
| 2748 | 'h' signed integer 2\n\ |
| 2749 | 'H' unsigned integer 2\n\ |
| 2750 | 'i' signed integer 2\n\ |
| 2751 | 'I' unsigned integer 2\n\ |
| 2752 | 'l' signed integer 4\n\ |
| 2753 | 'L' unsigned integer 4\n\ |
| 2754 | 'q' signed integer 8 (see note)\n\ |
| 2755 | 'Q' unsigned integer 8 (see note)\n\ |
| 2756 | 'f' floating point 4\n\ |
| 2757 | 'd' floating point 8\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2758 | \n\ |
oldk | aa0735f | 2018-02-02 16:52:55 +0800 | [diff] [blame] | 2759 | NOTE: The 'u' typecode corresponds to Python's unicode character. On\n\ |
Victor Stinner | 62bb394 | 2012-08-06 00:46:05 +0200 | [diff] [blame] | 2760 | narrow builds this is 2-bytes on wide builds this is 4-bytes.\n\ |
| 2761 | \n\ |
oldk | aa0735f | 2018-02-02 16:52:55 +0800 | [diff] [blame] | 2762 | NOTE: The 'q' and 'Q' type codes are only available if the platform\n\ |
| 2763 | C compiler used to build Python supports 'long long', or, on Windows,\n\ |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 2764 | '__int64'.\n\ |
| 2765 | \n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2766 | Methods:\n\ |
| 2767 | \n\ |
| 2768 | append() -- append a new item to the end of the array\n\ |
| 2769 | buffer_info() -- return information giving the current memory info\n\ |
| 2770 | byteswap() -- byteswap all the items of the array\n\ |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 2771 | count() -- return number of occurrences of an object\n\ |
Raymond Hettinger | 49f9bd1 | 2004-03-14 05:43:59 +0000 | [diff] [blame] | 2772 | extend() -- extend array by appending multiple elements from an iterable\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2773 | fromfile() -- read items from a file object\n\ |
| 2774 | fromlist() -- append items from the list\n\ |
Florent Xicluna | c45fb25 | 2011-10-24 13:14:55 +0200 | [diff] [blame] | 2775 | frombytes() -- append items from the string\n\ |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 2776 | index() -- return index of first occurrence of an object\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2777 | 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] | 2778 | pop() -- remove and return item (default last)\n\ |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 2779 | remove() -- remove first occurrence of an object\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2780 | reverse() -- reverse the order of the items in the array\n\ |
| 2781 | tofile() -- write all items to a file object\n\ |
| 2782 | tolist() -- return the array converted to an ordinary list\n\ |
Florent Xicluna | c45fb25 | 2011-10-24 13:14:55 +0200 | [diff] [blame] | 2783 | tobytes() -- return the array converted to a string\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2784 | \n\ |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2785 | Attributes:\n\ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2786 | \n\ |
| 2787 | typecode -- the typecode character used to create the array\n\ |
| 2788 | itemsize -- the length in bytes of one array item\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2789 | "); |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2790 | |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2791 | static PyObject *array_iter(arrayobject *ao); |
| 2792 | |
Tim Peters | 0c32279 | 2002-07-17 16:49:03 +0000 | [diff] [blame] | 2793 | static PyTypeObject Arraytype = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2794 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2795 | "array.array", |
| 2796 | sizeof(arrayobject), |
| 2797 | 0, |
| 2798 | (destructor)array_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 2799 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2800 | 0, /* tp_getattr */ |
| 2801 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 2802 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2803 | (reprfunc)array_repr, /* tp_repr */ |
| 2804 | 0, /* tp_as_number*/ |
| 2805 | &array_as_sequence, /* tp_as_sequence*/ |
| 2806 | &array_as_mapping, /* tp_as_mapping*/ |
| 2807 | 0, /* tp_hash */ |
| 2808 | 0, /* tp_call */ |
| 2809 | 0, /* tp_str */ |
| 2810 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2811 | 0, /* tp_setattro */ |
| 2812 | &array_as_buffer, /* tp_as_buffer*/ |
| 2813 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2814 | arraytype_doc, /* tp_doc */ |
| 2815 | 0, /* tp_traverse */ |
| 2816 | 0, /* tp_clear */ |
| 2817 | array_richcompare, /* tp_richcompare */ |
| 2818 | offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */ |
| 2819 | (getiterfunc)array_iter, /* tp_iter */ |
| 2820 | 0, /* tp_iternext */ |
| 2821 | array_methods, /* tp_methods */ |
| 2822 | 0, /* tp_members */ |
| 2823 | array_getsets, /* tp_getset */ |
| 2824 | 0, /* tp_base */ |
| 2825 | 0, /* tp_dict */ |
| 2826 | 0, /* tp_descr_get */ |
| 2827 | 0, /* tp_descr_set */ |
| 2828 | 0, /* tp_dictoffset */ |
| 2829 | 0, /* tp_init */ |
| 2830 | PyType_GenericAlloc, /* tp_alloc */ |
| 2831 | array_new, /* tp_new */ |
| 2832 | PyObject_Del, /* tp_free */ |
Guido van Rossum | b39b90d | 1998-10-13 14:27:22 +0000 | [diff] [blame] | 2833 | }; |
| 2834 | |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2835 | |
| 2836 | /*********************** Array Iterator **************************/ |
| 2837 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 2838 | /*[clinic input] |
| 2839 | class array.arrayiterator "arrayiterobject *" "&PyArrayIter_Type" |
| 2840 | [clinic start generated code]*/ |
| 2841 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=5aefd2d74d8c8e30]*/ |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2842 | |
| 2843 | static PyObject * |
| 2844 | array_iter(arrayobject *ao) |
| 2845 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2846 | arrayiterobject *it; |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2847 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2848 | if (!array_Check(ao)) { |
| 2849 | PyErr_BadInternalCall(); |
| 2850 | return NULL; |
| 2851 | } |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2852 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2853 | it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type); |
| 2854 | if (it == NULL) |
| 2855 | return NULL; |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2856 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2857 | Py_INCREF(ao); |
| 2858 | it->ao = ao; |
| 2859 | it->index = 0; |
| 2860 | it->getitem = ao->ob_descr->getitem; |
| 2861 | PyObject_GC_Track(it); |
| 2862 | return (PyObject *)it; |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
| 2865 | static PyObject * |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2866 | arrayiter_next(arrayiterobject *it) |
| 2867 | { |
Serhiy Storchaka | ab0d198 | 2016-03-30 21:11:16 +0300 | [diff] [blame] | 2868 | arrayobject *ao; |
| 2869 | |
| 2870 | assert(it != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2871 | assert(PyArrayIter_Check(it)); |
Serhiy Storchaka | ab0d198 | 2016-03-30 21:11:16 +0300 | [diff] [blame] | 2872 | ao = it->ao; |
| 2873 | if (ao == NULL) { |
| 2874 | return NULL; |
| 2875 | } |
| 2876 | assert(array_Check(ao)); |
| 2877 | if (it->index < Py_SIZE(ao)) { |
| 2878 | return (*it->getitem)(ao, it->index++); |
| 2879 | } |
| 2880 | it->ao = NULL; |
| 2881 | Py_DECREF(ao); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2882 | return NULL; |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
| 2885 | static void |
| 2886 | arrayiter_dealloc(arrayiterobject *it) |
| 2887 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2888 | PyObject_GC_UnTrack(it); |
| 2889 | Py_XDECREF(it->ao); |
| 2890 | PyObject_GC_Del(it); |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2891 | } |
| 2892 | |
| 2893 | static int |
| 2894 | arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg) |
| 2895 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2896 | Py_VISIT(it->ao); |
| 2897 | return 0; |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2898 | } |
| 2899 | |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 2900 | /*[clinic input] |
| 2901 | array.arrayiterator.__reduce__ |
| 2902 | |
| 2903 | Return state information for pickling. |
| 2904 | [clinic start generated code]*/ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2905 | |
| 2906 | static PyObject * |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 2907 | array_arrayiterator___reduce___impl(arrayiterobject *self) |
| 2908 | /*[clinic end generated code: output=7898a52e8e66e016 input=a062ea1e9951417a]*/ |
| 2909 | { |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 2910 | _Py_IDENTIFIER(iter); |
| 2911 | PyObject *func = _PyEval_GetBuiltinId(&PyId_iter); |
Serhiy Storchaka | ab0d198 | 2016-03-30 21:11:16 +0300 | [diff] [blame] | 2912 | if (self->ao == NULL) { |
| 2913 | return Py_BuildValue("N(())", func); |
| 2914 | } |
| 2915 | return Py_BuildValue("N(O)n", func, self->ao, self->index); |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 2916 | } |
| 2917 | |
| 2918 | /*[clinic input] |
| 2919 | array.arrayiterator.__setstate__ |
| 2920 | |
| 2921 | state: object |
| 2922 | / |
| 2923 | |
| 2924 | Set state information for unpickling. |
| 2925 | [clinic start generated code]*/ |
| 2926 | |
| 2927 | static PyObject * |
| 2928 | array_arrayiterator___setstate__(arrayiterobject *self, PyObject *state) |
| 2929 | /*[clinic end generated code: output=397da9904e443cbe input=f47d5ceda19e787b]*/ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2930 | { |
| 2931 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 2932 | if (index == -1 && PyErr_Occurred()) |
| 2933 | return NULL; |
| 2934 | if (index < 0) |
| 2935 | index = 0; |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 2936 | else if (index > Py_SIZE(self->ao)) |
| 2937 | index = Py_SIZE(self->ao); /* iterator exhausted */ |
| 2938 | self->index = index; |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2939 | Py_RETURN_NONE; |
| 2940 | } |
| 2941 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2942 | static PyMethodDef arrayiter_methods[] = { |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 2943 | ARRAY_ARRAYITERATOR___REDUCE___METHODDEF |
| 2944 | ARRAY_ARRAYITERATOR___SETSTATE___METHODDEF |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2945 | {NULL, NULL} /* sentinel */ |
| 2946 | }; |
| 2947 | |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2948 | static PyTypeObject PyArrayIter_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2949 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2950 | "arrayiterator", /* tp_name */ |
| 2951 | sizeof(arrayiterobject), /* tp_basicsize */ |
| 2952 | 0, /* tp_itemsize */ |
| 2953 | /* methods */ |
| 2954 | (destructor)arrayiter_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 2955 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2956 | 0, /* tp_getattr */ |
| 2957 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 2958 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2959 | 0, /* tp_repr */ |
| 2960 | 0, /* tp_as_number */ |
| 2961 | 0, /* tp_as_sequence */ |
| 2962 | 0, /* tp_as_mapping */ |
| 2963 | 0, /* tp_hash */ |
| 2964 | 0, /* tp_call */ |
| 2965 | 0, /* tp_str */ |
| 2966 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2967 | 0, /* tp_setattro */ |
| 2968 | 0, /* tp_as_buffer */ |
| 2969 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 2970 | 0, /* tp_doc */ |
| 2971 | (traverseproc)arrayiter_traverse, /* tp_traverse */ |
| 2972 | 0, /* tp_clear */ |
| 2973 | 0, /* tp_richcompare */ |
| 2974 | 0, /* tp_weaklistoffset */ |
| 2975 | PyObject_SelfIter, /* tp_iter */ |
| 2976 | (iternextfunc)arrayiter_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2977 | arrayiter_methods, /* tp_methods */ |
Raymond Hettinger | 625812f | 2003-01-07 01:58:52 +0000 | [diff] [blame] | 2978 | }; |
| 2979 | |
| 2980 | |
| 2981 | /*********************** Install Module **************************/ |
| 2982 | |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2983 | /* No functions in array module. */ |
| 2984 | static PyMethodDef a_methods[] = { |
Brett Cannon | 1eb32c2 | 2014-10-10 16:26:45 -0400 | [diff] [blame] | 2985 | ARRAY__ARRAY_RECONSTRUCTOR_METHODDEF |
Martin v. Löwis | 9986633 | 2002-03-01 10:27:01 +0000 | [diff] [blame] | 2986 | {NULL, NULL, 0, NULL} /* Sentinel */ |
| 2987 | }; |
| 2988 | |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 2989 | static int |
| 2990 | array_modexec(PyObject *m) |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 2991 | { |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2992 | char buffer[Py_ARRAY_LENGTH(descriptors)], *p; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2993 | PyObject *typecodes; |
| 2994 | Py_ssize_t size = 0; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 2995 | const struct arraydescr *descr; |
Fred Drake | 0d40ba4 | 2000-02-04 20:33:49 +0000 | [diff] [blame] | 2996 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2997 | if (PyType_Ready(&Arraytype) < 0) |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 2998 | return -1; |
Victor Stinner | d2ec81a | 2020-02-07 09:17:07 +0100 | [diff] [blame] | 2999 | Py_SET_TYPE(&PyArrayIter_Type, &PyType_Type); |
Fred Drake | f4e3484 | 2002-04-01 03:45:06 +0000 | [diff] [blame] | 3000 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3001 | Py_INCREF((PyObject *)&Arraytype); |
Marco Paolini | b44ffc8 | 2019-11-15 08:42:51 +0000 | [diff] [blame] | 3002 | if (PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype) < 0) { |
| 3003 | Py_DECREF((PyObject *)&Arraytype); |
| 3004 | return -1; |
| 3005 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3006 | Py_INCREF((PyObject *)&Arraytype); |
Marco Paolini | b44ffc8 | 2019-11-15 08:42:51 +0000 | [diff] [blame] | 3007 | if (PyModule_AddObject(m, "array", (PyObject *)&Arraytype) < 0) { |
| 3008 | Py_DECREF((PyObject *)&Arraytype); |
| 3009 | return -1; |
| 3010 | } |
Travis E. Oliphant | d5c0add | 2007-10-12 22:05:15 +0000 | [diff] [blame] | 3011 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3012 | for (descr=descriptors; descr->typecode != '\0'; descr++) { |
| 3013 | size++; |
| 3014 | } |
Travis E. Oliphant | d5c0add | 2007-10-12 22:05:15 +0000 | [diff] [blame] | 3015 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3016 | p = buffer; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3017 | for (descr = descriptors; descr->typecode != '\0'; descr++) { |
| 3018 | *p++ = (char)descr->typecode; |
| 3019 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3020 | typecodes = PyUnicode_DecodeASCII(buffer, p - buffer, NULL); |
Marco Paolini | b44ffc8 | 2019-11-15 08:42:51 +0000 | [diff] [blame] | 3021 | if (PyModule_AddObject(m, "typecodes", typecodes) < 0) { |
| 3022 | Py_XDECREF(typecodes); |
| 3023 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3024 | } |
Marco Paolini | b44ffc8 | 2019-11-15 08:42:51 +0000 | [diff] [blame] | 3025 | |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 3026 | return 0; |
| 3027 | } |
| 3028 | |
| 3029 | static PyModuleDef_Slot arrayslots[] = { |
| 3030 | {Py_mod_exec, array_modexec}, |
| 3031 | {0, NULL} |
| 3032 | }; |
| 3033 | |
| 3034 | |
| 3035 | static struct PyModuleDef arraymodule = { |
| 3036 | PyModuleDef_HEAD_INIT, |
| 3037 | "array", |
| 3038 | module_doc, |
| 3039 | 0, |
| 3040 | a_methods, |
| 3041 | arrayslots, |
| 3042 | NULL, |
| 3043 | NULL, |
| 3044 | NULL |
| 3045 | }; |
| 3046 | |
| 3047 | |
| 3048 | PyMODINIT_FUNC |
| 3049 | PyInit_array(void) |
| 3050 | { |
| 3051 | return PyModuleDef_Init(&arraymodule); |
Guido van Rossum | 778983b | 1993-02-19 15:55:02 +0000 | [diff] [blame] | 3052 | } |