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 | { |
| 101 | return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type, |
| 102 | input, NULL); |
| 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 | { |
| 249 | Py_ssize_t size; |
| 250 | Py_buffer va, vb; |
| 251 | PyByteArrayObject *result = NULL; |
| 252 | |
| 253 | va.len = -1; |
| 254 | vb.len = -1; |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 255 | if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 || |
| 256 | PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 257 | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
| 258 | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
| 259 | goto done; |
| 260 | } |
| 261 | |
| 262 | size = va.len + vb.len; |
| 263 | if (size < 0) { |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 264 | PyErr_NoMemory(); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 265 | goto done; |
| 266 | } |
| 267 | |
| 268 | result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, size); |
| 269 | if (result != NULL) { |
| 270 | memcpy(result->ob_bytes, va.buf, va.len); |
| 271 | memcpy(result->ob_bytes + va.len, vb.buf, vb.len); |
| 272 | } |
| 273 | |
| 274 | done: |
| 275 | if (va.len != -1) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 276 | PyBuffer_Release(&va); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 277 | if (vb.len != -1) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 278 | PyBuffer_Release(&vb); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 279 | return (PyObject *)result; |
| 280 | } |
| 281 | |
| 282 | /* Functions stuffed into the type object */ |
| 283 | |
| 284 | static Py_ssize_t |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 285 | bytearray_length(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 286 | { |
| 287 | return Py_SIZE(self); |
| 288 | } |
| 289 | |
| 290 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 291 | bytearray_iconcat(PyByteArrayObject *self, PyObject *other) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 292 | { |
| 293 | Py_ssize_t mysize; |
| 294 | Py_ssize_t size; |
| 295 | Py_buffer vo; |
| 296 | |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 297 | if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 298 | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
| 299 | Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name); |
| 300 | return NULL; |
| 301 | } |
| 302 | |
| 303 | mysize = Py_SIZE(self); |
| 304 | size = mysize + vo.len; |
| 305 | if (size < 0) { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 306 | PyBuffer_Release(&vo); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 307 | return PyErr_NoMemory(); |
| 308 | } |
Antoine Pitrou | 2545411 | 2015-05-19 20:52:27 +0200 | [diff] [blame] | 309 | if (PyByteArray_Resize((PyObject *)self, size) < 0) { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 310 | PyBuffer_Release(&vo); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 311 | return NULL; |
| 312 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 313 | memcpy(PyByteArray_AS_STRING(self) + mysize, vo.buf, vo.len); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 314 | PyBuffer_Release(&vo); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 315 | Py_INCREF(self); |
| 316 | return (PyObject *)self; |
| 317 | } |
| 318 | |
| 319 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 320 | bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 321 | { |
| 322 | PyByteArrayObject *result; |
| 323 | Py_ssize_t mysize; |
| 324 | Py_ssize_t size; |
| 325 | |
| 326 | if (count < 0) |
| 327 | count = 0; |
| 328 | mysize = Py_SIZE(self); |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 329 | if (count > 0 && mysize > PY_SSIZE_T_MAX / count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 330 | return PyErr_NoMemory(); |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 331 | size = mysize * count; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 332 | result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size); |
| 333 | if (result != NULL && size != 0) { |
| 334 | if (mysize == 1) |
| 335 | memset(result->ob_bytes, self->ob_bytes[0], size); |
| 336 | else { |
| 337 | Py_ssize_t i; |
| 338 | for (i = 0; i < count; i++) |
| 339 | memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize); |
| 340 | } |
| 341 | } |
| 342 | return (PyObject *)result; |
| 343 | } |
| 344 | |
| 345 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 346 | bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 347 | { |
| 348 | Py_ssize_t mysize; |
| 349 | Py_ssize_t size; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 350 | char *buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 351 | |
| 352 | if (count < 0) |
| 353 | count = 0; |
| 354 | mysize = Py_SIZE(self); |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 355 | if (count > 0 && mysize > PY_SSIZE_T_MAX / count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 356 | return PyErr_NoMemory(); |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 357 | size = mysize * count; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 358 | if (PyByteArray_Resize((PyObject *)self, size) < 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 359 | return NULL; |
| 360 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 361 | buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 362 | if (mysize == 1) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 363 | memset(buf, buf[0], size); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 364 | else { |
| 365 | Py_ssize_t i; |
| 366 | for (i = 1; i < count; i++) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 367 | memcpy(buf + i*mysize, buf, mysize); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | Py_INCREF(self); |
| 371 | return (PyObject *)self; |
| 372 | } |
| 373 | |
| 374 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 375 | bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 376 | { |
| 377 | if (i < 0) |
| 378 | i += Py_SIZE(self); |
| 379 | if (i < 0 || i >= Py_SIZE(self)) { |
| 380 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 381 | return NULL; |
| 382 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 383 | return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i])); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 387 | bytearray_subscript(PyByteArrayObject *self, PyObject *index) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 388 | { |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 389 | if (PyIndex_Check(index)) { |
| 390 | Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 391 | |
| 392 | if (i == -1 && PyErr_Occurred()) |
| 393 | return NULL; |
| 394 | |
| 395 | if (i < 0) |
| 396 | i += PyByteArray_GET_SIZE(self); |
| 397 | |
| 398 | if (i < 0 || i >= Py_SIZE(self)) { |
| 399 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 400 | return NULL; |
| 401 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 402 | return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i])); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 403 | } |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 404 | else if (PySlice_Check(index)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 405 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 406 | if (PySlice_GetIndicesEx(index, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 407 | PyByteArray_GET_SIZE(self), |
| 408 | &start, &stop, &step, &slicelength) < 0) { |
| 409 | return NULL; |
| 410 | } |
| 411 | |
| 412 | if (slicelength <= 0) |
| 413 | return PyByteArray_FromStringAndSize("", 0); |
| 414 | else if (step == 1) { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 415 | return PyByteArray_FromStringAndSize( |
| 416 | PyByteArray_AS_STRING(self) + start, slicelength); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 417 | } |
| 418 | else { |
| 419 | char *source_buf = PyByteArray_AS_STRING(self); |
Alexandre Vassalotti | e2641f4 | 2009-04-03 06:38:02 +0000 | [diff] [blame] | 420 | char *result_buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 421 | PyObject *result; |
| 422 | |
Alexandre Vassalotti | e2641f4 | 2009-04-03 06:38:02 +0000 | [diff] [blame] | 423 | result = PyByteArray_FromStringAndSize(NULL, slicelength); |
| 424 | if (result == NULL) |
| 425 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 426 | |
Alexandre Vassalotti | e2641f4 | 2009-04-03 06:38:02 +0000 | [diff] [blame] | 427 | result_buf = PyByteArray_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 428 | for (cur = start, i = 0; i < slicelength; |
| 429 | cur += step, i++) { |
| 430 | result_buf[i] = source_buf[cur]; |
| 431 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 432 | return result; |
| 433 | } |
| 434 | } |
| 435 | else { |
Terry Jan Reedy | ffff144 | 2014-08-02 01:30:37 -0400 | [diff] [blame] | 436 | PyErr_Format(PyExc_TypeError, |
| 437 | "bytearray indices must be integers or slices, not %.200s", |
| 438 | Py_TYPE(index)->tp_name); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 439 | return NULL; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | static int |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 444 | bytearray_setslice_linear(PyByteArrayObject *self, |
| 445 | Py_ssize_t lo, Py_ssize_t hi, |
| 446 | char *bytes, Py_ssize_t bytes_len) |
| 447 | { |
| 448 | Py_ssize_t avail = hi - lo; |
| 449 | char *buf = PyByteArray_AS_STRING(self); |
| 450 | Py_ssize_t growth = bytes_len - avail; |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 451 | int res = 0; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 452 | assert(avail >= 0); |
| 453 | |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 454 | if (growth < 0) { |
| 455 | if (!_canresize(self)) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 456 | return -1; |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 457 | |
| 458 | if (lo == 0) { |
| 459 | /* Shrink the buffer by advancing its logical start */ |
| 460 | self->ob_start -= growth; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 461 | /* |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 462 | 0 lo hi old_size |
| 463 | | |<----avail----->|<-----tail------>| |
| 464 | | |<-bytes_len->|<-----tail------>| |
| 465 | 0 new_lo new_hi new_size |
| 466 | */ |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 467 | } |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 468 | else { |
| 469 | /* |
| 470 | 0 lo hi old_size |
| 471 | | |<----avail----->|<-----tomove------>| |
| 472 | | |<-bytes_len->|<-----tomove------>| |
| 473 | 0 lo new_hi new_size |
| 474 | */ |
| 475 | memmove(buf + lo + bytes_len, buf + hi, |
| 476 | Py_SIZE(self) - hi); |
| 477 | } |
| 478 | if (PyByteArray_Resize((PyObject *)self, |
| 479 | Py_SIZE(self) + growth) < 0) { |
| 480 | /* Issue #19578: Handling the memory allocation failure here is |
| 481 | tricky here because the bytearray object has already been |
| 482 | modified. Depending on growth and lo, the behaviour is |
| 483 | different. |
| 484 | |
| 485 | If growth < 0 and lo != 0, the operation is completed, but a |
| 486 | MemoryError is still raised and the memory block is not |
| 487 | shrinked. Otherwise, the bytearray is restored in its previous |
| 488 | state and a MemoryError is raised. */ |
| 489 | if (lo == 0) { |
| 490 | self->ob_start += growth; |
| 491 | return -1; |
| 492 | } |
| 493 | /* memmove() removed bytes, the bytearray object cannot be |
| 494 | restored in its previous state. */ |
| 495 | Py_SIZE(self) += growth; |
| 496 | res = -1; |
| 497 | } |
| 498 | buf = PyByteArray_AS_STRING(self); |
| 499 | } |
| 500 | else if (growth > 0) { |
| 501 | if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) { |
| 502 | PyErr_NoMemory(); |
| 503 | return -1; |
| 504 | } |
| 505 | |
| 506 | if (PyByteArray_Resize((PyObject *)self, |
| 507 | Py_SIZE(self) + growth) < 0) { |
| 508 | return -1; |
| 509 | } |
| 510 | buf = PyByteArray_AS_STRING(self); |
| 511 | /* Make the place for the additional bytes */ |
| 512 | /* |
| 513 | 0 lo hi old_size |
| 514 | | |<-avail->|<-----tomove------>| |
| 515 | | |<---bytes_len-->|<-----tomove------>| |
| 516 | 0 lo new_hi new_size |
| 517 | */ |
| 518 | memmove(buf + lo + bytes_len, buf + hi, |
| 519 | Py_SIZE(self) - lo - bytes_len); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | if (bytes_len > 0) |
| 523 | memcpy(buf + lo, bytes, bytes_len); |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 524 | return res; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 528 | bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 529 | PyObject *values) |
| 530 | { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 531 | Py_ssize_t needed; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 532 | void *bytes; |
| 533 | Py_buffer vbytes; |
| 534 | int res = 0; |
| 535 | |
| 536 | vbytes.len = -1; |
| 537 | if (values == (PyObject *)self) { |
| 538 | /* Make a copy and call this function recursively */ |
| 539 | int err; |
| 540 | values = PyByteArray_FromObject(values); |
| 541 | if (values == NULL) |
| 542 | return -1; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 543 | err = bytearray_setslice(self, lo, hi, values); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 544 | Py_DECREF(values); |
| 545 | return err; |
| 546 | } |
| 547 | if (values == NULL) { |
| 548 | /* del b[lo:hi] */ |
| 549 | bytes = NULL; |
| 550 | needed = 0; |
| 551 | } |
| 552 | else { |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 553 | if (PyObject_GetBuffer(values, &vbytes, PyBUF_SIMPLE) != 0) { |
| 554 | PyErr_Format(PyExc_TypeError, |
| 555 | "can't set bytearray slice from %.100s", |
| 556 | Py_TYPE(values)->tp_name); |
| 557 | return -1; |
| 558 | } |
| 559 | needed = vbytes.len; |
| 560 | bytes = vbytes.buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | if (lo < 0) |
| 564 | lo = 0; |
| 565 | if (hi < lo) |
| 566 | hi = lo; |
| 567 | if (hi > Py_SIZE(self)) |
| 568 | hi = Py_SIZE(self); |
| 569 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 570 | res = bytearray_setslice_linear(self, lo, hi, bytes, needed); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 571 | if (vbytes.len != -1) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 572 | PyBuffer_Release(&vbytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 573 | return res; |
| 574 | } |
| 575 | |
| 576 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 577 | bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 578 | { |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 579 | int ival; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 580 | |
| 581 | if (i < 0) |
| 582 | i += Py_SIZE(self); |
| 583 | |
| 584 | if (i < 0 || i >= Py_SIZE(self)) { |
| 585 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 586 | return -1; |
| 587 | } |
| 588 | |
| 589 | if (value == NULL) |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 590 | return bytearray_setslice(self, i, i+1, NULL); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 591 | |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 592 | if (!_getbytevalue(value, &ival)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 593 | return -1; |
| 594 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 595 | PyByteArray_AS_STRING(self)[i] = ival; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 600 | bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 601 | { |
| 602 | Py_ssize_t start, stop, step, slicelen, needed; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 603 | char *buf, *bytes; |
| 604 | buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 605 | |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 606 | if (PyIndex_Check(index)) { |
| 607 | Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 608 | |
| 609 | if (i == -1 && PyErr_Occurred()) |
| 610 | return -1; |
| 611 | |
| 612 | if (i < 0) |
| 613 | i += PyByteArray_GET_SIZE(self); |
| 614 | |
| 615 | if (i < 0 || i >= Py_SIZE(self)) { |
| 616 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 617 | return -1; |
| 618 | } |
| 619 | |
| 620 | if (values == NULL) { |
| 621 | /* Fall through to slice assignment */ |
| 622 | start = i; |
| 623 | stop = i + 1; |
| 624 | step = 1; |
| 625 | slicelen = 1; |
| 626 | } |
| 627 | else { |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 628 | int ival; |
| 629 | if (!_getbytevalue(values, &ival)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 630 | return -1; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 631 | buf[i] = (char)ival; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 632 | return 0; |
| 633 | } |
| 634 | } |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 635 | else if (PySlice_Check(index)) { |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 636 | if (PySlice_GetIndicesEx(index, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 637 | PyByteArray_GET_SIZE(self), |
| 638 | &start, &stop, &step, &slicelen) < 0) { |
| 639 | return -1; |
| 640 | } |
| 641 | } |
| 642 | else { |
Terry Jan Reedy | ffff144 | 2014-08-02 01:30:37 -0400 | [diff] [blame] | 643 | PyErr_Format(PyExc_TypeError, |
| 644 | "bytearray indices must be integers or slices, not %.200s", |
| 645 | Py_TYPE(index)->tp_name); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 646 | return -1; |
| 647 | } |
| 648 | |
| 649 | if (values == NULL) { |
| 650 | bytes = NULL; |
| 651 | needed = 0; |
| 652 | } |
| 653 | else if (values == (PyObject *)self || !PyByteArray_Check(values)) { |
Christian Heimes | 6d26ade | 2012-11-03 23:07:59 +0100 | [diff] [blame] | 654 | int err; |
Ezio Melotti | c64bcbe | 2012-11-03 21:19:06 +0200 | [diff] [blame] | 655 | if (PyNumber_Check(values) || PyUnicode_Check(values)) { |
| 656 | PyErr_SetString(PyExc_TypeError, |
| 657 | "can assign only bytes, buffers, or iterables " |
| 658 | "of ints in range(0, 256)"); |
| 659 | return -1; |
| 660 | } |
Georg Brandl | f3fa568 | 2010-12-04 17:09:30 +0000 | [diff] [blame] | 661 | /* Make a copy and call this function recursively */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 662 | values = PyByteArray_FromObject(values); |
| 663 | if (values == NULL) |
| 664 | return -1; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 665 | err = bytearray_ass_subscript(self, index, values); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 666 | Py_DECREF(values); |
| 667 | return err; |
| 668 | } |
| 669 | else { |
| 670 | assert(PyByteArray_Check(values)); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 671 | bytes = PyByteArray_AS_STRING(values); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 672 | needed = Py_SIZE(values); |
| 673 | } |
| 674 | /* Make sure b[5:2] = ... inserts before 5, not before 2. */ |
| 675 | if ((step < 0 && start < stop) || |
| 676 | (step > 0 && start > stop)) |
| 677 | stop = start; |
| 678 | if (step == 1) { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 679 | return bytearray_setslice_linear(self, start, stop, bytes, needed); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 680 | } |
| 681 | else { |
| 682 | if (needed == 0) { |
| 683 | /* Delete slice */ |
Mark Dickinson | bc09964 | 2010-01-29 17:27:24 +0000 | [diff] [blame] | 684 | size_t cur; |
| 685 | Py_ssize_t i; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 686 | |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 687 | if (!_canresize(self)) |
| 688 | return -1; |
Mark Dickinson | 7e3b948 | 2010-08-06 21:33:18 +0000 | [diff] [blame] | 689 | |
| 690 | if (slicelen == 0) |
| 691 | /* Nothing to do here. */ |
| 692 | return 0; |
| 693 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 694 | if (step < 0) { |
| 695 | stop = start + 1; |
| 696 | start = stop + step * (slicelen - 1) - 1; |
| 697 | step = -step; |
| 698 | } |
| 699 | for (cur = start, i = 0; |
| 700 | i < slicelen; cur += step, i++) { |
| 701 | Py_ssize_t lim = step - 1; |
| 702 | |
Mark Dickinson | 66f575b | 2010-02-14 12:53:32 +0000 | [diff] [blame] | 703 | if (cur + step >= (size_t)PyByteArray_GET_SIZE(self)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 704 | lim = PyByteArray_GET_SIZE(self) - cur - 1; |
| 705 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 706 | memmove(buf + cur - i, |
| 707 | buf + cur + 1, lim); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 708 | } |
| 709 | /* Move the tail of the bytes, in one chunk */ |
Mark Dickinson | 7e3b948 | 2010-08-06 21:33:18 +0000 | [diff] [blame] | 710 | cur = start + (size_t)slicelen*step; |
Mark Dickinson | 66f575b | 2010-02-14 12:53:32 +0000 | [diff] [blame] | 711 | if (cur < (size_t)PyByteArray_GET_SIZE(self)) { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 712 | memmove(buf + cur - slicelen, |
| 713 | buf + cur, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 714 | PyByteArray_GET_SIZE(self) - cur); |
| 715 | } |
| 716 | if (PyByteArray_Resize((PyObject *)self, |
| 717 | PyByteArray_GET_SIZE(self) - slicelen) < 0) |
| 718 | return -1; |
| 719 | |
| 720 | return 0; |
| 721 | } |
| 722 | else { |
| 723 | /* Assign slice */ |
Mark Dickinson | 7e3b948 | 2010-08-06 21:33:18 +0000 | [diff] [blame] | 724 | Py_ssize_t i; |
| 725 | size_t cur; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 726 | |
| 727 | if (needed != slicelen) { |
| 728 | PyErr_Format(PyExc_ValueError, |
| 729 | "attempt to assign bytes of size %zd " |
| 730 | "to extended slice of size %zd", |
| 731 | needed, slicelen); |
| 732 | return -1; |
| 733 | } |
| 734 | for (cur = start, i = 0; i < slicelen; cur += step, i++) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 735 | buf[cur] = bytes[i]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 736 | return 0; |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 742 | bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 743 | { |
| 744 | static char *kwlist[] = {"source", "encoding", "errors", 0}; |
| 745 | PyObject *arg = NULL; |
| 746 | const char *encoding = NULL; |
| 747 | const char *errors = NULL; |
| 748 | Py_ssize_t count; |
| 749 | PyObject *it; |
| 750 | PyObject *(*iternext)(PyObject *); |
| 751 | |
| 752 | if (Py_SIZE(self) != 0) { |
| 753 | /* Empty previous contents (yes, do this first of all!) */ |
| 754 | if (PyByteArray_Resize((PyObject *)self, 0) < 0) |
| 755 | return -1; |
| 756 | } |
| 757 | |
| 758 | /* Parse arguments */ |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 759 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 760 | &arg, &encoding, &errors)) |
| 761 | return -1; |
| 762 | |
| 763 | /* Make a quick exit if no first argument */ |
| 764 | if (arg == NULL) { |
| 765 | if (encoding != NULL || errors != NULL) { |
| 766 | PyErr_SetString(PyExc_TypeError, |
| 767 | "encoding or errors without sequence argument"); |
| 768 | return -1; |
| 769 | } |
| 770 | return 0; |
| 771 | } |
| 772 | |
| 773 | if (PyUnicode_Check(arg)) { |
| 774 | /* Encode via the codec registry */ |
| 775 | PyObject *encoded, *new; |
| 776 | if (encoding == NULL) { |
| 777 | PyErr_SetString(PyExc_TypeError, |
| 778 | "string argument without an encoding"); |
| 779 | return -1; |
| 780 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 781 | encoded = PyUnicode_AsEncodedString(arg, encoding, errors); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 782 | if (encoded == NULL) |
| 783 | return -1; |
| 784 | assert(PyBytes_Check(encoded)); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 785 | new = bytearray_iconcat(self, encoded); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 786 | Py_DECREF(encoded); |
| 787 | if (new == NULL) |
| 788 | return -1; |
| 789 | Py_DECREF(new); |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | /* If it's not unicode, there can't be encoding or errors */ |
| 794 | if (encoding != NULL || errors != NULL) { |
| 795 | PyErr_SetString(PyExc_TypeError, |
| 796 | "encoding or errors without a string argument"); |
| 797 | return -1; |
| 798 | } |
| 799 | |
| 800 | /* Is it an int? */ |
Benjamin Peterson | 8380dd5 | 2010-04-16 22:51:37 +0000 | [diff] [blame] | 801 | count = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
| 802 | if (count == -1 && PyErr_Occurred()) { |
| 803 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 804 | return -1; |
Benjamin Peterson | 9c0e94f | 2010-04-16 23:00:53 +0000 | [diff] [blame] | 805 | PyErr_Clear(); |
Benjamin Peterson | 8380dd5 | 2010-04-16 22:51:37 +0000 | [diff] [blame] | 806 | } |
| 807 | else if (count < 0) { |
| 808 | PyErr_SetString(PyExc_ValueError, "negative count"); |
| 809 | return -1; |
| 810 | } |
| 811 | else { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 812 | if (count > 0) { |
Victor Stinner | 2bc4d95 | 2014-06-02 22:22:42 +0200 | [diff] [blame] | 813 | if (PyByteArray_Resize((PyObject *)self, count)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 814 | return -1; |
Victor Stinner | 2bc4d95 | 2014-06-02 22:22:42 +0200 | [diff] [blame] | 815 | memset(PyByteArray_AS_STRING(self), 0, count); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 816 | } |
| 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | /* Use the buffer API */ |
| 821 | if (PyObject_CheckBuffer(arg)) { |
| 822 | Py_ssize_t size; |
| 823 | Py_buffer view; |
| 824 | if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0) |
| 825 | return -1; |
| 826 | size = view.len; |
| 827 | if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 828 | if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self), |
| 829 | &view, size, 'C') < 0) |
Stefan Krah | 7d12d9d | 2012-07-28 12:25:55 +0200 | [diff] [blame] | 830 | goto fail; |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 831 | PyBuffer_Release(&view); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 832 | return 0; |
| 833 | fail: |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 834 | PyBuffer_Release(&view); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 835 | return -1; |
| 836 | } |
| 837 | |
| 838 | /* XXX Optimize this if the arguments is a list, tuple */ |
| 839 | |
| 840 | /* Get the iterator */ |
| 841 | it = PyObject_GetIter(arg); |
| 842 | if (it == NULL) |
| 843 | return -1; |
| 844 | iternext = *Py_TYPE(it)->tp_iternext; |
| 845 | |
| 846 | /* Run the iterator to exhaustion */ |
| 847 | for (;;) { |
| 848 | PyObject *item; |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 849 | int rc, value; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 850 | |
| 851 | /* Get the next item */ |
| 852 | item = iternext(it); |
| 853 | if (item == NULL) { |
| 854 | if (PyErr_Occurred()) { |
| 855 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 856 | goto error; |
| 857 | PyErr_Clear(); |
| 858 | } |
| 859 | break; |
| 860 | } |
| 861 | |
| 862 | /* Interpret it as an int (__index__) */ |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 863 | rc = _getbytevalue(item, &value); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 864 | Py_DECREF(item); |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 865 | if (!rc) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 866 | goto error; |
| 867 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 868 | /* Append the byte */ |
Serhiy Storchaka | 7b6e3b9 | 2015-06-29 21:14:06 +0300 | [diff] [blame] | 869 | if (Py_SIZE(self) + 1 < self->ob_alloc) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 870 | Py_SIZE(self)++; |
Serhiy Storchaka | 7b6e3b9 | 2015-06-29 21:14:06 +0300 | [diff] [blame] | 871 | PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0'; |
| 872 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 873 | else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0) |
| 874 | goto error; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 875 | PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | /* Clean up and return success */ |
| 879 | Py_DECREF(it); |
| 880 | return 0; |
| 881 | |
| 882 | error: |
| 883 | /* Error handling when it != NULL */ |
| 884 | Py_DECREF(it); |
| 885 | return -1; |
| 886 | } |
| 887 | |
| 888 | /* Mostly copied from string_repr, but without the |
| 889 | "smart quote" functionality. */ |
| 890 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 891 | bytearray_repr(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 892 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 893 | const char *quote_prefix = "bytearray(b"; |
| 894 | const char *quote_postfix = ")"; |
| 895 | Py_ssize_t length = Py_SIZE(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 896 | /* 15 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */ |
Mark Dickinson | 66f575b | 2010-02-14 12:53:32 +0000 | [diff] [blame] | 897 | size_t newsize; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 898 | PyObject *v; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 899 | Py_ssize_t i; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 900 | char *bytes; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 901 | char c; |
| 902 | char *p; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 903 | int quote; |
| 904 | char *test, *start; |
| 905 | char *buffer; |
| 906 | |
| 907 | if (length > (PY_SSIZE_T_MAX - 15) / 4) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 908 | PyErr_SetString(PyExc_OverflowError, |
| 909 | "bytearray object is too large to make repr"); |
| 910 | return NULL; |
| 911 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 912 | |
| 913 | newsize = 15 + length * 4; |
Antoine Pitrou | 39aba4f | 2011-11-12 21:15:28 +0100 | [diff] [blame] | 914 | buffer = PyObject_Malloc(newsize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 915 | if (buffer == NULL) { |
| 916 | PyErr_NoMemory(); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 917 | return NULL; |
| 918 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 919 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 920 | /* Figure out which quote to use; single is preferred */ |
| 921 | quote = '\''; |
| 922 | start = PyByteArray_AS_STRING(self); |
| 923 | for (test = start; test < start+length; ++test) { |
| 924 | if (*test == '"') { |
| 925 | quote = '\''; /* back to single */ |
| 926 | break; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 927 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 928 | else if (*test == '\'') |
| 929 | quote = '"'; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 930 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 931 | |
| 932 | p = buffer; |
| 933 | while (*quote_prefix) |
| 934 | *p++ = *quote_prefix++; |
| 935 | *p++ = quote; |
| 936 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 937 | bytes = PyByteArray_AS_STRING(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 938 | for (i = 0; i < length; i++) { |
| 939 | /* There's at least enough room for a hex escape |
| 940 | and a closing quote. */ |
| 941 | assert(newsize - (p - buffer) >= 5); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 942 | c = bytes[i]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 943 | if (c == '\'' || c == '\\') |
| 944 | *p++ = '\\', *p++ = c; |
| 945 | else if (c == '\t') |
| 946 | *p++ = '\\', *p++ = 't'; |
| 947 | else if (c == '\n') |
| 948 | *p++ = '\\', *p++ = 'n'; |
| 949 | else if (c == '\r') |
| 950 | *p++ = '\\', *p++ = 'r'; |
| 951 | else if (c == 0) |
| 952 | *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0'; |
| 953 | else if (c < ' ' || c >= 0x7f) { |
| 954 | *p++ = '\\'; |
| 955 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 956 | *p++ = Py_hexdigits[(c & 0xf0) >> 4]; |
| 957 | *p++ = Py_hexdigits[c & 0xf]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 958 | } |
| 959 | else |
| 960 | *p++ = c; |
| 961 | } |
| 962 | assert(newsize - (p - buffer) >= 1); |
| 963 | *p++ = quote; |
| 964 | while (*quote_postfix) { |
| 965 | *p++ = *quote_postfix++; |
| 966 | } |
| 967 | |
| 968 | v = PyUnicode_DecodeASCII(buffer, p - buffer, NULL); |
Antoine Pitrou | 39aba4f | 2011-11-12 21:15:28 +0100 | [diff] [blame] | 969 | PyObject_Free(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 970 | return v; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 974 | bytearray_str(PyObject *op) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 975 | { |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 976 | if (Py_BytesWarningFlag) { |
| 977 | if (PyErr_WarnEx(PyExc_BytesWarning, |
| 978 | "str() on a bytearray instance", 1)) |
| 979 | return NULL; |
| 980 | } |
| 981 | return bytearray_repr((PyByteArrayObject*)op); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 985 | bytearray_richcompare(PyObject *self, PyObject *other, int op) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 986 | { |
| 987 | Py_ssize_t self_size, other_size; |
| 988 | Py_buffer self_bytes, other_bytes; |
| 989 | PyObject *res; |
| 990 | Py_ssize_t minsize; |
Serhiy Storchaka | fa494fd | 2015-05-30 17:45:22 +0300 | [diff] [blame] | 991 | int cmp, rc; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 992 | |
| 993 | /* Bytes can be compared to anything that supports the (binary) |
| 994 | buffer API. Except that a comparison with Unicode is always an |
| 995 | error, even if the comparison is for equality. */ |
Serhiy Storchaka | fa494fd | 2015-05-30 17:45:22 +0300 | [diff] [blame] | 996 | rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type); |
| 997 | if (!rc) |
| 998 | rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type); |
| 999 | if (rc < 0) |
| 1000 | return NULL; |
| 1001 | if (rc) { |
Barry Warsaw | 9e9dcd6 | 2008-10-17 01:50:37 +0000 | [diff] [blame] | 1002 | if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1003 | if (PyErr_WarnEx(PyExc_BytesWarning, |
Georg Brandl | e5d68ac | 2008-06-04 11:30:26 +0000 | [diff] [blame] | 1004 | "Comparison between bytearray and string", 1)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1005 | return NULL; |
| 1006 | } |
| 1007 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1008 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1011 | if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1012 | PyErr_Clear(); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1013 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1014 | } |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1015 | self_size = self_bytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1016 | |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1017 | if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1018 | PyErr_Clear(); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1019 | PyBuffer_Release(&self_bytes); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1020 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1021 | } |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1022 | other_size = other_bytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1023 | |
| 1024 | if (self_size != other_size && (op == Py_EQ || op == Py_NE)) { |
| 1025 | /* Shortcut: if the lengths differ, the objects differ */ |
| 1026 | cmp = (op == Py_NE); |
| 1027 | } |
| 1028 | else { |
| 1029 | minsize = self_size; |
| 1030 | if (other_size < minsize) |
| 1031 | minsize = other_size; |
| 1032 | |
| 1033 | cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize); |
| 1034 | /* In ISO C, memcmp() guarantees to use unsigned bytes! */ |
| 1035 | |
| 1036 | if (cmp == 0) { |
| 1037 | if (self_size < other_size) |
| 1038 | cmp = -1; |
| 1039 | else if (self_size > other_size) |
| 1040 | cmp = 1; |
| 1041 | } |
| 1042 | |
| 1043 | switch (op) { |
| 1044 | case Py_LT: cmp = cmp < 0; break; |
| 1045 | case Py_LE: cmp = cmp <= 0; break; |
| 1046 | case Py_EQ: cmp = cmp == 0; break; |
| 1047 | case Py_NE: cmp = cmp != 0; break; |
| 1048 | case Py_GT: cmp = cmp > 0; break; |
| 1049 | case Py_GE: cmp = cmp >= 0; break; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | res = cmp ? Py_True : Py_False; |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1054 | PyBuffer_Release(&self_bytes); |
| 1055 | PyBuffer_Release(&other_bytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1056 | Py_INCREF(res); |
| 1057 | return res; |
| 1058 | } |
| 1059 | |
| 1060 | static void |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1061 | bytearray_dealloc(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1062 | { |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 1063 | if (self->ob_exports > 0) { |
| 1064 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1065 | "deallocated bytearray object has exported buffers"); |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 1066 | PyErr_Print(); |
| 1067 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1068 | if (self->ob_bytes != 0) { |
Antoine Pitrou | 39aba4f | 2011-11-12 21:15:28 +0100 | [diff] [blame] | 1069 | PyObject_Free(self->ob_bytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1070 | } |
| 1071 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 1072 | } |
| 1073 | |
| 1074 | |
| 1075 | /* -------------------------------------------------------------------- */ |
| 1076 | /* Methods */ |
| 1077 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1078 | #define FASTSEARCH fastsearch |
| 1079 | #define STRINGLIB(F) stringlib_##F |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1080 | #define STRINGLIB_CHAR char |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 1081 | #define STRINGLIB_SIZEOF_CHAR 1 |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1082 | #define STRINGLIB_LEN PyByteArray_GET_SIZE |
| 1083 | #define STRINGLIB_STR PyByteArray_AS_STRING |
| 1084 | #define STRINGLIB_NEW PyByteArray_FromStringAndSize |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1085 | #define STRINGLIB_ISSPACE Py_ISSPACE |
| 1086 | #define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r')) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1087 | #define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact |
| 1088 | #define STRINGLIB_MUTABLE 1 |
| 1089 | |
| 1090 | #include "stringlib/fastsearch.h" |
| 1091 | #include "stringlib/count.h" |
| 1092 | #include "stringlib/find.h" |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 1093 | #include "stringlib/join.h" |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1094 | #include "stringlib/partition.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1095 | #include "stringlib/split.h" |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1096 | #include "stringlib/ctype.h" |
| 1097 | #include "stringlib/transmogrify.h" |
| 1098 | |
| 1099 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1100 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1101 | bytearray_find(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1102 | { |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame^] | 1103 | return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1106 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1107 | bytearray_count(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1108 | { |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame^] | 1109 | return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1112 | /*[clinic input] |
| 1113 | bytearray.clear |
| 1114 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1115 | Remove all items from the bytearray. |
| 1116 | [clinic start generated code]*/ |
| 1117 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1118 | static PyObject * |
| 1119 | bytearray_clear_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1120 | /*[clinic end generated code: output=85c2fe6aede0956c input=ed6edae9de447ac4]*/ |
Eli Bendersky | 4db28d3 | 2011-03-03 18:21:02 +0000 | [diff] [blame] | 1121 | { |
| 1122 | if (PyByteArray_Resize((PyObject *)self, 0) < 0) |
| 1123 | return NULL; |
| 1124 | Py_RETURN_NONE; |
| 1125 | } |
| 1126 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1127 | /*[clinic input] |
| 1128 | bytearray.copy |
| 1129 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1130 | Return a copy of B. |
| 1131 | [clinic start generated code]*/ |
| 1132 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1133 | static PyObject * |
| 1134 | bytearray_copy_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1135 | /*[clinic end generated code: output=68cfbcfed484c132 input=6597b0c01bccaa9e]*/ |
Eli Bendersky | 4db28d3 | 2011-03-03 18:21:02 +0000 | [diff] [blame] | 1136 | { |
| 1137 | return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self), |
| 1138 | PyByteArray_GET_SIZE(self)); |
| 1139 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1140 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1141 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1142 | bytearray_index(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1143 | { |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame^] | 1144 | return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1147 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1148 | bytearray_rfind(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1149 | { |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame^] | 1150 | return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1153 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1154 | bytearray_rindex(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1155 | { |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame^] | 1156 | return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1159 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1160 | bytearray_contains(PyObject *self, PyObject *arg) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1161 | { |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame^] | 1162 | return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1165 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1166 | bytearray_startswith(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1167 | { |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame^] | 1168 | return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1171 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1172 | bytearray_endswith(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1173 | { |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame^] | 1174 | return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1178 | /*[clinic input] |
| 1179 | bytearray.translate |
| 1180 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1181 | table: object |
| 1182 | Translation table, which must be a bytes object of length 256. |
| 1183 | [ |
| 1184 | deletechars: object |
| 1185 | ] |
| 1186 | / |
| 1187 | |
| 1188 | Return a copy with each character mapped by the given translation table. |
| 1189 | |
| 1190 | All characters occurring in the optional argument deletechars are removed. |
| 1191 | The remaining characters are mapped through the given translation table. |
| 1192 | [clinic start generated code]*/ |
| 1193 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1194 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1195 | bytearray_translate_impl(PyByteArrayObject *self, PyObject *table, |
| 1196 | int group_right_1, PyObject *deletechars) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1197 | /*[clinic end generated code: output=2bebc86a9a1ff083 input=846a01671bccc1c5]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1198 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1199 | char *input, *output; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1200 | const char *table_chars; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1201 | Py_ssize_t i, c; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1202 | PyObject *input_obj = (PyObject*)self; |
| 1203 | const char *output_start; |
| 1204 | Py_ssize_t inlen; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1205 | PyObject *result = NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1206 | int trans_table[256]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1207 | Py_buffer vtable, vdel; |
| 1208 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1209 | if (table == Py_None) { |
| 1210 | table_chars = NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1211 | table = NULL; |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1212 | } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1213 | return NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1214 | } else { |
| 1215 | if (vtable.len != 256) { |
| 1216 | PyErr_SetString(PyExc_ValueError, |
| 1217 | "translation table must be 256 characters long"); |
Georg Brandl | 953152f | 2009-07-22 12:03:59 +0000 | [diff] [blame] | 1218 | PyBuffer_Release(&vtable); |
| 1219 | return NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1220 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1221 | table_chars = (const char*)vtable.buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1224 | if (deletechars != NULL) { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1225 | if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1226 | if (table != NULL) |
Georg Brandl | 953152f | 2009-07-22 12:03:59 +0000 | [diff] [blame] | 1227 | PyBuffer_Release(&vtable); |
| 1228 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | else { |
| 1232 | vdel.buf = NULL; |
| 1233 | vdel.len = 0; |
| 1234 | } |
| 1235 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1236 | inlen = PyByteArray_GET_SIZE(input_obj); |
| 1237 | result = PyByteArray_FromStringAndSize((char *)NULL, inlen); |
| 1238 | if (result == NULL) |
| 1239 | goto done; |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame^] | 1240 | output_start = output = PyByteArray_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1241 | input = PyByteArray_AS_STRING(input_obj); |
| 1242 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1243 | if (vdel.len == 0 && table_chars != NULL) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1244 | /* If no deletions are required, use faster code */ |
| 1245 | for (i = inlen; --i >= 0; ) { |
| 1246 | c = Py_CHARMASK(*input++); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1247 | *output++ = table_chars[c]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1248 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1249 | goto done; |
| 1250 | } |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1251 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1252 | if (table_chars == NULL) { |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1253 | for (i = 0; i < 256; i++) |
| 1254 | trans_table[i] = Py_CHARMASK(i); |
| 1255 | } else { |
| 1256 | for (i = 0; i < 256; i++) |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1257 | trans_table[i] = Py_CHARMASK(table_chars[i]); |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1258 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1259 | |
| 1260 | for (i = 0; i < vdel.len; i++) |
| 1261 | trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1; |
| 1262 | |
| 1263 | for (i = inlen; --i >= 0; ) { |
| 1264 | c = Py_CHARMASK(*input++); |
| 1265 | if (trans_table[c] != -1) |
| 1266 | if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
| 1267 | continue; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1268 | } |
| 1269 | /* Fix the size of the resulting string */ |
| 1270 | if (inlen > 0) |
Christian Heimes | c731bbe | 2013-07-21 02:04:35 +0200 | [diff] [blame] | 1271 | if (PyByteArray_Resize(result, output - output_start) < 0) { |
| 1272 | Py_CLEAR(result); |
| 1273 | goto done; |
| 1274 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1275 | |
| 1276 | done: |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1277 | if (table != NULL) |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1278 | PyBuffer_Release(&vtable); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1279 | if (deletechars != NULL) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1280 | PyBuffer_Release(&vdel); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1281 | return result; |
| 1282 | } |
| 1283 | |
| 1284 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1285 | /*[clinic input] |
| 1286 | |
| 1287 | @staticmethod |
| 1288 | bytearray.maketrans |
| 1289 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1290 | frm: Py_buffer |
| 1291 | to: Py_buffer |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1292 | / |
| 1293 | |
| 1294 | Return a translation table useable for the bytes or bytearray translate method. |
| 1295 | |
| 1296 | The returned table will be one where each byte in frm is mapped to the byte at |
| 1297 | the same position in to. |
| 1298 | |
| 1299 | The bytes objects frm and to must be of the same length. |
| 1300 | [clinic start generated code]*/ |
| 1301 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1302 | static PyObject * |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1303 | bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1304 | /*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1305 | { |
| 1306 | return _Py_bytes_maketrans(frm, to); |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1310 | /* find and count characters and substrings */ |
| 1311 | |
| 1312 | #define findchar(target, target_len, c) \ |
| 1313 | ((char *)memchr((const void *)(target), c, target_len)) |
| 1314 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1315 | |
Benjamin Peterson | 0f3641c | 2008-11-19 22:05:52 +0000 | [diff] [blame] | 1316 | /* Bytes ops must return a string, create a copy */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1317 | Py_LOCAL(PyByteArrayObject *) |
| 1318 | return_self(PyByteArrayObject *self) |
| 1319 | { |
Georg Brandl | 1e7217d | 2008-05-30 12:02:38 +0000 | [diff] [blame] | 1320 | /* always return a new bytearray */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1321 | return (PyByteArrayObject *)PyByteArray_FromStringAndSize( |
| 1322 | PyByteArray_AS_STRING(self), |
| 1323 | PyByteArray_GET_SIZE(self)); |
| 1324 | } |
| 1325 | |
| 1326 | Py_LOCAL_INLINE(Py_ssize_t) |
| 1327 | countchar(const char *target, Py_ssize_t target_len, char c, Py_ssize_t maxcount) |
| 1328 | { |
| 1329 | Py_ssize_t count=0; |
| 1330 | const char *start=target; |
| 1331 | const char *end=target+target_len; |
| 1332 | |
| 1333 | while ( (start=findchar(start, end-start, c)) != NULL ) { |
| 1334 | count++; |
| 1335 | if (count >= maxcount) |
| 1336 | break; |
| 1337 | start += 1; |
| 1338 | } |
| 1339 | return count; |
| 1340 | } |
| 1341 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1342 | |
| 1343 | /* Algorithms for different cases of string replacement */ |
| 1344 | |
| 1345 | /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */ |
| 1346 | Py_LOCAL(PyByteArrayObject *) |
| 1347 | replace_interleave(PyByteArrayObject *self, |
| 1348 | const char *to_s, Py_ssize_t to_len, |
| 1349 | Py_ssize_t maxcount) |
| 1350 | { |
| 1351 | char *self_s, *result_s; |
| 1352 | Py_ssize_t self_len, result_len; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1353 | Py_ssize_t count, i; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1354 | PyByteArrayObject *result; |
| 1355 | |
| 1356 | self_len = PyByteArray_GET_SIZE(self); |
| 1357 | |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1358 | /* 1 at the end plus 1 after every character; |
| 1359 | count = min(maxcount, self_len + 1) */ |
| 1360 | if (maxcount <= self_len) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1361 | count = maxcount; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1362 | else |
| 1363 | /* Can't overflow: self_len + 1 <= maxcount <= PY_SSIZE_T_MAX. */ |
| 1364 | count = self_len + 1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1365 | |
| 1366 | /* Check for overflow */ |
| 1367 | /* result_len = count * to_len + self_len; */ |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1368 | assert(count > 0); |
| 1369 | if (to_len > (PY_SSIZE_T_MAX - self_len) / count) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1370 | PyErr_SetString(PyExc_OverflowError, |
| 1371 | "replace string is too long"); |
| 1372 | return NULL; |
| 1373 | } |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1374 | result_len = count * to_len + self_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1375 | |
| 1376 | if (! (result = (PyByteArrayObject *) |
| 1377 | PyByteArray_FromStringAndSize(NULL, result_len)) ) |
| 1378 | return NULL; |
| 1379 | |
| 1380 | self_s = PyByteArray_AS_STRING(self); |
| 1381 | result_s = PyByteArray_AS_STRING(result); |
| 1382 | |
Victor Stinner | fac3956 | 2016-03-21 10:38:58 +0100 | [diff] [blame] | 1383 | if (to_len > 1) { |
| 1384 | /* Lay the first one down (guaranteed this will occur) */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1385 | Py_MEMCPY(result_s, to_s, to_len); |
| 1386 | result_s += to_len; |
Victor Stinner | fac3956 | 2016-03-21 10:38:58 +0100 | [diff] [blame] | 1387 | count -= 1; |
| 1388 | |
| 1389 | for (i = 0; i < count; i++) { |
| 1390 | *result_s++ = *self_s++; |
| 1391 | Py_MEMCPY(result_s, to_s, to_len); |
| 1392 | result_s += to_len; |
| 1393 | } |
| 1394 | } |
| 1395 | else { |
| 1396 | result_s[0] = to_s[0]; |
| 1397 | result_s += to_len; |
| 1398 | count -= 1; |
| 1399 | for (i = 0; i < count; i++) { |
| 1400 | *result_s++ = *self_s++; |
| 1401 | result_s[0] = to_s[0]; |
| 1402 | result_s += to_len; |
| 1403 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | /* Copy the rest of the original string */ |
| 1407 | Py_MEMCPY(result_s, self_s, self_len-i); |
| 1408 | |
| 1409 | return result; |
| 1410 | } |
| 1411 | |
| 1412 | /* Special case for deleting a single character */ |
| 1413 | /* len(self)>=1, len(from)==1, to="", maxcount>=1 */ |
| 1414 | Py_LOCAL(PyByteArrayObject *) |
| 1415 | replace_delete_single_character(PyByteArrayObject *self, |
| 1416 | char from_c, Py_ssize_t maxcount) |
| 1417 | { |
| 1418 | char *self_s, *result_s; |
| 1419 | char *start, *next, *end; |
| 1420 | Py_ssize_t self_len, result_len; |
| 1421 | Py_ssize_t count; |
| 1422 | PyByteArrayObject *result; |
| 1423 | |
| 1424 | self_len = PyByteArray_GET_SIZE(self); |
| 1425 | self_s = PyByteArray_AS_STRING(self); |
| 1426 | |
| 1427 | count = countchar(self_s, self_len, from_c, maxcount); |
| 1428 | if (count == 0) { |
| 1429 | return return_self(self); |
| 1430 | } |
| 1431 | |
| 1432 | result_len = self_len - count; /* from_len == 1 */ |
| 1433 | assert(result_len>=0); |
| 1434 | |
| 1435 | if ( (result = (PyByteArrayObject *) |
| 1436 | PyByteArray_FromStringAndSize(NULL, result_len)) == NULL) |
| 1437 | return NULL; |
| 1438 | result_s = PyByteArray_AS_STRING(result); |
| 1439 | |
| 1440 | start = self_s; |
| 1441 | end = self_s + self_len; |
| 1442 | while (count-- > 0) { |
| 1443 | next = findchar(start, end-start, from_c); |
| 1444 | if (next == NULL) |
| 1445 | break; |
| 1446 | Py_MEMCPY(result_s, start, next-start); |
| 1447 | result_s += (next-start); |
| 1448 | start = next+1; |
| 1449 | } |
| 1450 | Py_MEMCPY(result_s, start, end-start); |
| 1451 | |
| 1452 | return result; |
| 1453 | } |
| 1454 | |
| 1455 | /* len(self)>=1, len(from)>=2, to="", maxcount>=1 */ |
| 1456 | |
| 1457 | Py_LOCAL(PyByteArrayObject *) |
| 1458 | replace_delete_substring(PyByteArrayObject *self, |
| 1459 | const char *from_s, Py_ssize_t from_len, |
| 1460 | Py_ssize_t maxcount) |
| 1461 | { |
| 1462 | char *self_s, *result_s; |
| 1463 | char *start, *next, *end; |
| 1464 | Py_ssize_t self_len, result_len; |
| 1465 | Py_ssize_t count, offset; |
| 1466 | PyByteArrayObject *result; |
| 1467 | |
| 1468 | self_len = PyByteArray_GET_SIZE(self); |
| 1469 | self_s = PyByteArray_AS_STRING(self); |
| 1470 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1471 | count = stringlib_count(self_s, self_len, |
| 1472 | from_s, from_len, |
| 1473 | maxcount); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1474 | |
| 1475 | if (count == 0) { |
| 1476 | /* no matches */ |
| 1477 | return return_self(self); |
| 1478 | } |
| 1479 | |
| 1480 | result_len = self_len - (count * from_len); |
| 1481 | assert (result_len>=0); |
| 1482 | |
| 1483 | if ( (result = (PyByteArrayObject *) |
| 1484 | PyByteArray_FromStringAndSize(NULL, result_len)) == NULL ) |
| 1485 | return NULL; |
| 1486 | |
| 1487 | result_s = PyByteArray_AS_STRING(result); |
| 1488 | |
| 1489 | start = self_s; |
| 1490 | end = self_s + self_len; |
| 1491 | while (count-- > 0) { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1492 | offset = stringlib_find(start, end-start, |
| 1493 | from_s, from_len, |
| 1494 | 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1495 | if (offset == -1) |
| 1496 | break; |
| 1497 | next = start + offset; |
| 1498 | |
| 1499 | Py_MEMCPY(result_s, start, next-start); |
| 1500 | |
| 1501 | result_s += (next-start); |
| 1502 | start = next+from_len; |
| 1503 | } |
| 1504 | Py_MEMCPY(result_s, start, end-start); |
| 1505 | return result; |
| 1506 | } |
| 1507 | |
| 1508 | /* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */ |
| 1509 | Py_LOCAL(PyByteArrayObject *) |
| 1510 | replace_single_character_in_place(PyByteArrayObject *self, |
| 1511 | char from_c, char to_c, |
| 1512 | Py_ssize_t maxcount) |
| 1513 | { |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1514 | char *self_s, *result_s, *start, *end, *next; |
| 1515 | Py_ssize_t self_len; |
| 1516 | PyByteArrayObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1517 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1518 | /* The result string will be the same size */ |
| 1519 | self_s = PyByteArray_AS_STRING(self); |
| 1520 | self_len = PyByteArray_GET_SIZE(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1521 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1522 | next = findchar(self_s, self_len, from_c); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1523 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1524 | if (next == NULL) { |
| 1525 | /* No matches; return the original bytes */ |
| 1526 | return return_self(self); |
| 1527 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1528 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1529 | /* Need to make a new bytes */ |
| 1530 | result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len); |
| 1531 | if (result == NULL) |
| 1532 | return NULL; |
| 1533 | result_s = PyByteArray_AS_STRING(result); |
| 1534 | Py_MEMCPY(result_s, self_s, self_len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1535 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1536 | /* change everything in-place, starting with this one */ |
| 1537 | start = result_s + (next-self_s); |
| 1538 | *start = to_c; |
| 1539 | start++; |
| 1540 | end = result_s + self_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1541 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1542 | while (--maxcount > 0) { |
| 1543 | next = findchar(start, end-start, from_c); |
| 1544 | if (next == NULL) |
| 1545 | break; |
| 1546 | *next = to_c; |
| 1547 | start = next+1; |
| 1548 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1549 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1550 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | /* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */ |
| 1554 | Py_LOCAL(PyByteArrayObject *) |
| 1555 | replace_substring_in_place(PyByteArrayObject *self, |
| 1556 | const char *from_s, Py_ssize_t from_len, |
| 1557 | const char *to_s, Py_ssize_t to_len, |
| 1558 | Py_ssize_t maxcount) |
| 1559 | { |
| 1560 | char *result_s, *start, *end; |
| 1561 | char *self_s; |
| 1562 | Py_ssize_t self_len, offset; |
| 1563 | PyByteArrayObject *result; |
| 1564 | |
| 1565 | /* The result bytes will be the same size */ |
| 1566 | |
| 1567 | self_s = PyByteArray_AS_STRING(self); |
| 1568 | self_len = PyByteArray_GET_SIZE(self); |
| 1569 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1570 | offset = stringlib_find(self_s, self_len, |
| 1571 | from_s, from_len, |
| 1572 | 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1573 | if (offset == -1) { |
| 1574 | /* No matches; return the original bytes */ |
| 1575 | return return_self(self); |
| 1576 | } |
| 1577 | |
| 1578 | /* Need to make a new bytes */ |
| 1579 | result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len); |
| 1580 | if (result == NULL) |
| 1581 | return NULL; |
| 1582 | result_s = PyByteArray_AS_STRING(result); |
| 1583 | Py_MEMCPY(result_s, self_s, self_len); |
| 1584 | |
| 1585 | /* change everything in-place, starting with this one */ |
| 1586 | start = result_s + offset; |
| 1587 | Py_MEMCPY(start, to_s, from_len); |
| 1588 | start += from_len; |
| 1589 | end = result_s + self_len; |
| 1590 | |
| 1591 | while ( --maxcount > 0) { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1592 | offset = stringlib_find(start, end-start, |
| 1593 | from_s, from_len, |
| 1594 | 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1595 | if (offset==-1) |
| 1596 | break; |
| 1597 | Py_MEMCPY(start+offset, to_s, from_len); |
| 1598 | start += offset+from_len; |
| 1599 | } |
| 1600 | |
| 1601 | return result; |
| 1602 | } |
| 1603 | |
| 1604 | /* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */ |
| 1605 | Py_LOCAL(PyByteArrayObject *) |
| 1606 | replace_single_character(PyByteArrayObject *self, |
| 1607 | char from_c, |
| 1608 | const char *to_s, Py_ssize_t to_len, |
| 1609 | Py_ssize_t maxcount) |
| 1610 | { |
| 1611 | char *self_s, *result_s; |
| 1612 | char *start, *next, *end; |
| 1613 | Py_ssize_t self_len, result_len; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1614 | Py_ssize_t count; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1615 | PyByteArrayObject *result; |
| 1616 | |
| 1617 | self_s = PyByteArray_AS_STRING(self); |
| 1618 | self_len = PyByteArray_GET_SIZE(self); |
| 1619 | |
| 1620 | count = countchar(self_s, self_len, from_c, maxcount); |
| 1621 | if (count == 0) { |
| 1622 | /* no matches, return unchanged */ |
| 1623 | return return_self(self); |
| 1624 | } |
| 1625 | |
| 1626 | /* use the difference between current and new, hence the "-1" */ |
| 1627 | /* result_len = self_len + count * (to_len-1) */ |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1628 | assert(count > 0); |
| 1629 | if (to_len - 1 > (PY_SSIZE_T_MAX - self_len) / count) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1630 | PyErr_SetString(PyExc_OverflowError, "replace bytes is too long"); |
| 1631 | return NULL; |
| 1632 | } |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1633 | result_len = self_len + count * (to_len - 1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1634 | |
| 1635 | if ( (result = (PyByteArrayObject *) |
| 1636 | PyByteArray_FromStringAndSize(NULL, result_len)) == NULL) |
| 1637 | return NULL; |
| 1638 | result_s = PyByteArray_AS_STRING(result); |
| 1639 | |
| 1640 | start = self_s; |
| 1641 | end = self_s + self_len; |
| 1642 | while (count-- > 0) { |
| 1643 | next = findchar(start, end-start, from_c); |
| 1644 | if (next == NULL) |
| 1645 | break; |
| 1646 | |
| 1647 | if (next == start) { |
| 1648 | /* replace with the 'to' */ |
| 1649 | Py_MEMCPY(result_s, to_s, to_len); |
| 1650 | result_s += to_len; |
| 1651 | start += 1; |
| 1652 | } else { |
| 1653 | /* copy the unchanged old then the 'to' */ |
| 1654 | Py_MEMCPY(result_s, start, next-start); |
| 1655 | result_s += (next-start); |
| 1656 | Py_MEMCPY(result_s, to_s, to_len); |
| 1657 | result_s += to_len; |
| 1658 | start = next+1; |
| 1659 | } |
| 1660 | } |
| 1661 | /* Copy the remainder of the remaining bytes */ |
| 1662 | Py_MEMCPY(result_s, start, end-start); |
| 1663 | |
| 1664 | return result; |
| 1665 | } |
| 1666 | |
| 1667 | /* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */ |
| 1668 | Py_LOCAL(PyByteArrayObject *) |
| 1669 | replace_substring(PyByteArrayObject *self, |
| 1670 | const char *from_s, Py_ssize_t from_len, |
| 1671 | const char *to_s, Py_ssize_t to_len, |
| 1672 | Py_ssize_t maxcount) |
| 1673 | { |
| 1674 | char *self_s, *result_s; |
| 1675 | char *start, *next, *end; |
| 1676 | Py_ssize_t self_len, result_len; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1677 | Py_ssize_t count, offset; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1678 | PyByteArrayObject *result; |
| 1679 | |
| 1680 | self_s = PyByteArray_AS_STRING(self); |
| 1681 | self_len = PyByteArray_GET_SIZE(self); |
| 1682 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1683 | count = stringlib_count(self_s, self_len, |
| 1684 | from_s, from_len, |
| 1685 | maxcount); |
| 1686 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1687 | if (count == 0) { |
| 1688 | /* no matches, return unchanged */ |
| 1689 | return return_self(self); |
| 1690 | } |
| 1691 | |
| 1692 | /* Check for overflow */ |
| 1693 | /* result_len = self_len + count * (to_len-from_len) */ |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1694 | assert(count > 0); |
| 1695 | if (to_len - from_len > (PY_SSIZE_T_MAX - self_len) / count) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1696 | PyErr_SetString(PyExc_OverflowError, "replace bytes is too long"); |
| 1697 | return NULL; |
| 1698 | } |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1699 | result_len = self_len + count * (to_len - from_len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1700 | |
| 1701 | if ( (result = (PyByteArrayObject *) |
| 1702 | PyByteArray_FromStringAndSize(NULL, result_len)) == NULL) |
| 1703 | return NULL; |
| 1704 | result_s = PyByteArray_AS_STRING(result); |
| 1705 | |
| 1706 | start = self_s; |
| 1707 | end = self_s + self_len; |
| 1708 | while (count-- > 0) { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1709 | offset = stringlib_find(start, end-start, |
| 1710 | from_s, from_len, |
| 1711 | 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1712 | if (offset == -1) |
| 1713 | break; |
| 1714 | next = start+offset; |
| 1715 | if (next == start) { |
| 1716 | /* replace with the 'to' */ |
| 1717 | Py_MEMCPY(result_s, to_s, to_len); |
| 1718 | result_s += to_len; |
| 1719 | start += from_len; |
| 1720 | } else { |
| 1721 | /* copy the unchanged old then the 'to' */ |
| 1722 | Py_MEMCPY(result_s, start, next-start); |
| 1723 | result_s += (next-start); |
| 1724 | Py_MEMCPY(result_s, to_s, to_len); |
| 1725 | result_s += to_len; |
| 1726 | start = next+from_len; |
| 1727 | } |
| 1728 | } |
| 1729 | /* Copy the remainder of the remaining bytes */ |
| 1730 | Py_MEMCPY(result_s, start, end-start); |
| 1731 | |
| 1732 | return result; |
| 1733 | } |
| 1734 | |
| 1735 | |
| 1736 | Py_LOCAL(PyByteArrayObject *) |
| 1737 | replace(PyByteArrayObject *self, |
| 1738 | const char *from_s, Py_ssize_t from_len, |
| 1739 | const char *to_s, Py_ssize_t to_len, |
| 1740 | Py_ssize_t maxcount) |
| 1741 | { |
| 1742 | if (maxcount < 0) { |
| 1743 | maxcount = PY_SSIZE_T_MAX; |
| 1744 | } else if (maxcount == 0 || PyByteArray_GET_SIZE(self) == 0) { |
| 1745 | /* nothing to do; return the original bytes */ |
| 1746 | return return_self(self); |
| 1747 | } |
| 1748 | |
| 1749 | if (maxcount == 0 || |
| 1750 | (from_len == 0 && to_len == 0)) { |
| 1751 | /* nothing to do; return the original bytes */ |
| 1752 | return return_self(self); |
| 1753 | } |
| 1754 | |
| 1755 | /* Handle zero-length special cases */ |
| 1756 | |
| 1757 | if (from_len == 0) { |
| 1758 | /* insert the 'to' bytes everywhere. */ |
| 1759 | /* >>> "Python".replace("", ".") */ |
| 1760 | /* '.P.y.t.h.o.n.' */ |
| 1761 | return replace_interleave(self, to_s, to_len, maxcount); |
| 1762 | } |
| 1763 | |
| 1764 | /* Except for "".replace("", "A") == "A" there is no way beyond this */ |
| 1765 | /* point for an empty self bytes to generate a non-empty bytes */ |
| 1766 | /* Special case so the remaining code always gets a non-empty bytes */ |
| 1767 | if (PyByteArray_GET_SIZE(self) == 0) { |
| 1768 | return return_self(self); |
| 1769 | } |
| 1770 | |
| 1771 | if (to_len == 0) { |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1772 | /* delete all occurrences of 'from' bytes */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1773 | if (from_len == 1) { |
| 1774 | return replace_delete_single_character( |
| 1775 | self, from_s[0], maxcount); |
| 1776 | } else { |
| 1777 | return replace_delete_substring(self, from_s, from_len, maxcount); |
| 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | /* Handle special case where both bytes have the same length */ |
| 1782 | |
| 1783 | if (from_len == to_len) { |
| 1784 | if (from_len == 1) { |
| 1785 | return replace_single_character_in_place( |
| 1786 | self, |
| 1787 | from_s[0], |
| 1788 | to_s[0], |
| 1789 | maxcount); |
| 1790 | } else { |
| 1791 | return replace_substring_in_place( |
| 1792 | self, from_s, from_len, to_s, to_len, maxcount); |
| 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | /* Otherwise use the more generic algorithms */ |
| 1797 | if (from_len == 1) { |
| 1798 | return replace_single_character(self, from_s[0], |
| 1799 | to_s, to_len, maxcount); |
| 1800 | } else { |
| 1801 | /* len('from')>=2, len('to')>=1 */ |
| 1802 | return replace_substring(self, from_s, from_len, to_s, to_len, maxcount); |
| 1803 | } |
| 1804 | } |
| 1805 | |
| 1806 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1807 | /*[clinic input] |
| 1808 | bytearray.replace |
| 1809 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1810 | old: Py_buffer |
| 1811 | new: Py_buffer |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1812 | count: Py_ssize_t = -1 |
| 1813 | Maximum number of occurrences to replace. |
| 1814 | -1 (the default value) means replace all occurrences. |
| 1815 | / |
| 1816 | |
| 1817 | Return a copy with all occurrences of substring old replaced by new. |
| 1818 | |
| 1819 | If the optional argument count is given, only the first count occurrences are |
| 1820 | replaced. |
| 1821 | [clinic start generated code]*/ |
| 1822 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1823 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1824 | bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old, |
| 1825 | Py_buffer *new, Py_ssize_t count) |
| 1826 | /*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1827 | { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1828 | return (PyObject *)replace((PyByteArrayObject *) self, |
| 1829 | old->buf, old->len, |
| 1830 | new->buf, new->len, count); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1833 | /*[clinic input] |
| 1834 | bytearray.split |
| 1835 | |
| 1836 | sep: object = None |
| 1837 | The delimiter according which to split the bytearray. |
| 1838 | None (the default value) means split on ASCII whitespace characters |
| 1839 | (space, tab, return, newline, formfeed, vertical tab). |
| 1840 | maxsplit: Py_ssize_t = -1 |
| 1841 | Maximum number of splits to do. |
| 1842 | -1 (the default value) means no limit. |
| 1843 | |
| 1844 | Return a list of the sections in the bytearray, using sep as the delimiter. |
| 1845 | [clinic start generated code]*/ |
| 1846 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1847 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1848 | bytearray_split_impl(PyByteArrayObject *self, PyObject *sep, |
| 1849 | Py_ssize_t maxsplit) |
| 1850 | /*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1851 | { |
| 1852 | Py_ssize_t len = PyByteArray_GET_SIZE(self), n; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1853 | const char *s = PyByteArray_AS_STRING(self), *sub; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1854 | PyObject *list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1855 | Py_buffer vsub; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1856 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1857 | if (maxsplit < 0) |
| 1858 | maxsplit = PY_SSIZE_T_MAX; |
| 1859 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1860 | if (sep == Py_None) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1861 | return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1862 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1863 | if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1864 | return NULL; |
| 1865 | sub = vsub.buf; |
| 1866 | n = vsub.len; |
| 1867 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1868 | list = stringlib_split( |
| 1869 | (PyObject*) self, s, len, sub, n, maxsplit |
| 1870 | ); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1871 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1872 | return list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1875 | /*[clinic input] |
| 1876 | bytearray.partition |
| 1877 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1878 | sep: object |
| 1879 | / |
| 1880 | |
| 1881 | Partition the bytearray into three parts using the given separator. |
| 1882 | |
| 1883 | This will search for the separator sep in the bytearray. If the separator is |
| 1884 | found, returns a 3-tuple containing the part before the separator, the |
| 1885 | separator itself, and the part after it. |
| 1886 | |
| 1887 | If the separator is not found, returns a 3-tuple containing the original |
| 1888 | bytearray object and two empty bytearray objects. |
| 1889 | [clinic start generated code]*/ |
| 1890 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1891 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1892 | bytearray_partition(PyByteArrayObject *self, PyObject *sep) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1893 | /*[clinic end generated code: output=45d2525ddd35f957 input=86f89223892b70b5]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1894 | { |
| 1895 | PyObject *bytesep, *result; |
| 1896 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1897 | bytesep = PyByteArray_FromObject(sep); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1898 | if (! bytesep) |
| 1899 | return NULL; |
| 1900 | |
| 1901 | result = stringlib_partition( |
| 1902 | (PyObject*) self, |
| 1903 | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
| 1904 | bytesep, |
| 1905 | PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) |
| 1906 | ); |
| 1907 | |
| 1908 | Py_DECREF(bytesep); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1909 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1912 | /*[clinic input] |
| 1913 | bytearray.rpartition |
| 1914 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1915 | sep: object |
| 1916 | / |
| 1917 | |
| 1918 | Partition the bytes into three parts using the given separator. |
| 1919 | |
| 1920 | This will search for the separator sep in the bytearray, starting and the end. |
| 1921 | If the separator is found, returns a 3-tuple containing the part before the |
| 1922 | separator, the separator itself, and the part after it. |
| 1923 | |
| 1924 | If the separator is not found, returns a 3-tuple containing two empty bytearray |
| 1925 | objects and the original bytearray object. |
| 1926 | [clinic start generated code]*/ |
| 1927 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1928 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1929 | bytearray_rpartition(PyByteArrayObject *self, PyObject *sep) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1930 | /*[clinic end generated code: output=440de3c9426115e8 input=5f4094f2de87c8f3]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1931 | { |
| 1932 | PyObject *bytesep, *result; |
| 1933 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1934 | bytesep = PyByteArray_FromObject(sep); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1935 | if (! bytesep) |
| 1936 | return NULL; |
| 1937 | |
| 1938 | result = stringlib_rpartition( |
| 1939 | (PyObject*) self, |
| 1940 | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
| 1941 | bytesep, |
| 1942 | PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) |
| 1943 | ); |
| 1944 | |
| 1945 | Py_DECREF(bytesep); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1946 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1949 | /*[clinic input] |
| 1950 | bytearray.rsplit = bytearray.split |
| 1951 | |
| 1952 | Return a list of the sections in the bytearray, using sep as the delimiter. |
| 1953 | |
| 1954 | Splitting is done starting at the end of the bytearray and working to the front. |
| 1955 | [clinic start generated code]*/ |
| 1956 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1957 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1958 | bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep, |
| 1959 | Py_ssize_t maxsplit) |
| 1960 | /*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1961 | { |
| 1962 | Py_ssize_t len = PyByteArray_GET_SIZE(self), n; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1963 | const char *s = PyByteArray_AS_STRING(self), *sub; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1964 | PyObject *list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1965 | Py_buffer vsub; |
| 1966 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1967 | if (maxsplit < 0) |
| 1968 | maxsplit = PY_SSIZE_T_MAX; |
| 1969 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1970 | if (sep == Py_None) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1971 | return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1972 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1973 | if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1974 | return NULL; |
| 1975 | sub = vsub.buf; |
| 1976 | n = vsub.len; |
| 1977 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1978 | list = stringlib_rsplit( |
| 1979 | (PyObject*) self, s, len, sub, n, maxsplit |
| 1980 | ); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1981 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1982 | return list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1985 | /*[clinic input] |
| 1986 | bytearray.reverse |
| 1987 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1988 | Reverse the order of the values in B in place. |
| 1989 | [clinic start generated code]*/ |
| 1990 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1991 | static PyObject * |
| 1992 | bytearray_reverse_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1993 | /*[clinic end generated code: output=9f7616f29ab309d3 input=543356319fc78557]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1994 | { |
| 1995 | char swap, *head, *tail; |
| 1996 | Py_ssize_t i, j, n = Py_SIZE(self); |
| 1997 | |
| 1998 | j = n / 2; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1999 | head = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2000 | tail = head + n - 1; |
| 2001 | for (i = 0; i < j; i++) { |
| 2002 | swap = *head; |
| 2003 | *head++ = *tail; |
| 2004 | *tail-- = swap; |
| 2005 | } |
| 2006 | |
| 2007 | Py_RETURN_NONE; |
| 2008 | } |
| 2009 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2010 | |
| 2011 | /*[python input] |
| 2012 | class bytesvalue_converter(CConverter): |
| 2013 | type = 'int' |
| 2014 | converter = '_getbytevalue' |
| 2015 | [python start generated code]*/ |
| 2016 | /*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/ |
| 2017 | |
| 2018 | |
| 2019 | /*[clinic input] |
| 2020 | bytearray.insert |
| 2021 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2022 | index: Py_ssize_t |
| 2023 | The index where the value is to be inserted. |
| 2024 | item: bytesvalue |
| 2025 | The item to be inserted. |
| 2026 | / |
| 2027 | |
| 2028 | Insert a single item into the bytearray before the given index. |
| 2029 | [clinic start generated code]*/ |
| 2030 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2031 | static PyObject * |
| 2032 | bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 2033 | /*[clinic end generated code: output=76c775a70e7b07b7 input=b2b5d07e9de6c070]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2034 | { |
| 2035 | Py_ssize_t n = Py_SIZE(self); |
| 2036 | char *buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2037 | |
| 2038 | if (n == PY_SSIZE_T_MAX) { |
| 2039 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 2040 | "cannot add more objects to bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2041 | return NULL; |
| 2042 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2043 | if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) |
| 2044 | return NULL; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2045 | buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2046 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2047 | if (index < 0) { |
| 2048 | index += n; |
| 2049 | if (index < 0) |
| 2050 | index = 0; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2051 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2052 | if (index > n) |
| 2053 | index = n; |
| 2054 | memmove(buf + index + 1, buf + index, n - index); |
| 2055 | buf[index] = item; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2056 | |
| 2057 | Py_RETURN_NONE; |
| 2058 | } |
| 2059 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2060 | /*[clinic input] |
| 2061 | bytearray.append |
| 2062 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2063 | item: bytesvalue |
| 2064 | The item to be appended. |
| 2065 | / |
| 2066 | |
| 2067 | Append a single item to the end of the bytearray. |
| 2068 | [clinic start generated code]*/ |
| 2069 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2070 | static PyObject * |
| 2071 | bytearray_append_impl(PyByteArrayObject *self, int item) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 2072 | /*[clinic end generated code: output=a154e19ed1886cb6 input=20d6bec3d1340593]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2073 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2074 | Py_ssize_t n = Py_SIZE(self); |
| 2075 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2076 | if (n == PY_SSIZE_T_MAX) { |
| 2077 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 2078 | "cannot add more objects to bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2079 | return NULL; |
| 2080 | } |
| 2081 | if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) |
| 2082 | return NULL; |
| 2083 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2084 | PyByteArray_AS_STRING(self)[n] = item; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2085 | |
| 2086 | Py_RETURN_NONE; |
| 2087 | } |
| 2088 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2089 | /*[clinic input] |
| 2090 | bytearray.extend |
| 2091 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2092 | iterable_of_ints: object |
| 2093 | The iterable of items to append. |
| 2094 | / |
| 2095 | |
| 2096 | Append all the items from the iterator or sequence to the end of the bytearray. |
| 2097 | [clinic start generated code]*/ |
| 2098 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2099 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2100 | bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 2101 | /*[clinic end generated code: output=98155dbe249170b1 input=c617b3a93249ba28]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2102 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2103 | PyObject *it, *item, *bytearray_obj; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2104 | Py_ssize_t buf_size = 0, len = 0; |
| 2105 | int value; |
| 2106 | char *buf; |
| 2107 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2108 | /* bytearray_setslice code only accepts something supporting PEP 3118. */ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2109 | if (PyObject_CheckBuffer(iterable_of_ints)) { |
| 2110 | 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] | 2111 | return NULL; |
| 2112 | |
| 2113 | Py_RETURN_NONE; |
| 2114 | } |
| 2115 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2116 | it = PyObject_GetIter(iterable_of_ints); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2117 | if (it == NULL) |
| 2118 | return NULL; |
| 2119 | |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 2120 | /* 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] | 2121 | buf_size = PyObject_LengthHint(iterable_of_ints, 32); |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 2122 | if (buf_size == -1) { |
| 2123 | Py_DECREF(it); |
| 2124 | return NULL; |
| 2125 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2126 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2127 | bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size); |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 2128 | if (bytearray_obj == NULL) { |
| 2129 | Py_DECREF(it); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2130 | return NULL; |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 2131 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2132 | buf = PyByteArray_AS_STRING(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2133 | |
| 2134 | while ((item = PyIter_Next(it)) != NULL) { |
| 2135 | if (! _getbytevalue(item, &value)) { |
| 2136 | Py_DECREF(item); |
| 2137 | Py_DECREF(it); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2138 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2139 | return NULL; |
| 2140 | } |
| 2141 | buf[len++] = value; |
| 2142 | Py_DECREF(item); |
| 2143 | |
| 2144 | if (len >= buf_size) { |
| 2145 | buf_size = len + (len >> 1) + 1; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2146 | if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2147 | Py_DECREF(it); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2148 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2149 | return NULL; |
| 2150 | } |
| 2151 | /* Recompute the `buf' pointer, since the resizing operation may |
| 2152 | have invalidated it. */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2153 | buf = PyByteArray_AS_STRING(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2154 | } |
| 2155 | } |
| 2156 | Py_DECREF(it); |
| 2157 | |
| 2158 | /* Resize down to exact size. */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2159 | if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) { |
| 2160 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2161 | return NULL; |
| 2162 | } |
| 2163 | |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 2164 | if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) { |
| 2165 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2166 | return NULL; |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 2167 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2168 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2169 | |
| 2170 | Py_RETURN_NONE; |
| 2171 | } |
| 2172 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2173 | /*[clinic input] |
| 2174 | bytearray.pop |
| 2175 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2176 | index: Py_ssize_t = -1 |
| 2177 | The index from where to remove the item. |
| 2178 | -1 (the default value) means remove the last item. |
| 2179 | / |
| 2180 | |
| 2181 | Remove and return a single item from B. |
| 2182 | |
| 2183 | If no index argument is given, will pop the last item. |
| 2184 | [clinic start generated code]*/ |
| 2185 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2186 | static PyObject * |
| 2187 | bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 2188 | /*[clinic end generated code: output=e0ccd401f8021da8 input=3591df2d06c0d237]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2189 | { |
| 2190 | int value; |
| 2191 | Py_ssize_t n = Py_SIZE(self); |
| 2192 | char *buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2193 | |
| 2194 | if (n == 0) { |
Eli Bendersky | 1bc4f19 | 2011-03-04 04:55:25 +0000 | [diff] [blame] | 2195 | PyErr_SetString(PyExc_IndexError, |
| 2196 | "pop from empty bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2197 | return NULL; |
| 2198 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2199 | if (index < 0) |
| 2200 | index += Py_SIZE(self); |
| 2201 | if (index < 0 || index >= Py_SIZE(self)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2202 | PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
| 2203 | return NULL; |
| 2204 | } |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 2205 | if (!_canresize(self)) |
| 2206 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2207 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2208 | buf = PyByteArray_AS_STRING(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2209 | value = buf[index]; |
| 2210 | memmove(buf + index, buf + index + 1, n - index); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2211 | if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) |
| 2212 | return NULL; |
| 2213 | |
Mark Dickinson | 54a3db9 | 2009-09-06 10:19:23 +0000 | [diff] [blame] | 2214 | return PyLong_FromLong((unsigned char)value); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2217 | /*[clinic input] |
| 2218 | bytearray.remove |
| 2219 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2220 | value: bytesvalue |
| 2221 | The value to remove. |
| 2222 | / |
| 2223 | |
| 2224 | Remove the first occurrence of a value in the bytearray. |
| 2225 | [clinic start generated code]*/ |
| 2226 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2227 | static PyObject * |
| 2228 | bytearray_remove_impl(PyByteArrayObject *self, int value) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 2229 | /*[clinic end generated code: output=d659e37866709c13 input=121831240cd51ddf]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2230 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2231 | Py_ssize_t where, n = Py_SIZE(self); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2232 | char *buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2233 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2234 | for (where = 0; where < n; where++) { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2235 | if (buf[where] == value) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2236 | break; |
| 2237 | } |
| 2238 | if (where == n) { |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 2239 | PyErr_SetString(PyExc_ValueError, "value not found in bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2240 | return NULL; |
| 2241 | } |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 2242 | if (!_canresize(self)) |
| 2243 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2244 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2245 | memmove(buf + where, buf + where + 1, n - where); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2246 | if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) |
| 2247 | return NULL; |
| 2248 | |
| 2249 | Py_RETURN_NONE; |
| 2250 | } |
| 2251 | |
| 2252 | /* XXX These two helpers could be optimized if argsize == 1 */ |
| 2253 | |
| 2254 | static Py_ssize_t |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2255 | lstrip_helper(const char *myptr, Py_ssize_t mysize, |
| 2256 | const void *argptr, Py_ssize_t argsize) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2257 | { |
| 2258 | Py_ssize_t i = 0; |
Antoine Pitrou | 5b72075 | 2013-10-05 21:24:10 +0200 | [diff] [blame] | 2259 | while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2260 | i++; |
| 2261 | return i; |
| 2262 | } |
| 2263 | |
| 2264 | static Py_ssize_t |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2265 | rstrip_helper(const char *myptr, Py_ssize_t mysize, |
| 2266 | const void *argptr, Py_ssize_t argsize) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2267 | { |
| 2268 | Py_ssize_t i = mysize - 1; |
Antoine Pitrou | 5b72075 | 2013-10-05 21:24:10 +0200 | [diff] [blame] | 2269 | while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2270 | i--; |
| 2271 | return i + 1; |
| 2272 | } |
| 2273 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2274 | /*[clinic input] |
| 2275 | bytearray.strip |
| 2276 | |
| 2277 | bytes: object = None |
| 2278 | / |
| 2279 | |
| 2280 | Strip leading and trailing bytes contained in the argument. |
| 2281 | |
| 2282 | If the argument is omitted or None, strip leading and trailing ASCII whitespace. |
| 2283 | [clinic start generated code]*/ |
| 2284 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2285 | static PyObject * |
| 2286 | bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2287 | /*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2288 | { |
| 2289 | Py_ssize_t left, right, mysize, byteslen; |
| 2290 | char *myptr, *bytesptr; |
| 2291 | Py_buffer vbytes; |
| 2292 | |
| 2293 | if (bytes == Py_None) { |
| 2294 | bytesptr = "\t\n\r\f\v "; |
| 2295 | byteslen = 6; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2296 | } |
| 2297 | else { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2298 | if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2299 | return NULL; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2300 | bytesptr = (char *) vbytes.buf; |
| 2301 | byteslen = vbytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2302 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2303 | myptr = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2304 | mysize = Py_SIZE(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2305 | left = lstrip_helper(myptr, mysize, bytesptr, byteslen); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2306 | if (left == mysize) |
| 2307 | right = left; |
| 2308 | else |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2309 | right = rstrip_helper(myptr, mysize, bytesptr, byteslen); |
| 2310 | if (bytes != Py_None) |
| 2311 | PyBuffer_Release(&vbytes); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2312 | return PyByteArray_FromStringAndSize(myptr + left, right - left); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2313 | } |
| 2314 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2315 | /*[clinic input] |
| 2316 | bytearray.lstrip |
| 2317 | |
| 2318 | bytes: object = None |
| 2319 | / |
| 2320 | |
| 2321 | Strip leading bytes contained in the argument. |
| 2322 | |
| 2323 | If the argument is omitted or None, strip leading ASCII whitespace. |
| 2324 | [clinic start generated code]*/ |
| 2325 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2326 | static PyObject * |
| 2327 | bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2328 | /*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2329 | { |
| 2330 | Py_ssize_t left, right, mysize, byteslen; |
| 2331 | char *myptr, *bytesptr; |
| 2332 | Py_buffer vbytes; |
| 2333 | |
| 2334 | if (bytes == Py_None) { |
| 2335 | bytesptr = "\t\n\r\f\v "; |
| 2336 | byteslen = 6; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2337 | } |
| 2338 | else { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2339 | if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2340 | return NULL; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2341 | bytesptr = (char *) vbytes.buf; |
| 2342 | byteslen = vbytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2343 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2344 | myptr = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2345 | mysize = Py_SIZE(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2346 | left = lstrip_helper(myptr, mysize, bytesptr, byteslen); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2347 | right = mysize; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2348 | if (bytes != Py_None) |
| 2349 | PyBuffer_Release(&vbytes); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2350 | return PyByteArray_FromStringAndSize(myptr + left, right - left); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2351 | } |
| 2352 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2353 | /*[clinic input] |
| 2354 | bytearray.rstrip |
| 2355 | |
| 2356 | bytes: object = None |
| 2357 | / |
| 2358 | |
| 2359 | Strip trailing bytes contained in the argument. |
| 2360 | |
| 2361 | If the argument is omitted or None, strip trailing ASCII whitespace. |
| 2362 | [clinic start generated code]*/ |
| 2363 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2364 | static PyObject * |
| 2365 | bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2366 | /*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2367 | { |
| 2368 | Py_ssize_t right, mysize, byteslen; |
| 2369 | char *myptr, *bytesptr; |
| 2370 | Py_buffer vbytes; |
| 2371 | |
| 2372 | if (bytes == Py_None) { |
| 2373 | bytesptr = "\t\n\r\f\v "; |
| 2374 | byteslen = 6; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2375 | } |
| 2376 | else { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2377 | if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2378 | return NULL; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2379 | bytesptr = (char *) vbytes.buf; |
| 2380 | byteslen = vbytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2381 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2382 | myptr = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2383 | mysize = Py_SIZE(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2384 | right = rstrip_helper(myptr, mysize, bytesptr, byteslen); |
| 2385 | if (bytes != Py_None) |
| 2386 | PyBuffer_Release(&vbytes); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2387 | return PyByteArray_FromStringAndSize(myptr, right); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2390 | /*[clinic input] |
| 2391 | bytearray.decode |
| 2392 | |
| 2393 | encoding: str(c_default="NULL") = 'utf-8' |
| 2394 | The encoding with which to decode the bytearray. |
| 2395 | errors: str(c_default="NULL") = 'strict' |
| 2396 | The error handling scheme to use for the handling of decoding errors. |
| 2397 | The default is 'strict' meaning that decoding errors raise a |
| 2398 | UnicodeDecodeError. Other possible values are 'ignore' and 'replace' |
| 2399 | as well as any other name registered with codecs.register_error that |
| 2400 | can handle UnicodeDecodeErrors. |
| 2401 | |
| 2402 | Decode the bytearray using the codec registered for encoding. |
| 2403 | [clinic start generated code]*/ |
| 2404 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2405 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2406 | bytearray_decode_impl(PyByteArrayObject *self, const char *encoding, |
| 2407 | const char *errors) |
| 2408 | /*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2409 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2410 | if (encoding == NULL) |
| 2411 | encoding = PyUnicode_GetDefaultEncoding(); |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2412 | return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2413 | } |
| 2414 | |
| 2415 | PyDoc_STRVAR(alloc_doc, |
| 2416 | "B.__alloc__() -> int\n\ |
| 2417 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2418 | Return the number of bytes actually allocated."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2419 | |
| 2420 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2421 | bytearray_alloc(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2422 | { |
| 2423 | return PyLong_FromSsize_t(self->ob_alloc); |
| 2424 | } |
| 2425 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2426 | /*[clinic input] |
| 2427 | bytearray.join |
| 2428 | |
| 2429 | iterable_of_bytes: object |
| 2430 | / |
| 2431 | |
| 2432 | Concatenate any number of bytes/bytearray objects. |
| 2433 | |
| 2434 | The bytearray whose method is called is inserted in between each pair. |
| 2435 | |
| 2436 | The result is returned as a new bytearray object. |
| 2437 | [clinic start generated code]*/ |
| 2438 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2439 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2440 | bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2441 | /*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2442 | { |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2443 | return stringlib_bytes_join((PyObject*)self, iterable_of_bytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2446 | /*[clinic input] |
| 2447 | bytearray.splitlines |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2448 | |
Serhiy Storchaka | 8b2e8b6 | 2015-05-30 11:30:39 +0300 | [diff] [blame] | 2449 | keepends: int(c_default="0") = False |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2450 | |
| 2451 | Return a list of the lines in the bytearray, breaking at line boundaries. |
| 2452 | |
| 2453 | Line breaks are not included in the resulting list unless keepends is given and |
| 2454 | true. |
| 2455 | [clinic start generated code]*/ |
| 2456 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2457 | static PyObject * |
| 2458 | bytearray_splitlines_impl(PyByteArrayObject *self, int keepends) |
Serhiy Storchaka | 8b2e8b6 | 2015-05-30 11:30:39 +0300 | [diff] [blame] | 2459 | /*[clinic end generated code: output=4223c94b895f6ad9 input=8ccade941e5ea0bd]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2460 | { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2461 | return stringlib_splitlines( |
| 2462 | (PyObject*) self, PyByteArray_AS_STRING(self), |
| 2463 | PyByteArray_GET_SIZE(self), keepends |
| 2464 | ); |
| 2465 | } |
| 2466 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2467 | /*[clinic input] |
| 2468 | @classmethod |
| 2469 | bytearray.fromhex |
| 2470 | |
| 2471 | cls: self(type="PyObject*") |
| 2472 | string: unicode |
| 2473 | / |
| 2474 | |
| 2475 | Create a bytearray object from a string of hexadecimal numbers. |
| 2476 | |
| 2477 | Spaces between two numbers are accepted. |
| 2478 | Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef') |
| 2479 | [clinic start generated code]*/ |
| 2480 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2481 | static PyObject * |
| 2482 | bytearray_fromhex_impl(PyObject*cls, PyObject *string) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2483 | /*[clinic end generated code: output=df3da60129b3700c input=907bbd2d34d9367a]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2484 | { |
Victor Stinner | 2bf8993 | 2015-10-14 11:25:33 +0200 | [diff] [blame] | 2485 | return _PyBytes_FromHex(string, 1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
Gregory P. Smith | 8cb6569 | 2015-04-25 23:22:26 +0000 | [diff] [blame] | 2488 | PyDoc_STRVAR(hex__doc__, |
| 2489 | "B.hex() -> string\n\ |
| 2490 | \n\ |
| 2491 | Create a string of hexadecimal numbers from a bytearray object.\n\ |
| 2492 | Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'."); |
| 2493 | |
| 2494 | static PyObject * |
| 2495 | bytearray_hex(PyBytesObject *self) |
| 2496 | { |
| 2497 | char* argbuf = PyByteArray_AS_STRING(self); |
| 2498 | Py_ssize_t arglen = PyByteArray_GET_SIZE(self); |
| 2499 | return _Py_strhex(argbuf, arglen); |
| 2500 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2501 | |
| 2502 | static PyObject * |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2503 | _common_reduce(PyByteArrayObject *self, int proto) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2504 | { |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2505 | PyObject *dict; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2506 | _Py_IDENTIFIER(__dict__); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2507 | char *buf; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2508 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2509 | dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2510 | if (dict == NULL) { |
| 2511 | PyErr_Clear(); |
| 2512 | dict = Py_None; |
| 2513 | Py_INCREF(dict); |
| 2514 | } |
| 2515 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2516 | buf = PyByteArray_AS_STRING(self); |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2517 | if (proto < 3) { |
| 2518 | /* use str based reduction for backwards compatibility with Python 2.x */ |
| 2519 | PyObject *latin1; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2520 | if (Py_SIZE(self)) |
| 2521 | latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL); |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2522 | else |
| 2523 | latin1 = PyUnicode_FromString(""); |
| 2524 | return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict); |
| 2525 | } |
| 2526 | else { |
| 2527 | /* use more efficient byte based reduction */ |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2528 | if (Py_SIZE(self)) { |
| 2529 | 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] | 2530 | } |
| 2531 | else { |
| 2532 | return Py_BuildValue("(O()N)", Py_TYPE(self), dict); |
| 2533 | } |
| 2534 | } |
| 2535 | } |
| 2536 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2537 | /*[clinic input] |
| 2538 | bytearray.__reduce__ as bytearray_reduce |
| 2539 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2540 | Return state information for pickling. |
| 2541 | [clinic start generated code]*/ |
| 2542 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2543 | static PyObject * |
| 2544 | bytearray_reduce_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 2545 | /*[clinic end generated code: output=52bf304086464cab input=44b5737ada62dd3f]*/ |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2546 | { |
| 2547 | return _common_reduce(self, 2); |
| 2548 | } |
| 2549 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2550 | /*[clinic input] |
| 2551 | bytearray.__reduce_ex__ as bytearray_reduce_ex |
| 2552 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2553 | proto: int = 0 |
| 2554 | / |
| 2555 | |
| 2556 | Return state information for pickling. |
| 2557 | [clinic start generated code]*/ |
| 2558 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2559 | static PyObject * |
| 2560 | bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 2561 | /*[clinic end generated code: output=52eac33377197520 input=f129bc1a1aa151ee]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2562 | { |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2563 | return _common_reduce(self, proto); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2564 | } |
| 2565 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2566 | /*[clinic input] |
| 2567 | bytearray.__sizeof__ as bytearray_sizeof |
| 2568 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2569 | Returns the size of the bytearray object in memory, in bytes. |
| 2570 | [clinic start generated code]*/ |
| 2571 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2572 | static PyObject * |
| 2573 | bytearray_sizeof_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 2574 | /*[clinic end generated code: output=738abdd17951c427 input=e27320fd98a4bc5a]*/ |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2575 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 2576 | Py_ssize_t res; |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2577 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 2578 | res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char); |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 2579 | return PyLong_FromSsize_t(res); |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2580 | } |
| 2581 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2582 | static PySequenceMethods bytearray_as_sequence = { |
| 2583 | (lenfunc)bytearray_length, /* sq_length */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2584 | (binaryfunc)PyByteArray_Concat, /* sq_concat */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2585 | (ssizeargfunc)bytearray_repeat, /* sq_repeat */ |
| 2586 | (ssizeargfunc)bytearray_getitem, /* sq_item */ |
| 2587 | 0, /* sq_slice */ |
| 2588 | (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */ |
| 2589 | 0, /* sq_ass_slice */ |
| 2590 | (objobjproc)bytearray_contains, /* sq_contains */ |
| 2591 | (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */ |
| 2592 | (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2593 | }; |
| 2594 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2595 | static PyMappingMethods bytearray_as_mapping = { |
| 2596 | (lenfunc)bytearray_length, |
| 2597 | (binaryfunc)bytearray_subscript, |
| 2598 | (objobjargproc)bytearray_ass_subscript, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2599 | }; |
| 2600 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2601 | static PyBufferProcs bytearray_as_buffer = { |
| 2602 | (getbufferproc)bytearray_getbuffer, |
| 2603 | (releasebufferproc)bytearray_releasebuffer, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2604 | }; |
| 2605 | |
| 2606 | static PyMethodDef |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2607 | bytearray_methods[] = { |
| 2608 | {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2609 | BYTEARRAY_REDUCE_METHODDEF |
| 2610 | BYTEARRAY_REDUCE_EX_METHODDEF |
| 2611 | BYTEARRAY_SIZEOF_METHODDEF |
| 2612 | BYTEARRAY_APPEND_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2613 | {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS, |
| 2614 | _Py_capitalize__doc__}, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame^] | 2615 | {"center", (PyCFunction)stringlib_center, METH_VARARGS, _Py_center__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2616 | BYTEARRAY_CLEAR_METHODDEF |
| 2617 | BYTEARRAY_COPY_METHODDEF |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame^] | 2618 | {"count", (PyCFunction)bytearray_count, METH_VARARGS, |
| 2619 | _Py_count__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2620 | BYTEARRAY_DECODE_METHODDEF |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame^] | 2621 | {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, |
| 2622 | _Py_endswith__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 2623 | {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame^] | 2624 | _Py_expandtabs__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2625 | BYTEARRAY_EXTEND_METHODDEF |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame^] | 2626 | {"find", (PyCFunction)bytearray_find, METH_VARARGS, |
| 2627 | _Py_find__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2628 | BYTEARRAY_FROMHEX_METHODDEF |
Gregory P. Smith | 8cb6569 | 2015-04-25 23:22:26 +0000 | [diff] [blame] | 2629 | {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__}, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame^] | 2630 | {"index", (PyCFunction)bytearray_index, METH_VARARGS, _Py_index__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2631 | BYTEARRAY_INSERT_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2632 | {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS, |
| 2633 | _Py_isalnum__doc__}, |
| 2634 | {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS, |
| 2635 | _Py_isalpha__doc__}, |
| 2636 | {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS, |
| 2637 | _Py_isdigit__doc__}, |
| 2638 | {"islower", (PyCFunction)stringlib_islower, METH_NOARGS, |
| 2639 | _Py_islower__doc__}, |
| 2640 | {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS, |
| 2641 | _Py_isspace__doc__}, |
| 2642 | {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS, |
| 2643 | _Py_istitle__doc__}, |
| 2644 | {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS, |
| 2645 | _Py_isupper__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2646 | BYTEARRAY_JOIN_METHODDEF |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame^] | 2647 | {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, _Py_ljust__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2648 | {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2649 | BYTEARRAY_LSTRIP_METHODDEF |
| 2650 | BYTEARRAY_MAKETRANS_METHODDEF |
| 2651 | BYTEARRAY_PARTITION_METHODDEF |
| 2652 | BYTEARRAY_POP_METHODDEF |
| 2653 | BYTEARRAY_REMOVE_METHODDEF |
| 2654 | BYTEARRAY_REPLACE_METHODDEF |
| 2655 | BYTEARRAY_REVERSE_METHODDEF |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame^] | 2656 | {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__}, |
| 2657 | {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__}, |
| 2658 | {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, _Py_rjust__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2659 | BYTEARRAY_RPARTITION_METHODDEF |
| 2660 | BYTEARRAY_RSPLIT_METHODDEF |
| 2661 | BYTEARRAY_RSTRIP_METHODDEF |
| 2662 | BYTEARRAY_SPLIT_METHODDEF |
| 2663 | BYTEARRAY_SPLITLINES_METHODDEF |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2664 | {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS , |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame^] | 2665 | _Py_startswith__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2666 | BYTEARRAY_STRIP_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2667 | {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS, |
| 2668 | _Py_swapcase__doc__}, |
| 2669 | {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2670 | BYTEARRAY_TRANSLATE_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2671 | {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__}, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame^] | 2672 | {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, _Py_zfill__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2673 | {NULL} |
| 2674 | }; |
| 2675 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 2676 | static PyObject * |
| 2677 | bytearray_mod(PyObject *v, PyObject *w) |
| 2678 | { |
| 2679 | if (!PyByteArray_Check(v)) |
| 2680 | Py_RETURN_NOTIMPLEMENTED; |
Berker Peksag | 43de36d | 2016-04-16 01:20:47 +0300 | [diff] [blame] | 2681 | 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] | 2682 | } |
| 2683 | |
| 2684 | static PyNumberMethods bytearray_as_number = { |
| 2685 | 0, /*nb_add*/ |
| 2686 | 0, /*nb_subtract*/ |
| 2687 | 0, /*nb_multiply*/ |
| 2688 | bytearray_mod, /*nb_remainder*/ |
| 2689 | }; |
| 2690 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2691 | PyDoc_STRVAR(bytearray_doc, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2692 | "bytearray(iterable_of_ints) -> bytearray\n\ |
| 2693 | bytearray(string, encoding[, errors]) -> bytearray\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 2694 | bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\ |
| 2695 | bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\ |
| 2696 | bytearray() -> empty bytes array\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2697 | \n\ |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 2698 | Construct a mutable bytearray object from:\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2699 | - an iterable yielding integers in range(256)\n\ |
| 2700 | - a text string encoded using the specified encoding\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 2701 | - a bytes or a buffer object\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2702 | - any object implementing the buffer API.\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 2703 | - an integer"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2704 | |
| 2705 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2706 | static PyObject *bytearray_iter(PyObject *seq); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2707 | |
| 2708 | PyTypeObject PyByteArray_Type = { |
| 2709 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2710 | "bytearray", |
| 2711 | sizeof(PyByteArrayObject), |
| 2712 | 0, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2713 | (destructor)bytearray_dealloc, /* tp_dealloc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2714 | 0, /* tp_print */ |
| 2715 | 0, /* tp_getattr */ |
| 2716 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 2717 | 0, /* tp_reserved */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2718 | (reprfunc)bytearray_repr, /* tp_repr */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 2719 | &bytearray_as_number, /* tp_as_number */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2720 | &bytearray_as_sequence, /* tp_as_sequence */ |
| 2721 | &bytearray_as_mapping, /* tp_as_mapping */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2722 | 0, /* tp_hash */ |
| 2723 | 0, /* tp_call */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2724 | bytearray_str, /* tp_str */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2725 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2726 | 0, /* tp_setattro */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2727 | &bytearray_as_buffer, /* tp_as_buffer */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2728 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2729 | bytearray_doc, /* tp_doc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2730 | 0, /* tp_traverse */ |
| 2731 | 0, /* tp_clear */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2732 | (richcmpfunc)bytearray_richcompare, /* tp_richcompare */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2733 | 0, /* tp_weaklistoffset */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2734 | bytearray_iter, /* tp_iter */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2735 | 0, /* tp_iternext */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2736 | bytearray_methods, /* tp_methods */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2737 | 0, /* tp_members */ |
| 2738 | 0, /* tp_getset */ |
| 2739 | 0, /* tp_base */ |
| 2740 | 0, /* tp_dict */ |
| 2741 | 0, /* tp_descr_get */ |
| 2742 | 0, /* tp_descr_set */ |
| 2743 | 0, /* tp_dictoffset */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2744 | (initproc)bytearray_init, /* tp_init */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2745 | PyType_GenericAlloc, /* tp_alloc */ |
| 2746 | PyType_GenericNew, /* tp_new */ |
| 2747 | PyObject_Del, /* tp_free */ |
| 2748 | }; |
| 2749 | |
| 2750 | /*********************** Bytes Iterator ****************************/ |
| 2751 | |
| 2752 | typedef struct { |
| 2753 | PyObject_HEAD |
| 2754 | Py_ssize_t it_index; |
| 2755 | PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 2756 | } bytesiterobject; |
| 2757 | |
| 2758 | static void |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2759 | bytearrayiter_dealloc(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2760 | { |
| 2761 | _PyObject_GC_UNTRACK(it); |
| 2762 | Py_XDECREF(it->it_seq); |
| 2763 | PyObject_GC_Del(it); |
| 2764 | } |
| 2765 | |
| 2766 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2767 | bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2768 | { |
| 2769 | Py_VISIT(it->it_seq); |
| 2770 | return 0; |
| 2771 | } |
| 2772 | |
| 2773 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2774 | bytearrayiter_next(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2775 | { |
| 2776 | PyByteArrayObject *seq; |
| 2777 | PyObject *item; |
| 2778 | |
| 2779 | assert(it != NULL); |
| 2780 | seq = it->it_seq; |
| 2781 | if (seq == NULL) |
| 2782 | return NULL; |
| 2783 | assert(PyByteArray_Check(seq)); |
| 2784 | |
| 2785 | if (it->it_index < PyByteArray_GET_SIZE(seq)) { |
| 2786 | item = PyLong_FromLong( |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2787 | (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2788 | if (item != NULL) |
| 2789 | ++it->it_index; |
| 2790 | return item; |
| 2791 | } |
| 2792 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2793 | it->it_seq = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 2794 | Py_DECREF(seq); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2795 | return NULL; |
| 2796 | } |
| 2797 | |
| 2798 | static PyObject * |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2799 | bytearrayiter_length_hint(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2800 | { |
| 2801 | Py_ssize_t len = 0; |
| 2802 | if (it->it_seq) |
| 2803 | len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index; |
| 2804 | return PyLong_FromSsize_t(len); |
| 2805 | } |
| 2806 | |
| 2807 | PyDoc_STRVAR(length_hint_doc, |
| 2808 | "Private method returning an estimate of len(list(it))."); |
| 2809 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2810 | static PyObject * |
| 2811 | bytearrayiter_reduce(bytesiterobject *it) |
| 2812 | { |
| 2813 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 2814 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2815 | it->it_seq, it->it_index); |
| 2816 | } else { |
| 2817 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 2818 | if (u == NULL) |
| 2819 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 2820 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2821 | } |
| 2822 | } |
| 2823 | |
| 2824 | static PyObject * |
| 2825 | bytearrayiter_setstate(bytesiterobject *it, PyObject *state) |
| 2826 | { |
| 2827 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 2828 | if (index == -1 && PyErr_Occurred()) |
| 2829 | return NULL; |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 2830 | if (it->it_seq != NULL) { |
| 2831 | if (index < 0) |
| 2832 | index = 0; |
| 2833 | else if (index > PyByteArray_GET_SIZE(it->it_seq)) |
| 2834 | index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */ |
| 2835 | it->it_index = index; |
| 2836 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2837 | Py_RETURN_NONE; |
| 2838 | } |
| 2839 | |
| 2840 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 2841 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2842 | static PyMethodDef bytearrayiter_methods[] = { |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2843 | {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2844 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2845 | {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2846 | bytearray_reduce__doc__}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2847 | {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O, |
| 2848 | setstate_doc}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2849 | {NULL, NULL} /* sentinel */ |
| 2850 | }; |
| 2851 | |
| 2852 | PyTypeObject PyByteArrayIter_Type = { |
| 2853 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2854 | "bytearray_iterator", /* tp_name */ |
| 2855 | sizeof(bytesiterobject), /* tp_basicsize */ |
| 2856 | 0, /* tp_itemsize */ |
| 2857 | /* methods */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2858 | (destructor)bytearrayiter_dealloc, /* tp_dealloc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2859 | 0, /* tp_print */ |
| 2860 | 0, /* tp_getattr */ |
| 2861 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 2862 | 0, /* tp_reserved */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2863 | 0, /* tp_repr */ |
| 2864 | 0, /* tp_as_number */ |
| 2865 | 0, /* tp_as_sequence */ |
| 2866 | 0, /* tp_as_mapping */ |
| 2867 | 0, /* tp_hash */ |
| 2868 | 0, /* tp_call */ |
| 2869 | 0, /* tp_str */ |
| 2870 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2871 | 0, /* tp_setattro */ |
| 2872 | 0, /* tp_as_buffer */ |
| 2873 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 2874 | 0, /* tp_doc */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2875 | (traverseproc)bytearrayiter_traverse, /* tp_traverse */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2876 | 0, /* tp_clear */ |
| 2877 | 0, /* tp_richcompare */ |
| 2878 | 0, /* tp_weaklistoffset */ |
| 2879 | PyObject_SelfIter, /* tp_iter */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2880 | (iternextfunc)bytearrayiter_next, /* tp_iternext */ |
| 2881 | bytearrayiter_methods, /* tp_methods */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2882 | 0, |
| 2883 | }; |
| 2884 | |
| 2885 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2886 | bytearray_iter(PyObject *seq) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2887 | { |
| 2888 | bytesiterobject *it; |
| 2889 | |
| 2890 | if (!PyByteArray_Check(seq)) { |
| 2891 | PyErr_BadInternalCall(); |
| 2892 | return NULL; |
| 2893 | } |
| 2894 | it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type); |
| 2895 | if (it == NULL) |
| 2896 | return NULL; |
| 2897 | it->it_index = 0; |
| 2898 | Py_INCREF(seq); |
| 2899 | it->it_seq = (PyByteArrayObject *)seq; |
| 2900 | _PyObject_GC_TRACK(it); |
| 2901 | return (PyObject *)it; |
| 2902 | } |