blob: 7ebfa1f9434cc2212e9c1cbe7a0f0b088c68f533 [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 Stinnera15e2602020-04-08 02:01:56 +02005#include "pycore_abstract.h" // _PyIndex_Check()
Victor Stinner45876a92020-02-12 22:32:34 +01006#include "pycore_bytes_methods.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +01007#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01008#include "pycore_pymem.h"
9#include "pycore_pystate.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +000010#include "structmember.h"
Ethan Furmanb95b5612015-01-23 20:05:18 -080011#include "bytesobject.h"
Gregory P. Smith8cb65692015-04-25 23:22:26 +000012#include "pystrhex.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +000013
Martin v. Löwis7252a6e2014-07-27 16:25:09 +020014/*[clinic input]
15class bytearray "PyByteArrayObject *" "&PyByteArray_Type"
16[clinic start generated code]*/
17/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/
18
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000019char _PyByteArray_empty_string[] = "";
Christian Heimes2c9c7a52008-05-26 13:42:13 +000020
Christian Heimes2c9c7a52008-05-26 13:42:13 +000021/* end nullbytes support */
22
23/* Helpers */
24
25static int
26_getbytevalue(PyObject* arg, int *value)
27{
28 long face_value;
29
30 if (PyLong_Check(arg)) {
31 face_value = PyLong_AsLong(arg);
Georg Brandl9a54d7c2008-07-16 23:15:30 +000032 } else {
33 PyObject *index = PyNumber_Index(arg);
34 if (index == NULL) {
Mark Dickinson10de93a2010-07-09 19:25:48 +000035 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000036 return 0;
37 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +000038 face_value = PyLong_AsLong(index);
39 Py_DECREF(index);
40 }
41
42 if (face_value < 0 || face_value >= 256) {
43 /* this includes the OverflowError in case the long is too large */
44 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
Mark Dickinson10de93a2010-07-09 19:25:48 +000045 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000046 return 0;
47 }
48
49 *value = face_value;
50 return 1;
51}
52
53static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +000054bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000055{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000056 void *ptr;
57 if (view == NULL) {
Stefan Krah5178d912015-02-03 16:57:21 +010058 PyErr_SetString(PyExc_BufferError,
59 "bytearray_getbuffer: view==NULL argument is obsolete");
60 return -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000061 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000062 ptr = (void *) PyByteArray_AS_STRING(obj);
Stefan Krah5178d912015-02-03 16:57:21 +010063 /* cannot fail if view != NULL and readonly == 0 */
64 (void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
65 obj->ob_exports++;
66 return 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000067}
68
69static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +000070bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000071{
72 obj->ob_exports--;
73}
74
Antoine Pitrou5504e892008-12-06 21:27:53 +000075static int
76_canresize(PyByteArrayObject *self)
77{
78 if (self->ob_exports > 0) {
79 PyErr_SetString(PyExc_BufferError,
80 "Existing exports of data: object cannot be re-sized");
81 return 0;
82 }
83 return 1;
84}
85
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030086#include "clinic/bytearrayobject.c.h"
87
Christian Heimes2c9c7a52008-05-26 13:42:13 +000088/* Direct API functions */
89
90PyObject *
91PyByteArray_FromObject(PyObject *input)
92{
Petr Viktorinffd97532020-02-11 17:46:57 +010093 return PyObject_CallOneArg((PyObject *)&PyByteArray_Type, input);
Christian Heimes2c9c7a52008-05-26 13:42:13 +000094}
95
Serhiy Storchakaa2314282017-10-29 02:11:54 +030096static PyObject *
97_PyByteArray_FromBufferObject(PyObject *obj)
98{
99 PyObject *result;
100 Py_buffer view;
101
102 if (PyObject_GetBuffer(obj, &view, PyBUF_FULL_RO) < 0) {
103 return NULL;
104 }
105 result = PyByteArray_FromStringAndSize(NULL, view.len);
106 if (result != NULL &&
107 PyBuffer_ToContiguous(PyByteArray_AS_STRING(result),
108 &view, view.len, 'C') < 0)
109 {
110 Py_CLEAR(result);
111 }
112 PyBuffer_Release(&view);
113 return result;
114}
115
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000116PyObject *
117PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
118{
119 PyByteArrayObject *new;
120 Py_ssize_t alloc;
121
122 if (size < 0) {
123 PyErr_SetString(PyExc_SystemError,
124 "Negative size passed to PyByteArray_FromStringAndSize");
125 return NULL;
126 }
127
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000128 /* Prevent buffer overflow when setting alloc to size+1. */
129 if (size == PY_SSIZE_T_MAX) {
130 return PyErr_NoMemory();
131 }
132
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000133 new = PyObject_New(PyByteArrayObject, &PyByteArray_Type);
134 if (new == NULL)
135 return NULL;
136
137 if (size == 0) {
138 new->ob_bytes = NULL;
139 alloc = 0;
140 }
141 else {
142 alloc = size + 1;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100143 new->ob_bytes = PyObject_Malloc(alloc);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000144 if (new->ob_bytes == NULL) {
145 Py_DECREF(new);
146 return PyErr_NoMemory();
147 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +0000148 if (bytes != NULL && size > 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000149 memcpy(new->ob_bytes, bytes, size);
150 new->ob_bytes[size] = '\0'; /* Trailing null byte */
151 }
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100152 Py_SET_SIZE(new, size);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000153 new->ob_alloc = alloc;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200154 new->ob_start = new->ob_bytes;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000155 new->ob_exports = 0;
156
157 return (PyObject *)new;
158}
159
160Py_ssize_t
161PyByteArray_Size(PyObject *self)
162{
163 assert(self != NULL);
164 assert(PyByteArray_Check(self));
165
166 return PyByteArray_GET_SIZE(self);
167}
168
169char *
170PyByteArray_AsString(PyObject *self)
171{
172 assert(self != NULL);
173 assert(PyByteArray_Check(self));
174
175 return PyByteArray_AS_STRING(self);
176}
177
178int
Antoine Pitroucc231542014-11-02 18:40:09 +0100179PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000180{
181 void *sval;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200182 PyByteArrayObject *obj = ((PyByteArrayObject *)self);
Antoine Pitroucc231542014-11-02 18:40:09 +0100183 /* All computations are done unsigned to avoid integer overflows
184 (see issue #22335). */
185 size_t alloc = (size_t) obj->ob_alloc;
186 size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes);
187 size_t size = (size_t) requested_size;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000188
189 assert(self != NULL);
190 assert(PyByteArray_Check(self));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200191 assert(logical_offset <= alloc);
Antoine Pitroucc231542014-11-02 18:40:09 +0100192 assert(requested_size >= 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000193
Antoine Pitroucc231542014-11-02 18:40:09 +0100194 if (requested_size == Py_SIZE(self)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000195 return 0;
196 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200197 if (!_canresize(obj)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000198 return -1;
199 }
200
Antoine Pitrou25454112015-05-19 20:52:27 +0200201 if (size + logical_offset + 1 <= alloc) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200202 /* Current buffer is large enough to host the requested size,
203 decide on a strategy. */
204 if (size < alloc / 2) {
205 /* Major downsize; resize down to exact size */
206 alloc = size + 1;
207 }
208 else {
209 /* Minor downsize; quick exit */
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100210 Py_SET_SIZE(self, size);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200211 PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */
212 return 0;
213 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000214 }
215 else {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200216 /* Need growing, decide on a strategy */
217 if (size <= alloc * 1.125) {
218 /* Moderate upsize; overallocate similar to list_resize() */
219 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
220 }
221 else {
222 /* Major upsize; resize up to exact size */
223 alloc = size + 1;
224 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000225 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100226 if (alloc > PY_SSIZE_T_MAX) {
227 PyErr_NoMemory();
228 return -1;
229 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000230
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200231 if (logical_offset > 0) {
232 sval = PyObject_Malloc(alloc);
233 if (sval == NULL) {
234 PyErr_NoMemory();
235 return -1;
236 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100237 memcpy(sval, PyByteArray_AS_STRING(self),
Stefan Krahdce65022017-08-25 20:12:05 +0200238 Py_MIN((size_t)requested_size, (size_t)Py_SIZE(self)));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200239 PyObject_Free(obj->ob_bytes);
240 }
241 else {
242 sval = PyObject_Realloc(obj->ob_bytes, alloc);
243 if (sval == NULL) {
244 PyErr_NoMemory();
245 return -1;
246 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000247 }
248
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200249 obj->ob_bytes = obj->ob_start = sval;
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100250 Py_SET_SIZE(self, size);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200251 obj->ob_alloc = alloc;
252 obj->ob_bytes[size] = '\0'; /* Trailing null byte */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000253
254 return 0;
255}
256
257PyObject *
258PyByteArray_Concat(PyObject *a, PyObject *b)
259{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000260 Py_buffer va, vb;
261 PyByteArrayObject *result = NULL;
262
263 va.len = -1;
264 vb.len = -1;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200265 if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 ||
266 PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000267 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
Serhiy Storchaka6b5a9ec2017-03-19 19:47:02 +0200268 Py_TYPE(b)->tp_name, Py_TYPE(a)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000269 goto done;
270 }
271
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300272 if (va.len > PY_SSIZE_T_MAX - vb.len) {
273 PyErr_NoMemory();
274 goto done;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000275 }
276
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300277 result = (PyByteArrayObject *) \
278 PyByteArray_FromStringAndSize(NULL, va.len + vb.len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000279 if (result != NULL) {
280 memcpy(result->ob_bytes, va.buf, va.len);
281 memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
282 }
283
284 done:
285 if (va.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000286 PyBuffer_Release(&va);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000287 if (vb.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000288 PyBuffer_Release(&vb);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000289 return (PyObject *)result;
290}
291
292/* Functions stuffed into the type object */
293
294static Py_ssize_t
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000295bytearray_length(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000296{
297 return Py_SIZE(self);
298}
299
300static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000301bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000302{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000303 Py_ssize_t size;
304 Py_buffer vo;
305
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200306 if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000307 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
308 Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name);
309 return NULL;
310 }
311
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300312 size = Py_SIZE(self);
313 if (size > PY_SSIZE_T_MAX - vo.len) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000314 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000315 return PyErr_NoMemory();
316 }
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300317 if (PyByteArray_Resize((PyObject *)self, size + vo.len) < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000318 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000319 return NULL;
320 }
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300321 memcpy(PyByteArray_AS_STRING(self) + size, vo.buf, vo.len);
Martin v. Löwis423be952008-08-13 15:53:07 +0000322 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000323 Py_INCREF(self);
324 return (PyObject *)self;
325}
326
327static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000328bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000329{
330 PyByteArrayObject *result;
331 Py_ssize_t mysize;
332 Py_ssize_t size;
333
334 if (count < 0)
335 count = 0;
336 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000337 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000338 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000339 size = mysize * count;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000340 result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size);
341 if (result != NULL && size != 0) {
342 if (mysize == 1)
343 memset(result->ob_bytes, self->ob_bytes[0], size);
344 else {
345 Py_ssize_t i;
346 for (i = 0; i < count; i++)
347 memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize);
348 }
349 }
350 return (PyObject *)result;
351}
352
353static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000354bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000355{
356 Py_ssize_t mysize;
357 Py_ssize_t size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200358 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000359
360 if (count < 0)
361 count = 0;
362 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000363 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000364 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000365 size = mysize * count;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200366 if (PyByteArray_Resize((PyObject *)self, size) < 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000367 return NULL;
368
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200369 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000370 if (mysize == 1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200371 memset(buf, buf[0], size);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000372 else {
373 Py_ssize_t i;
374 for (i = 1; i < count; i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200375 memcpy(buf + i*mysize, buf, mysize);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000376 }
377
378 Py_INCREF(self);
379 return (PyObject *)self;
380}
381
382static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000383bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000384{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000385 if (i < 0 || i >= Py_SIZE(self)) {
386 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
387 return NULL;
388 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200389 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000390}
391
392static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000393bytearray_subscript(PyByteArrayObject *self, PyObject *index)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000394{
Victor Stinnera15e2602020-04-08 02:01:56 +0200395 if (_PyIndex_Check(index)) {
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000396 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000397
398 if (i == -1 && PyErr_Occurred())
399 return NULL;
400
401 if (i < 0)
402 i += PyByteArray_GET_SIZE(self);
403
404 if (i < 0 || i >= Py_SIZE(self)) {
405 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
406 return NULL;
407 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200408 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000409 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000410 else if (PySlice_Check(index)) {
Zackery Spytz14514d92019-05-17 01:13:03 -0600411 Py_ssize_t start, stop, step, slicelength, i;
412 size_t cur;
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300413 if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000414 return NULL;
415 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300416 slicelength = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self),
417 &start, &stop, step);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000418
419 if (slicelength <= 0)
420 return PyByteArray_FromStringAndSize("", 0);
421 else if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200422 return PyByteArray_FromStringAndSize(
423 PyByteArray_AS_STRING(self) + start, slicelength);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000424 }
425 else {
426 char *source_buf = PyByteArray_AS_STRING(self);
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000427 char *result_buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000428 PyObject *result;
429
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000430 result = PyByteArray_FromStringAndSize(NULL, slicelength);
431 if (result == NULL)
432 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000433
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000434 result_buf = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000435 for (cur = start, i = 0; i < slicelength;
436 cur += step, i++) {
437 result_buf[i] = source_buf[cur];
438 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000439 return result;
440 }
441 }
442 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400443 PyErr_Format(PyExc_TypeError,
444 "bytearray indices must be integers or slices, not %.200s",
445 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000446 return NULL;
447 }
448}
449
450static int
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200451bytearray_setslice_linear(PyByteArrayObject *self,
452 Py_ssize_t lo, Py_ssize_t hi,
453 char *bytes, Py_ssize_t bytes_len)
454{
455 Py_ssize_t avail = hi - lo;
456 char *buf = PyByteArray_AS_STRING(self);
457 Py_ssize_t growth = bytes_len - avail;
Victor Stinner84557232013-11-21 12:29:51 +0100458 int res = 0;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200459 assert(avail >= 0);
460
Victor Stinner84557232013-11-21 12:29:51 +0100461 if (growth < 0) {
462 if (!_canresize(self))
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200463 return -1;
Victor Stinner84557232013-11-21 12:29:51 +0100464
465 if (lo == 0) {
466 /* Shrink the buffer by advancing its logical start */
467 self->ob_start -= growth;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200468 /*
Victor Stinner84557232013-11-21 12:29:51 +0100469 0 lo hi old_size
470 | |<----avail----->|<-----tail------>|
471 | |<-bytes_len->|<-----tail------>|
472 0 new_lo new_hi new_size
473 */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200474 }
Victor Stinner84557232013-11-21 12:29:51 +0100475 else {
476 /*
477 0 lo hi old_size
478 | |<----avail----->|<-----tomove------>|
479 | |<-bytes_len->|<-----tomove------>|
480 0 lo new_hi new_size
481 */
482 memmove(buf + lo + bytes_len, buf + hi,
483 Py_SIZE(self) - hi);
484 }
485 if (PyByteArray_Resize((PyObject *)self,
486 Py_SIZE(self) + growth) < 0) {
487 /* Issue #19578: Handling the memory allocation failure here is
488 tricky here because the bytearray object has already been
489 modified. Depending on growth and lo, the behaviour is
490 different.
491
492 If growth < 0 and lo != 0, the operation is completed, but a
493 MemoryError is still raised and the memory block is not
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700494 shrunk. Otherwise, the bytearray is restored in its previous
Victor Stinner84557232013-11-21 12:29:51 +0100495 state and a MemoryError is raised. */
496 if (lo == 0) {
497 self->ob_start += growth;
498 return -1;
499 }
500 /* memmove() removed bytes, the bytearray object cannot be
501 restored in its previous state. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100502 Py_SET_SIZE(self, Py_SIZE(self) + growth);
Victor Stinner84557232013-11-21 12:29:51 +0100503 res = -1;
504 }
505 buf = PyByteArray_AS_STRING(self);
506 }
507 else if (growth > 0) {
508 if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) {
509 PyErr_NoMemory();
510 return -1;
511 }
512
513 if (PyByteArray_Resize((PyObject *)self,
514 Py_SIZE(self) + growth) < 0) {
515 return -1;
516 }
517 buf = PyByteArray_AS_STRING(self);
518 /* Make the place for the additional bytes */
519 /*
520 0 lo hi old_size
521 | |<-avail->|<-----tomove------>|
522 | |<---bytes_len-->|<-----tomove------>|
523 0 lo new_hi new_size
524 */
525 memmove(buf + lo + bytes_len, buf + hi,
526 Py_SIZE(self) - lo - bytes_len);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200527 }
528
529 if (bytes_len > 0)
530 memcpy(buf + lo, bytes, bytes_len);
Victor Stinner84557232013-11-21 12:29:51 +0100531 return res;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200532}
533
534static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000535bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000536 PyObject *values)
537{
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200538 Py_ssize_t needed;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000539 void *bytes;
540 Py_buffer vbytes;
541 int res = 0;
542
543 vbytes.len = -1;
544 if (values == (PyObject *)self) {
545 /* Make a copy and call this function recursively */
546 int err;
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300547 values = PyByteArray_FromStringAndSize(PyByteArray_AS_STRING(values),
548 PyByteArray_GET_SIZE(values));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000549 if (values == NULL)
550 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000551 err = bytearray_setslice(self, lo, hi, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000552 Py_DECREF(values);
553 return err;
554 }
555 if (values == NULL) {
556 /* del b[lo:hi] */
557 bytes = NULL;
558 needed = 0;
559 }
560 else {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200561 if (PyObject_GetBuffer(values, &vbytes, PyBUF_SIMPLE) != 0) {
562 PyErr_Format(PyExc_TypeError,
563 "can't set bytearray slice from %.100s",
564 Py_TYPE(values)->tp_name);
565 return -1;
566 }
567 needed = vbytes.len;
568 bytes = vbytes.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000569 }
570
571 if (lo < 0)
572 lo = 0;
573 if (hi < lo)
574 hi = lo;
575 if (hi > Py_SIZE(self))
576 hi = Py_SIZE(self);
577
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200578 res = bytearray_setslice_linear(self, lo, hi, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000579 if (vbytes.len != -1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200580 PyBuffer_Release(&vbytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000581 return res;
582}
583
584static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000585bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000586{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000587 int ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000588
589 if (i < 0)
590 i += Py_SIZE(self);
591
592 if (i < 0 || i >= Py_SIZE(self)) {
593 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
594 return -1;
595 }
596
597 if (value == NULL)
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000598 return bytearray_setslice(self, i, i+1, NULL);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000599
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000600 if (!_getbytevalue(value, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000601 return -1;
602
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200603 PyByteArray_AS_STRING(self)[i] = ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000604 return 0;
605}
606
607static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000608bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000609{
610 Py_ssize_t start, stop, step, slicelen, needed;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200611 char *buf, *bytes;
612 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000613
Victor Stinnera15e2602020-04-08 02:01:56 +0200614 if (_PyIndex_Check(index)) {
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000615 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000616
617 if (i == -1 && PyErr_Occurred())
618 return -1;
619
620 if (i < 0)
621 i += PyByteArray_GET_SIZE(self);
622
623 if (i < 0 || i >= Py_SIZE(self)) {
624 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
625 return -1;
626 }
627
628 if (values == NULL) {
629 /* Fall through to slice assignment */
630 start = i;
631 stop = i + 1;
632 step = 1;
633 slicelen = 1;
634 }
635 else {
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000636 int ival;
637 if (!_getbytevalue(values, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000638 return -1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200639 buf[i] = (char)ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000640 return 0;
641 }
642 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000643 else if (PySlice_Check(index)) {
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300644 if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000645 return -1;
646 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300647 slicelen = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self), &start,
648 &stop, step);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000649 }
650 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400651 PyErr_Format(PyExc_TypeError,
652 "bytearray indices must be integers or slices, not %.200s",
653 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000654 return -1;
655 }
656
657 if (values == NULL) {
658 bytes = NULL;
659 needed = 0;
660 }
661 else if (values == (PyObject *)self || !PyByteArray_Check(values)) {
Christian Heimes6d26ade2012-11-03 23:07:59 +0100662 int err;
Ezio Melottic64bcbe2012-11-03 21:19:06 +0200663 if (PyNumber_Check(values) || PyUnicode_Check(values)) {
664 PyErr_SetString(PyExc_TypeError,
665 "can assign only bytes, buffers, or iterables "
666 "of ints in range(0, 256)");
667 return -1;
668 }
Georg Brandlf3fa5682010-12-04 17:09:30 +0000669 /* Make a copy and call this function recursively */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000670 values = PyByteArray_FromObject(values);
671 if (values == NULL)
672 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000673 err = bytearray_ass_subscript(self, index, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000674 Py_DECREF(values);
675 return err;
676 }
677 else {
678 assert(PyByteArray_Check(values));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200679 bytes = PyByteArray_AS_STRING(values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000680 needed = Py_SIZE(values);
681 }
682 /* Make sure b[5:2] = ... inserts before 5, not before 2. */
683 if ((step < 0 && start < stop) ||
684 (step > 0 && start > stop))
685 stop = start;
686 if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200687 return bytearray_setslice_linear(self, start, stop, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000688 }
689 else {
690 if (needed == 0) {
691 /* Delete slice */
Mark Dickinsonbc099642010-01-29 17:27:24 +0000692 size_t cur;
693 Py_ssize_t i;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000694
Antoine Pitrou5504e892008-12-06 21:27:53 +0000695 if (!_canresize(self))
696 return -1;
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000697
698 if (slicelen == 0)
699 /* Nothing to do here. */
700 return 0;
701
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000702 if (step < 0) {
703 stop = start + 1;
704 start = stop + step * (slicelen - 1) - 1;
705 step = -step;
706 }
707 for (cur = start, i = 0;
708 i < slicelen; cur += step, i++) {
709 Py_ssize_t lim = step - 1;
710
Mark Dickinson66f575b2010-02-14 12:53:32 +0000711 if (cur + step >= (size_t)PyByteArray_GET_SIZE(self))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000712 lim = PyByteArray_GET_SIZE(self) - cur - 1;
713
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200714 memmove(buf + cur - i,
715 buf + cur + 1, lim);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000716 }
717 /* Move the tail of the bytes, in one chunk */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000718 cur = start + (size_t)slicelen*step;
Mark Dickinson66f575b2010-02-14 12:53:32 +0000719 if (cur < (size_t)PyByteArray_GET_SIZE(self)) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200720 memmove(buf + cur - slicelen,
721 buf + cur,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000722 PyByteArray_GET_SIZE(self) - cur);
723 }
724 if (PyByteArray_Resize((PyObject *)self,
725 PyByteArray_GET_SIZE(self) - slicelen) < 0)
726 return -1;
727
728 return 0;
729 }
730 else {
731 /* Assign slice */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000732 Py_ssize_t i;
733 size_t cur;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000734
735 if (needed != slicelen) {
736 PyErr_Format(PyExc_ValueError,
737 "attempt to assign bytes of size %zd "
738 "to extended slice of size %zd",
739 needed, slicelen);
740 return -1;
741 }
742 for (cur = start, i = 0; i < slicelen; cur += step, i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200743 buf[cur] = bytes[i];
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000744 return 0;
745 }
746 }
747}
748
749static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000750bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000751{
752 static char *kwlist[] = {"source", "encoding", "errors", 0};
753 PyObject *arg = NULL;
754 const char *encoding = NULL;
755 const char *errors = NULL;
756 Py_ssize_t count;
757 PyObject *it;
758 PyObject *(*iternext)(PyObject *);
759
760 if (Py_SIZE(self) != 0) {
761 /* Empty previous contents (yes, do this first of all!) */
762 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
763 return -1;
764 }
765
766 /* Parse arguments */
Georg Brandl3dbca812008-07-23 16:10:53 +0000767 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000768 &arg, &encoding, &errors))
769 return -1;
770
771 /* Make a quick exit if no first argument */
772 if (arg == NULL) {
773 if (encoding != NULL || errors != NULL) {
774 PyErr_SetString(PyExc_TypeError,
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300775 encoding != NULL ?
776 "encoding without a string argument" :
777 "errors without a string argument");
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000778 return -1;
779 }
780 return 0;
781 }
782
783 if (PyUnicode_Check(arg)) {
784 /* Encode via the codec registry */
785 PyObject *encoded, *new;
786 if (encoding == NULL) {
787 PyErr_SetString(PyExc_TypeError,
788 "string argument without an encoding");
789 return -1;
790 }
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000791 encoded = PyUnicode_AsEncodedString(arg, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000792 if (encoded == NULL)
793 return -1;
794 assert(PyBytes_Check(encoded));
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000795 new = bytearray_iconcat(self, encoded);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000796 Py_DECREF(encoded);
797 if (new == NULL)
798 return -1;
799 Py_DECREF(new);
800 return 0;
801 }
802
803 /* If it's not unicode, there can't be encoding or errors */
804 if (encoding != NULL || errors != NULL) {
805 PyErr_SetString(PyExc_TypeError,
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300806 encoding != NULL ?
807 "encoding without a string argument" :
808 "errors without a string argument");
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000809 return -1;
810 }
811
812 /* Is it an int? */
Victor Stinnera15e2602020-04-08 02:01:56 +0200813 if (_PyIndex_Check(arg)) {
Serhiy Storchakaeb249882016-08-15 09:46:07 +0300814 count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
815 if (count == -1 && PyErr_Occurred()) {
Serhiy Storchakae8904212018-10-15 00:02:57 +0300816 if (!PyErr_ExceptionMatches(PyExc_TypeError))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000817 return -1;
INADA Naokia634e232017-01-06 17:32:01 +0900818 PyErr_Clear(); /* fall through */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000819 }
INADA Naokia634e232017-01-06 17:32:01 +0900820 else {
821 if (count < 0) {
822 PyErr_SetString(PyExc_ValueError, "negative count");
823 return -1;
824 }
825 if (count > 0) {
826 if (PyByteArray_Resize((PyObject *)self, count))
827 return -1;
828 memset(PyByteArray_AS_STRING(self), 0, count);
829 }
830 return 0;
831 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000832 }
833
834 /* Use the buffer API */
835 if (PyObject_CheckBuffer(arg)) {
836 Py_ssize_t size;
837 Py_buffer view;
838 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
839 return -1;
840 size = view.len;
841 if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200842 if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self),
843 &view, size, 'C') < 0)
Stefan Krah7d12d9d2012-07-28 12:25:55 +0200844 goto fail;
Martin v. Löwis423be952008-08-13 15:53:07 +0000845 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000846 return 0;
847 fail:
Martin v. Löwis423be952008-08-13 15:53:07 +0000848 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000849 return -1;
850 }
851
852 /* XXX Optimize this if the arguments is a list, tuple */
853
854 /* Get the iterator */
855 it = PyObject_GetIter(arg);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300856 if (it == NULL) {
857 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
858 PyErr_Format(PyExc_TypeError,
859 "cannot convert '%.200s' object to bytearray",
Victor Stinner58ac7002020-02-07 03:04:21 +0100860 Py_TYPE(arg)->tp_name);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300861 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000862 return -1;
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300863 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000864 iternext = *Py_TYPE(it)->tp_iternext;
865
866 /* Run the iterator to exhaustion */
867 for (;;) {
868 PyObject *item;
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000869 int rc, value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000870
871 /* Get the next item */
872 item = iternext(it);
873 if (item == NULL) {
874 if (PyErr_Occurred()) {
875 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
876 goto error;
877 PyErr_Clear();
878 }
879 break;
880 }
881
882 /* Interpret it as an int (__index__) */
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000883 rc = _getbytevalue(item, &value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000884 Py_DECREF(item);
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000885 if (!rc)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000886 goto error;
887
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000888 /* Append the byte */
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300889 if (Py_SIZE(self) + 1 < self->ob_alloc) {
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100890 Py_SET_SIZE(self, Py_SIZE(self) + 1);
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300891 PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0';
892 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000893 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
894 goto error;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200895 PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000896 }
897
898 /* Clean up and return success */
899 Py_DECREF(it);
900 return 0;
901
902 error:
903 /* Error handling when it != NULL */
904 Py_DECREF(it);
905 return -1;
906}
907
908/* Mostly copied from string_repr, but without the
909 "smart quote" functionality. */
910static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000911bytearray_repr(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000912{
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300913 const char *className = _PyType_Name(Py_TYPE(self));
914 const char *quote_prefix = "(b";
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000915 const char *quote_postfix = ")";
916 Py_ssize_t length = Py_SIZE(self);
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300917 /* 6 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
918 Py_ssize_t newsize;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000919 PyObject *v;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200920 Py_ssize_t i;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200921 char *bytes;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200922 char c;
923 char *p;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200924 int quote;
925 char *test, *start;
926 char *buffer;
927
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300928 newsize = strlen(className);
929 if (length > (PY_SSIZE_T_MAX - 6 - newsize) / 4) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000930 PyErr_SetString(PyExc_OverflowError,
931 "bytearray object is too large to make repr");
932 return NULL;
933 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200934
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300935 newsize += 6 + length * 4;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100936 buffer = PyObject_Malloc(newsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200937 if (buffer == NULL) {
938 PyErr_NoMemory();
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000939 return NULL;
940 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000941
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200942 /* Figure out which quote to use; single is preferred */
943 quote = '\'';
944 start = PyByteArray_AS_STRING(self);
945 for (test = start; test < start+length; ++test) {
946 if (*test == '"') {
947 quote = '\''; /* back to single */
948 break;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000949 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200950 else if (*test == '\'')
951 quote = '"';
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000952 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200953
954 p = buffer;
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300955 while (*className)
956 *p++ = *className++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200957 while (*quote_prefix)
958 *p++ = *quote_prefix++;
959 *p++ = quote;
960
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200961 bytes = PyByteArray_AS_STRING(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200962 for (i = 0; i < length; i++) {
963 /* There's at least enough room for a hex escape
964 and a closing quote. */
965 assert(newsize - (p - buffer) >= 5);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200966 c = bytes[i];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200967 if (c == '\'' || c == '\\')
968 *p++ = '\\', *p++ = c;
969 else if (c == '\t')
970 *p++ = '\\', *p++ = 't';
971 else if (c == '\n')
972 *p++ = '\\', *p++ = 'n';
973 else if (c == '\r')
974 *p++ = '\\', *p++ = 'r';
975 else if (c == 0)
976 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
977 else if (c < ' ' || c >= 0x7f) {
978 *p++ = '\\';
979 *p++ = 'x';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200980 *p++ = Py_hexdigits[(c & 0xf0) >> 4];
981 *p++ = Py_hexdigits[c & 0xf];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200982 }
983 else
984 *p++ = c;
985 }
986 assert(newsize - (p - buffer) >= 1);
987 *p++ = quote;
988 while (*quote_postfix) {
989 *p++ = *quote_postfix++;
990 }
991
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300992 v = PyUnicode_FromStringAndSize(buffer, p - buffer);
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100993 PyObject_Free(buffer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200994 return v;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000995}
996
997static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000998bytearray_str(PyObject *op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000999{
Victor Stinner331a6a52019-05-27 16:39:22 +02001000 PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;
Victor Stinnerc96be812019-05-14 17:34:56 +02001001 if (config->bytes_warning) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001002 if (PyErr_WarnEx(PyExc_BytesWarning,
1003 "str() on a bytearray instance", 1)) {
1004 return NULL;
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001005 }
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001006 }
1007 return bytearray_repr((PyByteArrayObject*)op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001008}
1009
1010static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001011bytearray_richcompare(PyObject *self, PyObject *other, int op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001012{
1013 Py_ssize_t self_size, other_size;
1014 Py_buffer self_bytes, other_bytes;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001015 int cmp, rc;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001016
1017 /* Bytes can be compared to anything that supports the (binary)
1018 buffer API. Except that a comparison with Unicode is always an
1019 error, even if the comparison is for equality. */
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001020 rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type);
1021 if (!rc)
1022 rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type);
1023 if (rc < 0)
1024 return NULL;
1025 if (rc) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001026 PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;
Victor Stinnerc96be812019-05-14 17:34:56 +02001027 if (config->bytes_warning && (op == Py_EQ || op == Py_NE)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001028 if (PyErr_WarnEx(PyExc_BytesWarning,
Georg Brandle5d68ac2008-06-04 11:30:26 +00001029 "Comparison between bytearray and string", 1))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001030 return NULL;
1031 }
1032
Brian Curtindfc80e32011-08-10 20:28:54 -05001033 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001034 }
1035
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001036 if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001037 PyErr_Clear();
Brian Curtindfc80e32011-08-10 20:28:54 -05001038 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001039 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001040 self_size = self_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001041
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001042 if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001043 PyErr_Clear();
Martin v. Löwis423be952008-08-13 15:53:07 +00001044 PyBuffer_Release(&self_bytes);
Brian Curtindfc80e32011-08-10 20:28:54 -05001045 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001046 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001047 other_size = other_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001048
1049 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
1050 /* Shortcut: if the lengths differ, the objects differ */
stratakise8b19652017-11-02 11:32:54 +01001051 PyBuffer_Release(&self_bytes);
1052 PyBuffer_Release(&other_bytes);
1053 return PyBool_FromLong((op == Py_NE));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001054 }
1055 else {
stratakise8b19652017-11-02 11:32:54 +01001056 cmp = memcmp(self_bytes.buf, other_bytes.buf,
1057 Py_MIN(self_size, other_size));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001058 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
1059
stratakise8b19652017-11-02 11:32:54 +01001060 PyBuffer_Release(&self_bytes);
1061 PyBuffer_Release(&other_bytes);
1062
1063 if (cmp != 0) {
1064 Py_RETURN_RICHCOMPARE(cmp, 0, op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001065 }
1066
stratakise8b19652017-11-02 11:32:54 +01001067 Py_RETURN_RICHCOMPARE(self_size, other_size, op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001068 }
1069
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001070}
1071
1072static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001073bytearray_dealloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001074{
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001075 if (self->ob_exports > 0) {
1076 PyErr_SetString(PyExc_SystemError,
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001077 "deallocated bytearray object has exported buffers");
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001078 PyErr_Print();
1079 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001080 if (self->ob_bytes != 0) {
Antoine Pitrou39aba4f2011-11-12 21:15:28 +01001081 PyObject_Free(self->ob_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001082 }
1083 Py_TYPE(self)->tp_free((PyObject *)self);
1084}
1085
1086
1087/* -------------------------------------------------------------------- */
1088/* Methods */
1089
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001090#define FASTSEARCH fastsearch
1091#define STRINGLIB(F) stringlib_##F
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001092#define STRINGLIB_CHAR char
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001093#define STRINGLIB_SIZEOF_CHAR 1
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001094#define STRINGLIB_LEN PyByteArray_GET_SIZE
1095#define STRINGLIB_STR PyByteArray_AS_STRING
1096#define STRINGLIB_NEW PyByteArray_FromStringAndSize
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001097#define STRINGLIB_ISSPACE Py_ISSPACE
1098#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001099#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1100#define STRINGLIB_MUTABLE 1
1101
1102#include "stringlib/fastsearch.h"
1103#include "stringlib/count.h"
1104#include "stringlib/find.h"
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001105#include "stringlib/join.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001106#include "stringlib/partition.h"
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001107#include "stringlib/split.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001108#include "stringlib/ctype.h"
1109#include "stringlib/transmogrify.h"
1110
1111
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001112static PyObject *
1113bytearray_find(PyByteArrayObject *self, PyObject *args)
1114{
1115 return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1116}
1117
1118static PyObject *
1119bytearray_count(PyByteArrayObject *self, PyObject *args)
1120{
1121 return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1122}
1123
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001124/*[clinic input]
1125bytearray.clear
1126
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001127Remove all items from the bytearray.
1128[clinic start generated code]*/
1129
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001130static PyObject *
1131bytearray_clear_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001132/*[clinic end generated code: output=85c2fe6aede0956c input=ed6edae9de447ac4]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001133{
1134 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1135 return NULL;
1136 Py_RETURN_NONE;
1137}
1138
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001139/*[clinic input]
1140bytearray.copy
1141
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001142Return a copy of B.
1143[clinic start generated code]*/
1144
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001145static PyObject *
1146bytearray_copy_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001147/*[clinic end generated code: output=68cfbcfed484c132 input=6597b0c01bccaa9e]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001148{
1149 return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self),
1150 PyByteArray_GET_SIZE(self));
1151}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001152
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001153static PyObject *
1154bytearray_index(PyByteArrayObject *self, PyObject *args)
1155{
1156 return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1157}
1158
1159static PyObject *
1160bytearray_rfind(PyByteArrayObject *self, PyObject *args)
1161{
1162 return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1163}
1164
1165static PyObject *
1166bytearray_rindex(PyByteArrayObject *self, PyObject *args)
1167{
1168 return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1169}
1170
1171static int
1172bytearray_contains(PyObject *self, PyObject *arg)
1173{
1174 return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg);
1175}
1176
1177static PyObject *
1178bytearray_startswith(PyByteArrayObject *self, PyObject *args)
1179{
1180 return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1181}
1182
1183static PyObject *
1184bytearray_endswith(PyByteArrayObject *self, PyObject *args)
1185{
1186 return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1187}
1188
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001189
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001190/*[clinic input]
1191bytearray.translate
1192
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001193 table: object
1194 Translation table, which must be a bytes object of length 256.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001195 /
Martin Panter1b6c6da2016-08-27 08:35:02 +00001196 delete as deletechars: object(c_default="NULL") = b''
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001197
1198Return a copy with each character mapped by the given translation table.
1199
Martin Panter1b6c6da2016-08-27 08:35:02 +00001200All characters occurring in the optional argument delete are removed.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001201The remaining characters are mapped through the given translation table.
1202[clinic start generated code]*/
1203
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001204static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001205bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
Martin Panter1b6c6da2016-08-27 08:35:02 +00001206 PyObject *deletechars)
1207/*[clinic end generated code: output=b6a8f01c2a74e446 input=cfff956d4d127a9b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001208{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001209 char *input, *output;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001210 const char *table_chars;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001211 Py_ssize_t i, c;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001212 PyObject *input_obj = (PyObject*)self;
1213 const char *output_start;
1214 Py_ssize_t inlen;
Georg Brandlccc47b62008-12-28 11:44:14 +00001215 PyObject *result = NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001216 int trans_table[256];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001217 Py_buffer vtable, vdel;
1218
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001219 if (table == Py_None) {
1220 table_chars = NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001221 table = NULL;
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001222 } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001223 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001224 } else {
1225 if (vtable.len != 256) {
1226 PyErr_SetString(PyExc_ValueError,
1227 "translation table must be 256 characters long");
Georg Brandl953152f2009-07-22 12:03:59 +00001228 PyBuffer_Release(&vtable);
1229 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001230 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001231 table_chars = (const char*)vtable.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001232 }
1233
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001234 if (deletechars != NULL) {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001235 if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) {
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001236 if (table != NULL)
Georg Brandl953152f2009-07-22 12:03:59 +00001237 PyBuffer_Release(&vtable);
1238 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001239 }
1240 }
1241 else {
1242 vdel.buf = NULL;
1243 vdel.len = 0;
1244 }
1245
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001246 inlen = PyByteArray_GET_SIZE(input_obj);
1247 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1248 if (result == NULL)
1249 goto done;
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03001250 output_start = output = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001251 input = PyByteArray_AS_STRING(input_obj);
1252
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001253 if (vdel.len == 0 && table_chars != NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001254 /* If no deletions are required, use faster code */
1255 for (i = inlen; --i >= 0; ) {
1256 c = Py_CHARMASK(*input++);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001257 *output++ = table_chars[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001258 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001259 goto done;
1260 }
Georg Brandlccc47b62008-12-28 11:44:14 +00001261
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001262 if (table_chars == NULL) {
Georg Brandlccc47b62008-12-28 11:44:14 +00001263 for (i = 0; i < 256; i++)
1264 trans_table[i] = Py_CHARMASK(i);
1265 } else {
1266 for (i = 0; i < 256; i++)
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001267 trans_table[i] = Py_CHARMASK(table_chars[i]);
Georg Brandlccc47b62008-12-28 11:44:14 +00001268 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001269
1270 for (i = 0; i < vdel.len; i++)
1271 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1272
1273 for (i = inlen; --i >= 0; ) {
1274 c = Py_CHARMASK(*input++);
1275 if (trans_table[c] != -1)
Martin Panter1b6c6da2016-08-27 08:35:02 +00001276 *output++ = (char)trans_table[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001277 }
1278 /* Fix the size of the resulting string */
1279 if (inlen > 0)
Christian Heimesc731bbe2013-07-21 02:04:35 +02001280 if (PyByteArray_Resize(result, output - output_start) < 0) {
1281 Py_CLEAR(result);
1282 goto done;
1283 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001284
1285done:
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001286 if (table != NULL)
Georg Brandlccc47b62008-12-28 11:44:14 +00001287 PyBuffer_Release(&vtable);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001288 if (deletechars != NULL)
Martin v. Löwis423be952008-08-13 15:53:07 +00001289 PyBuffer_Release(&vdel);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001290 return result;
1291}
1292
1293
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001294/*[clinic input]
1295
1296@staticmethod
1297bytearray.maketrans
1298
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001299 frm: Py_buffer
1300 to: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001301 /
1302
1303Return a translation table useable for the bytes or bytearray translate method.
1304
1305The returned table will be one where each byte in frm is mapped to the byte at
1306the same position in to.
1307
1308The bytes objects frm and to must be of the same length.
1309[clinic start generated code]*/
1310
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001311static PyObject *
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001312bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001313/*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001314{
1315 return _Py_bytes_maketrans(frm, to);
Georg Brandlabc38772009-04-12 15:51:51 +00001316}
1317
1318
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001319/*[clinic input]
1320bytearray.replace
1321
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001322 old: Py_buffer
1323 new: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001324 count: Py_ssize_t = -1
1325 Maximum number of occurrences to replace.
1326 -1 (the default value) means replace all occurrences.
1327 /
1328
1329Return a copy with all occurrences of substring old replaced by new.
1330
1331If the optional argument count is given, only the first count occurrences are
1332replaced.
1333[clinic start generated code]*/
1334
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001335static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001336bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
1337 Py_buffer *new, Py_ssize_t count)
1338/*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001339{
Serhiy Storchakafb81d3c2016-05-05 09:26:07 +03001340 return stringlib_replace((PyObject *)self,
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001341 (const char *)old->buf, old->len,
1342 (const char *)new->buf, new->len, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001343}
1344
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001345/*[clinic input]
1346bytearray.split
1347
1348 sep: object = None
1349 The delimiter according which to split the bytearray.
1350 None (the default value) means split on ASCII whitespace characters
1351 (space, tab, return, newline, formfeed, vertical tab).
1352 maxsplit: Py_ssize_t = -1
1353 Maximum number of splits to do.
1354 -1 (the default value) means no limit.
1355
1356Return a list of the sections in the bytearray, using sep as the delimiter.
1357[clinic start generated code]*/
1358
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001359static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001360bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
1361 Py_ssize_t maxsplit)
1362/*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001363{
1364 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001365 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001366 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001367 Py_buffer vsub;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001368
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001369 if (maxsplit < 0)
1370 maxsplit = PY_SSIZE_T_MAX;
1371
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001372 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001373 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001374
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001375 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001376 return NULL;
1377 sub = vsub.buf;
1378 n = vsub.len;
1379
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001380 list = stringlib_split(
1381 (PyObject*) self, s, len, sub, n, maxsplit
1382 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001383 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001384 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001385}
1386
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001387/*[clinic input]
1388bytearray.partition
1389
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001390 sep: object
1391 /
1392
1393Partition the bytearray into three parts using the given separator.
1394
1395This will search for the separator sep in the bytearray. If the separator is
1396found, returns a 3-tuple containing the part before the separator, the
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001397separator itself, and the part after it as new bytearray objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001398
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001399If the separator is not found, returns a 3-tuple containing the copy of the
1400original bytearray object and two empty bytearray objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001401[clinic start generated code]*/
1402
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001403static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001404bytearray_partition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001405/*[clinic end generated code: output=45d2525ddd35f957 input=8f644749ee4fc83a]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001406{
1407 PyObject *bytesep, *result;
1408
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001409 bytesep = _PyByteArray_FromBufferObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001410 if (! bytesep)
1411 return NULL;
1412
1413 result = stringlib_partition(
1414 (PyObject*) self,
1415 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1416 bytesep,
1417 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1418 );
1419
1420 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001421 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001422}
1423
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001424/*[clinic input]
1425bytearray.rpartition
1426
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001427 sep: object
1428 /
1429
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001430Partition the bytearray into three parts using the given separator.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001431
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001432This will search for the separator sep in the bytearray, starting at the end.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001433If the separator is found, returns a 3-tuple containing the part before the
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001434separator, the separator itself, and the part after it as new bytearray
1435objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001436
1437If the separator is not found, returns a 3-tuple containing two empty bytearray
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001438objects and the copy of the original bytearray object.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001439[clinic start generated code]*/
1440
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001441static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001442bytearray_rpartition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001443/*[clinic end generated code: output=440de3c9426115e8 input=7e3df3e6cb8fa0ac]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001444{
1445 PyObject *bytesep, *result;
1446
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001447 bytesep = _PyByteArray_FromBufferObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001448 if (! bytesep)
1449 return NULL;
1450
1451 result = stringlib_rpartition(
1452 (PyObject*) self,
1453 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1454 bytesep,
1455 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1456 );
1457
1458 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001459 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001460}
1461
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001462/*[clinic input]
1463bytearray.rsplit = bytearray.split
1464
1465Return a list of the sections in the bytearray, using sep as the delimiter.
1466
1467Splitting is done starting at the end of the bytearray and working to the front.
1468[clinic start generated code]*/
1469
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001470static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001471bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
1472 Py_ssize_t maxsplit)
1473/*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001474{
1475 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001476 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001477 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001478 Py_buffer vsub;
1479
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001480 if (maxsplit < 0)
1481 maxsplit = PY_SSIZE_T_MAX;
1482
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001483 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001484 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001485
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001486 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001487 return NULL;
1488 sub = vsub.buf;
1489 n = vsub.len;
1490
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001491 list = stringlib_rsplit(
1492 (PyObject*) self, s, len, sub, n, maxsplit
1493 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001494 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001495 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001496}
1497
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001498/*[clinic input]
1499bytearray.reverse
1500
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001501Reverse the order of the values in B in place.
1502[clinic start generated code]*/
1503
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001504static PyObject *
1505bytearray_reverse_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001506/*[clinic end generated code: output=9f7616f29ab309d3 input=543356319fc78557]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001507{
1508 char swap, *head, *tail;
1509 Py_ssize_t i, j, n = Py_SIZE(self);
1510
1511 j = n / 2;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001512 head = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001513 tail = head + n - 1;
1514 for (i = 0; i < j; i++) {
1515 swap = *head;
1516 *head++ = *tail;
1517 *tail-- = swap;
1518 }
1519
1520 Py_RETURN_NONE;
1521}
1522
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001523
1524/*[python input]
1525class bytesvalue_converter(CConverter):
1526 type = 'int'
1527 converter = '_getbytevalue'
1528[python start generated code]*/
1529/*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/
1530
1531
1532/*[clinic input]
1533bytearray.insert
1534
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001535 index: Py_ssize_t
1536 The index where the value is to be inserted.
1537 item: bytesvalue
1538 The item to be inserted.
1539 /
1540
1541Insert a single item into the bytearray before the given index.
1542[clinic start generated code]*/
1543
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001544static PyObject *
1545bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001546/*[clinic end generated code: output=76c775a70e7b07b7 input=b2b5d07e9de6c070]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001547{
1548 Py_ssize_t n = Py_SIZE(self);
1549 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001550
1551 if (n == PY_SSIZE_T_MAX) {
1552 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001553 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001554 return NULL;
1555 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001556 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1557 return NULL;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001558 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001559
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001560 if (index < 0) {
1561 index += n;
1562 if (index < 0)
1563 index = 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001564 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001565 if (index > n)
1566 index = n;
1567 memmove(buf + index + 1, buf + index, n - index);
1568 buf[index] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001569
1570 Py_RETURN_NONE;
1571}
1572
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001573/*[clinic input]
1574bytearray.append
1575
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001576 item: bytesvalue
1577 The item to be appended.
1578 /
1579
1580Append a single item to the end of the bytearray.
1581[clinic start generated code]*/
1582
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001583static PyObject *
1584bytearray_append_impl(PyByteArrayObject *self, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001585/*[clinic end generated code: output=a154e19ed1886cb6 input=20d6bec3d1340593]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001586{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001587 Py_ssize_t n = Py_SIZE(self);
1588
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001589 if (n == PY_SSIZE_T_MAX) {
1590 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001591 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001592 return NULL;
1593 }
1594 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1595 return NULL;
1596
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001597 PyByteArray_AS_STRING(self)[n] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001598
1599 Py_RETURN_NONE;
1600}
1601
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001602/*[clinic input]
1603bytearray.extend
1604
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001605 iterable_of_ints: object
1606 The iterable of items to append.
1607 /
1608
1609Append all the items from the iterator or sequence to the end of the bytearray.
1610[clinic start generated code]*/
1611
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001612static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001613bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001614/*[clinic end generated code: output=98155dbe249170b1 input=c617b3a93249ba28]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001615{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001616 PyObject *it, *item, *bytearray_obj;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001617 Py_ssize_t buf_size = 0, len = 0;
1618 int value;
1619 char *buf;
1620
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001621 /* bytearray_setslice code only accepts something supporting PEP 3118. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001622 if (PyObject_CheckBuffer(iterable_of_ints)) {
1623 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_ints) == -1)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001624 return NULL;
1625
1626 Py_RETURN_NONE;
1627 }
1628
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001629 it = PyObject_GetIter(iterable_of_ints);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +03001630 if (it == NULL) {
1631 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
1632 PyErr_Format(PyExc_TypeError,
1633 "can't extend bytearray with %.100s",
Victor Stinner58ac7002020-02-07 03:04:21 +01001634 Py_TYPE(iterable_of_ints)->tp_name);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +03001635 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001636 return NULL;
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +03001637 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001638
Ezio Melotti42da6632011-03-15 05:18:48 +02001639 /* Try to determine the length of the argument. 32 is arbitrary. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001640 buf_size = PyObject_LengthHint(iterable_of_ints, 32);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001641 if (buf_size == -1) {
1642 Py_DECREF(it);
1643 return NULL;
1644 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001645
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001646 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001647 if (bytearray_obj == NULL) {
1648 Py_DECREF(it);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001649 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001650 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001651 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001652
1653 while ((item = PyIter_Next(it)) != NULL) {
1654 if (! _getbytevalue(item, &value)) {
1655 Py_DECREF(item);
1656 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001657 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001658 return NULL;
1659 }
1660 buf[len++] = value;
1661 Py_DECREF(item);
1662
1663 if (len >= buf_size) {
Martin Panter371731e2016-07-18 07:53:13 +00001664 Py_ssize_t addition;
1665 if (len == PY_SSIZE_T_MAX) {
1666 Py_DECREF(it);
1667 Py_DECREF(bytearray_obj);
1668 return PyErr_NoMemory();
1669 }
1670 addition = len >> 1;
1671 if (addition > PY_SSIZE_T_MAX - len - 1)
1672 buf_size = PY_SSIZE_T_MAX;
1673 else
1674 buf_size = len + addition + 1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001675 if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001676 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001677 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001678 return NULL;
1679 }
1680 /* Recompute the `buf' pointer, since the resizing operation may
1681 have invalidated it. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001682 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001683 }
1684 }
1685 Py_DECREF(it);
1686
1687 /* Resize down to exact size. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001688 if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) {
1689 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001690 return NULL;
1691 }
1692
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001693 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) {
1694 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001695 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001696 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001697 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001698
Brandt Bucher2a7d5962019-06-26 12:06:18 -07001699 if (PyErr_Occurred()) {
1700 return NULL;
1701 }
1702
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001703 Py_RETURN_NONE;
1704}
1705
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001706/*[clinic input]
1707bytearray.pop
1708
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001709 index: Py_ssize_t = -1
1710 The index from where to remove the item.
1711 -1 (the default value) means remove the last item.
1712 /
1713
1714Remove and return a single item from B.
1715
1716If no index argument is given, will pop the last item.
1717[clinic start generated code]*/
1718
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001719static PyObject *
1720bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001721/*[clinic end generated code: output=e0ccd401f8021da8 input=3591df2d06c0d237]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001722{
1723 int value;
1724 Py_ssize_t n = Py_SIZE(self);
1725 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001726
1727 if (n == 0) {
Eli Bendersky1bc4f192011-03-04 04:55:25 +00001728 PyErr_SetString(PyExc_IndexError,
1729 "pop from empty bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001730 return NULL;
1731 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001732 if (index < 0)
1733 index += Py_SIZE(self);
1734 if (index < 0 || index >= Py_SIZE(self)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001735 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1736 return NULL;
1737 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001738 if (!_canresize(self))
1739 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001740
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001741 buf = PyByteArray_AS_STRING(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001742 value = buf[index];
1743 memmove(buf + index, buf + index + 1, n - index);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001744 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1745 return NULL;
1746
Mark Dickinson54a3db92009-09-06 10:19:23 +00001747 return PyLong_FromLong((unsigned char)value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001748}
1749
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001750/*[clinic input]
1751bytearray.remove
1752
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001753 value: bytesvalue
1754 The value to remove.
1755 /
1756
1757Remove the first occurrence of a value in the bytearray.
1758[clinic start generated code]*/
1759
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001760static PyObject *
1761bytearray_remove_impl(PyByteArrayObject *self, int value)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001762/*[clinic end generated code: output=d659e37866709c13 input=121831240cd51ddf]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001763{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001764 Py_ssize_t where, n = Py_SIZE(self);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001765 char *buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001766
Serhiy Storchaka4b234942016-05-16 22:24:03 +03001767 where = stringlib_find_char(buf, n, value);
1768 if (where < 0) {
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001769 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001770 return NULL;
1771 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001772 if (!_canresize(self))
1773 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001774
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001775 memmove(buf + where, buf + where + 1, n - where);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001776 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1777 return NULL;
1778
1779 Py_RETURN_NONE;
1780}
1781
1782/* XXX These two helpers could be optimized if argsize == 1 */
1783
1784static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001785lstrip_helper(const char *myptr, Py_ssize_t mysize,
1786 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001787{
1788 Py_ssize_t i = 0;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001789 while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001790 i++;
1791 return i;
1792}
1793
1794static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001795rstrip_helper(const char *myptr, Py_ssize_t mysize,
1796 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001797{
1798 Py_ssize_t i = mysize - 1;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001799 while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001800 i--;
1801 return i + 1;
1802}
1803
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001804/*[clinic input]
1805bytearray.strip
1806
1807 bytes: object = None
1808 /
1809
1810Strip leading and trailing bytes contained in the argument.
1811
1812If the argument is omitted or None, strip leading and trailing ASCII whitespace.
1813[clinic start generated code]*/
1814
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001815static PyObject *
1816bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001817/*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001818{
1819 Py_ssize_t left, right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001820 char *myptr;
1821 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001822 Py_buffer vbytes;
1823
1824 if (bytes == Py_None) {
1825 bytesptr = "\t\n\r\f\v ";
1826 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001827 }
1828 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001829 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001830 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001831 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001832 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001833 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001834 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001835 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001836 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001837 if (left == mysize)
1838 right = left;
1839 else
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001840 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1841 if (bytes != Py_None)
1842 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001843 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001844}
1845
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001846/*[clinic input]
1847bytearray.lstrip
1848
1849 bytes: object = None
1850 /
1851
1852Strip leading bytes contained in the argument.
1853
1854If the argument is omitted or None, strip leading ASCII whitespace.
1855[clinic start generated code]*/
1856
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001857static PyObject *
1858bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001859/*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001860{
1861 Py_ssize_t left, right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001862 char *myptr;
1863 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001864 Py_buffer vbytes;
1865
1866 if (bytes == Py_None) {
1867 bytesptr = "\t\n\r\f\v ";
1868 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001869 }
1870 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001871 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001872 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001873 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001874 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001875 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001876 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001877 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001878 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001879 right = mysize;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001880 if (bytes != Py_None)
1881 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001882 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001883}
1884
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001885/*[clinic input]
1886bytearray.rstrip
1887
1888 bytes: object = None
1889 /
1890
1891Strip trailing bytes contained in the argument.
1892
1893If the argument is omitted or None, strip trailing ASCII whitespace.
1894[clinic start generated code]*/
1895
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001896static PyObject *
1897bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001898/*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001899{
1900 Py_ssize_t right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001901 char *myptr;
1902 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001903 Py_buffer vbytes;
1904
1905 if (bytes == Py_None) {
1906 bytesptr = "\t\n\r\f\v ";
1907 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001908 }
1909 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001910 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001911 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001912 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001913 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001914 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001915 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001916 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001917 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1918 if (bytes != Py_None)
1919 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001920 return PyByteArray_FromStringAndSize(myptr, right);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001921}
1922
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001923/*[clinic input]
1924bytearray.decode
1925
1926 encoding: str(c_default="NULL") = 'utf-8'
1927 The encoding with which to decode the bytearray.
1928 errors: str(c_default="NULL") = 'strict'
1929 The error handling scheme to use for the handling of decoding errors.
1930 The default is 'strict' meaning that decoding errors raise a
1931 UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
1932 as well as any other name registered with codecs.register_error that
1933 can handle UnicodeDecodeErrors.
1934
1935Decode the bytearray using the codec registered for encoding.
1936[clinic start generated code]*/
1937
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001938static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001939bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
1940 const char *errors)
1941/*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001942{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001943 if (encoding == NULL)
1944 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis0efea322014-07-27 17:29:17 +02001945 return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001946}
1947
1948PyDoc_STRVAR(alloc_doc,
1949"B.__alloc__() -> int\n\
1950\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00001951Return the number of bytes actually allocated.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001952
1953static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301954bytearray_alloc(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001955{
1956 return PyLong_FromSsize_t(self->ob_alloc);
1957}
1958
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001959/*[clinic input]
1960bytearray.join
1961
1962 iterable_of_bytes: object
1963 /
1964
1965Concatenate any number of bytes/bytearray objects.
1966
1967The bytearray whose method is called is inserted in between each pair.
1968
1969The result is returned as a new bytearray object.
1970[clinic start generated code]*/
1971
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001972static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001973bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001974/*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001975{
Martin v. Löwis0efea322014-07-27 17:29:17 +02001976 return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001977}
1978
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001979/*[clinic input]
1980bytearray.splitlines
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001981
Serhiy Storchaka202fda52017-03-12 10:10:47 +02001982 keepends: bool(accept={int}) = False
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001983
1984Return a list of the lines in the bytearray, breaking at line boundaries.
1985
1986Line breaks are not included in the resulting list unless keepends is given and
1987true.
1988[clinic start generated code]*/
1989
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001990static PyObject *
1991bytearray_splitlines_impl(PyByteArrayObject *self, int keepends)
Serhiy Storchaka202fda52017-03-12 10:10:47 +02001992/*[clinic end generated code: output=4223c94b895f6ad9 input=99a27ad959b9cf6b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001993{
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001994 return stringlib_splitlines(
1995 (PyObject*) self, PyByteArray_AS_STRING(self),
1996 PyByteArray_GET_SIZE(self), keepends
1997 );
1998}
1999
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002000/*[clinic input]
2001@classmethod
2002bytearray.fromhex
2003
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002004 string: unicode
2005 /
2006
2007Create a bytearray object from a string of hexadecimal numbers.
2008
2009Spaces between two numbers are accepted.
2010Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')
2011[clinic start generated code]*/
2012
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002013static PyObject *
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002014bytearray_fromhex_impl(PyTypeObject *type, PyObject *string)
2015/*[clinic end generated code: output=8f0f0b6d30fb3ba0 input=f033a16d1fb21f48]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002016{
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002017 PyObject *result = _PyBytes_FromHex(string, type == &PyByteArray_Type);
2018 if (type != &PyByteArray_Type && result != NULL) {
Petr Viktorinffd97532020-02-11 17:46:57 +01002019 Py_SETREF(result, PyObject_CallOneArg((PyObject *)type, result));
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002020 }
2021 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002022}
2023
Gregory P. Smith0c2f9302019-05-29 11:46:58 -07002024/*[clinic input]
2025bytearray.hex
2026
2027 sep: object = NULL
2028 An optional single character or byte to separate hex bytes.
2029 bytes_per_sep: int = 1
2030 How many bytes between separators. Positive values count from the
2031 right, negative values count from the left.
2032
2033Create a str of hexadecimal numbers from a bytearray object.
2034
2035Example:
2036>>> value = bytearray([0xb9, 0x01, 0xef])
2037>>> value.hex()
2038'b901ef'
2039>>> value.hex(':')
2040'b9:01:ef'
2041>>> value.hex(':', 2)
2042'b9:01ef'
2043>>> value.hex(':', -2)
2044'b901:ef'
2045[clinic start generated code]*/
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002046
2047static PyObject *
Gregory P. Smith0c2f9302019-05-29 11:46:58 -07002048bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep)
2049/*[clinic end generated code: output=29c4e5ef72c565a0 input=814c15830ac8c4b5]*/
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002050{
2051 char* argbuf = PyByteArray_AS_STRING(self);
2052 Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
Gregory P. Smith0c2f9302019-05-29 11:46:58 -07002053 return _Py_strhex_with_sep(argbuf, arglen, sep, bytes_per_sep);
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002054}
2055
2056static PyObject *
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002057_common_reduce(PyByteArrayObject *self, int proto)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002058{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002059 PyObject *dict;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002060 _Py_IDENTIFIER(__dict__);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002061 char *buf;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002062
Serhiy Storchaka41c57b32019-09-01 12:03:39 +03002063 if (_PyObject_LookupAttrId((PyObject *)self, &PyId___dict__, &dict) < 0) {
2064 return NULL;
2065 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002066 if (dict == NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002067 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}