Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1 | /* PyByteArray (bytearray) implementation */ |
| 2 | |
| 3 | #define PY_SSIZE_T_CLEAN |
| 4 | #include "Python.h" |
| 5 | #include "structmember.h" |
| 6 | #include "bytes_methods.h" |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 7 | #include "bytesobject.h" |
Gregory P. Smith | 8cb6569 | 2015-04-25 23:22:26 +0000 | [diff] [blame] | 8 | #include "pystrhex.h" |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 9 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 10 | /*[clinic input] |
| 11 | class bytearray "PyByteArrayObject *" "&PyByteArray_Type" |
| 12 | [clinic start generated code]*/ |
| 13 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/ |
| 14 | |
Antoine Pitrou | fc8d6f4 | 2010-01-17 12:38:54 +0000 | [diff] [blame] | 15 | char _PyByteArray_empty_string[] = ""; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 16 | |
| 17 | void |
| 18 | PyByteArray_Fini(void) |
| 19 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | int |
| 23 | PyByteArray_Init(void) |
| 24 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 25 | return 1; |
| 26 | } |
| 27 | |
| 28 | /* end nullbytes support */ |
| 29 | |
| 30 | /* Helpers */ |
| 31 | |
| 32 | static int |
| 33 | _getbytevalue(PyObject* arg, int *value) |
| 34 | { |
| 35 | long face_value; |
| 36 | |
| 37 | if (PyLong_Check(arg)) { |
| 38 | face_value = PyLong_AsLong(arg); |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 39 | } else { |
| 40 | PyObject *index = PyNumber_Index(arg); |
| 41 | if (index == NULL) { |
| 42 | PyErr_Format(PyExc_TypeError, "an integer is required"); |
Mark Dickinson | 10de93a | 2010-07-09 19:25:48 +0000 | [diff] [blame] | 43 | *value = -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 44 | return 0; |
| 45 | } |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 46 | face_value = PyLong_AsLong(index); |
| 47 | Py_DECREF(index); |
| 48 | } |
| 49 | |
| 50 | if (face_value < 0 || face_value >= 256) { |
| 51 | /* this includes the OverflowError in case the long is too large */ |
| 52 | PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
Mark Dickinson | 10de93a | 2010-07-09 19:25:48 +0000 | [diff] [blame] | 53 | *value = -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | *value = face_value; |
| 58 | return 1; |
| 59 | } |
| 60 | |
| 61 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 62 | bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 63 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 64 | void *ptr; |
| 65 | if (view == NULL) { |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 66 | PyErr_SetString(PyExc_BufferError, |
| 67 | "bytearray_getbuffer: view==NULL argument is obsolete"); |
| 68 | return -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 69 | } |
Antoine Pitrou | fc8d6f4 | 2010-01-17 12:38:54 +0000 | [diff] [blame] | 70 | ptr = (void *) PyByteArray_AS_STRING(obj); |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 71 | /* cannot fail if view != NULL and readonly == 0 */ |
| 72 | (void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags); |
| 73 | obj->ob_exports++; |
| 74 | return 0; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static void |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 78 | bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 79 | { |
| 80 | obj->ob_exports--; |
| 81 | } |
| 82 | |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 83 | static int |
| 84 | _canresize(PyByteArrayObject *self) |
| 85 | { |
| 86 | if (self->ob_exports > 0) { |
| 87 | PyErr_SetString(PyExc_BufferError, |
| 88 | "Existing exports of data: object cannot be re-sized"); |
| 89 | return 0; |
| 90 | } |
| 91 | return 1; |
| 92 | } |
| 93 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 94 | #include "clinic/bytearrayobject.c.h" |
| 95 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 96 | /* Direct API functions */ |
| 97 | |
| 98 | PyObject * |
| 99 | PyByteArray_FromObject(PyObject *input) |
| 100 | { |
Victor Stinner | de4ae3d | 2016-12-04 22:59:09 +0100 | [diff] [blame] | 101 | return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type, |
| 102 | input, NULL); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | PyObject * |
| 106 | PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size) |
| 107 | { |
| 108 | PyByteArrayObject *new; |
| 109 | Py_ssize_t alloc; |
| 110 | |
| 111 | if (size < 0) { |
| 112 | PyErr_SetString(PyExc_SystemError, |
| 113 | "Negative size passed to PyByteArray_FromStringAndSize"); |
| 114 | return NULL; |
| 115 | } |
| 116 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 117 | /* Prevent buffer overflow when setting alloc to size+1. */ |
| 118 | if (size == PY_SSIZE_T_MAX) { |
| 119 | return PyErr_NoMemory(); |
| 120 | } |
| 121 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 122 | new = PyObject_New(PyByteArrayObject, &PyByteArray_Type); |
| 123 | if (new == NULL) |
| 124 | return NULL; |
| 125 | |
| 126 | if (size == 0) { |
| 127 | new->ob_bytes = NULL; |
| 128 | alloc = 0; |
| 129 | } |
| 130 | else { |
| 131 | alloc = size + 1; |
Antoine Pitrou | 39aba4f | 2011-11-12 21:15:28 +0100 | [diff] [blame] | 132 | new->ob_bytes = PyObject_Malloc(alloc); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 133 | if (new->ob_bytes == NULL) { |
| 134 | Py_DECREF(new); |
| 135 | return PyErr_NoMemory(); |
| 136 | } |
Antoine Pitrou | fc8d6f4 | 2010-01-17 12:38:54 +0000 | [diff] [blame] | 137 | if (bytes != NULL && size > 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 138 | memcpy(new->ob_bytes, bytes, size); |
| 139 | new->ob_bytes[size] = '\0'; /* Trailing null byte */ |
| 140 | } |
| 141 | Py_SIZE(new) = size; |
| 142 | new->ob_alloc = alloc; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 143 | new->ob_start = new->ob_bytes; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 144 | new->ob_exports = 0; |
| 145 | |
| 146 | return (PyObject *)new; |
| 147 | } |
| 148 | |
| 149 | Py_ssize_t |
| 150 | PyByteArray_Size(PyObject *self) |
| 151 | { |
| 152 | assert(self != NULL); |
| 153 | assert(PyByteArray_Check(self)); |
| 154 | |
| 155 | return PyByteArray_GET_SIZE(self); |
| 156 | } |
| 157 | |
| 158 | char * |
| 159 | PyByteArray_AsString(PyObject *self) |
| 160 | { |
| 161 | assert(self != NULL); |
| 162 | assert(PyByteArray_Check(self)); |
| 163 | |
| 164 | return PyByteArray_AS_STRING(self); |
| 165 | } |
| 166 | |
| 167 | int |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 168 | PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 169 | { |
| 170 | void *sval; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 171 | PyByteArrayObject *obj = ((PyByteArrayObject *)self); |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 172 | /* All computations are done unsigned to avoid integer overflows |
| 173 | (see issue #22335). */ |
| 174 | size_t alloc = (size_t) obj->ob_alloc; |
| 175 | size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes); |
| 176 | size_t size = (size_t) requested_size; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 177 | |
| 178 | assert(self != NULL); |
| 179 | assert(PyByteArray_Check(self)); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 180 | assert(logical_offset <= alloc); |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 181 | assert(requested_size >= 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 182 | |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 183 | if (requested_size == Py_SIZE(self)) { |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 184 | return 0; |
| 185 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 186 | if (!_canresize(obj)) { |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 187 | return -1; |
| 188 | } |
| 189 | |
Antoine Pitrou | 2545411 | 2015-05-19 20:52:27 +0200 | [diff] [blame] | 190 | if (size + logical_offset + 1 <= alloc) { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 191 | /* Current buffer is large enough to host the requested size, |
| 192 | decide on a strategy. */ |
| 193 | if (size < alloc / 2) { |
| 194 | /* Major downsize; resize down to exact size */ |
| 195 | alloc = size + 1; |
| 196 | } |
| 197 | else { |
| 198 | /* Minor downsize; quick exit */ |
| 199 | Py_SIZE(self) = size; |
| 200 | PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */ |
| 201 | return 0; |
| 202 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 203 | } |
| 204 | else { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 205 | /* Need growing, decide on a strategy */ |
| 206 | if (size <= alloc * 1.125) { |
| 207 | /* Moderate upsize; overallocate similar to list_resize() */ |
| 208 | alloc = size + (size >> 3) + (size < 9 ? 3 : 6); |
| 209 | } |
| 210 | else { |
| 211 | /* Major upsize; resize up to exact size */ |
| 212 | alloc = size + 1; |
| 213 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 214 | } |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 215 | if (alloc > PY_SSIZE_T_MAX) { |
| 216 | PyErr_NoMemory(); |
| 217 | return -1; |
| 218 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 219 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 220 | if (logical_offset > 0) { |
| 221 | sval = PyObject_Malloc(alloc); |
| 222 | if (sval == NULL) { |
| 223 | PyErr_NoMemory(); |
| 224 | return -1; |
| 225 | } |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 226 | memcpy(sval, PyByteArray_AS_STRING(self), |
| 227 | Py_MIN(requested_size, Py_SIZE(self))); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 228 | PyObject_Free(obj->ob_bytes); |
| 229 | } |
| 230 | else { |
| 231 | sval = PyObject_Realloc(obj->ob_bytes, alloc); |
| 232 | if (sval == NULL) { |
| 233 | PyErr_NoMemory(); |
| 234 | return -1; |
| 235 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 238 | obj->ob_bytes = obj->ob_start = sval; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 239 | Py_SIZE(self) = size; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 240 | obj->ob_alloc = alloc; |
| 241 | obj->ob_bytes[size] = '\0'; /* Trailing null byte */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | PyObject * |
| 247 | PyByteArray_Concat(PyObject *a, PyObject *b) |
| 248 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 249 | Py_buffer va, vb; |
| 250 | PyByteArrayObject *result = NULL; |
| 251 | |
| 252 | va.len = -1; |
| 253 | vb.len = -1; |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 254 | if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 || |
| 255 | PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 256 | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
| 257 | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
| 258 | goto done; |
| 259 | } |
| 260 | |
Serhiy Storchaka | 06cfb0c | 2016-07-10 20:48:43 +0300 | [diff] [blame] | 261 | if (va.len > PY_SSIZE_T_MAX - vb.len) { |
| 262 | PyErr_NoMemory(); |
| 263 | goto done; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Serhiy Storchaka | 06cfb0c | 2016-07-10 20:48:43 +0300 | [diff] [blame] | 266 | result = (PyByteArrayObject *) \ |
| 267 | PyByteArray_FromStringAndSize(NULL, va.len + vb.len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 268 | if (result != NULL) { |
| 269 | memcpy(result->ob_bytes, va.buf, va.len); |
| 270 | memcpy(result->ob_bytes + va.len, vb.buf, vb.len); |
| 271 | } |
| 272 | |
| 273 | done: |
| 274 | if (va.len != -1) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 275 | PyBuffer_Release(&va); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 276 | if (vb.len != -1) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 277 | PyBuffer_Release(&vb); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 278 | return (PyObject *)result; |
| 279 | } |
| 280 | |
| 281 | /* Functions stuffed into the type object */ |
| 282 | |
| 283 | static Py_ssize_t |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 284 | bytearray_length(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 285 | { |
| 286 | return Py_SIZE(self); |
| 287 | } |
| 288 | |
| 289 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 290 | bytearray_iconcat(PyByteArrayObject *self, PyObject *other) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 291 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 292 | Py_ssize_t size; |
| 293 | Py_buffer vo; |
| 294 | |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 295 | if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 296 | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
| 297 | Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name); |
| 298 | return NULL; |
| 299 | } |
| 300 | |
Serhiy Storchaka | 06cfb0c | 2016-07-10 20:48:43 +0300 | [diff] [blame] | 301 | size = Py_SIZE(self); |
| 302 | if (size > PY_SSIZE_T_MAX - vo.len) { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 303 | PyBuffer_Release(&vo); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 304 | return PyErr_NoMemory(); |
| 305 | } |
Serhiy Storchaka | 06cfb0c | 2016-07-10 20:48:43 +0300 | [diff] [blame] | 306 | if (PyByteArray_Resize((PyObject *)self, size + vo.len) < 0) { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 307 | PyBuffer_Release(&vo); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 308 | return NULL; |
| 309 | } |
Serhiy Storchaka | 06cfb0c | 2016-07-10 20:48:43 +0300 | [diff] [blame] | 310 | memcpy(PyByteArray_AS_STRING(self) + size, vo.buf, vo.len); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 311 | PyBuffer_Release(&vo); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 312 | Py_INCREF(self); |
| 313 | return (PyObject *)self; |
| 314 | } |
| 315 | |
| 316 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 317 | bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 318 | { |
| 319 | PyByteArrayObject *result; |
| 320 | Py_ssize_t mysize; |
| 321 | Py_ssize_t size; |
| 322 | |
| 323 | if (count < 0) |
| 324 | count = 0; |
| 325 | mysize = Py_SIZE(self); |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 326 | if (count > 0 && mysize > PY_SSIZE_T_MAX / count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 327 | return PyErr_NoMemory(); |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 328 | size = mysize * count; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 329 | result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size); |
| 330 | if (result != NULL && size != 0) { |
| 331 | if (mysize == 1) |
| 332 | memset(result->ob_bytes, self->ob_bytes[0], size); |
| 333 | else { |
| 334 | Py_ssize_t i; |
| 335 | for (i = 0; i < count; i++) |
| 336 | memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize); |
| 337 | } |
| 338 | } |
| 339 | return (PyObject *)result; |
| 340 | } |
| 341 | |
| 342 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 343 | bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 344 | { |
| 345 | Py_ssize_t mysize; |
| 346 | Py_ssize_t size; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 347 | char *buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 348 | |
| 349 | if (count < 0) |
| 350 | count = 0; |
| 351 | mysize = Py_SIZE(self); |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 352 | if (count > 0 && mysize > PY_SSIZE_T_MAX / count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 353 | return PyErr_NoMemory(); |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 354 | size = mysize * count; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 355 | if (PyByteArray_Resize((PyObject *)self, size) < 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 356 | return NULL; |
| 357 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 358 | buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 359 | if (mysize == 1) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 360 | memset(buf, buf[0], size); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 361 | else { |
| 362 | Py_ssize_t i; |
| 363 | for (i = 1; i < count; i++) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 364 | memcpy(buf + i*mysize, buf, mysize); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | Py_INCREF(self); |
| 368 | return (PyObject *)self; |
| 369 | } |
| 370 | |
| 371 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 372 | bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 373 | { |
| 374 | if (i < 0) |
| 375 | i += Py_SIZE(self); |
| 376 | if (i < 0 || i >= Py_SIZE(self)) { |
| 377 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 378 | return NULL; |
| 379 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 380 | return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i])); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 384 | bytearray_subscript(PyByteArrayObject *self, PyObject *index) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 385 | { |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 386 | if (PyIndex_Check(index)) { |
| 387 | Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 388 | |
| 389 | if (i == -1 && PyErr_Occurred()) |
| 390 | return NULL; |
| 391 | |
| 392 | if (i < 0) |
| 393 | i += PyByteArray_GET_SIZE(self); |
| 394 | |
| 395 | if (i < 0 || i >= Py_SIZE(self)) { |
| 396 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 397 | return NULL; |
| 398 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 399 | return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i])); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 400 | } |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 401 | else if (PySlice_Check(index)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 402 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 403 | if (PySlice_GetIndicesEx(index, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 404 | PyByteArray_GET_SIZE(self), |
| 405 | &start, &stop, &step, &slicelength) < 0) { |
| 406 | return NULL; |
| 407 | } |
| 408 | |
| 409 | if (slicelength <= 0) |
| 410 | return PyByteArray_FromStringAndSize("", 0); |
| 411 | else if (step == 1) { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 412 | return PyByteArray_FromStringAndSize( |
| 413 | PyByteArray_AS_STRING(self) + start, slicelength); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 414 | } |
| 415 | else { |
| 416 | char *source_buf = PyByteArray_AS_STRING(self); |
Alexandre Vassalotti | e2641f4 | 2009-04-03 06:38:02 +0000 | [diff] [blame] | 417 | char *result_buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 418 | PyObject *result; |
| 419 | |
Alexandre Vassalotti | e2641f4 | 2009-04-03 06:38:02 +0000 | [diff] [blame] | 420 | result = PyByteArray_FromStringAndSize(NULL, slicelength); |
| 421 | if (result == NULL) |
| 422 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 423 | |
Alexandre Vassalotti | e2641f4 | 2009-04-03 06:38:02 +0000 | [diff] [blame] | 424 | result_buf = PyByteArray_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 425 | for (cur = start, i = 0; i < slicelength; |
| 426 | cur += step, i++) { |
| 427 | result_buf[i] = source_buf[cur]; |
| 428 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 429 | return result; |
| 430 | } |
| 431 | } |
| 432 | else { |
Terry Jan Reedy | ffff144 | 2014-08-02 01:30:37 -0400 | [diff] [blame] | 433 | PyErr_Format(PyExc_TypeError, |
| 434 | "bytearray indices must be integers or slices, not %.200s", |
| 435 | Py_TYPE(index)->tp_name); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 436 | return NULL; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | static int |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 441 | bytearray_setslice_linear(PyByteArrayObject *self, |
| 442 | Py_ssize_t lo, Py_ssize_t hi, |
| 443 | char *bytes, Py_ssize_t bytes_len) |
| 444 | { |
| 445 | Py_ssize_t avail = hi - lo; |
| 446 | char *buf = PyByteArray_AS_STRING(self); |
| 447 | Py_ssize_t growth = bytes_len - avail; |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 448 | int res = 0; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 449 | assert(avail >= 0); |
| 450 | |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 451 | if (growth < 0) { |
| 452 | if (!_canresize(self)) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 453 | return -1; |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 454 | |
| 455 | if (lo == 0) { |
| 456 | /* Shrink the buffer by advancing its logical start */ |
| 457 | self->ob_start -= growth; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 458 | /* |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 459 | 0 lo hi old_size |
| 460 | | |<----avail----->|<-----tail------>| |
| 461 | | |<-bytes_len->|<-----tail------>| |
| 462 | 0 new_lo new_hi new_size |
| 463 | */ |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 464 | } |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 465 | else { |
| 466 | /* |
| 467 | 0 lo hi old_size |
| 468 | | |<----avail----->|<-----tomove------>| |
| 469 | | |<-bytes_len->|<-----tomove------>| |
| 470 | 0 lo new_hi new_size |
| 471 | */ |
| 472 | memmove(buf + lo + bytes_len, buf + hi, |
| 473 | Py_SIZE(self) - hi); |
| 474 | } |
| 475 | if (PyByteArray_Resize((PyObject *)self, |
| 476 | Py_SIZE(self) + growth) < 0) { |
| 477 | /* Issue #19578: Handling the memory allocation failure here is |
| 478 | tricky here because the bytearray object has already been |
| 479 | modified. Depending on growth and lo, the behaviour is |
| 480 | different. |
| 481 | |
| 482 | If growth < 0 and lo != 0, the operation is completed, but a |
| 483 | MemoryError is still raised and the memory block is not |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 484 | shrunk. Otherwise, the bytearray is restored in its previous |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 485 | state and a MemoryError is raised. */ |
| 486 | if (lo == 0) { |
| 487 | self->ob_start += growth; |
| 488 | return -1; |
| 489 | } |
| 490 | /* memmove() removed bytes, the bytearray object cannot be |
| 491 | restored in its previous state. */ |
| 492 | Py_SIZE(self) += growth; |
| 493 | res = -1; |
| 494 | } |
| 495 | buf = PyByteArray_AS_STRING(self); |
| 496 | } |
| 497 | else if (growth > 0) { |
| 498 | if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) { |
| 499 | PyErr_NoMemory(); |
| 500 | return -1; |
| 501 | } |
| 502 | |
| 503 | if (PyByteArray_Resize((PyObject *)self, |
| 504 | Py_SIZE(self) + growth) < 0) { |
| 505 | return -1; |
| 506 | } |
| 507 | buf = PyByteArray_AS_STRING(self); |
| 508 | /* Make the place for the additional bytes */ |
| 509 | /* |
| 510 | 0 lo hi old_size |
| 511 | | |<-avail->|<-----tomove------>| |
| 512 | | |<---bytes_len-->|<-----tomove------>| |
| 513 | 0 lo new_hi new_size |
| 514 | */ |
| 515 | memmove(buf + lo + bytes_len, buf + hi, |
| 516 | Py_SIZE(self) - lo - bytes_len); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | if (bytes_len > 0) |
| 520 | memcpy(buf + lo, bytes, bytes_len); |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 521 | return res; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 525 | bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 526 | PyObject *values) |
| 527 | { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 528 | Py_ssize_t needed; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 529 | void *bytes; |
| 530 | Py_buffer vbytes; |
| 531 | int res = 0; |
| 532 | |
| 533 | vbytes.len = -1; |
| 534 | if (values == (PyObject *)self) { |
| 535 | /* Make a copy and call this function recursively */ |
| 536 | int err; |
| 537 | values = PyByteArray_FromObject(values); |
| 538 | if (values == NULL) |
| 539 | return -1; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 540 | err = bytearray_setslice(self, lo, hi, values); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 541 | Py_DECREF(values); |
| 542 | return err; |
| 543 | } |
| 544 | if (values == NULL) { |
| 545 | /* del b[lo:hi] */ |
| 546 | bytes = NULL; |
| 547 | needed = 0; |
| 548 | } |
| 549 | else { |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 550 | if (PyObject_GetBuffer(values, &vbytes, PyBUF_SIMPLE) != 0) { |
| 551 | PyErr_Format(PyExc_TypeError, |
| 552 | "can't set bytearray slice from %.100s", |
| 553 | Py_TYPE(values)->tp_name); |
| 554 | return -1; |
| 555 | } |
| 556 | needed = vbytes.len; |
| 557 | bytes = vbytes.buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | if (lo < 0) |
| 561 | lo = 0; |
| 562 | if (hi < lo) |
| 563 | hi = lo; |
| 564 | if (hi > Py_SIZE(self)) |
| 565 | hi = Py_SIZE(self); |
| 566 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 567 | res = bytearray_setslice_linear(self, lo, hi, bytes, needed); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 568 | if (vbytes.len != -1) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 569 | PyBuffer_Release(&vbytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 570 | return res; |
| 571 | } |
| 572 | |
| 573 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 574 | bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 575 | { |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 576 | int ival; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 577 | |
| 578 | if (i < 0) |
| 579 | i += Py_SIZE(self); |
| 580 | |
| 581 | if (i < 0 || i >= Py_SIZE(self)) { |
| 582 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 583 | return -1; |
| 584 | } |
| 585 | |
| 586 | if (value == NULL) |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 587 | return bytearray_setslice(self, i, i+1, NULL); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 588 | |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 589 | if (!_getbytevalue(value, &ival)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 590 | return -1; |
| 591 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 592 | PyByteArray_AS_STRING(self)[i] = ival; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 597 | bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 598 | { |
| 599 | Py_ssize_t start, stop, step, slicelen, needed; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 600 | char *buf, *bytes; |
| 601 | buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 602 | |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 603 | if (PyIndex_Check(index)) { |
| 604 | Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 605 | |
| 606 | if (i == -1 && PyErr_Occurred()) |
| 607 | return -1; |
| 608 | |
| 609 | if (i < 0) |
| 610 | i += PyByteArray_GET_SIZE(self); |
| 611 | |
| 612 | if (i < 0 || i >= Py_SIZE(self)) { |
| 613 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 614 | return -1; |
| 615 | } |
| 616 | |
| 617 | if (values == NULL) { |
| 618 | /* Fall through to slice assignment */ |
| 619 | start = i; |
| 620 | stop = i + 1; |
| 621 | step = 1; |
| 622 | slicelen = 1; |
| 623 | } |
| 624 | else { |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 625 | int ival; |
| 626 | if (!_getbytevalue(values, &ival)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 627 | return -1; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 628 | buf[i] = (char)ival; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 629 | return 0; |
| 630 | } |
| 631 | } |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 632 | else if (PySlice_Check(index)) { |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 633 | if (PySlice_GetIndicesEx(index, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 634 | PyByteArray_GET_SIZE(self), |
| 635 | &start, &stop, &step, &slicelen) < 0) { |
| 636 | return -1; |
| 637 | } |
| 638 | } |
| 639 | else { |
Terry Jan Reedy | ffff144 | 2014-08-02 01:30:37 -0400 | [diff] [blame] | 640 | PyErr_Format(PyExc_TypeError, |
| 641 | "bytearray indices must be integers or slices, not %.200s", |
| 642 | Py_TYPE(index)->tp_name); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 643 | return -1; |
| 644 | } |
| 645 | |
| 646 | if (values == NULL) { |
| 647 | bytes = NULL; |
| 648 | needed = 0; |
| 649 | } |
| 650 | else if (values == (PyObject *)self || !PyByteArray_Check(values)) { |
Christian Heimes | 6d26ade | 2012-11-03 23:07:59 +0100 | [diff] [blame] | 651 | int err; |
Ezio Melotti | c64bcbe | 2012-11-03 21:19:06 +0200 | [diff] [blame] | 652 | if (PyNumber_Check(values) || PyUnicode_Check(values)) { |
| 653 | PyErr_SetString(PyExc_TypeError, |
| 654 | "can assign only bytes, buffers, or iterables " |
| 655 | "of ints in range(0, 256)"); |
| 656 | return -1; |
| 657 | } |
Georg Brandl | f3fa568 | 2010-12-04 17:09:30 +0000 | [diff] [blame] | 658 | /* Make a copy and call this function recursively */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 659 | values = PyByteArray_FromObject(values); |
| 660 | if (values == NULL) |
| 661 | return -1; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 662 | err = bytearray_ass_subscript(self, index, values); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 663 | Py_DECREF(values); |
| 664 | return err; |
| 665 | } |
| 666 | else { |
| 667 | assert(PyByteArray_Check(values)); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 668 | bytes = PyByteArray_AS_STRING(values); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 669 | needed = Py_SIZE(values); |
| 670 | } |
| 671 | /* Make sure b[5:2] = ... inserts before 5, not before 2. */ |
| 672 | if ((step < 0 && start < stop) || |
| 673 | (step > 0 && start > stop)) |
| 674 | stop = start; |
| 675 | if (step == 1) { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 676 | return bytearray_setslice_linear(self, start, stop, bytes, needed); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 677 | } |
| 678 | else { |
| 679 | if (needed == 0) { |
| 680 | /* Delete slice */ |
Mark Dickinson | bc09964 | 2010-01-29 17:27:24 +0000 | [diff] [blame] | 681 | size_t cur; |
| 682 | Py_ssize_t i; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 683 | |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 684 | if (!_canresize(self)) |
| 685 | return -1; |
Mark Dickinson | 7e3b948 | 2010-08-06 21:33:18 +0000 | [diff] [blame] | 686 | |
| 687 | if (slicelen == 0) |
| 688 | /* Nothing to do here. */ |
| 689 | return 0; |
| 690 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 691 | if (step < 0) { |
| 692 | stop = start + 1; |
| 693 | start = stop + step * (slicelen - 1) - 1; |
| 694 | step = -step; |
| 695 | } |
| 696 | for (cur = start, i = 0; |
| 697 | i < slicelen; cur += step, i++) { |
| 698 | Py_ssize_t lim = step - 1; |
| 699 | |
Mark Dickinson | 66f575b | 2010-02-14 12:53:32 +0000 | [diff] [blame] | 700 | if (cur + step >= (size_t)PyByteArray_GET_SIZE(self)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 701 | lim = PyByteArray_GET_SIZE(self) - cur - 1; |
| 702 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 703 | memmove(buf + cur - i, |
| 704 | buf + cur + 1, lim); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 705 | } |
| 706 | /* Move the tail of the bytes, in one chunk */ |
Mark Dickinson | 7e3b948 | 2010-08-06 21:33:18 +0000 | [diff] [blame] | 707 | cur = start + (size_t)slicelen*step; |
Mark Dickinson | 66f575b | 2010-02-14 12:53:32 +0000 | [diff] [blame] | 708 | if (cur < (size_t)PyByteArray_GET_SIZE(self)) { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 709 | memmove(buf + cur - slicelen, |
| 710 | buf + cur, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 711 | PyByteArray_GET_SIZE(self) - cur); |
| 712 | } |
| 713 | if (PyByteArray_Resize((PyObject *)self, |
| 714 | PyByteArray_GET_SIZE(self) - slicelen) < 0) |
| 715 | return -1; |
| 716 | |
| 717 | return 0; |
| 718 | } |
| 719 | else { |
| 720 | /* Assign slice */ |
Mark Dickinson | 7e3b948 | 2010-08-06 21:33:18 +0000 | [diff] [blame] | 721 | Py_ssize_t i; |
| 722 | size_t cur; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 723 | |
| 724 | if (needed != slicelen) { |
| 725 | PyErr_Format(PyExc_ValueError, |
| 726 | "attempt to assign bytes of size %zd " |
| 727 | "to extended slice of size %zd", |
| 728 | needed, slicelen); |
| 729 | return -1; |
| 730 | } |
| 731 | for (cur = start, i = 0; i < slicelen; cur += step, i++) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 732 | buf[cur] = bytes[i]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 733 | return 0; |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 739 | bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 740 | { |
| 741 | static char *kwlist[] = {"source", "encoding", "errors", 0}; |
| 742 | PyObject *arg = NULL; |
| 743 | const char *encoding = NULL; |
| 744 | const char *errors = NULL; |
| 745 | Py_ssize_t count; |
| 746 | PyObject *it; |
| 747 | PyObject *(*iternext)(PyObject *); |
| 748 | |
| 749 | if (Py_SIZE(self) != 0) { |
| 750 | /* Empty previous contents (yes, do this first of all!) */ |
| 751 | if (PyByteArray_Resize((PyObject *)self, 0) < 0) |
| 752 | return -1; |
| 753 | } |
| 754 | |
| 755 | /* Parse arguments */ |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 756 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 757 | &arg, &encoding, &errors)) |
| 758 | return -1; |
| 759 | |
| 760 | /* Make a quick exit if no first argument */ |
| 761 | if (arg == NULL) { |
| 762 | if (encoding != NULL || errors != NULL) { |
| 763 | PyErr_SetString(PyExc_TypeError, |
| 764 | "encoding or errors without sequence argument"); |
| 765 | return -1; |
| 766 | } |
| 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | if (PyUnicode_Check(arg)) { |
| 771 | /* Encode via the codec registry */ |
| 772 | PyObject *encoded, *new; |
| 773 | if (encoding == NULL) { |
| 774 | PyErr_SetString(PyExc_TypeError, |
| 775 | "string argument without an encoding"); |
| 776 | return -1; |
| 777 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 778 | encoded = PyUnicode_AsEncodedString(arg, encoding, errors); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 779 | if (encoded == NULL) |
| 780 | return -1; |
| 781 | assert(PyBytes_Check(encoded)); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 782 | new = bytearray_iconcat(self, encoded); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 783 | Py_DECREF(encoded); |
| 784 | if (new == NULL) |
| 785 | return -1; |
| 786 | Py_DECREF(new); |
| 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | /* If it's not unicode, there can't be encoding or errors */ |
| 791 | if (encoding != NULL || errors != NULL) { |
| 792 | PyErr_SetString(PyExc_TypeError, |
| 793 | "encoding or errors without a string argument"); |
| 794 | return -1; |
| 795 | } |
| 796 | |
| 797 | /* Is it an int? */ |
Serhiy Storchaka | eb24988 | 2016-08-15 09:46:07 +0300 | [diff] [blame] | 798 | if (PyIndex_Check(arg)) { |
| 799 | count = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
| 800 | if (count == -1 && PyErr_Occurred()) { |
INADA Naoki | a634e23 | 2017-01-06 17:32:01 +0900 | [diff] [blame] | 801 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 802 | return -1; |
INADA Naoki | a634e23 | 2017-01-06 17:32:01 +0900 | [diff] [blame] | 803 | PyErr_Clear(); /* fall through */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 804 | } |
INADA Naoki | a634e23 | 2017-01-06 17:32:01 +0900 | [diff] [blame] | 805 | else { |
| 806 | if (count < 0) { |
| 807 | PyErr_SetString(PyExc_ValueError, "negative count"); |
| 808 | return -1; |
| 809 | } |
| 810 | if (count > 0) { |
| 811 | if (PyByteArray_Resize((PyObject *)self, count)) |
| 812 | return -1; |
| 813 | memset(PyByteArray_AS_STRING(self), 0, count); |
| 814 | } |
| 815 | return 0; |
| 816 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | /* Use the buffer API */ |
| 820 | if (PyObject_CheckBuffer(arg)) { |
| 821 | Py_ssize_t size; |
| 822 | Py_buffer view; |
| 823 | if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0) |
| 824 | return -1; |
| 825 | size = view.len; |
| 826 | if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 827 | if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self), |
| 828 | &view, size, 'C') < 0) |
Stefan Krah | 7d12d9d | 2012-07-28 12:25:55 +0200 | [diff] [blame] | 829 | goto fail; |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 830 | PyBuffer_Release(&view); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 831 | return 0; |
| 832 | fail: |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 833 | PyBuffer_Release(&view); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 834 | return -1; |
| 835 | } |
| 836 | |
| 837 | /* XXX Optimize this if the arguments is a list, tuple */ |
| 838 | |
| 839 | /* Get the iterator */ |
| 840 | it = PyObject_GetIter(arg); |
| 841 | if (it == NULL) |
| 842 | return -1; |
| 843 | iternext = *Py_TYPE(it)->tp_iternext; |
| 844 | |
| 845 | /* Run the iterator to exhaustion */ |
| 846 | for (;;) { |
| 847 | PyObject *item; |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 848 | int rc, value; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 849 | |
| 850 | /* Get the next item */ |
| 851 | item = iternext(it); |
| 852 | if (item == NULL) { |
| 853 | if (PyErr_Occurred()) { |
| 854 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 855 | goto error; |
| 856 | PyErr_Clear(); |
| 857 | } |
| 858 | break; |
| 859 | } |
| 860 | |
| 861 | /* Interpret it as an int (__index__) */ |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 862 | rc = _getbytevalue(item, &value); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 863 | Py_DECREF(item); |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 864 | if (!rc) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 865 | goto error; |
| 866 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 867 | /* Append the byte */ |
Serhiy Storchaka | 7b6e3b9 | 2015-06-29 21:14:06 +0300 | [diff] [blame] | 868 | if (Py_SIZE(self) + 1 < self->ob_alloc) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 869 | Py_SIZE(self)++; |
Serhiy Storchaka | 7b6e3b9 | 2015-06-29 21:14:06 +0300 | [diff] [blame] | 870 | PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0'; |
| 871 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 872 | else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0) |
| 873 | goto error; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 874 | PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | /* Clean up and return success */ |
| 878 | Py_DECREF(it); |
| 879 | return 0; |
| 880 | |
| 881 | error: |
| 882 | /* Error handling when it != NULL */ |
| 883 | Py_DECREF(it); |
| 884 | return -1; |
| 885 | } |
| 886 | |
| 887 | /* Mostly copied from string_repr, but without the |
| 888 | "smart quote" functionality. */ |
| 889 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 890 | bytearray_repr(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 891 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 892 | const char *quote_prefix = "bytearray(b"; |
| 893 | const char *quote_postfix = ")"; |
| 894 | Py_ssize_t length = Py_SIZE(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 895 | /* 15 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */ |
Mark Dickinson | 66f575b | 2010-02-14 12:53:32 +0000 | [diff] [blame] | 896 | size_t newsize; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 897 | PyObject *v; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 898 | Py_ssize_t i; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 899 | char *bytes; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 900 | char c; |
| 901 | char *p; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 902 | int quote; |
| 903 | char *test, *start; |
| 904 | char *buffer; |
| 905 | |
| 906 | if (length > (PY_SSIZE_T_MAX - 15) / 4) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 907 | PyErr_SetString(PyExc_OverflowError, |
| 908 | "bytearray object is too large to make repr"); |
| 909 | return NULL; |
| 910 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 911 | |
| 912 | newsize = 15 + length * 4; |
Antoine Pitrou | 39aba4f | 2011-11-12 21:15:28 +0100 | [diff] [blame] | 913 | buffer = PyObject_Malloc(newsize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 914 | if (buffer == NULL) { |
| 915 | PyErr_NoMemory(); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 916 | return NULL; |
| 917 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 918 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 919 | /* Figure out which quote to use; single is preferred */ |
| 920 | quote = '\''; |
| 921 | start = PyByteArray_AS_STRING(self); |
| 922 | for (test = start; test < start+length; ++test) { |
| 923 | if (*test == '"') { |
| 924 | quote = '\''; /* back to single */ |
| 925 | break; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 926 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 927 | else if (*test == '\'') |
| 928 | quote = '"'; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 929 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 930 | |
| 931 | p = buffer; |
| 932 | while (*quote_prefix) |
| 933 | *p++ = *quote_prefix++; |
| 934 | *p++ = quote; |
| 935 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 936 | bytes = PyByteArray_AS_STRING(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 937 | for (i = 0; i < length; i++) { |
| 938 | /* There's at least enough room for a hex escape |
| 939 | and a closing quote. */ |
| 940 | assert(newsize - (p - buffer) >= 5); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 941 | c = bytes[i]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 942 | if (c == '\'' || c == '\\') |
| 943 | *p++ = '\\', *p++ = c; |
| 944 | else if (c == '\t') |
| 945 | *p++ = '\\', *p++ = 't'; |
| 946 | else if (c == '\n') |
| 947 | *p++ = '\\', *p++ = 'n'; |
| 948 | else if (c == '\r') |
| 949 | *p++ = '\\', *p++ = 'r'; |
| 950 | else if (c == 0) |
| 951 | *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0'; |
| 952 | else if (c < ' ' || c >= 0x7f) { |
| 953 | *p++ = '\\'; |
| 954 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 955 | *p++ = Py_hexdigits[(c & 0xf0) >> 4]; |
| 956 | *p++ = Py_hexdigits[c & 0xf]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 957 | } |
| 958 | else |
| 959 | *p++ = c; |
| 960 | } |
| 961 | assert(newsize - (p - buffer) >= 1); |
| 962 | *p++ = quote; |
| 963 | while (*quote_postfix) { |
| 964 | *p++ = *quote_postfix++; |
| 965 | } |
| 966 | |
| 967 | v = PyUnicode_DecodeASCII(buffer, p - buffer, NULL); |
Antoine Pitrou | 39aba4f | 2011-11-12 21:15:28 +0100 | [diff] [blame] | 968 | PyObject_Free(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 969 | return v; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 973 | bytearray_str(PyObject *op) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 974 | { |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 975 | if (Py_BytesWarningFlag) { |
| 976 | if (PyErr_WarnEx(PyExc_BytesWarning, |
| 977 | "str() on a bytearray instance", 1)) |
| 978 | return NULL; |
| 979 | } |
| 980 | return bytearray_repr((PyByteArrayObject*)op); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 984 | bytearray_richcompare(PyObject *self, PyObject *other, int op) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 985 | { |
| 986 | Py_ssize_t self_size, other_size; |
| 987 | Py_buffer self_bytes, other_bytes; |
| 988 | PyObject *res; |
| 989 | Py_ssize_t minsize; |
Serhiy Storchaka | fa494fd | 2015-05-30 17:45:22 +0300 | [diff] [blame] | 990 | int cmp, rc; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 991 | |
| 992 | /* Bytes can be compared to anything that supports the (binary) |
| 993 | buffer API. Except that a comparison with Unicode is always an |
| 994 | error, even if the comparison is for equality. */ |
Serhiy Storchaka | fa494fd | 2015-05-30 17:45:22 +0300 | [diff] [blame] | 995 | rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type); |
| 996 | if (!rc) |
| 997 | rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type); |
| 998 | if (rc < 0) |
| 999 | return NULL; |
| 1000 | if (rc) { |
Barry Warsaw | 9e9dcd6 | 2008-10-17 01:50:37 +0000 | [diff] [blame] | 1001 | if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1002 | if (PyErr_WarnEx(PyExc_BytesWarning, |
Georg Brandl | e5d68ac | 2008-06-04 11:30:26 +0000 | [diff] [blame] | 1003 | "Comparison between bytearray and string", 1)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1004 | return NULL; |
| 1005 | } |
| 1006 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1007 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1010 | if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1011 | PyErr_Clear(); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1012 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1013 | } |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1014 | self_size = self_bytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1015 | |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1016 | if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1017 | PyErr_Clear(); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1018 | PyBuffer_Release(&self_bytes); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1019 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1020 | } |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1021 | other_size = other_bytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1022 | |
| 1023 | if (self_size != other_size && (op == Py_EQ || op == Py_NE)) { |
| 1024 | /* Shortcut: if the lengths differ, the objects differ */ |
| 1025 | cmp = (op == Py_NE); |
| 1026 | } |
| 1027 | else { |
| 1028 | minsize = self_size; |
| 1029 | if (other_size < minsize) |
| 1030 | minsize = other_size; |
| 1031 | |
| 1032 | cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize); |
| 1033 | /* In ISO C, memcmp() guarantees to use unsigned bytes! */ |
| 1034 | |
| 1035 | if (cmp == 0) { |
| 1036 | if (self_size < other_size) |
| 1037 | cmp = -1; |
| 1038 | else if (self_size > other_size) |
| 1039 | cmp = 1; |
| 1040 | } |
| 1041 | |
| 1042 | switch (op) { |
| 1043 | case Py_LT: cmp = cmp < 0; break; |
| 1044 | case Py_LE: cmp = cmp <= 0; break; |
| 1045 | case Py_EQ: cmp = cmp == 0; break; |
| 1046 | case Py_NE: cmp = cmp != 0; break; |
| 1047 | case Py_GT: cmp = cmp > 0; break; |
| 1048 | case Py_GE: cmp = cmp >= 0; break; |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | res = cmp ? Py_True : Py_False; |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1053 | PyBuffer_Release(&self_bytes); |
| 1054 | PyBuffer_Release(&other_bytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1055 | Py_INCREF(res); |
| 1056 | return res; |
| 1057 | } |
| 1058 | |
| 1059 | static void |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1060 | bytearray_dealloc(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1061 | { |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 1062 | if (self->ob_exports > 0) { |
| 1063 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1064 | "deallocated bytearray object has exported buffers"); |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 1065 | PyErr_Print(); |
| 1066 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1067 | if (self->ob_bytes != 0) { |
Antoine Pitrou | 39aba4f | 2011-11-12 21:15:28 +0100 | [diff] [blame] | 1068 | PyObject_Free(self->ob_bytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1069 | } |
| 1070 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 1071 | } |
| 1072 | |
| 1073 | |
| 1074 | /* -------------------------------------------------------------------- */ |
| 1075 | /* Methods */ |
| 1076 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1077 | #define FASTSEARCH fastsearch |
| 1078 | #define STRINGLIB(F) stringlib_##F |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1079 | #define STRINGLIB_CHAR char |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 1080 | #define STRINGLIB_SIZEOF_CHAR 1 |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1081 | #define STRINGLIB_LEN PyByteArray_GET_SIZE |
| 1082 | #define STRINGLIB_STR PyByteArray_AS_STRING |
| 1083 | #define STRINGLIB_NEW PyByteArray_FromStringAndSize |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1084 | #define STRINGLIB_ISSPACE Py_ISSPACE |
| 1085 | #define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r')) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1086 | #define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact |
| 1087 | #define STRINGLIB_MUTABLE 1 |
| 1088 | |
| 1089 | #include "stringlib/fastsearch.h" |
| 1090 | #include "stringlib/count.h" |
| 1091 | #include "stringlib/find.h" |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 1092 | #include "stringlib/join.h" |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1093 | #include "stringlib/partition.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1094 | #include "stringlib/split.h" |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1095 | #include "stringlib/ctype.h" |
| 1096 | #include "stringlib/transmogrify.h" |
| 1097 | |
| 1098 | |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 1099 | static PyObject * |
| 1100 | bytearray_find(PyByteArrayObject *self, PyObject *args) |
| 1101 | { |
| 1102 | return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
| 1103 | } |
| 1104 | |
| 1105 | static PyObject * |
| 1106 | bytearray_count(PyByteArrayObject *self, PyObject *args) |
| 1107 | { |
| 1108 | return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
| 1109 | } |
| 1110 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1111 | /*[clinic input] |
| 1112 | bytearray.clear |
| 1113 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1114 | Remove all items from the bytearray. |
| 1115 | [clinic start generated code]*/ |
| 1116 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1117 | static PyObject * |
| 1118 | bytearray_clear_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1119 | /*[clinic end generated code: output=85c2fe6aede0956c input=ed6edae9de447ac4]*/ |
Eli Bendersky | 4db28d3 | 2011-03-03 18:21:02 +0000 | [diff] [blame] | 1120 | { |
| 1121 | if (PyByteArray_Resize((PyObject *)self, 0) < 0) |
| 1122 | return NULL; |
| 1123 | Py_RETURN_NONE; |
| 1124 | } |
| 1125 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1126 | /*[clinic input] |
| 1127 | bytearray.copy |
| 1128 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1129 | Return a copy of B. |
| 1130 | [clinic start generated code]*/ |
| 1131 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1132 | static PyObject * |
| 1133 | bytearray_copy_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1134 | /*[clinic end generated code: output=68cfbcfed484c132 input=6597b0c01bccaa9e]*/ |
Eli Bendersky | 4db28d3 | 2011-03-03 18:21:02 +0000 | [diff] [blame] | 1135 | { |
| 1136 | return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self), |
| 1137 | PyByteArray_GET_SIZE(self)); |
| 1138 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1139 | |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 1140 | static PyObject * |
| 1141 | bytearray_index(PyByteArrayObject *self, PyObject *args) |
| 1142 | { |
| 1143 | return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
| 1144 | } |
| 1145 | |
| 1146 | static PyObject * |
| 1147 | bytearray_rfind(PyByteArrayObject *self, PyObject *args) |
| 1148 | { |
| 1149 | return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
| 1150 | } |
| 1151 | |
| 1152 | static PyObject * |
| 1153 | bytearray_rindex(PyByteArrayObject *self, PyObject *args) |
| 1154 | { |
| 1155 | return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
| 1156 | } |
| 1157 | |
| 1158 | static int |
| 1159 | bytearray_contains(PyObject *self, PyObject *arg) |
| 1160 | { |
| 1161 | return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg); |
| 1162 | } |
| 1163 | |
| 1164 | static PyObject * |
| 1165 | bytearray_startswith(PyByteArrayObject *self, PyObject *args) |
| 1166 | { |
| 1167 | return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
| 1168 | } |
| 1169 | |
| 1170 | static PyObject * |
| 1171 | bytearray_endswith(PyByteArrayObject *self, PyObject *args) |
| 1172 | { |
| 1173 | return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
| 1174 | } |
| 1175 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1176 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1177 | /*[clinic input] |
| 1178 | bytearray.translate |
| 1179 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1180 | table: object |
| 1181 | Translation table, which must be a bytes object of length 256. |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1182 | / |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 1183 | delete as deletechars: object(c_default="NULL") = b'' |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1184 | |
| 1185 | Return a copy with each character mapped by the given translation table. |
| 1186 | |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 1187 | All characters occurring in the optional argument delete are removed. |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1188 | The remaining characters are mapped through the given translation table. |
| 1189 | [clinic start generated code]*/ |
| 1190 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1191 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1192 | bytearray_translate_impl(PyByteArrayObject *self, PyObject *table, |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 1193 | PyObject *deletechars) |
| 1194 | /*[clinic end generated code: output=b6a8f01c2a74e446 input=cfff956d4d127a9b]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1195 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1196 | char *input, *output; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1197 | const char *table_chars; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1198 | Py_ssize_t i, c; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1199 | PyObject *input_obj = (PyObject*)self; |
| 1200 | const char *output_start; |
| 1201 | Py_ssize_t inlen; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1202 | PyObject *result = NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1203 | int trans_table[256]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1204 | Py_buffer vtable, vdel; |
| 1205 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1206 | if (table == Py_None) { |
| 1207 | table_chars = NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1208 | table = NULL; |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1209 | } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1210 | return NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1211 | } else { |
| 1212 | if (vtable.len != 256) { |
| 1213 | PyErr_SetString(PyExc_ValueError, |
| 1214 | "translation table must be 256 characters long"); |
Georg Brandl | 953152f | 2009-07-22 12:03:59 +0000 | [diff] [blame] | 1215 | PyBuffer_Release(&vtable); |
| 1216 | return NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1217 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1218 | table_chars = (const char*)vtable.buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1221 | if (deletechars != NULL) { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1222 | if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1223 | if (table != NULL) |
Georg Brandl | 953152f | 2009-07-22 12:03:59 +0000 | [diff] [blame] | 1224 | PyBuffer_Release(&vtable); |
| 1225 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1226 | } |
| 1227 | } |
| 1228 | else { |
| 1229 | vdel.buf = NULL; |
| 1230 | vdel.len = 0; |
| 1231 | } |
| 1232 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1233 | inlen = PyByteArray_GET_SIZE(input_obj); |
| 1234 | result = PyByteArray_FromStringAndSize((char *)NULL, inlen); |
| 1235 | if (result == NULL) |
| 1236 | goto done; |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 1237 | output_start = output = PyByteArray_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1238 | input = PyByteArray_AS_STRING(input_obj); |
| 1239 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1240 | if (vdel.len == 0 && table_chars != NULL) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1241 | /* If no deletions are required, use faster code */ |
| 1242 | for (i = inlen; --i >= 0; ) { |
| 1243 | c = Py_CHARMASK(*input++); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1244 | *output++ = table_chars[c]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1245 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1246 | goto done; |
| 1247 | } |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1248 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1249 | if (table_chars == NULL) { |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1250 | for (i = 0; i < 256; i++) |
| 1251 | trans_table[i] = Py_CHARMASK(i); |
| 1252 | } else { |
| 1253 | for (i = 0; i < 256; i++) |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1254 | trans_table[i] = Py_CHARMASK(table_chars[i]); |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1255 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1256 | |
| 1257 | for (i = 0; i < vdel.len; i++) |
| 1258 | trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1; |
| 1259 | |
| 1260 | for (i = inlen; --i >= 0; ) { |
| 1261 | c = Py_CHARMASK(*input++); |
| 1262 | if (trans_table[c] != -1) |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 1263 | *output++ = (char)trans_table[c]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1264 | } |
| 1265 | /* Fix the size of the resulting string */ |
| 1266 | if (inlen > 0) |
Christian Heimes | c731bbe | 2013-07-21 02:04:35 +0200 | [diff] [blame] | 1267 | if (PyByteArray_Resize(result, output - output_start) < 0) { |
| 1268 | Py_CLEAR(result); |
| 1269 | goto done; |
| 1270 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1271 | |
| 1272 | done: |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1273 | if (table != NULL) |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1274 | PyBuffer_Release(&vtable); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1275 | if (deletechars != NULL) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1276 | PyBuffer_Release(&vdel); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1277 | return result; |
| 1278 | } |
| 1279 | |
| 1280 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1281 | /*[clinic input] |
| 1282 | |
| 1283 | @staticmethod |
| 1284 | bytearray.maketrans |
| 1285 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1286 | frm: Py_buffer |
| 1287 | to: Py_buffer |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1288 | / |
| 1289 | |
| 1290 | Return a translation table useable for the bytes or bytearray translate method. |
| 1291 | |
| 1292 | The returned table will be one where each byte in frm is mapped to the byte at |
| 1293 | the same position in to. |
| 1294 | |
| 1295 | The bytes objects frm and to must be of the same length. |
| 1296 | [clinic start generated code]*/ |
| 1297 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1298 | static PyObject * |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1299 | bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1300 | /*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1301 | { |
| 1302 | return _Py_bytes_maketrans(frm, to); |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1306 | /*[clinic input] |
| 1307 | bytearray.replace |
| 1308 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1309 | old: Py_buffer |
| 1310 | new: Py_buffer |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1311 | count: Py_ssize_t = -1 |
| 1312 | Maximum number of occurrences to replace. |
| 1313 | -1 (the default value) means replace all occurrences. |
| 1314 | / |
| 1315 | |
| 1316 | Return a copy with all occurrences of substring old replaced by new. |
| 1317 | |
| 1318 | If the optional argument count is given, only the first count occurrences are |
| 1319 | replaced. |
| 1320 | [clinic start generated code]*/ |
| 1321 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1322 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1323 | bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old, |
| 1324 | Py_buffer *new, Py_ssize_t count) |
| 1325 | /*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1326 | { |
Serhiy Storchaka | fb81d3c | 2016-05-05 09:26:07 +0300 | [diff] [blame] | 1327 | return stringlib_replace((PyObject *)self, |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 1328 | (const char *)old->buf, old->len, |
| 1329 | (const char *)new->buf, new->len, count); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1332 | /*[clinic input] |
| 1333 | bytearray.split |
| 1334 | |
| 1335 | sep: object = None |
| 1336 | The delimiter according which to split the bytearray. |
| 1337 | None (the default value) means split on ASCII whitespace characters |
| 1338 | (space, tab, return, newline, formfeed, vertical tab). |
| 1339 | maxsplit: Py_ssize_t = -1 |
| 1340 | Maximum number of splits to do. |
| 1341 | -1 (the default value) means no limit. |
| 1342 | |
| 1343 | Return a list of the sections in the bytearray, using sep as the delimiter. |
| 1344 | [clinic start generated code]*/ |
| 1345 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1346 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1347 | bytearray_split_impl(PyByteArrayObject *self, PyObject *sep, |
| 1348 | Py_ssize_t maxsplit) |
| 1349 | /*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1350 | { |
| 1351 | Py_ssize_t len = PyByteArray_GET_SIZE(self), n; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1352 | const char *s = PyByteArray_AS_STRING(self), *sub; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1353 | PyObject *list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1354 | Py_buffer vsub; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1355 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1356 | if (maxsplit < 0) |
| 1357 | maxsplit = PY_SSIZE_T_MAX; |
| 1358 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1359 | if (sep == Py_None) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1360 | return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1361 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1362 | if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1363 | return NULL; |
| 1364 | sub = vsub.buf; |
| 1365 | n = vsub.len; |
| 1366 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1367 | list = stringlib_split( |
| 1368 | (PyObject*) self, s, len, sub, n, maxsplit |
| 1369 | ); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1370 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1371 | return list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1374 | /*[clinic input] |
| 1375 | bytearray.partition |
| 1376 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1377 | sep: object |
| 1378 | / |
| 1379 | |
| 1380 | Partition the bytearray into three parts using the given separator. |
| 1381 | |
| 1382 | This will search for the separator sep in the bytearray. If the separator is |
| 1383 | found, returns a 3-tuple containing the part before the separator, the |
| 1384 | separator itself, and the part after it. |
| 1385 | |
| 1386 | If the separator is not found, returns a 3-tuple containing the original |
| 1387 | bytearray object and two empty bytearray objects. |
| 1388 | [clinic start generated code]*/ |
| 1389 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1390 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1391 | bytearray_partition(PyByteArrayObject *self, PyObject *sep) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1392 | /*[clinic end generated code: output=45d2525ddd35f957 input=86f89223892b70b5]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1393 | { |
| 1394 | PyObject *bytesep, *result; |
| 1395 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1396 | bytesep = PyByteArray_FromObject(sep); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1397 | if (! bytesep) |
| 1398 | return NULL; |
| 1399 | |
| 1400 | result = stringlib_partition( |
| 1401 | (PyObject*) self, |
| 1402 | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
| 1403 | bytesep, |
| 1404 | PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) |
| 1405 | ); |
| 1406 | |
| 1407 | Py_DECREF(bytesep); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1408 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1411 | /*[clinic input] |
| 1412 | bytearray.rpartition |
| 1413 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1414 | sep: object |
| 1415 | / |
| 1416 | |
| 1417 | Partition the bytes into three parts using the given separator. |
| 1418 | |
| 1419 | This will search for the separator sep in the bytearray, starting and the end. |
| 1420 | If the separator is found, returns a 3-tuple containing the part before the |
| 1421 | separator, the separator itself, and the part after it. |
| 1422 | |
| 1423 | If the separator is not found, returns a 3-tuple containing two empty bytearray |
| 1424 | objects and the original bytearray object. |
| 1425 | [clinic start generated code]*/ |
| 1426 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1427 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1428 | bytearray_rpartition(PyByteArrayObject *self, PyObject *sep) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1429 | /*[clinic end generated code: output=440de3c9426115e8 input=5f4094f2de87c8f3]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1430 | { |
| 1431 | PyObject *bytesep, *result; |
| 1432 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1433 | bytesep = PyByteArray_FromObject(sep); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1434 | if (! bytesep) |
| 1435 | return NULL; |
| 1436 | |
| 1437 | result = stringlib_rpartition( |
| 1438 | (PyObject*) self, |
| 1439 | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
| 1440 | bytesep, |
| 1441 | PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) |
| 1442 | ); |
| 1443 | |
| 1444 | Py_DECREF(bytesep); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1445 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1448 | /*[clinic input] |
| 1449 | bytearray.rsplit = bytearray.split |
| 1450 | |
| 1451 | Return a list of the sections in the bytearray, using sep as the delimiter. |
| 1452 | |
| 1453 | Splitting is done starting at the end of the bytearray and working to the front. |
| 1454 | [clinic start generated code]*/ |
| 1455 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1456 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1457 | bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep, |
| 1458 | Py_ssize_t maxsplit) |
| 1459 | /*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1460 | { |
| 1461 | Py_ssize_t len = PyByteArray_GET_SIZE(self), n; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1462 | const char *s = PyByteArray_AS_STRING(self), *sub; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1463 | PyObject *list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1464 | Py_buffer vsub; |
| 1465 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1466 | if (maxsplit < 0) |
| 1467 | maxsplit = PY_SSIZE_T_MAX; |
| 1468 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1469 | if (sep == Py_None) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1470 | return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1471 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1472 | if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1473 | return NULL; |
| 1474 | sub = vsub.buf; |
| 1475 | n = vsub.len; |
| 1476 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1477 | list = stringlib_rsplit( |
| 1478 | (PyObject*) self, s, len, sub, n, maxsplit |
| 1479 | ); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1480 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1481 | return list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1484 | /*[clinic input] |
| 1485 | bytearray.reverse |
| 1486 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1487 | Reverse the order of the values in B in place. |
| 1488 | [clinic start generated code]*/ |
| 1489 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1490 | static PyObject * |
| 1491 | bytearray_reverse_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1492 | /*[clinic end generated code: output=9f7616f29ab309d3 input=543356319fc78557]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1493 | { |
| 1494 | char swap, *head, *tail; |
| 1495 | Py_ssize_t i, j, n = Py_SIZE(self); |
| 1496 | |
| 1497 | j = n / 2; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1498 | head = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1499 | tail = head + n - 1; |
| 1500 | for (i = 0; i < j; i++) { |
| 1501 | swap = *head; |
| 1502 | *head++ = *tail; |
| 1503 | *tail-- = swap; |
| 1504 | } |
| 1505 | |
| 1506 | Py_RETURN_NONE; |
| 1507 | } |
| 1508 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1509 | |
| 1510 | /*[python input] |
| 1511 | class bytesvalue_converter(CConverter): |
| 1512 | type = 'int' |
| 1513 | converter = '_getbytevalue' |
| 1514 | [python start generated code]*/ |
| 1515 | /*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/ |
| 1516 | |
| 1517 | |
| 1518 | /*[clinic input] |
| 1519 | bytearray.insert |
| 1520 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1521 | index: Py_ssize_t |
| 1522 | The index where the value is to be inserted. |
| 1523 | item: bytesvalue |
| 1524 | The item to be inserted. |
| 1525 | / |
| 1526 | |
| 1527 | Insert a single item into the bytearray before the given index. |
| 1528 | [clinic start generated code]*/ |
| 1529 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1530 | static PyObject * |
| 1531 | bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1532 | /*[clinic end generated code: output=76c775a70e7b07b7 input=b2b5d07e9de6c070]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1533 | { |
| 1534 | Py_ssize_t n = Py_SIZE(self); |
| 1535 | char *buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1536 | |
| 1537 | if (n == PY_SSIZE_T_MAX) { |
| 1538 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 1539 | "cannot add more objects to bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1540 | return NULL; |
| 1541 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1542 | if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) |
| 1543 | return NULL; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1544 | buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1545 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1546 | if (index < 0) { |
| 1547 | index += n; |
| 1548 | if (index < 0) |
| 1549 | index = 0; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1550 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1551 | if (index > n) |
| 1552 | index = n; |
| 1553 | memmove(buf + index + 1, buf + index, n - index); |
| 1554 | buf[index] = item; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1555 | |
| 1556 | Py_RETURN_NONE; |
| 1557 | } |
| 1558 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1559 | /*[clinic input] |
| 1560 | bytearray.append |
| 1561 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1562 | item: bytesvalue |
| 1563 | The item to be appended. |
| 1564 | / |
| 1565 | |
| 1566 | Append a single item to the end of the bytearray. |
| 1567 | [clinic start generated code]*/ |
| 1568 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1569 | static PyObject * |
| 1570 | bytearray_append_impl(PyByteArrayObject *self, int item) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1571 | /*[clinic end generated code: output=a154e19ed1886cb6 input=20d6bec3d1340593]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1572 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1573 | Py_ssize_t n = Py_SIZE(self); |
| 1574 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1575 | if (n == PY_SSIZE_T_MAX) { |
| 1576 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 1577 | "cannot add more objects to bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1578 | return NULL; |
| 1579 | } |
| 1580 | if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) |
| 1581 | return NULL; |
| 1582 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1583 | PyByteArray_AS_STRING(self)[n] = item; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1584 | |
| 1585 | Py_RETURN_NONE; |
| 1586 | } |
| 1587 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1588 | /*[clinic input] |
| 1589 | bytearray.extend |
| 1590 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1591 | iterable_of_ints: object |
| 1592 | The iterable of items to append. |
| 1593 | / |
| 1594 | |
| 1595 | Append all the items from the iterator or sequence to the end of the bytearray. |
| 1596 | [clinic start generated code]*/ |
| 1597 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1598 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1599 | bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1600 | /*[clinic end generated code: output=98155dbe249170b1 input=c617b3a93249ba28]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1601 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1602 | PyObject *it, *item, *bytearray_obj; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1603 | Py_ssize_t buf_size = 0, len = 0; |
| 1604 | int value; |
| 1605 | char *buf; |
| 1606 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1607 | /* bytearray_setslice code only accepts something supporting PEP 3118. */ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1608 | if (PyObject_CheckBuffer(iterable_of_ints)) { |
| 1609 | if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_ints) == -1) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1610 | return NULL; |
| 1611 | |
| 1612 | Py_RETURN_NONE; |
| 1613 | } |
| 1614 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1615 | it = PyObject_GetIter(iterable_of_ints); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1616 | if (it == NULL) |
| 1617 | return NULL; |
| 1618 | |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 1619 | /* Try to determine the length of the argument. 32 is arbitrary. */ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1620 | buf_size = PyObject_LengthHint(iterable_of_ints, 32); |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1621 | if (buf_size == -1) { |
| 1622 | Py_DECREF(it); |
| 1623 | return NULL; |
| 1624 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1625 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1626 | bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size); |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 1627 | if (bytearray_obj == NULL) { |
| 1628 | Py_DECREF(it); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1629 | return NULL; |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 1630 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1631 | buf = PyByteArray_AS_STRING(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1632 | |
| 1633 | while ((item = PyIter_Next(it)) != NULL) { |
| 1634 | if (! _getbytevalue(item, &value)) { |
| 1635 | Py_DECREF(item); |
| 1636 | Py_DECREF(it); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1637 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1638 | return NULL; |
| 1639 | } |
| 1640 | buf[len++] = value; |
| 1641 | Py_DECREF(item); |
| 1642 | |
| 1643 | if (len >= buf_size) { |
Martin Panter | 371731e | 2016-07-18 07:53:13 +0000 | [diff] [blame] | 1644 | Py_ssize_t addition; |
| 1645 | if (len == PY_SSIZE_T_MAX) { |
| 1646 | Py_DECREF(it); |
| 1647 | Py_DECREF(bytearray_obj); |
| 1648 | return PyErr_NoMemory(); |
| 1649 | } |
| 1650 | addition = len >> 1; |
| 1651 | if (addition > PY_SSIZE_T_MAX - len - 1) |
| 1652 | buf_size = PY_SSIZE_T_MAX; |
| 1653 | else |
| 1654 | buf_size = len + addition + 1; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1655 | if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1656 | Py_DECREF(it); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1657 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1658 | return NULL; |
| 1659 | } |
| 1660 | /* Recompute the `buf' pointer, since the resizing operation may |
| 1661 | have invalidated it. */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1662 | buf = PyByteArray_AS_STRING(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1663 | } |
| 1664 | } |
| 1665 | Py_DECREF(it); |
| 1666 | |
| 1667 | /* Resize down to exact size. */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1668 | if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) { |
| 1669 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1670 | return NULL; |
| 1671 | } |
| 1672 | |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 1673 | if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) { |
| 1674 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1675 | return NULL; |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 1676 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1677 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1678 | |
| 1679 | Py_RETURN_NONE; |
| 1680 | } |
| 1681 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1682 | /*[clinic input] |
| 1683 | bytearray.pop |
| 1684 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1685 | index: Py_ssize_t = -1 |
| 1686 | The index from where to remove the item. |
| 1687 | -1 (the default value) means remove the last item. |
| 1688 | / |
| 1689 | |
| 1690 | Remove and return a single item from B. |
| 1691 | |
| 1692 | If no index argument is given, will pop the last item. |
| 1693 | [clinic start generated code]*/ |
| 1694 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1695 | static PyObject * |
| 1696 | bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1697 | /*[clinic end generated code: output=e0ccd401f8021da8 input=3591df2d06c0d237]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1698 | { |
| 1699 | int value; |
| 1700 | Py_ssize_t n = Py_SIZE(self); |
| 1701 | char *buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1702 | |
| 1703 | if (n == 0) { |
Eli Bendersky | 1bc4f19 | 2011-03-04 04:55:25 +0000 | [diff] [blame] | 1704 | PyErr_SetString(PyExc_IndexError, |
| 1705 | "pop from empty bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1706 | return NULL; |
| 1707 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1708 | if (index < 0) |
| 1709 | index += Py_SIZE(self); |
| 1710 | if (index < 0 || index >= Py_SIZE(self)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1711 | PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
| 1712 | return NULL; |
| 1713 | } |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 1714 | if (!_canresize(self)) |
| 1715 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1716 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1717 | buf = PyByteArray_AS_STRING(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1718 | value = buf[index]; |
| 1719 | memmove(buf + index, buf + index + 1, n - index); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1720 | if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) |
| 1721 | return NULL; |
| 1722 | |
Mark Dickinson | 54a3db9 | 2009-09-06 10:19:23 +0000 | [diff] [blame] | 1723 | return PyLong_FromLong((unsigned char)value); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1726 | /*[clinic input] |
| 1727 | bytearray.remove |
| 1728 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1729 | value: bytesvalue |
| 1730 | The value to remove. |
| 1731 | / |
| 1732 | |
| 1733 | Remove the first occurrence of a value in the bytearray. |
| 1734 | [clinic start generated code]*/ |
| 1735 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1736 | static PyObject * |
| 1737 | bytearray_remove_impl(PyByteArrayObject *self, int value) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1738 | /*[clinic end generated code: output=d659e37866709c13 input=121831240cd51ddf]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1739 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1740 | Py_ssize_t where, n = Py_SIZE(self); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1741 | char *buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1742 | |
Serhiy Storchaka | 4b23494 | 2016-05-16 22:24:03 +0300 | [diff] [blame] | 1743 | where = stringlib_find_char(buf, n, value); |
| 1744 | if (where < 0) { |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 1745 | PyErr_SetString(PyExc_ValueError, "value not found in bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1746 | return NULL; |
| 1747 | } |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 1748 | if (!_canresize(self)) |
| 1749 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1750 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1751 | memmove(buf + where, buf + where + 1, n - where); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1752 | if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) |
| 1753 | return NULL; |
| 1754 | |
| 1755 | Py_RETURN_NONE; |
| 1756 | } |
| 1757 | |
| 1758 | /* XXX These two helpers could be optimized if argsize == 1 */ |
| 1759 | |
| 1760 | static Py_ssize_t |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1761 | lstrip_helper(const char *myptr, Py_ssize_t mysize, |
| 1762 | const void *argptr, Py_ssize_t argsize) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1763 | { |
| 1764 | Py_ssize_t i = 0; |
Antoine Pitrou | 5b72075 | 2013-10-05 21:24:10 +0200 | [diff] [blame] | 1765 | while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1766 | i++; |
| 1767 | return i; |
| 1768 | } |
| 1769 | |
| 1770 | static Py_ssize_t |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1771 | rstrip_helper(const char *myptr, Py_ssize_t mysize, |
| 1772 | const void *argptr, Py_ssize_t argsize) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1773 | { |
| 1774 | Py_ssize_t i = mysize - 1; |
Antoine Pitrou | 5b72075 | 2013-10-05 21:24:10 +0200 | [diff] [blame] | 1775 | while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1776 | i--; |
| 1777 | return i + 1; |
| 1778 | } |
| 1779 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1780 | /*[clinic input] |
| 1781 | bytearray.strip |
| 1782 | |
| 1783 | bytes: object = None |
| 1784 | / |
| 1785 | |
| 1786 | Strip leading and trailing bytes contained in the argument. |
| 1787 | |
| 1788 | If the argument is omitted or None, strip leading and trailing ASCII whitespace. |
| 1789 | [clinic start generated code]*/ |
| 1790 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1791 | static PyObject * |
| 1792 | bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1793 | /*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1794 | { |
| 1795 | Py_ssize_t left, right, mysize, byteslen; |
| 1796 | char *myptr, *bytesptr; |
| 1797 | Py_buffer vbytes; |
| 1798 | |
| 1799 | if (bytes == Py_None) { |
| 1800 | bytesptr = "\t\n\r\f\v "; |
| 1801 | byteslen = 6; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1802 | } |
| 1803 | else { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1804 | if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1805 | return NULL; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1806 | bytesptr = (char *) vbytes.buf; |
| 1807 | byteslen = vbytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1808 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1809 | myptr = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1810 | mysize = Py_SIZE(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1811 | left = lstrip_helper(myptr, mysize, bytesptr, byteslen); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1812 | if (left == mysize) |
| 1813 | right = left; |
| 1814 | else |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1815 | right = rstrip_helper(myptr, mysize, bytesptr, byteslen); |
| 1816 | if (bytes != Py_None) |
| 1817 | PyBuffer_Release(&vbytes); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1818 | return PyByteArray_FromStringAndSize(myptr + left, right - left); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1821 | /*[clinic input] |
| 1822 | bytearray.lstrip |
| 1823 | |
| 1824 | bytes: object = None |
| 1825 | / |
| 1826 | |
| 1827 | Strip leading bytes contained in the argument. |
| 1828 | |
| 1829 | If the argument is omitted or None, strip leading ASCII whitespace. |
| 1830 | [clinic start generated code]*/ |
| 1831 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1832 | static PyObject * |
| 1833 | bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1834 | /*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1835 | { |
| 1836 | Py_ssize_t left, right, mysize, byteslen; |
| 1837 | char *myptr, *bytesptr; |
| 1838 | Py_buffer vbytes; |
| 1839 | |
| 1840 | if (bytes == Py_None) { |
| 1841 | bytesptr = "\t\n\r\f\v "; |
| 1842 | byteslen = 6; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1843 | } |
| 1844 | else { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1845 | if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1846 | return NULL; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1847 | bytesptr = (char *) vbytes.buf; |
| 1848 | byteslen = vbytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1849 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1850 | myptr = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1851 | mysize = Py_SIZE(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1852 | left = lstrip_helper(myptr, mysize, bytesptr, byteslen); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1853 | right = mysize; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1854 | if (bytes != Py_None) |
| 1855 | PyBuffer_Release(&vbytes); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1856 | return PyByteArray_FromStringAndSize(myptr + left, right - left); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1859 | /*[clinic input] |
| 1860 | bytearray.rstrip |
| 1861 | |
| 1862 | bytes: object = None |
| 1863 | / |
| 1864 | |
| 1865 | Strip trailing bytes contained in the argument. |
| 1866 | |
| 1867 | If the argument is omitted or None, strip trailing ASCII whitespace. |
| 1868 | [clinic start generated code]*/ |
| 1869 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1870 | static PyObject * |
| 1871 | bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1872 | /*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1873 | { |
| 1874 | Py_ssize_t right, mysize, byteslen; |
| 1875 | char *myptr, *bytesptr; |
| 1876 | Py_buffer vbytes; |
| 1877 | |
| 1878 | if (bytes == Py_None) { |
| 1879 | bytesptr = "\t\n\r\f\v "; |
| 1880 | byteslen = 6; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1881 | } |
| 1882 | else { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1883 | if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1884 | return NULL; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1885 | bytesptr = (char *) vbytes.buf; |
| 1886 | byteslen = vbytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1887 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1888 | myptr = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1889 | mysize = Py_SIZE(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1890 | right = rstrip_helper(myptr, mysize, bytesptr, byteslen); |
| 1891 | if (bytes != Py_None) |
| 1892 | PyBuffer_Release(&vbytes); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1893 | return PyByteArray_FromStringAndSize(myptr, right); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1896 | /*[clinic input] |
| 1897 | bytearray.decode |
| 1898 | |
| 1899 | encoding: str(c_default="NULL") = 'utf-8' |
| 1900 | The encoding with which to decode the bytearray. |
| 1901 | errors: str(c_default="NULL") = 'strict' |
| 1902 | The error handling scheme to use for the handling of decoding errors. |
| 1903 | The default is 'strict' meaning that decoding errors raise a |
| 1904 | UnicodeDecodeError. Other possible values are 'ignore' and 'replace' |
| 1905 | as well as any other name registered with codecs.register_error that |
| 1906 | can handle UnicodeDecodeErrors. |
| 1907 | |
| 1908 | Decode the bytearray using the codec registered for encoding. |
| 1909 | [clinic start generated code]*/ |
| 1910 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1911 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1912 | bytearray_decode_impl(PyByteArrayObject *self, const char *encoding, |
| 1913 | const char *errors) |
| 1914 | /*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1915 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1916 | if (encoding == NULL) |
| 1917 | encoding = PyUnicode_GetDefaultEncoding(); |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1918 | return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | PyDoc_STRVAR(alloc_doc, |
| 1922 | "B.__alloc__() -> int\n\ |
| 1923 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1924 | Return the number of bytes actually allocated."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1925 | |
| 1926 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1927 | bytearray_alloc(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1928 | { |
| 1929 | return PyLong_FromSsize_t(self->ob_alloc); |
| 1930 | } |
| 1931 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1932 | /*[clinic input] |
| 1933 | bytearray.join |
| 1934 | |
| 1935 | iterable_of_bytes: object |
| 1936 | / |
| 1937 | |
| 1938 | Concatenate any number of bytes/bytearray objects. |
| 1939 | |
| 1940 | The bytearray whose method is called is inserted in between each pair. |
| 1941 | |
| 1942 | The result is returned as a new bytearray object. |
| 1943 | [clinic start generated code]*/ |
| 1944 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1945 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1946 | bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1947 | /*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1948 | { |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1949 | return stringlib_bytes_join((PyObject*)self, iterable_of_bytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1952 | /*[clinic input] |
| 1953 | bytearray.splitlines |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1954 | |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame^] | 1955 | keepends: bool(accept={int}) = False |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1956 | |
| 1957 | Return a list of the lines in the bytearray, breaking at line boundaries. |
| 1958 | |
| 1959 | Line breaks are not included in the resulting list unless keepends is given and |
| 1960 | true. |
| 1961 | [clinic start generated code]*/ |
| 1962 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1963 | static PyObject * |
| 1964 | bytearray_splitlines_impl(PyByteArrayObject *self, int keepends) |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame^] | 1965 | /*[clinic end generated code: output=4223c94b895f6ad9 input=99a27ad959b9cf6b]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1966 | { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1967 | return stringlib_splitlines( |
| 1968 | (PyObject*) self, PyByteArray_AS_STRING(self), |
| 1969 | PyByteArray_GET_SIZE(self), keepends |
| 1970 | ); |
| 1971 | } |
| 1972 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1973 | /*[clinic input] |
| 1974 | @classmethod |
| 1975 | bytearray.fromhex |
| 1976 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1977 | string: unicode |
| 1978 | / |
| 1979 | |
| 1980 | Create a bytearray object from a string of hexadecimal numbers. |
| 1981 | |
| 1982 | Spaces between two numbers are accepted. |
| 1983 | Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef') |
| 1984 | [clinic start generated code]*/ |
| 1985 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1986 | static PyObject * |
Serhiy Storchaka | 0855e70 | 2016-07-01 17:22:31 +0300 | [diff] [blame] | 1987 | bytearray_fromhex_impl(PyTypeObject *type, PyObject *string) |
| 1988 | /*[clinic end generated code: output=8f0f0b6d30fb3ba0 input=f033a16d1fb21f48]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1989 | { |
Serhiy Storchaka | 0855e70 | 2016-07-01 17:22:31 +0300 | [diff] [blame] | 1990 | PyObject *result = _PyBytes_FromHex(string, type == &PyByteArray_Type); |
| 1991 | if (type != &PyByteArray_Type && result != NULL) { |
Victor Stinner | de4ae3d | 2016-12-04 22:59:09 +0100 | [diff] [blame] | 1992 | Py_SETREF(result, PyObject_CallFunctionObjArgs((PyObject *)type, |
| 1993 | result, NULL)); |
Serhiy Storchaka | 0855e70 | 2016-07-01 17:22:31 +0300 | [diff] [blame] | 1994 | } |
| 1995 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
Gregory P. Smith | 8cb6569 | 2015-04-25 23:22:26 +0000 | [diff] [blame] | 1998 | PyDoc_STRVAR(hex__doc__, |
| 1999 | "B.hex() -> string\n\ |
| 2000 | \n\ |
| 2001 | Create a string of hexadecimal numbers from a bytearray object.\n\ |
| 2002 | Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'."); |
| 2003 | |
| 2004 | static PyObject * |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2005 | bytearray_hex(PyBytesObject *self) |
| 2006 | { |
| 2007 | char* argbuf = PyByteArray_AS_STRING(self); |
| 2008 | Py_ssize_t arglen = PyByteArray_GET_SIZE(self); |
| 2009 | return _Py_strhex(argbuf, arglen); |
| 2010 | } |
| 2011 | |
| 2012 | static PyObject * |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2013 | _common_reduce(PyByteArrayObject *self, int proto) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2014 | { |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2015 | PyObject *dict; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2016 | _Py_IDENTIFIER(__dict__); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2017 | char *buf; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2018 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2019 | dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2020 | if (dict == NULL) { |
| 2021 | PyErr_Clear(); |
| 2022 | dict = Py_None; |
| 2023 | Py_INCREF(dict); |
| 2024 | } |
| 2025 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2026 | buf = PyByteArray_AS_STRING(self); |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2027 | if (proto < 3) { |
| 2028 | /* use str based reduction for backwards compatibility with Python 2.x */ |
| 2029 | PyObject *latin1; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2030 | if (Py_SIZE(self)) |
| 2031 | latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL); |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2032 | else |
| 2033 | latin1 = PyUnicode_FromString(""); |
| 2034 | return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict); |
| 2035 | } |
| 2036 | else { |
| 2037 | /* use more efficient byte based reduction */ |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2038 | if (Py_SIZE(self)) { |
| 2039 | return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict); |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2040 | } |
| 2041 | else { |
| 2042 | return Py_BuildValue("(O()N)", Py_TYPE(self), dict); |
| 2043 | } |
| 2044 | } |
| 2045 | } |
| 2046 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2047 | /*[clinic input] |
| 2048 | bytearray.__reduce__ as bytearray_reduce |
| 2049 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2050 | Return state information for pickling. |
| 2051 | [clinic start generated code]*/ |
| 2052 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2053 | static PyObject * |
| 2054 | bytearray_reduce_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 2055 | /*[clinic end generated code: output=52bf304086464cab input=44b5737ada62dd3f]*/ |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2056 | { |
| 2057 | return _common_reduce(self, 2); |
| 2058 | } |
| 2059 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2060 | /*[clinic input] |
| 2061 | bytearray.__reduce_ex__ as bytearray_reduce_ex |
| 2062 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2063 | proto: int = 0 |
| 2064 | / |
| 2065 | |
| 2066 | Return state information for pickling. |
| 2067 | [clinic start generated code]*/ |
| 2068 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2069 | static PyObject * |
| 2070 | bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 2071 | /*[clinic end generated code: output=52eac33377197520 input=f129bc1a1aa151ee]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2072 | { |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2073 | return _common_reduce(self, proto); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2076 | /*[clinic input] |
| 2077 | bytearray.__sizeof__ as bytearray_sizeof |
| 2078 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2079 | Returns the size of the bytearray object in memory, in bytes. |
| 2080 | [clinic start generated code]*/ |
| 2081 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2082 | static PyObject * |
| 2083 | bytearray_sizeof_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 2084 | /*[clinic end generated code: output=738abdd17951c427 input=e27320fd98a4bc5a]*/ |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2085 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 2086 | Py_ssize_t res; |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2087 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 2088 | res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char); |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 2089 | return PyLong_FromSsize_t(res); |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2092 | static PySequenceMethods bytearray_as_sequence = { |
| 2093 | (lenfunc)bytearray_length, /* sq_length */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2094 | (binaryfunc)PyByteArray_Concat, /* sq_concat */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2095 | (ssizeargfunc)bytearray_repeat, /* sq_repeat */ |
| 2096 | (ssizeargfunc)bytearray_getitem, /* sq_item */ |
| 2097 | 0, /* sq_slice */ |
| 2098 | (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */ |
| 2099 | 0, /* sq_ass_slice */ |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2100 | (objobjproc)bytearray_contains, /* sq_contains */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2101 | (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */ |
| 2102 | (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2103 | }; |
| 2104 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2105 | static PyMappingMethods bytearray_as_mapping = { |
| 2106 | (lenfunc)bytearray_length, |
| 2107 | (binaryfunc)bytearray_subscript, |
| 2108 | (objobjargproc)bytearray_ass_subscript, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2109 | }; |
| 2110 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2111 | static PyBufferProcs bytearray_as_buffer = { |
| 2112 | (getbufferproc)bytearray_getbuffer, |
| 2113 | (releasebufferproc)bytearray_releasebuffer, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2114 | }; |
| 2115 | |
| 2116 | static PyMethodDef |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2117 | bytearray_methods[] = { |
| 2118 | {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2119 | BYTEARRAY_REDUCE_METHODDEF |
| 2120 | BYTEARRAY_REDUCE_EX_METHODDEF |
| 2121 | BYTEARRAY_SIZEOF_METHODDEF |
| 2122 | BYTEARRAY_APPEND_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2123 | {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS, |
| 2124 | _Py_capitalize__doc__}, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2125 | {"center", (PyCFunction)stringlib_center, METH_VARARGS, _Py_center__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2126 | BYTEARRAY_CLEAR_METHODDEF |
| 2127 | BYTEARRAY_COPY_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2128 | {"count", (PyCFunction)bytearray_count, METH_VARARGS, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2129 | _Py_count__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2130 | BYTEARRAY_DECODE_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2131 | {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2132 | _Py_endswith__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 2133 | {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2134 | _Py_expandtabs__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2135 | BYTEARRAY_EXTEND_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2136 | {"find", (PyCFunction)bytearray_find, METH_VARARGS, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2137 | _Py_find__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2138 | BYTEARRAY_FROMHEX_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2139 | {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__}, |
| 2140 | {"index", (PyCFunction)bytearray_index, METH_VARARGS, _Py_index__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2141 | BYTEARRAY_INSERT_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2142 | {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS, |
| 2143 | _Py_isalnum__doc__}, |
| 2144 | {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS, |
| 2145 | _Py_isalpha__doc__}, |
| 2146 | {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS, |
| 2147 | _Py_isdigit__doc__}, |
| 2148 | {"islower", (PyCFunction)stringlib_islower, METH_NOARGS, |
| 2149 | _Py_islower__doc__}, |
| 2150 | {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS, |
| 2151 | _Py_isspace__doc__}, |
| 2152 | {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS, |
| 2153 | _Py_istitle__doc__}, |
| 2154 | {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS, |
| 2155 | _Py_isupper__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2156 | BYTEARRAY_JOIN_METHODDEF |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2157 | {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, _Py_ljust__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2158 | {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2159 | BYTEARRAY_LSTRIP_METHODDEF |
| 2160 | BYTEARRAY_MAKETRANS_METHODDEF |
| 2161 | BYTEARRAY_PARTITION_METHODDEF |
| 2162 | BYTEARRAY_POP_METHODDEF |
| 2163 | BYTEARRAY_REMOVE_METHODDEF |
| 2164 | BYTEARRAY_REPLACE_METHODDEF |
| 2165 | BYTEARRAY_REVERSE_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2166 | {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__}, |
| 2167 | {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__}, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2168 | {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, _Py_rjust__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2169 | BYTEARRAY_RPARTITION_METHODDEF |
| 2170 | BYTEARRAY_RSPLIT_METHODDEF |
| 2171 | BYTEARRAY_RSTRIP_METHODDEF |
| 2172 | BYTEARRAY_SPLIT_METHODDEF |
| 2173 | BYTEARRAY_SPLITLINES_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2174 | {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS , |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2175 | _Py_startswith__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2176 | BYTEARRAY_STRIP_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2177 | {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS, |
| 2178 | _Py_swapcase__doc__}, |
| 2179 | {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2180 | BYTEARRAY_TRANSLATE_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2181 | {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__}, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2182 | {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, _Py_zfill__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2183 | {NULL} |
| 2184 | }; |
| 2185 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 2186 | static PyObject * |
| 2187 | bytearray_mod(PyObject *v, PyObject *w) |
| 2188 | { |
| 2189 | if (!PyByteArray_Check(v)) |
| 2190 | Py_RETURN_NOTIMPLEMENTED; |
Berker Peksag | 43de36d | 2016-04-16 01:20:47 +0300 | [diff] [blame] | 2191 | return _PyBytes_FormatEx(PyByteArray_AS_STRING(v), PyByteArray_GET_SIZE(v), w, 1); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | static PyNumberMethods bytearray_as_number = { |
| 2195 | 0, /*nb_add*/ |
| 2196 | 0, /*nb_subtract*/ |
| 2197 | 0, /*nb_multiply*/ |
| 2198 | bytearray_mod, /*nb_remainder*/ |
| 2199 | }; |
| 2200 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2201 | PyDoc_STRVAR(bytearray_doc, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2202 | "bytearray(iterable_of_ints) -> bytearray\n\ |
| 2203 | bytearray(string, encoding[, errors]) -> bytearray\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 2204 | bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\ |
| 2205 | bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\ |
| 2206 | bytearray() -> empty bytes array\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2207 | \n\ |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 2208 | Construct a mutable bytearray object from:\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2209 | - an iterable yielding integers in range(256)\n\ |
| 2210 | - a text string encoded using the specified encoding\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 2211 | - a bytes or a buffer object\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2212 | - any object implementing the buffer API.\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 2213 | - an integer"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2214 | |
| 2215 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2216 | static PyObject *bytearray_iter(PyObject *seq); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2217 | |
| 2218 | PyTypeObject PyByteArray_Type = { |
| 2219 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2220 | "bytearray", |
| 2221 | sizeof(PyByteArrayObject), |
| 2222 | 0, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2223 | (destructor)bytearray_dealloc, /* tp_dealloc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2224 | 0, /* tp_print */ |
| 2225 | 0, /* tp_getattr */ |
| 2226 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 2227 | 0, /* tp_reserved */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2228 | (reprfunc)bytearray_repr, /* tp_repr */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 2229 | &bytearray_as_number, /* tp_as_number */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2230 | &bytearray_as_sequence, /* tp_as_sequence */ |
| 2231 | &bytearray_as_mapping, /* tp_as_mapping */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2232 | 0, /* tp_hash */ |
| 2233 | 0, /* tp_call */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2234 | bytearray_str, /* tp_str */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2235 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2236 | 0, /* tp_setattro */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2237 | &bytearray_as_buffer, /* tp_as_buffer */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2238 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2239 | bytearray_doc, /* tp_doc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2240 | 0, /* tp_traverse */ |
| 2241 | 0, /* tp_clear */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2242 | (richcmpfunc)bytearray_richcompare, /* tp_richcompare */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2243 | 0, /* tp_weaklistoffset */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2244 | bytearray_iter, /* tp_iter */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2245 | 0, /* tp_iternext */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2246 | bytearray_methods, /* tp_methods */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2247 | 0, /* tp_members */ |
| 2248 | 0, /* tp_getset */ |
| 2249 | 0, /* tp_base */ |
| 2250 | 0, /* tp_dict */ |
| 2251 | 0, /* tp_descr_get */ |
| 2252 | 0, /* tp_descr_set */ |
| 2253 | 0, /* tp_dictoffset */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2254 | (initproc)bytearray_init, /* tp_init */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2255 | PyType_GenericAlloc, /* tp_alloc */ |
| 2256 | PyType_GenericNew, /* tp_new */ |
| 2257 | PyObject_Del, /* tp_free */ |
| 2258 | }; |
| 2259 | |
| 2260 | /*********************** Bytes Iterator ****************************/ |
| 2261 | |
| 2262 | typedef struct { |
| 2263 | PyObject_HEAD |
| 2264 | Py_ssize_t it_index; |
| 2265 | PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 2266 | } bytesiterobject; |
| 2267 | |
| 2268 | static void |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2269 | bytearrayiter_dealloc(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2270 | { |
| 2271 | _PyObject_GC_UNTRACK(it); |
| 2272 | Py_XDECREF(it->it_seq); |
| 2273 | PyObject_GC_Del(it); |
| 2274 | } |
| 2275 | |
| 2276 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2277 | bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2278 | { |
| 2279 | Py_VISIT(it->it_seq); |
| 2280 | return 0; |
| 2281 | } |
| 2282 | |
| 2283 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2284 | bytearrayiter_next(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2285 | { |
| 2286 | PyByteArrayObject *seq; |
| 2287 | PyObject *item; |
| 2288 | |
| 2289 | assert(it != NULL); |
| 2290 | seq = it->it_seq; |
| 2291 | if (seq == NULL) |
| 2292 | return NULL; |
| 2293 | assert(PyByteArray_Check(seq)); |
| 2294 | |
| 2295 | if (it->it_index < PyByteArray_GET_SIZE(seq)) { |
| 2296 | item = PyLong_FromLong( |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2297 | (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2298 | if (item != NULL) |
| 2299 | ++it->it_index; |
| 2300 | return item; |
| 2301 | } |
| 2302 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2303 | it->it_seq = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 2304 | Py_DECREF(seq); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2305 | return NULL; |
| 2306 | } |
| 2307 | |
| 2308 | static PyObject * |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2309 | bytearrayiter_length_hint(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2310 | { |
| 2311 | Py_ssize_t len = 0; |
Serhiy Storchaka | af65872 | 2016-07-03 14:41:36 +0300 | [diff] [blame] | 2312 | if (it->it_seq) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2313 | len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index; |
Serhiy Storchaka | af65872 | 2016-07-03 14:41:36 +0300 | [diff] [blame] | 2314 | if (len < 0) { |
| 2315 | len = 0; |
| 2316 | } |
| 2317 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2318 | return PyLong_FromSsize_t(len); |
| 2319 | } |
| 2320 | |
| 2321 | PyDoc_STRVAR(length_hint_doc, |
| 2322 | "Private method returning an estimate of len(list(it))."); |
| 2323 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2324 | static PyObject * |
| 2325 | bytearrayiter_reduce(bytesiterobject *it) |
| 2326 | { |
| 2327 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 2328 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2329 | it->it_seq, it->it_index); |
| 2330 | } else { |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 2331 | return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter")); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2332 | } |
| 2333 | } |
| 2334 | |
| 2335 | static PyObject * |
| 2336 | bytearrayiter_setstate(bytesiterobject *it, PyObject *state) |
| 2337 | { |
| 2338 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 2339 | if (index == -1 && PyErr_Occurred()) |
| 2340 | return NULL; |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 2341 | if (it->it_seq != NULL) { |
| 2342 | if (index < 0) |
| 2343 | index = 0; |
| 2344 | else if (index > PyByteArray_GET_SIZE(it->it_seq)) |
| 2345 | index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */ |
| 2346 | it->it_index = index; |
| 2347 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2348 | Py_RETURN_NONE; |
| 2349 | } |
| 2350 | |
| 2351 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 2352 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2353 | static PyMethodDef bytearrayiter_methods[] = { |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2354 | {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2355 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2356 | {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2357 | bytearray_reduce__doc__}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2358 | {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O, |
| 2359 | setstate_doc}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2360 | {NULL, NULL} /* sentinel */ |
| 2361 | }; |
| 2362 | |
| 2363 | PyTypeObject PyByteArrayIter_Type = { |
| 2364 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2365 | "bytearray_iterator", /* tp_name */ |
| 2366 | sizeof(bytesiterobject), /* tp_basicsize */ |
| 2367 | 0, /* tp_itemsize */ |
| 2368 | /* methods */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2369 | (destructor)bytearrayiter_dealloc, /* tp_dealloc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2370 | 0, /* tp_print */ |
| 2371 | 0, /* tp_getattr */ |
| 2372 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 2373 | 0, /* tp_reserved */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2374 | 0, /* tp_repr */ |
| 2375 | 0, /* tp_as_number */ |
| 2376 | 0, /* tp_as_sequence */ |
| 2377 | 0, /* tp_as_mapping */ |
| 2378 | 0, /* tp_hash */ |
| 2379 | 0, /* tp_call */ |
| 2380 | 0, /* tp_str */ |
| 2381 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2382 | 0, /* tp_setattro */ |
| 2383 | 0, /* tp_as_buffer */ |
| 2384 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 2385 | 0, /* tp_doc */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2386 | (traverseproc)bytearrayiter_traverse, /* tp_traverse */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2387 | 0, /* tp_clear */ |
| 2388 | 0, /* tp_richcompare */ |
| 2389 | 0, /* tp_weaklistoffset */ |
| 2390 | PyObject_SelfIter, /* tp_iter */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2391 | (iternextfunc)bytearrayiter_next, /* tp_iternext */ |
| 2392 | bytearrayiter_methods, /* tp_methods */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2393 | 0, |
| 2394 | }; |
| 2395 | |
| 2396 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2397 | bytearray_iter(PyObject *seq) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2398 | { |
| 2399 | bytesiterobject *it; |
| 2400 | |
| 2401 | if (!PyByteArray_Check(seq)) { |
| 2402 | PyErr_BadInternalCall(); |
| 2403 | return NULL; |
| 2404 | } |
| 2405 | it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type); |
| 2406 | if (it == NULL) |
| 2407 | return NULL; |
| 2408 | it->it_index = 0; |
| 2409 | Py_INCREF(seq); |
| 2410 | it->it_seq = (PyByteArrayObject *)seq; |
| 2411 | _PyObject_GC_TRACK(it); |
| 2412 | return (PyObject *)it; |
| 2413 | } |