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" |
Victor Stinner | bcda8f1 | 2018-11-21 22:27:47 +0100 | [diff] [blame] | 5 | #include "pycore_object.h" |
Victor Stinner | 621cebe | 2018-11-12 16:53:38 +0100 | [diff] [blame] | 6 | #include "pycore_pymem.h" |
| 7 | #include "pycore_pystate.h" |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 8 | #include "structmember.h" |
| 9 | #include "bytes_methods.h" |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 10 | #include "bytesobject.h" |
Gregory P. Smith | 8cb6569 | 2015-04-25 23:22:26 +0000 | [diff] [blame] | 11 | #include "pystrhex.h" |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 12 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 13 | /*[clinic input] |
| 14 | class bytearray "PyByteArrayObject *" "&PyByteArray_Type" |
| 15 | [clinic start generated code]*/ |
| 16 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/ |
| 17 | |
Antoine Pitrou | fc8d6f4 | 2010-01-17 12:38:54 +0000 | [diff] [blame] | 18 | char _PyByteArray_empty_string[] = ""; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 19 | |
| 20 | void |
| 21 | PyByteArray_Fini(void) |
| 22 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | int |
| 26 | PyByteArray_Init(void) |
| 27 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 28 | return 1; |
| 29 | } |
| 30 | |
| 31 | /* end nullbytes support */ |
| 32 | |
| 33 | /* Helpers */ |
| 34 | |
| 35 | static int |
| 36 | _getbytevalue(PyObject* arg, int *value) |
| 37 | { |
| 38 | long face_value; |
| 39 | |
| 40 | if (PyLong_Check(arg)) { |
| 41 | face_value = PyLong_AsLong(arg); |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 42 | } else { |
| 43 | PyObject *index = PyNumber_Index(arg); |
| 44 | if (index == NULL) { |
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, |
Serhiy Storchaka | 2c2044e | 2018-10-21 15:29:12 +0300 | [diff] [blame] | 787 | encoding != NULL ? |
| 788 | "encoding without a string argument" : |
| 789 | "errors without a string argument"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 790 | return -1; |
| 791 | } |
| 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | if (PyUnicode_Check(arg)) { |
| 796 | /* Encode via the codec registry */ |
| 797 | PyObject *encoded, *new; |
| 798 | if (encoding == NULL) { |
| 799 | PyErr_SetString(PyExc_TypeError, |
| 800 | "string argument without an encoding"); |
| 801 | return -1; |
| 802 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 803 | encoded = PyUnicode_AsEncodedString(arg, encoding, errors); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 804 | if (encoded == NULL) |
| 805 | return -1; |
| 806 | assert(PyBytes_Check(encoded)); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 807 | new = bytearray_iconcat(self, encoded); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 808 | Py_DECREF(encoded); |
| 809 | if (new == NULL) |
| 810 | return -1; |
| 811 | Py_DECREF(new); |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | /* If it's not unicode, there can't be encoding or errors */ |
| 816 | if (encoding != NULL || errors != NULL) { |
| 817 | PyErr_SetString(PyExc_TypeError, |
Serhiy Storchaka | 2c2044e | 2018-10-21 15:29:12 +0300 | [diff] [blame] | 818 | encoding != NULL ? |
| 819 | "encoding without a string argument" : |
| 820 | "errors without a string argument"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 821 | return -1; |
| 822 | } |
| 823 | |
| 824 | /* Is it an int? */ |
Serhiy Storchaka | eb24988 | 2016-08-15 09:46:07 +0300 | [diff] [blame] | 825 | if (PyIndex_Check(arg)) { |
| 826 | count = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
| 827 | if (count == -1 && PyErr_Occurred()) { |
Serhiy Storchaka | e890421 | 2018-10-15 00:02:57 +0300 | [diff] [blame] | 828 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 829 | return -1; |
INADA Naoki | a634e23 | 2017-01-06 17:32:01 +0900 | [diff] [blame] | 830 | PyErr_Clear(); /* fall through */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 831 | } |
INADA Naoki | a634e23 | 2017-01-06 17:32:01 +0900 | [diff] [blame] | 832 | else { |
| 833 | if (count < 0) { |
| 834 | PyErr_SetString(PyExc_ValueError, "negative count"); |
| 835 | return -1; |
| 836 | } |
| 837 | if (count > 0) { |
| 838 | if (PyByteArray_Resize((PyObject *)self, count)) |
| 839 | return -1; |
| 840 | memset(PyByteArray_AS_STRING(self), 0, count); |
| 841 | } |
| 842 | return 0; |
| 843 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | /* Use the buffer API */ |
| 847 | if (PyObject_CheckBuffer(arg)) { |
| 848 | Py_ssize_t size; |
| 849 | Py_buffer view; |
| 850 | if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0) |
| 851 | return -1; |
| 852 | size = view.len; |
| 853 | if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 854 | if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self), |
| 855 | &view, size, 'C') < 0) |
Stefan Krah | 7d12d9d | 2012-07-28 12:25:55 +0200 | [diff] [blame] | 856 | goto fail; |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 857 | PyBuffer_Release(&view); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 858 | return 0; |
| 859 | fail: |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 860 | PyBuffer_Release(&view); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 861 | return -1; |
| 862 | } |
| 863 | |
| 864 | /* XXX Optimize this if the arguments is a list, tuple */ |
| 865 | |
| 866 | /* Get the iterator */ |
| 867 | it = PyObject_GetIter(arg); |
Serhiy Storchaka | 2c2044e | 2018-10-21 15:29:12 +0300 | [diff] [blame] | 868 | if (it == NULL) { |
| 869 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 870 | PyErr_Format(PyExc_TypeError, |
| 871 | "cannot convert '%.200s' object to bytearray", |
| 872 | arg->ob_type->tp_name); |
| 873 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 874 | return -1; |
Serhiy Storchaka | 2c2044e | 2018-10-21 15:29:12 +0300 | [diff] [blame] | 875 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 876 | iternext = *Py_TYPE(it)->tp_iternext; |
| 877 | |
| 878 | /* Run the iterator to exhaustion */ |
| 879 | for (;;) { |
| 880 | PyObject *item; |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 881 | int rc, value; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 882 | |
| 883 | /* Get the next item */ |
| 884 | item = iternext(it); |
| 885 | if (item == NULL) { |
| 886 | if (PyErr_Occurred()) { |
| 887 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 888 | goto error; |
| 889 | PyErr_Clear(); |
| 890 | } |
| 891 | break; |
| 892 | } |
| 893 | |
| 894 | /* Interpret it as an int (__index__) */ |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 895 | rc = _getbytevalue(item, &value); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 896 | Py_DECREF(item); |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 897 | if (!rc) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 898 | goto error; |
| 899 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 900 | /* Append the byte */ |
Serhiy Storchaka | 7b6e3b9 | 2015-06-29 21:14:06 +0300 | [diff] [blame] | 901 | if (Py_SIZE(self) + 1 < self->ob_alloc) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 902 | Py_SIZE(self)++; |
Serhiy Storchaka | 7b6e3b9 | 2015-06-29 21:14:06 +0300 | [diff] [blame] | 903 | PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0'; |
| 904 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 905 | else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0) |
| 906 | goto error; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 907 | PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | /* Clean up and return success */ |
| 911 | Py_DECREF(it); |
| 912 | return 0; |
| 913 | |
| 914 | error: |
| 915 | /* Error handling when it != NULL */ |
| 916 | Py_DECREF(it); |
| 917 | return -1; |
| 918 | } |
| 919 | |
| 920 | /* Mostly copied from string_repr, but without the |
| 921 | "smart quote" functionality. */ |
| 922 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 923 | bytearray_repr(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 924 | { |
Serhiy Storchaka | b3a7796 | 2017-09-21 14:24:13 +0300 | [diff] [blame] | 925 | const char *className = _PyType_Name(Py_TYPE(self)); |
| 926 | const char *quote_prefix = "(b"; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 927 | const char *quote_postfix = ")"; |
| 928 | Py_ssize_t length = Py_SIZE(self); |
Serhiy Storchaka | b3a7796 | 2017-09-21 14:24:13 +0300 | [diff] [blame] | 929 | /* 6 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */ |
| 930 | Py_ssize_t newsize; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 931 | PyObject *v; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 932 | Py_ssize_t i; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 933 | char *bytes; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 934 | char c; |
| 935 | char *p; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 936 | int quote; |
| 937 | char *test, *start; |
| 938 | char *buffer; |
| 939 | |
Serhiy Storchaka | b3a7796 | 2017-09-21 14:24:13 +0300 | [diff] [blame] | 940 | newsize = strlen(className); |
| 941 | if (length > (PY_SSIZE_T_MAX - 6 - newsize) / 4) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 942 | PyErr_SetString(PyExc_OverflowError, |
| 943 | "bytearray object is too large to make repr"); |
| 944 | return NULL; |
| 945 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 946 | |
Serhiy Storchaka | b3a7796 | 2017-09-21 14:24:13 +0300 | [diff] [blame] | 947 | newsize += 6 + length * 4; |
Antoine Pitrou | 39aba4f | 2011-11-12 21:15:28 +0100 | [diff] [blame] | 948 | buffer = PyObject_Malloc(newsize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 949 | if (buffer == NULL) { |
| 950 | PyErr_NoMemory(); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 951 | return NULL; |
| 952 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 953 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 954 | /* Figure out which quote to use; single is preferred */ |
| 955 | quote = '\''; |
| 956 | start = PyByteArray_AS_STRING(self); |
| 957 | for (test = start; test < start+length; ++test) { |
| 958 | if (*test == '"') { |
| 959 | quote = '\''; /* back to single */ |
| 960 | break; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 961 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 962 | else if (*test == '\'') |
| 963 | quote = '"'; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 964 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 965 | |
| 966 | p = buffer; |
Serhiy Storchaka | b3a7796 | 2017-09-21 14:24:13 +0300 | [diff] [blame] | 967 | while (*className) |
| 968 | *p++ = *className++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 969 | while (*quote_prefix) |
| 970 | *p++ = *quote_prefix++; |
| 971 | *p++ = quote; |
| 972 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 973 | bytes = PyByteArray_AS_STRING(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 974 | for (i = 0; i < length; i++) { |
| 975 | /* There's at least enough room for a hex escape |
| 976 | and a closing quote. */ |
| 977 | assert(newsize - (p - buffer) >= 5); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 978 | c = bytes[i]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 979 | if (c == '\'' || c == '\\') |
| 980 | *p++ = '\\', *p++ = c; |
| 981 | else if (c == '\t') |
| 982 | *p++ = '\\', *p++ = 't'; |
| 983 | else if (c == '\n') |
| 984 | *p++ = '\\', *p++ = 'n'; |
| 985 | else if (c == '\r') |
| 986 | *p++ = '\\', *p++ = 'r'; |
| 987 | else if (c == 0) |
| 988 | *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0'; |
| 989 | else if (c < ' ' || c >= 0x7f) { |
| 990 | *p++ = '\\'; |
| 991 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 992 | *p++ = Py_hexdigits[(c & 0xf0) >> 4]; |
| 993 | *p++ = Py_hexdigits[c & 0xf]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 994 | } |
| 995 | else |
| 996 | *p++ = c; |
| 997 | } |
| 998 | assert(newsize - (p - buffer) >= 1); |
| 999 | *p++ = quote; |
| 1000 | while (*quote_postfix) { |
| 1001 | *p++ = *quote_postfix++; |
| 1002 | } |
| 1003 | |
Serhiy Storchaka | b3a7796 | 2017-09-21 14:24:13 +0300 | [diff] [blame] | 1004 | v = PyUnicode_FromStringAndSize(buffer, p - buffer); |
Antoine Pitrou | 39aba4f | 2011-11-12 21:15:28 +0100 | [diff] [blame] | 1005 | PyObject_Free(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1006 | return v; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1010 | bytearray_str(PyObject *op) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1011 | { |
Victor Stinner | 53b7d4e | 2018-07-25 01:37:05 +0200 | [diff] [blame] | 1012 | if (Py_BytesWarningFlag) { |
| 1013 | if (PyErr_WarnEx(PyExc_BytesWarning, |
| 1014 | "str() on a bytearray instance", 1)) { |
| 1015 | return NULL; |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 1016 | } |
Victor Stinner | 53b7d4e | 2018-07-25 01:37:05 +0200 | [diff] [blame] | 1017 | } |
| 1018 | return bytearray_repr((PyByteArrayObject*)op); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1022 | bytearray_richcompare(PyObject *self, PyObject *other, int op) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1023 | { |
| 1024 | Py_ssize_t self_size, other_size; |
| 1025 | Py_buffer self_bytes, other_bytes; |
Serhiy Storchaka | fa494fd | 2015-05-30 17:45:22 +0300 | [diff] [blame] | 1026 | int cmp, rc; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1027 | |
| 1028 | /* Bytes can be compared to anything that supports the (binary) |
| 1029 | buffer API. Except that a comparison with Unicode is always an |
| 1030 | error, even if the comparison is for equality. */ |
Serhiy Storchaka | fa494fd | 2015-05-30 17:45:22 +0300 | [diff] [blame] | 1031 | rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type); |
| 1032 | if (!rc) |
| 1033 | rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type); |
| 1034 | if (rc < 0) |
| 1035 | return NULL; |
| 1036 | if (rc) { |
Barry Warsaw | 9e9dcd6 | 2008-10-17 01:50:37 +0000 | [diff] [blame] | 1037 | if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1038 | if (PyErr_WarnEx(PyExc_BytesWarning, |
Georg Brandl | e5d68ac | 2008-06-04 11:30:26 +0000 | [diff] [blame] | 1039 | "Comparison between bytearray and string", 1)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1040 | return NULL; |
| 1041 | } |
| 1042 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1043 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1046 | if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1047 | PyErr_Clear(); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1048 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1049 | } |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1050 | self_size = self_bytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1051 | |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1052 | if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1053 | PyErr_Clear(); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1054 | PyBuffer_Release(&self_bytes); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1055 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1056 | } |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1057 | other_size = other_bytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1058 | |
| 1059 | if (self_size != other_size && (op == Py_EQ || op == Py_NE)) { |
| 1060 | /* Shortcut: if the lengths differ, the objects differ */ |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 1061 | PyBuffer_Release(&self_bytes); |
| 1062 | PyBuffer_Release(&other_bytes); |
| 1063 | return PyBool_FromLong((op == Py_NE)); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1064 | } |
| 1065 | else { |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 1066 | cmp = memcmp(self_bytes.buf, other_bytes.buf, |
| 1067 | Py_MIN(self_size, other_size)); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1068 | /* In ISO C, memcmp() guarantees to use unsigned bytes! */ |
| 1069 | |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 1070 | PyBuffer_Release(&self_bytes); |
| 1071 | PyBuffer_Release(&other_bytes); |
| 1072 | |
| 1073 | if (cmp != 0) { |
| 1074 | Py_RETURN_RICHCOMPARE(cmp, 0, op); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 1077 | Py_RETURN_RICHCOMPARE(self_size, other_size, op); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | static void |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1083 | bytearray_dealloc(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1084 | { |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 1085 | if (self->ob_exports > 0) { |
| 1086 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1087 | "deallocated bytearray object has exported buffers"); |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 1088 | PyErr_Print(); |
| 1089 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1090 | if (self->ob_bytes != 0) { |
Antoine Pitrou | 39aba4f | 2011-11-12 21:15:28 +0100 | [diff] [blame] | 1091 | PyObject_Free(self->ob_bytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1092 | } |
| 1093 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 1094 | } |
| 1095 | |
| 1096 | |
| 1097 | /* -------------------------------------------------------------------- */ |
| 1098 | /* Methods */ |
| 1099 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1100 | #define FASTSEARCH fastsearch |
| 1101 | #define STRINGLIB(F) stringlib_##F |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1102 | #define STRINGLIB_CHAR char |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 1103 | #define STRINGLIB_SIZEOF_CHAR 1 |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1104 | #define STRINGLIB_LEN PyByteArray_GET_SIZE |
| 1105 | #define STRINGLIB_STR PyByteArray_AS_STRING |
| 1106 | #define STRINGLIB_NEW PyByteArray_FromStringAndSize |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1107 | #define STRINGLIB_ISSPACE Py_ISSPACE |
| 1108 | #define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r')) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1109 | #define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact |
| 1110 | #define STRINGLIB_MUTABLE 1 |
| 1111 | |
| 1112 | #include "stringlib/fastsearch.h" |
| 1113 | #include "stringlib/count.h" |
| 1114 | #include "stringlib/find.h" |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 1115 | #include "stringlib/join.h" |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1116 | #include "stringlib/partition.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1117 | #include "stringlib/split.h" |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1118 | #include "stringlib/ctype.h" |
| 1119 | #include "stringlib/transmogrify.h" |
| 1120 | |
| 1121 | |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 1122 | static PyObject * |
| 1123 | bytearray_find(PyByteArrayObject *self, PyObject *args) |
| 1124 | { |
| 1125 | return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
| 1126 | } |
| 1127 | |
| 1128 | static PyObject * |
| 1129 | bytearray_count(PyByteArrayObject *self, PyObject *args) |
| 1130 | { |
| 1131 | return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
| 1132 | } |
| 1133 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1134 | /*[clinic input] |
| 1135 | bytearray.clear |
| 1136 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1137 | Remove all items from the bytearray. |
| 1138 | [clinic start generated code]*/ |
| 1139 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1140 | static PyObject * |
| 1141 | bytearray_clear_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1142 | /*[clinic end generated code: output=85c2fe6aede0956c input=ed6edae9de447ac4]*/ |
Eli Bendersky | 4db28d3 | 2011-03-03 18:21:02 +0000 | [diff] [blame] | 1143 | { |
| 1144 | if (PyByteArray_Resize((PyObject *)self, 0) < 0) |
| 1145 | return NULL; |
| 1146 | Py_RETURN_NONE; |
| 1147 | } |
| 1148 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1149 | /*[clinic input] |
| 1150 | bytearray.copy |
| 1151 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1152 | Return a copy of B. |
| 1153 | [clinic start generated code]*/ |
| 1154 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1155 | static PyObject * |
| 1156 | bytearray_copy_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1157 | /*[clinic end generated code: output=68cfbcfed484c132 input=6597b0c01bccaa9e]*/ |
Eli Bendersky | 4db28d3 | 2011-03-03 18:21:02 +0000 | [diff] [blame] | 1158 | { |
| 1159 | return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self), |
| 1160 | PyByteArray_GET_SIZE(self)); |
| 1161 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1162 | |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 1163 | static PyObject * |
| 1164 | bytearray_index(PyByteArrayObject *self, PyObject *args) |
| 1165 | { |
| 1166 | return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
| 1167 | } |
| 1168 | |
| 1169 | static PyObject * |
| 1170 | bytearray_rfind(PyByteArrayObject *self, PyObject *args) |
| 1171 | { |
| 1172 | return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
| 1173 | } |
| 1174 | |
| 1175 | static PyObject * |
| 1176 | bytearray_rindex(PyByteArrayObject *self, PyObject *args) |
| 1177 | { |
| 1178 | return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
| 1179 | } |
| 1180 | |
| 1181 | static int |
| 1182 | bytearray_contains(PyObject *self, PyObject *arg) |
| 1183 | { |
| 1184 | return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg); |
| 1185 | } |
| 1186 | |
| 1187 | static PyObject * |
| 1188 | bytearray_startswith(PyByteArrayObject *self, PyObject *args) |
| 1189 | { |
| 1190 | return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
| 1191 | } |
| 1192 | |
| 1193 | static PyObject * |
| 1194 | bytearray_endswith(PyByteArrayObject *self, PyObject *args) |
| 1195 | { |
| 1196 | return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
| 1197 | } |
| 1198 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1199 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1200 | /*[clinic input] |
| 1201 | bytearray.translate |
| 1202 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1203 | table: object |
| 1204 | 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] | 1205 | / |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 1206 | delete as deletechars: object(c_default="NULL") = b'' |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1207 | |
| 1208 | Return a copy with each character mapped by the given translation table. |
| 1209 | |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 1210 | All characters occurring in the optional argument delete are removed. |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1211 | The remaining characters are mapped through the given translation table. |
| 1212 | [clinic start generated code]*/ |
| 1213 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1214 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1215 | bytearray_translate_impl(PyByteArrayObject *self, PyObject *table, |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 1216 | PyObject *deletechars) |
| 1217 | /*[clinic end generated code: output=b6a8f01c2a74e446 input=cfff956d4d127a9b]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1218 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1219 | char *input, *output; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1220 | const char *table_chars; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1221 | Py_ssize_t i, c; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1222 | PyObject *input_obj = (PyObject*)self; |
| 1223 | const char *output_start; |
| 1224 | Py_ssize_t inlen; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1225 | PyObject *result = NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1226 | int trans_table[256]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1227 | Py_buffer vtable, vdel; |
| 1228 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1229 | if (table == Py_None) { |
| 1230 | table_chars = NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1231 | table = NULL; |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1232 | } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1233 | return NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1234 | } else { |
| 1235 | if (vtable.len != 256) { |
| 1236 | PyErr_SetString(PyExc_ValueError, |
| 1237 | "translation table must be 256 characters long"); |
Georg Brandl | 953152f | 2009-07-22 12:03:59 +0000 | [diff] [blame] | 1238 | PyBuffer_Release(&vtable); |
| 1239 | return NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1240 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1241 | table_chars = (const char*)vtable.buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1244 | if (deletechars != NULL) { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1245 | if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1246 | if (table != NULL) |
Georg Brandl | 953152f | 2009-07-22 12:03:59 +0000 | [diff] [blame] | 1247 | PyBuffer_Release(&vtable); |
| 1248 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1249 | } |
| 1250 | } |
| 1251 | else { |
| 1252 | vdel.buf = NULL; |
| 1253 | vdel.len = 0; |
| 1254 | } |
| 1255 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1256 | inlen = PyByteArray_GET_SIZE(input_obj); |
| 1257 | result = PyByteArray_FromStringAndSize((char *)NULL, inlen); |
| 1258 | if (result == NULL) |
| 1259 | goto done; |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 1260 | output_start = output = PyByteArray_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1261 | input = PyByteArray_AS_STRING(input_obj); |
| 1262 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1263 | if (vdel.len == 0 && table_chars != NULL) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1264 | /* If no deletions are required, use faster code */ |
| 1265 | for (i = inlen; --i >= 0; ) { |
| 1266 | c = Py_CHARMASK(*input++); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1267 | *output++ = table_chars[c]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1268 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1269 | goto done; |
| 1270 | } |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1271 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1272 | if (table_chars == NULL) { |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1273 | for (i = 0; i < 256; i++) |
| 1274 | trans_table[i] = Py_CHARMASK(i); |
| 1275 | } else { |
| 1276 | for (i = 0; i < 256; i++) |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1277 | trans_table[i] = Py_CHARMASK(table_chars[i]); |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1278 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1279 | |
| 1280 | for (i = 0; i < vdel.len; i++) |
| 1281 | trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1; |
| 1282 | |
| 1283 | for (i = inlen; --i >= 0; ) { |
| 1284 | c = Py_CHARMASK(*input++); |
| 1285 | if (trans_table[c] != -1) |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 1286 | *output++ = (char)trans_table[c]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1287 | } |
| 1288 | /* Fix the size of the resulting string */ |
| 1289 | if (inlen > 0) |
Christian Heimes | c731bbe | 2013-07-21 02:04:35 +0200 | [diff] [blame] | 1290 | if (PyByteArray_Resize(result, output - output_start) < 0) { |
| 1291 | Py_CLEAR(result); |
| 1292 | goto done; |
| 1293 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1294 | |
| 1295 | done: |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1296 | if (table != NULL) |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1297 | PyBuffer_Release(&vtable); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1298 | if (deletechars != NULL) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1299 | PyBuffer_Release(&vdel); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1300 | return result; |
| 1301 | } |
| 1302 | |
| 1303 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1304 | /*[clinic input] |
| 1305 | |
| 1306 | @staticmethod |
| 1307 | bytearray.maketrans |
| 1308 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1309 | frm: Py_buffer |
| 1310 | to: Py_buffer |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1311 | / |
| 1312 | |
| 1313 | Return a translation table useable for the bytes or bytearray translate method. |
| 1314 | |
| 1315 | The returned table will be one where each byte in frm is mapped to the byte at |
| 1316 | the same position in to. |
| 1317 | |
| 1318 | The bytes objects frm and to must be of the same length. |
| 1319 | [clinic start generated code]*/ |
| 1320 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1321 | static PyObject * |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1322 | bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1323 | /*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1324 | { |
| 1325 | return _Py_bytes_maketrans(frm, to); |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1329 | /*[clinic input] |
| 1330 | bytearray.replace |
| 1331 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1332 | old: Py_buffer |
| 1333 | new: Py_buffer |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1334 | count: Py_ssize_t = -1 |
| 1335 | Maximum number of occurrences to replace. |
| 1336 | -1 (the default value) means replace all occurrences. |
| 1337 | / |
| 1338 | |
| 1339 | Return a copy with all occurrences of substring old replaced by new. |
| 1340 | |
| 1341 | If the optional argument count is given, only the first count occurrences are |
| 1342 | replaced. |
| 1343 | [clinic start generated code]*/ |
| 1344 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1345 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1346 | bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old, |
| 1347 | Py_buffer *new, Py_ssize_t count) |
| 1348 | /*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1349 | { |
Serhiy Storchaka | fb81d3c | 2016-05-05 09:26:07 +0300 | [diff] [blame] | 1350 | return stringlib_replace((PyObject *)self, |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 1351 | (const char *)old->buf, old->len, |
| 1352 | (const char *)new->buf, new->len, count); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1355 | /*[clinic input] |
| 1356 | bytearray.split |
| 1357 | |
| 1358 | sep: object = None |
| 1359 | The delimiter according which to split the bytearray. |
| 1360 | None (the default value) means split on ASCII whitespace characters |
| 1361 | (space, tab, return, newline, formfeed, vertical tab). |
| 1362 | maxsplit: Py_ssize_t = -1 |
| 1363 | Maximum number of splits to do. |
| 1364 | -1 (the default value) means no limit. |
| 1365 | |
| 1366 | Return a list of the sections in the bytearray, using sep as the delimiter. |
| 1367 | [clinic start generated code]*/ |
| 1368 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1369 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1370 | bytearray_split_impl(PyByteArrayObject *self, PyObject *sep, |
| 1371 | Py_ssize_t maxsplit) |
| 1372 | /*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1373 | { |
| 1374 | Py_ssize_t len = PyByteArray_GET_SIZE(self), n; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1375 | const char *s = PyByteArray_AS_STRING(self), *sub; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1376 | PyObject *list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1377 | Py_buffer vsub; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1378 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1379 | if (maxsplit < 0) |
| 1380 | maxsplit = PY_SSIZE_T_MAX; |
| 1381 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1382 | if (sep == Py_None) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1383 | return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1384 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1385 | if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1386 | return NULL; |
| 1387 | sub = vsub.buf; |
| 1388 | n = vsub.len; |
| 1389 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1390 | list = stringlib_split( |
| 1391 | (PyObject*) self, s, len, sub, n, maxsplit |
| 1392 | ); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1393 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1394 | return list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1397 | /*[clinic input] |
| 1398 | bytearray.partition |
| 1399 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1400 | sep: object |
| 1401 | / |
| 1402 | |
| 1403 | Partition the bytearray into three parts using the given separator. |
| 1404 | |
| 1405 | This will search for the separator sep in the bytearray. If the separator is |
| 1406 | found, returns a 3-tuple containing the part before the separator, the |
Serhiy Storchaka | a231428 | 2017-10-29 02:11:54 +0300 | [diff] [blame] | 1407 | 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] | 1408 | |
Serhiy Storchaka | a231428 | 2017-10-29 02:11:54 +0300 | [diff] [blame] | 1409 | If the separator is not found, returns a 3-tuple containing the copy of the |
| 1410 | original bytearray object and two empty bytearray objects. |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1411 | [clinic start generated code]*/ |
| 1412 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1413 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1414 | bytearray_partition(PyByteArrayObject *self, PyObject *sep) |
Serhiy Storchaka | a231428 | 2017-10-29 02:11:54 +0300 | [diff] [blame] | 1415 | /*[clinic end generated code: output=45d2525ddd35f957 input=8f644749ee4fc83a]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1416 | { |
| 1417 | PyObject *bytesep, *result; |
| 1418 | |
Serhiy Storchaka | a231428 | 2017-10-29 02:11:54 +0300 | [diff] [blame] | 1419 | bytesep = _PyByteArray_FromBufferObject(sep); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1420 | if (! bytesep) |
| 1421 | return NULL; |
| 1422 | |
| 1423 | result = stringlib_partition( |
| 1424 | (PyObject*) self, |
| 1425 | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
| 1426 | bytesep, |
| 1427 | PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) |
| 1428 | ); |
| 1429 | |
| 1430 | Py_DECREF(bytesep); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1431 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1434 | /*[clinic input] |
| 1435 | bytearray.rpartition |
| 1436 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1437 | sep: object |
| 1438 | / |
| 1439 | |
Serhiy Storchaka | a231428 | 2017-10-29 02:11:54 +0300 | [diff] [blame] | 1440 | Partition the bytearray into three parts using the given separator. |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1441 | |
Serhiy Storchaka | a231428 | 2017-10-29 02:11:54 +0300 | [diff] [blame] | 1442 | 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] | 1443 | 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] | 1444 | separator, the separator itself, and the part after it as new bytearray |
| 1445 | objects. |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1446 | |
| 1447 | 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] | 1448 | objects and the copy of the original bytearray object. |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1449 | [clinic start generated code]*/ |
| 1450 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1451 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1452 | bytearray_rpartition(PyByteArrayObject *self, PyObject *sep) |
Serhiy Storchaka | a231428 | 2017-10-29 02:11:54 +0300 | [diff] [blame] | 1453 | /*[clinic end generated code: output=440de3c9426115e8 input=7e3df3e6cb8fa0ac]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1454 | { |
| 1455 | PyObject *bytesep, *result; |
| 1456 | |
Serhiy Storchaka | a231428 | 2017-10-29 02:11:54 +0300 | [diff] [blame] | 1457 | bytesep = _PyByteArray_FromBufferObject(sep); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1458 | if (! bytesep) |
| 1459 | return NULL; |
| 1460 | |
| 1461 | result = stringlib_rpartition( |
| 1462 | (PyObject*) self, |
| 1463 | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
| 1464 | bytesep, |
| 1465 | PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) |
| 1466 | ); |
| 1467 | |
| 1468 | Py_DECREF(bytesep); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1469 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1472 | /*[clinic input] |
| 1473 | bytearray.rsplit = bytearray.split |
| 1474 | |
| 1475 | Return a list of the sections in the bytearray, using sep as the delimiter. |
| 1476 | |
| 1477 | Splitting is done starting at the end of the bytearray and working to the front. |
| 1478 | [clinic start generated code]*/ |
| 1479 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1480 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1481 | bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep, |
| 1482 | Py_ssize_t maxsplit) |
| 1483 | /*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1484 | { |
| 1485 | Py_ssize_t len = PyByteArray_GET_SIZE(self), n; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1486 | const char *s = PyByteArray_AS_STRING(self), *sub; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1487 | PyObject *list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1488 | Py_buffer vsub; |
| 1489 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1490 | if (maxsplit < 0) |
| 1491 | maxsplit = PY_SSIZE_T_MAX; |
| 1492 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1493 | if (sep == Py_None) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1494 | return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1495 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1496 | if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1497 | return NULL; |
| 1498 | sub = vsub.buf; |
| 1499 | n = vsub.len; |
| 1500 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1501 | list = stringlib_rsplit( |
| 1502 | (PyObject*) self, s, len, sub, n, maxsplit |
| 1503 | ); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1504 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1505 | return list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1508 | /*[clinic input] |
| 1509 | bytearray.reverse |
| 1510 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1511 | Reverse the order of the values in B in place. |
| 1512 | [clinic start generated code]*/ |
| 1513 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1514 | static PyObject * |
| 1515 | bytearray_reverse_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1516 | /*[clinic end generated code: output=9f7616f29ab309d3 input=543356319fc78557]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1517 | { |
| 1518 | char swap, *head, *tail; |
| 1519 | Py_ssize_t i, j, n = Py_SIZE(self); |
| 1520 | |
| 1521 | j = n / 2; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1522 | head = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1523 | tail = head + n - 1; |
| 1524 | for (i = 0; i < j; i++) { |
| 1525 | swap = *head; |
| 1526 | *head++ = *tail; |
| 1527 | *tail-- = swap; |
| 1528 | } |
| 1529 | |
| 1530 | Py_RETURN_NONE; |
| 1531 | } |
| 1532 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1533 | |
| 1534 | /*[python input] |
| 1535 | class bytesvalue_converter(CConverter): |
| 1536 | type = 'int' |
| 1537 | converter = '_getbytevalue' |
| 1538 | [python start generated code]*/ |
| 1539 | /*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/ |
| 1540 | |
| 1541 | |
| 1542 | /*[clinic input] |
| 1543 | bytearray.insert |
| 1544 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1545 | index: Py_ssize_t |
| 1546 | The index where the value is to be inserted. |
| 1547 | item: bytesvalue |
| 1548 | The item to be inserted. |
| 1549 | / |
| 1550 | |
| 1551 | Insert a single item into the bytearray before the given index. |
| 1552 | [clinic start generated code]*/ |
| 1553 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1554 | static PyObject * |
| 1555 | bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1556 | /*[clinic end generated code: output=76c775a70e7b07b7 input=b2b5d07e9de6c070]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1557 | { |
| 1558 | Py_ssize_t n = Py_SIZE(self); |
| 1559 | char *buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1560 | |
| 1561 | if (n == PY_SSIZE_T_MAX) { |
| 1562 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 1563 | "cannot add more objects to bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1564 | return NULL; |
| 1565 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1566 | if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) |
| 1567 | return NULL; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1568 | buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1569 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1570 | if (index < 0) { |
| 1571 | index += n; |
| 1572 | if (index < 0) |
| 1573 | index = 0; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1574 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1575 | if (index > n) |
| 1576 | index = n; |
| 1577 | memmove(buf + index + 1, buf + index, n - index); |
| 1578 | buf[index] = item; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1579 | |
| 1580 | Py_RETURN_NONE; |
| 1581 | } |
| 1582 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1583 | /*[clinic input] |
| 1584 | bytearray.append |
| 1585 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1586 | item: bytesvalue |
| 1587 | The item to be appended. |
| 1588 | / |
| 1589 | |
| 1590 | Append a single item to the end of the bytearray. |
| 1591 | [clinic start generated code]*/ |
| 1592 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1593 | static PyObject * |
| 1594 | bytearray_append_impl(PyByteArrayObject *self, int item) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1595 | /*[clinic end generated code: output=a154e19ed1886cb6 input=20d6bec3d1340593]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1596 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1597 | Py_ssize_t n = Py_SIZE(self); |
| 1598 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1599 | if (n == PY_SSIZE_T_MAX) { |
| 1600 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 1601 | "cannot add more objects to bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1602 | return NULL; |
| 1603 | } |
| 1604 | if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) |
| 1605 | return NULL; |
| 1606 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1607 | PyByteArray_AS_STRING(self)[n] = item; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1608 | |
| 1609 | Py_RETURN_NONE; |
| 1610 | } |
| 1611 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1612 | /*[clinic input] |
| 1613 | bytearray.extend |
| 1614 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1615 | iterable_of_ints: object |
| 1616 | The iterable of items to append. |
| 1617 | / |
| 1618 | |
| 1619 | Append all the items from the iterator or sequence to the end of the bytearray. |
| 1620 | [clinic start generated code]*/ |
| 1621 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1622 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1623 | bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1624 | /*[clinic end generated code: output=98155dbe249170b1 input=c617b3a93249ba28]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1625 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1626 | PyObject *it, *item, *bytearray_obj; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1627 | Py_ssize_t buf_size = 0, len = 0; |
| 1628 | int value; |
| 1629 | char *buf; |
| 1630 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1631 | /* bytearray_setslice code only accepts something supporting PEP 3118. */ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1632 | if (PyObject_CheckBuffer(iterable_of_ints)) { |
| 1633 | 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] | 1634 | return NULL; |
| 1635 | |
| 1636 | Py_RETURN_NONE; |
| 1637 | } |
| 1638 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1639 | it = PyObject_GetIter(iterable_of_ints); |
Serhiy Storchaka | 2c2044e | 2018-10-21 15:29:12 +0300 | [diff] [blame] | 1640 | if (it == NULL) { |
| 1641 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 1642 | PyErr_Format(PyExc_TypeError, |
| 1643 | "can't extend bytearray with %.100s", |
| 1644 | iterable_of_ints->ob_type->tp_name); |
| 1645 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1646 | return NULL; |
Serhiy Storchaka | 2c2044e | 2018-10-21 15:29:12 +0300 | [diff] [blame] | 1647 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1648 | |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 1649 | /* 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] | 1650 | buf_size = PyObject_LengthHint(iterable_of_ints, 32); |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1651 | if (buf_size == -1) { |
| 1652 | Py_DECREF(it); |
| 1653 | return NULL; |
| 1654 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1655 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1656 | bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size); |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 1657 | if (bytearray_obj == NULL) { |
| 1658 | Py_DECREF(it); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1659 | return NULL; |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 1660 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1661 | buf = PyByteArray_AS_STRING(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1662 | |
| 1663 | while ((item = PyIter_Next(it)) != NULL) { |
| 1664 | if (! _getbytevalue(item, &value)) { |
| 1665 | Py_DECREF(item); |
| 1666 | Py_DECREF(it); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1667 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1668 | return NULL; |
| 1669 | } |
| 1670 | buf[len++] = value; |
| 1671 | Py_DECREF(item); |
| 1672 | |
| 1673 | if (len >= buf_size) { |
Martin Panter | 371731e | 2016-07-18 07:53:13 +0000 | [diff] [blame] | 1674 | Py_ssize_t addition; |
| 1675 | if (len == PY_SSIZE_T_MAX) { |
| 1676 | Py_DECREF(it); |
| 1677 | Py_DECREF(bytearray_obj); |
| 1678 | return PyErr_NoMemory(); |
| 1679 | } |
| 1680 | addition = len >> 1; |
| 1681 | if (addition > PY_SSIZE_T_MAX - len - 1) |
| 1682 | buf_size = PY_SSIZE_T_MAX; |
| 1683 | else |
| 1684 | buf_size = len + addition + 1; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1685 | if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1686 | Py_DECREF(it); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1687 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1688 | return NULL; |
| 1689 | } |
| 1690 | /* Recompute the `buf' pointer, since the resizing operation may |
| 1691 | have invalidated it. */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1692 | buf = PyByteArray_AS_STRING(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1693 | } |
| 1694 | } |
| 1695 | Py_DECREF(it); |
| 1696 | |
| 1697 | /* Resize down to exact size. */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1698 | if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) { |
| 1699 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1700 | return NULL; |
| 1701 | } |
| 1702 | |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 1703 | if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) { |
| 1704 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1705 | return NULL; |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 1706 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1707 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1708 | |
| 1709 | Py_RETURN_NONE; |
| 1710 | } |
| 1711 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1712 | /*[clinic input] |
| 1713 | bytearray.pop |
| 1714 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1715 | index: Py_ssize_t = -1 |
| 1716 | The index from where to remove the item. |
| 1717 | -1 (the default value) means remove the last item. |
| 1718 | / |
| 1719 | |
| 1720 | Remove and return a single item from B. |
| 1721 | |
| 1722 | If no index argument is given, will pop the last item. |
| 1723 | [clinic start generated code]*/ |
| 1724 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1725 | static PyObject * |
| 1726 | bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1727 | /*[clinic end generated code: output=e0ccd401f8021da8 input=3591df2d06c0d237]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1728 | { |
| 1729 | int value; |
| 1730 | Py_ssize_t n = Py_SIZE(self); |
| 1731 | char *buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1732 | |
| 1733 | if (n == 0) { |
Eli Bendersky | 1bc4f19 | 2011-03-04 04:55:25 +0000 | [diff] [blame] | 1734 | PyErr_SetString(PyExc_IndexError, |
| 1735 | "pop from empty bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1736 | return NULL; |
| 1737 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1738 | if (index < 0) |
| 1739 | index += Py_SIZE(self); |
| 1740 | if (index < 0 || index >= Py_SIZE(self)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1741 | PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
| 1742 | return NULL; |
| 1743 | } |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 1744 | if (!_canresize(self)) |
| 1745 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1746 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1747 | buf = PyByteArray_AS_STRING(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1748 | value = buf[index]; |
| 1749 | memmove(buf + index, buf + index + 1, n - index); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1750 | if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) |
| 1751 | return NULL; |
| 1752 | |
Mark Dickinson | 54a3db9 | 2009-09-06 10:19:23 +0000 | [diff] [blame] | 1753 | return PyLong_FromLong((unsigned char)value); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1756 | /*[clinic input] |
| 1757 | bytearray.remove |
| 1758 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1759 | value: bytesvalue |
| 1760 | The value to remove. |
| 1761 | / |
| 1762 | |
| 1763 | Remove the first occurrence of a value in the bytearray. |
| 1764 | [clinic start generated code]*/ |
| 1765 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1766 | static PyObject * |
| 1767 | bytearray_remove_impl(PyByteArrayObject *self, int value) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1768 | /*[clinic end generated code: output=d659e37866709c13 input=121831240cd51ddf]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1769 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1770 | Py_ssize_t where, n = Py_SIZE(self); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1771 | char *buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1772 | |
Serhiy Storchaka | 4b23494 | 2016-05-16 22:24:03 +0300 | [diff] [blame] | 1773 | where = stringlib_find_char(buf, n, value); |
| 1774 | if (where < 0) { |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 1775 | PyErr_SetString(PyExc_ValueError, "value not found in bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1776 | return NULL; |
| 1777 | } |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 1778 | if (!_canresize(self)) |
| 1779 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1780 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1781 | memmove(buf + where, buf + where + 1, n - where); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1782 | if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) |
| 1783 | return NULL; |
| 1784 | |
| 1785 | Py_RETURN_NONE; |
| 1786 | } |
| 1787 | |
| 1788 | /* XXX These two helpers could be optimized if argsize == 1 */ |
| 1789 | |
| 1790 | static Py_ssize_t |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1791 | lstrip_helper(const char *myptr, Py_ssize_t mysize, |
| 1792 | const void *argptr, Py_ssize_t argsize) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1793 | { |
| 1794 | Py_ssize_t i = 0; |
Antoine Pitrou | 5b72075 | 2013-10-05 21:24:10 +0200 | [diff] [blame] | 1795 | while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1796 | i++; |
| 1797 | return i; |
| 1798 | } |
| 1799 | |
| 1800 | static Py_ssize_t |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1801 | rstrip_helper(const char *myptr, Py_ssize_t mysize, |
| 1802 | const void *argptr, Py_ssize_t argsize) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1803 | { |
| 1804 | Py_ssize_t i = mysize - 1; |
Antoine Pitrou | 5b72075 | 2013-10-05 21:24:10 +0200 | [diff] [blame] | 1805 | while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1806 | i--; |
| 1807 | return i + 1; |
| 1808 | } |
| 1809 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1810 | /*[clinic input] |
| 1811 | bytearray.strip |
| 1812 | |
| 1813 | bytes: object = None |
| 1814 | / |
| 1815 | |
| 1816 | Strip leading and trailing bytes contained in the argument. |
| 1817 | |
| 1818 | If the argument is omitted or None, strip leading and trailing ASCII whitespace. |
| 1819 | [clinic start generated code]*/ |
| 1820 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1821 | static PyObject * |
| 1822 | bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1823 | /*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1824 | { |
| 1825 | Py_ssize_t left, right, mysize, byteslen; |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 1826 | char *myptr; |
| 1827 | const char *bytesptr; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1828 | Py_buffer vbytes; |
| 1829 | |
| 1830 | if (bytes == Py_None) { |
| 1831 | bytesptr = "\t\n\r\f\v "; |
| 1832 | byteslen = 6; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1833 | } |
| 1834 | else { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1835 | if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1836 | return NULL; |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 1837 | bytesptr = (const char *) vbytes.buf; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1838 | byteslen = vbytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1839 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1840 | myptr = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1841 | mysize = Py_SIZE(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1842 | left = lstrip_helper(myptr, mysize, bytesptr, byteslen); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1843 | if (left == mysize) |
| 1844 | right = left; |
| 1845 | else |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1846 | right = rstrip_helper(myptr, mysize, bytesptr, byteslen); |
| 1847 | if (bytes != Py_None) |
| 1848 | PyBuffer_Release(&vbytes); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1849 | return PyByteArray_FromStringAndSize(myptr + left, right - left); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1852 | /*[clinic input] |
| 1853 | bytearray.lstrip |
| 1854 | |
| 1855 | bytes: object = None |
| 1856 | / |
| 1857 | |
| 1858 | Strip leading bytes contained in the argument. |
| 1859 | |
| 1860 | If the argument is omitted or None, strip leading ASCII whitespace. |
| 1861 | [clinic start generated code]*/ |
| 1862 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1863 | static PyObject * |
| 1864 | bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1865 | /*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1866 | { |
| 1867 | Py_ssize_t left, right, mysize, byteslen; |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 1868 | char *myptr; |
| 1869 | const char *bytesptr; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1870 | Py_buffer vbytes; |
| 1871 | |
| 1872 | if (bytes == Py_None) { |
| 1873 | bytesptr = "\t\n\r\f\v "; |
| 1874 | byteslen = 6; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1875 | } |
| 1876 | else { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1877 | if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1878 | return NULL; |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 1879 | bytesptr = (const char *) vbytes.buf; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1880 | byteslen = vbytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1881 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1882 | myptr = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1883 | mysize = Py_SIZE(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1884 | left = lstrip_helper(myptr, mysize, bytesptr, byteslen); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1885 | right = mysize; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1886 | if (bytes != Py_None) |
| 1887 | PyBuffer_Release(&vbytes); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1888 | return PyByteArray_FromStringAndSize(myptr + left, right - left); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1891 | /*[clinic input] |
| 1892 | bytearray.rstrip |
| 1893 | |
| 1894 | bytes: object = None |
| 1895 | / |
| 1896 | |
| 1897 | Strip trailing bytes contained in the argument. |
| 1898 | |
| 1899 | If the argument is omitted or None, strip trailing ASCII whitespace. |
| 1900 | [clinic start generated code]*/ |
| 1901 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1902 | static PyObject * |
| 1903 | bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1904 | /*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1905 | { |
| 1906 | Py_ssize_t right, mysize, byteslen; |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 1907 | char *myptr; |
| 1908 | const char *bytesptr; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1909 | Py_buffer vbytes; |
| 1910 | |
| 1911 | if (bytes == Py_None) { |
| 1912 | bytesptr = "\t\n\r\f\v "; |
| 1913 | byteslen = 6; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1914 | } |
| 1915 | else { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1916 | if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1917 | return NULL; |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 1918 | bytesptr = (const char *) vbytes.buf; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1919 | byteslen = vbytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1920 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1921 | myptr = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1922 | mysize = Py_SIZE(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1923 | right = rstrip_helper(myptr, mysize, bytesptr, byteslen); |
| 1924 | if (bytes != Py_None) |
| 1925 | PyBuffer_Release(&vbytes); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1926 | return PyByteArray_FromStringAndSize(myptr, right); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1929 | /*[clinic input] |
| 1930 | bytearray.decode |
| 1931 | |
| 1932 | encoding: str(c_default="NULL") = 'utf-8' |
| 1933 | The encoding with which to decode the bytearray. |
| 1934 | errors: str(c_default="NULL") = 'strict' |
| 1935 | The error handling scheme to use for the handling of decoding errors. |
| 1936 | The default is 'strict' meaning that decoding errors raise a |
| 1937 | UnicodeDecodeError. Other possible values are 'ignore' and 'replace' |
| 1938 | as well as any other name registered with codecs.register_error that |
| 1939 | can handle UnicodeDecodeErrors. |
| 1940 | |
| 1941 | Decode the bytearray using the codec registered for encoding. |
| 1942 | [clinic start generated code]*/ |
| 1943 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1944 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1945 | bytearray_decode_impl(PyByteArrayObject *self, const char *encoding, |
| 1946 | const char *errors) |
| 1947 | /*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1948 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1949 | if (encoding == NULL) |
| 1950 | encoding = PyUnicode_GetDefaultEncoding(); |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1951 | return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | PyDoc_STRVAR(alloc_doc, |
| 1955 | "B.__alloc__() -> int\n\ |
| 1956 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1957 | Return the number of bytes actually allocated."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1958 | |
| 1959 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1960 | bytearray_alloc(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1961 | { |
| 1962 | return PyLong_FromSsize_t(self->ob_alloc); |
| 1963 | } |
| 1964 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1965 | /*[clinic input] |
| 1966 | bytearray.join |
| 1967 | |
| 1968 | iterable_of_bytes: object |
| 1969 | / |
| 1970 | |
| 1971 | Concatenate any number of bytes/bytearray objects. |
| 1972 | |
| 1973 | The bytearray whose method is called is inserted in between each pair. |
| 1974 | |
| 1975 | The result is returned as a new bytearray object. |
| 1976 | [clinic start generated code]*/ |
| 1977 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1978 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1979 | bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1980 | /*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1981 | { |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1982 | return stringlib_bytes_join((PyObject*)self, iterable_of_bytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1985 | /*[clinic input] |
| 1986 | bytearray.splitlines |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1987 | |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 1988 | keepends: bool(accept={int}) = False |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1989 | |
| 1990 | Return a list of the lines in the bytearray, breaking at line boundaries. |
| 1991 | |
| 1992 | Line breaks are not included in the resulting list unless keepends is given and |
| 1993 | true. |
| 1994 | [clinic start generated code]*/ |
| 1995 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1996 | static PyObject * |
| 1997 | bytearray_splitlines_impl(PyByteArrayObject *self, int keepends) |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 1998 | /*[clinic end generated code: output=4223c94b895f6ad9 input=99a27ad959b9cf6b]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1999 | { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2000 | return stringlib_splitlines( |
| 2001 | (PyObject*) self, PyByteArray_AS_STRING(self), |
| 2002 | PyByteArray_GET_SIZE(self), keepends |
| 2003 | ); |
| 2004 | } |
| 2005 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2006 | /*[clinic input] |
| 2007 | @classmethod |
| 2008 | bytearray.fromhex |
| 2009 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2010 | string: unicode |
| 2011 | / |
| 2012 | |
| 2013 | Create a bytearray object from a string of hexadecimal numbers. |
| 2014 | |
| 2015 | Spaces between two numbers are accepted. |
| 2016 | Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef') |
| 2017 | [clinic start generated code]*/ |
| 2018 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2019 | static PyObject * |
Serhiy Storchaka | 0855e70 | 2016-07-01 17:22:31 +0300 | [diff] [blame] | 2020 | bytearray_fromhex_impl(PyTypeObject *type, PyObject *string) |
| 2021 | /*[clinic end generated code: output=8f0f0b6d30fb3ba0 input=f033a16d1fb21f48]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2022 | { |
Serhiy Storchaka | 0855e70 | 2016-07-01 17:22:31 +0300 | [diff] [blame] | 2023 | PyObject *result = _PyBytes_FromHex(string, type == &PyByteArray_Type); |
| 2024 | if (type != &PyByteArray_Type && result != NULL) { |
Victor Stinner | de4ae3d | 2016-12-04 22:59:09 +0100 | [diff] [blame] | 2025 | Py_SETREF(result, PyObject_CallFunctionObjArgs((PyObject *)type, |
| 2026 | result, NULL)); |
Serhiy Storchaka | 0855e70 | 2016-07-01 17:22:31 +0300 | [diff] [blame] | 2027 | } |
| 2028 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
Gregory P. Smith | 8cb6569 | 2015-04-25 23:22:26 +0000 | [diff] [blame] | 2031 | PyDoc_STRVAR(hex__doc__, |
| 2032 | "B.hex() -> string\n\ |
| 2033 | \n\ |
| 2034 | Create a string of hexadecimal numbers from a bytearray object.\n\ |
| 2035 | Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'."); |
| 2036 | |
| 2037 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2038 | bytearray_hex(PyBytesObject *self, PyObject *Py_UNUSED(ignored)) |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2039 | { |
| 2040 | char* argbuf = PyByteArray_AS_STRING(self); |
| 2041 | Py_ssize_t arglen = PyByteArray_GET_SIZE(self); |
| 2042 | return _Py_strhex(argbuf, arglen); |
| 2043 | } |
| 2044 | |
| 2045 | static PyObject * |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2046 | _common_reduce(PyByteArrayObject *self, int proto) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2047 | { |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2048 | PyObject *dict; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2049 | _Py_IDENTIFIER(__dict__); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2050 | char *buf; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2051 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2052 | dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2053 | if (dict == NULL) { |
| 2054 | PyErr_Clear(); |
| 2055 | dict = Py_None; |
| 2056 | Py_INCREF(dict); |
| 2057 | } |
| 2058 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2059 | buf = PyByteArray_AS_STRING(self); |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2060 | if (proto < 3) { |
| 2061 | /* use str based reduction for backwards compatibility with Python 2.x */ |
| 2062 | PyObject *latin1; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2063 | if (Py_SIZE(self)) |
| 2064 | latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL); |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2065 | else |
| 2066 | latin1 = PyUnicode_FromString(""); |
| 2067 | return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict); |
| 2068 | } |
| 2069 | else { |
| 2070 | /* use more efficient byte based reduction */ |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2071 | if (Py_SIZE(self)) { |
| 2072 | 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] | 2073 | } |
| 2074 | else { |
| 2075 | return Py_BuildValue("(O()N)", Py_TYPE(self), dict); |
| 2076 | } |
| 2077 | } |
| 2078 | } |
| 2079 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2080 | /*[clinic input] |
| 2081 | bytearray.__reduce__ as bytearray_reduce |
| 2082 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 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_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 2088 | /*[clinic end generated code: output=52bf304086464cab input=44b5737ada62dd3f]*/ |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2089 | { |
| 2090 | return _common_reduce(self, 2); |
| 2091 | } |
| 2092 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2093 | /*[clinic input] |
| 2094 | bytearray.__reduce_ex__ as bytearray_reduce_ex |
| 2095 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2096 | proto: int = 0 |
| 2097 | / |
| 2098 | |
| 2099 | Return state information for pickling. |
| 2100 | [clinic start generated code]*/ |
| 2101 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2102 | static PyObject * |
| 2103 | bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 2104 | /*[clinic end generated code: output=52eac33377197520 input=f129bc1a1aa151ee]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2105 | { |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2106 | return _common_reduce(self, proto); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2107 | } |
| 2108 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2109 | /*[clinic input] |
| 2110 | bytearray.__sizeof__ as bytearray_sizeof |
| 2111 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2112 | Returns the size of the bytearray object in memory, in bytes. |
| 2113 | [clinic start generated code]*/ |
| 2114 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2115 | static PyObject * |
| 2116 | bytearray_sizeof_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 2117 | /*[clinic end generated code: output=738abdd17951c427 input=e27320fd98a4bc5a]*/ |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2118 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 2119 | Py_ssize_t res; |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2120 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 2121 | res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char); |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 2122 | return PyLong_FromSsize_t(res); |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2125 | static PySequenceMethods bytearray_as_sequence = { |
| 2126 | (lenfunc)bytearray_length, /* sq_length */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2127 | (binaryfunc)PyByteArray_Concat, /* sq_concat */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2128 | (ssizeargfunc)bytearray_repeat, /* sq_repeat */ |
| 2129 | (ssizeargfunc)bytearray_getitem, /* sq_item */ |
| 2130 | 0, /* sq_slice */ |
| 2131 | (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */ |
| 2132 | 0, /* sq_ass_slice */ |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2133 | (objobjproc)bytearray_contains, /* sq_contains */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2134 | (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */ |
| 2135 | (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2136 | }; |
| 2137 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2138 | static PyMappingMethods bytearray_as_mapping = { |
| 2139 | (lenfunc)bytearray_length, |
| 2140 | (binaryfunc)bytearray_subscript, |
| 2141 | (objobjargproc)bytearray_ass_subscript, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2142 | }; |
| 2143 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2144 | static PyBufferProcs bytearray_as_buffer = { |
| 2145 | (getbufferproc)bytearray_getbuffer, |
| 2146 | (releasebufferproc)bytearray_releasebuffer, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2147 | }; |
| 2148 | |
| 2149 | static PyMethodDef |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2150 | bytearray_methods[] = { |
| 2151 | {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2152 | BYTEARRAY_REDUCE_METHODDEF |
| 2153 | BYTEARRAY_REDUCE_EX_METHODDEF |
| 2154 | BYTEARRAY_SIZEOF_METHODDEF |
| 2155 | BYTEARRAY_APPEND_METHODDEF |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2156 | {"capitalize", stringlib_capitalize, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2157 | _Py_capitalize__doc__}, |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 2158 | STRINGLIB_CENTER_METHODDEF |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2159 | BYTEARRAY_CLEAR_METHODDEF |
| 2160 | BYTEARRAY_COPY_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2161 | {"count", (PyCFunction)bytearray_count, METH_VARARGS, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2162 | _Py_count__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2163 | BYTEARRAY_DECODE_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2164 | {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2165 | _Py_endswith__doc__}, |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 2166 | STRINGLIB_EXPANDTABS_METHODDEF |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2167 | BYTEARRAY_EXTEND_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2168 | {"find", (PyCFunction)bytearray_find, METH_VARARGS, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2169 | _Py_find__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2170 | BYTEARRAY_FROMHEX_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2171 | {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__}, |
| 2172 | {"index", (PyCFunction)bytearray_index, METH_VARARGS, _Py_index__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2173 | BYTEARRAY_INSERT_METHODDEF |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2174 | {"isalnum", stringlib_isalnum, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2175 | _Py_isalnum__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2176 | {"isalpha", stringlib_isalpha, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2177 | _Py_isalpha__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2178 | {"isascii", stringlib_isascii, METH_NOARGS, |
INADA Naoki | a49ac99 | 2018-01-27 14:06:21 +0900 | [diff] [blame] | 2179 | _Py_isascii__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2180 | {"isdigit", stringlib_isdigit, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2181 | _Py_isdigit__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2182 | {"islower", stringlib_islower, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2183 | _Py_islower__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2184 | {"isspace", stringlib_isspace, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2185 | _Py_isspace__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2186 | {"istitle", stringlib_istitle, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2187 | _Py_istitle__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2188 | {"isupper", stringlib_isupper, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2189 | _Py_isupper__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2190 | BYTEARRAY_JOIN_METHODDEF |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 2191 | STRINGLIB_LJUST_METHODDEF |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2192 | {"lower", stringlib_lower, METH_NOARGS, _Py_lower__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2193 | BYTEARRAY_LSTRIP_METHODDEF |
| 2194 | BYTEARRAY_MAKETRANS_METHODDEF |
| 2195 | BYTEARRAY_PARTITION_METHODDEF |
| 2196 | BYTEARRAY_POP_METHODDEF |
| 2197 | BYTEARRAY_REMOVE_METHODDEF |
| 2198 | BYTEARRAY_REPLACE_METHODDEF |
| 2199 | BYTEARRAY_REVERSE_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2200 | {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__}, |
| 2201 | {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__}, |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 2202 | STRINGLIB_RJUST_METHODDEF |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2203 | BYTEARRAY_RPARTITION_METHODDEF |
| 2204 | BYTEARRAY_RSPLIT_METHODDEF |
| 2205 | BYTEARRAY_RSTRIP_METHODDEF |
| 2206 | BYTEARRAY_SPLIT_METHODDEF |
| 2207 | BYTEARRAY_SPLITLINES_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2208 | {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS , |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2209 | _Py_startswith__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2210 | BYTEARRAY_STRIP_METHODDEF |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2211 | {"swapcase", stringlib_swapcase, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2212 | _Py_swapcase__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2213 | {"title", stringlib_title, METH_NOARGS, _Py_title__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2214 | BYTEARRAY_TRANSLATE_METHODDEF |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2215 | {"upper", stringlib_upper, METH_NOARGS, _Py_upper__doc__}, |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 2216 | STRINGLIB_ZFILL_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2217 | {NULL} |
| 2218 | }; |
| 2219 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 2220 | static PyObject * |
| 2221 | bytearray_mod(PyObject *v, PyObject *w) |
| 2222 | { |
| 2223 | if (!PyByteArray_Check(v)) |
| 2224 | Py_RETURN_NOTIMPLEMENTED; |
Berker Peksag | 43de36d | 2016-04-16 01:20:47 +0300 | [diff] [blame] | 2225 | 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] | 2226 | } |
| 2227 | |
| 2228 | static PyNumberMethods bytearray_as_number = { |
| 2229 | 0, /*nb_add*/ |
| 2230 | 0, /*nb_subtract*/ |
| 2231 | 0, /*nb_multiply*/ |
| 2232 | bytearray_mod, /*nb_remainder*/ |
| 2233 | }; |
| 2234 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2235 | PyDoc_STRVAR(bytearray_doc, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2236 | "bytearray(iterable_of_ints) -> bytearray\n\ |
| 2237 | bytearray(string, encoding[, errors]) -> bytearray\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 2238 | bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\ |
| 2239 | bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\ |
| 2240 | bytearray() -> empty bytes array\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2241 | \n\ |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 2242 | Construct a mutable bytearray object from:\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2243 | - an iterable yielding integers in range(256)\n\ |
| 2244 | - a text string encoded using the specified encoding\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 2245 | - a bytes or a buffer object\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2246 | - any object implementing the buffer API.\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 2247 | - an integer"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2248 | |
| 2249 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2250 | static PyObject *bytearray_iter(PyObject *seq); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2251 | |
| 2252 | PyTypeObject PyByteArray_Type = { |
| 2253 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2254 | "bytearray", |
| 2255 | sizeof(PyByteArrayObject), |
| 2256 | 0, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2257 | (destructor)bytearray_dealloc, /* tp_dealloc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2258 | 0, /* tp_print */ |
| 2259 | 0, /* tp_getattr */ |
| 2260 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 2261 | 0, /* tp_reserved */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2262 | (reprfunc)bytearray_repr, /* tp_repr */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 2263 | &bytearray_as_number, /* tp_as_number */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2264 | &bytearray_as_sequence, /* tp_as_sequence */ |
| 2265 | &bytearray_as_mapping, /* tp_as_mapping */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2266 | 0, /* tp_hash */ |
| 2267 | 0, /* tp_call */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2268 | bytearray_str, /* tp_str */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2269 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2270 | 0, /* tp_setattro */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2271 | &bytearray_as_buffer, /* tp_as_buffer */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2272 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2273 | bytearray_doc, /* tp_doc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2274 | 0, /* tp_traverse */ |
| 2275 | 0, /* tp_clear */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2276 | (richcmpfunc)bytearray_richcompare, /* tp_richcompare */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2277 | 0, /* tp_weaklistoffset */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2278 | bytearray_iter, /* tp_iter */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2279 | 0, /* tp_iternext */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2280 | bytearray_methods, /* tp_methods */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2281 | 0, /* tp_members */ |
| 2282 | 0, /* tp_getset */ |
| 2283 | 0, /* tp_base */ |
| 2284 | 0, /* tp_dict */ |
| 2285 | 0, /* tp_descr_get */ |
| 2286 | 0, /* tp_descr_set */ |
| 2287 | 0, /* tp_dictoffset */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2288 | (initproc)bytearray_init, /* tp_init */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2289 | PyType_GenericAlloc, /* tp_alloc */ |
| 2290 | PyType_GenericNew, /* tp_new */ |
| 2291 | PyObject_Del, /* tp_free */ |
| 2292 | }; |
| 2293 | |
| 2294 | /*********************** Bytes Iterator ****************************/ |
| 2295 | |
| 2296 | typedef struct { |
| 2297 | PyObject_HEAD |
| 2298 | Py_ssize_t it_index; |
| 2299 | PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 2300 | } bytesiterobject; |
| 2301 | |
| 2302 | static void |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2303 | bytearrayiter_dealloc(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2304 | { |
| 2305 | _PyObject_GC_UNTRACK(it); |
| 2306 | Py_XDECREF(it->it_seq); |
| 2307 | PyObject_GC_Del(it); |
| 2308 | } |
| 2309 | |
| 2310 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2311 | bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2312 | { |
| 2313 | Py_VISIT(it->it_seq); |
| 2314 | return 0; |
| 2315 | } |
| 2316 | |
| 2317 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2318 | bytearrayiter_next(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2319 | { |
| 2320 | PyByteArrayObject *seq; |
| 2321 | PyObject *item; |
| 2322 | |
| 2323 | assert(it != NULL); |
| 2324 | seq = it->it_seq; |
| 2325 | if (seq == NULL) |
| 2326 | return NULL; |
| 2327 | assert(PyByteArray_Check(seq)); |
| 2328 | |
| 2329 | if (it->it_index < PyByteArray_GET_SIZE(seq)) { |
| 2330 | item = PyLong_FromLong( |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2331 | (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2332 | if (item != NULL) |
| 2333 | ++it->it_index; |
| 2334 | return item; |
| 2335 | } |
| 2336 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2337 | it->it_seq = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 2338 | Py_DECREF(seq); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2339 | return NULL; |
| 2340 | } |
| 2341 | |
| 2342 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2343 | bytearrayiter_length_hint(bytesiterobject *it, PyObject *Py_UNUSED(ignored)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2344 | { |
| 2345 | Py_ssize_t len = 0; |
Serhiy Storchaka | af65872 | 2016-07-03 14:41:36 +0300 | [diff] [blame] | 2346 | if (it->it_seq) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2347 | len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index; |
Serhiy Storchaka | af65872 | 2016-07-03 14:41:36 +0300 | [diff] [blame] | 2348 | if (len < 0) { |
| 2349 | len = 0; |
| 2350 | } |
| 2351 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2352 | return PyLong_FromSsize_t(len); |
| 2353 | } |
| 2354 | |
| 2355 | PyDoc_STRVAR(length_hint_doc, |
| 2356 | "Private method returning an estimate of len(list(it))."); |
| 2357 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2358 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2359 | bytearrayiter_reduce(bytesiterobject *it, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2360 | { |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 2361 | _Py_IDENTIFIER(iter); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2362 | if (it->it_seq != NULL) { |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 2363 | return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2364 | it->it_seq, it->it_index); |
| 2365 | } else { |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 2366 | return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter)); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2367 | } |
| 2368 | } |
| 2369 | |
| 2370 | static PyObject * |
| 2371 | bytearrayiter_setstate(bytesiterobject *it, PyObject *state) |
| 2372 | { |
| 2373 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 2374 | if (index == -1 && PyErr_Occurred()) |
| 2375 | return NULL; |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 2376 | if (it->it_seq != NULL) { |
| 2377 | if (index < 0) |
| 2378 | index = 0; |
| 2379 | else if (index > PyByteArray_GET_SIZE(it->it_seq)) |
| 2380 | index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */ |
| 2381 | it->it_index = index; |
| 2382 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2383 | Py_RETURN_NONE; |
| 2384 | } |
| 2385 | |
| 2386 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 2387 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2388 | static PyMethodDef bytearrayiter_methods[] = { |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2389 | {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2390 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2391 | {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2392 | bytearray_reduce__doc__}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2393 | {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O, |
| 2394 | setstate_doc}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2395 | {NULL, NULL} /* sentinel */ |
| 2396 | }; |
| 2397 | |
| 2398 | PyTypeObject PyByteArrayIter_Type = { |
| 2399 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2400 | "bytearray_iterator", /* tp_name */ |
| 2401 | sizeof(bytesiterobject), /* tp_basicsize */ |
| 2402 | 0, /* tp_itemsize */ |
| 2403 | /* methods */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2404 | (destructor)bytearrayiter_dealloc, /* tp_dealloc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2405 | 0, /* tp_print */ |
| 2406 | 0, /* tp_getattr */ |
| 2407 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 2408 | 0, /* tp_reserved */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2409 | 0, /* tp_repr */ |
| 2410 | 0, /* tp_as_number */ |
| 2411 | 0, /* tp_as_sequence */ |
| 2412 | 0, /* tp_as_mapping */ |
| 2413 | 0, /* tp_hash */ |
| 2414 | 0, /* tp_call */ |
| 2415 | 0, /* tp_str */ |
| 2416 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2417 | 0, /* tp_setattro */ |
| 2418 | 0, /* tp_as_buffer */ |
| 2419 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 2420 | 0, /* tp_doc */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2421 | (traverseproc)bytearrayiter_traverse, /* tp_traverse */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2422 | 0, /* tp_clear */ |
| 2423 | 0, /* tp_richcompare */ |
| 2424 | 0, /* tp_weaklistoffset */ |
| 2425 | PyObject_SelfIter, /* tp_iter */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2426 | (iternextfunc)bytearrayiter_next, /* tp_iternext */ |
| 2427 | bytearrayiter_methods, /* tp_methods */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2428 | 0, |
| 2429 | }; |
| 2430 | |
| 2431 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2432 | bytearray_iter(PyObject *seq) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2433 | { |
| 2434 | bytesiterobject *it; |
| 2435 | |
| 2436 | if (!PyByteArray_Check(seq)) { |
| 2437 | PyErr_BadInternalCall(); |
| 2438 | return NULL; |
| 2439 | } |
| 2440 | it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type); |
| 2441 | if (it == NULL) |
| 2442 | return NULL; |
| 2443 | it->it_index = 0; |
| 2444 | Py_INCREF(seq); |
| 2445 | it->it_seq = (PyByteArrayObject *)seq; |
| 2446 | _PyObject_GC_TRACK(it); |
| 2447 | return (PyObject *)it; |
| 2448 | } |