blob: 8b57fb679d31dc44732f65272b6d1e0a5a762c8e [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 Stinner4a21e572020-04-15 02:35:41 +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"
Ethan Furmanb95b5612015-01-23 20:05:18 -08008#include "bytesobject.h"
Gregory P. Smith8cb65692015-04-25 23:22:26 +00009#include "pystrhex.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +000010
Martin v. Löwis7252a6e2014-07-27 16:25:09 +020011/*[clinic input]
12class bytearray "PyByteArrayObject *" "&PyByteArray_Type"
13[clinic start generated code]*/
14/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/
15
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000016char _PyByteArray_empty_string[] = "";
Christian Heimes2c9c7a52008-05-26 13:42:13 +000017
Christian Heimes2c9c7a52008-05-26 13:42:13 +000018/* end nullbytes support */
19
20/* Helpers */
21
22static int
23_getbytevalue(PyObject* arg, int *value)
24{
Serhiy Storchakae67f7db2020-06-29 22:36:41 +030025 int overflow;
26 long face_value = PyLong_AsLongAndOverflow(arg, &overflow);
Christian Heimes2c9c7a52008-05-26 13:42:13 +000027
Serhiy Storchakae67f7db2020-06-29 22:36:41 +030028 if (face_value == -1 && PyErr_Occurred()) {
29 *value = -1;
30 return 0;
Georg Brandl9a54d7c2008-07-16 23:15:30 +000031 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +000032 if (face_value < 0 || face_value >= 256) {
Serhiy Storchakae67f7db2020-06-29 22:36:41 +030033 /* this includes an overflow in converting to C long */
Georg Brandl9a54d7c2008-07-16 23:15:30 +000034 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
Mark Dickinson10de93a2010-07-09 19:25:48 +000035 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000036 return 0;
37 }
38
39 *value = face_value;
40 return 1;
41}
42
43static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +000044bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000045{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000046 void *ptr;
47 if (view == NULL) {
Stefan Krah5178d912015-02-03 16:57:21 +010048 PyErr_SetString(PyExc_BufferError,
49 "bytearray_getbuffer: view==NULL argument is obsolete");
50 return -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000051 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000052 ptr = (void *) PyByteArray_AS_STRING(obj);
Stefan Krah5178d912015-02-03 16:57:21 +010053 /* cannot fail if view != NULL and readonly == 0 */
54 (void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
55 obj->ob_exports++;
56 return 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000057}
58
59static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +000060bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000061{
62 obj->ob_exports--;
63}
64
Antoine Pitrou5504e892008-12-06 21:27:53 +000065static int
66_canresize(PyByteArrayObject *self)
67{
68 if (self->ob_exports > 0) {
69 PyErr_SetString(PyExc_BufferError,
70 "Existing exports of data: object cannot be re-sized");
71 return 0;
72 }
73 return 1;
74}
75
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030076#include "clinic/bytearrayobject.c.h"
77
Christian Heimes2c9c7a52008-05-26 13:42:13 +000078/* Direct API functions */
79
80PyObject *
81PyByteArray_FromObject(PyObject *input)
82{
Petr Viktorinffd97532020-02-11 17:46:57 +010083 return PyObject_CallOneArg((PyObject *)&PyByteArray_Type, input);
Christian Heimes2c9c7a52008-05-26 13:42:13 +000084}
85
Serhiy Storchakaa2314282017-10-29 02:11:54 +030086static PyObject *
87_PyByteArray_FromBufferObject(PyObject *obj)
88{
89 PyObject *result;
90 Py_buffer view;
91
92 if (PyObject_GetBuffer(obj, &view, PyBUF_FULL_RO) < 0) {
93 return NULL;
94 }
95 result = PyByteArray_FromStringAndSize(NULL, view.len);
96 if (result != NULL &&
97 PyBuffer_ToContiguous(PyByteArray_AS_STRING(result),
98 &view, view.len, 'C') < 0)
99 {
100 Py_CLEAR(result);
101 }
102 PyBuffer_Release(&view);
103 return result;
104}
105
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000106PyObject *
107PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
108{
109 PyByteArrayObject *new;
110 Py_ssize_t alloc;
111
112 if (size < 0) {
113 PyErr_SetString(PyExc_SystemError,
114 "Negative size passed to PyByteArray_FromStringAndSize");
115 return NULL;
116 }
117
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000118 /* Prevent buffer overflow when setting alloc to size+1. */
119 if (size == PY_SSIZE_T_MAX) {
120 return PyErr_NoMemory();
121 }
122
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000123 new = PyObject_New(PyByteArrayObject, &PyByteArray_Type);
124 if (new == NULL)
125 return NULL;
126
127 if (size == 0) {
128 new->ob_bytes = NULL;
129 alloc = 0;
130 }
131 else {
132 alloc = size + 1;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100133 new->ob_bytes = PyObject_Malloc(alloc);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000134 if (new->ob_bytes == NULL) {
135 Py_DECREF(new);
136 return PyErr_NoMemory();
137 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +0000138 if (bytes != NULL && size > 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000139 memcpy(new->ob_bytes, bytes, size);
140 new->ob_bytes[size] = '\0'; /* Trailing null byte */
141 }
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100142 Py_SET_SIZE(new, size);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000143 new->ob_alloc = alloc;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200144 new->ob_start = new->ob_bytes;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000145 new->ob_exports = 0;
146
147 return (PyObject *)new;
148}
149
150Py_ssize_t
151PyByteArray_Size(PyObject *self)
152{
153 assert(self != NULL);
154 assert(PyByteArray_Check(self));
155
156 return PyByteArray_GET_SIZE(self);
157}
158
159char *
160PyByteArray_AsString(PyObject *self)
161{
162 assert(self != NULL);
163 assert(PyByteArray_Check(self));
164
165 return PyByteArray_AS_STRING(self);
166}
167
168int
Antoine Pitroucc231542014-11-02 18:40:09 +0100169PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000170{
171 void *sval;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200172 PyByteArrayObject *obj = ((PyByteArrayObject *)self);
Antoine Pitroucc231542014-11-02 18:40:09 +0100173 /* All computations are done unsigned to avoid integer overflows
174 (see issue #22335). */
175 size_t alloc = (size_t) obj->ob_alloc;
176 size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes);
177 size_t size = (size_t) requested_size;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000178
179 assert(self != NULL);
180 assert(PyByteArray_Check(self));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200181 assert(logical_offset <= alloc);
Antoine Pitroucc231542014-11-02 18:40:09 +0100182 assert(requested_size >= 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000183
Antoine Pitroucc231542014-11-02 18:40:09 +0100184 if (requested_size == Py_SIZE(self)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000185 return 0;
186 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200187 if (!_canresize(obj)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000188 return -1;
189 }
190
Antoine Pitrou25454112015-05-19 20:52:27 +0200191 if (size + logical_offset + 1 <= alloc) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200192 /* Current buffer is large enough to host the requested size,
193 decide on a strategy. */
194 if (size < alloc / 2) {
195 /* Major downsize; resize down to exact size */
196 alloc = size + 1;
197 }
198 else {
199 /* Minor downsize; quick exit */
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100200 Py_SET_SIZE(self, size);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200201 PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */
202 return 0;
203 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000204 }
205 else {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200206 /* Need growing, decide on a strategy */
207 if (size <= alloc * 1.125) {
208 /* Moderate upsize; overallocate similar to list_resize() */
209 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
210 }
211 else {
212 /* Major upsize; resize up to exact size */
213 alloc = size + 1;
214 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000215 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100216 if (alloc > PY_SSIZE_T_MAX) {
217 PyErr_NoMemory();
218 return -1;
219 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000220
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200221 if (logical_offset > 0) {
222 sval = PyObject_Malloc(alloc);
223 if (sval == NULL) {
224 PyErr_NoMemory();
225 return -1;
226 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100227 memcpy(sval, PyByteArray_AS_STRING(self),
Stefan Krahdce65022017-08-25 20:12:05 +0200228 Py_MIN((size_t)requested_size, (size_t)Py_SIZE(self)));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200229 PyObject_Free(obj->ob_bytes);
230 }
231 else {
232 sval = PyObject_Realloc(obj->ob_bytes, alloc);
233 if (sval == NULL) {
234 PyErr_NoMemory();
235 return -1;
236 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000237 }
238
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200239 obj->ob_bytes = obj->ob_start = sval;
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100240 Py_SET_SIZE(self, size);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200241 obj->ob_alloc = alloc;
242 obj->ob_bytes[size] = '\0'; /* Trailing null byte */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000243
244 return 0;
245}
246
247PyObject *
248PyByteArray_Concat(PyObject *a, PyObject *b)
249{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000250 Py_buffer va, vb;
251 PyByteArrayObject *result = NULL;
252
253 va.len = -1;
254 vb.len = -1;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200255 if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 ||
256 PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000257 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
Serhiy Storchaka6b5a9ec2017-03-19 19:47:02 +0200258 Py_TYPE(b)->tp_name, Py_TYPE(a)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000259 goto done;
260 }
261
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300262 if (va.len > PY_SSIZE_T_MAX - vb.len) {
263 PyErr_NoMemory();
264 goto done;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000265 }
266
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300267 result = (PyByteArrayObject *) \
268 PyByteArray_FromStringAndSize(NULL, va.len + vb.len);
stratakis61fc23c2020-07-08 22:39:41 +0200269 // result->ob_bytes is NULL if result is an empty string:
270 // if va.len + vb.len equals zero.
271 if (result != NULL && result->ob_bytes != NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000272 memcpy(result->ob_bytes, va.buf, va.len);
273 memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
274 }
275
276 done:
277 if (va.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000278 PyBuffer_Release(&va);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000279 if (vb.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000280 PyBuffer_Release(&vb);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000281 return (PyObject *)result;
282}
283
284/* Functions stuffed into the type object */
285
286static Py_ssize_t
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000287bytearray_length(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000288{
289 return Py_SIZE(self);
290}
291
292static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000293bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000294{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000295 Py_ssize_t size;
296 Py_buffer vo;
297
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200298 if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000299 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
300 Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name);
301 return NULL;
302 }
303
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300304 size = Py_SIZE(self);
305 if (size > PY_SSIZE_T_MAX - vo.len) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000306 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000307 return PyErr_NoMemory();
308 }
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300309 if (PyByteArray_Resize((PyObject *)self, size + vo.len) < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000310 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000311 return NULL;
312 }
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300313 memcpy(PyByteArray_AS_STRING(self) + size, vo.buf, vo.len);
Martin v. Löwis423be952008-08-13 15:53:07 +0000314 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000315 Py_INCREF(self);
316 return (PyObject *)self;
317}
318
319static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000320bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000321{
322 PyByteArrayObject *result;
323 Py_ssize_t mysize;
324 Py_ssize_t size;
325
326 if (count < 0)
327 count = 0;
328 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000329 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000330 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000331 size = mysize * count;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000332 result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size);
333 if (result != NULL && size != 0) {
334 if (mysize == 1)
335 memset(result->ob_bytes, self->ob_bytes[0], size);
336 else {
337 Py_ssize_t i;
338 for (i = 0; i < count; i++)
339 memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize);
340 }
341 }
342 return (PyObject *)result;
343}
344
345static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000346bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000347{
348 Py_ssize_t mysize;
349 Py_ssize_t size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200350 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000351
352 if (count < 0)
353 count = 0;
354 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000355 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000356 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000357 size = mysize * count;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200358 if (PyByteArray_Resize((PyObject *)self, size) < 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000359 return NULL;
360
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200361 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000362 if (mysize == 1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200363 memset(buf, buf[0], size);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000364 else {
365 Py_ssize_t i;
366 for (i = 1; i < count; i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200367 memcpy(buf + i*mysize, buf, mysize);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000368 }
369
370 Py_INCREF(self);
371 return (PyObject *)self;
372}
373
374static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000375bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000376{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000377 if (i < 0 || i >= Py_SIZE(self)) {
378 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
379 return NULL;
380 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200381 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000382}
383
384static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000385bytearray_subscript(PyByteArrayObject *self, PyObject *index)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000386{
Victor Stinnera15e2602020-04-08 02:01:56 +0200387 if (_PyIndex_Check(index)) {
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000388 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000389
390 if (i == -1 && PyErr_Occurred())
391 return NULL;
392
393 if (i < 0)
394 i += PyByteArray_GET_SIZE(self);
395
396 if (i < 0 || i >= Py_SIZE(self)) {
397 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
398 return NULL;
399 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200400 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000401 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000402 else if (PySlice_Check(index)) {
Zackery Spytz14514d92019-05-17 01:13:03 -0600403 Py_ssize_t start, stop, step, slicelength, i;
404 size_t cur;
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300405 if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000406 return NULL;
407 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300408 slicelength = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self),
409 &start, &stop, step);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000410
411 if (slicelength <= 0)
412 return PyByteArray_FromStringAndSize("", 0);
413 else if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200414 return PyByteArray_FromStringAndSize(
415 PyByteArray_AS_STRING(self) + start, slicelength);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000416 }
417 else {
418 char *source_buf = PyByteArray_AS_STRING(self);
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000419 char *result_buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000420 PyObject *result;
421
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000422 result = PyByteArray_FromStringAndSize(NULL, slicelength);
423 if (result == NULL)
424 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000425
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000426 result_buf = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000427 for (cur = start, i = 0; i < slicelength;
428 cur += step, i++) {
429 result_buf[i] = source_buf[cur];
430 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000431 return result;
432 }
433 }
434 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400435 PyErr_Format(PyExc_TypeError,
436 "bytearray indices must be integers or slices, not %.200s",
437 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000438 return NULL;
439 }
440}
441
442static int
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200443bytearray_setslice_linear(PyByteArrayObject *self,
444 Py_ssize_t lo, Py_ssize_t hi,
445 char *bytes, Py_ssize_t bytes_len)
446{
447 Py_ssize_t avail = hi - lo;
448 char *buf = PyByteArray_AS_STRING(self);
449 Py_ssize_t growth = bytes_len - avail;
Victor Stinner84557232013-11-21 12:29:51 +0100450 int res = 0;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200451 assert(avail >= 0);
452
Victor Stinner84557232013-11-21 12:29:51 +0100453 if (growth < 0) {
454 if (!_canresize(self))
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200455 return -1;
Victor Stinner84557232013-11-21 12:29:51 +0100456
457 if (lo == 0) {
458 /* Shrink the buffer by advancing its logical start */
459 self->ob_start -= growth;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200460 /*
Victor Stinner84557232013-11-21 12:29:51 +0100461 0 lo hi old_size
462 | |<----avail----->|<-----tail------>|
463 | |<-bytes_len->|<-----tail------>|
464 0 new_lo new_hi new_size
465 */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200466 }
Victor Stinner84557232013-11-21 12:29:51 +0100467 else {
468 /*
469 0 lo hi old_size
470 | |<----avail----->|<-----tomove------>|
471 | |<-bytes_len->|<-----tomove------>|
472 0 lo new_hi new_size
473 */
474 memmove(buf + lo + bytes_len, buf + hi,
475 Py_SIZE(self) - hi);
476 }
477 if (PyByteArray_Resize((PyObject *)self,
478 Py_SIZE(self) + growth) < 0) {
479 /* Issue #19578: Handling the memory allocation failure here is
480 tricky here because the bytearray object has already been
481 modified. Depending on growth and lo, the behaviour is
482 different.
483
484 If growth < 0 and lo != 0, the operation is completed, but a
485 MemoryError is still raised and the memory block is not
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700486 shrunk. Otherwise, the bytearray is restored in its previous
Victor Stinner84557232013-11-21 12:29:51 +0100487 state and a MemoryError is raised. */
488 if (lo == 0) {
489 self->ob_start += growth;
490 return -1;
491 }
492 /* memmove() removed bytes, the bytearray object cannot be
493 restored in its previous state. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100494 Py_SET_SIZE(self, Py_SIZE(self) + growth);
Victor Stinner84557232013-11-21 12:29:51 +0100495 res = -1;
496 }
497 buf = PyByteArray_AS_STRING(self);
498 }
499 else if (growth > 0) {
500 if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) {
501 PyErr_NoMemory();
502 return -1;
503 }
504
505 if (PyByteArray_Resize((PyObject *)self,
506 Py_SIZE(self) + growth) < 0) {
507 return -1;
508 }
509 buf = PyByteArray_AS_STRING(self);
510 /* Make the place for the additional bytes */
511 /*
512 0 lo hi old_size
513 | |<-avail->|<-----tomove------>|
514 | |<---bytes_len-->|<-----tomove------>|
515 0 lo new_hi new_size
516 */
517 memmove(buf + lo + bytes_len, buf + hi,
518 Py_SIZE(self) - lo - bytes_len);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200519 }
520
521 if (bytes_len > 0)
522 memcpy(buf + lo, bytes, bytes_len);
Victor Stinner84557232013-11-21 12:29:51 +0100523 return res;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200524}
525
526static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000527bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000528 PyObject *values)
529{
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200530 Py_ssize_t needed;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000531 void *bytes;
532 Py_buffer vbytes;
533 int res = 0;
534
535 vbytes.len = -1;
536 if (values == (PyObject *)self) {
537 /* Make a copy and call this function recursively */
538 int err;
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300539 values = PyByteArray_FromStringAndSize(PyByteArray_AS_STRING(values),
540 PyByteArray_GET_SIZE(values));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000541 if (values == NULL)
542 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000543 err = bytearray_setslice(self, lo, hi, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000544 Py_DECREF(values);
545 return err;
546 }
547 if (values == NULL) {
548 /* del b[lo:hi] */
549 bytes = NULL;
550 needed = 0;
551 }
552 else {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200553 if (PyObject_GetBuffer(values, &vbytes, PyBUF_SIMPLE) != 0) {
554 PyErr_Format(PyExc_TypeError,
555 "can't set bytearray slice from %.100s",
556 Py_TYPE(values)->tp_name);
557 return -1;
558 }
559 needed = vbytes.len;
560 bytes = vbytes.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000561 }
562
563 if (lo < 0)
564 lo = 0;
565 if (hi < lo)
566 hi = lo;
567 if (hi > Py_SIZE(self))
568 hi = Py_SIZE(self);
569
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200570 res = bytearray_setslice_linear(self, lo, hi, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000571 if (vbytes.len != -1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200572 PyBuffer_Release(&vbytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000573 return res;
574}
575
576static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000577bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000578{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000579 int ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000580
581 if (i < 0)
582 i += Py_SIZE(self);
583
584 if (i < 0 || i >= Py_SIZE(self)) {
585 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
586 return -1;
587 }
588
589 if (value == NULL)
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000590 return bytearray_setslice(self, i, i+1, NULL);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000591
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000592 if (!_getbytevalue(value, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000593 return -1;
594
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200595 PyByteArray_AS_STRING(self)[i] = ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000596 return 0;
597}
598
599static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000600bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000601{
602 Py_ssize_t start, stop, step, slicelen, needed;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200603 char *buf, *bytes;
604 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000605
Victor Stinnera15e2602020-04-08 02:01:56 +0200606 if (_PyIndex_Check(index)) {
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000607 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000608
609 if (i == -1 && PyErr_Occurred())
610 return -1;
611
612 if (i < 0)
613 i += PyByteArray_GET_SIZE(self);
614
615 if (i < 0 || i >= Py_SIZE(self)) {
616 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
617 return -1;
618 }
619
620 if (values == NULL) {
621 /* Fall through to slice assignment */
622 start = i;
623 stop = i + 1;
624 step = 1;
625 slicelen = 1;
626 }
627 else {
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000628 int ival;
629 if (!_getbytevalue(values, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000630 return -1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200631 buf[i] = (char)ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000632 return 0;
633 }
634 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000635 else if (PySlice_Check(index)) {
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300636 if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000637 return -1;
638 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300639 slicelen = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self), &start,
640 &stop, step);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000641 }
642 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400643 PyErr_Format(PyExc_TypeError,
644 "bytearray indices must be integers or slices, not %.200s",
645 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000646 return -1;
647 }
648
649 if (values == NULL) {
650 bytes = NULL;
651 needed = 0;
652 }
653 else if (values == (PyObject *)self || !PyByteArray_Check(values)) {
Christian Heimes6d26ade2012-11-03 23:07:59 +0100654 int err;
Ezio Melottic64bcbe2012-11-03 21:19:06 +0200655 if (PyNumber_Check(values) || PyUnicode_Check(values)) {
656 PyErr_SetString(PyExc_TypeError,
657 "can assign only bytes, buffers, or iterables "
658 "of ints in range(0, 256)");
659 return -1;
660 }
Georg Brandlf3fa5682010-12-04 17:09:30 +0000661 /* Make a copy and call this function recursively */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000662 values = PyByteArray_FromObject(values);
663 if (values == NULL)
664 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000665 err = bytearray_ass_subscript(self, index, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000666 Py_DECREF(values);
667 return err;
668 }
669 else {
670 assert(PyByteArray_Check(values));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200671 bytes = PyByteArray_AS_STRING(values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000672 needed = Py_SIZE(values);
673 }
674 /* Make sure b[5:2] = ... inserts before 5, not before 2. */
675 if ((step < 0 && start < stop) ||
676 (step > 0 && start > stop))
677 stop = start;
678 if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200679 return bytearray_setslice_linear(self, start, stop, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000680 }
681 else {
682 if (needed == 0) {
683 /* Delete slice */
Mark Dickinsonbc099642010-01-29 17:27:24 +0000684 size_t cur;
685 Py_ssize_t i;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000686
Antoine Pitrou5504e892008-12-06 21:27:53 +0000687 if (!_canresize(self))
688 return -1;
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000689
690 if (slicelen == 0)
691 /* Nothing to do here. */
692 return 0;
693
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000694 if (step < 0) {
695 stop = start + 1;
696 start = stop + step * (slicelen - 1) - 1;
697 step = -step;
698 }
699 for (cur = start, i = 0;
700 i < slicelen; cur += step, i++) {
701 Py_ssize_t lim = step - 1;
702
Mark Dickinson66f575b2010-02-14 12:53:32 +0000703 if (cur + step >= (size_t)PyByteArray_GET_SIZE(self))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000704 lim = PyByteArray_GET_SIZE(self) - cur - 1;
705
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200706 memmove(buf + cur - i,
707 buf + cur + 1, lim);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000708 }
709 /* Move the tail of the bytes, in one chunk */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000710 cur = start + (size_t)slicelen*step;
Mark Dickinson66f575b2010-02-14 12:53:32 +0000711 if (cur < (size_t)PyByteArray_GET_SIZE(self)) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200712 memmove(buf + cur - slicelen,
713 buf + cur,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000714 PyByteArray_GET_SIZE(self) - cur);
715 }
716 if (PyByteArray_Resize((PyObject *)self,
717 PyByteArray_GET_SIZE(self) - slicelen) < 0)
718 return -1;
719
720 return 0;
721 }
722 else {
723 /* Assign slice */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000724 Py_ssize_t i;
725 size_t cur;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000726
727 if (needed != slicelen) {
728 PyErr_Format(PyExc_ValueError,
729 "attempt to assign bytes of size %zd "
730 "to extended slice of size %zd",
731 needed, slicelen);
732 return -1;
733 }
734 for (cur = start, i = 0; i < slicelen; cur += step, i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200735 buf[cur] = bytes[i];
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000736 return 0;
737 }
738 }
739}
740
Serhiy Storchaka12f43342020-07-20 15:53:55 +0300741/*[clinic input]
742bytearray.__init__
743
744 source as arg: object = NULL
745 encoding: str = NULL
746 errors: str = NULL
747
748[clinic start generated code]*/
749
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000750static int
Serhiy Storchaka12f43342020-07-20 15:53:55 +0300751bytearray___init___impl(PyByteArrayObject *self, PyObject *arg,
752 const char *encoding, const char *errors)
753/*[clinic end generated code: output=4ce1304649c2f8b3 input=1141a7122eefd7b9]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000754{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000755 Py_ssize_t count;
756 PyObject *it;
757 PyObject *(*iternext)(PyObject *);
758
759 if (Py_SIZE(self) != 0) {
760 /* Empty previous contents (yes, do this first of all!) */
761 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
762 return -1;
763 }
764
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000765 /* Make a quick exit if no first argument */
766 if (arg == NULL) {
767 if (encoding != NULL || errors != NULL) {
768 PyErr_SetString(PyExc_TypeError,
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300769 encoding != NULL ?
770 "encoding without a string argument" :
771 "errors without a string argument");
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000772 return -1;
773 }
774 return 0;
775 }
776
777 if (PyUnicode_Check(arg)) {
778 /* Encode via the codec registry */
779 PyObject *encoded, *new;
780 if (encoding == NULL) {
781 PyErr_SetString(PyExc_TypeError,
782 "string argument without an encoding");
783 return -1;
784 }
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000785 encoded = PyUnicode_AsEncodedString(arg, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000786 if (encoded == NULL)
787 return -1;
788 assert(PyBytes_Check(encoded));
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000789 new = bytearray_iconcat(self, encoded);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000790 Py_DECREF(encoded);
791 if (new == NULL)
792 return -1;
793 Py_DECREF(new);
794 return 0;
795 }
796
797 /* If it's not unicode, there can't be encoding or errors */
798 if (encoding != NULL || errors != NULL) {
799 PyErr_SetString(PyExc_TypeError,
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300800 encoding != NULL ?
801 "encoding without a string argument" :
802 "errors without a string argument");
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000803 return -1;
804 }
805
806 /* Is it an int? */
Victor Stinnera15e2602020-04-08 02:01:56 +0200807 if (_PyIndex_Check(arg)) {
Serhiy Storchakaeb249882016-08-15 09:46:07 +0300808 count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
809 if (count == -1 && PyErr_Occurred()) {
Serhiy Storchakae8904212018-10-15 00:02:57 +0300810 if (!PyErr_ExceptionMatches(PyExc_TypeError))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000811 return -1;
INADA Naokia634e232017-01-06 17:32:01 +0900812 PyErr_Clear(); /* fall through */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000813 }
INADA Naokia634e232017-01-06 17:32:01 +0900814 else {
815 if (count < 0) {
816 PyErr_SetString(PyExc_ValueError, "negative count");
817 return -1;
818 }
819 if (count > 0) {
820 if (PyByteArray_Resize((PyObject *)self, count))
821 return -1;
822 memset(PyByteArray_AS_STRING(self), 0, count);
823 }
824 return 0;
825 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000826 }
827
828 /* Use the buffer API */
829 if (PyObject_CheckBuffer(arg)) {
830 Py_ssize_t size;
831 Py_buffer view;
832 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
833 return -1;
834 size = view.len;
835 if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200836 if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self),
837 &view, size, 'C') < 0)
Stefan Krah7d12d9d2012-07-28 12:25:55 +0200838 goto fail;
Martin v. Löwis423be952008-08-13 15:53:07 +0000839 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000840 return 0;
841 fail:
Martin v. Löwis423be952008-08-13 15:53:07 +0000842 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000843 return -1;
844 }
845
846 /* XXX Optimize this if the arguments is a list, tuple */
847
848 /* Get the iterator */
849 it = PyObject_GetIter(arg);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300850 if (it == NULL) {
851 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
852 PyErr_Format(PyExc_TypeError,
853 "cannot convert '%.200s' object to bytearray",
Victor Stinner58ac7002020-02-07 03:04:21 +0100854 Py_TYPE(arg)->tp_name);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300855 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000856 return -1;
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300857 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000858 iternext = *Py_TYPE(it)->tp_iternext;
859
860 /* Run the iterator to exhaustion */
861 for (;;) {
862 PyObject *item;
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000863 int rc, value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000864
865 /* Get the next item */
866 item = iternext(it);
867 if (item == NULL) {
868 if (PyErr_Occurred()) {
869 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
870 goto error;
871 PyErr_Clear();
872 }
873 break;
874 }
875
876 /* Interpret it as an int (__index__) */
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000877 rc = _getbytevalue(item, &value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000878 Py_DECREF(item);
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000879 if (!rc)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000880 goto error;
881
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000882 /* Append the byte */
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300883 if (Py_SIZE(self) + 1 < self->ob_alloc) {
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100884 Py_SET_SIZE(self, Py_SIZE(self) + 1);
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300885 PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0';
886 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000887 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
888 goto error;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200889 PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000890 }
891
892 /* Clean up and return success */
893 Py_DECREF(it);
894 return 0;
895
896 error:
897 /* Error handling when it != NULL */
898 Py_DECREF(it);
899 return -1;
900}
901
902/* Mostly copied from string_repr, but without the
903 "smart quote" functionality. */
904static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000905bytearray_repr(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000906{
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300907 const char *className = _PyType_Name(Py_TYPE(self));
908 const char *quote_prefix = "(b";
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000909 const char *quote_postfix = ")";
910 Py_ssize_t length = Py_SIZE(self);
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300911 /* 6 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
912 Py_ssize_t newsize;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000913 PyObject *v;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200914 Py_ssize_t i;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200915 char *bytes;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200916 char c;
917 char *p;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200918 int quote;
919 char *test, *start;
920 char *buffer;
921
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300922 newsize = strlen(className);
923 if (length > (PY_SSIZE_T_MAX - 6 - newsize) / 4) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000924 PyErr_SetString(PyExc_OverflowError,
925 "bytearray object is too large to make repr");
926 return NULL;
927 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200928
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300929 newsize += 6 + length * 4;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100930 buffer = PyObject_Malloc(newsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200931 if (buffer == NULL) {
932 PyErr_NoMemory();
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000933 return NULL;
934 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000935
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200936 /* Figure out which quote to use; single is preferred */
937 quote = '\'';
938 start = PyByteArray_AS_STRING(self);
939 for (test = start; test < start+length; ++test) {
940 if (*test == '"') {
941 quote = '\''; /* back to single */
942 break;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000943 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200944 else if (*test == '\'')
945 quote = '"';
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000946 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200947
948 p = buffer;
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300949 while (*className)
950 *p++ = *className++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200951 while (*quote_prefix)
952 *p++ = *quote_prefix++;
953 *p++ = quote;
954
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200955 bytes = PyByteArray_AS_STRING(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200956 for (i = 0; i < length; i++) {
957 /* There's at least enough room for a hex escape
958 and a closing quote. */
959 assert(newsize - (p - buffer) >= 5);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200960 c = bytes[i];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200961 if (c == '\'' || c == '\\')
962 *p++ = '\\', *p++ = c;
963 else if (c == '\t')
964 *p++ = '\\', *p++ = 't';
965 else if (c == '\n')
966 *p++ = '\\', *p++ = 'n';
967 else if (c == '\r')
968 *p++ = '\\', *p++ = 'r';
969 else if (c == 0)
970 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
971 else if (c < ' ' || c >= 0x7f) {
972 *p++ = '\\';
973 *p++ = 'x';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200974 *p++ = Py_hexdigits[(c & 0xf0) >> 4];
975 *p++ = Py_hexdigits[c & 0xf];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200976 }
977 else
978 *p++ = c;
979 }
980 assert(newsize - (p - buffer) >= 1);
981 *p++ = quote;
982 while (*quote_postfix) {
983 *p++ = *quote_postfix++;
984 }
985
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300986 v = PyUnicode_FromStringAndSize(buffer, p - buffer);
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100987 PyObject_Free(buffer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200988 return v;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000989}
990
991static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000992bytearray_str(PyObject *op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000993{
Victor Stinnerda7933e2020-04-13 03:04:28 +0200994 if (_Py_GetConfig()->bytes_warning) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200995 if (PyErr_WarnEx(PyExc_BytesWarning,
996 "str() on a bytearray instance", 1)) {
997 return NULL;
Alexander Belopolskyf0f45142010-08-11 17:31:17 +0000998 }
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200999 }
1000 return bytearray_repr((PyByteArrayObject*)op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001001}
1002
1003static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001004bytearray_richcompare(PyObject *self, PyObject *other, int op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001005{
1006 Py_ssize_t self_size, other_size;
1007 Py_buffer self_bytes, other_bytes;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001008 int cmp, rc;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001009
1010 /* Bytes can be compared to anything that supports the (binary)
1011 buffer API. Except that a comparison with Unicode is always an
1012 error, even if the comparison is for equality. */
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001013 rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type);
1014 if (!rc)
1015 rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type);
1016 if (rc < 0)
1017 return NULL;
1018 if (rc) {
Victor Stinnerda7933e2020-04-13 03:04:28 +02001019 if (_Py_GetConfig()->bytes_warning && (op == Py_EQ || op == Py_NE)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001020 if (PyErr_WarnEx(PyExc_BytesWarning,
Georg Brandle5d68ac2008-06-04 11:30:26 +00001021 "Comparison between bytearray and string", 1))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001022 return NULL;
1023 }
1024
Brian Curtindfc80e32011-08-10 20:28:54 -05001025 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001026 }
1027
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001028 if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001029 PyErr_Clear();
Brian Curtindfc80e32011-08-10 20:28:54 -05001030 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001031 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001032 self_size = self_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001033
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001034 if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001035 PyErr_Clear();
Martin v. Löwis423be952008-08-13 15:53:07 +00001036 PyBuffer_Release(&self_bytes);
Brian Curtindfc80e32011-08-10 20:28:54 -05001037 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001038 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001039 other_size = other_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001040
1041 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
1042 /* Shortcut: if the lengths differ, the objects differ */
stratakise8b19652017-11-02 11:32:54 +01001043 PyBuffer_Release(&self_bytes);
1044 PyBuffer_Release(&other_bytes);
1045 return PyBool_FromLong((op == Py_NE));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001046 }
1047 else {
stratakise8b19652017-11-02 11:32:54 +01001048 cmp = memcmp(self_bytes.buf, other_bytes.buf,
1049 Py_MIN(self_size, other_size));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001050 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
1051
stratakise8b19652017-11-02 11:32:54 +01001052 PyBuffer_Release(&self_bytes);
1053 PyBuffer_Release(&other_bytes);
1054
1055 if (cmp != 0) {
1056 Py_RETURN_RICHCOMPARE(cmp, 0, op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001057 }
1058
stratakise8b19652017-11-02 11:32:54 +01001059 Py_RETURN_RICHCOMPARE(self_size, other_size, op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001060 }
1061
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001062}
1063
1064static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001065bytearray_dealloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001066{
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001067 if (self->ob_exports > 0) {
1068 PyErr_SetString(PyExc_SystemError,
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001069 "deallocated bytearray object has exported buffers");
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001070 PyErr_Print();
1071 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001072 if (self->ob_bytes != 0) {
Antoine Pitrou39aba4f2011-11-12 21:15:28 +01001073 PyObject_Free(self->ob_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001074 }
1075 Py_TYPE(self)->tp_free((PyObject *)self);
1076}
1077
1078
1079/* -------------------------------------------------------------------- */
1080/* Methods */
1081
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001082#define FASTSEARCH fastsearch
1083#define STRINGLIB(F) stringlib_##F
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001084#define STRINGLIB_CHAR char
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001085#define STRINGLIB_SIZEOF_CHAR 1
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001086#define STRINGLIB_LEN PyByteArray_GET_SIZE
1087#define STRINGLIB_STR PyByteArray_AS_STRING
1088#define STRINGLIB_NEW PyByteArray_FromStringAndSize
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001089#define STRINGLIB_ISSPACE Py_ISSPACE
1090#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001091#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1092#define STRINGLIB_MUTABLE 1
1093
1094#include "stringlib/fastsearch.h"
1095#include "stringlib/count.h"
1096#include "stringlib/find.h"
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001097#include "stringlib/join.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001098#include "stringlib/partition.h"
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001099#include "stringlib/split.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001100#include "stringlib/ctype.h"
1101#include "stringlib/transmogrify.h"
1102
1103
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001104static PyObject *
1105bytearray_find(PyByteArrayObject *self, PyObject *args)
1106{
1107 return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1108}
1109
1110static PyObject *
1111bytearray_count(PyByteArrayObject *self, PyObject *args)
1112{
1113 return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1114}
1115
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001116/*[clinic input]
1117bytearray.clear
1118
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001119Remove all items from the bytearray.
1120[clinic start generated code]*/
1121
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001122static PyObject *
1123bytearray_clear_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001124/*[clinic end generated code: output=85c2fe6aede0956c input=ed6edae9de447ac4]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001125{
1126 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1127 return NULL;
1128 Py_RETURN_NONE;
1129}
1130
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001131/*[clinic input]
1132bytearray.copy
1133
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001134Return a copy of B.
1135[clinic start generated code]*/
1136
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001137static PyObject *
1138bytearray_copy_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001139/*[clinic end generated code: output=68cfbcfed484c132 input=6597b0c01bccaa9e]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001140{
1141 return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self),
1142 PyByteArray_GET_SIZE(self));
1143}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001144
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001145static PyObject *
1146bytearray_index(PyByteArrayObject *self, PyObject *args)
1147{
1148 return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1149}
1150
1151static PyObject *
1152bytearray_rfind(PyByteArrayObject *self, PyObject *args)
1153{
1154 return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1155}
1156
1157static PyObject *
1158bytearray_rindex(PyByteArrayObject *self, PyObject *args)
1159{
1160 return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1161}
1162
1163static int
1164bytearray_contains(PyObject *self, PyObject *arg)
1165{
1166 return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg);
1167}
1168
1169static PyObject *
1170bytearray_startswith(PyByteArrayObject *self, PyObject *args)
1171{
1172 return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1173}
1174
1175static PyObject *
1176bytearray_endswith(PyByteArrayObject *self, PyObject *args)
1177{
1178 return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1179}
1180
sweeneydea81849b2020-04-22 17:05:48 -04001181/*[clinic input]
1182bytearray.removeprefix as bytearray_removeprefix
1183
1184 prefix: Py_buffer
1185 /
1186
1187Return a bytearray with the given prefix string removed if present.
1188
1189If the bytearray starts with the prefix string, return
1190bytearray[len(prefix):]. Otherwise, return a copy of the original
1191bytearray.
1192[clinic start generated code]*/
1193
1194static PyObject *
1195bytearray_removeprefix_impl(PyByteArrayObject *self, Py_buffer *prefix)
1196/*[clinic end generated code: output=6cabc585e7f502e0 input=968aada38aedd262]*/
1197{
1198 const char *self_start = PyByteArray_AS_STRING(self);
1199 Py_ssize_t self_len = PyByteArray_GET_SIZE(self);
1200 const char *prefix_start = prefix->buf;
1201 Py_ssize_t prefix_len = prefix->len;
1202
1203 if (self_len >= prefix_len
1204 && memcmp(self_start, prefix_start, prefix_len) == 0)
1205 {
1206 return PyByteArray_FromStringAndSize(self_start + prefix_len,
1207 self_len - prefix_len);
1208 }
1209
1210 return PyByteArray_FromStringAndSize(self_start, self_len);
1211}
1212
1213/*[clinic input]
1214bytearray.removesuffix as bytearray_removesuffix
1215
1216 suffix: Py_buffer
1217 /
1218
1219Return a bytearray with the given suffix string removed if present.
1220
1221If the bytearray ends with the suffix string and that suffix is not
1222empty, return bytearray[:-len(suffix)]. Otherwise, return a copy of
1223the original bytearray.
1224[clinic start generated code]*/
1225
1226static PyObject *
1227bytearray_removesuffix_impl(PyByteArrayObject *self, Py_buffer *suffix)
1228/*[clinic end generated code: output=2bc8cfb79de793d3 input=c1827e810b2f6b99]*/
1229{
1230 const char *self_start = PyByteArray_AS_STRING(self);
1231 Py_ssize_t self_len = PyByteArray_GET_SIZE(self);
1232 const char *suffix_start = suffix->buf;
1233 Py_ssize_t suffix_len = suffix->len;
1234
1235 if (self_len >= suffix_len
1236 && memcmp(self_start + self_len - suffix_len,
1237 suffix_start, suffix_len) == 0)
1238 {
1239 return PyByteArray_FromStringAndSize(self_start,
1240 self_len - suffix_len);
1241 }
1242
1243 return PyByteArray_FromStringAndSize(self_start, self_len);
1244}
1245
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001246
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001247/*[clinic input]
1248bytearray.translate
1249
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001250 table: object
1251 Translation table, which must be a bytes object of length 256.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001252 /
Martin Panter1b6c6da2016-08-27 08:35:02 +00001253 delete as deletechars: object(c_default="NULL") = b''
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001254
1255Return a copy with each character mapped by the given translation table.
1256
Martin Panter1b6c6da2016-08-27 08:35:02 +00001257All characters occurring in the optional argument delete are removed.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001258The remaining characters are mapped through the given translation table.
1259[clinic start generated code]*/
1260
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001261static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001262bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
Martin Panter1b6c6da2016-08-27 08:35:02 +00001263 PyObject *deletechars)
1264/*[clinic end generated code: output=b6a8f01c2a74e446 input=cfff956d4d127a9b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001265{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001266 char *input, *output;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001267 const char *table_chars;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001268 Py_ssize_t i, c;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001269 PyObject *input_obj = (PyObject*)self;
1270 const char *output_start;
1271 Py_ssize_t inlen;
Georg Brandlccc47b62008-12-28 11:44:14 +00001272 PyObject *result = NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001273 int trans_table[256];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001274 Py_buffer vtable, vdel;
1275
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001276 if (table == Py_None) {
1277 table_chars = NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001278 table = NULL;
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001279 } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001280 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001281 } else {
1282 if (vtable.len != 256) {
1283 PyErr_SetString(PyExc_ValueError,
1284 "translation table must be 256 characters long");
Georg Brandl953152f2009-07-22 12:03:59 +00001285 PyBuffer_Release(&vtable);
1286 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001287 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001288 table_chars = (const char*)vtable.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001289 }
1290
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001291 if (deletechars != NULL) {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001292 if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) {
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001293 if (table != NULL)
Georg Brandl953152f2009-07-22 12:03:59 +00001294 PyBuffer_Release(&vtable);
1295 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001296 }
1297 }
1298 else {
1299 vdel.buf = NULL;
1300 vdel.len = 0;
1301 }
1302
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001303 inlen = PyByteArray_GET_SIZE(input_obj);
1304 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1305 if (result == NULL)
1306 goto done;
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03001307 output_start = output = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001308 input = PyByteArray_AS_STRING(input_obj);
1309
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001310 if (vdel.len == 0 && table_chars != NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001311 /* If no deletions are required, use faster code */
1312 for (i = inlen; --i >= 0; ) {
1313 c = Py_CHARMASK(*input++);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001314 *output++ = table_chars[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001315 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001316 goto done;
1317 }
Georg Brandlccc47b62008-12-28 11:44:14 +00001318
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001319 if (table_chars == NULL) {
Georg Brandlccc47b62008-12-28 11:44:14 +00001320 for (i = 0; i < 256; i++)
1321 trans_table[i] = Py_CHARMASK(i);
1322 } else {
1323 for (i = 0; i < 256; i++)
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001324 trans_table[i] = Py_CHARMASK(table_chars[i]);
Georg Brandlccc47b62008-12-28 11:44:14 +00001325 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001326
1327 for (i = 0; i < vdel.len; i++)
1328 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1329
1330 for (i = inlen; --i >= 0; ) {
1331 c = Py_CHARMASK(*input++);
1332 if (trans_table[c] != -1)
Martin Panter1b6c6da2016-08-27 08:35:02 +00001333 *output++ = (char)trans_table[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001334 }
1335 /* Fix the size of the resulting string */
1336 if (inlen > 0)
Christian Heimesc731bbe2013-07-21 02:04:35 +02001337 if (PyByteArray_Resize(result, output - output_start) < 0) {
1338 Py_CLEAR(result);
1339 goto done;
1340 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001341
1342done:
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001343 if (table != NULL)
Georg Brandlccc47b62008-12-28 11:44:14 +00001344 PyBuffer_Release(&vtable);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001345 if (deletechars != NULL)
Martin v. Löwis423be952008-08-13 15:53:07 +00001346 PyBuffer_Release(&vdel);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001347 return result;
1348}
1349
1350
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001351/*[clinic input]
1352
1353@staticmethod
1354bytearray.maketrans
1355
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001356 frm: Py_buffer
1357 to: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001358 /
1359
1360Return a translation table useable for the bytes or bytearray translate method.
1361
1362The returned table will be one where each byte in frm is mapped to the byte at
1363the same position in to.
1364
1365The bytes objects frm and to must be of the same length.
1366[clinic start generated code]*/
1367
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001368static PyObject *
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001369bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001370/*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001371{
1372 return _Py_bytes_maketrans(frm, to);
Georg Brandlabc38772009-04-12 15:51:51 +00001373}
1374
1375
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001376/*[clinic input]
1377bytearray.replace
1378
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001379 old: Py_buffer
1380 new: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001381 count: Py_ssize_t = -1
1382 Maximum number of occurrences to replace.
1383 -1 (the default value) means replace all occurrences.
1384 /
1385
1386Return a copy with all occurrences of substring old replaced by new.
1387
1388If the optional argument count is given, only the first count occurrences are
1389replaced.
1390[clinic start generated code]*/
1391
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001392static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001393bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
1394 Py_buffer *new, Py_ssize_t count)
1395/*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001396{
Serhiy Storchakafb81d3c2016-05-05 09:26:07 +03001397 return stringlib_replace((PyObject *)self,
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001398 (const char *)old->buf, old->len,
1399 (const char *)new->buf, new->len, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001400}
1401
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001402/*[clinic input]
1403bytearray.split
1404
1405 sep: object = None
1406 The delimiter according which to split the bytearray.
1407 None (the default value) means split on ASCII whitespace characters
1408 (space, tab, return, newline, formfeed, vertical tab).
1409 maxsplit: Py_ssize_t = -1
1410 Maximum number of splits to do.
1411 -1 (the default value) means no limit.
1412
1413Return a list of the sections in the bytearray, using sep as the delimiter.
1414[clinic start generated code]*/
1415
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001416static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001417bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
1418 Py_ssize_t maxsplit)
1419/*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001420{
1421 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001422 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001423 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001424 Py_buffer vsub;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001425
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001426 if (maxsplit < 0)
1427 maxsplit = PY_SSIZE_T_MAX;
1428
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001429 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001430 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001431
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001432 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001433 return NULL;
1434 sub = vsub.buf;
1435 n = vsub.len;
1436
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001437 list = stringlib_split(
1438 (PyObject*) self, s, len, sub, n, maxsplit
1439 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001440 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001441 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001442}
1443
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001444/*[clinic input]
1445bytearray.partition
1446
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001447 sep: object
1448 /
1449
1450Partition the bytearray into three parts using the given separator.
1451
1452This will search for the separator sep in the bytearray. If the separator is
1453found, returns a 3-tuple containing the part before the separator, the
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001454separator itself, and the part after it as new bytearray objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001455
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001456If the separator is not found, returns a 3-tuple containing the copy of the
1457original bytearray object and two empty bytearray objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001458[clinic start generated code]*/
1459
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001460static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001461bytearray_partition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001462/*[clinic end generated code: output=45d2525ddd35f957 input=8f644749ee4fc83a]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001463{
1464 PyObject *bytesep, *result;
1465
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001466 bytesep = _PyByteArray_FromBufferObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001467 if (! bytesep)
1468 return NULL;
1469
1470 result = stringlib_partition(
1471 (PyObject*) self,
1472 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1473 bytesep,
1474 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1475 );
1476
1477 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001478 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001479}
1480
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001481/*[clinic input]
1482bytearray.rpartition
1483
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001484 sep: object
1485 /
1486
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001487Partition the bytearray into three parts using the given separator.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001488
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001489This will search for the separator sep in the bytearray, starting at the end.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001490If the separator is found, returns a 3-tuple containing the part before the
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001491separator, the separator itself, and the part after it as new bytearray
1492objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001493
1494If the separator is not found, returns a 3-tuple containing two empty bytearray
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001495objects and the copy of the original bytearray object.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001496[clinic start generated code]*/
1497
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001498static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001499bytearray_rpartition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001500/*[clinic end generated code: output=440de3c9426115e8 input=7e3df3e6cb8fa0ac]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001501{
1502 PyObject *bytesep, *result;
1503
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001504 bytesep = _PyByteArray_FromBufferObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001505 if (! bytesep)
1506 return NULL;
1507
1508 result = stringlib_rpartition(
1509 (PyObject*) self,
1510 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1511 bytesep,
1512 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1513 );
1514
1515 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001516 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001517}
1518
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001519/*[clinic input]
1520bytearray.rsplit = bytearray.split
1521
1522Return a list of the sections in the bytearray, using sep as the delimiter.
1523
1524Splitting is done starting at the end of the bytearray and working to the front.
1525[clinic start generated code]*/
1526
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001527static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001528bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
1529 Py_ssize_t maxsplit)
1530/*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001531{
1532 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001533 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001534 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001535 Py_buffer vsub;
1536
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001537 if (maxsplit < 0)
1538 maxsplit = PY_SSIZE_T_MAX;
1539
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001540 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001541 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001542
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001543 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001544 return NULL;
1545 sub = vsub.buf;
1546 n = vsub.len;
1547
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001548 list = stringlib_rsplit(
1549 (PyObject*) self, s, len, sub, n, maxsplit
1550 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001551 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001552 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001553}
1554
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001555/*[clinic input]
1556bytearray.reverse
1557
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001558Reverse the order of the values in B in place.
1559[clinic start generated code]*/
1560
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001561static PyObject *
1562bytearray_reverse_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001563/*[clinic end generated code: output=9f7616f29ab309d3 input=543356319fc78557]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001564{
1565 char swap, *head, *tail;
1566 Py_ssize_t i, j, n = Py_SIZE(self);
1567
1568 j = n / 2;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001569 head = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001570 tail = head + n - 1;
1571 for (i = 0; i < j; i++) {
1572 swap = *head;
1573 *head++ = *tail;
1574 *tail-- = swap;
1575 }
1576
1577 Py_RETURN_NONE;
1578}
1579
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001580
1581/*[python input]
1582class bytesvalue_converter(CConverter):
1583 type = 'int'
1584 converter = '_getbytevalue'
1585[python start generated code]*/
1586/*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/
1587
1588
1589/*[clinic input]
1590bytearray.insert
1591
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001592 index: Py_ssize_t
1593 The index where the value is to be inserted.
1594 item: bytesvalue
1595 The item to be inserted.
1596 /
1597
1598Insert a single item into the bytearray before the given index.
1599[clinic start generated code]*/
1600
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001601static PyObject *
1602bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001603/*[clinic end generated code: output=76c775a70e7b07b7 input=b2b5d07e9de6c070]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001604{
1605 Py_ssize_t n = Py_SIZE(self);
1606 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001607
1608 if (n == PY_SSIZE_T_MAX) {
1609 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001610 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001611 return NULL;
1612 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001613 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1614 return NULL;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001615 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001616
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001617 if (index < 0) {
1618 index += n;
1619 if (index < 0)
1620 index = 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001621 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001622 if (index > n)
1623 index = n;
1624 memmove(buf + index + 1, buf + index, n - index);
1625 buf[index] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001626
1627 Py_RETURN_NONE;
1628}
1629
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001630/*[clinic input]
1631bytearray.append
1632
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001633 item: bytesvalue
1634 The item to be appended.
1635 /
1636
1637Append a single item to the end of the bytearray.
1638[clinic start generated code]*/
1639
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001640static PyObject *
1641bytearray_append_impl(PyByteArrayObject *self, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001642/*[clinic end generated code: output=a154e19ed1886cb6 input=20d6bec3d1340593]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001643{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001644 Py_ssize_t n = Py_SIZE(self);
1645
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001646 if (n == PY_SSIZE_T_MAX) {
1647 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001648 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001649 return NULL;
1650 }
1651 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1652 return NULL;
1653
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001654 PyByteArray_AS_STRING(self)[n] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001655
1656 Py_RETURN_NONE;
1657}
1658
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001659/*[clinic input]
1660bytearray.extend
1661
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001662 iterable_of_ints: object
1663 The iterable of items to append.
1664 /
1665
1666Append all the items from the iterator or sequence to the end of the bytearray.
1667[clinic start generated code]*/
1668
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001669static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001670bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001671/*[clinic end generated code: output=98155dbe249170b1 input=c617b3a93249ba28]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001672{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001673 PyObject *it, *item, *bytearray_obj;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001674 Py_ssize_t buf_size = 0, len = 0;
1675 int value;
1676 char *buf;
1677
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001678 /* bytearray_setslice code only accepts something supporting PEP 3118. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001679 if (PyObject_CheckBuffer(iterable_of_ints)) {
1680 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_ints) == -1)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001681 return NULL;
1682
1683 Py_RETURN_NONE;
1684 }
1685
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001686 it = PyObject_GetIter(iterable_of_ints);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +03001687 if (it == NULL) {
1688 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
1689 PyErr_Format(PyExc_TypeError,
1690 "can't extend bytearray with %.100s",
Victor Stinner58ac7002020-02-07 03:04:21 +01001691 Py_TYPE(iterable_of_ints)->tp_name);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +03001692 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001693 return NULL;
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +03001694 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001695
Ezio Melotti42da6632011-03-15 05:18:48 +02001696 /* Try to determine the length of the argument. 32 is arbitrary. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001697 buf_size = PyObject_LengthHint(iterable_of_ints, 32);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001698 if (buf_size == -1) {
1699 Py_DECREF(it);
1700 return NULL;
1701 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001702
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001703 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001704 if (bytearray_obj == NULL) {
1705 Py_DECREF(it);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001706 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001707 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001708 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001709
1710 while ((item = PyIter_Next(it)) != NULL) {
1711 if (! _getbytevalue(item, &value)) {
1712 Py_DECREF(item);
1713 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001714 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001715 return NULL;
1716 }
1717 buf[len++] = value;
1718 Py_DECREF(item);
1719
1720 if (len >= buf_size) {
Martin Panter371731e2016-07-18 07:53:13 +00001721 Py_ssize_t addition;
1722 if (len == PY_SSIZE_T_MAX) {
1723 Py_DECREF(it);
1724 Py_DECREF(bytearray_obj);
1725 return PyErr_NoMemory();
1726 }
1727 addition = len >> 1;
1728 if (addition > PY_SSIZE_T_MAX - len - 1)
1729 buf_size = PY_SSIZE_T_MAX;
1730 else
1731 buf_size = len + addition + 1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001732 if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001733 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001734 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001735 return NULL;
1736 }
1737 /* Recompute the `buf' pointer, since the resizing operation may
1738 have invalidated it. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001739 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001740 }
1741 }
1742 Py_DECREF(it);
1743
1744 /* Resize down to exact size. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001745 if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) {
1746 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001747 return NULL;
1748 }
1749
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001750 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) {
1751 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001752 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001753 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001754 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001755
Brandt Bucher2a7d5962019-06-26 12:06:18 -07001756 if (PyErr_Occurred()) {
1757 return NULL;
1758 }
1759
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001760 Py_RETURN_NONE;
1761}
1762
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001763/*[clinic input]
1764bytearray.pop
1765
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001766 index: Py_ssize_t = -1
1767 The index from where to remove the item.
1768 -1 (the default value) means remove the last item.
1769 /
1770
1771Remove and return a single item from B.
1772
1773If no index argument is given, will pop the last item.
1774[clinic start generated code]*/
1775
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001776static PyObject *
1777bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001778/*[clinic end generated code: output=e0ccd401f8021da8 input=3591df2d06c0d237]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001779{
1780 int value;
1781 Py_ssize_t n = Py_SIZE(self);
1782 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001783
1784 if (n == 0) {
Eli Bendersky1bc4f192011-03-04 04:55:25 +00001785 PyErr_SetString(PyExc_IndexError,
1786 "pop from empty bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001787 return NULL;
1788 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001789 if (index < 0)
1790 index += Py_SIZE(self);
1791 if (index < 0 || index >= Py_SIZE(self)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001792 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1793 return NULL;
1794 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001795 if (!_canresize(self))
1796 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001797
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001798 buf = PyByteArray_AS_STRING(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001799 value = buf[index];
1800 memmove(buf + index, buf + index + 1, n - index);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001801 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1802 return NULL;
1803
Mark Dickinson54a3db92009-09-06 10:19:23 +00001804 return PyLong_FromLong((unsigned char)value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001805}
1806
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001807/*[clinic input]
1808bytearray.remove
1809
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001810 value: bytesvalue
1811 The value to remove.
1812 /
1813
1814Remove the first occurrence of a value in the bytearray.
1815[clinic start generated code]*/
1816
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001817static PyObject *
1818bytearray_remove_impl(PyByteArrayObject *self, int value)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001819/*[clinic end generated code: output=d659e37866709c13 input=121831240cd51ddf]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001820{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001821 Py_ssize_t where, n = Py_SIZE(self);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001822 char *buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001823
Serhiy Storchaka4b234942016-05-16 22:24:03 +03001824 where = stringlib_find_char(buf, n, value);
1825 if (where < 0) {
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001826 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001827 return NULL;
1828 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001829 if (!_canresize(self))
1830 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001831
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001832 memmove(buf + where, buf + where + 1, n - where);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001833 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1834 return NULL;
1835
1836 Py_RETURN_NONE;
1837}
1838
1839/* XXX These two helpers could be optimized if argsize == 1 */
1840
1841static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001842lstrip_helper(const char *myptr, Py_ssize_t mysize,
1843 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001844{
1845 Py_ssize_t i = 0;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001846 while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001847 i++;
1848 return i;
1849}
1850
1851static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001852rstrip_helper(const char *myptr, Py_ssize_t mysize,
1853 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001854{
1855 Py_ssize_t i = mysize - 1;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001856 while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001857 i--;
1858 return i + 1;
1859}
1860
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001861/*[clinic input]
1862bytearray.strip
1863
1864 bytes: object = None
1865 /
1866
1867Strip leading and trailing bytes contained in the argument.
1868
1869If the argument is omitted or None, strip leading and trailing ASCII whitespace.
1870[clinic start generated code]*/
1871
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001872static PyObject *
1873bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001874/*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001875{
1876 Py_ssize_t left, right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001877 char *myptr;
1878 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001879 Py_buffer vbytes;
1880
1881 if (bytes == Py_None) {
1882 bytesptr = "\t\n\r\f\v ";
1883 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001884 }
1885 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001886 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001887 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001888 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001889 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001890 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001891 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001892 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001893 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001894 if (left == mysize)
1895 right = left;
1896 else
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001897 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1898 if (bytes != Py_None)
1899 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001900 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001901}
1902
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001903/*[clinic input]
1904bytearray.lstrip
1905
1906 bytes: object = None
1907 /
1908
1909Strip leading bytes contained in the argument.
1910
1911If the argument is omitted or None, strip leading ASCII whitespace.
1912[clinic start generated code]*/
1913
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001914static PyObject *
1915bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001916/*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001917{
1918 Py_ssize_t left, right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001919 char *myptr;
1920 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001921 Py_buffer vbytes;
1922
1923 if (bytes == Py_None) {
1924 bytesptr = "\t\n\r\f\v ";
1925 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001926 }
1927 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001928 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001929 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001930 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001931 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001932 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001933 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001934 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001935 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001936 right = mysize;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001937 if (bytes != Py_None)
1938 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001939 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001940}
1941
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001942/*[clinic input]
1943bytearray.rstrip
1944
1945 bytes: object = None
1946 /
1947
1948Strip trailing bytes contained in the argument.
1949
1950If the argument is omitted or None, strip trailing ASCII whitespace.
1951[clinic start generated code]*/
1952
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001953static PyObject *
1954bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001955/*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001956{
1957 Py_ssize_t right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001958 char *myptr;
1959 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001960 Py_buffer vbytes;
1961
1962 if (bytes == Py_None) {
1963 bytesptr = "\t\n\r\f\v ";
1964 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001965 }
1966 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001967 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001968 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001969 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001970 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001971 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001972 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001973 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001974 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1975 if (bytes != Py_None)
1976 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001977 return PyByteArray_FromStringAndSize(myptr, right);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001978}
1979
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001980/*[clinic input]
1981bytearray.decode
1982
1983 encoding: str(c_default="NULL") = 'utf-8'
1984 The encoding with which to decode the bytearray.
1985 errors: str(c_default="NULL") = 'strict'
1986 The error handling scheme to use for the handling of decoding errors.
1987 The default is 'strict' meaning that decoding errors raise a
1988 UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
1989 as well as any other name registered with codecs.register_error that
1990 can handle UnicodeDecodeErrors.
1991
1992Decode the bytearray using the codec registered for encoding.
1993[clinic start generated code]*/
1994
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001995static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001996bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
1997 const char *errors)
1998/*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001999{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002000 if (encoding == NULL)
2001 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis0efea322014-07-27 17:29:17 +02002002 return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002003}
2004
2005PyDoc_STRVAR(alloc_doc,
2006"B.__alloc__() -> int\n\
2007\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00002008Return the number of bytes actually allocated.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002009
2010static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302011bytearray_alloc(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002012{
2013 return PyLong_FromSsize_t(self->ob_alloc);
2014}
2015
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002016/*[clinic input]
2017bytearray.join
2018
2019 iterable_of_bytes: object
2020 /
2021
2022Concatenate any number of bytes/bytearray objects.
2023
2024The bytearray whose method is called is inserted in between each pair.
2025
2026The result is returned as a new bytearray object.
2027[clinic start generated code]*/
2028
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002029static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002030bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002031/*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002032{
Martin v. Löwis0efea322014-07-27 17:29:17 +02002033 return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002034}
2035
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002036/*[clinic input]
2037bytearray.splitlines
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002038
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002039 keepends: bool(accept={int}) = False
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002040
2041Return a list of the lines in the bytearray, breaking at line boundaries.
2042
2043Line breaks are not included in the resulting list unless keepends is given and
2044true.
2045[clinic start generated code]*/
2046
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002047static PyObject *
2048bytearray_splitlines_impl(PyByteArrayObject *self, int keepends)
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002049/*[clinic end generated code: output=4223c94b895f6ad9 input=99a27ad959b9cf6b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002050{
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002051 return stringlib_splitlines(
2052 (PyObject*) self, PyByteArray_AS_STRING(self),
2053 PyByteArray_GET_SIZE(self), keepends
2054 );
2055}
2056
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002057/*[clinic input]
2058@classmethod
2059bytearray.fromhex
2060
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002061 string: unicode
2062 /
2063
2064Create a bytearray object from a string of hexadecimal numbers.
2065
2066Spaces between two numbers are accepted.
2067Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')
2068[clinic start generated code]*/
2069
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002070static PyObject *
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002071bytearray_fromhex_impl(PyTypeObject *type, PyObject *string)
2072/*[clinic end generated code: output=8f0f0b6d30fb3ba0 input=f033a16d1fb21f48]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002073{
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002074 PyObject *result = _PyBytes_FromHex(string, type == &PyByteArray_Type);
2075 if (type != &PyByteArray_Type && result != NULL) {
Petr Viktorinffd97532020-02-11 17:46:57 +01002076 Py_SETREF(result, PyObject_CallOneArg((PyObject *)type, result));
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002077 }
2078 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002079}
2080
Gregory P. Smith0c2f9302019-05-29 11:46:58 -07002081/*[clinic input]
2082bytearray.hex
2083
2084 sep: object = NULL
2085 An optional single character or byte to separate hex bytes.
2086 bytes_per_sep: int = 1
2087 How many bytes between separators. Positive values count from the
2088 right, negative values count from the left.
2089
2090Create a str of hexadecimal numbers from a bytearray object.
2091
2092Example:
2093>>> value = bytearray([0xb9, 0x01, 0xef])
2094>>> value.hex()
2095'b901ef'
2096>>> value.hex(':')
2097'b9:01:ef'
2098>>> value.hex(':', 2)
2099'b9:01ef'
2100>>> value.hex(':', -2)
2101'b901:ef'
2102[clinic start generated code]*/
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002103
2104static PyObject *
Gregory P. Smith0c2f9302019-05-29 11:46:58 -07002105bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep)
2106/*[clinic end generated code: output=29c4e5ef72c565a0 input=814c15830ac8c4b5]*/
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002107{
2108 char* argbuf = PyByteArray_AS_STRING(self);
2109 Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
Gregory P. Smith0c2f9302019-05-29 11:46:58 -07002110 return _Py_strhex_with_sep(argbuf, arglen, sep, bytes_per_sep);
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002111}
2112
2113static PyObject *
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002114_common_reduce(PyByteArrayObject *self, int proto)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002115{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002116 PyObject *dict;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002117 _Py_IDENTIFIER(__dict__);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002118 char *buf;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002119
Serhiy Storchaka41c57b32019-09-01 12:03:39 +03002120 if (_PyObject_LookupAttrId((PyObject *)self, &PyId___dict__, &dict) < 0) {
2121 return NULL;
2122 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002123 if (dict == NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002124 dict = Py_None;
2125 Py_INCREF(dict);
2126 }
2127
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002128 buf = PyByteArray_AS_STRING(self);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002129 if (proto < 3) {
2130 /* use str based reduction for backwards compatibility with Python 2.x */
2131 PyObject *latin1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002132 if (Py_SIZE(self))
2133 latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002134 else
2135 latin1 = PyUnicode_FromString("");
2136 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
2137 }
2138 else {
2139 /* use more efficient byte based reduction */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002140 if (Py_SIZE(self)) {
2141 return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002142 }
2143 else {
2144 return Py_BuildValue("(O()N)", Py_TYPE(self), dict);
2145 }
2146 }
2147}
2148
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002149/*[clinic input]
2150bytearray.__reduce__ as bytearray_reduce
2151
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002152Return state information for pickling.
2153[clinic start generated code]*/
2154
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002155static PyObject *
2156bytearray_reduce_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002157/*[clinic end generated code: output=52bf304086464cab input=44b5737ada62dd3f]*/
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002158{
2159 return _common_reduce(self, 2);
2160}
2161
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002162/*[clinic input]
2163bytearray.__reduce_ex__ as bytearray_reduce_ex
2164
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002165 proto: int = 0
2166 /
2167
2168Return state information for pickling.
2169[clinic start generated code]*/
2170
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002171static PyObject *
2172bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002173/*[clinic end generated code: output=52eac33377197520 input=f129bc1a1aa151ee]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002174{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002175 return _common_reduce(self, proto);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002176}
2177
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002178/*[clinic input]
2179bytearray.__sizeof__ as bytearray_sizeof
2180
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002181Returns the size of the bytearray object in memory, in bytes.
2182[clinic start generated code]*/
2183
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002184static PyObject *
2185bytearray_sizeof_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002186/*[clinic end generated code: output=738abdd17951c427 input=e27320fd98a4bc5a]*/
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002187{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002188 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002189
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002190 res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002191 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002192}
2193
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002194static PySequenceMethods bytearray_as_sequence = {
2195 (lenfunc)bytearray_length, /* sq_length */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002196 (binaryfunc)PyByteArray_Concat, /* sq_concat */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002197 (ssizeargfunc)bytearray_repeat, /* sq_repeat */
2198 (ssizeargfunc)bytearray_getitem, /* sq_item */
2199 0, /* sq_slice */
2200 (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */
2201 0, /* sq_ass_slice */
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002202 (objobjproc)bytearray_contains, /* sq_contains */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002203 (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */
2204 (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002205};
2206
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002207static PyMappingMethods bytearray_as_mapping = {
2208 (lenfunc)bytearray_length,
2209 (binaryfunc)bytearray_subscript,
2210 (objobjargproc)bytearray_ass_subscript,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002211};
2212
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002213static PyBufferProcs bytearray_as_buffer = {
2214 (getbufferproc)bytearray_getbuffer,
2215 (releasebufferproc)bytearray_releasebuffer,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002216};
2217
2218static PyMethodDef
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002219bytearray_methods[] = {
2220 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002221 BYTEARRAY_REDUCE_METHODDEF
2222 BYTEARRAY_REDUCE_EX_METHODDEF
2223 BYTEARRAY_SIZEOF_METHODDEF
2224 BYTEARRAY_APPEND_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302225 {"capitalize", stringlib_capitalize, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002226 _Py_capitalize__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002227 STRINGLIB_CENTER_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002228 BYTEARRAY_CLEAR_METHODDEF
2229 BYTEARRAY_COPY_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002230 {"count", (PyCFunction)bytearray_count, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002231 _Py_count__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002232 BYTEARRAY_DECODE_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002233 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002234 _Py_endswith__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002235 STRINGLIB_EXPANDTABS_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002236 BYTEARRAY_EXTEND_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002237 {"find", (PyCFunction)bytearray_find, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002238 _Py_find__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002239 BYTEARRAY_FROMHEX_METHODDEF
Gregory P. Smith0c2f9302019-05-29 11:46:58 -07002240 BYTEARRAY_HEX_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002241 {"index", (PyCFunction)bytearray_index, METH_VARARGS, _Py_index__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002242 BYTEARRAY_INSERT_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302243 {"isalnum", stringlib_isalnum, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002244 _Py_isalnum__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302245 {"isalpha", stringlib_isalpha, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002246 _Py_isalpha__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302247 {"isascii", stringlib_isascii, METH_NOARGS,
INADA Naokia49ac992018-01-27 14:06:21 +09002248 _Py_isascii__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302249 {"isdigit", stringlib_isdigit, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002250 _Py_isdigit__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302251 {"islower", stringlib_islower, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002252 _Py_islower__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302253 {"isspace", stringlib_isspace, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002254 _Py_isspace__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302255 {"istitle", stringlib_istitle, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002256 _Py_istitle__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302257 {"isupper", stringlib_isupper, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002258 _Py_isupper__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002259 BYTEARRAY_JOIN_METHODDEF
Tal Einatc929df32018-07-06 13:17:38 +03002260 STRINGLIB_LJUST_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302261 {"lower", stringlib_lower, METH_NOARGS, _Py_lower__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002262 BYTEARRAY_LSTRIP_METHODDEF
2263 BYTEARRAY_MAKETRANS_METHODDEF
2264 BYTEARRAY_PARTITION_METHODDEF
2265 BYTEARRAY_POP_METHODDEF
2266 BYTEARRAY_REMOVE_METHODDEF
2267 BYTEARRAY_REPLACE_METHODDEF
sweeneydea81849b2020-04-22 17:05:48 -04002268 BYTEARRAY_REMOVEPREFIX_METHODDEF
2269 BYTEARRAY_REMOVESUFFIX_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002270 BYTEARRAY_REVERSE_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002271 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__},
2272 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002273 STRINGLIB_RJUST_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002274 BYTEARRAY_RPARTITION_METHODDEF
2275 BYTEARRAY_RSPLIT_METHODDEF
2276 BYTEARRAY_RSTRIP_METHODDEF
2277 BYTEARRAY_SPLIT_METHODDEF
2278 BYTEARRAY_SPLITLINES_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002279 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002280 _Py_startswith__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002281 BYTEARRAY_STRIP_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302282 {"swapcase", stringlib_swapcase, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002283 _Py_swapcase__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302284 {"title", stringlib_title, METH_NOARGS, _Py_title__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002285 BYTEARRAY_TRANSLATE_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302286 {"upper", stringlib_upper, METH_NOARGS, _Py_upper__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002287 STRINGLIB_ZFILL_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002288 {NULL}
2289};
2290
Ethan Furmanb95b5612015-01-23 20:05:18 -08002291static PyObject *
2292bytearray_mod(PyObject *v, PyObject *w)
2293{
2294 if (!PyByteArray_Check(v))
2295 Py_RETURN_NOTIMPLEMENTED;
Berker Peksag43de36d2016-04-16 01:20:47 +03002296 return _PyBytes_FormatEx(PyByteArray_AS_STRING(v), PyByteArray_GET_SIZE(v), w, 1);
Ethan Furmanb95b5612015-01-23 20:05:18 -08002297}
2298
2299static PyNumberMethods bytearray_as_number = {
2300 0, /*nb_add*/
2301 0, /*nb_subtract*/
2302 0, /*nb_multiply*/
2303 bytearray_mod, /*nb_remainder*/
2304};
2305
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002306PyDoc_STRVAR(bytearray_doc,
Georg Brandl17cb8a82008-05-30 08:20:09 +00002307"bytearray(iterable_of_ints) -> bytearray\n\
2308bytearray(string, encoding[, errors]) -> bytearray\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002309bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\
2310bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\
2311bytearray() -> empty bytes array\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002312\n\
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03002313Construct a mutable bytearray object from:\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002314 - an iterable yielding integers in range(256)\n\
2315 - a text string encoded using the specified encoding\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002316 - a bytes or a buffer object\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002317 - any object implementing the buffer API.\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002318 - an integer");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002319
2320
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002321static PyObject *bytearray_iter(PyObject *seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002322
2323PyTypeObject PyByteArray_Type = {
2324 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2325 "bytearray",
2326 sizeof(PyByteArrayObject),
2327 0,
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002328 (destructor)bytearray_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002329 0, /* tp_vectorcall_offset */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002330 0, /* tp_getattr */
2331 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002332 0, /* tp_as_async */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002333 (reprfunc)bytearray_repr, /* tp_repr */
Ethan Furmanb95b5612015-01-23 20:05:18 -08002334 &bytearray_as_number, /* tp_as_number */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002335 &bytearray_as_sequence, /* tp_as_sequence */
2336 &bytearray_as_mapping, /* tp_as_mapping */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002337 0, /* tp_hash */
2338 0, /* tp_call */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002339 bytearray_str, /* tp_str */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002340 PyObject_GenericGetAttr, /* tp_getattro */
2341 0, /* tp_setattro */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002342 &bytearray_as_buffer, /* tp_as_buffer */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002343 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002344 bytearray_doc, /* tp_doc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002345 0, /* tp_traverse */
2346 0, /* tp_clear */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002347 (richcmpfunc)bytearray_richcompare, /* tp_richcompare */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002348 0, /* tp_weaklistoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002349 bytearray_iter, /* tp_iter */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002350 0, /* tp_iternext */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002351 bytearray_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002352 0, /* tp_members */
2353 0, /* tp_getset */
2354 0, /* tp_base */
2355 0, /* tp_dict */
2356 0, /* tp_descr_get */
2357 0, /* tp_descr_set */
2358 0, /* tp_dictoffset */
Serhiy Storchaka12f43342020-07-20 15:53:55 +03002359 (initproc)bytearray___init__, /* tp_init */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002360 PyType_GenericAlloc, /* tp_alloc */
2361 PyType_GenericNew, /* tp_new */
2362 PyObject_Del, /* tp_free */
2363};
2364
2365/*********************** Bytes Iterator ****************************/
2366
2367typedef struct {
2368 PyObject_HEAD
2369 Py_ssize_t it_index;
2370 PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
2371} bytesiterobject;
2372
2373static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002374bytearrayiter_dealloc(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002375{
2376 _PyObject_GC_UNTRACK(it);
2377 Py_XDECREF(it->it_seq);
2378 PyObject_GC_Del(it);
2379}
2380
2381static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002382bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002383{
2384 Py_VISIT(it->it_seq);
2385 return 0;
2386}
2387
2388static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002389bytearrayiter_next(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002390{
2391 PyByteArrayObject *seq;
2392 PyObject *item;
2393
2394 assert(it != NULL);
2395 seq = it->it_seq;
2396 if (seq == NULL)
2397 return NULL;
2398 assert(PyByteArray_Check(seq));
2399
2400 if (it->it_index < PyByteArray_GET_SIZE(seq)) {
2401 item = PyLong_FromLong(
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002402 (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002403 if (item != NULL)
2404 ++it->it_index;
2405 return item;
2406 }
2407
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002408 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03002409 Py_DECREF(seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002410 return NULL;
2411}
2412
2413static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302414bytearrayiter_length_hint(bytesiterobject *it, PyObject *Py_UNUSED(ignored))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002415{
2416 Py_ssize_t len = 0;
Serhiy Storchakaaf658722016-07-03 14:41:36 +03002417 if (it->it_seq) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002418 len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
Serhiy Storchakaaf658722016-07-03 14:41:36 +03002419 if (len < 0) {
2420 len = 0;
2421 }
2422 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002423 return PyLong_FromSsize_t(len);
2424}
2425
2426PyDoc_STRVAR(length_hint_doc,
2427 "Private method returning an estimate of len(list(it)).");
2428
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002429static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302430bytearrayiter_reduce(bytesiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002431{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002432 _Py_IDENTIFIER(iter);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002433 if (it->it_seq != NULL) {
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002434 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002435 it->it_seq, it->it_index);
2436 } else {
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002437 return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002438 }
2439}
2440
2441static PyObject *
2442bytearrayiter_setstate(bytesiterobject *it, PyObject *state)
2443{
2444 Py_ssize_t index = PyLong_AsSsize_t(state);
2445 if (index == -1 && PyErr_Occurred())
2446 return NULL;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00002447 if (it->it_seq != NULL) {
2448 if (index < 0)
2449 index = 0;
2450 else if (index > PyByteArray_GET_SIZE(it->it_seq))
2451 index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */
2452 it->it_index = index;
2453 }
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002454 Py_RETURN_NONE;
2455}
2456
2457PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
2458
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002459static PyMethodDef bytearrayiter_methods[] = {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002460 {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002461 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002462 {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002463 bytearray_reduce__doc__},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002464 {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
2465 setstate_doc},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002466 {NULL, NULL} /* sentinel */
2467};
2468
2469PyTypeObject PyByteArrayIter_Type = {
2470 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2471 "bytearray_iterator", /* tp_name */
2472 sizeof(bytesiterobject), /* tp_basicsize */
2473 0, /* tp_itemsize */
2474 /* methods */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002475 (destructor)bytearrayiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002476 0, /* tp_vectorcall_offset */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002477 0, /* tp_getattr */
2478 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002479 0, /* tp_as_async */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002480 0, /* tp_repr */
2481 0, /* tp_as_number */
2482 0, /* tp_as_sequence */
2483 0, /* tp_as_mapping */
2484 0, /* tp_hash */
2485 0, /* tp_call */
2486 0, /* tp_str */
2487 PyObject_GenericGetAttr, /* tp_getattro */
2488 0, /* tp_setattro */
2489 0, /* tp_as_buffer */
2490 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
2491 0, /* tp_doc */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002492 (traverseproc)bytearrayiter_traverse, /* tp_traverse */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002493 0, /* tp_clear */
2494 0, /* tp_richcompare */
2495 0, /* tp_weaklistoffset */
2496 PyObject_SelfIter, /* tp_iter */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002497 (iternextfunc)bytearrayiter_next, /* tp_iternext */
2498 bytearrayiter_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002499 0,
2500};
2501
2502static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002503bytearray_iter(PyObject *seq)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002504{
2505 bytesiterobject *it;
2506
2507 if (!PyByteArray_Check(seq)) {
2508 PyErr_BadInternalCall();
2509 return NULL;
2510 }
2511 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
2512 if (it == NULL)
2513 return NULL;
2514 it->it_index = 0;
2515 Py_INCREF(seq);
2516 it->it_seq = (PyByteArrayObject *)seq;
2517 _PyObject_GC_TRACK(it);
2518 return (PyObject *)it;
2519}