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