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" |
| 5 | #include "structmember.h" |
| 6 | #include "bytes_methods.h" |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 7 | #include "bytesobject.h" |
Gregory P. Smith | 8cb6569 | 2015-04-25 23:22:26 +0000 | [diff] [blame] | 8 | #include "pystrhex.h" |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 9 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 10 | /*[clinic input] |
| 11 | class bytearray "PyByteArrayObject *" "&PyByteArray_Type" |
| 12 | [clinic start generated code]*/ |
| 13 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/ |
| 14 | |
Antoine Pitrou | fc8d6f4 | 2010-01-17 12:38:54 +0000 | [diff] [blame] | 15 | char _PyByteArray_empty_string[] = ""; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 16 | |
| 17 | void |
| 18 | PyByteArray_Fini(void) |
| 19 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | int |
| 23 | PyByteArray_Init(void) |
| 24 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 25 | return 1; |
| 26 | } |
| 27 | |
| 28 | /* end nullbytes support */ |
| 29 | |
| 30 | /* Helpers */ |
| 31 | |
| 32 | static int |
| 33 | _getbytevalue(PyObject* arg, int *value) |
| 34 | { |
| 35 | long face_value; |
| 36 | |
| 37 | if (PyLong_Check(arg)) { |
| 38 | face_value = PyLong_AsLong(arg); |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 39 | } else { |
| 40 | PyObject *index = PyNumber_Index(arg); |
| 41 | if (index == NULL) { |
| 42 | PyErr_Format(PyExc_TypeError, "an integer is required"); |
Mark Dickinson | 10de93a | 2010-07-09 19:25:48 +0000 | [diff] [blame] | 43 | *value = -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 44 | return 0; |
| 45 | } |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 46 | face_value = PyLong_AsLong(index); |
| 47 | Py_DECREF(index); |
| 48 | } |
| 49 | |
| 50 | if (face_value < 0 || face_value >= 256) { |
| 51 | /* this includes the OverflowError in case the long is too large */ |
| 52 | PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
Mark Dickinson | 10de93a | 2010-07-09 19:25:48 +0000 | [diff] [blame] | 53 | *value = -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | *value = face_value; |
| 58 | return 1; |
| 59 | } |
| 60 | |
| 61 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 62 | bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 63 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 64 | void *ptr; |
| 65 | if (view == NULL) { |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 66 | PyErr_SetString(PyExc_BufferError, |
| 67 | "bytearray_getbuffer: view==NULL argument is obsolete"); |
| 68 | return -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 69 | } |
Antoine Pitrou | fc8d6f4 | 2010-01-17 12:38:54 +0000 | [diff] [blame] | 70 | ptr = (void *) PyByteArray_AS_STRING(obj); |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 71 | /* cannot fail if view != NULL and readonly == 0 */ |
| 72 | (void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags); |
| 73 | obj->ob_exports++; |
| 74 | return 0; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static void |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 78 | bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 79 | { |
| 80 | obj->ob_exports--; |
| 81 | } |
| 82 | |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 83 | static int |
| 84 | _canresize(PyByteArrayObject *self) |
| 85 | { |
| 86 | if (self->ob_exports > 0) { |
| 87 | PyErr_SetString(PyExc_BufferError, |
| 88 | "Existing exports of data: object cannot be re-sized"); |
| 89 | return 0; |
| 90 | } |
| 91 | return 1; |
| 92 | } |
| 93 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 94 | #include "clinic/bytearrayobject.c.h" |
| 95 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 96 | /* Direct API functions */ |
| 97 | |
| 98 | PyObject * |
| 99 | PyByteArray_FromObject(PyObject *input) |
| 100 | { |
| 101 | return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type, |
| 102 | input, NULL); |
| 103 | } |
| 104 | |
| 105 | PyObject * |
| 106 | PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size) |
| 107 | { |
| 108 | PyByteArrayObject *new; |
| 109 | Py_ssize_t alloc; |
| 110 | |
| 111 | if (size < 0) { |
| 112 | PyErr_SetString(PyExc_SystemError, |
| 113 | "Negative size passed to PyByteArray_FromStringAndSize"); |
| 114 | return NULL; |
| 115 | } |
| 116 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 117 | /* Prevent buffer overflow when setting alloc to size+1. */ |
| 118 | if (size == PY_SSIZE_T_MAX) { |
| 119 | return PyErr_NoMemory(); |
| 120 | } |
| 121 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 122 | new = PyObject_New(PyByteArrayObject, &PyByteArray_Type); |
| 123 | if (new == NULL) |
| 124 | return NULL; |
| 125 | |
| 126 | if (size == 0) { |
| 127 | new->ob_bytes = NULL; |
| 128 | alloc = 0; |
| 129 | } |
| 130 | else { |
| 131 | alloc = size + 1; |
Antoine Pitrou | 39aba4f | 2011-11-12 21:15:28 +0100 | [diff] [blame] | 132 | new->ob_bytes = PyObject_Malloc(alloc); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 133 | if (new->ob_bytes == NULL) { |
| 134 | Py_DECREF(new); |
| 135 | return PyErr_NoMemory(); |
| 136 | } |
Antoine Pitrou | fc8d6f4 | 2010-01-17 12:38:54 +0000 | [diff] [blame] | 137 | if (bytes != NULL && size > 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 138 | memcpy(new->ob_bytes, bytes, size); |
| 139 | new->ob_bytes[size] = '\0'; /* Trailing null byte */ |
| 140 | } |
| 141 | Py_SIZE(new) = size; |
| 142 | new->ob_alloc = alloc; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 143 | new->ob_start = new->ob_bytes; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 144 | new->ob_exports = 0; |
| 145 | |
| 146 | return (PyObject *)new; |
| 147 | } |
| 148 | |
| 149 | Py_ssize_t |
| 150 | PyByteArray_Size(PyObject *self) |
| 151 | { |
| 152 | assert(self != NULL); |
| 153 | assert(PyByteArray_Check(self)); |
| 154 | |
| 155 | return PyByteArray_GET_SIZE(self); |
| 156 | } |
| 157 | |
| 158 | char * |
| 159 | PyByteArray_AsString(PyObject *self) |
| 160 | { |
| 161 | assert(self != NULL); |
| 162 | assert(PyByteArray_Check(self)); |
| 163 | |
| 164 | return PyByteArray_AS_STRING(self); |
| 165 | } |
| 166 | |
| 167 | int |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 168 | PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 169 | { |
| 170 | void *sval; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 171 | PyByteArrayObject *obj = ((PyByteArrayObject *)self); |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 172 | /* All computations are done unsigned to avoid integer overflows |
| 173 | (see issue #22335). */ |
| 174 | size_t alloc = (size_t) obj->ob_alloc; |
| 175 | size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes); |
| 176 | size_t size = (size_t) requested_size; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 177 | |
| 178 | assert(self != NULL); |
| 179 | assert(PyByteArray_Check(self)); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 180 | assert(logical_offset <= alloc); |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 181 | assert(requested_size >= 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 182 | |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 183 | if (requested_size == Py_SIZE(self)) { |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 184 | return 0; |
| 185 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 186 | if (!_canresize(obj)) { |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 187 | return -1; |
| 188 | } |
| 189 | |
Antoine Pitrou | 2545411 | 2015-05-19 20:52:27 +0200 | [diff] [blame] | 190 | if (size + logical_offset + 1 <= alloc) { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 191 | /* Current buffer is large enough to host the requested size, |
| 192 | decide on a strategy. */ |
| 193 | if (size < alloc / 2) { |
| 194 | /* Major downsize; resize down to exact size */ |
| 195 | alloc = size + 1; |
| 196 | } |
| 197 | else { |
| 198 | /* Minor downsize; quick exit */ |
| 199 | Py_SIZE(self) = size; |
| 200 | PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */ |
| 201 | return 0; |
| 202 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 203 | } |
| 204 | else { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 205 | /* Need growing, decide on a strategy */ |
| 206 | if (size <= alloc * 1.125) { |
| 207 | /* Moderate upsize; overallocate similar to list_resize() */ |
| 208 | alloc = size + (size >> 3) + (size < 9 ? 3 : 6); |
| 209 | } |
| 210 | else { |
| 211 | /* Major upsize; resize up to exact size */ |
| 212 | alloc = size + 1; |
| 213 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 214 | } |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 215 | if (alloc > PY_SSIZE_T_MAX) { |
| 216 | PyErr_NoMemory(); |
| 217 | return -1; |
| 218 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 219 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 220 | if (logical_offset > 0) { |
| 221 | sval = PyObject_Malloc(alloc); |
| 222 | if (sval == NULL) { |
| 223 | PyErr_NoMemory(); |
| 224 | return -1; |
| 225 | } |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 226 | memcpy(sval, PyByteArray_AS_STRING(self), |
| 227 | Py_MIN(requested_size, Py_SIZE(self))); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 228 | PyObject_Free(obj->ob_bytes); |
| 229 | } |
| 230 | else { |
| 231 | sval = PyObject_Realloc(obj->ob_bytes, alloc); |
| 232 | if (sval == NULL) { |
| 233 | PyErr_NoMemory(); |
| 234 | return -1; |
| 235 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 238 | obj->ob_bytes = obj->ob_start = sval; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 239 | Py_SIZE(self) = size; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 240 | obj->ob_alloc = alloc; |
| 241 | obj->ob_bytes[size] = '\0'; /* Trailing null byte */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | PyObject * |
| 247 | PyByteArray_Concat(PyObject *a, PyObject *b) |
| 248 | { |
| 249 | Py_ssize_t size; |
| 250 | Py_buffer va, vb; |
| 251 | PyByteArrayObject *result = NULL; |
| 252 | |
| 253 | va.len = -1; |
| 254 | vb.len = -1; |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 255 | if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 || |
| 256 | PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 257 | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
| 258 | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
| 259 | goto done; |
| 260 | } |
| 261 | |
| 262 | size = va.len + vb.len; |
| 263 | if (size < 0) { |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 264 | PyErr_NoMemory(); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 265 | goto done; |
| 266 | } |
| 267 | |
| 268 | result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, size); |
| 269 | if (result != NULL) { |
| 270 | memcpy(result->ob_bytes, va.buf, va.len); |
| 271 | memcpy(result->ob_bytes + va.len, vb.buf, vb.len); |
| 272 | } |
| 273 | |
| 274 | done: |
| 275 | if (va.len != -1) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 276 | PyBuffer_Release(&va); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 277 | if (vb.len != -1) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 278 | PyBuffer_Release(&vb); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 279 | return (PyObject *)result; |
| 280 | } |
| 281 | |
| 282 | /* Functions stuffed into the type object */ |
| 283 | |
| 284 | static Py_ssize_t |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 285 | bytearray_length(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 286 | { |
| 287 | return Py_SIZE(self); |
| 288 | } |
| 289 | |
| 290 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 291 | bytearray_iconcat(PyByteArrayObject *self, PyObject *other) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 292 | { |
| 293 | Py_ssize_t mysize; |
| 294 | Py_ssize_t size; |
| 295 | Py_buffer vo; |
| 296 | |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 297 | if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 298 | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
| 299 | Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name); |
| 300 | return NULL; |
| 301 | } |
| 302 | |
| 303 | mysize = Py_SIZE(self); |
| 304 | size = mysize + vo.len; |
| 305 | if (size < 0) { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 306 | PyBuffer_Release(&vo); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 307 | return PyErr_NoMemory(); |
| 308 | } |
Antoine Pitrou | 2545411 | 2015-05-19 20:52:27 +0200 | [diff] [blame] | 309 | if (PyByteArray_Resize((PyObject *)self, size) < 0) { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 310 | PyBuffer_Release(&vo); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 311 | return NULL; |
| 312 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 313 | memcpy(PyByteArray_AS_STRING(self) + mysize, vo.buf, 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 | Py_INCREF(self); |
| 316 | return (PyObject *)self; |
| 317 | } |
| 318 | |
| 319 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 320 | bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 321 | { |
| 322 | PyByteArrayObject *result; |
| 323 | Py_ssize_t mysize; |
| 324 | Py_ssize_t size; |
| 325 | |
| 326 | if (count < 0) |
| 327 | count = 0; |
| 328 | mysize = Py_SIZE(self); |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 329 | if (count > 0 && mysize > PY_SSIZE_T_MAX / count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 330 | return PyErr_NoMemory(); |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 331 | size = mysize * count; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 332 | result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size); |
| 333 | if (result != NULL && size != 0) { |
| 334 | if (mysize == 1) |
| 335 | memset(result->ob_bytes, self->ob_bytes[0], size); |
| 336 | else { |
| 337 | Py_ssize_t i; |
| 338 | for (i = 0; i < count; i++) |
| 339 | memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize); |
| 340 | } |
| 341 | } |
| 342 | return (PyObject *)result; |
| 343 | } |
| 344 | |
| 345 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 346 | bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 347 | { |
| 348 | Py_ssize_t mysize; |
| 349 | Py_ssize_t size; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 350 | char *buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 351 | |
| 352 | if (count < 0) |
| 353 | count = 0; |
| 354 | mysize = Py_SIZE(self); |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 355 | if (count > 0 && mysize > PY_SSIZE_T_MAX / count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 356 | return PyErr_NoMemory(); |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 357 | size = mysize * count; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 358 | if (PyByteArray_Resize((PyObject *)self, size) < 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 359 | return NULL; |
| 360 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 361 | buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 362 | if (mysize == 1) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 363 | memset(buf, buf[0], size); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 364 | else { |
| 365 | Py_ssize_t i; |
| 366 | for (i = 1; i < count; i++) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 367 | memcpy(buf + i*mysize, buf, mysize); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | Py_INCREF(self); |
| 371 | return (PyObject *)self; |
| 372 | } |
| 373 | |
| 374 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 375 | bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 376 | { |
| 377 | if (i < 0) |
| 378 | i += Py_SIZE(self); |
| 379 | if (i < 0 || i >= Py_SIZE(self)) { |
| 380 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 381 | return NULL; |
| 382 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 383 | return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i])); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 387 | bytearray_subscript(PyByteArrayObject *self, PyObject *index) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 388 | { |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 389 | if (PyIndex_Check(index)) { |
| 390 | Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 391 | |
| 392 | if (i == -1 && PyErr_Occurred()) |
| 393 | return NULL; |
| 394 | |
| 395 | if (i < 0) |
| 396 | i += PyByteArray_GET_SIZE(self); |
| 397 | |
| 398 | if (i < 0 || i >= Py_SIZE(self)) { |
| 399 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 400 | return NULL; |
| 401 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 402 | return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i])); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 403 | } |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 404 | else if (PySlice_Check(index)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 405 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 406 | if (PySlice_GetIndicesEx(index, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 407 | PyByteArray_GET_SIZE(self), |
| 408 | &start, &stop, &step, &slicelength) < 0) { |
| 409 | return NULL; |
| 410 | } |
| 411 | |
| 412 | if (slicelength <= 0) |
| 413 | return PyByteArray_FromStringAndSize("", 0); |
| 414 | else if (step == 1) { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 415 | return PyByteArray_FromStringAndSize( |
| 416 | PyByteArray_AS_STRING(self) + start, slicelength); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 417 | } |
| 418 | else { |
| 419 | char *source_buf = PyByteArray_AS_STRING(self); |
Alexandre Vassalotti | e2641f4 | 2009-04-03 06:38:02 +0000 | [diff] [blame] | 420 | char *result_buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 421 | PyObject *result; |
| 422 | |
Alexandre Vassalotti | e2641f4 | 2009-04-03 06:38:02 +0000 | [diff] [blame] | 423 | result = PyByteArray_FromStringAndSize(NULL, slicelength); |
| 424 | if (result == NULL) |
| 425 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 426 | |
Alexandre Vassalotti | e2641f4 | 2009-04-03 06:38:02 +0000 | [diff] [blame] | 427 | result_buf = PyByteArray_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 428 | for (cur = start, i = 0; i < slicelength; |
| 429 | cur += step, i++) { |
| 430 | result_buf[i] = source_buf[cur]; |
| 431 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 432 | return result; |
| 433 | } |
| 434 | } |
| 435 | else { |
Terry Jan Reedy | ffff144 | 2014-08-02 01:30:37 -0400 | [diff] [blame] | 436 | PyErr_Format(PyExc_TypeError, |
| 437 | "bytearray indices must be integers or slices, not %.200s", |
| 438 | Py_TYPE(index)->tp_name); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 439 | return NULL; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | static int |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 444 | bytearray_setslice_linear(PyByteArrayObject *self, |
| 445 | Py_ssize_t lo, Py_ssize_t hi, |
| 446 | char *bytes, Py_ssize_t bytes_len) |
| 447 | { |
| 448 | Py_ssize_t avail = hi - lo; |
| 449 | char *buf = PyByteArray_AS_STRING(self); |
| 450 | Py_ssize_t growth = bytes_len - avail; |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 451 | int res = 0; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 452 | assert(avail >= 0); |
| 453 | |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 454 | if (growth < 0) { |
| 455 | if (!_canresize(self)) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 456 | return -1; |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 457 | |
| 458 | if (lo == 0) { |
| 459 | /* Shrink the buffer by advancing its logical start */ |
| 460 | self->ob_start -= growth; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 461 | /* |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 462 | 0 lo hi old_size |
| 463 | | |<----avail----->|<-----tail------>| |
| 464 | | |<-bytes_len->|<-----tail------>| |
| 465 | 0 new_lo new_hi new_size |
| 466 | */ |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 467 | } |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 468 | else { |
| 469 | /* |
| 470 | 0 lo hi old_size |
| 471 | | |<----avail----->|<-----tomove------>| |
| 472 | | |<-bytes_len->|<-----tomove------>| |
| 473 | 0 lo new_hi new_size |
| 474 | */ |
| 475 | memmove(buf + lo + bytes_len, buf + hi, |
| 476 | Py_SIZE(self) - hi); |
| 477 | } |
| 478 | if (PyByteArray_Resize((PyObject *)self, |
| 479 | Py_SIZE(self) + growth) < 0) { |
| 480 | /* Issue #19578: Handling the memory allocation failure here is |
| 481 | tricky here because the bytearray object has already been |
| 482 | modified. Depending on growth and lo, the behaviour is |
| 483 | different. |
| 484 | |
| 485 | If growth < 0 and lo != 0, the operation is completed, but a |
| 486 | MemoryError is still raised and the memory block is not |
| 487 | shrinked. Otherwise, the bytearray is restored in its previous |
| 488 | state and a MemoryError is raised. */ |
| 489 | if (lo == 0) { |
| 490 | self->ob_start += growth; |
| 491 | return -1; |
| 492 | } |
| 493 | /* memmove() removed bytes, the bytearray object cannot be |
| 494 | restored in its previous state. */ |
| 495 | Py_SIZE(self) += growth; |
| 496 | res = -1; |
| 497 | } |
| 498 | buf = PyByteArray_AS_STRING(self); |
| 499 | } |
| 500 | else if (growth > 0) { |
| 501 | if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) { |
| 502 | PyErr_NoMemory(); |
| 503 | return -1; |
| 504 | } |
| 505 | |
| 506 | if (PyByteArray_Resize((PyObject *)self, |
| 507 | Py_SIZE(self) + growth) < 0) { |
| 508 | return -1; |
| 509 | } |
| 510 | buf = PyByteArray_AS_STRING(self); |
| 511 | /* Make the place for the additional bytes */ |
| 512 | /* |
| 513 | 0 lo hi old_size |
| 514 | | |<-avail->|<-----tomove------>| |
| 515 | | |<---bytes_len-->|<-----tomove------>| |
| 516 | 0 lo new_hi new_size |
| 517 | */ |
| 518 | memmove(buf + lo + bytes_len, buf + hi, |
| 519 | Py_SIZE(self) - lo - bytes_len); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | if (bytes_len > 0) |
| 523 | memcpy(buf + lo, bytes, bytes_len); |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 524 | return res; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 528 | bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 529 | PyObject *values) |
| 530 | { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 531 | Py_ssize_t needed; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 532 | void *bytes; |
| 533 | Py_buffer vbytes; |
| 534 | int res = 0; |
| 535 | |
| 536 | vbytes.len = -1; |
| 537 | if (values == (PyObject *)self) { |
| 538 | /* Make a copy and call this function recursively */ |
| 539 | int err; |
| 540 | values = PyByteArray_FromObject(values); |
| 541 | if (values == NULL) |
| 542 | return -1; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 543 | err = bytearray_setslice(self, lo, hi, values); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 544 | Py_DECREF(values); |
| 545 | return err; |
| 546 | } |
| 547 | if (values == NULL) { |
| 548 | /* del b[lo:hi] */ |
| 549 | bytes = NULL; |
| 550 | needed = 0; |
| 551 | } |
| 552 | else { |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 553 | if (PyObject_GetBuffer(values, &vbytes, PyBUF_SIMPLE) != 0) { |
| 554 | PyErr_Format(PyExc_TypeError, |
| 555 | "can't set bytearray slice from %.100s", |
| 556 | Py_TYPE(values)->tp_name); |
| 557 | return -1; |
| 558 | } |
| 559 | needed = vbytes.len; |
| 560 | bytes = vbytes.buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | if (lo < 0) |
| 564 | lo = 0; |
| 565 | if (hi < lo) |
| 566 | hi = lo; |
| 567 | if (hi > Py_SIZE(self)) |
| 568 | hi = Py_SIZE(self); |
| 569 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 570 | res = bytearray_setslice_linear(self, lo, hi, bytes, needed); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 571 | if (vbytes.len != -1) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 572 | PyBuffer_Release(&vbytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 573 | return res; |
| 574 | } |
| 575 | |
| 576 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 577 | bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 578 | { |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 579 | int ival; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 580 | |
| 581 | if (i < 0) |
| 582 | i += Py_SIZE(self); |
| 583 | |
| 584 | if (i < 0 || i >= Py_SIZE(self)) { |
| 585 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 586 | return -1; |
| 587 | } |
| 588 | |
| 589 | if (value == NULL) |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 590 | return bytearray_setslice(self, i, i+1, NULL); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 591 | |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 592 | if (!_getbytevalue(value, &ival)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 593 | return -1; |
| 594 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 595 | PyByteArray_AS_STRING(self)[i] = ival; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 600 | bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 601 | { |
| 602 | Py_ssize_t start, stop, step, slicelen, needed; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 603 | char *buf, *bytes; |
| 604 | buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 605 | |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 606 | if (PyIndex_Check(index)) { |
| 607 | Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 608 | |
| 609 | if (i == -1 && PyErr_Occurred()) |
| 610 | return -1; |
| 611 | |
| 612 | if (i < 0) |
| 613 | i += PyByteArray_GET_SIZE(self); |
| 614 | |
| 615 | if (i < 0 || i >= Py_SIZE(self)) { |
| 616 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 617 | return -1; |
| 618 | } |
| 619 | |
| 620 | if (values == NULL) { |
| 621 | /* Fall through to slice assignment */ |
| 622 | start = i; |
| 623 | stop = i + 1; |
| 624 | step = 1; |
| 625 | slicelen = 1; |
| 626 | } |
| 627 | else { |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 628 | int ival; |
| 629 | if (!_getbytevalue(values, &ival)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 630 | return -1; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 631 | buf[i] = (char)ival; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 632 | return 0; |
| 633 | } |
| 634 | } |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 635 | else if (PySlice_Check(index)) { |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 636 | if (PySlice_GetIndicesEx(index, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 637 | PyByteArray_GET_SIZE(self), |
| 638 | &start, &stop, &step, &slicelen) < 0) { |
| 639 | return -1; |
| 640 | } |
| 641 | } |
| 642 | else { |
Terry Jan Reedy | ffff144 | 2014-08-02 01:30:37 -0400 | [diff] [blame] | 643 | PyErr_Format(PyExc_TypeError, |
| 644 | "bytearray indices must be integers or slices, not %.200s", |
| 645 | Py_TYPE(index)->tp_name); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 646 | return -1; |
| 647 | } |
| 648 | |
| 649 | if (values == NULL) { |
| 650 | bytes = NULL; |
| 651 | needed = 0; |
| 652 | } |
| 653 | else if (values == (PyObject *)self || !PyByteArray_Check(values)) { |
Christian Heimes | 6d26ade | 2012-11-03 23:07:59 +0100 | [diff] [blame] | 654 | int err; |
Ezio Melotti | c64bcbe | 2012-11-03 21:19:06 +0200 | [diff] [blame] | 655 | if (PyNumber_Check(values) || PyUnicode_Check(values)) { |
| 656 | PyErr_SetString(PyExc_TypeError, |
| 657 | "can assign only bytes, buffers, or iterables " |
| 658 | "of ints in range(0, 256)"); |
| 659 | return -1; |
| 660 | } |
Georg Brandl | f3fa568 | 2010-12-04 17:09:30 +0000 | [diff] [blame] | 661 | /* Make a copy and call this function recursively */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 662 | values = PyByteArray_FromObject(values); |
| 663 | if (values == NULL) |
| 664 | return -1; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 665 | err = bytearray_ass_subscript(self, index, values); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 666 | Py_DECREF(values); |
| 667 | return err; |
| 668 | } |
| 669 | else { |
| 670 | assert(PyByteArray_Check(values)); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 671 | bytes = PyByteArray_AS_STRING(values); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 672 | needed = Py_SIZE(values); |
| 673 | } |
| 674 | /* Make sure b[5:2] = ... inserts before 5, not before 2. */ |
| 675 | if ((step < 0 && start < stop) || |
| 676 | (step > 0 && start > stop)) |
| 677 | stop = start; |
| 678 | if (step == 1) { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 679 | return bytearray_setslice_linear(self, start, stop, bytes, needed); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 680 | } |
| 681 | else { |
| 682 | if (needed == 0) { |
| 683 | /* Delete slice */ |
Mark Dickinson | bc09964 | 2010-01-29 17:27:24 +0000 | [diff] [blame] | 684 | size_t cur; |
| 685 | Py_ssize_t i; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 686 | |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 687 | if (!_canresize(self)) |
| 688 | return -1; |
Mark Dickinson | 7e3b948 | 2010-08-06 21:33:18 +0000 | [diff] [blame] | 689 | |
| 690 | if (slicelen == 0) |
| 691 | /* Nothing to do here. */ |
| 692 | return 0; |
| 693 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 694 | if (step < 0) { |
| 695 | stop = start + 1; |
| 696 | start = stop + step * (slicelen - 1) - 1; |
| 697 | step = -step; |
| 698 | } |
| 699 | for (cur = start, i = 0; |
| 700 | i < slicelen; cur += step, i++) { |
| 701 | Py_ssize_t lim = step - 1; |
| 702 | |
Mark Dickinson | 66f575b | 2010-02-14 12:53:32 +0000 | [diff] [blame] | 703 | if (cur + step >= (size_t)PyByteArray_GET_SIZE(self)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 704 | lim = PyByteArray_GET_SIZE(self) - cur - 1; |
| 705 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 706 | memmove(buf + cur - i, |
| 707 | buf + cur + 1, lim); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 708 | } |
| 709 | /* Move the tail of the bytes, in one chunk */ |
Mark Dickinson | 7e3b948 | 2010-08-06 21:33:18 +0000 | [diff] [blame] | 710 | cur = start + (size_t)slicelen*step; |
Mark Dickinson | 66f575b | 2010-02-14 12:53:32 +0000 | [diff] [blame] | 711 | if (cur < (size_t)PyByteArray_GET_SIZE(self)) { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 712 | memmove(buf + cur - slicelen, |
| 713 | buf + cur, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 714 | PyByteArray_GET_SIZE(self) - cur); |
| 715 | } |
| 716 | if (PyByteArray_Resize((PyObject *)self, |
| 717 | PyByteArray_GET_SIZE(self) - slicelen) < 0) |
| 718 | return -1; |
| 719 | |
| 720 | return 0; |
| 721 | } |
| 722 | else { |
| 723 | /* Assign slice */ |
Mark Dickinson | 7e3b948 | 2010-08-06 21:33:18 +0000 | [diff] [blame] | 724 | Py_ssize_t i; |
| 725 | size_t cur; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 726 | |
| 727 | if (needed != slicelen) { |
| 728 | PyErr_Format(PyExc_ValueError, |
| 729 | "attempt to assign bytes of size %zd " |
| 730 | "to extended slice of size %zd", |
| 731 | needed, slicelen); |
| 732 | return -1; |
| 733 | } |
| 734 | for (cur = start, i = 0; i < slicelen; cur += step, i++) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 735 | buf[cur] = bytes[i]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 736 | return 0; |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 742 | bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 743 | { |
| 744 | static char *kwlist[] = {"source", "encoding", "errors", 0}; |
| 745 | PyObject *arg = NULL; |
| 746 | const char *encoding = NULL; |
| 747 | const char *errors = NULL; |
| 748 | Py_ssize_t count; |
| 749 | PyObject *it; |
| 750 | PyObject *(*iternext)(PyObject *); |
| 751 | |
| 752 | if (Py_SIZE(self) != 0) { |
| 753 | /* Empty previous contents (yes, do this first of all!) */ |
| 754 | if (PyByteArray_Resize((PyObject *)self, 0) < 0) |
| 755 | return -1; |
| 756 | } |
| 757 | |
| 758 | /* Parse arguments */ |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 759 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 760 | &arg, &encoding, &errors)) |
| 761 | return -1; |
| 762 | |
| 763 | /* Make a quick exit if no first argument */ |
| 764 | if (arg == NULL) { |
| 765 | if (encoding != NULL || errors != NULL) { |
| 766 | PyErr_SetString(PyExc_TypeError, |
| 767 | "encoding or errors without sequence argument"); |
| 768 | return -1; |
| 769 | } |
| 770 | return 0; |
| 771 | } |
| 772 | |
| 773 | if (PyUnicode_Check(arg)) { |
| 774 | /* Encode via the codec registry */ |
| 775 | PyObject *encoded, *new; |
| 776 | if (encoding == NULL) { |
| 777 | PyErr_SetString(PyExc_TypeError, |
| 778 | "string argument without an encoding"); |
| 779 | return -1; |
| 780 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 781 | encoded = PyUnicode_AsEncodedString(arg, encoding, errors); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 782 | if (encoded == NULL) |
| 783 | return -1; |
| 784 | assert(PyBytes_Check(encoded)); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 785 | new = bytearray_iconcat(self, encoded); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 786 | Py_DECREF(encoded); |
| 787 | if (new == NULL) |
| 788 | return -1; |
| 789 | Py_DECREF(new); |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | /* If it's not unicode, there can't be encoding or errors */ |
| 794 | if (encoding != NULL || errors != NULL) { |
| 795 | PyErr_SetString(PyExc_TypeError, |
| 796 | "encoding or errors without a string argument"); |
| 797 | return -1; |
| 798 | } |
| 799 | |
| 800 | /* Is it an int? */ |
Benjamin Peterson | 8380dd5 | 2010-04-16 22:51:37 +0000 | [diff] [blame] | 801 | count = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
| 802 | if (count == -1 && PyErr_Occurred()) { |
| 803 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 804 | return -1; |
Benjamin Peterson | 9c0e94f | 2010-04-16 23:00:53 +0000 | [diff] [blame] | 805 | PyErr_Clear(); |
Benjamin Peterson | 8380dd5 | 2010-04-16 22:51:37 +0000 | [diff] [blame] | 806 | } |
| 807 | else if (count < 0) { |
| 808 | PyErr_SetString(PyExc_ValueError, "negative count"); |
| 809 | return -1; |
| 810 | } |
| 811 | else { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 812 | if (count > 0) { |
Victor Stinner | 2bc4d95 | 2014-06-02 22:22:42 +0200 | [diff] [blame] | 813 | if (PyByteArray_Resize((PyObject *)self, count)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 814 | return -1; |
Victor Stinner | 2bc4d95 | 2014-06-02 22:22:42 +0200 | [diff] [blame] | 815 | memset(PyByteArray_AS_STRING(self), 0, count); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 816 | } |
| 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | /* Use the buffer API */ |
| 821 | if (PyObject_CheckBuffer(arg)) { |
| 822 | Py_ssize_t size; |
| 823 | Py_buffer view; |
| 824 | if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0) |
| 825 | return -1; |
| 826 | size = view.len; |
| 827 | if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 828 | if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self), |
| 829 | &view, size, 'C') < 0) |
Stefan Krah | 7d12d9d | 2012-07-28 12:25:55 +0200 | [diff] [blame] | 830 | goto fail; |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 831 | PyBuffer_Release(&view); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 832 | return 0; |
| 833 | fail: |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 834 | PyBuffer_Release(&view); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 835 | return -1; |
| 836 | } |
| 837 | |
| 838 | /* XXX Optimize this if the arguments is a list, tuple */ |
| 839 | |
| 840 | /* Get the iterator */ |
| 841 | it = PyObject_GetIter(arg); |
| 842 | if (it == NULL) |
| 843 | return -1; |
| 844 | iternext = *Py_TYPE(it)->tp_iternext; |
| 845 | |
| 846 | /* Run the iterator to exhaustion */ |
| 847 | for (;;) { |
| 848 | PyObject *item; |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 849 | int rc, value; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 850 | |
| 851 | /* Get the next item */ |
| 852 | item = iternext(it); |
| 853 | if (item == NULL) { |
| 854 | if (PyErr_Occurred()) { |
| 855 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 856 | goto error; |
| 857 | PyErr_Clear(); |
| 858 | } |
| 859 | break; |
| 860 | } |
| 861 | |
| 862 | /* Interpret it as an int (__index__) */ |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 863 | rc = _getbytevalue(item, &value); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 864 | Py_DECREF(item); |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 865 | if (!rc) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 866 | goto error; |
| 867 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 868 | /* Append the byte */ |
Serhiy Storchaka | 7b6e3b9 | 2015-06-29 21:14:06 +0300 | [diff] [blame] | 869 | if (Py_SIZE(self) + 1 < self->ob_alloc) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 870 | Py_SIZE(self)++; |
Serhiy Storchaka | 7b6e3b9 | 2015-06-29 21:14:06 +0300 | [diff] [blame] | 871 | PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0'; |
| 872 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 873 | else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0) |
| 874 | goto error; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 875 | PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | /* Clean up and return success */ |
| 879 | Py_DECREF(it); |
| 880 | return 0; |
| 881 | |
| 882 | error: |
| 883 | /* Error handling when it != NULL */ |
| 884 | Py_DECREF(it); |
| 885 | return -1; |
| 886 | } |
| 887 | |
| 888 | /* Mostly copied from string_repr, but without the |
| 889 | "smart quote" functionality. */ |
| 890 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 891 | bytearray_repr(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 892 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 893 | const char *quote_prefix = "bytearray(b"; |
| 894 | const char *quote_postfix = ")"; |
| 895 | Py_ssize_t length = Py_SIZE(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 896 | /* 15 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */ |
Mark Dickinson | 66f575b | 2010-02-14 12:53:32 +0000 | [diff] [blame] | 897 | size_t newsize; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 898 | PyObject *v; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 899 | Py_ssize_t i; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 900 | char *bytes; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 901 | char c; |
| 902 | char *p; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 903 | int quote; |
| 904 | char *test, *start; |
| 905 | char *buffer; |
| 906 | |
| 907 | if (length > (PY_SSIZE_T_MAX - 15) / 4) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 908 | PyErr_SetString(PyExc_OverflowError, |
| 909 | "bytearray object is too large to make repr"); |
| 910 | return NULL; |
| 911 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 912 | |
| 913 | newsize = 15 + length * 4; |
Antoine Pitrou | 39aba4f | 2011-11-12 21:15:28 +0100 | [diff] [blame] | 914 | buffer = PyObject_Malloc(newsize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 915 | if (buffer == NULL) { |
| 916 | PyErr_NoMemory(); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 917 | return NULL; |
| 918 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 919 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 920 | /* Figure out which quote to use; single is preferred */ |
| 921 | quote = '\''; |
| 922 | start = PyByteArray_AS_STRING(self); |
| 923 | for (test = start; test < start+length; ++test) { |
| 924 | if (*test == '"') { |
| 925 | quote = '\''; /* back to single */ |
| 926 | break; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 927 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 928 | else if (*test == '\'') |
| 929 | quote = '"'; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 930 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 931 | |
| 932 | p = buffer; |
| 933 | while (*quote_prefix) |
| 934 | *p++ = *quote_prefix++; |
| 935 | *p++ = quote; |
| 936 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 937 | bytes = PyByteArray_AS_STRING(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 938 | for (i = 0; i < length; i++) { |
| 939 | /* There's at least enough room for a hex escape |
| 940 | and a closing quote. */ |
| 941 | assert(newsize - (p - buffer) >= 5); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 942 | c = bytes[i]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 943 | if (c == '\'' || c == '\\') |
| 944 | *p++ = '\\', *p++ = c; |
| 945 | else if (c == '\t') |
| 946 | *p++ = '\\', *p++ = 't'; |
| 947 | else if (c == '\n') |
| 948 | *p++ = '\\', *p++ = 'n'; |
| 949 | else if (c == '\r') |
| 950 | *p++ = '\\', *p++ = 'r'; |
| 951 | else if (c == 0) |
| 952 | *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0'; |
| 953 | else if (c < ' ' || c >= 0x7f) { |
| 954 | *p++ = '\\'; |
| 955 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 956 | *p++ = Py_hexdigits[(c & 0xf0) >> 4]; |
| 957 | *p++ = Py_hexdigits[c & 0xf]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 958 | } |
| 959 | else |
| 960 | *p++ = c; |
| 961 | } |
| 962 | assert(newsize - (p - buffer) >= 1); |
| 963 | *p++ = quote; |
| 964 | while (*quote_postfix) { |
| 965 | *p++ = *quote_postfix++; |
| 966 | } |
| 967 | |
| 968 | v = PyUnicode_DecodeASCII(buffer, p - buffer, NULL); |
Antoine Pitrou | 39aba4f | 2011-11-12 21:15:28 +0100 | [diff] [blame] | 969 | PyObject_Free(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 970 | return v; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 974 | bytearray_str(PyObject *op) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 975 | { |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 976 | if (Py_BytesWarningFlag) { |
| 977 | if (PyErr_WarnEx(PyExc_BytesWarning, |
| 978 | "str() on a bytearray instance", 1)) |
| 979 | return NULL; |
| 980 | } |
| 981 | return bytearray_repr((PyByteArrayObject*)op); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 985 | bytearray_richcompare(PyObject *self, PyObject *other, int op) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 986 | { |
| 987 | Py_ssize_t self_size, other_size; |
| 988 | Py_buffer self_bytes, other_bytes; |
| 989 | PyObject *res; |
| 990 | Py_ssize_t minsize; |
Serhiy Storchaka | fa494fd | 2015-05-30 17:45:22 +0300 | [diff] [blame] | 991 | int cmp, rc; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 992 | |
| 993 | /* Bytes can be compared to anything that supports the (binary) |
| 994 | buffer API. Except that a comparison with Unicode is always an |
| 995 | error, even if the comparison is for equality. */ |
Serhiy Storchaka | fa494fd | 2015-05-30 17:45:22 +0300 | [diff] [blame] | 996 | rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type); |
| 997 | if (!rc) |
| 998 | rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type); |
| 999 | if (rc < 0) |
| 1000 | return NULL; |
| 1001 | if (rc) { |
Barry Warsaw | 9e9dcd6 | 2008-10-17 01:50:37 +0000 | [diff] [blame] | 1002 | if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1003 | if (PyErr_WarnEx(PyExc_BytesWarning, |
Georg Brandl | e5d68ac | 2008-06-04 11:30:26 +0000 | [diff] [blame] | 1004 | "Comparison between bytearray and string", 1)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1005 | return NULL; |
| 1006 | } |
| 1007 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1008 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1011 | if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1012 | PyErr_Clear(); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1013 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1014 | } |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1015 | self_size = self_bytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1016 | |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1017 | if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1018 | PyErr_Clear(); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1019 | PyBuffer_Release(&self_bytes); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1020 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1021 | } |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1022 | other_size = other_bytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1023 | |
| 1024 | if (self_size != other_size && (op == Py_EQ || op == Py_NE)) { |
| 1025 | /* Shortcut: if the lengths differ, the objects differ */ |
| 1026 | cmp = (op == Py_NE); |
| 1027 | } |
| 1028 | else { |
| 1029 | minsize = self_size; |
| 1030 | if (other_size < minsize) |
| 1031 | minsize = other_size; |
| 1032 | |
| 1033 | cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize); |
| 1034 | /* In ISO C, memcmp() guarantees to use unsigned bytes! */ |
| 1035 | |
| 1036 | if (cmp == 0) { |
| 1037 | if (self_size < other_size) |
| 1038 | cmp = -1; |
| 1039 | else if (self_size > other_size) |
| 1040 | cmp = 1; |
| 1041 | } |
| 1042 | |
| 1043 | switch (op) { |
| 1044 | case Py_LT: cmp = cmp < 0; break; |
| 1045 | case Py_LE: cmp = cmp <= 0; break; |
| 1046 | case Py_EQ: cmp = cmp == 0; break; |
| 1047 | case Py_NE: cmp = cmp != 0; break; |
| 1048 | case Py_GT: cmp = cmp > 0; break; |
| 1049 | case Py_GE: cmp = cmp >= 0; break; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | res = cmp ? Py_True : Py_False; |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1054 | PyBuffer_Release(&self_bytes); |
| 1055 | PyBuffer_Release(&other_bytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1056 | Py_INCREF(res); |
| 1057 | return res; |
| 1058 | } |
| 1059 | |
| 1060 | static void |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1061 | bytearray_dealloc(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1062 | { |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 1063 | if (self->ob_exports > 0) { |
| 1064 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1065 | "deallocated bytearray object has exported buffers"); |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 1066 | PyErr_Print(); |
| 1067 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1068 | if (self->ob_bytes != 0) { |
Antoine Pitrou | 39aba4f | 2011-11-12 21:15:28 +0100 | [diff] [blame] | 1069 | PyObject_Free(self->ob_bytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1070 | } |
| 1071 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 1072 | } |
| 1073 | |
| 1074 | |
| 1075 | /* -------------------------------------------------------------------- */ |
| 1076 | /* Methods */ |
| 1077 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1078 | #define FASTSEARCH fastsearch |
| 1079 | #define STRINGLIB(F) stringlib_##F |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1080 | #define STRINGLIB_CHAR char |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 1081 | #define STRINGLIB_SIZEOF_CHAR 1 |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1082 | #define STRINGLIB_LEN PyByteArray_GET_SIZE |
| 1083 | #define STRINGLIB_STR PyByteArray_AS_STRING |
| 1084 | #define STRINGLIB_NEW PyByteArray_FromStringAndSize |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1085 | #define STRINGLIB_ISSPACE Py_ISSPACE |
| 1086 | #define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r')) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1087 | #define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact |
| 1088 | #define STRINGLIB_MUTABLE 1 |
| 1089 | |
| 1090 | #include "stringlib/fastsearch.h" |
| 1091 | #include "stringlib/count.h" |
| 1092 | #include "stringlib/find.h" |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 1093 | #include "stringlib/join.h" |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1094 | #include "stringlib/partition.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1095 | #include "stringlib/split.h" |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1096 | #include "stringlib/ctype.h" |
| 1097 | #include "stringlib/transmogrify.h" |
| 1098 | |
| 1099 | |
| 1100 | /* The following Py_LOCAL_INLINE and Py_LOCAL functions |
| 1101 | were copied from the old char* style string object. */ |
| 1102 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1103 | /* helper macro to fixup start/end slice values */ |
| 1104 | #define ADJUST_INDICES(start, end, len) \ |
| 1105 | if (end > len) \ |
| 1106 | end = len; \ |
| 1107 | else if (end < 0) { \ |
| 1108 | end += len; \ |
| 1109 | if (end < 0) \ |
| 1110 | end = 0; \ |
| 1111 | } \ |
| 1112 | if (start < 0) { \ |
| 1113 | start += len; \ |
| 1114 | if (start < 0) \ |
| 1115 | start = 0; \ |
| 1116 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1117 | |
| 1118 | Py_LOCAL_INLINE(Py_ssize_t) |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1119 | bytearray_find_internal(PyByteArrayObject *self, PyObject *args, int dir) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1120 | { |
| 1121 | PyObject *subobj; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1122 | char byte; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1123 | Py_buffer subbuf; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1124 | const char *sub; |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 1125 | Py_ssize_t len, sub_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1126 | Py_ssize_t start=0, end=PY_SSIZE_T_MAX; |
| 1127 | Py_ssize_t res; |
| 1128 | |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1129 | if (!stringlib_parse_args_finds_byte("find/rfind/index/rindex", |
| 1130 | args, &subobj, &byte, &start, &end)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1131 | return -2; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1132 | |
| 1133 | if (subobj) { |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1134 | if (PyObject_GetBuffer(subobj, &subbuf, PyBUF_SIMPLE) != 0) |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1135 | return -2; |
| 1136 | |
| 1137 | sub = subbuf.buf; |
| 1138 | sub_len = subbuf.len; |
| 1139 | } |
| 1140 | else { |
| 1141 | sub = &byte; |
| 1142 | sub_len = 1; |
| 1143 | } |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 1144 | len = PyByteArray_GET_SIZE(self); |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1145 | |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 1146 | ADJUST_INDICES(start, end, len); |
| 1147 | if (end - start < sub_len) |
| 1148 | res = -1; |
Serhiy Storchaka | 413fdce | 2015-11-14 15:42:17 +0200 | [diff] [blame] | 1149 | else if (sub_len == 1) { |
| 1150 | if (dir > 0) |
| 1151 | res = stringlib_find_char( |
| 1152 | PyByteArray_AS_STRING(self) + start, end - start, |
| 1153 | *sub); |
| 1154 | else |
| 1155 | res = stringlib_rfind_char( |
| 1156 | PyByteArray_AS_STRING(self) + start, end - start, |
| 1157 | *sub); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 1158 | if (res >= 0) |
| 1159 | res += start; |
| 1160 | } |
| 1161 | else { |
| 1162 | if (dir > 0) |
| 1163 | res = stringlib_find_slice( |
| 1164 | PyByteArray_AS_STRING(self), len, |
| 1165 | sub, sub_len, start, end); |
| 1166 | else |
| 1167 | res = stringlib_rfind_slice( |
| 1168 | PyByteArray_AS_STRING(self), len, |
| 1169 | sub, sub_len, start, end); |
| 1170 | } |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1171 | |
| 1172 | if (subobj) |
| 1173 | PyBuffer_Release(&subbuf); |
| 1174 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1175 | return res; |
| 1176 | } |
| 1177 | |
| 1178 | PyDoc_STRVAR(find__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1179 | "B.find(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1180 | \n\ |
| 1181 | Return the lowest index in B where subsection sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 1182 | such that sub is contained within B[start,end]. Optional\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1183 | arguments start and end are interpreted as in slice notation.\n\ |
| 1184 | \n\ |
| 1185 | Return -1 on failure."); |
| 1186 | |
| 1187 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1188 | bytearray_find(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1189 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1190 | Py_ssize_t result = bytearray_find_internal(self, args, +1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1191 | if (result == -2) |
| 1192 | return NULL; |
| 1193 | return PyLong_FromSsize_t(result); |
| 1194 | } |
| 1195 | |
| 1196 | PyDoc_STRVAR(count__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1197 | "B.count(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1198 | \n\ |
| 1199 | Return the number of non-overlapping occurrences of subsection sub in\n\ |
| 1200 | bytes B[start:end]. Optional arguments start and end are interpreted\n\ |
| 1201 | as in slice notation."); |
| 1202 | |
| 1203 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1204 | bytearray_count(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1205 | { |
| 1206 | PyObject *sub_obj; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1207 | const char *str = PyByteArray_AS_STRING(self), *sub; |
| 1208 | Py_ssize_t sub_len; |
| 1209 | char byte; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1210 | Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1211 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1212 | Py_buffer vsub; |
| 1213 | PyObject *count_obj; |
| 1214 | |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1215 | if (!stringlib_parse_args_finds_byte("count", args, &sub_obj, &byte, |
| 1216 | &start, &end)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1217 | return NULL; |
| 1218 | |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1219 | if (sub_obj) { |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1220 | if (PyObject_GetBuffer(sub_obj, &vsub, PyBUF_SIMPLE) != 0) |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1221 | return NULL; |
| 1222 | |
| 1223 | sub = vsub.buf; |
| 1224 | sub_len = vsub.len; |
| 1225 | } |
| 1226 | else { |
| 1227 | sub = &byte; |
| 1228 | sub_len = 1; |
| 1229 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1230 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1231 | ADJUST_INDICES(start, end, PyByteArray_GET_SIZE(self)); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1232 | |
| 1233 | count_obj = PyLong_FromSsize_t( |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1234 | stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1235 | ); |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1236 | |
| 1237 | if (sub_obj) |
| 1238 | PyBuffer_Release(&vsub); |
| 1239 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1240 | return count_obj; |
| 1241 | } |
| 1242 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1243 | /*[clinic input] |
| 1244 | bytearray.clear |
| 1245 | |
| 1246 | self: self(type="PyByteArrayObject *") |
| 1247 | |
| 1248 | Remove all items from the bytearray. |
| 1249 | [clinic start generated code]*/ |
| 1250 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1251 | static PyObject * |
| 1252 | bytearray_clear_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1253 | /*[clinic end generated code: output=85c2fe6aede0956c input=e524fd330abcdc18]*/ |
Eli Bendersky | 4db28d3 | 2011-03-03 18:21:02 +0000 | [diff] [blame] | 1254 | { |
| 1255 | if (PyByteArray_Resize((PyObject *)self, 0) < 0) |
| 1256 | return NULL; |
| 1257 | Py_RETURN_NONE; |
| 1258 | } |
| 1259 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1260 | /*[clinic input] |
| 1261 | bytearray.copy |
| 1262 | |
| 1263 | self: self(type="PyByteArrayObject *") |
| 1264 | |
| 1265 | Return a copy of B. |
| 1266 | [clinic start generated code]*/ |
| 1267 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1268 | static PyObject * |
| 1269 | bytearray_copy_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1270 | /*[clinic end generated code: output=68cfbcfed484c132 input=6d5d2975aa0f33f3]*/ |
Eli Bendersky | 4db28d3 | 2011-03-03 18:21:02 +0000 | [diff] [blame] | 1271 | { |
| 1272 | return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self), |
| 1273 | PyByteArray_GET_SIZE(self)); |
| 1274 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1275 | |
| 1276 | PyDoc_STRVAR(index__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1277 | "B.index(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1278 | \n\ |
| 1279 | Like B.find() but raise ValueError when the subsection is not found."); |
| 1280 | |
| 1281 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1282 | bytearray_index(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1283 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1284 | Py_ssize_t result = bytearray_find_internal(self, args, +1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1285 | if (result == -2) |
| 1286 | return NULL; |
| 1287 | if (result == -1) { |
| 1288 | PyErr_SetString(PyExc_ValueError, |
| 1289 | "subsection not found"); |
| 1290 | return NULL; |
| 1291 | } |
| 1292 | return PyLong_FromSsize_t(result); |
| 1293 | } |
| 1294 | |
| 1295 | |
| 1296 | PyDoc_STRVAR(rfind__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1297 | "B.rfind(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1298 | \n\ |
| 1299 | Return the highest index in B where subsection sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 1300 | such that sub is contained within B[start,end]. Optional\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1301 | arguments start and end are interpreted as in slice notation.\n\ |
| 1302 | \n\ |
| 1303 | Return -1 on failure."); |
| 1304 | |
| 1305 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1306 | bytearray_rfind(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1307 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1308 | Py_ssize_t result = bytearray_find_internal(self, args, -1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1309 | if (result == -2) |
| 1310 | return NULL; |
| 1311 | return PyLong_FromSsize_t(result); |
| 1312 | } |
| 1313 | |
| 1314 | |
| 1315 | PyDoc_STRVAR(rindex__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1316 | "B.rindex(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1317 | \n\ |
| 1318 | Like B.rfind() but raise ValueError when the subsection is not found."); |
| 1319 | |
| 1320 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1321 | bytearray_rindex(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1322 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1323 | Py_ssize_t result = bytearray_find_internal(self, args, -1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1324 | if (result == -2) |
| 1325 | return NULL; |
| 1326 | if (result == -1) { |
| 1327 | PyErr_SetString(PyExc_ValueError, |
| 1328 | "subsection not found"); |
| 1329 | return NULL; |
| 1330 | } |
| 1331 | return PyLong_FromSsize_t(result); |
| 1332 | } |
| 1333 | |
| 1334 | |
| 1335 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1336 | bytearray_contains(PyObject *self, PyObject *arg) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1337 | { |
| 1338 | Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError); |
| 1339 | if (ival == -1 && PyErr_Occurred()) { |
| 1340 | Py_buffer varg; |
Antoine Pitrou | 0010d37 | 2010-08-15 17:12:55 +0000 | [diff] [blame] | 1341 | Py_ssize_t pos; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1342 | PyErr_Clear(); |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1343 | if (PyObject_GetBuffer(arg, &varg, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1344 | return -1; |
| 1345 | pos = stringlib_find(PyByteArray_AS_STRING(self), Py_SIZE(self), |
| 1346 | varg.buf, varg.len, 0); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1347 | PyBuffer_Release(&varg); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1348 | return pos >= 0; |
| 1349 | } |
| 1350 | if (ival < 0 || ival >= 256) { |
| 1351 | PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
| 1352 | return -1; |
| 1353 | } |
| 1354 | |
Antoine Pitrou | 0010d37 | 2010-08-15 17:12:55 +0000 | [diff] [blame] | 1355 | return memchr(PyByteArray_AS_STRING(self), (int) ival, Py_SIZE(self)) != NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | |
| 1359 | /* Matches the end (direction >= 0) or start (direction < 0) of self |
| 1360 | * against substr, using the start and end arguments. Returns |
| 1361 | * -1 on error, 0 if not found and 1 if found. |
| 1362 | */ |
| 1363 | Py_LOCAL(int) |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1364 | _bytearray_tailmatch(PyByteArrayObject *self, PyObject *substr, Py_ssize_t start, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1365 | Py_ssize_t end, int direction) |
| 1366 | { |
| 1367 | Py_ssize_t len = PyByteArray_GET_SIZE(self); |
| 1368 | const char* str; |
| 1369 | Py_buffer vsubstr; |
| 1370 | int rv = 0; |
| 1371 | |
| 1372 | str = PyByteArray_AS_STRING(self); |
| 1373 | |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1374 | if (PyObject_GetBuffer(substr, &vsubstr, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1375 | return -1; |
| 1376 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1377 | ADJUST_INDICES(start, end, len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1378 | |
| 1379 | if (direction < 0) { |
| 1380 | /* startswith */ |
| 1381 | if (start+vsubstr.len > len) { |
| 1382 | goto done; |
| 1383 | } |
| 1384 | } else { |
| 1385 | /* endswith */ |
| 1386 | if (end-start < vsubstr.len || start > len) { |
| 1387 | goto done; |
| 1388 | } |
| 1389 | |
| 1390 | if (end-vsubstr.len > start) |
| 1391 | start = end - vsubstr.len; |
| 1392 | } |
| 1393 | if (end-start >= vsubstr.len) |
| 1394 | rv = ! memcmp(str+start, vsubstr.buf, vsubstr.len); |
| 1395 | |
| 1396 | done: |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1397 | PyBuffer_Release(&vsubstr); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1398 | return rv; |
| 1399 | } |
| 1400 | |
| 1401 | |
| 1402 | PyDoc_STRVAR(startswith__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1403 | "B.startswith(prefix[, start[, end]]) -> bool\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1404 | \n\ |
| 1405 | Return True if B starts with the specified prefix, False otherwise.\n\ |
| 1406 | With optional start, test B beginning at that position.\n\ |
| 1407 | With optional end, stop comparing B at that position.\n\ |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 1408 | prefix can also be a tuple of bytes to try."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1409 | |
| 1410 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1411 | bytearray_startswith(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1412 | { |
| 1413 | Py_ssize_t start = 0; |
| 1414 | Py_ssize_t end = PY_SSIZE_T_MAX; |
| 1415 | PyObject *subobj; |
| 1416 | int result; |
| 1417 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 1418 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1419 | return NULL; |
| 1420 | if (PyTuple_Check(subobj)) { |
| 1421 | Py_ssize_t i; |
| 1422 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1423 | result = _bytearray_tailmatch(self, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1424 | PyTuple_GET_ITEM(subobj, i), |
| 1425 | start, end, -1); |
| 1426 | if (result == -1) |
| 1427 | return NULL; |
| 1428 | else if (result) { |
| 1429 | Py_RETURN_TRUE; |
| 1430 | } |
| 1431 | } |
| 1432 | Py_RETURN_FALSE; |
| 1433 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1434 | result = _bytearray_tailmatch(self, subobj, start, end, -1); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 1435 | if (result == -1) { |
| 1436 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1437 | PyErr_Format(PyExc_TypeError, "startswith first arg must be bytes " |
| 1438 | "or a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1439 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 1440 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1441 | else |
| 1442 | return PyBool_FromLong(result); |
| 1443 | } |
| 1444 | |
| 1445 | PyDoc_STRVAR(endswith__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1446 | "B.endswith(suffix[, start[, end]]) -> bool\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1447 | \n\ |
| 1448 | Return True if B ends with the specified suffix, False otherwise.\n\ |
| 1449 | With optional start, test B beginning at that position.\n\ |
| 1450 | With optional end, stop comparing B at that position.\n\ |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 1451 | suffix can also be a tuple of bytes to try."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1452 | |
| 1453 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1454 | bytearray_endswith(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1455 | { |
| 1456 | Py_ssize_t start = 0; |
| 1457 | Py_ssize_t end = PY_SSIZE_T_MAX; |
| 1458 | PyObject *subobj; |
| 1459 | int result; |
| 1460 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 1461 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1462 | return NULL; |
| 1463 | if (PyTuple_Check(subobj)) { |
| 1464 | Py_ssize_t i; |
| 1465 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1466 | result = _bytearray_tailmatch(self, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1467 | PyTuple_GET_ITEM(subobj, i), |
| 1468 | start, end, +1); |
| 1469 | if (result == -1) |
| 1470 | return NULL; |
| 1471 | else if (result) { |
| 1472 | Py_RETURN_TRUE; |
| 1473 | } |
| 1474 | } |
| 1475 | Py_RETURN_FALSE; |
| 1476 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1477 | result = _bytearray_tailmatch(self, subobj, start, end, +1); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 1478 | if (result == -1) { |
| 1479 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1480 | PyErr_Format(PyExc_TypeError, "endswith first arg must be bytes or " |
| 1481 | "a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1482 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 1483 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1484 | else |
| 1485 | return PyBool_FromLong(result); |
| 1486 | } |
| 1487 | |
| 1488 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1489 | /*[clinic input] |
| 1490 | bytearray.translate |
| 1491 | |
| 1492 | self: self(type="PyByteArrayObject *") |
| 1493 | table: object |
| 1494 | Translation table, which must be a bytes object of length 256. |
| 1495 | [ |
| 1496 | deletechars: object |
| 1497 | ] |
| 1498 | / |
| 1499 | |
| 1500 | Return a copy with each character mapped by the given translation table. |
| 1501 | |
| 1502 | All characters occurring in the optional argument deletechars are removed. |
| 1503 | The remaining characters are mapped through the given translation table. |
| 1504 | [clinic start generated code]*/ |
| 1505 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1506 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1507 | bytearray_translate_impl(PyByteArrayObject *self, PyObject *table, |
| 1508 | int group_right_1, PyObject *deletechars) |
| 1509 | /*[clinic end generated code: output=2bebc86a9a1ff083 input=b749ad85f4860824]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1510 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1511 | char *input, *output; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1512 | const char *table_chars; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1513 | Py_ssize_t i, c; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1514 | PyObject *input_obj = (PyObject*)self; |
| 1515 | const char *output_start; |
| 1516 | Py_ssize_t inlen; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1517 | PyObject *result = NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1518 | int trans_table[256]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1519 | Py_buffer vtable, vdel; |
| 1520 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1521 | if (table == Py_None) { |
| 1522 | table_chars = NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1523 | table = NULL; |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1524 | } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1525 | return NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1526 | } else { |
| 1527 | if (vtable.len != 256) { |
| 1528 | PyErr_SetString(PyExc_ValueError, |
| 1529 | "translation table must be 256 characters long"); |
Georg Brandl | 953152f | 2009-07-22 12:03:59 +0000 | [diff] [blame] | 1530 | PyBuffer_Release(&vtable); |
| 1531 | return NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1532 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1533 | table_chars = (const char*)vtable.buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1536 | if (deletechars != NULL) { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1537 | if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1538 | if (table != NULL) |
Georg Brandl | 953152f | 2009-07-22 12:03:59 +0000 | [diff] [blame] | 1539 | PyBuffer_Release(&vtable); |
| 1540 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1541 | } |
| 1542 | } |
| 1543 | else { |
| 1544 | vdel.buf = NULL; |
| 1545 | vdel.len = 0; |
| 1546 | } |
| 1547 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1548 | inlen = PyByteArray_GET_SIZE(input_obj); |
| 1549 | result = PyByteArray_FromStringAndSize((char *)NULL, inlen); |
| 1550 | if (result == NULL) |
| 1551 | goto done; |
| 1552 | output_start = output = PyByteArray_AsString(result); |
| 1553 | input = PyByteArray_AS_STRING(input_obj); |
| 1554 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1555 | if (vdel.len == 0 && table_chars != NULL) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1556 | /* If no deletions are required, use faster code */ |
| 1557 | for (i = inlen; --i >= 0; ) { |
| 1558 | c = Py_CHARMASK(*input++); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1559 | *output++ = table_chars[c]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1560 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1561 | goto done; |
| 1562 | } |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1563 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1564 | if (table_chars == NULL) { |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1565 | for (i = 0; i < 256; i++) |
| 1566 | trans_table[i] = Py_CHARMASK(i); |
| 1567 | } else { |
| 1568 | for (i = 0; i < 256; i++) |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1569 | trans_table[i] = Py_CHARMASK(table_chars[i]); |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1570 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1571 | |
| 1572 | for (i = 0; i < vdel.len; i++) |
| 1573 | trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1; |
| 1574 | |
| 1575 | for (i = inlen; --i >= 0; ) { |
| 1576 | c = Py_CHARMASK(*input++); |
| 1577 | if (trans_table[c] != -1) |
| 1578 | if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
| 1579 | continue; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1580 | } |
| 1581 | /* Fix the size of the resulting string */ |
| 1582 | if (inlen > 0) |
Christian Heimes | c731bbe | 2013-07-21 02:04:35 +0200 | [diff] [blame] | 1583 | if (PyByteArray_Resize(result, output - output_start) < 0) { |
| 1584 | Py_CLEAR(result); |
| 1585 | goto done; |
| 1586 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1587 | |
| 1588 | done: |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1589 | if (table != NULL) |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1590 | PyBuffer_Release(&vtable); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1591 | if (deletechars != NULL) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1592 | PyBuffer_Release(&vdel); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1593 | return result; |
| 1594 | } |
| 1595 | |
| 1596 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1597 | /*[clinic input] |
| 1598 | |
| 1599 | @staticmethod |
| 1600 | bytearray.maketrans |
| 1601 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1602 | frm: Py_buffer |
| 1603 | to: Py_buffer |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1604 | / |
| 1605 | |
| 1606 | Return a translation table useable for the bytes or bytearray translate method. |
| 1607 | |
| 1608 | The returned table will be one where each byte in frm is mapped to the byte at |
| 1609 | the same position in to. |
| 1610 | |
| 1611 | The bytes objects frm and to must be of the same length. |
| 1612 | [clinic start generated code]*/ |
| 1613 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1614 | static PyObject * |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1615 | bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1616 | /*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1617 | { |
| 1618 | return _Py_bytes_maketrans(frm, to); |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1622 | /* find and count characters and substrings */ |
| 1623 | |
| 1624 | #define findchar(target, target_len, c) \ |
| 1625 | ((char *)memchr((const void *)(target), c, target_len)) |
| 1626 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1627 | |
Benjamin Peterson | 0f3641c | 2008-11-19 22:05:52 +0000 | [diff] [blame] | 1628 | /* Bytes ops must return a string, create a copy */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1629 | Py_LOCAL(PyByteArrayObject *) |
| 1630 | return_self(PyByteArrayObject *self) |
| 1631 | { |
Georg Brandl | 1e7217d | 2008-05-30 12:02:38 +0000 | [diff] [blame] | 1632 | /* always return a new bytearray */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1633 | return (PyByteArrayObject *)PyByteArray_FromStringAndSize( |
| 1634 | PyByteArray_AS_STRING(self), |
| 1635 | PyByteArray_GET_SIZE(self)); |
| 1636 | } |
| 1637 | |
| 1638 | Py_LOCAL_INLINE(Py_ssize_t) |
| 1639 | countchar(const char *target, Py_ssize_t target_len, char c, Py_ssize_t maxcount) |
| 1640 | { |
| 1641 | Py_ssize_t count=0; |
| 1642 | const char *start=target; |
| 1643 | const char *end=target+target_len; |
| 1644 | |
| 1645 | while ( (start=findchar(start, end-start, c)) != NULL ) { |
| 1646 | count++; |
| 1647 | if (count >= maxcount) |
| 1648 | break; |
| 1649 | start += 1; |
| 1650 | } |
| 1651 | return count; |
| 1652 | } |
| 1653 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1654 | |
| 1655 | /* Algorithms for different cases of string replacement */ |
| 1656 | |
| 1657 | /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */ |
| 1658 | Py_LOCAL(PyByteArrayObject *) |
| 1659 | replace_interleave(PyByteArrayObject *self, |
| 1660 | const char *to_s, Py_ssize_t to_len, |
| 1661 | Py_ssize_t maxcount) |
| 1662 | { |
| 1663 | char *self_s, *result_s; |
| 1664 | Py_ssize_t self_len, result_len; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1665 | Py_ssize_t count, i; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1666 | PyByteArrayObject *result; |
| 1667 | |
| 1668 | self_len = PyByteArray_GET_SIZE(self); |
| 1669 | |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1670 | /* 1 at the end plus 1 after every character; |
| 1671 | count = min(maxcount, self_len + 1) */ |
| 1672 | if (maxcount <= self_len) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1673 | count = maxcount; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1674 | else |
| 1675 | /* Can't overflow: self_len + 1 <= maxcount <= PY_SSIZE_T_MAX. */ |
| 1676 | count = self_len + 1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1677 | |
| 1678 | /* Check for overflow */ |
| 1679 | /* result_len = count * to_len + self_len; */ |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1680 | assert(count > 0); |
| 1681 | if (to_len > (PY_SSIZE_T_MAX - self_len) / count) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1682 | PyErr_SetString(PyExc_OverflowError, |
| 1683 | "replace string is too long"); |
| 1684 | return NULL; |
| 1685 | } |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1686 | result_len = count * to_len + self_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1687 | |
| 1688 | if (! (result = (PyByteArrayObject *) |
| 1689 | PyByteArray_FromStringAndSize(NULL, result_len)) ) |
| 1690 | return NULL; |
| 1691 | |
| 1692 | self_s = PyByteArray_AS_STRING(self); |
| 1693 | result_s = PyByteArray_AS_STRING(result); |
| 1694 | |
Victor Stinner | fac3956 | 2016-03-21 10:38:58 +0100 | [diff] [blame] | 1695 | if (to_len > 1) { |
| 1696 | /* Lay the first one down (guaranteed this will occur) */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1697 | Py_MEMCPY(result_s, to_s, to_len); |
| 1698 | result_s += to_len; |
Victor Stinner | fac3956 | 2016-03-21 10:38:58 +0100 | [diff] [blame] | 1699 | count -= 1; |
| 1700 | |
| 1701 | for (i = 0; i < count; i++) { |
| 1702 | *result_s++ = *self_s++; |
| 1703 | Py_MEMCPY(result_s, to_s, to_len); |
| 1704 | result_s += to_len; |
| 1705 | } |
| 1706 | } |
| 1707 | else { |
| 1708 | result_s[0] = to_s[0]; |
| 1709 | result_s += to_len; |
| 1710 | count -= 1; |
| 1711 | for (i = 0; i < count; i++) { |
| 1712 | *result_s++ = *self_s++; |
| 1713 | result_s[0] = to_s[0]; |
| 1714 | result_s += to_len; |
| 1715 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | /* Copy the rest of the original string */ |
| 1719 | Py_MEMCPY(result_s, self_s, self_len-i); |
| 1720 | |
| 1721 | return result; |
| 1722 | } |
| 1723 | |
| 1724 | /* Special case for deleting a single character */ |
| 1725 | /* len(self)>=1, len(from)==1, to="", maxcount>=1 */ |
| 1726 | Py_LOCAL(PyByteArrayObject *) |
| 1727 | replace_delete_single_character(PyByteArrayObject *self, |
| 1728 | char from_c, Py_ssize_t maxcount) |
| 1729 | { |
| 1730 | char *self_s, *result_s; |
| 1731 | char *start, *next, *end; |
| 1732 | Py_ssize_t self_len, result_len; |
| 1733 | Py_ssize_t count; |
| 1734 | PyByteArrayObject *result; |
| 1735 | |
| 1736 | self_len = PyByteArray_GET_SIZE(self); |
| 1737 | self_s = PyByteArray_AS_STRING(self); |
| 1738 | |
| 1739 | count = countchar(self_s, self_len, from_c, maxcount); |
| 1740 | if (count == 0) { |
| 1741 | return return_self(self); |
| 1742 | } |
| 1743 | |
| 1744 | result_len = self_len - count; /* from_len == 1 */ |
| 1745 | assert(result_len>=0); |
| 1746 | |
| 1747 | if ( (result = (PyByteArrayObject *) |
| 1748 | PyByteArray_FromStringAndSize(NULL, result_len)) == NULL) |
| 1749 | return NULL; |
| 1750 | result_s = PyByteArray_AS_STRING(result); |
| 1751 | |
| 1752 | start = self_s; |
| 1753 | end = self_s + self_len; |
| 1754 | while (count-- > 0) { |
| 1755 | next = findchar(start, end-start, from_c); |
| 1756 | if (next == NULL) |
| 1757 | break; |
| 1758 | Py_MEMCPY(result_s, start, next-start); |
| 1759 | result_s += (next-start); |
| 1760 | start = next+1; |
| 1761 | } |
| 1762 | Py_MEMCPY(result_s, start, end-start); |
| 1763 | |
| 1764 | return result; |
| 1765 | } |
| 1766 | |
| 1767 | /* len(self)>=1, len(from)>=2, to="", maxcount>=1 */ |
| 1768 | |
| 1769 | Py_LOCAL(PyByteArrayObject *) |
| 1770 | replace_delete_substring(PyByteArrayObject *self, |
| 1771 | const char *from_s, Py_ssize_t from_len, |
| 1772 | Py_ssize_t maxcount) |
| 1773 | { |
| 1774 | char *self_s, *result_s; |
| 1775 | char *start, *next, *end; |
| 1776 | Py_ssize_t self_len, result_len; |
| 1777 | Py_ssize_t count, offset; |
| 1778 | PyByteArrayObject *result; |
| 1779 | |
| 1780 | self_len = PyByteArray_GET_SIZE(self); |
| 1781 | self_s = PyByteArray_AS_STRING(self); |
| 1782 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1783 | count = stringlib_count(self_s, self_len, |
| 1784 | from_s, from_len, |
| 1785 | maxcount); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1786 | |
| 1787 | if (count == 0) { |
| 1788 | /* no matches */ |
| 1789 | return return_self(self); |
| 1790 | } |
| 1791 | |
| 1792 | result_len = self_len - (count * from_len); |
| 1793 | assert (result_len>=0); |
| 1794 | |
| 1795 | if ( (result = (PyByteArrayObject *) |
| 1796 | PyByteArray_FromStringAndSize(NULL, result_len)) == NULL ) |
| 1797 | return NULL; |
| 1798 | |
| 1799 | result_s = PyByteArray_AS_STRING(result); |
| 1800 | |
| 1801 | start = self_s; |
| 1802 | end = self_s + self_len; |
| 1803 | while (count-- > 0) { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1804 | offset = stringlib_find(start, end-start, |
| 1805 | from_s, from_len, |
| 1806 | 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1807 | if (offset == -1) |
| 1808 | break; |
| 1809 | next = start + offset; |
| 1810 | |
| 1811 | Py_MEMCPY(result_s, start, next-start); |
| 1812 | |
| 1813 | result_s += (next-start); |
| 1814 | start = next+from_len; |
| 1815 | } |
| 1816 | Py_MEMCPY(result_s, start, end-start); |
| 1817 | return result; |
| 1818 | } |
| 1819 | |
| 1820 | /* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */ |
| 1821 | Py_LOCAL(PyByteArrayObject *) |
| 1822 | replace_single_character_in_place(PyByteArrayObject *self, |
| 1823 | char from_c, char to_c, |
| 1824 | Py_ssize_t maxcount) |
| 1825 | { |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1826 | char *self_s, *result_s, *start, *end, *next; |
| 1827 | Py_ssize_t self_len; |
| 1828 | PyByteArrayObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1829 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1830 | /* The result string will be the same size */ |
| 1831 | self_s = PyByteArray_AS_STRING(self); |
| 1832 | self_len = PyByteArray_GET_SIZE(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1833 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1834 | next = findchar(self_s, self_len, from_c); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1835 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1836 | if (next == NULL) { |
| 1837 | /* No matches; return the original bytes */ |
| 1838 | return return_self(self); |
| 1839 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1840 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1841 | /* Need to make a new bytes */ |
| 1842 | result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len); |
| 1843 | if (result == NULL) |
| 1844 | return NULL; |
| 1845 | result_s = PyByteArray_AS_STRING(result); |
| 1846 | Py_MEMCPY(result_s, self_s, self_len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1847 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1848 | /* change everything in-place, starting with this one */ |
| 1849 | start = result_s + (next-self_s); |
| 1850 | *start = to_c; |
| 1851 | start++; |
| 1852 | end = result_s + self_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1853 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1854 | while (--maxcount > 0) { |
| 1855 | next = findchar(start, end-start, from_c); |
| 1856 | if (next == NULL) |
| 1857 | break; |
| 1858 | *next = to_c; |
| 1859 | start = next+1; |
| 1860 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1861 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1862 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | /* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */ |
| 1866 | Py_LOCAL(PyByteArrayObject *) |
| 1867 | replace_substring_in_place(PyByteArrayObject *self, |
| 1868 | const char *from_s, Py_ssize_t from_len, |
| 1869 | const char *to_s, Py_ssize_t to_len, |
| 1870 | Py_ssize_t maxcount) |
| 1871 | { |
| 1872 | char *result_s, *start, *end; |
| 1873 | char *self_s; |
| 1874 | Py_ssize_t self_len, offset; |
| 1875 | PyByteArrayObject *result; |
| 1876 | |
| 1877 | /* The result bytes will be the same size */ |
| 1878 | |
| 1879 | self_s = PyByteArray_AS_STRING(self); |
| 1880 | self_len = PyByteArray_GET_SIZE(self); |
| 1881 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1882 | offset = stringlib_find(self_s, self_len, |
| 1883 | from_s, from_len, |
| 1884 | 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1885 | if (offset == -1) { |
| 1886 | /* No matches; return the original bytes */ |
| 1887 | return return_self(self); |
| 1888 | } |
| 1889 | |
| 1890 | /* Need to make a new bytes */ |
| 1891 | result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len); |
| 1892 | if (result == NULL) |
| 1893 | return NULL; |
| 1894 | result_s = PyByteArray_AS_STRING(result); |
| 1895 | Py_MEMCPY(result_s, self_s, self_len); |
| 1896 | |
| 1897 | /* change everything in-place, starting with this one */ |
| 1898 | start = result_s + offset; |
| 1899 | Py_MEMCPY(start, to_s, from_len); |
| 1900 | start += from_len; |
| 1901 | end = result_s + self_len; |
| 1902 | |
| 1903 | while ( --maxcount > 0) { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1904 | offset = stringlib_find(start, end-start, |
| 1905 | from_s, from_len, |
| 1906 | 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1907 | if (offset==-1) |
| 1908 | break; |
| 1909 | Py_MEMCPY(start+offset, to_s, from_len); |
| 1910 | start += offset+from_len; |
| 1911 | } |
| 1912 | |
| 1913 | return result; |
| 1914 | } |
| 1915 | |
| 1916 | /* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */ |
| 1917 | Py_LOCAL(PyByteArrayObject *) |
| 1918 | replace_single_character(PyByteArrayObject *self, |
| 1919 | char from_c, |
| 1920 | const char *to_s, Py_ssize_t to_len, |
| 1921 | Py_ssize_t maxcount) |
| 1922 | { |
| 1923 | char *self_s, *result_s; |
| 1924 | char *start, *next, *end; |
| 1925 | Py_ssize_t self_len, result_len; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1926 | Py_ssize_t count; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1927 | PyByteArrayObject *result; |
| 1928 | |
| 1929 | self_s = PyByteArray_AS_STRING(self); |
| 1930 | self_len = PyByteArray_GET_SIZE(self); |
| 1931 | |
| 1932 | count = countchar(self_s, self_len, from_c, maxcount); |
| 1933 | if (count == 0) { |
| 1934 | /* no matches, return unchanged */ |
| 1935 | return return_self(self); |
| 1936 | } |
| 1937 | |
| 1938 | /* use the difference between current and new, hence the "-1" */ |
| 1939 | /* result_len = self_len + count * (to_len-1) */ |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1940 | assert(count > 0); |
| 1941 | if (to_len - 1 > (PY_SSIZE_T_MAX - self_len) / count) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1942 | PyErr_SetString(PyExc_OverflowError, "replace bytes is too long"); |
| 1943 | return NULL; |
| 1944 | } |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1945 | result_len = self_len + count * (to_len - 1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1946 | |
| 1947 | if ( (result = (PyByteArrayObject *) |
| 1948 | PyByteArray_FromStringAndSize(NULL, result_len)) == NULL) |
| 1949 | return NULL; |
| 1950 | result_s = PyByteArray_AS_STRING(result); |
| 1951 | |
| 1952 | start = self_s; |
| 1953 | end = self_s + self_len; |
| 1954 | while (count-- > 0) { |
| 1955 | next = findchar(start, end-start, from_c); |
| 1956 | if (next == NULL) |
| 1957 | break; |
| 1958 | |
| 1959 | if (next == start) { |
| 1960 | /* replace with the 'to' */ |
| 1961 | Py_MEMCPY(result_s, to_s, to_len); |
| 1962 | result_s += to_len; |
| 1963 | start += 1; |
| 1964 | } else { |
| 1965 | /* copy the unchanged old then the 'to' */ |
| 1966 | Py_MEMCPY(result_s, start, next-start); |
| 1967 | result_s += (next-start); |
| 1968 | Py_MEMCPY(result_s, to_s, to_len); |
| 1969 | result_s += to_len; |
| 1970 | start = next+1; |
| 1971 | } |
| 1972 | } |
| 1973 | /* Copy the remainder of the remaining bytes */ |
| 1974 | Py_MEMCPY(result_s, start, end-start); |
| 1975 | |
| 1976 | return result; |
| 1977 | } |
| 1978 | |
| 1979 | /* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */ |
| 1980 | Py_LOCAL(PyByteArrayObject *) |
| 1981 | replace_substring(PyByteArrayObject *self, |
| 1982 | const char *from_s, Py_ssize_t from_len, |
| 1983 | const char *to_s, Py_ssize_t to_len, |
| 1984 | Py_ssize_t maxcount) |
| 1985 | { |
| 1986 | char *self_s, *result_s; |
| 1987 | char *start, *next, *end; |
| 1988 | Py_ssize_t self_len, result_len; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1989 | Py_ssize_t count, offset; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1990 | PyByteArrayObject *result; |
| 1991 | |
| 1992 | self_s = PyByteArray_AS_STRING(self); |
| 1993 | self_len = PyByteArray_GET_SIZE(self); |
| 1994 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1995 | count = stringlib_count(self_s, self_len, |
| 1996 | from_s, from_len, |
| 1997 | maxcount); |
| 1998 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1999 | if (count == 0) { |
| 2000 | /* no matches, return unchanged */ |
| 2001 | return return_self(self); |
| 2002 | } |
| 2003 | |
| 2004 | /* Check for overflow */ |
| 2005 | /* result_len = self_len + count * (to_len-from_len) */ |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2006 | assert(count > 0); |
| 2007 | if (to_len - from_len > (PY_SSIZE_T_MAX - self_len) / count) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2008 | PyErr_SetString(PyExc_OverflowError, "replace bytes is too long"); |
| 2009 | return NULL; |
| 2010 | } |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2011 | result_len = self_len + count * (to_len - from_len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2012 | |
| 2013 | if ( (result = (PyByteArrayObject *) |
| 2014 | PyByteArray_FromStringAndSize(NULL, result_len)) == NULL) |
| 2015 | return NULL; |
| 2016 | result_s = PyByteArray_AS_STRING(result); |
| 2017 | |
| 2018 | start = self_s; |
| 2019 | end = self_s + self_len; |
| 2020 | while (count-- > 0) { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2021 | offset = stringlib_find(start, end-start, |
| 2022 | from_s, from_len, |
| 2023 | 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2024 | if (offset == -1) |
| 2025 | break; |
| 2026 | next = start+offset; |
| 2027 | if (next == start) { |
| 2028 | /* replace with the 'to' */ |
| 2029 | Py_MEMCPY(result_s, to_s, to_len); |
| 2030 | result_s += to_len; |
| 2031 | start += from_len; |
| 2032 | } else { |
| 2033 | /* copy the unchanged old then the 'to' */ |
| 2034 | Py_MEMCPY(result_s, start, next-start); |
| 2035 | result_s += (next-start); |
| 2036 | Py_MEMCPY(result_s, to_s, to_len); |
| 2037 | result_s += to_len; |
| 2038 | start = next+from_len; |
| 2039 | } |
| 2040 | } |
| 2041 | /* Copy the remainder of the remaining bytes */ |
| 2042 | Py_MEMCPY(result_s, start, end-start); |
| 2043 | |
| 2044 | return result; |
| 2045 | } |
| 2046 | |
| 2047 | |
| 2048 | Py_LOCAL(PyByteArrayObject *) |
| 2049 | replace(PyByteArrayObject *self, |
| 2050 | const char *from_s, Py_ssize_t from_len, |
| 2051 | const char *to_s, Py_ssize_t to_len, |
| 2052 | Py_ssize_t maxcount) |
| 2053 | { |
| 2054 | if (maxcount < 0) { |
| 2055 | maxcount = PY_SSIZE_T_MAX; |
| 2056 | } else if (maxcount == 0 || PyByteArray_GET_SIZE(self) == 0) { |
| 2057 | /* nothing to do; return the original bytes */ |
| 2058 | return return_self(self); |
| 2059 | } |
| 2060 | |
| 2061 | if (maxcount == 0 || |
| 2062 | (from_len == 0 && to_len == 0)) { |
| 2063 | /* nothing to do; return the original bytes */ |
| 2064 | return return_self(self); |
| 2065 | } |
| 2066 | |
| 2067 | /* Handle zero-length special cases */ |
| 2068 | |
| 2069 | if (from_len == 0) { |
| 2070 | /* insert the 'to' bytes everywhere. */ |
| 2071 | /* >>> "Python".replace("", ".") */ |
| 2072 | /* '.P.y.t.h.o.n.' */ |
| 2073 | return replace_interleave(self, to_s, to_len, maxcount); |
| 2074 | } |
| 2075 | |
| 2076 | /* Except for "".replace("", "A") == "A" there is no way beyond this */ |
| 2077 | /* point for an empty self bytes to generate a non-empty bytes */ |
| 2078 | /* Special case so the remaining code always gets a non-empty bytes */ |
| 2079 | if (PyByteArray_GET_SIZE(self) == 0) { |
| 2080 | return return_self(self); |
| 2081 | } |
| 2082 | |
| 2083 | if (to_len == 0) { |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2084 | /* delete all occurrences of 'from' bytes */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2085 | if (from_len == 1) { |
| 2086 | return replace_delete_single_character( |
| 2087 | self, from_s[0], maxcount); |
| 2088 | } else { |
| 2089 | return replace_delete_substring(self, from_s, from_len, maxcount); |
| 2090 | } |
| 2091 | } |
| 2092 | |
| 2093 | /* Handle special case where both bytes have the same length */ |
| 2094 | |
| 2095 | if (from_len == to_len) { |
| 2096 | if (from_len == 1) { |
| 2097 | return replace_single_character_in_place( |
| 2098 | self, |
| 2099 | from_s[0], |
| 2100 | to_s[0], |
| 2101 | maxcount); |
| 2102 | } else { |
| 2103 | return replace_substring_in_place( |
| 2104 | self, from_s, from_len, to_s, to_len, maxcount); |
| 2105 | } |
| 2106 | } |
| 2107 | |
| 2108 | /* Otherwise use the more generic algorithms */ |
| 2109 | if (from_len == 1) { |
| 2110 | return replace_single_character(self, from_s[0], |
| 2111 | to_s, to_len, maxcount); |
| 2112 | } else { |
| 2113 | /* len('from')>=2, len('to')>=1 */ |
| 2114 | return replace_substring(self, from_s, from_len, to_s, to_len, maxcount); |
| 2115 | } |
| 2116 | } |
| 2117 | |
| 2118 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2119 | /*[clinic input] |
| 2120 | bytearray.replace |
| 2121 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2122 | old: Py_buffer |
| 2123 | new: Py_buffer |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2124 | count: Py_ssize_t = -1 |
| 2125 | Maximum number of occurrences to replace. |
| 2126 | -1 (the default value) means replace all occurrences. |
| 2127 | / |
| 2128 | |
| 2129 | Return a copy with all occurrences of substring old replaced by new. |
| 2130 | |
| 2131 | If the optional argument count is given, only the first count occurrences are |
| 2132 | replaced. |
| 2133 | [clinic start generated code]*/ |
| 2134 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2135 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2136 | bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old, |
| 2137 | Py_buffer *new, Py_ssize_t count) |
| 2138 | /*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2139 | { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2140 | return (PyObject *)replace((PyByteArrayObject *) self, |
| 2141 | old->buf, old->len, |
| 2142 | new->buf, new->len, count); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2145 | /*[clinic input] |
| 2146 | bytearray.split |
| 2147 | |
| 2148 | sep: object = None |
| 2149 | The delimiter according which to split the bytearray. |
| 2150 | None (the default value) means split on ASCII whitespace characters |
| 2151 | (space, tab, return, newline, formfeed, vertical tab). |
| 2152 | maxsplit: Py_ssize_t = -1 |
| 2153 | Maximum number of splits to do. |
| 2154 | -1 (the default value) means no limit. |
| 2155 | |
| 2156 | Return a list of the sections in the bytearray, using sep as the delimiter. |
| 2157 | [clinic start generated code]*/ |
| 2158 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2159 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2160 | bytearray_split_impl(PyByteArrayObject *self, PyObject *sep, |
| 2161 | Py_ssize_t maxsplit) |
| 2162 | /*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2163 | { |
| 2164 | Py_ssize_t len = PyByteArray_GET_SIZE(self), n; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2165 | const char *s = PyByteArray_AS_STRING(self), *sub; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2166 | PyObject *list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2167 | Py_buffer vsub; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2168 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2169 | if (maxsplit < 0) |
| 2170 | maxsplit = PY_SSIZE_T_MAX; |
| 2171 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2172 | if (sep == Py_None) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2173 | return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2174 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2175 | if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2176 | return NULL; |
| 2177 | sub = vsub.buf; |
| 2178 | n = vsub.len; |
| 2179 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2180 | list = stringlib_split( |
| 2181 | (PyObject*) self, s, len, sub, n, maxsplit |
| 2182 | ); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2183 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2184 | return list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2185 | } |
| 2186 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2187 | /*[clinic input] |
| 2188 | bytearray.partition |
| 2189 | |
| 2190 | self: self(type="PyByteArrayObject *") |
| 2191 | sep: object |
| 2192 | / |
| 2193 | |
| 2194 | Partition the bytearray into three parts using the given separator. |
| 2195 | |
| 2196 | This will search for the separator sep in the bytearray. If the separator is |
| 2197 | found, returns a 3-tuple containing the part before the separator, the |
| 2198 | separator itself, and the part after it. |
| 2199 | |
| 2200 | If the separator is not found, returns a 3-tuple containing the original |
| 2201 | bytearray object and two empty bytearray objects. |
| 2202 | [clinic start generated code]*/ |
| 2203 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2204 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2205 | bytearray_partition(PyByteArrayObject *self, PyObject *sep) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2206 | /*[clinic end generated code: output=45d2525ddd35f957 input=7d7fe37b1696d506]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2207 | { |
| 2208 | PyObject *bytesep, *result; |
| 2209 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2210 | bytesep = PyByteArray_FromObject(sep); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2211 | if (! bytesep) |
| 2212 | return NULL; |
| 2213 | |
| 2214 | result = stringlib_partition( |
| 2215 | (PyObject*) self, |
| 2216 | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
| 2217 | bytesep, |
| 2218 | PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) |
| 2219 | ); |
| 2220 | |
| 2221 | Py_DECREF(bytesep); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2222 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2225 | /*[clinic input] |
| 2226 | bytearray.rpartition |
| 2227 | |
| 2228 | self: self(type="PyByteArrayObject *") |
| 2229 | sep: object |
| 2230 | / |
| 2231 | |
| 2232 | Partition the bytes into three parts using the given separator. |
| 2233 | |
| 2234 | This will search for the separator sep in the bytearray, starting and the end. |
| 2235 | If the separator is found, returns a 3-tuple containing the part before the |
| 2236 | separator, the separator itself, and the part after it. |
| 2237 | |
| 2238 | If the separator is not found, returns a 3-tuple containing two empty bytearray |
| 2239 | objects and the original bytearray object. |
| 2240 | [clinic start generated code]*/ |
| 2241 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2242 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2243 | bytearray_rpartition(PyByteArrayObject *self, PyObject *sep) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2244 | /*[clinic end generated code: output=440de3c9426115e8 input=9b8cd540c1b75853]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2245 | { |
| 2246 | PyObject *bytesep, *result; |
| 2247 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2248 | bytesep = PyByteArray_FromObject(sep); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2249 | if (! bytesep) |
| 2250 | return NULL; |
| 2251 | |
| 2252 | result = stringlib_rpartition( |
| 2253 | (PyObject*) self, |
| 2254 | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
| 2255 | bytesep, |
| 2256 | PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) |
| 2257 | ); |
| 2258 | |
| 2259 | Py_DECREF(bytesep); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2260 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2261 | } |
| 2262 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2263 | /*[clinic input] |
| 2264 | bytearray.rsplit = bytearray.split |
| 2265 | |
| 2266 | Return a list of the sections in the bytearray, using sep as the delimiter. |
| 2267 | |
| 2268 | Splitting is done starting at the end of the bytearray and working to the front. |
| 2269 | [clinic start generated code]*/ |
| 2270 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2271 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2272 | bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep, |
| 2273 | Py_ssize_t maxsplit) |
| 2274 | /*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2275 | { |
| 2276 | Py_ssize_t len = PyByteArray_GET_SIZE(self), n; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2277 | const char *s = PyByteArray_AS_STRING(self), *sub; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2278 | PyObject *list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2279 | Py_buffer vsub; |
| 2280 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2281 | if (maxsplit < 0) |
| 2282 | maxsplit = PY_SSIZE_T_MAX; |
| 2283 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2284 | if (sep == Py_None) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2285 | return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2286 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2287 | if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2288 | return NULL; |
| 2289 | sub = vsub.buf; |
| 2290 | n = vsub.len; |
| 2291 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2292 | list = stringlib_rsplit( |
| 2293 | (PyObject*) self, s, len, sub, n, maxsplit |
| 2294 | ); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2295 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2296 | return list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2299 | /*[clinic input] |
| 2300 | bytearray.reverse |
| 2301 | |
| 2302 | self: self(type="PyByteArrayObject *") |
| 2303 | |
| 2304 | Reverse the order of the values in B in place. |
| 2305 | [clinic start generated code]*/ |
| 2306 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2307 | static PyObject * |
| 2308 | bytearray_reverse_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2309 | /*[clinic end generated code: output=9f7616f29ab309d3 input=7933a499b8597bd1]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2310 | { |
| 2311 | char swap, *head, *tail; |
| 2312 | Py_ssize_t i, j, n = Py_SIZE(self); |
| 2313 | |
| 2314 | j = n / 2; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2315 | head = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2316 | tail = head + n - 1; |
| 2317 | for (i = 0; i < j; i++) { |
| 2318 | swap = *head; |
| 2319 | *head++ = *tail; |
| 2320 | *tail-- = swap; |
| 2321 | } |
| 2322 | |
| 2323 | Py_RETURN_NONE; |
| 2324 | } |
| 2325 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2326 | |
| 2327 | /*[python input] |
| 2328 | class bytesvalue_converter(CConverter): |
| 2329 | type = 'int' |
| 2330 | converter = '_getbytevalue' |
| 2331 | [python start generated code]*/ |
| 2332 | /*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/ |
| 2333 | |
| 2334 | |
| 2335 | /*[clinic input] |
| 2336 | bytearray.insert |
| 2337 | |
| 2338 | self: self(type="PyByteArrayObject *") |
| 2339 | index: Py_ssize_t |
| 2340 | The index where the value is to be inserted. |
| 2341 | item: bytesvalue |
| 2342 | The item to be inserted. |
| 2343 | / |
| 2344 | |
| 2345 | Insert a single item into the bytearray before the given index. |
| 2346 | [clinic start generated code]*/ |
| 2347 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2348 | static PyObject * |
| 2349 | bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2350 | /*[clinic end generated code: output=76c775a70e7b07b7 input=833766836ba30e1e]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2351 | { |
| 2352 | Py_ssize_t n = Py_SIZE(self); |
| 2353 | char *buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2354 | |
| 2355 | if (n == PY_SSIZE_T_MAX) { |
| 2356 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 2357 | "cannot add more objects to bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2358 | return NULL; |
| 2359 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2360 | if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) |
| 2361 | return NULL; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2362 | buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2363 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2364 | if (index < 0) { |
| 2365 | index += n; |
| 2366 | if (index < 0) |
| 2367 | index = 0; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2368 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2369 | if (index > n) |
| 2370 | index = n; |
| 2371 | memmove(buf + index + 1, buf + index, n - index); |
| 2372 | buf[index] = item; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2373 | |
| 2374 | Py_RETURN_NONE; |
| 2375 | } |
| 2376 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2377 | /*[clinic input] |
| 2378 | bytearray.append |
| 2379 | |
| 2380 | self: self(type="PyByteArrayObject *") |
| 2381 | item: bytesvalue |
| 2382 | The item to be appended. |
| 2383 | / |
| 2384 | |
| 2385 | Append a single item to the end of the bytearray. |
| 2386 | [clinic start generated code]*/ |
| 2387 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2388 | static PyObject * |
| 2389 | bytearray_append_impl(PyByteArrayObject *self, int item) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2390 | /*[clinic end generated code: output=a154e19ed1886cb6 input=ae56ea87380407cc]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2391 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2392 | Py_ssize_t n = Py_SIZE(self); |
| 2393 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2394 | if (n == PY_SSIZE_T_MAX) { |
| 2395 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 2396 | "cannot add more objects to bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2397 | return NULL; |
| 2398 | } |
| 2399 | if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) |
| 2400 | return NULL; |
| 2401 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2402 | PyByteArray_AS_STRING(self)[n] = item; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2403 | |
| 2404 | Py_RETURN_NONE; |
| 2405 | } |
| 2406 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2407 | /*[clinic input] |
| 2408 | bytearray.extend |
| 2409 | |
| 2410 | self: self(type="PyByteArrayObject *") |
| 2411 | iterable_of_ints: object |
| 2412 | The iterable of items to append. |
| 2413 | / |
| 2414 | |
| 2415 | Append all the items from the iterator or sequence to the end of the bytearray. |
| 2416 | [clinic start generated code]*/ |
| 2417 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2418 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2419 | bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2420 | /*[clinic end generated code: output=98155dbe249170b1 input=ce83a5d75b70d850]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2421 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2422 | PyObject *it, *item, *bytearray_obj; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2423 | Py_ssize_t buf_size = 0, len = 0; |
| 2424 | int value; |
| 2425 | char *buf; |
| 2426 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2427 | /* bytearray_setslice code only accepts something supporting PEP 3118. */ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2428 | if (PyObject_CheckBuffer(iterable_of_ints)) { |
| 2429 | 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] | 2430 | return NULL; |
| 2431 | |
| 2432 | Py_RETURN_NONE; |
| 2433 | } |
| 2434 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2435 | it = PyObject_GetIter(iterable_of_ints); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2436 | if (it == NULL) |
| 2437 | return NULL; |
| 2438 | |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 2439 | /* 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] | 2440 | buf_size = PyObject_LengthHint(iterable_of_ints, 32); |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 2441 | if (buf_size == -1) { |
| 2442 | Py_DECREF(it); |
| 2443 | return NULL; |
| 2444 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2445 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2446 | bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size); |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 2447 | if (bytearray_obj == NULL) { |
| 2448 | Py_DECREF(it); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2449 | return NULL; |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 2450 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2451 | buf = PyByteArray_AS_STRING(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2452 | |
| 2453 | while ((item = PyIter_Next(it)) != NULL) { |
| 2454 | if (! _getbytevalue(item, &value)) { |
| 2455 | Py_DECREF(item); |
| 2456 | Py_DECREF(it); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2457 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2458 | return NULL; |
| 2459 | } |
| 2460 | buf[len++] = value; |
| 2461 | Py_DECREF(item); |
| 2462 | |
| 2463 | if (len >= buf_size) { |
| 2464 | buf_size = len + (len >> 1) + 1; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2465 | if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2466 | Py_DECREF(it); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2467 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2468 | return NULL; |
| 2469 | } |
| 2470 | /* Recompute the `buf' pointer, since the resizing operation may |
| 2471 | have invalidated it. */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2472 | buf = PyByteArray_AS_STRING(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2473 | } |
| 2474 | } |
| 2475 | Py_DECREF(it); |
| 2476 | |
| 2477 | /* Resize down to exact size. */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2478 | if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) { |
| 2479 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2480 | return NULL; |
| 2481 | } |
| 2482 | |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 2483 | if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) { |
| 2484 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2485 | return NULL; |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 2486 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2487 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2488 | |
| 2489 | Py_RETURN_NONE; |
| 2490 | } |
| 2491 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2492 | /*[clinic input] |
| 2493 | bytearray.pop |
| 2494 | |
| 2495 | self: self(type="PyByteArrayObject *") |
| 2496 | index: Py_ssize_t = -1 |
| 2497 | The index from where to remove the item. |
| 2498 | -1 (the default value) means remove the last item. |
| 2499 | / |
| 2500 | |
| 2501 | Remove and return a single item from B. |
| 2502 | |
| 2503 | If no index argument is given, will pop the last item. |
| 2504 | [clinic start generated code]*/ |
| 2505 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2506 | static PyObject * |
| 2507 | bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2508 | /*[clinic end generated code: output=e0ccd401f8021da8 input=0797e6c0ca9d5a85]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2509 | { |
| 2510 | int value; |
| 2511 | Py_ssize_t n = Py_SIZE(self); |
| 2512 | char *buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2513 | |
| 2514 | if (n == 0) { |
Eli Bendersky | 1bc4f19 | 2011-03-04 04:55:25 +0000 | [diff] [blame] | 2515 | PyErr_SetString(PyExc_IndexError, |
| 2516 | "pop from empty bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2517 | return NULL; |
| 2518 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2519 | if (index < 0) |
| 2520 | index += Py_SIZE(self); |
| 2521 | if (index < 0 || index >= Py_SIZE(self)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2522 | PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
| 2523 | return NULL; |
| 2524 | } |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 2525 | if (!_canresize(self)) |
| 2526 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2527 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2528 | buf = PyByteArray_AS_STRING(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2529 | value = buf[index]; |
| 2530 | memmove(buf + index, buf + index + 1, n - index); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2531 | if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) |
| 2532 | return NULL; |
| 2533 | |
Mark Dickinson | 54a3db9 | 2009-09-06 10:19:23 +0000 | [diff] [blame] | 2534 | return PyLong_FromLong((unsigned char)value); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2537 | /*[clinic input] |
| 2538 | bytearray.remove |
| 2539 | |
| 2540 | self: self(type="PyByteArrayObject *") |
| 2541 | value: bytesvalue |
| 2542 | The value to remove. |
| 2543 | / |
| 2544 | |
| 2545 | Remove the first occurrence of a value in the bytearray. |
| 2546 | [clinic start generated code]*/ |
| 2547 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2548 | static PyObject * |
| 2549 | bytearray_remove_impl(PyByteArrayObject *self, int value) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2550 | /*[clinic end generated code: output=d659e37866709c13 input=47560b11fd856c24]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2551 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2552 | Py_ssize_t where, n = Py_SIZE(self); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2553 | char *buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2554 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2555 | for (where = 0; where < n; where++) { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2556 | if (buf[where] == value) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2557 | break; |
| 2558 | } |
| 2559 | if (where == n) { |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 2560 | PyErr_SetString(PyExc_ValueError, "value not found in bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2561 | return NULL; |
| 2562 | } |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 2563 | if (!_canresize(self)) |
| 2564 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2565 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2566 | memmove(buf + where, buf + where + 1, n - where); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2567 | if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) |
| 2568 | return NULL; |
| 2569 | |
| 2570 | Py_RETURN_NONE; |
| 2571 | } |
| 2572 | |
| 2573 | /* XXX These two helpers could be optimized if argsize == 1 */ |
| 2574 | |
| 2575 | static Py_ssize_t |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2576 | lstrip_helper(const char *myptr, Py_ssize_t mysize, |
| 2577 | const void *argptr, Py_ssize_t argsize) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2578 | { |
| 2579 | Py_ssize_t i = 0; |
Antoine Pitrou | 5b72075 | 2013-10-05 21:24:10 +0200 | [diff] [blame] | 2580 | while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2581 | i++; |
| 2582 | return i; |
| 2583 | } |
| 2584 | |
| 2585 | static Py_ssize_t |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2586 | rstrip_helper(const char *myptr, Py_ssize_t mysize, |
| 2587 | const void *argptr, Py_ssize_t argsize) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2588 | { |
| 2589 | Py_ssize_t i = mysize - 1; |
Antoine Pitrou | 5b72075 | 2013-10-05 21:24:10 +0200 | [diff] [blame] | 2590 | while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2591 | i--; |
| 2592 | return i + 1; |
| 2593 | } |
| 2594 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2595 | /*[clinic input] |
| 2596 | bytearray.strip |
| 2597 | |
| 2598 | bytes: object = None |
| 2599 | / |
| 2600 | |
| 2601 | Strip leading and trailing bytes contained in the argument. |
| 2602 | |
| 2603 | If the argument is omitted or None, strip leading and trailing ASCII whitespace. |
| 2604 | [clinic start generated code]*/ |
| 2605 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2606 | static PyObject * |
| 2607 | bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2608 | /*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2609 | { |
| 2610 | Py_ssize_t left, right, mysize, byteslen; |
| 2611 | char *myptr, *bytesptr; |
| 2612 | Py_buffer vbytes; |
| 2613 | |
| 2614 | if (bytes == Py_None) { |
| 2615 | bytesptr = "\t\n\r\f\v "; |
| 2616 | byteslen = 6; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2617 | } |
| 2618 | else { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2619 | if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2620 | return NULL; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2621 | bytesptr = (char *) vbytes.buf; |
| 2622 | byteslen = vbytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2623 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2624 | myptr = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2625 | mysize = Py_SIZE(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2626 | left = lstrip_helper(myptr, mysize, bytesptr, byteslen); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2627 | if (left == mysize) |
| 2628 | right = left; |
| 2629 | else |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2630 | right = rstrip_helper(myptr, mysize, bytesptr, byteslen); |
| 2631 | if (bytes != Py_None) |
| 2632 | PyBuffer_Release(&vbytes); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2633 | return PyByteArray_FromStringAndSize(myptr + left, right - left); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2634 | } |
| 2635 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2636 | /*[clinic input] |
| 2637 | bytearray.lstrip |
| 2638 | |
| 2639 | bytes: object = None |
| 2640 | / |
| 2641 | |
| 2642 | Strip leading bytes contained in the argument. |
| 2643 | |
| 2644 | If the argument is omitted or None, strip leading ASCII whitespace. |
| 2645 | [clinic start generated code]*/ |
| 2646 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2647 | static PyObject * |
| 2648 | bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2649 | /*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2650 | { |
| 2651 | Py_ssize_t left, right, mysize, byteslen; |
| 2652 | char *myptr, *bytesptr; |
| 2653 | Py_buffer vbytes; |
| 2654 | |
| 2655 | if (bytes == Py_None) { |
| 2656 | bytesptr = "\t\n\r\f\v "; |
| 2657 | byteslen = 6; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2658 | } |
| 2659 | else { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2660 | if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2661 | return NULL; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2662 | bytesptr = (char *) vbytes.buf; |
| 2663 | byteslen = vbytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2664 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2665 | myptr = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2666 | mysize = Py_SIZE(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2667 | left = lstrip_helper(myptr, mysize, bytesptr, byteslen); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2668 | right = mysize; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2669 | if (bytes != Py_None) |
| 2670 | PyBuffer_Release(&vbytes); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2671 | return PyByteArray_FromStringAndSize(myptr + left, right - left); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2674 | /*[clinic input] |
| 2675 | bytearray.rstrip |
| 2676 | |
| 2677 | bytes: object = None |
| 2678 | / |
| 2679 | |
| 2680 | Strip trailing bytes contained in the argument. |
| 2681 | |
| 2682 | If the argument is omitted or None, strip trailing ASCII whitespace. |
| 2683 | [clinic start generated code]*/ |
| 2684 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2685 | static PyObject * |
| 2686 | bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2687 | /*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2688 | { |
| 2689 | Py_ssize_t right, mysize, byteslen; |
| 2690 | char *myptr, *bytesptr; |
| 2691 | Py_buffer vbytes; |
| 2692 | |
| 2693 | if (bytes == Py_None) { |
| 2694 | bytesptr = "\t\n\r\f\v "; |
| 2695 | byteslen = 6; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2696 | } |
| 2697 | else { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2698 | if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2699 | return NULL; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2700 | bytesptr = (char *) vbytes.buf; |
| 2701 | byteslen = vbytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2702 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2703 | myptr = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2704 | mysize = Py_SIZE(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2705 | right = rstrip_helper(myptr, mysize, bytesptr, byteslen); |
| 2706 | if (bytes != Py_None) |
| 2707 | PyBuffer_Release(&vbytes); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2708 | return PyByteArray_FromStringAndSize(myptr, right); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2709 | } |
| 2710 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2711 | /*[clinic input] |
| 2712 | bytearray.decode |
| 2713 | |
| 2714 | encoding: str(c_default="NULL") = 'utf-8' |
| 2715 | The encoding with which to decode the bytearray. |
| 2716 | errors: str(c_default="NULL") = 'strict' |
| 2717 | The error handling scheme to use for the handling of decoding errors. |
| 2718 | The default is 'strict' meaning that decoding errors raise a |
| 2719 | UnicodeDecodeError. Other possible values are 'ignore' and 'replace' |
| 2720 | as well as any other name registered with codecs.register_error that |
| 2721 | can handle UnicodeDecodeErrors. |
| 2722 | |
| 2723 | Decode the bytearray using the codec registered for encoding. |
| 2724 | [clinic start generated code]*/ |
| 2725 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2726 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2727 | bytearray_decode_impl(PyByteArrayObject *self, const char *encoding, |
| 2728 | const char *errors) |
| 2729 | /*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2730 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2731 | if (encoding == NULL) |
| 2732 | encoding = PyUnicode_GetDefaultEncoding(); |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2733 | return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
| 2736 | PyDoc_STRVAR(alloc_doc, |
| 2737 | "B.__alloc__() -> int\n\ |
| 2738 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2739 | Return the number of bytes actually allocated."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2740 | |
| 2741 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2742 | bytearray_alloc(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2743 | { |
| 2744 | return PyLong_FromSsize_t(self->ob_alloc); |
| 2745 | } |
| 2746 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2747 | /*[clinic input] |
| 2748 | bytearray.join |
| 2749 | |
| 2750 | iterable_of_bytes: object |
| 2751 | / |
| 2752 | |
| 2753 | Concatenate any number of bytes/bytearray objects. |
| 2754 | |
| 2755 | The bytearray whose method is called is inserted in between each pair. |
| 2756 | |
| 2757 | The result is returned as a new bytearray object. |
| 2758 | [clinic start generated code]*/ |
| 2759 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2760 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2761 | bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2762 | /*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2763 | { |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2764 | return stringlib_bytes_join((PyObject*)self, iterable_of_bytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2765 | } |
| 2766 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2767 | /*[clinic input] |
| 2768 | bytearray.splitlines |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2769 | |
Serhiy Storchaka | 8b2e8b6 | 2015-05-30 11:30:39 +0300 | [diff] [blame] | 2770 | keepends: int(c_default="0") = False |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2771 | |
| 2772 | Return a list of the lines in the bytearray, breaking at line boundaries. |
| 2773 | |
| 2774 | Line breaks are not included in the resulting list unless keepends is given and |
| 2775 | true. |
| 2776 | [clinic start generated code]*/ |
| 2777 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2778 | static PyObject * |
| 2779 | bytearray_splitlines_impl(PyByteArrayObject *self, int keepends) |
Serhiy Storchaka | 8b2e8b6 | 2015-05-30 11:30:39 +0300 | [diff] [blame] | 2780 | /*[clinic end generated code: output=4223c94b895f6ad9 input=8ccade941e5ea0bd]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2781 | { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2782 | return stringlib_splitlines( |
| 2783 | (PyObject*) self, PyByteArray_AS_STRING(self), |
| 2784 | PyByteArray_GET_SIZE(self), keepends |
| 2785 | ); |
| 2786 | } |
| 2787 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2788 | /*[clinic input] |
| 2789 | @classmethod |
| 2790 | bytearray.fromhex |
| 2791 | |
| 2792 | cls: self(type="PyObject*") |
| 2793 | string: unicode |
| 2794 | / |
| 2795 | |
| 2796 | Create a bytearray object from a string of hexadecimal numbers. |
| 2797 | |
| 2798 | Spaces between two numbers are accepted. |
| 2799 | Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef') |
| 2800 | [clinic start generated code]*/ |
| 2801 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2802 | static PyObject * |
| 2803 | bytearray_fromhex_impl(PyObject*cls, PyObject *string) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2804 | /*[clinic end generated code: output=df3da60129b3700c input=907bbd2d34d9367a]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2805 | { |
Victor Stinner | 2bf8993 | 2015-10-14 11:25:33 +0200 | [diff] [blame] | 2806 | return _PyBytes_FromHex(string, 1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2807 | } |
| 2808 | |
Gregory P. Smith | 8cb6569 | 2015-04-25 23:22:26 +0000 | [diff] [blame] | 2809 | PyDoc_STRVAR(hex__doc__, |
| 2810 | "B.hex() -> string\n\ |
| 2811 | \n\ |
| 2812 | Create a string of hexadecimal numbers from a bytearray object.\n\ |
| 2813 | Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'."); |
| 2814 | |
| 2815 | static PyObject * |
| 2816 | bytearray_hex(PyBytesObject *self) |
| 2817 | { |
| 2818 | char* argbuf = PyByteArray_AS_STRING(self); |
| 2819 | Py_ssize_t arglen = PyByteArray_GET_SIZE(self); |
| 2820 | return _Py_strhex(argbuf, arglen); |
| 2821 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2822 | |
| 2823 | static PyObject * |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2824 | _common_reduce(PyByteArrayObject *self, int proto) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2825 | { |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2826 | PyObject *dict; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2827 | _Py_IDENTIFIER(__dict__); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2828 | char *buf; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2829 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2830 | dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2831 | if (dict == NULL) { |
| 2832 | PyErr_Clear(); |
| 2833 | dict = Py_None; |
| 2834 | Py_INCREF(dict); |
| 2835 | } |
| 2836 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2837 | buf = PyByteArray_AS_STRING(self); |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2838 | if (proto < 3) { |
| 2839 | /* use str based reduction for backwards compatibility with Python 2.x */ |
| 2840 | PyObject *latin1; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2841 | if (Py_SIZE(self)) |
| 2842 | latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL); |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2843 | else |
| 2844 | latin1 = PyUnicode_FromString(""); |
| 2845 | return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict); |
| 2846 | } |
| 2847 | else { |
| 2848 | /* use more efficient byte based reduction */ |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2849 | if (Py_SIZE(self)) { |
| 2850 | 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] | 2851 | } |
| 2852 | else { |
| 2853 | return Py_BuildValue("(O()N)", Py_TYPE(self), dict); |
| 2854 | } |
| 2855 | } |
| 2856 | } |
| 2857 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2858 | /*[clinic input] |
| 2859 | bytearray.__reduce__ as bytearray_reduce |
| 2860 | |
| 2861 | self: self(type="PyByteArrayObject *") |
| 2862 | |
| 2863 | Return state information for pickling. |
| 2864 | [clinic start generated code]*/ |
| 2865 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2866 | static PyObject * |
| 2867 | bytearray_reduce_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2868 | /*[clinic end generated code: output=52bf304086464cab input=fbb07de4d102a03a]*/ |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2869 | { |
| 2870 | return _common_reduce(self, 2); |
| 2871 | } |
| 2872 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2873 | /*[clinic input] |
| 2874 | bytearray.__reduce_ex__ as bytearray_reduce_ex |
| 2875 | |
| 2876 | self: self(type="PyByteArrayObject *") |
| 2877 | proto: int = 0 |
| 2878 | / |
| 2879 | |
| 2880 | Return state information for pickling. |
| 2881 | [clinic start generated code]*/ |
| 2882 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2883 | static PyObject * |
| 2884 | bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2885 | /*[clinic end generated code: output=52eac33377197520 input=0e091a42ca6dbd91]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2886 | { |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2887 | return _common_reduce(self, proto); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2888 | } |
| 2889 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2890 | /*[clinic input] |
| 2891 | bytearray.__sizeof__ as bytearray_sizeof |
| 2892 | |
| 2893 | self: self(type="PyByteArrayObject *") |
| 2894 | |
| 2895 | Returns the size of the bytearray object in memory, in bytes. |
| 2896 | [clinic start generated code]*/ |
| 2897 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2898 | static PyObject * |
| 2899 | bytearray_sizeof_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2900 | /*[clinic end generated code: output=738abdd17951c427 input=6b23d305362b462b]*/ |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2901 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 2902 | Py_ssize_t res; |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2903 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 2904 | res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char); |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 2905 | return PyLong_FromSsize_t(res); |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2906 | } |
| 2907 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2908 | static PySequenceMethods bytearray_as_sequence = { |
| 2909 | (lenfunc)bytearray_length, /* sq_length */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2910 | (binaryfunc)PyByteArray_Concat, /* sq_concat */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2911 | (ssizeargfunc)bytearray_repeat, /* sq_repeat */ |
| 2912 | (ssizeargfunc)bytearray_getitem, /* sq_item */ |
| 2913 | 0, /* sq_slice */ |
| 2914 | (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */ |
| 2915 | 0, /* sq_ass_slice */ |
| 2916 | (objobjproc)bytearray_contains, /* sq_contains */ |
| 2917 | (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */ |
| 2918 | (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2919 | }; |
| 2920 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2921 | static PyMappingMethods bytearray_as_mapping = { |
| 2922 | (lenfunc)bytearray_length, |
| 2923 | (binaryfunc)bytearray_subscript, |
| 2924 | (objobjargproc)bytearray_ass_subscript, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2925 | }; |
| 2926 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2927 | static PyBufferProcs bytearray_as_buffer = { |
| 2928 | (getbufferproc)bytearray_getbuffer, |
| 2929 | (releasebufferproc)bytearray_releasebuffer, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2930 | }; |
| 2931 | |
| 2932 | static PyMethodDef |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2933 | bytearray_methods[] = { |
| 2934 | {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2935 | BYTEARRAY_REDUCE_METHODDEF |
| 2936 | BYTEARRAY_REDUCE_EX_METHODDEF |
| 2937 | BYTEARRAY_SIZEOF_METHODDEF |
| 2938 | BYTEARRAY_APPEND_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2939 | {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS, |
| 2940 | _Py_capitalize__doc__}, |
| 2941 | {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2942 | BYTEARRAY_CLEAR_METHODDEF |
| 2943 | BYTEARRAY_COPY_METHODDEF |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2944 | {"count", (PyCFunction)bytearray_count, METH_VARARGS, count__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2945 | BYTEARRAY_DECODE_METHODDEF |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2946 | {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, endswith__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 2947 | {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2948 | expandtabs__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2949 | BYTEARRAY_EXTEND_METHODDEF |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2950 | {"find", (PyCFunction)bytearray_find, METH_VARARGS, find__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2951 | BYTEARRAY_FROMHEX_METHODDEF |
Gregory P. Smith | 8cb6569 | 2015-04-25 23:22:26 +0000 | [diff] [blame] | 2952 | {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__}, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2953 | {"index", (PyCFunction)bytearray_index, METH_VARARGS, index__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2954 | BYTEARRAY_INSERT_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2955 | {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS, |
| 2956 | _Py_isalnum__doc__}, |
| 2957 | {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS, |
| 2958 | _Py_isalpha__doc__}, |
| 2959 | {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS, |
| 2960 | _Py_isdigit__doc__}, |
| 2961 | {"islower", (PyCFunction)stringlib_islower, METH_NOARGS, |
| 2962 | _Py_islower__doc__}, |
| 2963 | {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS, |
| 2964 | _Py_isspace__doc__}, |
| 2965 | {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS, |
| 2966 | _Py_istitle__doc__}, |
| 2967 | {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS, |
| 2968 | _Py_isupper__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2969 | BYTEARRAY_JOIN_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2970 | {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__}, |
| 2971 | {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2972 | BYTEARRAY_LSTRIP_METHODDEF |
| 2973 | BYTEARRAY_MAKETRANS_METHODDEF |
| 2974 | BYTEARRAY_PARTITION_METHODDEF |
| 2975 | BYTEARRAY_POP_METHODDEF |
| 2976 | BYTEARRAY_REMOVE_METHODDEF |
| 2977 | BYTEARRAY_REPLACE_METHODDEF |
| 2978 | BYTEARRAY_REVERSE_METHODDEF |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2979 | {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, rfind__doc__}, |
| 2980 | {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, rindex__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2981 | {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2982 | BYTEARRAY_RPARTITION_METHODDEF |
| 2983 | BYTEARRAY_RSPLIT_METHODDEF |
| 2984 | BYTEARRAY_RSTRIP_METHODDEF |
| 2985 | BYTEARRAY_SPLIT_METHODDEF |
| 2986 | BYTEARRAY_SPLITLINES_METHODDEF |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2987 | {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS , |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2988 | startswith__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2989 | BYTEARRAY_STRIP_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2990 | {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS, |
| 2991 | _Py_swapcase__doc__}, |
| 2992 | {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2993 | BYTEARRAY_TRANSLATE_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2994 | {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__}, |
| 2995 | {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__}, |
| 2996 | {NULL} |
| 2997 | }; |
| 2998 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 2999 | static PyObject * |
| 3000 | bytearray_mod(PyObject *v, PyObject *w) |
| 3001 | { |
| 3002 | if (!PyByteArray_Check(v)) |
| 3003 | Py_RETURN_NOTIMPLEMENTED; |
Berker Peksag | 43de36d | 2016-04-16 01:20:47 +0300 | [diff] [blame] | 3004 | 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] | 3005 | } |
| 3006 | |
| 3007 | static PyNumberMethods bytearray_as_number = { |
| 3008 | 0, /*nb_add*/ |
| 3009 | 0, /*nb_subtract*/ |
| 3010 | 0, /*nb_multiply*/ |
| 3011 | bytearray_mod, /*nb_remainder*/ |
| 3012 | }; |
| 3013 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3014 | PyDoc_STRVAR(bytearray_doc, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 3015 | "bytearray(iterable_of_ints) -> bytearray\n\ |
| 3016 | bytearray(string, encoding[, errors]) -> bytearray\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 3017 | bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\ |
| 3018 | bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\ |
| 3019 | bytearray() -> empty bytes array\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3020 | \n\ |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 3021 | Construct a mutable bytearray object from:\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3022 | - an iterable yielding integers in range(256)\n\ |
| 3023 | - a text string encoded using the specified encoding\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 3024 | - a bytes or a buffer object\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3025 | - any object implementing the buffer API.\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 3026 | - an integer"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3027 | |
| 3028 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3029 | static PyObject *bytearray_iter(PyObject *seq); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3030 | |
| 3031 | PyTypeObject PyByteArray_Type = { |
| 3032 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3033 | "bytearray", |
| 3034 | sizeof(PyByteArrayObject), |
| 3035 | 0, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3036 | (destructor)bytearray_dealloc, /* tp_dealloc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3037 | 0, /* tp_print */ |
| 3038 | 0, /* tp_getattr */ |
| 3039 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 3040 | 0, /* tp_reserved */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3041 | (reprfunc)bytearray_repr, /* tp_repr */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 3042 | &bytearray_as_number, /* tp_as_number */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3043 | &bytearray_as_sequence, /* tp_as_sequence */ |
| 3044 | &bytearray_as_mapping, /* tp_as_mapping */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3045 | 0, /* tp_hash */ |
| 3046 | 0, /* tp_call */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3047 | bytearray_str, /* tp_str */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3048 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3049 | 0, /* tp_setattro */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3050 | &bytearray_as_buffer, /* tp_as_buffer */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3051 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3052 | bytearray_doc, /* tp_doc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3053 | 0, /* tp_traverse */ |
| 3054 | 0, /* tp_clear */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3055 | (richcmpfunc)bytearray_richcompare, /* tp_richcompare */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3056 | 0, /* tp_weaklistoffset */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3057 | bytearray_iter, /* tp_iter */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3058 | 0, /* tp_iternext */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3059 | bytearray_methods, /* tp_methods */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3060 | 0, /* tp_members */ |
| 3061 | 0, /* tp_getset */ |
| 3062 | 0, /* tp_base */ |
| 3063 | 0, /* tp_dict */ |
| 3064 | 0, /* tp_descr_get */ |
| 3065 | 0, /* tp_descr_set */ |
| 3066 | 0, /* tp_dictoffset */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3067 | (initproc)bytearray_init, /* tp_init */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3068 | PyType_GenericAlloc, /* tp_alloc */ |
| 3069 | PyType_GenericNew, /* tp_new */ |
| 3070 | PyObject_Del, /* tp_free */ |
| 3071 | }; |
| 3072 | |
| 3073 | /*********************** Bytes Iterator ****************************/ |
| 3074 | |
| 3075 | typedef struct { |
| 3076 | PyObject_HEAD |
| 3077 | Py_ssize_t it_index; |
| 3078 | PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 3079 | } bytesiterobject; |
| 3080 | |
| 3081 | static void |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3082 | bytearrayiter_dealloc(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3083 | { |
| 3084 | _PyObject_GC_UNTRACK(it); |
| 3085 | Py_XDECREF(it->it_seq); |
| 3086 | PyObject_GC_Del(it); |
| 3087 | } |
| 3088 | |
| 3089 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3090 | bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3091 | { |
| 3092 | Py_VISIT(it->it_seq); |
| 3093 | return 0; |
| 3094 | } |
| 3095 | |
| 3096 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3097 | bytearrayiter_next(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3098 | { |
| 3099 | PyByteArrayObject *seq; |
| 3100 | PyObject *item; |
| 3101 | |
| 3102 | assert(it != NULL); |
| 3103 | seq = it->it_seq; |
| 3104 | if (seq == NULL) |
| 3105 | return NULL; |
| 3106 | assert(PyByteArray_Check(seq)); |
| 3107 | |
| 3108 | if (it->it_index < PyByteArray_GET_SIZE(seq)) { |
| 3109 | item = PyLong_FromLong( |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 3110 | (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3111 | if (item != NULL) |
| 3112 | ++it->it_index; |
| 3113 | return item; |
| 3114 | } |
| 3115 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3116 | it->it_seq = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 3117 | Py_DECREF(seq); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3118 | return NULL; |
| 3119 | } |
| 3120 | |
| 3121 | static PyObject * |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3122 | bytearrayiter_length_hint(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3123 | { |
| 3124 | Py_ssize_t len = 0; |
| 3125 | if (it->it_seq) |
| 3126 | len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index; |
| 3127 | return PyLong_FromSsize_t(len); |
| 3128 | } |
| 3129 | |
| 3130 | PyDoc_STRVAR(length_hint_doc, |
| 3131 | "Private method returning an estimate of len(list(it))."); |
| 3132 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3133 | static PyObject * |
| 3134 | bytearrayiter_reduce(bytesiterobject *it) |
| 3135 | { |
| 3136 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 3137 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3138 | it->it_seq, it->it_index); |
| 3139 | } else { |
| 3140 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 3141 | if (u == NULL) |
| 3142 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 3143 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3144 | } |
| 3145 | } |
| 3146 | |
| 3147 | static PyObject * |
| 3148 | bytearrayiter_setstate(bytesiterobject *it, PyObject *state) |
| 3149 | { |
| 3150 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 3151 | if (index == -1 && PyErr_Occurred()) |
| 3152 | return NULL; |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 3153 | if (it->it_seq != NULL) { |
| 3154 | if (index < 0) |
| 3155 | index = 0; |
| 3156 | else if (index > PyByteArray_GET_SIZE(it->it_seq)) |
| 3157 | index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */ |
| 3158 | it->it_index = index; |
| 3159 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3160 | Py_RETURN_NONE; |
| 3161 | } |
| 3162 | |
| 3163 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 3164 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3165 | static PyMethodDef bytearrayiter_methods[] = { |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3166 | {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3167 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3168 | {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3169 | bytearray_reduce__doc__}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3170 | {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O, |
| 3171 | setstate_doc}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3172 | {NULL, NULL} /* sentinel */ |
| 3173 | }; |
| 3174 | |
| 3175 | PyTypeObject PyByteArrayIter_Type = { |
| 3176 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3177 | "bytearray_iterator", /* tp_name */ |
| 3178 | sizeof(bytesiterobject), /* tp_basicsize */ |
| 3179 | 0, /* tp_itemsize */ |
| 3180 | /* methods */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3181 | (destructor)bytearrayiter_dealloc, /* tp_dealloc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3182 | 0, /* tp_print */ |
| 3183 | 0, /* tp_getattr */ |
| 3184 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 3185 | 0, /* tp_reserved */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3186 | 0, /* tp_repr */ |
| 3187 | 0, /* tp_as_number */ |
| 3188 | 0, /* tp_as_sequence */ |
| 3189 | 0, /* tp_as_mapping */ |
| 3190 | 0, /* tp_hash */ |
| 3191 | 0, /* tp_call */ |
| 3192 | 0, /* tp_str */ |
| 3193 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3194 | 0, /* tp_setattro */ |
| 3195 | 0, /* tp_as_buffer */ |
| 3196 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 3197 | 0, /* tp_doc */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3198 | (traverseproc)bytearrayiter_traverse, /* tp_traverse */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3199 | 0, /* tp_clear */ |
| 3200 | 0, /* tp_richcompare */ |
| 3201 | 0, /* tp_weaklistoffset */ |
| 3202 | PyObject_SelfIter, /* tp_iter */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3203 | (iternextfunc)bytearrayiter_next, /* tp_iternext */ |
| 3204 | bytearrayiter_methods, /* tp_methods */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3205 | 0, |
| 3206 | }; |
| 3207 | |
| 3208 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3209 | bytearray_iter(PyObject *seq) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3210 | { |
| 3211 | bytesiterobject *it; |
| 3212 | |
| 3213 | if (!PyByteArray_Check(seq)) { |
| 3214 | PyErr_BadInternalCall(); |
| 3215 | return NULL; |
| 3216 | } |
| 3217 | it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type); |
| 3218 | if (it == NULL) |
| 3219 | return NULL; |
| 3220 | it->it_index = 0; |
| 3221 | Py_INCREF(seq); |
| 3222 | it->it_seq = (PyByteArrayObject *)seq; |
| 3223 | _PyObject_GC_TRACK(it); |
| 3224 | return (PyObject *)it; |
| 3225 | } |