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" |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 5 | #include "internal/mem.h" |
| 6 | #include "internal/pystate.h" |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 7 | #include "structmember.h" |
| 8 | #include "bytes_methods.h" |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 9 | #include "bytesobject.h" |
Gregory P. Smith | 8cb6569 | 2015-04-25 23:22:26 +0000 | [diff] [blame] | 10 | #include "pystrhex.h" |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 11 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 12 | /*[clinic input] |
| 13 | class bytearray "PyByteArrayObject *" "&PyByteArray_Type" |
| 14 | [clinic start generated code]*/ |
| 15 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/ |
| 16 | |
Antoine Pitrou | fc8d6f4 | 2010-01-17 12:38:54 +0000 | [diff] [blame] | 17 | char _PyByteArray_empty_string[] = ""; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 18 | |
| 19 | void |
| 20 | PyByteArray_Fini(void) |
| 21 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | int |
| 25 | PyByteArray_Init(void) |
| 26 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 27 | return 1; |
| 28 | } |
| 29 | |
| 30 | /* end nullbytes support */ |
| 31 | |
| 32 | /* Helpers */ |
| 33 | |
| 34 | static int |
| 35 | _getbytevalue(PyObject* arg, int *value) |
| 36 | { |
| 37 | long face_value; |
| 38 | |
| 39 | if (PyLong_Check(arg)) { |
| 40 | face_value = PyLong_AsLong(arg); |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 41 | } else { |
| 42 | PyObject *index = PyNumber_Index(arg); |
| 43 | if (index == NULL) { |
| 44 | PyErr_Format(PyExc_TypeError, "an integer is required"); |
Mark Dickinson | 10de93a | 2010-07-09 19:25:48 +0000 | [diff] [blame] | 45 | *value = -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 46 | return 0; |
| 47 | } |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 48 | face_value = PyLong_AsLong(index); |
| 49 | Py_DECREF(index); |
| 50 | } |
| 51 | |
| 52 | if (face_value < 0 || face_value >= 256) { |
| 53 | /* this includes the OverflowError in case the long is too large */ |
| 54 | PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
Mark Dickinson | 10de93a | 2010-07-09 19:25:48 +0000 | [diff] [blame] | 55 | *value = -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | *value = face_value; |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 64 | bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 65 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 66 | void *ptr; |
| 67 | if (view == NULL) { |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 68 | PyErr_SetString(PyExc_BufferError, |
| 69 | "bytearray_getbuffer: view==NULL argument is obsolete"); |
| 70 | return -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 71 | } |
Antoine Pitrou | fc8d6f4 | 2010-01-17 12:38:54 +0000 | [diff] [blame] | 72 | ptr = (void *) PyByteArray_AS_STRING(obj); |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 73 | /* cannot fail if view != NULL and readonly == 0 */ |
| 74 | (void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags); |
| 75 | obj->ob_exports++; |
| 76 | return 0; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static void |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 80 | bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 81 | { |
| 82 | obj->ob_exports--; |
| 83 | } |
| 84 | |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 85 | static int |
| 86 | _canresize(PyByteArrayObject *self) |
| 87 | { |
| 88 | if (self->ob_exports > 0) { |
| 89 | PyErr_SetString(PyExc_BufferError, |
| 90 | "Existing exports of data: object cannot be re-sized"); |
| 91 | return 0; |
| 92 | } |
| 93 | return 1; |
| 94 | } |
| 95 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 96 | #include "clinic/bytearrayobject.c.h" |
| 97 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 98 | /* Direct API functions */ |
| 99 | |
| 100 | PyObject * |
| 101 | PyByteArray_FromObject(PyObject *input) |
| 102 | { |
Victor Stinner | de4ae3d | 2016-12-04 22:59:09 +0100 | [diff] [blame] | 103 | return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type, |
| 104 | input, NULL); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | PyObject * |
| 108 | PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size) |
| 109 | { |
| 110 | PyByteArrayObject *new; |
| 111 | Py_ssize_t alloc; |
| 112 | |
| 113 | if (size < 0) { |
| 114 | PyErr_SetString(PyExc_SystemError, |
| 115 | "Negative size passed to PyByteArray_FromStringAndSize"); |
| 116 | return NULL; |
| 117 | } |
| 118 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 119 | /* Prevent buffer overflow when setting alloc to size+1. */ |
| 120 | if (size == PY_SSIZE_T_MAX) { |
| 121 | return PyErr_NoMemory(); |
| 122 | } |
| 123 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 124 | new = PyObject_New(PyByteArrayObject, &PyByteArray_Type); |
| 125 | if (new == NULL) |
| 126 | return NULL; |
| 127 | |
| 128 | if (size == 0) { |
| 129 | new->ob_bytes = NULL; |
| 130 | alloc = 0; |
| 131 | } |
| 132 | else { |
| 133 | alloc = size + 1; |
Antoine Pitrou | 39aba4f | 2011-11-12 21:15:28 +0100 | [diff] [blame] | 134 | new->ob_bytes = PyObject_Malloc(alloc); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 135 | if (new->ob_bytes == NULL) { |
| 136 | Py_DECREF(new); |
| 137 | return PyErr_NoMemory(); |
| 138 | } |
Antoine Pitrou | fc8d6f4 | 2010-01-17 12:38:54 +0000 | [diff] [blame] | 139 | if (bytes != NULL && size > 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 140 | memcpy(new->ob_bytes, bytes, size); |
| 141 | new->ob_bytes[size] = '\0'; /* Trailing null byte */ |
| 142 | } |
| 143 | Py_SIZE(new) = size; |
| 144 | new->ob_alloc = alloc; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 145 | new->ob_start = new->ob_bytes; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 146 | new->ob_exports = 0; |
| 147 | |
| 148 | return (PyObject *)new; |
| 149 | } |
| 150 | |
| 151 | Py_ssize_t |
| 152 | PyByteArray_Size(PyObject *self) |
| 153 | { |
| 154 | assert(self != NULL); |
| 155 | assert(PyByteArray_Check(self)); |
| 156 | |
| 157 | return PyByteArray_GET_SIZE(self); |
| 158 | } |
| 159 | |
| 160 | char * |
| 161 | PyByteArray_AsString(PyObject *self) |
| 162 | { |
| 163 | assert(self != NULL); |
| 164 | assert(PyByteArray_Check(self)); |
| 165 | |
| 166 | return PyByteArray_AS_STRING(self); |
| 167 | } |
| 168 | |
| 169 | int |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 170 | PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 171 | { |
| 172 | void *sval; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 173 | PyByteArrayObject *obj = ((PyByteArrayObject *)self); |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 174 | /* All computations are done unsigned to avoid integer overflows |
| 175 | (see issue #22335). */ |
| 176 | size_t alloc = (size_t) obj->ob_alloc; |
| 177 | size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes); |
| 178 | size_t size = (size_t) requested_size; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 179 | |
| 180 | assert(self != NULL); |
| 181 | assert(PyByteArray_Check(self)); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 182 | assert(logical_offset <= alloc); |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 183 | assert(requested_size >= 0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 184 | |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 185 | if (requested_size == Py_SIZE(self)) { |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 186 | return 0; |
| 187 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 188 | if (!_canresize(obj)) { |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 189 | return -1; |
| 190 | } |
| 191 | |
Antoine Pitrou | 2545411 | 2015-05-19 20:52:27 +0200 | [diff] [blame] | 192 | if (size + logical_offset + 1 <= alloc) { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 193 | /* Current buffer is large enough to host the requested size, |
| 194 | decide on a strategy. */ |
| 195 | if (size < alloc / 2) { |
| 196 | /* Major downsize; resize down to exact size */ |
| 197 | alloc = size + 1; |
| 198 | } |
| 199 | else { |
| 200 | /* Minor downsize; quick exit */ |
| 201 | Py_SIZE(self) = size; |
| 202 | PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */ |
| 203 | return 0; |
| 204 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 205 | } |
| 206 | else { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 207 | /* Need growing, decide on a strategy */ |
| 208 | if (size <= alloc * 1.125) { |
| 209 | /* Moderate upsize; overallocate similar to list_resize() */ |
| 210 | alloc = size + (size >> 3) + (size < 9 ? 3 : 6); |
| 211 | } |
| 212 | else { |
| 213 | /* Major upsize; resize up to exact size */ |
| 214 | alloc = size + 1; |
| 215 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 216 | } |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 217 | if (alloc > PY_SSIZE_T_MAX) { |
| 218 | PyErr_NoMemory(); |
| 219 | return -1; |
| 220 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 221 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 222 | if (logical_offset > 0) { |
| 223 | sval = PyObject_Malloc(alloc); |
| 224 | if (sval == NULL) { |
| 225 | PyErr_NoMemory(); |
| 226 | return -1; |
| 227 | } |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 228 | memcpy(sval, PyByteArray_AS_STRING(self), |
Stefan Krah | dce6502 | 2017-08-25 20:12:05 +0200 | [diff] [blame] | 229 | Py_MIN((size_t)requested_size, (size_t)Py_SIZE(self))); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 230 | PyObject_Free(obj->ob_bytes); |
| 231 | } |
| 232 | else { |
| 233 | sval = PyObject_Realloc(obj->ob_bytes, alloc); |
| 234 | if (sval == NULL) { |
| 235 | PyErr_NoMemory(); |
| 236 | return -1; |
| 237 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 240 | obj->ob_bytes = obj->ob_start = sval; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 241 | Py_SIZE(self) = size; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 242 | obj->ob_alloc = alloc; |
| 243 | obj->ob_bytes[size] = '\0'; /* Trailing null byte */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | PyObject * |
| 249 | PyByteArray_Concat(PyObject *a, PyObject *b) |
| 250 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 251 | Py_buffer va, vb; |
| 252 | PyByteArrayObject *result = NULL; |
| 253 | |
| 254 | va.len = -1; |
| 255 | vb.len = -1; |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 256 | if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 || |
| 257 | PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 258 | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
Serhiy Storchaka | 6b5a9ec | 2017-03-19 19:47:02 +0200 | [diff] [blame] | 259 | Py_TYPE(b)->tp_name, Py_TYPE(a)->tp_name); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 260 | goto done; |
| 261 | } |
| 262 | |
Serhiy Storchaka | 06cfb0c | 2016-07-10 20:48:43 +0300 | [diff] [blame] | 263 | if (va.len > PY_SSIZE_T_MAX - vb.len) { |
| 264 | PyErr_NoMemory(); |
| 265 | goto done; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Serhiy Storchaka | 06cfb0c | 2016-07-10 20:48:43 +0300 | [diff] [blame] | 268 | result = (PyByteArrayObject *) \ |
| 269 | PyByteArray_FromStringAndSize(NULL, va.len + vb.len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 270 | if (result != NULL) { |
| 271 | memcpy(result->ob_bytes, va.buf, va.len); |
| 272 | memcpy(result->ob_bytes + va.len, vb.buf, vb.len); |
| 273 | } |
| 274 | |
| 275 | done: |
| 276 | if (va.len != -1) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 277 | PyBuffer_Release(&va); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 278 | if (vb.len != -1) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 279 | PyBuffer_Release(&vb); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 280 | return (PyObject *)result; |
| 281 | } |
| 282 | |
| 283 | /* Functions stuffed into the type object */ |
| 284 | |
| 285 | static Py_ssize_t |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 286 | bytearray_length(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 287 | { |
| 288 | return Py_SIZE(self); |
| 289 | } |
| 290 | |
| 291 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 292 | bytearray_iconcat(PyByteArrayObject *self, PyObject *other) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 293 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 294 | Py_ssize_t size; |
| 295 | Py_buffer vo; |
| 296 | |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 297 | if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 298 | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
| 299 | Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name); |
| 300 | return NULL; |
| 301 | } |
| 302 | |
Serhiy Storchaka | 06cfb0c | 2016-07-10 20:48:43 +0300 | [diff] [blame] | 303 | size = Py_SIZE(self); |
| 304 | if (size > PY_SSIZE_T_MAX - vo.len) { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 305 | PyBuffer_Release(&vo); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 306 | return PyErr_NoMemory(); |
| 307 | } |
Serhiy Storchaka | 06cfb0c | 2016-07-10 20:48:43 +0300 | [diff] [blame] | 308 | if (PyByteArray_Resize((PyObject *)self, size + vo.len) < 0) { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 309 | PyBuffer_Release(&vo); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 310 | return NULL; |
| 311 | } |
Serhiy Storchaka | 06cfb0c | 2016-07-10 20:48:43 +0300 | [diff] [blame] | 312 | memcpy(PyByteArray_AS_STRING(self) + size, vo.buf, vo.len); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 313 | PyBuffer_Release(&vo); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 314 | Py_INCREF(self); |
| 315 | return (PyObject *)self; |
| 316 | } |
| 317 | |
| 318 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 319 | bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 320 | { |
| 321 | PyByteArrayObject *result; |
| 322 | Py_ssize_t mysize; |
| 323 | Py_ssize_t size; |
| 324 | |
| 325 | if (count < 0) |
| 326 | count = 0; |
| 327 | mysize = Py_SIZE(self); |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 328 | if (count > 0 && mysize > PY_SSIZE_T_MAX / count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 329 | return PyErr_NoMemory(); |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 330 | size = mysize * count; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 331 | result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size); |
| 332 | if (result != NULL && size != 0) { |
| 333 | if (mysize == 1) |
| 334 | memset(result->ob_bytes, self->ob_bytes[0], size); |
| 335 | else { |
| 336 | Py_ssize_t i; |
| 337 | for (i = 0; i < count; i++) |
| 338 | memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize); |
| 339 | } |
| 340 | } |
| 341 | return (PyObject *)result; |
| 342 | } |
| 343 | |
| 344 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 345 | bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 346 | { |
| 347 | Py_ssize_t mysize; |
| 348 | Py_ssize_t size; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 349 | char *buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 350 | |
| 351 | if (count < 0) |
| 352 | count = 0; |
| 353 | mysize = Py_SIZE(self); |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 354 | if (count > 0 && mysize > PY_SSIZE_T_MAX / count) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 355 | return PyErr_NoMemory(); |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 356 | size = mysize * count; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 357 | if (PyByteArray_Resize((PyObject *)self, size) < 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 358 | return NULL; |
| 359 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 360 | buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 361 | if (mysize == 1) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 362 | memset(buf, buf[0], size); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 363 | else { |
| 364 | Py_ssize_t i; |
| 365 | for (i = 1; i < count; i++) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 366 | memcpy(buf + i*mysize, buf, mysize); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | Py_INCREF(self); |
| 370 | return (PyObject *)self; |
| 371 | } |
| 372 | |
| 373 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 374 | bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 375 | { |
| 376 | if (i < 0) |
| 377 | i += Py_SIZE(self); |
| 378 | if (i < 0 || i >= Py_SIZE(self)) { |
| 379 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 380 | return NULL; |
| 381 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 382 | return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i])); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 386 | bytearray_subscript(PyByteArrayObject *self, PyObject *index) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 387 | { |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 388 | if (PyIndex_Check(index)) { |
| 389 | Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 390 | |
| 391 | if (i == -1 && PyErr_Occurred()) |
| 392 | return NULL; |
| 393 | |
| 394 | if (i < 0) |
| 395 | i += PyByteArray_GET_SIZE(self); |
| 396 | |
| 397 | if (i < 0 || i >= Py_SIZE(self)) { |
| 398 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 399 | return NULL; |
| 400 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 401 | return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i])); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 402 | } |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 403 | else if (PySlice_Check(index)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 404 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Serhiy Storchaka | b879fe8 | 2017-04-08 09:53:51 +0300 | [diff] [blame] | 405 | if (PySlice_Unpack(index, &start, &stop, &step) < 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 406 | return NULL; |
| 407 | } |
Serhiy Storchaka | b879fe8 | 2017-04-08 09:53:51 +0300 | [diff] [blame] | 408 | slicelength = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self), |
| 409 | &start, &stop, step); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 410 | |
| 411 | if (slicelength <= 0) |
| 412 | return PyByteArray_FromStringAndSize("", 0); |
| 413 | else if (step == 1) { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 414 | return PyByteArray_FromStringAndSize( |
| 415 | PyByteArray_AS_STRING(self) + start, slicelength); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 416 | } |
| 417 | else { |
| 418 | char *source_buf = PyByteArray_AS_STRING(self); |
Alexandre Vassalotti | e2641f4 | 2009-04-03 06:38:02 +0000 | [diff] [blame] | 419 | char *result_buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 420 | PyObject *result; |
| 421 | |
Alexandre Vassalotti | e2641f4 | 2009-04-03 06:38:02 +0000 | [diff] [blame] | 422 | result = PyByteArray_FromStringAndSize(NULL, slicelength); |
| 423 | if (result == NULL) |
| 424 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 425 | |
Alexandre Vassalotti | e2641f4 | 2009-04-03 06:38:02 +0000 | [diff] [blame] | 426 | result_buf = PyByteArray_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 427 | for (cur = start, i = 0; i < slicelength; |
| 428 | cur += step, i++) { |
| 429 | result_buf[i] = source_buf[cur]; |
| 430 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 431 | return result; |
| 432 | } |
| 433 | } |
| 434 | else { |
Terry Jan Reedy | ffff144 | 2014-08-02 01:30:37 -0400 | [diff] [blame] | 435 | PyErr_Format(PyExc_TypeError, |
| 436 | "bytearray indices must be integers or slices, not %.200s", |
| 437 | Py_TYPE(index)->tp_name); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 438 | return NULL; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | static int |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 443 | bytearray_setslice_linear(PyByteArrayObject *self, |
| 444 | Py_ssize_t lo, Py_ssize_t hi, |
| 445 | char *bytes, Py_ssize_t bytes_len) |
| 446 | { |
| 447 | Py_ssize_t avail = hi - lo; |
| 448 | char *buf = PyByteArray_AS_STRING(self); |
| 449 | Py_ssize_t growth = bytes_len - avail; |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 450 | int res = 0; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 451 | assert(avail >= 0); |
| 452 | |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 453 | if (growth < 0) { |
| 454 | if (!_canresize(self)) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 455 | return -1; |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 456 | |
| 457 | if (lo == 0) { |
| 458 | /* Shrink the buffer by advancing its logical start */ |
| 459 | self->ob_start -= growth; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 460 | /* |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 461 | 0 lo hi old_size |
| 462 | | |<----avail----->|<-----tail------>| |
| 463 | | |<-bytes_len->|<-----tail------>| |
| 464 | 0 new_lo new_hi new_size |
| 465 | */ |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 466 | } |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 467 | else { |
| 468 | /* |
| 469 | 0 lo hi old_size |
| 470 | | |<----avail----->|<-----tomove------>| |
| 471 | | |<-bytes_len->|<-----tomove------>| |
| 472 | 0 lo new_hi new_size |
| 473 | */ |
| 474 | memmove(buf + lo + bytes_len, buf + hi, |
| 475 | Py_SIZE(self) - hi); |
| 476 | } |
| 477 | if (PyByteArray_Resize((PyObject *)self, |
| 478 | Py_SIZE(self) + growth) < 0) { |
| 479 | /* Issue #19578: Handling the memory allocation failure here is |
| 480 | tricky here because the bytearray object has already been |
| 481 | modified. Depending on growth and lo, the behaviour is |
| 482 | different. |
| 483 | |
| 484 | If growth < 0 and lo != 0, the operation is completed, but a |
| 485 | MemoryError is still raised and the memory block is not |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 486 | shrunk. Otherwise, the bytearray is restored in its previous |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 487 | state and a MemoryError is raised. */ |
| 488 | if (lo == 0) { |
| 489 | self->ob_start += growth; |
| 490 | return -1; |
| 491 | } |
| 492 | /* memmove() removed bytes, the bytearray object cannot be |
| 493 | restored in its previous state. */ |
| 494 | Py_SIZE(self) += growth; |
| 495 | res = -1; |
| 496 | } |
| 497 | buf = PyByteArray_AS_STRING(self); |
| 498 | } |
| 499 | else if (growth > 0) { |
| 500 | if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) { |
| 501 | PyErr_NoMemory(); |
| 502 | return -1; |
| 503 | } |
| 504 | |
| 505 | if (PyByteArray_Resize((PyObject *)self, |
| 506 | Py_SIZE(self) + growth) < 0) { |
| 507 | return -1; |
| 508 | } |
| 509 | buf = PyByteArray_AS_STRING(self); |
| 510 | /* Make the place for the additional bytes */ |
| 511 | /* |
| 512 | 0 lo hi old_size |
| 513 | | |<-avail->|<-----tomove------>| |
| 514 | | |<---bytes_len-->|<-----tomove------>| |
| 515 | 0 lo new_hi new_size |
| 516 | */ |
| 517 | memmove(buf + lo + bytes_len, buf + hi, |
| 518 | Py_SIZE(self) - lo - bytes_len); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | if (bytes_len > 0) |
| 522 | memcpy(buf + lo, bytes, bytes_len); |
Victor Stinner | 8455723 | 2013-11-21 12:29:51 +0100 | [diff] [blame] | 523 | return res; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 527 | bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 528 | PyObject *values) |
| 529 | { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 530 | Py_ssize_t needed; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 531 | void *bytes; |
| 532 | Py_buffer vbytes; |
| 533 | int res = 0; |
| 534 | |
| 535 | vbytes.len = -1; |
| 536 | if (values == (PyObject *)self) { |
| 537 | /* Make a copy and call this function recursively */ |
| 538 | int err; |
| 539 | values = PyByteArray_FromObject(values); |
| 540 | if (values == NULL) |
| 541 | return -1; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 542 | err = bytearray_setslice(self, lo, hi, values); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 543 | Py_DECREF(values); |
| 544 | return err; |
| 545 | } |
| 546 | if (values == NULL) { |
| 547 | /* del b[lo:hi] */ |
| 548 | bytes = NULL; |
| 549 | needed = 0; |
| 550 | } |
| 551 | else { |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 552 | if (PyObject_GetBuffer(values, &vbytes, PyBUF_SIMPLE) != 0) { |
| 553 | PyErr_Format(PyExc_TypeError, |
| 554 | "can't set bytearray slice from %.100s", |
| 555 | Py_TYPE(values)->tp_name); |
| 556 | return -1; |
| 557 | } |
| 558 | needed = vbytes.len; |
| 559 | bytes = vbytes.buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | if (lo < 0) |
| 563 | lo = 0; |
| 564 | if (hi < lo) |
| 565 | hi = lo; |
| 566 | if (hi > Py_SIZE(self)) |
| 567 | hi = Py_SIZE(self); |
| 568 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 569 | res = bytearray_setslice_linear(self, lo, hi, bytes, needed); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 570 | if (vbytes.len != -1) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 571 | PyBuffer_Release(&vbytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 572 | return res; |
| 573 | } |
| 574 | |
| 575 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 576 | bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 577 | { |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 578 | int ival; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 579 | |
| 580 | if (i < 0) |
| 581 | i += Py_SIZE(self); |
| 582 | |
| 583 | if (i < 0 || i >= Py_SIZE(self)) { |
| 584 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 585 | return -1; |
| 586 | } |
| 587 | |
| 588 | if (value == NULL) |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 589 | return bytearray_setslice(self, i, i+1, NULL); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 590 | |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 591 | if (!_getbytevalue(value, &ival)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 592 | return -1; |
| 593 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 594 | PyByteArray_AS_STRING(self)[i] = ival; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 599 | bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 600 | { |
| 601 | Py_ssize_t start, stop, step, slicelen, needed; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 602 | char *buf, *bytes; |
| 603 | buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 604 | |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 605 | if (PyIndex_Check(index)) { |
| 606 | Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 607 | |
| 608 | if (i == -1 && PyErr_Occurred()) |
| 609 | return -1; |
| 610 | |
| 611 | if (i < 0) |
| 612 | i += PyByteArray_GET_SIZE(self); |
| 613 | |
| 614 | if (i < 0 || i >= Py_SIZE(self)) { |
| 615 | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
| 616 | return -1; |
| 617 | } |
| 618 | |
| 619 | if (values == NULL) { |
| 620 | /* Fall through to slice assignment */ |
| 621 | start = i; |
| 622 | stop = i + 1; |
| 623 | step = 1; |
| 624 | slicelen = 1; |
| 625 | } |
| 626 | else { |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 627 | int ival; |
| 628 | if (!_getbytevalue(values, &ival)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 629 | return -1; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 630 | buf[i] = (char)ival; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 631 | return 0; |
| 632 | } |
| 633 | } |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 634 | else if (PySlice_Check(index)) { |
Serhiy Storchaka | b879fe8 | 2017-04-08 09:53:51 +0300 | [diff] [blame] | 635 | if (PySlice_Unpack(index, &start, &stop, &step) < 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 636 | return -1; |
| 637 | } |
Serhiy Storchaka | b879fe8 | 2017-04-08 09:53:51 +0300 | [diff] [blame] | 638 | slicelen = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self), &start, |
| 639 | &stop, step); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 640 | } |
| 641 | else { |
Terry Jan Reedy | ffff144 | 2014-08-02 01:30:37 -0400 | [diff] [blame] | 642 | PyErr_Format(PyExc_TypeError, |
| 643 | "bytearray indices must be integers or slices, not %.200s", |
| 644 | Py_TYPE(index)->tp_name); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 645 | return -1; |
| 646 | } |
| 647 | |
| 648 | if (values == NULL) { |
| 649 | bytes = NULL; |
| 650 | needed = 0; |
| 651 | } |
| 652 | else if (values == (PyObject *)self || !PyByteArray_Check(values)) { |
Christian Heimes | 6d26ade | 2012-11-03 23:07:59 +0100 | [diff] [blame] | 653 | int err; |
Ezio Melotti | c64bcbe | 2012-11-03 21:19:06 +0200 | [diff] [blame] | 654 | if (PyNumber_Check(values) || PyUnicode_Check(values)) { |
| 655 | PyErr_SetString(PyExc_TypeError, |
| 656 | "can assign only bytes, buffers, or iterables " |
| 657 | "of ints in range(0, 256)"); |
| 658 | return -1; |
| 659 | } |
Georg Brandl | f3fa568 | 2010-12-04 17:09:30 +0000 | [diff] [blame] | 660 | /* Make a copy and call this function recursively */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 661 | values = PyByteArray_FromObject(values); |
| 662 | if (values == NULL) |
| 663 | return -1; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 664 | err = bytearray_ass_subscript(self, index, values); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 665 | Py_DECREF(values); |
| 666 | return err; |
| 667 | } |
| 668 | else { |
| 669 | assert(PyByteArray_Check(values)); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 670 | bytes = PyByteArray_AS_STRING(values); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 671 | needed = Py_SIZE(values); |
| 672 | } |
| 673 | /* Make sure b[5:2] = ... inserts before 5, not before 2. */ |
| 674 | if ((step < 0 && start < stop) || |
| 675 | (step > 0 && start > stop)) |
| 676 | stop = start; |
| 677 | if (step == 1) { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 678 | return bytearray_setslice_linear(self, start, stop, bytes, needed); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 679 | } |
| 680 | else { |
| 681 | if (needed == 0) { |
| 682 | /* Delete slice */ |
Mark Dickinson | bc09964 | 2010-01-29 17:27:24 +0000 | [diff] [blame] | 683 | size_t cur; |
| 684 | Py_ssize_t i; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 685 | |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 686 | if (!_canresize(self)) |
| 687 | return -1; |
Mark Dickinson | 7e3b948 | 2010-08-06 21:33:18 +0000 | [diff] [blame] | 688 | |
| 689 | if (slicelen == 0) |
| 690 | /* Nothing to do here. */ |
| 691 | return 0; |
| 692 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 693 | if (step < 0) { |
| 694 | stop = start + 1; |
| 695 | start = stop + step * (slicelen - 1) - 1; |
| 696 | step = -step; |
| 697 | } |
| 698 | for (cur = start, i = 0; |
| 699 | i < slicelen; cur += step, i++) { |
| 700 | Py_ssize_t lim = step - 1; |
| 701 | |
Mark Dickinson | 66f575b | 2010-02-14 12:53:32 +0000 | [diff] [blame] | 702 | if (cur + step >= (size_t)PyByteArray_GET_SIZE(self)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 703 | lim = PyByteArray_GET_SIZE(self) - cur - 1; |
| 704 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 705 | memmove(buf + cur - i, |
| 706 | buf + cur + 1, lim); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 707 | } |
| 708 | /* Move the tail of the bytes, in one chunk */ |
Mark Dickinson | 7e3b948 | 2010-08-06 21:33:18 +0000 | [diff] [blame] | 709 | cur = start + (size_t)slicelen*step; |
Mark Dickinson | 66f575b | 2010-02-14 12:53:32 +0000 | [diff] [blame] | 710 | if (cur < (size_t)PyByteArray_GET_SIZE(self)) { |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 711 | memmove(buf + cur - slicelen, |
| 712 | buf + cur, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 713 | PyByteArray_GET_SIZE(self) - cur); |
| 714 | } |
| 715 | if (PyByteArray_Resize((PyObject *)self, |
| 716 | PyByteArray_GET_SIZE(self) - slicelen) < 0) |
| 717 | return -1; |
| 718 | |
| 719 | return 0; |
| 720 | } |
| 721 | else { |
| 722 | /* Assign slice */ |
Mark Dickinson | 7e3b948 | 2010-08-06 21:33:18 +0000 | [diff] [blame] | 723 | Py_ssize_t i; |
| 724 | size_t cur; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 725 | |
| 726 | if (needed != slicelen) { |
| 727 | PyErr_Format(PyExc_ValueError, |
| 728 | "attempt to assign bytes of size %zd " |
| 729 | "to extended slice of size %zd", |
| 730 | needed, slicelen); |
| 731 | return -1; |
| 732 | } |
| 733 | for (cur = start, i = 0; i < slicelen; cur += step, i++) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 734 | buf[cur] = bytes[i]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 735 | return 0; |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 741 | bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 742 | { |
| 743 | static char *kwlist[] = {"source", "encoding", "errors", 0}; |
| 744 | PyObject *arg = NULL; |
| 745 | const char *encoding = NULL; |
| 746 | const char *errors = NULL; |
| 747 | Py_ssize_t count; |
| 748 | PyObject *it; |
| 749 | PyObject *(*iternext)(PyObject *); |
| 750 | |
| 751 | if (Py_SIZE(self) != 0) { |
| 752 | /* Empty previous contents (yes, do this first of all!) */ |
| 753 | if (PyByteArray_Resize((PyObject *)self, 0) < 0) |
| 754 | return -1; |
| 755 | } |
| 756 | |
| 757 | /* Parse arguments */ |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 758 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 759 | &arg, &encoding, &errors)) |
| 760 | return -1; |
| 761 | |
| 762 | /* Make a quick exit if no first argument */ |
| 763 | if (arg == NULL) { |
| 764 | if (encoding != NULL || errors != NULL) { |
| 765 | PyErr_SetString(PyExc_TypeError, |
| 766 | "encoding or errors without sequence argument"); |
| 767 | return -1; |
| 768 | } |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | if (PyUnicode_Check(arg)) { |
| 773 | /* Encode via the codec registry */ |
| 774 | PyObject *encoded, *new; |
| 775 | if (encoding == NULL) { |
| 776 | PyErr_SetString(PyExc_TypeError, |
| 777 | "string argument without an encoding"); |
| 778 | return -1; |
| 779 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 780 | encoded = PyUnicode_AsEncodedString(arg, encoding, errors); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 781 | if (encoded == NULL) |
| 782 | return -1; |
| 783 | assert(PyBytes_Check(encoded)); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 784 | new = bytearray_iconcat(self, encoded); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 785 | Py_DECREF(encoded); |
| 786 | if (new == NULL) |
| 787 | return -1; |
| 788 | Py_DECREF(new); |
| 789 | return 0; |
| 790 | } |
| 791 | |
| 792 | /* If it's not unicode, there can't be encoding or errors */ |
| 793 | if (encoding != NULL || errors != NULL) { |
| 794 | PyErr_SetString(PyExc_TypeError, |
| 795 | "encoding or errors without a string argument"); |
| 796 | return -1; |
| 797 | } |
| 798 | |
| 799 | /* Is it an int? */ |
Serhiy Storchaka | eb24988 | 2016-08-15 09:46:07 +0300 | [diff] [blame] | 800 | if (PyIndex_Check(arg)) { |
| 801 | count = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
| 802 | if (count == -1 && PyErr_Occurred()) { |
INADA Naoki | a634e23 | 2017-01-06 17:32:01 +0900 | [diff] [blame] | 803 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 804 | return -1; |
INADA Naoki | a634e23 | 2017-01-06 17:32:01 +0900 | [diff] [blame] | 805 | PyErr_Clear(); /* fall through */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 806 | } |
INADA Naoki | a634e23 | 2017-01-06 17:32:01 +0900 | [diff] [blame] | 807 | else { |
| 808 | if (count < 0) { |
| 809 | PyErr_SetString(PyExc_ValueError, "negative count"); |
| 810 | return -1; |
| 811 | } |
| 812 | if (count > 0) { |
| 813 | if (PyByteArray_Resize((PyObject *)self, count)) |
| 814 | return -1; |
| 815 | memset(PyByteArray_AS_STRING(self), 0, count); |
| 816 | } |
| 817 | return 0; |
| 818 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | /* Use the buffer API */ |
| 822 | if (PyObject_CheckBuffer(arg)) { |
| 823 | Py_ssize_t size; |
| 824 | Py_buffer view; |
| 825 | if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0) |
| 826 | return -1; |
| 827 | size = view.len; |
| 828 | if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 829 | if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self), |
| 830 | &view, size, 'C') < 0) |
Stefan Krah | 7d12d9d | 2012-07-28 12:25:55 +0200 | [diff] [blame] | 831 | goto fail; |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 832 | PyBuffer_Release(&view); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 833 | return 0; |
| 834 | fail: |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 835 | PyBuffer_Release(&view); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 836 | return -1; |
| 837 | } |
| 838 | |
| 839 | /* XXX Optimize this if the arguments is a list, tuple */ |
| 840 | |
| 841 | /* Get the iterator */ |
| 842 | it = PyObject_GetIter(arg); |
| 843 | if (it == NULL) |
| 844 | return -1; |
| 845 | iternext = *Py_TYPE(it)->tp_iternext; |
| 846 | |
| 847 | /* Run the iterator to exhaustion */ |
| 848 | for (;;) { |
| 849 | PyObject *item; |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 850 | int rc, value; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 851 | |
| 852 | /* Get the next item */ |
| 853 | item = iternext(it); |
| 854 | if (item == NULL) { |
| 855 | if (PyErr_Occurred()) { |
| 856 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 857 | goto error; |
| 858 | PyErr_Clear(); |
| 859 | } |
| 860 | break; |
| 861 | } |
| 862 | |
| 863 | /* Interpret it as an int (__index__) */ |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 864 | rc = _getbytevalue(item, &value); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 865 | Py_DECREF(item); |
Georg Brandl | 9a54d7c | 2008-07-16 23:15:30 +0000 | [diff] [blame] | 866 | if (!rc) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 867 | goto error; |
| 868 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 869 | /* Append the byte */ |
Serhiy Storchaka | 7b6e3b9 | 2015-06-29 21:14:06 +0300 | [diff] [blame] | 870 | if (Py_SIZE(self) + 1 < self->ob_alloc) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 871 | Py_SIZE(self)++; |
Serhiy Storchaka | 7b6e3b9 | 2015-06-29 21:14:06 +0300 | [diff] [blame] | 872 | PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0'; |
| 873 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 874 | else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0) |
| 875 | goto error; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 876 | PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | /* Clean up and return success */ |
| 880 | Py_DECREF(it); |
| 881 | return 0; |
| 882 | |
| 883 | error: |
| 884 | /* Error handling when it != NULL */ |
| 885 | Py_DECREF(it); |
| 886 | return -1; |
| 887 | } |
| 888 | |
| 889 | /* Mostly copied from string_repr, but without the |
| 890 | "smart quote" functionality. */ |
| 891 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 892 | bytearray_repr(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 893 | { |
Serhiy Storchaka | b3a7796 | 2017-09-21 14:24:13 +0300 | [diff] [blame] | 894 | const char *className = _PyType_Name(Py_TYPE(self)); |
| 895 | const char *quote_prefix = "(b"; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 896 | const char *quote_postfix = ")"; |
| 897 | Py_ssize_t length = Py_SIZE(self); |
Serhiy Storchaka | b3a7796 | 2017-09-21 14:24:13 +0300 | [diff] [blame] | 898 | /* 6 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */ |
| 899 | Py_ssize_t newsize; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 900 | PyObject *v; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 901 | Py_ssize_t i; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 902 | char *bytes; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 903 | char c; |
| 904 | char *p; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 905 | int quote; |
| 906 | char *test, *start; |
| 907 | char *buffer; |
| 908 | |
Serhiy Storchaka | b3a7796 | 2017-09-21 14:24:13 +0300 | [diff] [blame] | 909 | newsize = strlen(className); |
| 910 | if (length > (PY_SSIZE_T_MAX - 6 - newsize) / 4) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 911 | PyErr_SetString(PyExc_OverflowError, |
| 912 | "bytearray object is too large to make repr"); |
| 913 | return NULL; |
| 914 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 915 | |
Serhiy Storchaka | b3a7796 | 2017-09-21 14:24:13 +0300 | [diff] [blame] | 916 | newsize += 6 + length * 4; |
Antoine Pitrou | 39aba4f | 2011-11-12 21:15:28 +0100 | [diff] [blame] | 917 | buffer = PyObject_Malloc(newsize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 918 | if (buffer == NULL) { |
| 919 | PyErr_NoMemory(); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 920 | return NULL; |
| 921 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 922 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 923 | /* Figure out which quote to use; single is preferred */ |
| 924 | quote = '\''; |
| 925 | start = PyByteArray_AS_STRING(self); |
| 926 | for (test = start; test < start+length; ++test) { |
| 927 | if (*test == '"') { |
| 928 | quote = '\''; /* back to single */ |
| 929 | break; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 930 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 931 | else if (*test == '\'') |
| 932 | quote = '"'; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 933 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 934 | |
| 935 | p = buffer; |
Serhiy Storchaka | b3a7796 | 2017-09-21 14:24:13 +0300 | [diff] [blame] | 936 | while (*className) |
| 937 | *p++ = *className++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 938 | while (*quote_prefix) |
| 939 | *p++ = *quote_prefix++; |
| 940 | *p++ = quote; |
| 941 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 942 | bytes = PyByteArray_AS_STRING(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 943 | for (i = 0; i < length; i++) { |
| 944 | /* There's at least enough room for a hex escape |
| 945 | and a closing quote. */ |
| 946 | assert(newsize - (p - buffer) >= 5); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 947 | c = bytes[i]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 948 | if (c == '\'' || c == '\\') |
| 949 | *p++ = '\\', *p++ = c; |
| 950 | else if (c == '\t') |
| 951 | *p++ = '\\', *p++ = 't'; |
| 952 | else if (c == '\n') |
| 953 | *p++ = '\\', *p++ = 'n'; |
| 954 | else if (c == '\r') |
| 955 | *p++ = '\\', *p++ = 'r'; |
| 956 | else if (c == 0) |
| 957 | *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0'; |
| 958 | else if (c < ' ' || c >= 0x7f) { |
| 959 | *p++ = '\\'; |
| 960 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 961 | *p++ = Py_hexdigits[(c & 0xf0) >> 4]; |
| 962 | *p++ = Py_hexdigits[c & 0xf]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 963 | } |
| 964 | else |
| 965 | *p++ = c; |
| 966 | } |
| 967 | assert(newsize - (p - buffer) >= 1); |
| 968 | *p++ = quote; |
| 969 | while (*quote_postfix) { |
| 970 | *p++ = *quote_postfix++; |
| 971 | } |
| 972 | |
Serhiy Storchaka | b3a7796 | 2017-09-21 14:24:13 +0300 | [diff] [blame] | 973 | v = PyUnicode_FromStringAndSize(buffer, p - buffer); |
Antoine Pitrou | 39aba4f | 2011-11-12 21:15:28 +0100 | [diff] [blame] | 974 | PyObject_Free(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 975 | return v; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 979 | bytearray_str(PyObject *op) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 980 | { |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 981 | if (Py_BytesWarningFlag) { |
| 982 | if (PyErr_WarnEx(PyExc_BytesWarning, |
| 983 | "str() on a bytearray instance", 1)) |
| 984 | return NULL; |
| 985 | } |
| 986 | return bytearray_repr((PyByteArrayObject*)op); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 990 | bytearray_richcompare(PyObject *self, PyObject *other, int op) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 991 | { |
| 992 | Py_ssize_t self_size, other_size; |
| 993 | Py_buffer self_bytes, other_bytes; |
| 994 | PyObject *res; |
| 995 | Py_ssize_t minsize; |
Serhiy Storchaka | fa494fd | 2015-05-30 17:45:22 +0300 | [diff] [blame] | 996 | int cmp, rc; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 997 | |
| 998 | /* Bytes can be compared to anything that supports the (binary) |
| 999 | buffer API. Except that a comparison with Unicode is always an |
| 1000 | error, even if the comparison is for equality. */ |
Serhiy Storchaka | fa494fd | 2015-05-30 17:45:22 +0300 | [diff] [blame] | 1001 | rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type); |
| 1002 | if (!rc) |
| 1003 | rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type); |
| 1004 | if (rc < 0) |
| 1005 | return NULL; |
| 1006 | if (rc) { |
Barry Warsaw | 9e9dcd6 | 2008-10-17 01:50:37 +0000 | [diff] [blame] | 1007 | if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1008 | if (PyErr_WarnEx(PyExc_BytesWarning, |
Georg Brandl | e5d68ac | 2008-06-04 11:30:26 +0000 | [diff] [blame] | 1009 | "Comparison between bytearray and string", 1)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1010 | return NULL; |
| 1011 | } |
| 1012 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1013 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1016 | if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1017 | PyErr_Clear(); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1018 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1019 | } |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1020 | self_size = self_bytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1021 | |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1022 | if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1023 | PyErr_Clear(); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1024 | PyBuffer_Release(&self_bytes); |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1025 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1026 | } |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1027 | other_size = other_bytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1028 | |
| 1029 | if (self_size != other_size && (op == Py_EQ || op == Py_NE)) { |
| 1030 | /* Shortcut: if the lengths differ, the objects differ */ |
| 1031 | cmp = (op == Py_NE); |
| 1032 | } |
| 1033 | else { |
| 1034 | minsize = self_size; |
| 1035 | if (other_size < minsize) |
| 1036 | minsize = other_size; |
| 1037 | |
| 1038 | cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize); |
| 1039 | /* In ISO C, memcmp() guarantees to use unsigned bytes! */ |
| 1040 | |
| 1041 | if (cmp == 0) { |
| 1042 | if (self_size < other_size) |
| 1043 | cmp = -1; |
| 1044 | else if (self_size > other_size) |
| 1045 | cmp = 1; |
| 1046 | } |
| 1047 | |
| 1048 | switch (op) { |
| 1049 | case Py_LT: cmp = cmp < 0; break; |
| 1050 | case Py_LE: cmp = cmp <= 0; break; |
| 1051 | case Py_EQ: cmp = cmp == 0; break; |
| 1052 | case Py_NE: cmp = cmp != 0; break; |
| 1053 | case Py_GT: cmp = cmp > 0; break; |
| 1054 | case Py_GE: cmp = cmp >= 0; break; |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | res = cmp ? Py_True : Py_False; |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1059 | PyBuffer_Release(&self_bytes); |
| 1060 | PyBuffer_Release(&other_bytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1061 | Py_INCREF(res); |
| 1062 | return res; |
| 1063 | } |
| 1064 | |
| 1065 | static void |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1066 | bytearray_dealloc(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1067 | { |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 1068 | if (self->ob_exports > 0) { |
| 1069 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1070 | "deallocated bytearray object has exported buffers"); |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 1071 | PyErr_Print(); |
| 1072 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1073 | if (self->ob_bytes != 0) { |
Antoine Pitrou | 39aba4f | 2011-11-12 21:15:28 +0100 | [diff] [blame] | 1074 | PyObject_Free(self->ob_bytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1075 | } |
| 1076 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 1077 | } |
| 1078 | |
| 1079 | |
| 1080 | /* -------------------------------------------------------------------- */ |
| 1081 | /* Methods */ |
| 1082 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1083 | #define FASTSEARCH fastsearch |
| 1084 | #define STRINGLIB(F) stringlib_##F |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1085 | #define STRINGLIB_CHAR char |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 1086 | #define STRINGLIB_SIZEOF_CHAR 1 |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1087 | #define STRINGLIB_LEN PyByteArray_GET_SIZE |
| 1088 | #define STRINGLIB_STR PyByteArray_AS_STRING |
| 1089 | #define STRINGLIB_NEW PyByteArray_FromStringAndSize |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1090 | #define STRINGLIB_ISSPACE Py_ISSPACE |
| 1091 | #define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r')) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1092 | #define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact |
| 1093 | #define STRINGLIB_MUTABLE 1 |
| 1094 | |
| 1095 | #include "stringlib/fastsearch.h" |
| 1096 | #include "stringlib/count.h" |
| 1097 | #include "stringlib/find.h" |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 1098 | #include "stringlib/join.h" |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1099 | #include "stringlib/partition.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1100 | #include "stringlib/split.h" |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1101 | #include "stringlib/ctype.h" |
| 1102 | #include "stringlib/transmogrify.h" |
| 1103 | |
| 1104 | |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 1105 | static PyObject * |
| 1106 | bytearray_find(PyByteArrayObject *self, PyObject *args) |
| 1107 | { |
| 1108 | return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
| 1109 | } |
| 1110 | |
| 1111 | static PyObject * |
| 1112 | bytearray_count(PyByteArrayObject *self, PyObject *args) |
| 1113 | { |
| 1114 | return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
| 1115 | } |
| 1116 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1117 | /*[clinic input] |
| 1118 | bytearray.clear |
| 1119 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1120 | Remove all items from the bytearray. |
| 1121 | [clinic start generated code]*/ |
| 1122 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1123 | static PyObject * |
| 1124 | bytearray_clear_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1125 | /*[clinic end generated code: output=85c2fe6aede0956c input=ed6edae9de447ac4]*/ |
Eli Bendersky | 4db28d3 | 2011-03-03 18:21:02 +0000 | [diff] [blame] | 1126 | { |
| 1127 | if (PyByteArray_Resize((PyObject *)self, 0) < 0) |
| 1128 | return NULL; |
| 1129 | Py_RETURN_NONE; |
| 1130 | } |
| 1131 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1132 | /*[clinic input] |
| 1133 | bytearray.copy |
| 1134 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1135 | Return a copy of B. |
| 1136 | [clinic start generated code]*/ |
| 1137 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1138 | static PyObject * |
| 1139 | bytearray_copy_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1140 | /*[clinic end generated code: output=68cfbcfed484c132 input=6597b0c01bccaa9e]*/ |
Eli Bendersky | 4db28d3 | 2011-03-03 18:21:02 +0000 | [diff] [blame] | 1141 | { |
| 1142 | return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self), |
| 1143 | PyByteArray_GET_SIZE(self)); |
| 1144 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1145 | |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 1146 | static PyObject * |
| 1147 | bytearray_index(PyByteArrayObject *self, PyObject *args) |
| 1148 | { |
| 1149 | return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
| 1150 | } |
| 1151 | |
| 1152 | static PyObject * |
| 1153 | bytearray_rfind(PyByteArrayObject *self, PyObject *args) |
| 1154 | { |
| 1155 | return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
| 1156 | } |
| 1157 | |
| 1158 | static PyObject * |
| 1159 | bytearray_rindex(PyByteArrayObject *self, PyObject *args) |
| 1160 | { |
| 1161 | return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
| 1162 | } |
| 1163 | |
| 1164 | static int |
| 1165 | bytearray_contains(PyObject *self, PyObject *arg) |
| 1166 | { |
| 1167 | return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg); |
| 1168 | } |
| 1169 | |
| 1170 | static PyObject * |
| 1171 | bytearray_startswith(PyByteArrayObject *self, PyObject *args) |
| 1172 | { |
| 1173 | return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
| 1174 | } |
| 1175 | |
| 1176 | static PyObject * |
| 1177 | bytearray_endswith(PyByteArrayObject *self, PyObject *args) |
| 1178 | { |
| 1179 | return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
| 1180 | } |
| 1181 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1182 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1183 | /*[clinic input] |
| 1184 | bytearray.translate |
| 1185 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1186 | table: object |
| 1187 | Translation table, which must be a bytes object of length 256. |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1188 | / |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 1189 | delete as deletechars: object(c_default="NULL") = b'' |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1190 | |
| 1191 | Return a copy with each character mapped by the given translation table. |
| 1192 | |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 1193 | All characters occurring in the optional argument delete are removed. |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1194 | The remaining characters are mapped through the given translation table. |
| 1195 | [clinic start generated code]*/ |
| 1196 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1197 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1198 | bytearray_translate_impl(PyByteArrayObject *self, PyObject *table, |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 1199 | PyObject *deletechars) |
| 1200 | /*[clinic end generated code: output=b6a8f01c2a74e446 input=cfff956d4d127a9b]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1201 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1202 | char *input, *output; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1203 | const char *table_chars; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1204 | Py_ssize_t i, c; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1205 | PyObject *input_obj = (PyObject*)self; |
| 1206 | const char *output_start; |
| 1207 | Py_ssize_t inlen; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1208 | PyObject *result = NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1209 | int trans_table[256]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1210 | Py_buffer vtable, vdel; |
| 1211 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1212 | if (table == Py_None) { |
| 1213 | table_chars = NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1214 | table = NULL; |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1215 | } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1216 | return NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1217 | } else { |
| 1218 | if (vtable.len != 256) { |
| 1219 | PyErr_SetString(PyExc_ValueError, |
| 1220 | "translation table must be 256 characters long"); |
Georg Brandl | 953152f | 2009-07-22 12:03:59 +0000 | [diff] [blame] | 1221 | PyBuffer_Release(&vtable); |
| 1222 | return NULL; |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1223 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1224 | table_chars = (const char*)vtable.buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1227 | if (deletechars != NULL) { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1228 | if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1229 | if (table != NULL) |
Georg Brandl | 953152f | 2009-07-22 12:03:59 +0000 | [diff] [blame] | 1230 | PyBuffer_Release(&vtable); |
| 1231 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1232 | } |
| 1233 | } |
| 1234 | else { |
| 1235 | vdel.buf = NULL; |
| 1236 | vdel.len = 0; |
| 1237 | } |
| 1238 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1239 | inlen = PyByteArray_GET_SIZE(input_obj); |
| 1240 | result = PyByteArray_FromStringAndSize((char *)NULL, inlen); |
| 1241 | if (result == NULL) |
| 1242 | goto done; |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 1243 | output_start = output = PyByteArray_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1244 | input = PyByteArray_AS_STRING(input_obj); |
| 1245 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1246 | if (vdel.len == 0 && table_chars != NULL) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1247 | /* If no deletions are required, use faster code */ |
| 1248 | for (i = inlen; --i >= 0; ) { |
| 1249 | c = Py_CHARMASK(*input++); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1250 | *output++ = table_chars[c]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1251 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1252 | goto done; |
| 1253 | } |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1254 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1255 | if (table_chars == NULL) { |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1256 | for (i = 0; i < 256; i++) |
| 1257 | trans_table[i] = Py_CHARMASK(i); |
| 1258 | } else { |
| 1259 | for (i = 0; i < 256; i++) |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1260 | trans_table[i] = Py_CHARMASK(table_chars[i]); |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1261 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1262 | |
| 1263 | for (i = 0; i < vdel.len; i++) |
| 1264 | trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1; |
| 1265 | |
| 1266 | for (i = inlen; --i >= 0; ) { |
| 1267 | c = Py_CHARMASK(*input++); |
| 1268 | if (trans_table[c] != -1) |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 1269 | *output++ = (char)trans_table[c]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1270 | } |
| 1271 | /* Fix the size of the resulting string */ |
| 1272 | if (inlen > 0) |
Christian Heimes | c731bbe | 2013-07-21 02:04:35 +0200 | [diff] [blame] | 1273 | if (PyByteArray_Resize(result, output - output_start) < 0) { |
| 1274 | Py_CLEAR(result); |
| 1275 | goto done; |
| 1276 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1277 | |
| 1278 | done: |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1279 | if (table != NULL) |
Georg Brandl | ccc47b6 | 2008-12-28 11:44:14 +0000 | [diff] [blame] | 1280 | PyBuffer_Release(&vtable); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1281 | if (deletechars != NULL) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1282 | PyBuffer_Release(&vdel); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1283 | return result; |
| 1284 | } |
| 1285 | |
| 1286 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1287 | /*[clinic input] |
| 1288 | |
| 1289 | @staticmethod |
| 1290 | bytearray.maketrans |
| 1291 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1292 | frm: Py_buffer |
| 1293 | to: Py_buffer |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1294 | / |
| 1295 | |
| 1296 | Return a translation table useable for the bytes or bytearray translate method. |
| 1297 | |
| 1298 | The returned table will be one where each byte in frm is mapped to the byte at |
| 1299 | the same position in to. |
| 1300 | |
| 1301 | The bytes objects frm and to must be of the same length. |
| 1302 | [clinic start generated code]*/ |
| 1303 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1304 | static PyObject * |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1305 | bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1306 | /*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1307 | { |
| 1308 | return _Py_bytes_maketrans(frm, to); |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1312 | /*[clinic input] |
| 1313 | bytearray.replace |
| 1314 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1315 | old: Py_buffer |
| 1316 | new: Py_buffer |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1317 | count: Py_ssize_t = -1 |
| 1318 | Maximum number of occurrences to replace. |
| 1319 | -1 (the default value) means replace all occurrences. |
| 1320 | / |
| 1321 | |
| 1322 | Return a copy with all occurrences of substring old replaced by new. |
| 1323 | |
| 1324 | If the optional argument count is given, only the first count occurrences are |
| 1325 | replaced. |
| 1326 | [clinic start generated code]*/ |
| 1327 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1328 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1329 | bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old, |
| 1330 | Py_buffer *new, Py_ssize_t count) |
| 1331 | /*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1332 | { |
Serhiy Storchaka | fb81d3c | 2016-05-05 09:26:07 +0300 | [diff] [blame] | 1333 | return stringlib_replace((PyObject *)self, |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 1334 | (const char *)old->buf, old->len, |
| 1335 | (const char *)new->buf, new->len, count); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1338 | /*[clinic input] |
| 1339 | bytearray.split |
| 1340 | |
| 1341 | sep: object = None |
| 1342 | The delimiter according which to split the bytearray. |
| 1343 | None (the default value) means split on ASCII whitespace characters |
| 1344 | (space, tab, return, newline, formfeed, vertical tab). |
| 1345 | maxsplit: Py_ssize_t = -1 |
| 1346 | Maximum number of splits to do. |
| 1347 | -1 (the default value) means no limit. |
| 1348 | |
| 1349 | Return a list of the sections in the bytearray, using sep as the delimiter. |
| 1350 | [clinic start generated code]*/ |
| 1351 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1352 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1353 | bytearray_split_impl(PyByteArrayObject *self, PyObject *sep, |
| 1354 | Py_ssize_t maxsplit) |
| 1355 | /*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1356 | { |
| 1357 | Py_ssize_t len = PyByteArray_GET_SIZE(self), n; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1358 | const char *s = PyByteArray_AS_STRING(self), *sub; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1359 | PyObject *list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1360 | Py_buffer vsub; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1361 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1362 | if (maxsplit < 0) |
| 1363 | maxsplit = PY_SSIZE_T_MAX; |
| 1364 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1365 | if (sep == Py_None) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1366 | return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1367 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1368 | if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1369 | return NULL; |
| 1370 | sub = vsub.buf; |
| 1371 | n = vsub.len; |
| 1372 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1373 | list = stringlib_split( |
| 1374 | (PyObject*) self, s, len, sub, n, maxsplit |
| 1375 | ); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1376 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1377 | return list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1380 | /*[clinic input] |
| 1381 | bytearray.partition |
| 1382 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1383 | sep: object |
| 1384 | / |
| 1385 | |
| 1386 | Partition the bytearray into three parts using the given separator. |
| 1387 | |
| 1388 | This will search for the separator sep in the bytearray. If the separator is |
| 1389 | found, returns a 3-tuple containing the part before the separator, the |
| 1390 | separator itself, and the part after it. |
| 1391 | |
| 1392 | If the separator is not found, returns a 3-tuple containing the original |
| 1393 | bytearray object and two empty bytearray objects. |
| 1394 | [clinic start generated code]*/ |
| 1395 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1396 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1397 | bytearray_partition(PyByteArrayObject *self, PyObject *sep) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1398 | /*[clinic end generated code: output=45d2525ddd35f957 input=86f89223892b70b5]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1399 | { |
| 1400 | PyObject *bytesep, *result; |
| 1401 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1402 | bytesep = PyByteArray_FromObject(sep); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1403 | if (! bytesep) |
| 1404 | return NULL; |
| 1405 | |
| 1406 | result = stringlib_partition( |
| 1407 | (PyObject*) self, |
| 1408 | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
| 1409 | bytesep, |
| 1410 | PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) |
| 1411 | ); |
| 1412 | |
| 1413 | Py_DECREF(bytesep); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1414 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1417 | /*[clinic input] |
| 1418 | bytearray.rpartition |
| 1419 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1420 | sep: object |
| 1421 | / |
| 1422 | |
| 1423 | Partition the bytes into three parts using the given separator. |
| 1424 | |
| 1425 | This will search for the separator sep in the bytearray, starting and the end. |
| 1426 | If the separator is found, returns a 3-tuple containing the part before the |
| 1427 | separator, the separator itself, and the part after it. |
| 1428 | |
| 1429 | If the separator is not found, returns a 3-tuple containing two empty bytearray |
| 1430 | objects and the original bytearray object. |
| 1431 | [clinic start generated code]*/ |
| 1432 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1433 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1434 | bytearray_rpartition(PyByteArrayObject *self, PyObject *sep) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1435 | /*[clinic end generated code: output=440de3c9426115e8 input=5f4094f2de87c8f3]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1436 | { |
| 1437 | PyObject *bytesep, *result; |
| 1438 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1439 | bytesep = PyByteArray_FromObject(sep); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1440 | if (! bytesep) |
| 1441 | return NULL; |
| 1442 | |
| 1443 | result = stringlib_rpartition( |
| 1444 | (PyObject*) self, |
| 1445 | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
| 1446 | bytesep, |
| 1447 | PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) |
| 1448 | ); |
| 1449 | |
| 1450 | Py_DECREF(bytesep); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1451 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1454 | /*[clinic input] |
| 1455 | bytearray.rsplit = bytearray.split |
| 1456 | |
| 1457 | Return a list of the sections in the bytearray, using sep as the delimiter. |
| 1458 | |
| 1459 | Splitting is done starting at the end of the bytearray and working to the front. |
| 1460 | [clinic start generated code]*/ |
| 1461 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1462 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1463 | bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep, |
| 1464 | Py_ssize_t maxsplit) |
| 1465 | /*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1466 | { |
| 1467 | Py_ssize_t len = PyByteArray_GET_SIZE(self), n; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1468 | const char *s = PyByteArray_AS_STRING(self), *sub; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1469 | PyObject *list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1470 | Py_buffer vsub; |
| 1471 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1472 | if (maxsplit < 0) |
| 1473 | maxsplit = PY_SSIZE_T_MAX; |
| 1474 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1475 | if (sep == Py_None) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1476 | return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1477 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1478 | if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1479 | return NULL; |
| 1480 | sub = vsub.buf; |
| 1481 | n = vsub.len; |
| 1482 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1483 | list = stringlib_rsplit( |
| 1484 | (PyObject*) self, s, len, sub, n, maxsplit |
| 1485 | ); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1486 | PyBuffer_Release(&vsub); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1487 | return list; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1490 | /*[clinic input] |
| 1491 | bytearray.reverse |
| 1492 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1493 | Reverse the order of the values in B in place. |
| 1494 | [clinic start generated code]*/ |
| 1495 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1496 | static PyObject * |
| 1497 | bytearray_reverse_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1498 | /*[clinic end generated code: output=9f7616f29ab309d3 input=543356319fc78557]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1499 | { |
| 1500 | char swap, *head, *tail; |
| 1501 | Py_ssize_t i, j, n = Py_SIZE(self); |
| 1502 | |
| 1503 | j = n / 2; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1504 | head = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1505 | tail = head + n - 1; |
| 1506 | for (i = 0; i < j; i++) { |
| 1507 | swap = *head; |
| 1508 | *head++ = *tail; |
| 1509 | *tail-- = swap; |
| 1510 | } |
| 1511 | |
| 1512 | Py_RETURN_NONE; |
| 1513 | } |
| 1514 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1515 | |
| 1516 | /*[python input] |
| 1517 | class bytesvalue_converter(CConverter): |
| 1518 | type = 'int' |
| 1519 | converter = '_getbytevalue' |
| 1520 | [python start generated code]*/ |
| 1521 | /*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/ |
| 1522 | |
| 1523 | |
| 1524 | /*[clinic input] |
| 1525 | bytearray.insert |
| 1526 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1527 | index: Py_ssize_t |
| 1528 | The index where the value is to be inserted. |
| 1529 | item: bytesvalue |
| 1530 | The item to be inserted. |
| 1531 | / |
| 1532 | |
| 1533 | Insert a single item into the bytearray before the given index. |
| 1534 | [clinic start generated code]*/ |
| 1535 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1536 | static PyObject * |
| 1537 | bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1538 | /*[clinic end generated code: output=76c775a70e7b07b7 input=b2b5d07e9de6c070]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1539 | { |
| 1540 | Py_ssize_t n = Py_SIZE(self); |
| 1541 | char *buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1542 | |
| 1543 | if (n == PY_SSIZE_T_MAX) { |
| 1544 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 1545 | "cannot add more objects to bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1546 | return NULL; |
| 1547 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1548 | if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) |
| 1549 | return NULL; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1550 | buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1551 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1552 | if (index < 0) { |
| 1553 | index += n; |
| 1554 | if (index < 0) |
| 1555 | index = 0; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1556 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1557 | if (index > n) |
| 1558 | index = n; |
| 1559 | memmove(buf + index + 1, buf + index, n - index); |
| 1560 | buf[index] = item; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1561 | |
| 1562 | Py_RETURN_NONE; |
| 1563 | } |
| 1564 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1565 | /*[clinic input] |
| 1566 | bytearray.append |
| 1567 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1568 | item: bytesvalue |
| 1569 | The item to be appended. |
| 1570 | / |
| 1571 | |
| 1572 | Append a single item to the end of the bytearray. |
| 1573 | [clinic start generated code]*/ |
| 1574 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1575 | static PyObject * |
| 1576 | bytearray_append_impl(PyByteArrayObject *self, int item) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1577 | /*[clinic end generated code: output=a154e19ed1886cb6 input=20d6bec3d1340593]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1578 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1579 | Py_ssize_t n = Py_SIZE(self); |
| 1580 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1581 | if (n == PY_SSIZE_T_MAX) { |
| 1582 | PyErr_SetString(PyExc_OverflowError, |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 1583 | "cannot add more objects to bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1584 | return NULL; |
| 1585 | } |
| 1586 | if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) |
| 1587 | return NULL; |
| 1588 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1589 | PyByteArray_AS_STRING(self)[n] = item; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1590 | |
| 1591 | Py_RETURN_NONE; |
| 1592 | } |
| 1593 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1594 | /*[clinic input] |
| 1595 | bytearray.extend |
| 1596 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1597 | iterable_of_ints: object |
| 1598 | The iterable of items to append. |
| 1599 | / |
| 1600 | |
| 1601 | Append all the items from the iterator or sequence to the end of the bytearray. |
| 1602 | [clinic start generated code]*/ |
| 1603 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1604 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1605 | bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1606 | /*[clinic end generated code: output=98155dbe249170b1 input=c617b3a93249ba28]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1607 | { |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1608 | PyObject *it, *item, *bytearray_obj; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1609 | Py_ssize_t buf_size = 0, len = 0; |
| 1610 | int value; |
| 1611 | char *buf; |
| 1612 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1613 | /* bytearray_setslice code only accepts something supporting PEP 3118. */ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1614 | if (PyObject_CheckBuffer(iterable_of_ints)) { |
| 1615 | 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] | 1616 | return NULL; |
| 1617 | |
| 1618 | Py_RETURN_NONE; |
| 1619 | } |
| 1620 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1621 | it = PyObject_GetIter(iterable_of_ints); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1622 | if (it == NULL) |
| 1623 | return NULL; |
| 1624 | |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 1625 | /* 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] | 1626 | buf_size = PyObject_LengthHint(iterable_of_ints, 32); |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1627 | if (buf_size == -1) { |
| 1628 | Py_DECREF(it); |
| 1629 | return NULL; |
| 1630 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1631 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1632 | bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size); |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 1633 | if (bytearray_obj == NULL) { |
| 1634 | Py_DECREF(it); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1635 | return NULL; |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 1636 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1637 | buf = PyByteArray_AS_STRING(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1638 | |
| 1639 | while ((item = PyIter_Next(it)) != NULL) { |
| 1640 | if (! _getbytevalue(item, &value)) { |
| 1641 | Py_DECREF(item); |
| 1642 | Py_DECREF(it); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1643 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1644 | return NULL; |
| 1645 | } |
| 1646 | buf[len++] = value; |
| 1647 | Py_DECREF(item); |
| 1648 | |
| 1649 | if (len >= buf_size) { |
Martin Panter | 371731e | 2016-07-18 07:53:13 +0000 | [diff] [blame] | 1650 | Py_ssize_t addition; |
| 1651 | if (len == PY_SSIZE_T_MAX) { |
| 1652 | Py_DECREF(it); |
| 1653 | Py_DECREF(bytearray_obj); |
| 1654 | return PyErr_NoMemory(); |
| 1655 | } |
| 1656 | addition = len >> 1; |
| 1657 | if (addition > PY_SSIZE_T_MAX - len - 1) |
| 1658 | buf_size = PY_SSIZE_T_MAX; |
| 1659 | else |
| 1660 | buf_size = len + addition + 1; |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1661 | if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1662 | Py_DECREF(it); |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1663 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1664 | return NULL; |
| 1665 | } |
| 1666 | /* Recompute the `buf' pointer, since the resizing operation may |
| 1667 | have invalidated it. */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1668 | buf = PyByteArray_AS_STRING(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1669 | } |
| 1670 | } |
| 1671 | Py_DECREF(it); |
| 1672 | |
| 1673 | /* Resize down to exact size. */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1674 | if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) { |
| 1675 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1676 | return NULL; |
| 1677 | } |
| 1678 | |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 1679 | if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) { |
| 1680 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1681 | return NULL; |
Antoine Pitrou | 58bb82e | 2012-04-01 16:05:46 +0200 | [diff] [blame] | 1682 | } |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1683 | Py_DECREF(bytearray_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1684 | |
| 1685 | Py_RETURN_NONE; |
| 1686 | } |
| 1687 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1688 | /*[clinic input] |
| 1689 | bytearray.pop |
| 1690 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1691 | index: Py_ssize_t = -1 |
| 1692 | The index from where to remove the item. |
| 1693 | -1 (the default value) means remove the last item. |
| 1694 | / |
| 1695 | |
| 1696 | Remove and return a single item from B. |
| 1697 | |
| 1698 | If no index argument is given, will pop the last item. |
| 1699 | [clinic start generated code]*/ |
| 1700 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1701 | static PyObject * |
| 1702 | bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1703 | /*[clinic end generated code: output=e0ccd401f8021da8 input=3591df2d06c0d237]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1704 | { |
| 1705 | int value; |
| 1706 | Py_ssize_t n = Py_SIZE(self); |
| 1707 | char *buf; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1708 | |
| 1709 | if (n == 0) { |
Eli Bendersky | 1bc4f19 | 2011-03-04 04:55:25 +0000 | [diff] [blame] | 1710 | PyErr_SetString(PyExc_IndexError, |
| 1711 | "pop from empty bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1712 | return NULL; |
| 1713 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1714 | if (index < 0) |
| 1715 | index += Py_SIZE(self); |
| 1716 | if (index < 0 || index >= Py_SIZE(self)) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1717 | PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
| 1718 | return NULL; |
| 1719 | } |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 1720 | if (!_canresize(self)) |
| 1721 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1722 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1723 | buf = PyByteArray_AS_STRING(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1724 | value = buf[index]; |
| 1725 | memmove(buf + index, buf + index + 1, n - index); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1726 | if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) |
| 1727 | return NULL; |
| 1728 | |
Mark Dickinson | 54a3db9 | 2009-09-06 10:19:23 +0000 | [diff] [blame] | 1729 | return PyLong_FromLong((unsigned char)value); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1732 | /*[clinic input] |
| 1733 | bytearray.remove |
| 1734 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1735 | value: bytesvalue |
| 1736 | The value to remove. |
| 1737 | / |
| 1738 | |
| 1739 | Remove the first occurrence of a value in the bytearray. |
| 1740 | [clinic start generated code]*/ |
| 1741 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1742 | static PyObject * |
| 1743 | bytearray_remove_impl(PyByteArrayObject *self, int value) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 1744 | /*[clinic end generated code: output=d659e37866709c13 input=121831240cd51ddf]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1745 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1746 | Py_ssize_t where, n = Py_SIZE(self); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1747 | char *buf = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1748 | |
Serhiy Storchaka | 4b23494 | 2016-05-16 22:24:03 +0300 | [diff] [blame] | 1749 | where = stringlib_find_char(buf, n, value); |
| 1750 | if (where < 0) { |
Mark Dickinson | 2b6705f | 2009-09-06 10:34:47 +0000 | [diff] [blame] | 1751 | PyErr_SetString(PyExc_ValueError, "value not found in bytearray"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1752 | return NULL; |
| 1753 | } |
Antoine Pitrou | 5504e89 | 2008-12-06 21:27:53 +0000 | [diff] [blame] | 1754 | if (!_canresize(self)) |
| 1755 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1756 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1757 | memmove(buf + where, buf + where + 1, n - where); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1758 | if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) |
| 1759 | return NULL; |
| 1760 | |
| 1761 | Py_RETURN_NONE; |
| 1762 | } |
| 1763 | |
| 1764 | /* XXX These two helpers could be optimized if argsize == 1 */ |
| 1765 | |
| 1766 | static Py_ssize_t |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1767 | lstrip_helper(const char *myptr, Py_ssize_t mysize, |
| 1768 | const void *argptr, Py_ssize_t argsize) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1769 | { |
| 1770 | Py_ssize_t i = 0; |
Antoine Pitrou | 5b72075 | 2013-10-05 21:24:10 +0200 | [diff] [blame] | 1771 | while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1772 | i++; |
| 1773 | return i; |
| 1774 | } |
| 1775 | |
| 1776 | static Py_ssize_t |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1777 | rstrip_helper(const char *myptr, Py_ssize_t mysize, |
| 1778 | const void *argptr, Py_ssize_t argsize) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1779 | { |
| 1780 | Py_ssize_t i = mysize - 1; |
Antoine Pitrou | 5b72075 | 2013-10-05 21:24:10 +0200 | [diff] [blame] | 1781 | while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize)) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1782 | i--; |
| 1783 | return i + 1; |
| 1784 | } |
| 1785 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1786 | /*[clinic input] |
| 1787 | bytearray.strip |
| 1788 | |
| 1789 | bytes: object = None |
| 1790 | / |
| 1791 | |
| 1792 | Strip leading and trailing bytes contained in the argument. |
| 1793 | |
| 1794 | If the argument is omitted or None, strip leading and trailing ASCII whitespace. |
| 1795 | [clinic start generated code]*/ |
| 1796 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1797 | static PyObject * |
| 1798 | bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1799 | /*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1800 | { |
| 1801 | Py_ssize_t left, right, mysize, byteslen; |
| 1802 | char *myptr, *bytesptr; |
| 1803 | Py_buffer vbytes; |
| 1804 | |
| 1805 | if (bytes == Py_None) { |
| 1806 | bytesptr = "\t\n\r\f\v "; |
| 1807 | byteslen = 6; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1808 | } |
| 1809 | else { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1810 | if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1811 | return NULL; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1812 | bytesptr = (char *) vbytes.buf; |
| 1813 | byteslen = vbytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1814 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1815 | myptr = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1816 | mysize = Py_SIZE(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1817 | left = lstrip_helper(myptr, mysize, bytesptr, byteslen); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1818 | if (left == mysize) |
| 1819 | right = left; |
| 1820 | else |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1821 | right = rstrip_helper(myptr, mysize, bytesptr, byteslen); |
| 1822 | if (bytes != Py_None) |
| 1823 | PyBuffer_Release(&vbytes); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1824 | return PyByteArray_FromStringAndSize(myptr + left, right - left); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1827 | /*[clinic input] |
| 1828 | bytearray.lstrip |
| 1829 | |
| 1830 | bytes: object = None |
| 1831 | / |
| 1832 | |
| 1833 | Strip leading bytes contained in the argument. |
| 1834 | |
| 1835 | If the argument is omitted or None, strip leading ASCII whitespace. |
| 1836 | [clinic start generated code]*/ |
| 1837 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1838 | static PyObject * |
| 1839 | bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1840 | /*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1841 | { |
| 1842 | Py_ssize_t left, right, mysize, byteslen; |
| 1843 | char *myptr, *bytesptr; |
| 1844 | Py_buffer vbytes; |
| 1845 | |
| 1846 | if (bytes == Py_None) { |
| 1847 | bytesptr = "\t\n\r\f\v "; |
| 1848 | byteslen = 6; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1849 | } |
| 1850 | else { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1851 | if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1852 | return NULL; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1853 | bytesptr = (char *) vbytes.buf; |
| 1854 | byteslen = vbytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1855 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1856 | myptr = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1857 | mysize = Py_SIZE(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1858 | left = lstrip_helper(myptr, mysize, bytesptr, byteslen); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1859 | right = mysize; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1860 | if (bytes != Py_None) |
| 1861 | PyBuffer_Release(&vbytes); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1862 | return PyByteArray_FromStringAndSize(myptr + left, right - left); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1865 | /*[clinic input] |
| 1866 | bytearray.rstrip |
| 1867 | |
| 1868 | bytes: object = None |
| 1869 | / |
| 1870 | |
| 1871 | Strip trailing bytes contained in the argument. |
| 1872 | |
| 1873 | If the argument is omitted or None, strip trailing ASCII whitespace. |
| 1874 | [clinic start generated code]*/ |
| 1875 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1876 | static PyObject * |
| 1877 | bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1878 | /*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1879 | { |
| 1880 | Py_ssize_t right, mysize, byteslen; |
| 1881 | char *myptr, *bytesptr; |
| 1882 | Py_buffer vbytes; |
| 1883 | |
| 1884 | if (bytes == Py_None) { |
| 1885 | bytesptr = "\t\n\r\f\v "; |
| 1886 | byteslen = 6; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1887 | } |
| 1888 | else { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1889 | if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1890 | return NULL; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1891 | bytesptr = (char *) vbytes.buf; |
| 1892 | byteslen = vbytes.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1893 | } |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1894 | myptr = PyByteArray_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1895 | mysize = Py_SIZE(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1896 | right = rstrip_helper(myptr, mysize, bytesptr, byteslen); |
| 1897 | if (bytes != Py_None) |
| 1898 | PyBuffer_Release(&vbytes); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 1899 | return PyByteArray_FromStringAndSize(myptr, right); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1902 | /*[clinic input] |
| 1903 | bytearray.decode |
| 1904 | |
| 1905 | encoding: str(c_default="NULL") = 'utf-8' |
| 1906 | The encoding with which to decode the bytearray. |
| 1907 | errors: str(c_default="NULL") = 'strict' |
| 1908 | The error handling scheme to use for the handling of decoding errors. |
| 1909 | The default is 'strict' meaning that decoding errors raise a |
| 1910 | UnicodeDecodeError. Other possible values are 'ignore' and 'replace' |
| 1911 | as well as any other name registered with codecs.register_error that |
| 1912 | can handle UnicodeDecodeErrors. |
| 1913 | |
| 1914 | Decode the bytearray using the codec registered for encoding. |
| 1915 | [clinic start generated code]*/ |
| 1916 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1917 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 1918 | bytearray_decode_impl(PyByteArrayObject *self, const char *encoding, |
| 1919 | const char *errors) |
| 1920 | /*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1921 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1922 | if (encoding == NULL) |
| 1923 | encoding = PyUnicode_GetDefaultEncoding(); |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1924 | return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
| 1927 | PyDoc_STRVAR(alloc_doc, |
| 1928 | "B.__alloc__() -> int\n\ |
| 1929 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1930 | Return the number of bytes actually allocated."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1931 | |
| 1932 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 1933 | bytearray_alloc(PyByteArrayObject *self) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1934 | { |
| 1935 | return PyLong_FromSsize_t(self->ob_alloc); |
| 1936 | } |
| 1937 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1938 | /*[clinic input] |
| 1939 | bytearray.join |
| 1940 | |
| 1941 | iterable_of_bytes: object |
| 1942 | / |
| 1943 | |
| 1944 | Concatenate any number of bytes/bytearray objects. |
| 1945 | |
| 1946 | The bytearray whose method is called is inserted in between each pair. |
| 1947 | |
| 1948 | The result is returned as a new bytearray object. |
| 1949 | [clinic start generated code]*/ |
| 1950 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1951 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1952 | bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1953 | /*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1954 | { |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1955 | return stringlib_bytes_join((PyObject*)self, iterable_of_bytes); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1958 | /*[clinic input] |
| 1959 | bytearray.splitlines |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1960 | |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 1961 | keepends: bool(accept={int}) = False |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1962 | |
| 1963 | Return a list of the lines in the bytearray, breaking at line boundaries. |
| 1964 | |
| 1965 | Line breaks are not included in the resulting list unless keepends is given and |
| 1966 | true. |
| 1967 | [clinic start generated code]*/ |
| 1968 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1969 | static PyObject * |
| 1970 | bytearray_splitlines_impl(PyByteArrayObject *self, int keepends) |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 1971 | /*[clinic end generated code: output=4223c94b895f6ad9 input=99a27ad959b9cf6b]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1972 | { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1973 | return stringlib_splitlines( |
| 1974 | (PyObject*) self, PyByteArray_AS_STRING(self), |
| 1975 | PyByteArray_GET_SIZE(self), keepends |
| 1976 | ); |
| 1977 | } |
| 1978 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1979 | /*[clinic input] |
| 1980 | @classmethod |
| 1981 | bytearray.fromhex |
| 1982 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1983 | string: unicode |
| 1984 | / |
| 1985 | |
| 1986 | Create a bytearray object from a string of hexadecimal numbers. |
| 1987 | |
| 1988 | Spaces between two numbers are accepted. |
| 1989 | Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef') |
| 1990 | [clinic start generated code]*/ |
| 1991 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1992 | static PyObject * |
Serhiy Storchaka | 0855e70 | 2016-07-01 17:22:31 +0300 | [diff] [blame] | 1993 | bytearray_fromhex_impl(PyTypeObject *type, PyObject *string) |
| 1994 | /*[clinic end generated code: output=8f0f0b6d30fb3ba0 input=f033a16d1fb21f48]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1995 | { |
Serhiy Storchaka | 0855e70 | 2016-07-01 17:22:31 +0300 | [diff] [blame] | 1996 | PyObject *result = _PyBytes_FromHex(string, type == &PyByteArray_Type); |
| 1997 | if (type != &PyByteArray_Type && result != NULL) { |
Victor Stinner | de4ae3d | 2016-12-04 22:59:09 +0100 | [diff] [blame] | 1998 | Py_SETREF(result, PyObject_CallFunctionObjArgs((PyObject *)type, |
| 1999 | result, NULL)); |
Serhiy Storchaka | 0855e70 | 2016-07-01 17:22:31 +0300 | [diff] [blame] | 2000 | } |
| 2001 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
Gregory P. Smith | 8cb6569 | 2015-04-25 23:22:26 +0000 | [diff] [blame] | 2004 | PyDoc_STRVAR(hex__doc__, |
| 2005 | "B.hex() -> string\n\ |
| 2006 | \n\ |
| 2007 | Create a string of hexadecimal numbers from a bytearray object.\n\ |
| 2008 | Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'."); |
| 2009 | |
| 2010 | static PyObject * |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2011 | bytearray_hex(PyBytesObject *self) |
| 2012 | { |
| 2013 | char* argbuf = PyByteArray_AS_STRING(self); |
| 2014 | Py_ssize_t arglen = PyByteArray_GET_SIZE(self); |
| 2015 | return _Py_strhex(argbuf, arglen); |
| 2016 | } |
| 2017 | |
| 2018 | static PyObject * |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2019 | _common_reduce(PyByteArrayObject *self, int proto) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2020 | { |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2021 | PyObject *dict; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2022 | _Py_IDENTIFIER(__dict__); |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2023 | char *buf; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2024 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2025 | dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2026 | if (dict == NULL) { |
| 2027 | PyErr_Clear(); |
| 2028 | dict = Py_None; |
| 2029 | Py_INCREF(dict); |
| 2030 | } |
| 2031 | |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2032 | buf = PyByteArray_AS_STRING(self); |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2033 | if (proto < 3) { |
| 2034 | /* use str based reduction for backwards compatibility with Python 2.x */ |
| 2035 | PyObject *latin1; |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2036 | if (Py_SIZE(self)) |
| 2037 | latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL); |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2038 | else |
| 2039 | latin1 = PyUnicode_FromString(""); |
| 2040 | return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict); |
| 2041 | } |
| 2042 | else { |
| 2043 | /* use more efficient byte based reduction */ |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2044 | if (Py_SIZE(self)) { |
| 2045 | 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] | 2046 | } |
| 2047 | else { |
| 2048 | return Py_BuildValue("(O()N)", Py_TYPE(self), dict); |
| 2049 | } |
| 2050 | } |
| 2051 | } |
| 2052 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2053 | /*[clinic input] |
| 2054 | bytearray.__reduce__ as bytearray_reduce |
| 2055 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2056 | Return state information for pickling. |
| 2057 | [clinic start generated code]*/ |
| 2058 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2059 | static PyObject * |
| 2060 | bytearray_reduce_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 2061 | /*[clinic end generated code: output=52bf304086464cab input=44b5737ada62dd3f]*/ |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2062 | { |
| 2063 | return _common_reduce(self, 2); |
| 2064 | } |
| 2065 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2066 | /*[clinic input] |
| 2067 | bytearray.__reduce_ex__ as bytearray_reduce_ex |
| 2068 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2069 | proto: int = 0 |
| 2070 | / |
| 2071 | |
| 2072 | Return state information for pickling. |
| 2073 | [clinic start generated code]*/ |
| 2074 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2075 | static PyObject * |
| 2076 | bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 2077 | /*[clinic end generated code: output=52eac33377197520 input=f129bc1a1aa151ee]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2078 | { |
Antoine Pitrou | b0e1f8b | 2011-12-05 20:40:08 +0100 | [diff] [blame] | 2079 | return _common_reduce(self, proto); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2082 | /*[clinic input] |
| 2083 | bytearray.__sizeof__ as bytearray_sizeof |
| 2084 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2085 | Returns the size of the bytearray object in memory, in bytes. |
| 2086 | [clinic start generated code]*/ |
| 2087 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2088 | static PyObject * |
| 2089 | bytearray_sizeof_impl(PyByteArrayObject *self) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 2090 | /*[clinic end generated code: output=738abdd17951c427 input=e27320fd98a4bc5a]*/ |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2091 | { |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 2092 | Py_ssize_t res; |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2093 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 2094 | res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char); |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 2095 | return PyLong_FromSsize_t(res); |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2098 | static PySequenceMethods bytearray_as_sequence = { |
| 2099 | (lenfunc)bytearray_length, /* sq_length */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2100 | (binaryfunc)PyByteArray_Concat, /* sq_concat */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2101 | (ssizeargfunc)bytearray_repeat, /* sq_repeat */ |
| 2102 | (ssizeargfunc)bytearray_getitem, /* sq_item */ |
| 2103 | 0, /* sq_slice */ |
| 2104 | (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */ |
| 2105 | 0, /* sq_ass_slice */ |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2106 | (objobjproc)bytearray_contains, /* sq_contains */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2107 | (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */ |
| 2108 | (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2109 | }; |
| 2110 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2111 | static PyMappingMethods bytearray_as_mapping = { |
| 2112 | (lenfunc)bytearray_length, |
| 2113 | (binaryfunc)bytearray_subscript, |
| 2114 | (objobjargproc)bytearray_ass_subscript, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2115 | }; |
| 2116 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2117 | static PyBufferProcs bytearray_as_buffer = { |
| 2118 | (getbufferproc)bytearray_getbuffer, |
| 2119 | (releasebufferproc)bytearray_releasebuffer, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2120 | }; |
| 2121 | |
| 2122 | static PyMethodDef |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2123 | bytearray_methods[] = { |
| 2124 | {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2125 | BYTEARRAY_REDUCE_METHODDEF |
| 2126 | BYTEARRAY_REDUCE_EX_METHODDEF |
| 2127 | BYTEARRAY_SIZEOF_METHODDEF |
| 2128 | BYTEARRAY_APPEND_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2129 | {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS, |
| 2130 | _Py_capitalize__doc__}, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2131 | {"center", (PyCFunction)stringlib_center, METH_VARARGS, _Py_center__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2132 | BYTEARRAY_CLEAR_METHODDEF |
| 2133 | BYTEARRAY_COPY_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2134 | {"count", (PyCFunction)bytearray_count, METH_VARARGS, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2135 | _Py_count__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2136 | BYTEARRAY_DECODE_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2137 | {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2138 | _Py_endswith__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 2139 | {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2140 | _Py_expandtabs__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2141 | BYTEARRAY_EXTEND_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2142 | {"find", (PyCFunction)bytearray_find, METH_VARARGS, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2143 | _Py_find__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2144 | BYTEARRAY_FROMHEX_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2145 | {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__}, |
| 2146 | {"index", (PyCFunction)bytearray_index, METH_VARARGS, _Py_index__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2147 | BYTEARRAY_INSERT_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2148 | {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS, |
| 2149 | _Py_isalnum__doc__}, |
| 2150 | {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS, |
| 2151 | _Py_isalpha__doc__}, |
| 2152 | {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS, |
| 2153 | _Py_isdigit__doc__}, |
| 2154 | {"islower", (PyCFunction)stringlib_islower, METH_NOARGS, |
| 2155 | _Py_islower__doc__}, |
| 2156 | {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS, |
| 2157 | _Py_isspace__doc__}, |
| 2158 | {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS, |
| 2159 | _Py_istitle__doc__}, |
| 2160 | {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS, |
| 2161 | _Py_isupper__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2162 | BYTEARRAY_JOIN_METHODDEF |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2163 | {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, _Py_ljust__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2164 | {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2165 | BYTEARRAY_LSTRIP_METHODDEF |
| 2166 | BYTEARRAY_MAKETRANS_METHODDEF |
| 2167 | BYTEARRAY_PARTITION_METHODDEF |
| 2168 | BYTEARRAY_POP_METHODDEF |
| 2169 | BYTEARRAY_REMOVE_METHODDEF |
| 2170 | BYTEARRAY_REPLACE_METHODDEF |
| 2171 | BYTEARRAY_REVERSE_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2172 | {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__}, |
| 2173 | {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__}, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2174 | {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, _Py_rjust__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2175 | BYTEARRAY_RPARTITION_METHODDEF |
| 2176 | BYTEARRAY_RSPLIT_METHODDEF |
| 2177 | BYTEARRAY_RSTRIP_METHODDEF |
| 2178 | BYTEARRAY_SPLIT_METHODDEF |
| 2179 | BYTEARRAY_SPLITLINES_METHODDEF |
Serhiy Storchaka | e09132f | 2016-07-03 13:57:48 +0300 | [diff] [blame] | 2180 | {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS , |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2181 | _Py_startswith__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2182 | BYTEARRAY_STRIP_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2183 | {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS, |
| 2184 | _Py_swapcase__doc__}, |
| 2185 | {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2186 | BYTEARRAY_TRANSLATE_METHODDEF |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2187 | {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__}, |
Serhiy Storchaka | dd40fc3 | 2016-05-04 22:23:26 +0300 | [diff] [blame] | 2188 | {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, _Py_zfill__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2189 | {NULL} |
| 2190 | }; |
| 2191 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 2192 | static PyObject * |
| 2193 | bytearray_mod(PyObject *v, PyObject *w) |
| 2194 | { |
| 2195 | if (!PyByteArray_Check(v)) |
| 2196 | Py_RETURN_NOTIMPLEMENTED; |
Berker Peksag | 43de36d | 2016-04-16 01:20:47 +0300 | [diff] [blame] | 2197 | return _PyBytes_FormatEx(PyByteArray_AS_STRING(v), PyByteArray_GET_SIZE(v), w, 1); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 2198 | } |
| 2199 | |
| 2200 | static PyNumberMethods bytearray_as_number = { |
| 2201 | 0, /*nb_add*/ |
| 2202 | 0, /*nb_subtract*/ |
| 2203 | 0, /*nb_multiply*/ |
| 2204 | bytearray_mod, /*nb_remainder*/ |
| 2205 | }; |
| 2206 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2207 | PyDoc_STRVAR(bytearray_doc, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2208 | "bytearray(iterable_of_ints) -> bytearray\n\ |
| 2209 | bytearray(string, encoding[, errors]) -> bytearray\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 2210 | bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\ |
| 2211 | bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\ |
| 2212 | bytearray() -> empty bytes array\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2213 | \n\ |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 2214 | Construct a mutable bytearray object from:\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2215 | - an iterable yielding integers in range(256)\n\ |
| 2216 | - a text string encoded using the specified encoding\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 2217 | - a bytes or a buffer object\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2218 | - any object implementing the buffer API.\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 2219 | - an integer"); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2220 | |
| 2221 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2222 | static PyObject *bytearray_iter(PyObject *seq); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2223 | |
| 2224 | PyTypeObject PyByteArray_Type = { |
| 2225 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2226 | "bytearray", |
| 2227 | sizeof(PyByteArrayObject), |
| 2228 | 0, |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2229 | (destructor)bytearray_dealloc, /* tp_dealloc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2230 | 0, /* tp_print */ |
| 2231 | 0, /* tp_getattr */ |
| 2232 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 2233 | 0, /* tp_reserved */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2234 | (reprfunc)bytearray_repr, /* tp_repr */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 2235 | &bytearray_as_number, /* tp_as_number */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2236 | &bytearray_as_sequence, /* tp_as_sequence */ |
| 2237 | &bytearray_as_mapping, /* tp_as_mapping */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2238 | 0, /* tp_hash */ |
| 2239 | 0, /* tp_call */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2240 | bytearray_str, /* tp_str */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2241 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2242 | 0, /* tp_setattro */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2243 | &bytearray_as_buffer, /* tp_as_buffer */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2244 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2245 | bytearray_doc, /* tp_doc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2246 | 0, /* tp_traverse */ |
| 2247 | 0, /* tp_clear */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2248 | (richcmpfunc)bytearray_richcompare, /* tp_richcompare */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2249 | 0, /* tp_weaklistoffset */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2250 | bytearray_iter, /* tp_iter */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2251 | 0, /* tp_iternext */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2252 | bytearray_methods, /* tp_methods */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2253 | 0, /* tp_members */ |
| 2254 | 0, /* tp_getset */ |
| 2255 | 0, /* tp_base */ |
| 2256 | 0, /* tp_dict */ |
| 2257 | 0, /* tp_descr_get */ |
| 2258 | 0, /* tp_descr_set */ |
| 2259 | 0, /* tp_dictoffset */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2260 | (initproc)bytearray_init, /* tp_init */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2261 | PyType_GenericAlloc, /* tp_alloc */ |
| 2262 | PyType_GenericNew, /* tp_new */ |
| 2263 | PyObject_Del, /* tp_free */ |
| 2264 | }; |
| 2265 | |
| 2266 | /*********************** Bytes Iterator ****************************/ |
| 2267 | |
| 2268 | typedef struct { |
| 2269 | PyObject_HEAD |
| 2270 | Py_ssize_t it_index; |
| 2271 | PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 2272 | } bytesiterobject; |
| 2273 | |
| 2274 | static void |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2275 | bytearrayiter_dealloc(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2276 | { |
| 2277 | _PyObject_GC_UNTRACK(it); |
| 2278 | Py_XDECREF(it->it_seq); |
| 2279 | PyObject_GC_Del(it); |
| 2280 | } |
| 2281 | |
| 2282 | static int |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2283 | bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2284 | { |
| 2285 | Py_VISIT(it->it_seq); |
| 2286 | return 0; |
| 2287 | } |
| 2288 | |
| 2289 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2290 | bytearrayiter_next(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2291 | { |
| 2292 | PyByteArrayObject *seq; |
| 2293 | PyObject *item; |
| 2294 | |
| 2295 | assert(it != NULL); |
| 2296 | seq = it->it_seq; |
| 2297 | if (seq == NULL) |
| 2298 | return NULL; |
| 2299 | assert(PyByteArray_Check(seq)); |
| 2300 | |
| 2301 | if (it->it_index < PyByteArray_GET_SIZE(seq)) { |
| 2302 | item = PyLong_FromLong( |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 2303 | (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2304 | if (item != NULL) |
| 2305 | ++it->it_index; |
| 2306 | return item; |
| 2307 | } |
| 2308 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2309 | it->it_seq = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 2310 | Py_DECREF(seq); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2311 | return NULL; |
| 2312 | } |
| 2313 | |
| 2314 | static PyObject * |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2315 | bytearrayiter_length_hint(bytesiterobject *it) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2316 | { |
| 2317 | Py_ssize_t len = 0; |
Serhiy Storchaka | af65872 | 2016-07-03 14:41:36 +0300 | [diff] [blame] | 2318 | if (it->it_seq) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2319 | len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index; |
Serhiy Storchaka | af65872 | 2016-07-03 14:41:36 +0300 | [diff] [blame] | 2320 | if (len < 0) { |
| 2321 | len = 0; |
| 2322 | } |
| 2323 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2324 | return PyLong_FromSsize_t(len); |
| 2325 | } |
| 2326 | |
| 2327 | PyDoc_STRVAR(length_hint_doc, |
| 2328 | "Private method returning an estimate of len(list(it))."); |
| 2329 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2330 | static PyObject * |
| 2331 | bytearrayiter_reduce(bytesiterobject *it) |
| 2332 | { |
| 2333 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 2334 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2335 | it->it_seq, it->it_index); |
| 2336 | } else { |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 2337 | return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter")); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2338 | } |
| 2339 | } |
| 2340 | |
| 2341 | static PyObject * |
| 2342 | bytearrayiter_setstate(bytesiterobject *it, PyObject *state) |
| 2343 | { |
| 2344 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 2345 | if (index == -1 && PyErr_Occurred()) |
| 2346 | return NULL; |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 2347 | if (it->it_seq != NULL) { |
| 2348 | if (index < 0) |
| 2349 | index = 0; |
| 2350 | else if (index > PyByteArray_GET_SIZE(it->it_seq)) |
| 2351 | index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */ |
| 2352 | it->it_index = index; |
| 2353 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2354 | Py_RETURN_NONE; |
| 2355 | } |
| 2356 | |
| 2357 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 2358 | |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2359 | static PyMethodDef bytearrayiter_methods[] = { |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2360 | {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2361 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2362 | {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2363 | bytearray_reduce__doc__}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2364 | {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O, |
| 2365 | setstate_doc}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2366 | {NULL, NULL} /* sentinel */ |
| 2367 | }; |
| 2368 | |
| 2369 | PyTypeObject PyByteArrayIter_Type = { |
| 2370 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2371 | "bytearray_iterator", /* tp_name */ |
| 2372 | sizeof(bytesiterobject), /* tp_basicsize */ |
| 2373 | 0, /* tp_itemsize */ |
| 2374 | /* methods */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2375 | (destructor)bytearrayiter_dealloc, /* tp_dealloc */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2376 | 0, /* tp_print */ |
| 2377 | 0, /* tp_getattr */ |
| 2378 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 2379 | 0, /* tp_reserved */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2380 | 0, /* tp_repr */ |
| 2381 | 0, /* tp_as_number */ |
| 2382 | 0, /* tp_as_sequence */ |
| 2383 | 0, /* tp_as_mapping */ |
| 2384 | 0, /* tp_hash */ |
| 2385 | 0, /* tp_call */ |
| 2386 | 0, /* tp_str */ |
| 2387 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2388 | 0, /* tp_setattro */ |
| 2389 | 0, /* tp_as_buffer */ |
| 2390 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 2391 | 0, /* tp_doc */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2392 | (traverseproc)bytearrayiter_traverse, /* tp_traverse */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2393 | 0, /* tp_clear */ |
| 2394 | 0, /* tp_richcompare */ |
| 2395 | 0, /* tp_weaklistoffset */ |
| 2396 | PyObject_SelfIter, /* tp_iter */ |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2397 | (iternextfunc)bytearrayiter_next, /* tp_iternext */ |
| 2398 | bytearrayiter_methods, /* tp_methods */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2399 | 0, |
| 2400 | }; |
| 2401 | |
| 2402 | static PyObject * |
Benjamin Peterson | 153c70f | 2009-04-18 15:42:12 +0000 | [diff] [blame] | 2403 | bytearray_iter(PyObject *seq) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2404 | { |
| 2405 | bytesiterobject *it; |
| 2406 | |
| 2407 | if (!PyByteArray_Check(seq)) { |
| 2408 | PyErr_BadInternalCall(); |
| 2409 | return NULL; |
| 2410 | } |
| 2411 | it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type); |
| 2412 | if (it == NULL) |
| 2413 | return NULL; |
| 2414 | it->it_index = 0; |
| 2415 | Py_INCREF(seq); |
| 2416 | it->it_seq = (PyByteArrayObject *)seq; |
| 2417 | _PyObject_GC_TRACK(it); |
| 2418 | return (PyObject *)it; |
| 2419 | } |