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