blob: 94577689bb5c859e925f5ab4068abd8f43cee0ad [file] [log] [blame]
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001/* PyByteArray (bytearray) implementation */
2
3#define PY_SSIZE_T_CLEAN
4#include "Python.h"
5#include "structmember.h"
6#include "bytes_methods.h"
Ethan Furmanb95b5612015-01-23 20:05:18 -08007#include "bytesobject.h"
Gregory P. Smith8cb65692015-04-25 23:22:26 +00008#include "pystrhex.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00009
Martin v. Löwis7252a6e2014-07-27 16:25:09 +020010/*[clinic input]
11class bytearray "PyByteArrayObject *" "&PyByteArray_Type"
12[clinic start generated code]*/
13/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/
14
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000015char _PyByteArray_empty_string[] = "";
Christian Heimes2c9c7a52008-05-26 13:42:13 +000016
17void
18PyByteArray_Fini(void)
19{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000020}
21
22int
23PyByteArray_Init(void)
24{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000025 return 1;
26}
27
28/* end nullbytes support */
29
30/* Helpers */
31
32static int
33_getbytevalue(PyObject* arg, int *value)
34{
35 long face_value;
36
37 if (PyLong_Check(arg)) {
38 face_value = PyLong_AsLong(arg);
Georg Brandl9a54d7c2008-07-16 23:15:30 +000039 } else {
40 PyObject *index = PyNumber_Index(arg);
41 if (index == NULL) {
42 PyErr_Format(PyExc_TypeError, "an integer is required");
Mark Dickinson10de93a2010-07-09 19:25:48 +000043 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000044 return 0;
45 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +000046 face_value = PyLong_AsLong(index);
47 Py_DECREF(index);
48 }
49
50 if (face_value < 0 || face_value >= 256) {
51 /* this includes the OverflowError in case the long is too large */
52 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
Mark Dickinson10de93a2010-07-09 19:25:48 +000053 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000054 return 0;
55 }
56
57 *value = face_value;
58 return 1;
59}
60
61static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +000062bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000063{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000064 void *ptr;
65 if (view == NULL) {
Stefan Krah5178d912015-02-03 16:57:21 +010066 PyErr_SetString(PyExc_BufferError,
67 "bytearray_getbuffer: view==NULL argument is obsolete");
68 return -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000069 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000070 ptr = (void *) PyByteArray_AS_STRING(obj);
Stefan Krah5178d912015-02-03 16:57:21 +010071 /* cannot fail if view != NULL and readonly == 0 */
72 (void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
73 obj->ob_exports++;
74 return 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000075}
76
77static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +000078bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000079{
80 obj->ob_exports--;
81}
82
Antoine Pitrou5504e892008-12-06 21:27:53 +000083static int
84_canresize(PyByteArrayObject *self)
85{
86 if (self->ob_exports > 0) {
87 PyErr_SetString(PyExc_BufferError,
88 "Existing exports of data: object cannot be re-sized");
89 return 0;
90 }
91 return 1;
92}
93
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030094#include "clinic/bytearrayobject.c.h"
95
Christian Heimes2c9c7a52008-05-26 13:42:13 +000096/* Direct API functions */
97
98PyObject *
99PyByteArray_FromObject(PyObject *input)
100{
101 return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type,
102 input, NULL);
103}
104
105PyObject *
106PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
107{
108 PyByteArrayObject *new;
109 Py_ssize_t alloc;
110
111 if (size < 0) {
112 PyErr_SetString(PyExc_SystemError,
113 "Negative size passed to PyByteArray_FromStringAndSize");
114 return NULL;
115 }
116
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000117 /* Prevent buffer overflow when setting alloc to size+1. */
118 if (size == PY_SSIZE_T_MAX) {
119 return PyErr_NoMemory();
120 }
121
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000122 new = PyObject_New(PyByteArrayObject, &PyByteArray_Type);
123 if (new == NULL)
124 return NULL;
125
126 if (size == 0) {
127 new->ob_bytes = NULL;
128 alloc = 0;
129 }
130 else {
131 alloc = size + 1;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100132 new->ob_bytes = PyObject_Malloc(alloc);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000133 if (new->ob_bytes == NULL) {
134 Py_DECREF(new);
135 return PyErr_NoMemory();
136 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +0000137 if (bytes != NULL && size > 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000138 memcpy(new->ob_bytes, bytes, size);
139 new->ob_bytes[size] = '\0'; /* Trailing null byte */
140 }
141 Py_SIZE(new) = size;
142 new->ob_alloc = alloc;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200143 new->ob_start = new->ob_bytes;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000144 new->ob_exports = 0;
145
146 return (PyObject *)new;
147}
148
149Py_ssize_t
150PyByteArray_Size(PyObject *self)
151{
152 assert(self != NULL);
153 assert(PyByteArray_Check(self));
154
155 return PyByteArray_GET_SIZE(self);
156}
157
158char *
159PyByteArray_AsString(PyObject *self)
160{
161 assert(self != NULL);
162 assert(PyByteArray_Check(self));
163
164 return PyByteArray_AS_STRING(self);
165}
166
167int
Antoine Pitroucc231542014-11-02 18:40:09 +0100168PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000169{
170 void *sval;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200171 PyByteArrayObject *obj = ((PyByteArrayObject *)self);
Antoine Pitroucc231542014-11-02 18:40:09 +0100172 /* All computations are done unsigned to avoid integer overflows
173 (see issue #22335). */
174 size_t alloc = (size_t) obj->ob_alloc;
175 size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes);
176 size_t size = (size_t) requested_size;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000177
178 assert(self != NULL);
179 assert(PyByteArray_Check(self));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200180 assert(logical_offset <= alloc);
Antoine Pitroucc231542014-11-02 18:40:09 +0100181 assert(requested_size >= 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000182
Antoine Pitroucc231542014-11-02 18:40:09 +0100183 if (requested_size == Py_SIZE(self)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000184 return 0;
185 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200186 if (!_canresize(obj)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000187 return -1;
188 }
189
Antoine Pitrou25454112015-05-19 20:52:27 +0200190 if (size + logical_offset + 1 <= alloc) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200191 /* Current buffer is large enough to host the requested size,
192 decide on a strategy. */
193 if (size < alloc / 2) {
194 /* Major downsize; resize down to exact size */
195 alloc = size + 1;
196 }
197 else {
198 /* Minor downsize; quick exit */
199 Py_SIZE(self) = size;
200 PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */
201 return 0;
202 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000203 }
204 else {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200205 /* Need growing, decide on a strategy */
206 if (size <= alloc * 1.125) {
207 /* Moderate upsize; overallocate similar to list_resize() */
208 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
209 }
210 else {
211 /* Major upsize; resize up to exact size */
212 alloc = size + 1;
213 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000214 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100215 if (alloc > PY_SSIZE_T_MAX) {
216 PyErr_NoMemory();
217 return -1;
218 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000219
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200220 if (logical_offset > 0) {
221 sval = PyObject_Malloc(alloc);
222 if (sval == NULL) {
223 PyErr_NoMemory();
224 return -1;
225 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100226 memcpy(sval, PyByteArray_AS_STRING(self),
227 Py_MIN(requested_size, Py_SIZE(self)));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200228 PyObject_Free(obj->ob_bytes);
229 }
230 else {
231 sval = PyObject_Realloc(obj->ob_bytes, alloc);
232 if (sval == NULL) {
233 PyErr_NoMemory();
234 return -1;
235 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000236 }
237
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200238 obj->ob_bytes = obj->ob_start = sval;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000239 Py_SIZE(self) = size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200240 obj->ob_alloc = alloc;
241 obj->ob_bytes[size] = '\0'; /* Trailing null byte */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000242
243 return 0;
244}
245
246PyObject *
247PyByteArray_Concat(PyObject *a, PyObject *b)
248{
249 Py_ssize_t size;
250 Py_buffer va, vb;
251 PyByteArrayObject *result = NULL;
252
253 va.len = -1;
254 vb.len = -1;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200255 if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 ||
256 PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000257 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
258 Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
259 goto done;
260 }
261
262 size = va.len + vb.len;
263 if (size < 0) {
Benjamin Petersone0124bd2009-03-09 21:04:33 +0000264 PyErr_NoMemory();
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000265 goto done;
266 }
267
268 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, size);
269 if (result != NULL) {
270 memcpy(result->ob_bytes, va.buf, va.len);
271 memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
272 }
273
274 done:
275 if (va.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000276 PyBuffer_Release(&va);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000277 if (vb.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000278 PyBuffer_Release(&vb);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000279 return (PyObject *)result;
280}
281
282/* Functions stuffed into the type object */
283
284static Py_ssize_t
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000285bytearray_length(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000286{
287 return Py_SIZE(self);
288}
289
290static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000291bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000292{
293 Py_ssize_t mysize;
294 Py_ssize_t size;
295 Py_buffer vo;
296
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200297 if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000298 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
303 mysize = Py_SIZE(self);
304 size = mysize + vo.len;
305 if (size < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000306 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000307 return PyErr_NoMemory();
308 }
Antoine Pitrou25454112015-05-19 20:52:27 +0200309 if (PyByteArray_Resize((PyObject *)self, size) < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000310 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000311 return NULL;
312 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200313 memcpy(PyByteArray_AS_STRING(self) + mysize, vo.buf, vo.len);
Martin v. Löwis423be952008-08-13 15:53:07 +0000314 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000315 Py_INCREF(self);
316 return (PyObject *)self;
317}
318
319static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000320bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000321{
322 PyByteArrayObject *result;
323 Py_ssize_t mysize;
324 Py_ssize_t size;
325
326 if (count < 0)
327 count = 0;
328 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000329 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000330 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000331 size = mysize * count;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000332 result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size);
333 if (result != NULL && size != 0) {
334 if (mysize == 1)
335 memset(result->ob_bytes, self->ob_bytes[0], size);
336 else {
337 Py_ssize_t i;
338 for (i = 0; i < count; i++)
339 memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize);
340 }
341 }
342 return (PyObject *)result;
343}
344
345static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000346bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000347{
348 Py_ssize_t mysize;
349 Py_ssize_t size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200350 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000351
352 if (count < 0)
353 count = 0;
354 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000355 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000356 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000357 size = mysize * count;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200358 if (PyByteArray_Resize((PyObject *)self, size) < 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000359 return NULL;
360
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200361 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000362 if (mysize == 1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200363 memset(buf, buf[0], size);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000364 else {
365 Py_ssize_t i;
366 for (i = 1; i < count; i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200367 memcpy(buf + i*mysize, buf, mysize);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000368 }
369
370 Py_INCREF(self);
371 return (PyObject *)self;
372}
373
374static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000375bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000376{
377 if (i < 0)
378 i += Py_SIZE(self);
379 if (i < 0 || i >= Py_SIZE(self)) {
380 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
381 return NULL;
382 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200383 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000384}
385
386static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000387bytearray_subscript(PyByteArrayObject *self, PyObject *index)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000388{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000389 if (PyIndex_Check(index)) {
390 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000391
392 if (i == -1 && PyErr_Occurred())
393 return NULL;
394
395 if (i < 0)
396 i += PyByteArray_GET_SIZE(self);
397
398 if (i < 0 || i >= Py_SIZE(self)) {
399 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
400 return NULL;
401 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200402 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000403 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000404 else if (PySlice_Check(index)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000405 Py_ssize_t start, stop, step, slicelength, cur, i;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000406 if (PySlice_GetIndicesEx(index,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000407 PyByteArray_GET_SIZE(self),
408 &start, &stop, &step, &slicelength) < 0) {
409 return NULL;
410 }
411
412 if (slicelength <= 0)
413 return PyByteArray_FromStringAndSize("", 0);
414 else if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200415 return PyByteArray_FromStringAndSize(
416 PyByteArray_AS_STRING(self) + start, slicelength);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000417 }
418 else {
419 char *source_buf = PyByteArray_AS_STRING(self);
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000420 char *result_buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000421 PyObject *result;
422
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000423 result = PyByteArray_FromStringAndSize(NULL, slicelength);
424 if (result == NULL)
425 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000426
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000427 result_buf = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000428 for (cur = start, i = 0; i < slicelength;
429 cur += step, i++) {
430 result_buf[i] = source_buf[cur];
431 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000432 return result;
433 }
434 }
435 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400436 PyErr_Format(PyExc_TypeError,
437 "bytearray indices must be integers or slices, not %.200s",
438 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000439 return NULL;
440 }
441}
442
443static int
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200444bytearray_setslice_linear(PyByteArrayObject *self,
445 Py_ssize_t lo, Py_ssize_t hi,
446 char *bytes, Py_ssize_t bytes_len)
447{
448 Py_ssize_t avail = hi - lo;
449 char *buf = PyByteArray_AS_STRING(self);
450 Py_ssize_t growth = bytes_len - avail;
Victor Stinner84557232013-11-21 12:29:51 +0100451 int res = 0;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200452 assert(avail >= 0);
453
Victor Stinner84557232013-11-21 12:29:51 +0100454 if (growth < 0) {
455 if (!_canresize(self))
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200456 return -1;
Victor Stinner84557232013-11-21 12:29:51 +0100457
458 if (lo == 0) {
459 /* Shrink the buffer by advancing its logical start */
460 self->ob_start -= growth;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200461 /*
Victor Stinner84557232013-11-21 12:29:51 +0100462 0 lo hi old_size
463 | |<----avail----->|<-----tail------>|
464 | |<-bytes_len->|<-----tail------>|
465 0 new_lo new_hi new_size
466 */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200467 }
Victor Stinner84557232013-11-21 12:29:51 +0100468 else {
469 /*
470 0 lo hi old_size
471 | |<----avail----->|<-----tomove------>|
472 | |<-bytes_len->|<-----tomove------>|
473 0 lo new_hi new_size
474 */
475 memmove(buf + lo + bytes_len, buf + hi,
476 Py_SIZE(self) - hi);
477 }
478 if (PyByteArray_Resize((PyObject *)self,
479 Py_SIZE(self) + growth) < 0) {
480 /* Issue #19578: Handling the memory allocation failure here is
481 tricky here because the bytearray object has already been
482 modified. Depending on growth and lo, the behaviour is
483 different.
484
485 If growth < 0 and lo != 0, the operation is completed, but a
486 MemoryError is still raised and the memory block is not
487 shrinked. Otherwise, the bytearray is restored in its previous
488 state and a MemoryError is raised. */
489 if (lo == 0) {
490 self->ob_start += growth;
491 return -1;
492 }
493 /* memmove() removed bytes, the bytearray object cannot be
494 restored in its previous state. */
495 Py_SIZE(self) += growth;
496 res = -1;
497 }
498 buf = PyByteArray_AS_STRING(self);
499 }
500 else if (growth > 0) {
501 if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) {
502 PyErr_NoMemory();
503 return -1;
504 }
505
506 if (PyByteArray_Resize((PyObject *)self,
507 Py_SIZE(self) + growth) < 0) {
508 return -1;
509 }
510 buf = PyByteArray_AS_STRING(self);
511 /* Make the place for the additional bytes */
512 /*
513 0 lo hi old_size
514 | |<-avail->|<-----tomove------>|
515 | |<---bytes_len-->|<-----tomove------>|
516 0 lo new_hi new_size
517 */
518 memmove(buf + lo + bytes_len, buf + hi,
519 Py_SIZE(self) - lo - bytes_len);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200520 }
521
522 if (bytes_len > 0)
523 memcpy(buf + lo, bytes, bytes_len);
Victor Stinner84557232013-11-21 12:29:51 +0100524 return res;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200525}
526
527static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000528bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000529 PyObject *values)
530{
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200531 Py_ssize_t needed;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000532 void *bytes;
533 Py_buffer vbytes;
534 int res = 0;
535
536 vbytes.len = -1;
537 if (values == (PyObject *)self) {
538 /* Make a copy and call this function recursively */
539 int err;
540 values = PyByteArray_FromObject(values);
541 if (values == NULL)
542 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000543 err = bytearray_setslice(self, lo, hi, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000544 Py_DECREF(values);
545 return err;
546 }
547 if (values == NULL) {
548 /* del b[lo:hi] */
549 bytes = NULL;
550 needed = 0;
551 }
552 else {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200553 if (PyObject_GetBuffer(values, &vbytes, PyBUF_SIMPLE) != 0) {
554 PyErr_Format(PyExc_TypeError,
555 "can't set bytearray slice from %.100s",
556 Py_TYPE(values)->tp_name);
557 return -1;
558 }
559 needed = vbytes.len;
560 bytes = vbytes.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000561 }
562
563 if (lo < 0)
564 lo = 0;
565 if (hi < lo)
566 hi = lo;
567 if (hi > Py_SIZE(self))
568 hi = Py_SIZE(self);
569
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200570 res = bytearray_setslice_linear(self, lo, hi, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000571 if (vbytes.len != -1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200572 PyBuffer_Release(&vbytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000573 return res;
574}
575
576static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000577bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000578{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000579 int ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000580
581 if (i < 0)
582 i += Py_SIZE(self);
583
584 if (i < 0 || i >= Py_SIZE(self)) {
585 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
586 return -1;
587 }
588
589 if (value == NULL)
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000590 return bytearray_setslice(self, i, i+1, NULL);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000591
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000592 if (!_getbytevalue(value, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000593 return -1;
594
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200595 PyByteArray_AS_STRING(self)[i] = ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000596 return 0;
597}
598
599static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000600bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000601{
602 Py_ssize_t start, stop, step, slicelen, needed;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200603 char *buf, *bytes;
604 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000605
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000606 if (PyIndex_Check(index)) {
607 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000608
609 if (i == -1 && PyErr_Occurred())
610 return -1;
611
612 if (i < 0)
613 i += PyByteArray_GET_SIZE(self);
614
615 if (i < 0 || i >= Py_SIZE(self)) {
616 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
617 return -1;
618 }
619
620 if (values == NULL) {
621 /* Fall through to slice assignment */
622 start = i;
623 stop = i + 1;
624 step = 1;
625 slicelen = 1;
626 }
627 else {
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000628 int ival;
629 if (!_getbytevalue(values, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000630 return -1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200631 buf[i] = (char)ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000632 return 0;
633 }
634 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000635 else if (PySlice_Check(index)) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000636 if (PySlice_GetIndicesEx(index,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000637 PyByteArray_GET_SIZE(self),
638 &start, &stop, &step, &slicelen) < 0) {
639 return -1;
640 }
641 }
642 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400643 PyErr_Format(PyExc_TypeError,
644 "bytearray indices must be integers or slices, not %.200s",
645 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000646 return -1;
647 }
648
649 if (values == NULL) {
650 bytes = NULL;
651 needed = 0;
652 }
653 else if (values == (PyObject *)self || !PyByteArray_Check(values)) {
Christian Heimes6d26ade2012-11-03 23:07:59 +0100654 int err;
Ezio Melottic64bcbe2012-11-03 21:19:06 +0200655 if (PyNumber_Check(values) || PyUnicode_Check(values)) {
656 PyErr_SetString(PyExc_TypeError,
657 "can assign only bytes, buffers, or iterables "
658 "of ints in range(0, 256)");
659 return -1;
660 }
Georg Brandlf3fa5682010-12-04 17:09:30 +0000661 /* Make a copy and call this function recursively */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000662 values = PyByteArray_FromObject(values);
663 if (values == NULL)
664 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000665 err = bytearray_ass_subscript(self, index, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000666 Py_DECREF(values);
667 return err;
668 }
669 else {
670 assert(PyByteArray_Check(values));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200671 bytes = PyByteArray_AS_STRING(values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000672 needed = Py_SIZE(values);
673 }
674 /* Make sure b[5:2] = ... inserts before 5, not before 2. */
675 if ((step < 0 && start < stop) ||
676 (step > 0 && start > stop))
677 stop = start;
678 if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200679 return bytearray_setslice_linear(self, start, stop, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000680 }
681 else {
682 if (needed == 0) {
683 /* Delete slice */
Mark Dickinsonbc099642010-01-29 17:27:24 +0000684 size_t cur;
685 Py_ssize_t i;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000686
Antoine Pitrou5504e892008-12-06 21:27:53 +0000687 if (!_canresize(self))
688 return -1;
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000689
690 if (slicelen == 0)
691 /* Nothing to do here. */
692 return 0;
693
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000694 if (step < 0) {
695 stop = start + 1;
696 start = stop + step * (slicelen - 1) - 1;
697 step = -step;
698 }
699 for (cur = start, i = 0;
700 i < slicelen; cur += step, i++) {
701 Py_ssize_t lim = step - 1;
702
Mark Dickinson66f575b2010-02-14 12:53:32 +0000703 if (cur + step >= (size_t)PyByteArray_GET_SIZE(self))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000704 lim = PyByteArray_GET_SIZE(self) - cur - 1;
705
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200706 memmove(buf + cur - i,
707 buf + cur + 1, lim);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000708 }
709 /* Move the tail of the bytes, in one chunk */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000710 cur = start + (size_t)slicelen*step;
Mark Dickinson66f575b2010-02-14 12:53:32 +0000711 if (cur < (size_t)PyByteArray_GET_SIZE(self)) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200712 memmove(buf + cur - slicelen,
713 buf + cur,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000714 PyByteArray_GET_SIZE(self) - cur);
715 }
716 if (PyByteArray_Resize((PyObject *)self,
717 PyByteArray_GET_SIZE(self) - slicelen) < 0)
718 return -1;
719
720 return 0;
721 }
722 else {
723 /* Assign slice */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000724 Py_ssize_t i;
725 size_t cur;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000726
727 if (needed != slicelen) {
728 PyErr_Format(PyExc_ValueError,
729 "attempt to assign bytes of size %zd "
730 "to extended slice of size %zd",
731 needed, slicelen);
732 return -1;
733 }
734 for (cur = start, i = 0; i < slicelen; cur += step, i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200735 buf[cur] = bytes[i];
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000736 return 0;
737 }
738 }
739}
740
741static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000742bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000743{
744 static char *kwlist[] = {"source", "encoding", "errors", 0};
745 PyObject *arg = NULL;
746 const char *encoding = NULL;
747 const char *errors = NULL;
748 Py_ssize_t count;
749 PyObject *it;
750 PyObject *(*iternext)(PyObject *);
751
752 if (Py_SIZE(self) != 0) {
753 /* Empty previous contents (yes, do this first of all!) */
754 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
755 return -1;
756 }
757
758 /* Parse arguments */
Georg Brandl3dbca812008-07-23 16:10:53 +0000759 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000760 &arg, &encoding, &errors))
761 return -1;
762
763 /* Make a quick exit if no first argument */
764 if (arg == NULL) {
765 if (encoding != NULL || errors != NULL) {
766 PyErr_SetString(PyExc_TypeError,
767 "encoding or errors without sequence argument");
768 return -1;
769 }
770 return 0;
771 }
772
773 if (PyUnicode_Check(arg)) {
774 /* Encode via the codec registry */
775 PyObject *encoded, *new;
776 if (encoding == NULL) {
777 PyErr_SetString(PyExc_TypeError,
778 "string argument without an encoding");
779 return -1;
780 }
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000781 encoded = PyUnicode_AsEncodedString(arg, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000782 if (encoded == NULL)
783 return -1;
784 assert(PyBytes_Check(encoded));
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000785 new = bytearray_iconcat(self, encoded);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000786 Py_DECREF(encoded);
787 if (new == NULL)
788 return -1;
789 Py_DECREF(new);
790 return 0;
791 }
792
793 /* If it's not unicode, there can't be encoding or errors */
794 if (encoding != NULL || errors != NULL) {
795 PyErr_SetString(PyExc_TypeError,
796 "encoding or errors without a string argument");
797 return -1;
798 }
799
800 /* Is it an int? */
Benjamin Peterson8380dd52010-04-16 22:51:37 +0000801 count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
802 if (count == -1 && PyErr_Occurred()) {
803 if (PyErr_ExceptionMatches(PyExc_OverflowError))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000804 return -1;
Benjamin Peterson9c0e94f2010-04-16 23:00:53 +0000805 PyErr_Clear();
Benjamin Peterson8380dd52010-04-16 22:51:37 +0000806 }
807 else if (count < 0) {
808 PyErr_SetString(PyExc_ValueError, "negative count");
809 return -1;
810 }
811 else {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000812 if (count > 0) {
Victor Stinner2bc4d952014-06-02 22:22:42 +0200813 if (PyByteArray_Resize((PyObject *)self, count))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000814 return -1;
Victor Stinner2bc4d952014-06-02 22:22:42 +0200815 memset(PyByteArray_AS_STRING(self), 0, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000816 }
817 return 0;
818 }
819
820 /* Use the buffer API */
821 if (PyObject_CheckBuffer(arg)) {
822 Py_ssize_t size;
823 Py_buffer view;
824 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
825 return -1;
826 size = view.len;
827 if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200828 if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self),
829 &view, size, 'C') < 0)
Stefan Krah7d12d9d2012-07-28 12:25:55 +0200830 goto fail;
Martin v. Löwis423be952008-08-13 15:53:07 +0000831 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000832 return 0;
833 fail:
Martin v. Löwis423be952008-08-13 15:53:07 +0000834 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000835 return -1;
836 }
837
838 /* XXX Optimize this if the arguments is a list, tuple */
839
840 /* Get the iterator */
841 it = PyObject_GetIter(arg);
842 if (it == NULL)
843 return -1;
844 iternext = *Py_TYPE(it)->tp_iternext;
845
846 /* Run the iterator to exhaustion */
847 for (;;) {
848 PyObject *item;
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000849 int rc, value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000850
851 /* Get the next item */
852 item = iternext(it);
853 if (item == NULL) {
854 if (PyErr_Occurred()) {
855 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
856 goto error;
857 PyErr_Clear();
858 }
859 break;
860 }
861
862 /* Interpret it as an int (__index__) */
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000863 rc = _getbytevalue(item, &value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000864 Py_DECREF(item);
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000865 if (!rc)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000866 goto error;
867
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000868 /* Append the byte */
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300869 if (Py_SIZE(self) + 1 < self->ob_alloc) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000870 Py_SIZE(self)++;
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300871 PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0';
872 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000873 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
874 goto error;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200875 PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000876 }
877
878 /* Clean up and return success */
879 Py_DECREF(it);
880 return 0;
881
882 error:
883 /* Error handling when it != NULL */
884 Py_DECREF(it);
885 return -1;
886}
887
888/* Mostly copied from string_repr, but without the
889 "smart quote" functionality. */
890static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000891bytearray_repr(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000892{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000893 const char *quote_prefix = "bytearray(b";
894 const char *quote_postfix = ")";
895 Py_ssize_t length = Py_SIZE(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200896 /* 15 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
Mark Dickinson66f575b2010-02-14 12:53:32 +0000897 size_t newsize;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000898 PyObject *v;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200899 Py_ssize_t i;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200900 char *bytes;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200901 char c;
902 char *p;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200903 int quote;
904 char *test, *start;
905 char *buffer;
906
907 if (length > (PY_SSIZE_T_MAX - 15) / 4) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000908 PyErr_SetString(PyExc_OverflowError,
909 "bytearray object is too large to make repr");
910 return NULL;
911 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200912
913 newsize = 15 + length * 4;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100914 buffer = PyObject_Malloc(newsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200915 if (buffer == NULL) {
916 PyErr_NoMemory();
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000917 return NULL;
918 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000919
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200920 /* Figure out which quote to use; single is preferred */
921 quote = '\'';
922 start = PyByteArray_AS_STRING(self);
923 for (test = start; test < start+length; ++test) {
924 if (*test == '"') {
925 quote = '\''; /* back to single */
926 break;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000927 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200928 else if (*test == '\'')
929 quote = '"';
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000930 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200931
932 p = buffer;
933 while (*quote_prefix)
934 *p++ = *quote_prefix++;
935 *p++ = quote;
936
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200937 bytes = PyByteArray_AS_STRING(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200938 for (i = 0; i < length; i++) {
939 /* There's at least enough room for a hex escape
940 and a closing quote. */
941 assert(newsize - (p - buffer) >= 5);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200942 c = bytes[i];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200943 if (c == '\'' || c == '\\')
944 *p++ = '\\', *p++ = c;
945 else if (c == '\t')
946 *p++ = '\\', *p++ = 't';
947 else if (c == '\n')
948 *p++ = '\\', *p++ = 'n';
949 else if (c == '\r')
950 *p++ = '\\', *p++ = 'r';
951 else if (c == 0)
952 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
953 else if (c < ' ' || c >= 0x7f) {
954 *p++ = '\\';
955 *p++ = 'x';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200956 *p++ = Py_hexdigits[(c & 0xf0) >> 4];
957 *p++ = Py_hexdigits[c & 0xf];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200958 }
959 else
960 *p++ = c;
961 }
962 assert(newsize - (p - buffer) >= 1);
963 *p++ = quote;
964 while (*quote_postfix) {
965 *p++ = *quote_postfix++;
966 }
967
968 v = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100969 PyObject_Free(buffer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200970 return v;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000971}
972
973static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000974bytearray_str(PyObject *op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000975{
Alexander Belopolskyf0f45142010-08-11 17:31:17 +0000976 if (Py_BytesWarningFlag) {
977 if (PyErr_WarnEx(PyExc_BytesWarning,
978 "str() on a bytearray instance", 1))
979 return NULL;
980 }
981 return bytearray_repr((PyByteArrayObject*)op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000982}
983
984static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000985bytearray_richcompare(PyObject *self, PyObject *other, int op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000986{
987 Py_ssize_t self_size, other_size;
988 Py_buffer self_bytes, other_bytes;
989 PyObject *res;
990 Py_ssize_t minsize;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +0300991 int cmp, rc;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000992
993 /* Bytes can be compared to anything that supports the (binary)
994 buffer API. Except that a comparison with Unicode is always an
995 error, even if the comparison is for equality. */
Serhiy Storchakafa494fd2015-05-30 17:45:22 +0300996 rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type);
997 if (!rc)
998 rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type);
999 if (rc < 0)
1000 return NULL;
1001 if (rc) {
Barry Warsaw9e9dcd62008-10-17 01:50:37 +00001002 if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001003 if (PyErr_WarnEx(PyExc_BytesWarning,
Georg Brandle5d68ac2008-06-04 11:30:26 +00001004 "Comparison between bytearray and string", 1))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001005 return NULL;
1006 }
1007
Brian Curtindfc80e32011-08-10 20:28:54 -05001008 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001009 }
1010
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001011 if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001012 PyErr_Clear();
Brian Curtindfc80e32011-08-10 20:28:54 -05001013 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001014 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001015 self_size = self_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001016
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001017 if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001018 PyErr_Clear();
Martin v. Löwis423be952008-08-13 15:53:07 +00001019 PyBuffer_Release(&self_bytes);
Brian Curtindfc80e32011-08-10 20:28:54 -05001020 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001021 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001022 other_size = other_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001023
1024 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
1025 /* Shortcut: if the lengths differ, the objects differ */
1026 cmp = (op == Py_NE);
1027 }
1028 else {
1029 minsize = self_size;
1030 if (other_size < minsize)
1031 minsize = other_size;
1032
1033 cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize);
1034 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
1035
1036 if (cmp == 0) {
1037 if (self_size < other_size)
1038 cmp = -1;
1039 else if (self_size > other_size)
1040 cmp = 1;
1041 }
1042
1043 switch (op) {
1044 case Py_LT: cmp = cmp < 0; break;
1045 case Py_LE: cmp = cmp <= 0; break;
1046 case Py_EQ: cmp = cmp == 0; break;
1047 case Py_NE: cmp = cmp != 0; break;
1048 case Py_GT: cmp = cmp > 0; break;
1049 case Py_GE: cmp = cmp >= 0; break;
1050 }
1051 }
1052
1053 res = cmp ? Py_True : Py_False;
Martin v. Löwis423be952008-08-13 15:53:07 +00001054 PyBuffer_Release(&self_bytes);
1055 PyBuffer_Release(&other_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001056 Py_INCREF(res);
1057 return res;
1058}
1059
1060static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001061bytearray_dealloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001062{
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001063 if (self->ob_exports > 0) {
1064 PyErr_SetString(PyExc_SystemError,
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001065 "deallocated bytearray object has exported buffers");
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001066 PyErr_Print();
1067 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001068 if (self->ob_bytes != 0) {
Antoine Pitrou39aba4f2011-11-12 21:15:28 +01001069 PyObject_Free(self->ob_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001070 }
1071 Py_TYPE(self)->tp_free((PyObject *)self);
1072}
1073
1074
1075/* -------------------------------------------------------------------- */
1076/* Methods */
1077
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001078#define FASTSEARCH fastsearch
1079#define STRINGLIB(F) stringlib_##F
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001080#define STRINGLIB_CHAR char
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001081#define STRINGLIB_SIZEOF_CHAR 1
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001082#define STRINGLIB_LEN PyByteArray_GET_SIZE
1083#define STRINGLIB_STR PyByteArray_AS_STRING
1084#define STRINGLIB_NEW PyByteArray_FromStringAndSize
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001085#define STRINGLIB_ISSPACE Py_ISSPACE
1086#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001087#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1088#define STRINGLIB_MUTABLE 1
1089
1090#include "stringlib/fastsearch.h"
1091#include "stringlib/count.h"
1092#include "stringlib/find.h"
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001093#include "stringlib/join.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001094#include "stringlib/partition.h"
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001095#include "stringlib/split.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001096#include "stringlib/ctype.h"
1097#include "stringlib/transmogrify.h"
1098
1099
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001100static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001101bytearray_find(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001102{
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03001103 return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001104}
1105
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001106static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001107bytearray_count(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001108{
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03001109 return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001110}
1111
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001112/*[clinic input]
1113bytearray.clear
1114
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001115Remove all items from the bytearray.
1116[clinic start generated code]*/
1117
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001118static PyObject *
1119bytearray_clear_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001120/*[clinic end generated code: output=85c2fe6aede0956c input=ed6edae9de447ac4]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001121{
1122 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1123 return NULL;
1124 Py_RETURN_NONE;
1125}
1126
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001127/*[clinic input]
1128bytearray.copy
1129
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001130Return a copy of B.
1131[clinic start generated code]*/
1132
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001133static PyObject *
1134bytearray_copy_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001135/*[clinic end generated code: output=68cfbcfed484c132 input=6597b0c01bccaa9e]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001136{
1137 return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self),
1138 PyByteArray_GET_SIZE(self));
1139}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001140
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001141static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001142bytearray_index(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001143{
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03001144 return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001145}
1146
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001147static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001148bytearray_rfind(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001149{
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03001150 return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001151}
1152
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001153static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001154bytearray_rindex(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001155{
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03001156 return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001157}
1158
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001159static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001160bytearray_contains(PyObject *self, PyObject *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001161{
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03001162 return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001163}
1164
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001165static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001166bytearray_startswith(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001167{
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03001168 return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001169}
1170
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001171static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001172bytearray_endswith(PyByteArrayObject *self, PyObject *args)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001173{
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03001174 return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001175}
1176
1177
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001178/*[clinic input]
1179bytearray.translate
1180
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001181 table: object
1182 Translation table, which must be a bytes object of length 256.
1183 [
1184 deletechars: object
1185 ]
1186 /
1187
1188Return a copy with each character mapped by the given translation table.
1189
1190All characters occurring in the optional argument deletechars are removed.
1191The remaining characters are mapped through the given translation table.
1192[clinic start generated code]*/
1193
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001194static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001195bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
1196 int group_right_1, PyObject *deletechars)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001197/*[clinic end generated code: output=2bebc86a9a1ff083 input=846a01671bccc1c5]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001198{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001199 char *input, *output;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001200 const char *table_chars;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001201 Py_ssize_t i, c;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001202 PyObject *input_obj = (PyObject*)self;
1203 const char *output_start;
1204 Py_ssize_t inlen;
Georg Brandlccc47b62008-12-28 11:44:14 +00001205 PyObject *result = NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001206 int trans_table[256];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001207 Py_buffer vtable, vdel;
1208
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001209 if (table == Py_None) {
1210 table_chars = NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001211 table = NULL;
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001212 } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001213 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001214 } else {
1215 if (vtable.len != 256) {
1216 PyErr_SetString(PyExc_ValueError,
1217 "translation table must be 256 characters long");
Georg Brandl953152f2009-07-22 12:03:59 +00001218 PyBuffer_Release(&vtable);
1219 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001220 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001221 table_chars = (const char*)vtable.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001222 }
1223
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001224 if (deletechars != NULL) {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001225 if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) {
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001226 if (table != NULL)
Georg Brandl953152f2009-07-22 12:03:59 +00001227 PyBuffer_Release(&vtable);
1228 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001229 }
1230 }
1231 else {
1232 vdel.buf = NULL;
1233 vdel.len = 0;
1234 }
1235
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001236 inlen = PyByteArray_GET_SIZE(input_obj);
1237 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1238 if (result == NULL)
1239 goto done;
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03001240 output_start = output = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001241 input = PyByteArray_AS_STRING(input_obj);
1242
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001243 if (vdel.len == 0 && table_chars != NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001244 /* If no deletions are required, use faster code */
1245 for (i = inlen; --i >= 0; ) {
1246 c = Py_CHARMASK(*input++);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001247 *output++ = table_chars[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001248 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001249 goto done;
1250 }
Georg Brandlccc47b62008-12-28 11:44:14 +00001251
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001252 if (table_chars == NULL) {
Georg Brandlccc47b62008-12-28 11:44:14 +00001253 for (i = 0; i < 256; i++)
1254 trans_table[i] = Py_CHARMASK(i);
1255 } else {
1256 for (i = 0; i < 256; i++)
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001257 trans_table[i] = Py_CHARMASK(table_chars[i]);
Georg Brandlccc47b62008-12-28 11:44:14 +00001258 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001259
1260 for (i = 0; i < vdel.len; i++)
1261 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1262
1263 for (i = inlen; --i >= 0; ) {
1264 c = Py_CHARMASK(*input++);
1265 if (trans_table[c] != -1)
1266 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
1267 continue;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001268 }
1269 /* Fix the size of the resulting string */
1270 if (inlen > 0)
Christian Heimesc731bbe2013-07-21 02:04:35 +02001271 if (PyByteArray_Resize(result, output - output_start) < 0) {
1272 Py_CLEAR(result);
1273 goto done;
1274 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001275
1276done:
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001277 if (table != NULL)
Georg Brandlccc47b62008-12-28 11:44:14 +00001278 PyBuffer_Release(&vtable);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001279 if (deletechars != NULL)
Martin v. Löwis423be952008-08-13 15:53:07 +00001280 PyBuffer_Release(&vdel);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001281 return result;
1282}
1283
1284
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001285/*[clinic input]
1286
1287@staticmethod
1288bytearray.maketrans
1289
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001290 frm: Py_buffer
1291 to: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001292 /
1293
1294Return a translation table useable for the bytes or bytearray translate method.
1295
1296The returned table will be one where each byte in frm is mapped to the byte at
1297the same position in to.
1298
1299The bytes objects frm and to must be of the same length.
1300[clinic start generated code]*/
1301
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001302static PyObject *
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001303bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001304/*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001305{
1306 return _Py_bytes_maketrans(frm, to);
Georg Brandlabc38772009-04-12 15:51:51 +00001307}
1308
1309
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001310/*[clinic input]
1311bytearray.replace
1312
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001313 old: Py_buffer
1314 new: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001315 count: Py_ssize_t = -1
1316 Maximum number of occurrences to replace.
1317 -1 (the default value) means replace all occurrences.
1318 /
1319
1320Return a copy with all occurrences of substring old replaced by new.
1321
1322If the optional argument count is given, only the first count occurrences are
1323replaced.
1324[clinic start generated code]*/
1325
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001326static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001327bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
1328 Py_buffer *new, Py_ssize_t count)
1329/*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001330{
Serhiy Storchakafb81d3c2016-05-05 09:26:07 +03001331 return stringlib_replace((PyObject *)self,
1332 (const char *)old->buf, old->len,
1333 (const char *)new->buf, new->len, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001334}
1335
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001336/*[clinic input]
1337bytearray.split
1338
1339 sep: object = None
1340 The delimiter according which to split the bytearray.
1341 None (the default value) means split on ASCII whitespace characters
1342 (space, tab, return, newline, formfeed, vertical tab).
1343 maxsplit: Py_ssize_t = -1
1344 Maximum number of splits to do.
1345 -1 (the default value) means no limit.
1346
1347Return a list of the sections in the bytearray, using sep as the delimiter.
1348[clinic start generated code]*/
1349
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001350static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001351bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
1352 Py_ssize_t maxsplit)
1353/*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001354{
1355 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001356 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001357 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001358 Py_buffer vsub;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001359
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001360 if (maxsplit < 0)
1361 maxsplit = PY_SSIZE_T_MAX;
1362
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001363 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001364 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001365
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001366 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001367 return NULL;
1368 sub = vsub.buf;
1369 n = vsub.len;
1370
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001371 list = stringlib_split(
1372 (PyObject*) self, s, len, sub, n, maxsplit
1373 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001374 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001375 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001376}
1377
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001378/*[clinic input]
1379bytearray.partition
1380
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001381 sep: object
1382 /
1383
1384Partition the bytearray into three parts using the given separator.
1385
1386This will search for the separator sep in the bytearray. If the separator is
1387found, returns a 3-tuple containing the part before the separator, the
1388separator itself, and the part after it.
1389
1390If the separator is not found, returns a 3-tuple containing the original
1391bytearray object and two empty bytearray objects.
1392[clinic start generated code]*/
1393
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001394static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001395bytearray_partition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001396/*[clinic end generated code: output=45d2525ddd35f957 input=86f89223892b70b5]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001397{
1398 PyObject *bytesep, *result;
1399
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001400 bytesep = PyByteArray_FromObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001401 if (! bytesep)
1402 return NULL;
1403
1404 result = stringlib_partition(
1405 (PyObject*) self,
1406 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1407 bytesep,
1408 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1409 );
1410
1411 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001412 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001413}
1414
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001415/*[clinic input]
1416bytearray.rpartition
1417
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001418 sep: object
1419 /
1420
1421Partition the bytes into three parts using the given separator.
1422
1423This will search for the separator sep in the bytearray, starting and the end.
1424If the separator is found, returns a 3-tuple containing the part before the
1425separator, the separator itself, and the part after it.
1426
1427If the separator is not found, returns a 3-tuple containing two empty bytearray
1428objects and the original bytearray object.
1429[clinic start generated code]*/
1430
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001431static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001432bytearray_rpartition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001433/*[clinic end generated code: output=440de3c9426115e8 input=5f4094f2de87c8f3]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001434{
1435 PyObject *bytesep, *result;
1436
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001437 bytesep = PyByteArray_FromObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001438 if (! bytesep)
1439 return NULL;
1440
1441 result = stringlib_rpartition(
1442 (PyObject*) self,
1443 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1444 bytesep,
1445 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1446 );
1447
1448 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001449 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001450}
1451
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001452/*[clinic input]
1453bytearray.rsplit = bytearray.split
1454
1455Return a list of the sections in the bytearray, using sep as the delimiter.
1456
1457Splitting is done starting at the end of the bytearray and working to the front.
1458[clinic start generated code]*/
1459
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001460static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001461bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
1462 Py_ssize_t maxsplit)
1463/*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001464{
1465 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001466 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001467 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001468 Py_buffer vsub;
1469
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001470 if (maxsplit < 0)
1471 maxsplit = PY_SSIZE_T_MAX;
1472
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001473 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001474 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001475
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001476 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001477 return NULL;
1478 sub = vsub.buf;
1479 n = vsub.len;
1480
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001481 list = stringlib_rsplit(
1482 (PyObject*) self, s, len, sub, n, maxsplit
1483 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001484 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001485 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001486}
1487
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001488/*[clinic input]
1489bytearray.reverse
1490
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001491Reverse the order of the values in B in place.
1492[clinic start generated code]*/
1493
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001494static PyObject *
1495bytearray_reverse_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001496/*[clinic end generated code: output=9f7616f29ab309d3 input=543356319fc78557]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001497{
1498 char swap, *head, *tail;
1499 Py_ssize_t i, j, n = Py_SIZE(self);
1500
1501 j = n / 2;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001502 head = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001503 tail = head + n - 1;
1504 for (i = 0; i < j; i++) {
1505 swap = *head;
1506 *head++ = *tail;
1507 *tail-- = swap;
1508 }
1509
1510 Py_RETURN_NONE;
1511}
1512
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001513
1514/*[python input]
1515class bytesvalue_converter(CConverter):
1516 type = 'int'
1517 converter = '_getbytevalue'
1518[python start generated code]*/
1519/*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/
1520
1521
1522/*[clinic input]
1523bytearray.insert
1524
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001525 index: Py_ssize_t
1526 The index where the value is to be inserted.
1527 item: bytesvalue
1528 The item to be inserted.
1529 /
1530
1531Insert a single item into the bytearray before the given index.
1532[clinic start generated code]*/
1533
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001534static PyObject *
1535bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001536/*[clinic end generated code: output=76c775a70e7b07b7 input=b2b5d07e9de6c070]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001537{
1538 Py_ssize_t n = Py_SIZE(self);
1539 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001540
1541 if (n == PY_SSIZE_T_MAX) {
1542 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001543 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001544 return NULL;
1545 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001546 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1547 return NULL;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001548 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001549
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001550 if (index < 0) {
1551 index += n;
1552 if (index < 0)
1553 index = 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001554 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001555 if (index > n)
1556 index = n;
1557 memmove(buf + index + 1, buf + index, n - index);
1558 buf[index] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001559
1560 Py_RETURN_NONE;
1561}
1562
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001563/*[clinic input]
1564bytearray.append
1565
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001566 item: bytesvalue
1567 The item to be appended.
1568 /
1569
1570Append a single item to the end of the bytearray.
1571[clinic start generated code]*/
1572
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001573static PyObject *
1574bytearray_append_impl(PyByteArrayObject *self, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001575/*[clinic end generated code: output=a154e19ed1886cb6 input=20d6bec3d1340593]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001576{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001577 Py_ssize_t n = Py_SIZE(self);
1578
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001579 if (n == PY_SSIZE_T_MAX) {
1580 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001581 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001582 return NULL;
1583 }
1584 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1585 return NULL;
1586
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001587 PyByteArray_AS_STRING(self)[n] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001588
1589 Py_RETURN_NONE;
1590}
1591
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001592/*[clinic input]
1593bytearray.extend
1594
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001595 iterable_of_ints: object
1596 The iterable of items to append.
1597 /
1598
1599Append all the items from the iterator or sequence to the end of the bytearray.
1600[clinic start generated code]*/
1601
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001602static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001603bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001604/*[clinic end generated code: output=98155dbe249170b1 input=c617b3a93249ba28]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001605{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001606 PyObject *it, *item, *bytearray_obj;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001607 Py_ssize_t buf_size = 0, len = 0;
1608 int value;
1609 char *buf;
1610
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001611 /* bytearray_setslice code only accepts something supporting PEP 3118. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001612 if (PyObject_CheckBuffer(iterable_of_ints)) {
1613 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_ints) == -1)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001614 return NULL;
1615
1616 Py_RETURN_NONE;
1617 }
1618
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001619 it = PyObject_GetIter(iterable_of_ints);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001620 if (it == NULL)
1621 return NULL;
1622
Ezio Melotti42da6632011-03-15 05:18:48 +02001623 /* Try to determine the length of the argument. 32 is arbitrary. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001624 buf_size = PyObject_LengthHint(iterable_of_ints, 32);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001625 if (buf_size == -1) {
1626 Py_DECREF(it);
1627 return NULL;
1628 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001629
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001630 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001631 if (bytearray_obj == NULL) {
1632 Py_DECREF(it);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001633 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001634 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001635 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001636
1637 while ((item = PyIter_Next(it)) != NULL) {
1638 if (! _getbytevalue(item, &value)) {
1639 Py_DECREF(item);
1640 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001641 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001642 return NULL;
1643 }
1644 buf[len++] = value;
1645 Py_DECREF(item);
1646
1647 if (len >= buf_size) {
1648 buf_size = len + (len >> 1) + 1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001649 if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001650 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001651 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001652 return NULL;
1653 }
1654 /* Recompute the `buf' pointer, since the resizing operation may
1655 have invalidated it. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001656 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001657 }
1658 }
1659 Py_DECREF(it);
1660
1661 /* Resize down to exact size. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001662 if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) {
1663 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001664 return NULL;
1665 }
1666
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001667 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) {
1668 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001669 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001670 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001671 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001672
1673 Py_RETURN_NONE;
1674}
1675
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001676/*[clinic input]
1677bytearray.pop
1678
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001679 index: Py_ssize_t = -1
1680 The index from where to remove the item.
1681 -1 (the default value) means remove the last item.
1682 /
1683
1684Remove and return a single item from B.
1685
1686If no index argument is given, will pop the last item.
1687[clinic start generated code]*/
1688
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001689static PyObject *
1690bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001691/*[clinic end generated code: output=e0ccd401f8021da8 input=3591df2d06c0d237]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001692{
1693 int value;
1694 Py_ssize_t n = Py_SIZE(self);
1695 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001696
1697 if (n == 0) {
Eli Bendersky1bc4f192011-03-04 04:55:25 +00001698 PyErr_SetString(PyExc_IndexError,
1699 "pop from empty bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001700 return NULL;
1701 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001702 if (index < 0)
1703 index += Py_SIZE(self);
1704 if (index < 0 || index >= Py_SIZE(self)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001705 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1706 return NULL;
1707 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001708 if (!_canresize(self))
1709 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001710
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001711 buf = PyByteArray_AS_STRING(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001712 value = buf[index];
1713 memmove(buf + index, buf + index + 1, n - index);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001714 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1715 return NULL;
1716
Mark Dickinson54a3db92009-09-06 10:19:23 +00001717 return PyLong_FromLong((unsigned char)value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001718}
1719
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001720/*[clinic input]
1721bytearray.remove
1722
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001723 value: bytesvalue
1724 The value to remove.
1725 /
1726
1727Remove the first occurrence of a value in the bytearray.
1728[clinic start generated code]*/
1729
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001730static PyObject *
1731bytearray_remove_impl(PyByteArrayObject *self, int value)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001732/*[clinic end generated code: output=d659e37866709c13 input=121831240cd51ddf]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001733{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001734 Py_ssize_t where, n = Py_SIZE(self);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001735 char *buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001736
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001737 for (where = 0; where < n; where++) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001738 if (buf[where] == value)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001739 break;
1740 }
1741 if (where == n) {
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001742 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001743 return NULL;
1744 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001745 if (!_canresize(self))
1746 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001747
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001748 memmove(buf + where, buf + where + 1, n - where);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001749 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1750 return NULL;
1751
1752 Py_RETURN_NONE;
1753}
1754
1755/* XXX These two helpers could be optimized if argsize == 1 */
1756
1757static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001758lstrip_helper(const char *myptr, Py_ssize_t mysize,
1759 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001760{
1761 Py_ssize_t i = 0;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001762 while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001763 i++;
1764 return i;
1765}
1766
1767static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001768rstrip_helper(const char *myptr, Py_ssize_t mysize,
1769 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001770{
1771 Py_ssize_t i = mysize - 1;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001772 while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001773 i--;
1774 return i + 1;
1775}
1776
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001777/*[clinic input]
1778bytearray.strip
1779
1780 bytes: object = None
1781 /
1782
1783Strip leading and trailing bytes contained in the argument.
1784
1785If the argument is omitted or None, strip leading and trailing ASCII whitespace.
1786[clinic start generated code]*/
1787
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001788static PyObject *
1789bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001790/*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001791{
1792 Py_ssize_t left, right, mysize, byteslen;
1793 char *myptr, *bytesptr;
1794 Py_buffer vbytes;
1795
1796 if (bytes == Py_None) {
1797 bytesptr = "\t\n\r\f\v ";
1798 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001799 }
1800 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001801 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001802 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001803 bytesptr = (char *) vbytes.buf;
1804 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001805 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001806 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001807 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001808 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001809 if (left == mysize)
1810 right = left;
1811 else
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001812 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1813 if (bytes != Py_None)
1814 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001815 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001816}
1817
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001818/*[clinic input]
1819bytearray.lstrip
1820
1821 bytes: object = None
1822 /
1823
1824Strip leading bytes contained in the argument.
1825
1826If the argument is omitted or None, strip leading ASCII whitespace.
1827[clinic start generated code]*/
1828
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001829static PyObject *
1830bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001831/*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001832{
1833 Py_ssize_t left, right, mysize, byteslen;
1834 char *myptr, *bytesptr;
1835 Py_buffer vbytes;
1836
1837 if (bytes == Py_None) {
1838 bytesptr = "\t\n\r\f\v ";
1839 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001840 }
1841 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001842 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001843 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001844 bytesptr = (char *) vbytes.buf;
1845 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001846 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001847 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001848 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001849 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001850 right = mysize;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001851 if (bytes != Py_None)
1852 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001853 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001854}
1855
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001856/*[clinic input]
1857bytearray.rstrip
1858
1859 bytes: object = None
1860 /
1861
1862Strip trailing bytes contained in the argument.
1863
1864If the argument is omitted or None, strip trailing ASCII whitespace.
1865[clinic start generated code]*/
1866
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001867static PyObject *
1868bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001869/*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001870{
1871 Py_ssize_t right, mysize, byteslen;
1872 char *myptr, *bytesptr;
1873 Py_buffer vbytes;
1874
1875 if (bytes == Py_None) {
1876 bytesptr = "\t\n\r\f\v ";
1877 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001878 }
1879 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001880 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001881 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001882 bytesptr = (char *) vbytes.buf;
1883 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001884 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001885 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001886 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001887 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1888 if (bytes != Py_None)
1889 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001890 return PyByteArray_FromStringAndSize(myptr, right);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001891}
1892
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001893/*[clinic input]
1894bytearray.decode
1895
1896 encoding: str(c_default="NULL") = 'utf-8'
1897 The encoding with which to decode the bytearray.
1898 errors: str(c_default="NULL") = 'strict'
1899 The error handling scheme to use for the handling of decoding errors.
1900 The default is 'strict' meaning that decoding errors raise a
1901 UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
1902 as well as any other name registered with codecs.register_error that
1903 can handle UnicodeDecodeErrors.
1904
1905Decode the bytearray using the codec registered for encoding.
1906[clinic start generated code]*/
1907
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001908static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001909bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
1910 const char *errors)
1911/*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001912{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001913 if (encoding == NULL)
1914 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis0efea322014-07-27 17:29:17 +02001915 return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001916}
1917
1918PyDoc_STRVAR(alloc_doc,
1919"B.__alloc__() -> int\n\
1920\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00001921Return the number of bytes actually allocated.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001922
1923static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001924bytearray_alloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001925{
1926 return PyLong_FromSsize_t(self->ob_alloc);
1927}
1928
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001929/*[clinic input]
1930bytearray.join
1931
1932 iterable_of_bytes: object
1933 /
1934
1935Concatenate any number of bytes/bytearray objects.
1936
1937The bytearray whose method is called is inserted in between each pair.
1938
1939The result is returned as a new bytearray object.
1940[clinic start generated code]*/
1941
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001942static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001943bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001944/*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001945{
Martin v. Löwis0efea322014-07-27 17:29:17 +02001946 return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001947}
1948
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001949/*[clinic input]
1950bytearray.splitlines
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001951
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +03001952 keepends: int(c_default="0") = False
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001953
1954Return a list of the lines in the bytearray, breaking at line boundaries.
1955
1956Line breaks are not included in the resulting list unless keepends is given and
1957true.
1958[clinic start generated code]*/
1959
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001960static PyObject *
1961bytearray_splitlines_impl(PyByteArrayObject *self, int keepends)
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +03001962/*[clinic end generated code: output=4223c94b895f6ad9 input=8ccade941e5ea0bd]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001963{
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001964 return stringlib_splitlines(
1965 (PyObject*) self, PyByteArray_AS_STRING(self),
1966 PyByteArray_GET_SIZE(self), keepends
1967 );
1968}
1969
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001970/*[clinic input]
1971@classmethod
1972bytearray.fromhex
1973
1974 cls: self(type="PyObject*")
1975 string: unicode
1976 /
1977
1978Create a bytearray object from a string of hexadecimal numbers.
1979
1980Spaces between two numbers are accepted.
1981Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')
1982[clinic start generated code]*/
1983
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001984static PyObject *
1985bytearray_fromhex_impl(PyObject*cls, PyObject *string)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001986/*[clinic end generated code: output=df3da60129b3700c input=907bbd2d34d9367a]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001987{
Victor Stinner2bf89932015-10-14 11:25:33 +02001988 return _PyBytes_FromHex(string, 1);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001989}
1990
Gregory P. Smith8cb65692015-04-25 23:22:26 +00001991PyDoc_STRVAR(hex__doc__,
1992"B.hex() -> string\n\
1993\n\
1994Create a string of hexadecimal numbers from a bytearray object.\n\
1995Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'.");
1996
1997static PyObject *
1998bytearray_hex(PyBytesObject *self)
1999{
2000 char* argbuf = PyByteArray_AS_STRING(self);
2001 Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
2002 return _Py_strhex(argbuf, arglen);
2003}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002004
2005static PyObject *
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002006_common_reduce(PyByteArrayObject *self, int proto)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002007{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002008 PyObject *dict;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002009 _Py_IDENTIFIER(__dict__);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002010 char *buf;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002011
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002012 dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002013 if (dict == NULL) {
2014 PyErr_Clear();
2015 dict = Py_None;
2016 Py_INCREF(dict);
2017 }
2018
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002019 buf = PyByteArray_AS_STRING(self);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002020 if (proto < 3) {
2021 /* use str based reduction for backwards compatibility with Python 2.x */
2022 PyObject *latin1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002023 if (Py_SIZE(self))
2024 latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002025 else
2026 latin1 = PyUnicode_FromString("");
2027 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
2028 }
2029 else {
2030 /* use more efficient byte based reduction */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002031 if (Py_SIZE(self)) {
2032 return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002033 }
2034 else {
2035 return Py_BuildValue("(O()N)", Py_TYPE(self), dict);
2036 }
2037 }
2038}
2039
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002040/*[clinic input]
2041bytearray.__reduce__ as bytearray_reduce
2042
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002043Return state information for pickling.
2044[clinic start generated code]*/
2045
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002046static PyObject *
2047bytearray_reduce_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002048/*[clinic end generated code: output=52bf304086464cab input=44b5737ada62dd3f]*/
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002049{
2050 return _common_reduce(self, 2);
2051}
2052
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002053/*[clinic input]
2054bytearray.__reduce_ex__ as bytearray_reduce_ex
2055
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002056 proto: int = 0
2057 /
2058
2059Return state information for pickling.
2060[clinic start generated code]*/
2061
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002062static PyObject *
2063bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002064/*[clinic end generated code: output=52eac33377197520 input=f129bc1a1aa151ee]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002065{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002066 return _common_reduce(self, proto);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002067}
2068
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002069/*[clinic input]
2070bytearray.__sizeof__ as bytearray_sizeof
2071
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002072Returns the size of the bytearray object in memory, in bytes.
2073[clinic start generated code]*/
2074
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002075static PyObject *
2076bytearray_sizeof_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002077/*[clinic end generated code: output=738abdd17951c427 input=e27320fd98a4bc5a]*/
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002078{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002079 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002080
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002081 res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002082 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002083}
2084
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002085static PySequenceMethods bytearray_as_sequence = {
2086 (lenfunc)bytearray_length, /* sq_length */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002087 (binaryfunc)PyByteArray_Concat, /* sq_concat */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002088 (ssizeargfunc)bytearray_repeat, /* sq_repeat */
2089 (ssizeargfunc)bytearray_getitem, /* sq_item */
2090 0, /* sq_slice */
2091 (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */
2092 0, /* sq_ass_slice */
2093 (objobjproc)bytearray_contains, /* sq_contains */
2094 (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */
2095 (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002096};
2097
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002098static PyMappingMethods bytearray_as_mapping = {
2099 (lenfunc)bytearray_length,
2100 (binaryfunc)bytearray_subscript,
2101 (objobjargproc)bytearray_ass_subscript,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002102};
2103
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002104static PyBufferProcs bytearray_as_buffer = {
2105 (getbufferproc)bytearray_getbuffer,
2106 (releasebufferproc)bytearray_releasebuffer,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002107};
2108
2109static PyMethodDef
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002110bytearray_methods[] = {
2111 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002112 BYTEARRAY_REDUCE_METHODDEF
2113 BYTEARRAY_REDUCE_EX_METHODDEF
2114 BYTEARRAY_SIZEOF_METHODDEF
2115 BYTEARRAY_APPEND_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002116 {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS,
2117 _Py_capitalize__doc__},
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002118 {"center", (PyCFunction)stringlib_center, METH_VARARGS, _Py_center__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002119 BYTEARRAY_CLEAR_METHODDEF
2120 BYTEARRAY_COPY_METHODDEF
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002121 {"count", (PyCFunction)bytearray_count, METH_VARARGS,
2122 _Py_count__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002123 BYTEARRAY_DECODE_METHODDEF
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002124 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS,
2125 _Py_endswith__doc__},
Ezio Melotti745d54d2013-11-16 19:10:57 +02002126 {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002127 _Py_expandtabs__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002128 BYTEARRAY_EXTEND_METHODDEF
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002129 {"find", (PyCFunction)bytearray_find, METH_VARARGS,
2130 _Py_find__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002131 BYTEARRAY_FROMHEX_METHODDEF
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002132 {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__},
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002133 {"index", (PyCFunction)bytearray_index, METH_VARARGS, _Py_index__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002134 BYTEARRAY_INSERT_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002135 {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS,
2136 _Py_isalnum__doc__},
2137 {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS,
2138 _Py_isalpha__doc__},
2139 {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS,
2140 _Py_isdigit__doc__},
2141 {"islower", (PyCFunction)stringlib_islower, METH_NOARGS,
2142 _Py_islower__doc__},
2143 {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS,
2144 _Py_isspace__doc__},
2145 {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS,
2146 _Py_istitle__doc__},
2147 {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS,
2148 _Py_isupper__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002149 BYTEARRAY_JOIN_METHODDEF
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002150 {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, _Py_ljust__doc__},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002151 {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002152 BYTEARRAY_LSTRIP_METHODDEF
2153 BYTEARRAY_MAKETRANS_METHODDEF
2154 BYTEARRAY_PARTITION_METHODDEF
2155 BYTEARRAY_POP_METHODDEF
2156 BYTEARRAY_REMOVE_METHODDEF
2157 BYTEARRAY_REPLACE_METHODDEF
2158 BYTEARRAY_REVERSE_METHODDEF
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002159 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__},
2160 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__},
2161 {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, _Py_rjust__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002162 BYTEARRAY_RPARTITION_METHODDEF
2163 BYTEARRAY_RSPLIT_METHODDEF
2164 BYTEARRAY_RSTRIP_METHODDEF
2165 BYTEARRAY_SPLIT_METHODDEF
2166 BYTEARRAY_SPLITLINES_METHODDEF
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002167 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002168 _Py_startswith__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002169 BYTEARRAY_STRIP_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002170 {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS,
2171 _Py_swapcase__doc__},
2172 {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002173 BYTEARRAY_TRANSLATE_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002174 {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__},
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002175 {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, _Py_zfill__doc__},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002176 {NULL}
2177};
2178
Ethan Furmanb95b5612015-01-23 20:05:18 -08002179static PyObject *
2180bytearray_mod(PyObject *v, PyObject *w)
2181{
2182 if (!PyByteArray_Check(v))
2183 Py_RETURN_NOTIMPLEMENTED;
Berker Peksag43de36d2016-04-16 01:20:47 +03002184 return _PyBytes_FormatEx(PyByteArray_AS_STRING(v), PyByteArray_GET_SIZE(v), w, 1);
Ethan Furmanb95b5612015-01-23 20:05:18 -08002185}
2186
2187static PyNumberMethods bytearray_as_number = {
2188 0, /*nb_add*/
2189 0, /*nb_subtract*/
2190 0, /*nb_multiply*/
2191 bytearray_mod, /*nb_remainder*/
2192};
2193
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002194PyDoc_STRVAR(bytearray_doc,
Georg Brandl17cb8a82008-05-30 08:20:09 +00002195"bytearray(iterable_of_ints) -> bytearray\n\
2196bytearray(string, encoding[, errors]) -> bytearray\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002197bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\
2198bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\
2199bytearray() -> empty bytes array\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002200\n\
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03002201Construct a mutable bytearray object from:\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002202 - an iterable yielding integers in range(256)\n\
2203 - a text string encoded using the specified encoding\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002204 - a bytes or a buffer object\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002205 - any object implementing the buffer API.\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002206 - an integer");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002207
2208
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002209static PyObject *bytearray_iter(PyObject *seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002210
2211PyTypeObject PyByteArray_Type = {
2212 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2213 "bytearray",
2214 sizeof(PyByteArrayObject),
2215 0,
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002216 (destructor)bytearray_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002217 0, /* tp_print */
2218 0, /* tp_getattr */
2219 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002220 0, /* tp_reserved */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002221 (reprfunc)bytearray_repr, /* tp_repr */
Ethan Furmanb95b5612015-01-23 20:05:18 -08002222 &bytearray_as_number, /* tp_as_number */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002223 &bytearray_as_sequence, /* tp_as_sequence */
2224 &bytearray_as_mapping, /* tp_as_mapping */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002225 0, /* tp_hash */
2226 0, /* tp_call */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002227 bytearray_str, /* tp_str */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002228 PyObject_GenericGetAttr, /* tp_getattro */
2229 0, /* tp_setattro */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002230 &bytearray_as_buffer, /* tp_as_buffer */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002231 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002232 bytearray_doc, /* tp_doc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002233 0, /* tp_traverse */
2234 0, /* tp_clear */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002235 (richcmpfunc)bytearray_richcompare, /* tp_richcompare */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002236 0, /* tp_weaklistoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002237 bytearray_iter, /* tp_iter */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002238 0, /* tp_iternext */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002239 bytearray_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002240 0, /* tp_members */
2241 0, /* tp_getset */
2242 0, /* tp_base */
2243 0, /* tp_dict */
2244 0, /* tp_descr_get */
2245 0, /* tp_descr_set */
2246 0, /* tp_dictoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002247 (initproc)bytearray_init, /* tp_init */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002248 PyType_GenericAlloc, /* tp_alloc */
2249 PyType_GenericNew, /* tp_new */
2250 PyObject_Del, /* tp_free */
2251};
2252
2253/*********************** Bytes Iterator ****************************/
2254
2255typedef struct {
2256 PyObject_HEAD
2257 Py_ssize_t it_index;
2258 PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
2259} bytesiterobject;
2260
2261static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002262bytearrayiter_dealloc(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002263{
2264 _PyObject_GC_UNTRACK(it);
2265 Py_XDECREF(it->it_seq);
2266 PyObject_GC_Del(it);
2267}
2268
2269static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002270bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002271{
2272 Py_VISIT(it->it_seq);
2273 return 0;
2274}
2275
2276static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002277bytearrayiter_next(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002278{
2279 PyByteArrayObject *seq;
2280 PyObject *item;
2281
2282 assert(it != NULL);
2283 seq = it->it_seq;
2284 if (seq == NULL)
2285 return NULL;
2286 assert(PyByteArray_Check(seq));
2287
2288 if (it->it_index < PyByteArray_GET_SIZE(seq)) {
2289 item = PyLong_FromLong(
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002290 (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002291 if (item != NULL)
2292 ++it->it_index;
2293 return item;
2294 }
2295
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002296 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03002297 Py_DECREF(seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002298 return NULL;
2299}
2300
2301static PyObject *
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002302bytearrayiter_length_hint(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002303{
2304 Py_ssize_t len = 0;
2305 if (it->it_seq)
2306 len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
2307 return PyLong_FromSsize_t(len);
2308}
2309
2310PyDoc_STRVAR(length_hint_doc,
2311 "Private method returning an estimate of len(list(it)).");
2312
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002313static PyObject *
2314bytearrayiter_reduce(bytesiterobject *it)
2315{
2316 if (it->it_seq != NULL) {
Antoine Pitroua7013882012-04-05 00:04:20 +02002317 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002318 it->it_seq, it->it_index);
2319 } else {
2320 PyObject *u = PyUnicode_FromUnicode(NULL, 0);
2321 if (u == NULL)
2322 return NULL;
Antoine Pitroua7013882012-04-05 00:04:20 +02002323 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002324 }
2325}
2326
2327static PyObject *
2328bytearrayiter_setstate(bytesiterobject *it, PyObject *state)
2329{
2330 Py_ssize_t index = PyLong_AsSsize_t(state);
2331 if (index == -1 && PyErr_Occurred())
2332 return NULL;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00002333 if (it->it_seq != NULL) {
2334 if (index < 0)
2335 index = 0;
2336 else if (index > PyByteArray_GET_SIZE(it->it_seq))
2337 index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */
2338 it->it_index = index;
2339 }
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002340 Py_RETURN_NONE;
2341}
2342
2343PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
2344
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002345static PyMethodDef bytearrayiter_methods[] = {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002346 {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002347 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002348 {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002349 bytearray_reduce__doc__},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002350 {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
2351 setstate_doc},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002352 {NULL, NULL} /* sentinel */
2353};
2354
2355PyTypeObject PyByteArrayIter_Type = {
2356 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2357 "bytearray_iterator", /* tp_name */
2358 sizeof(bytesiterobject), /* tp_basicsize */
2359 0, /* tp_itemsize */
2360 /* methods */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002361 (destructor)bytearrayiter_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002362 0, /* tp_print */
2363 0, /* tp_getattr */
2364 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002365 0, /* tp_reserved */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002366 0, /* tp_repr */
2367 0, /* tp_as_number */
2368 0, /* tp_as_sequence */
2369 0, /* tp_as_mapping */
2370 0, /* tp_hash */
2371 0, /* tp_call */
2372 0, /* tp_str */
2373 PyObject_GenericGetAttr, /* tp_getattro */
2374 0, /* tp_setattro */
2375 0, /* tp_as_buffer */
2376 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
2377 0, /* tp_doc */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002378 (traverseproc)bytearrayiter_traverse, /* tp_traverse */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002379 0, /* tp_clear */
2380 0, /* tp_richcompare */
2381 0, /* tp_weaklistoffset */
2382 PyObject_SelfIter, /* tp_iter */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002383 (iternextfunc)bytearrayiter_next, /* tp_iternext */
2384 bytearrayiter_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002385 0,
2386};
2387
2388static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002389bytearray_iter(PyObject *seq)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002390{
2391 bytesiterobject *it;
2392
2393 if (!PyByteArray_Check(seq)) {
2394 PyErr_BadInternalCall();
2395 return NULL;
2396 }
2397 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
2398 if (it == NULL)
2399 return NULL;
2400 it->it_index = 0;
2401 Py_INCREF(seq);
2402 it->it_seq = (PyByteArrayObject *)seq;
2403 _PyObject_GC_TRACK(it);
2404 return (PyObject *)it;
2405}