blob: eaf5dceb03a4caa68604ea439ec5d9122e2309a5 [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 Stinnerc96be812019-05-14 17:34:56 +02001001 _PyCoreConfig *config = &_PyInterpreterState_GET_UNSAFE()->core_config;
1002 if (config->bytes_warning) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001003 if (PyErr_WarnEx(PyExc_BytesWarning,
1004 "str() on a bytearray instance", 1)) {
1005 return NULL;
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001006 }
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001007 }
1008 return bytearray_repr((PyByteArrayObject*)op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001009}
1010
1011static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001012bytearray_richcompare(PyObject *self, PyObject *other, int op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001013{
1014 Py_ssize_t self_size, other_size;
1015 Py_buffer self_bytes, other_bytes;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001016 int cmp, rc;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001017
1018 /* Bytes can be compared to anything that supports the (binary)
1019 buffer API. Except that a comparison with Unicode is always an
1020 error, even if the comparison is for equality. */
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001021 rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type);
1022 if (!rc)
1023 rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type);
1024 if (rc < 0)
1025 return NULL;
1026 if (rc) {
Victor Stinnerc96be812019-05-14 17:34:56 +02001027 _PyCoreConfig *config = &_PyInterpreterState_GET_UNSAFE()->core_config;
1028 if (config->bytes_warning && (op == Py_EQ || op == Py_NE)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001029 if (PyErr_WarnEx(PyExc_BytesWarning,
Georg Brandle5d68ac2008-06-04 11:30:26 +00001030 "Comparison between bytearray and string", 1))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001031 return NULL;
1032 }
1033
Brian Curtindfc80e32011-08-10 20:28:54 -05001034 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001035 }
1036
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001037 if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001038 PyErr_Clear();
Brian Curtindfc80e32011-08-10 20:28:54 -05001039 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001040 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001041 self_size = self_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001042
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001043 if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001044 PyErr_Clear();
Martin v. Löwis423be952008-08-13 15:53:07 +00001045 PyBuffer_Release(&self_bytes);
Brian Curtindfc80e32011-08-10 20:28:54 -05001046 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001047 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001048 other_size = other_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001049
1050 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
1051 /* Shortcut: if the lengths differ, the objects differ */
stratakise8b19652017-11-02 11:32:54 +01001052 PyBuffer_Release(&self_bytes);
1053 PyBuffer_Release(&other_bytes);
1054 return PyBool_FromLong((op == Py_NE));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001055 }
1056 else {
stratakise8b19652017-11-02 11:32:54 +01001057 cmp = memcmp(self_bytes.buf, other_bytes.buf,
1058 Py_MIN(self_size, other_size));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001059 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
1060
stratakise8b19652017-11-02 11:32:54 +01001061 PyBuffer_Release(&self_bytes);
1062 PyBuffer_Release(&other_bytes);
1063
1064 if (cmp != 0) {
1065 Py_RETURN_RICHCOMPARE(cmp, 0, op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001066 }
1067
stratakise8b19652017-11-02 11:32:54 +01001068 Py_RETURN_RICHCOMPARE(self_size, other_size, op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001069 }
1070
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001071}
1072
1073static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001074bytearray_dealloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001075{
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001076 if (self->ob_exports > 0) {
1077 PyErr_SetString(PyExc_SystemError,
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001078 "deallocated bytearray object has exported buffers");
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001079 PyErr_Print();
1080 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001081 if (self->ob_bytes != 0) {
Antoine Pitrou39aba4f2011-11-12 21:15:28 +01001082 PyObject_Free(self->ob_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001083 }
1084 Py_TYPE(self)->tp_free((PyObject *)self);
1085}
1086
1087
1088/* -------------------------------------------------------------------- */
1089/* Methods */
1090
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001091#define FASTSEARCH fastsearch
1092#define STRINGLIB(F) stringlib_##F
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001093#define STRINGLIB_CHAR char
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001094#define STRINGLIB_SIZEOF_CHAR 1
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001095#define STRINGLIB_LEN PyByteArray_GET_SIZE
1096#define STRINGLIB_STR PyByteArray_AS_STRING
1097#define STRINGLIB_NEW PyByteArray_FromStringAndSize
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001098#define STRINGLIB_ISSPACE Py_ISSPACE
1099#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001100#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1101#define STRINGLIB_MUTABLE 1
1102
1103#include "stringlib/fastsearch.h"
1104#include "stringlib/count.h"
1105#include "stringlib/find.h"
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001106#include "stringlib/join.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001107#include "stringlib/partition.h"
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001108#include "stringlib/split.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001109#include "stringlib/ctype.h"
1110#include "stringlib/transmogrify.h"
1111
1112
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001113static PyObject *
1114bytearray_find(PyByteArrayObject *self, PyObject *args)
1115{
1116 return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1117}
1118
1119static PyObject *
1120bytearray_count(PyByteArrayObject *self, PyObject *args)
1121{
1122 return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1123}
1124
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001125/*[clinic input]
1126bytearray.clear
1127
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001128Remove all items from the bytearray.
1129[clinic start generated code]*/
1130
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001131static PyObject *
1132bytearray_clear_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001133/*[clinic end generated code: output=85c2fe6aede0956c input=ed6edae9de447ac4]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001134{
1135 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1136 return NULL;
1137 Py_RETURN_NONE;
1138}
1139
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001140/*[clinic input]
1141bytearray.copy
1142
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001143Return a copy of B.
1144[clinic start generated code]*/
1145
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001146static PyObject *
1147bytearray_copy_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001148/*[clinic end generated code: output=68cfbcfed484c132 input=6597b0c01bccaa9e]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001149{
1150 return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self),
1151 PyByteArray_GET_SIZE(self));
1152}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001153
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001154static PyObject *
1155bytearray_index(PyByteArrayObject *self, PyObject *args)
1156{
1157 return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1158}
1159
1160static PyObject *
1161bytearray_rfind(PyByteArrayObject *self, PyObject *args)
1162{
1163 return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1164}
1165
1166static PyObject *
1167bytearray_rindex(PyByteArrayObject *self, PyObject *args)
1168{
1169 return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1170}
1171
1172static int
1173bytearray_contains(PyObject *self, PyObject *arg)
1174{
1175 return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg);
1176}
1177
1178static PyObject *
1179bytearray_startswith(PyByteArrayObject *self, PyObject *args)
1180{
1181 return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1182}
1183
1184static PyObject *
1185bytearray_endswith(PyByteArrayObject *self, PyObject *args)
1186{
1187 return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1188}
1189
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001190
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001191/*[clinic input]
1192bytearray.translate
1193
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001194 table: object
1195 Translation table, which must be a bytes object of length 256.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001196 /
Martin Panter1b6c6da2016-08-27 08:35:02 +00001197 delete as deletechars: object(c_default="NULL") = b''
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001198
1199Return a copy with each character mapped by the given translation table.
1200
Martin Panter1b6c6da2016-08-27 08:35:02 +00001201All characters occurring in the optional argument delete are removed.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001202The remaining characters are mapped through the given translation table.
1203[clinic start generated code]*/
1204
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001205static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001206bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
Martin Panter1b6c6da2016-08-27 08:35:02 +00001207 PyObject *deletechars)
1208/*[clinic end generated code: output=b6a8f01c2a74e446 input=cfff956d4d127a9b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001209{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001210 char *input, *output;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001211 const char *table_chars;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001212 Py_ssize_t i, c;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001213 PyObject *input_obj = (PyObject*)self;
1214 const char *output_start;
1215 Py_ssize_t inlen;
Georg Brandlccc47b62008-12-28 11:44:14 +00001216 PyObject *result = NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001217 int trans_table[256];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001218 Py_buffer vtable, vdel;
1219
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001220 if (table == Py_None) {
1221 table_chars = NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001222 table = NULL;
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001223 } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001224 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001225 } else {
1226 if (vtable.len != 256) {
1227 PyErr_SetString(PyExc_ValueError,
1228 "translation table must be 256 characters long");
Georg Brandl953152f2009-07-22 12:03:59 +00001229 PyBuffer_Release(&vtable);
1230 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001231 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001232 table_chars = (const char*)vtable.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001233 }
1234
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001235 if (deletechars != NULL) {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001236 if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) {
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001237 if (table != NULL)
Georg Brandl953152f2009-07-22 12:03:59 +00001238 PyBuffer_Release(&vtable);
1239 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001240 }
1241 }
1242 else {
1243 vdel.buf = NULL;
1244 vdel.len = 0;
1245 }
1246
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001247 inlen = PyByteArray_GET_SIZE(input_obj);
1248 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1249 if (result == NULL)
1250 goto done;
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03001251 output_start = output = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001252 input = PyByteArray_AS_STRING(input_obj);
1253
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001254 if (vdel.len == 0 && table_chars != NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001255 /* If no deletions are required, use faster code */
1256 for (i = inlen; --i >= 0; ) {
1257 c = Py_CHARMASK(*input++);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001258 *output++ = table_chars[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001259 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001260 goto done;
1261 }
Georg Brandlccc47b62008-12-28 11:44:14 +00001262
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001263 if (table_chars == NULL) {
Georg Brandlccc47b62008-12-28 11:44:14 +00001264 for (i = 0; i < 256; i++)
1265 trans_table[i] = Py_CHARMASK(i);
1266 } else {
1267 for (i = 0; i < 256; i++)
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001268 trans_table[i] = Py_CHARMASK(table_chars[i]);
Georg Brandlccc47b62008-12-28 11:44:14 +00001269 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001270
1271 for (i = 0; i < vdel.len; i++)
1272 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1273
1274 for (i = inlen; --i >= 0; ) {
1275 c = Py_CHARMASK(*input++);
1276 if (trans_table[c] != -1)
Martin Panter1b6c6da2016-08-27 08:35:02 +00001277 *output++ = (char)trans_table[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001278 }
1279 /* Fix the size of the resulting string */
1280 if (inlen > 0)
Christian Heimesc731bbe2013-07-21 02:04:35 +02001281 if (PyByteArray_Resize(result, output - output_start) < 0) {
1282 Py_CLEAR(result);
1283 goto done;
1284 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001285
1286done:
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001287 if (table != NULL)
Georg Brandlccc47b62008-12-28 11:44:14 +00001288 PyBuffer_Release(&vtable);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001289 if (deletechars != NULL)
Martin v. Löwis423be952008-08-13 15:53:07 +00001290 PyBuffer_Release(&vdel);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001291 return result;
1292}
1293
1294
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001295/*[clinic input]
1296
1297@staticmethod
1298bytearray.maketrans
1299
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001300 frm: Py_buffer
1301 to: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001302 /
1303
1304Return a translation table useable for the bytes or bytearray translate method.
1305
1306The returned table will be one where each byte in frm is mapped to the byte at
1307the same position in to.
1308
1309The bytes objects frm and to must be of the same length.
1310[clinic start generated code]*/
1311
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001312static PyObject *
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001313bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001314/*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001315{
1316 return _Py_bytes_maketrans(frm, to);
Georg Brandlabc38772009-04-12 15:51:51 +00001317}
1318
1319
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001320/*[clinic input]
1321bytearray.replace
1322
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001323 old: Py_buffer
1324 new: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001325 count: Py_ssize_t = -1
1326 Maximum number of occurrences to replace.
1327 -1 (the default value) means replace all occurrences.
1328 /
1329
1330Return a copy with all occurrences of substring old replaced by new.
1331
1332If the optional argument count is given, only the first count occurrences are
1333replaced.
1334[clinic start generated code]*/
1335
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001336static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001337bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
1338 Py_buffer *new, Py_ssize_t count)
1339/*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001340{
Serhiy Storchakafb81d3c2016-05-05 09:26:07 +03001341 return stringlib_replace((PyObject *)self,
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001342 (const char *)old->buf, old->len,
1343 (const char *)new->buf, new->len, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001344}
1345
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001346/*[clinic input]
1347bytearray.split
1348
1349 sep: object = None
1350 The delimiter according which to split the bytearray.
1351 None (the default value) means split on ASCII whitespace characters
1352 (space, tab, return, newline, formfeed, vertical tab).
1353 maxsplit: Py_ssize_t = -1
1354 Maximum number of splits to do.
1355 -1 (the default value) means no limit.
1356
1357Return a list of the sections in the bytearray, using sep as the delimiter.
1358[clinic start generated code]*/
1359
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001360static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001361bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
1362 Py_ssize_t maxsplit)
1363/*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001364{
1365 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001366 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001367 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001368 Py_buffer vsub;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001369
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001370 if (maxsplit < 0)
1371 maxsplit = PY_SSIZE_T_MAX;
1372
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001373 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001374 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001375
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001376 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001377 return NULL;
1378 sub = vsub.buf;
1379 n = vsub.len;
1380
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001381 list = stringlib_split(
1382 (PyObject*) self, s, len, sub, n, maxsplit
1383 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001384 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001385 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001386}
1387
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001388/*[clinic input]
1389bytearray.partition
1390
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001391 sep: object
1392 /
1393
1394Partition the bytearray into three parts using the given separator.
1395
1396This will search for the separator sep in the bytearray. If the separator is
1397found, returns a 3-tuple containing the part before the separator, the
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001398separator itself, and the part after it as new bytearray objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001399
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001400If the separator is not found, returns a 3-tuple containing the copy of the
1401original bytearray object and two empty bytearray objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001402[clinic start generated code]*/
1403
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001404static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001405bytearray_partition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001406/*[clinic end generated code: output=45d2525ddd35f957 input=8f644749ee4fc83a]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001407{
1408 PyObject *bytesep, *result;
1409
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001410 bytesep = _PyByteArray_FromBufferObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001411 if (! bytesep)
1412 return NULL;
1413
1414 result = stringlib_partition(
1415 (PyObject*) self,
1416 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1417 bytesep,
1418 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1419 );
1420
1421 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001422 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001423}
1424
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001425/*[clinic input]
1426bytearray.rpartition
1427
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001428 sep: object
1429 /
1430
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001431Partition the bytearray into three parts using the given separator.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001432
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001433This will search for the separator sep in the bytearray, starting at the end.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001434If the separator is found, returns a 3-tuple containing the part before the
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001435separator, the separator itself, and the part after it as new bytearray
1436objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001437
1438If the separator is not found, returns a 3-tuple containing two empty bytearray
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001439objects and the copy of the original bytearray object.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001440[clinic start generated code]*/
1441
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001442static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001443bytearray_rpartition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001444/*[clinic end generated code: output=440de3c9426115e8 input=7e3df3e6cb8fa0ac]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001445{
1446 PyObject *bytesep, *result;
1447
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001448 bytesep = _PyByteArray_FromBufferObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001449 if (! bytesep)
1450 return NULL;
1451
1452 result = stringlib_rpartition(
1453 (PyObject*) self,
1454 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1455 bytesep,
1456 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1457 );
1458
1459 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001460 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001461}
1462
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001463/*[clinic input]
1464bytearray.rsplit = bytearray.split
1465
1466Return a list of the sections in the bytearray, using sep as the delimiter.
1467
1468Splitting is done starting at the end of the bytearray and working to the front.
1469[clinic start generated code]*/
1470
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001471static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001472bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
1473 Py_ssize_t maxsplit)
1474/*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001475{
1476 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001477 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001478 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001479 Py_buffer vsub;
1480
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001481 if (maxsplit < 0)
1482 maxsplit = PY_SSIZE_T_MAX;
1483
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001484 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001485 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001486
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001487 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001488 return NULL;
1489 sub = vsub.buf;
1490 n = vsub.len;
1491
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001492 list = stringlib_rsplit(
1493 (PyObject*) self, s, len, sub, n, maxsplit
1494 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001495 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001496 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001497}
1498
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001499/*[clinic input]
1500bytearray.reverse
1501
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001502Reverse the order of the values in B in place.
1503[clinic start generated code]*/
1504
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001505static PyObject *
1506bytearray_reverse_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001507/*[clinic end generated code: output=9f7616f29ab309d3 input=543356319fc78557]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001508{
1509 char swap, *head, *tail;
1510 Py_ssize_t i, j, n = Py_SIZE(self);
1511
1512 j = n / 2;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001513 head = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001514 tail = head + n - 1;
1515 for (i = 0; i < j; i++) {
1516 swap = *head;
1517 *head++ = *tail;
1518 *tail-- = swap;
1519 }
1520
1521 Py_RETURN_NONE;
1522}
1523
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001524
1525/*[python input]
1526class bytesvalue_converter(CConverter):
1527 type = 'int'
1528 converter = '_getbytevalue'
1529[python start generated code]*/
1530/*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/
1531
1532
1533/*[clinic input]
1534bytearray.insert
1535
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001536 index: Py_ssize_t
1537 The index where the value is to be inserted.
1538 item: bytesvalue
1539 The item to be inserted.
1540 /
1541
1542Insert a single item into the bytearray before the given index.
1543[clinic start generated code]*/
1544
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001545static PyObject *
1546bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001547/*[clinic end generated code: output=76c775a70e7b07b7 input=b2b5d07e9de6c070]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001548{
1549 Py_ssize_t n = Py_SIZE(self);
1550 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001551
1552 if (n == PY_SSIZE_T_MAX) {
1553 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001554 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001555 return NULL;
1556 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001557 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1558 return NULL;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001559 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001560
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001561 if (index < 0) {
1562 index += n;
1563 if (index < 0)
1564 index = 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001565 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001566 if (index > n)
1567 index = n;
1568 memmove(buf + index + 1, buf + index, n - index);
1569 buf[index] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001570
1571 Py_RETURN_NONE;
1572}
1573
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001574/*[clinic input]
1575bytearray.append
1576
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001577 item: bytesvalue
1578 The item to be appended.
1579 /
1580
1581Append a single item to the end of the bytearray.
1582[clinic start generated code]*/
1583
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001584static PyObject *
1585bytearray_append_impl(PyByteArrayObject *self, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001586/*[clinic end generated code: output=a154e19ed1886cb6 input=20d6bec3d1340593]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001587{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001588 Py_ssize_t n = Py_SIZE(self);
1589
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001590 if (n == PY_SSIZE_T_MAX) {
1591 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001592 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001593 return NULL;
1594 }
1595 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1596 return NULL;
1597
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001598 PyByteArray_AS_STRING(self)[n] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001599
1600 Py_RETURN_NONE;
1601}
1602
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001603/*[clinic input]
1604bytearray.extend
1605
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001606 iterable_of_ints: object
1607 The iterable of items to append.
1608 /
1609
1610Append all the items from the iterator or sequence to the end of the bytearray.
1611[clinic start generated code]*/
1612
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001613static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001614bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001615/*[clinic end generated code: output=98155dbe249170b1 input=c617b3a93249ba28]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001616{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001617 PyObject *it, *item, *bytearray_obj;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001618 Py_ssize_t buf_size = 0, len = 0;
1619 int value;
1620 char *buf;
1621
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001622 /* bytearray_setslice code only accepts something supporting PEP 3118. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001623 if (PyObject_CheckBuffer(iterable_of_ints)) {
1624 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_ints) == -1)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001625 return NULL;
1626
1627 Py_RETURN_NONE;
1628 }
1629
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001630 it = PyObject_GetIter(iterable_of_ints);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +03001631 if (it == NULL) {
1632 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
1633 PyErr_Format(PyExc_TypeError,
1634 "can't extend bytearray with %.100s",
1635 iterable_of_ints->ob_type->tp_name);
1636 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001637 return NULL;
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +03001638 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001639
Ezio Melotti42da6632011-03-15 05:18:48 +02001640 /* Try to determine the length of the argument. 32 is arbitrary. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001641 buf_size = PyObject_LengthHint(iterable_of_ints, 32);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001642 if (buf_size == -1) {
1643 Py_DECREF(it);
1644 return NULL;
1645 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001646
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001647 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001648 if (bytearray_obj == NULL) {
1649 Py_DECREF(it);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001650 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001651 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001652 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001653
1654 while ((item = PyIter_Next(it)) != NULL) {
1655 if (! _getbytevalue(item, &value)) {
1656 Py_DECREF(item);
1657 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001658 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001659 return NULL;
1660 }
1661 buf[len++] = value;
1662 Py_DECREF(item);
1663
1664 if (len >= buf_size) {
Martin Panter371731e2016-07-18 07:53:13 +00001665 Py_ssize_t addition;
1666 if (len == PY_SSIZE_T_MAX) {
1667 Py_DECREF(it);
1668 Py_DECREF(bytearray_obj);
1669 return PyErr_NoMemory();
1670 }
1671 addition = len >> 1;
1672 if (addition > PY_SSIZE_T_MAX - len - 1)
1673 buf_size = PY_SSIZE_T_MAX;
1674 else
1675 buf_size = len + addition + 1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001676 if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001677 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001678 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001679 return NULL;
1680 }
1681 /* Recompute the `buf' pointer, since the resizing operation may
1682 have invalidated it. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001683 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001684 }
1685 }
1686 Py_DECREF(it);
1687
1688 /* Resize down to exact size. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001689 if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) {
1690 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001691 return NULL;
1692 }
1693
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001694 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) {
1695 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001696 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001697 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001698 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001699
1700 Py_RETURN_NONE;
1701}
1702
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001703/*[clinic input]
1704bytearray.pop
1705
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001706 index: Py_ssize_t = -1
1707 The index from where to remove the item.
1708 -1 (the default value) means remove the last item.
1709 /
1710
1711Remove and return a single item from B.
1712
1713If no index argument is given, will pop the last item.
1714[clinic start generated code]*/
1715
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001716static PyObject *
1717bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001718/*[clinic end generated code: output=e0ccd401f8021da8 input=3591df2d06c0d237]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001719{
1720 int value;
1721 Py_ssize_t n = Py_SIZE(self);
1722 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001723
1724 if (n == 0) {
Eli Bendersky1bc4f192011-03-04 04:55:25 +00001725 PyErr_SetString(PyExc_IndexError,
1726 "pop from empty bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001727 return NULL;
1728 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001729 if (index < 0)
1730 index += Py_SIZE(self);
1731 if (index < 0 || index >= Py_SIZE(self)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001732 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1733 return NULL;
1734 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001735 if (!_canresize(self))
1736 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001737
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001738 buf = PyByteArray_AS_STRING(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001739 value = buf[index];
1740 memmove(buf + index, buf + index + 1, n - index);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001741 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1742 return NULL;
1743
Mark Dickinson54a3db92009-09-06 10:19:23 +00001744 return PyLong_FromLong((unsigned char)value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001745}
1746
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001747/*[clinic input]
1748bytearray.remove
1749
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001750 value: bytesvalue
1751 The value to remove.
1752 /
1753
1754Remove the first occurrence of a value in the bytearray.
1755[clinic start generated code]*/
1756
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001757static PyObject *
1758bytearray_remove_impl(PyByteArrayObject *self, int value)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001759/*[clinic end generated code: output=d659e37866709c13 input=121831240cd51ddf]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001760{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001761 Py_ssize_t where, n = Py_SIZE(self);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001762 char *buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001763
Serhiy Storchaka4b234942016-05-16 22:24:03 +03001764 where = stringlib_find_char(buf, n, value);
1765 if (where < 0) {
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001766 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001767 return NULL;
1768 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001769 if (!_canresize(self))
1770 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001771
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001772 memmove(buf + where, buf + where + 1, n - where);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001773 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1774 return NULL;
1775
1776 Py_RETURN_NONE;
1777}
1778
1779/* XXX These two helpers could be optimized if argsize == 1 */
1780
1781static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001782lstrip_helper(const char *myptr, Py_ssize_t mysize,
1783 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001784{
1785 Py_ssize_t i = 0;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001786 while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001787 i++;
1788 return i;
1789}
1790
1791static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001792rstrip_helper(const char *myptr, Py_ssize_t mysize,
1793 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001794{
1795 Py_ssize_t i = mysize - 1;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001796 while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001797 i--;
1798 return i + 1;
1799}
1800
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001801/*[clinic input]
1802bytearray.strip
1803
1804 bytes: object = None
1805 /
1806
1807Strip leading and trailing bytes contained in the argument.
1808
1809If the argument is omitted or None, strip leading and trailing ASCII whitespace.
1810[clinic start generated code]*/
1811
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001812static PyObject *
1813bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001814/*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001815{
1816 Py_ssize_t left, right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001817 char *myptr;
1818 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001819 Py_buffer vbytes;
1820
1821 if (bytes == Py_None) {
1822 bytesptr = "\t\n\r\f\v ";
1823 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001824 }
1825 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001826 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001827 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001828 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001829 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001830 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001831 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001832 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001833 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001834 if (left == mysize)
1835 right = left;
1836 else
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001837 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1838 if (bytes != Py_None)
1839 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001840 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001841}
1842
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001843/*[clinic input]
1844bytearray.lstrip
1845
1846 bytes: object = None
1847 /
1848
1849Strip leading bytes contained in the argument.
1850
1851If the argument is omitted or None, strip leading ASCII whitespace.
1852[clinic start generated code]*/
1853
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001854static PyObject *
1855bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001856/*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001857{
1858 Py_ssize_t left, right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001859 char *myptr;
1860 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001861 Py_buffer vbytes;
1862
1863 if (bytes == Py_None) {
1864 bytesptr = "\t\n\r\f\v ";
1865 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001866 }
1867 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001868 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001869 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001870 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001871 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001872 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001873 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001874 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001875 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001876 right = mysize;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001877 if (bytes != Py_None)
1878 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001879 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001880}
1881
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001882/*[clinic input]
1883bytearray.rstrip
1884
1885 bytes: object = None
1886 /
1887
1888Strip trailing bytes contained in the argument.
1889
1890If the argument is omitted or None, strip trailing ASCII whitespace.
1891[clinic start generated code]*/
1892
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001893static PyObject *
1894bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001895/*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001896{
1897 Py_ssize_t right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001898 char *myptr;
1899 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001900 Py_buffer vbytes;
1901
1902 if (bytes == Py_None) {
1903 bytesptr = "\t\n\r\f\v ";
1904 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001905 }
1906 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001907 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001908 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001909 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001910 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001911 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001912 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001913 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001914 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1915 if (bytes != Py_None)
1916 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001917 return PyByteArray_FromStringAndSize(myptr, right);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001918}
1919
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001920/*[clinic input]
1921bytearray.decode
1922
1923 encoding: str(c_default="NULL") = 'utf-8'
1924 The encoding with which to decode the bytearray.
1925 errors: str(c_default="NULL") = 'strict'
1926 The error handling scheme to use for the handling of decoding errors.
1927 The default is 'strict' meaning that decoding errors raise a
1928 UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
1929 as well as any other name registered with codecs.register_error that
1930 can handle UnicodeDecodeErrors.
1931
1932Decode the bytearray using the codec registered for encoding.
1933[clinic start generated code]*/
1934
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001935static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001936bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
1937 const char *errors)
1938/*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001939{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001940 if (encoding == NULL)
1941 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis0efea322014-07-27 17:29:17 +02001942 return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001943}
1944
1945PyDoc_STRVAR(alloc_doc,
1946"B.__alloc__() -> int\n\
1947\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00001948Return the number of bytes actually allocated.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001949
1950static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301951bytearray_alloc(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001952{
1953 return PyLong_FromSsize_t(self->ob_alloc);
1954}
1955
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001956/*[clinic input]
1957bytearray.join
1958
1959 iterable_of_bytes: object
1960 /
1961
1962Concatenate any number of bytes/bytearray objects.
1963
1964The bytearray whose method is called is inserted in between each pair.
1965
1966The result is returned as a new bytearray object.
1967[clinic start generated code]*/
1968
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001969static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001970bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001971/*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001972{
Martin v. Löwis0efea322014-07-27 17:29:17 +02001973 return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001974}
1975
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001976/*[clinic input]
1977bytearray.splitlines
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001978
Serhiy Storchaka202fda52017-03-12 10:10:47 +02001979 keepends: bool(accept={int}) = False
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001980
1981Return a list of the lines in the bytearray, breaking at line boundaries.
1982
1983Line breaks are not included in the resulting list unless keepends is given and
1984true.
1985[clinic start generated code]*/
1986
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001987static PyObject *
1988bytearray_splitlines_impl(PyByteArrayObject *self, int keepends)
Serhiy Storchaka202fda52017-03-12 10:10:47 +02001989/*[clinic end generated code: output=4223c94b895f6ad9 input=99a27ad959b9cf6b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001990{
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001991 return stringlib_splitlines(
1992 (PyObject*) self, PyByteArray_AS_STRING(self),
1993 PyByteArray_GET_SIZE(self), keepends
1994 );
1995}
1996
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001997/*[clinic input]
1998@classmethod
1999bytearray.fromhex
2000
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002001 string: unicode
2002 /
2003
2004Create a bytearray object from a string of hexadecimal numbers.
2005
2006Spaces between two numbers are accepted.
2007Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')
2008[clinic start generated code]*/
2009
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002010static PyObject *
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002011bytearray_fromhex_impl(PyTypeObject *type, PyObject *string)
2012/*[clinic end generated code: output=8f0f0b6d30fb3ba0 input=f033a16d1fb21f48]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002013{
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002014 PyObject *result = _PyBytes_FromHex(string, type == &PyByteArray_Type);
2015 if (type != &PyByteArray_Type && result != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002016 Py_SETREF(result, PyObject_CallFunctionObjArgs((PyObject *)type,
2017 result, NULL));
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002018 }
2019 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002020}
2021
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002022PyDoc_STRVAR(hex__doc__,
2023"B.hex() -> string\n\
2024\n\
2025Create a string of hexadecimal numbers from a bytearray object.\n\
2026Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'.");
2027
2028static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302029bytearray_hex(PyBytesObject *self, PyObject *Py_UNUSED(ignored))
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002030{
2031 char* argbuf = PyByteArray_AS_STRING(self);
2032 Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
2033 return _Py_strhex(argbuf, arglen);
2034}
2035
2036static PyObject *
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002037_common_reduce(PyByteArrayObject *self, int proto)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002038{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002039 PyObject *dict;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002040 _Py_IDENTIFIER(__dict__);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002041 char *buf;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002042
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002043 dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002044 if (dict == NULL) {
2045 PyErr_Clear();
2046 dict = Py_None;
2047 Py_INCREF(dict);
2048 }
2049
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002050 buf = PyByteArray_AS_STRING(self);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002051 if (proto < 3) {
2052 /* use str based reduction for backwards compatibility with Python 2.x */
2053 PyObject *latin1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002054 if (Py_SIZE(self))
2055 latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002056 else
2057 latin1 = PyUnicode_FromString("");
2058 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
2059 }
2060 else {
2061 /* use more efficient byte based reduction */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002062 if (Py_SIZE(self)) {
2063 return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002064 }
2065 else {
2066 return Py_BuildValue("(O()N)", Py_TYPE(self), dict);
2067 }
2068 }
2069}
2070
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002071/*[clinic input]
2072bytearray.__reduce__ as bytearray_reduce
2073
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002074Return state information for pickling.
2075[clinic start generated code]*/
2076
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002077static PyObject *
2078bytearray_reduce_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002079/*[clinic end generated code: output=52bf304086464cab input=44b5737ada62dd3f]*/
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002080{
2081 return _common_reduce(self, 2);
2082}
2083
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002084/*[clinic input]
2085bytearray.__reduce_ex__ as bytearray_reduce_ex
2086
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002087 proto: int = 0
2088 /
2089
2090Return state information for pickling.
2091[clinic start generated code]*/
2092
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002093static PyObject *
2094bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002095/*[clinic end generated code: output=52eac33377197520 input=f129bc1a1aa151ee]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002096{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002097 return _common_reduce(self, proto);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002098}
2099
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002100/*[clinic input]
2101bytearray.__sizeof__ as bytearray_sizeof
2102
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002103Returns the size of the bytearray object in memory, in bytes.
2104[clinic start generated code]*/
2105
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002106static PyObject *
2107bytearray_sizeof_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002108/*[clinic end generated code: output=738abdd17951c427 input=e27320fd98a4bc5a]*/
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002109{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002110 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002111
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002112 res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002113 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002114}
2115
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002116static PySequenceMethods bytearray_as_sequence = {
2117 (lenfunc)bytearray_length, /* sq_length */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002118 (binaryfunc)PyByteArray_Concat, /* sq_concat */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002119 (ssizeargfunc)bytearray_repeat, /* sq_repeat */
2120 (ssizeargfunc)bytearray_getitem, /* sq_item */
2121 0, /* sq_slice */
2122 (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */
2123 0, /* sq_ass_slice */
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002124 (objobjproc)bytearray_contains, /* sq_contains */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002125 (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */
2126 (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002127};
2128
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002129static PyMappingMethods bytearray_as_mapping = {
2130 (lenfunc)bytearray_length,
2131 (binaryfunc)bytearray_subscript,
2132 (objobjargproc)bytearray_ass_subscript,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002133};
2134
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002135static PyBufferProcs bytearray_as_buffer = {
2136 (getbufferproc)bytearray_getbuffer,
2137 (releasebufferproc)bytearray_releasebuffer,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002138};
2139
2140static PyMethodDef
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002141bytearray_methods[] = {
2142 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002143 BYTEARRAY_REDUCE_METHODDEF
2144 BYTEARRAY_REDUCE_EX_METHODDEF
2145 BYTEARRAY_SIZEOF_METHODDEF
2146 BYTEARRAY_APPEND_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302147 {"capitalize", stringlib_capitalize, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002148 _Py_capitalize__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002149 STRINGLIB_CENTER_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002150 BYTEARRAY_CLEAR_METHODDEF
2151 BYTEARRAY_COPY_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002152 {"count", (PyCFunction)bytearray_count, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002153 _Py_count__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002154 BYTEARRAY_DECODE_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002155 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002156 _Py_endswith__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002157 STRINGLIB_EXPANDTABS_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002158 BYTEARRAY_EXTEND_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002159 {"find", (PyCFunction)bytearray_find, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002160 _Py_find__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002161 BYTEARRAY_FROMHEX_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002162 {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__},
2163 {"index", (PyCFunction)bytearray_index, METH_VARARGS, _Py_index__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002164 BYTEARRAY_INSERT_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302165 {"isalnum", stringlib_isalnum, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002166 _Py_isalnum__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302167 {"isalpha", stringlib_isalpha, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002168 _Py_isalpha__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302169 {"isascii", stringlib_isascii, METH_NOARGS,
INADA Naokia49ac992018-01-27 14:06:21 +09002170 _Py_isascii__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302171 {"isdigit", stringlib_isdigit, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002172 _Py_isdigit__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302173 {"islower", stringlib_islower, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002174 _Py_islower__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302175 {"isspace", stringlib_isspace, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002176 _Py_isspace__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302177 {"istitle", stringlib_istitle, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002178 _Py_istitle__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302179 {"isupper", stringlib_isupper, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002180 _Py_isupper__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002181 BYTEARRAY_JOIN_METHODDEF
Tal Einatc929df32018-07-06 13:17:38 +03002182 STRINGLIB_LJUST_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302183 {"lower", stringlib_lower, METH_NOARGS, _Py_lower__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002184 BYTEARRAY_LSTRIP_METHODDEF
2185 BYTEARRAY_MAKETRANS_METHODDEF
2186 BYTEARRAY_PARTITION_METHODDEF
2187 BYTEARRAY_POP_METHODDEF
2188 BYTEARRAY_REMOVE_METHODDEF
2189 BYTEARRAY_REPLACE_METHODDEF
2190 BYTEARRAY_REVERSE_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002191 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__},
2192 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002193 STRINGLIB_RJUST_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002194 BYTEARRAY_RPARTITION_METHODDEF
2195 BYTEARRAY_RSPLIT_METHODDEF
2196 BYTEARRAY_RSTRIP_METHODDEF
2197 BYTEARRAY_SPLIT_METHODDEF
2198 BYTEARRAY_SPLITLINES_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002199 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002200 _Py_startswith__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002201 BYTEARRAY_STRIP_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302202 {"swapcase", stringlib_swapcase, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002203 _Py_swapcase__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302204 {"title", stringlib_title, METH_NOARGS, _Py_title__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002205 BYTEARRAY_TRANSLATE_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302206 {"upper", stringlib_upper, METH_NOARGS, _Py_upper__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002207 STRINGLIB_ZFILL_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002208 {NULL}
2209};
2210
Ethan Furmanb95b5612015-01-23 20:05:18 -08002211static PyObject *
2212bytearray_mod(PyObject *v, PyObject *w)
2213{
2214 if (!PyByteArray_Check(v))
2215 Py_RETURN_NOTIMPLEMENTED;
Berker Peksag43de36d2016-04-16 01:20:47 +03002216 return _PyBytes_FormatEx(PyByteArray_AS_STRING(v), PyByteArray_GET_SIZE(v), w, 1);
Ethan Furmanb95b5612015-01-23 20:05:18 -08002217}
2218
2219static PyNumberMethods bytearray_as_number = {
2220 0, /*nb_add*/
2221 0, /*nb_subtract*/
2222 0, /*nb_multiply*/
2223 bytearray_mod, /*nb_remainder*/
2224};
2225
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002226PyDoc_STRVAR(bytearray_doc,
Georg Brandl17cb8a82008-05-30 08:20:09 +00002227"bytearray(iterable_of_ints) -> bytearray\n\
2228bytearray(string, encoding[, errors]) -> bytearray\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002229bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\
2230bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\
2231bytearray() -> empty bytes array\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002232\n\
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03002233Construct a mutable bytearray object from:\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002234 - an iterable yielding integers in range(256)\n\
2235 - a text string encoded using the specified encoding\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002236 - a bytes or a buffer object\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002237 - any object implementing the buffer API.\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002238 - an integer");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002239
2240
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002241static PyObject *bytearray_iter(PyObject *seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002242
2243PyTypeObject PyByteArray_Type = {
2244 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2245 "bytearray",
2246 sizeof(PyByteArrayObject),
2247 0,
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002248 (destructor)bytearray_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002249 0, /* tp_print */
2250 0, /* tp_getattr */
2251 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002252 0, /* tp_reserved */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002253 (reprfunc)bytearray_repr, /* tp_repr */
Ethan Furmanb95b5612015-01-23 20:05:18 -08002254 &bytearray_as_number, /* tp_as_number */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002255 &bytearray_as_sequence, /* tp_as_sequence */
2256 &bytearray_as_mapping, /* tp_as_mapping */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002257 0, /* tp_hash */
2258 0, /* tp_call */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002259 bytearray_str, /* tp_str */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002260 PyObject_GenericGetAttr, /* tp_getattro */
2261 0, /* tp_setattro */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002262 &bytearray_as_buffer, /* tp_as_buffer */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002263 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002264 bytearray_doc, /* tp_doc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002265 0, /* tp_traverse */
2266 0, /* tp_clear */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002267 (richcmpfunc)bytearray_richcompare, /* tp_richcompare */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002268 0, /* tp_weaklistoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002269 bytearray_iter, /* tp_iter */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002270 0, /* tp_iternext */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002271 bytearray_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002272 0, /* tp_members */
2273 0, /* tp_getset */
2274 0, /* tp_base */
2275 0, /* tp_dict */
2276 0, /* tp_descr_get */
2277 0, /* tp_descr_set */
2278 0, /* tp_dictoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002279 (initproc)bytearray_init, /* tp_init */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002280 PyType_GenericAlloc, /* tp_alloc */
2281 PyType_GenericNew, /* tp_new */
2282 PyObject_Del, /* tp_free */
2283};
2284
2285/*********************** Bytes Iterator ****************************/
2286
2287typedef struct {
2288 PyObject_HEAD
2289 Py_ssize_t it_index;
2290 PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
2291} bytesiterobject;
2292
2293static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002294bytearrayiter_dealloc(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002295{
2296 _PyObject_GC_UNTRACK(it);
2297 Py_XDECREF(it->it_seq);
2298 PyObject_GC_Del(it);
2299}
2300
2301static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002302bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002303{
2304 Py_VISIT(it->it_seq);
2305 return 0;
2306}
2307
2308static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002309bytearrayiter_next(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002310{
2311 PyByteArrayObject *seq;
2312 PyObject *item;
2313
2314 assert(it != NULL);
2315 seq = it->it_seq;
2316 if (seq == NULL)
2317 return NULL;
2318 assert(PyByteArray_Check(seq));
2319
2320 if (it->it_index < PyByteArray_GET_SIZE(seq)) {
2321 item = PyLong_FromLong(
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002322 (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002323 if (item != NULL)
2324 ++it->it_index;
2325 return item;
2326 }
2327
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002328 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03002329 Py_DECREF(seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002330 return NULL;
2331}
2332
2333static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302334bytearrayiter_length_hint(bytesiterobject *it, PyObject *Py_UNUSED(ignored))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002335{
2336 Py_ssize_t len = 0;
Serhiy Storchakaaf658722016-07-03 14:41:36 +03002337 if (it->it_seq) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002338 len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
Serhiy Storchakaaf658722016-07-03 14:41:36 +03002339 if (len < 0) {
2340 len = 0;
2341 }
2342 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002343 return PyLong_FromSsize_t(len);
2344}
2345
2346PyDoc_STRVAR(length_hint_doc,
2347 "Private method returning an estimate of len(list(it)).");
2348
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002349static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302350bytearrayiter_reduce(bytesiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002351{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002352 _Py_IDENTIFIER(iter);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002353 if (it->it_seq != NULL) {
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002354 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002355 it->it_seq, it->it_index);
2356 } else {
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002357 return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002358 }
2359}
2360
2361static PyObject *
2362bytearrayiter_setstate(bytesiterobject *it, PyObject *state)
2363{
2364 Py_ssize_t index = PyLong_AsSsize_t(state);
2365 if (index == -1 && PyErr_Occurred())
2366 return NULL;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00002367 if (it->it_seq != NULL) {
2368 if (index < 0)
2369 index = 0;
2370 else if (index > PyByteArray_GET_SIZE(it->it_seq))
2371 index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */
2372 it->it_index = index;
2373 }
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002374 Py_RETURN_NONE;
2375}
2376
2377PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
2378
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002379static PyMethodDef bytearrayiter_methods[] = {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002380 {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002381 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002382 {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002383 bytearray_reduce__doc__},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002384 {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
2385 setstate_doc},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002386 {NULL, NULL} /* sentinel */
2387};
2388
2389PyTypeObject PyByteArrayIter_Type = {
2390 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2391 "bytearray_iterator", /* tp_name */
2392 sizeof(bytesiterobject), /* tp_basicsize */
2393 0, /* tp_itemsize */
2394 /* methods */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002395 (destructor)bytearrayiter_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002396 0, /* tp_print */
2397 0, /* tp_getattr */
2398 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002399 0, /* tp_reserved */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002400 0, /* tp_repr */
2401 0, /* tp_as_number */
2402 0, /* tp_as_sequence */
2403 0, /* tp_as_mapping */
2404 0, /* tp_hash */
2405 0, /* tp_call */
2406 0, /* tp_str */
2407 PyObject_GenericGetAttr, /* tp_getattro */
2408 0, /* tp_setattro */
2409 0, /* tp_as_buffer */
2410 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
2411 0, /* tp_doc */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002412 (traverseproc)bytearrayiter_traverse, /* tp_traverse */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002413 0, /* tp_clear */
2414 0, /* tp_richcompare */
2415 0, /* tp_weaklistoffset */
2416 PyObject_SelfIter, /* tp_iter */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002417 (iternextfunc)bytearrayiter_next, /* tp_iternext */
2418 bytearrayiter_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002419 0,
2420};
2421
2422static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002423bytearray_iter(PyObject *seq)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002424{
2425 bytesiterobject *it;
2426
2427 if (!PyByteArray_Check(seq)) {
2428 PyErr_BadInternalCall();
2429 return NULL;
2430 }
2431 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
2432 if (it == NULL)
2433 return NULL;
2434 it->it_index = 0;
2435 Py_INCREF(seq);
2436 it->it_seq = (PyByteArrayObject *)seq;
2437 _PyObject_GC_TRACK(it);
2438 return (PyObject *)it;
2439}