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 | 413fdce | 2015-11-14 15:42:17 +0200 | [diff] [blame] | 1162 | else if (sub_len == 1) { |
| 1163 | if (dir > 0) |
| 1164 | res = stringlib_find_char( |
| 1165 | PyByteArray_AS_STRING(self) + start, end - start, |
| 1166 | *sub); |
| 1167 | else |
| 1168 | res = stringlib_rfind_char( |
| 1169 | PyByteArray_AS_STRING(self) + start, end - start, |
| 1170 | *sub); |
Serhiy Storchaka | d9d769f | 2015-03-24 21:55:47 +0200 | [diff] [blame] | 1171 | if (res >= 0) |
| 1172 | res += start; |
| 1173 | } |
| 1174 | else { |
| 1175 | if (dir > 0) |
| 1176 | res = stringlib_find_slice( |
| 1177 | PyByteArray_AS_STRING(self), len, |
| 1178 | sub, sub_len, start, end); |
| 1179 | else |
| 1180 | res = stringlib_rfind_slice( |
| 1181 | PyByteArray_AS_STRING(self), len, |
| 1182 | sub, sub_len, start, end); |
| 1183 | } |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1184 | |
| 1185 | if (subobj) |
| 1186 | PyBuffer_Release(&subbuf); |
| 1187 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1188 | return res; |
| 1189 | } |
| 1190 | |
| 1191 | PyDoc_STRVAR(find__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1192 | "B.find(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1193 | \n\ |
| 1194 | Return the lowest index in B where subsection sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 1195 | such that sub is contained within B[start,end]. Optional\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1196 | arguments start and end are interpreted as in slice notation.\n\ |
| 1197 | \n\ |
| 1198 | Return -1 on failure."); |
| 1199 | |
| 1200 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1201 | bytearray_find(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1202 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1203 | Py_ssize_t result = bytearray_find_internal(self, args, +1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1204 | if (result == -2) |
| 1205 | return NULL; |
| 1206 | return PyLong_FromSsize_t(result); |
| 1207 | } |
| 1208 | |
| 1209 | PyDoc_STRVAR(count__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1210 | "B.count(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1211 | \n\ |
| 1212 | Return the number of non-overlapping occurrences of subsection sub in\n\ |
| 1213 | bytes B[start:end]. Optional arguments start and end are interpreted\n\ |
| 1214 | as in slice notation."); |
| 1215 | |
| 1216 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1217 | bytearray_count(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1218 | { |
| 1219 | PyObject *sub_obj; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1220 | const char *str = PyByteArray_AS_STRING(self), *sub; |
| 1221 | Py_ssize_t sub_len; |
| 1222 | char byte; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1223 | Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1224 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1225 | Py_buffer vsub; |
| 1226 | PyObject *count_obj; |
| 1227 | |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1228 | if (!stringlib_parse_args_finds_byte("count", args, &sub_obj, &byte, |
| 1229 | &start, &end)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1230 | return NULL; |
| 1231 | |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1232 | if (sub_obj) { |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1233 | if (PyObject_GetBuffer(sub_obj, &vsub, PyBUF_SIMPLE) != 0) |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1234 | return NULL; |
| 1235 | |
| 1236 | sub = vsub.buf; |
| 1237 | sub_len = vsub.len; |
| 1238 | } |
| 1239 | else { |
| 1240 | sub = &byte; |
| 1241 | sub_len = 1; |
| 1242 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1243 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1244 | ADJUST_INDICES(start, end, PyByteArray_GET_SIZE(self)); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1245 | |
| 1246 | count_obj = PyLong_FromSsize_t( |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1247 | 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] | 1248 | ); |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1249 | |
| 1250 | if (sub_obj) |
| 1251 | PyBuffer_Release(&vsub); |
| 1252 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1253 | return count_obj; |
| 1254 | } |
| 1255 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1256 | /*[clinic input] |
| 1257 | bytearray.clear |
| 1258 | |
| 1259 | self: self(type="PyByteArrayObject *") |
| 1260 | |
| 1261 | Remove all items from the bytearray. |
| 1262 | [clinic start generated code]*/ |
| 1263 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1264 | static PyObject * |
| 1265 | bytearray_clear_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1266 | /*[clinic end generated code: output=85c2fe6aede0956c input=e524fd330abcdc18]*/ |
Eli Bendersky | 4db28d3 | 2011-03-03 18:21:02 +0000 | [diff] [blame] | 1267 | { |
| 1268 | if (PyByteArray_Resize((PyObject *)self, 0) < 0) |
| 1269 | return NULL; |
| 1270 | Py_RETURN_NONE; |
| 1271 | } |
| 1272 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1273 | /*[clinic input] |
| 1274 | bytearray.copy |
| 1275 | |
| 1276 | self: self(type="PyByteArrayObject *") |
| 1277 | |
| 1278 | Return a copy of B. |
| 1279 | [clinic start generated code]*/ |
| 1280 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1281 | static PyObject * |
| 1282 | bytearray_copy_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1283 | /*[clinic end generated code: output=68cfbcfed484c132 input=6d5d2975aa0f33f3]*/ |
Eli Bendersky | 4db28d3 | 2011-03-03 18:21:02 +0000 | [diff] [blame] | 1284 | { |
| 1285 | return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self), |
| 1286 | PyByteArray_GET_SIZE(self)); |
| 1287 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1288 | |
| 1289 | PyDoc_STRVAR(index__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1290 | "B.index(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1291 | \n\ |
| 1292 | Like B.find() but raise ValueError when the subsection is not found."); |
| 1293 | |
| 1294 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1295 | bytearray_index(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1296 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1297 | Py_ssize_t result = bytearray_find_internal(self, args, +1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1298 | if (result == -2) |
| 1299 | return NULL; |
| 1300 | if (result == -1) { |
| 1301 | PyErr_SetString(PyExc_ValueError, |
| 1302 | "subsection not found"); |
| 1303 | return NULL; |
| 1304 | } |
| 1305 | return PyLong_FromSsize_t(result); |
| 1306 | } |
| 1307 | |
| 1308 | |
| 1309 | PyDoc_STRVAR(rfind__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1310 | "B.rfind(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1311 | \n\ |
| 1312 | Return the highest index in B where subsection sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 1313 | such that sub is contained within B[start,end]. Optional\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1314 | arguments start and end are interpreted as in slice notation.\n\ |
| 1315 | \n\ |
| 1316 | Return -1 on failure."); |
| 1317 | |
| 1318 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1319 | bytearray_rfind(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1320 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1321 | Py_ssize_t result = bytearray_find_internal(self, args, -1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1322 | if (result == -2) |
| 1323 | return NULL; |
| 1324 | return PyLong_FromSsize_t(result); |
| 1325 | } |
| 1326 | |
| 1327 | |
| 1328 | PyDoc_STRVAR(rindex__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1329 | "B.rindex(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1330 | \n\ |
| 1331 | Like B.rfind() but raise ValueError when the subsection is not found."); |
| 1332 | |
| 1333 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1334 | bytearray_rindex(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1335 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1336 | Py_ssize_t result = bytearray_find_internal(self, args, -1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1337 | if (result == -2) |
| 1338 | return NULL; |
| 1339 | if (result == -1) { |
| 1340 | PyErr_SetString(PyExc_ValueError, |
| 1341 | "subsection not found"); |
| 1342 | return NULL; |
| 1343 | } |
| 1344 | return PyLong_FromSsize_t(result); |
| 1345 | } |
| 1346 | |
| 1347 | |
| 1348 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1349 | bytearray_contains(PyObject *self, PyObject *arg) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1350 | { |
| 1351 | Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError); |
| 1352 | if (ival == -1 && PyErr_Occurred()) { |
| 1353 | Py_buffer varg; |
Antoine Pitrou | 0010d37 | 2010-08-15 17:12:55 +0000 | [diff] [blame] | 1354 | Py_ssize_t pos; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1355 | PyErr_Clear(); |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1356 | if (PyObject_GetBuffer(arg, &varg, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1357 | return -1; |
| 1358 | pos = stringlib_find(PyByteArray_AS_STRING(self), Py_SIZE(self), |
| 1359 | varg.buf, varg.len, 0); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1360 | PyBuffer_Release(&varg); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1361 | return pos >= 0; |
| 1362 | } |
| 1363 | if (ival < 0 || ival >= 256) { |
| 1364 | PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
| 1365 | return -1; |
| 1366 | } |
| 1367 | |
Antoine Pitrou | 0010d37 | 2010-08-15 17:12:55 +0000 | [diff] [blame] | 1368 | return memchr(PyByteArray_AS_STRING(self), (int) ival, Py_SIZE(self)) != NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | |
| 1372 | /* Matches the end (direction >= 0) or start (direction < 0) of self |
| 1373 | * against substr, using the start and end arguments. Returns |
| 1374 | * -1 on error, 0 if not found and 1 if found. |
| 1375 | */ |
| 1376 | Py_LOCAL(int) |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1377 | _bytearray_tailmatch(PyByteArrayObject *self, PyObject *substr, Py_ssize_t start, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1378 | Py_ssize_t end, int direction) |
| 1379 | { |
| 1380 | Py_ssize_t len = PyByteArray_GET_SIZE(self); |
| 1381 | const char* str; |
| 1382 | Py_buffer vsubstr; |
| 1383 | int rv = 0; |
| 1384 | |
| 1385 | str = PyByteArray_AS_STRING(self); |
| 1386 | |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1387 | if (PyObject_GetBuffer(substr, &vsubstr, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1388 | return -1; |
| 1389 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1390 | ADJUST_INDICES(start, end, len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1391 | |
| 1392 | if (direction < 0) { |
| 1393 | /* startswith */ |
| 1394 | if (start+vsubstr.len > len) { |
| 1395 | goto done; |
| 1396 | } |
| 1397 | } else { |
| 1398 | /* endswith */ |
| 1399 | if (end-start < vsubstr.len || start > len) { |
| 1400 | goto done; |
| 1401 | } |
| 1402 | |
| 1403 | if (end-vsubstr.len > start) |
| 1404 | start = end - vsubstr.len; |
| 1405 | } |
| 1406 | if (end-start >= vsubstr.len) |
| 1407 | rv = ! memcmp(str+start, vsubstr.buf, vsubstr.len); |
| 1408 | |
| 1409 | done: |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1410 | PyBuffer_Release(&vsubstr); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1411 | return rv; |
| 1412 | } |
| 1413 | |
| 1414 | |
| 1415 | PyDoc_STRVAR(startswith__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1416 | "B.startswith(prefix[, start[, end]]) -> bool\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1417 | \n\ |
| 1418 | Return True if B starts with the specified prefix, False otherwise.\n\ |
| 1419 | With optional start, test B beginning at that position.\n\ |
| 1420 | With optional end, stop comparing B at that position.\n\ |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 1421 | prefix can also be a tuple of bytes to try."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1422 | |
| 1423 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1424 | bytearray_startswith(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1425 | { |
| 1426 | Py_ssize_t start = 0; |
| 1427 | Py_ssize_t end = PY_SSIZE_T_MAX; |
| 1428 | PyObject *subobj; |
| 1429 | int result; |
| 1430 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 1431 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1432 | return NULL; |
| 1433 | if (PyTuple_Check(subobj)) { |
| 1434 | Py_ssize_t i; |
| 1435 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1436 | result = _bytearray_tailmatch(self, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1437 | PyTuple_GET_ITEM(subobj, i), |
| 1438 | start, end, -1); |
| 1439 | if (result == -1) |
| 1440 | return NULL; |
| 1441 | else if (result) { |
| 1442 | Py_RETURN_TRUE; |
| 1443 | } |
| 1444 | } |
| 1445 | Py_RETURN_FALSE; |
| 1446 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1447 | result = _bytearray_tailmatch(self, subobj, start, end, -1); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 1448 | if (result == -1) { |
| 1449 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1450 | PyErr_Format(PyExc_TypeError, "startswith first arg must be bytes " |
| 1451 | "or a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1452 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 1453 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1454 | else |
| 1455 | return PyBool_FromLong(result); |
| 1456 | } |
| 1457 | |
| 1458 | PyDoc_STRVAR(endswith__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1459 | "B.endswith(suffix[, start[, end]]) -> bool\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1460 | \n\ |
| 1461 | Return True if B ends with the specified suffix, False otherwise.\n\ |
| 1462 | With optional start, test B beginning at that position.\n\ |
| 1463 | With optional end, stop comparing B at that position.\n\ |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 1464 | suffix can also be a tuple of bytes to try."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1465 | |
| 1466 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1467 | bytearray_endswith(PyByteArrayObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1468 | { |
| 1469 | Py_ssize_t start = 0; |
| 1470 | Py_ssize_t end = PY_SSIZE_T_MAX; |
| 1471 | PyObject *subobj; |
| 1472 | int result; |
| 1473 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 1474 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1475 | return NULL; |
| 1476 | if (PyTuple_Check(subobj)) { |
| 1477 | Py_ssize_t i; |
| 1478 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1479 | result = _bytearray_tailmatch(self, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1480 | PyTuple_GET_ITEM(subobj, i), |
| 1481 | start, end, +1); |
| 1482 | if (result == -1) |
| 1483 | return NULL; |
| 1484 | else if (result) { |
| 1485 | Py_RETURN_TRUE; |
| 1486 | } |
| 1487 | } |
| 1488 | Py_RETURN_FALSE; |
| 1489 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1490 | result = _bytearray_tailmatch(self, subobj, start, end, +1); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 1491 | if (result == -1) { |
| 1492 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1493 | PyErr_Format(PyExc_TypeError, "endswith first arg must be bytes or " |
| 1494 | "a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1495 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 1496 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1497 | else |
| 1498 | return PyBool_FromLong(result); |
| 1499 | } |
| 1500 | |
| 1501 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1502 | /*[clinic input] |
| 1503 | bytearray.translate |
| 1504 | |
| 1505 | self: self(type="PyByteArrayObject *") |
| 1506 | table: object |
| 1507 | Translation table, which must be a bytes object of length 256. |
| 1508 | [ |
| 1509 | deletechars: object |
| 1510 | ] |
| 1511 | / |
| 1512 | |
| 1513 | Return a copy with each character mapped by the given translation table. |
| 1514 | |
| 1515 | All characters occurring in the optional argument deletechars are removed. |
| 1516 | The remaining characters are mapped through the given translation table. |
| 1517 | [clinic start generated code]*/ |
| 1518 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1519 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1520 | bytearray_translate_impl(PyByteArrayObject *self, PyObject *table, |
| 1521 | int group_right_1, PyObject *deletechars) |
| 1522 | /*[clinic end generated code: output=2bebc86a9a1ff083 input=b749ad85f4860824]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1523 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1524 | char *input, *output; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1525 | const char *table_chars; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1526 | Py_ssize_t i, c; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1527 | PyObject *input_obj = (PyObject*)self; |
| 1528 | const char *output_start; |
| 1529 | Py_ssize_t inlen; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1530 | PyObject *result = NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1531 | int trans_table[256]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1532 | Py_buffer vtable, vdel; |
| 1533 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1534 | if (table == Py_None) { |
| 1535 | table_chars = NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1536 | table = NULL; |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1537 | } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1538 | return NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1539 | } else { |
| 1540 | if (vtable.len != 256) { |
| 1541 | PyErr_SetString(PyExc_ValueError, |
| 1542 | "translation table must be 256 characters long"); |
Georg Brandl | 953152f | 2009-07-22 12:03:59 +0000 | [diff] [blame] | 1543 | PyBuffer_Release(&vtable); |
| 1544 | return NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1545 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1546 | table_chars = (const char*)vtable.buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1549 | if (deletechars != NULL) { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1550 | if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1551 | if (table != NULL) |
Georg Brandl | 953152f | 2009-07-22 12:03:59 +0000 | [diff] [blame] | 1552 | PyBuffer_Release(&vtable); |
| 1553 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1554 | } |
| 1555 | } |
| 1556 | else { |
| 1557 | vdel.buf = NULL; |
| 1558 | vdel.len = 0; |
| 1559 | } |
| 1560 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1561 | inlen = PyByteArray_GET_SIZE(input_obj); |
| 1562 | result = PyByteArray_FromStringAndSize((char *)NULL, inlen); |
| 1563 | if (result == NULL) |
| 1564 | goto done; |
| 1565 | output_start = output = PyByteArray_AsString(result); |
| 1566 | input = PyByteArray_AS_STRING(input_obj); |
| 1567 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1568 | if (vdel.len == 0 && table_chars != NULL) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1569 | /* If no deletions are required, use faster code */ |
| 1570 | for (i = inlen; --i >= 0; ) { |
| 1571 | c = Py_CHARMASK(*input++); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1572 | *output++ = table_chars[c]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1573 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1574 | goto done; |
| 1575 | } |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1576 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1577 | if (table_chars == NULL) { |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1578 | for (i = 0; i < 256; i++) |
| 1579 | trans_table[i] = Py_CHARMASK(i); |
| 1580 | } else { |
| 1581 | for (i = 0; i < 256; i++) |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1582 | trans_table[i] = Py_CHARMASK(table_chars[i]); |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1583 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1584 | |
| 1585 | for (i = 0; i < vdel.len; i++) |
| 1586 | trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1; |
| 1587 | |
| 1588 | for (i = inlen; --i >= 0; ) { |
| 1589 | c = Py_CHARMASK(*input++); |
| 1590 | if (trans_table[c] != -1) |
| 1591 | if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
| 1592 | continue; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1593 | } |
| 1594 | /* Fix the size of the resulting string */ |
| 1595 | if (inlen > 0) |
Christian Heimes | c731bbe | 2013-07-21 02:04:35 +0200 | [diff] [blame] | 1596 | if (PyByteArray_Resize(result, output - output_start) < 0) { |
| 1597 | Py_CLEAR(result); |
| 1598 | goto done; |
| 1599 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1600 | |
| 1601 | done: |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1602 | if (table != NULL) |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1603 | PyBuffer_Release(&vtable); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1604 | if (deletechars != NULL) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1605 | PyBuffer_Release(&vdel); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1606 | return result; |
| 1607 | } |
| 1608 | |
| 1609 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1610 | /*[clinic input] |
| 1611 | |
| 1612 | @staticmethod |
| 1613 | bytearray.maketrans |
| 1614 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1615 | frm: Py_buffer |
| 1616 | to: Py_buffer |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1617 | / |
| 1618 | |
| 1619 | Return a translation table useable for the bytes or bytearray translate method. |
| 1620 | |
| 1621 | The returned table will be one where each byte in frm is mapped to the byte at |
| 1622 | the same position in to. |
| 1623 | |
| 1624 | The bytes objects frm and to must be of the same length. |
| 1625 | [clinic start generated code]*/ |
| 1626 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1627 | static PyObject * |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1628 | bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1629 | /*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1630 | { |
| 1631 | return _Py_bytes_maketrans(frm, to); |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1635 | /* find and count characters and substrings */ |
| 1636 | |
| 1637 | #define findchar(target, target_len, c) \ |
| 1638 | ((char *)memchr((const void *)(target), c, target_len)) |
| 1639 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1640 | |
Benjamin Peterson | 0f3641c | 2008-11-19 22:05:52 +0000 | [diff] [blame] | 1641 | /* Bytes ops must return a string, create a copy */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1642 | Py_LOCAL(PyByteArrayObject *) |
| 1643 | return_self(PyByteArrayObject *self) |
| 1644 | { |
Georg Brandl | 1e7217d | 2008-05-30 12:02:38 +0000 | [diff] [blame] | 1645 | /* always return a new bytearray */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1646 | return (PyByteArrayObject *)PyByteArray_FromStringAndSize( |
| 1647 | PyByteArray_AS_STRING(self), |
| 1648 | PyByteArray_GET_SIZE(self)); |
| 1649 | } |
| 1650 | |
| 1651 | Py_LOCAL_INLINE(Py_ssize_t) |
| 1652 | countchar(const char *target, Py_ssize_t target_len, char c, Py_ssize_t maxcount) |
| 1653 | { |
| 1654 | Py_ssize_t count=0; |
| 1655 | const char *start=target; |
| 1656 | const char *end=target+target_len; |
| 1657 | |
| 1658 | while ( (start=findchar(start, end-start, c)) != NULL ) { |
| 1659 | count++; |
| 1660 | if (count >= maxcount) |
| 1661 | break; |
| 1662 | start += 1; |
| 1663 | } |
| 1664 | return count; |
| 1665 | } |
| 1666 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1667 | |
| 1668 | /* Algorithms for different cases of string replacement */ |
| 1669 | |
| 1670 | /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */ |
| 1671 | Py_LOCAL(PyByteArrayObject *) |
| 1672 | replace_interleave(PyByteArrayObject *self, |
| 1673 | const char *to_s, Py_ssize_t to_len, |
| 1674 | Py_ssize_t maxcount) |
| 1675 | { |
| 1676 | char *self_s, *result_s; |
| 1677 | Py_ssize_t self_len, result_len; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1678 | Py_ssize_t count, i; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1679 | PyByteArrayObject *result; |
| 1680 | |
| 1681 | self_len = PyByteArray_GET_SIZE(self); |
| 1682 | |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1683 | /* 1 at the end plus 1 after every character; |
| 1684 | count = min(maxcount, self_len + 1) */ |
| 1685 | if (maxcount <= self_len) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1686 | count = maxcount; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1687 | else |
| 1688 | /* Can't overflow: self_len + 1 <= maxcount <= PY_SSIZE_T_MAX. */ |
| 1689 | count = self_len + 1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1690 | |
| 1691 | /* Check for overflow */ |
| 1692 | /* result_len = count * to_len + self_len; */ |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1693 | assert(count > 0); |
| 1694 | if (to_len > (PY_SSIZE_T_MAX - self_len) / count) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1695 | PyErr_SetString(PyExc_OverflowError, |
| 1696 | "replace string is too long"); |
| 1697 | return NULL; |
| 1698 | } |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1699 | result_len = count * to_len + self_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1700 | |
| 1701 | if (! (result = (PyByteArrayObject *) |
| 1702 | PyByteArray_FromStringAndSize(NULL, result_len)) ) |
| 1703 | return NULL; |
| 1704 | |
| 1705 | self_s = PyByteArray_AS_STRING(self); |
| 1706 | result_s = PyByteArray_AS_STRING(result); |
| 1707 | |
| 1708 | /* TODO: special case single character, which doesn't need memcpy */ |
| 1709 | |
| 1710 | /* Lay the first one down (guaranteed this will occur) */ |
| 1711 | Py_MEMCPY(result_s, to_s, to_len); |
| 1712 | result_s += to_len; |
| 1713 | count -= 1; |
| 1714 | |
| 1715 | for (i=0; i<count; i++) { |
| 1716 | *result_s++ = *self_s++; |
| 1717 | Py_MEMCPY(result_s, to_s, to_len); |
| 1718 | result_s += to_len; |
| 1719 | } |
| 1720 | |
| 1721 | /* Copy the rest of the original string */ |
| 1722 | Py_MEMCPY(result_s, self_s, self_len-i); |
| 1723 | |
| 1724 | return result; |
| 1725 | } |
| 1726 | |
| 1727 | /* Special case for deleting a single character */ |
| 1728 | /* len(self)>=1, len(from)==1, to="", maxcount>=1 */ |
| 1729 | Py_LOCAL(PyByteArrayObject *) |
| 1730 | replace_delete_single_character(PyByteArrayObject *self, |
| 1731 | char from_c, Py_ssize_t maxcount) |
| 1732 | { |
| 1733 | char *self_s, *result_s; |
| 1734 | char *start, *next, *end; |
| 1735 | Py_ssize_t self_len, result_len; |
| 1736 | Py_ssize_t count; |
| 1737 | PyByteArrayObject *result; |
| 1738 | |
| 1739 | self_len = PyByteArray_GET_SIZE(self); |
| 1740 | self_s = PyByteArray_AS_STRING(self); |
| 1741 | |
| 1742 | count = countchar(self_s, self_len, from_c, maxcount); |
| 1743 | if (count == 0) { |
| 1744 | return return_self(self); |
| 1745 | } |
| 1746 | |
| 1747 | result_len = self_len - count; /* from_len == 1 */ |
| 1748 | assert(result_len>=0); |
| 1749 | |
| 1750 | if ( (result = (PyByteArrayObject *) |
| 1751 | PyByteArray_FromStringAndSize(NULL, result_len)) == NULL) |
| 1752 | return NULL; |
| 1753 | result_s = PyByteArray_AS_STRING(result); |
| 1754 | |
| 1755 | start = self_s; |
| 1756 | end = self_s + self_len; |
| 1757 | while (count-- > 0) { |
| 1758 | next = findchar(start, end-start, from_c); |
| 1759 | if (next == NULL) |
| 1760 | break; |
| 1761 | Py_MEMCPY(result_s, start, next-start); |
| 1762 | result_s += (next-start); |
| 1763 | start = next+1; |
| 1764 | } |
| 1765 | Py_MEMCPY(result_s, start, end-start); |
| 1766 | |
| 1767 | return result; |
| 1768 | } |
| 1769 | |
| 1770 | /* len(self)>=1, len(from)>=2, to="", maxcount>=1 */ |
| 1771 | |
| 1772 | Py_LOCAL(PyByteArrayObject *) |
| 1773 | replace_delete_substring(PyByteArrayObject *self, |
| 1774 | const char *from_s, Py_ssize_t from_len, |
| 1775 | Py_ssize_t maxcount) |
| 1776 | { |
| 1777 | char *self_s, *result_s; |
| 1778 | char *start, *next, *end; |
| 1779 | Py_ssize_t self_len, result_len; |
| 1780 | Py_ssize_t count, offset; |
| 1781 | PyByteArrayObject *result; |
| 1782 | |
| 1783 | self_len = PyByteArray_GET_SIZE(self); |
| 1784 | self_s = PyByteArray_AS_STRING(self); |
| 1785 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1786 | count = stringlib_count(self_s, self_len, |
| 1787 | from_s, from_len, |
| 1788 | maxcount); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1789 | |
| 1790 | if (count == 0) { |
| 1791 | /* no matches */ |
| 1792 | return return_self(self); |
| 1793 | } |
| 1794 | |
| 1795 | result_len = self_len - (count * from_len); |
| 1796 | assert (result_len>=0); |
| 1797 | |
| 1798 | if ( (result = (PyByteArrayObject *) |
| 1799 | PyByteArray_FromStringAndSize(NULL, result_len)) == NULL ) |
| 1800 | return NULL; |
| 1801 | |
| 1802 | result_s = PyByteArray_AS_STRING(result); |
| 1803 | |
| 1804 | start = self_s; |
| 1805 | end = self_s + self_len; |
| 1806 | while (count-- > 0) { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1807 | offset = stringlib_find(start, end-start, |
| 1808 | from_s, from_len, |
| 1809 | 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1810 | if (offset == -1) |
| 1811 | break; |
| 1812 | next = start + offset; |
| 1813 | |
| 1814 | Py_MEMCPY(result_s, start, next-start); |
| 1815 | |
| 1816 | result_s += (next-start); |
| 1817 | start = next+from_len; |
| 1818 | } |
| 1819 | Py_MEMCPY(result_s, start, end-start); |
| 1820 | return result; |
| 1821 | } |
| 1822 | |
| 1823 | /* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */ |
| 1824 | Py_LOCAL(PyByteArrayObject *) |
| 1825 | replace_single_character_in_place(PyByteArrayObject *self, |
| 1826 | char from_c, char to_c, |
| 1827 | Py_ssize_t maxcount) |
| 1828 | { |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1829 | char *self_s, *result_s, *start, *end, *next; |
| 1830 | Py_ssize_t self_len; |
| 1831 | PyByteArrayObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1832 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1833 | /* The result string will be the same size */ |
| 1834 | self_s = PyByteArray_AS_STRING(self); |
| 1835 | self_len = PyByteArray_GET_SIZE(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1836 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1837 | next = findchar(self_s, self_len, from_c); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1838 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1839 | if (next == NULL) { |
| 1840 | /* No matches; return the original bytes */ |
| 1841 | return return_self(self); |
| 1842 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1843 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1844 | /* Need to make a new bytes */ |
| 1845 | result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len); |
| 1846 | if (result == NULL) |
| 1847 | return NULL; |
| 1848 | result_s = PyByteArray_AS_STRING(result); |
| 1849 | Py_MEMCPY(result_s, self_s, self_len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1850 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1851 | /* change everything in-place, starting with this one */ |
| 1852 | start = result_s + (next-self_s); |
| 1853 | *start = to_c; |
| 1854 | start++; |
| 1855 | end = result_s + self_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1856 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1857 | while (--maxcount > 0) { |
| 1858 | next = findchar(start, end-start, from_c); |
| 1859 | if (next == NULL) |
| 1860 | break; |
| 1861 | *next = to_c; |
| 1862 | start = next+1; |
| 1863 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1864 | |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1865 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | /* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */ |
| 1869 | Py_LOCAL(PyByteArrayObject *) |
| 1870 | replace_substring_in_place(PyByteArrayObject *self, |
| 1871 | const char *from_s, Py_ssize_t from_len, |
| 1872 | const char *to_s, Py_ssize_t to_len, |
| 1873 | Py_ssize_t maxcount) |
| 1874 | { |
| 1875 | char *result_s, *start, *end; |
| 1876 | char *self_s; |
| 1877 | Py_ssize_t self_len, offset; |
| 1878 | PyByteArrayObject *result; |
| 1879 | |
| 1880 | /* The result bytes will be the same size */ |
| 1881 | |
| 1882 | self_s = PyByteArray_AS_STRING(self); |
| 1883 | self_len = PyByteArray_GET_SIZE(self); |
| 1884 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1885 | offset = stringlib_find(self_s, self_len, |
| 1886 | from_s, from_len, |
| 1887 | 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1888 | if (offset == -1) { |
| 1889 | /* No matches; return the original bytes */ |
| 1890 | return return_self(self); |
| 1891 | } |
| 1892 | |
| 1893 | /* Need to make a new bytes */ |
| 1894 | result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len); |
| 1895 | if (result == NULL) |
| 1896 | return NULL; |
| 1897 | result_s = PyByteArray_AS_STRING(result); |
| 1898 | Py_MEMCPY(result_s, self_s, self_len); |
| 1899 | |
| 1900 | /* change everything in-place, starting with this one */ |
| 1901 | start = result_s + offset; |
| 1902 | Py_MEMCPY(start, to_s, from_len); |
| 1903 | start += from_len; |
| 1904 | end = result_s + self_len; |
| 1905 | |
| 1906 | while ( --maxcount > 0) { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1907 | offset = stringlib_find(start, end-start, |
| 1908 | from_s, from_len, |
| 1909 | 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1910 | if (offset==-1) |
| 1911 | break; |
| 1912 | Py_MEMCPY(start+offset, to_s, from_len); |
| 1913 | start += offset+from_len; |
| 1914 | } |
| 1915 | |
| 1916 | return result; |
| 1917 | } |
| 1918 | |
| 1919 | /* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */ |
| 1920 | Py_LOCAL(PyByteArrayObject *) |
| 1921 | replace_single_character(PyByteArrayObject *self, |
| 1922 | char from_c, |
| 1923 | const char *to_s, Py_ssize_t to_len, |
| 1924 | Py_ssize_t maxcount) |
| 1925 | { |
| 1926 | char *self_s, *result_s; |
| 1927 | char *start, *next, *end; |
| 1928 | Py_ssize_t self_len, result_len; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1929 | Py_ssize_t count; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1930 | PyByteArrayObject *result; |
| 1931 | |
| 1932 | self_s = PyByteArray_AS_STRING(self); |
| 1933 | self_len = PyByteArray_GET_SIZE(self); |
| 1934 | |
| 1935 | count = countchar(self_s, self_len, from_c, maxcount); |
| 1936 | if (count == 0) { |
| 1937 | /* no matches, return unchanged */ |
| 1938 | return return_self(self); |
| 1939 | } |
| 1940 | |
| 1941 | /* use the difference between current and new, hence the "-1" */ |
| 1942 | /* result_len = self_len + count * (to_len-1) */ |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1943 | assert(count > 0); |
| 1944 | if (to_len - 1 > (PY_SSIZE_T_MAX - self_len) / count) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1945 | PyErr_SetString(PyExc_OverflowError, "replace bytes is too long"); |
| 1946 | return NULL; |
| 1947 | } |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1948 | result_len = self_len + count * (to_len - 1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1949 | |
| 1950 | if ( (result = (PyByteArrayObject *) |
| 1951 | PyByteArray_FromStringAndSize(NULL, result_len)) == NULL) |
| 1952 | return NULL; |
| 1953 | result_s = PyByteArray_AS_STRING(result); |
| 1954 | |
| 1955 | start = self_s; |
| 1956 | end = self_s + self_len; |
| 1957 | while (count-- > 0) { |
| 1958 | next = findchar(start, end-start, from_c); |
| 1959 | if (next == NULL) |
| 1960 | break; |
| 1961 | |
| 1962 | if (next == start) { |
| 1963 | /* replace with the 'to' */ |
| 1964 | Py_MEMCPY(result_s, to_s, to_len); |
| 1965 | result_s += to_len; |
| 1966 | start += 1; |
| 1967 | } else { |
| 1968 | /* copy the unchanged old then the 'to' */ |
| 1969 | Py_MEMCPY(result_s, start, next-start); |
| 1970 | result_s += (next-start); |
| 1971 | Py_MEMCPY(result_s, to_s, to_len); |
| 1972 | result_s += to_len; |
| 1973 | start = next+1; |
| 1974 | } |
| 1975 | } |
| 1976 | /* Copy the remainder of the remaining bytes */ |
| 1977 | Py_MEMCPY(result_s, start, end-start); |
| 1978 | |
| 1979 | return result; |
| 1980 | } |
| 1981 | |
| 1982 | /* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */ |
| 1983 | Py_LOCAL(PyByteArrayObject *) |
| 1984 | replace_substring(PyByteArrayObject *self, |
| 1985 | const char *from_s, Py_ssize_t from_len, |
| 1986 | const char *to_s, Py_ssize_t to_len, |
| 1987 | Py_ssize_t maxcount) |
| 1988 | { |
| 1989 | char *self_s, *result_s; |
| 1990 | char *start, *next, *end; |
| 1991 | Py_ssize_t self_len, result_len; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1992 | Py_ssize_t count, offset; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1993 | PyByteArrayObject *result; |
| 1994 | |
| 1995 | self_s = PyByteArray_AS_STRING(self); |
| 1996 | self_len = PyByteArray_GET_SIZE(self); |
| 1997 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1998 | count = stringlib_count(self_s, self_len, |
| 1999 | from_s, from_len, |
| 2000 | maxcount); |
| 2001 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2002 | if (count == 0) { |
| 2003 | /* no matches, return unchanged */ |
| 2004 | return return_self(self); |
| 2005 | } |
| 2006 | |
| 2007 | /* Check for overflow */ |
| 2008 | /* result_len = self_len + count * (to_len-from_len) */ |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2009 | assert(count > 0); |
| 2010 | if (to_len - from_len > (PY_SSIZE_T_MAX - self_len) / count) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2011 | PyErr_SetString(PyExc_OverflowError, "replace bytes is too long"); |
| 2012 | return NULL; |
| 2013 | } |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2014 | result_len = self_len + count * (to_len - from_len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2015 | |
| 2016 | if ( (result = (PyByteArrayObject *) |
| 2017 | PyByteArray_FromStringAndSize(NULL, result_len)) == NULL) |
| 2018 | return NULL; |
| 2019 | result_s = PyByteArray_AS_STRING(result); |
| 2020 | |
| 2021 | start = self_s; |
| 2022 | end = self_s + self_len; |
| 2023 | while (count-- > 0) { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2024 | offset = stringlib_find(start, end-start, |
| 2025 | from_s, from_len, |
| 2026 | 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2027 | if (offset == -1) |
| 2028 | break; |
| 2029 | next = start+offset; |
| 2030 | if (next == start) { |
| 2031 | /* replace with the 'to' */ |
| 2032 | Py_MEMCPY(result_s, to_s, to_len); |
| 2033 | result_s += to_len; |
| 2034 | start += from_len; |
| 2035 | } else { |
| 2036 | /* copy the unchanged old then the 'to' */ |
| 2037 | Py_MEMCPY(result_s, start, next-start); |
| 2038 | result_s += (next-start); |
| 2039 | Py_MEMCPY(result_s, to_s, to_len); |
| 2040 | result_s += to_len; |
| 2041 | start = next+from_len; |
| 2042 | } |
| 2043 | } |
| 2044 | /* Copy the remainder of the remaining bytes */ |
| 2045 | Py_MEMCPY(result_s, start, end-start); |
| 2046 | |
| 2047 | return result; |
| 2048 | } |
| 2049 | |
| 2050 | |
| 2051 | Py_LOCAL(PyByteArrayObject *) |
| 2052 | replace(PyByteArrayObject *self, |
| 2053 | const char *from_s, Py_ssize_t from_len, |
| 2054 | const char *to_s, Py_ssize_t to_len, |
| 2055 | Py_ssize_t maxcount) |
| 2056 | { |
| 2057 | if (maxcount < 0) { |
| 2058 | maxcount = PY_SSIZE_T_MAX; |
| 2059 | } else if (maxcount == 0 || PyByteArray_GET_SIZE(self) == 0) { |
| 2060 | /* nothing to do; return the original bytes */ |
| 2061 | return return_self(self); |
| 2062 | } |
| 2063 | |
| 2064 | if (maxcount == 0 || |
| 2065 | (from_len == 0 && to_len == 0)) { |
| 2066 | /* nothing to do; return the original bytes */ |
| 2067 | return return_self(self); |
| 2068 | } |
| 2069 | |
| 2070 | /* Handle zero-length special cases */ |
| 2071 | |
| 2072 | if (from_len == 0) { |
| 2073 | /* insert the 'to' bytes everywhere. */ |
| 2074 | /* >>> "Python".replace("", ".") */ |
| 2075 | /* '.P.y.t.h.o.n.' */ |
| 2076 | return replace_interleave(self, to_s, to_len, maxcount); |
| 2077 | } |
| 2078 | |
| 2079 | /* Except for "".replace("", "A") == "A" there is no way beyond this */ |
| 2080 | /* point for an empty self bytes to generate a non-empty bytes */ |
| 2081 | /* Special case so the remaining code always gets a non-empty bytes */ |
| 2082 | if (PyByteArray_GET_SIZE(self) == 0) { |
| 2083 | return return_self(self); |
| 2084 | } |
| 2085 | |
| 2086 | if (to_len == 0) { |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2087 | /* delete all occurrences of 'from' bytes */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2088 | if (from_len == 1) { |
| 2089 | return replace_delete_single_character( |
| 2090 | self, from_s[0], maxcount); |
| 2091 | } else { |
| 2092 | return replace_delete_substring(self, from_s, from_len, maxcount); |
| 2093 | } |
| 2094 | } |
| 2095 | |
| 2096 | /* Handle special case where both bytes have the same length */ |
| 2097 | |
| 2098 | if (from_len == to_len) { |
| 2099 | if (from_len == 1) { |
| 2100 | return replace_single_character_in_place( |
| 2101 | self, |
| 2102 | from_s[0], |
| 2103 | to_s[0], |
| 2104 | maxcount); |
| 2105 | } else { |
| 2106 | return replace_substring_in_place( |
| 2107 | self, from_s, from_len, to_s, to_len, maxcount); |
| 2108 | } |
| 2109 | } |
| 2110 | |
| 2111 | /* Otherwise use the more generic algorithms */ |
| 2112 | if (from_len == 1) { |
| 2113 | return replace_single_character(self, from_s[0], |
| 2114 | to_s, to_len, maxcount); |
| 2115 | } else { |
| 2116 | /* len('from')>=2, len('to')>=1 */ |
| 2117 | return replace_substring(self, from_s, from_len, to_s, to_len, maxcount); |
| 2118 | } |
| 2119 | } |
| 2120 | |
| 2121 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2122 | /*[clinic input] |
| 2123 | bytearray.replace |
| 2124 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2125 | old: Py_buffer |
| 2126 | new: Py_buffer |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2127 | count: Py_ssize_t = -1 |
| 2128 | Maximum number of occurrences to replace. |
| 2129 | -1 (the default value) means replace all occurrences. |
| 2130 | / |
| 2131 | |
| 2132 | Return a copy with all occurrences of substring old replaced by new. |
| 2133 | |
| 2134 | If the optional argument count is given, only the first count occurrences are |
| 2135 | replaced. |
| 2136 | [clinic start generated code]*/ |
| 2137 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2138 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2139 | bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old, |
| 2140 | Py_buffer *new, Py_ssize_t count) |
| 2141 | /*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2142 | { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2143 | return (PyObject *)replace((PyByteArrayObject *) self, |
| 2144 | old->buf, old->len, |
| 2145 | new->buf, new->len, count); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2148 | /*[clinic input] |
| 2149 | bytearray.split |
| 2150 | |
| 2151 | sep: object = None |
| 2152 | The delimiter according which to split the bytearray. |
| 2153 | None (the default value) means split on ASCII whitespace characters |
| 2154 | (space, tab, return, newline, formfeed, vertical tab). |
| 2155 | maxsplit: Py_ssize_t = -1 |
| 2156 | Maximum number of splits to do. |
| 2157 | -1 (the default value) means no limit. |
| 2158 | |
| 2159 | Return a list of the sections in the bytearray, using sep as the delimiter. |
| 2160 | [clinic start generated code]*/ |
| 2161 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2162 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2163 | bytearray_split_impl(PyByteArrayObject *self, PyObject *sep, |
| 2164 | Py_ssize_t maxsplit) |
| 2165 | /*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2166 | { |
| 2167 | Py_ssize_t len = PyByteArray_GET_SIZE(self), n; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2168 | const char *s = PyByteArray_AS_STRING(self), *sub; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2169 | PyObject *list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2170 | Py_buffer vsub; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2171 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2172 | if (maxsplit < 0) |
| 2173 | maxsplit = PY_SSIZE_T_MAX; |
| 2174 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2175 | if (sep == Py_None) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2176 | return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2177 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2178 | if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2179 | return NULL; |
| 2180 | sub = vsub.buf; |
| 2181 | n = vsub.len; |
| 2182 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2183 | list = stringlib_split( |
| 2184 | (PyObject*) self, s, len, sub, n, maxsplit |
| 2185 | ); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2186 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2187 | return list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2190 | /*[clinic input] |
| 2191 | bytearray.partition |
| 2192 | |
| 2193 | self: self(type="PyByteArrayObject *") |
| 2194 | sep: object |
| 2195 | / |
| 2196 | |
| 2197 | Partition the bytearray into three parts using the given separator. |
| 2198 | |
| 2199 | This will search for the separator sep in the bytearray. If the separator is |
| 2200 | found, returns a 3-tuple containing the part before the separator, the |
| 2201 | separator itself, and the part after it. |
| 2202 | |
| 2203 | If the separator is not found, returns a 3-tuple containing the original |
| 2204 | bytearray object and two empty bytearray objects. |
| 2205 | [clinic start generated code]*/ |
| 2206 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2207 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2208 | bytearray_partition(PyByteArrayObject *self, PyObject *sep) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2209 | /*[clinic end generated code: output=45d2525ddd35f957 input=7d7fe37b1696d506]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2210 | { |
| 2211 | PyObject *bytesep, *result; |
| 2212 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2213 | bytesep = PyByteArray_FromObject(sep); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2214 | if (! bytesep) |
| 2215 | return NULL; |
| 2216 | |
| 2217 | result = stringlib_partition( |
| 2218 | (PyObject*) self, |
| 2219 | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
| 2220 | bytesep, |
| 2221 | PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) |
| 2222 | ); |
| 2223 | |
| 2224 | Py_DECREF(bytesep); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2225 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2228 | /*[clinic input] |
| 2229 | bytearray.rpartition |
| 2230 | |
| 2231 | self: self(type="PyByteArrayObject *") |
| 2232 | sep: object |
| 2233 | / |
| 2234 | |
| 2235 | Partition the bytes into three parts using the given separator. |
| 2236 | |
| 2237 | This will search for the separator sep in the bytearray, starting and the end. |
| 2238 | If the separator is found, returns a 3-tuple containing the part before the |
| 2239 | separator, the separator itself, and the part after it. |
| 2240 | |
| 2241 | If the separator is not found, returns a 3-tuple containing two empty bytearray |
| 2242 | objects and the original bytearray object. |
| 2243 | [clinic start generated code]*/ |
| 2244 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2245 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2246 | bytearray_rpartition(PyByteArrayObject *self, PyObject *sep) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2247 | /*[clinic end generated code: output=440de3c9426115e8 input=9b8cd540c1b75853]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2248 | { |
| 2249 | PyObject *bytesep, *result; |
| 2250 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2251 | bytesep = PyByteArray_FromObject(sep); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2252 | if (! bytesep) |
| 2253 | return NULL; |
| 2254 | |
| 2255 | result = stringlib_rpartition( |
| 2256 | (PyObject*) self, |
| 2257 | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
| 2258 | bytesep, |
| 2259 | PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) |
| 2260 | ); |
| 2261 | |
| 2262 | Py_DECREF(bytesep); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2263 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2266 | /*[clinic input] |
| 2267 | bytearray.rsplit = bytearray.split |
| 2268 | |
| 2269 | Return a list of the sections in the bytearray, using sep as the delimiter. |
| 2270 | |
| 2271 | Splitting is done starting at the end of the bytearray and working to the front. |
| 2272 | [clinic start generated code]*/ |
| 2273 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2274 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2275 | bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep, |
| 2276 | Py_ssize_t maxsplit) |
| 2277 | /*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2278 | { |
| 2279 | Py_ssize_t len = PyByteArray_GET_SIZE(self), n; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2280 | const char *s = PyByteArray_AS_STRING(self), *sub; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2281 | PyObject *list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2282 | Py_buffer vsub; |
| 2283 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2284 | if (maxsplit < 0) |
| 2285 | maxsplit = PY_SSIZE_T_MAX; |
| 2286 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2287 | if (sep == Py_None) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2288 | return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2289 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2290 | if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2291 | return NULL; |
| 2292 | sub = vsub.buf; |
| 2293 | n = vsub.len; |
| 2294 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2295 | list = stringlib_rsplit( |
| 2296 | (PyObject*) self, s, len, sub, n, maxsplit |
| 2297 | ); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 2298 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2299 | return list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2302 | /*[clinic input] |
| 2303 | bytearray.reverse |
| 2304 | |
| 2305 | self: self(type="PyByteArrayObject *") |
| 2306 | |
| 2307 | Reverse the order of the values in B in place. |
| 2308 | [clinic start generated code]*/ |
| 2309 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2310 | static PyObject * |
| 2311 | bytearray_reverse_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2312 | /*[clinic end generated code: output=9f7616f29ab309d3 input=7933a499b8597bd1]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2313 | { |
| 2314 | char swap, *head, *tail; |
| 2315 | Py_ssize_t i, j, n = Py_SIZE(self); |
| 2316 | |
| 2317 | j = n / 2; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2318 | head = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2319 | tail = head + n - 1; |
| 2320 | for (i = 0; i < j; i++) { |
| 2321 | swap = *head; |
| 2322 | *head++ = *tail; |
| 2323 | *tail-- = swap; |
| 2324 | } |
| 2325 | |
| 2326 | Py_RETURN_NONE; |
| 2327 | } |
| 2328 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2329 | |
| 2330 | /*[python input] |
| 2331 | class bytesvalue_converter(CConverter): |
| 2332 | type = 'int' |
| 2333 | converter = '_getbytevalue' |
| 2334 | [python start generated code]*/ |
| 2335 | /*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/ |
| 2336 | |
| 2337 | |
| 2338 | /*[clinic input] |
| 2339 | bytearray.insert |
| 2340 | |
| 2341 | self: self(type="PyByteArrayObject *") |
| 2342 | index: Py_ssize_t |
| 2343 | The index where the value is to be inserted. |
| 2344 | item: bytesvalue |
| 2345 | The item to be inserted. |
| 2346 | / |
| 2347 | |
| 2348 | Insert a single item into the bytearray before the given index. |
| 2349 | [clinic start generated code]*/ |
| 2350 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2351 | static PyObject * |
| 2352 | bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2353 | /*[clinic end generated code: output=76c775a70e7b07b7 input=833766836ba30e1e]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2354 | { |
| 2355 | Py_ssize_t n = Py_SIZE(self); |
| 2356 | char *buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2357 | |
| 2358 | if (n == PY_SSIZE_T_MAX) { |
| 2359 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 2360 | "cannot add more objects to bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2361 | return NULL; |
| 2362 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2363 | if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) |
| 2364 | return NULL; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2365 | buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2366 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2367 | if (index < 0) { |
| 2368 | index += n; |
| 2369 | if (index < 0) |
| 2370 | index = 0; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2371 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2372 | if (index > n) |
| 2373 | index = n; |
| 2374 | memmove(buf + index + 1, buf + index, n - index); |
| 2375 | buf[index] = item; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2376 | |
| 2377 | Py_RETURN_NONE; |
| 2378 | } |
| 2379 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2380 | /*[clinic input] |
| 2381 | bytearray.append |
| 2382 | |
| 2383 | self: self(type="PyByteArrayObject *") |
| 2384 | item: bytesvalue |
| 2385 | The item to be appended. |
| 2386 | / |
| 2387 | |
| 2388 | Append a single item to the end of the bytearray. |
| 2389 | [clinic start generated code]*/ |
| 2390 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2391 | static PyObject * |
| 2392 | bytearray_append_impl(PyByteArrayObject *self, int item) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2393 | /*[clinic end generated code: output=a154e19ed1886cb6 input=ae56ea87380407cc]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2394 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2395 | Py_ssize_t n = Py_SIZE(self); |
| 2396 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2397 | if (n == PY_SSIZE_T_MAX) { |
| 2398 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 2399 | "cannot add more objects to bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2400 | return NULL; |
| 2401 | } |
| 2402 | if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) |
| 2403 | return NULL; |
| 2404 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2405 | PyByteArray_AS_STRING(self)[n] = item; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2406 | |
| 2407 | Py_RETURN_NONE; |
| 2408 | } |
| 2409 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2410 | /*[clinic input] |
| 2411 | bytearray.extend |
| 2412 | |
| 2413 | self: self(type="PyByteArrayObject *") |
| 2414 | iterable_of_ints: object |
| 2415 | The iterable of items to append. |
| 2416 | / |
| 2417 | |
| 2418 | Append all the items from the iterator or sequence to the end of the bytearray. |
| 2419 | [clinic start generated code]*/ |
| 2420 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2421 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2422 | bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2423 | /*[clinic end generated code: output=98155dbe249170b1 input=ce83a5d75b70d850]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2424 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2425 | PyObject *it, *item, *bytearray_obj; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2426 | Py_ssize_t buf_size = 0, len = 0; |
| 2427 | int value; |
| 2428 | char *buf; |
| 2429 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2430 | /* bytearray_setslice code only accepts something supporting PEP 3118. */ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2431 | if (PyObject_CheckBuffer(iterable_of_ints)) { |
| 2432 | 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] | 2433 | return NULL; |
| 2434 | |
| 2435 | Py_RETURN_NONE; |
| 2436 | } |
| 2437 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2438 | it = PyObject_GetIter(iterable_of_ints); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2439 | if (it == NULL) |
| 2440 | return NULL; |
| 2441 | |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 2442 | /* 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] | 2443 | buf_size = PyObject_LengthHint(iterable_of_ints, 32); |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 2444 | if (buf_size == -1) { |
| 2445 | Py_DECREF(it); |
| 2446 | return NULL; |
| 2447 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2448 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2449 | bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size); |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 2450 | if (bytearray_obj == NULL) { |
| 2451 | Py_DECREF(it); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2452 | return NULL; |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 2453 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2454 | buf = PyByteArray_AS_STRING(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2455 | |
| 2456 | while ((item = PyIter_Next(it)) != NULL) { |
| 2457 | if (! _getbytevalue(item, &value)) { |
| 2458 | Py_DECREF(item); |
| 2459 | Py_DECREF(it); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2460 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2461 | return NULL; |
| 2462 | } |
| 2463 | buf[len++] = value; |
| 2464 | Py_DECREF(item); |
| 2465 | |
| 2466 | if (len >= buf_size) { |
| 2467 | buf_size = len + (len >> 1) + 1; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2468 | if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2469 | Py_DECREF(it); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2470 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2471 | return NULL; |
| 2472 | } |
| 2473 | /* Recompute the `buf' pointer, since the resizing operation may |
| 2474 | have invalidated it. */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2475 | buf = PyByteArray_AS_STRING(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2476 | } |
| 2477 | } |
| 2478 | Py_DECREF(it); |
| 2479 | |
| 2480 | /* Resize down to exact size. */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2481 | if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) { |
| 2482 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2483 | return NULL; |
| 2484 | } |
| 2485 | |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 2486 | if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) { |
| 2487 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2488 | return NULL; |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 2489 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2490 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2491 | |
| 2492 | Py_RETURN_NONE; |
| 2493 | } |
| 2494 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2495 | /*[clinic input] |
| 2496 | bytearray.pop |
| 2497 | |
| 2498 | self: self(type="PyByteArrayObject *") |
| 2499 | index: Py_ssize_t = -1 |
| 2500 | The index from where to remove the item. |
| 2501 | -1 (the default value) means remove the last item. |
| 2502 | / |
| 2503 | |
| 2504 | Remove and return a single item from B. |
| 2505 | |
| 2506 | If no index argument is given, will pop the last item. |
| 2507 | [clinic start generated code]*/ |
| 2508 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2509 | static PyObject * |
| 2510 | bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2511 | /*[clinic end generated code: output=e0ccd401f8021da8 input=0797e6c0ca9d5a85]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2512 | { |
| 2513 | int value; |
| 2514 | Py_ssize_t n = Py_SIZE(self); |
| 2515 | char *buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2516 | |
| 2517 | if (n == 0) { |
Eli Bendersky | 1bc4f19 | 2011-03-04 04:55:25 +0000 | [diff] [blame] | 2518 | PyErr_SetString(PyExc_IndexError, |
| 2519 | "pop from empty bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2520 | return NULL; |
| 2521 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2522 | if (index < 0) |
| 2523 | index += Py_SIZE(self); |
| 2524 | if (index < 0 || index >= Py_SIZE(self)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2525 | PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
| 2526 | return NULL; |
| 2527 | } |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 2528 | if (!_canresize(self)) |
| 2529 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2530 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2531 | buf = PyByteArray_AS_STRING(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2532 | value = buf[index]; |
| 2533 | memmove(buf + index, buf + index + 1, n - index); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2534 | if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) |
| 2535 | return NULL; |
| 2536 | |
Mark Dickinson | 54a3db9 | 2009-09-06 10:19:23 +0000 | [diff] [blame] | 2537 | return PyLong_FromLong((unsigned char)value); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2538 | } |
| 2539 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2540 | /*[clinic input] |
| 2541 | bytearray.remove |
| 2542 | |
| 2543 | self: self(type="PyByteArrayObject *") |
| 2544 | value: bytesvalue |
| 2545 | The value to remove. |
| 2546 | / |
| 2547 | |
| 2548 | Remove the first occurrence of a value in the bytearray. |
| 2549 | [clinic start generated code]*/ |
| 2550 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2551 | static PyObject * |
| 2552 | bytearray_remove_impl(PyByteArrayObject *self, int value) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2553 | /*[clinic end generated code: output=d659e37866709c13 input=47560b11fd856c24]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2554 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2555 | Py_ssize_t where, n = Py_SIZE(self); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2556 | char *buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2557 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2558 | for (where = 0; where < n; where++) { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2559 | if (buf[where] == value) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2560 | break; |
| 2561 | } |
| 2562 | if (where == n) { |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 2563 | PyErr_SetString(PyExc_ValueError, "value not found in bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2564 | return NULL; |
| 2565 | } |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 2566 | if (!_canresize(self)) |
| 2567 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2568 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2569 | memmove(buf + where, buf + where + 1, n - where); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2570 | if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) |
| 2571 | return NULL; |
| 2572 | |
| 2573 | Py_RETURN_NONE; |
| 2574 | } |
| 2575 | |
| 2576 | /* XXX These two helpers could be optimized if argsize == 1 */ |
| 2577 | |
| 2578 | static Py_ssize_t |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2579 | lstrip_helper(const char *myptr, Py_ssize_t mysize, |
| 2580 | const void *argptr, Py_ssize_t argsize) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2581 | { |
| 2582 | Py_ssize_t i = 0; |
Antoine Pitrou | 5b72075 | 2013-10-05 21:24:10 +0200 | [diff] [blame] | 2583 | while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2584 | i++; |
| 2585 | return i; |
| 2586 | } |
| 2587 | |
| 2588 | static Py_ssize_t |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2589 | rstrip_helper(const char *myptr, Py_ssize_t mysize, |
| 2590 | const void *argptr, Py_ssize_t argsize) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2591 | { |
| 2592 | Py_ssize_t i = mysize - 1; |
Antoine Pitrou | 5b72075 | 2013-10-05 21:24:10 +0200 | [diff] [blame] | 2593 | while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2594 | i--; |
| 2595 | return i + 1; |
| 2596 | } |
| 2597 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2598 | /*[clinic input] |
| 2599 | bytearray.strip |
| 2600 | |
| 2601 | bytes: object = None |
| 2602 | / |
| 2603 | |
| 2604 | Strip leading and trailing bytes contained in the argument. |
| 2605 | |
| 2606 | If the argument is omitted or None, strip leading and trailing ASCII whitespace. |
| 2607 | [clinic start generated code]*/ |
| 2608 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2609 | static PyObject * |
| 2610 | bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2611 | /*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2612 | { |
| 2613 | Py_ssize_t left, right, mysize, byteslen; |
| 2614 | char *myptr, *bytesptr; |
| 2615 | Py_buffer vbytes; |
| 2616 | |
| 2617 | if (bytes == Py_None) { |
| 2618 | bytesptr = "\t\n\r\f\v "; |
| 2619 | byteslen = 6; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2620 | } |
| 2621 | else { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2622 | if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2623 | return NULL; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2624 | bytesptr = (char *) vbytes.buf; |
| 2625 | byteslen = vbytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2626 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2627 | myptr = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2628 | mysize = Py_SIZE(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2629 | left = lstrip_helper(myptr, mysize, bytesptr, byteslen); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2630 | if (left == mysize) |
| 2631 | right = left; |
| 2632 | else |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2633 | right = rstrip_helper(myptr, mysize, bytesptr, byteslen); |
| 2634 | if (bytes != Py_None) |
| 2635 | PyBuffer_Release(&vbytes); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2636 | return PyByteArray_FromStringAndSize(myptr + left, right - left); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2637 | } |
| 2638 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2639 | /*[clinic input] |
| 2640 | bytearray.lstrip |
| 2641 | |
| 2642 | bytes: object = None |
| 2643 | / |
| 2644 | |
| 2645 | Strip leading bytes contained in the argument. |
| 2646 | |
| 2647 | If the argument is omitted or None, strip leading ASCII whitespace. |
| 2648 | [clinic start generated code]*/ |
| 2649 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2650 | static PyObject * |
| 2651 | bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2652 | /*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2653 | { |
| 2654 | Py_ssize_t left, right, mysize, byteslen; |
| 2655 | char *myptr, *bytesptr; |
| 2656 | Py_buffer vbytes; |
| 2657 | |
| 2658 | if (bytes == Py_None) { |
| 2659 | bytesptr = "\t\n\r\f\v "; |
| 2660 | byteslen = 6; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2661 | } |
| 2662 | else { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2663 | if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2664 | return NULL; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2665 | bytesptr = (char *) vbytes.buf; |
| 2666 | byteslen = vbytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2667 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2668 | myptr = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2669 | mysize = Py_SIZE(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2670 | left = lstrip_helper(myptr, mysize, bytesptr, byteslen); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2671 | right = mysize; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2672 | if (bytes != Py_None) |
| 2673 | PyBuffer_Release(&vbytes); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2674 | return PyByteArray_FromStringAndSize(myptr + left, right - left); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2675 | } |
| 2676 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2677 | /*[clinic input] |
| 2678 | bytearray.rstrip |
| 2679 | |
| 2680 | bytes: object = None |
| 2681 | / |
| 2682 | |
| 2683 | Strip trailing bytes contained in the argument. |
| 2684 | |
| 2685 | If the argument is omitted or None, strip trailing ASCII whitespace. |
| 2686 | [clinic start generated code]*/ |
| 2687 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2688 | static PyObject * |
| 2689 | bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2690 | /*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2691 | { |
| 2692 | Py_ssize_t right, mysize, byteslen; |
| 2693 | char *myptr, *bytesptr; |
| 2694 | Py_buffer vbytes; |
| 2695 | |
| 2696 | if (bytes == Py_None) { |
| 2697 | bytesptr = "\t\n\r\f\v "; |
| 2698 | byteslen = 6; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2699 | } |
| 2700 | else { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2701 | if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2702 | return NULL; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2703 | bytesptr = (char *) vbytes.buf; |
| 2704 | byteslen = vbytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2705 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2706 | myptr = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2707 | mysize = Py_SIZE(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2708 | right = rstrip_helper(myptr, mysize, bytesptr, byteslen); |
| 2709 | if (bytes != Py_None) |
| 2710 | PyBuffer_Release(&vbytes); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2711 | return PyByteArray_FromStringAndSize(myptr, right); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2712 | } |
| 2713 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2714 | /*[clinic input] |
| 2715 | bytearray.decode |
| 2716 | |
| 2717 | encoding: str(c_default="NULL") = 'utf-8' |
| 2718 | The encoding with which to decode the bytearray. |
| 2719 | errors: str(c_default="NULL") = 'strict' |
| 2720 | The error handling scheme to use for the handling of decoding errors. |
| 2721 | The default is 'strict' meaning that decoding errors raise a |
| 2722 | UnicodeDecodeError. Other possible values are 'ignore' and 'replace' |
| 2723 | as well as any other name registered with codecs.register_error that |
| 2724 | can handle UnicodeDecodeErrors. |
| 2725 | |
| 2726 | Decode the bytearray using the codec registered for encoding. |
| 2727 | [clinic start generated code]*/ |
| 2728 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2729 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2730 | bytearray_decode_impl(PyByteArrayObject *self, const char *encoding, |
| 2731 | const char *errors) |
| 2732 | /*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2733 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2734 | if (encoding == NULL) |
| 2735 | encoding = PyUnicode_GetDefaultEncoding(); |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2736 | return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
| 2739 | PyDoc_STRVAR(alloc_doc, |
| 2740 | "B.__alloc__() -> int\n\ |
| 2741 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2742 | Return the number of bytes actually allocated."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2743 | |
| 2744 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2745 | bytearray_alloc(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2746 | { |
| 2747 | return PyLong_FromSsize_t(self->ob_alloc); |
| 2748 | } |
| 2749 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2750 | /*[clinic input] |
| 2751 | bytearray.join |
| 2752 | |
| 2753 | iterable_of_bytes: object |
| 2754 | / |
| 2755 | |
| 2756 | Concatenate any number of bytes/bytearray objects. |
| 2757 | |
| 2758 | The bytearray whose method is called is inserted in between each pair. |
| 2759 | |
| 2760 | The result is returned as a new bytearray object. |
| 2761 | [clinic start generated code]*/ |
| 2762 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2763 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2764 | bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2765 | /*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2766 | { |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2767 | return stringlib_bytes_join((PyObject*)self, iterable_of_bytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2770 | /*[clinic input] |
| 2771 | bytearray.splitlines |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2772 | |
Serhiy Storchaka | 8b2e8b6 | 2015-05-30 11:30:39 +0300 | [diff] [blame] | 2773 | keepends: int(c_default="0") = False |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2774 | |
| 2775 | Return a list of the lines in the bytearray, breaking at line boundaries. |
| 2776 | |
| 2777 | Line breaks are not included in the resulting list unless keepends is given and |
| 2778 | true. |
| 2779 | [clinic start generated code]*/ |
| 2780 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2781 | static PyObject * |
| 2782 | bytearray_splitlines_impl(PyByteArrayObject *self, int keepends) |
Serhiy Storchaka | 8b2e8b6 | 2015-05-30 11:30:39 +0300 | [diff] [blame] | 2783 | /*[clinic end generated code: output=4223c94b895f6ad9 input=8ccade941e5ea0bd]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2784 | { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2785 | return stringlib_splitlines( |
| 2786 | (PyObject*) self, PyByteArray_AS_STRING(self), |
| 2787 | PyByteArray_GET_SIZE(self), keepends |
| 2788 | ); |
| 2789 | } |
| 2790 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2791 | /*[clinic input] |
| 2792 | @classmethod |
| 2793 | bytearray.fromhex |
| 2794 | |
| 2795 | cls: self(type="PyObject*") |
| 2796 | string: unicode |
| 2797 | / |
| 2798 | |
| 2799 | Create a bytearray object from a string of hexadecimal numbers. |
| 2800 | |
| 2801 | Spaces between two numbers are accepted. |
| 2802 | Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef') |
| 2803 | [clinic start generated code]*/ |
| 2804 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2805 | static PyObject * |
| 2806 | bytearray_fromhex_impl(PyObject*cls, PyObject *string) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2807 | /*[clinic end generated code: output=df3da60129b3700c input=907bbd2d34d9367a]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2808 | { |
Victor Stinner | 2bf8993 | 2015-10-14 11:25:33 +0200 | [diff] [blame] | 2809 | return _PyBytes_FromHex(string, 1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
Gregory P. Smith | 8cb6569 | 2015-04-25 23:22:26 +0000 | [diff] [blame] | 2812 | PyDoc_STRVAR(hex__doc__, |
| 2813 | "B.hex() -> string\n\ |
| 2814 | \n\ |
| 2815 | Create a string of hexadecimal numbers from a bytearray object.\n\ |
| 2816 | Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'."); |
| 2817 | |
| 2818 | static PyObject * |
| 2819 | bytearray_hex(PyBytesObject *self) |
| 2820 | { |
| 2821 | char* argbuf = PyByteArray_AS_STRING(self); |
| 2822 | Py_ssize_t arglen = PyByteArray_GET_SIZE(self); |
| 2823 | return _Py_strhex(argbuf, arglen); |
| 2824 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2825 | |
| 2826 | static PyObject * |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2827 | _common_reduce(PyByteArrayObject *self, int proto) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2828 | { |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2829 | PyObject *dict; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2830 | _Py_IDENTIFIER(__dict__); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2831 | char *buf; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2832 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2833 | dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2834 | if (dict == NULL) { |
| 2835 | PyErr_Clear(); |
| 2836 | dict = Py_None; |
| 2837 | Py_INCREF(dict); |
| 2838 | } |
| 2839 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2840 | buf = PyByteArray_AS_STRING(self); |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2841 | if (proto < 3) { |
| 2842 | /* use str based reduction for backwards compatibility with Python 2.x */ |
| 2843 | PyObject *latin1; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2844 | if (Py_SIZE(self)) |
| 2845 | latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL); |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2846 | else |
| 2847 | latin1 = PyUnicode_FromString(""); |
| 2848 | return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict); |
| 2849 | } |
| 2850 | else { |
| 2851 | /* use more efficient byte based reduction */ |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2852 | if (Py_SIZE(self)) { |
| 2853 | 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] | 2854 | } |
| 2855 | else { |
| 2856 | return Py_BuildValue("(O()N)", Py_TYPE(self), dict); |
| 2857 | } |
| 2858 | } |
| 2859 | } |
| 2860 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2861 | /*[clinic input] |
| 2862 | bytearray.__reduce__ as bytearray_reduce |
| 2863 | |
| 2864 | self: self(type="PyByteArrayObject *") |
| 2865 | |
| 2866 | Return state information for pickling. |
| 2867 | [clinic start generated code]*/ |
| 2868 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2869 | static PyObject * |
| 2870 | bytearray_reduce_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2871 | /*[clinic end generated code: output=52bf304086464cab input=fbb07de4d102a03a]*/ |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2872 | { |
| 2873 | return _common_reduce(self, 2); |
| 2874 | } |
| 2875 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2876 | /*[clinic input] |
| 2877 | bytearray.__reduce_ex__ as bytearray_reduce_ex |
| 2878 | |
| 2879 | self: self(type="PyByteArrayObject *") |
| 2880 | proto: int = 0 |
| 2881 | / |
| 2882 | |
| 2883 | Return state information for pickling. |
| 2884 | [clinic start generated code]*/ |
| 2885 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2886 | static PyObject * |
| 2887 | bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2888 | /*[clinic end generated code: output=52eac33377197520 input=0e091a42ca6dbd91]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2889 | { |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2890 | return _common_reduce(self, proto); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2891 | } |
| 2892 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2893 | /*[clinic input] |
| 2894 | bytearray.__sizeof__ as bytearray_sizeof |
| 2895 | |
| 2896 | self: self(type="PyByteArrayObject *") |
| 2897 | |
| 2898 | Returns the size of the bytearray object in memory, in bytes. |
| 2899 | [clinic start generated code]*/ |
| 2900 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2901 | static PyObject * |
| 2902 | bytearray_sizeof_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 2903 | /*[clinic end generated code: output=738abdd17951c427 input=6b23d305362b462b]*/ |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2904 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 2905 | Py_ssize_t res; |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2906 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 2907 | res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char); |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 2908 | return PyLong_FromSsize_t(res); |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2909 | } |
| 2910 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2911 | static PySequenceMethods bytearray_as_sequence = { |
| 2912 | (lenfunc)bytearray_length, /* sq_length */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2913 | (binaryfunc)PyByteArray_Concat, /* sq_concat */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2914 | (ssizeargfunc)bytearray_repeat, /* sq_repeat */ |
| 2915 | (ssizeargfunc)bytearray_getitem, /* sq_item */ |
| 2916 | 0, /* sq_slice */ |
| 2917 | (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */ |
| 2918 | 0, /* sq_ass_slice */ |
| 2919 | (objobjproc)bytearray_contains, /* sq_contains */ |
| 2920 | (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */ |
| 2921 | (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2922 | }; |
| 2923 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2924 | static PyMappingMethods bytearray_as_mapping = { |
| 2925 | (lenfunc)bytearray_length, |
| 2926 | (binaryfunc)bytearray_subscript, |
| 2927 | (objobjargproc)bytearray_ass_subscript, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2928 | }; |
| 2929 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2930 | static PyBufferProcs bytearray_as_buffer = { |
| 2931 | (getbufferproc)bytearray_getbuffer, |
| 2932 | (releasebufferproc)bytearray_releasebuffer, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2933 | }; |
| 2934 | |
| 2935 | static PyMethodDef |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2936 | bytearray_methods[] = { |
| 2937 | {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2938 | BYTEARRAY_REDUCE_METHODDEF |
| 2939 | BYTEARRAY_REDUCE_EX_METHODDEF |
| 2940 | BYTEARRAY_SIZEOF_METHODDEF |
| 2941 | BYTEARRAY_APPEND_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2942 | {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS, |
| 2943 | _Py_capitalize__doc__}, |
| 2944 | {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2945 | BYTEARRAY_CLEAR_METHODDEF |
| 2946 | BYTEARRAY_COPY_METHODDEF |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2947 | {"count", (PyCFunction)bytearray_count, METH_VARARGS, count__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2948 | BYTEARRAY_DECODE_METHODDEF |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2949 | {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, endswith__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 2950 | {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2951 | expandtabs__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2952 | BYTEARRAY_EXTEND_METHODDEF |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2953 | {"find", (PyCFunction)bytearray_find, METH_VARARGS, find__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2954 | BYTEARRAY_FROMHEX_METHODDEF |
Gregory P. Smith | 8cb6569 | 2015-04-25 23:22:26 +0000 | [diff] [blame] | 2955 | {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__}, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2956 | {"index", (PyCFunction)bytearray_index, METH_VARARGS, index__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2957 | BYTEARRAY_INSERT_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2958 | {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS, |
| 2959 | _Py_isalnum__doc__}, |
| 2960 | {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS, |
| 2961 | _Py_isalpha__doc__}, |
| 2962 | {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS, |
| 2963 | _Py_isdigit__doc__}, |
| 2964 | {"islower", (PyCFunction)stringlib_islower, METH_NOARGS, |
| 2965 | _Py_islower__doc__}, |
| 2966 | {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS, |
| 2967 | _Py_isspace__doc__}, |
| 2968 | {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS, |
| 2969 | _Py_istitle__doc__}, |
| 2970 | {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS, |
| 2971 | _Py_isupper__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2972 | BYTEARRAY_JOIN_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2973 | {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__}, |
| 2974 | {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2975 | BYTEARRAY_LSTRIP_METHODDEF |
| 2976 | BYTEARRAY_MAKETRANS_METHODDEF |
| 2977 | BYTEARRAY_PARTITION_METHODDEF |
| 2978 | BYTEARRAY_POP_METHODDEF |
| 2979 | BYTEARRAY_REMOVE_METHODDEF |
| 2980 | BYTEARRAY_REPLACE_METHODDEF |
| 2981 | BYTEARRAY_REVERSE_METHODDEF |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2982 | {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, rfind__doc__}, |
| 2983 | {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, rindex__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2984 | {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2985 | BYTEARRAY_RPARTITION_METHODDEF |
| 2986 | BYTEARRAY_RSPLIT_METHODDEF |
| 2987 | BYTEARRAY_RSTRIP_METHODDEF |
| 2988 | BYTEARRAY_SPLIT_METHODDEF |
| 2989 | BYTEARRAY_SPLITLINES_METHODDEF |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2990 | {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS , |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2991 | startswith__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2992 | BYTEARRAY_STRIP_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2993 | {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS, |
| 2994 | _Py_swapcase__doc__}, |
| 2995 | {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2996 | BYTEARRAY_TRANSLATE_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2997 | {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__}, |
| 2998 | {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__}, |
| 2999 | {NULL} |
| 3000 | }; |
| 3001 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 3002 | static PyObject * |
| 3003 | bytearray_mod(PyObject *v, PyObject *w) |
| 3004 | { |
| 3005 | if (!PyByteArray_Check(v)) |
| 3006 | Py_RETURN_NOTIMPLEMENTED; |
| 3007 | return bytearray_format((PyByteArrayObject *)v, w); |
| 3008 | } |
| 3009 | |
| 3010 | static PyNumberMethods bytearray_as_number = { |
| 3011 | 0, /*nb_add*/ |
| 3012 | 0, /*nb_subtract*/ |
| 3013 | 0, /*nb_multiply*/ |
| 3014 | bytearray_mod, /*nb_remainder*/ |
| 3015 | }; |
| 3016 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3017 | PyDoc_STRVAR(bytearray_doc, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 3018 | "bytearray(iterable_of_ints) -> bytearray\n\ |
| 3019 | bytearray(string, encoding[, errors]) -> bytearray\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 3020 | bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\ |
| 3021 | bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\ |
| 3022 | bytearray() -> empty bytes array\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3023 | \n\ |
| 3024 | Construct an mutable bytearray object from:\n\ |
| 3025 | - an iterable yielding integers in range(256)\n\ |
| 3026 | - a text string encoded using the specified encoding\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 3027 | - a bytes or a buffer object\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3028 | - any object implementing the buffer API.\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 3029 | - an integer"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3030 | |
| 3031 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3032 | static PyObject *bytearray_iter(PyObject *seq); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3033 | |
| 3034 | PyTypeObject PyByteArray_Type = { |
| 3035 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3036 | "bytearray", |
| 3037 | sizeof(PyByteArrayObject), |
| 3038 | 0, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3039 | (destructor)bytearray_dealloc, /* tp_dealloc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3040 | 0, /* tp_print */ |
| 3041 | 0, /* tp_getattr */ |
| 3042 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 3043 | 0, /* tp_reserved */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3044 | (reprfunc)bytearray_repr, /* tp_repr */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 3045 | &bytearray_as_number, /* tp_as_number */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3046 | &bytearray_as_sequence, /* tp_as_sequence */ |
| 3047 | &bytearray_as_mapping, /* tp_as_mapping */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3048 | 0, /* tp_hash */ |
| 3049 | 0, /* tp_call */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3050 | bytearray_str, /* tp_str */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3051 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3052 | 0, /* tp_setattro */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3053 | &bytearray_as_buffer, /* tp_as_buffer */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3054 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3055 | bytearray_doc, /* tp_doc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3056 | 0, /* tp_traverse */ |
| 3057 | 0, /* tp_clear */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3058 | (richcmpfunc)bytearray_richcompare, /* tp_richcompare */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3059 | 0, /* tp_weaklistoffset */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3060 | bytearray_iter, /* tp_iter */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3061 | 0, /* tp_iternext */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3062 | bytearray_methods, /* tp_methods */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3063 | 0, /* tp_members */ |
| 3064 | 0, /* tp_getset */ |
| 3065 | 0, /* tp_base */ |
| 3066 | 0, /* tp_dict */ |
| 3067 | 0, /* tp_descr_get */ |
| 3068 | 0, /* tp_descr_set */ |
| 3069 | 0, /* tp_dictoffset */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3070 | (initproc)bytearray_init, /* tp_init */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3071 | PyType_GenericAlloc, /* tp_alloc */ |
| 3072 | PyType_GenericNew, /* tp_new */ |
| 3073 | PyObject_Del, /* tp_free */ |
| 3074 | }; |
| 3075 | |
| 3076 | /*********************** Bytes Iterator ****************************/ |
| 3077 | |
| 3078 | typedef struct { |
| 3079 | PyObject_HEAD |
| 3080 | Py_ssize_t it_index; |
| 3081 | PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 3082 | } bytesiterobject; |
| 3083 | |
| 3084 | static void |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3085 | bytearrayiter_dealloc(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3086 | { |
| 3087 | _PyObject_GC_UNTRACK(it); |
| 3088 | Py_XDECREF(it->it_seq); |
| 3089 | PyObject_GC_Del(it); |
| 3090 | } |
| 3091 | |
| 3092 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3093 | bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3094 | { |
| 3095 | Py_VISIT(it->it_seq); |
| 3096 | return 0; |
| 3097 | } |
| 3098 | |
| 3099 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3100 | bytearrayiter_next(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3101 | { |
| 3102 | PyByteArrayObject *seq; |
| 3103 | PyObject *item; |
| 3104 | |
| 3105 | assert(it != NULL); |
| 3106 | seq = it->it_seq; |
| 3107 | if (seq == NULL) |
| 3108 | return NULL; |
| 3109 | assert(PyByteArray_Check(seq)); |
| 3110 | |
| 3111 | if (it->it_index < PyByteArray_GET_SIZE(seq)) { |
| 3112 | item = PyLong_FromLong( |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 3113 | (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3114 | if (item != NULL) |
| 3115 | ++it->it_index; |
| 3116 | return item; |
| 3117 | } |
| 3118 | |
| 3119 | Py_DECREF(seq); |
| 3120 | it->it_seq = NULL; |
| 3121 | return NULL; |
| 3122 | } |
| 3123 | |
| 3124 | static PyObject * |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3125 | bytearrayiter_length_hint(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3126 | { |
| 3127 | Py_ssize_t len = 0; |
| 3128 | if (it->it_seq) |
| 3129 | len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index; |
| 3130 | return PyLong_FromSsize_t(len); |
| 3131 | } |
| 3132 | |
| 3133 | PyDoc_STRVAR(length_hint_doc, |
| 3134 | "Private method returning an estimate of len(list(it))."); |
| 3135 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3136 | static PyObject * |
| 3137 | bytearrayiter_reduce(bytesiterobject *it) |
| 3138 | { |
| 3139 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 3140 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3141 | it->it_seq, it->it_index); |
| 3142 | } else { |
| 3143 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 3144 | if (u == NULL) |
| 3145 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 3146 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3147 | } |
| 3148 | } |
| 3149 | |
| 3150 | static PyObject * |
| 3151 | bytearrayiter_setstate(bytesiterobject *it, PyObject *state) |
| 3152 | { |
| 3153 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 3154 | if (index == -1 && PyErr_Occurred()) |
| 3155 | return NULL; |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 3156 | if (it->it_seq != NULL) { |
| 3157 | if (index < 0) |
| 3158 | index = 0; |
| 3159 | else if (index > PyByteArray_GET_SIZE(it->it_seq)) |
| 3160 | index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */ |
| 3161 | it->it_index = index; |
| 3162 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3163 | Py_RETURN_NONE; |
| 3164 | } |
| 3165 | |
| 3166 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 3167 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3168 | static PyMethodDef bytearrayiter_methods[] = { |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3169 | {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3170 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3171 | {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3172 | bytearray_reduce__doc__}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3173 | {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O, |
| 3174 | setstate_doc}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3175 | {NULL, NULL} /* sentinel */ |
| 3176 | }; |
| 3177 | |
| 3178 | PyTypeObject PyByteArrayIter_Type = { |
| 3179 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3180 | "bytearray_iterator", /* tp_name */ |
| 3181 | sizeof(bytesiterobject), /* tp_basicsize */ |
| 3182 | 0, /* tp_itemsize */ |
| 3183 | /* methods */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3184 | (destructor)bytearrayiter_dealloc, /* tp_dealloc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3185 | 0, /* tp_print */ |
| 3186 | 0, /* tp_getattr */ |
| 3187 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 3188 | 0, /* tp_reserved */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3189 | 0, /* tp_repr */ |
| 3190 | 0, /* tp_as_number */ |
| 3191 | 0, /* tp_as_sequence */ |
| 3192 | 0, /* tp_as_mapping */ |
| 3193 | 0, /* tp_hash */ |
| 3194 | 0, /* tp_call */ |
| 3195 | 0, /* tp_str */ |
| 3196 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3197 | 0, /* tp_setattro */ |
| 3198 | 0, /* tp_as_buffer */ |
| 3199 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 3200 | 0, /* tp_doc */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3201 | (traverseproc)bytearrayiter_traverse, /* tp_traverse */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3202 | 0, /* tp_clear */ |
| 3203 | 0, /* tp_richcompare */ |
| 3204 | 0, /* tp_weaklistoffset */ |
| 3205 | PyObject_SelfIter, /* tp_iter */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3206 | (iternextfunc)bytearrayiter_next, /* tp_iternext */ |
| 3207 | bytearrayiter_methods, /* tp_methods */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3208 | 0, |
| 3209 | }; |
| 3210 | |
| 3211 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 3212 | bytearray_iter(PyObject *seq) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3213 | { |
| 3214 | bytesiterobject *it; |
| 3215 | |
| 3216 | if (!PyByteArray_Check(seq)) { |
| 3217 | PyErr_BadInternalCall(); |
| 3218 | return NULL; |
| 3219 | } |
| 3220 | it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type); |
| 3221 | if (it == NULL) |
| 3222 | return NULL; |
| 3223 | it->it_index = 0; |
| 3224 | Py_INCREF(seq); |
| 3225 | it->it_seq = (PyByteArrayObject *)seq; |
| 3226 | _PyObject_GC_TRACK(it); |
| 3227 | return (PyObject *)it; |
| 3228 | } |