blob: 782e27569780f2aceeae762dbb331fe85362629b [file] [log] [blame]
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001/* PyByteArray (bytearray) implementation */
2
3#define PY_SSIZE_T_CLEAN
4#include "Python.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06005#include "internal/mem.h"
6#include "internal/pystate.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00007#include "structmember.h"
8#include "bytes_methods.h"
Ethan Furmanb95b5612015-01-23 20:05:18 -08009#include "bytesobject.h"
Gregory P. Smith8cb65692015-04-25 23:22:26 +000010#include "pystrhex.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +000011
Martin v. Löwis7252a6e2014-07-27 16:25:09 +020012/*[clinic input]
13class bytearray "PyByteArrayObject *" "&PyByteArray_Type"
14[clinic start generated code]*/
15/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/
16
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000017char _PyByteArray_empty_string[] = "";
Christian Heimes2c9c7a52008-05-26 13:42:13 +000018
19void
20PyByteArray_Fini(void)
21{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000022}
23
24int
25PyByteArray_Init(void)
26{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000027 return 1;
28}
29
30/* end nullbytes support */
31
32/* Helpers */
33
34static int
35_getbytevalue(PyObject* arg, int *value)
36{
37 long face_value;
38
39 if (PyLong_Check(arg)) {
40 face_value = PyLong_AsLong(arg);
Georg Brandl9a54d7c2008-07-16 23:15:30 +000041 } else {
42 PyObject *index = PyNumber_Index(arg);
43 if (index == NULL) {
Mark Dickinson10de93a2010-07-09 19:25:48 +000044 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000045 return 0;
46 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +000047 face_value = PyLong_AsLong(index);
48 Py_DECREF(index);
49 }
50
51 if (face_value < 0 || face_value >= 256) {
52 /* this includes the OverflowError in case the long is too large */
53 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
Mark Dickinson10de93a2010-07-09 19:25:48 +000054 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000055 return 0;
56 }
57
58 *value = face_value;
59 return 1;
60}
61
62static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +000063bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000064{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000065 void *ptr;
66 if (view == NULL) {
Stefan Krah5178d912015-02-03 16:57:21 +010067 PyErr_SetString(PyExc_BufferError,
68 "bytearray_getbuffer: view==NULL argument is obsolete");
69 return -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000070 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000071 ptr = (void *) PyByteArray_AS_STRING(obj);
Stefan Krah5178d912015-02-03 16:57:21 +010072 /* cannot fail if view != NULL and readonly == 0 */
73 (void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
74 obj->ob_exports++;
75 return 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000076}
77
78static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +000079bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000080{
81 obj->ob_exports--;
82}
83
Antoine Pitrou5504e892008-12-06 21:27:53 +000084static int
85_canresize(PyByteArrayObject *self)
86{
87 if (self->ob_exports > 0) {
88 PyErr_SetString(PyExc_BufferError,
89 "Existing exports of data: object cannot be re-sized");
90 return 0;
91 }
92 return 1;
93}
94
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030095#include "clinic/bytearrayobject.c.h"
96
Christian Heimes2c9c7a52008-05-26 13:42:13 +000097/* Direct API functions */
98
99PyObject *
100PyByteArray_FromObject(PyObject *input)
101{
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100102 return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type,
103 input, NULL);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000104}
105
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300106static PyObject *
107_PyByteArray_FromBufferObject(PyObject *obj)
108{
109 PyObject *result;
110 Py_buffer view;
111
112 if (PyObject_GetBuffer(obj, &view, PyBUF_FULL_RO) < 0) {
113 return NULL;
114 }
115 result = PyByteArray_FromStringAndSize(NULL, view.len);
116 if (result != NULL &&
117 PyBuffer_ToContiguous(PyByteArray_AS_STRING(result),
118 &view, view.len, 'C') < 0)
119 {
120 Py_CLEAR(result);
121 }
122 PyBuffer_Release(&view);
123 return result;
124}
125
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000126PyObject *
127PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
128{
129 PyByteArrayObject *new;
130 Py_ssize_t alloc;
131
132 if (size < 0) {
133 PyErr_SetString(PyExc_SystemError,
134 "Negative size passed to PyByteArray_FromStringAndSize");
135 return NULL;
136 }
137
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000138 /* Prevent buffer overflow when setting alloc to size+1. */
139 if (size == PY_SSIZE_T_MAX) {
140 return PyErr_NoMemory();
141 }
142
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000143 new = PyObject_New(PyByteArrayObject, &PyByteArray_Type);
144 if (new == NULL)
145 return NULL;
146
147 if (size == 0) {
148 new->ob_bytes = NULL;
149 alloc = 0;
150 }
151 else {
152 alloc = size + 1;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100153 new->ob_bytes = PyObject_Malloc(alloc);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000154 if (new->ob_bytes == NULL) {
155 Py_DECREF(new);
156 return PyErr_NoMemory();
157 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +0000158 if (bytes != NULL && size > 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000159 memcpy(new->ob_bytes, bytes, size);
160 new->ob_bytes[size] = '\0'; /* Trailing null byte */
161 }
162 Py_SIZE(new) = size;
163 new->ob_alloc = alloc;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200164 new->ob_start = new->ob_bytes;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000165 new->ob_exports = 0;
166
167 return (PyObject *)new;
168}
169
170Py_ssize_t
171PyByteArray_Size(PyObject *self)
172{
173 assert(self != NULL);
174 assert(PyByteArray_Check(self));
175
176 return PyByteArray_GET_SIZE(self);
177}
178
179char *
180PyByteArray_AsString(PyObject *self)
181{
182 assert(self != NULL);
183 assert(PyByteArray_Check(self));
184
185 return PyByteArray_AS_STRING(self);
186}
187
188int
Antoine Pitroucc231542014-11-02 18:40:09 +0100189PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000190{
191 void *sval;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200192 PyByteArrayObject *obj = ((PyByteArrayObject *)self);
Antoine Pitroucc231542014-11-02 18:40:09 +0100193 /* All computations are done unsigned to avoid integer overflows
194 (see issue #22335). */
195 size_t alloc = (size_t) obj->ob_alloc;
196 size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes);
197 size_t size = (size_t) requested_size;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000198
199 assert(self != NULL);
200 assert(PyByteArray_Check(self));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200201 assert(logical_offset <= alloc);
Antoine Pitroucc231542014-11-02 18:40:09 +0100202 assert(requested_size >= 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000203
Antoine Pitroucc231542014-11-02 18:40:09 +0100204 if (requested_size == Py_SIZE(self)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000205 return 0;
206 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200207 if (!_canresize(obj)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000208 return -1;
209 }
210
Antoine Pitrou25454112015-05-19 20:52:27 +0200211 if (size + logical_offset + 1 <= alloc) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200212 /* Current buffer is large enough to host the requested size,
213 decide on a strategy. */
214 if (size < alloc / 2) {
215 /* Major downsize; resize down to exact size */
216 alloc = size + 1;
217 }
218 else {
219 /* Minor downsize; quick exit */
220 Py_SIZE(self) = size;
221 PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */
222 return 0;
223 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000224 }
225 else {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200226 /* Need growing, decide on a strategy */
227 if (size <= alloc * 1.125) {
228 /* Moderate upsize; overallocate similar to list_resize() */
229 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
230 }
231 else {
232 /* Major upsize; resize up to exact size */
233 alloc = size + 1;
234 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000235 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100236 if (alloc > PY_SSIZE_T_MAX) {
237 PyErr_NoMemory();
238 return -1;
239 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000240
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200241 if (logical_offset > 0) {
242 sval = PyObject_Malloc(alloc);
243 if (sval == NULL) {
244 PyErr_NoMemory();
245 return -1;
246 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100247 memcpy(sval, PyByteArray_AS_STRING(self),
Stefan Krahdce65022017-08-25 20:12:05 +0200248 Py_MIN((size_t)requested_size, (size_t)Py_SIZE(self)));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200249 PyObject_Free(obj->ob_bytes);
250 }
251 else {
252 sval = PyObject_Realloc(obj->ob_bytes, alloc);
253 if (sval == NULL) {
254 PyErr_NoMemory();
255 return -1;
256 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000257 }
258
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200259 obj->ob_bytes = obj->ob_start = sval;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000260 Py_SIZE(self) = size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200261 obj->ob_alloc = alloc;
262 obj->ob_bytes[size] = '\0'; /* Trailing null byte */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000263
264 return 0;
265}
266
267PyObject *
268PyByteArray_Concat(PyObject *a, PyObject *b)
269{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000270 Py_buffer va, vb;
271 PyByteArrayObject *result = NULL;
272
273 va.len = -1;
274 vb.len = -1;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200275 if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 ||
276 PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000277 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
Serhiy Storchaka6b5a9ec2017-03-19 19:47:02 +0200278 Py_TYPE(b)->tp_name, Py_TYPE(a)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000279 goto done;
280 }
281
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300282 if (va.len > PY_SSIZE_T_MAX - vb.len) {
283 PyErr_NoMemory();
284 goto done;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000285 }
286
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300287 result = (PyByteArrayObject *) \
288 PyByteArray_FromStringAndSize(NULL, va.len + vb.len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000289 if (result != NULL) {
290 memcpy(result->ob_bytes, va.buf, va.len);
291 memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
292 }
293
294 done:
295 if (va.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000296 PyBuffer_Release(&va);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000297 if (vb.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000298 PyBuffer_Release(&vb);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000299 return (PyObject *)result;
300}
301
302/* Functions stuffed into the type object */
303
304static Py_ssize_t
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000305bytearray_length(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000306{
307 return Py_SIZE(self);
308}
309
310static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000311bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000312{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000313 Py_ssize_t size;
314 Py_buffer vo;
315
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200316 if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000317 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
318 Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name);
319 return NULL;
320 }
321
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300322 size = Py_SIZE(self);
323 if (size > PY_SSIZE_T_MAX - vo.len) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000324 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000325 return PyErr_NoMemory();
326 }
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300327 if (PyByteArray_Resize((PyObject *)self, size + vo.len) < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000328 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000329 return NULL;
330 }
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300331 memcpy(PyByteArray_AS_STRING(self) + size, vo.buf, vo.len);
Martin v. Löwis423be952008-08-13 15:53:07 +0000332 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000333 Py_INCREF(self);
334 return (PyObject *)self;
335}
336
337static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000338bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000339{
340 PyByteArrayObject *result;
341 Py_ssize_t mysize;
342 Py_ssize_t size;
343
344 if (count < 0)
345 count = 0;
346 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000347 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000348 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000349 size = mysize * count;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000350 result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size);
351 if (result != NULL && size != 0) {
352 if (mysize == 1)
353 memset(result->ob_bytes, self->ob_bytes[0], size);
354 else {
355 Py_ssize_t i;
356 for (i = 0; i < count; i++)
357 memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize);
358 }
359 }
360 return (PyObject *)result;
361}
362
363static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000364bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000365{
366 Py_ssize_t mysize;
367 Py_ssize_t size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200368 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000369
370 if (count < 0)
371 count = 0;
372 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000373 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000374 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000375 size = mysize * count;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200376 if (PyByteArray_Resize((PyObject *)self, size) < 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000377 return NULL;
378
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200379 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000380 if (mysize == 1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200381 memset(buf, buf[0], size);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000382 else {
383 Py_ssize_t i;
384 for (i = 1; i < count; i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200385 memcpy(buf + i*mysize, buf, mysize);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000386 }
387
388 Py_INCREF(self);
389 return (PyObject *)self;
390}
391
392static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000393bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000394{
395 if (i < 0)
396 i += Py_SIZE(self);
397 if (i < 0 || i >= Py_SIZE(self)) {
398 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
399 return NULL;
400 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200401 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000402}
403
404static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000405bytearray_subscript(PyByteArrayObject *self, PyObject *index)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000406{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000407 if (PyIndex_Check(index)) {
408 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000409
410 if (i == -1 && PyErr_Occurred())
411 return NULL;
412
413 if (i < 0)
414 i += PyByteArray_GET_SIZE(self);
415
416 if (i < 0 || i >= Py_SIZE(self)) {
417 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
418 return NULL;
419 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200420 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000421 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000422 else if (PySlice_Check(index)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000423 Py_ssize_t start, stop, step, slicelength, cur, i;
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300424 if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000425 return NULL;
426 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300427 slicelength = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self),
428 &start, &stop, step);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000429
430 if (slicelength <= 0)
431 return PyByteArray_FromStringAndSize("", 0);
432 else if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200433 return PyByteArray_FromStringAndSize(
434 PyByteArray_AS_STRING(self) + start, slicelength);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000435 }
436 else {
437 char *source_buf = PyByteArray_AS_STRING(self);
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000438 char *result_buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000439 PyObject *result;
440
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000441 result = PyByteArray_FromStringAndSize(NULL, slicelength);
442 if (result == NULL)
443 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000444
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000445 result_buf = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000446 for (cur = start, i = 0; i < slicelength;
447 cur += step, i++) {
448 result_buf[i] = source_buf[cur];
449 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000450 return result;
451 }
452 }
453 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400454 PyErr_Format(PyExc_TypeError,
455 "bytearray indices must be integers or slices, not %.200s",
456 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000457 return NULL;
458 }
459}
460
461static int
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200462bytearray_setslice_linear(PyByteArrayObject *self,
463 Py_ssize_t lo, Py_ssize_t hi,
464 char *bytes, Py_ssize_t bytes_len)
465{
466 Py_ssize_t avail = hi - lo;
467 char *buf = PyByteArray_AS_STRING(self);
468 Py_ssize_t growth = bytes_len - avail;
Victor Stinner84557232013-11-21 12:29:51 +0100469 int res = 0;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200470 assert(avail >= 0);
471
Victor Stinner84557232013-11-21 12:29:51 +0100472 if (growth < 0) {
473 if (!_canresize(self))
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200474 return -1;
Victor Stinner84557232013-11-21 12:29:51 +0100475
476 if (lo == 0) {
477 /* Shrink the buffer by advancing its logical start */
478 self->ob_start -= growth;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200479 /*
Victor Stinner84557232013-11-21 12:29:51 +0100480 0 lo hi old_size
481 | |<----avail----->|<-----tail------>|
482 | |<-bytes_len->|<-----tail------>|
483 0 new_lo new_hi new_size
484 */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200485 }
Victor Stinner84557232013-11-21 12:29:51 +0100486 else {
487 /*
488 0 lo hi old_size
489 | |<----avail----->|<-----tomove------>|
490 | |<-bytes_len->|<-----tomove------>|
491 0 lo new_hi new_size
492 */
493 memmove(buf + lo + bytes_len, buf + hi,
494 Py_SIZE(self) - hi);
495 }
496 if (PyByteArray_Resize((PyObject *)self,
497 Py_SIZE(self) + growth) < 0) {
498 /* Issue #19578: Handling the memory allocation failure here is
499 tricky here because the bytearray object has already been
500 modified. Depending on growth and lo, the behaviour is
501 different.
502
503 If growth < 0 and lo != 0, the operation is completed, but a
504 MemoryError is still raised and the memory block is not
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700505 shrunk. Otherwise, the bytearray is restored in its previous
Victor Stinner84557232013-11-21 12:29:51 +0100506 state and a MemoryError is raised. */
507 if (lo == 0) {
508 self->ob_start += growth;
509 return -1;
510 }
511 /* memmove() removed bytes, the bytearray object cannot be
512 restored in its previous state. */
513 Py_SIZE(self) += growth;
514 res = -1;
515 }
516 buf = PyByteArray_AS_STRING(self);
517 }
518 else if (growth > 0) {
519 if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) {
520 PyErr_NoMemory();
521 return -1;
522 }
523
524 if (PyByteArray_Resize((PyObject *)self,
525 Py_SIZE(self) + growth) < 0) {
526 return -1;
527 }
528 buf = PyByteArray_AS_STRING(self);
529 /* Make the place for the additional bytes */
530 /*
531 0 lo hi old_size
532 | |<-avail->|<-----tomove------>|
533 | |<---bytes_len-->|<-----tomove------>|
534 0 lo new_hi new_size
535 */
536 memmove(buf + lo + bytes_len, buf + hi,
537 Py_SIZE(self) - lo - bytes_len);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200538 }
539
540 if (bytes_len > 0)
541 memcpy(buf + lo, bytes, bytes_len);
Victor Stinner84557232013-11-21 12:29:51 +0100542 return res;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200543}
544
545static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000546bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000547 PyObject *values)
548{
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200549 Py_ssize_t needed;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000550 void *bytes;
551 Py_buffer vbytes;
552 int res = 0;
553
554 vbytes.len = -1;
555 if (values == (PyObject *)self) {
556 /* Make a copy and call this function recursively */
557 int err;
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300558 values = PyByteArray_FromStringAndSize(PyByteArray_AS_STRING(values),
559 PyByteArray_GET_SIZE(values));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000560 if (values == NULL)
561 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000562 err = bytearray_setslice(self, lo, hi, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000563 Py_DECREF(values);
564 return err;
565 }
566 if (values == NULL) {
567 /* del b[lo:hi] */
568 bytes = NULL;
569 needed = 0;
570 }
571 else {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200572 if (PyObject_GetBuffer(values, &vbytes, PyBUF_SIMPLE) != 0) {
573 PyErr_Format(PyExc_TypeError,
574 "can't set bytearray slice from %.100s",
575 Py_TYPE(values)->tp_name);
576 return -1;
577 }
578 needed = vbytes.len;
579 bytes = vbytes.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000580 }
581
582 if (lo < 0)
583 lo = 0;
584 if (hi < lo)
585 hi = lo;
586 if (hi > Py_SIZE(self))
587 hi = Py_SIZE(self);
588
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200589 res = bytearray_setslice_linear(self, lo, hi, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000590 if (vbytes.len != -1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200591 PyBuffer_Release(&vbytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000592 return res;
593}
594
595static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000596bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000597{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000598 int ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000599
600 if (i < 0)
601 i += Py_SIZE(self);
602
603 if (i < 0 || i >= Py_SIZE(self)) {
604 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
605 return -1;
606 }
607
608 if (value == NULL)
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000609 return bytearray_setslice(self, i, i+1, NULL);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000610
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000611 if (!_getbytevalue(value, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000612 return -1;
613
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200614 PyByteArray_AS_STRING(self)[i] = ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000615 return 0;
616}
617
618static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000619bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000620{
621 Py_ssize_t start, stop, step, slicelen, needed;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200622 char *buf, *bytes;
623 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000624
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000625 if (PyIndex_Check(index)) {
626 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000627
628 if (i == -1 && PyErr_Occurred())
629 return -1;
630
631 if (i < 0)
632 i += PyByteArray_GET_SIZE(self);
633
634 if (i < 0 || i >= Py_SIZE(self)) {
635 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
636 return -1;
637 }
638
639 if (values == NULL) {
640 /* Fall through to slice assignment */
641 start = i;
642 stop = i + 1;
643 step = 1;
644 slicelen = 1;
645 }
646 else {
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000647 int ival;
648 if (!_getbytevalue(values, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000649 return -1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200650 buf[i] = (char)ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000651 return 0;
652 }
653 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000654 else if (PySlice_Check(index)) {
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300655 if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000656 return -1;
657 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300658 slicelen = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self), &start,
659 &stop, step);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000660 }
661 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400662 PyErr_Format(PyExc_TypeError,
663 "bytearray indices must be integers or slices, not %.200s",
664 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000665 return -1;
666 }
667
668 if (values == NULL) {
669 bytes = NULL;
670 needed = 0;
671 }
672 else if (values == (PyObject *)self || !PyByteArray_Check(values)) {
Christian Heimes6d26ade2012-11-03 23:07:59 +0100673 int err;
Ezio Melottic64bcbe2012-11-03 21:19:06 +0200674 if (PyNumber_Check(values) || PyUnicode_Check(values)) {
675 PyErr_SetString(PyExc_TypeError,
676 "can assign only bytes, buffers, or iterables "
677 "of ints in range(0, 256)");
678 return -1;
679 }
Georg Brandlf3fa5682010-12-04 17:09:30 +0000680 /* Make a copy and call this function recursively */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000681 values = PyByteArray_FromObject(values);
682 if (values == NULL)
683 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000684 err = bytearray_ass_subscript(self, index, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000685 Py_DECREF(values);
686 return err;
687 }
688 else {
689 assert(PyByteArray_Check(values));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200690 bytes = PyByteArray_AS_STRING(values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000691 needed = Py_SIZE(values);
692 }
693 /* Make sure b[5:2] = ... inserts before 5, not before 2. */
694 if ((step < 0 && start < stop) ||
695 (step > 0 && start > stop))
696 stop = start;
697 if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200698 return bytearray_setslice_linear(self, start, stop, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000699 }
700 else {
701 if (needed == 0) {
702 /* Delete slice */
Mark Dickinsonbc099642010-01-29 17:27:24 +0000703 size_t cur;
704 Py_ssize_t i;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000705
Antoine Pitrou5504e892008-12-06 21:27:53 +0000706 if (!_canresize(self))
707 return -1;
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000708
709 if (slicelen == 0)
710 /* Nothing to do here. */
711 return 0;
712
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000713 if (step < 0) {
714 stop = start + 1;
715 start = stop + step * (slicelen - 1) - 1;
716 step = -step;
717 }
718 for (cur = start, i = 0;
719 i < slicelen; cur += step, i++) {
720 Py_ssize_t lim = step - 1;
721
Mark Dickinson66f575b2010-02-14 12:53:32 +0000722 if (cur + step >= (size_t)PyByteArray_GET_SIZE(self))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000723 lim = PyByteArray_GET_SIZE(self) - cur - 1;
724
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200725 memmove(buf + cur - i,
726 buf + cur + 1, lim);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000727 }
728 /* Move the tail of the bytes, in one chunk */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000729 cur = start + (size_t)slicelen*step;
Mark Dickinson66f575b2010-02-14 12:53:32 +0000730 if (cur < (size_t)PyByteArray_GET_SIZE(self)) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200731 memmove(buf + cur - slicelen,
732 buf + cur,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000733 PyByteArray_GET_SIZE(self) - cur);
734 }
735 if (PyByteArray_Resize((PyObject *)self,
736 PyByteArray_GET_SIZE(self) - slicelen) < 0)
737 return -1;
738
739 return 0;
740 }
741 else {
742 /* Assign slice */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000743 Py_ssize_t i;
744 size_t cur;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000745
746 if (needed != slicelen) {
747 PyErr_Format(PyExc_ValueError,
748 "attempt to assign bytes of size %zd "
749 "to extended slice of size %zd",
750 needed, slicelen);
751 return -1;
752 }
753 for (cur = start, i = 0; i < slicelen; cur += step, i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200754 buf[cur] = bytes[i];
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000755 return 0;
756 }
757 }
758}
759
760static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000761bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000762{
763 static char *kwlist[] = {"source", "encoding", "errors", 0};
764 PyObject *arg = NULL;
765 const char *encoding = NULL;
766 const char *errors = NULL;
767 Py_ssize_t count;
768 PyObject *it;
769 PyObject *(*iternext)(PyObject *);
770
771 if (Py_SIZE(self) != 0) {
772 /* Empty previous contents (yes, do this first of all!) */
773 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
774 return -1;
775 }
776
777 /* Parse arguments */
Georg Brandl3dbca812008-07-23 16:10:53 +0000778 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000779 &arg, &encoding, &errors))
780 return -1;
781
782 /* Make a quick exit if no first argument */
783 if (arg == NULL) {
784 if (encoding != NULL || errors != NULL) {
785 PyErr_SetString(PyExc_TypeError,
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300786 encoding != NULL ?
787 "encoding without a string argument" :
788 "errors without a string argument");
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000789 return -1;
790 }
791 return 0;
792 }
793
794 if (PyUnicode_Check(arg)) {
795 /* Encode via the codec registry */
796 PyObject *encoded, *new;
797 if (encoding == NULL) {
798 PyErr_SetString(PyExc_TypeError,
799 "string argument without an encoding");
800 return -1;
801 }
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000802 encoded = PyUnicode_AsEncodedString(arg, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000803 if (encoded == NULL)
804 return -1;
805 assert(PyBytes_Check(encoded));
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000806 new = bytearray_iconcat(self, encoded);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000807 Py_DECREF(encoded);
808 if (new == NULL)
809 return -1;
810 Py_DECREF(new);
811 return 0;
812 }
813
814 /* If it's not unicode, there can't be encoding or errors */
815 if (encoding != NULL || errors != NULL) {
816 PyErr_SetString(PyExc_TypeError,
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300817 encoding != NULL ?
818 "encoding without a string argument" :
819 "errors without a string argument");
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000820 return -1;
821 }
822
823 /* Is it an int? */
Serhiy Storchakaeb249882016-08-15 09:46:07 +0300824 if (PyIndex_Check(arg)) {
825 count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
826 if (count == -1 && PyErr_Occurred()) {
Serhiy Storchakae8904212018-10-15 00:02:57 +0300827 if (!PyErr_ExceptionMatches(PyExc_TypeError))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000828 return -1;
INADA Naokia634e232017-01-06 17:32:01 +0900829 PyErr_Clear(); /* fall through */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000830 }
INADA Naokia634e232017-01-06 17:32:01 +0900831 else {
832 if (count < 0) {
833 PyErr_SetString(PyExc_ValueError, "negative count");
834 return -1;
835 }
836 if (count > 0) {
837 if (PyByteArray_Resize((PyObject *)self, count))
838 return -1;
839 memset(PyByteArray_AS_STRING(self), 0, count);
840 }
841 return 0;
842 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000843 }
844
845 /* Use the buffer API */
846 if (PyObject_CheckBuffer(arg)) {
847 Py_ssize_t size;
848 Py_buffer view;
849 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
850 return -1;
851 size = view.len;
852 if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200853 if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self),
854 &view, size, 'C') < 0)
Stefan Krah7d12d9d2012-07-28 12:25:55 +0200855 goto fail;
Martin v. Löwis423be952008-08-13 15:53:07 +0000856 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000857 return 0;
858 fail:
Martin v. Löwis423be952008-08-13 15:53:07 +0000859 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000860 return -1;
861 }
862
863 /* XXX Optimize this if the arguments is a list, tuple */
864
865 /* Get the iterator */
866 it = PyObject_GetIter(arg);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300867 if (it == NULL) {
868 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
869 PyErr_Format(PyExc_TypeError,
870 "cannot convert '%.200s' object to bytearray",
871 arg->ob_type->tp_name);
872 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000873 return -1;
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300874 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000875 iternext = *Py_TYPE(it)->tp_iternext;
876
877 /* Run the iterator to exhaustion */
878 for (;;) {
879 PyObject *item;
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000880 int rc, value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000881
882 /* Get the next item */
883 item = iternext(it);
884 if (item == NULL) {
885 if (PyErr_Occurred()) {
886 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
887 goto error;
888 PyErr_Clear();
889 }
890 break;
891 }
892
893 /* Interpret it as an int (__index__) */
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000894 rc = _getbytevalue(item, &value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000895 Py_DECREF(item);
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000896 if (!rc)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000897 goto error;
898
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000899 /* Append the byte */
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300900 if (Py_SIZE(self) + 1 < self->ob_alloc) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000901 Py_SIZE(self)++;
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300902 PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0';
903 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000904 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
905 goto error;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200906 PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000907 }
908
909 /* Clean up and return success */
910 Py_DECREF(it);
911 return 0;
912
913 error:
914 /* Error handling when it != NULL */
915 Py_DECREF(it);
916 return -1;
917}
918
919/* Mostly copied from string_repr, but without the
920 "smart quote" functionality. */
921static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000922bytearray_repr(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000923{
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300924 const char *className = _PyType_Name(Py_TYPE(self));
925 const char *quote_prefix = "(b";
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000926 const char *quote_postfix = ")";
927 Py_ssize_t length = Py_SIZE(self);
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300928 /* 6 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
929 Py_ssize_t newsize;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000930 PyObject *v;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200931 Py_ssize_t i;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200932 char *bytes;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200933 char c;
934 char *p;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200935 int quote;
936 char *test, *start;
937 char *buffer;
938
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300939 newsize = strlen(className);
940 if (length > (PY_SSIZE_T_MAX - 6 - newsize) / 4) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000941 PyErr_SetString(PyExc_OverflowError,
942 "bytearray object is too large to make repr");
943 return NULL;
944 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200945
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300946 newsize += 6 + length * 4;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100947 buffer = PyObject_Malloc(newsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200948 if (buffer == NULL) {
949 PyErr_NoMemory();
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000950 return NULL;
951 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000952
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200953 /* Figure out which quote to use; single is preferred */
954 quote = '\'';
955 start = PyByteArray_AS_STRING(self);
956 for (test = start; test < start+length; ++test) {
957 if (*test == '"') {
958 quote = '\''; /* back to single */
959 break;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000960 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200961 else if (*test == '\'')
962 quote = '"';
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000963 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200964
965 p = buffer;
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300966 while (*className)
967 *p++ = *className++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200968 while (*quote_prefix)
969 *p++ = *quote_prefix++;
970 *p++ = quote;
971
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200972 bytes = PyByteArray_AS_STRING(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200973 for (i = 0; i < length; i++) {
974 /* There's at least enough room for a hex escape
975 and a closing quote. */
976 assert(newsize - (p - buffer) >= 5);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200977 c = bytes[i];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200978 if (c == '\'' || c == '\\')
979 *p++ = '\\', *p++ = c;
980 else if (c == '\t')
981 *p++ = '\\', *p++ = 't';
982 else if (c == '\n')
983 *p++ = '\\', *p++ = 'n';
984 else if (c == '\r')
985 *p++ = '\\', *p++ = 'r';
986 else if (c == 0)
987 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
988 else if (c < ' ' || c >= 0x7f) {
989 *p++ = '\\';
990 *p++ = 'x';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200991 *p++ = Py_hexdigits[(c & 0xf0) >> 4];
992 *p++ = Py_hexdigits[c & 0xf];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200993 }
994 else
995 *p++ = c;
996 }
997 assert(newsize - (p - buffer) >= 1);
998 *p++ = quote;
999 while (*quote_postfix) {
1000 *p++ = *quote_postfix++;
1001 }
1002
Serhiy Storchakab3a77962017-09-21 14:24:13 +03001003 v = PyUnicode_FromStringAndSize(buffer, p - buffer);
Antoine Pitrou39aba4f2011-11-12 21:15:28 +01001004 PyObject_Free(buffer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001005 return v;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001006}
1007
1008static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001009bytearray_str(PyObject *op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001010{
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001011 if (Py_BytesWarningFlag) {
1012 if (PyErr_WarnEx(PyExc_BytesWarning,
1013 "str() on a bytearray instance", 1)) {
1014 return NULL;
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001015 }
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001016 }
1017 return bytearray_repr((PyByteArrayObject*)op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001018}
1019
1020static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001021bytearray_richcompare(PyObject *self, PyObject *other, int op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001022{
1023 Py_ssize_t self_size, other_size;
1024 Py_buffer self_bytes, other_bytes;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001025 int cmp, rc;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001026
1027 /* Bytes can be compared to anything that supports the (binary)
1028 buffer API. Except that a comparison with Unicode is always an
1029 error, even if the comparison is for equality. */
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001030 rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type);
1031 if (!rc)
1032 rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type);
1033 if (rc < 0)
1034 return NULL;
1035 if (rc) {
Barry Warsaw9e9dcd62008-10-17 01:50:37 +00001036 if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001037 if (PyErr_WarnEx(PyExc_BytesWarning,
Georg Brandle5d68ac2008-06-04 11:30:26 +00001038 "Comparison between bytearray and string", 1))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001039 return NULL;
1040 }
1041
Brian Curtindfc80e32011-08-10 20:28:54 -05001042 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001043 }
1044
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001045 if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001046 PyErr_Clear();
Brian Curtindfc80e32011-08-10 20:28:54 -05001047 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001048 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001049 self_size = self_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001050
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001051 if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001052 PyErr_Clear();
Martin v. Löwis423be952008-08-13 15:53:07 +00001053 PyBuffer_Release(&self_bytes);
Brian Curtindfc80e32011-08-10 20:28:54 -05001054 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001055 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001056 other_size = other_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001057
1058 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
1059 /* Shortcut: if the lengths differ, the objects differ */
stratakise8b19652017-11-02 11:32:54 +01001060 PyBuffer_Release(&self_bytes);
1061 PyBuffer_Release(&other_bytes);
1062 return PyBool_FromLong((op == Py_NE));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001063 }
1064 else {
stratakise8b19652017-11-02 11:32:54 +01001065 cmp = memcmp(self_bytes.buf, other_bytes.buf,
1066 Py_MIN(self_size, other_size));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001067 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
1068
stratakise8b19652017-11-02 11:32:54 +01001069 PyBuffer_Release(&self_bytes);
1070 PyBuffer_Release(&other_bytes);
1071
1072 if (cmp != 0) {
1073 Py_RETURN_RICHCOMPARE(cmp, 0, op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001074 }
1075
stratakise8b19652017-11-02 11:32:54 +01001076 Py_RETURN_RICHCOMPARE(self_size, other_size, op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001077 }
1078
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001079}
1080
1081static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001082bytearray_dealloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001083{
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001084 if (self->ob_exports > 0) {
1085 PyErr_SetString(PyExc_SystemError,
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001086 "deallocated bytearray object has exported buffers");
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001087 PyErr_Print();
1088 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001089 if (self->ob_bytes != 0) {
Antoine Pitrou39aba4f2011-11-12 21:15:28 +01001090 PyObject_Free(self->ob_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001091 }
1092 Py_TYPE(self)->tp_free((PyObject *)self);
1093}
1094
1095
1096/* -------------------------------------------------------------------- */
1097/* Methods */
1098
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001099#define FASTSEARCH fastsearch
1100#define STRINGLIB(F) stringlib_##F
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001101#define STRINGLIB_CHAR char
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001102#define STRINGLIB_SIZEOF_CHAR 1
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001103#define STRINGLIB_LEN PyByteArray_GET_SIZE
1104#define STRINGLIB_STR PyByteArray_AS_STRING
1105#define STRINGLIB_NEW PyByteArray_FromStringAndSize
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001106#define STRINGLIB_ISSPACE Py_ISSPACE
1107#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001108#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1109#define STRINGLIB_MUTABLE 1
1110
1111#include "stringlib/fastsearch.h"
1112#include "stringlib/count.h"
1113#include "stringlib/find.h"
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001114#include "stringlib/join.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001115#include "stringlib/partition.h"
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001116#include "stringlib/split.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001117#include "stringlib/ctype.h"
1118#include "stringlib/transmogrify.h"
1119
1120
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001121static PyObject *
1122bytearray_find(PyByteArrayObject *self, PyObject *args)
1123{
1124 return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1125}
1126
1127static PyObject *
1128bytearray_count(PyByteArrayObject *self, PyObject *args)
1129{
1130 return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1131}
1132
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001133/*[clinic input]
1134bytearray.clear
1135
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001136Remove all items from the bytearray.
1137[clinic start generated code]*/
1138
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001139static PyObject *
1140bytearray_clear_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001141/*[clinic end generated code: output=85c2fe6aede0956c input=ed6edae9de447ac4]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001142{
1143 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1144 return NULL;
1145 Py_RETURN_NONE;
1146}
1147
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001148/*[clinic input]
1149bytearray.copy
1150
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001151Return a copy of B.
1152[clinic start generated code]*/
1153
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001154static PyObject *
1155bytearray_copy_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001156/*[clinic end generated code: output=68cfbcfed484c132 input=6597b0c01bccaa9e]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001157{
1158 return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self),
1159 PyByteArray_GET_SIZE(self));
1160}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001161
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001162static PyObject *
1163bytearray_index(PyByteArrayObject *self, PyObject *args)
1164{
1165 return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1166}
1167
1168static PyObject *
1169bytearray_rfind(PyByteArrayObject *self, PyObject *args)
1170{
1171 return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1172}
1173
1174static PyObject *
1175bytearray_rindex(PyByteArrayObject *self, PyObject *args)
1176{
1177 return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1178}
1179
1180static int
1181bytearray_contains(PyObject *self, PyObject *arg)
1182{
1183 return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg);
1184}
1185
1186static PyObject *
1187bytearray_startswith(PyByteArrayObject *self, PyObject *args)
1188{
1189 return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1190}
1191
1192static PyObject *
1193bytearray_endswith(PyByteArrayObject *self, PyObject *args)
1194{
1195 return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1196}
1197
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001198
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001199/*[clinic input]
1200bytearray.translate
1201
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001202 table: object
1203 Translation table, which must be a bytes object of length 256.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001204 /
Martin Panter1b6c6da2016-08-27 08:35:02 +00001205 delete as deletechars: object(c_default="NULL") = b''
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001206
1207Return a copy with each character mapped by the given translation table.
1208
Martin Panter1b6c6da2016-08-27 08:35:02 +00001209All characters occurring in the optional argument delete are removed.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001210The remaining characters are mapped through the given translation table.
1211[clinic start generated code]*/
1212
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001213static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001214bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
Martin Panter1b6c6da2016-08-27 08:35:02 +00001215 PyObject *deletechars)
1216/*[clinic end generated code: output=b6a8f01c2a74e446 input=cfff956d4d127a9b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001217{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001218 char *input, *output;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001219 const char *table_chars;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001220 Py_ssize_t i, c;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001221 PyObject *input_obj = (PyObject*)self;
1222 const char *output_start;
1223 Py_ssize_t inlen;
Georg Brandlccc47b62008-12-28 11:44:14 +00001224 PyObject *result = NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001225 int trans_table[256];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001226 Py_buffer vtable, vdel;
1227
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001228 if (table == Py_None) {
1229 table_chars = NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001230 table = NULL;
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001231 } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001232 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001233 } else {
1234 if (vtable.len != 256) {
1235 PyErr_SetString(PyExc_ValueError,
1236 "translation table must be 256 characters long");
Georg Brandl953152f2009-07-22 12:03:59 +00001237 PyBuffer_Release(&vtable);
1238 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001239 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001240 table_chars = (const char*)vtable.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001241 }
1242
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001243 if (deletechars != NULL) {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001244 if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) {
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001245 if (table != NULL)
Georg Brandl953152f2009-07-22 12:03:59 +00001246 PyBuffer_Release(&vtable);
1247 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001248 }
1249 }
1250 else {
1251 vdel.buf = NULL;
1252 vdel.len = 0;
1253 }
1254
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001255 inlen = PyByteArray_GET_SIZE(input_obj);
1256 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1257 if (result == NULL)
1258 goto done;
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03001259 output_start = output = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001260 input = PyByteArray_AS_STRING(input_obj);
1261
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001262 if (vdel.len == 0 && table_chars != NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001263 /* If no deletions are required, use faster code */
1264 for (i = inlen; --i >= 0; ) {
1265 c = Py_CHARMASK(*input++);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001266 *output++ = table_chars[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001267 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001268 goto done;
1269 }
Georg Brandlccc47b62008-12-28 11:44:14 +00001270
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001271 if (table_chars == NULL) {
Georg Brandlccc47b62008-12-28 11:44:14 +00001272 for (i = 0; i < 256; i++)
1273 trans_table[i] = Py_CHARMASK(i);
1274 } else {
1275 for (i = 0; i < 256; i++)
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001276 trans_table[i] = Py_CHARMASK(table_chars[i]);
Georg Brandlccc47b62008-12-28 11:44:14 +00001277 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001278
1279 for (i = 0; i < vdel.len; i++)
1280 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1281
1282 for (i = inlen; --i >= 0; ) {
1283 c = Py_CHARMASK(*input++);
1284 if (trans_table[c] != -1)
Martin Panter1b6c6da2016-08-27 08:35:02 +00001285 *output++ = (char)trans_table[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001286 }
1287 /* Fix the size of the resulting string */
1288 if (inlen > 0)
Christian Heimesc731bbe2013-07-21 02:04:35 +02001289 if (PyByteArray_Resize(result, output - output_start) < 0) {
1290 Py_CLEAR(result);
1291 goto done;
1292 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001293
1294done:
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001295 if (table != NULL)
Georg Brandlccc47b62008-12-28 11:44:14 +00001296 PyBuffer_Release(&vtable);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001297 if (deletechars != NULL)
Martin v. Löwis423be952008-08-13 15:53:07 +00001298 PyBuffer_Release(&vdel);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001299 return result;
1300}
1301
1302
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001303/*[clinic input]
1304
1305@staticmethod
1306bytearray.maketrans
1307
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001308 frm: Py_buffer
1309 to: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001310 /
1311
1312Return a translation table useable for the bytes or bytearray translate method.
1313
1314The returned table will be one where each byte in frm is mapped to the byte at
1315the same position in to.
1316
1317The bytes objects frm and to must be of the same length.
1318[clinic start generated code]*/
1319
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001320static PyObject *
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001321bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001322/*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001323{
1324 return _Py_bytes_maketrans(frm, to);
Georg Brandlabc38772009-04-12 15:51:51 +00001325}
1326
1327
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001328/*[clinic input]
1329bytearray.replace
1330
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001331 old: Py_buffer
1332 new: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001333 count: Py_ssize_t = -1
1334 Maximum number of occurrences to replace.
1335 -1 (the default value) means replace all occurrences.
1336 /
1337
1338Return a copy with all occurrences of substring old replaced by new.
1339
1340If the optional argument count is given, only the first count occurrences are
1341replaced.
1342[clinic start generated code]*/
1343
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001344static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001345bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
1346 Py_buffer *new, Py_ssize_t count)
1347/*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001348{
Serhiy Storchakafb81d3c2016-05-05 09:26:07 +03001349 return stringlib_replace((PyObject *)self,
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001350 (const char *)old->buf, old->len,
1351 (const char *)new->buf, new->len, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001352}
1353
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001354/*[clinic input]
1355bytearray.split
1356
1357 sep: object = None
1358 The delimiter according which to split the bytearray.
1359 None (the default value) means split on ASCII whitespace characters
1360 (space, tab, return, newline, formfeed, vertical tab).
1361 maxsplit: Py_ssize_t = -1
1362 Maximum number of splits to do.
1363 -1 (the default value) means no limit.
1364
1365Return a list of the sections in the bytearray, using sep as the delimiter.
1366[clinic start generated code]*/
1367
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001368static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001369bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
1370 Py_ssize_t maxsplit)
1371/*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001372{
1373 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001374 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001375 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001376 Py_buffer vsub;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001377
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001378 if (maxsplit < 0)
1379 maxsplit = PY_SSIZE_T_MAX;
1380
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001381 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001382 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001383
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001384 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001385 return NULL;
1386 sub = vsub.buf;
1387 n = vsub.len;
1388
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001389 list = stringlib_split(
1390 (PyObject*) self, s, len, sub, n, maxsplit
1391 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001392 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001393 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001394}
1395
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001396/*[clinic input]
1397bytearray.partition
1398
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001399 sep: object
1400 /
1401
1402Partition the bytearray into three parts using the given separator.
1403
1404This will search for the separator sep in the bytearray. If the separator is
1405found, returns a 3-tuple containing the part before the separator, the
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001406separator itself, and the part after it as new bytearray objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001407
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001408If the separator is not found, returns a 3-tuple containing the copy of the
1409original bytearray object and two empty bytearray objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001410[clinic start generated code]*/
1411
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001412static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001413bytearray_partition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001414/*[clinic end generated code: output=45d2525ddd35f957 input=8f644749ee4fc83a]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001415{
1416 PyObject *bytesep, *result;
1417
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001418 bytesep = _PyByteArray_FromBufferObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001419 if (! bytesep)
1420 return NULL;
1421
1422 result = stringlib_partition(
1423 (PyObject*) self,
1424 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1425 bytesep,
1426 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1427 );
1428
1429 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001430 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001431}
1432
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001433/*[clinic input]
1434bytearray.rpartition
1435
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001436 sep: object
1437 /
1438
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001439Partition the bytearray into three parts using the given separator.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001440
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001441This will search for the separator sep in the bytearray, starting at the end.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001442If the separator is found, returns a 3-tuple containing the part before the
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001443separator, the separator itself, and the part after it as new bytearray
1444objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001445
1446If the separator is not found, returns a 3-tuple containing two empty bytearray
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001447objects and the copy of the original bytearray object.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001448[clinic start generated code]*/
1449
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001450static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001451bytearray_rpartition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001452/*[clinic end generated code: output=440de3c9426115e8 input=7e3df3e6cb8fa0ac]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001453{
1454 PyObject *bytesep, *result;
1455
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001456 bytesep = _PyByteArray_FromBufferObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001457 if (! bytesep)
1458 return NULL;
1459
1460 result = stringlib_rpartition(
1461 (PyObject*) self,
1462 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1463 bytesep,
1464 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1465 );
1466
1467 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001468 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001469}
1470
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001471/*[clinic input]
1472bytearray.rsplit = bytearray.split
1473
1474Return a list of the sections in the bytearray, using sep as the delimiter.
1475
1476Splitting is done starting at the end of the bytearray and working to the front.
1477[clinic start generated code]*/
1478
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001479static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001480bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
1481 Py_ssize_t maxsplit)
1482/*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001483{
1484 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001485 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001486 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001487 Py_buffer vsub;
1488
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001489 if (maxsplit < 0)
1490 maxsplit = PY_SSIZE_T_MAX;
1491
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001492 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001493 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001494
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001495 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001496 return NULL;
1497 sub = vsub.buf;
1498 n = vsub.len;
1499
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001500 list = stringlib_rsplit(
1501 (PyObject*) self, s, len, sub, n, maxsplit
1502 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001503 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001504 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001505}
1506
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001507/*[clinic input]
1508bytearray.reverse
1509
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001510Reverse the order of the values in B in place.
1511[clinic start generated code]*/
1512
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001513static PyObject *
1514bytearray_reverse_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001515/*[clinic end generated code: output=9f7616f29ab309d3 input=543356319fc78557]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001516{
1517 char swap, *head, *tail;
1518 Py_ssize_t i, j, n = Py_SIZE(self);
1519
1520 j = n / 2;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001521 head = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001522 tail = head + n - 1;
1523 for (i = 0; i < j; i++) {
1524 swap = *head;
1525 *head++ = *tail;
1526 *tail-- = swap;
1527 }
1528
1529 Py_RETURN_NONE;
1530}
1531
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001532
1533/*[python input]
1534class bytesvalue_converter(CConverter):
1535 type = 'int'
1536 converter = '_getbytevalue'
1537[python start generated code]*/
1538/*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/
1539
1540
1541/*[clinic input]
1542bytearray.insert
1543
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001544 index: Py_ssize_t
1545 The index where the value is to be inserted.
1546 item: bytesvalue
1547 The item to be inserted.
1548 /
1549
1550Insert a single item into the bytearray before the given index.
1551[clinic start generated code]*/
1552
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001553static PyObject *
1554bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001555/*[clinic end generated code: output=76c775a70e7b07b7 input=b2b5d07e9de6c070]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001556{
1557 Py_ssize_t n = Py_SIZE(self);
1558 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001559
1560 if (n == PY_SSIZE_T_MAX) {
1561 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001562 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001563 return NULL;
1564 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001565 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1566 return NULL;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001567 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001568
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001569 if (index < 0) {
1570 index += n;
1571 if (index < 0)
1572 index = 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001573 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001574 if (index > n)
1575 index = n;
1576 memmove(buf + index + 1, buf + index, n - index);
1577 buf[index] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001578
1579 Py_RETURN_NONE;
1580}
1581
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001582/*[clinic input]
1583bytearray.append
1584
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001585 item: bytesvalue
1586 The item to be appended.
1587 /
1588
1589Append a single item to the end of the bytearray.
1590[clinic start generated code]*/
1591
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001592static PyObject *
1593bytearray_append_impl(PyByteArrayObject *self, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001594/*[clinic end generated code: output=a154e19ed1886cb6 input=20d6bec3d1340593]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001595{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001596 Py_ssize_t n = Py_SIZE(self);
1597
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001598 if (n == PY_SSIZE_T_MAX) {
1599 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001600 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001601 return NULL;
1602 }
1603 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1604 return NULL;
1605
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001606 PyByteArray_AS_STRING(self)[n] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001607
1608 Py_RETURN_NONE;
1609}
1610
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001611/*[clinic input]
1612bytearray.extend
1613
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001614 iterable_of_ints: object
1615 The iterable of items to append.
1616 /
1617
1618Append all the items from the iterator or sequence to the end of the bytearray.
1619[clinic start generated code]*/
1620
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001621static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001622bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001623/*[clinic end generated code: output=98155dbe249170b1 input=c617b3a93249ba28]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001624{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001625 PyObject *it, *item, *bytearray_obj;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001626 Py_ssize_t buf_size = 0, len = 0;
1627 int value;
1628 char *buf;
1629
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001630 /* bytearray_setslice code only accepts something supporting PEP 3118. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001631 if (PyObject_CheckBuffer(iterable_of_ints)) {
1632 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_ints) == -1)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001633 return NULL;
1634
1635 Py_RETURN_NONE;
1636 }
1637
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001638 it = PyObject_GetIter(iterable_of_ints);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +03001639 if (it == NULL) {
1640 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
1641 PyErr_Format(PyExc_TypeError,
1642 "can't extend bytearray with %.100s",
1643 iterable_of_ints->ob_type->tp_name);
1644 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001645 return NULL;
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +03001646 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001647
Ezio Melotti42da6632011-03-15 05:18:48 +02001648 /* Try to determine the length of the argument. 32 is arbitrary. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001649 buf_size = PyObject_LengthHint(iterable_of_ints, 32);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001650 if (buf_size == -1) {
1651 Py_DECREF(it);
1652 return NULL;
1653 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001654
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001655 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001656 if (bytearray_obj == NULL) {
1657 Py_DECREF(it);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001658 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001659 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001660 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001661
1662 while ((item = PyIter_Next(it)) != NULL) {
1663 if (! _getbytevalue(item, &value)) {
1664 Py_DECREF(item);
1665 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001666 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001667 return NULL;
1668 }
1669 buf[len++] = value;
1670 Py_DECREF(item);
1671
1672 if (len >= buf_size) {
Martin Panter371731e2016-07-18 07:53:13 +00001673 Py_ssize_t addition;
1674 if (len == PY_SSIZE_T_MAX) {
1675 Py_DECREF(it);
1676 Py_DECREF(bytearray_obj);
1677 return PyErr_NoMemory();
1678 }
1679 addition = len >> 1;
1680 if (addition > PY_SSIZE_T_MAX - len - 1)
1681 buf_size = PY_SSIZE_T_MAX;
1682 else
1683 buf_size = len + addition + 1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001684 if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001685 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001686 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001687 return NULL;
1688 }
1689 /* Recompute the `buf' pointer, since the resizing operation may
1690 have invalidated it. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001691 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001692 }
1693 }
1694 Py_DECREF(it);
1695
1696 /* Resize down to exact size. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001697 if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) {
1698 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001699 return NULL;
1700 }
1701
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001702 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) {
1703 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001704 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001705 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001706 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001707
1708 Py_RETURN_NONE;
1709}
1710
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001711/*[clinic input]
1712bytearray.pop
1713
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001714 index: Py_ssize_t = -1
1715 The index from where to remove the item.
1716 -1 (the default value) means remove the last item.
1717 /
1718
1719Remove and return a single item from B.
1720
1721If no index argument is given, will pop the last item.
1722[clinic start generated code]*/
1723
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001724static PyObject *
1725bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001726/*[clinic end generated code: output=e0ccd401f8021da8 input=3591df2d06c0d237]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001727{
1728 int value;
1729 Py_ssize_t n = Py_SIZE(self);
1730 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001731
1732 if (n == 0) {
Eli Bendersky1bc4f192011-03-04 04:55:25 +00001733 PyErr_SetString(PyExc_IndexError,
1734 "pop from empty bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001735 return NULL;
1736 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001737 if (index < 0)
1738 index += Py_SIZE(self);
1739 if (index < 0 || index >= Py_SIZE(self)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001740 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1741 return NULL;
1742 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001743 if (!_canresize(self))
1744 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001745
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001746 buf = PyByteArray_AS_STRING(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001747 value = buf[index];
1748 memmove(buf + index, buf + index + 1, n - index);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001749 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1750 return NULL;
1751
Mark Dickinson54a3db92009-09-06 10:19:23 +00001752 return PyLong_FromLong((unsigned char)value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001753}
1754
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001755/*[clinic input]
1756bytearray.remove
1757
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001758 value: bytesvalue
1759 The value to remove.
1760 /
1761
1762Remove the first occurrence of a value in the bytearray.
1763[clinic start generated code]*/
1764
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001765static PyObject *
1766bytearray_remove_impl(PyByteArrayObject *self, int value)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001767/*[clinic end generated code: output=d659e37866709c13 input=121831240cd51ddf]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001768{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001769 Py_ssize_t where, n = Py_SIZE(self);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001770 char *buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001771
Serhiy Storchaka4b234942016-05-16 22:24:03 +03001772 where = stringlib_find_char(buf, n, value);
1773 if (where < 0) {
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001774 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001775 return NULL;
1776 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001777 if (!_canresize(self))
1778 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001779
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001780 memmove(buf + where, buf + where + 1, n - where);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001781 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1782 return NULL;
1783
1784 Py_RETURN_NONE;
1785}
1786
1787/* XXX These two helpers could be optimized if argsize == 1 */
1788
1789static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001790lstrip_helper(const char *myptr, Py_ssize_t mysize,
1791 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001792{
1793 Py_ssize_t i = 0;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001794 while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001795 i++;
1796 return i;
1797}
1798
1799static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001800rstrip_helper(const char *myptr, Py_ssize_t mysize,
1801 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001802{
1803 Py_ssize_t i = mysize - 1;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001804 while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001805 i--;
1806 return i + 1;
1807}
1808
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001809/*[clinic input]
1810bytearray.strip
1811
1812 bytes: object = None
1813 /
1814
1815Strip leading and trailing bytes contained in the argument.
1816
1817If the argument is omitted or None, strip leading and trailing ASCII whitespace.
1818[clinic start generated code]*/
1819
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001820static PyObject *
1821bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001822/*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001823{
1824 Py_ssize_t left, right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001825 char *myptr;
1826 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001827 Py_buffer vbytes;
1828
1829 if (bytes == Py_None) {
1830 bytesptr = "\t\n\r\f\v ";
1831 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001832 }
1833 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001834 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001835 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001836 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001837 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001838 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001839 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001840 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001841 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001842 if (left == mysize)
1843 right = left;
1844 else
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001845 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1846 if (bytes != Py_None)
1847 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001848 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001849}
1850
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001851/*[clinic input]
1852bytearray.lstrip
1853
1854 bytes: object = None
1855 /
1856
1857Strip leading bytes contained in the argument.
1858
1859If the argument is omitted or None, strip leading ASCII whitespace.
1860[clinic start generated code]*/
1861
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001862static PyObject *
1863bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001864/*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001865{
1866 Py_ssize_t left, right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001867 char *myptr;
1868 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001869 Py_buffer vbytes;
1870
1871 if (bytes == Py_None) {
1872 bytesptr = "\t\n\r\f\v ";
1873 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001874 }
1875 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001876 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001877 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001878 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001879 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001880 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001881 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001882 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001883 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001884 right = mysize;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001885 if (bytes != Py_None)
1886 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001887 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001888}
1889
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001890/*[clinic input]
1891bytearray.rstrip
1892
1893 bytes: object = None
1894 /
1895
1896Strip trailing bytes contained in the argument.
1897
1898If the argument is omitted or None, strip trailing ASCII whitespace.
1899[clinic start generated code]*/
1900
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001901static PyObject *
1902bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001903/*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001904{
1905 Py_ssize_t right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001906 char *myptr;
1907 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001908 Py_buffer vbytes;
1909
1910 if (bytes == Py_None) {
1911 bytesptr = "\t\n\r\f\v ";
1912 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001913 }
1914 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001915 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001916 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001917 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001918 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001919 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001920 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001921 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001922 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1923 if (bytes != Py_None)
1924 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001925 return PyByteArray_FromStringAndSize(myptr, right);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001926}
1927
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001928/*[clinic input]
1929bytearray.decode
1930
1931 encoding: str(c_default="NULL") = 'utf-8'
1932 The encoding with which to decode the bytearray.
1933 errors: str(c_default="NULL") = 'strict'
1934 The error handling scheme to use for the handling of decoding errors.
1935 The default is 'strict' meaning that decoding errors raise a
1936 UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
1937 as well as any other name registered with codecs.register_error that
1938 can handle UnicodeDecodeErrors.
1939
1940Decode the bytearray using the codec registered for encoding.
1941[clinic start generated code]*/
1942
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001943static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001944bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
1945 const char *errors)
1946/*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001947{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001948 if (encoding == NULL)
1949 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis0efea322014-07-27 17:29:17 +02001950 return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001951}
1952
1953PyDoc_STRVAR(alloc_doc,
1954"B.__alloc__() -> int\n\
1955\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00001956Return the number of bytes actually allocated.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001957
1958static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301959bytearray_alloc(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001960{
1961 return PyLong_FromSsize_t(self->ob_alloc);
1962}
1963
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001964/*[clinic input]
1965bytearray.join
1966
1967 iterable_of_bytes: object
1968 /
1969
1970Concatenate any number of bytes/bytearray objects.
1971
1972The bytearray whose method is called is inserted in between each pair.
1973
1974The result is returned as a new bytearray object.
1975[clinic start generated code]*/
1976
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001977static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001978bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001979/*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001980{
Martin v. Löwis0efea322014-07-27 17:29:17 +02001981 return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001982}
1983
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001984/*[clinic input]
1985bytearray.splitlines
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001986
Serhiy Storchaka202fda52017-03-12 10:10:47 +02001987 keepends: bool(accept={int}) = False
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001988
1989Return a list of the lines in the bytearray, breaking at line boundaries.
1990
1991Line breaks are not included in the resulting list unless keepends is given and
1992true.
1993[clinic start generated code]*/
1994
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001995static PyObject *
1996bytearray_splitlines_impl(PyByteArrayObject *self, int keepends)
Serhiy Storchaka202fda52017-03-12 10:10:47 +02001997/*[clinic end generated code: output=4223c94b895f6ad9 input=99a27ad959b9cf6b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001998{
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001999 return stringlib_splitlines(
2000 (PyObject*) self, PyByteArray_AS_STRING(self),
2001 PyByteArray_GET_SIZE(self), keepends
2002 );
2003}
2004
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002005/*[clinic input]
2006@classmethod
2007bytearray.fromhex
2008
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002009 string: unicode
2010 /
2011
2012Create a bytearray object from a string of hexadecimal numbers.
2013
2014Spaces between two numbers are accepted.
2015Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')
2016[clinic start generated code]*/
2017
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002018static PyObject *
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002019bytearray_fromhex_impl(PyTypeObject *type, PyObject *string)
2020/*[clinic end generated code: output=8f0f0b6d30fb3ba0 input=f033a16d1fb21f48]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002021{
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002022 PyObject *result = _PyBytes_FromHex(string, type == &PyByteArray_Type);
2023 if (type != &PyByteArray_Type && result != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002024 Py_SETREF(result, PyObject_CallFunctionObjArgs((PyObject *)type,
2025 result, NULL));
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002026 }
2027 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002028}
2029
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002030PyDoc_STRVAR(hex__doc__,
2031"B.hex() -> string\n\
2032\n\
2033Create a string of hexadecimal numbers from a bytearray object.\n\
2034Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'.");
2035
2036static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302037bytearray_hex(PyBytesObject *self, PyObject *Py_UNUSED(ignored))
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002038{
2039 char* argbuf = PyByteArray_AS_STRING(self);
2040 Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
2041 return _Py_strhex(argbuf, arglen);
2042}
2043
2044static PyObject *
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002045_common_reduce(PyByteArrayObject *self, int proto)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002046{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002047 PyObject *dict;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002048 _Py_IDENTIFIER(__dict__);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002049 char *buf;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002050
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002051 dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002052 if (dict == NULL) {
2053 PyErr_Clear();
2054 dict = Py_None;
2055 Py_INCREF(dict);
2056 }
2057
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002058 buf = PyByteArray_AS_STRING(self);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002059 if (proto < 3) {
2060 /* use str based reduction for backwards compatibility with Python 2.x */
2061 PyObject *latin1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002062 if (Py_SIZE(self))
2063 latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002064 else
2065 latin1 = PyUnicode_FromString("");
2066 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
2067 }
2068 else {
2069 /* use more efficient byte based reduction */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002070 if (Py_SIZE(self)) {
2071 return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002072 }
2073 else {
2074 return Py_BuildValue("(O()N)", Py_TYPE(self), dict);
2075 }
2076 }
2077}
2078
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002079/*[clinic input]
2080bytearray.__reduce__ as bytearray_reduce
2081
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002082Return state information for pickling.
2083[clinic start generated code]*/
2084
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002085static PyObject *
2086bytearray_reduce_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002087/*[clinic end generated code: output=52bf304086464cab input=44b5737ada62dd3f]*/
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002088{
2089 return _common_reduce(self, 2);
2090}
2091
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002092/*[clinic input]
2093bytearray.__reduce_ex__ as bytearray_reduce_ex
2094
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002095 proto: int = 0
2096 /
2097
2098Return state information for pickling.
2099[clinic start generated code]*/
2100
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002101static PyObject *
2102bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002103/*[clinic end generated code: output=52eac33377197520 input=f129bc1a1aa151ee]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002104{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002105 return _common_reduce(self, proto);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002106}
2107
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002108/*[clinic input]
2109bytearray.__sizeof__ as bytearray_sizeof
2110
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002111Returns the size of the bytearray object in memory, in bytes.
2112[clinic start generated code]*/
2113
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002114static PyObject *
2115bytearray_sizeof_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002116/*[clinic end generated code: output=738abdd17951c427 input=e27320fd98a4bc5a]*/
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002117{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002118 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002119
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002120 res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002121 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002122}
2123
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002124static PySequenceMethods bytearray_as_sequence = {
2125 (lenfunc)bytearray_length, /* sq_length */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002126 (binaryfunc)PyByteArray_Concat, /* sq_concat */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002127 (ssizeargfunc)bytearray_repeat, /* sq_repeat */
2128 (ssizeargfunc)bytearray_getitem, /* sq_item */
2129 0, /* sq_slice */
2130 (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */
2131 0, /* sq_ass_slice */
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002132 (objobjproc)bytearray_contains, /* sq_contains */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002133 (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */
2134 (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002135};
2136
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002137static PyMappingMethods bytearray_as_mapping = {
2138 (lenfunc)bytearray_length,
2139 (binaryfunc)bytearray_subscript,
2140 (objobjargproc)bytearray_ass_subscript,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002141};
2142
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002143static PyBufferProcs bytearray_as_buffer = {
2144 (getbufferproc)bytearray_getbuffer,
2145 (releasebufferproc)bytearray_releasebuffer,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002146};
2147
2148static PyMethodDef
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002149bytearray_methods[] = {
2150 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002151 BYTEARRAY_REDUCE_METHODDEF
2152 BYTEARRAY_REDUCE_EX_METHODDEF
2153 BYTEARRAY_SIZEOF_METHODDEF
2154 BYTEARRAY_APPEND_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302155 {"capitalize", stringlib_capitalize, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002156 _Py_capitalize__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002157 STRINGLIB_CENTER_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002158 BYTEARRAY_CLEAR_METHODDEF
2159 BYTEARRAY_COPY_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002160 {"count", (PyCFunction)bytearray_count, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002161 _Py_count__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002162 BYTEARRAY_DECODE_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002163 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002164 _Py_endswith__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002165 STRINGLIB_EXPANDTABS_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002166 BYTEARRAY_EXTEND_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002167 {"find", (PyCFunction)bytearray_find, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002168 _Py_find__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002169 BYTEARRAY_FROMHEX_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002170 {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__},
2171 {"index", (PyCFunction)bytearray_index, METH_VARARGS, _Py_index__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002172 BYTEARRAY_INSERT_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302173 {"isalnum", stringlib_isalnum, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002174 _Py_isalnum__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302175 {"isalpha", stringlib_isalpha, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002176 _Py_isalpha__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302177 {"isascii", stringlib_isascii, METH_NOARGS,
INADA Naokia49ac992018-01-27 14:06:21 +09002178 _Py_isascii__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302179 {"isdigit", stringlib_isdigit, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002180 _Py_isdigit__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302181 {"islower", stringlib_islower, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002182 _Py_islower__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302183 {"isspace", stringlib_isspace, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002184 _Py_isspace__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302185 {"istitle", stringlib_istitle, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002186 _Py_istitle__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302187 {"isupper", stringlib_isupper, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002188 _Py_isupper__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002189 BYTEARRAY_JOIN_METHODDEF
Tal Einatc929df32018-07-06 13:17:38 +03002190 STRINGLIB_LJUST_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302191 {"lower", stringlib_lower, METH_NOARGS, _Py_lower__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002192 BYTEARRAY_LSTRIP_METHODDEF
2193 BYTEARRAY_MAKETRANS_METHODDEF
2194 BYTEARRAY_PARTITION_METHODDEF
2195 BYTEARRAY_POP_METHODDEF
2196 BYTEARRAY_REMOVE_METHODDEF
2197 BYTEARRAY_REPLACE_METHODDEF
2198 BYTEARRAY_REVERSE_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002199 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__},
2200 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002201 STRINGLIB_RJUST_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002202 BYTEARRAY_RPARTITION_METHODDEF
2203 BYTEARRAY_RSPLIT_METHODDEF
2204 BYTEARRAY_RSTRIP_METHODDEF
2205 BYTEARRAY_SPLIT_METHODDEF
2206 BYTEARRAY_SPLITLINES_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002207 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002208 _Py_startswith__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002209 BYTEARRAY_STRIP_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302210 {"swapcase", stringlib_swapcase, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002211 _Py_swapcase__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302212 {"title", stringlib_title, METH_NOARGS, _Py_title__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002213 BYTEARRAY_TRANSLATE_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302214 {"upper", stringlib_upper, METH_NOARGS, _Py_upper__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002215 STRINGLIB_ZFILL_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002216 {NULL}
2217};
2218
Ethan Furmanb95b5612015-01-23 20:05:18 -08002219static PyObject *
2220bytearray_mod(PyObject *v, PyObject *w)
2221{
2222 if (!PyByteArray_Check(v))
2223 Py_RETURN_NOTIMPLEMENTED;
Berker Peksag43de36d2016-04-16 01:20:47 +03002224 return _PyBytes_FormatEx(PyByteArray_AS_STRING(v), PyByteArray_GET_SIZE(v), w, 1);
Ethan Furmanb95b5612015-01-23 20:05:18 -08002225}
2226
2227static PyNumberMethods bytearray_as_number = {
2228 0, /*nb_add*/
2229 0, /*nb_subtract*/
2230 0, /*nb_multiply*/
2231 bytearray_mod, /*nb_remainder*/
2232};
2233
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002234PyDoc_STRVAR(bytearray_doc,
Georg Brandl17cb8a82008-05-30 08:20:09 +00002235"bytearray(iterable_of_ints) -> bytearray\n\
2236bytearray(string, encoding[, errors]) -> bytearray\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002237bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\
2238bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\
2239bytearray() -> empty bytes array\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002240\n\
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03002241Construct a mutable bytearray object from:\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002242 - an iterable yielding integers in range(256)\n\
2243 - a text string encoded using the specified encoding\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002244 - a bytes or a buffer object\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002245 - any object implementing the buffer API.\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002246 - an integer");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002247
2248
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002249static PyObject *bytearray_iter(PyObject *seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002250
2251PyTypeObject PyByteArray_Type = {
2252 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2253 "bytearray",
2254 sizeof(PyByteArrayObject),
2255 0,
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002256 (destructor)bytearray_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002257 0, /* tp_print */
2258 0, /* tp_getattr */
2259 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002260 0, /* tp_reserved */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002261 (reprfunc)bytearray_repr, /* tp_repr */
Ethan Furmanb95b5612015-01-23 20:05:18 -08002262 &bytearray_as_number, /* tp_as_number */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002263 &bytearray_as_sequence, /* tp_as_sequence */
2264 &bytearray_as_mapping, /* tp_as_mapping */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002265 0, /* tp_hash */
2266 0, /* tp_call */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002267 bytearray_str, /* tp_str */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002268 PyObject_GenericGetAttr, /* tp_getattro */
2269 0, /* tp_setattro */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002270 &bytearray_as_buffer, /* tp_as_buffer */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002271 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002272 bytearray_doc, /* tp_doc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002273 0, /* tp_traverse */
2274 0, /* tp_clear */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002275 (richcmpfunc)bytearray_richcompare, /* tp_richcompare */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002276 0, /* tp_weaklistoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002277 bytearray_iter, /* tp_iter */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002278 0, /* tp_iternext */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002279 bytearray_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002280 0, /* tp_members */
2281 0, /* tp_getset */
2282 0, /* tp_base */
2283 0, /* tp_dict */
2284 0, /* tp_descr_get */
2285 0, /* tp_descr_set */
2286 0, /* tp_dictoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002287 (initproc)bytearray_init, /* tp_init */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002288 PyType_GenericAlloc, /* tp_alloc */
2289 PyType_GenericNew, /* tp_new */
2290 PyObject_Del, /* tp_free */
2291};
2292
2293/*********************** Bytes Iterator ****************************/
2294
2295typedef struct {
2296 PyObject_HEAD
2297 Py_ssize_t it_index;
2298 PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
2299} bytesiterobject;
2300
2301static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002302bytearrayiter_dealloc(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002303{
2304 _PyObject_GC_UNTRACK(it);
2305 Py_XDECREF(it->it_seq);
2306 PyObject_GC_Del(it);
2307}
2308
2309static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002310bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002311{
2312 Py_VISIT(it->it_seq);
2313 return 0;
2314}
2315
2316static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002317bytearrayiter_next(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002318{
2319 PyByteArrayObject *seq;
2320 PyObject *item;
2321
2322 assert(it != NULL);
2323 seq = it->it_seq;
2324 if (seq == NULL)
2325 return NULL;
2326 assert(PyByteArray_Check(seq));
2327
2328 if (it->it_index < PyByteArray_GET_SIZE(seq)) {
2329 item = PyLong_FromLong(
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002330 (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002331 if (item != NULL)
2332 ++it->it_index;
2333 return item;
2334 }
2335
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002336 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03002337 Py_DECREF(seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002338 return NULL;
2339}
2340
2341static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302342bytearrayiter_length_hint(bytesiterobject *it, PyObject *Py_UNUSED(ignored))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002343{
2344 Py_ssize_t len = 0;
Serhiy Storchakaaf658722016-07-03 14:41:36 +03002345 if (it->it_seq) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002346 len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
Serhiy Storchakaaf658722016-07-03 14:41:36 +03002347 if (len < 0) {
2348 len = 0;
2349 }
2350 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002351 return PyLong_FromSsize_t(len);
2352}
2353
2354PyDoc_STRVAR(length_hint_doc,
2355 "Private method returning an estimate of len(list(it)).");
2356
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002357static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302358bytearrayiter_reduce(bytesiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002359{
2360 if (it->it_seq != NULL) {
Antoine Pitroua7013882012-04-05 00:04:20 +02002361 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002362 it->it_seq, it->it_index);
2363 } else {
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02002364 return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002365 }
2366}
2367
2368static PyObject *
2369bytearrayiter_setstate(bytesiterobject *it, PyObject *state)
2370{
2371 Py_ssize_t index = PyLong_AsSsize_t(state);
2372 if (index == -1 && PyErr_Occurred())
2373 return NULL;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00002374 if (it->it_seq != NULL) {
2375 if (index < 0)
2376 index = 0;
2377 else if (index > PyByteArray_GET_SIZE(it->it_seq))
2378 index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */
2379 it->it_index = index;
2380 }
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002381 Py_RETURN_NONE;
2382}
2383
2384PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
2385
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002386static PyMethodDef bytearrayiter_methods[] = {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002387 {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002388 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002389 {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002390 bytearray_reduce__doc__},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002391 {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
2392 setstate_doc},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002393 {NULL, NULL} /* sentinel */
2394};
2395
2396PyTypeObject PyByteArrayIter_Type = {
2397 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2398 "bytearray_iterator", /* tp_name */
2399 sizeof(bytesiterobject), /* tp_basicsize */
2400 0, /* tp_itemsize */
2401 /* methods */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002402 (destructor)bytearrayiter_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002403 0, /* tp_print */
2404 0, /* tp_getattr */
2405 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002406 0, /* tp_reserved */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002407 0, /* tp_repr */
2408 0, /* tp_as_number */
2409 0, /* tp_as_sequence */
2410 0, /* tp_as_mapping */
2411 0, /* tp_hash */
2412 0, /* tp_call */
2413 0, /* tp_str */
2414 PyObject_GenericGetAttr, /* tp_getattro */
2415 0, /* tp_setattro */
2416 0, /* tp_as_buffer */
2417 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
2418 0, /* tp_doc */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002419 (traverseproc)bytearrayiter_traverse, /* tp_traverse */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002420 0, /* tp_clear */
2421 0, /* tp_richcompare */
2422 0, /* tp_weaklistoffset */
2423 PyObject_SelfIter, /* tp_iter */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002424 (iternextfunc)bytearrayiter_next, /* tp_iternext */
2425 bytearrayiter_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002426 0,
2427};
2428
2429static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002430bytearray_iter(PyObject *seq)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002431{
2432 bytesiterobject *it;
2433
2434 if (!PyByteArray_Check(seq)) {
2435 PyErr_BadInternalCall();
2436 return NULL;
2437 }
2438 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
2439 if (it == NULL)
2440 return NULL;
2441 it->it_index = 0;
2442 Py_INCREF(seq);
2443 it->it_seq = (PyByteArrayObject *)seq;
2444 _PyObject_GC_TRACK(it);
2445 return (PyObject *)it;
2446}