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