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 | { |
Victor Stinner | de4ae3d | 2016-12-04 22:59:09 +0100 | [diff] [blame] | 92 | return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type, |
| 93 | input, NULL); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Serhiy Storchaka | a231428 | 2017-10-29 02:11:54 +0300 | [diff] [blame] | 96 | static PyObject * |
| 97 | _PyByteArray_FromBufferObject(PyObject *obj) |
| 98 | { |
| 99 | PyObject *result; |
| 100 | Py_buffer view; |
| 101 | |
| 102 | if (PyObject_GetBuffer(obj, &view, PyBUF_FULL_RO) < 0) { |
| 103 | return NULL; |
| 104 | } |
| 105 | result = PyByteArray_FromStringAndSize(NULL, view.len); |
| 106 | if (result != NULL && |
| 107 | PyBuffer_ToContiguous(PyByteArray_AS_STRING(result), |
| 108 | &view, view.len, 'C') < 0) |
| 109 | { |
| 110 | Py_CLEAR(result); |
| 111 | } |
| 112 | PyBuffer_Release(&view); |
| 113 | return result; |
| 114 | } |
| 115 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 116 | PyObject * |
| 117 | PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size) |
| 118 | { |
| 119 | PyByteArrayObject *new; |
| 120 | Py_ssize_t alloc; |
| 121 | |
| 122 | if (size < 0) { |
| 123 | PyErr_SetString(PyExc_SystemError, |
| 124 | "Negative size passed to PyByteArray_FromStringAndSize"); |
| 125 | return NULL; |
| 126 | } |
| 127 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 128 | /* Prevent buffer overflow when setting alloc to size+1. */ |
| 129 | if (size == PY_SSIZE_T_MAX) { |
| 130 | return PyErr_NoMemory(); |
| 131 | } |
| 132 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 133 | new = PyObject_New(PyByteArrayObject, &PyByteArray_Type); |
| 134 | if (new == NULL) |
| 135 | return NULL; |
| 136 | |
| 137 | if (size == 0) { |
| 138 | new->ob_bytes = NULL; |
| 139 | alloc = 0; |
| 140 | } |
| 141 | else { |
| 142 | alloc = size + 1; |
Antoine Pitrou | 39aba4f | 2011-11-12 21:15:28 +0100 | [diff] [blame] | 143 | new->ob_bytes = PyObject_Malloc(alloc); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 144 | if (new->ob_bytes == NULL) { |
| 145 | Py_DECREF(new); |
| 146 | return PyErr_NoMemory(); |
| 147 | } |
Antoine Pitrou | fc8d6f4 | 2010-01-17 12:38:54 +0000 | [diff] [blame] | 148 | if (bytes != NULL && size > 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 149 | memcpy(new->ob_bytes, bytes, size); |
| 150 | new->ob_bytes[size] = '\0'; /* Trailing null byte */ |
| 151 | } |
| 152 | Py_SIZE(new) = size; |
| 153 | new->ob_alloc = alloc; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 154 | new->ob_start = new->ob_bytes; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 155 | new->ob_exports = 0; |
| 156 | |
| 157 | return (PyObject *)new; |
| 158 | } |
| 159 | |
| 160 | Py_ssize_t |
| 161 | PyByteArray_Size(PyObject *self) |
| 162 | { |
| 163 | assert(self != NULL); |
| 164 | assert(PyByteArray_Check(self)); |
| 165 | |
| 166 | return PyByteArray_GET_SIZE(self); |
| 167 | } |
| 168 | |
| 169 | char * |
| 170 | PyByteArray_AsString(PyObject *self) |
| 171 | { |
| 172 | assert(self != NULL); |
| 173 | assert(PyByteArray_Check(self)); |
| 174 | |
| 175 | return PyByteArray_AS_STRING(self); |
| 176 | } |
| 177 | |
| 178 | int |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 179 | PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 180 | { |
| 181 | void *sval; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 182 | PyByteArrayObject *obj = ((PyByteArrayObject *)self); |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 183 | /* All computations are done unsigned to avoid integer overflows |
| 184 | (see issue #22335). */ |
| 185 | size_t alloc = (size_t) obj->ob_alloc; |
| 186 | size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes); |
| 187 | size_t size = (size_t) requested_size; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 188 | |
| 189 | assert(self != NULL); |
| 190 | assert(PyByteArray_Check(self)); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 191 | assert(logical_offset <= alloc); |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 192 | assert(requested_size >= 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 193 | |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 194 | if (requested_size == Py_SIZE(self)) { |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 195 | return 0; |
| 196 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 197 | if (!_canresize(obj)) { |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 198 | return -1; |
| 199 | } |
| 200 | |
Antoine Pitrou | 2545411 | 2015-05-19 20:52:27 +0200 | [diff] [blame] | 201 | if (size + logical_offset + 1 <= alloc) { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 202 | /* Current buffer is large enough to host the requested size, |
| 203 | decide on a strategy. */ |
| 204 | if (size < alloc / 2) { |
| 205 | /* Major downsize; resize down to exact size */ |
| 206 | alloc = size + 1; |
| 207 | } |
| 208 | else { |
| 209 | /* Minor downsize; quick exit */ |
| 210 | Py_SIZE(self) = size; |
| 211 | PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */ |
| 212 | return 0; |
| 213 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 214 | } |
| 215 | else { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 216 | /* Need growing, decide on a strategy */ |
| 217 | if (size <= alloc * 1.125) { |
| 218 | /* Moderate upsize; overallocate similar to list_resize() */ |
| 219 | alloc = size + (size >> 3) + (size < 9 ? 3 : 6); |
| 220 | } |
| 221 | else { |
| 222 | /* Major upsize; resize up to exact size */ |
| 223 | alloc = size + 1; |
| 224 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 225 | } |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 226 | if (alloc > PY_SSIZE_T_MAX) { |
| 227 | PyErr_NoMemory(); |
| 228 | return -1; |
| 229 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 230 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 231 | if (logical_offset > 0) { |
| 232 | sval = PyObject_Malloc(alloc); |
| 233 | if (sval == NULL) { |
| 234 | PyErr_NoMemory(); |
| 235 | return -1; |
| 236 | } |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 237 | memcpy(sval, PyByteArray_AS_STRING(self), |
Stefan Krah | dce6502 | 2017-08-25 20:12:05 +0200 | [diff] [blame] | 238 | Py_MIN((size_t)requested_size, (size_t)Py_SIZE(self))); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 239 | PyObject_Free(obj->ob_bytes); |
| 240 | } |
| 241 | else { |
| 242 | sval = PyObject_Realloc(obj->ob_bytes, alloc); |
| 243 | if (sval == NULL) { |
| 244 | PyErr_NoMemory(); |
| 245 | return -1; |
| 246 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 249 | obj->ob_bytes = obj->ob_start = sval; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 250 | Py_SIZE(self) = size; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 251 | obj->ob_alloc = alloc; |
| 252 | obj->ob_bytes[size] = '\0'; /* Trailing null byte */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | PyObject * |
| 258 | PyByteArray_Concat(PyObject *a, PyObject *b) |
| 259 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 260 | Py_buffer va, vb; |
| 261 | PyByteArrayObject *result = NULL; |
| 262 | |
| 263 | va.len = -1; |
| 264 | vb.len = -1; |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 265 | if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 || |
| 266 | PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 267 | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
Serhiy Storchaka | 6b5a9ec | 2017-03-19 19:47:02 +0200 | [diff] [blame] | 268 | Py_TYPE(b)->tp_name, Py_TYPE(a)->tp_name); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 269 | goto done; |
| 270 | } |
| 271 | |
Serhiy Storchaka | 06cfb0c | 2016-07-10 20:48:43 +0300 | [diff] [blame] | 272 | if (va.len > PY_SSIZE_T_MAX - vb.len) { |
| 273 | PyErr_NoMemory(); |
| 274 | goto done; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Serhiy Storchaka | 06cfb0c | 2016-07-10 20:48:43 +0300 | [diff] [blame] | 277 | result = (PyByteArrayObject *) \ |
| 278 | PyByteArray_FromStringAndSize(NULL, va.len + vb.len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 279 | if (result != NULL) { |
| 280 | memcpy(result->ob_bytes, va.buf, va.len); |
| 281 | memcpy(result->ob_bytes + va.len, vb.buf, vb.len); |
| 282 | } |
| 283 | |
| 284 | done: |
| 285 | if (va.len != -1) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 286 | PyBuffer_Release(&va); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 287 | if (vb.len != -1) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 288 | PyBuffer_Release(&vb); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 289 | return (PyObject *)result; |
| 290 | } |
| 291 | |
| 292 | /* Functions stuffed into the type object */ |
| 293 | |
| 294 | static Py_ssize_t |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 295 | bytearray_length(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 296 | { |
| 297 | return Py_SIZE(self); |
| 298 | } |
| 299 | |
| 300 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 301 | bytearray_iconcat(PyByteArrayObject *self, PyObject *other) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 302 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 303 | Py_ssize_t size; |
| 304 | Py_buffer vo; |
| 305 | |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 306 | if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 307 | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
| 308 | Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name); |
| 309 | return NULL; |
| 310 | } |
| 311 | |
Serhiy Storchaka | 06cfb0c | 2016-07-10 20:48:43 +0300 | [diff] [blame] | 312 | size = Py_SIZE(self); |
| 313 | if (size > PY_SSIZE_T_MAX - vo.len) { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 314 | PyBuffer_Release(&vo); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 315 | return PyErr_NoMemory(); |
| 316 | } |
Serhiy Storchaka | 06cfb0c | 2016-07-10 20:48:43 +0300 | [diff] [blame] | 317 | if (PyByteArray_Resize((PyObject *)self, size + vo.len) < 0) { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 318 | PyBuffer_Release(&vo); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 319 | return NULL; |
| 320 | } |
Serhiy Storchaka | 06cfb0c | 2016-07-10 20:48:43 +0300 | [diff] [blame] | 321 | memcpy(PyByteArray_AS_STRING(self) + size, vo.buf, vo.len); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 322 | PyBuffer_Release(&vo); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 323 | Py_INCREF(self); |
| 324 | return (PyObject *)self; |
| 325 | } |
| 326 | |
| 327 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 328 | bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 329 | { |
| 330 | PyByteArrayObject *result; |
| 331 | Py_ssize_t mysize; |
| 332 | Py_ssize_t size; |
| 333 | |
| 334 | if (count < 0) |
| 335 | count = 0; |
| 336 | mysize = Py_SIZE(self); |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 337 | if (count > 0 && mysize > PY_SSIZE_T_MAX / count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 338 | return PyErr_NoMemory(); |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 339 | size = mysize * count; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 340 | result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size); |
| 341 | if (result != NULL && size != 0) { |
| 342 | if (mysize == 1) |
| 343 | memset(result->ob_bytes, self->ob_bytes[0], size); |
| 344 | else { |
| 345 | Py_ssize_t i; |
| 346 | for (i = 0; i < count; i++) |
| 347 | memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize); |
| 348 | } |
| 349 | } |
| 350 | return (PyObject *)result; |
| 351 | } |
| 352 | |
| 353 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 354 | bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 355 | { |
| 356 | Py_ssize_t mysize; |
| 357 | Py_ssize_t size; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 358 | char *buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 359 | |
| 360 | if (count < 0) |
| 361 | count = 0; |
| 362 | mysize = Py_SIZE(self); |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 363 | if (count > 0 && mysize > PY_SSIZE_T_MAX / count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 364 | return PyErr_NoMemory(); |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 365 | size = mysize * count; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 366 | if (PyByteArray_Resize((PyObject *)self, size) < 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 367 | return NULL; |
| 368 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 369 | buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 370 | if (mysize == 1) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 371 | memset(buf, buf[0], size); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 372 | else { |
| 373 | Py_ssize_t i; |
| 374 | for (i = 1; i < count; i++) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 375 | memcpy(buf + i*mysize, buf, mysize); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | Py_INCREF(self); |
| 379 | return (PyObject *)self; |
| 380 | } |
| 381 | |
| 382 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 383 | bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 384 | { |
| 385 | if (i < 0) |
| 386 | i += Py_SIZE(self); |
| 387 | if (i < 0 || i >= Py_SIZE(self)) { |
| 388 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 389 | return NULL; |
| 390 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 391 | return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i])); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 395 | bytearray_subscript(PyByteArrayObject *self, PyObject *index) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 396 | { |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 397 | if (PyIndex_Check(index)) { |
| 398 | Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 399 | |
| 400 | if (i == -1 && PyErr_Occurred()) |
| 401 | return NULL; |
| 402 | |
| 403 | if (i < 0) |
| 404 | i += PyByteArray_GET_SIZE(self); |
| 405 | |
| 406 | if (i < 0 || i >= Py_SIZE(self)) { |
| 407 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 408 | return NULL; |
| 409 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 410 | return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i])); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 411 | } |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 412 | else if (PySlice_Check(index)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 413 | Py_ssize_t start, stop, step, slicelength, cur, i; |
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 | c96be81 | 2019-05-14 17:34:56 +0200 | [diff] [blame^] | 1001 | _PyCoreConfig *config = &_PyInterpreterState_GET_UNSAFE()->core_config; |
| 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 | c96be81 | 2019-05-14 17:34:56 +0200 | [diff] [blame^] | 1027 | _PyCoreConfig *config = &_PyInterpreterState_GET_UNSAFE()->core_config; |
| 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 | |
| 1700 | Py_RETURN_NONE; |
| 1701 | } |
| 1702 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1703 | /*[clinic input] |
| 1704 | bytearray.pop |
| 1705 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1706 | index: Py_ssize_t = -1 |
| 1707 | The index from where to remove the item. |
| 1708 | -1 (the default value) means remove the last item. |
| 1709 | / |
| 1710 | |
| 1711 | Remove and return a single item from B. |
| 1712 | |
| 1713 | If no index argument is given, will pop the last item. |
| 1714 | [clinic start generated code]*/ |
| 1715 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1716 | static PyObject * |
| 1717 | bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1718 | /*[clinic end generated code: output=e0ccd401f8021da8 input=3591df2d06c0d237]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1719 | { |
| 1720 | int value; |
| 1721 | Py_ssize_t n = Py_SIZE(self); |
| 1722 | char *buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1723 | |
| 1724 | if (n == 0) { |
Eli Bendersky | 1bc4f19 | 2011-03-04 04:55:25 +0000 | [diff] [blame] | 1725 | PyErr_SetString(PyExc_IndexError, |
| 1726 | "pop from empty bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1727 | return NULL; |
| 1728 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1729 | if (index < 0) |
| 1730 | index += Py_SIZE(self); |
| 1731 | if (index < 0 || index >= Py_SIZE(self)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1732 | PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
| 1733 | return NULL; |
| 1734 | } |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 1735 | if (!_canresize(self)) |
| 1736 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1737 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1738 | buf = PyByteArray_AS_STRING(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1739 | value = buf[index]; |
| 1740 | memmove(buf + index, buf + index + 1, n - index); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1741 | if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) |
| 1742 | return NULL; |
| 1743 | |
Mark Dickinson | 54a3db9 | 2009-09-06 10:19:23 +0000 | [diff] [blame] | 1744 | return PyLong_FromLong((unsigned char)value); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1747 | /*[clinic input] |
| 1748 | bytearray.remove |
| 1749 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1750 | value: bytesvalue |
| 1751 | The value to remove. |
| 1752 | / |
| 1753 | |
| 1754 | Remove the first occurrence of a value in the bytearray. |
| 1755 | [clinic start generated code]*/ |
| 1756 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1757 | static PyObject * |
| 1758 | bytearray_remove_impl(PyByteArrayObject *self, int value) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1759 | /*[clinic end generated code: output=d659e37866709c13 input=121831240cd51ddf]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1760 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1761 | Py_ssize_t where, n = Py_SIZE(self); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1762 | char *buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1763 | |
Serhiy Storchaka | 4b23494 | 2016-05-16 22:24:03 +0300 | [diff] [blame] | 1764 | where = stringlib_find_char(buf, n, value); |
| 1765 | if (where < 0) { |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 1766 | PyErr_SetString(PyExc_ValueError, "value not found in bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1767 | return NULL; |
| 1768 | } |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 1769 | if (!_canresize(self)) |
| 1770 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1771 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1772 | memmove(buf + where, buf + where + 1, n - where); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1773 | if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) |
| 1774 | return NULL; |
| 1775 | |
| 1776 | Py_RETURN_NONE; |
| 1777 | } |
| 1778 | |
| 1779 | /* XXX These two helpers could be optimized if argsize == 1 */ |
| 1780 | |
| 1781 | static Py_ssize_t |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1782 | lstrip_helper(const char *myptr, Py_ssize_t mysize, |
| 1783 | const void *argptr, Py_ssize_t argsize) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1784 | { |
| 1785 | Py_ssize_t i = 0; |
Antoine Pitrou | 5b72075 | 2013-10-05 21:24:10 +0200 | [diff] [blame] | 1786 | while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1787 | i++; |
| 1788 | return i; |
| 1789 | } |
| 1790 | |
| 1791 | static Py_ssize_t |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1792 | rstrip_helper(const char *myptr, Py_ssize_t mysize, |
| 1793 | const void *argptr, Py_ssize_t argsize) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1794 | { |
| 1795 | Py_ssize_t i = mysize - 1; |
Antoine Pitrou | 5b72075 | 2013-10-05 21:24:10 +0200 | [diff] [blame] | 1796 | while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1797 | i--; |
| 1798 | return i + 1; |
| 1799 | } |
| 1800 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1801 | /*[clinic input] |
| 1802 | bytearray.strip |
| 1803 | |
| 1804 | bytes: object = None |
| 1805 | / |
| 1806 | |
| 1807 | Strip leading and trailing bytes contained in the argument. |
| 1808 | |
| 1809 | If the argument is omitted or None, strip leading and trailing ASCII whitespace. |
| 1810 | [clinic start generated code]*/ |
| 1811 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1812 | static PyObject * |
| 1813 | bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1814 | /*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1815 | { |
| 1816 | Py_ssize_t left, right, mysize, byteslen; |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 1817 | char *myptr; |
| 1818 | const char *bytesptr; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1819 | Py_buffer vbytes; |
| 1820 | |
| 1821 | if (bytes == Py_None) { |
| 1822 | bytesptr = "\t\n\r\f\v "; |
| 1823 | byteslen = 6; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1824 | } |
| 1825 | else { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1826 | if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1827 | return NULL; |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 1828 | bytesptr = (const char *) vbytes.buf; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1829 | byteslen = vbytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1830 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1831 | myptr = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1832 | mysize = Py_SIZE(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1833 | left = lstrip_helper(myptr, mysize, bytesptr, byteslen); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1834 | if (left == mysize) |
| 1835 | right = left; |
| 1836 | else |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1837 | right = rstrip_helper(myptr, mysize, bytesptr, byteslen); |
| 1838 | if (bytes != Py_None) |
| 1839 | PyBuffer_Release(&vbytes); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1840 | return PyByteArray_FromStringAndSize(myptr + left, right - left); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1843 | /*[clinic input] |
| 1844 | bytearray.lstrip |
| 1845 | |
| 1846 | bytes: object = None |
| 1847 | / |
| 1848 | |
| 1849 | Strip leading bytes contained in the argument. |
| 1850 | |
| 1851 | If the argument is omitted or None, strip leading ASCII whitespace. |
| 1852 | [clinic start generated code]*/ |
| 1853 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1854 | static PyObject * |
| 1855 | bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1856 | /*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1857 | { |
| 1858 | Py_ssize_t left, right, mysize, byteslen; |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 1859 | char *myptr; |
| 1860 | const char *bytesptr; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1861 | Py_buffer vbytes; |
| 1862 | |
| 1863 | if (bytes == Py_None) { |
| 1864 | bytesptr = "\t\n\r\f\v "; |
| 1865 | byteslen = 6; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1866 | } |
| 1867 | else { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1868 | if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1869 | return NULL; |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 1870 | bytesptr = (const char *) vbytes.buf; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1871 | byteslen = vbytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1872 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1873 | myptr = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1874 | mysize = Py_SIZE(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1875 | left = lstrip_helper(myptr, mysize, bytesptr, byteslen); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1876 | right = mysize; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1877 | if (bytes != Py_None) |
| 1878 | PyBuffer_Release(&vbytes); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1879 | return PyByteArray_FromStringAndSize(myptr + left, right - left); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1882 | /*[clinic input] |
| 1883 | bytearray.rstrip |
| 1884 | |
| 1885 | bytes: object = None |
| 1886 | / |
| 1887 | |
| 1888 | Strip trailing bytes contained in the argument. |
| 1889 | |
| 1890 | If the argument is omitted or None, strip trailing ASCII whitespace. |
| 1891 | [clinic start generated code]*/ |
| 1892 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1893 | static PyObject * |
| 1894 | bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1895 | /*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1896 | { |
| 1897 | Py_ssize_t right, mysize, byteslen; |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 1898 | char *myptr; |
| 1899 | const char *bytesptr; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1900 | Py_buffer vbytes; |
| 1901 | |
| 1902 | if (bytes == Py_None) { |
| 1903 | bytesptr = "\t\n\r\f\v "; |
| 1904 | byteslen = 6; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1905 | } |
| 1906 | else { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1907 | if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1908 | return NULL; |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 1909 | bytesptr = (const char *) vbytes.buf; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1910 | byteslen = vbytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1911 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1912 | myptr = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1913 | mysize = Py_SIZE(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1914 | right = rstrip_helper(myptr, mysize, bytesptr, byteslen); |
| 1915 | if (bytes != Py_None) |
| 1916 | PyBuffer_Release(&vbytes); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1917 | return PyByteArray_FromStringAndSize(myptr, right); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1920 | /*[clinic input] |
| 1921 | bytearray.decode |
| 1922 | |
| 1923 | encoding: str(c_default="NULL") = 'utf-8' |
| 1924 | The encoding with which to decode the bytearray. |
| 1925 | errors: str(c_default="NULL") = 'strict' |
| 1926 | The error handling scheme to use for the handling of decoding errors. |
| 1927 | The default is 'strict' meaning that decoding errors raise a |
| 1928 | UnicodeDecodeError. Other possible values are 'ignore' and 'replace' |
| 1929 | as well as any other name registered with codecs.register_error that |
| 1930 | can handle UnicodeDecodeErrors. |
| 1931 | |
| 1932 | Decode the bytearray using the codec registered for encoding. |
| 1933 | [clinic start generated code]*/ |
| 1934 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1935 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1936 | bytearray_decode_impl(PyByteArrayObject *self, const char *encoding, |
| 1937 | const char *errors) |
| 1938 | /*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1939 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1940 | if (encoding == NULL) |
| 1941 | encoding = PyUnicode_GetDefaultEncoding(); |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1942 | return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | PyDoc_STRVAR(alloc_doc, |
| 1946 | "B.__alloc__() -> int\n\ |
| 1947 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1948 | Return the number of bytes actually allocated."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1949 | |
| 1950 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1951 | bytearray_alloc(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1952 | { |
| 1953 | return PyLong_FromSsize_t(self->ob_alloc); |
| 1954 | } |
| 1955 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1956 | /*[clinic input] |
| 1957 | bytearray.join |
| 1958 | |
| 1959 | iterable_of_bytes: object |
| 1960 | / |
| 1961 | |
| 1962 | Concatenate any number of bytes/bytearray objects. |
| 1963 | |
| 1964 | The bytearray whose method is called is inserted in between each pair. |
| 1965 | |
| 1966 | The result is returned as a new bytearray object. |
| 1967 | [clinic start generated code]*/ |
| 1968 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1969 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1970 | bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1971 | /*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1972 | { |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1973 | return stringlib_bytes_join((PyObject*)self, iterable_of_bytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1976 | /*[clinic input] |
| 1977 | bytearray.splitlines |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1978 | |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 1979 | keepends: bool(accept={int}) = False |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1980 | |
| 1981 | Return a list of the lines in the bytearray, breaking at line boundaries. |
| 1982 | |
| 1983 | Line breaks are not included in the resulting list unless keepends is given and |
| 1984 | true. |
| 1985 | [clinic start generated code]*/ |
| 1986 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1987 | static PyObject * |
| 1988 | bytearray_splitlines_impl(PyByteArrayObject *self, int keepends) |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 1989 | /*[clinic end generated code: output=4223c94b895f6ad9 input=99a27ad959b9cf6b]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1990 | { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1991 | return stringlib_splitlines( |
| 1992 | (PyObject*) self, PyByteArray_AS_STRING(self), |
| 1993 | PyByteArray_GET_SIZE(self), keepends |
| 1994 | ); |
| 1995 | } |
| 1996 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1997 | /*[clinic input] |
| 1998 | @classmethod |
| 1999 | bytearray.fromhex |
| 2000 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2001 | string: unicode |
| 2002 | / |
| 2003 | |
| 2004 | Create a bytearray object from a string of hexadecimal numbers. |
| 2005 | |
| 2006 | Spaces between two numbers are accepted. |
| 2007 | Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef') |
| 2008 | [clinic start generated code]*/ |
| 2009 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2010 | static PyObject * |
Serhiy Storchaka | 0855e70 | 2016-07-01 17:22:31 +0300 | [diff] [blame] | 2011 | bytearray_fromhex_impl(PyTypeObject *type, PyObject *string) |
| 2012 | /*[clinic end generated code: output=8f0f0b6d30fb3ba0 input=f033a16d1fb21f48]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2013 | { |
Serhiy Storchaka | 0855e70 | 2016-07-01 17:22:31 +0300 | [diff] [blame] | 2014 | PyObject *result = _PyBytes_FromHex(string, type == &PyByteArray_Type); |
| 2015 | if (type != &PyByteArray_Type && result != NULL) { |
Victor Stinner | de4ae3d | 2016-12-04 22:59:09 +0100 | [diff] [blame] | 2016 | Py_SETREF(result, PyObject_CallFunctionObjArgs((PyObject *)type, |
| 2017 | result, NULL)); |
Serhiy Storchaka | 0855e70 | 2016-07-01 17:22:31 +0300 | [diff] [blame] | 2018 | } |
| 2019 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
Gregory P. Smith | 8cb6569 | 2015-04-25 23:22:26 +0000 | [diff] [blame] | 2022 | PyDoc_STRVAR(hex__doc__, |
| 2023 | "B.hex() -> string\n\ |
| 2024 | \n\ |
| 2025 | Create a string of hexadecimal numbers from a bytearray object.\n\ |
| 2026 | Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'."); |
| 2027 | |
| 2028 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2029 | bytearray_hex(PyBytesObject *self, PyObject *Py_UNUSED(ignored)) |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2030 | { |
| 2031 | char* argbuf = PyByteArray_AS_STRING(self); |
| 2032 | Py_ssize_t arglen = PyByteArray_GET_SIZE(self); |
| 2033 | return _Py_strhex(argbuf, arglen); |
| 2034 | } |
| 2035 | |
| 2036 | static PyObject * |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2037 | _common_reduce(PyByteArrayObject *self, int proto) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2038 | { |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2039 | PyObject *dict; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2040 | _Py_IDENTIFIER(__dict__); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2041 | char *buf; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2042 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2043 | dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2044 | if (dict == NULL) { |
| 2045 | PyErr_Clear(); |
| 2046 | dict = Py_None; |
| 2047 | Py_INCREF(dict); |
| 2048 | } |
| 2049 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2050 | buf = PyByteArray_AS_STRING(self); |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2051 | if (proto < 3) { |
| 2052 | /* use str based reduction for backwards compatibility with Python 2.x */ |
| 2053 | PyObject *latin1; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2054 | if (Py_SIZE(self)) |
| 2055 | latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL); |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2056 | else |
| 2057 | latin1 = PyUnicode_FromString(""); |
| 2058 | return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict); |
| 2059 | } |
| 2060 | else { |
| 2061 | /* use more efficient byte based reduction */ |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2062 | if (Py_SIZE(self)) { |
| 2063 | 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] | 2064 | } |
| 2065 | else { |
| 2066 | return Py_BuildValue("(O()N)", Py_TYPE(self), dict); |
| 2067 | } |
| 2068 | } |
| 2069 | } |
| 2070 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2071 | /*[clinic input] |
| 2072 | bytearray.__reduce__ as bytearray_reduce |
| 2073 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2074 | Return state information for pickling. |
| 2075 | [clinic start generated code]*/ |
| 2076 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2077 | static PyObject * |
| 2078 | bytearray_reduce_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 2079 | /*[clinic end generated code: output=52bf304086464cab input=44b5737ada62dd3f]*/ |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2080 | { |
| 2081 | return _common_reduce(self, 2); |
| 2082 | } |
| 2083 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2084 | /*[clinic input] |
| 2085 | bytearray.__reduce_ex__ as bytearray_reduce_ex |
| 2086 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2087 | proto: int = 0 |
| 2088 | / |
| 2089 | |
| 2090 | Return state information for pickling. |
| 2091 | [clinic start generated code]*/ |
| 2092 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2093 | static PyObject * |
| 2094 | bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 2095 | /*[clinic end generated code: output=52eac33377197520 input=f129bc1a1aa151ee]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2096 | { |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2097 | return _common_reduce(self, proto); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2098 | } |
| 2099 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2100 | /*[clinic input] |
| 2101 | bytearray.__sizeof__ as bytearray_sizeof |
| 2102 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2103 | Returns the size of the bytearray object in memory, in bytes. |
| 2104 | [clinic start generated code]*/ |
| 2105 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2106 | static PyObject * |
| 2107 | bytearray_sizeof_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 2108 | /*[clinic end generated code: output=738abdd17951c427 input=e27320fd98a4bc5a]*/ |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2109 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 2110 | Py_ssize_t res; |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2111 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 2112 | res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char); |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 2113 | return PyLong_FromSsize_t(res); |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2116 | static PySequenceMethods bytearray_as_sequence = { |
| 2117 | (lenfunc)bytearray_length, /* sq_length */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2118 | (binaryfunc)PyByteArray_Concat, /* sq_concat */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2119 | (ssizeargfunc)bytearray_repeat, /* sq_repeat */ |
| 2120 | (ssizeargfunc)bytearray_getitem, /* sq_item */ |
| 2121 | 0, /* sq_slice */ |
| 2122 | (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */ |
| 2123 | 0, /* sq_ass_slice */ |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2124 | (objobjproc)bytearray_contains, /* sq_contains */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2125 | (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */ |
| 2126 | (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2127 | }; |
| 2128 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2129 | static PyMappingMethods bytearray_as_mapping = { |
| 2130 | (lenfunc)bytearray_length, |
| 2131 | (binaryfunc)bytearray_subscript, |
| 2132 | (objobjargproc)bytearray_ass_subscript, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2133 | }; |
| 2134 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2135 | static PyBufferProcs bytearray_as_buffer = { |
| 2136 | (getbufferproc)bytearray_getbuffer, |
| 2137 | (releasebufferproc)bytearray_releasebuffer, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2138 | }; |
| 2139 | |
| 2140 | static PyMethodDef |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2141 | bytearray_methods[] = { |
| 2142 | {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2143 | BYTEARRAY_REDUCE_METHODDEF |
| 2144 | BYTEARRAY_REDUCE_EX_METHODDEF |
| 2145 | BYTEARRAY_SIZEOF_METHODDEF |
| 2146 | BYTEARRAY_APPEND_METHODDEF |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2147 | {"capitalize", stringlib_capitalize, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2148 | _Py_capitalize__doc__}, |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 2149 | STRINGLIB_CENTER_METHODDEF |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2150 | BYTEARRAY_CLEAR_METHODDEF |
| 2151 | BYTEARRAY_COPY_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2152 | {"count", (PyCFunction)bytearray_count, METH_VARARGS, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2153 | _Py_count__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2154 | BYTEARRAY_DECODE_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2155 | {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2156 | _Py_endswith__doc__}, |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 2157 | STRINGLIB_EXPANDTABS_METHODDEF |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2158 | BYTEARRAY_EXTEND_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2159 | {"find", (PyCFunction)bytearray_find, METH_VARARGS, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2160 | _Py_find__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2161 | BYTEARRAY_FROMHEX_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2162 | {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__}, |
| 2163 | {"index", (PyCFunction)bytearray_index, METH_VARARGS, _Py_index__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2164 | BYTEARRAY_INSERT_METHODDEF |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2165 | {"isalnum", stringlib_isalnum, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2166 | _Py_isalnum__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2167 | {"isalpha", stringlib_isalpha, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2168 | _Py_isalpha__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2169 | {"isascii", stringlib_isascii, METH_NOARGS, |
INADA Naoki | a49ac99 | 2018-01-27 14:06:21 +0900 | [diff] [blame] | 2170 | _Py_isascii__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2171 | {"isdigit", stringlib_isdigit, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2172 | _Py_isdigit__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2173 | {"islower", stringlib_islower, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2174 | _Py_islower__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2175 | {"isspace", stringlib_isspace, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2176 | _Py_isspace__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2177 | {"istitle", stringlib_istitle, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2178 | _Py_istitle__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2179 | {"isupper", stringlib_isupper, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2180 | _Py_isupper__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2181 | BYTEARRAY_JOIN_METHODDEF |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 2182 | STRINGLIB_LJUST_METHODDEF |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2183 | {"lower", stringlib_lower, METH_NOARGS, _Py_lower__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2184 | BYTEARRAY_LSTRIP_METHODDEF |
| 2185 | BYTEARRAY_MAKETRANS_METHODDEF |
| 2186 | BYTEARRAY_PARTITION_METHODDEF |
| 2187 | BYTEARRAY_POP_METHODDEF |
| 2188 | BYTEARRAY_REMOVE_METHODDEF |
| 2189 | BYTEARRAY_REPLACE_METHODDEF |
| 2190 | BYTEARRAY_REVERSE_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2191 | {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__}, |
| 2192 | {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__}, |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 2193 | STRINGLIB_RJUST_METHODDEF |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2194 | BYTEARRAY_RPARTITION_METHODDEF |
| 2195 | BYTEARRAY_RSPLIT_METHODDEF |
| 2196 | BYTEARRAY_RSTRIP_METHODDEF |
| 2197 | BYTEARRAY_SPLIT_METHODDEF |
| 2198 | BYTEARRAY_SPLITLINES_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2199 | {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS , |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2200 | _Py_startswith__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2201 | BYTEARRAY_STRIP_METHODDEF |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2202 | {"swapcase", stringlib_swapcase, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2203 | _Py_swapcase__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2204 | {"title", stringlib_title, METH_NOARGS, _Py_title__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2205 | BYTEARRAY_TRANSLATE_METHODDEF |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2206 | {"upper", stringlib_upper, METH_NOARGS, _Py_upper__doc__}, |
Tal Einat | c929df3 | 2018-07-06 13:17:38 +0300 | [diff] [blame] | 2207 | STRINGLIB_ZFILL_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2208 | {NULL} |
| 2209 | }; |
| 2210 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 2211 | static PyObject * |
| 2212 | bytearray_mod(PyObject *v, PyObject *w) |
| 2213 | { |
| 2214 | if (!PyByteArray_Check(v)) |
| 2215 | Py_RETURN_NOTIMPLEMENTED; |
Berker Peksag | 43de36d | 2016-04-16 01:20:47 +0300 | [diff] [blame] | 2216 | 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] | 2217 | } |
| 2218 | |
| 2219 | static PyNumberMethods bytearray_as_number = { |
| 2220 | 0, /*nb_add*/ |
| 2221 | 0, /*nb_subtract*/ |
| 2222 | 0, /*nb_multiply*/ |
| 2223 | bytearray_mod, /*nb_remainder*/ |
| 2224 | }; |
| 2225 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2226 | PyDoc_STRVAR(bytearray_doc, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2227 | "bytearray(iterable_of_ints) -> bytearray\n\ |
| 2228 | bytearray(string, encoding[, errors]) -> bytearray\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 2229 | bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\ |
| 2230 | bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\ |
| 2231 | bytearray() -> empty bytes array\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2232 | \n\ |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 2233 | Construct a mutable bytearray object from:\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2234 | - an iterable yielding integers in range(256)\n\ |
| 2235 | - a text string encoded using the specified encoding\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 2236 | - a bytes or a buffer object\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2237 | - any object implementing the buffer API.\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 2238 | - an integer"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2239 | |
| 2240 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2241 | static PyObject *bytearray_iter(PyObject *seq); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2242 | |
| 2243 | PyTypeObject PyByteArray_Type = { |
| 2244 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2245 | "bytearray", |
| 2246 | sizeof(PyByteArrayObject), |
| 2247 | 0, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2248 | (destructor)bytearray_dealloc, /* tp_dealloc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2249 | 0, /* tp_print */ |
| 2250 | 0, /* tp_getattr */ |
| 2251 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 2252 | 0, /* tp_reserved */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2253 | (reprfunc)bytearray_repr, /* tp_repr */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 2254 | &bytearray_as_number, /* tp_as_number */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2255 | &bytearray_as_sequence, /* tp_as_sequence */ |
| 2256 | &bytearray_as_mapping, /* tp_as_mapping */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2257 | 0, /* tp_hash */ |
| 2258 | 0, /* tp_call */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2259 | bytearray_str, /* tp_str */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2260 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2261 | 0, /* tp_setattro */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2262 | &bytearray_as_buffer, /* tp_as_buffer */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2263 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2264 | bytearray_doc, /* tp_doc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2265 | 0, /* tp_traverse */ |
| 2266 | 0, /* tp_clear */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2267 | (richcmpfunc)bytearray_richcompare, /* tp_richcompare */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2268 | 0, /* tp_weaklistoffset */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2269 | bytearray_iter, /* tp_iter */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2270 | 0, /* tp_iternext */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2271 | bytearray_methods, /* tp_methods */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2272 | 0, /* tp_members */ |
| 2273 | 0, /* tp_getset */ |
| 2274 | 0, /* tp_base */ |
| 2275 | 0, /* tp_dict */ |
| 2276 | 0, /* tp_descr_get */ |
| 2277 | 0, /* tp_descr_set */ |
| 2278 | 0, /* tp_dictoffset */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2279 | (initproc)bytearray_init, /* tp_init */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2280 | PyType_GenericAlloc, /* tp_alloc */ |
| 2281 | PyType_GenericNew, /* tp_new */ |
| 2282 | PyObject_Del, /* tp_free */ |
| 2283 | }; |
| 2284 | |
| 2285 | /*********************** Bytes Iterator ****************************/ |
| 2286 | |
| 2287 | typedef struct { |
| 2288 | PyObject_HEAD |
| 2289 | Py_ssize_t it_index; |
| 2290 | PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 2291 | } bytesiterobject; |
| 2292 | |
| 2293 | static void |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2294 | bytearrayiter_dealloc(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2295 | { |
| 2296 | _PyObject_GC_UNTRACK(it); |
| 2297 | Py_XDECREF(it->it_seq); |
| 2298 | PyObject_GC_Del(it); |
| 2299 | } |
| 2300 | |
| 2301 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2302 | bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2303 | { |
| 2304 | Py_VISIT(it->it_seq); |
| 2305 | return 0; |
| 2306 | } |
| 2307 | |
| 2308 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2309 | bytearrayiter_next(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2310 | { |
| 2311 | PyByteArrayObject *seq; |
| 2312 | PyObject *item; |
| 2313 | |
| 2314 | assert(it != NULL); |
| 2315 | seq = it->it_seq; |
| 2316 | if (seq == NULL) |
| 2317 | return NULL; |
| 2318 | assert(PyByteArray_Check(seq)); |
| 2319 | |
| 2320 | if (it->it_index < PyByteArray_GET_SIZE(seq)) { |
| 2321 | item = PyLong_FromLong( |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2322 | (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2323 | if (item != NULL) |
| 2324 | ++it->it_index; |
| 2325 | return item; |
| 2326 | } |
| 2327 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2328 | it->it_seq = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 2329 | Py_DECREF(seq); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2330 | return NULL; |
| 2331 | } |
| 2332 | |
| 2333 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2334 | bytearrayiter_length_hint(bytesiterobject *it, PyObject *Py_UNUSED(ignored)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2335 | { |
| 2336 | Py_ssize_t len = 0; |
Serhiy Storchaka | af65872 | 2016-07-03 14:41:36 +0300 | [diff] [blame] | 2337 | if (it->it_seq) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2338 | len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index; |
Serhiy Storchaka | af65872 | 2016-07-03 14:41:36 +0300 | [diff] [blame] | 2339 | if (len < 0) { |
| 2340 | len = 0; |
| 2341 | } |
| 2342 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2343 | return PyLong_FromSsize_t(len); |
| 2344 | } |
| 2345 | |
| 2346 | PyDoc_STRVAR(length_hint_doc, |
| 2347 | "Private method returning an estimate of len(list(it))."); |
| 2348 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2349 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2350 | bytearrayiter_reduce(bytesiterobject *it, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2351 | { |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 2352 | _Py_IDENTIFIER(iter); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2353 | if (it->it_seq != NULL) { |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 2354 | 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] | 2355 | it->it_seq, it->it_index); |
| 2356 | } else { |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 2357 | return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter)); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2358 | } |
| 2359 | } |
| 2360 | |
| 2361 | static PyObject * |
| 2362 | bytearrayiter_setstate(bytesiterobject *it, PyObject *state) |
| 2363 | { |
| 2364 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 2365 | if (index == -1 && PyErr_Occurred()) |
| 2366 | return NULL; |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 2367 | if (it->it_seq != NULL) { |
| 2368 | if (index < 0) |
| 2369 | index = 0; |
| 2370 | else if (index > PyByteArray_GET_SIZE(it->it_seq)) |
| 2371 | index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */ |
| 2372 | it->it_index = index; |
| 2373 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2374 | Py_RETURN_NONE; |
| 2375 | } |
| 2376 | |
| 2377 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 2378 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2379 | static PyMethodDef bytearrayiter_methods[] = { |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2380 | {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2381 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2382 | {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2383 | bytearray_reduce__doc__}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2384 | {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O, |
| 2385 | setstate_doc}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2386 | {NULL, NULL} /* sentinel */ |
| 2387 | }; |
| 2388 | |
| 2389 | PyTypeObject PyByteArrayIter_Type = { |
| 2390 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2391 | "bytearray_iterator", /* tp_name */ |
| 2392 | sizeof(bytesiterobject), /* tp_basicsize */ |
| 2393 | 0, /* tp_itemsize */ |
| 2394 | /* methods */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2395 | (destructor)bytearrayiter_dealloc, /* tp_dealloc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2396 | 0, /* tp_print */ |
| 2397 | 0, /* tp_getattr */ |
| 2398 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 2399 | 0, /* tp_reserved */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2400 | 0, /* tp_repr */ |
| 2401 | 0, /* tp_as_number */ |
| 2402 | 0, /* tp_as_sequence */ |
| 2403 | 0, /* tp_as_mapping */ |
| 2404 | 0, /* tp_hash */ |
| 2405 | 0, /* tp_call */ |
| 2406 | 0, /* tp_str */ |
| 2407 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2408 | 0, /* tp_setattro */ |
| 2409 | 0, /* tp_as_buffer */ |
| 2410 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 2411 | 0, /* tp_doc */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2412 | (traverseproc)bytearrayiter_traverse, /* tp_traverse */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2413 | 0, /* tp_clear */ |
| 2414 | 0, /* tp_richcompare */ |
| 2415 | 0, /* tp_weaklistoffset */ |
| 2416 | PyObject_SelfIter, /* tp_iter */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2417 | (iternextfunc)bytearrayiter_next, /* tp_iternext */ |
| 2418 | bytearrayiter_methods, /* tp_methods */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2419 | 0, |
| 2420 | }; |
| 2421 | |
| 2422 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2423 | bytearray_iter(PyObject *seq) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2424 | { |
| 2425 | bytesiterobject *it; |
| 2426 | |
| 2427 | if (!PyByteArray_Check(seq)) { |
| 2428 | PyErr_BadInternalCall(); |
| 2429 | return NULL; |
| 2430 | } |
| 2431 | it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type); |
| 2432 | if (it == NULL) |
| 2433 | return NULL; |
| 2434 | it->it_index = 0; |
| 2435 | Py_INCREF(seq); |
| 2436 | it->it_seq = (PyByteArrayObject *)seq; |
| 2437 | _PyObject_GC_TRACK(it); |
| 2438 | return (PyObject *)it; |
| 2439 | } |