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