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