blob: 9dd67127b6146470e78fef7f861ddf96ac0a7b35 [file] [log] [blame]
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001/* PyByteArray (bytearray) implementation */
2
3#define PY_SSIZE_T_CLEAN
4#include "Python.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +01005#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01006#include "pycore_pymem.h"
7#include "pycore_pystate.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00008#include "structmember.h"
9#include "bytes_methods.h"
Ethan Furmanb95b5612015-01-23 20:05:18 -080010#include "bytesobject.h"
Gregory P. Smith8cb65692015-04-25 23:22:26 +000011#include "pystrhex.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +000012
Martin v. Löwis7252a6e2014-07-27 16:25:09 +020013/*[clinic input]
14class bytearray "PyByteArrayObject *" "&PyByteArray_Type"
15[clinic start generated code]*/
16/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/
17
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000018char _PyByteArray_empty_string[] = "";
Christian Heimes2c9c7a52008-05-26 13:42:13 +000019
Christian Heimes2c9c7a52008-05-26 13:42:13 +000020/* end nullbytes support */
21
22/* Helpers */
23
24static int
25_getbytevalue(PyObject* arg, int *value)
26{
27 long face_value;
28
29 if (PyLong_Check(arg)) {
30 face_value = PyLong_AsLong(arg);
Georg Brandl9a54d7c2008-07-16 23:15:30 +000031 } else {
32 PyObject *index = PyNumber_Index(arg);
33 if (index == NULL) {
Mark Dickinson10de93a2010-07-09 19:25:48 +000034 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000035 return 0;
36 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +000037 face_value = PyLong_AsLong(index);
38 Py_DECREF(index);
39 }
40
41 if (face_value < 0 || face_value >= 256) {
42 /* this includes the OverflowError in case the long is too large */
43 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
Mark Dickinson10de93a2010-07-09 19:25:48 +000044 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000045 return 0;
46 }
47
48 *value = face_value;
49 return 1;
50}
51
52static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +000053bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000054{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000055 void *ptr;
56 if (view == NULL) {
Stefan Krah5178d912015-02-03 16:57:21 +010057 PyErr_SetString(PyExc_BufferError,
58 "bytearray_getbuffer: view==NULL argument is obsolete");
59 return -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000060 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000061 ptr = (void *) PyByteArray_AS_STRING(obj);
Stefan Krah5178d912015-02-03 16:57:21 +010062 /* cannot fail if view != NULL and readonly == 0 */
63 (void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
64 obj->ob_exports++;
65 return 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000066}
67
68static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +000069bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000070{
71 obj->ob_exports--;
72}
73
Antoine Pitrou5504e892008-12-06 21:27:53 +000074static int
75_canresize(PyByteArrayObject *self)
76{
77 if (self->ob_exports > 0) {
78 PyErr_SetString(PyExc_BufferError,
79 "Existing exports of data: object cannot be re-sized");
80 return 0;
81 }
82 return 1;
83}
84
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030085#include "clinic/bytearrayobject.c.h"
86
Christian Heimes2c9c7a52008-05-26 13:42:13 +000087/* Direct API functions */
88
89PyObject *
90PyByteArray_FromObject(PyObject *input)
91{
Jeroen Demeyer196a5302019-07-04 12:31:34 +020092 return _PyObject_CallOneArg((PyObject *)&PyByteArray_Type, input);
Christian Heimes2c9c7a52008-05-26 13:42:13 +000093}
94
Serhiy Storchakaa2314282017-10-29 02:11:54 +030095static PyObject *
96_PyByteArray_FromBufferObject(PyObject *obj)
97{
98 PyObject *result;
99 Py_buffer view;
100
101 if (PyObject_GetBuffer(obj, &view, PyBUF_FULL_RO) < 0) {
102 return NULL;
103 }
104 result = PyByteArray_FromStringAndSize(NULL, view.len);
105 if (result != NULL &&
106 PyBuffer_ToContiguous(PyByteArray_AS_STRING(result),
107 &view, view.len, 'C') < 0)
108 {
109 Py_CLEAR(result);
110 }
111 PyBuffer_Release(&view);
112 return result;
113}
114
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000115PyObject *
116PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
117{
118 PyByteArrayObject *new;
119 Py_ssize_t alloc;
120
121 if (size < 0) {
122 PyErr_SetString(PyExc_SystemError,
123 "Negative size passed to PyByteArray_FromStringAndSize");
124 return NULL;
125 }
126
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000127 /* Prevent buffer overflow when setting alloc to size+1. */
128 if (size == PY_SSIZE_T_MAX) {
129 return PyErr_NoMemory();
130 }
131
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000132 new = PyObject_New(PyByteArrayObject, &PyByteArray_Type);
133 if (new == NULL)
134 return NULL;
135
136 if (size == 0) {
137 new->ob_bytes = NULL;
138 alloc = 0;
139 }
140 else {
141 alloc = size + 1;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100142 new->ob_bytes = PyObject_Malloc(alloc);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000143 if (new->ob_bytes == NULL) {
144 Py_DECREF(new);
145 return PyErr_NoMemory();
146 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +0000147 if (bytes != NULL && size > 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000148 memcpy(new->ob_bytes, bytes, size);
149 new->ob_bytes[size] = '\0'; /* Trailing null byte */
150 }
151 Py_SIZE(new) = size;
152 new->ob_alloc = alloc;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200153 new->ob_start = new->ob_bytes;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000154 new->ob_exports = 0;
155
156 return (PyObject *)new;
157}
158
159Py_ssize_t
160PyByteArray_Size(PyObject *self)
161{
162 assert(self != NULL);
163 assert(PyByteArray_Check(self));
164
165 return PyByteArray_GET_SIZE(self);
166}
167
168char *
169PyByteArray_AsString(PyObject *self)
170{
171 assert(self != NULL);
172 assert(PyByteArray_Check(self));
173
174 return PyByteArray_AS_STRING(self);
175}
176
177int
Antoine Pitroucc231542014-11-02 18:40:09 +0100178PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000179{
180 void *sval;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200181 PyByteArrayObject *obj = ((PyByteArrayObject *)self);
Antoine Pitroucc231542014-11-02 18:40:09 +0100182 /* All computations are done unsigned to avoid integer overflows
183 (see issue #22335). */
184 size_t alloc = (size_t) obj->ob_alloc;
185 size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes);
186 size_t size = (size_t) requested_size;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000187
188 assert(self != NULL);
189 assert(PyByteArray_Check(self));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200190 assert(logical_offset <= alloc);
Antoine Pitroucc231542014-11-02 18:40:09 +0100191 assert(requested_size >= 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000192
Antoine Pitroucc231542014-11-02 18:40:09 +0100193 if (requested_size == Py_SIZE(self)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000194 return 0;
195 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200196 if (!_canresize(obj)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000197 return -1;
198 }
199
Antoine Pitrou25454112015-05-19 20:52:27 +0200200 if (size + logical_offset + 1 <= alloc) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200201 /* Current buffer is large enough to host the requested size,
202 decide on a strategy. */
203 if (size < alloc / 2) {
204 /* Major downsize; resize down to exact size */
205 alloc = size + 1;
206 }
207 else {
208 /* Minor downsize; quick exit */
209 Py_SIZE(self) = size;
210 PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */
211 return 0;
212 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000213 }
214 else {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200215 /* Need growing, decide on a strategy */
216 if (size <= alloc * 1.125) {
217 /* Moderate upsize; overallocate similar to list_resize() */
218 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
219 }
220 else {
221 /* Major upsize; resize up to exact size */
222 alloc = size + 1;
223 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000224 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100225 if (alloc > PY_SSIZE_T_MAX) {
226 PyErr_NoMemory();
227 return -1;
228 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000229
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200230 if (logical_offset > 0) {
231 sval = PyObject_Malloc(alloc);
232 if (sval == NULL) {
233 PyErr_NoMemory();
234 return -1;
235 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100236 memcpy(sval, PyByteArray_AS_STRING(self),
Stefan Krahdce65022017-08-25 20:12:05 +0200237 Py_MIN((size_t)requested_size, (size_t)Py_SIZE(self)));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200238 PyObject_Free(obj->ob_bytes);
239 }
240 else {
241 sval = PyObject_Realloc(obj->ob_bytes, alloc);
242 if (sval == NULL) {
243 PyErr_NoMemory();
244 return -1;
245 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000246 }
247
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200248 obj->ob_bytes = obj->ob_start = sval;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000249 Py_SIZE(self) = size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200250 obj->ob_alloc = alloc;
251 obj->ob_bytes[size] = '\0'; /* Trailing null byte */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000252
253 return 0;
254}
255
256PyObject *
257PyByteArray_Concat(PyObject *a, PyObject *b)
258{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000259 Py_buffer va, vb;
260 PyByteArrayObject *result = NULL;
261
262 va.len = -1;
263 vb.len = -1;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200264 if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 ||
265 PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000266 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
Serhiy Storchaka6b5a9ec2017-03-19 19:47:02 +0200267 Py_TYPE(b)->tp_name, Py_TYPE(a)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000268 goto done;
269 }
270
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300271 if (va.len > PY_SSIZE_T_MAX - vb.len) {
272 PyErr_NoMemory();
273 goto done;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000274 }
275
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300276 result = (PyByteArrayObject *) \
277 PyByteArray_FromStringAndSize(NULL, va.len + vb.len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000278 if (result != NULL) {
279 memcpy(result->ob_bytes, va.buf, va.len);
280 memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
281 }
282
283 done:
284 if (va.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000285 PyBuffer_Release(&va);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000286 if (vb.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000287 PyBuffer_Release(&vb);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000288 return (PyObject *)result;
289}
290
291/* Functions stuffed into the type object */
292
293static Py_ssize_t
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000294bytearray_length(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000295{
296 return Py_SIZE(self);
297}
298
299static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000300bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000301{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000302 Py_ssize_t size;
303 Py_buffer vo;
304
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200305 if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000306 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
307 Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name);
308 return NULL;
309 }
310
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300311 size = Py_SIZE(self);
312 if (size > PY_SSIZE_T_MAX - vo.len) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000313 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000314 return PyErr_NoMemory();
315 }
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300316 if (PyByteArray_Resize((PyObject *)self, size + vo.len) < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000317 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000318 return NULL;
319 }
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300320 memcpy(PyByteArray_AS_STRING(self) + size, vo.buf, vo.len);
Martin v. Löwis423be952008-08-13 15:53:07 +0000321 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000322 Py_INCREF(self);
323 return (PyObject *)self;
324}
325
326static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000327bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000328{
329 PyByteArrayObject *result;
330 Py_ssize_t mysize;
331 Py_ssize_t size;
332
333 if (count < 0)
334 count = 0;
335 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000336 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000337 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000338 size = mysize * count;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000339 result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size);
340 if (result != NULL && size != 0) {
341 if (mysize == 1)
342 memset(result->ob_bytes, self->ob_bytes[0], size);
343 else {
344 Py_ssize_t i;
345 for (i = 0; i < count; i++)
346 memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize);
347 }
348 }
349 return (PyObject *)result;
350}
351
352static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000353bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000354{
355 Py_ssize_t mysize;
356 Py_ssize_t size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200357 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000358
359 if (count < 0)
360 count = 0;
361 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000362 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000363 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000364 size = mysize * count;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200365 if (PyByteArray_Resize((PyObject *)self, size) < 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000366 return NULL;
367
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200368 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000369 if (mysize == 1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200370 memset(buf, buf[0], size);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000371 else {
372 Py_ssize_t i;
373 for (i = 1; i < count; i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200374 memcpy(buf + i*mysize, buf, mysize);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000375 }
376
377 Py_INCREF(self);
378 return (PyObject *)self;
379}
380
381static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000382bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000383{
384 if (i < 0)
385 i += Py_SIZE(self);
386 if (i < 0 || i >= Py_SIZE(self)) {
387 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
388 return NULL;
389 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200390 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000391}
392
393static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000394bytearray_subscript(PyByteArrayObject *self, PyObject *index)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000395{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000396 if (PyIndex_Check(index)) {
397 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000398
399 if (i == -1 && PyErr_Occurred())
400 return NULL;
401
402 if (i < 0)
403 i += PyByteArray_GET_SIZE(self);
404
405 if (i < 0 || i >= Py_SIZE(self)) {
406 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
407 return NULL;
408 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200409 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000410 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000411 else if (PySlice_Check(index)) {
Zackery Spytz14514d92019-05-17 01:13:03 -0600412 Py_ssize_t start, stop, step, slicelength, i;
413 size_t cur;
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300414 if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000415 return NULL;
416 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300417 slicelength = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self),
418 &start, &stop, step);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000419
420 if (slicelength <= 0)
421 return PyByteArray_FromStringAndSize("", 0);
422 else if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200423 return PyByteArray_FromStringAndSize(
424 PyByteArray_AS_STRING(self) + start, slicelength);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000425 }
426 else {
427 char *source_buf = PyByteArray_AS_STRING(self);
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000428 char *result_buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000429 PyObject *result;
430
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000431 result = PyByteArray_FromStringAndSize(NULL, slicelength);
432 if (result == NULL)
433 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000434
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000435 result_buf = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000436 for (cur = start, i = 0; i < slicelength;
437 cur += step, i++) {
438 result_buf[i] = source_buf[cur];
439 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000440 return result;
441 }
442 }
443 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400444 PyErr_Format(PyExc_TypeError,
445 "bytearray indices must be integers or slices, not %.200s",
446 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000447 return NULL;
448 }
449}
450
451static int
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200452bytearray_setslice_linear(PyByteArrayObject *self,
453 Py_ssize_t lo, Py_ssize_t hi,
454 char *bytes, Py_ssize_t bytes_len)
455{
456 Py_ssize_t avail = hi - lo;
457 char *buf = PyByteArray_AS_STRING(self);
458 Py_ssize_t growth = bytes_len - avail;
Victor Stinner84557232013-11-21 12:29:51 +0100459 int res = 0;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200460 assert(avail >= 0);
461
Victor Stinner84557232013-11-21 12:29:51 +0100462 if (growth < 0) {
463 if (!_canresize(self))
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200464 return -1;
Victor Stinner84557232013-11-21 12:29:51 +0100465
466 if (lo == 0) {
467 /* Shrink the buffer by advancing its logical start */
468 self->ob_start -= growth;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200469 /*
Victor Stinner84557232013-11-21 12:29:51 +0100470 0 lo hi old_size
471 | |<----avail----->|<-----tail------>|
472 | |<-bytes_len->|<-----tail------>|
473 0 new_lo new_hi new_size
474 */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200475 }
Victor Stinner84557232013-11-21 12:29:51 +0100476 else {
477 /*
478 0 lo hi old_size
479 | |<----avail----->|<-----tomove------>|
480 | |<-bytes_len->|<-----tomove------>|
481 0 lo new_hi new_size
482 */
483 memmove(buf + lo + bytes_len, buf + hi,
484 Py_SIZE(self) - hi);
485 }
486 if (PyByteArray_Resize((PyObject *)self,
487 Py_SIZE(self) + growth) < 0) {
488 /* Issue #19578: Handling the memory allocation failure here is
489 tricky here because the bytearray object has already been
490 modified. Depending on growth and lo, the behaviour is
491 different.
492
493 If growth < 0 and lo != 0, the operation is completed, but a
494 MemoryError is still raised and the memory block is not
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700495 shrunk. Otherwise, the bytearray is restored in its previous
Victor Stinner84557232013-11-21 12:29:51 +0100496 state and a MemoryError is raised. */
497 if (lo == 0) {
498 self->ob_start += growth;
499 return -1;
500 }
501 /* memmove() removed bytes, the bytearray object cannot be
502 restored in its previous state. */
503 Py_SIZE(self) += growth;
504 res = -1;
505 }
506 buf = PyByteArray_AS_STRING(self);
507 }
508 else if (growth > 0) {
509 if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) {
510 PyErr_NoMemory();
511 return -1;
512 }
513
514 if (PyByteArray_Resize((PyObject *)self,
515 Py_SIZE(self) + growth) < 0) {
516 return -1;
517 }
518 buf = PyByteArray_AS_STRING(self);
519 /* Make the place for the additional bytes */
520 /*
521 0 lo hi old_size
522 | |<-avail->|<-----tomove------>|
523 | |<---bytes_len-->|<-----tomove------>|
524 0 lo new_hi new_size
525 */
526 memmove(buf + lo + bytes_len, buf + hi,
527 Py_SIZE(self) - lo - bytes_len);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200528 }
529
530 if (bytes_len > 0)
531 memcpy(buf + lo, bytes, bytes_len);
Victor Stinner84557232013-11-21 12:29:51 +0100532 return res;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200533}
534
535static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000536bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000537 PyObject *values)
538{
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200539 Py_ssize_t needed;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000540 void *bytes;
541 Py_buffer vbytes;
542 int res = 0;
543
544 vbytes.len = -1;
545 if (values == (PyObject *)self) {
546 /* Make a copy and call this function recursively */
547 int err;
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300548 values = PyByteArray_FromStringAndSize(PyByteArray_AS_STRING(values),
549 PyByteArray_GET_SIZE(values));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000550 if (values == NULL)
551 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000552 err = bytearray_setslice(self, lo, hi, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000553 Py_DECREF(values);
554 return err;
555 }
556 if (values == NULL) {
557 /* del b[lo:hi] */
558 bytes = NULL;
559 needed = 0;
560 }
561 else {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200562 if (PyObject_GetBuffer(values, &vbytes, PyBUF_SIMPLE) != 0) {
563 PyErr_Format(PyExc_TypeError,
564 "can't set bytearray slice from %.100s",
565 Py_TYPE(values)->tp_name);
566 return -1;
567 }
568 needed = vbytes.len;
569 bytes = vbytes.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000570 }
571
572 if (lo < 0)
573 lo = 0;
574 if (hi < lo)
575 hi = lo;
576 if (hi > Py_SIZE(self))
577 hi = Py_SIZE(self);
578
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200579 res = bytearray_setslice_linear(self, lo, hi, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000580 if (vbytes.len != -1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200581 PyBuffer_Release(&vbytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000582 return res;
583}
584
585static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000586bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000587{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000588 int ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000589
590 if (i < 0)
591 i += Py_SIZE(self);
592
593 if (i < 0 || i >= Py_SIZE(self)) {
594 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
595 return -1;
596 }
597
598 if (value == NULL)
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000599 return bytearray_setslice(self, i, i+1, NULL);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000600
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000601 if (!_getbytevalue(value, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000602 return -1;
603
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200604 PyByteArray_AS_STRING(self)[i] = ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000605 return 0;
606}
607
608static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000609bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000610{
611 Py_ssize_t start, stop, step, slicelen, needed;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200612 char *buf, *bytes;
613 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000614
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000615 if (PyIndex_Check(index)) {
616 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000617
618 if (i == -1 && PyErr_Occurred())
619 return -1;
620
621 if (i < 0)
622 i += PyByteArray_GET_SIZE(self);
623
624 if (i < 0 || i >= Py_SIZE(self)) {
625 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
626 return -1;
627 }
628
629 if (values == NULL) {
630 /* Fall through to slice assignment */
631 start = i;
632 stop = i + 1;
633 step = 1;
634 slicelen = 1;
635 }
636 else {
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000637 int ival;
638 if (!_getbytevalue(values, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000639 return -1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200640 buf[i] = (char)ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000641 return 0;
642 }
643 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000644 else if (PySlice_Check(index)) {
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300645 if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000646 return -1;
647 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300648 slicelen = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self), &start,
649 &stop, step);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000650 }
651 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400652 PyErr_Format(PyExc_TypeError,
653 "bytearray indices must be integers or slices, not %.200s",
654 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000655 return -1;
656 }
657
658 if (values == NULL) {
659 bytes = NULL;
660 needed = 0;
661 }
662 else if (values == (PyObject *)self || !PyByteArray_Check(values)) {
Christian Heimes6d26ade2012-11-03 23:07:59 +0100663 int err;
Ezio Melottic64bcbe2012-11-03 21:19:06 +0200664 if (PyNumber_Check(values) || PyUnicode_Check(values)) {
665 PyErr_SetString(PyExc_TypeError,
666 "can assign only bytes, buffers, or iterables "
667 "of ints in range(0, 256)");
668 return -1;
669 }
Georg Brandlf3fa5682010-12-04 17:09:30 +0000670 /* Make a copy and call this function recursively */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000671 values = PyByteArray_FromObject(values);
672 if (values == NULL)
673 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000674 err = bytearray_ass_subscript(self, index, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000675 Py_DECREF(values);
676 return err;
677 }
678 else {
679 assert(PyByteArray_Check(values));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200680 bytes = PyByteArray_AS_STRING(values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000681 needed = Py_SIZE(values);
682 }
683 /* Make sure b[5:2] = ... inserts before 5, not before 2. */
684 if ((step < 0 && start < stop) ||
685 (step > 0 && start > stop))
686 stop = start;
687 if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200688 return bytearray_setslice_linear(self, start, stop, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000689 }
690 else {
691 if (needed == 0) {
692 /* Delete slice */
Mark Dickinsonbc099642010-01-29 17:27:24 +0000693 size_t cur;
694 Py_ssize_t i;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000695
Antoine Pitrou5504e892008-12-06 21:27:53 +0000696 if (!_canresize(self))
697 return -1;
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000698
699 if (slicelen == 0)
700 /* Nothing to do here. */
701 return 0;
702
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000703 if (step < 0) {
704 stop = start + 1;
705 start = stop + step * (slicelen - 1) - 1;
706 step = -step;
707 }
708 for (cur = start, i = 0;
709 i < slicelen; cur += step, i++) {
710 Py_ssize_t lim = step - 1;
711
Mark Dickinson66f575b2010-02-14 12:53:32 +0000712 if (cur + step >= (size_t)PyByteArray_GET_SIZE(self))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000713 lim = PyByteArray_GET_SIZE(self) - cur - 1;
714
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200715 memmove(buf + cur - i,
716 buf + cur + 1, lim);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000717 }
718 /* Move the tail of the bytes, in one chunk */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000719 cur = start + (size_t)slicelen*step;
Mark Dickinson66f575b2010-02-14 12:53:32 +0000720 if (cur < (size_t)PyByteArray_GET_SIZE(self)) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200721 memmove(buf + cur - slicelen,
722 buf + cur,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000723 PyByteArray_GET_SIZE(self) - cur);
724 }
725 if (PyByteArray_Resize((PyObject *)self,
726 PyByteArray_GET_SIZE(self) - slicelen) < 0)
727 return -1;
728
729 return 0;
730 }
731 else {
732 /* Assign slice */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000733 Py_ssize_t i;
734 size_t cur;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000735
736 if (needed != slicelen) {
737 PyErr_Format(PyExc_ValueError,
738 "attempt to assign bytes of size %zd "
739 "to extended slice of size %zd",
740 needed, slicelen);
741 return -1;
742 }
743 for (cur = start, i = 0; i < slicelen; cur += step, i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200744 buf[cur] = bytes[i];
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000745 return 0;
746 }
747 }
748}
749
750static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000751bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000752{
753 static char *kwlist[] = {"source", "encoding", "errors", 0};
754 PyObject *arg = NULL;
755 const char *encoding = NULL;
756 const char *errors = NULL;
757 Py_ssize_t count;
758 PyObject *it;
759 PyObject *(*iternext)(PyObject *);
760
761 if (Py_SIZE(self) != 0) {
762 /* Empty previous contents (yes, do this first of all!) */
763 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
764 return -1;
765 }
766
767 /* Parse arguments */
Georg Brandl3dbca812008-07-23 16:10:53 +0000768 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000769 &arg, &encoding, &errors))
770 return -1;
771
772 /* Make a quick exit if no first argument */
773 if (arg == NULL) {
774 if (encoding != NULL || errors != NULL) {
775 PyErr_SetString(PyExc_TypeError,
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300776 encoding != NULL ?
777 "encoding without a string argument" :
778 "errors without a string argument");
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000779 return -1;
780 }
781 return 0;
782 }
783
784 if (PyUnicode_Check(arg)) {
785 /* Encode via the codec registry */
786 PyObject *encoded, *new;
787 if (encoding == NULL) {
788 PyErr_SetString(PyExc_TypeError,
789 "string argument without an encoding");
790 return -1;
791 }
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000792 encoded = PyUnicode_AsEncodedString(arg, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000793 if (encoded == NULL)
794 return -1;
795 assert(PyBytes_Check(encoded));
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000796 new = bytearray_iconcat(self, encoded);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000797 Py_DECREF(encoded);
798 if (new == NULL)
799 return -1;
800 Py_DECREF(new);
801 return 0;
802 }
803
804 /* If it's not unicode, there can't be encoding or errors */
805 if (encoding != NULL || errors != NULL) {
806 PyErr_SetString(PyExc_TypeError,
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300807 encoding != NULL ?
808 "encoding without a string argument" :
809 "errors without a string argument");
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000810 return -1;
811 }
812
813 /* Is it an int? */
Serhiy Storchakaeb249882016-08-15 09:46:07 +0300814 if (PyIndex_Check(arg)) {
815 count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
816 if (count == -1 && PyErr_Occurred()) {
Serhiy Storchakae8904212018-10-15 00:02:57 +0300817 if (!PyErr_ExceptionMatches(PyExc_TypeError))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000818 return -1;
INADA Naokia634e232017-01-06 17:32:01 +0900819 PyErr_Clear(); /* fall through */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000820 }
INADA Naokia634e232017-01-06 17:32:01 +0900821 else {
822 if (count < 0) {
823 PyErr_SetString(PyExc_ValueError, "negative count");
824 return -1;
825 }
826 if (count > 0) {
827 if (PyByteArray_Resize((PyObject *)self, count))
828 return -1;
829 memset(PyByteArray_AS_STRING(self), 0, count);
830 }
831 return 0;
832 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000833 }
834
835 /* Use the buffer API */
836 if (PyObject_CheckBuffer(arg)) {
837 Py_ssize_t size;
838 Py_buffer view;
839 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
840 return -1;
841 size = view.len;
842 if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200843 if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self),
844 &view, size, 'C') < 0)
Stefan Krah7d12d9d2012-07-28 12:25:55 +0200845 goto fail;
Martin v. Löwis423be952008-08-13 15:53:07 +0000846 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000847 return 0;
848 fail:
Martin v. Löwis423be952008-08-13 15:53:07 +0000849 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000850 return -1;
851 }
852
853 /* XXX Optimize this if the arguments is a list, tuple */
854
855 /* Get the iterator */
856 it = PyObject_GetIter(arg);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300857 if (it == NULL) {
858 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
859 PyErr_Format(PyExc_TypeError,
860 "cannot convert '%.200s' object to bytearray",
861 arg->ob_type->tp_name);
862 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000863 return -1;
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300864 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000865 iternext = *Py_TYPE(it)->tp_iternext;
866
867 /* Run the iterator to exhaustion */
868 for (;;) {
869 PyObject *item;
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000870 int rc, value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000871
872 /* Get the next item */
873 item = iternext(it);
874 if (item == NULL) {
875 if (PyErr_Occurred()) {
876 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
877 goto error;
878 PyErr_Clear();
879 }
880 break;
881 }
882
883 /* Interpret it as an int (__index__) */
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000884 rc = _getbytevalue(item, &value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000885 Py_DECREF(item);
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000886 if (!rc)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000887 goto error;
888
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000889 /* Append the byte */
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300890 if (Py_SIZE(self) + 1 < self->ob_alloc) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000891 Py_SIZE(self)++;
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300892 PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0';
893 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000894 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
895 goto error;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200896 PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000897 }
898
899 /* Clean up and return success */
900 Py_DECREF(it);
901 return 0;
902
903 error:
904 /* Error handling when it != NULL */
905 Py_DECREF(it);
906 return -1;
907}
908
909/* Mostly copied from string_repr, but without the
910 "smart quote" functionality. */
911static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000912bytearray_repr(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000913{
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300914 const char *className = _PyType_Name(Py_TYPE(self));
915 const char *quote_prefix = "(b";
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000916 const char *quote_postfix = ")";
917 Py_ssize_t length = Py_SIZE(self);
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300918 /* 6 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
919 Py_ssize_t newsize;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000920 PyObject *v;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200921 Py_ssize_t i;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200922 char *bytes;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200923 char c;
924 char *p;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200925 int quote;
926 char *test, *start;
927 char *buffer;
928
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300929 newsize = strlen(className);
930 if (length > (PY_SSIZE_T_MAX - 6 - newsize) / 4) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000931 PyErr_SetString(PyExc_OverflowError,
932 "bytearray object is too large to make repr");
933 return NULL;
934 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200935
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300936 newsize += 6 + length * 4;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100937 buffer = PyObject_Malloc(newsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200938 if (buffer == NULL) {
939 PyErr_NoMemory();
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000940 return NULL;
941 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000942
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200943 /* Figure out which quote to use; single is preferred */
944 quote = '\'';
945 start = PyByteArray_AS_STRING(self);
946 for (test = start; test < start+length; ++test) {
947 if (*test == '"') {
948 quote = '\''; /* back to single */
949 break;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000950 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200951 else if (*test == '\'')
952 quote = '"';
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000953 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200954
955 p = buffer;
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300956 while (*className)
957 *p++ = *className++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200958 while (*quote_prefix)
959 *p++ = *quote_prefix++;
960 *p++ = quote;
961
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200962 bytes = PyByteArray_AS_STRING(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200963 for (i = 0; i < length; i++) {
964 /* There's at least enough room for a hex escape
965 and a closing quote. */
966 assert(newsize - (p - buffer) >= 5);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200967 c = bytes[i];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200968 if (c == '\'' || c == '\\')
969 *p++ = '\\', *p++ = c;
970 else if (c == '\t')
971 *p++ = '\\', *p++ = 't';
972 else if (c == '\n')
973 *p++ = '\\', *p++ = 'n';
974 else if (c == '\r')
975 *p++ = '\\', *p++ = 'r';
976 else if (c == 0)
977 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
978 else if (c < ' ' || c >= 0x7f) {
979 *p++ = '\\';
980 *p++ = 'x';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200981 *p++ = Py_hexdigits[(c & 0xf0) >> 4];
982 *p++ = Py_hexdigits[c & 0xf];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200983 }
984 else
985 *p++ = c;
986 }
987 assert(newsize - (p - buffer) >= 1);
988 *p++ = quote;
989 while (*quote_postfix) {
990 *p++ = *quote_postfix++;
991 }
992
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300993 v = PyUnicode_FromStringAndSize(buffer, p - buffer);
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100994 PyObject_Free(buffer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200995 return v;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000996}
997
998static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000999bytearray_str(PyObject *op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001000{
Victor Stinner331a6a52019-05-27 16:39:22 +02001001 PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;
Victor Stinnerc96be812019-05-14 17:34:56 +02001002 if (config->bytes_warning) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001003 if (PyErr_WarnEx(PyExc_BytesWarning,
1004 "str() on a bytearray instance", 1)) {
1005 return NULL;
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001006 }
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001007 }
1008 return bytearray_repr((PyByteArrayObject*)op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001009}
1010
1011static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001012bytearray_richcompare(PyObject *self, PyObject *other, int op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001013{
1014 Py_ssize_t self_size, other_size;
1015 Py_buffer self_bytes, other_bytes;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001016 int cmp, rc;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001017
1018 /* Bytes can be compared to anything that supports the (binary)
1019 buffer API. Except that a comparison with Unicode is always an
1020 error, even if the comparison is for equality. */
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001021 rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type);
1022 if (!rc)
1023 rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type);
1024 if (rc < 0)
1025 return NULL;
1026 if (rc) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001027 PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;
Victor Stinnerc96be812019-05-14 17:34:56 +02001028 if (config->bytes_warning && (op == Py_EQ || op == Py_NE)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001029 if (PyErr_WarnEx(PyExc_BytesWarning,
Georg Brandle5d68ac2008-06-04 11:30:26 +00001030 "Comparison between bytearray and string", 1))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001031 return NULL;
1032 }
1033
Brian Curtindfc80e32011-08-10 20:28:54 -05001034 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001035 }
1036
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001037 if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001038 PyErr_Clear();
Brian Curtindfc80e32011-08-10 20:28:54 -05001039 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001040 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001041 self_size = self_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001042
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001043 if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001044 PyErr_Clear();
Martin v. Löwis423be952008-08-13 15:53:07 +00001045 PyBuffer_Release(&self_bytes);
Brian Curtindfc80e32011-08-10 20:28:54 -05001046 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001047 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001048 other_size = other_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001049
1050 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
1051 /* Shortcut: if the lengths differ, the objects differ */
stratakise8b19652017-11-02 11:32:54 +01001052 PyBuffer_Release(&self_bytes);
1053 PyBuffer_Release(&other_bytes);
1054 return PyBool_FromLong((op == Py_NE));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001055 }
1056 else {
stratakise8b19652017-11-02 11:32:54 +01001057 cmp = memcmp(self_bytes.buf, other_bytes.buf,
1058 Py_MIN(self_size, other_size));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001059 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
1060
stratakise8b19652017-11-02 11:32:54 +01001061 PyBuffer_Release(&self_bytes);
1062 PyBuffer_Release(&other_bytes);
1063
1064 if (cmp != 0) {
1065 Py_RETURN_RICHCOMPARE(cmp, 0, op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001066 }
1067
stratakise8b19652017-11-02 11:32:54 +01001068 Py_RETURN_RICHCOMPARE(self_size, other_size, op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001069 }
1070
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001071}
1072
1073static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001074bytearray_dealloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001075{
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001076 if (self->ob_exports > 0) {
1077 PyErr_SetString(PyExc_SystemError,
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001078 "deallocated bytearray object has exported buffers");
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001079 PyErr_Print();
1080 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001081 if (self->ob_bytes != 0) {
Antoine Pitrou39aba4f2011-11-12 21:15:28 +01001082 PyObject_Free(self->ob_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001083 }
1084 Py_TYPE(self)->tp_free((PyObject *)self);
1085}
1086
1087
1088/* -------------------------------------------------------------------- */
1089/* Methods */
1090
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001091#define FASTSEARCH fastsearch
1092#define STRINGLIB(F) stringlib_##F
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001093#define STRINGLIB_CHAR char
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001094#define STRINGLIB_SIZEOF_CHAR 1
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001095#define STRINGLIB_LEN PyByteArray_GET_SIZE
1096#define STRINGLIB_STR PyByteArray_AS_STRING
1097#define STRINGLIB_NEW PyByteArray_FromStringAndSize
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001098#define STRINGLIB_ISSPACE Py_ISSPACE
1099#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001100#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1101#define STRINGLIB_MUTABLE 1
1102
1103#include "stringlib/fastsearch.h"
1104#include "stringlib/count.h"
1105#include "stringlib/find.h"
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001106#include "stringlib/join.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001107#include "stringlib/partition.h"
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001108#include "stringlib/split.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001109#include "stringlib/ctype.h"
1110#include "stringlib/transmogrify.h"
1111
1112
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001113static PyObject *
1114bytearray_find(PyByteArrayObject *self, PyObject *args)
1115{
1116 return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1117}
1118
1119static PyObject *
1120bytearray_count(PyByteArrayObject *self, PyObject *args)
1121{
1122 return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1123}
1124
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001125/*[clinic input]
1126bytearray.clear
1127
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001128Remove all items from the bytearray.
1129[clinic start generated code]*/
1130
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001131static PyObject *
1132bytearray_clear_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001133/*[clinic end generated code: output=85c2fe6aede0956c input=ed6edae9de447ac4]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001134{
1135 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1136 return NULL;
1137 Py_RETURN_NONE;
1138}
1139
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001140/*[clinic input]
1141bytearray.copy
1142
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001143Return a copy of B.
1144[clinic start generated code]*/
1145
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001146static PyObject *
1147bytearray_copy_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001148/*[clinic end generated code: output=68cfbcfed484c132 input=6597b0c01bccaa9e]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001149{
1150 return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self),
1151 PyByteArray_GET_SIZE(self));
1152}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001153
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001154static PyObject *
1155bytearray_index(PyByteArrayObject *self, PyObject *args)
1156{
1157 return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1158}
1159
1160static PyObject *
1161bytearray_rfind(PyByteArrayObject *self, PyObject *args)
1162{
1163 return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1164}
1165
1166static PyObject *
1167bytearray_rindex(PyByteArrayObject *self, PyObject *args)
1168{
1169 return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1170}
1171
1172static int
1173bytearray_contains(PyObject *self, PyObject *arg)
1174{
1175 return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg);
1176}
1177
1178static PyObject *
1179bytearray_startswith(PyByteArrayObject *self, PyObject *args)
1180{
1181 return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1182}
1183
1184static PyObject *
1185bytearray_endswith(PyByteArrayObject *self, PyObject *args)
1186{
1187 return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1188}
1189
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001190
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001191/*[clinic input]
1192bytearray.translate
1193
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001194 table: object
1195 Translation table, which must be a bytes object of length 256.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001196 /
Martin Panter1b6c6da2016-08-27 08:35:02 +00001197 delete as deletechars: object(c_default="NULL") = b''
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001198
1199Return a copy with each character mapped by the given translation table.
1200
Martin Panter1b6c6da2016-08-27 08:35:02 +00001201All characters occurring in the optional argument delete are removed.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001202The remaining characters are mapped through the given translation table.
1203[clinic start generated code]*/
1204
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001205static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001206bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
Martin Panter1b6c6da2016-08-27 08:35:02 +00001207 PyObject *deletechars)
1208/*[clinic end generated code: output=b6a8f01c2a74e446 input=cfff956d4d127a9b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001209{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001210 char *input, *output;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001211 const char *table_chars;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001212 Py_ssize_t i, c;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001213 PyObject *input_obj = (PyObject*)self;
1214 const char *output_start;
1215 Py_ssize_t inlen;
Georg Brandlccc47b62008-12-28 11:44:14 +00001216 PyObject *result = NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001217 int trans_table[256];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001218 Py_buffer vtable, vdel;
1219
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001220 if (table == Py_None) {
1221 table_chars = NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001222 table = NULL;
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001223 } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001224 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001225 } else {
1226 if (vtable.len != 256) {
1227 PyErr_SetString(PyExc_ValueError,
1228 "translation table must be 256 characters long");
Georg Brandl953152f2009-07-22 12:03:59 +00001229 PyBuffer_Release(&vtable);
1230 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001231 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001232 table_chars = (const char*)vtable.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001233 }
1234
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001235 if (deletechars != NULL) {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001236 if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) {
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001237 if (table != NULL)
Georg Brandl953152f2009-07-22 12:03:59 +00001238 PyBuffer_Release(&vtable);
1239 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001240 }
1241 }
1242 else {
1243 vdel.buf = NULL;
1244 vdel.len = 0;
1245 }
1246
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001247 inlen = PyByteArray_GET_SIZE(input_obj);
1248 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1249 if (result == NULL)
1250 goto done;
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03001251 output_start = output = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001252 input = PyByteArray_AS_STRING(input_obj);
1253
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001254 if (vdel.len == 0 && table_chars != NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001255 /* If no deletions are required, use faster code */
1256 for (i = inlen; --i >= 0; ) {
1257 c = Py_CHARMASK(*input++);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001258 *output++ = table_chars[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001259 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001260 goto done;
1261 }
Georg Brandlccc47b62008-12-28 11:44:14 +00001262
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001263 if (table_chars == NULL) {
Georg Brandlccc47b62008-12-28 11:44:14 +00001264 for (i = 0; i < 256; i++)
1265 trans_table[i] = Py_CHARMASK(i);
1266 } else {
1267 for (i = 0; i < 256; i++)
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001268 trans_table[i] = Py_CHARMASK(table_chars[i]);
Georg Brandlccc47b62008-12-28 11:44:14 +00001269 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001270
1271 for (i = 0; i < vdel.len; i++)
1272 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1273
1274 for (i = inlen; --i >= 0; ) {
1275 c = Py_CHARMASK(*input++);
1276 if (trans_table[c] != -1)
Martin Panter1b6c6da2016-08-27 08:35:02 +00001277 *output++ = (char)trans_table[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001278 }
1279 /* Fix the size of the resulting string */
1280 if (inlen > 0)
Christian Heimesc731bbe2013-07-21 02:04:35 +02001281 if (PyByteArray_Resize(result, output - output_start) < 0) {
1282 Py_CLEAR(result);
1283 goto done;
1284 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001285
1286done:
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001287 if (table != NULL)
Georg Brandlccc47b62008-12-28 11:44:14 +00001288 PyBuffer_Release(&vtable);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001289 if (deletechars != NULL)
Martin v. Löwis423be952008-08-13 15:53:07 +00001290 PyBuffer_Release(&vdel);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001291 return result;
1292}
1293
1294
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001295/*[clinic input]
1296
1297@staticmethod
1298bytearray.maketrans
1299
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001300 frm: Py_buffer
1301 to: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001302 /
1303
1304Return a translation table useable for the bytes or bytearray translate method.
1305
1306The returned table will be one where each byte in frm is mapped to the byte at
1307the same position in to.
1308
1309The bytes objects frm and to must be of the same length.
1310[clinic start generated code]*/
1311
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001312static PyObject *
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001313bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001314/*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001315{
1316 return _Py_bytes_maketrans(frm, to);
Georg Brandlabc38772009-04-12 15:51:51 +00001317}
1318
1319
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001320/*[clinic input]
1321bytearray.replace
1322
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001323 old: Py_buffer
1324 new: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001325 count: Py_ssize_t = -1
1326 Maximum number of occurrences to replace.
1327 -1 (the default value) means replace all occurrences.
1328 /
1329
1330Return a copy with all occurrences of substring old replaced by new.
1331
1332If the optional argument count is given, only the first count occurrences are
1333replaced.
1334[clinic start generated code]*/
1335
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001336static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001337bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
1338 Py_buffer *new, Py_ssize_t count)
1339/*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001340{
Serhiy Storchakafb81d3c2016-05-05 09:26:07 +03001341 return stringlib_replace((PyObject *)self,
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001342 (const char *)old->buf, old->len,
1343 (const char *)new->buf, new->len, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001344}
1345
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001346/*[clinic input]
1347bytearray.split
1348
1349 sep: object = None
1350 The delimiter according which to split the bytearray.
1351 None (the default value) means split on ASCII whitespace characters
1352 (space, tab, return, newline, formfeed, vertical tab).
1353 maxsplit: Py_ssize_t = -1
1354 Maximum number of splits to do.
1355 -1 (the default value) means no limit.
1356
1357Return a list of the sections in the bytearray, using sep as the delimiter.
1358[clinic start generated code]*/
1359
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001360static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001361bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
1362 Py_ssize_t maxsplit)
1363/*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001364{
1365 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001366 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001367 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001368 Py_buffer vsub;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001369
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001370 if (maxsplit < 0)
1371 maxsplit = PY_SSIZE_T_MAX;
1372
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001373 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001374 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001375
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001376 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001377 return NULL;
1378 sub = vsub.buf;
1379 n = vsub.len;
1380
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001381 list = stringlib_split(
1382 (PyObject*) self, s, len, sub, n, maxsplit
1383 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001384 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001385 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001386}
1387
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001388/*[clinic input]
1389bytearray.partition
1390
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001391 sep: object
1392 /
1393
1394Partition the bytearray into three parts using the given separator.
1395
1396This will search for the separator sep in the bytearray. If the separator is
1397found, returns a 3-tuple containing the part before the separator, the
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001398separator itself, and the part after it as new bytearray objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001399
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001400If the separator is not found, returns a 3-tuple containing the copy of the
1401original bytearray object and two empty bytearray objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001402[clinic start generated code]*/
1403
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001404static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001405bytearray_partition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001406/*[clinic end generated code: output=45d2525ddd35f957 input=8f644749ee4fc83a]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001407{
1408 PyObject *bytesep, *result;
1409
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001410 bytesep = _PyByteArray_FromBufferObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001411 if (! bytesep)
1412 return NULL;
1413
1414 result = stringlib_partition(
1415 (PyObject*) self,
1416 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1417 bytesep,
1418 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1419 );
1420
1421 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001422 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001423}
1424
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001425/*[clinic input]
1426bytearray.rpartition
1427
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001428 sep: object
1429 /
1430
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001431Partition the bytearray into three parts using the given separator.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001432
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001433This will search for the separator sep in the bytearray, starting at the end.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001434If the separator is found, returns a 3-tuple containing the part before the
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001435separator, the separator itself, and the part after it as new bytearray
1436objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001437
1438If the separator is not found, returns a 3-tuple containing two empty bytearray
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001439objects and the copy of the original bytearray object.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001440[clinic start generated code]*/
1441
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001442static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001443bytearray_rpartition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001444/*[clinic end generated code: output=440de3c9426115e8 input=7e3df3e6cb8fa0ac]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001445{
1446 PyObject *bytesep, *result;
1447
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001448 bytesep = _PyByteArray_FromBufferObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001449 if (! bytesep)
1450 return NULL;
1451
1452 result = stringlib_rpartition(
1453 (PyObject*) self,
1454 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1455 bytesep,
1456 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1457 );
1458
1459 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001460 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001461}
1462
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001463/*[clinic input]
1464bytearray.rsplit = bytearray.split
1465
1466Return a list of the sections in the bytearray, using sep as the delimiter.
1467
1468Splitting is done starting at the end of the bytearray and working to the front.
1469[clinic start generated code]*/
1470
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001471static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001472bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
1473 Py_ssize_t maxsplit)
1474/*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001475{
1476 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001477 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001478 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001479 Py_buffer vsub;
1480
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001481 if (maxsplit < 0)
1482 maxsplit = PY_SSIZE_T_MAX;
1483
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001484 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001485 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001486
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001487 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001488 return NULL;
1489 sub = vsub.buf;
1490 n = vsub.len;
1491
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001492 list = stringlib_rsplit(
1493 (PyObject*) self, s, len, sub, n, maxsplit
1494 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001495 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001496 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001497}
1498
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001499/*[clinic input]
1500bytearray.reverse
1501
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001502Reverse the order of the values in B in place.
1503[clinic start generated code]*/
1504
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001505static PyObject *
1506bytearray_reverse_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001507/*[clinic end generated code: output=9f7616f29ab309d3 input=543356319fc78557]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001508{
1509 char swap, *head, *tail;
1510 Py_ssize_t i, j, n = Py_SIZE(self);
1511
1512 j = n / 2;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001513 head = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001514 tail = head + n - 1;
1515 for (i = 0; i < j; i++) {
1516 swap = *head;
1517 *head++ = *tail;
1518 *tail-- = swap;
1519 }
1520
1521 Py_RETURN_NONE;
1522}
1523
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001524
1525/*[python input]
1526class bytesvalue_converter(CConverter):
1527 type = 'int'
1528 converter = '_getbytevalue'
1529[python start generated code]*/
1530/*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/
1531
1532
1533/*[clinic input]
1534bytearray.insert
1535
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001536 index: Py_ssize_t
1537 The index where the value is to be inserted.
1538 item: bytesvalue
1539 The item to be inserted.
1540 /
1541
1542Insert a single item into the bytearray before the given index.
1543[clinic start generated code]*/
1544
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001545static PyObject *
1546bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001547/*[clinic end generated code: output=76c775a70e7b07b7 input=b2b5d07e9de6c070]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001548{
1549 Py_ssize_t n = Py_SIZE(self);
1550 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001551
1552 if (n == PY_SSIZE_T_MAX) {
1553 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001554 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001555 return NULL;
1556 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001557 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1558 return NULL;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001559 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001560
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001561 if (index < 0) {
1562 index += n;
1563 if (index < 0)
1564 index = 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001565 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001566 if (index > n)
1567 index = n;
1568 memmove(buf + index + 1, buf + index, n - index);
1569 buf[index] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001570
1571 Py_RETURN_NONE;
1572}
1573
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001574/*[clinic input]
1575bytearray.append
1576
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001577 item: bytesvalue
1578 The item to be appended.
1579 /
1580
1581Append a single item to the end of the bytearray.
1582[clinic start generated code]*/
1583
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001584static PyObject *
1585bytearray_append_impl(PyByteArrayObject *self, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001586/*[clinic end generated code: output=a154e19ed1886cb6 input=20d6bec3d1340593]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001587{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001588 Py_ssize_t n = Py_SIZE(self);
1589
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001590 if (n == PY_SSIZE_T_MAX) {
1591 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001592 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001593 return NULL;
1594 }
1595 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1596 return NULL;
1597
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001598 PyByteArray_AS_STRING(self)[n] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001599
1600 Py_RETURN_NONE;
1601}
1602
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001603/*[clinic input]
1604bytearray.extend
1605
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001606 iterable_of_ints: object
1607 The iterable of items to append.
1608 /
1609
1610Append all the items from the iterator or sequence to the end of the bytearray.
1611[clinic start generated code]*/
1612
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001613static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001614bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001615/*[clinic end generated code: output=98155dbe249170b1 input=c617b3a93249ba28]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001616{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001617 PyObject *it, *item, *bytearray_obj;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001618 Py_ssize_t buf_size = 0, len = 0;
1619 int value;
1620 char *buf;
1621
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001622 /* bytearray_setslice code only accepts something supporting PEP 3118. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001623 if (PyObject_CheckBuffer(iterable_of_ints)) {
1624 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_ints) == -1)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001625 return NULL;
1626
1627 Py_RETURN_NONE;
1628 }
1629
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001630 it = PyObject_GetIter(iterable_of_ints);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +03001631 if (it == NULL) {
1632 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
1633 PyErr_Format(PyExc_TypeError,
1634 "can't extend bytearray with %.100s",
1635 iterable_of_ints->ob_type->tp_name);
1636 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001637 return NULL;
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +03001638 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001639
Ezio Melotti42da6632011-03-15 05:18:48 +02001640 /* Try to determine the length of the argument. 32 is arbitrary. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001641 buf_size = PyObject_LengthHint(iterable_of_ints, 32);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001642 if (buf_size == -1) {
1643 Py_DECREF(it);
1644 return NULL;
1645 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001646
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001647 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001648 if (bytearray_obj == NULL) {
1649 Py_DECREF(it);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001650 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001651 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001652 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001653
1654 while ((item = PyIter_Next(it)) != NULL) {
1655 if (! _getbytevalue(item, &value)) {
1656 Py_DECREF(item);
1657 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001658 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001659 return NULL;
1660 }
1661 buf[len++] = value;
1662 Py_DECREF(item);
1663
1664 if (len >= buf_size) {
Martin Panter371731e2016-07-18 07:53:13 +00001665 Py_ssize_t addition;
1666 if (len == PY_SSIZE_T_MAX) {
1667 Py_DECREF(it);
1668 Py_DECREF(bytearray_obj);
1669 return PyErr_NoMemory();
1670 }
1671 addition = len >> 1;
1672 if (addition > PY_SSIZE_T_MAX - len - 1)
1673 buf_size = PY_SSIZE_T_MAX;
1674 else
1675 buf_size = len + addition + 1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001676 if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001677 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001678 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001679 return NULL;
1680 }
1681 /* Recompute the `buf' pointer, since the resizing operation may
1682 have invalidated it. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001683 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001684 }
1685 }
1686 Py_DECREF(it);
1687
1688 /* Resize down to exact size. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001689 if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) {
1690 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001691 return NULL;
1692 }
1693
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001694 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) {
1695 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001696 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001697 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001698 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001699
Brandt Bucher2a7d5962019-06-26 12:06:18 -07001700 if (PyErr_Occurred()) {
1701 return NULL;
1702 }
1703
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001704 Py_RETURN_NONE;
1705}
1706
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001707/*[clinic input]
1708bytearray.pop
1709
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001710 index: Py_ssize_t = -1
1711 The index from where to remove the item.
1712 -1 (the default value) means remove the last item.
1713 /
1714
1715Remove and return a single item from B.
1716
1717If no index argument is given, will pop the last item.
1718[clinic start generated code]*/
1719
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001720static PyObject *
1721bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001722/*[clinic end generated code: output=e0ccd401f8021da8 input=3591df2d06c0d237]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001723{
1724 int value;
1725 Py_ssize_t n = Py_SIZE(self);
1726 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001727
1728 if (n == 0) {
Eli Bendersky1bc4f192011-03-04 04:55:25 +00001729 PyErr_SetString(PyExc_IndexError,
1730 "pop from empty bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001731 return NULL;
1732 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001733 if (index < 0)
1734 index += Py_SIZE(self);
1735 if (index < 0 || index >= Py_SIZE(self)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001736 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1737 return NULL;
1738 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001739 if (!_canresize(self))
1740 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001741
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001742 buf = PyByteArray_AS_STRING(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001743 value = buf[index];
1744 memmove(buf + index, buf + index + 1, n - index);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001745 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1746 return NULL;
1747
Mark Dickinson54a3db92009-09-06 10:19:23 +00001748 return PyLong_FromLong((unsigned char)value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001749}
1750
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001751/*[clinic input]
1752bytearray.remove
1753
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001754 value: bytesvalue
1755 The value to remove.
1756 /
1757
1758Remove the first occurrence of a value in the bytearray.
1759[clinic start generated code]*/
1760
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001761static PyObject *
1762bytearray_remove_impl(PyByteArrayObject *self, int value)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001763/*[clinic end generated code: output=d659e37866709c13 input=121831240cd51ddf]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001764{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001765 Py_ssize_t where, n = Py_SIZE(self);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001766 char *buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001767
Serhiy Storchaka4b234942016-05-16 22:24:03 +03001768 where = stringlib_find_char(buf, n, value);
1769 if (where < 0) {
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001770 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001771 return NULL;
1772 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001773 if (!_canresize(self))
1774 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001775
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001776 memmove(buf + where, buf + where + 1, n - where);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001777 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1778 return NULL;
1779
1780 Py_RETURN_NONE;
1781}
1782
1783/* XXX These two helpers could be optimized if argsize == 1 */
1784
1785static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001786lstrip_helper(const char *myptr, Py_ssize_t mysize,
1787 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001788{
1789 Py_ssize_t i = 0;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001790 while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001791 i++;
1792 return i;
1793}
1794
1795static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001796rstrip_helper(const char *myptr, Py_ssize_t mysize,
1797 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001798{
1799 Py_ssize_t i = mysize - 1;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001800 while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001801 i--;
1802 return i + 1;
1803}
1804
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001805/*[clinic input]
1806bytearray.strip
1807
1808 bytes: object = None
1809 /
1810
1811Strip leading and trailing bytes contained in the argument.
1812
1813If the argument is omitted or None, strip leading and trailing ASCII whitespace.
1814[clinic start generated code]*/
1815
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001816static PyObject *
1817bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001818/*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001819{
1820 Py_ssize_t left, right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001821 char *myptr;
1822 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001823 Py_buffer vbytes;
1824
1825 if (bytes == Py_None) {
1826 bytesptr = "\t\n\r\f\v ";
1827 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001828 }
1829 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001830 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001831 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001832 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001833 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001834 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001835 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001836 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001837 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001838 if (left == mysize)
1839 right = left;
1840 else
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001841 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1842 if (bytes != Py_None)
1843 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001844 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001845}
1846
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001847/*[clinic input]
1848bytearray.lstrip
1849
1850 bytes: object = None
1851 /
1852
1853Strip leading bytes contained in the argument.
1854
1855If the argument is omitted or None, strip leading ASCII whitespace.
1856[clinic start generated code]*/
1857
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001858static PyObject *
1859bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001860/*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001861{
1862 Py_ssize_t left, right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001863 char *myptr;
1864 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001865 Py_buffer vbytes;
1866
1867 if (bytes == Py_None) {
1868 bytesptr = "\t\n\r\f\v ";
1869 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001870 }
1871 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001872 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001873 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001874 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001875 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001876 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001877 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001878 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001879 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001880 right = mysize;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001881 if (bytes != Py_None)
1882 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001883 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001884}
1885
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001886/*[clinic input]
1887bytearray.rstrip
1888
1889 bytes: object = None
1890 /
1891
1892Strip trailing bytes contained in the argument.
1893
1894If the argument is omitted or None, strip trailing ASCII whitespace.
1895[clinic start generated code]*/
1896
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001897static PyObject *
1898bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001899/*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001900{
1901 Py_ssize_t right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001902 char *myptr;
1903 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001904 Py_buffer vbytes;
1905
1906 if (bytes == Py_None) {
1907 bytesptr = "\t\n\r\f\v ";
1908 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001909 }
1910 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001911 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001912 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001913 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001914 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001915 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001916 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001917 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001918 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1919 if (bytes != Py_None)
1920 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001921 return PyByteArray_FromStringAndSize(myptr, right);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001922}
1923
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001924/*[clinic input]
1925bytearray.decode
1926
1927 encoding: str(c_default="NULL") = 'utf-8'
1928 The encoding with which to decode the bytearray.
1929 errors: str(c_default="NULL") = 'strict'
1930 The error handling scheme to use for the handling of decoding errors.
1931 The default is 'strict' meaning that decoding errors raise a
1932 UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
1933 as well as any other name registered with codecs.register_error that
1934 can handle UnicodeDecodeErrors.
1935
1936Decode the bytearray using the codec registered for encoding.
1937[clinic start generated code]*/
1938
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001939static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001940bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
1941 const char *errors)
1942/*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001943{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001944 if (encoding == NULL)
1945 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis0efea322014-07-27 17:29:17 +02001946 return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001947}
1948
1949PyDoc_STRVAR(alloc_doc,
1950"B.__alloc__() -> int\n\
1951\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00001952Return the number of bytes actually allocated.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001953
1954static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301955bytearray_alloc(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001956{
1957 return PyLong_FromSsize_t(self->ob_alloc);
1958}
1959
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001960/*[clinic input]
1961bytearray.join
1962
1963 iterable_of_bytes: object
1964 /
1965
1966Concatenate any number of bytes/bytearray objects.
1967
1968The bytearray whose method is called is inserted in between each pair.
1969
1970The result is returned as a new bytearray object.
1971[clinic start generated code]*/
1972
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001973static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001974bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001975/*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001976{
Martin v. Löwis0efea322014-07-27 17:29:17 +02001977 return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001978}
1979
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001980/*[clinic input]
1981bytearray.splitlines
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001982
Serhiy Storchaka202fda52017-03-12 10:10:47 +02001983 keepends: bool(accept={int}) = False
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001984
1985Return a list of the lines in the bytearray, breaking at line boundaries.
1986
1987Line breaks are not included in the resulting list unless keepends is given and
1988true.
1989[clinic start generated code]*/
1990
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001991static PyObject *
1992bytearray_splitlines_impl(PyByteArrayObject *self, int keepends)
Serhiy Storchaka202fda52017-03-12 10:10:47 +02001993/*[clinic end generated code: output=4223c94b895f6ad9 input=99a27ad959b9cf6b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001994{
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001995 return stringlib_splitlines(
1996 (PyObject*) self, PyByteArray_AS_STRING(self),
1997 PyByteArray_GET_SIZE(self), keepends
1998 );
1999}
2000
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002001/*[clinic input]
2002@classmethod
2003bytearray.fromhex
2004
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002005 string: unicode
2006 /
2007
2008Create a bytearray object from a string of hexadecimal numbers.
2009
2010Spaces between two numbers are accepted.
2011Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')
2012[clinic start generated code]*/
2013
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002014static PyObject *
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002015bytearray_fromhex_impl(PyTypeObject *type, PyObject *string)
2016/*[clinic end generated code: output=8f0f0b6d30fb3ba0 input=f033a16d1fb21f48]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002017{
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002018 PyObject *result = _PyBytes_FromHex(string, type == &PyByteArray_Type);
2019 if (type != &PyByteArray_Type && result != NULL) {
Jeroen Demeyer196a5302019-07-04 12:31:34 +02002020 Py_SETREF(result, _PyObject_CallOneArg((PyObject *)type, result));
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002021 }
2022 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002023}
2024
Gregory P. Smith0c2f9302019-05-29 11:46:58 -07002025/*[clinic input]
2026bytearray.hex
2027
2028 sep: object = NULL
2029 An optional single character or byte to separate hex bytes.
2030 bytes_per_sep: int = 1
2031 How many bytes between separators. Positive values count from the
2032 right, negative values count from the left.
2033
2034Create a str of hexadecimal numbers from a bytearray object.
2035
2036Example:
2037>>> value = bytearray([0xb9, 0x01, 0xef])
2038>>> value.hex()
2039'b901ef'
2040>>> value.hex(':')
2041'b9:01:ef'
2042>>> value.hex(':', 2)
2043'b9:01ef'
2044>>> value.hex(':', -2)
2045'b901:ef'
2046[clinic start generated code]*/
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002047
2048static PyObject *
Gregory P. Smith0c2f9302019-05-29 11:46:58 -07002049bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep)
2050/*[clinic end generated code: output=29c4e5ef72c565a0 input=814c15830ac8c4b5]*/
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002051{
2052 char* argbuf = PyByteArray_AS_STRING(self);
2053 Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
Gregory P. Smith0c2f9302019-05-29 11:46:58 -07002054 return _Py_strhex_with_sep(argbuf, arglen, sep, bytes_per_sep);
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002055}
2056
2057static PyObject *
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002058_common_reduce(PyByteArrayObject *self, int proto)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002059{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002060 PyObject *dict;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002061 _Py_IDENTIFIER(__dict__);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002062 char *buf;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002063
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002064 dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002065 if (dict == NULL) {
2066 PyErr_Clear();
2067 dict = Py_None;
2068 Py_INCREF(dict);
2069 }
2070
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002071 buf = PyByteArray_AS_STRING(self);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002072 if (proto < 3) {
2073 /* use str based reduction for backwards compatibility with Python 2.x */
2074 PyObject *latin1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002075 if (Py_SIZE(self))
2076 latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002077 else
2078 latin1 = PyUnicode_FromString("");
2079 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
2080 }
2081 else {
2082 /* use more efficient byte based reduction */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002083 if (Py_SIZE(self)) {
2084 return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002085 }
2086 else {
2087 return Py_BuildValue("(O()N)", Py_TYPE(self), dict);
2088 }
2089 }
2090}
2091
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002092/*[clinic input]
2093bytearray.__reduce__ as bytearray_reduce
2094
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002095Return state information for pickling.
2096[clinic start generated code]*/
2097
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002098static PyObject *
2099bytearray_reduce_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002100/*[clinic end generated code: output=52bf304086464cab input=44b5737ada62dd3f]*/
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002101{
2102 return _common_reduce(self, 2);
2103}
2104
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002105/*[clinic input]
2106bytearray.__reduce_ex__ as bytearray_reduce_ex
2107
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002108 proto: int = 0
2109 /
2110
2111Return state information for pickling.
2112[clinic start generated code]*/
2113
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002114static PyObject *
2115bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002116/*[clinic end generated code: output=52eac33377197520 input=f129bc1a1aa151ee]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002117{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002118 return _common_reduce(self, proto);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002119}
2120
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002121/*[clinic input]
2122bytearray.__sizeof__ as bytearray_sizeof
2123
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002124Returns the size of the bytearray object in memory, in bytes.
2125[clinic start generated code]*/
2126
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002127static PyObject *
2128bytearray_sizeof_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002129/*[clinic end generated code: output=738abdd17951c427 input=e27320fd98a4bc5a]*/
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002130{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002131 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002132
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002133 res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002134 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002135}
2136
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002137static PySequenceMethods bytearray_as_sequence = {
2138 (lenfunc)bytearray_length, /* sq_length */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002139 (binaryfunc)PyByteArray_Concat, /* sq_concat */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002140 (ssizeargfunc)bytearray_repeat, /* sq_repeat */
2141 (ssizeargfunc)bytearray_getitem, /* sq_item */
2142 0, /* sq_slice */
2143 (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */
2144 0, /* sq_ass_slice */
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002145 (objobjproc)bytearray_contains, /* sq_contains */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002146 (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */
2147 (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002148};
2149
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002150static PyMappingMethods bytearray_as_mapping = {
2151 (lenfunc)bytearray_length,
2152 (binaryfunc)bytearray_subscript,
2153 (objobjargproc)bytearray_ass_subscript,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002154};
2155
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002156static PyBufferProcs bytearray_as_buffer = {
2157 (getbufferproc)bytearray_getbuffer,
2158 (releasebufferproc)bytearray_releasebuffer,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002159};
2160
2161static PyMethodDef
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002162bytearray_methods[] = {
2163 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002164 BYTEARRAY_REDUCE_METHODDEF
2165 BYTEARRAY_REDUCE_EX_METHODDEF
2166 BYTEARRAY_SIZEOF_METHODDEF
2167 BYTEARRAY_APPEND_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302168 {"capitalize", stringlib_capitalize, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002169 _Py_capitalize__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002170 STRINGLIB_CENTER_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002171 BYTEARRAY_CLEAR_METHODDEF
2172 BYTEARRAY_COPY_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002173 {"count", (PyCFunction)bytearray_count, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002174 _Py_count__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002175 BYTEARRAY_DECODE_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002176 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002177 _Py_endswith__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002178 STRINGLIB_EXPANDTABS_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002179 BYTEARRAY_EXTEND_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002180 {"find", (PyCFunction)bytearray_find, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002181 _Py_find__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002182 BYTEARRAY_FROMHEX_METHODDEF
Gregory P. Smith0c2f9302019-05-29 11:46:58 -07002183 BYTEARRAY_HEX_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002184 {"index", (PyCFunction)bytearray_index, METH_VARARGS, _Py_index__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002185 BYTEARRAY_INSERT_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302186 {"isalnum", stringlib_isalnum, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002187 _Py_isalnum__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302188 {"isalpha", stringlib_isalpha, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002189 _Py_isalpha__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302190 {"isascii", stringlib_isascii, METH_NOARGS,
INADA Naokia49ac992018-01-27 14:06:21 +09002191 _Py_isascii__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302192 {"isdigit", stringlib_isdigit, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002193 _Py_isdigit__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302194 {"islower", stringlib_islower, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002195 _Py_islower__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302196 {"isspace", stringlib_isspace, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002197 _Py_isspace__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302198 {"istitle", stringlib_istitle, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002199 _Py_istitle__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302200 {"isupper", stringlib_isupper, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002201 _Py_isupper__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002202 BYTEARRAY_JOIN_METHODDEF
Tal Einatc929df32018-07-06 13:17:38 +03002203 STRINGLIB_LJUST_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302204 {"lower", stringlib_lower, METH_NOARGS, _Py_lower__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002205 BYTEARRAY_LSTRIP_METHODDEF
2206 BYTEARRAY_MAKETRANS_METHODDEF
2207 BYTEARRAY_PARTITION_METHODDEF
2208 BYTEARRAY_POP_METHODDEF
2209 BYTEARRAY_REMOVE_METHODDEF
2210 BYTEARRAY_REPLACE_METHODDEF
2211 BYTEARRAY_REVERSE_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002212 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__},
2213 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002214 STRINGLIB_RJUST_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002215 BYTEARRAY_RPARTITION_METHODDEF
2216 BYTEARRAY_RSPLIT_METHODDEF
2217 BYTEARRAY_RSTRIP_METHODDEF
2218 BYTEARRAY_SPLIT_METHODDEF
2219 BYTEARRAY_SPLITLINES_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002220 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002221 _Py_startswith__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002222 BYTEARRAY_STRIP_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302223 {"swapcase", stringlib_swapcase, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002224 _Py_swapcase__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302225 {"title", stringlib_title, METH_NOARGS, _Py_title__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002226 BYTEARRAY_TRANSLATE_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302227 {"upper", stringlib_upper, METH_NOARGS, _Py_upper__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002228 STRINGLIB_ZFILL_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002229 {NULL}
2230};
2231
Ethan Furmanb95b5612015-01-23 20:05:18 -08002232static PyObject *
2233bytearray_mod(PyObject *v, PyObject *w)
2234{
2235 if (!PyByteArray_Check(v))
2236 Py_RETURN_NOTIMPLEMENTED;
Berker Peksag43de36d2016-04-16 01:20:47 +03002237 return _PyBytes_FormatEx(PyByteArray_AS_STRING(v), PyByteArray_GET_SIZE(v), w, 1);
Ethan Furmanb95b5612015-01-23 20:05:18 -08002238}
2239
2240static PyNumberMethods bytearray_as_number = {
2241 0, /*nb_add*/
2242 0, /*nb_subtract*/
2243 0, /*nb_multiply*/
2244 bytearray_mod, /*nb_remainder*/
2245};
2246
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002247PyDoc_STRVAR(bytearray_doc,
Georg Brandl17cb8a82008-05-30 08:20:09 +00002248"bytearray(iterable_of_ints) -> bytearray\n\
2249bytearray(string, encoding[, errors]) -> bytearray\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002250bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\
2251bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\
2252bytearray() -> empty bytes array\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002253\n\
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03002254Construct a mutable bytearray object from:\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002255 - an iterable yielding integers in range(256)\n\
2256 - a text string encoded using the specified encoding\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002257 - a bytes or a buffer object\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002258 - any object implementing the buffer API.\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002259 - an integer");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002260
2261
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002262static PyObject *bytearray_iter(PyObject *seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002263
2264PyTypeObject PyByteArray_Type = {
2265 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2266 "bytearray",
2267 sizeof(PyByteArrayObject),
2268 0,
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002269 (destructor)bytearray_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002270 0, /* tp_vectorcall_offset */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002271 0, /* tp_getattr */
2272 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002273 0, /* tp_as_async */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002274 (reprfunc)bytearray_repr, /* tp_repr */
Ethan Furmanb95b5612015-01-23 20:05:18 -08002275 &bytearray_as_number, /* tp_as_number */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002276 &bytearray_as_sequence, /* tp_as_sequence */
2277 &bytearray_as_mapping, /* tp_as_mapping */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002278 0, /* tp_hash */
2279 0, /* tp_call */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002280 bytearray_str, /* tp_str */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002281 PyObject_GenericGetAttr, /* tp_getattro */
2282 0, /* tp_setattro */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002283 &bytearray_as_buffer, /* tp_as_buffer */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002284 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002285 bytearray_doc, /* tp_doc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002286 0, /* tp_traverse */
2287 0, /* tp_clear */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002288 (richcmpfunc)bytearray_richcompare, /* tp_richcompare */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002289 0, /* tp_weaklistoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002290 bytearray_iter, /* tp_iter */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002291 0, /* tp_iternext */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002292 bytearray_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002293 0, /* tp_members */
2294 0, /* tp_getset */
2295 0, /* tp_base */
2296 0, /* tp_dict */
2297 0, /* tp_descr_get */
2298 0, /* tp_descr_set */
2299 0, /* tp_dictoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002300 (initproc)bytearray_init, /* tp_init */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002301 PyType_GenericAlloc, /* tp_alloc */
2302 PyType_GenericNew, /* tp_new */
2303 PyObject_Del, /* tp_free */
2304};
2305
2306/*********************** Bytes Iterator ****************************/
2307
2308typedef struct {
2309 PyObject_HEAD
2310 Py_ssize_t it_index;
2311 PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
2312} bytesiterobject;
2313
2314static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002315bytearrayiter_dealloc(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002316{
2317 _PyObject_GC_UNTRACK(it);
2318 Py_XDECREF(it->it_seq);
2319 PyObject_GC_Del(it);
2320}
2321
2322static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002323bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002324{
2325 Py_VISIT(it->it_seq);
2326 return 0;
2327}
2328
2329static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002330bytearrayiter_next(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002331{
2332 PyByteArrayObject *seq;
2333 PyObject *item;
2334
2335 assert(it != NULL);
2336 seq = it->it_seq;
2337 if (seq == NULL)
2338 return NULL;
2339 assert(PyByteArray_Check(seq));
2340
2341 if (it->it_index < PyByteArray_GET_SIZE(seq)) {
2342 item = PyLong_FromLong(
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002343 (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002344 if (item != NULL)
2345 ++it->it_index;
2346 return item;
2347 }
2348
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002349 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03002350 Py_DECREF(seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002351 return NULL;
2352}
2353
2354static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302355bytearrayiter_length_hint(bytesiterobject *it, PyObject *Py_UNUSED(ignored))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002356{
2357 Py_ssize_t len = 0;
Serhiy Storchakaaf658722016-07-03 14:41:36 +03002358 if (it->it_seq) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002359 len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
Serhiy Storchakaaf658722016-07-03 14:41:36 +03002360 if (len < 0) {
2361 len = 0;
2362 }
2363 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002364 return PyLong_FromSsize_t(len);
2365}
2366
2367PyDoc_STRVAR(length_hint_doc,
2368 "Private method returning an estimate of len(list(it)).");
2369
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002370static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302371bytearrayiter_reduce(bytesiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002372{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002373 _Py_IDENTIFIER(iter);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002374 if (it->it_seq != NULL) {
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002375 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002376 it->it_seq, it->it_index);
2377 } else {
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002378 return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002379 }
2380}
2381
2382static PyObject *
2383bytearrayiter_setstate(bytesiterobject *it, PyObject *state)
2384{
2385 Py_ssize_t index = PyLong_AsSsize_t(state);
2386 if (index == -1 && PyErr_Occurred())
2387 return NULL;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00002388 if (it->it_seq != NULL) {
2389 if (index < 0)
2390 index = 0;
2391 else if (index > PyByteArray_GET_SIZE(it->it_seq))
2392 index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */
2393 it->it_index = index;
2394 }
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002395 Py_RETURN_NONE;
2396}
2397
2398PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
2399
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002400static PyMethodDef bytearrayiter_methods[] = {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002401 {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002402 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002403 {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002404 bytearray_reduce__doc__},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002405 {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
2406 setstate_doc},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002407 {NULL, NULL} /* sentinel */
2408};
2409
2410PyTypeObject PyByteArrayIter_Type = {
2411 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2412 "bytearray_iterator", /* tp_name */
2413 sizeof(bytesiterobject), /* tp_basicsize */
2414 0, /* tp_itemsize */
2415 /* methods */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002416 (destructor)bytearrayiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002417 0, /* tp_vectorcall_offset */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002418 0, /* tp_getattr */
2419 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002420 0, /* tp_as_async */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002421 0, /* tp_repr */
2422 0, /* tp_as_number */
2423 0, /* tp_as_sequence */
2424 0, /* tp_as_mapping */
2425 0, /* tp_hash */
2426 0, /* tp_call */
2427 0, /* tp_str */
2428 PyObject_GenericGetAttr, /* tp_getattro */
2429 0, /* tp_setattro */
2430 0, /* tp_as_buffer */
2431 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
2432 0, /* tp_doc */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002433 (traverseproc)bytearrayiter_traverse, /* tp_traverse */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002434 0, /* tp_clear */
2435 0, /* tp_richcompare */
2436 0, /* tp_weaklistoffset */
2437 PyObject_SelfIter, /* tp_iter */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002438 (iternextfunc)bytearrayiter_next, /* tp_iternext */
2439 bytearrayiter_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002440 0,
2441};
2442
2443static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002444bytearray_iter(PyObject *seq)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002445{
2446 bytesiterobject *it;
2447
2448 if (!PyByteArray_Check(seq)) {
2449 PyErr_BadInternalCall();
2450 return NULL;
2451 }
2452 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
2453 if (it == NULL)
2454 return NULL;
2455 it->it_index = 0;
2456 Py_INCREF(seq);
2457 it->it_seq = (PyByteArrayObject *)seq;
2458 _PyObject_GC_TRACK(it);
2459 return (PyObject *)it;
2460}