blob: 3926095a1812e3c2478e71a41fd1238156023903 [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
20void
21PyByteArray_Fini(void)
22{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000023}
24
25int
26PyByteArray_Init(void)
27{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000028 return 1;
29}
30
31/* end nullbytes support */
32
33/* Helpers */
34
35static int
36_getbytevalue(PyObject* arg, int *value)
37{
38 long face_value;
39
40 if (PyLong_Check(arg)) {
41 face_value = PyLong_AsLong(arg);
Georg Brandl9a54d7c2008-07-16 23:15:30 +000042 } else {
43 PyObject *index = PyNumber_Index(arg);
44 if (index == NULL) {
Mark Dickinson10de93a2010-07-09 19:25:48 +000045 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000046 return 0;
47 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +000048 face_value = PyLong_AsLong(index);
49 Py_DECREF(index);
50 }
51
52 if (face_value < 0 || face_value >= 256) {
53 /* this includes the OverflowError in case the long is too large */
54 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
Mark Dickinson10de93a2010-07-09 19:25:48 +000055 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000056 return 0;
57 }
58
59 *value = face_value;
60 return 1;
61}
62
63static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +000064bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000065{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000066 void *ptr;
67 if (view == NULL) {
Stefan Krah5178d912015-02-03 16:57:21 +010068 PyErr_SetString(PyExc_BufferError,
69 "bytearray_getbuffer: view==NULL argument is obsolete");
70 return -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000071 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000072 ptr = (void *) PyByteArray_AS_STRING(obj);
Stefan Krah5178d912015-02-03 16:57:21 +010073 /* cannot fail if view != NULL and readonly == 0 */
74 (void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
75 obj->ob_exports++;
76 return 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000077}
78
79static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +000080bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000081{
82 obj->ob_exports--;
83}
84
Antoine Pitrou5504e892008-12-06 21:27:53 +000085static int
86_canresize(PyByteArrayObject *self)
87{
88 if (self->ob_exports > 0) {
89 PyErr_SetString(PyExc_BufferError,
90 "Existing exports of data: object cannot be re-sized");
91 return 0;
92 }
93 return 1;
94}
95
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030096#include "clinic/bytearrayobject.c.h"
97
Christian Heimes2c9c7a52008-05-26 13:42:13 +000098/* Direct API functions */
99
100PyObject *
101PyByteArray_FromObject(PyObject *input)
102{
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100103 return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type,
104 input, NULL);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000105}
106
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300107static PyObject *
108_PyByteArray_FromBufferObject(PyObject *obj)
109{
110 PyObject *result;
111 Py_buffer view;
112
113 if (PyObject_GetBuffer(obj, &view, PyBUF_FULL_RO) < 0) {
114 return NULL;
115 }
116 result = PyByteArray_FromStringAndSize(NULL, view.len);
117 if (result != NULL &&
118 PyBuffer_ToContiguous(PyByteArray_AS_STRING(result),
119 &view, view.len, 'C') < 0)
120 {
121 Py_CLEAR(result);
122 }
123 PyBuffer_Release(&view);
124 return result;
125}
126
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000127PyObject *
128PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
129{
130 PyByteArrayObject *new;
131 Py_ssize_t alloc;
132
133 if (size < 0) {
134 PyErr_SetString(PyExc_SystemError,
135 "Negative size passed to PyByteArray_FromStringAndSize");
136 return NULL;
137 }
138
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000139 /* Prevent buffer overflow when setting alloc to size+1. */
140 if (size == PY_SSIZE_T_MAX) {
141 return PyErr_NoMemory();
142 }
143
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000144 new = PyObject_New(PyByteArrayObject, &PyByteArray_Type);
145 if (new == NULL)
146 return NULL;
147
148 if (size == 0) {
149 new->ob_bytes = NULL;
150 alloc = 0;
151 }
152 else {
153 alloc = size + 1;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100154 new->ob_bytes = PyObject_Malloc(alloc);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000155 if (new->ob_bytes == NULL) {
156 Py_DECREF(new);
157 return PyErr_NoMemory();
158 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +0000159 if (bytes != NULL && size > 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000160 memcpy(new->ob_bytes, bytes, size);
161 new->ob_bytes[size] = '\0'; /* Trailing null byte */
162 }
163 Py_SIZE(new) = size;
164 new->ob_alloc = alloc;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200165 new->ob_start = new->ob_bytes;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000166 new->ob_exports = 0;
167
168 return (PyObject *)new;
169}
170
171Py_ssize_t
172PyByteArray_Size(PyObject *self)
173{
174 assert(self != NULL);
175 assert(PyByteArray_Check(self));
176
177 return PyByteArray_GET_SIZE(self);
178}
179
180char *
181PyByteArray_AsString(PyObject *self)
182{
183 assert(self != NULL);
184 assert(PyByteArray_Check(self));
185
186 return PyByteArray_AS_STRING(self);
187}
188
189int
Antoine Pitroucc231542014-11-02 18:40:09 +0100190PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000191{
192 void *sval;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200193 PyByteArrayObject *obj = ((PyByteArrayObject *)self);
Antoine Pitroucc231542014-11-02 18:40:09 +0100194 /* All computations are done unsigned to avoid integer overflows
195 (see issue #22335). */
196 size_t alloc = (size_t) obj->ob_alloc;
197 size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes);
198 size_t size = (size_t) requested_size;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000199
200 assert(self != NULL);
201 assert(PyByteArray_Check(self));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200202 assert(logical_offset <= alloc);
Antoine Pitroucc231542014-11-02 18:40:09 +0100203 assert(requested_size >= 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000204
Antoine Pitroucc231542014-11-02 18:40:09 +0100205 if (requested_size == Py_SIZE(self)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000206 return 0;
207 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200208 if (!_canresize(obj)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000209 return -1;
210 }
211
Antoine Pitrou25454112015-05-19 20:52:27 +0200212 if (size + logical_offset + 1 <= alloc) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200213 /* Current buffer is large enough to host the requested size,
214 decide on a strategy. */
215 if (size < alloc / 2) {
216 /* Major downsize; resize down to exact size */
217 alloc = size + 1;
218 }
219 else {
220 /* Minor downsize; quick exit */
221 Py_SIZE(self) = size;
222 PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */
223 return 0;
224 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000225 }
226 else {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200227 /* Need growing, decide on a strategy */
228 if (size <= alloc * 1.125) {
229 /* Moderate upsize; overallocate similar to list_resize() */
230 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
231 }
232 else {
233 /* Major upsize; resize up to exact size */
234 alloc = size + 1;
235 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000236 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100237 if (alloc > PY_SSIZE_T_MAX) {
238 PyErr_NoMemory();
239 return -1;
240 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000241
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200242 if (logical_offset > 0) {
243 sval = PyObject_Malloc(alloc);
244 if (sval == NULL) {
245 PyErr_NoMemory();
246 return -1;
247 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100248 memcpy(sval, PyByteArray_AS_STRING(self),
Stefan Krahdce65022017-08-25 20:12:05 +0200249 Py_MIN((size_t)requested_size, (size_t)Py_SIZE(self)));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200250 PyObject_Free(obj->ob_bytes);
251 }
252 else {
253 sval = PyObject_Realloc(obj->ob_bytes, alloc);
254 if (sval == NULL) {
255 PyErr_NoMemory();
256 return -1;
257 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000258 }
259
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200260 obj->ob_bytes = obj->ob_start = sval;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000261 Py_SIZE(self) = size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200262 obj->ob_alloc = alloc;
263 obj->ob_bytes[size] = '\0'; /* Trailing null byte */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000264
265 return 0;
266}
267
268PyObject *
269PyByteArray_Concat(PyObject *a, PyObject *b)
270{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000271 Py_buffer va, vb;
272 PyByteArrayObject *result = NULL;
273
274 va.len = -1;
275 vb.len = -1;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200276 if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 ||
277 PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000278 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
Serhiy Storchaka6b5a9ec2017-03-19 19:47:02 +0200279 Py_TYPE(b)->tp_name, Py_TYPE(a)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000280 goto done;
281 }
282
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300283 if (va.len > PY_SSIZE_T_MAX - vb.len) {
284 PyErr_NoMemory();
285 goto done;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000286 }
287
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300288 result = (PyByteArrayObject *) \
289 PyByteArray_FromStringAndSize(NULL, va.len + vb.len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000290 if (result != NULL) {
291 memcpy(result->ob_bytes, va.buf, va.len);
292 memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
293 }
294
295 done:
296 if (va.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000297 PyBuffer_Release(&va);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000298 if (vb.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000299 PyBuffer_Release(&vb);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000300 return (PyObject *)result;
301}
302
303/* Functions stuffed into the type object */
304
305static Py_ssize_t
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000306bytearray_length(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000307{
308 return Py_SIZE(self);
309}
310
311static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000312bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000313{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000314 Py_ssize_t size;
315 Py_buffer vo;
316
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200317 if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000318 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
319 Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name);
320 return NULL;
321 }
322
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300323 size = Py_SIZE(self);
324 if (size > PY_SSIZE_T_MAX - vo.len) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000325 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000326 return PyErr_NoMemory();
327 }
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300328 if (PyByteArray_Resize((PyObject *)self, size + vo.len) < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000329 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000330 return NULL;
331 }
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300332 memcpy(PyByteArray_AS_STRING(self) + size, vo.buf, vo.len);
Martin v. Löwis423be952008-08-13 15:53:07 +0000333 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000334 Py_INCREF(self);
335 return (PyObject *)self;
336}
337
338static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000339bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000340{
341 PyByteArrayObject *result;
342 Py_ssize_t mysize;
343 Py_ssize_t size;
344
345 if (count < 0)
346 count = 0;
347 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000348 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000349 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000350 size = mysize * count;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000351 result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size);
352 if (result != NULL && size != 0) {
353 if (mysize == 1)
354 memset(result->ob_bytes, self->ob_bytes[0], size);
355 else {
356 Py_ssize_t i;
357 for (i = 0; i < count; i++)
358 memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize);
359 }
360 }
361 return (PyObject *)result;
362}
363
364static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000365bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000366{
367 Py_ssize_t mysize;
368 Py_ssize_t size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200369 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000370
371 if (count < 0)
372 count = 0;
373 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000374 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000375 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000376 size = mysize * count;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200377 if (PyByteArray_Resize((PyObject *)self, size) < 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000378 return NULL;
379
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200380 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000381 if (mysize == 1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200382 memset(buf, buf[0], size);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000383 else {
384 Py_ssize_t i;
385 for (i = 1; i < count; i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200386 memcpy(buf + i*mysize, buf, mysize);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000387 }
388
389 Py_INCREF(self);
390 return (PyObject *)self;
391}
392
393static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000394bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000395{
396 if (i < 0)
397 i += Py_SIZE(self);
398 if (i < 0 || i >= Py_SIZE(self)) {
399 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
400 return NULL;
401 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200402 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000403}
404
405static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000406bytearray_subscript(PyByteArrayObject *self, PyObject *index)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000407{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000408 if (PyIndex_Check(index)) {
409 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000410
411 if (i == -1 && PyErr_Occurred())
412 return NULL;
413
414 if (i < 0)
415 i += PyByteArray_GET_SIZE(self);
416
417 if (i < 0 || i >= Py_SIZE(self)) {
418 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
419 return NULL;
420 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200421 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000422 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000423 else if (PySlice_Check(index)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000424 Py_ssize_t start, stop, step, slicelength, cur, i;
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300425 if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000426 return NULL;
427 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300428 slicelength = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self),
429 &start, &stop, step);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000430
431 if (slicelength <= 0)
432 return PyByteArray_FromStringAndSize("", 0);
433 else if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200434 return PyByteArray_FromStringAndSize(
435 PyByteArray_AS_STRING(self) + start, slicelength);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000436 }
437 else {
438 char *source_buf = PyByteArray_AS_STRING(self);
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000439 char *result_buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000440 PyObject *result;
441
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000442 result = PyByteArray_FromStringAndSize(NULL, slicelength);
443 if (result == NULL)
444 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000445
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000446 result_buf = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000447 for (cur = start, i = 0; i < slicelength;
448 cur += step, i++) {
449 result_buf[i] = source_buf[cur];
450 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000451 return result;
452 }
453 }
454 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400455 PyErr_Format(PyExc_TypeError,
456 "bytearray indices must be integers or slices, not %.200s",
457 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000458 return NULL;
459 }
460}
461
462static int
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200463bytearray_setslice_linear(PyByteArrayObject *self,
464 Py_ssize_t lo, Py_ssize_t hi,
465 char *bytes, Py_ssize_t bytes_len)
466{
467 Py_ssize_t avail = hi - lo;
468 char *buf = PyByteArray_AS_STRING(self);
469 Py_ssize_t growth = bytes_len - avail;
Victor Stinner84557232013-11-21 12:29:51 +0100470 int res = 0;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200471 assert(avail >= 0);
472
Victor Stinner84557232013-11-21 12:29:51 +0100473 if (growth < 0) {
474 if (!_canresize(self))
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200475 return -1;
Victor Stinner84557232013-11-21 12:29:51 +0100476
477 if (lo == 0) {
478 /* Shrink the buffer by advancing its logical start */
479 self->ob_start -= growth;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200480 /*
Victor Stinner84557232013-11-21 12:29:51 +0100481 0 lo hi old_size
482 | |<----avail----->|<-----tail------>|
483 | |<-bytes_len->|<-----tail------>|
484 0 new_lo new_hi new_size
485 */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200486 }
Victor Stinner84557232013-11-21 12:29:51 +0100487 else {
488 /*
489 0 lo hi old_size
490 | |<----avail----->|<-----tomove------>|
491 | |<-bytes_len->|<-----tomove------>|
492 0 lo new_hi new_size
493 */
494 memmove(buf + lo + bytes_len, buf + hi,
495 Py_SIZE(self) - hi);
496 }
497 if (PyByteArray_Resize((PyObject *)self,
498 Py_SIZE(self) + growth) < 0) {
499 /* Issue #19578: Handling the memory allocation failure here is
500 tricky here because the bytearray object has already been
501 modified. Depending on growth and lo, the behaviour is
502 different.
503
504 If growth < 0 and lo != 0, the operation is completed, but a
505 MemoryError is still raised and the memory block is not
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700506 shrunk. Otherwise, the bytearray is restored in its previous
Victor Stinner84557232013-11-21 12:29:51 +0100507 state and a MemoryError is raised. */
508 if (lo == 0) {
509 self->ob_start += growth;
510 return -1;
511 }
512 /* memmove() removed bytes, the bytearray object cannot be
513 restored in its previous state. */
514 Py_SIZE(self) += growth;
515 res = -1;
516 }
517 buf = PyByteArray_AS_STRING(self);
518 }
519 else if (growth > 0) {
520 if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) {
521 PyErr_NoMemory();
522 return -1;
523 }
524
525 if (PyByteArray_Resize((PyObject *)self,
526 Py_SIZE(self) + growth) < 0) {
527 return -1;
528 }
529 buf = PyByteArray_AS_STRING(self);
530 /* Make the place for the additional bytes */
531 /*
532 0 lo hi old_size
533 | |<-avail->|<-----tomove------>|
534 | |<---bytes_len-->|<-----tomove------>|
535 0 lo new_hi new_size
536 */
537 memmove(buf + lo + bytes_len, buf + hi,
538 Py_SIZE(self) - lo - bytes_len);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200539 }
540
541 if (bytes_len > 0)
542 memcpy(buf + lo, bytes, bytes_len);
Victor Stinner84557232013-11-21 12:29:51 +0100543 return res;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200544}
545
546static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000547bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000548 PyObject *values)
549{
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200550 Py_ssize_t needed;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000551 void *bytes;
552 Py_buffer vbytes;
553 int res = 0;
554
555 vbytes.len = -1;
556 if (values == (PyObject *)self) {
557 /* Make a copy and call this function recursively */
558 int err;
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300559 values = PyByteArray_FromStringAndSize(PyByteArray_AS_STRING(values),
560 PyByteArray_GET_SIZE(values));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000561 if (values == NULL)
562 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000563 err = bytearray_setslice(self, lo, hi, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000564 Py_DECREF(values);
565 return err;
566 }
567 if (values == NULL) {
568 /* del b[lo:hi] */
569 bytes = NULL;
570 needed = 0;
571 }
572 else {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200573 if (PyObject_GetBuffer(values, &vbytes, PyBUF_SIMPLE) != 0) {
574 PyErr_Format(PyExc_TypeError,
575 "can't set bytearray slice from %.100s",
576 Py_TYPE(values)->tp_name);
577 return -1;
578 }
579 needed = vbytes.len;
580 bytes = vbytes.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000581 }
582
583 if (lo < 0)
584 lo = 0;
585 if (hi < lo)
586 hi = lo;
587 if (hi > Py_SIZE(self))
588 hi = Py_SIZE(self);
589
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200590 res = bytearray_setslice_linear(self, lo, hi, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000591 if (vbytes.len != -1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200592 PyBuffer_Release(&vbytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000593 return res;
594}
595
596static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000597bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000598{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000599 int ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000600
601 if (i < 0)
602 i += Py_SIZE(self);
603
604 if (i < 0 || i >= Py_SIZE(self)) {
605 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
606 return -1;
607 }
608
609 if (value == NULL)
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000610 return bytearray_setslice(self, i, i+1, NULL);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000611
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000612 if (!_getbytevalue(value, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000613 return -1;
614
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200615 PyByteArray_AS_STRING(self)[i] = ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000616 return 0;
617}
618
619static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000620bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000621{
622 Py_ssize_t start, stop, step, slicelen, needed;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200623 char *buf, *bytes;
624 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000625
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000626 if (PyIndex_Check(index)) {
627 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000628
629 if (i == -1 && PyErr_Occurred())
630 return -1;
631
632 if (i < 0)
633 i += PyByteArray_GET_SIZE(self);
634
635 if (i < 0 || i >= Py_SIZE(self)) {
636 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
637 return -1;
638 }
639
640 if (values == NULL) {
641 /* Fall through to slice assignment */
642 start = i;
643 stop = i + 1;
644 step = 1;
645 slicelen = 1;
646 }
647 else {
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000648 int ival;
649 if (!_getbytevalue(values, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000650 return -1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200651 buf[i] = (char)ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000652 return 0;
653 }
654 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000655 else if (PySlice_Check(index)) {
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300656 if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000657 return -1;
658 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300659 slicelen = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self), &start,
660 &stop, step);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000661 }
662 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400663 PyErr_Format(PyExc_TypeError,
664 "bytearray indices must be integers or slices, not %.200s",
665 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000666 return -1;
667 }
668
669 if (values == NULL) {
670 bytes = NULL;
671 needed = 0;
672 }
673 else if (values == (PyObject *)self || !PyByteArray_Check(values)) {
Christian Heimes6d26ade2012-11-03 23:07:59 +0100674 int err;
Ezio Melottic64bcbe2012-11-03 21:19:06 +0200675 if (PyNumber_Check(values) || PyUnicode_Check(values)) {
676 PyErr_SetString(PyExc_TypeError,
677 "can assign only bytes, buffers, or iterables "
678 "of ints in range(0, 256)");
679 return -1;
680 }
Georg Brandlf3fa5682010-12-04 17:09:30 +0000681 /* Make a copy and call this function recursively */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000682 values = PyByteArray_FromObject(values);
683 if (values == NULL)
684 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000685 err = bytearray_ass_subscript(self, index, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000686 Py_DECREF(values);
687 return err;
688 }
689 else {
690 assert(PyByteArray_Check(values));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200691 bytes = PyByteArray_AS_STRING(values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000692 needed = Py_SIZE(values);
693 }
694 /* Make sure b[5:2] = ... inserts before 5, not before 2. */
695 if ((step < 0 && start < stop) ||
696 (step > 0 && start > stop))
697 stop = start;
698 if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200699 return bytearray_setslice_linear(self, start, stop, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000700 }
701 else {
702 if (needed == 0) {
703 /* Delete slice */
Mark Dickinsonbc099642010-01-29 17:27:24 +0000704 size_t cur;
705 Py_ssize_t i;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000706
Antoine Pitrou5504e892008-12-06 21:27:53 +0000707 if (!_canresize(self))
708 return -1;
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000709
710 if (slicelen == 0)
711 /* Nothing to do here. */
712 return 0;
713
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000714 if (step < 0) {
715 stop = start + 1;
716 start = stop + step * (slicelen - 1) - 1;
717 step = -step;
718 }
719 for (cur = start, i = 0;
720 i < slicelen; cur += step, i++) {
721 Py_ssize_t lim = step - 1;
722
Mark Dickinson66f575b2010-02-14 12:53:32 +0000723 if (cur + step >= (size_t)PyByteArray_GET_SIZE(self))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000724 lim = PyByteArray_GET_SIZE(self) - cur - 1;
725
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200726 memmove(buf + cur - i,
727 buf + cur + 1, lim);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000728 }
729 /* Move the tail of the bytes, in one chunk */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000730 cur = start + (size_t)slicelen*step;
Mark Dickinson66f575b2010-02-14 12:53:32 +0000731 if (cur < (size_t)PyByteArray_GET_SIZE(self)) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200732 memmove(buf + cur - slicelen,
733 buf + cur,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000734 PyByteArray_GET_SIZE(self) - cur);
735 }
736 if (PyByteArray_Resize((PyObject *)self,
737 PyByteArray_GET_SIZE(self) - slicelen) < 0)
738 return -1;
739
740 return 0;
741 }
742 else {
743 /* Assign slice */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000744 Py_ssize_t i;
745 size_t cur;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000746
747 if (needed != slicelen) {
748 PyErr_Format(PyExc_ValueError,
749 "attempt to assign bytes of size %zd "
750 "to extended slice of size %zd",
751 needed, slicelen);
752 return -1;
753 }
754 for (cur = start, i = 0; i < slicelen; cur += step, i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200755 buf[cur] = bytes[i];
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000756 return 0;
757 }
758 }
759}
760
761static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000762bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000763{
764 static char *kwlist[] = {"source", "encoding", "errors", 0};
765 PyObject *arg = NULL;
766 const char *encoding = NULL;
767 const char *errors = NULL;
768 Py_ssize_t count;
769 PyObject *it;
770 PyObject *(*iternext)(PyObject *);
771
772 if (Py_SIZE(self) != 0) {
773 /* Empty previous contents (yes, do this first of all!) */
774 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
775 return -1;
776 }
777
778 /* Parse arguments */
Georg Brandl3dbca812008-07-23 16:10:53 +0000779 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000780 &arg, &encoding, &errors))
781 return -1;
782
783 /* Make a quick exit if no first argument */
784 if (arg == NULL) {
785 if (encoding != NULL || errors != NULL) {
786 PyErr_SetString(PyExc_TypeError,
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300787 encoding != NULL ?
788 "encoding without a string argument" :
789 "errors without a string argument");
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000790 return -1;
791 }
792 return 0;
793 }
794
795 if (PyUnicode_Check(arg)) {
796 /* Encode via the codec registry */
797 PyObject *encoded, *new;
798 if (encoding == NULL) {
799 PyErr_SetString(PyExc_TypeError,
800 "string argument without an encoding");
801 return -1;
802 }
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000803 encoded = PyUnicode_AsEncodedString(arg, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000804 if (encoded == NULL)
805 return -1;
806 assert(PyBytes_Check(encoded));
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000807 new = bytearray_iconcat(self, encoded);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000808 Py_DECREF(encoded);
809 if (new == NULL)
810 return -1;
811 Py_DECREF(new);
812 return 0;
813 }
814
815 /* If it's not unicode, there can't be encoding or errors */
816 if (encoding != NULL || errors != NULL) {
817 PyErr_SetString(PyExc_TypeError,
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300818 encoding != NULL ?
819 "encoding without a string argument" :
820 "errors without a string argument");
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000821 return -1;
822 }
823
824 /* Is it an int? */
Serhiy Storchakaeb249882016-08-15 09:46:07 +0300825 if (PyIndex_Check(arg)) {
826 count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
827 if (count == -1 && PyErr_Occurred()) {
Serhiy Storchakae8904212018-10-15 00:02:57 +0300828 if (!PyErr_ExceptionMatches(PyExc_TypeError))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000829 return -1;
INADA Naokia634e232017-01-06 17:32:01 +0900830 PyErr_Clear(); /* fall through */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000831 }
INADA Naokia634e232017-01-06 17:32:01 +0900832 else {
833 if (count < 0) {
834 PyErr_SetString(PyExc_ValueError, "negative count");
835 return -1;
836 }
837 if (count > 0) {
838 if (PyByteArray_Resize((PyObject *)self, count))
839 return -1;
840 memset(PyByteArray_AS_STRING(self), 0, count);
841 }
842 return 0;
843 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000844 }
845
846 /* Use the buffer API */
847 if (PyObject_CheckBuffer(arg)) {
848 Py_ssize_t size;
849 Py_buffer view;
850 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
851 return -1;
852 size = view.len;
853 if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200854 if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self),
855 &view, size, 'C') < 0)
Stefan Krah7d12d9d2012-07-28 12:25:55 +0200856 goto fail;
Martin v. Löwis423be952008-08-13 15:53:07 +0000857 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000858 return 0;
859 fail:
Martin v. Löwis423be952008-08-13 15:53:07 +0000860 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000861 return -1;
862 }
863
864 /* XXX Optimize this if the arguments is a list, tuple */
865
866 /* Get the iterator */
867 it = PyObject_GetIter(arg);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300868 if (it == NULL) {
869 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
870 PyErr_Format(PyExc_TypeError,
871 "cannot convert '%.200s' object to bytearray",
872 arg->ob_type->tp_name);
873 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000874 return -1;
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300875 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000876 iternext = *Py_TYPE(it)->tp_iternext;
877
878 /* Run the iterator to exhaustion */
879 for (;;) {
880 PyObject *item;
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000881 int rc, value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000882
883 /* Get the next item */
884 item = iternext(it);
885 if (item == NULL) {
886 if (PyErr_Occurred()) {
887 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
888 goto error;
889 PyErr_Clear();
890 }
891 break;
892 }
893
894 /* Interpret it as an int (__index__) */
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000895 rc = _getbytevalue(item, &value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000896 Py_DECREF(item);
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000897 if (!rc)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000898 goto error;
899
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000900 /* Append the byte */
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300901 if (Py_SIZE(self) + 1 < self->ob_alloc) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000902 Py_SIZE(self)++;
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300903 PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0';
904 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000905 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
906 goto error;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200907 PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000908 }
909
910 /* Clean up and return success */
911 Py_DECREF(it);
912 return 0;
913
914 error:
915 /* Error handling when it != NULL */
916 Py_DECREF(it);
917 return -1;
918}
919
920/* Mostly copied from string_repr, but without the
921 "smart quote" functionality. */
922static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000923bytearray_repr(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000924{
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300925 const char *className = _PyType_Name(Py_TYPE(self));
926 const char *quote_prefix = "(b";
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000927 const char *quote_postfix = ")";
928 Py_ssize_t length = Py_SIZE(self);
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300929 /* 6 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
930 Py_ssize_t newsize;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000931 PyObject *v;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200932 Py_ssize_t i;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200933 char *bytes;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200934 char c;
935 char *p;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200936 int quote;
937 char *test, *start;
938 char *buffer;
939
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300940 newsize = strlen(className);
941 if (length > (PY_SSIZE_T_MAX - 6 - newsize) / 4) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000942 PyErr_SetString(PyExc_OverflowError,
943 "bytearray object is too large to make repr");
944 return NULL;
945 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200946
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300947 newsize += 6 + length * 4;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100948 buffer = PyObject_Malloc(newsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200949 if (buffer == NULL) {
950 PyErr_NoMemory();
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000951 return NULL;
952 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000953
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200954 /* Figure out which quote to use; single is preferred */
955 quote = '\'';
956 start = PyByteArray_AS_STRING(self);
957 for (test = start; test < start+length; ++test) {
958 if (*test == '"') {
959 quote = '\''; /* back to single */
960 break;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000961 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200962 else if (*test == '\'')
963 quote = '"';
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000964 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200965
966 p = buffer;
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300967 while (*className)
968 *p++ = *className++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200969 while (*quote_prefix)
970 *p++ = *quote_prefix++;
971 *p++ = quote;
972
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200973 bytes = PyByteArray_AS_STRING(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200974 for (i = 0; i < length; i++) {
975 /* There's at least enough room for a hex escape
976 and a closing quote. */
977 assert(newsize - (p - buffer) >= 5);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200978 c = bytes[i];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200979 if (c == '\'' || c == '\\')
980 *p++ = '\\', *p++ = c;
981 else if (c == '\t')
982 *p++ = '\\', *p++ = 't';
983 else if (c == '\n')
984 *p++ = '\\', *p++ = 'n';
985 else if (c == '\r')
986 *p++ = '\\', *p++ = 'r';
987 else if (c == 0)
988 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
989 else if (c < ' ' || c >= 0x7f) {
990 *p++ = '\\';
991 *p++ = 'x';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200992 *p++ = Py_hexdigits[(c & 0xf0) >> 4];
993 *p++ = Py_hexdigits[c & 0xf];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200994 }
995 else
996 *p++ = c;
997 }
998 assert(newsize - (p - buffer) >= 1);
999 *p++ = quote;
1000 while (*quote_postfix) {
1001 *p++ = *quote_postfix++;
1002 }
1003
Serhiy Storchakab3a77962017-09-21 14:24:13 +03001004 v = PyUnicode_FromStringAndSize(buffer, p - buffer);
Antoine Pitrou39aba4f2011-11-12 21:15:28 +01001005 PyObject_Free(buffer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001006 return v;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001007}
1008
1009static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001010bytearray_str(PyObject *op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001011{
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001012 if (Py_BytesWarningFlag) {
1013 if (PyErr_WarnEx(PyExc_BytesWarning,
1014 "str() on a bytearray instance", 1)) {
1015 return NULL;
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001016 }
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001017 }
1018 return bytearray_repr((PyByteArrayObject*)op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001019}
1020
1021static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001022bytearray_richcompare(PyObject *self, PyObject *other, int op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001023{
1024 Py_ssize_t self_size, other_size;
1025 Py_buffer self_bytes, other_bytes;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001026 int cmp, rc;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001027
1028 /* Bytes can be compared to anything that supports the (binary)
1029 buffer API. Except that a comparison with Unicode is always an
1030 error, even if the comparison is for equality. */
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001031 rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type);
1032 if (!rc)
1033 rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type);
1034 if (rc < 0)
1035 return NULL;
1036 if (rc) {
Barry Warsaw9e9dcd62008-10-17 01:50:37 +00001037 if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001038 if (PyErr_WarnEx(PyExc_BytesWarning,
Georg Brandle5d68ac2008-06-04 11:30:26 +00001039 "Comparison between bytearray and string", 1))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001040 return NULL;
1041 }
1042
Brian Curtindfc80e32011-08-10 20:28:54 -05001043 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001044 }
1045
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001046 if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001047 PyErr_Clear();
Brian Curtindfc80e32011-08-10 20:28:54 -05001048 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001049 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001050 self_size = self_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001051
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001052 if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001053 PyErr_Clear();
Martin v. Löwis423be952008-08-13 15:53:07 +00001054 PyBuffer_Release(&self_bytes);
Brian Curtindfc80e32011-08-10 20:28:54 -05001055 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001056 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001057 other_size = other_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001058
1059 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
1060 /* Shortcut: if the lengths differ, the objects differ */
stratakise8b19652017-11-02 11:32:54 +01001061 PyBuffer_Release(&self_bytes);
1062 PyBuffer_Release(&other_bytes);
1063 return PyBool_FromLong((op == Py_NE));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001064 }
1065 else {
stratakise8b19652017-11-02 11:32:54 +01001066 cmp = memcmp(self_bytes.buf, other_bytes.buf,
1067 Py_MIN(self_size, other_size));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001068 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
1069
stratakise8b19652017-11-02 11:32:54 +01001070 PyBuffer_Release(&self_bytes);
1071 PyBuffer_Release(&other_bytes);
1072
1073 if (cmp != 0) {
1074 Py_RETURN_RICHCOMPARE(cmp, 0, op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001075 }
1076
stratakise8b19652017-11-02 11:32:54 +01001077 Py_RETURN_RICHCOMPARE(self_size, other_size, op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001078 }
1079
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001080}
1081
1082static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001083bytearray_dealloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001084{
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001085 if (self->ob_exports > 0) {
1086 PyErr_SetString(PyExc_SystemError,
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001087 "deallocated bytearray object has exported buffers");
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001088 PyErr_Print();
1089 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001090 if (self->ob_bytes != 0) {
Antoine Pitrou39aba4f2011-11-12 21:15:28 +01001091 PyObject_Free(self->ob_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001092 }
1093 Py_TYPE(self)->tp_free((PyObject *)self);
1094}
1095
1096
1097/* -------------------------------------------------------------------- */
1098/* Methods */
1099
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001100#define FASTSEARCH fastsearch
1101#define STRINGLIB(F) stringlib_##F
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001102#define STRINGLIB_CHAR char
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001103#define STRINGLIB_SIZEOF_CHAR 1
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001104#define STRINGLIB_LEN PyByteArray_GET_SIZE
1105#define STRINGLIB_STR PyByteArray_AS_STRING
1106#define STRINGLIB_NEW PyByteArray_FromStringAndSize
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001107#define STRINGLIB_ISSPACE Py_ISSPACE
1108#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001109#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1110#define STRINGLIB_MUTABLE 1
1111
1112#include "stringlib/fastsearch.h"
1113#include "stringlib/count.h"
1114#include "stringlib/find.h"
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001115#include "stringlib/join.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001116#include "stringlib/partition.h"
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001117#include "stringlib/split.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001118#include "stringlib/ctype.h"
1119#include "stringlib/transmogrify.h"
1120
1121
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001122static PyObject *
1123bytearray_find(PyByteArrayObject *self, PyObject *args)
1124{
1125 return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1126}
1127
1128static PyObject *
1129bytearray_count(PyByteArrayObject *self, PyObject *args)
1130{
1131 return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1132}
1133
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001134/*[clinic input]
1135bytearray.clear
1136
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001137Remove all items from the bytearray.
1138[clinic start generated code]*/
1139
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001140static PyObject *
1141bytearray_clear_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001142/*[clinic end generated code: output=85c2fe6aede0956c input=ed6edae9de447ac4]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001143{
1144 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1145 return NULL;
1146 Py_RETURN_NONE;
1147}
1148
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001149/*[clinic input]
1150bytearray.copy
1151
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001152Return a copy of B.
1153[clinic start generated code]*/
1154
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001155static PyObject *
1156bytearray_copy_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001157/*[clinic end generated code: output=68cfbcfed484c132 input=6597b0c01bccaa9e]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001158{
1159 return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self),
1160 PyByteArray_GET_SIZE(self));
1161}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001162
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001163static PyObject *
1164bytearray_index(PyByteArrayObject *self, PyObject *args)
1165{
1166 return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1167}
1168
1169static PyObject *
1170bytearray_rfind(PyByteArrayObject *self, PyObject *args)
1171{
1172 return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1173}
1174
1175static PyObject *
1176bytearray_rindex(PyByteArrayObject *self, PyObject *args)
1177{
1178 return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1179}
1180
1181static int
1182bytearray_contains(PyObject *self, PyObject *arg)
1183{
1184 return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg);
1185}
1186
1187static PyObject *
1188bytearray_startswith(PyByteArrayObject *self, PyObject *args)
1189{
1190 return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1191}
1192
1193static PyObject *
1194bytearray_endswith(PyByteArrayObject *self, PyObject *args)
1195{
1196 return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1197}
1198
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001199
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001200/*[clinic input]
1201bytearray.translate
1202
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001203 table: object
1204 Translation table, which must be a bytes object of length 256.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001205 /
Martin Panter1b6c6da2016-08-27 08:35:02 +00001206 delete as deletechars: object(c_default="NULL") = b''
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001207
1208Return a copy with each character mapped by the given translation table.
1209
Martin Panter1b6c6da2016-08-27 08:35:02 +00001210All characters occurring in the optional argument delete are removed.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001211The remaining characters are mapped through the given translation table.
1212[clinic start generated code]*/
1213
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001214static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001215bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
Martin Panter1b6c6da2016-08-27 08:35:02 +00001216 PyObject *deletechars)
1217/*[clinic end generated code: output=b6a8f01c2a74e446 input=cfff956d4d127a9b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001218{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001219 char *input, *output;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001220 const char *table_chars;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001221 Py_ssize_t i, c;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001222 PyObject *input_obj = (PyObject*)self;
1223 const char *output_start;
1224 Py_ssize_t inlen;
Georg Brandlccc47b62008-12-28 11:44:14 +00001225 PyObject *result = NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001226 int trans_table[256];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001227 Py_buffer vtable, vdel;
1228
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001229 if (table == Py_None) {
1230 table_chars = NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001231 table = NULL;
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001232 } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001233 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001234 } else {
1235 if (vtable.len != 256) {
1236 PyErr_SetString(PyExc_ValueError,
1237 "translation table must be 256 characters long");
Georg Brandl953152f2009-07-22 12:03:59 +00001238 PyBuffer_Release(&vtable);
1239 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001240 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001241 table_chars = (const char*)vtable.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001242 }
1243
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001244 if (deletechars != NULL) {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001245 if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) {
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001246 if (table != NULL)
Georg Brandl953152f2009-07-22 12:03:59 +00001247 PyBuffer_Release(&vtable);
1248 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001249 }
1250 }
1251 else {
1252 vdel.buf = NULL;
1253 vdel.len = 0;
1254 }
1255
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001256 inlen = PyByteArray_GET_SIZE(input_obj);
1257 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1258 if (result == NULL)
1259 goto done;
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03001260 output_start = output = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001261 input = PyByteArray_AS_STRING(input_obj);
1262
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001263 if (vdel.len == 0 && table_chars != NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001264 /* If no deletions are required, use faster code */
1265 for (i = inlen; --i >= 0; ) {
1266 c = Py_CHARMASK(*input++);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001267 *output++ = table_chars[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001268 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001269 goto done;
1270 }
Georg Brandlccc47b62008-12-28 11:44:14 +00001271
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001272 if (table_chars == NULL) {
Georg Brandlccc47b62008-12-28 11:44:14 +00001273 for (i = 0; i < 256; i++)
1274 trans_table[i] = Py_CHARMASK(i);
1275 } else {
1276 for (i = 0; i < 256; i++)
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001277 trans_table[i] = Py_CHARMASK(table_chars[i]);
Georg Brandlccc47b62008-12-28 11:44:14 +00001278 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001279
1280 for (i = 0; i < vdel.len; i++)
1281 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1282
1283 for (i = inlen; --i >= 0; ) {
1284 c = Py_CHARMASK(*input++);
1285 if (trans_table[c] != -1)
Martin Panter1b6c6da2016-08-27 08:35:02 +00001286 *output++ = (char)trans_table[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001287 }
1288 /* Fix the size of the resulting string */
1289 if (inlen > 0)
Christian Heimesc731bbe2013-07-21 02:04:35 +02001290 if (PyByteArray_Resize(result, output - output_start) < 0) {
1291 Py_CLEAR(result);
1292 goto done;
1293 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001294
1295done:
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001296 if (table != NULL)
Georg Brandlccc47b62008-12-28 11:44:14 +00001297 PyBuffer_Release(&vtable);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001298 if (deletechars != NULL)
Martin v. Löwis423be952008-08-13 15:53:07 +00001299 PyBuffer_Release(&vdel);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001300 return result;
1301}
1302
1303
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001304/*[clinic input]
1305
1306@staticmethod
1307bytearray.maketrans
1308
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001309 frm: Py_buffer
1310 to: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001311 /
1312
1313Return a translation table useable for the bytes or bytearray translate method.
1314
1315The returned table will be one where each byte in frm is mapped to the byte at
1316the same position in to.
1317
1318The bytes objects frm and to must be of the same length.
1319[clinic start generated code]*/
1320
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001321static PyObject *
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001322bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001323/*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001324{
1325 return _Py_bytes_maketrans(frm, to);
Georg Brandlabc38772009-04-12 15:51:51 +00001326}
1327
1328
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001329/*[clinic input]
1330bytearray.replace
1331
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001332 old: Py_buffer
1333 new: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001334 count: Py_ssize_t = -1
1335 Maximum number of occurrences to replace.
1336 -1 (the default value) means replace all occurrences.
1337 /
1338
1339Return a copy with all occurrences of substring old replaced by new.
1340
1341If the optional argument count is given, only the first count occurrences are
1342replaced.
1343[clinic start generated code]*/
1344
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001345static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001346bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
1347 Py_buffer *new, Py_ssize_t count)
1348/*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001349{
Serhiy Storchakafb81d3c2016-05-05 09:26:07 +03001350 return stringlib_replace((PyObject *)self,
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001351 (const char *)old->buf, old->len,
1352 (const char *)new->buf, new->len, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001353}
1354
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001355/*[clinic input]
1356bytearray.split
1357
1358 sep: object = None
1359 The delimiter according which to split the bytearray.
1360 None (the default value) means split on ASCII whitespace characters
1361 (space, tab, return, newline, formfeed, vertical tab).
1362 maxsplit: Py_ssize_t = -1
1363 Maximum number of splits to do.
1364 -1 (the default value) means no limit.
1365
1366Return a list of the sections in the bytearray, using sep as the delimiter.
1367[clinic start generated code]*/
1368
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001369static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001370bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
1371 Py_ssize_t maxsplit)
1372/*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001373{
1374 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001375 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001376 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001377 Py_buffer vsub;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001378
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001379 if (maxsplit < 0)
1380 maxsplit = PY_SSIZE_T_MAX;
1381
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001382 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001383 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001384
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001385 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001386 return NULL;
1387 sub = vsub.buf;
1388 n = vsub.len;
1389
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001390 list = stringlib_split(
1391 (PyObject*) self, s, len, sub, n, maxsplit
1392 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001393 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001394 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001395}
1396
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001397/*[clinic input]
1398bytearray.partition
1399
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001400 sep: object
1401 /
1402
1403Partition the bytearray into three parts using the given separator.
1404
1405This will search for the separator sep in the bytearray. If the separator is
1406found, returns a 3-tuple containing the part before the separator, the
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001407separator itself, and the part after it as new bytearray objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001408
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001409If the separator is not found, returns a 3-tuple containing the copy of the
1410original bytearray object and two empty bytearray objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001411[clinic start generated code]*/
1412
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001413static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001414bytearray_partition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001415/*[clinic end generated code: output=45d2525ddd35f957 input=8f644749ee4fc83a]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001416{
1417 PyObject *bytesep, *result;
1418
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001419 bytesep = _PyByteArray_FromBufferObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001420 if (! bytesep)
1421 return NULL;
1422
1423 result = stringlib_partition(
1424 (PyObject*) self,
1425 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1426 bytesep,
1427 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1428 );
1429
1430 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001431 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001432}
1433
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001434/*[clinic input]
1435bytearray.rpartition
1436
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001437 sep: object
1438 /
1439
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001440Partition the bytearray into three parts using the given separator.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001441
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001442This will search for the separator sep in the bytearray, starting at the end.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001443If the separator is found, returns a 3-tuple containing the part before the
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001444separator, the separator itself, and the part after it as new bytearray
1445objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001446
1447If the separator is not found, returns a 3-tuple containing two empty bytearray
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001448objects and the copy of the original bytearray object.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001449[clinic start generated code]*/
1450
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001451static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001452bytearray_rpartition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001453/*[clinic end generated code: output=440de3c9426115e8 input=7e3df3e6cb8fa0ac]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001454{
1455 PyObject *bytesep, *result;
1456
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001457 bytesep = _PyByteArray_FromBufferObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001458 if (! bytesep)
1459 return NULL;
1460
1461 result = stringlib_rpartition(
1462 (PyObject*) self,
1463 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1464 bytesep,
1465 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1466 );
1467
1468 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001469 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001470}
1471
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001472/*[clinic input]
1473bytearray.rsplit = bytearray.split
1474
1475Return a list of the sections in the bytearray, using sep as the delimiter.
1476
1477Splitting is done starting at the end of the bytearray and working to the front.
1478[clinic start generated code]*/
1479
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001480static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001481bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
1482 Py_ssize_t maxsplit)
1483/*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001484{
1485 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001486 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001487 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001488 Py_buffer vsub;
1489
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001490 if (maxsplit < 0)
1491 maxsplit = PY_SSIZE_T_MAX;
1492
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001493 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001494 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001495
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001496 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001497 return NULL;
1498 sub = vsub.buf;
1499 n = vsub.len;
1500
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001501 list = stringlib_rsplit(
1502 (PyObject*) self, s, len, sub, n, maxsplit
1503 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001504 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001505 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001506}
1507
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001508/*[clinic input]
1509bytearray.reverse
1510
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001511Reverse the order of the values in B in place.
1512[clinic start generated code]*/
1513
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001514static PyObject *
1515bytearray_reverse_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001516/*[clinic end generated code: output=9f7616f29ab309d3 input=543356319fc78557]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001517{
1518 char swap, *head, *tail;
1519 Py_ssize_t i, j, n = Py_SIZE(self);
1520
1521 j = n / 2;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001522 head = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001523 tail = head + n - 1;
1524 for (i = 0; i < j; i++) {
1525 swap = *head;
1526 *head++ = *tail;
1527 *tail-- = swap;
1528 }
1529
1530 Py_RETURN_NONE;
1531}
1532
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001533
1534/*[python input]
1535class bytesvalue_converter(CConverter):
1536 type = 'int'
1537 converter = '_getbytevalue'
1538[python start generated code]*/
1539/*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/
1540
1541
1542/*[clinic input]
1543bytearray.insert
1544
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001545 index: Py_ssize_t
1546 The index where the value is to be inserted.
1547 item: bytesvalue
1548 The item to be inserted.
1549 /
1550
1551Insert a single item into the bytearray before the given index.
1552[clinic start generated code]*/
1553
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001554static PyObject *
1555bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001556/*[clinic end generated code: output=76c775a70e7b07b7 input=b2b5d07e9de6c070]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001557{
1558 Py_ssize_t n = Py_SIZE(self);
1559 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001560
1561 if (n == PY_SSIZE_T_MAX) {
1562 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001563 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001564 return NULL;
1565 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001566 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1567 return NULL;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001568 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001569
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001570 if (index < 0) {
1571 index += n;
1572 if (index < 0)
1573 index = 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001574 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001575 if (index > n)
1576 index = n;
1577 memmove(buf + index + 1, buf + index, n - index);
1578 buf[index] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001579
1580 Py_RETURN_NONE;
1581}
1582
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001583/*[clinic input]
1584bytearray.append
1585
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001586 item: bytesvalue
1587 The item to be appended.
1588 /
1589
1590Append a single item to the end of the bytearray.
1591[clinic start generated code]*/
1592
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001593static PyObject *
1594bytearray_append_impl(PyByteArrayObject *self, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001595/*[clinic end generated code: output=a154e19ed1886cb6 input=20d6bec3d1340593]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001596{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001597 Py_ssize_t n = Py_SIZE(self);
1598
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001599 if (n == PY_SSIZE_T_MAX) {
1600 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001601 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001602 return NULL;
1603 }
1604 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1605 return NULL;
1606
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001607 PyByteArray_AS_STRING(self)[n] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001608
1609 Py_RETURN_NONE;
1610}
1611
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001612/*[clinic input]
1613bytearray.extend
1614
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001615 iterable_of_ints: object
1616 The iterable of items to append.
1617 /
1618
1619Append all the items from the iterator or sequence to the end of the bytearray.
1620[clinic start generated code]*/
1621
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001622static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001623bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001624/*[clinic end generated code: output=98155dbe249170b1 input=c617b3a93249ba28]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001625{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001626 PyObject *it, *item, *bytearray_obj;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001627 Py_ssize_t buf_size = 0, len = 0;
1628 int value;
1629 char *buf;
1630
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001631 /* bytearray_setslice code only accepts something supporting PEP 3118. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001632 if (PyObject_CheckBuffer(iterable_of_ints)) {
1633 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_ints) == -1)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001634 return NULL;
1635
1636 Py_RETURN_NONE;
1637 }
1638
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001639 it = PyObject_GetIter(iterable_of_ints);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +03001640 if (it == NULL) {
1641 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
1642 PyErr_Format(PyExc_TypeError,
1643 "can't extend bytearray with %.100s",
1644 iterable_of_ints->ob_type->tp_name);
1645 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001646 return NULL;
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +03001647 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001648
Ezio Melotti42da6632011-03-15 05:18:48 +02001649 /* Try to determine the length of the argument. 32 is arbitrary. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001650 buf_size = PyObject_LengthHint(iterable_of_ints, 32);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001651 if (buf_size == -1) {
1652 Py_DECREF(it);
1653 return NULL;
1654 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001655
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001656 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001657 if (bytearray_obj == NULL) {
1658 Py_DECREF(it);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001659 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001660 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001661 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001662
1663 while ((item = PyIter_Next(it)) != NULL) {
1664 if (! _getbytevalue(item, &value)) {
1665 Py_DECREF(item);
1666 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001667 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001668 return NULL;
1669 }
1670 buf[len++] = value;
1671 Py_DECREF(item);
1672
1673 if (len >= buf_size) {
Martin Panter371731e2016-07-18 07:53:13 +00001674 Py_ssize_t addition;
1675 if (len == PY_SSIZE_T_MAX) {
1676 Py_DECREF(it);
1677 Py_DECREF(bytearray_obj);
1678 return PyErr_NoMemory();
1679 }
1680 addition = len >> 1;
1681 if (addition > PY_SSIZE_T_MAX - len - 1)
1682 buf_size = PY_SSIZE_T_MAX;
1683 else
1684 buf_size = len + addition + 1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001685 if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001686 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001687 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001688 return NULL;
1689 }
1690 /* Recompute the `buf' pointer, since the resizing operation may
1691 have invalidated it. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001692 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001693 }
1694 }
1695 Py_DECREF(it);
1696
1697 /* Resize down to exact size. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001698 if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) {
1699 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001700 return NULL;
1701 }
1702
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001703 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) {
1704 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001705 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001706 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001707 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001708
1709 Py_RETURN_NONE;
1710}
1711
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001712/*[clinic input]
1713bytearray.pop
1714
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001715 index: Py_ssize_t = -1
1716 The index from where to remove the item.
1717 -1 (the default value) means remove the last item.
1718 /
1719
1720Remove and return a single item from B.
1721
1722If no index argument is given, will pop the last item.
1723[clinic start generated code]*/
1724
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001725static PyObject *
1726bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001727/*[clinic end generated code: output=e0ccd401f8021da8 input=3591df2d06c0d237]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001728{
1729 int value;
1730 Py_ssize_t n = Py_SIZE(self);
1731 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001732
1733 if (n == 0) {
Eli Bendersky1bc4f192011-03-04 04:55:25 +00001734 PyErr_SetString(PyExc_IndexError,
1735 "pop from empty bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001736 return NULL;
1737 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001738 if (index < 0)
1739 index += Py_SIZE(self);
1740 if (index < 0 || index >= Py_SIZE(self)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001741 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1742 return NULL;
1743 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001744 if (!_canresize(self))
1745 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001746
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001747 buf = PyByteArray_AS_STRING(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001748 value = buf[index];
1749 memmove(buf + index, buf + index + 1, n - index);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001750 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1751 return NULL;
1752
Mark Dickinson54a3db92009-09-06 10:19:23 +00001753 return PyLong_FromLong((unsigned char)value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001754}
1755
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001756/*[clinic input]
1757bytearray.remove
1758
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001759 value: bytesvalue
1760 The value to remove.
1761 /
1762
1763Remove the first occurrence of a value in the bytearray.
1764[clinic start generated code]*/
1765
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001766static PyObject *
1767bytearray_remove_impl(PyByteArrayObject *self, int value)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001768/*[clinic end generated code: output=d659e37866709c13 input=121831240cd51ddf]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001769{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001770 Py_ssize_t where, n = Py_SIZE(self);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001771 char *buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001772
Serhiy Storchaka4b234942016-05-16 22:24:03 +03001773 where = stringlib_find_char(buf, n, value);
1774 if (where < 0) {
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001775 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001776 return NULL;
1777 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001778 if (!_canresize(self))
1779 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001780
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001781 memmove(buf + where, buf + where + 1, n - where);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001782 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1783 return NULL;
1784
1785 Py_RETURN_NONE;
1786}
1787
1788/* XXX These two helpers could be optimized if argsize == 1 */
1789
1790static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001791lstrip_helper(const char *myptr, Py_ssize_t mysize,
1792 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001793{
1794 Py_ssize_t i = 0;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001795 while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001796 i++;
1797 return i;
1798}
1799
1800static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001801rstrip_helper(const char *myptr, Py_ssize_t mysize,
1802 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001803{
1804 Py_ssize_t i = mysize - 1;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001805 while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001806 i--;
1807 return i + 1;
1808}
1809
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001810/*[clinic input]
1811bytearray.strip
1812
1813 bytes: object = None
1814 /
1815
1816Strip leading and trailing bytes contained in the argument.
1817
1818If the argument is omitted or None, strip leading and trailing ASCII whitespace.
1819[clinic start generated code]*/
1820
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001821static PyObject *
1822bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001823/*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001824{
1825 Py_ssize_t left, right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001826 char *myptr;
1827 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001828 Py_buffer vbytes;
1829
1830 if (bytes == Py_None) {
1831 bytesptr = "\t\n\r\f\v ";
1832 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001833 }
1834 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001835 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001836 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001837 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001838 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001839 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001840 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001841 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001842 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001843 if (left == mysize)
1844 right = left;
1845 else
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001846 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1847 if (bytes != Py_None)
1848 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001849 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001850}
1851
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001852/*[clinic input]
1853bytearray.lstrip
1854
1855 bytes: object = None
1856 /
1857
1858Strip leading bytes contained in the argument.
1859
1860If the argument is omitted or None, strip leading ASCII whitespace.
1861[clinic start generated code]*/
1862
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001863static PyObject *
1864bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001865/*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001866{
1867 Py_ssize_t left, right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001868 char *myptr;
1869 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001870 Py_buffer vbytes;
1871
1872 if (bytes == Py_None) {
1873 bytesptr = "\t\n\r\f\v ";
1874 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001875 }
1876 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001877 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001878 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001879 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001880 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001881 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001882 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001883 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001884 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001885 right = mysize;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001886 if (bytes != Py_None)
1887 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001888 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001889}
1890
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001891/*[clinic input]
1892bytearray.rstrip
1893
1894 bytes: object = None
1895 /
1896
1897Strip trailing bytes contained in the argument.
1898
1899If the argument is omitted or None, strip trailing ASCII whitespace.
1900[clinic start generated code]*/
1901
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001902static PyObject *
1903bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001904/*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001905{
1906 Py_ssize_t right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001907 char *myptr;
1908 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001909 Py_buffer vbytes;
1910
1911 if (bytes == Py_None) {
1912 bytesptr = "\t\n\r\f\v ";
1913 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001914 }
1915 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001916 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001917 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001918 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001919 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001920 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001921 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001922 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001923 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1924 if (bytes != Py_None)
1925 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001926 return PyByteArray_FromStringAndSize(myptr, right);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001927}
1928
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001929/*[clinic input]
1930bytearray.decode
1931
1932 encoding: str(c_default="NULL") = 'utf-8'
1933 The encoding with which to decode the bytearray.
1934 errors: str(c_default="NULL") = 'strict'
1935 The error handling scheme to use for the handling of decoding errors.
1936 The default is 'strict' meaning that decoding errors raise a
1937 UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
1938 as well as any other name registered with codecs.register_error that
1939 can handle UnicodeDecodeErrors.
1940
1941Decode the bytearray using the codec registered for encoding.
1942[clinic start generated code]*/
1943
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001944static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001945bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
1946 const char *errors)
1947/*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001948{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001949 if (encoding == NULL)
1950 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis0efea322014-07-27 17:29:17 +02001951 return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001952}
1953
1954PyDoc_STRVAR(alloc_doc,
1955"B.__alloc__() -> int\n\
1956\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00001957Return the number of bytes actually allocated.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001958
1959static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301960bytearray_alloc(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001961{
1962 return PyLong_FromSsize_t(self->ob_alloc);
1963}
1964
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001965/*[clinic input]
1966bytearray.join
1967
1968 iterable_of_bytes: object
1969 /
1970
1971Concatenate any number of bytes/bytearray objects.
1972
1973The bytearray whose method is called is inserted in between each pair.
1974
1975The result is returned as a new bytearray object.
1976[clinic start generated code]*/
1977
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001978static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001979bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001980/*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001981{
Martin v. Löwis0efea322014-07-27 17:29:17 +02001982 return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001983}
1984
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001985/*[clinic input]
1986bytearray.splitlines
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001987
Serhiy Storchaka202fda52017-03-12 10:10:47 +02001988 keepends: bool(accept={int}) = False
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001989
1990Return a list of the lines in the bytearray, breaking at line boundaries.
1991
1992Line breaks are not included in the resulting list unless keepends is given and
1993true.
1994[clinic start generated code]*/
1995
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001996static PyObject *
1997bytearray_splitlines_impl(PyByteArrayObject *self, int keepends)
Serhiy Storchaka202fda52017-03-12 10:10:47 +02001998/*[clinic end generated code: output=4223c94b895f6ad9 input=99a27ad959b9cf6b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001999{
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002000 return stringlib_splitlines(
2001 (PyObject*) self, PyByteArray_AS_STRING(self),
2002 PyByteArray_GET_SIZE(self), keepends
2003 );
2004}
2005
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002006/*[clinic input]
2007@classmethod
2008bytearray.fromhex
2009
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002010 string: unicode
2011 /
2012
2013Create a bytearray object from a string of hexadecimal numbers.
2014
2015Spaces between two numbers are accepted.
2016Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')
2017[clinic start generated code]*/
2018
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002019static PyObject *
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002020bytearray_fromhex_impl(PyTypeObject *type, PyObject *string)
2021/*[clinic end generated code: output=8f0f0b6d30fb3ba0 input=f033a16d1fb21f48]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002022{
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002023 PyObject *result = _PyBytes_FromHex(string, type == &PyByteArray_Type);
2024 if (type != &PyByteArray_Type && result != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002025 Py_SETREF(result, PyObject_CallFunctionObjArgs((PyObject *)type,
2026 result, NULL));
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002027 }
2028 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002029}
2030
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002031PyDoc_STRVAR(hex__doc__,
2032"B.hex() -> string\n\
2033\n\
2034Create a string of hexadecimal numbers from a bytearray object.\n\
2035Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'.");
2036
2037static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302038bytearray_hex(PyBytesObject *self, PyObject *Py_UNUSED(ignored))
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002039{
2040 char* argbuf = PyByteArray_AS_STRING(self);
2041 Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
2042 return _Py_strhex(argbuf, arglen);
2043}
2044
2045static PyObject *
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002046_common_reduce(PyByteArrayObject *self, int proto)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002047{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002048 PyObject *dict;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002049 _Py_IDENTIFIER(__dict__);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002050 char *buf;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002051
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002052 dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002053 if (dict == NULL) {
2054 PyErr_Clear();
2055 dict = Py_None;
2056 Py_INCREF(dict);
2057 }
2058
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002059 buf = PyByteArray_AS_STRING(self);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002060 if (proto < 3) {
2061 /* use str based reduction for backwards compatibility with Python 2.x */
2062 PyObject *latin1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002063 if (Py_SIZE(self))
2064 latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002065 else
2066 latin1 = PyUnicode_FromString("");
2067 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
2068 }
2069 else {
2070 /* use more efficient byte based reduction */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002071 if (Py_SIZE(self)) {
2072 return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002073 }
2074 else {
2075 return Py_BuildValue("(O()N)", Py_TYPE(self), dict);
2076 }
2077 }
2078}
2079
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002080/*[clinic input]
2081bytearray.__reduce__ as bytearray_reduce
2082
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002083Return state information for pickling.
2084[clinic start generated code]*/
2085
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002086static PyObject *
2087bytearray_reduce_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002088/*[clinic end generated code: output=52bf304086464cab input=44b5737ada62dd3f]*/
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002089{
2090 return _common_reduce(self, 2);
2091}
2092
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002093/*[clinic input]
2094bytearray.__reduce_ex__ as bytearray_reduce_ex
2095
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002096 proto: int = 0
2097 /
2098
2099Return state information for pickling.
2100[clinic start generated code]*/
2101
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002102static PyObject *
2103bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002104/*[clinic end generated code: output=52eac33377197520 input=f129bc1a1aa151ee]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002105{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002106 return _common_reduce(self, proto);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002107}
2108
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002109/*[clinic input]
2110bytearray.__sizeof__ as bytearray_sizeof
2111
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002112Returns the size of the bytearray object in memory, in bytes.
2113[clinic start generated code]*/
2114
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002115static PyObject *
2116bytearray_sizeof_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002117/*[clinic end generated code: output=738abdd17951c427 input=e27320fd98a4bc5a]*/
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002118{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002119 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002120
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002121 res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002122 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002123}
2124
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002125static PySequenceMethods bytearray_as_sequence = {
2126 (lenfunc)bytearray_length, /* sq_length */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002127 (binaryfunc)PyByteArray_Concat, /* sq_concat */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002128 (ssizeargfunc)bytearray_repeat, /* sq_repeat */
2129 (ssizeargfunc)bytearray_getitem, /* sq_item */
2130 0, /* sq_slice */
2131 (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */
2132 0, /* sq_ass_slice */
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002133 (objobjproc)bytearray_contains, /* sq_contains */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002134 (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */
2135 (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002136};
2137
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002138static PyMappingMethods bytearray_as_mapping = {
2139 (lenfunc)bytearray_length,
2140 (binaryfunc)bytearray_subscript,
2141 (objobjargproc)bytearray_ass_subscript,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002142};
2143
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002144static PyBufferProcs bytearray_as_buffer = {
2145 (getbufferproc)bytearray_getbuffer,
2146 (releasebufferproc)bytearray_releasebuffer,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002147};
2148
2149static PyMethodDef
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002150bytearray_methods[] = {
2151 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002152 BYTEARRAY_REDUCE_METHODDEF
2153 BYTEARRAY_REDUCE_EX_METHODDEF
2154 BYTEARRAY_SIZEOF_METHODDEF
2155 BYTEARRAY_APPEND_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302156 {"capitalize", stringlib_capitalize, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002157 _Py_capitalize__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002158 STRINGLIB_CENTER_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002159 BYTEARRAY_CLEAR_METHODDEF
2160 BYTEARRAY_COPY_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002161 {"count", (PyCFunction)bytearray_count, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002162 _Py_count__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002163 BYTEARRAY_DECODE_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002164 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002165 _Py_endswith__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002166 STRINGLIB_EXPANDTABS_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002167 BYTEARRAY_EXTEND_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002168 {"find", (PyCFunction)bytearray_find, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002169 _Py_find__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002170 BYTEARRAY_FROMHEX_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002171 {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__},
2172 {"index", (PyCFunction)bytearray_index, METH_VARARGS, _Py_index__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002173 BYTEARRAY_INSERT_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302174 {"isalnum", stringlib_isalnum, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002175 _Py_isalnum__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302176 {"isalpha", stringlib_isalpha, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002177 _Py_isalpha__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302178 {"isascii", stringlib_isascii, METH_NOARGS,
INADA Naokia49ac992018-01-27 14:06:21 +09002179 _Py_isascii__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302180 {"isdigit", stringlib_isdigit, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002181 _Py_isdigit__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302182 {"islower", stringlib_islower, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002183 _Py_islower__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302184 {"isspace", stringlib_isspace, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002185 _Py_isspace__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302186 {"istitle", stringlib_istitle, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002187 _Py_istitle__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302188 {"isupper", stringlib_isupper, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002189 _Py_isupper__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002190 BYTEARRAY_JOIN_METHODDEF
Tal Einatc929df32018-07-06 13:17:38 +03002191 STRINGLIB_LJUST_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302192 {"lower", stringlib_lower, METH_NOARGS, _Py_lower__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002193 BYTEARRAY_LSTRIP_METHODDEF
2194 BYTEARRAY_MAKETRANS_METHODDEF
2195 BYTEARRAY_PARTITION_METHODDEF
2196 BYTEARRAY_POP_METHODDEF
2197 BYTEARRAY_REMOVE_METHODDEF
2198 BYTEARRAY_REPLACE_METHODDEF
2199 BYTEARRAY_REVERSE_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002200 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__},
2201 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002202 STRINGLIB_RJUST_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002203 BYTEARRAY_RPARTITION_METHODDEF
2204 BYTEARRAY_RSPLIT_METHODDEF
2205 BYTEARRAY_RSTRIP_METHODDEF
2206 BYTEARRAY_SPLIT_METHODDEF
2207 BYTEARRAY_SPLITLINES_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002208 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002209 _Py_startswith__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002210 BYTEARRAY_STRIP_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302211 {"swapcase", stringlib_swapcase, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002212 _Py_swapcase__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302213 {"title", stringlib_title, METH_NOARGS, _Py_title__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002214 BYTEARRAY_TRANSLATE_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302215 {"upper", stringlib_upper, METH_NOARGS, _Py_upper__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002216 STRINGLIB_ZFILL_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002217 {NULL}
2218};
2219
Ethan Furmanb95b5612015-01-23 20:05:18 -08002220static PyObject *
2221bytearray_mod(PyObject *v, PyObject *w)
2222{
2223 if (!PyByteArray_Check(v))
2224 Py_RETURN_NOTIMPLEMENTED;
Berker Peksag43de36d2016-04-16 01:20:47 +03002225 return _PyBytes_FormatEx(PyByteArray_AS_STRING(v), PyByteArray_GET_SIZE(v), w, 1);
Ethan Furmanb95b5612015-01-23 20:05:18 -08002226}
2227
2228static PyNumberMethods bytearray_as_number = {
2229 0, /*nb_add*/
2230 0, /*nb_subtract*/
2231 0, /*nb_multiply*/
2232 bytearray_mod, /*nb_remainder*/
2233};
2234
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002235PyDoc_STRVAR(bytearray_doc,
Georg Brandl17cb8a82008-05-30 08:20:09 +00002236"bytearray(iterable_of_ints) -> bytearray\n\
2237bytearray(string, encoding[, errors]) -> bytearray\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002238bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\
2239bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\
2240bytearray() -> empty bytes array\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002241\n\
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03002242Construct a mutable bytearray object from:\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002243 - an iterable yielding integers in range(256)\n\
2244 - a text string encoded using the specified encoding\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002245 - a bytes or a buffer object\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002246 - any object implementing the buffer API.\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002247 - an integer");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002248
2249
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002250static PyObject *bytearray_iter(PyObject *seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002251
2252PyTypeObject PyByteArray_Type = {
2253 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2254 "bytearray",
2255 sizeof(PyByteArrayObject),
2256 0,
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002257 (destructor)bytearray_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002258 0, /* tp_print */
2259 0, /* tp_getattr */
2260 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002261 0, /* tp_reserved */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002262 (reprfunc)bytearray_repr, /* tp_repr */
Ethan Furmanb95b5612015-01-23 20:05:18 -08002263 &bytearray_as_number, /* tp_as_number */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002264 &bytearray_as_sequence, /* tp_as_sequence */
2265 &bytearray_as_mapping, /* tp_as_mapping */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002266 0, /* tp_hash */
2267 0, /* tp_call */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002268 bytearray_str, /* tp_str */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002269 PyObject_GenericGetAttr, /* tp_getattro */
2270 0, /* tp_setattro */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002271 &bytearray_as_buffer, /* tp_as_buffer */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002272 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002273 bytearray_doc, /* tp_doc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002274 0, /* tp_traverse */
2275 0, /* tp_clear */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002276 (richcmpfunc)bytearray_richcompare, /* tp_richcompare */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002277 0, /* tp_weaklistoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002278 bytearray_iter, /* tp_iter */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002279 0, /* tp_iternext */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002280 bytearray_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002281 0, /* tp_members */
2282 0, /* tp_getset */
2283 0, /* tp_base */
2284 0, /* tp_dict */
2285 0, /* tp_descr_get */
2286 0, /* tp_descr_set */
2287 0, /* tp_dictoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002288 (initproc)bytearray_init, /* tp_init */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002289 PyType_GenericAlloc, /* tp_alloc */
2290 PyType_GenericNew, /* tp_new */
2291 PyObject_Del, /* tp_free */
2292};
2293
2294/*********************** Bytes Iterator ****************************/
2295
2296typedef struct {
2297 PyObject_HEAD
2298 Py_ssize_t it_index;
2299 PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
2300} bytesiterobject;
2301
2302static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002303bytearrayiter_dealloc(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002304{
2305 _PyObject_GC_UNTRACK(it);
2306 Py_XDECREF(it->it_seq);
2307 PyObject_GC_Del(it);
2308}
2309
2310static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002311bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002312{
2313 Py_VISIT(it->it_seq);
2314 return 0;
2315}
2316
2317static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002318bytearrayiter_next(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002319{
2320 PyByteArrayObject *seq;
2321 PyObject *item;
2322
2323 assert(it != NULL);
2324 seq = it->it_seq;
2325 if (seq == NULL)
2326 return NULL;
2327 assert(PyByteArray_Check(seq));
2328
2329 if (it->it_index < PyByteArray_GET_SIZE(seq)) {
2330 item = PyLong_FromLong(
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002331 (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002332 if (item != NULL)
2333 ++it->it_index;
2334 return item;
2335 }
2336
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002337 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03002338 Py_DECREF(seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002339 return NULL;
2340}
2341
2342static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302343bytearrayiter_length_hint(bytesiterobject *it, PyObject *Py_UNUSED(ignored))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002344{
2345 Py_ssize_t len = 0;
Serhiy Storchakaaf658722016-07-03 14:41:36 +03002346 if (it->it_seq) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002347 len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
Serhiy Storchakaaf658722016-07-03 14:41:36 +03002348 if (len < 0) {
2349 len = 0;
2350 }
2351 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002352 return PyLong_FromSsize_t(len);
2353}
2354
2355PyDoc_STRVAR(length_hint_doc,
2356 "Private method returning an estimate of len(list(it)).");
2357
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002358static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302359bytearrayiter_reduce(bytesiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002360{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002361 _Py_IDENTIFIER(iter);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002362 if (it->it_seq != NULL) {
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002363 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002364 it->it_seq, it->it_index);
2365 } else {
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002366 return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002367 }
2368}
2369
2370static PyObject *
2371bytearrayiter_setstate(bytesiterobject *it, PyObject *state)
2372{
2373 Py_ssize_t index = PyLong_AsSsize_t(state);
2374 if (index == -1 && PyErr_Occurred())
2375 return NULL;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00002376 if (it->it_seq != NULL) {
2377 if (index < 0)
2378 index = 0;
2379 else if (index > PyByteArray_GET_SIZE(it->it_seq))
2380 index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */
2381 it->it_index = index;
2382 }
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002383 Py_RETURN_NONE;
2384}
2385
2386PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
2387
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002388static PyMethodDef bytearrayiter_methods[] = {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002389 {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002390 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002391 {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002392 bytearray_reduce__doc__},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002393 {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
2394 setstate_doc},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002395 {NULL, NULL} /* sentinel */
2396};
2397
2398PyTypeObject PyByteArrayIter_Type = {
2399 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2400 "bytearray_iterator", /* tp_name */
2401 sizeof(bytesiterobject), /* tp_basicsize */
2402 0, /* tp_itemsize */
2403 /* methods */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002404 (destructor)bytearrayiter_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002405 0, /* tp_print */
2406 0, /* tp_getattr */
2407 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002408 0, /* tp_reserved */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002409 0, /* tp_repr */
2410 0, /* tp_as_number */
2411 0, /* tp_as_sequence */
2412 0, /* tp_as_mapping */
2413 0, /* tp_hash */
2414 0, /* tp_call */
2415 0, /* tp_str */
2416 PyObject_GenericGetAttr, /* tp_getattro */
2417 0, /* tp_setattro */
2418 0, /* tp_as_buffer */
2419 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
2420 0, /* tp_doc */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002421 (traverseproc)bytearrayiter_traverse, /* tp_traverse */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002422 0, /* tp_clear */
2423 0, /* tp_richcompare */
2424 0, /* tp_weaklistoffset */
2425 PyObject_SelfIter, /* tp_iter */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002426 (iternextfunc)bytearrayiter_next, /* tp_iternext */
2427 bytearrayiter_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002428 0,
2429};
2430
2431static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002432bytearray_iter(PyObject *seq)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002433{
2434 bytesiterobject *it;
2435
2436 if (!PyByteArray_Check(seq)) {
2437 PyErr_BadInternalCall();
2438 return NULL;
2439 }
2440 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
2441 if (it == NULL)
2442 return NULL;
2443 it->it_index = 0;
2444 Py_INCREF(seq);
2445 it->it_seq = (PyByteArrayObject *)seq;
2446 _PyObject_GC_TRACK(it);
2447 return (PyObject *)it;
2448}