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