blob: 6672136193442ed753b5d8c843f83df122b739cd [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"
Victor Stinnerbcda8f12018-11-21 22:27:47 +01005#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01006#include "pycore_pymem.h"
7#include "pycore_pystate.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00008#include "structmember.h"
9#include "bytes_methods.h"
Ethan Furmanb95b5612015-01-23 20:05:18 -080010#include "bytesobject.h"
Gregory P. Smith8cb65692015-04-25 23:22:26 +000011#include "pystrhex.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +000012
Martin v. Löwis7252a6e2014-07-27 16:25:09 +020013/*[clinic input]
14class bytearray "PyByteArrayObject *" "&PyByteArray_Type"
15[clinic start generated code]*/
16/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/
17
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000018char _PyByteArray_empty_string[] = "";
Christian Heimes2c9c7a52008-05-26 13:42:13 +000019
Christian Heimes2c9c7a52008-05-26 13:42:13 +000020/* end nullbytes support */
21
22/* Helpers */
23
24static int
25_getbytevalue(PyObject* arg, int *value)
26{
27 long face_value;
28
29 if (PyLong_Check(arg)) {
30 face_value = PyLong_AsLong(arg);
Georg Brandl9a54d7c2008-07-16 23:15:30 +000031 } else {
32 PyObject *index = PyNumber_Index(arg);
33 if (index == NULL) {
Mark Dickinson10de93a2010-07-09 19:25:48 +000034 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000035 return 0;
36 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +000037 face_value = PyLong_AsLong(index);
38 Py_DECREF(index);
39 }
40
41 if (face_value < 0 || face_value >= 256) {
42 /* this includes the OverflowError in case the long is too large */
43 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
Mark Dickinson10de93a2010-07-09 19:25:48 +000044 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000045 return 0;
46 }
47
48 *value = face_value;
49 return 1;
50}
51
52static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +000053bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000054{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000055 void *ptr;
56 if (view == NULL) {
Stefan Krah5178d912015-02-03 16:57:21 +010057 PyErr_SetString(PyExc_BufferError,
58 "bytearray_getbuffer: view==NULL argument is obsolete");
59 return -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000060 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000061 ptr = (void *) PyByteArray_AS_STRING(obj);
Stefan Krah5178d912015-02-03 16:57:21 +010062 /* cannot fail if view != NULL and readonly == 0 */
63 (void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
64 obj->ob_exports++;
65 return 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000066}
67
68static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +000069bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000070{
71 obj->ob_exports--;
72}
73
Antoine Pitrou5504e892008-12-06 21:27:53 +000074static int
75_canresize(PyByteArrayObject *self)
76{
77 if (self->ob_exports > 0) {
78 PyErr_SetString(PyExc_BufferError,
79 "Existing exports of data: object cannot be re-sized");
80 return 0;
81 }
82 return 1;
83}
84
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030085#include "clinic/bytearrayobject.c.h"
86
Christian Heimes2c9c7a52008-05-26 13:42:13 +000087/* Direct API functions */
88
89PyObject *
90PyByteArray_FromObject(PyObject *input)
91{
Victor Stinnerde4ae3d2016-12-04 22:59:09 +010092 return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type,
93 input, NULL);
Christian Heimes2c9c7a52008-05-26 13:42:13 +000094}
95
Serhiy Storchakaa2314282017-10-29 02:11:54 +030096static PyObject *
97_PyByteArray_FromBufferObject(PyObject *obj)
98{
99 PyObject *result;
100 Py_buffer view;
101
102 if (PyObject_GetBuffer(obj, &view, PyBUF_FULL_RO) < 0) {
103 return NULL;
104 }
105 result = PyByteArray_FromStringAndSize(NULL, view.len);
106 if (result != NULL &&
107 PyBuffer_ToContiguous(PyByteArray_AS_STRING(result),
108 &view, view.len, 'C') < 0)
109 {
110 Py_CLEAR(result);
111 }
112 PyBuffer_Release(&view);
113 return result;
114}
115
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000116PyObject *
117PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
118{
119 PyByteArrayObject *new;
120 Py_ssize_t alloc;
121
122 if (size < 0) {
123 PyErr_SetString(PyExc_SystemError,
124 "Negative size passed to PyByteArray_FromStringAndSize");
125 return NULL;
126 }
127
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000128 /* Prevent buffer overflow when setting alloc to size+1. */
129 if (size == PY_SSIZE_T_MAX) {
130 return PyErr_NoMemory();
131 }
132
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000133 new = PyObject_New(PyByteArrayObject, &PyByteArray_Type);
134 if (new == NULL)
135 return NULL;
136
137 if (size == 0) {
138 new->ob_bytes = NULL;
139 alloc = 0;
140 }
141 else {
142 alloc = size + 1;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100143 new->ob_bytes = PyObject_Malloc(alloc);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000144 if (new->ob_bytes == NULL) {
145 Py_DECREF(new);
146 return PyErr_NoMemory();
147 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +0000148 if (bytes != NULL && size > 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000149 memcpy(new->ob_bytes, bytes, size);
150 new->ob_bytes[size] = '\0'; /* Trailing null byte */
151 }
152 Py_SIZE(new) = size;
153 new->ob_alloc = alloc;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200154 new->ob_start = new->ob_bytes;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000155 new->ob_exports = 0;
156
157 return (PyObject *)new;
158}
159
160Py_ssize_t
161PyByteArray_Size(PyObject *self)
162{
163 assert(self != NULL);
164 assert(PyByteArray_Check(self));
165
166 return PyByteArray_GET_SIZE(self);
167}
168
169char *
170PyByteArray_AsString(PyObject *self)
171{
172 assert(self != NULL);
173 assert(PyByteArray_Check(self));
174
175 return PyByteArray_AS_STRING(self);
176}
177
178int
Antoine Pitroucc231542014-11-02 18:40:09 +0100179PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000180{
181 void *sval;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200182 PyByteArrayObject *obj = ((PyByteArrayObject *)self);
Antoine Pitroucc231542014-11-02 18:40:09 +0100183 /* All computations are done unsigned to avoid integer overflows
184 (see issue #22335). */
185 size_t alloc = (size_t) obj->ob_alloc;
186 size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes);
187 size_t size = (size_t) requested_size;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000188
189 assert(self != NULL);
190 assert(PyByteArray_Check(self));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200191 assert(logical_offset <= alloc);
Antoine Pitroucc231542014-11-02 18:40:09 +0100192 assert(requested_size >= 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000193
Antoine Pitroucc231542014-11-02 18:40:09 +0100194 if (requested_size == Py_SIZE(self)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000195 return 0;
196 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200197 if (!_canresize(obj)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000198 return -1;
199 }
200
Antoine Pitrou25454112015-05-19 20:52:27 +0200201 if (size + logical_offset + 1 <= alloc) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200202 /* Current buffer is large enough to host the requested size,
203 decide on a strategy. */
204 if (size < alloc / 2) {
205 /* Major downsize; resize down to exact size */
206 alloc = size + 1;
207 }
208 else {
209 /* Minor downsize; quick exit */
210 Py_SIZE(self) = size;
211 PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */
212 return 0;
213 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000214 }
215 else {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200216 /* Need growing, decide on a strategy */
217 if (size <= alloc * 1.125) {
218 /* Moderate upsize; overallocate similar to list_resize() */
219 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
220 }
221 else {
222 /* Major upsize; resize up to exact size */
223 alloc = size + 1;
224 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000225 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100226 if (alloc > PY_SSIZE_T_MAX) {
227 PyErr_NoMemory();
228 return -1;
229 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000230
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200231 if (logical_offset > 0) {
232 sval = PyObject_Malloc(alloc);
233 if (sval == NULL) {
234 PyErr_NoMemory();
235 return -1;
236 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100237 memcpy(sval, PyByteArray_AS_STRING(self),
Stefan Krahdce65022017-08-25 20:12:05 +0200238 Py_MIN((size_t)requested_size, (size_t)Py_SIZE(self)));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200239 PyObject_Free(obj->ob_bytes);
240 }
241 else {
242 sval = PyObject_Realloc(obj->ob_bytes, alloc);
243 if (sval == NULL) {
244 PyErr_NoMemory();
245 return -1;
246 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000247 }
248
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200249 obj->ob_bytes = obj->ob_start = sval;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000250 Py_SIZE(self) = size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200251 obj->ob_alloc = alloc;
252 obj->ob_bytes[size] = '\0'; /* Trailing null byte */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000253
254 return 0;
255}
256
257PyObject *
258PyByteArray_Concat(PyObject *a, PyObject *b)
259{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000260 Py_buffer va, vb;
261 PyByteArrayObject *result = NULL;
262
263 va.len = -1;
264 vb.len = -1;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200265 if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 ||
266 PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000267 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
Serhiy Storchaka6b5a9ec2017-03-19 19:47:02 +0200268 Py_TYPE(b)->tp_name, Py_TYPE(a)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000269 goto done;
270 }
271
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300272 if (va.len > PY_SSIZE_T_MAX - vb.len) {
273 PyErr_NoMemory();
274 goto done;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000275 }
276
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300277 result = (PyByteArrayObject *) \
278 PyByteArray_FromStringAndSize(NULL, va.len + vb.len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000279 if (result != NULL) {
280 memcpy(result->ob_bytes, va.buf, va.len);
281 memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
282 }
283
284 done:
285 if (va.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000286 PyBuffer_Release(&va);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000287 if (vb.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000288 PyBuffer_Release(&vb);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000289 return (PyObject *)result;
290}
291
292/* Functions stuffed into the type object */
293
294static Py_ssize_t
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000295bytearray_length(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000296{
297 return Py_SIZE(self);
298}
299
300static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000301bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000302{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000303 Py_ssize_t size;
304 Py_buffer vo;
305
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200306 if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000307 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
308 Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name);
309 return NULL;
310 }
311
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300312 size = Py_SIZE(self);
313 if (size > PY_SSIZE_T_MAX - vo.len) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000314 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000315 return PyErr_NoMemory();
316 }
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300317 if (PyByteArray_Resize((PyObject *)self, size + vo.len) < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000318 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000319 return NULL;
320 }
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300321 memcpy(PyByteArray_AS_STRING(self) + size, vo.buf, vo.len);
Martin v. Löwis423be952008-08-13 15:53:07 +0000322 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000323 Py_INCREF(self);
324 return (PyObject *)self;
325}
326
327static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000328bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000329{
330 PyByteArrayObject *result;
331 Py_ssize_t mysize;
332 Py_ssize_t size;
333
334 if (count < 0)
335 count = 0;
336 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000337 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000338 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000339 size = mysize * count;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000340 result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size);
341 if (result != NULL && size != 0) {
342 if (mysize == 1)
343 memset(result->ob_bytes, self->ob_bytes[0], size);
344 else {
345 Py_ssize_t i;
346 for (i = 0; i < count; i++)
347 memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize);
348 }
349 }
350 return (PyObject *)result;
351}
352
353static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000354bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000355{
356 Py_ssize_t mysize;
357 Py_ssize_t size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200358 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000359
360 if (count < 0)
361 count = 0;
362 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000363 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000364 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000365 size = mysize * count;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200366 if (PyByteArray_Resize((PyObject *)self, size) < 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000367 return NULL;
368
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200369 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000370 if (mysize == 1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200371 memset(buf, buf[0], size);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000372 else {
373 Py_ssize_t i;
374 for (i = 1; i < count; i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200375 memcpy(buf + i*mysize, buf, mysize);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000376 }
377
378 Py_INCREF(self);
379 return (PyObject *)self;
380}
381
382static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000383bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000384{
385 if (i < 0)
386 i += Py_SIZE(self);
387 if (i < 0 || i >= Py_SIZE(self)) {
388 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
389 return NULL;
390 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200391 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000392}
393
394static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000395bytearray_subscript(PyByteArrayObject *self, PyObject *index)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000396{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000397 if (PyIndex_Check(index)) {
398 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000399
400 if (i == -1 && PyErr_Occurred())
401 return NULL;
402
403 if (i < 0)
404 i += PyByteArray_GET_SIZE(self);
405
406 if (i < 0 || i >= Py_SIZE(self)) {
407 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
408 return NULL;
409 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200410 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000411 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000412 else if (PySlice_Check(index)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000413 Py_ssize_t start, stop, step, slicelength, cur, i;
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300414 if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000415 return NULL;
416 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300417 slicelength = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self),
418 &start, &stop, step);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000419
420 if (slicelength <= 0)
421 return PyByteArray_FromStringAndSize("", 0);
422 else if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200423 return PyByteArray_FromStringAndSize(
424 PyByteArray_AS_STRING(self) + start, slicelength);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000425 }
426 else {
427 char *source_buf = PyByteArray_AS_STRING(self);
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000428 char *result_buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000429 PyObject *result;
430
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000431 result = PyByteArray_FromStringAndSize(NULL, slicelength);
432 if (result == NULL)
433 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000434
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000435 result_buf = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000436 for (cur = start, i = 0; i < slicelength;
437 cur += step, i++) {
438 result_buf[i] = source_buf[cur];
439 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000440 return result;
441 }
442 }
443 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400444 PyErr_Format(PyExc_TypeError,
445 "bytearray indices must be integers or slices, not %.200s",
446 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000447 return NULL;
448 }
449}
450
451static int
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200452bytearray_setslice_linear(PyByteArrayObject *self,
453 Py_ssize_t lo, Py_ssize_t hi,
454 char *bytes, Py_ssize_t bytes_len)
455{
456 Py_ssize_t avail = hi - lo;
457 char *buf = PyByteArray_AS_STRING(self);
458 Py_ssize_t growth = bytes_len - avail;
Victor Stinner84557232013-11-21 12:29:51 +0100459 int res = 0;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200460 assert(avail >= 0);
461
Victor Stinner84557232013-11-21 12:29:51 +0100462 if (growth < 0) {
463 if (!_canresize(self))
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200464 return -1;
Victor Stinner84557232013-11-21 12:29:51 +0100465
466 if (lo == 0) {
467 /* Shrink the buffer by advancing its logical start */
468 self->ob_start -= growth;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200469 /*
Victor Stinner84557232013-11-21 12:29:51 +0100470 0 lo hi old_size
471 | |<----avail----->|<-----tail------>|
472 | |<-bytes_len->|<-----tail------>|
473 0 new_lo new_hi new_size
474 */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200475 }
Victor Stinner84557232013-11-21 12:29:51 +0100476 else {
477 /*
478 0 lo hi old_size
479 | |<----avail----->|<-----tomove------>|
480 | |<-bytes_len->|<-----tomove------>|
481 0 lo new_hi new_size
482 */
483 memmove(buf + lo + bytes_len, buf + hi,
484 Py_SIZE(self) - hi);
485 }
486 if (PyByteArray_Resize((PyObject *)self,
487 Py_SIZE(self) + growth) < 0) {
488 /* Issue #19578: Handling the memory allocation failure here is
489 tricky here because the bytearray object has already been
490 modified. Depending on growth and lo, the behaviour is
491 different.
492
493 If growth < 0 and lo != 0, the operation is completed, but a
494 MemoryError is still raised and the memory block is not
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700495 shrunk. Otherwise, the bytearray is restored in its previous
Victor Stinner84557232013-11-21 12:29:51 +0100496 state and a MemoryError is raised. */
497 if (lo == 0) {
498 self->ob_start += growth;
499 return -1;
500 }
501 /* memmove() removed bytes, the bytearray object cannot be
502 restored in its previous state. */
503 Py_SIZE(self) += growth;
504 res = -1;
505 }
506 buf = PyByteArray_AS_STRING(self);
507 }
508 else if (growth > 0) {
509 if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) {
510 PyErr_NoMemory();
511 return -1;
512 }
513
514 if (PyByteArray_Resize((PyObject *)self,
515 Py_SIZE(self) + growth) < 0) {
516 return -1;
517 }
518 buf = PyByteArray_AS_STRING(self);
519 /* Make the place for the additional bytes */
520 /*
521 0 lo hi old_size
522 | |<-avail->|<-----tomove------>|
523 | |<---bytes_len-->|<-----tomove------>|
524 0 lo new_hi new_size
525 */
526 memmove(buf + lo + bytes_len, buf + hi,
527 Py_SIZE(self) - lo - bytes_len);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200528 }
529
530 if (bytes_len > 0)
531 memcpy(buf + lo, bytes, bytes_len);
Victor Stinner84557232013-11-21 12:29:51 +0100532 return res;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200533}
534
535static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000536bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000537 PyObject *values)
538{
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200539 Py_ssize_t needed;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000540 void *bytes;
541 Py_buffer vbytes;
542 int res = 0;
543
544 vbytes.len = -1;
545 if (values == (PyObject *)self) {
546 /* Make a copy and call this function recursively */
547 int err;
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300548 values = PyByteArray_FromStringAndSize(PyByteArray_AS_STRING(values),
549 PyByteArray_GET_SIZE(values));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000550 if (values == NULL)
551 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000552 err = bytearray_setslice(self, lo, hi, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000553 Py_DECREF(values);
554 return err;
555 }
556 if (values == NULL) {
557 /* del b[lo:hi] */
558 bytes = NULL;
559 needed = 0;
560 }
561 else {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200562 if (PyObject_GetBuffer(values, &vbytes, PyBUF_SIMPLE) != 0) {
563 PyErr_Format(PyExc_TypeError,
564 "can't set bytearray slice from %.100s",
565 Py_TYPE(values)->tp_name);
566 return -1;
567 }
568 needed = vbytes.len;
569 bytes = vbytes.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000570 }
571
572 if (lo < 0)
573 lo = 0;
574 if (hi < lo)
575 hi = lo;
576 if (hi > Py_SIZE(self))
577 hi = Py_SIZE(self);
578
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200579 res = bytearray_setslice_linear(self, lo, hi, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000580 if (vbytes.len != -1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200581 PyBuffer_Release(&vbytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000582 return res;
583}
584
585static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000586bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000587{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000588 int ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000589
590 if (i < 0)
591 i += Py_SIZE(self);
592
593 if (i < 0 || i >= Py_SIZE(self)) {
594 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
595 return -1;
596 }
597
598 if (value == NULL)
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000599 return bytearray_setslice(self, i, i+1, NULL);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000600
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000601 if (!_getbytevalue(value, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000602 return -1;
603
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200604 PyByteArray_AS_STRING(self)[i] = ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000605 return 0;
606}
607
608static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000609bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000610{
611 Py_ssize_t start, stop, step, slicelen, needed;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200612 char *buf, *bytes;
613 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000614
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000615 if (PyIndex_Check(index)) {
616 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000617
618 if (i == -1 && PyErr_Occurred())
619 return -1;
620
621 if (i < 0)
622 i += PyByteArray_GET_SIZE(self);
623
624 if (i < 0 || i >= Py_SIZE(self)) {
625 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
626 return -1;
627 }
628
629 if (values == NULL) {
630 /* Fall through to slice assignment */
631 start = i;
632 stop = i + 1;
633 step = 1;
634 slicelen = 1;
635 }
636 else {
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000637 int ival;
638 if (!_getbytevalue(values, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000639 return -1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200640 buf[i] = (char)ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000641 return 0;
642 }
643 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000644 else if (PySlice_Check(index)) {
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300645 if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000646 return -1;
647 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300648 slicelen = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self), &start,
649 &stop, step);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000650 }
651 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400652 PyErr_Format(PyExc_TypeError,
653 "bytearray indices must be integers or slices, not %.200s",
654 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000655 return -1;
656 }
657
658 if (values == NULL) {
659 bytes = NULL;
660 needed = 0;
661 }
662 else if (values == (PyObject *)self || !PyByteArray_Check(values)) {
Christian Heimes6d26ade2012-11-03 23:07:59 +0100663 int err;
Ezio Melottic64bcbe2012-11-03 21:19:06 +0200664 if (PyNumber_Check(values) || PyUnicode_Check(values)) {
665 PyErr_SetString(PyExc_TypeError,
666 "can assign only bytes, buffers, or iterables "
667 "of ints in range(0, 256)");
668 return -1;
669 }
Georg Brandlf3fa5682010-12-04 17:09:30 +0000670 /* Make a copy and call this function recursively */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000671 values = PyByteArray_FromObject(values);
672 if (values == NULL)
673 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000674 err = bytearray_ass_subscript(self, index, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000675 Py_DECREF(values);
676 return err;
677 }
678 else {
679 assert(PyByteArray_Check(values));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200680 bytes = PyByteArray_AS_STRING(values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000681 needed = Py_SIZE(values);
682 }
683 /* Make sure b[5:2] = ... inserts before 5, not before 2. */
684 if ((step < 0 && start < stop) ||
685 (step > 0 && start > stop))
686 stop = start;
687 if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200688 return bytearray_setslice_linear(self, start, stop, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000689 }
690 else {
691 if (needed == 0) {
692 /* Delete slice */
Mark Dickinsonbc099642010-01-29 17:27:24 +0000693 size_t cur;
694 Py_ssize_t i;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000695
Antoine Pitrou5504e892008-12-06 21:27:53 +0000696 if (!_canresize(self))
697 return -1;
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000698
699 if (slicelen == 0)
700 /* Nothing to do here. */
701 return 0;
702
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000703 if (step < 0) {
704 stop = start + 1;
705 start = stop + step * (slicelen - 1) - 1;
706 step = -step;
707 }
708 for (cur = start, i = 0;
709 i < slicelen; cur += step, i++) {
710 Py_ssize_t lim = step - 1;
711
Mark Dickinson66f575b2010-02-14 12:53:32 +0000712 if (cur + step >= (size_t)PyByteArray_GET_SIZE(self))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000713 lim = PyByteArray_GET_SIZE(self) - cur - 1;
714
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200715 memmove(buf + cur - i,
716 buf + cur + 1, lim);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000717 }
718 /* Move the tail of the bytes, in one chunk */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000719 cur = start + (size_t)slicelen*step;
Mark Dickinson66f575b2010-02-14 12:53:32 +0000720 if (cur < (size_t)PyByteArray_GET_SIZE(self)) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200721 memmove(buf + cur - slicelen,
722 buf + cur,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000723 PyByteArray_GET_SIZE(self) - cur);
724 }
725 if (PyByteArray_Resize((PyObject *)self,
726 PyByteArray_GET_SIZE(self) - slicelen) < 0)
727 return -1;
728
729 return 0;
730 }
731 else {
732 /* Assign slice */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000733 Py_ssize_t i;
734 size_t cur;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000735
736 if (needed != slicelen) {
737 PyErr_Format(PyExc_ValueError,
738 "attempt to assign bytes of size %zd "
739 "to extended slice of size %zd",
740 needed, slicelen);
741 return -1;
742 }
743 for (cur = start, i = 0; i < slicelen; cur += step, i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200744 buf[cur] = bytes[i];
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000745 return 0;
746 }
747 }
748}
749
750static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000751bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000752{
753 static char *kwlist[] = {"source", "encoding", "errors", 0};
754 PyObject *arg = NULL;
755 const char *encoding = NULL;
756 const char *errors = NULL;
757 Py_ssize_t count;
758 PyObject *it;
759 PyObject *(*iternext)(PyObject *);
760
761 if (Py_SIZE(self) != 0) {
762 /* Empty previous contents (yes, do this first of all!) */
763 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
764 return -1;
765 }
766
767 /* Parse arguments */
Georg Brandl3dbca812008-07-23 16:10:53 +0000768 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000769 &arg, &encoding, &errors))
770 return -1;
771
772 /* Make a quick exit if no first argument */
773 if (arg == NULL) {
774 if (encoding != NULL || errors != NULL) {
775 PyErr_SetString(PyExc_TypeError,
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300776 encoding != NULL ?
777 "encoding without a string argument" :
778 "errors without a string argument");
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000779 return -1;
780 }
781 return 0;
782 }
783
784 if (PyUnicode_Check(arg)) {
785 /* Encode via the codec registry */
786 PyObject *encoded, *new;
787 if (encoding == NULL) {
788 PyErr_SetString(PyExc_TypeError,
789 "string argument without an encoding");
790 return -1;
791 }
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000792 encoded = PyUnicode_AsEncodedString(arg, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000793 if (encoded == NULL)
794 return -1;
795 assert(PyBytes_Check(encoded));
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000796 new = bytearray_iconcat(self, encoded);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000797 Py_DECREF(encoded);
798 if (new == NULL)
799 return -1;
800 Py_DECREF(new);
801 return 0;
802 }
803
804 /* If it's not unicode, there can't be encoding or errors */
805 if (encoding != NULL || errors != NULL) {
806 PyErr_SetString(PyExc_TypeError,
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300807 encoding != NULL ?
808 "encoding without a string argument" :
809 "errors without a string argument");
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000810 return -1;
811 }
812
813 /* Is it an int? */
Serhiy Storchakaeb249882016-08-15 09:46:07 +0300814 if (PyIndex_Check(arg)) {
815 count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
816 if (count == -1 && PyErr_Occurred()) {
Serhiy Storchakae8904212018-10-15 00:02:57 +0300817 if (!PyErr_ExceptionMatches(PyExc_TypeError))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000818 return -1;
INADA Naokia634e232017-01-06 17:32:01 +0900819 PyErr_Clear(); /* fall through */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000820 }
INADA Naokia634e232017-01-06 17:32:01 +0900821 else {
822 if (count < 0) {
823 PyErr_SetString(PyExc_ValueError, "negative count");
824 return -1;
825 }
826 if (count > 0) {
827 if (PyByteArray_Resize((PyObject *)self, count))
828 return -1;
829 memset(PyByteArray_AS_STRING(self), 0, count);
830 }
831 return 0;
832 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000833 }
834
835 /* Use the buffer API */
836 if (PyObject_CheckBuffer(arg)) {
837 Py_ssize_t size;
838 Py_buffer view;
839 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
840 return -1;
841 size = view.len;
842 if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200843 if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self),
844 &view, size, 'C') < 0)
Stefan Krah7d12d9d2012-07-28 12:25:55 +0200845 goto fail;
Martin v. Löwis423be952008-08-13 15:53:07 +0000846 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000847 return 0;
848 fail:
Martin v. Löwis423be952008-08-13 15:53:07 +0000849 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000850 return -1;
851 }
852
853 /* XXX Optimize this if the arguments is a list, tuple */
854
855 /* Get the iterator */
856 it = PyObject_GetIter(arg);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300857 if (it == NULL) {
858 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
859 PyErr_Format(PyExc_TypeError,
860 "cannot convert '%.200s' object to bytearray",
861 arg->ob_type->tp_name);
862 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000863 return -1;
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300864 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000865 iternext = *Py_TYPE(it)->tp_iternext;
866
867 /* Run the iterator to exhaustion */
868 for (;;) {
869 PyObject *item;
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000870 int rc, value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000871
872 /* Get the next item */
873 item = iternext(it);
874 if (item == NULL) {
875 if (PyErr_Occurred()) {
876 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
877 goto error;
878 PyErr_Clear();
879 }
880 break;
881 }
882
883 /* Interpret it as an int (__index__) */
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000884 rc = _getbytevalue(item, &value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000885 Py_DECREF(item);
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000886 if (!rc)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000887 goto error;
888
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000889 /* Append the byte */
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300890 if (Py_SIZE(self) + 1 < self->ob_alloc) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000891 Py_SIZE(self)++;
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300892 PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0';
893 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000894 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
895 goto error;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200896 PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000897 }
898
899 /* Clean up and return success */
900 Py_DECREF(it);
901 return 0;
902
903 error:
904 /* Error handling when it != NULL */
905 Py_DECREF(it);
906 return -1;
907}
908
909/* Mostly copied from string_repr, but without the
910 "smart quote" functionality. */
911static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000912bytearray_repr(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000913{
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300914 const char *className = _PyType_Name(Py_TYPE(self));
915 const char *quote_prefix = "(b";
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000916 const char *quote_postfix = ")";
917 Py_ssize_t length = Py_SIZE(self);
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300918 /* 6 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
919 Py_ssize_t newsize;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000920 PyObject *v;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200921 Py_ssize_t i;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200922 char *bytes;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200923 char c;
924 char *p;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200925 int quote;
926 char *test, *start;
927 char *buffer;
928
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300929 newsize = strlen(className);
930 if (length > (PY_SSIZE_T_MAX - 6 - newsize) / 4) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000931 PyErr_SetString(PyExc_OverflowError,
932 "bytearray object is too large to make repr");
933 return NULL;
934 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200935
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300936 newsize += 6 + length * 4;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100937 buffer = PyObject_Malloc(newsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200938 if (buffer == NULL) {
939 PyErr_NoMemory();
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000940 return NULL;
941 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000942
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200943 /* Figure out which quote to use; single is preferred */
944 quote = '\'';
945 start = PyByteArray_AS_STRING(self);
946 for (test = start; test < start+length; ++test) {
947 if (*test == '"') {
948 quote = '\''; /* back to single */
949 break;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000950 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200951 else if (*test == '\'')
952 quote = '"';
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000953 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200954
955 p = buffer;
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300956 while (*className)
957 *p++ = *className++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200958 while (*quote_prefix)
959 *p++ = *quote_prefix++;
960 *p++ = quote;
961
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200962 bytes = PyByteArray_AS_STRING(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200963 for (i = 0; i < length; i++) {
964 /* There's at least enough room for a hex escape
965 and a closing quote. */
966 assert(newsize - (p - buffer) >= 5);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200967 c = bytes[i];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200968 if (c == '\'' || c == '\\')
969 *p++ = '\\', *p++ = c;
970 else if (c == '\t')
971 *p++ = '\\', *p++ = 't';
972 else if (c == '\n')
973 *p++ = '\\', *p++ = 'n';
974 else if (c == '\r')
975 *p++ = '\\', *p++ = 'r';
976 else if (c == 0)
977 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
978 else if (c < ' ' || c >= 0x7f) {
979 *p++ = '\\';
980 *p++ = 'x';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200981 *p++ = Py_hexdigits[(c & 0xf0) >> 4];
982 *p++ = Py_hexdigits[c & 0xf];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200983 }
984 else
985 *p++ = c;
986 }
987 assert(newsize - (p - buffer) >= 1);
988 *p++ = quote;
989 while (*quote_postfix) {
990 *p++ = *quote_postfix++;
991 }
992
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300993 v = PyUnicode_FromStringAndSize(buffer, p - buffer);
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100994 PyObject_Free(buffer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200995 return v;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000996}
997
998static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000999bytearray_str(PyObject *op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001000{
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001001 if (Py_BytesWarningFlag) {
1002 if (PyErr_WarnEx(PyExc_BytesWarning,
1003 "str() on a bytearray instance", 1)) {
1004 return NULL;
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001005 }
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001006 }
1007 return bytearray_repr((PyByteArrayObject*)op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001008}
1009
1010static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001011bytearray_richcompare(PyObject *self, PyObject *other, int op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001012{
1013 Py_ssize_t self_size, other_size;
1014 Py_buffer self_bytes, other_bytes;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001015 int cmp, rc;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001016
1017 /* Bytes can be compared to anything that supports the (binary)
1018 buffer API. Except that a comparison with Unicode is always an
1019 error, even if the comparison is for equality. */
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001020 rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type);
1021 if (!rc)
1022 rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type);
1023 if (rc < 0)
1024 return NULL;
1025 if (rc) {
Barry Warsaw9e9dcd62008-10-17 01:50:37 +00001026 if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001027 if (PyErr_WarnEx(PyExc_BytesWarning,
Georg Brandle5d68ac2008-06-04 11:30:26 +00001028 "Comparison between bytearray and string", 1))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001029 return NULL;
1030 }
1031
Brian Curtindfc80e32011-08-10 20:28:54 -05001032 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001033 }
1034
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001035 if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001036 PyErr_Clear();
Brian Curtindfc80e32011-08-10 20:28:54 -05001037 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001038 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001039 self_size = self_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001040
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001041 if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001042 PyErr_Clear();
Martin v. Löwis423be952008-08-13 15:53:07 +00001043 PyBuffer_Release(&self_bytes);
Brian Curtindfc80e32011-08-10 20:28:54 -05001044 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001045 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001046 other_size = other_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001047
1048 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
1049 /* Shortcut: if the lengths differ, the objects differ */
stratakise8b19652017-11-02 11:32:54 +01001050 PyBuffer_Release(&self_bytes);
1051 PyBuffer_Release(&other_bytes);
1052 return PyBool_FromLong((op == Py_NE));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001053 }
1054 else {
stratakise8b19652017-11-02 11:32:54 +01001055 cmp = memcmp(self_bytes.buf, other_bytes.buf,
1056 Py_MIN(self_size, other_size));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001057 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
1058
stratakise8b19652017-11-02 11:32:54 +01001059 PyBuffer_Release(&self_bytes);
1060 PyBuffer_Release(&other_bytes);
1061
1062 if (cmp != 0) {
1063 Py_RETURN_RICHCOMPARE(cmp, 0, op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001064 }
1065
stratakise8b19652017-11-02 11:32:54 +01001066 Py_RETURN_RICHCOMPARE(self_size, other_size, op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001067 }
1068
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001069}
1070
1071static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001072bytearray_dealloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001073{
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001074 if (self->ob_exports > 0) {
1075 PyErr_SetString(PyExc_SystemError,
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001076 "deallocated bytearray object has exported buffers");
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001077 PyErr_Print();
1078 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001079 if (self->ob_bytes != 0) {
Antoine Pitrou39aba4f2011-11-12 21:15:28 +01001080 PyObject_Free(self->ob_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001081 }
1082 Py_TYPE(self)->tp_free((PyObject *)self);
1083}
1084
1085
1086/* -------------------------------------------------------------------- */
1087/* Methods */
1088
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001089#define FASTSEARCH fastsearch
1090#define STRINGLIB(F) stringlib_##F
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001091#define STRINGLIB_CHAR char
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001092#define STRINGLIB_SIZEOF_CHAR 1
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001093#define STRINGLIB_LEN PyByteArray_GET_SIZE
1094#define STRINGLIB_STR PyByteArray_AS_STRING
1095#define STRINGLIB_NEW PyByteArray_FromStringAndSize
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001096#define STRINGLIB_ISSPACE Py_ISSPACE
1097#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001098#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1099#define STRINGLIB_MUTABLE 1
1100
1101#include "stringlib/fastsearch.h"
1102#include "stringlib/count.h"
1103#include "stringlib/find.h"
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001104#include "stringlib/join.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001105#include "stringlib/partition.h"
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001106#include "stringlib/split.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001107#include "stringlib/ctype.h"
1108#include "stringlib/transmogrify.h"
1109
1110
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001111static PyObject *
1112bytearray_find(PyByteArrayObject *self, PyObject *args)
1113{
1114 return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1115}
1116
1117static PyObject *
1118bytearray_count(PyByteArrayObject *self, PyObject *args)
1119{
1120 return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1121}
1122
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001123/*[clinic input]
1124bytearray.clear
1125
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001126Remove all items from the bytearray.
1127[clinic start generated code]*/
1128
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001129static PyObject *
1130bytearray_clear_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001131/*[clinic end generated code: output=85c2fe6aede0956c input=ed6edae9de447ac4]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001132{
1133 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1134 return NULL;
1135 Py_RETURN_NONE;
1136}
1137
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001138/*[clinic input]
1139bytearray.copy
1140
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001141Return a copy of B.
1142[clinic start generated code]*/
1143
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001144static PyObject *
1145bytearray_copy_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001146/*[clinic end generated code: output=68cfbcfed484c132 input=6597b0c01bccaa9e]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001147{
1148 return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self),
1149 PyByteArray_GET_SIZE(self));
1150}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001151
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001152static PyObject *
1153bytearray_index(PyByteArrayObject *self, PyObject *args)
1154{
1155 return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1156}
1157
1158static PyObject *
1159bytearray_rfind(PyByteArrayObject *self, PyObject *args)
1160{
1161 return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1162}
1163
1164static PyObject *
1165bytearray_rindex(PyByteArrayObject *self, PyObject *args)
1166{
1167 return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1168}
1169
1170static int
1171bytearray_contains(PyObject *self, PyObject *arg)
1172{
1173 return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg);
1174}
1175
1176static PyObject *
1177bytearray_startswith(PyByteArrayObject *self, PyObject *args)
1178{
1179 return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1180}
1181
1182static PyObject *
1183bytearray_endswith(PyByteArrayObject *self, PyObject *args)
1184{
1185 return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1186}
1187
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001188
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001189/*[clinic input]
1190bytearray.translate
1191
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001192 table: object
1193 Translation table, which must be a bytes object of length 256.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001194 /
Martin Panter1b6c6da2016-08-27 08:35:02 +00001195 delete as deletechars: object(c_default="NULL") = b''
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001196
1197Return a copy with each character mapped by the given translation table.
1198
Martin Panter1b6c6da2016-08-27 08:35:02 +00001199All characters occurring in the optional argument delete are removed.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001200The remaining characters are mapped through the given translation table.
1201[clinic start generated code]*/
1202
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001203static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001204bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
Martin Panter1b6c6da2016-08-27 08:35:02 +00001205 PyObject *deletechars)
1206/*[clinic end generated code: output=b6a8f01c2a74e446 input=cfff956d4d127a9b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001207{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001208 char *input, *output;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001209 const char *table_chars;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001210 Py_ssize_t i, c;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001211 PyObject *input_obj = (PyObject*)self;
1212 const char *output_start;
1213 Py_ssize_t inlen;
Georg Brandlccc47b62008-12-28 11:44:14 +00001214 PyObject *result = NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001215 int trans_table[256];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001216 Py_buffer vtable, vdel;
1217
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001218 if (table == Py_None) {
1219 table_chars = NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001220 table = NULL;
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001221 } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001222 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001223 } else {
1224 if (vtable.len != 256) {
1225 PyErr_SetString(PyExc_ValueError,
1226 "translation table must be 256 characters long");
Georg Brandl953152f2009-07-22 12:03:59 +00001227 PyBuffer_Release(&vtable);
1228 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001229 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001230 table_chars = (const char*)vtable.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001231 }
1232
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001233 if (deletechars != NULL) {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001234 if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) {
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001235 if (table != NULL)
Georg Brandl953152f2009-07-22 12:03:59 +00001236 PyBuffer_Release(&vtable);
1237 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001238 }
1239 }
1240 else {
1241 vdel.buf = NULL;
1242 vdel.len = 0;
1243 }
1244
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001245 inlen = PyByteArray_GET_SIZE(input_obj);
1246 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1247 if (result == NULL)
1248 goto done;
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03001249 output_start = output = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001250 input = PyByteArray_AS_STRING(input_obj);
1251
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001252 if (vdel.len == 0 && table_chars != NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001253 /* If no deletions are required, use faster code */
1254 for (i = inlen; --i >= 0; ) {
1255 c = Py_CHARMASK(*input++);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001256 *output++ = table_chars[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001257 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001258 goto done;
1259 }
Georg Brandlccc47b62008-12-28 11:44:14 +00001260
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001261 if (table_chars == NULL) {
Georg Brandlccc47b62008-12-28 11:44:14 +00001262 for (i = 0; i < 256; i++)
1263 trans_table[i] = Py_CHARMASK(i);
1264 } else {
1265 for (i = 0; i < 256; i++)
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001266 trans_table[i] = Py_CHARMASK(table_chars[i]);
Georg Brandlccc47b62008-12-28 11:44:14 +00001267 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001268
1269 for (i = 0; i < vdel.len; i++)
1270 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1271
1272 for (i = inlen; --i >= 0; ) {
1273 c = Py_CHARMASK(*input++);
1274 if (trans_table[c] != -1)
Martin Panter1b6c6da2016-08-27 08:35:02 +00001275 *output++ = (char)trans_table[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001276 }
1277 /* Fix the size of the resulting string */
1278 if (inlen > 0)
Christian Heimesc731bbe2013-07-21 02:04:35 +02001279 if (PyByteArray_Resize(result, output - output_start) < 0) {
1280 Py_CLEAR(result);
1281 goto done;
1282 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001283
1284done:
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001285 if (table != NULL)
Georg Brandlccc47b62008-12-28 11:44:14 +00001286 PyBuffer_Release(&vtable);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001287 if (deletechars != NULL)
Martin v. Löwis423be952008-08-13 15:53:07 +00001288 PyBuffer_Release(&vdel);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001289 return result;
1290}
1291
1292
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001293/*[clinic input]
1294
1295@staticmethod
1296bytearray.maketrans
1297
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001298 frm: Py_buffer
1299 to: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001300 /
1301
1302Return a translation table useable for the bytes or bytearray translate method.
1303
1304The returned table will be one where each byte in frm is mapped to the byte at
1305the same position in to.
1306
1307The bytes objects frm and to must be of the same length.
1308[clinic start generated code]*/
1309
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001310static PyObject *
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001311bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001312/*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001313{
1314 return _Py_bytes_maketrans(frm, to);
Georg Brandlabc38772009-04-12 15:51:51 +00001315}
1316
1317
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001318/*[clinic input]
1319bytearray.replace
1320
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001321 old: Py_buffer
1322 new: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001323 count: Py_ssize_t = -1
1324 Maximum number of occurrences to replace.
1325 -1 (the default value) means replace all occurrences.
1326 /
1327
1328Return a copy with all occurrences of substring old replaced by new.
1329
1330If the optional argument count is given, only the first count occurrences are
1331replaced.
1332[clinic start generated code]*/
1333
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001334static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001335bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
1336 Py_buffer *new, Py_ssize_t count)
1337/*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001338{
Serhiy Storchakafb81d3c2016-05-05 09:26:07 +03001339 return stringlib_replace((PyObject *)self,
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001340 (const char *)old->buf, old->len,
1341 (const char *)new->buf, new->len, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001342}
1343
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001344/*[clinic input]
1345bytearray.split
1346
1347 sep: object = None
1348 The delimiter according which to split the bytearray.
1349 None (the default value) means split on ASCII whitespace characters
1350 (space, tab, return, newline, formfeed, vertical tab).
1351 maxsplit: Py_ssize_t = -1
1352 Maximum number of splits to do.
1353 -1 (the default value) means no limit.
1354
1355Return a list of the sections in the bytearray, using sep as the delimiter.
1356[clinic start generated code]*/
1357
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001358static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001359bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
1360 Py_ssize_t maxsplit)
1361/*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001362{
1363 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001364 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001365 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001366 Py_buffer vsub;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001367
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001368 if (maxsplit < 0)
1369 maxsplit = PY_SSIZE_T_MAX;
1370
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001371 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001372 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001373
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001374 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001375 return NULL;
1376 sub = vsub.buf;
1377 n = vsub.len;
1378
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001379 list = stringlib_split(
1380 (PyObject*) self, s, len, sub, n, maxsplit
1381 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001382 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001383 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001384}
1385
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001386/*[clinic input]
1387bytearray.partition
1388
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001389 sep: object
1390 /
1391
1392Partition the bytearray into three parts using the given separator.
1393
1394This will search for the separator sep in the bytearray. If the separator is
1395found, returns a 3-tuple containing the part before the separator, the
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001396separator itself, and the part after it as new bytearray objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001397
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001398If the separator is not found, returns a 3-tuple containing the copy of the
1399original bytearray object and two empty bytearray objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001400[clinic start generated code]*/
1401
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001402static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001403bytearray_partition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001404/*[clinic end generated code: output=45d2525ddd35f957 input=8f644749ee4fc83a]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001405{
1406 PyObject *bytesep, *result;
1407
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001408 bytesep = _PyByteArray_FromBufferObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001409 if (! bytesep)
1410 return NULL;
1411
1412 result = stringlib_partition(
1413 (PyObject*) self,
1414 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1415 bytesep,
1416 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1417 );
1418
1419 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001420 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001421}
1422
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001423/*[clinic input]
1424bytearray.rpartition
1425
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001426 sep: object
1427 /
1428
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001429Partition the bytearray into three parts using the given separator.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001430
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001431This will search for the separator sep in the bytearray, starting at the end.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001432If the separator is found, returns a 3-tuple containing the part before the
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001433separator, the separator itself, and the part after it as new bytearray
1434objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001435
1436If the separator is not found, returns a 3-tuple containing two empty bytearray
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001437objects and the copy of the original bytearray object.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001438[clinic start generated code]*/
1439
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001440static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001441bytearray_rpartition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001442/*[clinic end generated code: output=440de3c9426115e8 input=7e3df3e6cb8fa0ac]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001443{
1444 PyObject *bytesep, *result;
1445
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001446 bytesep = _PyByteArray_FromBufferObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001447 if (! bytesep)
1448 return NULL;
1449
1450 result = stringlib_rpartition(
1451 (PyObject*) self,
1452 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1453 bytesep,
1454 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1455 );
1456
1457 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001458 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001459}
1460
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001461/*[clinic input]
1462bytearray.rsplit = bytearray.split
1463
1464Return a list of the sections in the bytearray, using sep as the delimiter.
1465
1466Splitting is done starting at the end of the bytearray and working to the front.
1467[clinic start generated code]*/
1468
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001469static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001470bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
1471 Py_ssize_t maxsplit)
1472/*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001473{
1474 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001475 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001476 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001477 Py_buffer vsub;
1478
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001479 if (maxsplit < 0)
1480 maxsplit = PY_SSIZE_T_MAX;
1481
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001482 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001483 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001484
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001485 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001486 return NULL;
1487 sub = vsub.buf;
1488 n = vsub.len;
1489
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001490 list = stringlib_rsplit(
1491 (PyObject*) self, s, len, sub, n, maxsplit
1492 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001493 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001494 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001495}
1496
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001497/*[clinic input]
1498bytearray.reverse
1499
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001500Reverse the order of the values in B in place.
1501[clinic start generated code]*/
1502
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001503static PyObject *
1504bytearray_reverse_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001505/*[clinic end generated code: output=9f7616f29ab309d3 input=543356319fc78557]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001506{
1507 char swap, *head, *tail;
1508 Py_ssize_t i, j, n = Py_SIZE(self);
1509
1510 j = n / 2;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001511 head = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001512 tail = head + n - 1;
1513 for (i = 0; i < j; i++) {
1514 swap = *head;
1515 *head++ = *tail;
1516 *tail-- = swap;
1517 }
1518
1519 Py_RETURN_NONE;
1520}
1521
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001522
1523/*[python input]
1524class bytesvalue_converter(CConverter):
1525 type = 'int'
1526 converter = '_getbytevalue'
1527[python start generated code]*/
1528/*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/
1529
1530
1531/*[clinic input]
1532bytearray.insert
1533
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001534 index: Py_ssize_t
1535 The index where the value is to be inserted.
1536 item: bytesvalue
1537 The item to be inserted.
1538 /
1539
1540Insert a single item into the bytearray before the given index.
1541[clinic start generated code]*/
1542
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001543static PyObject *
1544bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001545/*[clinic end generated code: output=76c775a70e7b07b7 input=b2b5d07e9de6c070]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001546{
1547 Py_ssize_t n = Py_SIZE(self);
1548 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001549
1550 if (n == PY_SSIZE_T_MAX) {
1551 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001552 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001553 return NULL;
1554 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001555 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1556 return NULL;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001557 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001558
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001559 if (index < 0) {
1560 index += n;
1561 if (index < 0)
1562 index = 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001563 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001564 if (index > n)
1565 index = n;
1566 memmove(buf + index + 1, buf + index, n - index);
1567 buf[index] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001568
1569 Py_RETURN_NONE;
1570}
1571
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001572/*[clinic input]
1573bytearray.append
1574
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001575 item: bytesvalue
1576 The item to be appended.
1577 /
1578
1579Append a single item to the end of the bytearray.
1580[clinic start generated code]*/
1581
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001582static PyObject *
1583bytearray_append_impl(PyByteArrayObject *self, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001584/*[clinic end generated code: output=a154e19ed1886cb6 input=20d6bec3d1340593]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001585{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001586 Py_ssize_t n = Py_SIZE(self);
1587
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001588 if (n == PY_SSIZE_T_MAX) {
1589 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001590 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001591 return NULL;
1592 }
1593 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1594 return NULL;
1595
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001596 PyByteArray_AS_STRING(self)[n] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001597
1598 Py_RETURN_NONE;
1599}
1600
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001601/*[clinic input]
1602bytearray.extend
1603
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001604 iterable_of_ints: object
1605 The iterable of items to append.
1606 /
1607
1608Append all the items from the iterator or sequence to the end of the bytearray.
1609[clinic start generated code]*/
1610
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001611static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001612bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001613/*[clinic end generated code: output=98155dbe249170b1 input=c617b3a93249ba28]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001614{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001615 PyObject *it, *item, *bytearray_obj;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001616 Py_ssize_t buf_size = 0, len = 0;
1617 int value;
1618 char *buf;
1619
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001620 /* bytearray_setslice code only accepts something supporting PEP 3118. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001621 if (PyObject_CheckBuffer(iterable_of_ints)) {
1622 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_ints) == -1)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001623 return NULL;
1624
1625 Py_RETURN_NONE;
1626 }
1627
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001628 it = PyObject_GetIter(iterable_of_ints);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +03001629 if (it == NULL) {
1630 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
1631 PyErr_Format(PyExc_TypeError,
1632 "can't extend bytearray with %.100s",
1633 iterable_of_ints->ob_type->tp_name);
1634 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001635 return NULL;
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +03001636 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001637
Ezio Melotti42da6632011-03-15 05:18:48 +02001638 /* Try to determine the length of the argument. 32 is arbitrary. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001639 buf_size = PyObject_LengthHint(iterable_of_ints, 32);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001640 if (buf_size == -1) {
1641 Py_DECREF(it);
1642 return NULL;
1643 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001644
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001645 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001646 if (bytearray_obj == NULL) {
1647 Py_DECREF(it);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001648 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001649 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001650 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001651
1652 while ((item = PyIter_Next(it)) != NULL) {
1653 if (! _getbytevalue(item, &value)) {
1654 Py_DECREF(item);
1655 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001656 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001657 return NULL;
1658 }
1659 buf[len++] = value;
1660 Py_DECREF(item);
1661
1662 if (len >= buf_size) {
Martin Panter371731e2016-07-18 07:53:13 +00001663 Py_ssize_t addition;
1664 if (len == PY_SSIZE_T_MAX) {
1665 Py_DECREF(it);
1666 Py_DECREF(bytearray_obj);
1667 return PyErr_NoMemory();
1668 }
1669 addition = len >> 1;
1670 if (addition > PY_SSIZE_T_MAX - len - 1)
1671 buf_size = PY_SSIZE_T_MAX;
1672 else
1673 buf_size = len + addition + 1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001674 if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001675 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001676 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001677 return NULL;
1678 }
1679 /* Recompute the `buf' pointer, since the resizing operation may
1680 have invalidated it. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001681 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001682 }
1683 }
1684 Py_DECREF(it);
1685
1686 /* Resize down to exact size. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001687 if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) {
1688 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001689 return NULL;
1690 }
1691
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001692 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) {
1693 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001694 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001695 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001696 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001697
1698 Py_RETURN_NONE;
1699}
1700
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001701/*[clinic input]
1702bytearray.pop
1703
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001704 index: Py_ssize_t = -1
1705 The index from where to remove the item.
1706 -1 (the default value) means remove the last item.
1707 /
1708
1709Remove and return a single item from B.
1710
1711If no index argument is given, will pop the last item.
1712[clinic start generated code]*/
1713
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001714static PyObject *
1715bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001716/*[clinic end generated code: output=e0ccd401f8021da8 input=3591df2d06c0d237]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001717{
1718 int value;
1719 Py_ssize_t n = Py_SIZE(self);
1720 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001721
1722 if (n == 0) {
Eli Bendersky1bc4f192011-03-04 04:55:25 +00001723 PyErr_SetString(PyExc_IndexError,
1724 "pop from empty bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001725 return NULL;
1726 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001727 if (index < 0)
1728 index += Py_SIZE(self);
1729 if (index < 0 || index >= Py_SIZE(self)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001730 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1731 return NULL;
1732 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001733 if (!_canresize(self))
1734 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001735
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001736 buf = PyByteArray_AS_STRING(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001737 value = buf[index];
1738 memmove(buf + index, buf + index + 1, n - index);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001739 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1740 return NULL;
1741
Mark Dickinson54a3db92009-09-06 10:19:23 +00001742 return PyLong_FromLong((unsigned char)value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001743}
1744
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001745/*[clinic input]
1746bytearray.remove
1747
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001748 value: bytesvalue
1749 The value to remove.
1750 /
1751
1752Remove the first occurrence of a value in the bytearray.
1753[clinic start generated code]*/
1754
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001755static PyObject *
1756bytearray_remove_impl(PyByteArrayObject *self, int value)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001757/*[clinic end generated code: output=d659e37866709c13 input=121831240cd51ddf]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001758{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001759 Py_ssize_t where, n = Py_SIZE(self);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001760 char *buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001761
Serhiy Storchaka4b234942016-05-16 22:24:03 +03001762 where = stringlib_find_char(buf, n, value);
1763 if (where < 0) {
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001764 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001765 return NULL;
1766 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001767 if (!_canresize(self))
1768 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001769
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001770 memmove(buf + where, buf + where + 1, n - where);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001771 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1772 return NULL;
1773
1774 Py_RETURN_NONE;
1775}
1776
1777/* XXX These two helpers could be optimized if argsize == 1 */
1778
1779static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001780lstrip_helper(const char *myptr, Py_ssize_t mysize,
1781 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001782{
1783 Py_ssize_t i = 0;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001784 while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001785 i++;
1786 return i;
1787}
1788
1789static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001790rstrip_helper(const char *myptr, Py_ssize_t mysize,
1791 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001792{
1793 Py_ssize_t i = mysize - 1;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001794 while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001795 i--;
1796 return i + 1;
1797}
1798
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001799/*[clinic input]
1800bytearray.strip
1801
1802 bytes: object = None
1803 /
1804
1805Strip leading and trailing bytes contained in the argument.
1806
1807If the argument is omitted or None, strip leading and trailing ASCII whitespace.
1808[clinic start generated code]*/
1809
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001810static PyObject *
1811bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001812/*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001813{
1814 Py_ssize_t left, right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001815 char *myptr;
1816 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001817 Py_buffer vbytes;
1818
1819 if (bytes == Py_None) {
1820 bytesptr = "\t\n\r\f\v ";
1821 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001822 }
1823 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001824 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001825 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001826 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001827 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001828 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001829 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001830 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001831 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001832 if (left == mysize)
1833 right = left;
1834 else
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001835 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1836 if (bytes != Py_None)
1837 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001838 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001839}
1840
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001841/*[clinic input]
1842bytearray.lstrip
1843
1844 bytes: object = None
1845 /
1846
1847Strip leading bytes contained in the argument.
1848
1849If the argument is omitted or None, strip leading ASCII whitespace.
1850[clinic start generated code]*/
1851
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001852static PyObject *
1853bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001854/*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001855{
1856 Py_ssize_t left, right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001857 char *myptr;
1858 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001859 Py_buffer vbytes;
1860
1861 if (bytes == Py_None) {
1862 bytesptr = "\t\n\r\f\v ";
1863 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001864 }
1865 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001866 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001867 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001868 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001869 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001870 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001871 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001872 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001873 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001874 right = mysize;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001875 if (bytes != Py_None)
1876 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001877 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001878}
1879
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001880/*[clinic input]
1881bytearray.rstrip
1882
1883 bytes: object = None
1884 /
1885
1886Strip trailing bytes contained in the argument.
1887
1888If the argument is omitted or None, strip trailing ASCII whitespace.
1889[clinic start generated code]*/
1890
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001891static PyObject *
1892bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001893/*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001894{
1895 Py_ssize_t right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001896 char *myptr;
1897 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001898 Py_buffer vbytes;
1899
1900 if (bytes == Py_None) {
1901 bytesptr = "\t\n\r\f\v ";
1902 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001903 }
1904 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001905 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001906 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001907 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001908 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001909 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001910 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001911 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001912 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1913 if (bytes != Py_None)
1914 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001915 return PyByteArray_FromStringAndSize(myptr, right);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001916}
1917
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001918/*[clinic input]
1919bytearray.decode
1920
1921 encoding: str(c_default="NULL") = 'utf-8'
1922 The encoding with which to decode the bytearray.
1923 errors: str(c_default="NULL") = 'strict'
1924 The error handling scheme to use for the handling of decoding errors.
1925 The default is 'strict' meaning that decoding errors raise a
1926 UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
1927 as well as any other name registered with codecs.register_error that
1928 can handle UnicodeDecodeErrors.
1929
1930Decode the bytearray using the codec registered for encoding.
1931[clinic start generated code]*/
1932
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001933static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001934bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
1935 const char *errors)
1936/*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001937{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001938 if (encoding == NULL)
1939 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis0efea322014-07-27 17:29:17 +02001940 return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001941}
1942
1943PyDoc_STRVAR(alloc_doc,
1944"B.__alloc__() -> int\n\
1945\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00001946Return the number of bytes actually allocated.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001947
1948static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301949bytearray_alloc(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001950{
1951 return PyLong_FromSsize_t(self->ob_alloc);
1952}
1953
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001954/*[clinic input]
1955bytearray.join
1956
1957 iterable_of_bytes: object
1958 /
1959
1960Concatenate any number of bytes/bytearray objects.
1961
1962The bytearray whose method is called is inserted in between each pair.
1963
1964The result is returned as a new bytearray object.
1965[clinic start generated code]*/
1966
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001967static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001968bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001969/*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001970{
Martin v. Löwis0efea322014-07-27 17:29:17 +02001971 return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001972}
1973
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001974/*[clinic input]
1975bytearray.splitlines
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001976
Serhiy Storchaka202fda52017-03-12 10:10:47 +02001977 keepends: bool(accept={int}) = False
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001978
1979Return a list of the lines in the bytearray, breaking at line boundaries.
1980
1981Line breaks are not included in the resulting list unless keepends is given and
1982true.
1983[clinic start generated code]*/
1984
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001985static PyObject *
1986bytearray_splitlines_impl(PyByteArrayObject *self, int keepends)
Serhiy Storchaka202fda52017-03-12 10:10:47 +02001987/*[clinic end generated code: output=4223c94b895f6ad9 input=99a27ad959b9cf6b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001988{
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001989 return stringlib_splitlines(
1990 (PyObject*) self, PyByteArray_AS_STRING(self),
1991 PyByteArray_GET_SIZE(self), keepends
1992 );
1993}
1994
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001995/*[clinic input]
1996@classmethod
1997bytearray.fromhex
1998
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001999 string: unicode
2000 /
2001
2002Create a bytearray object from a string of hexadecimal numbers.
2003
2004Spaces between two numbers are accepted.
2005Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')
2006[clinic start generated code]*/
2007
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002008static PyObject *
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002009bytearray_fromhex_impl(PyTypeObject *type, PyObject *string)
2010/*[clinic end generated code: output=8f0f0b6d30fb3ba0 input=f033a16d1fb21f48]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002011{
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002012 PyObject *result = _PyBytes_FromHex(string, type == &PyByteArray_Type);
2013 if (type != &PyByteArray_Type && result != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002014 Py_SETREF(result, PyObject_CallFunctionObjArgs((PyObject *)type,
2015 result, NULL));
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002016 }
2017 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002018}
2019
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002020PyDoc_STRVAR(hex__doc__,
2021"B.hex() -> string\n\
2022\n\
2023Create a string of hexadecimal numbers from a bytearray object.\n\
2024Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'.");
2025
2026static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302027bytearray_hex(PyBytesObject *self, PyObject *Py_UNUSED(ignored))
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002028{
2029 char* argbuf = PyByteArray_AS_STRING(self);
2030 Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
2031 return _Py_strhex(argbuf, arglen);
2032}
2033
2034static PyObject *
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002035_common_reduce(PyByteArrayObject *self, int proto)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002036{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002037 PyObject *dict;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002038 _Py_IDENTIFIER(__dict__);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002039 char *buf;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002040
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002041 dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002042 if (dict == NULL) {
2043 PyErr_Clear();
2044 dict = Py_None;
2045 Py_INCREF(dict);
2046 }
2047
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002048 buf = PyByteArray_AS_STRING(self);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002049 if (proto < 3) {
2050 /* use str based reduction for backwards compatibility with Python 2.x */
2051 PyObject *latin1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002052 if (Py_SIZE(self))
2053 latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002054 else
2055 latin1 = PyUnicode_FromString("");
2056 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
2057 }
2058 else {
2059 /* use more efficient byte based reduction */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002060 if (Py_SIZE(self)) {
2061 return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002062 }
2063 else {
2064 return Py_BuildValue("(O()N)", Py_TYPE(self), dict);
2065 }
2066 }
2067}
2068
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002069/*[clinic input]
2070bytearray.__reduce__ as bytearray_reduce
2071
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002072Return state information for pickling.
2073[clinic start generated code]*/
2074
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002075static PyObject *
2076bytearray_reduce_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002077/*[clinic end generated code: output=52bf304086464cab input=44b5737ada62dd3f]*/
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002078{
2079 return _common_reduce(self, 2);
2080}
2081
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002082/*[clinic input]
2083bytearray.__reduce_ex__ as bytearray_reduce_ex
2084
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002085 proto: int = 0
2086 /
2087
2088Return state information for pickling.
2089[clinic start generated code]*/
2090
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002091static PyObject *
2092bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002093/*[clinic end generated code: output=52eac33377197520 input=f129bc1a1aa151ee]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002094{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002095 return _common_reduce(self, proto);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002096}
2097
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002098/*[clinic input]
2099bytearray.__sizeof__ as bytearray_sizeof
2100
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002101Returns the size of the bytearray object in memory, in bytes.
2102[clinic start generated code]*/
2103
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002104static PyObject *
2105bytearray_sizeof_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002106/*[clinic end generated code: output=738abdd17951c427 input=e27320fd98a4bc5a]*/
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002107{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002108 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002109
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002110 res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002111 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002112}
2113
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002114static PySequenceMethods bytearray_as_sequence = {
2115 (lenfunc)bytearray_length, /* sq_length */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002116 (binaryfunc)PyByteArray_Concat, /* sq_concat */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002117 (ssizeargfunc)bytearray_repeat, /* sq_repeat */
2118 (ssizeargfunc)bytearray_getitem, /* sq_item */
2119 0, /* sq_slice */
2120 (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */
2121 0, /* sq_ass_slice */
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002122 (objobjproc)bytearray_contains, /* sq_contains */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002123 (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */
2124 (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002125};
2126
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002127static PyMappingMethods bytearray_as_mapping = {
2128 (lenfunc)bytearray_length,
2129 (binaryfunc)bytearray_subscript,
2130 (objobjargproc)bytearray_ass_subscript,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002131};
2132
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002133static PyBufferProcs bytearray_as_buffer = {
2134 (getbufferproc)bytearray_getbuffer,
2135 (releasebufferproc)bytearray_releasebuffer,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002136};
2137
2138static PyMethodDef
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002139bytearray_methods[] = {
2140 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002141 BYTEARRAY_REDUCE_METHODDEF
2142 BYTEARRAY_REDUCE_EX_METHODDEF
2143 BYTEARRAY_SIZEOF_METHODDEF
2144 BYTEARRAY_APPEND_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302145 {"capitalize", stringlib_capitalize, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002146 _Py_capitalize__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002147 STRINGLIB_CENTER_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002148 BYTEARRAY_CLEAR_METHODDEF
2149 BYTEARRAY_COPY_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002150 {"count", (PyCFunction)bytearray_count, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002151 _Py_count__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002152 BYTEARRAY_DECODE_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002153 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002154 _Py_endswith__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002155 STRINGLIB_EXPANDTABS_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002156 BYTEARRAY_EXTEND_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002157 {"find", (PyCFunction)bytearray_find, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002158 _Py_find__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002159 BYTEARRAY_FROMHEX_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002160 {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__},
2161 {"index", (PyCFunction)bytearray_index, METH_VARARGS, _Py_index__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002162 BYTEARRAY_INSERT_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302163 {"isalnum", stringlib_isalnum, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002164 _Py_isalnum__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302165 {"isalpha", stringlib_isalpha, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002166 _Py_isalpha__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302167 {"isascii", stringlib_isascii, METH_NOARGS,
INADA Naokia49ac992018-01-27 14:06:21 +09002168 _Py_isascii__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302169 {"isdigit", stringlib_isdigit, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002170 _Py_isdigit__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302171 {"islower", stringlib_islower, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002172 _Py_islower__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302173 {"isspace", stringlib_isspace, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002174 _Py_isspace__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302175 {"istitle", stringlib_istitle, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002176 _Py_istitle__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302177 {"isupper", stringlib_isupper, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002178 _Py_isupper__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002179 BYTEARRAY_JOIN_METHODDEF
Tal Einatc929df32018-07-06 13:17:38 +03002180 STRINGLIB_LJUST_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302181 {"lower", stringlib_lower, METH_NOARGS, _Py_lower__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002182 BYTEARRAY_LSTRIP_METHODDEF
2183 BYTEARRAY_MAKETRANS_METHODDEF
2184 BYTEARRAY_PARTITION_METHODDEF
2185 BYTEARRAY_POP_METHODDEF
2186 BYTEARRAY_REMOVE_METHODDEF
2187 BYTEARRAY_REPLACE_METHODDEF
2188 BYTEARRAY_REVERSE_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002189 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__},
2190 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002191 STRINGLIB_RJUST_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002192 BYTEARRAY_RPARTITION_METHODDEF
2193 BYTEARRAY_RSPLIT_METHODDEF
2194 BYTEARRAY_RSTRIP_METHODDEF
2195 BYTEARRAY_SPLIT_METHODDEF
2196 BYTEARRAY_SPLITLINES_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002197 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002198 _Py_startswith__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002199 BYTEARRAY_STRIP_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302200 {"swapcase", stringlib_swapcase, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002201 _Py_swapcase__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302202 {"title", stringlib_title, METH_NOARGS, _Py_title__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002203 BYTEARRAY_TRANSLATE_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302204 {"upper", stringlib_upper, METH_NOARGS, _Py_upper__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002205 STRINGLIB_ZFILL_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002206 {NULL}
2207};
2208
Ethan Furmanb95b5612015-01-23 20:05:18 -08002209static PyObject *
2210bytearray_mod(PyObject *v, PyObject *w)
2211{
2212 if (!PyByteArray_Check(v))
2213 Py_RETURN_NOTIMPLEMENTED;
Berker Peksag43de36d2016-04-16 01:20:47 +03002214 return _PyBytes_FormatEx(PyByteArray_AS_STRING(v), PyByteArray_GET_SIZE(v), w, 1);
Ethan Furmanb95b5612015-01-23 20:05:18 -08002215}
2216
2217static PyNumberMethods bytearray_as_number = {
2218 0, /*nb_add*/
2219 0, /*nb_subtract*/
2220 0, /*nb_multiply*/
2221 bytearray_mod, /*nb_remainder*/
2222};
2223
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002224PyDoc_STRVAR(bytearray_doc,
Georg Brandl17cb8a82008-05-30 08:20:09 +00002225"bytearray(iterable_of_ints) -> bytearray\n\
2226bytearray(string, encoding[, errors]) -> bytearray\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002227bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\
2228bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\
2229bytearray() -> empty bytes array\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002230\n\
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03002231Construct a mutable bytearray object from:\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002232 - an iterable yielding integers in range(256)\n\
2233 - a text string encoded using the specified encoding\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002234 - a bytes or a buffer object\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002235 - any object implementing the buffer API.\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002236 - an integer");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002237
2238
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002239static PyObject *bytearray_iter(PyObject *seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002240
2241PyTypeObject PyByteArray_Type = {
2242 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2243 "bytearray",
2244 sizeof(PyByteArrayObject),
2245 0,
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002246 (destructor)bytearray_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002247 0, /* tp_print */
2248 0, /* tp_getattr */
2249 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002250 0, /* tp_reserved */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002251 (reprfunc)bytearray_repr, /* tp_repr */
Ethan Furmanb95b5612015-01-23 20:05:18 -08002252 &bytearray_as_number, /* tp_as_number */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002253 &bytearray_as_sequence, /* tp_as_sequence */
2254 &bytearray_as_mapping, /* tp_as_mapping */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002255 0, /* tp_hash */
2256 0, /* tp_call */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002257 bytearray_str, /* tp_str */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002258 PyObject_GenericGetAttr, /* tp_getattro */
2259 0, /* tp_setattro */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002260 &bytearray_as_buffer, /* tp_as_buffer */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002261 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002262 bytearray_doc, /* tp_doc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002263 0, /* tp_traverse */
2264 0, /* tp_clear */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002265 (richcmpfunc)bytearray_richcompare, /* tp_richcompare */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002266 0, /* tp_weaklistoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002267 bytearray_iter, /* tp_iter */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002268 0, /* tp_iternext */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002269 bytearray_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002270 0, /* tp_members */
2271 0, /* tp_getset */
2272 0, /* tp_base */
2273 0, /* tp_dict */
2274 0, /* tp_descr_get */
2275 0, /* tp_descr_set */
2276 0, /* tp_dictoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002277 (initproc)bytearray_init, /* tp_init */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002278 PyType_GenericAlloc, /* tp_alloc */
2279 PyType_GenericNew, /* tp_new */
2280 PyObject_Del, /* tp_free */
2281};
2282
2283/*********************** Bytes Iterator ****************************/
2284
2285typedef struct {
2286 PyObject_HEAD
2287 Py_ssize_t it_index;
2288 PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
2289} bytesiterobject;
2290
2291static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002292bytearrayiter_dealloc(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002293{
2294 _PyObject_GC_UNTRACK(it);
2295 Py_XDECREF(it->it_seq);
2296 PyObject_GC_Del(it);
2297}
2298
2299static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002300bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002301{
2302 Py_VISIT(it->it_seq);
2303 return 0;
2304}
2305
2306static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002307bytearrayiter_next(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002308{
2309 PyByteArrayObject *seq;
2310 PyObject *item;
2311
2312 assert(it != NULL);
2313 seq = it->it_seq;
2314 if (seq == NULL)
2315 return NULL;
2316 assert(PyByteArray_Check(seq));
2317
2318 if (it->it_index < PyByteArray_GET_SIZE(seq)) {
2319 item = PyLong_FromLong(
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002320 (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002321 if (item != NULL)
2322 ++it->it_index;
2323 return item;
2324 }
2325
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002326 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03002327 Py_DECREF(seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002328 return NULL;
2329}
2330
2331static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302332bytearrayiter_length_hint(bytesiterobject *it, PyObject *Py_UNUSED(ignored))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002333{
2334 Py_ssize_t len = 0;
Serhiy Storchakaaf658722016-07-03 14:41:36 +03002335 if (it->it_seq) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002336 len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
Serhiy Storchakaaf658722016-07-03 14:41:36 +03002337 if (len < 0) {
2338 len = 0;
2339 }
2340 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002341 return PyLong_FromSsize_t(len);
2342}
2343
2344PyDoc_STRVAR(length_hint_doc,
2345 "Private method returning an estimate of len(list(it)).");
2346
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002347static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302348bytearrayiter_reduce(bytesiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002349{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002350 _Py_IDENTIFIER(iter);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002351 if (it->it_seq != NULL) {
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002352 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002353 it->it_seq, it->it_index);
2354 } else {
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002355 return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002356 }
2357}
2358
2359static PyObject *
2360bytearrayiter_setstate(bytesiterobject *it, PyObject *state)
2361{
2362 Py_ssize_t index = PyLong_AsSsize_t(state);
2363 if (index == -1 && PyErr_Occurred())
2364 return NULL;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00002365 if (it->it_seq != NULL) {
2366 if (index < 0)
2367 index = 0;
2368 else if (index > PyByteArray_GET_SIZE(it->it_seq))
2369 index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */
2370 it->it_index = index;
2371 }
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002372 Py_RETURN_NONE;
2373}
2374
2375PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
2376
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002377static PyMethodDef bytearrayiter_methods[] = {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002378 {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002379 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002380 {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002381 bytearray_reduce__doc__},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002382 {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
2383 setstate_doc},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002384 {NULL, NULL} /* sentinel */
2385};
2386
2387PyTypeObject PyByteArrayIter_Type = {
2388 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2389 "bytearray_iterator", /* tp_name */
2390 sizeof(bytesiterobject), /* tp_basicsize */
2391 0, /* tp_itemsize */
2392 /* methods */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002393 (destructor)bytearrayiter_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002394 0, /* tp_print */
2395 0, /* tp_getattr */
2396 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002397 0, /* tp_reserved */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002398 0, /* tp_repr */
2399 0, /* tp_as_number */
2400 0, /* tp_as_sequence */
2401 0, /* tp_as_mapping */
2402 0, /* tp_hash */
2403 0, /* tp_call */
2404 0, /* tp_str */
2405 PyObject_GenericGetAttr, /* tp_getattro */
2406 0, /* tp_setattro */
2407 0, /* tp_as_buffer */
2408 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
2409 0, /* tp_doc */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002410 (traverseproc)bytearrayiter_traverse, /* tp_traverse */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002411 0, /* tp_clear */
2412 0, /* tp_richcompare */
2413 0, /* tp_weaklistoffset */
2414 PyObject_SelfIter, /* tp_iter */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002415 (iternextfunc)bytearrayiter_next, /* tp_iternext */
2416 bytearrayiter_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002417 0,
2418};
2419
2420static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002421bytearray_iter(PyObject *seq)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002422{
2423 bytesiterobject *it;
2424
2425 if (!PyByteArray_Check(seq)) {
2426 PyErr_BadInternalCall();
2427 return NULL;
2428 }
2429 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
2430 if (it == NULL)
2431 return NULL;
2432 it->it_index = 0;
2433 Py_INCREF(seq);
2434 it->it_seq = (PyByteArrayObject *)seq;
2435 _PyObject_GC_TRACK(it);
2436 return (PyObject *)it;
2437}