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