blob: 16b4fd7a900c17c7e7e70c3456e94882e3d3a226 [file] [log] [blame]
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001/* PyByteArray (bytearray) implementation */
2
3#define PY_SSIZE_T_CLEAN
4#include "Python.h"
5#include "structmember.h"
6#include "bytes_methods.h"
Ethan Furmanb95b5612015-01-23 20:05:18 -08007#include "bytesobject.h"
Gregory P. Smith8cb65692015-04-25 23:22:26 +00008#include "pystrhex.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00009
Martin v. Löwis7252a6e2014-07-27 16:25:09 +020010/*[clinic input]
11class bytearray "PyByteArrayObject *" "&PyByteArray_Type"
12[clinic start generated code]*/
13/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/
14
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000015char _PyByteArray_empty_string[] = "";
Christian Heimes2c9c7a52008-05-26 13:42:13 +000016
17void
18PyByteArray_Fini(void)
19{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000020}
21
22int
23PyByteArray_Init(void)
24{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000025 return 1;
26}
27
28/* end nullbytes support */
29
30/* Helpers */
31
32static int
33_getbytevalue(PyObject* arg, int *value)
34{
35 long face_value;
36
37 if (PyLong_Check(arg)) {
38 face_value = PyLong_AsLong(arg);
Georg Brandl9a54d7c2008-07-16 23:15:30 +000039 } else {
40 PyObject *index = PyNumber_Index(arg);
41 if (index == NULL) {
42 PyErr_Format(PyExc_TypeError, "an integer is required");
Mark Dickinson10de93a2010-07-09 19:25:48 +000043 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000044 return 0;
45 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +000046 face_value = PyLong_AsLong(index);
47 Py_DECREF(index);
48 }
49
50 if (face_value < 0 || face_value >= 256) {
51 /* this includes the OverflowError in case the long is too large */
52 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
Mark Dickinson10de93a2010-07-09 19:25:48 +000053 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000054 return 0;
55 }
56
57 *value = face_value;
58 return 1;
59}
60
61static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +000062bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000063{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000064 void *ptr;
65 if (view == NULL) {
Stefan Krah5178d912015-02-03 16:57:21 +010066 PyErr_SetString(PyExc_BufferError,
67 "bytearray_getbuffer: view==NULL argument is obsolete");
68 return -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000069 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000070 ptr = (void *) PyByteArray_AS_STRING(obj);
Stefan Krah5178d912015-02-03 16:57:21 +010071 /* cannot fail if view != NULL and readonly == 0 */
72 (void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
73 obj->ob_exports++;
74 return 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000075}
76
77static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +000078bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000079{
80 obj->ob_exports--;
81}
82
Antoine Pitrou5504e892008-12-06 21:27:53 +000083static int
84_canresize(PyByteArrayObject *self)
85{
86 if (self->ob_exports > 0) {
87 PyErr_SetString(PyExc_BufferError,
88 "Existing exports of data: object cannot be re-sized");
89 return 0;
90 }
91 return 1;
92}
93
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030094#include "clinic/bytearrayobject.c.h"
95
Christian Heimes2c9c7a52008-05-26 13:42:13 +000096/* Direct API functions */
97
98PyObject *
99PyByteArray_FromObject(PyObject *input)
100{
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100101 return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type,
102 input, NULL);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000103}
104
105PyObject *
106PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
107{
108 PyByteArrayObject *new;
109 Py_ssize_t alloc;
110
111 if (size < 0) {
112 PyErr_SetString(PyExc_SystemError,
113 "Negative size passed to PyByteArray_FromStringAndSize");
114 return NULL;
115 }
116
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000117 /* Prevent buffer overflow when setting alloc to size+1. */
118 if (size == PY_SSIZE_T_MAX) {
119 return PyErr_NoMemory();
120 }
121
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000122 new = PyObject_New(PyByteArrayObject, &PyByteArray_Type);
123 if (new == NULL)
124 return NULL;
125
126 if (size == 0) {
127 new->ob_bytes = NULL;
128 alloc = 0;
129 }
130 else {
131 alloc = size + 1;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100132 new->ob_bytes = PyObject_Malloc(alloc);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000133 if (new->ob_bytes == NULL) {
134 Py_DECREF(new);
135 return PyErr_NoMemory();
136 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +0000137 if (bytes != NULL && size > 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000138 memcpy(new->ob_bytes, bytes, size);
139 new->ob_bytes[size] = '\0'; /* Trailing null byte */
140 }
141 Py_SIZE(new) = size;
142 new->ob_alloc = alloc;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200143 new->ob_start = new->ob_bytes;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000144 new->ob_exports = 0;
145
146 return (PyObject *)new;
147}
148
149Py_ssize_t
150PyByteArray_Size(PyObject *self)
151{
152 assert(self != NULL);
153 assert(PyByteArray_Check(self));
154
155 return PyByteArray_GET_SIZE(self);
156}
157
158char *
159PyByteArray_AsString(PyObject *self)
160{
161 assert(self != NULL);
162 assert(PyByteArray_Check(self));
163
164 return PyByteArray_AS_STRING(self);
165}
166
167int
Antoine Pitroucc231542014-11-02 18:40:09 +0100168PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000169{
170 void *sval;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200171 PyByteArrayObject *obj = ((PyByteArrayObject *)self);
Antoine Pitroucc231542014-11-02 18:40:09 +0100172 /* All computations are done unsigned to avoid integer overflows
173 (see issue #22335). */
174 size_t alloc = (size_t) obj->ob_alloc;
175 size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes);
176 size_t size = (size_t) requested_size;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000177
178 assert(self != NULL);
179 assert(PyByteArray_Check(self));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200180 assert(logical_offset <= alloc);
Antoine Pitroucc231542014-11-02 18:40:09 +0100181 assert(requested_size >= 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000182
Antoine Pitroucc231542014-11-02 18:40:09 +0100183 if (requested_size == Py_SIZE(self)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000184 return 0;
185 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200186 if (!_canresize(obj)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000187 return -1;
188 }
189
Antoine Pitrou25454112015-05-19 20:52:27 +0200190 if (size + logical_offset + 1 <= alloc) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200191 /* Current buffer is large enough to host the requested size,
192 decide on a strategy. */
193 if (size < alloc / 2) {
194 /* Major downsize; resize down to exact size */
195 alloc = size + 1;
196 }
197 else {
198 /* Minor downsize; quick exit */
199 Py_SIZE(self) = size;
200 PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */
201 return 0;
202 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000203 }
204 else {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200205 /* Need growing, decide on a strategy */
206 if (size <= alloc * 1.125) {
207 /* Moderate upsize; overallocate similar to list_resize() */
208 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
209 }
210 else {
211 /* Major upsize; resize up to exact size */
212 alloc = size + 1;
213 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000214 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100215 if (alloc > PY_SSIZE_T_MAX) {
216 PyErr_NoMemory();
217 return -1;
218 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000219
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200220 if (logical_offset > 0) {
221 sval = PyObject_Malloc(alloc);
222 if (sval == NULL) {
223 PyErr_NoMemory();
224 return -1;
225 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100226 memcpy(sval, PyByteArray_AS_STRING(self),
227 Py_MIN(requested_size, Py_SIZE(self)));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200228 PyObject_Free(obj->ob_bytes);
229 }
230 else {
231 sval = PyObject_Realloc(obj->ob_bytes, alloc);
232 if (sval == NULL) {
233 PyErr_NoMemory();
234 return -1;
235 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000236 }
237
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200238 obj->ob_bytes = obj->ob_start = sval;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000239 Py_SIZE(self) = size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200240 obj->ob_alloc = alloc;
241 obj->ob_bytes[size] = '\0'; /* Trailing null byte */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000242
243 return 0;
244}
245
246PyObject *
247PyByteArray_Concat(PyObject *a, PyObject *b)
248{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000249 Py_buffer va, vb;
250 PyByteArrayObject *result = NULL;
251
252 va.len = -1;
253 vb.len = -1;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200254 if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 ||
255 PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000256 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
257 Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
258 goto done;
259 }
260
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300261 if (va.len > PY_SSIZE_T_MAX - vb.len) {
262 PyErr_NoMemory();
263 goto done;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000264 }
265
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300266 result = (PyByteArrayObject *) \
267 PyByteArray_FromStringAndSize(NULL, va.len + vb.len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000268 if (result != NULL) {
269 memcpy(result->ob_bytes, va.buf, va.len);
270 memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
271 }
272
273 done:
274 if (va.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000275 PyBuffer_Release(&va);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000276 if (vb.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000277 PyBuffer_Release(&vb);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000278 return (PyObject *)result;
279}
280
281/* Functions stuffed into the type object */
282
283static Py_ssize_t
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000284bytearray_length(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000285{
286 return Py_SIZE(self);
287}
288
289static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000290bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000291{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000292 Py_ssize_t size;
293 Py_buffer vo;
294
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200295 if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000296 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
297 Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name);
298 return NULL;
299 }
300
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300301 size = Py_SIZE(self);
302 if (size > PY_SSIZE_T_MAX - vo.len) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000303 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000304 return PyErr_NoMemory();
305 }
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300306 if (PyByteArray_Resize((PyObject *)self, size + vo.len) < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000307 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000308 return NULL;
309 }
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300310 memcpy(PyByteArray_AS_STRING(self) + size, vo.buf, vo.len);
Martin v. Löwis423be952008-08-13 15:53:07 +0000311 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000312 Py_INCREF(self);
313 return (PyObject *)self;
314}
315
316static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000317bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000318{
319 PyByteArrayObject *result;
320 Py_ssize_t mysize;
321 Py_ssize_t size;
322
323 if (count < 0)
324 count = 0;
325 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000326 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000327 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000328 size = mysize * count;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000329 result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size);
330 if (result != NULL && size != 0) {
331 if (mysize == 1)
332 memset(result->ob_bytes, self->ob_bytes[0], size);
333 else {
334 Py_ssize_t i;
335 for (i = 0; i < count; i++)
336 memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize);
337 }
338 }
339 return (PyObject *)result;
340}
341
342static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000343bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000344{
345 Py_ssize_t mysize;
346 Py_ssize_t size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200347 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000348
349 if (count < 0)
350 count = 0;
351 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000352 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000353 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000354 size = mysize * count;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200355 if (PyByteArray_Resize((PyObject *)self, size) < 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000356 return NULL;
357
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200358 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000359 if (mysize == 1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200360 memset(buf, buf[0], size);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000361 else {
362 Py_ssize_t i;
363 for (i = 1; i < count; i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200364 memcpy(buf + i*mysize, buf, mysize);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000365 }
366
367 Py_INCREF(self);
368 return (PyObject *)self;
369}
370
371static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000372bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000373{
374 if (i < 0)
375 i += Py_SIZE(self);
376 if (i < 0 || i >= Py_SIZE(self)) {
377 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
378 return NULL;
379 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200380 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000381}
382
383static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000384bytearray_subscript(PyByteArrayObject *self, PyObject *index)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000385{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000386 if (PyIndex_Check(index)) {
387 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000388
389 if (i == -1 && PyErr_Occurred())
390 return NULL;
391
392 if (i < 0)
393 i += PyByteArray_GET_SIZE(self);
394
395 if (i < 0 || i >= Py_SIZE(self)) {
396 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
397 return NULL;
398 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200399 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000400 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000401 else if (PySlice_Check(index)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000402 Py_ssize_t start, stop, step, slicelength, cur, i;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000403 if (PySlice_GetIndicesEx(index,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000404 PyByteArray_GET_SIZE(self),
405 &start, &stop, &step, &slicelength) < 0) {
406 return NULL;
407 }
408
409 if (slicelength <= 0)
410 return PyByteArray_FromStringAndSize("", 0);
411 else if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200412 return PyByteArray_FromStringAndSize(
413 PyByteArray_AS_STRING(self) + start, slicelength);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000414 }
415 else {
416 char *source_buf = PyByteArray_AS_STRING(self);
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000417 char *result_buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000418 PyObject *result;
419
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000420 result = PyByteArray_FromStringAndSize(NULL, slicelength);
421 if (result == NULL)
422 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000423
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000424 result_buf = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000425 for (cur = start, i = 0; i < slicelength;
426 cur += step, i++) {
427 result_buf[i] = source_buf[cur];
428 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000429 return result;
430 }
431 }
432 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400433 PyErr_Format(PyExc_TypeError,
434 "bytearray indices must be integers or slices, not %.200s",
435 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000436 return NULL;
437 }
438}
439
440static int
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200441bytearray_setslice_linear(PyByteArrayObject *self,
442 Py_ssize_t lo, Py_ssize_t hi,
443 char *bytes, Py_ssize_t bytes_len)
444{
445 Py_ssize_t avail = hi - lo;
446 char *buf = PyByteArray_AS_STRING(self);
447 Py_ssize_t growth = bytes_len - avail;
Victor Stinner84557232013-11-21 12:29:51 +0100448 int res = 0;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200449 assert(avail >= 0);
450
Victor Stinner84557232013-11-21 12:29:51 +0100451 if (growth < 0) {
452 if (!_canresize(self))
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200453 return -1;
Victor Stinner84557232013-11-21 12:29:51 +0100454
455 if (lo == 0) {
456 /* Shrink the buffer by advancing its logical start */
457 self->ob_start -= growth;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200458 /*
Victor Stinner84557232013-11-21 12:29:51 +0100459 0 lo hi old_size
460 | |<----avail----->|<-----tail------>|
461 | |<-bytes_len->|<-----tail------>|
462 0 new_lo new_hi new_size
463 */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200464 }
Victor Stinner84557232013-11-21 12:29:51 +0100465 else {
466 /*
467 0 lo hi old_size
468 | |<----avail----->|<-----tomove------>|
469 | |<-bytes_len->|<-----tomove------>|
470 0 lo new_hi new_size
471 */
472 memmove(buf + lo + bytes_len, buf + hi,
473 Py_SIZE(self) - hi);
474 }
475 if (PyByteArray_Resize((PyObject *)self,
476 Py_SIZE(self) + growth) < 0) {
477 /* Issue #19578: Handling the memory allocation failure here is
478 tricky here because the bytearray object has already been
479 modified. Depending on growth and lo, the behaviour is
480 different.
481
482 If growth < 0 and lo != 0, the operation is completed, but a
483 MemoryError is still raised and the memory block is not
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700484 shrunk. Otherwise, the bytearray is restored in its previous
Victor Stinner84557232013-11-21 12:29:51 +0100485 state and a MemoryError is raised. */
486 if (lo == 0) {
487 self->ob_start += growth;
488 return -1;
489 }
490 /* memmove() removed bytes, the bytearray object cannot be
491 restored in its previous state. */
492 Py_SIZE(self) += growth;
493 res = -1;
494 }
495 buf = PyByteArray_AS_STRING(self);
496 }
497 else if (growth > 0) {
498 if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) {
499 PyErr_NoMemory();
500 return -1;
501 }
502
503 if (PyByteArray_Resize((PyObject *)self,
504 Py_SIZE(self) + growth) < 0) {
505 return -1;
506 }
507 buf = PyByteArray_AS_STRING(self);
508 /* Make the place for the additional bytes */
509 /*
510 0 lo hi old_size
511 | |<-avail->|<-----tomove------>|
512 | |<---bytes_len-->|<-----tomove------>|
513 0 lo new_hi new_size
514 */
515 memmove(buf + lo + bytes_len, buf + hi,
516 Py_SIZE(self) - lo - bytes_len);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200517 }
518
519 if (bytes_len > 0)
520 memcpy(buf + lo, bytes, bytes_len);
Victor Stinner84557232013-11-21 12:29:51 +0100521 return res;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200522}
523
524static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000525bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000526 PyObject *values)
527{
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200528 Py_ssize_t needed;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000529 void *bytes;
530 Py_buffer vbytes;
531 int res = 0;
532
533 vbytes.len = -1;
534 if (values == (PyObject *)self) {
535 /* Make a copy and call this function recursively */
536 int err;
537 values = PyByteArray_FromObject(values);
538 if (values == NULL)
539 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000540 err = bytearray_setslice(self, lo, hi, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000541 Py_DECREF(values);
542 return err;
543 }
544 if (values == NULL) {
545 /* del b[lo:hi] */
546 bytes = NULL;
547 needed = 0;
548 }
549 else {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200550 if (PyObject_GetBuffer(values, &vbytes, PyBUF_SIMPLE) != 0) {
551 PyErr_Format(PyExc_TypeError,
552 "can't set bytearray slice from %.100s",
553 Py_TYPE(values)->tp_name);
554 return -1;
555 }
556 needed = vbytes.len;
557 bytes = vbytes.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000558 }
559
560 if (lo < 0)
561 lo = 0;
562 if (hi < lo)
563 hi = lo;
564 if (hi > Py_SIZE(self))
565 hi = Py_SIZE(self);
566
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200567 res = bytearray_setslice_linear(self, lo, hi, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000568 if (vbytes.len != -1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200569 PyBuffer_Release(&vbytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000570 return res;
571}
572
573static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000574bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000575{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000576 int ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000577
578 if (i < 0)
579 i += Py_SIZE(self);
580
581 if (i < 0 || i >= Py_SIZE(self)) {
582 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
583 return -1;
584 }
585
586 if (value == NULL)
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000587 return bytearray_setslice(self, i, i+1, NULL);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000588
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000589 if (!_getbytevalue(value, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000590 return -1;
591
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200592 PyByteArray_AS_STRING(self)[i] = ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000593 return 0;
594}
595
596static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000597bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000598{
599 Py_ssize_t start, stop, step, slicelen, needed;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200600 char *buf, *bytes;
601 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000602
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000603 if (PyIndex_Check(index)) {
604 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000605
606 if (i == -1 && PyErr_Occurred())
607 return -1;
608
609 if (i < 0)
610 i += PyByteArray_GET_SIZE(self);
611
612 if (i < 0 || i >= Py_SIZE(self)) {
613 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
614 return -1;
615 }
616
617 if (values == NULL) {
618 /* Fall through to slice assignment */
619 start = i;
620 stop = i + 1;
621 step = 1;
622 slicelen = 1;
623 }
624 else {
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000625 int ival;
626 if (!_getbytevalue(values, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000627 return -1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200628 buf[i] = (char)ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000629 return 0;
630 }
631 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000632 else if (PySlice_Check(index)) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000633 if (PySlice_GetIndicesEx(index,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000634 PyByteArray_GET_SIZE(self),
635 &start, &stop, &step, &slicelen) < 0) {
636 return -1;
637 }
638 }
639 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400640 PyErr_Format(PyExc_TypeError,
641 "bytearray indices must be integers or slices, not %.200s",
642 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000643 return -1;
644 }
645
646 if (values == NULL) {
647 bytes = NULL;
648 needed = 0;
649 }
650 else if (values == (PyObject *)self || !PyByteArray_Check(values)) {
Christian Heimes6d26ade2012-11-03 23:07:59 +0100651 int err;
Ezio Melottic64bcbe2012-11-03 21:19:06 +0200652 if (PyNumber_Check(values) || PyUnicode_Check(values)) {
653 PyErr_SetString(PyExc_TypeError,
654 "can assign only bytes, buffers, or iterables "
655 "of ints in range(0, 256)");
656 return -1;
657 }
Georg Brandlf3fa5682010-12-04 17:09:30 +0000658 /* Make a copy and call this function recursively */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000659 values = PyByteArray_FromObject(values);
660 if (values == NULL)
661 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000662 err = bytearray_ass_subscript(self, index, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000663 Py_DECREF(values);
664 return err;
665 }
666 else {
667 assert(PyByteArray_Check(values));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200668 bytes = PyByteArray_AS_STRING(values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000669 needed = Py_SIZE(values);
670 }
671 /* Make sure b[5:2] = ... inserts before 5, not before 2. */
672 if ((step < 0 && start < stop) ||
673 (step > 0 && start > stop))
674 stop = start;
675 if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200676 return bytearray_setslice_linear(self, start, stop, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000677 }
678 else {
679 if (needed == 0) {
680 /* Delete slice */
Mark Dickinsonbc099642010-01-29 17:27:24 +0000681 size_t cur;
682 Py_ssize_t i;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000683
Antoine Pitrou5504e892008-12-06 21:27:53 +0000684 if (!_canresize(self))
685 return -1;
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000686
687 if (slicelen == 0)
688 /* Nothing to do here. */
689 return 0;
690
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000691 if (step < 0) {
692 stop = start + 1;
693 start = stop + step * (slicelen - 1) - 1;
694 step = -step;
695 }
696 for (cur = start, i = 0;
697 i < slicelen; cur += step, i++) {
698 Py_ssize_t lim = step - 1;
699
Mark Dickinson66f575b2010-02-14 12:53:32 +0000700 if (cur + step >= (size_t)PyByteArray_GET_SIZE(self))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000701 lim = PyByteArray_GET_SIZE(self) - cur - 1;
702
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200703 memmove(buf + cur - i,
704 buf + cur + 1, lim);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000705 }
706 /* Move the tail of the bytes, in one chunk */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000707 cur = start + (size_t)slicelen*step;
Mark Dickinson66f575b2010-02-14 12:53:32 +0000708 if (cur < (size_t)PyByteArray_GET_SIZE(self)) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200709 memmove(buf + cur - slicelen,
710 buf + cur,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000711 PyByteArray_GET_SIZE(self) - cur);
712 }
713 if (PyByteArray_Resize((PyObject *)self,
714 PyByteArray_GET_SIZE(self) - slicelen) < 0)
715 return -1;
716
717 return 0;
718 }
719 else {
720 /* Assign slice */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000721 Py_ssize_t i;
722 size_t cur;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000723
724 if (needed != slicelen) {
725 PyErr_Format(PyExc_ValueError,
726 "attempt to assign bytes of size %zd "
727 "to extended slice of size %zd",
728 needed, slicelen);
729 return -1;
730 }
731 for (cur = start, i = 0; i < slicelen; cur += step, i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200732 buf[cur] = bytes[i];
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000733 return 0;
734 }
735 }
736}
737
738static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000739bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000740{
741 static char *kwlist[] = {"source", "encoding", "errors", 0};
742 PyObject *arg = NULL;
743 const char *encoding = NULL;
744 const char *errors = NULL;
745 Py_ssize_t count;
746 PyObject *it;
747 PyObject *(*iternext)(PyObject *);
748
749 if (Py_SIZE(self) != 0) {
750 /* Empty previous contents (yes, do this first of all!) */
751 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
752 return -1;
753 }
754
755 /* Parse arguments */
Georg Brandl3dbca812008-07-23 16:10:53 +0000756 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000757 &arg, &encoding, &errors))
758 return -1;
759
760 /* Make a quick exit if no first argument */
761 if (arg == NULL) {
762 if (encoding != NULL || errors != NULL) {
763 PyErr_SetString(PyExc_TypeError,
764 "encoding or errors without sequence argument");
765 return -1;
766 }
767 return 0;
768 }
769
770 if (PyUnicode_Check(arg)) {
771 /* Encode via the codec registry */
772 PyObject *encoded, *new;
773 if (encoding == NULL) {
774 PyErr_SetString(PyExc_TypeError,
775 "string argument without an encoding");
776 return -1;
777 }
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000778 encoded = PyUnicode_AsEncodedString(arg, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000779 if (encoded == NULL)
780 return -1;
781 assert(PyBytes_Check(encoded));
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000782 new = bytearray_iconcat(self, encoded);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000783 Py_DECREF(encoded);
784 if (new == NULL)
785 return -1;
786 Py_DECREF(new);
787 return 0;
788 }
789
790 /* If it's not unicode, there can't be encoding or errors */
791 if (encoding != NULL || errors != NULL) {
792 PyErr_SetString(PyExc_TypeError,
793 "encoding or errors without a string argument");
794 return -1;
795 }
796
797 /* Is it an int? */
Serhiy Storchakaeb249882016-08-15 09:46:07 +0300798 if (PyIndex_Check(arg)) {
799 count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
800 if (count == -1 && PyErr_Occurred()) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000801 return -1;
Serhiy Storchakaeb249882016-08-15 09:46:07 +0300802 }
803 if (count < 0) {
804 PyErr_SetString(PyExc_ValueError, "negative count");
805 return -1;
806 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000807 if (count > 0) {
Victor Stinner2bc4d952014-06-02 22:22:42 +0200808 if (PyByteArray_Resize((PyObject *)self, count))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000809 return -1;
Victor Stinner2bc4d952014-06-02 22:22:42 +0200810 memset(PyByteArray_AS_STRING(self), 0, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000811 }
812 return 0;
813 }
814
815 /* Use the buffer API */
816 if (PyObject_CheckBuffer(arg)) {
817 Py_ssize_t size;
818 Py_buffer view;
819 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
820 return -1;
821 size = view.len;
822 if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200823 if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self),
824 &view, size, 'C') < 0)
Stefan Krah7d12d9d2012-07-28 12:25:55 +0200825 goto fail;
Martin v. Löwis423be952008-08-13 15:53:07 +0000826 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000827 return 0;
828 fail:
Martin v. Löwis423be952008-08-13 15:53:07 +0000829 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000830 return -1;
831 }
832
833 /* XXX Optimize this if the arguments is a list, tuple */
834
835 /* Get the iterator */
836 it = PyObject_GetIter(arg);
837 if (it == NULL)
838 return -1;
839 iternext = *Py_TYPE(it)->tp_iternext;
840
841 /* Run the iterator to exhaustion */
842 for (;;) {
843 PyObject *item;
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000844 int rc, value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000845
846 /* Get the next item */
847 item = iternext(it);
848 if (item == NULL) {
849 if (PyErr_Occurred()) {
850 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
851 goto error;
852 PyErr_Clear();
853 }
854 break;
855 }
856
857 /* Interpret it as an int (__index__) */
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000858 rc = _getbytevalue(item, &value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000859 Py_DECREF(item);
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000860 if (!rc)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000861 goto error;
862
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000863 /* Append the byte */
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300864 if (Py_SIZE(self) + 1 < self->ob_alloc) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000865 Py_SIZE(self)++;
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300866 PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0';
867 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000868 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
869 goto error;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200870 PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000871 }
872
873 /* Clean up and return success */
874 Py_DECREF(it);
875 return 0;
876
877 error:
878 /* Error handling when it != NULL */
879 Py_DECREF(it);
880 return -1;
881}
882
883/* Mostly copied from string_repr, but without the
884 "smart quote" functionality. */
885static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000886bytearray_repr(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000887{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000888 const char *quote_prefix = "bytearray(b";
889 const char *quote_postfix = ")";
890 Py_ssize_t length = Py_SIZE(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200891 /* 15 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
Mark Dickinson66f575b2010-02-14 12:53:32 +0000892 size_t newsize;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000893 PyObject *v;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200894 Py_ssize_t i;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200895 char *bytes;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200896 char c;
897 char *p;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200898 int quote;
899 char *test, *start;
900 char *buffer;
901
902 if (length > (PY_SSIZE_T_MAX - 15) / 4) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000903 PyErr_SetString(PyExc_OverflowError,
904 "bytearray object is too large to make repr");
905 return NULL;
906 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200907
908 newsize = 15 + length * 4;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100909 buffer = PyObject_Malloc(newsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200910 if (buffer == NULL) {
911 PyErr_NoMemory();
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000912 return NULL;
913 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000914
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200915 /* Figure out which quote to use; single is preferred */
916 quote = '\'';
917 start = PyByteArray_AS_STRING(self);
918 for (test = start; test < start+length; ++test) {
919 if (*test == '"') {
920 quote = '\''; /* back to single */
921 break;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000922 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200923 else if (*test == '\'')
924 quote = '"';
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000925 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200926
927 p = buffer;
928 while (*quote_prefix)
929 *p++ = *quote_prefix++;
930 *p++ = quote;
931
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200932 bytes = PyByteArray_AS_STRING(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200933 for (i = 0; i < length; i++) {
934 /* There's at least enough room for a hex escape
935 and a closing quote. */
936 assert(newsize - (p - buffer) >= 5);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200937 c = bytes[i];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200938 if (c == '\'' || c == '\\')
939 *p++ = '\\', *p++ = c;
940 else if (c == '\t')
941 *p++ = '\\', *p++ = 't';
942 else if (c == '\n')
943 *p++ = '\\', *p++ = 'n';
944 else if (c == '\r')
945 *p++ = '\\', *p++ = 'r';
946 else if (c == 0)
947 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
948 else if (c < ' ' || c >= 0x7f) {
949 *p++ = '\\';
950 *p++ = 'x';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200951 *p++ = Py_hexdigits[(c & 0xf0) >> 4];
952 *p++ = Py_hexdigits[c & 0xf];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200953 }
954 else
955 *p++ = c;
956 }
957 assert(newsize - (p - buffer) >= 1);
958 *p++ = quote;
959 while (*quote_postfix) {
960 *p++ = *quote_postfix++;
961 }
962
963 v = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100964 PyObject_Free(buffer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200965 return v;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000966}
967
968static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000969bytearray_str(PyObject *op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000970{
Alexander Belopolskyf0f45142010-08-11 17:31:17 +0000971 if (Py_BytesWarningFlag) {
972 if (PyErr_WarnEx(PyExc_BytesWarning,
973 "str() on a bytearray instance", 1))
974 return NULL;
975 }
976 return bytearray_repr((PyByteArrayObject*)op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000977}
978
979static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000980bytearray_richcompare(PyObject *self, PyObject *other, int op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000981{
982 Py_ssize_t self_size, other_size;
983 Py_buffer self_bytes, other_bytes;
984 PyObject *res;
985 Py_ssize_t minsize;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +0300986 int cmp, rc;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000987
988 /* Bytes can be compared to anything that supports the (binary)
989 buffer API. Except that a comparison with Unicode is always an
990 error, even if the comparison is for equality. */
Serhiy Storchakafa494fd2015-05-30 17:45:22 +0300991 rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type);
992 if (!rc)
993 rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type);
994 if (rc < 0)
995 return NULL;
996 if (rc) {
Barry Warsaw9e9dcd62008-10-17 01:50:37 +0000997 if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000998 if (PyErr_WarnEx(PyExc_BytesWarning,
Georg Brandle5d68ac2008-06-04 11:30:26 +0000999 "Comparison between bytearray and string", 1))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001000 return NULL;
1001 }
1002
Brian Curtindfc80e32011-08-10 20:28:54 -05001003 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001004 }
1005
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001006 if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001007 PyErr_Clear();
Brian Curtindfc80e32011-08-10 20:28:54 -05001008 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001009 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001010 self_size = self_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001011
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001012 if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001013 PyErr_Clear();
Martin v. Löwis423be952008-08-13 15:53:07 +00001014 PyBuffer_Release(&self_bytes);
Brian Curtindfc80e32011-08-10 20:28:54 -05001015 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001016 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001017 other_size = other_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001018
1019 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
1020 /* Shortcut: if the lengths differ, the objects differ */
1021 cmp = (op == Py_NE);
1022 }
1023 else {
1024 minsize = self_size;
1025 if (other_size < minsize)
1026 minsize = other_size;
1027
1028 cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize);
1029 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
1030
1031 if (cmp == 0) {
1032 if (self_size < other_size)
1033 cmp = -1;
1034 else if (self_size > other_size)
1035 cmp = 1;
1036 }
1037
1038 switch (op) {
1039 case Py_LT: cmp = cmp < 0; break;
1040 case Py_LE: cmp = cmp <= 0; break;
1041 case Py_EQ: cmp = cmp == 0; break;
1042 case Py_NE: cmp = cmp != 0; break;
1043 case Py_GT: cmp = cmp > 0; break;
1044 case Py_GE: cmp = cmp >= 0; break;
1045 }
1046 }
1047
1048 res = cmp ? Py_True : Py_False;
Martin v. Löwis423be952008-08-13 15:53:07 +00001049 PyBuffer_Release(&self_bytes);
1050 PyBuffer_Release(&other_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001051 Py_INCREF(res);
1052 return res;
1053}
1054
1055static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001056bytearray_dealloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001057{
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001058 if (self->ob_exports > 0) {
1059 PyErr_SetString(PyExc_SystemError,
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001060 "deallocated bytearray object has exported buffers");
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001061 PyErr_Print();
1062 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001063 if (self->ob_bytes != 0) {
Antoine Pitrou39aba4f2011-11-12 21:15:28 +01001064 PyObject_Free(self->ob_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001065 }
1066 Py_TYPE(self)->tp_free((PyObject *)self);
1067}
1068
1069
1070/* -------------------------------------------------------------------- */
1071/* Methods */
1072
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001073#define FASTSEARCH fastsearch
1074#define STRINGLIB(F) stringlib_##F
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001075#define STRINGLIB_CHAR char
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001076#define STRINGLIB_SIZEOF_CHAR 1
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001077#define STRINGLIB_LEN PyByteArray_GET_SIZE
1078#define STRINGLIB_STR PyByteArray_AS_STRING
1079#define STRINGLIB_NEW PyByteArray_FromStringAndSize
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001080#define STRINGLIB_ISSPACE Py_ISSPACE
1081#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001082#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1083#define STRINGLIB_MUTABLE 1
1084
1085#include "stringlib/fastsearch.h"
1086#include "stringlib/count.h"
1087#include "stringlib/find.h"
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001088#include "stringlib/join.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001089#include "stringlib/partition.h"
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001090#include "stringlib/split.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001091#include "stringlib/ctype.h"
1092#include "stringlib/transmogrify.h"
1093
1094
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001095static PyObject *
1096bytearray_find(PyByteArrayObject *self, PyObject *args)
1097{
1098 return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1099}
1100
1101static PyObject *
1102bytearray_count(PyByteArrayObject *self, PyObject *args)
1103{
1104 return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1105}
1106
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001107/*[clinic input]
1108bytearray.clear
1109
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001110Remove all items from the bytearray.
1111[clinic start generated code]*/
1112
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001113static PyObject *
1114bytearray_clear_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001115/*[clinic end generated code: output=85c2fe6aede0956c input=ed6edae9de447ac4]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001116{
1117 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1118 return NULL;
1119 Py_RETURN_NONE;
1120}
1121
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001122/*[clinic input]
1123bytearray.copy
1124
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001125Return a copy of B.
1126[clinic start generated code]*/
1127
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001128static PyObject *
1129bytearray_copy_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001130/*[clinic end generated code: output=68cfbcfed484c132 input=6597b0c01bccaa9e]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001131{
1132 return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self),
1133 PyByteArray_GET_SIZE(self));
1134}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001135
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001136static PyObject *
1137bytearray_index(PyByteArrayObject *self, PyObject *args)
1138{
1139 return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1140}
1141
1142static PyObject *
1143bytearray_rfind(PyByteArrayObject *self, PyObject *args)
1144{
1145 return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1146}
1147
1148static PyObject *
1149bytearray_rindex(PyByteArrayObject *self, PyObject *args)
1150{
1151 return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1152}
1153
1154static int
1155bytearray_contains(PyObject *self, PyObject *arg)
1156{
1157 return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg);
1158}
1159
1160static PyObject *
1161bytearray_startswith(PyByteArrayObject *self, PyObject *args)
1162{
1163 return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1164}
1165
1166static PyObject *
1167bytearray_endswith(PyByteArrayObject *self, PyObject *args)
1168{
1169 return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1170}
1171
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001172
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001173/*[clinic input]
1174bytearray.translate
1175
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001176 table: object
1177 Translation table, which must be a bytes object of length 256.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001178 /
Martin Panter1b6c6da2016-08-27 08:35:02 +00001179 delete as deletechars: object(c_default="NULL") = b''
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001180
1181Return a copy with each character mapped by the given translation table.
1182
Martin Panter1b6c6da2016-08-27 08:35:02 +00001183All characters occurring in the optional argument delete are removed.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001184The remaining characters are mapped through the given translation table.
1185[clinic start generated code]*/
1186
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001187static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001188bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
Martin Panter1b6c6da2016-08-27 08:35:02 +00001189 PyObject *deletechars)
1190/*[clinic end generated code: output=b6a8f01c2a74e446 input=cfff956d4d127a9b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001191{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001192 char *input, *output;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001193 const char *table_chars;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001194 Py_ssize_t i, c;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001195 PyObject *input_obj = (PyObject*)self;
1196 const char *output_start;
1197 Py_ssize_t inlen;
Georg Brandlccc47b62008-12-28 11:44:14 +00001198 PyObject *result = NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001199 int trans_table[256];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001200 Py_buffer vtable, vdel;
1201
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001202 if (table == Py_None) {
1203 table_chars = NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001204 table = NULL;
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001205 } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001206 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001207 } else {
1208 if (vtable.len != 256) {
1209 PyErr_SetString(PyExc_ValueError,
1210 "translation table must be 256 characters long");
Georg Brandl953152f2009-07-22 12:03:59 +00001211 PyBuffer_Release(&vtable);
1212 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001213 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001214 table_chars = (const char*)vtable.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001215 }
1216
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001217 if (deletechars != NULL) {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001218 if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) {
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001219 if (table != NULL)
Georg Brandl953152f2009-07-22 12:03:59 +00001220 PyBuffer_Release(&vtable);
1221 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001222 }
1223 }
1224 else {
1225 vdel.buf = NULL;
1226 vdel.len = 0;
1227 }
1228
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001229 inlen = PyByteArray_GET_SIZE(input_obj);
1230 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1231 if (result == NULL)
1232 goto done;
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03001233 output_start = output = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001234 input = PyByteArray_AS_STRING(input_obj);
1235
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001236 if (vdel.len == 0 && table_chars != NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001237 /* If no deletions are required, use faster code */
1238 for (i = inlen; --i >= 0; ) {
1239 c = Py_CHARMASK(*input++);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001240 *output++ = table_chars[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001241 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001242 goto done;
1243 }
Georg Brandlccc47b62008-12-28 11:44:14 +00001244
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001245 if (table_chars == NULL) {
Georg Brandlccc47b62008-12-28 11:44:14 +00001246 for (i = 0; i < 256; i++)
1247 trans_table[i] = Py_CHARMASK(i);
1248 } else {
1249 for (i = 0; i < 256; i++)
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001250 trans_table[i] = Py_CHARMASK(table_chars[i]);
Georg Brandlccc47b62008-12-28 11:44:14 +00001251 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001252
1253 for (i = 0; i < vdel.len; i++)
1254 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1255
1256 for (i = inlen; --i >= 0; ) {
1257 c = Py_CHARMASK(*input++);
1258 if (trans_table[c] != -1)
Martin Panter1b6c6da2016-08-27 08:35:02 +00001259 *output++ = (char)trans_table[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001260 }
1261 /* Fix the size of the resulting string */
1262 if (inlen > 0)
Christian Heimesc731bbe2013-07-21 02:04:35 +02001263 if (PyByteArray_Resize(result, output - output_start) < 0) {
1264 Py_CLEAR(result);
1265 goto done;
1266 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001267
1268done:
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001269 if (table != NULL)
Georg Brandlccc47b62008-12-28 11:44:14 +00001270 PyBuffer_Release(&vtable);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001271 if (deletechars != NULL)
Martin v. Löwis423be952008-08-13 15:53:07 +00001272 PyBuffer_Release(&vdel);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001273 return result;
1274}
1275
1276
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001277/*[clinic input]
1278
1279@staticmethod
1280bytearray.maketrans
1281
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001282 frm: Py_buffer
1283 to: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001284 /
1285
1286Return a translation table useable for the bytes or bytearray translate method.
1287
1288The returned table will be one where each byte in frm is mapped to the byte at
1289the same position in to.
1290
1291The bytes objects frm and to must be of the same length.
1292[clinic start generated code]*/
1293
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001294static PyObject *
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001295bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001296/*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001297{
1298 return _Py_bytes_maketrans(frm, to);
Georg Brandlabc38772009-04-12 15:51:51 +00001299}
1300
1301
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001302/*[clinic input]
1303bytearray.replace
1304
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001305 old: Py_buffer
1306 new: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001307 count: Py_ssize_t = -1
1308 Maximum number of occurrences to replace.
1309 -1 (the default value) means replace all occurrences.
1310 /
1311
1312Return a copy with all occurrences of substring old replaced by new.
1313
1314If the optional argument count is given, only the first count occurrences are
1315replaced.
1316[clinic start generated code]*/
1317
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001318static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001319bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
1320 Py_buffer *new, Py_ssize_t count)
1321/*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001322{
Serhiy Storchakafb81d3c2016-05-05 09:26:07 +03001323 return stringlib_replace((PyObject *)self,
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001324 (const char *)old->buf, old->len,
1325 (const char *)new->buf, new->len, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001326}
1327
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001328/*[clinic input]
1329bytearray.split
1330
1331 sep: object = None
1332 The delimiter according which to split the bytearray.
1333 None (the default value) means split on ASCII whitespace characters
1334 (space, tab, return, newline, formfeed, vertical tab).
1335 maxsplit: Py_ssize_t = -1
1336 Maximum number of splits to do.
1337 -1 (the default value) means no limit.
1338
1339Return a list of the sections in the bytearray, using sep as the delimiter.
1340[clinic start generated code]*/
1341
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001342static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001343bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
1344 Py_ssize_t maxsplit)
1345/*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001346{
1347 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001348 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001349 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001350 Py_buffer vsub;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001351
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001352 if (maxsplit < 0)
1353 maxsplit = PY_SSIZE_T_MAX;
1354
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001355 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001356 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001357
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001358 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001359 return NULL;
1360 sub = vsub.buf;
1361 n = vsub.len;
1362
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001363 list = stringlib_split(
1364 (PyObject*) self, s, len, sub, n, maxsplit
1365 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001366 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001367 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001368}
1369
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001370/*[clinic input]
1371bytearray.partition
1372
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001373 sep: object
1374 /
1375
1376Partition the bytearray into three parts using the given separator.
1377
1378This will search for the separator sep in the bytearray. If the separator is
1379found, returns a 3-tuple containing the part before the separator, the
1380separator itself, and the part after it.
1381
1382If the separator is not found, returns a 3-tuple containing the original
1383bytearray object and two empty bytearray objects.
1384[clinic start generated code]*/
1385
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001386static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001387bytearray_partition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001388/*[clinic end generated code: output=45d2525ddd35f957 input=86f89223892b70b5]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001389{
1390 PyObject *bytesep, *result;
1391
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001392 bytesep = PyByteArray_FromObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001393 if (! bytesep)
1394 return NULL;
1395
1396 result = stringlib_partition(
1397 (PyObject*) self,
1398 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1399 bytesep,
1400 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1401 );
1402
1403 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001404 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001405}
1406
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001407/*[clinic input]
1408bytearray.rpartition
1409
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001410 sep: object
1411 /
1412
1413Partition the bytes into three parts using the given separator.
1414
1415This will search for the separator sep in the bytearray, starting and the end.
1416If the separator is found, returns a 3-tuple containing the part before the
1417separator, the separator itself, and the part after it.
1418
1419If the separator is not found, returns a 3-tuple containing two empty bytearray
1420objects and the original bytearray object.
1421[clinic start generated code]*/
1422
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001423static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001424bytearray_rpartition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001425/*[clinic end generated code: output=440de3c9426115e8 input=5f4094f2de87c8f3]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001426{
1427 PyObject *bytesep, *result;
1428
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001429 bytesep = PyByteArray_FromObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001430 if (! bytesep)
1431 return NULL;
1432
1433 result = stringlib_rpartition(
1434 (PyObject*) self,
1435 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1436 bytesep,
1437 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1438 );
1439
1440 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001441 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001442}
1443
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001444/*[clinic input]
1445bytearray.rsplit = bytearray.split
1446
1447Return a list of the sections in the bytearray, using sep as the delimiter.
1448
1449Splitting is done starting at the end of the bytearray and working to the front.
1450[clinic start generated code]*/
1451
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001452static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001453bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
1454 Py_ssize_t maxsplit)
1455/*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001456{
1457 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001458 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001459 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001460 Py_buffer vsub;
1461
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001462 if (maxsplit < 0)
1463 maxsplit = PY_SSIZE_T_MAX;
1464
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001465 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001466 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001467
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001468 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001469 return NULL;
1470 sub = vsub.buf;
1471 n = vsub.len;
1472
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001473 list = stringlib_rsplit(
1474 (PyObject*) self, s, len, sub, n, maxsplit
1475 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001476 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001477 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001478}
1479
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001480/*[clinic input]
1481bytearray.reverse
1482
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001483Reverse the order of the values in B in place.
1484[clinic start generated code]*/
1485
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001486static PyObject *
1487bytearray_reverse_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001488/*[clinic end generated code: output=9f7616f29ab309d3 input=543356319fc78557]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001489{
1490 char swap, *head, *tail;
1491 Py_ssize_t i, j, n = Py_SIZE(self);
1492
1493 j = n / 2;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001494 head = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001495 tail = head + n - 1;
1496 for (i = 0; i < j; i++) {
1497 swap = *head;
1498 *head++ = *tail;
1499 *tail-- = swap;
1500 }
1501
1502 Py_RETURN_NONE;
1503}
1504
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001505
1506/*[python input]
1507class bytesvalue_converter(CConverter):
1508 type = 'int'
1509 converter = '_getbytevalue'
1510[python start generated code]*/
1511/*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/
1512
1513
1514/*[clinic input]
1515bytearray.insert
1516
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001517 index: Py_ssize_t
1518 The index where the value is to be inserted.
1519 item: bytesvalue
1520 The item to be inserted.
1521 /
1522
1523Insert a single item into the bytearray before the given index.
1524[clinic start generated code]*/
1525
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001526static PyObject *
1527bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001528/*[clinic end generated code: output=76c775a70e7b07b7 input=b2b5d07e9de6c070]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001529{
1530 Py_ssize_t n = Py_SIZE(self);
1531 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001532
1533 if (n == PY_SSIZE_T_MAX) {
1534 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001535 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001536 return NULL;
1537 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001538 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1539 return NULL;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001540 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001541
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001542 if (index < 0) {
1543 index += n;
1544 if (index < 0)
1545 index = 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001546 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001547 if (index > n)
1548 index = n;
1549 memmove(buf + index + 1, buf + index, n - index);
1550 buf[index] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001551
1552 Py_RETURN_NONE;
1553}
1554
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001555/*[clinic input]
1556bytearray.append
1557
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001558 item: bytesvalue
1559 The item to be appended.
1560 /
1561
1562Append a single item to the end of the bytearray.
1563[clinic start generated code]*/
1564
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001565static PyObject *
1566bytearray_append_impl(PyByteArrayObject *self, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001567/*[clinic end generated code: output=a154e19ed1886cb6 input=20d6bec3d1340593]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001568{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001569 Py_ssize_t n = Py_SIZE(self);
1570
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001571 if (n == PY_SSIZE_T_MAX) {
1572 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001573 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001574 return NULL;
1575 }
1576 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1577 return NULL;
1578
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001579 PyByteArray_AS_STRING(self)[n] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001580
1581 Py_RETURN_NONE;
1582}
1583
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001584/*[clinic input]
1585bytearray.extend
1586
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001587 iterable_of_ints: object
1588 The iterable of items to append.
1589 /
1590
1591Append all the items from the iterator or sequence to the end of the bytearray.
1592[clinic start generated code]*/
1593
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001594static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001595bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001596/*[clinic end generated code: output=98155dbe249170b1 input=c617b3a93249ba28]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001597{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001598 PyObject *it, *item, *bytearray_obj;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001599 Py_ssize_t buf_size = 0, len = 0;
1600 int value;
1601 char *buf;
1602
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001603 /* bytearray_setslice code only accepts something supporting PEP 3118. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001604 if (PyObject_CheckBuffer(iterable_of_ints)) {
1605 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_ints) == -1)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001606 return NULL;
1607
1608 Py_RETURN_NONE;
1609 }
1610
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001611 it = PyObject_GetIter(iterable_of_ints);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001612 if (it == NULL)
1613 return NULL;
1614
Ezio Melotti42da6632011-03-15 05:18:48 +02001615 /* Try to determine the length of the argument. 32 is arbitrary. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001616 buf_size = PyObject_LengthHint(iterable_of_ints, 32);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001617 if (buf_size == -1) {
1618 Py_DECREF(it);
1619 return NULL;
1620 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001621
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001622 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001623 if (bytearray_obj == NULL) {
1624 Py_DECREF(it);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001625 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001626 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001627 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001628
1629 while ((item = PyIter_Next(it)) != NULL) {
1630 if (! _getbytevalue(item, &value)) {
1631 Py_DECREF(item);
1632 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001633 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001634 return NULL;
1635 }
1636 buf[len++] = value;
1637 Py_DECREF(item);
1638
1639 if (len >= buf_size) {
Martin Panter371731e2016-07-18 07:53:13 +00001640 Py_ssize_t addition;
1641 if (len == PY_SSIZE_T_MAX) {
1642 Py_DECREF(it);
1643 Py_DECREF(bytearray_obj);
1644 return PyErr_NoMemory();
1645 }
1646 addition = len >> 1;
1647 if (addition > PY_SSIZE_T_MAX - len - 1)
1648 buf_size = PY_SSIZE_T_MAX;
1649 else
1650 buf_size = len + addition + 1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001651 if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001652 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001653 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001654 return NULL;
1655 }
1656 /* Recompute the `buf' pointer, since the resizing operation may
1657 have invalidated it. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001658 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001659 }
1660 }
1661 Py_DECREF(it);
1662
1663 /* Resize down to exact size. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001664 if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) {
1665 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001666 return NULL;
1667 }
1668
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001669 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) {
1670 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001671 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001672 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001673 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001674
1675 Py_RETURN_NONE;
1676}
1677
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001678/*[clinic input]
1679bytearray.pop
1680
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001681 index: Py_ssize_t = -1
1682 The index from where to remove the item.
1683 -1 (the default value) means remove the last item.
1684 /
1685
1686Remove and return a single item from B.
1687
1688If no index argument is given, will pop the last item.
1689[clinic start generated code]*/
1690
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001691static PyObject *
1692bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001693/*[clinic end generated code: output=e0ccd401f8021da8 input=3591df2d06c0d237]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001694{
1695 int value;
1696 Py_ssize_t n = Py_SIZE(self);
1697 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001698
1699 if (n == 0) {
Eli Bendersky1bc4f192011-03-04 04:55:25 +00001700 PyErr_SetString(PyExc_IndexError,
1701 "pop from empty bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001702 return NULL;
1703 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001704 if (index < 0)
1705 index += Py_SIZE(self);
1706 if (index < 0 || index >= Py_SIZE(self)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001707 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1708 return NULL;
1709 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001710 if (!_canresize(self))
1711 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001712
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001713 buf = PyByteArray_AS_STRING(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001714 value = buf[index];
1715 memmove(buf + index, buf + index + 1, n - index);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001716 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1717 return NULL;
1718
Mark Dickinson54a3db92009-09-06 10:19:23 +00001719 return PyLong_FromLong((unsigned char)value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001720}
1721
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001722/*[clinic input]
1723bytearray.remove
1724
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001725 value: bytesvalue
1726 The value to remove.
1727 /
1728
1729Remove the first occurrence of a value in the bytearray.
1730[clinic start generated code]*/
1731
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001732static PyObject *
1733bytearray_remove_impl(PyByteArrayObject *self, int value)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001734/*[clinic end generated code: output=d659e37866709c13 input=121831240cd51ddf]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001735{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001736 Py_ssize_t where, n = Py_SIZE(self);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001737 char *buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001738
Serhiy Storchaka4b234942016-05-16 22:24:03 +03001739 where = stringlib_find_char(buf, n, value);
1740 if (where < 0) {
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001741 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001742 return NULL;
1743 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001744 if (!_canresize(self))
1745 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001746
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001747 memmove(buf + where, buf + where + 1, n - where);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001748 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1749 return NULL;
1750
1751 Py_RETURN_NONE;
1752}
1753
1754/* XXX These two helpers could be optimized if argsize == 1 */
1755
1756static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001757lstrip_helper(const char *myptr, Py_ssize_t mysize,
1758 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001759{
1760 Py_ssize_t i = 0;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001761 while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001762 i++;
1763 return i;
1764}
1765
1766static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001767rstrip_helper(const char *myptr, Py_ssize_t mysize,
1768 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001769{
1770 Py_ssize_t i = mysize - 1;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001771 while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001772 i--;
1773 return i + 1;
1774}
1775
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001776/*[clinic input]
1777bytearray.strip
1778
1779 bytes: object = None
1780 /
1781
1782Strip leading and trailing bytes contained in the argument.
1783
1784If the argument is omitted or None, strip leading and trailing ASCII whitespace.
1785[clinic start generated code]*/
1786
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001787static PyObject *
1788bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001789/*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001790{
1791 Py_ssize_t left, right, mysize, byteslen;
1792 char *myptr, *bytesptr;
1793 Py_buffer vbytes;
1794
1795 if (bytes == Py_None) {
1796 bytesptr = "\t\n\r\f\v ";
1797 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001798 }
1799 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001800 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001801 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001802 bytesptr = (char *) vbytes.buf;
1803 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001804 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001805 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001806 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001807 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001808 if (left == mysize)
1809 right = left;
1810 else
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001811 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1812 if (bytes != Py_None)
1813 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001814 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001815}
1816
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001817/*[clinic input]
1818bytearray.lstrip
1819
1820 bytes: object = None
1821 /
1822
1823Strip leading bytes contained in the argument.
1824
1825If the argument is omitted or None, strip leading ASCII whitespace.
1826[clinic start generated code]*/
1827
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001828static PyObject *
1829bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001830/*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001831{
1832 Py_ssize_t left, right, mysize, byteslen;
1833 char *myptr, *bytesptr;
1834 Py_buffer vbytes;
1835
1836 if (bytes == Py_None) {
1837 bytesptr = "\t\n\r\f\v ";
1838 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001839 }
1840 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001841 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001842 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001843 bytesptr = (char *) vbytes.buf;
1844 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001845 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001846 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001847 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001848 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001849 right = mysize;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001850 if (bytes != Py_None)
1851 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001852 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001853}
1854
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001855/*[clinic input]
1856bytearray.rstrip
1857
1858 bytes: object = None
1859 /
1860
1861Strip trailing bytes contained in the argument.
1862
1863If the argument is omitted or None, strip trailing ASCII whitespace.
1864[clinic start generated code]*/
1865
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001866static PyObject *
1867bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001868/*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001869{
1870 Py_ssize_t right, mysize, byteslen;
1871 char *myptr, *bytesptr;
1872 Py_buffer vbytes;
1873
1874 if (bytes == Py_None) {
1875 bytesptr = "\t\n\r\f\v ";
1876 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001877 }
1878 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001879 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001880 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001881 bytesptr = (char *) vbytes.buf;
1882 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001883 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001884 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001885 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001886 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1887 if (bytes != Py_None)
1888 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001889 return PyByteArray_FromStringAndSize(myptr, right);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001890}
1891
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001892/*[clinic input]
1893bytearray.decode
1894
1895 encoding: str(c_default="NULL") = 'utf-8'
1896 The encoding with which to decode the bytearray.
1897 errors: str(c_default="NULL") = 'strict'
1898 The error handling scheme to use for the handling of decoding errors.
1899 The default is 'strict' meaning that decoding errors raise a
1900 UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
1901 as well as any other name registered with codecs.register_error that
1902 can handle UnicodeDecodeErrors.
1903
1904Decode the bytearray using the codec registered for encoding.
1905[clinic start generated code]*/
1906
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001907static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001908bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
1909 const char *errors)
1910/*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001911{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001912 if (encoding == NULL)
1913 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis0efea322014-07-27 17:29:17 +02001914 return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001915}
1916
1917PyDoc_STRVAR(alloc_doc,
1918"B.__alloc__() -> int\n\
1919\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00001920Return the number of bytes actually allocated.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001921
1922static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001923bytearray_alloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001924{
1925 return PyLong_FromSsize_t(self->ob_alloc);
1926}
1927
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001928/*[clinic input]
1929bytearray.join
1930
1931 iterable_of_bytes: object
1932 /
1933
1934Concatenate any number of bytes/bytearray objects.
1935
1936The bytearray whose method is called is inserted in between each pair.
1937
1938The result is returned as a new bytearray object.
1939[clinic start generated code]*/
1940
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001941static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001942bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001943/*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001944{
Martin v. Löwis0efea322014-07-27 17:29:17 +02001945 return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001946}
1947
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001948/*[clinic input]
1949bytearray.splitlines
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001950
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +03001951 keepends: int(c_default="0") = False
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001952
1953Return a list of the lines in the bytearray, breaking at line boundaries.
1954
1955Line breaks are not included in the resulting list unless keepends is given and
1956true.
1957[clinic start generated code]*/
1958
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001959static PyObject *
1960bytearray_splitlines_impl(PyByteArrayObject *self, int keepends)
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +03001961/*[clinic end generated code: output=4223c94b895f6ad9 input=8ccade941e5ea0bd]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001962{
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001963 return stringlib_splitlines(
1964 (PyObject*) self, PyByteArray_AS_STRING(self),
1965 PyByteArray_GET_SIZE(self), keepends
1966 );
1967}
1968
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001969/*[clinic input]
1970@classmethod
1971bytearray.fromhex
1972
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001973 string: unicode
1974 /
1975
1976Create a bytearray object from a string of hexadecimal numbers.
1977
1978Spaces between two numbers are accepted.
1979Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')
1980[clinic start generated code]*/
1981
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001982static PyObject *
Serhiy Storchaka0855e702016-07-01 17:22:31 +03001983bytearray_fromhex_impl(PyTypeObject *type, PyObject *string)
1984/*[clinic end generated code: output=8f0f0b6d30fb3ba0 input=f033a16d1fb21f48]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001985{
Serhiy Storchaka0855e702016-07-01 17:22:31 +03001986 PyObject *result = _PyBytes_FromHex(string, type == &PyByteArray_Type);
1987 if (type != &PyByteArray_Type && result != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001988 Py_SETREF(result, PyObject_CallFunctionObjArgs((PyObject *)type,
1989 result, NULL));
Serhiy Storchaka0855e702016-07-01 17:22:31 +03001990 }
1991 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001992}
1993
Gregory P. Smith8cb65692015-04-25 23:22:26 +00001994PyDoc_STRVAR(hex__doc__,
1995"B.hex() -> string\n\
1996\n\
1997Create a string of hexadecimal numbers from a bytearray object.\n\
1998Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'.");
1999
2000static PyObject *
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002001bytearray_hex(PyBytesObject *self)
2002{
2003 char* argbuf = PyByteArray_AS_STRING(self);
2004 Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
2005 return _Py_strhex(argbuf, arglen);
2006}
2007
2008static PyObject *
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002009_common_reduce(PyByteArrayObject *self, int proto)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002010{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002011 PyObject *dict;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002012 _Py_IDENTIFIER(__dict__);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002013 char *buf;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002014
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002015 dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002016 if (dict == NULL) {
2017 PyErr_Clear();
2018 dict = Py_None;
2019 Py_INCREF(dict);
2020 }
2021
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002022 buf = PyByteArray_AS_STRING(self);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002023 if (proto < 3) {
2024 /* use str based reduction for backwards compatibility with Python 2.x */
2025 PyObject *latin1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002026 if (Py_SIZE(self))
2027 latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002028 else
2029 latin1 = PyUnicode_FromString("");
2030 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
2031 }
2032 else {
2033 /* use more efficient byte based reduction */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002034 if (Py_SIZE(self)) {
2035 return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002036 }
2037 else {
2038 return Py_BuildValue("(O()N)", Py_TYPE(self), dict);
2039 }
2040 }
2041}
2042
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002043/*[clinic input]
2044bytearray.__reduce__ as bytearray_reduce
2045
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002046Return state information for pickling.
2047[clinic start generated code]*/
2048
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002049static PyObject *
2050bytearray_reduce_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002051/*[clinic end generated code: output=52bf304086464cab input=44b5737ada62dd3f]*/
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002052{
2053 return _common_reduce(self, 2);
2054}
2055
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002056/*[clinic input]
2057bytearray.__reduce_ex__ as bytearray_reduce_ex
2058
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002059 proto: int = 0
2060 /
2061
2062Return state information for pickling.
2063[clinic start generated code]*/
2064
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002065static PyObject *
2066bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002067/*[clinic end generated code: output=52eac33377197520 input=f129bc1a1aa151ee]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002068{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002069 return _common_reduce(self, proto);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002070}
2071
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002072/*[clinic input]
2073bytearray.__sizeof__ as bytearray_sizeof
2074
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002075Returns the size of the bytearray object in memory, in bytes.
2076[clinic start generated code]*/
2077
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002078static PyObject *
2079bytearray_sizeof_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002080/*[clinic end generated code: output=738abdd17951c427 input=e27320fd98a4bc5a]*/
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002081{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002082 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002083
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002084 res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002085 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002086}
2087
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002088static PySequenceMethods bytearray_as_sequence = {
2089 (lenfunc)bytearray_length, /* sq_length */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002090 (binaryfunc)PyByteArray_Concat, /* sq_concat */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002091 (ssizeargfunc)bytearray_repeat, /* sq_repeat */
2092 (ssizeargfunc)bytearray_getitem, /* sq_item */
2093 0, /* sq_slice */
2094 (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */
2095 0, /* sq_ass_slice */
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002096 (objobjproc)bytearray_contains, /* sq_contains */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002097 (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */
2098 (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002099};
2100
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002101static PyMappingMethods bytearray_as_mapping = {
2102 (lenfunc)bytearray_length,
2103 (binaryfunc)bytearray_subscript,
2104 (objobjargproc)bytearray_ass_subscript,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002105};
2106
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002107static PyBufferProcs bytearray_as_buffer = {
2108 (getbufferproc)bytearray_getbuffer,
2109 (releasebufferproc)bytearray_releasebuffer,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002110};
2111
2112static PyMethodDef
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002113bytearray_methods[] = {
2114 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002115 BYTEARRAY_REDUCE_METHODDEF
2116 BYTEARRAY_REDUCE_EX_METHODDEF
2117 BYTEARRAY_SIZEOF_METHODDEF
2118 BYTEARRAY_APPEND_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002119 {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS,
2120 _Py_capitalize__doc__},
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002121 {"center", (PyCFunction)stringlib_center, METH_VARARGS, _Py_center__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002122 BYTEARRAY_CLEAR_METHODDEF
2123 BYTEARRAY_COPY_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002124 {"count", (PyCFunction)bytearray_count, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002125 _Py_count__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002126 BYTEARRAY_DECODE_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002127 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002128 _Py_endswith__doc__},
Ezio Melotti745d54d2013-11-16 19:10:57 +02002129 {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002130 _Py_expandtabs__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002131 BYTEARRAY_EXTEND_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002132 {"find", (PyCFunction)bytearray_find, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002133 _Py_find__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002134 BYTEARRAY_FROMHEX_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002135 {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__},
2136 {"index", (PyCFunction)bytearray_index, METH_VARARGS, _Py_index__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002137 BYTEARRAY_INSERT_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002138 {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS,
2139 _Py_isalnum__doc__},
2140 {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS,
2141 _Py_isalpha__doc__},
2142 {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS,
2143 _Py_isdigit__doc__},
2144 {"islower", (PyCFunction)stringlib_islower, METH_NOARGS,
2145 _Py_islower__doc__},
2146 {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS,
2147 _Py_isspace__doc__},
2148 {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS,
2149 _Py_istitle__doc__},
2150 {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS,
2151 _Py_isupper__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002152 BYTEARRAY_JOIN_METHODDEF
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002153 {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, _Py_ljust__doc__},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002154 {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002155 BYTEARRAY_LSTRIP_METHODDEF
2156 BYTEARRAY_MAKETRANS_METHODDEF
2157 BYTEARRAY_PARTITION_METHODDEF
2158 BYTEARRAY_POP_METHODDEF
2159 BYTEARRAY_REMOVE_METHODDEF
2160 BYTEARRAY_REPLACE_METHODDEF
2161 BYTEARRAY_REVERSE_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002162 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__},
2163 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__},
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002164 {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, _Py_rjust__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002165 BYTEARRAY_RPARTITION_METHODDEF
2166 BYTEARRAY_RSPLIT_METHODDEF
2167 BYTEARRAY_RSTRIP_METHODDEF
2168 BYTEARRAY_SPLIT_METHODDEF
2169 BYTEARRAY_SPLITLINES_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002170 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002171 _Py_startswith__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002172 BYTEARRAY_STRIP_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002173 {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS,
2174 _Py_swapcase__doc__},
2175 {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002176 BYTEARRAY_TRANSLATE_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002177 {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__},
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002178 {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, _Py_zfill__doc__},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002179 {NULL}
2180};
2181
Ethan Furmanb95b5612015-01-23 20:05:18 -08002182static PyObject *
2183bytearray_mod(PyObject *v, PyObject *w)
2184{
2185 if (!PyByteArray_Check(v))
2186 Py_RETURN_NOTIMPLEMENTED;
Berker Peksag43de36d2016-04-16 01:20:47 +03002187 return _PyBytes_FormatEx(PyByteArray_AS_STRING(v), PyByteArray_GET_SIZE(v), w, 1);
Ethan Furmanb95b5612015-01-23 20:05:18 -08002188}
2189
2190static PyNumberMethods bytearray_as_number = {
2191 0, /*nb_add*/
2192 0, /*nb_subtract*/
2193 0, /*nb_multiply*/
2194 bytearray_mod, /*nb_remainder*/
2195};
2196
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002197PyDoc_STRVAR(bytearray_doc,
Georg Brandl17cb8a82008-05-30 08:20:09 +00002198"bytearray(iterable_of_ints) -> bytearray\n\
2199bytearray(string, encoding[, errors]) -> bytearray\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002200bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\
2201bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\
2202bytearray() -> empty bytes array\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002203\n\
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03002204Construct a mutable bytearray object from:\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002205 - an iterable yielding integers in range(256)\n\
2206 - a text string encoded using the specified encoding\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002207 - a bytes or a buffer object\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002208 - any object implementing the buffer API.\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002209 - an integer");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002210
2211
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002212static PyObject *bytearray_iter(PyObject *seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002213
2214PyTypeObject PyByteArray_Type = {
2215 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2216 "bytearray",
2217 sizeof(PyByteArrayObject),
2218 0,
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002219 (destructor)bytearray_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002220 0, /* tp_print */
2221 0, /* tp_getattr */
2222 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002223 0, /* tp_reserved */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002224 (reprfunc)bytearray_repr, /* tp_repr */
Ethan Furmanb95b5612015-01-23 20:05:18 -08002225 &bytearray_as_number, /* tp_as_number */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002226 &bytearray_as_sequence, /* tp_as_sequence */
2227 &bytearray_as_mapping, /* tp_as_mapping */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002228 0, /* tp_hash */
2229 0, /* tp_call */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002230 bytearray_str, /* tp_str */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002231 PyObject_GenericGetAttr, /* tp_getattro */
2232 0, /* tp_setattro */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002233 &bytearray_as_buffer, /* tp_as_buffer */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002234 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002235 bytearray_doc, /* tp_doc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002236 0, /* tp_traverse */
2237 0, /* tp_clear */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002238 (richcmpfunc)bytearray_richcompare, /* tp_richcompare */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002239 0, /* tp_weaklistoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002240 bytearray_iter, /* tp_iter */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002241 0, /* tp_iternext */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002242 bytearray_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002243 0, /* tp_members */
2244 0, /* tp_getset */
2245 0, /* tp_base */
2246 0, /* tp_dict */
2247 0, /* tp_descr_get */
2248 0, /* tp_descr_set */
2249 0, /* tp_dictoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002250 (initproc)bytearray_init, /* tp_init */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002251 PyType_GenericAlloc, /* tp_alloc */
2252 PyType_GenericNew, /* tp_new */
2253 PyObject_Del, /* tp_free */
2254};
2255
2256/*********************** Bytes Iterator ****************************/
2257
2258typedef struct {
2259 PyObject_HEAD
2260 Py_ssize_t it_index;
2261 PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
2262} bytesiterobject;
2263
2264static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002265bytearrayiter_dealloc(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002266{
2267 _PyObject_GC_UNTRACK(it);
2268 Py_XDECREF(it->it_seq);
2269 PyObject_GC_Del(it);
2270}
2271
2272static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002273bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002274{
2275 Py_VISIT(it->it_seq);
2276 return 0;
2277}
2278
2279static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002280bytearrayiter_next(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002281{
2282 PyByteArrayObject *seq;
2283 PyObject *item;
2284
2285 assert(it != NULL);
2286 seq = it->it_seq;
2287 if (seq == NULL)
2288 return NULL;
2289 assert(PyByteArray_Check(seq));
2290
2291 if (it->it_index < PyByteArray_GET_SIZE(seq)) {
2292 item = PyLong_FromLong(
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002293 (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002294 if (item != NULL)
2295 ++it->it_index;
2296 return item;
2297 }
2298
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002299 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03002300 Py_DECREF(seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002301 return NULL;
2302}
2303
2304static PyObject *
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002305bytearrayiter_length_hint(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002306{
2307 Py_ssize_t len = 0;
Serhiy Storchakaaf658722016-07-03 14:41:36 +03002308 if (it->it_seq) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002309 len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
Serhiy Storchakaaf658722016-07-03 14:41:36 +03002310 if (len < 0) {
2311 len = 0;
2312 }
2313 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002314 return PyLong_FromSsize_t(len);
2315}
2316
2317PyDoc_STRVAR(length_hint_doc,
2318 "Private method returning an estimate of len(list(it)).");
2319
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002320static PyObject *
2321bytearrayiter_reduce(bytesiterobject *it)
2322{
2323 if (it->it_seq != NULL) {
Antoine Pitroua7013882012-04-05 00:04:20 +02002324 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002325 it->it_seq, it->it_index);
2326 } else {
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02002327 return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002328 }
2329}
2330
2331static PyObject *
2332bytearrayiter_setstate(bytesiterobject *it, PyObject *state)
2333{
2334 Py_ssize_t index = PyLong_AsSsize_t(state);
2335 if (index == -1 && PyErr_Occurred())
2336 return NULL;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00002337 if (it->it_seq != NULL) {
2338 if (index < 0)
2339 index = 0;
2340 else if (index > PyByteArray_GET_SIZE(it->it_seq))
2341 index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */
2342 it->it_index = index;
2343 }
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002344 Py_RETURN_NONE;
2345}
2346
2347PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
2348
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002349static PyMethodDef bytearrayiter_methods[] = {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002350 {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002351 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002352 {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002353 bytearray_reduce__doc__},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002354 {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
2355 setstate_doc},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002356 {NULL, NULL} /* sentinel */
2357};
2358
2359PyTypeObject PyByteArrayIter_Type = {
2360 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2361 "bytearray_iterator", /* tp_name */
2362 sizeof(bytesiterobject), /* tp_basicsize */
2363 0, /* tp_itemsize */
2364 /* methods */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002365 (destructor)bytearrayiter_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002366 0, /* tp_print */
2367 0, /* tp_getattr */
2368 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002369 0, /* tp_reserved */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002370 0, /* tp_repr */
2371 0, /* tp_as_number */
2372 0, /* tp_as_sequence */
2373 0, /* tp_as_mapping */
2374 0, /* tp_hash */
2375 0, /* tp_call */
2376 0, /* tp_str */
2377 PyObject_GenericGetAttr, /* tp_getattro */
2378 0, /* tp_setattro */
2379 0, /* tp_as_buffer */
2380 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
2381 0, /* tp_doc */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002382 (traverseproc)bytearrayiter_traverse, /* tp_traverse */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002383 0, /* tp_clear */
2384 0, /* tp_richcompare */
2385 0, /* tp_weaklistoffset */
2386 PyObject_SelfIter, /* tp_iter */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002387 (iternextfunc)bytearrayiter_next, /* tp_iternext */
2388 bytearrayiter_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002389 0,
2390};
2391
2392static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002393bytearray_iter(PyObject *seq)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002394{
2395 bytesiterobject *it;
2396
2397 if (!PyByteArray_Check(seq)) {
2398 PyErr_BadInternalCall();
2399 return NULL;
2400 }
2401 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
2402 if (it == NULL)
2403 return NULL;
2404 it->it_index = 0;
2405 Py_INCREF(seq);
2406 it->it_seq = (PyByteArrayObject *)seq;
2407 _PyObject_GC_TRACK(it);
2408 return (PyObject *)it;
2409}