blob: d09b1f22b44d0ba810831a224c3addf2b498a9e1 [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"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06005#include "internal/mem.h"
6#include "internal/pystate.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00007#include "structmember.h"
8#include "bytes_methods.h"
Ethan Furmanb95b5612015-01-23 20:05:18 -08009#include "bytesobject.h"
Gregory P. Smith8cb65692015-04-25 23:22:26 +000010#include "pystrhex.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +000011
Martin v. Löwis7252a6e2014-07-27 16:25:09 +020012/*[clinic input]
13class bytearray "PyByteArrayObject *" "&PyByteArray_Type"
14[clinic start generated code]*/
15/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/
16
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000017char _PyByteArray_empty_string[] = "";
Christian Heimes2c9c7a52008-05-26 13:42:13 +000018
19void
20PyByteArray_Fini(void)
21{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000022}
23
24int
25PyByteArray_Init(void)
26{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000027 return 1;
28}
29
30/* end nullbytes support */
31
32/* Helpers */
33
34static int
35_getbytevalue(PyObject* arg, int *value)
36{
37 long face_value;
38
39 if (PyLong_Check(arg)) {
40 face_value = PyLong_AsLong(arg);
Georg Brandl9a54d7c2008-07-16 23:15:30 +000041 } else {
42 PyObject *index = PyNumber_Index(arg);
43 if (index == NULL) {
44 PyErr_Format(PyExc_TypeError, "an integer is required");
Mark Dickinson10de93a2010-07-09 19:25:48 +000045 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000046 return 0;
47 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +000048 face_value = PyLong_AsLong(index);
49 Py_DECREF(index);
50 }
51
52 if (face_value < 0 || face_value >= 256) {
53 /* this includes the OverflowError in case the long is too large */
54 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
Mark Dickinson10de93a2010-07-09 19:25:48 +000055 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000056 return 0;
57 }
58
59 *value = face_value;
60 return 1;
61}
62
63static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +000064bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000065{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000066 void *ptr;
67 if (view == NULL) {
Stefan Krah5178d912015-02-03 16:57:21 +010068 PyErr_SetString(PyExc_BufferError,
69 "bytearray_getbuffer: view==NULL argument is obsolete");
70 return -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000071 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000072 ptr = (void *) PyByteArray_AS_STRING(obj);
Stefan Krah5178d912015-02-03 16:57:21 +010073 /* cannot fail if view != NULL and readonly == 0 */
74 (void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
75 obj->ob_exports++;
76 return 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000077}
78
79static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +000080bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000081{
82 obj->ob_exports--;
83}
84
Antoine Pitrou5504e892008-12-06 21:27:53 +000085static int
86_canresize(PyByteArrayObject *self)
87{
88 if (self->ob_exports > 0) {
89 PyErr_SetString(PyExc_BufferError,
90 "Existing exports of data: object cannot be re-sized");
91 return 0;
92 }
93 return 1;
94}
95
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030096#include "clinic/bytearrayobject.c.h"
97
Christian Heimes2c9c7a52008-05-26 13:42:13 +000098/* Direct API functions */
99
100PyObject *
101PyByteArray_FromObject(PyObject *input)
102{
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100103 return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type,
104 input, NULL);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000105}
106
107PyObject *
108PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
109{
110 PyByteArrayObject *new;
111 Py_ssize_t alloc;
112
113 if (size < 0) {
114 PyErr_SetString(PyExc_SystemError,
115 "Negative size passed to PyByteArray_FromStringAndSize");
116 return NULL;
117 }
118
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000119 /* Prevent buffer overflow when setting alloc to size+1. */
120 if (size == PY_SSIZE_T_MAX) {
121 return PyErr_NoMemory();
122 }
123
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000124 new = PyObject_New(PyByteArrayObject, &PyByteArray_Type);
125 if (new == NULL)
126 return NULL;
127
128 if (size == 0) {
129 new->ob_bytes = NULL;
130 alloc = 0;
131 }
132 else {
133 alloc = size + 1;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100134 new->ob_bytes = PyObject_Malloc(alloc);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000135 if (new->ob_bytes == NULL) {
136 Py_DECREF(new);
137 return PyErr_NoMemory();
138 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +0000139 if (bytes != NULL && size > 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000140 memcpy(new->ob_bytes, bytes, size);
141 new->ob_bytes[size] = '\0'; /* Trailing null byte */
142 }
143 Py_SIZE(new) = size;
144 new->ob_alloc = alloc;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200145 new->ob_start = new->ob_bytes;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000146 new->ob_exports = 0;
147
148 return (PyObject *)new;
149}
150
151Py_ssize_t
152PyByteArray_Size(PyObject *self)
153{
154 assert(self != NULL);
155 assert(PyByteArray_Check(self));
156
157 return PyByteArray_GET_SIZE(self);
158}
159
160char *
161PyByteArray_AsString(PyObject *self)
162{
163 assert(self != NULL);
164 assert(PyByteArray_Check(self));
165
166 return PyByteArray_AS_STRING(self);
167}
168
169int
Antoine Pitroucc231542014-11-02 18:40:09 +0100170PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000171{
172 void *sval;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200173 PyByteArrayObject *obj = ((PyByteArrayObject *)self);
Antoine Pitroucc231542014-11-02 18:40:09 +0100174 /* All computations are done unsigned to avoid integer overflows
175 (see issue #22335). */
176 size_t alloc = (size_t) obj->ob_alloc;
177 size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes);
178 size_t size = (size_t) requested_size;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000179
180 assert(self != NULL);
181 assert(PyByteArray_Check(self));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200182 assert(logical_offset <= alloc);
Antoine Pitroucc231542014-11-02 18:40:09 +0100183 assert(requested_size >= 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000184
Antoine Pitroucc231542014-11-02 18:40:09 +0100185 if (requested_size == Py_SIZE(self)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000186 return 0;
187 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200188 if (!_canresize(obj)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000189 return -1;
190 }
191
Antoine Pitrou25454112015-05-19 20:52:27 +0200192 if (size + logical_offset + 1 <= alloc) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200193 /* Current buffer is large enough to host the requested size,
194 decide on a strategy. */
195 if (size < alloc / 2) {
196 /* Major downsize; resize down to exact size */
197 alloc = size + 1;
198 }
199 else {
200 /* Minor downsize; quick exit */
201 Py_SIZE(self) = size;
202 PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */
203 return 0;
204 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000205 }
206 else {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200207 /* Need growing, decide on a strategy */
208 if (size <= alloc * 1.125) {
209 /* Moderate upsize; overallocate similar to list_resize() */
210 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
211 }
212 else {
213 /* Major upsize; resize up to exact size */
214 alloc = size + 1;
215 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000216 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100217 if (alloc > PY_SSIZE_T_MAX) {
218 PyErr_NoMemory();
219 return -1;
220 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000221
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200222 if (logical_offset > 0) {
223 sval = PyObject_Malloc(alloc);
224 if (sval == NULL) {
225 PyErr_NoMemory();
226 return -1;
227 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100228 memcpy(sval, PyByteArray_AS_STRING(self),
Stefan Krahdce65022017-08-25 20:12:05 +0200229 Py_MIN((size_t)requested_size, (size_t)Py_SIZE(self)));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200230 PyObject_Free(obj->ob_bytes);
231 }
232 else {
233 sval = PyObject_Realloc(obj->ob_bytes, alloc);
234 if (sval == NULL) {
235 PyErr_NoMemory();
236 return -1;
237 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000238 }
239
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200240 obj->ob_bytes = obj->ob_start = sval;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000241 Py_SIZE(self) = size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200242 obj->ob_alloc = alloc;
243 obj->ob_bytes[size] = '\0'; /* Trailing null byte */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000244
245 return 0;
246}
247
248PyObject *
249PyByteArray_Concat(PyObject *a, PyObject *b)
250{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000251 Py_buffer va, vb;
252 PyByteArrayObject *result = NULL;
253
254 va.len = -1;
255 vb.len = -1;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200256 if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 ||
257 PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000258 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
Serhiy Storchaka6b5a9ec2017-03-19 19:47:02 +0200259 Py_TYPE(b)->tp_name, Py_TYPE(a)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000260 goto done;
261 }
262
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300263 if (va.len > PY_SSIZE_T_MAX - vb.len) {
264 PyErr_NoMemory();
265 goto done;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000266 }
267
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300268 result = (PyByteArrayObject *) \
269 PyByteArray_FromStringAndSize(NULL, va.len + vb.len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000270 if (result != NULL) {
271 memcpy(result->ob_bytes, va.buf, va.len);
272 memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
273 }
274
275 done:
276 if (va.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000277 PyBuffer_Release(&va);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000278 if (vb.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000279 PyBuffer_Release(&vb);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000280 return (PyObject *)result;
281}
282
283/* Functions stuffed into the type object */
284
285static Py_ssize_t
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000286bytearray_length(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000287{
288 return Py_SIZE(self);
289}
290
291static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000292bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000293{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000294 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
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300303 size = Py_SIZE(self);
304 if (size > PY_SSIZE_T_MAX - vo.len) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000305 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000306 return PyErr_NoMemory();
307 }
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300308 if (PyByteArray_Resize((PyObject *)self, size + vo.len) < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000309 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000310 return NULL;
311 }
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300312 memcpy(PyByteArray_AS_STRING(self) + size, vo.buf, vo.len);
Martin v. Löwis423be952008-08-13 15:53:07 +0000313 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000314 Py_INCREF(self);
315 return (PyObject *)self;
316}
317
318static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000319bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000320{
321 PyByteArrayObject *result;
322 Py_ssize_t mysize;
323 Py_ssize_t size;
324
325 if (count < 0)
326 count = 0;
327 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000328 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000329 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000330 size = mysize * count;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000331 result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size);
332 if (result != NULL && size != 0) {
333 if (mysize == 1)
334 memset(result->ob_bytes, self->ob_bytes[0], size);
335 else {
336 Py_ssize_t i;
337 for (i = 0; i < count; i++)
338 memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize);
339 }
340 }
341 return (PyObject *)result;
342}
343
344static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000345bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000346{
347 Py_ssize_t mysize;
348 Py_ssize_t size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200349 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000350
351 if (count < 0)
352 count = 0;
353 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000354 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000355 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000356 size = mysize * count;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200357 if (PyByteArray_Resize((PyObject *)self, size) < 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000358 return NULL;
359
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200360 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000361 if (mysize == 1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200362 memset(buf, buf[0], size);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000363 else {
364 Py_ssize_t i;
365 for (i = 1; i < count; i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200366 memcpy(buf + i*mysize, buf, mysize);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000367 }
368
369 Py_INCREF(self);
370 return (PyObject *)self;
371}
372
373static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000374bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000375{
376 if (i < 0)
377 i += Py_SIZE(self);
378 if (i < 0 || i >= Py_SIZE(self)) {
379 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
380 return NULL;
381 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200382 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000383}
384
385static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000386bytearray_subscript(PyByteArrayObject *self, PyObject *index)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000387{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000388 if (PyIndex_Check(index)) {
389 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000390
391 if (i == -1 && PyErr_Occurred())
392 return NULL;
393
394 if (i < 0)
395 i += PyByteArray_GET_SIZE(self);
396
397 if (i < 0 || i >= Py_SIZE(self)) {
398 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
399 return NULL;
400 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200401 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000402 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000403 else if (PySlice_Check(index)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000404 Py_ssize_t start, stop, step, slicelength, cur, i;
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300405 if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000406 return NULL;
407 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300408 slicelength = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self),
409 &start, &stop, step);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000410
411 if (slicelength <= 0)
412 return PyByteArray_FromStringAndSize("", 0);
413 else if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200414 return PyByteArray_FromStringAndSize(
415 PyByteArray_AS_STRING(self) + start, slicelength);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000416 }
417 else {
418 char *source_buf = PyByteArray_AS_STRING(self);
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000419 char *result_buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000420 PyObject *result;
421
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000422 result = PyByteArray_FromStringAndSize(NULL, slicelength);
423 if (result == NULL)
424 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000425
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000426 result_buf = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000427 for (cur = start, i = 0; i < slicelength;
428 cur += step, i++) {
429 result_buf[i] = source_buf[cur];
430 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000431 return result;
432 }
433 }
434 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400435 PyErr_Format(PyExc_TypeError,
436 "bytearray indices must be integers or slices, not %.200s",
437 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000438 return NULL;
439 }
440}
441
442static int
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200443bytearray_setslice_linear(PyByteArrayObject *self,
444 Py_ssize_t lo, Py_ssize_t hi,
445 char *bytes, Py_ssize_t bytes_len)
446{
447 Py_ssize_t avail = hi - lo;
448 char *buf = PyByteArray_AS_STRING(self);
449 Py_ssize_t growth = bytes_len - avail;
Victor Stinner84557232013-11-21 12:29:51 +0100450 int res = 0;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200451 assert(avail >= 0);
452
Victor Stinner84557232013-11-21 12:29:51 +0100453 if (growth < 0) {
454 if (!_canresize(self))
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200455 return -1;
Victor Stinner84557232013-11-21 12:29:51 +0100456
457 if (lo == 0) {
458 /* Shrink the buffer by advancing its logical start */
459 self->ob_start -= growth;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200460 /*
Victor Stinner84557232013-11-21 12:29:51 +0100461 0 lo hi old_size
462 | |<----avail----->|<-----tail------>|
463 | |<-bytes_len->|<-----tail------>|
464 0 new_lo new_hi new_size
465 */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200466 }
Victor Stinner84557232013-11-21 12:29:51 +0100467 else {
468 /*
469 0 lo hi old_size
470 | |<----avail----->|<-----tomove------>|
471 | |<-bytes_len->|<-----tomove------>|
472 0 lo new_hi new_size
473 */
474 memmove(buf + lo + bytes_len, buf + hi,
475 Py_SIZE(self) - hi);
476 }
477 if (PyByteArray_Resize((PyObject *)self,
478 Py_SIZE(self) + growth) < 0) {
479 /* Issue #19578: Handling the memory allocation failure here is
480 tricky here because the bytearray object has already been
481 modified. Depending on growth and lo, the behaviour is
482 different.
483
484 If growth < 0 and lo != 0, the operation is completed, but a
485 MemoryError is still raised and the memory block is not
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700486 shrunk. Otherwise, the bytearray is restored in its previous
Victor Stinner84557232013-11-21 12:29:51 +0100487 state and a MemoryError is raised. */
488 if (lo == 0) {
489 self->ob_start += growth;
490 return -1;
491 }
492 /* memmove() removed bytes, the bytearray object cannot be
493 restored in its previous state. */
494 Py_SIZE(self) += growth;
495 res = -1;
496 }
497 buf = PyByteArray_AS_STRING(self);
498 }
499 else if (growth > 0) {
500 if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) {
501 PyErr_NoMemory();
502 return -1;
503 }
504
505 if (PyByteArray_Resize((PyObject *)self,
506 Py_SIZE(self) + growth) < 0) {
507 return -1;
508 }
509 buf = PyByteArray_AS_STRING(self);
510 /* Make the place for the additional bytes */
511 /*
512 0 lo hi old_size
513 | |<-avail->|<-----tomove------>|
514 | |<---bytes_len-->|<-----tomove------>|
515 0 lo new_hi new_size
516 */
517 memmove(buf + lo + bytes_len, buf + hi,
518 Py_SIZE(self) - lo - bytes_len);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200519 }
520
521 if (bytes_len > 0)
522 memcpy(buf + lo, bytes, bytes_len);
Victor Stinner84557232013-11-21 12:29:51 +0100523 return res;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200524}
525
526static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000527bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000528 PyObject *values)
529{
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200530 Py_ssize_t needed;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000531 void *bytes;
532 Py_buffer vbytes;
533 int res = 0;
534
535 vbytes.len = -1;
536 if (values == (PyObject *)self) {
537 /* Make a copy and call this function recursively */
538 int err;
539 values = PyByteArray_FromObject(values);
540 if (values == NULL)
541 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000542 err = bytearray_setslice(self, lo, hi, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000543 Py_DECREF(values);
544 return err;
545 }
546 if (values == NULL) {
547 /* del b[lo:hi] */
548 bytes = NULL;
549 needed = 0;
550 }
551 else {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200552 if (PyObject_GetBuffer(values, &vbytes, PyBUF_SIMPLE) != 0) {
553 PyErr_Format(PyExc_TypeError,
554 "can't set bytearray slice from %.100s",
555 Py_TYPE(values)->tp_name);
556 return -1;
557 }
558 needed = vbytes.len;
559 bytes = vbytes.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000560 }
561
562 if (lo < 0)
563 lo = 0;
564 if (hi < lo)
565 hi = lo;
566 if (hi > Py_SIZE(self))
567 hi = Py_SIZE(self);
568
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200569 res = bytearray_setslice_linear(self, lo, hi, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000570 if (vbytes.len != -1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200571 PyBuffer_Release(&vbytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000572 return res;
573}
574
575static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000576bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000577{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000578 int ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000579
580 if (i < 0)
581 i += Py_SIZE(self);
582
583 if (i < 0 || i >= Py_SIZE(self)) {
584 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
585 return -1;
586 }
587
588 if (value == NULL)
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000589 return bytearray_setslice(self, i, i+1, NULL);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000590
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000591 if (!_getbytevalue(value, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000592 return -1;
593
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200594 PyByteArray_AS_STRING(self)[i] = ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000595 return 0;
596}
597
598static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000599bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000600{
601 Py_ssize_t start, stop, step, slicelen, needed;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200602 char *buf, *bytes;
603 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000604
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000605 if (PyIndex_Check(index)) {
606 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000607
608 if (i == -1 && PyErr_Occurred())
609 return -1;
610
611 if (i < 0)
612 i += PyByteArray_GET_SIZE(self);
613
614 if (i < 0 || i >= Py_SIZE(self)) {
615 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
616 return -1;
617 }
618
619 if (values == NULL) {
620 /* Fall through to slice assignment */
621 start = i;
622 stop = i + 1;
623 step = 1;
624 slicelen = 1;
625 }
626 else {
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000627 int ival;
628 if (!_getbytevalue(values, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000629 return -1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200630 buf[i] = (char)ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000631 return 0;
632 }
633 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000634 else if (PySlice_Check(index)) {
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300635 if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000636 return -1;
637 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300638 slicelen = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self), &start,
639 &stop, step);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000640 }
641 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400642 PyErr_Format(PyExc_TypeError,
643 "bytearray indices must be integers or slices, not %.200s",
644 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000645 return -1;
646 }
647
648 if (values == NULL) {
649 bytes = NULL;
650 needed = 0;
651 }
652 else if (values == (PyObject *)self || !PyByteArray_Check(values)) {
Christian Heimes6d26ade2012-11-03 23:07:59 +0100653 int err;
Ezio Melottic64bcbe2012-11-03 21:19:06 +0200654 if (PyNumber_Check(values) || PyUnicode_Check(values)) {
655 PyErr_SetString(PyExc_TypeError,
656 "can assign only bytes, buffers, or iterables "
657 "of ints in range(0, 256)");
658 return -1;
659 }
Georg Brandlf3fa5682010-12-04 17:09:30 +0000660 /* Make a copy and call this function recursively */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000661 values = PyByteArray_FromObject(values);
662 if (values == NULL)
663 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000664 err = bytearray_ass_subscript(self, index, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000665 Py_DECREF(values);
666 return err;
667 }
668 else {
669 assert(PyByteArray_Check(values));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200670 bytes = PyByteArray_AS_STRING(values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000671 needed = Py_SIZE(values);
672 }
673 /* Make sure b[5:2] = ... inserts before 5, not before 2. */
674 if ((step < 0 && start < stop) ||
675 (step > 0 && start > stop))
676 stop = start;
677 if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200678 return bytearray_setslice_linear(self, start, stop, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000679 }
680 else {
681 if (needed == 0) {
682 /* Delete slice */
Mark Dickinsonbc099642010-01-29 17:27:24 +0000683 size_t cur;
684 Py_ssize_t i;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000685
Antoine Pitrou5504e892008-12-06 21:27:53 +0000686 if (!_canresize(self))
687 return -1;
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000688
689 if (slicelen == 0)
690 /* Nothing to do here. */
691 return 0;
692
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000693 if (step < 0) {
694 stop = start + 1;
695 start = stop + step * (slicelen - 1) - 1;
696 step = -step;
697 }
698 for (cur = start, i = 0;
699 i < slicelen; cur += step, i++) {
700 Py_ssize_t lim = step - 1;
701
Mark Dickinson66f575b2010-02-14 12:53:32 +0000702 if (cur + step >= (size_t)PyByteArray_GET_SIZE(self))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000703 lim = PyByteArray_GET_SIZE(self) - cur - 1;
704
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200705 memmove(buf + cur - i,
706 buf + cur + 1, lim);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000707 }
708 /* Move the tail of the bytes, in one chunk */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000709 cur = start + (size_t)slicelen*step;
Mark Dickinson66f575b2010-02-14 12:53:32 +0000710 if (cur < (size_t)PyByteArray_GET_SIZE(self)) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200711 memmove(buf + cur - slicelen,
712 buf + cur,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000713 PyByteArray_GET_SIZE(self) - cur);
714 }
715 if (PyByteArray_Resize((PyObject *)self,
716 PyByteArray_GET_SIZE(self) - slicelen) < 0)
717 return -1;
718
719 return 0;
720 }
721 else {
722 /* Assign slice */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000723 Py_ssize_t i;
724 size_t cur;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000725
726 if (needed != slicelen) {
727 PyErr_Format(PyExc_ValueError,
728 "attempt to assign bytes of size %zd "
729 "to extended slice of size %zd",
730 needed, slicelen);
731 return -1;
732 }
733 for (cur = start, i = 0; i < slicelen; cur += step, i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200734 buf[cur] = bytes[i];
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000735 return 0;
736 }
737 }
738}
739
740static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000741bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000742{
743 static char *kwlist[] = {"source", "encoding", "errors", 0};
744 PyObject *arg = NULL;
745 const char *encoding = NULL;
746 const char *errors = NULL;
747 Py_ssize_t count;
748 PyObject *it;
749 PyObject *(*iternext)(PyObject *);
750
751 if (Py_SIZE(self) != 0) {
752 /* Empty previous contents (yes, do this first of all!) */
753 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
754 return -1;
755 }
756
757 /* Parse arguments */
Georg Brandl3dbca812008-07-23 16:10:53 +0000758 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000759 &arg, &encoding, &errors))
760 return -1;
761
762 /* Make a quick exit if no first argument */
763 if (arg == NULL) {
764 if (encoding != NULL || errors != NULL) {
765 PyErr_SetString(PyExc_TypeError,
766 "encoding or errors without sequence argument");
767 return -1;
768 }
769 return 0;
770 }
771
772 if (PyUnicode_Check(arg)) {
773 /* Encode via the codec registry */
774 PyObject *encoded, *new;
775 if (encoding == NULL) {
776 PyErr_SetString(PyExc_TypeError,
777 "string argument without an encoding");
778 return -1;
779 }
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000780 encoded = PyUnicode_AsEncodedString(arg, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000781 if (encoded == NULL)
782 return -1;
783 assert(PyBytes_Check(encoded));
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000784 new = bytearray_iconcat(self, encoded);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000785 Py_DECREF(encoded);
786 if (new == NULL)
787 return -1;
788 Py_DECREF(new);
789 return 0;
790 }
791
792 /* If it's not unicode, there can't be encoding or errors */
793 if (encoding != NULL || errors != NULL) {
794 PyErr_SetString(PyExc_TypeError,
795 "encoding or errors without a string argument");
796 return -1;
797 }
798
799 /* Is it an int? */
Serhiy Storchakaeb249882016-08-15 09:46:07 +0300800 if (PyIndex_Check(arg)) {
801 count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
802 if (count == -1 && PyErr_Occurred()) {
INADA Naokia634e232017-01-06 17:32:01 +0900803 if (PyErr_ExceptionMatches(PyExc_OverflowError))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000804 return -1;
INADA Naokia634e232017-01-06 17:32:01 +0900805 PyErr_Clear(); /* fall through */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000806 }
INADA Naokia634e232017-01-06 17:32:01 +0900807 else {
808 if (count < 0) {
809 PyErr_SetString(PyExc_ValueError, "negative count");
810 return -1;
811 }
812 if (count > 0) {
813 if (PyByteArray_Resize((PyObject *)self, count))
814 return -1;
815 memset(PyByteArray_AS_STRING(self), 0, count);
816 }
817 return 0;
818 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000819 }
820
821 /* Use the buffer API */
822 if (PyObject_CheckBuffer(arg)) {
823 Py_ssize_t size;
824 Py_buffer view;
825 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
826 return -1;
827 size = view.len;
828 if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200829 if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self),
830 &view, size, 'C') < 0)
Stefan Krah7d12d9d2012-07-28 12:25:55 +0200831 goto fail;
Martin v. Löwis423be952008-08-13 15:53:07 +0000832 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000833 return 0;
834 fail:
Martin v. Löwis423be952008-08-13 15:53:07 +0000835 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000836 return -1;
837 }
838
839 /* XXX Optimize this if the arguments is a list, tuple */
840
841 /* Get the iterator */
842 it = PyObject_GetIter(arg);
843 if (it == NULL)
844 return -1;
845 iternext = *Py_TYPE(it)->tp_iternext;
846
847 /* Run the iterator to exhaustion */
848 for (;;) {
849 PyObject *item;
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000850 int rc, value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000851
852 /* Get the next item */
853 item = iternext(it);
854 if (item == NULL) {
855 if (PyErr_Occurred()) {
856 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
857 goto error;
858 PyErr_Clear();
859 }
860 break;
861 }
862
863 /* Interpret it as an int (__index__) */
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000864 rc = _getbytevalue(item, &value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000865 Py_DECREF(item);
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000866 if (!rc)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000867 goto error;
868
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000869 /* Append the byte */
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300870 if (Py_SIZE(self) + 1 < self->ob_alloc) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000871 Py_SIZE(self)++;
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300872 PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0';
873 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000874 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
875 goto error;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200876 PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000877 }
878
879 /* Clean up and return success */
880 Py_DECREF(it);
881 return 0;
882
883 error:
884 /* Error handling when it != NULL */
885 Py_DECREF(it);
886 return -1;
887}
888
889/* Mostly copied from string_repr, but without the
890 "smart quote" functionality. */
891static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000892bytearray_repr(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000893{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000894 const char *quote_prefix = "bytearray(b";
895 const char *quote_postfix = ")";
896 Py_ssize_t length = Py_SIZE(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200897 /* 15 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
Mark Dickinson66f575b2010-02-14 12:53:32 +0000898 size_t newsize;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000899 PyObject *v;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200900 Py_ssize_t i;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200901 char *bytes;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200902 char c;
903 char *p;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200904 int quote;
905 char *test, *start;
906 char *buffer;
907
908 if (length > (PY_SSIZE_T_MAX - 15) / 4) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000909 PyErr_SetString(PyExc_OverflowError,
910 "bytearray object is too large to make repr");
911 return NULL;
912 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200913
914 newsize = 15 + length * 4;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100915 buffer = PyObject_Malloc(newsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200916 if (buffer == NULL) {
917 PyErr_NoMemory();
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000918 return NULL;
919 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000920
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200921 /* Figure out which quote to use; single is preferred */
922 quote = '\'';
923 start = PyByteArray_AS_STRING(self);
924 for (test = start; test < start+length; ++test) {
925 if (*test == '"') {
926 quote = '\''; /* back to single */
927 break;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000928 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200929 else if (*test == '\'')
930 quote = '"';
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000931 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200932
933 p = buffer;
934 while (*quote_prefix)
935 *p++ = *quote_prefix++;
936 *p++ = quote;
937
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200938 bytes = PyByteArray_AS_STRING(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200939 for (i = 0; i < length; i++) {
940 /* There's at least enough room for a hex escape
941 and a closing quote. */
942 assert(newsize - (p - buffer) >= 5);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200943 c = bytes[i];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200944 if (c == '\'' || c == '\\')
945 *p++ = '\\', *p++ = c;
946 else if (c == '\t')
947 *p++ = '\\', *p++ = 't';
948 else if (c == '\n')
949 *p++ = '\\', *p++ = 'n';
950 else if (c == '\r')
951 *p++ = '\\', *p++ = 'r';
952 else if (c == 0)
953 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
954 else if (c < ' ' || c >= 0x7f) {
955 *p++ = '\\';
956 *p++ = 'x';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200957 *p++ = Py_hexdigits[(c & 0xf0) >> 4];
958 *p++ = Py_hexdigits[c & 0xf];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200959 }
960 else
961 *p++ = c;
962 }
963 assert(newsize - (p - buffer) >= 1);
964 *p++ = quote;
965 while (*quote_postfix) {
966 *p++ = *quote_postfix++;
967 }
968
969 v = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100970 PyObject_Free(buffer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200971 return v;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000972}
973
974static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000975bytearray_str(PyObject *op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000976{
Alexander Belopolskyf0f45142010-08-11 17:31:17 +0000977 if (Py_BytesWarningFlag) {
978 if (PyErr_WarnEx(PyExc_BytesWarning,
979 "str() on a bytearray instance", 1))
980 return NULL;
981 }
982 return bytearray_repr((PyByteArrayObject*)op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000983}
984
985static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000986bytearray_richcompare(PyObject *self, PyObject *other, int op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000987{
988 Py_ssize_t self_size, other_size;
989 Py_buffer self_bytes, other_bytes;
990 PyObject *res;
991 Py_ssize_t minsize;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +0300992 int cmp, rc;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000993
994 /* Bytes can be compared to anything that supports the (binary)
995 buffer API. Except that a comparison with Unicode is always an
996 error, even if the comparison is for equality. */
Serhiy Storchakafa494fd2015-05-30 17:45:22 +0300997 rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type);
998 if (!rc)
999 rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type);
1000 if (rc < 0)
1001 return NULL;
1002 if (rc) {
Barry Warsaw9e9dcd62008-10-17 01:50:37 +00001003 if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001004 if (PyErr_WarnEx(PyExc_BytesWarning,
Georg Brandle5d68ac2008-06-04 11:30:26 +00001005 "Comparison between bytearray and string", 1))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001006 return NULL;
1007 }
1008
Brian Curtindfc80e32011-08-10 20:28:54 -05001009 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001010 }
1011
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001012 if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001013 PyErr_Clear();
Brian Curtindfc80e32011-08-10 20:28:54 -05001014 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001015 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001016 self_size = self_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001017
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001018 if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001019 PyErr_Clear();
Martin v. Löwis423be952008-08-13 15:53:07 +00001020 PyBuffer_Release(&self_bytes);
Brian Curtindfc80e32011-08-10 20:28:54 -05001021 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001022 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001023 other_size = other_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001024
1025 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
1026 /* Shortcut: if the lengths differ, the objects differ */
1027 cmp = (op == Py_NE);
1028 }
1029 else {
1030 minsize = self_size;
1031 if (other_size < minsize)
1032 minsize = other_size;
1033
1034 cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize);
1035 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
1036
1037 if (cmp == 0) {
1038 if (self_size < other_size)
1039 cmp = -1;
1040 else if (self_size > other_size)
1041 cmp = 1;
1042 }
1043
1044 switch (op) {
1045 case Py_LT: cmp = cmp < 0; break;
1046 case Py_LE: cmp = cmp <= 0; break;
1047 case Py_EQ: cmp = cmp == 0; break;
1048 case Py_NE: cmp = cmp != 0; break;
1049 case Py_GT: cmp = cmp > 0; break;
1050 case Py_GE: cmp = cmp >= 0; break;
1051 }
1052 }
1053
1054 res = cmp ? Py_True : Py_False;
Martin v. Löwis423be952008-08-13 15:53:07 +00001055 PyBuffer_Release(&self_bytes);
1056 PyBuffer_Release(&other_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001057 Py_INCREF(res);
1058 return res;
1059}
1060
1061static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001062bytearray_dealloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001063{
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001064 if (self->ob_exports > 0) {
1065 PyErr_SetString(PyExc_SystemError,
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001066 "deallocated bytearray object has exported buffers");
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001067 PyErr_Print();
1068 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001069 if (self->ob_bytes != 0) {
Antoine Pitrou39aba4f2011-11-12 21:15:28 +01001070 PyObject_Free(self->ob_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001071 }
1072 Py_TYPE(self)->tp_free((PyObject *)self);
1073}
1074
1075
1076/* -------------------------------------------------------------------- */
1077/* Methods */
1078
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001079#define FASTSEARCH fastsearch
1080#define STRINGLIB(F) stringlib_##F
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001081#define STRINGLIB_CHAR char
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001082#define STRINGLIB_SIZEOF_CHAR 1
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001083#define STRINGLIB_LEN PyByteArray_GET_SIZE
1084#define STRINGLIB_STR PyByteArray_AS_STRING
1085#define STRINGLIB_NEW PyByteArray_FromStringAndSize
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001086#define STRINGLIB_ISSPACE Py_ISSPACE
1087#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001088#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1089#define STRINGLIB_MUTABLE 1
1090
1091#include "stringlib/fastsearch.h"
1092#include "stringlib/count.h"
1093#include "stringlib/find.h"
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001094#include "stringlib/join.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001095#include "stringlib/partition.h"
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001096#include "stringlib/split.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001097#include "stringlib/ctype.h"
1098#include "stringlib/transmogrify.h"
1099
1100
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001101static PyObject *
1102bytearray_find(PyByteArrayObject *self, PyObject *args)
1103{
1104 return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1105}
1106
1107static PyObject *
1108bytearray_count(PyByteArrayObject *self, PyObject *args)
1109{
1110 return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1111}
1112
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001113/*[clinic input]
1114bytearray.clear
1115
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001116Remove all items from the bytearray.
1117[clinic start generated code]*/
1118
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001119static PyObject *
1120bytearray_clear_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001121/*[clinic end generated code: output=85c2fe6aede0956c input=ed6edae9de447ac4]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001122{
1123 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1124 return NULL;
1125 Py_RETURN_NONE;
1126}
1127
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001128/*[clinic input]
1129bytearray.copy
1130
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001131Return a copy of B.
1132[clinic start generated code]*/
1133
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001134static PyObject *
1135bytearray_copy_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001136/*[clinic end generated code: output=68cfbcfed484c132 input=6597b0c01bccaa9e]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001137{
1138 return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self),
1139 PyByteArray_GET_SIZE(self));
1140}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001141
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001142static PyObject *
1143bytearray_index(PyByteArrayObject *self, PyObject *args)
1144{
1145 return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1146}
1147
1148static PyObject *
1149bytearray_rfind(PyByteArrayObject *self, PyObject *args)
1150{
1151 return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1152}
1153
1154static PyObject *
1155bytearray_rindex(PyByteArrayObject *self, PyObject *args)
1156{
1157 return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1158}
1159
1160static int
1161bytearray_contains(PyObject *self, PyObject *arg)
1162{
1163 return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg);
1164}
1165
1166static PyObject *
1167bytearray_startswith(PyByteArrayObject *self, PyObject *args)
1168{
1169 return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1170}
1171
1172static PyObject *
1173bytearray_endswith(PyByteArrayObject *self, PyObject *args)
1174{
1175 return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1176}
1177
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001178
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001179/*[clinic input]
1180bytearray.translate
1181
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001182 table: object
1183 Translation table, which must be a bytes object of length 256.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001184 /
Martin Panter1b6c6da2016-08-27 08:35:02 +00001185 delete as deletechars: object(c_default="NULL") = b''
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001186
1187Return a copy with each character mapped by the given translation table.
1188
Martin Panter1b6c6da2016-08-27 08:35:02 +00001189All characters occurring in the optional argument delete are removed.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001190The remaining characters are mapped through the given translation table.
1191[clinic start generated code]*/
1192
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001193static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001194bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
Martin Panter1b6c6da2016-08-27 08:35:02 +00001195 PyObject *deletechars)
1196/*[clinic end generated code: output=b6a8f01c2a74e446 input=cfff956d4d127a9b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001197{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001198 char *input, *output;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001199 const char *table_chars;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001200 Py_ssize_t i, c;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001201 PyObject *input_obj = (PyObject*)self;
1202 const char *output_start;
1203 Py_ssize_t inlen;
Georg Brandlccc47b62008-12-28 11:44:14 +00001204 PyObject *result = NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001205 int trans_table[256];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001206 Py_buffer vtable, vdel;
1207
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001208 if (table == Py_None) {
1209 table_chars = NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001210 table = NULL;
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001211 } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001212 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001213 } else {
1214 if (vtable.len != 256) {
1215 PyErr_SetString(PyExc_ValueError,
1216 "translation table must be 256 characters long");
Georg Brandl953152f2009-07-22 12:03:59 +00001217 PyBuffer_Release(&vtable);
1218 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001219 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001220 table_chars = (const char*)vtable.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001221 }
1222
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001223 if (deletechars != NULL) {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001224 if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) {
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001225 if (table != NULL)
Georg Brandl953152f2009-07-22 12:03:59 +00001226 PyBuffer_Release(&vtable);
1227 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001228 }
1229 }
1230 else {
1231 vdel.buf = NULL;
1232 vdel.len = 0;
1233 }
1234
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001235 inlen = PyByteArray_GET_SIZE(input_obj);
1236 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1237 if (result == NULL)
1238 goto done;
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03001239 output_start = output = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001240 input = PyByteArray_AS_STRING(input_obj);
1241
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001242 if (vdel.len == 0 && table_chars != NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001243 /* If no deletions are required, use faster code */
1244 for (i = inlen; --i >= 0; ) {
1245 c = Py_CHARMASK(*input++);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001246 *output++ = table_chars[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001247 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001248 goto done;
1249 }
Georg Brandlccc47b62008-12-28 11:44:14 +00001250
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001251 if (table_chars == NULL) {
Georg Brandlccc47b62008-12-28 11:44:14 +00001252 for (i = 0; i < 256; i++)
1253 trans_table[i] = Py_CHARMASK(i);
1254 } else {
1255 for (i = 0; i < 256; i++)
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001256 trans_table[i] = Py_CHARMASK(table_chars[i]);
Georg Brandlccc47b62008-12-28 11:44:14 +00001257 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001258
1259 for (i = 0; i < vdel.len; i++)
1260 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1261
1262 for (i = inlen; --i >= 0; ) {
1263 c = Py_CHARMASK(*input++);
1264 if (trans_table[c] != -1)
Martin Panter1b6c6da2016-08-27 08:35:02 +00001265 *output++ = (char)trans_table[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001266 }
1267 /* Fix the size of the resulting string */
1268 if (inlen > 0)
Christian Heimesc731bbe2013-07-21 02:04:35 +02001269 if (PyByteArray_Resize(result, output - output_start) < 0) {
1270 Py_CLEAR(result);
1271 goto done;
1272 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001273
1274done:
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001275 if (table != NULL)
Georg Brandlccc47b62008-12-28 11:44:14 +00001276 PyBuffer_Release(&vtable);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001277 if (deletechars != NULL)
Martin v. Löwis423be952008-08-13 15:53:07 +00001278 PyBuffer_Release(&vdel);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001279 return result;
1280}
1281
1282
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001283/*[clinic input]
1284
1285@staticmethod
1286bytearray.maketrans
1287
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001288 frm: Py_buffer
1289 to: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001290 /
1291
1292Return a translation table useable for the bytes or bytearray translate method.
1293
1294The returned table will be one where each byte in frm is mapped to the byte at
1295the same position in to.
1296
1297The bytes objects frm and to must be of the same length.
1298[clinic start generated code]*/
1299
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001300static PyObject *
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001301bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001302/*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001303{
1304 return _Py_bytes_maketrans(frm, to);
Georg Brandlabc38772009-04-12 15:51:51 +00001305}
1306
1307
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001308/*[clinic input]
1309bytearray.replace
1310
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001311 old: Py_buffer
1312 new: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001313 count: Py_ssize_t = -1
1314 Maximum number of occurrences to replace.
1315 -1 (the default value) means replace all occurrences.
1316 /
1317
1318Return a copy with all occurrences of substring old replaced by new.
1319
1320If the optional argument count is given, only the first count occurrences are
1321replaced.
1322[clinic start generated code]*/
1323
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001324static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001325bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
1326 Py_buffer *new, Py_ssize_t count)
1327/*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001328{
Serhiy Storchakafb81d3c2016-05-05 09:26:07 +03001329 return stringlib_replace((PyObject *)self,
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001330 (const char *)old->buf, old->len,
1331 (const char *)new->buf, new->len, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001332}
1333
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001334/*[clinic input]
1335bytearray.split
1336
1337 sep: object = None
1338 The delimiter according which to split the bytearray.
1339 None (the default value) means split on ASCII whitespace characters
1340 (space, tab, return, newline, formfeed, vertical tab).
1341 maxsplit: Py_ssize_t = -1
1342 Maximum number of splits to do.
1343 -1 (the default value) means no limit.
1344
1345Return a list of the sections in the bytearray, using sep as the delimiter.
1346[clinic start generated code]*/
1347
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001348static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001349bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
1350 Py_ssize_t maxsplit)
1351/*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001352{
1353 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001354 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001355 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001356 Py_buffer vsub;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001357
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001358 if (maxsplit < 0)
1359 maxsplit = PY_SSIZE_T_MAX;
1360
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001361 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001362 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001363
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001364 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001365 return NULL;
1366 sub = vsub.buf;
1367 n = vsub.len;
1368
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001369 list = stringlib_split(
1370 (PyObject*) self, s, len, sub, n, maxsplit
1371 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001372 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001373 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001374}
1375
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001376/*[clinic input]
1377bytearray.partition
1378
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001379 sep: object
1380 /
1381
1382Partition the bytearray into three parts using the given separator.
1383
1384This will search for the separator sep in the bytearray. If the separator is
1385found, returns a 3-tuple containing the part before the separator, the
1386separator itself, and the part after it.
1387
1388If the separator is not found, returns a 3-tuple containing the original
1389bytearray object and two empty bytearray objects.
1390[clinic start generated code]*/
1391
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001392static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001393bytearray_partition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001394/*[clinic end generated code: output=45d2525ddd35f957 input=86f89223892b70b5]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001395{
1396 PyObject *bytesep, *result;
1397
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001398 bytesep = PyByteArray_FromObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001399 if (! bytesep)
1400 return NULL;
1401
1402 result = stringlib_partition(
1403 (PyObject*) self,
1404 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1405 bytesep,
1406 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1407 );
1408
1409 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001410 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001411}
1412
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001413/*[clinic input]
1414bytearray.rpartition
1415
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001416 sep: object
1417 /
1418
1419Partition the bytes into three parts using the given separator.
1420
1421This will search for the separator sep in the bytearray, starting and the end.
1422If the separator is found, returns a 3-tuple containing the part before the
1423separator, the separator itself, and the part after it.
1424
1425If the separator is not found, returns a 3-tuple containing two empty bytearray
1426objects and the original bytearray object.
1427[clinic start generated code]*/
1428
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001429static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001430bytearray_rpartition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001431/*[clinic end generated code: output=440de3c9426115e8 input=5f4094f2de87c8f3]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001432{
1433 PyObject *bytesep, *result;
1434
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001435 bytesep = PyByteArray_FromObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001436 if (! bytesep)
1437 return NULL;
1438
1439 result = stringlib_rpartition(
1440 (PyObject*) self,
1441 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1442 bytesep,
1443 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1444 );
1445
1446 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001447 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001448}
1449
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001450/*[clinic input]
1451bytearray.rsplit = bytearray.split
1452
1453Return a list of the sections in the bytearray, using sep as the delimiter.
1454
1455Splitting is done starting at the end of the bytearray and working to the front.
1456[clinic start generated code]*/
1457
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001458static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001459bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
1460 Py_ssize_t maxsplit)
1461/*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001462{
1463 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001464 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001465 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001466 Py_buffer vsub;
1467
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001468 if (maxsplit < 0)
1469 maxsplit = PY_SSIZE_T_MAX;
1470
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001471 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001472 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001473
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001474 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001475 return NULL;
1476 sub = vsub.buf;
1477 n = vsub.len;
1478
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001479 list = stringlib_rsplit(
1480 (PyObject*) self, s, len, sub, n, maxsplit
1481 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001482 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001483 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001484}
1485
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001486/*[clinic input]
1487bytearray.reverse
1488
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001489Reverse the order of the values in B in place.
1490[clinic start generated code]*/
1491
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001492static PyObject *
1493bytearray_reverse_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001494/*[clinic end generated code: output=9f7616f29ab309d3 input=543356319fc78557]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001495{
1496 char swap, *head, *tail;
1497 Py_ssize_t i, j, n = Py_SIZE(self);
1498
1499 j = n / 2;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001500 head = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001501 tail = head + n - 1;
1502 for (i = 0; i < j; i++) {
1503 swap = *head;
1504 *head++ = *tail;
1505 *tail-- = swap;
1506 }
1507
1508 Py_RETURN_NONE;
1509}
1510
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001511
1512/*[python input]
1513class bytesvalue_converter(CConverter):
1514 type = 'int'
1515 converter = '_getbytevalue'
1516[python start generated code]*/
1517/*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/
1518
1519
1520/*[clinic input]
1521bytearray.insert
1522
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001523 index: Py_ssize_t
1524 The index where the value is to be inserted.
1525 item: bytesvalue
1526 The item to be inserted.
1527 /
1528
1529Insert a single item into the bytearray before the given index.
1530[clinic start generated code]*/
1531
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001532static PyObject *
1533bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001534/*[clinic end generated code: output=76c775a70e7b07b7 input=b2b5d07e9de6c070]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001535{
1536 Py_ssize_t n = Py_SIZE(self);
1537 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001538
1539 if (n == PY_SSIZE_T_MAX) {
1540 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001541 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001542 return NULL;
1543 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001544 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1545 return NULL;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001546 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001547
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001548 if (index < 0) {
1549 index += n;
1550 if (index < 0)
1551 index = 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001552 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001553 if (index > n)
1554 index = n;
1555 memmove(buf + index + 1, buf + index, n - index);
1556 buf[index] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001557
1558 Py_RETURN_NONE;
1559}
1560
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001561/*[clinic input]
1562bytearray.append
1563
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001564 item: bytesvalue
1565 The item to be appended.
1566 /
1567
1568Append a single item to the end of the bytearray.
1569[clinic start generated code]*/
1570
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001571static PyObject *
1572bytearray_append_impl(PyByteArrayObject *self, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001573/*[clinic end generated code: output=a154e19ed1886cb6 input=20d6bec3d1340593]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001574{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001575 Py_ssize_t n = Py_SIZE(self);
1576
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001577 if (n == PY_SSIZE_T_MAX) {
1578 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001579 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001580 return NULL;
1581 }
1582 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1583 return NULL;
1584
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001585 PyByteArray_AS_STRING(self)[n] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001586
1587 Py_RETURN_NONE;
1588}
1589
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001590/*[clinic input]
1591bytearray.extend
1592
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001593 iterable_of_ints: object
1594 The iterable of items to append.
1595 /
1596
1597Append all the items from the iterator or sequence to the end of the bytearray.
1598[clinic start generated code]*/
1599
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001600static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001601bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001602/*[clinic end generated code: output=98155dbe249170b1 input=c617b3a93249ba28]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001603{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001604 PyObject *it, *item, *bytearray_obj;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001605 Py_ssize_t buf_size = 0, len = 0;
1606 int value;
1607 char *buf;
1608
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001609 /* bytearray_setslice code only accepts something supporting PEP 3118. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001610 if (PyObject_CheckBuffer(iterable_of_ints)) {
1611 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_ints) == -1)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001612 return NULL;
1613
1614 Py_RETURN_NONE;
1615 }
1616
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001617 it = PyObject_GetIter(iterable_of_ints);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001618 if (it == NULL)
1619 return NULL;
1620
Ezio Melotti42da6632011-03-15 05:18:48 +02001621 /* Try to determine the length of the argument. 32 is arbitrary. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001622 buf_size = PyObject_LengthHint(iterable_of_ints, 32);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001623 if (buf_size == -1) {
1624 Py_DECREF(it);
1625 return NULL;
1626 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001627
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001628 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001629 if (bytearray_obj == NULL) {
1630 Py_DECREF(it);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001631 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001632 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001633 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001634
1635 while ((item = PyIter_Next(it)) != NULL) {
1636 if (! _getbytevalue(item, &value)) {
1637 Py_DECREF(item);
1638 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001639 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001640 return NULL;
1641 }
1642 buf[len++] = value;
1643 Py_DECREF(item);
1644
1645 if (len >= buf_size) {
Martin Panter371731e2016-07-18 07:53:13 +00001646 Py_ssize_t addition;
1647 if (len == PY_SSIZE_T_MAX) {
1648 Py_DECREF(it);
1649 Py_DECREF(bytearray_obj);
1650 return PyErr_NoMemory();
1651 }
1652 addition = len >> 1;
1653 if (addition > PY_SSIZE_T_MAX - len - 1)
1654 buf_size = PY_SSIZE_T_MAX;
1655 else
1656 buf_size = len + addition + 1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001657 if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001658 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001659 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001660 return NULL;
1661 }
1662 /* Recompute the `buf' pointer, since the resizing operation may
1663 have invalidated it. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001664 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001665 }
1666 }
1667 Py_DECREF(it);
1668
1669 /* Resize down to exact size. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001670 if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) {
1671 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001672 return NULL;
1673 }
1674
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001675 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) {
1676 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001677 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001678 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001679 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001680
1681 Py_RETURN_NONE;
1682}
1683
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001684/*[clinic input]
1685bytearray.pop
1686
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001687 index: Py_ssize_t = -1
1688 The index from where to remove the item.
1689 -1 (the default value) means remove the last item.
1690 /
1691
1692Remove and return a single item from B.
1693
1694If no index argument is given, will pop the last item.
1695[clinic start generated code]*/
1696
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001697static PyObject *
1698bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001699/*[clinic end generated code: output=e0ccd401f8021da8 input=3591df2d06c0d237]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001700{
1701 int value;
1702 Py_ssize_t n = Py_SIZE(self);
1703 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001704
1705 if (n == 0) {
Eli Bendersky1bc4f192011-03-04 04:55:25 +00001706 PyErr_SetString(PyExc_IndexError,
1707 "pop from empty bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001708 return NULL;
1709 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001710 if (index < 0)
1711 index += Py_SIZE(self);
1712 if (index < 0 || index >= Py_SIZE(self)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001713 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1714 return NULL;
1715 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001716 if (!_canresize(self))
1717 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001718
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001719 buf = PyByteArray_AS_STRING(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001720 value = buf[index];
1721 memmove(buf + index, buf + index + 1, n - index);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001722 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1723 return NULL;
1724
Mark Dickinson54a3db92009-09-06 10:19:23 +00001725 return PyLong_FromLong((unsigned char)value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001726}
1727
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001728/*[clinic input]
1729bytearray.remove
1730
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001731 value: bytesvalue
1732 The value to remove.
1733 /
1734
1735Remove the first occurrence of a value in the bytearray.
1736[clinic start generated code]*/
1737
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001738static PyObject *
1739bytearray_remove_impl(PyByteArrayObject *self, int value)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001740/*[clinic end generated code: output=d659e37866709c13 input=121831240cd51ddf]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001741{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001742 Py_ssize_t where, n = Py_SIZE(self);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001743 char *buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001744
Serhiy Storchaka4b234942016-05-16 22:24:03 +03001745 where = stringlib_find_char(buf, n, value);
1746 if (where < 0) {
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001747 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001748 return NULL;
1749 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001750 if (!_canresize(self))
1751 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001752
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001753 memmove(buf + where, buf + where + 1, n - where);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001754 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1755 return NULL;
1756
1757 Py_RETURN_NONE;
1758}
1759
1760/* XXX These two helpers could be optimized if argsize == 1 */
1761
1762static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001763lstrip_helper(const char *myptr, Py_ssize_t mysize,
1764 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001765{
1766 Py_ssize_t i = 0;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001767 while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001768 i++;
1769 return i;
1770}
1771
1772static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001773rstrip_helper(const char *myptr, Py_ssize_t mysize,
1774 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001775{
1776 Py_ssize_t i = mysize - 1;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001777 while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001778 i--;
1779 return i + 1;
1780}
1781
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001782/*[clinic input]
1783bytearray.strip
1784
1785 bytes: object = None
1786 /
1787
1788Strip leading and trailing bytes contained in the argument.
1789
1790If the argument is omitted or None, strip leading and trailing ASCII whitespace.
1791[clinic start generated code]*/
1792
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001793static PyObject *
1794bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001795/*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001796{
1797 Py_ssize_t left, right, mysize, byteslen;
1798 char *myptr, *bytesptr;
1799 Py_buffer vbytes;
1800
1801 if (bytes == Py_None) {
1802 bytesptr = "\t\n\r\f\v ";
1803 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001804 }
1805 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001806 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001807 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001808 bytesptr = (char *) vbytes.buf;
1809 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001810 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001811 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001812 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001813 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001814 if (left == mysize)
1815 right = left;
1816 else
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001817 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1818 if (bytes != Py_None)
1819 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001820 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001821}
1822
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001823/*[clinic input]
1824bytearray.lstrip
1825
1826 bytes: object = None
1827 /
1828
1829Strip leading bytes contained in the argument.
1830
1831If the argument is omitted or None, strip leading ASCII whitespace.
1832[clinic start generated code]*/
1833
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001834static PyObject *
1835bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001836/*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001837{
1838 Py_ssize_t left, right, mysize, byteslen;
1839 char *myptr, *bytesptr;
1840 Py_buffer vbytes;
1841
1842 if (bytes == Py_None) {
1843 bytesptr = "\t\n\r\f\v ";
1844 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001845 }
1846 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001847 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001848 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001849 bytesptr = (char *) vbytes.buf;
1850 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001851 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001852 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001853 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001854 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001855 right = mysize;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001856 if (bytes != Py_None)
1857 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001858 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001859}
1860
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001861/*[clinic input]
1862bytearray.rstrip
1863
1864 bytes: object = None
1865 /
1866
1867Strip trailing bytes contained in the argument.
1868
1869If the argument is omitted or None, strip trailing ASCII whitespace.
1870[clinic start generated code]*/
1871
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001872static PyObject *
1873bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001874/*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001875{
1876 Py_ssize_t right, mysize, byteslen;
1877 char *myptr, *bytesptr;
1878 Py_buffer vbytes;
1879
1880 if (bytes == Py_None) {
1881 bytesptr = "\t\n\r\f\v ";
1882 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001883 }
1884 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001885 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001886 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001887 bytesptr = (char *) vbytes.buf;
1888 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001889 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001890 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001891 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001892 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1893 if (bytes != Py_None)
1894 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001895 return PyByteArray_FromStringAndSize(myptr, right);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001896}
1897
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001898/*[clinic input]
1899bytearray.decode
1900
1901 encoding: str(c_default="NULL") = 'utf-8'
1902 The encoding with which to decode the bytearray.
1903 errors: str(c_default="NULL") = 'strict'
1904 The error handling scheme to use for the handling of decoding errors.
1905 The default is 'strict' meaning that decoding errors raise a
1906 UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
1907 as well as any other name registered with codecs.register_error that
1908 can handle UnicodeDecodeErrors.
1909
1910Decode the bytearray using the codec registered for encoding.
1911[clinic start generated code]*/
1912
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001913static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001914bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
1915 const char *errors)
1916/*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001917{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001918 if (encoding == NULL)
1919 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis0efea322014-07-27 17:29:17 +02001920 return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001921}
1922
1923PyDoc_STRVAR(alloc_doc,
1924"B.__alloc__() -> int\n\
1925\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00001926Return the number of bytes actually allocated.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001927
1928static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001929bytearray_alloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001930{
1931 return PyLong_FromSsize_t(self->ob_alloc);
1932}
1933
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001934/*[clinic input]
1935bytearray.join
1936
1937 iterable_of_bytes: object
1938 /
1939
1940Concatenate any number of bytes/bytearray objects.
1941
1942The bytearray whose method is called is inserted in between each pair.
1943
1944The result is returned as a new bytearray object.
1945[clinic start generated code]*/
1946
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001947static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001948bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001949/*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001950{
Martin v. Löwis0efea322014-07-27 17:29:17 +02001951 return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001952}
1953
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001954/*[clinic input]
1955bytearray.splitlines
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001956
Serhiy Storchaka202fda52017-03-12 10:10:47 +02001957 keepends: bool(accept={int}) = False
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001958
1959Return a list of the lines in the bytearray, breaking at line boundaries.
1960
1961Line breaks are not included in the resulting list unless keepends is given and
1962true.
1963[clinic start generated code]*/
1964
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001965static PyObject *
1966bytearray_splitlines_impl(PyByteArrayObject *self, int keepends)
Serhiy Storchaka202fda52017-03-12 10:10:47 +02001967/*[clinic end generated code: output=4223c94b895f6ad9 input=99a27ad959b9cf6b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001968{
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001969 return stringlib_splitlines(
1970 (PyObject*) self, PyByteArray_AS_STRING(self),
1971 PyByteArray_GET_SIZE(self), keepends
1972 );
1973}
1974
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001975/*[clinic input]
1976@classmethod
1977bytearray.fromhex
1978
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001979 string: unicode
1980 /
1981
1982Create a bytearray object from a string of hexadecimal numbers.
1983
1984Spaces between two numbers are accepted.
1985Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')
1986[clinic start generated code]*/
1987
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001988static PyObject *
Serhiy Storchaka0855e702016-07-01 17:22:31 +03001989bytearray_fromhex_impl(PyTypeObject *type, PyObject *string)
1990/*[clinic end generated code: output=8f0f0b6d30fb3ba0 input=f033a16d1fb21f48]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001991{
Serhiy Storchaka0855e702016-07-01 17:22:31 +03001992 PyObject *result = _PyBytes_FromHex(string, type == &PyByteArray_Type);
1993 if (type != &PyByteArray_Type && result != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001994 Py_SETREF(result, PyObject_CallFunctionObjArgs((PyObject *)type,
1995 result, NULL));
Serhiy Storchaka0855e702016-07-01 17:22:31 +03001996 }
1997 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001998}
1999
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002000PyDoc_STRVAR(hex__doc__,
2001"B.hex() -> string\n\
2002\n\
2003Create a string of hexadecimal numbers from a bytearray object.\n\
2004Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'.");
2005
2006static PyObject *
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002007bytearray_hex(PyBytesObject *self)
2008{
2009 char* argbuf = PyByteArray_AS_STRING(self);
2010 Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
2011 return _Py_strhex(argbuf, arglen);
2012}
2013
2014static PyObject *
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002015_common_reduce(PyByteArrayObject *self, int proto)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002016{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002017 PyObject *dict;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002018 _Py_IDENTIFIER(__dict__);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002019 char *buf;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002020
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002021 dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002022 if (dict == NULL) {
2023 PyErr_Clear();
2024 dict = Py_None;
2025 Py_INCREF(dict);
2026 }
2027
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002028 buf = PyByteArray_AS_STRING(self);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002029 if (proto < 3) {
2030 /* use str based reduction for backwards compatibility with Python 2.x */
2031 PyObject *latin1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002032 if (Py_SIZE(self))
2033 latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002034 else
2035 latin1 = PyUnicode_FromString("");
2036 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
2037 }
2038 else {
2039 /* use more efficient byte based reduction */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002040 if (Py_SIZE(self)) {
2041 return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002042 }
2043 else {
2044 return Py_BuildValue("(O()N)", Py_TYPE(self), dict);
2045 }
2046 }
2047}
2048
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002049/*[clinic input]
2050bytearray.__reduce__ as bytearray_reduce
2051
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002052Return state information for pickling.
2053[clinic start generated code]*/
2054
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002055static PyObject *
2056bytearray_reduce_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002057/*[clinic end generated code: output=52bf304086464cab input=44b5737ada62dd3f]*/
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002058{
2059 return _common_reduce(self, 2);
2060}
2061
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002062/*[clinic input]
2063bytearray.__reduce_ex__ as bytearray_reduce_ex
2064
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002065 proto: int = 0
2066 /
2067
2068Return state information for pickling.
2069[clinic start generated code]*/
2070
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002071static PyObject *
2072bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002073/*[clinic end generated code: output=52eac33377197520 input=f129bc1a1aa151ee]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002074{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002075 return _common_reduce(self, proto);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002076}
2077
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002078/*[clinic input]
2079bytearray.__sizeof__ as bytearray_sizeof
2080
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002081Returns the size of the bytearray object in memory, in bytes.
2082[clinic start generated code]*/
2083
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002084static PyObject *
2085bytearray_sizeof_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002086/*[clinic end generated code: output=738abdd17951c427 input=e27320fd98a4bc5a]*/
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002087{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002088 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002089
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002090 res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002091 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002092}
2093
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002094static PySequenceMethods bytearray_as_sequence = {
2095 (lenfunc)bytearray_length, /* sq_length */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002096 (binaryfunc)PyByteArray_Concat, /* sq_concat */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002097 (ssizeargfunc)bytearray_repeat, /* sq_repeat */
2098 (ssizeargfunc)bytearray_getitem, /* sq_item */
2099 0, /* sq_slice */
2100 (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */
2101 0, /* sq_ass_slice */
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002102 (objobjproc)bytearray_contains, /* sq_contains */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002103 (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */
2104 (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002105};
2106
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002107static PyMappingMethods bytearray_as_mapping = {
2108 (lenfunc)bytearray_length,
2109 (binaryfunc)bytearray_subscript,
2110 (objobjargproc)bytearray_ass_subscript,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002111};
2112
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002113static PyBufferProcs bytearray_as_buffer = {
2114 (getbufferproc)bytearray_getbuffer,
2115 (releasebufferproc)bytearray_releasebuffer,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002116};
2117
2118static PyMethodDef
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002119bytearray_methods[] = {
2120 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002121 BYTEARRAY_REDUCE_METHODDEF
2122 BYTEARRAY_REDUCE_EX_METHODDEF
2123 BYTEARRAY_SIZEOF_METHODDEF
2124 BYTEARRAY_APPEND_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002125 {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS,
2126 _Py_capitalize__doc__},
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002127 {"center", (PyCFunction)stringlib_center, METH_VARARGS, _Py_center__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002128 BYTEARRAY_CLEAR_METHODDEF
2129 BYTEARRAY_COPY_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002130 {"count", (PyCFunction)bytearray_count, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002131 _Py_count__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002132 BYTEARRAY_DECODE_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002133 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002134 _Py_endswith__doc__},
Ezio Melotti745d54d2013-11-16 19:10:57 +02002135 {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002136 _Py_expandtabs__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002137 BYTEARRAY_EXTEND_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002138 {"find", (PyCFunction)bytearray_find, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002139 _Py_find__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002140 BYTEARRAY_FROMHEX_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002141 {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__},
2142 {"index", (PyCFunction)bytearray_index, METH_VARARGS, _Py_index__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002143 BYTEARRAY_INSERT_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002144 {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS,
2145 _Py_isalnum__doc__},
2146 {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS,
2147 _Py_isalpha__doc__},
2148 {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS,
2149 _Py_isdigit__doc__},
2150 {"islower", (PyCFunction)stringlib_islower, METH_NOARGS,
2151 _Py_islower__doc__},
2152 {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS,
2153 _Py_isspace__doc__},
2154 {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS,
2155 _Py_istitle__doc__},
2156 {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS,
2157 _Py_isupper__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002158 BYTEARRAY_JOIN_METHODDEF
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002159 {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, _Py_ljust__doc__},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002160 {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002161 BYTEARRAY_LSTRIP_METHODDEF
2162 BYTEARRAY_MAKETRANS_METHODDEF
2163 BYTEARRAY_PARTITION_METHODDEF
2164 BYTEARRAY_POP_METHODDEF
2165 BYTEARRAY_REMOVE_METHODDEF
2166 BYTEARRAY_REPLACE_METHODDEF
2167 BYTEARRAY_REVERSE_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002168 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__},
2169 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__},
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002170 {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, _Py_rjust__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002171 BYTEARRAY_RPARTITION_METHODDEF
2172 BYTEARRAY_RSPLIT_METHODDEF
2173 BYTEARRAY_RSTRIP_METHODDEF
2174 BYTEARRAY_SPLIT_METHODDEF
2175 BYTEARRAY_SPLITLINES_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002176 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002177 _Py_startswith__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002178 BYTEARRAY_STRIP_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002179 {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS,
2180 _Py_swapcase__doc__},
2181 {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002182 BYTEARRAY_TRANSLATE_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002183 {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__},
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002184 {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, _Py_zfill__doc__},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002185 {NULL}
2186};
2187
Ethan Furmanb95b5612015-01-23 20:05:18 -08002188static PyObject *
2189bytearray_mod(PyObject *v, PyObject *w)
2190{
2191 if (!PyByteArray_Check(v))
2192 Py_RETURN_NOTIMPLEMENTED;
Berker Peksag43de36d2016-04-16 01:20:47 +03002193 return _PyBytes_FormatEx(PyByteArray_AS_STRING(v), PyByteArray_GET_SIZE(v), w, 1);
Ethan Furmanb95b5612015-01-23 20:05:18 -08002194}
2195
2196static PyNumberMethods bytearray_as_number = {
2197 0, /*nb_add*/
2198 0, /*nb_subtract*/
2199 0, /*nb_multiply*/
2200 bytearray_mod, /*nb_remainder*/
2201};
2202
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002203PyDoc_STRVAR(bytearray_doc,
Georg Brandl17cb8a82008-05-30 08:20:09 +00002204"bytearray(iterable_of_ints) -> bytearray\n\
2205bytearray(string, encoding[, errors]) -> bytearray\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002206bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\
2207bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\
2208bytearray() -> empty bytes array\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002209\n\
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03002210Construct a mutable bytearray object from:\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002211 - an iterable yielding integers in range(256)\n\
2212 - a text string encoded using the specified encoding\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002213 - a bytes or a buffer object\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002214 - any object implementing the buffer API.\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002215 - an integer");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002216
2217
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002218static PyObject *bytearray_iter(PyObject *seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002219
2220PyTypeObject PyByteArray_Type = {
2221 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2222 "bytearray",
2223 sizeof(PyByteArrayObject),
2224 0,
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002225 (destructor)bytearray_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002226 0, /* tp_print */
2227 0, /* tp_getattr */
2228 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002229 0, /* tp_reserved */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002230 (reprfunc)bytearray_repr, /* tp_repr */
Ethan Furmanb95b5612015-01-23 20:05:18 -08002231 &bytearray_as_number, /* tp_as_number */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002232 &bytearray_as_sequence, /* tp_as_sequence */
2233 &bytearray_as_mapping, /* tp_as_mapping */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002234 0, /* tp_hash */
2235 0, /* tp_call */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002236 bytearray_str, /* tp_str */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002237 PyObject_GenericGetAttr, /* tp_getattro */
2238 0, /* tp_setattro */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002239 &bytearray_as_buffer, /* tp_as_buffer */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002240 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002241 bytearray_doc, /* tp_doc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002242 0, /* tp_traverse */
2243 0, /* tp_clear */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002244 (richcmpfunc)bytearray_richcompare, /* tp_richcompare */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002245 0, /* tp_weaklistoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002246 bytearray_iter, /* tp_iter */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002247 0, /* tp_iternext */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002248 bytearray_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002249 0, /* tp_members */
2250 0, /* tp_getset */
2251 0, /* tp_base */
2252 0, /* tp_dict */
2253 0, /* tp_descr_get */
2254 0, /* tp_descr_set */
2255 0, /* tp_dictoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002256 (initproc)bytearray_init, /* tp_init */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002257 PyType_GenericAlloc, /* tp_alloc */
2258 PyType_GenericNew, /* tp_new */
2259 PyObject_Del, /* tp_free */
2260};
2261
2262/*********************** Bytes Iterator ****************************/
2263
2264typedef struct {
2265 PyObject_HEAD
2266 Py_ssize_t it_index;
2267 PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
2268} bytesiterobject;
2269
2270static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002271bytearrayiter_dealloc(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002272{
2273 _PyObject_GC_UNTRACK(it);
2274 Py_XDECREF(it->it_seq);
2275 PyObject_GC_Del(it);
2276}
2277
2278static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002279bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002280{
2281 Py_VISIT(it->it_seq);
2282 return 0;
2283}
2284
2285static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002286bytearrayiter_next(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002287{
2288 PyByteArrayObject *seq;
2289 PyObject *item;
2290
2291 assert(it != NULL);
2292 seq = it->it_seq;
2293 if (seq == NULL)
2294 return NULL;
2295 assert(PyByteArray_Check(seq));
2296
2297 if (it->it_index < PyByteArray_GET_SIZE(seq)) {
2298 item = PyLong_FromLong(
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002299 (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002300 if (item != NULL)
2301 ++it->it_index;
2302 return item;
2303 }
2304
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002305 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03002306 Py_DECREF(seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002307 return NULL;
2308}
2309
2310static PyObject *
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002311bytearrayiter_length_hint(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002312{
2313 Py_ssize_t len = 0;
Serhiy Storchakaaf658722016-07-03 14:41:36 +03002314 if (it->it_seq) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002315 len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
Serhiy Storchakaaf658722016-07-03 14:41:36 +03002316 if (len < 0) {
2317 len = 0;
2318 }
2319 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002320 return PyLong_FromSsize_t(len);
2321}
2322
2323PyDoc_STRVAR(length_hint_doc,
2324 "Private method returning an estimate of len(list(it)).");
2325
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002326static PyObject *
2327bytearrayiter_reduce(bytesiterobject *it)
2328{
2329 if (it->it_seq != NULL) {
Antoine Pitroua7013882012-04-05 00:04:20 +02002330 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002331 it->it_seq, it->it_index);
2332 } else {
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02002333 return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002334 }
2335}
2336
2337static PyObject *
2338bytearrayiter_setstate(bytesiterobject *it, PyObject *state)
2339{
2340 Py_ssize_t index = PyLong_AsSsize_t(state);
2341 if (index == -1 && PyErr_Occurred())
2342 return NULL;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00002343 if (it->it_seq != NULL) {
2344 if (index < 0)
2345 index = 0;
2346 else if (index > PyByteArray_GET_SIZE(it->it_seq))
2347 index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */
2348 it->it_index = index;
2349 }
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002350 Py_RETURN_NONE;
2351}
2352
2353PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
2354
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002355static PyMethodDef bytearrayiter_methods[] = {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002356 {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002357 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002358 {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002359 bytearray_reduce__doc__},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002360 {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
2361 setstate_doc},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002362 {NULL, NULL} /* sentinel */
2363};
2364
2365PyTypeObject PyByteArrayIter_Type = {
2366 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2367 "bytearray_iterator", /* tp_name */
2368 sizeof(bytesiterobject), /* tp_basicsize */
2369 0, /* tp_itemsize */
2370 /* methods */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002371 (destructor)bytearrayiter_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002372 0, /* tp_print */
2373 0, /* tp_getattr */
2374 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002375 0, /* tp_reserved */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002376 0, /* tp_repr */
2377 0, /* tp_as_number */
2378 0, /* tp_as_sequence */
2379 0, /* tp_as_mapping */
2380 0, /* tp_hash */
2381 0, /* tp_call */
2382 0, /* tp_str */
2383 PyObject_GenericGetAttr, /* tp_getattro */
2384 0, /* tp_setattro */
2385 0, /* tp_as_buffer */
2386 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
2387 0, /* tp_doc */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002388 (traverseproc)bytearrayiter_traverse, /* tp_traverse */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002389 0, /* tp_clear */
2390 0, /* tp_richcompare */
2391 0, /* tp_weaklistoffset */
2392 PyObject_SelfIter, /* tp_iter */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002393 (iternextfunc)bytearrayiter_next, /* tp_iternext */
2394 bytearrayiter_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002395 0,
2396};
2397
2398static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002399bytearray_iter(PyObject *seq)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002400{
2401 bytesiterobject *it;
2402
2403 if (!PyByteArray_Check(seq)) {
2404 PyErr_BadInternalCall();
2405 return NULL;
2406 }
2407 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
2408 if (it == NULL)
2409 return NULL;
2410 it->it_index = 0;
2411 Py_INCREF(seq);
2412 it->it_seq = (PyByteArrayObject *)seq;
2413 _PyObject_GC_TRACK(it);
2414 return (PyObject *)it;
2415}