blob: 1ab9621b1f2656a150e6ebb60ae54da01336dff7 [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
Serhiy Storchaka2ad93822020-12-03 12:46:16 +020016/* For PyByteArray_AS_STRING(). */
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000017char _PyByteArray_empty_string[] = "";
Christian Heimes2c9c7a52008-05-26 13:42:13 +000018
Christian Heimes2c9c7a52008-05-26 13:42:13 +000019/* Helpers */
20
21static int
22_getbytevalue(PyObject* arg, int *value)
23{
Serhiy Storchakae67f7db2020-06-29 22:36:41 +030024 int overflow;
25 long face_value = PyLong_AsLongAndOverflow(arg, &overflow);
Christian Heimes2c9c7a52008-05-26 13:42:13 +000026
Serhiy Storchakae67f7db2020-06-29 22:36:41 +030027 if (face_value == -1 && PyErr_Occurred()) {
28 *value = -1;
29 return 0;
Georg Brandl9a54d7c2008-07-16 23:15:30 +000030 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +000031 if (face_value < 0 || face_value >= 256) {
Serhiy Storchakae67f7db2020-06-29 22:36:41 +030032 /* this includes an overflow in converting to C long */
Georg Brandl9a54d7c2008-07-16 23:15:30 +000033 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
Mark Dickinson10de93a2010-07-09 19:25:48 +000034 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000035 return 0;
36 }
37
38 *value = face_value;
39 return 1;
40}
41
42static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +000043bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000044{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000045 void *ptr;
46 if (view == NULL) {
Stefan Krah5178d912015-02-03 16:57:21 +010047 PyErr_SetString(PyExc_BufferError,
48 "bytearray_getbuffer: view==NULL argument is obsolete");
49 return -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000050 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000051 ptr = (void *) PyByteArray_AS_STRING(obj);
Stefan Krah5178d912015-02-03 16:57:21 +010052 /* cannot fail if view != NULL and readonly == 0 */
53 (void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
54 obj->ob_exports++;
55 return 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000056}
57
58static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +000059bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000060{
61 obj->ob_exports--;
62}
63
Antoine Pitrou5504e892008-12-06 21:27:53 +000064static int
65_canresize(PyByteArrayObject *self)
66{
67 if (self->ob_exports > 0) {
68 PyErr_SetString(PyExc_BufferError,
69 "Existing exports of data: object cannot be re-sized");
70 return 0;
71 }
72 return 1;
73}
74
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030075#include "clinic/bytearrayobject.c.h"
76
Christian Heimes2c9c7a52008-05-26 13:42:13 +000077/* Direct API functions */
78
79PyObject *
80PyByteArray_FromObject(PyObject *input)
81{
Petr Viktorinffd97532020-02-11 17:46:57 +010082 return PyObject_CallOneArg((PyObject *)&PyByteArray_Type, input);
Christian Heimes2c9c7a52008-05-26 13:42:13 +000083}
84
Serhiy Storchakaa2314282017-10-29 02:11:54 +030085static PyObject *
86_PyByteArray_FromBufferObject(PyObject *obj)
87{
88 PyObject *result;
89 Py_buffer view;
90
91 if (PyObject_GetBuffer(obj, &view, PyBUF_FULL_RO) < 0) {
92 return NULL;
93 }
94 result = PyByteArray_FromStringAndSize(NULL, view.len);
95 if (result != NULL &&
96 PyBuffer_ToContiguous(PyByteArray_AS_STRING(result),
97 &view, view.len, 'C') < 0)
98 {
99 Py_CLEAR(result);
100 }
101 PyBuffer_Release(&view);
102 return result;
103}
104
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000105PyObject *
106PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
107{
108 PyByteArrayObject *new;
109 Py_ssize_t alloc;
110
111 if (size < 0) {
112 PyErr_SetString(PyExc_SystemError,
113 "Negative size passed to PyByteArray_FromStringAndSize");
114 return NULL;
115 }
116
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000117 /* Prevent buffer overflow when setting alloc to size+1. */
118 if (size == PY_SSIZE_T_MAX) {
119 return PyErr_NoMemory();
120 }
121
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000122 new = PyObject_New(PyByteArrayObject, &PyByteArray_Type);
123 if (new == NULL)
124 return NULL;
125
126 if (size == 0) {
127 new->ob_bytes = NULL;
128 alloc = 0;
129 }
130 else {
131 alloc = size + 1;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100132 new->ob_bytes = PyObject_Malloc(alloc);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000133 if (new->ob_bytes == NULL) {
134 Py_DECREF(new);
135 return PyErr_NoMemory();
136 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +0000137 if (bytes != NULL && size > 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000138 memcpy(new->ob_bytes, bytes, size);
139 new->ob_bytes[size] = '\0'; /* Trailing null byte */
140 }
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100141 Py_SET_SIZE(new, size);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000142 new->ob_alloc = alloc;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200143 new->ob_start = new->ob_bytes;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000144 new->ob_exports = 0;
145
146 return (PyObject *)new;
147}
148
149Py_ssize_t
150PyByteArray_Size(PyObject *self)
151{
152 assert(self != NULL);
153 assert(PyByteArray_Check(self));
154
155 return PyByteArray_GET_SIZE(self);
156}
157
158char *
159PyByteArray_AsString(PyObject *self)
160{
161 assert(self != NULL);
162 assert(PyByteArray_Check(self));
163
164 return PyByteArray_AS_STRING(self);
165}
166
167int
Antoine Pitroucc231542014-11-02 18:40:09 +0100168PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000169{
170 void *sval;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200171 PyByteArrayObject *obj = ((PyByteArrayObject *)self);
Antoine Pitroucc231542014-11-02 18:40:09 +0100172 /* All computations are done unsigned to avoid integer overflows
173 (see issue #22335). */
174 size_t alloc = (size_t) obj->ob_alloc;
175 size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes);
176 size_t size = (size_t) requested_size;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000177
178 assert(self != NULL);
179 assert(PyByteArray_Check(self));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200180 assert(logical_offset <= alloc);
Antoine Pitroucc231542014-11-02 18:40:09 +0100181 assert(requested_size >= 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000182
Antoine Pitroucc231542014-11-02 18:40:09 +0100183 if (requested_size == Py_SIZE(self)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000184 return 0;
185 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200186 if (!_canresize(obj)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000187 return -1;
188 }
189
Antoine Pitrou25454112015-05-19 20:52:27 +0200190 if (size + logical_offset + 1 <= alloc) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200191 /* Current buffer is large enough to host the requested size,
192 decide on a strategy. */
193 if (size < alloc / 2) {
194 /* Major downsize; resize down to exact size */
195 alloc = size + 1;
196 }
197 else {
198 /* Minor downsize; quick exit */
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100199 Py_SET_SIZE(self, size);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200200 PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */
201 return 0;
202 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000203 }
204 else {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200205 /* Need growing, decide on a strategy */
206 if (size <= alloc * 1.125) {
207 /* Moderate upsize; overallocate similar to list_resize() */
208 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
209 }
210 else {
211 /* Major upsize; resize up to exact size */
212 alloc = size + 1;
213 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000214 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100215 if (alloc > PY_SSIZE_T_MAX) {
216 PyErr_NoMemory();
217 return -1;
218 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000219
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200220 if (logical_offset > 0) {
221 sval = PyObject_Malloc(alloc);
222 if (sval == NULL) {
223 PyErr_NoMemory();
224 return -1;
225 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100226 memcpy(sval, PyByteArray_AS_STRING(self),
Stefan Krahdce65022017-08-25 20:12:05 +0200227 Py_MIN((size_t)requested_size, (size_t)Py_SIZE(self)));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200228 PyObject_Free(obj->ob_bytes);
229 }
230 else {
231 sval = PyObject_Realloc(obj->ob_bytes, alloc);
232 if (sval == NULL) {
233 PyErr_NoMemory();
234 return -1;
235 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000236 }
237
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200238 obj->ob_bytes = obj->ob_start = sval;
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100239 Py_SET_SIZE(self, size);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200240 obj->ob_alloc = alloc;
241 obj->ob_bytes[size] = '\0'; /* Trailing null byte */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000242
243 return 0;
244}
245
246PyObject *
247PyByteArray_Concat(PyObject *a, PyObject *b)
248{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000249 Py_buffer va, vb;
250 PyByteArrayObject *result = NULL;
251
252 va.len = -1;
253 vb.len = -1;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200254 if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 ||
255 PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000256 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
Serhiy Storchaka6b5a9ec2017-03-19 19:47:02 +0200257 Py_TYPE(b)->tp_name, Py_TYPE(a)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000258 goto done;
259 }
260
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300261 if (va.len > PY_SSIZE_T_MAX - vb.len) {
262 PyErr_NoMemory();
263 goto done;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000264 }
265
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300266 result = (PyByteArrayObject *) \
267 PyByteArray_FromStringAndSize(NULL, va.len + vb.len);
Serhiy Storchaka2ad93822020-12-03 12:46:16 +0200268 // result->ob_bytes is NULL if result is an empty bytearray:
stratakis61fc23c2020-07-08 22:39:41 +0200269 // if va.len + vb.len equals zero.
270 if (result != NULL && result->ob_bytes != NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000271 memcpy(result->ob_bytes, va.buf, va.len);
272 memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
273 }
274
275 done:
276 if (va.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000277 PyBuffer_Release(&va);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000278 if (vb.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000279 PyBuffer_Release(&vb);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000280 return (PyObject *)result;
281}
282
283/* Functions stuffed into the type object */
284
285static Py_ssize_t
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000286bytearray_length(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000287{
288 return Py_SIZE(self);
289}
290
291static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000292bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000293{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000294 Py_ssize_t size;
295 Py_buffer vo;
296
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200297 if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000298 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
299 Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name);
300 return NULL;
301 }
302
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300303 size = Py_SIZE(self);
304 if (size > PY_SSIZE_T_MAX - vo.len) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000305 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000306 return PyErr_NoMemory();
307 }
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300308 if (PyByteArray_Resize((PyObject *)self, size + vo.len) < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000309 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000310 return NULL;
311 }
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300312 memcpy(PyByteArray_AS_STRING(self) + size, vo.buf, vo.len);
Martin v. Löwis423be952008-08-13 15:53:07 +0000313 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000314 Py_INCREF(self);
315 return (PyObject *)self;
316}
317
318static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000319bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000320{
321 PyByteArrayObject *result;
322 Py_ssize_t mysize;
323 Py_ssize_t size;
Tobias Holl61d8c542021-01-13 17:16:40 +0100324 const char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000325
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);
Tobias Holl61d8c542021-01-13 17:16:40 +0100333 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000334 if (result != NULL && size != 0) {
335 if (mysize == 1)
Tobias Holl61d8c542021-01-13 17:16:40 +0100336 memset(result->ob_bytes, buf[0], size);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000337 else {
338 Py_ssize_t i;
339 for (i = 0; i < count; i++)
Tobias Holl61d8c542021-01-13 17:16:40 +0100340 memcpy(result->ob_bytes + i*mysize, buf, mysize);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000341 }
342 }
343 return (PyObject *)result;
344}
345
346static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000347bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000348{
349 Py_ssize_t mysize;
350 Py_ssize_t size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200351 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000352
353 if (count < 0)
354 count = 0;
355 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000356 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000357 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000358 size = mysize * count;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200359 if (PyByteArray_Resize((PyObject *)self, size) < 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000360 return NULL;
361
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200362 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000363 if (mysize == 1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200364 memset(buf, buf[0], size);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000365 else {
366 Py_ssize_t i;
367 for (i = 1; i < count; i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200368 memcpy(buf + i*mysize, buf, mysize);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000369 }
370
371 Py_INCREF(self);
372 return (PyObject *)self;
373}
374
375static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000376bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000377{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000378 if (i < 0 || i >= Py_SIZE(self)) {
379 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
380 return NULL;
381 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200382 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000383}
384
385static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000386bytearray_subscript(PyByteArrayObject *self, PyObject *index)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000387{
Victor Stinnera15e2602020-04-08 02:01:56 +0200388 if (_PyIndex_Check(index)) {
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000389 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000390
391 if (i == -1 && PyErr_Occurred())
392 return NULL;
393
394 if (i < 0)
395 i += PyByteArray_GET_SIZE(self);
396
397 if (i < 0 || i >= Py_SIZE(self)) {
398 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
399 return NULL;
400 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200401 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000402 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000403 else if (PySlice_Check(index)) {
Zackery Spytz14514d92019-05-17 01:13:03 -0600404 Py_ssize_t start, stop, step, slicelength, i;
405 size_t cur;
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300406 if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000407 return NULL;
408 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300409 slicelength = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self),
410 &start, &stop, step);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000411
412 if (slicelength <= 0)
413 return PyByteArray_FromStringAndSize("", 0);
414 else if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200415 return PyByteArray_FromStringAndSize(
416 PyByteArray_AS_STRING(self) + start, slicelength);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000417 }
418 else {
419 char *source_buf = PyByteArray_AS_STRING(self);
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000420 char *result_buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000421 PyObject *result;
422
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000423 result = PyByteArray_FromStringAndSize(NULL, slicelength);
424 if (result == NULL)
425 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000426
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000427 result_buf = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000428 for (cur = start, i = 0; i < slicelength;
429 cur += step, i++) {
430 result_buf[i] = source_buf[cur];
431 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000432 return result;
433 }
434 }
435 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400436 PyErr_Format(PyExc_TypeError,
437 "bytearray indices must be integers or slices, not %.200s",
438 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000439 return NULL;
440 }
441}
442
443static int
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200444bytearray_setslice_linear(PyByteArrayObject *self,
445 Py_ssize_t lo, Py_ssize_t hi,
446 char *bytes, Py_ssize_t bytes_len)
447{
448 Py_ssize_t avail = hi - lo;
449 char *buf = PyByteArray_AS_STRING(self);
450 Py_ssize_t growth = bytes_len - avail;
Victor Stinner84557232013-11-21 12:29:51 +0100451 int res = 0;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200452 assert(avail >= 0);
453
Victor Stinner84557232013-11-21 12:29:51 +0100454 if (growth < 0) {
455 if (!_canresize(self))
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200456 return -1;
Victor Stinner84557232013-11-21 12:29:51 +0100457
458 if (lo == 0) {
459 /* Shrink the buffer by advancing its logical start */
460 self->ob_start -= growth;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200461 /*
Victor Stinner84557232013-11-21 12:29:51 +0100462 0 lo hi old_size
463 | |<----avail----->|<-----tail------>|
464 | |<-bytes_len->|<-----tail------>|
465 0 new_lo new_hi new_size
466 */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200467 }
Victor Stinner84557232013-11-21 12:29:51 +0100468 else {
469 /*
470 0 lo hi old_size
471 | |<----avail----->|<-----tomove------>|
472 | |<-bytes_len->|<-----tomove------>|
473 0 lo new_hi new_size
474 */
475 memmove(buf + lo + bytes_len, buf + hi,
476 Py_SIZE(self) - hi);
477 }
478 if (PyByteArray_Resize((PyObject *)self,
479 Py_SIZE(self) + growth) < 0) {
480 /* Issue #19578: Handling the memory allocation failure here is
481 tricky here because the bytearray object has already been
482 modified. Depending on growth and lo, the behaviour is
483 different.
484
485 If growth < 0 and lo != 0, the operation is completed, but a
486 MemoryError is still raised and the memory block is not
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700487 shrunk. Otherwise, the bytearray is restored in its previous
Victor Stinner84557232013-11-21 12:29:51 +0100488 state and a MemoryError is raised. */
489 if (lo == 0) {
490 self->ob_start += growth;
491 return -1;
492 }
493 /* memmove() removed bytes, the bytearray object cannot be
494 restored in its previous state. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100495 Py_SET_SIZE(self, Py_SIZE(self) + growth);
Victor Stinner84557232013-11-21 12:29:51 +0100496 res = -1;
497 }
498 buf = PyByteArray_AS_STRING(self);
499 }
500 else if (growth > 0) {
501 if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) {
502 PyErr_NoMemory();
503 return -1;
504 }
505
506 if (PyByteArray_Resize((PyObject *)self,
507 Py_SIZE(self) + growth) < 0) {
508 return -1;
509 }
510 buf = PyByteArray_AS_STRING(self);
511 /* Make the place for the additional bytes */
512 /*
513 0 lo hi old_size
514 | |<-avail->|<-----tomove------>|
515 | |<---bytes_len-->|<-----tomove------>|
516 0 lo new_hi new_size
517 */
518 memmove(buf + lo + bytes_len, buf + hi,
519 Py_SIZE(self) - lo - bytes_len);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200520 }
521
522 if (bytes_len > 0)
523 memcpy(buf + lo, bytes, bytes_len);
Victor Stinner84557232013-11-21 12:29:51 +0100524 return res;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200525}
526
527static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000528bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000529 PyObject *values)
530{
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200531 Py_ssize_t needed;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000532 void *bytes;
533 Py_buffer vbytes;
534 int res = 0;
535
536 vbytes.len = -1;
537 if (values == (PyObject *)self) {
538 /* Make a copy and call this function recursively */
539 int err;
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300540 values = PyByteArray_FromStringAndSize(PyByteArray_AS_STRING(values),
541 PyByteArray_GET_SIZE(values));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000542 if (values == NULL)
543 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000544 err = bytearray_setslice(self, lo, hi, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000545 Py_DECREF(values);
546 return err;
547 }
548 if (values == NULL) {
549 /* del b[lo:hi] */
550 bytes = NULL;
551 needed = 0;
552 }
553 else {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200554 if (PyObject_GetBuffer(values, &vbytes, PyBUF_SIMPLE) != 0) {
555 PyErr_Format(PyExc_TypeError,
556 "can't set bytearray slice from %.100s",
557 Py_TYPE(values)->tp_name);
558 return -1;
559 }
560 needed = vbytes.len;
561 bytes = vbytes.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000562 }
563
564 if (lo < 0)
565 lo = 0;
566 if (hi < lo)
567 hi = lo;
568 if (hi > Py_SIZE(self))
569 hi = Py_SIZE(self);
570
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200571 res = bytearray_setslice_linear(self, lo, hi, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000572 if (vbytes.len != -1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200573 PyBuffer_Release(&vbytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000574 return res;
575}
576
577static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000578bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000579{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000580 int ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000581
582 if (i < 0)
583 i += Py_SIZE(self);
584
585 if (i < 0 || i >= Py_SIZE(self)) {
586 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
587 return -1;
588 }
589
590 if (value == NULL)
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000591 return bytearray_setslice(self, i, i+1, NULL);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000592
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000593 if (!_getbytevalue(value, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000594 return -1;
595
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200596 PyByteArray_AS_STRING(self)[i] = ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000597 return 0;
598}
599
600static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000601bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000602{
603 Py_ssize_t start, stop, step, slicelen, needed;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200604 char *buf, *bytes;
605 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000606
Victor Stinnera15e2602020-04-08 02:01:56 +0200607 if (_PyIndex_Check(index)) {
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000608 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000609
610 if (i == -1 && PyErr_Occurred())
611 return -1;
612
613 if (i < 0)
614 i += PyByteArray_GET_SIZE(self);
615
616 if (i < 0 || i >= Py_SIZE(self)) {
617 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
618 return -1;
619 }
620
621 if (values == NULL) {
622 /* Fall through to slice assignment */
623 start = i;
624 stop = i + 1;
625 step = 1;
626 slicelen = 1;
627 }
628 else {
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000629 int ival;
630 if (!_getbytevalue(values, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000631 return -1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200632 buf[i] = (char)ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000633 return 0;
634 }
635 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000636 else if (PySlice_Check(index)) {
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300637 if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000638 return -1;
639 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300640 slicelen = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self), &start,
641 &stop, step);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000642 }
643 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400644 PyErr_Format(PyExc_TypeError,
645 "bytearray indices must be integers or slices, not %.200s",
646 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000647 return -1;
648 }
649
650 if (values == NULL) {
651 bytes = NULL;
652 needed = 0;
653 }
654 else if (values == (PyObject *)self || !PyByteArray_Check(values)) {
Christian Heimes6d26ade2012-11-03 23:07:59 +0100655 int err;
Ezio Melottic64bcbe2012-11-03 21:19:06 +0200656 if (PyNumber_Check(values) || PyUnicode_Check(values)) {
657 PyErr_SetString(PyExc_TypeError,
658 "can assign only bytes, buffers, or iterables "
659 "of ints in range(0, 256)");
660 return -1;
661 }
Georg Brandlf3fa5682010-12-04 17:09:30 +0000662 /* Make a copy and call this function recursively */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000663 values = PyByteArray_FromObject(values);
664 if (values == NULL)
665 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000666 err = bytearray_ass_subscript(self, index, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000667 Py_DECREF(values);
668 return err;
669 }
670 else {
671 assert(PyByteArray_Check(values));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200672 bytes = PyByteArray_AS_STRING(values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000673 needed = Py_SIZE(values);
674 }
675 /* Make sure b[5:2] = ... inserts before 5, not before 2. */
676 if ((step < 0 && start < stop) ||
677 (step > 0 && start > stop))
678 stop = start;
679 if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200680 return bytearray_setslice_linear(self, start, stop, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000681 }
682 else {
683 if (needed == 0) {
684 /* Delete slice */
Mark Dickinsonbc099642010-01-29 17:27:24 +0000685 size_t cur;
686 Py_ssize_t i;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000687
Antoine Pitrou5504e892008-12-06 21:27:53 +0000688 if (!_canresize(self))
689 return -1;
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000690
691 if (slicelen == 0)
692 /* Nothing to do here. */
693 return 0;
694
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000695 if (step < 0) {
696 stop = start + 1;
697 start = stop + step * (slicelen - 1) - 1;
698 step = -step;
699 }
700 for (cur = start, i = 0;
701 i < slicelen; cur += step, i++) {
702 Py_ssize_t lim = step - 1;
703
Mark Dickinson66f575b2010-02-14 12:53:32 +0000704 if (cur + step >= (size_t)PyByteArray_GET_SIZE(self))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000705 lim = PyByteArray_GET_SIZE(self) - cur - 1;
706
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200707 memmove(buf + cur - i,
708 buf + cur + 1, lim);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000709 }
710 /* Move the tail of the bytes, in one chunk */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000711 cur = start + (size_t)slicelen*step;
Mark Dickinson66f575b2010-02-14 12:53:32 +0000712 if (cur < (size_t)PyByteArray_GET_SIZE(self)) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200713 memmove(buf + cur - slicelen,
714 buf + cur,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000715 PyByteArray_GET_SIZE(self) - cur);
716 }
717 if (PyByteArray_Resize((PyObject *)self,
718 PyByteArray_GET_SIZE(self) - slicelen) < 0)
719 return -1;
720
721 return 0;
722 }
723 else {
724 /* Assign slice */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000725 Py_ssize_t i;
726 size_t cur;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000727
728 if (needed != slicelen) {
729 PyErr_Format(PyExc_ValueError,
730 "attempt to assign bytes of size %zd "
731 "to extended slice of size %zd",
732 needed, slicelen);
733 return -1;
734 }
735 for (cur = start, i = 0; i < slicelen; cur += step, i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200736 buf[cur] = bytes[i];
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000737 return 0;
738 }
739 }
740}
741
Serhiy Storchaka12f43342020-07-20 15:53:55 +0300742/*[clinic input]
743bytearray.__init__
744
745 source as arg: object = NULL
746 encoding: str = NULL
747 errors: str = NULL
748
749[clinic start generated code]*/
750
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000751static int
Serhiy Storchaka12f43342020-07-20 15:53:55 +0300752bytearray___init___impl(PyByteArrayObject *self, PyObject *arg,
753 const char *encoding, const char *errors)
754/*[clinic end generated code: output=4ce1304649c2f8b3 input=1141a7122eefd7b9]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000755{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000756 Py_ssize_t count;
757 PyObject *it;
758 PyObject *(*iternext)(PyObject *);
759
760 if (Py_SIZE(self) != 0) {
761 /* Empty previous contents (yes, do this first of all!) */
762 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
763 return -1;
764 }
765
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000766 /* Make a quick exit if no first argument */
767 if (arg == NULL) {
768 if (encoding != NULL || errors != NULL) {
769 PyErr_SetString(PyExc_TypeError,
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300770 encoding != NULL ?
771 "encoding without a string argument" :
772 "errors without a string argument");
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000773 return -1;
774 }
775 return 0;
776 }
777
778 if (PyUnicode_Check(arg)) {
779 /* Encode via the codec registry */
780 PyObject *encoded, *new;
781 if (encoding == NULL) {
782 PyErr_SetString(PyExc_TypeError,
783 "string argument without an encoding");
784 return -1;
785 }
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000786 encoded = PyUnicode_AsEncodedString(arg, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000787 if (encoded == NULL)
788 return -1;
789 assert(PyBytes_Check(encoded));
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000790 new = bytearray_iconcat(self, encoded);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000791 Py_DECREF(encoded);
792 if (new == NULL)
793 return -1;
794 Py_DECREF(new);
795 return 0;
796 }
797
798 /* If it's not unicode, there can't be encoding or errors */
799 if (encoding != NULL || errors != NULL) {
800 PyErr_SetString(PyExc_TypeError,
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300801 encoding != NULL ?
802 "encoding without a string argument" :
803 "errors without a string argument");
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000804 return -1;
805 }
806
807 /* Is it an int? */
Victor Stinnera15e2602020-04-08 02:01:56 +0200808 if (_PyIndex_Check(arg)) {
Serhiy Storchakaeb249882016-08-15 09:46:07 +0300809 count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
810 if (count == -1 && PyErr_Occurred()) {
Serhiy Storchakae8904212018-10-15 00:02:57 +0300811 if (!PyErr_ExceptionMatches(PyExc_TypeError))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000812 return -1;
INADA Naokia634e232017-01-06 17:32:01 +0900813 PyErr_Clear(); /* fall through */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000814 }
INADA Naokia634e232017-01-06 17:32:01 +0900815 else {
816 if (count < 0) {
817 PyErr_SetString(PyExc_ValueError, "negative count");
818 return -1;
819 }
820 if (count > 0) {
821 if (PyByteArray_Resize((PyObject *)self, count))
822 return -1;
823 memset(PyByteArray_AS_STRING(self), 0, count);
824 }
825 return 0;
826 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000827 }
828
829 /* Use the buffer API */
830 if (PyObject_CheckBuffer(arg)) {
831 Py_ssize_t size;
832 Py_buffer view;
833 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
834 return -1;
835 size = view.len;
836 if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200837 if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self),
838 &view, size, 'C') < 0)
Stefan Krah7d12d9d2012-07-28 12:25:55 +0200839 goto fail;
Martin v. Löwis423be952008-08-13 15:53:07 +0000840 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000841 return 0;
842 fail:
Martin v. Löwis423be952008-08-13 15:53:07 +0000843 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000844 return -1;
845 }
846
847 /* XXX Optimize this if the arguments is a list, tuple */
848
849 /* Get the iterator */
850 it = PyObject_GetIter(arg);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300851 if (it == NULL) {
852 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
853 PyErr_Format(PyExc_TypeError,
854 "cannot convert '%.200s' object to bytearray",
Victor Stinner58ac7002020-02-07 03:04:21 +0100855 Py_TYPE(arg)->tp_name);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300856 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000857 return -1;
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300858 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000859 iternext = *Py_TYPE(it)->tp_iternext;
860
861 /* Run the iterator to exhaustion */
862 for (;;) {
863 PyObject *item;
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000864 int rc, value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000865
866 /* Get the next item */
867 item = iternext(it);
868 if (item == NULL) {
869 if (PyErr_Occurred()) {
870 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
871 goto error;
872 PyErr_Clear();
873 }
874 break;
875 }
876
877 /* Interpret it as an int (__index__) */
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000878 rc = _getbytevalue(item, &value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000879 Py_DECREF(item);
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000880 if (!rc)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000881 goto error;
882
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000883 /* Append the byte */
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300884 if (Py_SIZE(self) + 1 < self->ob_alloc) {
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100885 Py_SET_SIZE(self, Py_SIZE(self) + 1);
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300886 PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0';
887 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000888 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
889 goto error;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200890 PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000891 }
892
893 /* Clean up and return success */
894 Py_DECREF(it);
895 return 0;
896
897 error:
898 /* Error handling when it != NULL */
899 Py_DECREF(it);
900 return -1;
901}
902
903/* Mostly copied from string_repr, but without the
904 "smart quote" functionality. */
905static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000906bytearray_repr(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000907{
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300908 const char *className = _PyType_Name(Py_TYPE(self));
909 const char *quote_prefix = "(b";
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000910 const char *quote_postfix = ")";
911 Py_ssize_t length = Py_SIZE(self);
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300912 /* 6 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
913 Py_ssize_t newsize;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000914 PyObject *v;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200915 Py_ssize_t i;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200916 char *bytes;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200917 char c;
918 char *p;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200919 int quote;
920 char *test, *start;
921 char *buffer;
922
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300923 newsize = strlen(className);
924 if (length > (PY_SSIZE_T_MAX - 6 - newsize) / 4) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000925 PyErr_SetString(PyExc_OverflowError,
926 "bytearray object is too large to make repr");
927 return NULL;
928 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200929
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300930 newsize += 6 + length * 4;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100931 buffer = PyObject_Malloc(newsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200932 if (buffer == NULL) {
933 PyErr_NoMemory();
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000934 return NULL;
935 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000936
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200937 /* Figure out which quote to use; single is preferred */
938 quote = '\'';
939 start = PyByteArray_AS_STRING(self);
940 for (test = start; test < start+length; ++test) {
941 if (*test == '"') {
942 quote = '\''; /* back to single */
943 break;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000944 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200945 else if (*test == '\'')
946 quote = '"';
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000947 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200948
949 p = buffer;
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300950 while (*className)
951 *p++ = *className++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200952 while (*quote_prefix)
953 *p++ = *quote_prefix++;
954 *p++ = quote;
955
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200956 bytes = PyByteArray_AS_STRING(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200957 for (i = 0; i < length; i++) {
958 /* There's at least enough room for a hex escape
959 and a closing quote. */
960 assert(newsize - (p - buffer) >= 5);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200961 c = bytes[i];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200962 if (c == '\'' || c == '\\')
963 *p++ = '\\', *p++ = c;
964 else if (c == '\t')
965 *p++ = '\\', *p++ = 't';
966 else if (c == '\n')
967 *p++ = '\\', *p++ = 'n';
968 else if (c == '\r')
969 *p++ = '\\', *p++ = 'r';
970 else if (c == 0)
971 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
972 else if (c < ' ' || c >= 0x7f) {
973 *p++ = '\\';
974 *p++ = 'x';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200975 *p++ = Py_hexdigits[(c & 0xf0) >> 4];
976 *p++ = Py_hexdigits[c & 0xf];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200977 }
978 else
979 *p++ = c;
980 }
981 assert(newsize - (p - buffer) >= 1);
982 *p++ = quote;
983 while (*quote_postfix) {
984 *p++ = *quote_postfix++;
985 }
986
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300987 v = PyUnicode_FromStringAndSize(buffer, p - buffer);
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100988 PyObject_Free(buffer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200989 return v;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000990}
991
992static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000993bytearray_str(PyObject *op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000994{
Victor Stinnerda7933e2020-04-13 03:04:28 +0200995 if (_Py_GetConfig()->bytes_warning) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200996 if (PyErr_WarnEx(PyExc_BytesWarning,
997 "str() on a bytearray instance", 1)) {
998 return NULL;
Alexander Belopolskyf0f45142010-08-11 17:31:17 +0000999 }
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001000 }
1001 return bytearray_repr((PyByteArrayObject*)op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001002}
1003
1004static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001005bytearray_richcompare(PyObject *self, PyObject *other, int op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001006{
1007 Py_ssize_t self_size, other_size;
1008 Py_buffer self_bytes, other_bytes;
Serhiy Storchaka313467e2020-11-22 22:00:53 +02001009 int cmp;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001010
Serhiy Storchaka313467e2020-11-22 22:00:53 +02001011 if (!PyObject_CheckBuffer(self) || !PyObject_CheckBuffer(other)) {
1012 if (PyUnicode_Check(self) || PyUnicode_Check(other)) {
1013 if (_Py_GetConfig()->bytes_warning && (op == Py_EQ || op == Py_NE)) {
1014 if (PyErr_WarnEx(PyExc_BytesWarning,
1015 "Comparison between bytearray and string", 1))
1016 return NULL;
1017 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001018 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001019 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001020 }
1021
Serhiy Storchaka2ad93822020-12-03 12:46:16 +02001022 /* Bytearrays can be compared to anything that supports the buffer API. */
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001023 if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001024 PyErr_Clear();
Brian Curtindfc80e32011-08-10 20:28:54 -05001025 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001026 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001027 self_size = self_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001028
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001029 if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001030 PyErr_Clear();
Martin v. Löwis423be952008-08-13 15:53:07 +00001031 PyBuffer_Release(&self_bytes);
Brian Curtindfc80e32011-08-10 20:28:54 -05001032 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001033 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001034 other_size = other_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001035
1036 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
1037 /* Shortcut: if the lengths differ, the objects differ */
stratakise8b19652017-11-02 11:32:54 +01001038 PyBuffer_Release(&self_bytes);
1039 PyBuffer_Release(&other_bytes);
1040 return PyBool_FromLong((op == Py_NE));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001041 }
1042 else {
stratakise8b19652017-11-02 11:32:54 +01001043 cmp = memcmp(self_bytes.buf, other_bytes.buf,
1044 Py_MIN(self_size, other_size));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001045 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
1046
stratakise8b19652017-11-02 11:32:54 +01001047 PyBuffer_Release(&self_bytes);
1048 PyBuffer_Release(&other_bytes);
1049
1050 if (cmp != 0) {
1051 Py_RETURN_RICHCOMPARE(cmp, 0, op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001052 }
1053
stratakise8b19652017-11-02 11:32:54 +01001054 Py_RETURN_RICHCOMPARE(self_size, other_size, op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001055 }
1056
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001057}
1058
1059static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001060bytearray_dealloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001061{
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001062 if (self->ob_exports > 0) {
1063 PyErr_SetString(PyExc_SystemError,
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001064 "deallocated bytearray object has exported buffers");
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001065 PyErr_Print();
1066 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001067 if (self->ob_bytes != 0) {
Antoine Pitrou39aba4f2011-11-12 21:15:28 +01001068 PyObject_Free(self->ob_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001069 }
1070 Py_TYPE(self)->tp_free((PyObject *)self);
1071}
1072
1073
1074/* -------------------------------------------------------------------- */
1075/* Methods */
1076
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001077#define FASTSEARCH fastsearch
1078#define STRINGLIB(F) stringlib_##F
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001079#define STRINGLIB_CHAR char
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001080#define STRINGLIB_SIZEOF_CHAR 1
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001081#define STRINGLIB_LEN PyByteArray_GET_SIZE
1082#define STRINGLIB_STR PyByteArray_AS_STRING
1083#define STRINGLIB_NEW PyByteArray_FromStringAndSize
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001084#define STRINGLIB_ISSPACE Py_ISSPACE
1085#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001086#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1087#define STRINGLIB_MUTABLE 1
1088
1089#include "stringlib/fastsearch.h"
1090#include "stringlib/count.h"
1091#include "stringlib/find.h"
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001092#include "stringlib/join.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001093#include "stringlib/partition.h"
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001094#include "stringlib/split.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001095#include "stringlib/ctype.h"
1096#include "stringlib/transmogrify.h"
1097
1098
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001099static PyObject *
1100bytearray_find(PyByteArrayObject *self, PyObject *args)
1101{
1102 return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1103}
1104
1105static PyObject *
1106bytearray_count(PyByteArrayObject *self, PyObject *args)
1107{
1108 return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1109}
1110
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001111/*[clinic input]
1112bytearray.clear
1113
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001114Remove all items from the bytearray.
1115[clinic start generated code]*/
1116
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001117static PyObject *
1118bytearray_clear_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001119/*[clinic end generated code: output=85c2fe6aede0956c input=ed6edae9de447ac4]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001120{
1121 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1122 return NULL;
1123 Py_RETURN_NONE;
1124}
1125
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001126/*[clinic input]
1127bytearray.copy
1128
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001129Return a copy of B.
1130[clinic start generated code]*/
1131
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001132static PyObject *
1133bytearray_copy_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001134/*[clinic end generated code: output=68cfbcfed484c132 input=6597b0c01bccaa9e]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001135{
1136 return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self),
1137 PyByteArray_GET_SIZE(self));
1138}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001139
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001140static PyObject *
1141bytearray_index(PyByteArrayObject *self, PyObject *args)
1142{
1143 return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1144}
1145
1146static PyObject *
1147bytearray_rfind(PyByteArrayObject *self, PyObject *args)
1148{
1149 return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1150}
1151
1152static PyObject *
1153bytearray_rindex(PyByteArrayObject *self, PyObject *args)
1154{
1155 return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1156}
1157
1158static int
1159bytearray_contains(PyObject *self, PyObject *arg)
1160{
1161 return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg);
1162}
1163
1164static PyObject *
1165bytearray_startswith(PyByteArrayObject *self, PyObject *args)
1166{
1167 return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1168}
1169
1170static PyObject *
1171bytearray_endswith(PyByteArrayObject *self, PyObject *args)
1172{
1173 return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1174}
1175
sweeneydea81849b2020-04-22 17:05:48 -04001176/*[clinic input]
1177bytearray.removeprefix as bytearray_removeprefix
1178
1179 prefix: Py_buffer
1180 /
1181
1182Return a bytearray with the given prefix string removed if present.
1183
1184If the bytearray starts with the prefix string, return
1185bytearray[len(prefix):]. Otherwise, return a copy of the original
1186bytearray.
1187[clinic start generated code]*/
1188
1189static PyObject *
1190bytearray_removeprefix_impl(PyByteArrayObject *self, Py_buffer *prefix)
1191/*[clinic end generated code: output=6cabc585e7f502e0 input=968aada38aedd262]*/
1192{
1193 const char *self_start = PyByteArray_AS_STRING(self);
1194 Py_ssize_t self_len = PyByteArray_GET_SIZE(self);
1195 const char *prefix_start = prefix->buf;
1196 Py_ssize_t prefix_len = prefix->len;
1197
1198 if (self_len >= prefix_len
1199 && memcmp(self_start, prefix_start, prefix_len) == 0)
1200 {
1201 return PyByteArray_FromStringAndSize(self_start + prefix_len,
1202 self_len - prefix_len);
1203 }
1204
1205 return PyByteArray_FromStringAndSize(self_start, self_len);
1206}
1207
1208/*[clinic input]
1209bytearray.removesuffix as bytearray_removesuffix
1210
1211 suffix: Py_buffer
1212 /
1213
1214Return a bytearray with the given suffix string removed if present.
1215
1216If the bytearray ends with the suffix string and that suffix is not
1217empty, return bytearray[:-len(suffix)]. Otherwise, return a copy of
1218the original bytearray.
1219[clinic start generated code]*/
1220
1221static PyObject *
1222bytearray_removesuffix_impl(PyByteArrayObject *self, Py_buffer *suffix)
1223/*[clinic end generated code: output=2bc8cfb79de793d3 input=c1827e810b2f6b99]*/
1224{
1225 const char *self_start = PyByteArray_AS_STRING(self);
1226 Py_ssize_t self_len = PyByteArray_GET_SIZE(self);
1227 const char *suffix_start = suffix->buf;
1228 Py_ssize_t suffix_len = suffix->len;
1229
1230 if (self_len >= suffix_len
1231 && memcmp(self_start + self_len - suffix_len,
1232 suffix_start, suffix_len) == 0)
1233 {
1234 return PyByteArray_FromStringAndSize(self_start,
1235 self_len - suffix_len);
1236 }
1237
1238 return PyByteArray_FromStringAndSize(self_start, self_len);
1239}
1240
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001241
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001242/*[clinic input]
1243bytearray.translate
1244
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001245 table: object
1246 Translation table, which must be a bytes object of length 256.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001247 /
Martin Panter1b6c6da2016-08-27 08:35:02 +00001248 delete as deletechars: object(c_default="NULL") = b''
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001249
1250Return a copy with each character mapped by the given translation table.
1251
Martin Panter1b6c6da2016-08-27 08:35:02 +00001252All characters occurring in the optional argument delete are removed.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001253The remaining characters are mapped through the given translation table.
1254[clinic start generated code]*/
1255
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001256static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001257bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
Martin Panter1b6c6da2016-08-27 08:35:02 +00001258 PyObject *deletechars)
1259/*[clinic end generated code: output=b6a8f01c2a74e446 input=cfff956d4d127a9b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001260{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001261 char *input, *output;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001262 const char *table_chars;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001263 Py_ssize_t i, c;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001264 PyObject *input_obj = (PyObject*)self;
1265 const char *output_start;
1266 Py_ssize_t inlen;
Georg Brandlccc47b62008-12-28 11:44:14 +00001267 PyObject *result = NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001268 int trans_table[256];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001269 Py_buffer vtable, vdel;
1270
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001271 if (table == Py_None) {
1272 table_chars = NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001273 table = NULL;
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001274 } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001275 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001276 } else {
1277 if (vtable.len != 256) {
1278 PyErr_SetString(PyExc_ValueError,
1279 "translation table must be 256 characters long");
Georg Brandl953152f2009-07-22 12:03:59 +00001280 PyBuffer_Release(&vtable);
1281 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001282 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001283 table_chars = (const char*)vtable.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001284 }
1285
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001286 if (deletechars != NULL) {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001287 if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) {
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001288 if (table != NULL)
Georg Brandl953152f2009-07-22 12:03:59 +00001289 PyBuffer_Release(&vtable);
1290 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001291 }
1292 }
1293 else {
1294 vdel.buf = NULL;
1295 vdel.len = 0;
1296 }
1297
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001298 inlen = PyByteArray_GET_SIZE(input_obj);
1299 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1300 if (result == NULL)
1301 goto done;
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03001302 output_start = output = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001303 input = PyByteArray_AS_STRING(input_obj);
1304
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001305 if (vdel.len == 0 && table_chars != NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001306 /* If no deletions are required, use faster code */
1307 for (i = inlen; --i >= 0; ) {
1308 c = Py_CHARMASK(*input++);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001309 *output++ = table_chars[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001310 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001311 goto done;
1312 }
Georg Brandlccc47b62008-12-28 11:44:14 +00001313
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001314 if (table_chars == NULL) {
Georg Brandlccc47b62008-12-28 11:44:14 +00001315 for (i = 0; i < 256; i++)
1316 trans_table[i] = Py_CHARMASK(i);
1317 } else {
1318 for (i = 0; i < 256; i++)
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001319 trans_table[i] = Py_CHARMASK(table_chars[i]);
Georg Brandlccc47b62008-12-28 11:44:14 +00001320 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001321
1322 for (i = 0; i < vdel.len; i++)
1323 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1324
1325 for (i = inlen; --i >= 0; ) {
1326 c = Py_CHARMASK(*input++);
1327 if (trans_table[c] != -1)
Martin Panter1b6c6da2016-08-27 08:35:02 +00001328 *output++ = (char)trans_table[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001329 }
Serhiy Storchaka2ad93822020-12-03 12:46:16 +02001330 /* Fix the size of the resulting bytearray */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001331 if (inlen > 0)
Christian Heimesc731bbe2013-07-21 02:04:35 +02001332 if (PyByteArray_Resize(result, output - output_start) < 0) {
1333 Py_CLEAR(result);
1334 goto done;
1335 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001336
1337done:
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001338 if (table != NULL)
Georg Brandlccc47b62008-12-28 11:44:14 +00001339 PyBuffer_Release(&vtable);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001340 if (deletechars != NULL)
Martin v. Löwis423be952008-08-13 15:53:07 +00001341 PyBuffer_Release(&vdel);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001342 return result;
1343}
1344
1345
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001346/*[clinic input]
1347
1348@staticmethod
1349bytearray.maketrans
1350
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001351 frm: Py_buffer
1352 to: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001353 /
1354
1355Return a translation table useable for the bytes or bytearray translate method.
1356
1357The returned table will be one where each byte in frm is mapped to the byte at
1358the same position in to.
1359
1360The bytes objects frm and to must be of the same length.
1361[clinic start generated code]*/
1362
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001363static PyObject *
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001364bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001365/*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001366{
1367 return _Py_bytes_maketrans(frm, to);
Georg Brandlabc38772009-04-12 15:51:51 +00001368}
1369
1370
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001371/*[clinic input]
1372bytearray.replace
1373
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001374 old: Py_buffer
1375 new: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001376 count: Py_ssize_t = -1
1377 Maximum number of occurrences to replace.
1378 -1 (the default value) means replace all occurrences.
1379 /
1380
1381Return a copy with all occurrences of substring old replaced by new.
1382
1383If the optional argument count is given, only the first count occurrences are
1384replaced.
1385[clinic start generated code]*/
1386
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001387static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001388bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
1389 Py_buffer *new, Py_ssize_t count)
1390/*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001391{
Serhiy Storchakafb81d3c2016-05-05 09:26:07 +03001392 return stringlib_replace((PyObject *)self,
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001393 (const char *)old->buf, old->len,
1394 (const char *)new->buf, new->len, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001395}
1396
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001397/*[clinic input]
1398bytearray.split
1399
1400 sep: object = None
1401 The delimiter according which to split the bytearray.
1402 None (the default value) means split on ASCII whitespace characters
1403 (space, tab, return, newline, formfeed, vertical tab).
1404 maxsplit: Py_ssize_t = -1
1405 Maximum number of splits to do.
1406 -1 (the default value) means no limit.
1407
1408Return a list of the sections in the bytearray, using sep as the delimiter.
1409[clinic start generated code]*/
1410
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001411static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001412bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
1413 Py_ssize_t maxsplit)
1414/*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001415{
1416 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001417 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001418 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001419 Py_buffer vsub;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001420
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001421 if (maxsplit < 0)
1422 maxsplit = PY_SSIZE_T_MAX;
1423
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001424 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001425 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001426
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001427 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001428 return NULL;
1429 sub = vsub.buf;
1430 n = vsub.len;
1431
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001432 list = stringlib_split(
1433 (PyObject*) self, s, len, sub, n, maxsplit
1434 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001435 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001436 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001437}
1438
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001439/*[clinic input]
1440bytearray.partition
1441
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001442 sep: object
1443 /
1444
1445Partition the bytearray into three parts using the given separator.
1446
1447This will search for the separator sep in the bytearray. If the separator is
1448found, returns a 3-tuple containing the part before the separator, the
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001449separator itself, and the part after it as new bytearray objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001450
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001451If the separator is not found, returns a 3-tuple containing the copy of the
1452original bytearray object and two empty bytearray objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001453[clinic start generated code]*/
1454
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001455static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001456bytearray_partition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001457/*[clinic end generated code: output=45d2525ddd35f957 input=8f644749ee4fc83a]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001458{
1459 PyObject *bytesep, *result;
1460
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001461 bytesep = _PyByteArray_FromBufferObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001462 if (! bytesep)
1463 return NULL;
1464
1465 result = stringlib_partition(
1466 (PyObject*) self,
1467 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1468 bytesep,
1469 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1470 );
1471
1472 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001473 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001474}
1475
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001476/*[clinic input]
1477bytearray.rpartition
1478
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001479 sep: object
1480 /
1481
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001482Partition the bytearray into three parts using the given separator.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001483
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001484This will search for the separator sep in the bytearray, starting at the end.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001485If the separator is found, returns a 3-tuple containing the part before the
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001486separator, the separator itself, and the part after it as new bytearray
1487objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001488
1489If the separator is not found, returns a 3-tuple containing two empty bytearray
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001490objects and the copy of the original bytearray object.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001491[clinic start generated code]*/
1492
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001493static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001494bytearray_rpartition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001495/*[clinic end generated code: output=440de3c9426115e8 input=7e3df3e6cb8fa0ac]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001496{
1497 PyObject *bytesep, *result;
1498
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001499 bytesep = _PyByteArray_FromBufferObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001500 if (! bytesep)
1501 return NULL;
1502
1503 result = stringlib_rpartition(
1504 (PyObject*) self,
1505 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1506 bytesep,
1507 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1508 );
1509
1510 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001511 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001512}
1513
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001514/*[clinic input]
1515bytearray.rsplit = bytearray.split
1516
1517Return a list of the sections in the bytearray, using sep as the delimiter.
1518
1519Splitting is done starting at the end of the bytearray and working to the front.
1520[clinic start generated code]*/
1521
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001522static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001523bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
1524 Py_ssize_t maxsplit)
1525/*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001526{
1527 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001528 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001529 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001530 Py_buffer vsub;
1531
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001532 if (maxsplit < 0)
1533 maxsplit = PY_SSIZE_T_MAX;
1534
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001535 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001536 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001537
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001538 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001539 return NULL;
1540 sub = vsub.buf;
1541 n = vsub.len;
1542
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001543 list = stringlib_rsplit(
1544 (PyObject*) self, s, len, sub, n, maxsplit
1545 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001546 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001547 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001548}
1549
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001550/*[clinic input]
1551bytearray.reverse
1552
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001553Reverse the order of the values in B in place.
1554[clinic start generated code]*/
1555
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001556static PyObject *
1557bytearray_reverse_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001558/*[clinic end generated code: output=9f7616f29ab309d3 input=543356319fc78557]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001559{
1560 char swap, *head, *tail;
1561 Py_ssize_t i, j, n = Py_SIZE(self);
1562
1563 j = n / 2;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001564 head = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001565 tail = head + n - 1;
1566 for (i = 0; i < j; i++) {
1567 swap = *head;
1568 *head++ = *tail;
1569 *tail-- = swap;
1570 }
1571
1572 Py_RETURN_NONE;
1573}
1574
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001575
1576/*[python input]
1577class bytesvalue_converter(CConverter):
1578 type = 'int'
1579 converter = '_getbytevalue'
1580[python start generated code]*/
1581/*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/
1582
1583
1584/*[clinic input]
1585bytearray.insert
1586
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001587 index: Py_ssize_t
1588 The index where the value is to be inserted.
1589 item: bytesvalue
1590 The item to be inserted.
1591 /
1592
1593Insert a single item into the bytearray before the given index.
1594[clinic start generated code]*/
1595
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001596static PyObject *
1597bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001598/*[clinic end generated code: output=76c775a70e7b07b7 input=b2b5d07e9de6c070]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001599{
1600 Py_ssize_t n = Py_SIZE(self);
1601 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001602
1603 if (n == PY_SSIZE_T_MAX) {
1604 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001605 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001606 return NULL;
1607 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001608 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1609 return NULL;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001610 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001611
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001612 if (index < 0) {
1613 index += n;
1614 if (index < 0)
1615 index = 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001616 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001617 if (index > n)
1618 index = n;
1619 memmove(buf + index + 1, buf + index, n - index);
1620 buf[index] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001621
1622 Py_RETURN_NONE;
1623}
1624
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001625/*[clinic input]
1626bytearray.append
1627
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001628 item: bytesvalue
1629 The item to be appended.
1630 /
1631
1632Append a single item to the end of the bytearray.
1633[clinic start generated code]*/
1634
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001635static PyObject *
1636bytearray_append_impl(PyByteArrayObject *self, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001637/*[clinic end generated code: output=a154e19ed1886cb6 input=20d6bec3d1340593]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001638{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001639 Py_ssize_t n = Py_SIZE(self);
1640
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001641 if (n == PY_SSIZE_T_MAX) {
1642 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001643 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001644 return NULL;
1645 }
1646 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1647 return NULL;
1648
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001649 PyByteArray_AS_STRING(self)[n] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001650
1651 Py_RETURN_NONE;
1652}
1653
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001654/*[clinic input]
1655bytearray.extend
1656
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001657 iterable_of_ints: object
1658 The iterable of items to append.
1659 /
1660
1661Append all the items from the iterator or sequence to the end of the bytearray.
1662[clinic start generated code]*/
1663
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001664static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001665bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001666/*[clinic end generated code: output=98155dbe249170b1 input=c617b3a93249ba28]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001667{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001668 PyObject *it, *item, *bytearray_obj;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001669 Py_ssize_t buf_size = 0, len = 0;
1670 int value;
1671 char *buf;
1672
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001673 /* bytearray_setslice code only accepts something supporting PEP 3118. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001674 if (PyObject_CheckBuffer(iterable_of_ints)) {
1675 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_ints) == -1)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001676 return NULL;
1677
1678 Py_RETURN_NONE;
1679 }
1680
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001681 it = PyObject_GetIter(iterable_of_ints);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +03001682 if (it == NULL) {
1683 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
1684 PyErr_Format(PyExc_TypeError,
1685 "can't extend bytearray with %.100s",
Victor Stinner58ac7002020-02-07 03:04:21 +01001686 Py_TYPE(iterable_of_ints)->tp_name);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +03001687 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001688 return NULL;
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +03001689 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001690
Ezio Melotti42da6632011-03-15 05:18:48 +02001691 /* Try to determine the length of the argument. 32 is arbitrary. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001692 buf_size = PyObject_LengthHint(iterable_of_ints, 32);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001693 if (buf_size == -1) {
1694 Py_DECREF(it);
1695 return NULL;
1696 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001697
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001698 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001699 if (bytearray_obj == NULL) {
1700 Py_DECREF(it);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001701 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001702 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001703 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001704
1705 while ((item = PyIter_Next(it)) != NULL) {
1706 if (! _getbytevalue(item, &value)) {
1707 Py_DECREF(item);
1708 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001709 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001710 return NULL;
1711 }
1712 buf[len++] = value;
1713 Py_DECREF(item);
1714
1715 if (len >= buf_size) {
Martin Panter371731e2016-07-18 07:53:13 +00001716 Py_ssize_t addition;
1717 if (len == PY_SSIZE_T_MAX) {
1718 Py_DECREF(it);
1719 Py_DECREF(bytearray_obj);
1720 return PyErr_NoMemory();
1721 }
1722 addition = len >> 1;
1723 if (addition > PY_SSIZE_T_MAX - len - 1)
1724 buf_size = PY_SSIZE_T_MAX;
1725 else
1726 buf_size = len + addition + 1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001727 if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001728 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001729 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001730 return NULL;
1731 }
1732 /* Recompute the `buf' pointer, since the resizing operation may
1733 have invalidated it. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001734 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001735 }
1736 }
1737 Py_DECREF(it);
1738
Victor Stinnera6192632021-01-29 16:53:03 +01001739 if (PyErr_Occurred()) {
1740 Py_DECREF(bytearray_obj);
1741 return NULL;
1742 }
1743
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001744 /* 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
Victor Stinnera6192632021-01-29 16:53:03 +01001756 assert(!PyErr_Occurred());
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001757 Py_RETURN_NONE;
1758}
1759
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001760/*[clinic input]
1761bytearray.pop
1762
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001763 index: Py_ssize_t = -1
1764 The index from where to remove the item.
1765 -1 (the default value) means remove the last item.
1766 /
1767
1768Remove and return a single item from B.
1769
1770If no index argument is given, will pop the last item.
1771[clinic start generated code]*/
1772
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001773static PyObject *
1774bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001775/*[clinic end generated code: output=e0ccd401f8021da8 input=3591df2d06c0d237]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001776{
1777 int value;
1778 Py_ssize_t n = Py_SIZE(self);
1779 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001780
1781 if (n == 0) {
Eli Bendersky1bc4f192011-03-04 04:55:25 +00001782 PyErr_SetString(PyExc_IndexError,
1783 "pop from empty bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001784 return NULL;
1785 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001786 if (index < 0)
1787 index += Py_SIZE(self);
1788 if (index < 0 || index >= Py_SIZE(self)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001789 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1790 return NULL;
1791 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001792 if (!_canresize(self))
1793 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001794
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001795 buf = PyByteArray_AS_STRING(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001796 value = buf[index];
1797 memmove(buf + index, buf + index + 1, n - index);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001798 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1799 return NULL;
1800
Mark Dickinson54a3db92009-09-06 10:19:23 +00001801 return PyLong_FromLong((unsigned char)value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001802}
1803
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001804/*[clinic input]
1805bytearray.remove
1806
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001807 value: bytesvalue
1808 The value to remove.
1809 /
1810
1811Remove the first occurrence of a value in the bytearray.
1812[clinic start generated code]*/
1813
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001814static PyObject *
1815bytearray_remove_impl(PyByteArrayObject *self, int value)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001816/*[clinic end generated code: output=d659e37866709c13 input=121831240cd51ddf]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001817{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001818 Py_ssize_t where, n = Py_SIZE(self);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001819 char *buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001820
Serhiy Storchaka4b234942016-05-16 22:24:03 +03001821 where = stringlib_find_char(buf, n, value);
1822 if (where < 0) {
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001823 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001824 return NULL;
1825 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001826 if (!_canresize(self))
1827 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001828
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001829 memmove(buf + where, buf + where + 1, n - where);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001830 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1831 return NULL;
1832
1833 Py_RETURN_NONE;
1834}
1835
1836/* XXX These two helpers could be optimized if argsize == 1 */
1837
1838static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001839lstrip_helper(const char *myptr, Py_ssize_t mysize,
1840 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001841{
1842 Py_ssize_t i = 0;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001843 while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001844 i++;
1845 return i;
1846}
1847
1848static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001849rstrip_helper(const char *myptr, Py_ssize_t mysize,
1850 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001851{
1852 Py_ssize_t i = mysize - 1;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001853 while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001854 i--;
1855 return i + 1;
1856}
1857
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001858/*[clinic input]
1859bytearray.strip
1860
1861 bytes: object = None
1862 /
1863
1864Strip leading and trailing bytes contained in the argument.
1865
1866If the argument is omitted or None, strip leading and trailing ASCII whitespace.
1867[clinic start generated code]*/
1868
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001869static PyObject *
1870bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001871/*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001872{
1873 Py_ssize_t left, right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001874 char *myptr;
1875 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001876 Py_buffer vbytes;
1877
1878 if (bytes == Py_None) {
1879 bytesptr = "\t\n\r\f\v ";
1880 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001881 }
1882 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001883 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001884 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001885 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001886 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001887 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001888 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001889 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001890 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001891 if (left == mysize)
1892 right = left;
1893 else
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001894 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1895 if (bytes != Py_None)
1896 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001897 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001898}
1899
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001900/*[clinic input]
1901bytearray.lstrip
1902
1903 bytes: object = None
1904 /
1905
1906Strip leading bytes contained in the argument.
1907
1908If the argument is omitted or None, strip leading ASCII whitespace.
1909[clinic start generated code]*/
1910
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001911static PyObject *
1912bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001913/*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001914{
1915 Py_ssize_t left, right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001916 char *myptr;
1917 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001918 Py_buffer vbytes;
1919
1920 if (bytes == Py_None) {
1921 bytesptr = "\t\n\r\f\v ";
1922 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001923 }
1924 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001925 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001926 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001927 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001928 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001929 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001930 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001931 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001932 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001933 right = mysize;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001934 if (bytes != Py_None)
1935 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001936 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001937}
1938
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001939/*[clinic input]
1940bytearray.rstrip
1941
1942 bytes: object = None
1943 /
1944
1945Strip trailing bytes contained in the argument.
1946
1947If the argument is omitted or None, strip trailing ASCII whitespace.
1948[clinic start generated code]*/
1949
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001950static PyObject *
1951bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001952/*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001953{
1954 Py_ssize_t right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001955 char *myptr;
1956 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001957 Py_buffer vbytes;
1958
1959 if (bytes == Py_None) {
1960 bytesptr = "\t\n\r\f\v ";
1961 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001962 }
1963 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001964 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001965 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001966 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001967 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001968 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001969 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001970 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001971 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1972 if (bytes != Py_None)
1973 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001974 return PyByteArray_FromStringAndSize(myptr, right);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001975}
1976
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001977/*[clinic input]
1978bytearray.decode
1979
1980 encoding: str(c_default="NULL") = 'utf-8'
1981 The encoding with which to decode the bytearray.
1982 errors: str(c_default="NULL") = 'strict'
1983 The error handling scheme to use for the handling of decoding errors.
1984 The default is 'strict' meaning that decoding errors raise a
1985 UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
1986 as well as any other name registered with codecs.register_error that
1987 can handle UnicodeDecodeErrors.
1988
1989Decode the bytearray using the codec registered for encoding.
1990[clinic start generated code]*/
1991
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001992static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001993bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
1994 const char *errors)
1995/*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001996{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001997 if (encoding == NULL)
1998 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis0efea322014-07-27 17:29:17 +02001999 return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002000}
2001
2002PyDoc_STRVAR(alloc_doc,
2003"B.__alloc__() -> int\n\
2004\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00002005Return the number of bytes actually allocated.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002006
2007static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302008bytearray_alloc(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002009{
2010 return PyLong_FromSsize_t(self->ob_alloc);
2011}
2012
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002013/*[clinic input]
2014bytearray.join
2015
2016 iterable_of_bytes: object
2017 /
2018
2019Concatenate any number of bytes/bytearray objects.
2020
2021The bytearray whose method is called is inserted in between each pair.
2022
2023The result is returned as a new bytearray object.
2024[clinic start generated code]*/
2025
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002026static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002027bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002028/*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002029{
Martin v. Löwis0efea322014-07-27 17:29:17 +02002030 return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002031}
2032
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002033/*[clinic input]
2034bytearray.splitlines
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002035
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002036 keepends: bool(accept={int}) = False
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002037
2038Return a list of the lines in the bytearray, breaking at line boundaries.
2039
2040Line breaks are not included in the resulting list unless keepends is given and
2041true.
2042[clinic start generated code]*/
2043
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002044static PyObject *
2045bytearray_splitlines_impl(PyByteArrayObject *self, int keepends)
Serhiy Storchaka202fda52017-03-12 10:10:47 +02002046/*[clinic end generated code: output=4223c94b895f6ad9 input=99a27ad959b9cf6b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002047{
Antoine Pitrouf2c54842010-01-13 08:07:53 +00002048 return stringlib_splitlines(
2049 (PyObject*) self, PyByteArray_AS_STRING(self),
2050 PyByteArray_GET_SIZE(self), keepends
2051 );
2052}
2053
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002054/*[clinic input]
2055@classmethod
2056bytearray.fromhex
2057
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002058 string: unicode
2059 /
2060
2061Create a bytearray object from a string of hexadecimal numbers.
2062
2063Spaces between two numbers are accepted.
2064Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')
2065[clinic start generated code]*/
2066
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002067static PyObject *
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002068bytearray_fromhex_impl(PyTypeObject *type, PyObject *string)
2069/*[clinic end generated code: output=8f0f0b6d30fb3ba0 input=f033a16d1fb21f48]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002070{
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002071 PyObject *result = _PyBytes_FromHex(string, type == &PyByteArray_Type);
2072 if (type != &PyByteArray_Type && result != NULL) {
Petr Viktorinffd97532020-02-11 17:46:57 +01002073 Py_SETREF(result, PyObject_CallOneArg((PyObject *)type, result));
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002074 }
2075 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002076}
2077
Gregory P. Smith0c2f9302019-05-29 11:46:58 -07002078/*[clinic input]
2079bytearray.hex
2080
2081 sep: object = NULL
2082 An optional single character or byte to separate hex bytes.
2083 bytes_per_sep: int = 1
2084 How many bytes between separators. Positive values count from the
2085 right, negative values count from the left.
2086
Serhiy Storchaka2ad93822020-12-03 12:46:16 +02002087Create a string of hexadecimal numbers from a bytearray object.
Gregory P. Smith0c2f9302019-05-29 11:46:58 -07002088
2089Example:
2090>>> value = bytearray([0xb9, 0x01, 0xef])
2091>>> value.hex()
2092'b901ef'
2093>>> value.hex(':')
2094'b9:01:ef'
2095>>> value.hex(':', 2)
2096'b9:01ef'
2097>>> value.hex(':', -2)
2098'b901:ef'
2099[clinic start generated code]*/
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002100
2101static PyObject *
Gregory P. Smith0c2f9302019-05-29 11:46:58 -07002102bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep)
Serhiy Storchaka2ad93822020-12-03 12:46:16 +02002103/*[clinic end generated code: output=29c4e5ef72c565a0 input=808667e49bcccb54]*/
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002104{
2105 char* argbuf = PyByteArray_AS_STRING(self);
2106 Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
Gregory P. Smith0c2f9302019-05-29 11:46:58 -07002107 return _Py_strhex_with_sep(argbuf, arglen, sep, bytes_per_sep);
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002108}
2109
2110static PyObject *
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002111_common_reduce(PyByteArrayObject *self, int proto)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002112{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002113 PyObject *dict;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002114 _Py_IDENTIFIER(__dict__);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002115 char *buf;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002116
Serhiy Storchaka41c57b32019-09-01 12:03:39 +03002117 if (_PyObject_LookupAttrId((PyObject *)self, &PyId___dict__, &dict) < 0) {
2118 return NULL;
2119 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002120 if (dict == NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002121 dict = Py_None;
2122 Py_INCREF(dict);
2123 }
2124
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002125 buf = PyByteArray_AS_STRING(self);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002126 if (proto < 3) {
2127 /* use str based reduction for backwards compatibility with Python 2.x */
2128 PyObject *latin1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002129 if (Py_SIZE(self))
2130 latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002131 else
2132 latin1 = PyUnicode_FromString("");
2133 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
2134 }
2135 else {
2136 /* use more efficient byte based reduction */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002137 if (Py_SIZE(self)) {
2138 return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002139 }
2140 else {
2141 return Py_BuildValue("(O()N)", Py_TYPE(self), dict);
2142 }
2143 }
2144}
2145
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002146/*[clinic input]
2147bytearray.__reduce__ as bytearray_reduce
2148
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002149Return state information for pickling.
2150[clinic start generated code]*/
2151
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002152static PyObject *
2153bytearray_reduce_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002154/*[clinic end generated code: output=52bf304086464cab input=44b5737ada62dd3f]*/
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002155{
2156 return _common_reduce(self, 2);
2157}
2158
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002159/*[clinic input]
2160bytearray.__reduce_ex__ as bytearray_reduce_ex
2161
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002162 proto: int = 0
2163 /
2164
2165Return state information for pickling.
2166[clinic start generated code]*/
2167
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002168static PyObject *
2169bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002170/*[clinic end generated code: output=52eac33377197520 input=f129bc1a1aa151ee]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002171{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002172 return _common_reduce(self, proto);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002173}
2174
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002175/*[clinic input]
2176bytearray.__sizeof__ as bytearray_sizeof
2177
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002178Returns the size of the bytearray object in memory, in bytes.
2179[clinic start generated code]*/
2180
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002181static PyObject *
2182bytearray_sizeof_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002183/*[clinic end generated code: output=738abdd17951c427 input=e27320fd98a4bc5a]*/
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002184{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002185 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002186
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002187 res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002188 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002189}
2190
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002191static PySequenceMethods bytearray_as_sequence = {
2192 (lenfunc)bytearray_length, /* sq_length */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002193 (binaryfunc)PyByteArray_Concat, /* sq_concat */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002194 (ssizeargfunc)bytearray_repeat, /* sq_repeat */
2195 (ssizeargfunc)bytearray_getitem, /* sq_item */
2196 0, /* sq_slice */
2197 (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */
2198 0, /* sq_ass_slice */
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002199 (objobjproc)bytearray_contains, /* sq_contains */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002200 (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */
2201 (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002202};
2203
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002204static PyMappingMethods bytearray_as_mapping = {
2205 (lenfunc)bytearray_length,
2206 (binaryfunc)bytearray_subscript,
2207 (objobjargproc)bytearray_ass_subscript,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002208};
2209
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002210static PyBufferProcs bytearray_as_buffer = {
2211 (getbufferproc)bytearray_getbuffer,
2212 (releasebufferproc)bytearray_releasebuffer,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002213};
2214
2215static PyMethodDef
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002216bytearray_methods[] = {
2217 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002218 BYTEARRAY_REDUCE_METHODDEF
2219 BYTEARRAY_REDUCE_EX_METHODDEF
2220 BYTEARRAY_SIZEOF_METHODDEF
2221 BYTEARRAY_APPEND_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302222 {"capitalize", stringlib_capitalize, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002223 _Py_capitalize__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002224 STRINGLIB_CENTER_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002225 BYTEARRAY_CLEAR_METHODDEF
2226 BYTEARRAY_COPY_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002227 {"count", (PyCFunction)bytearray_count, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002228 _Py_count__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002229 BYTEARRAY_DECODE_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002230 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002231 _Py_endswith__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002232 STRINGLIB_EXPANDTABS_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002233 BYTEARRAY_EXTEND_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002234 {"find", (PyCFunction)bytearray_find, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002235 _Py_find__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002236 BYTEARRAY_FROMHEX_METHODDEF
Gregory P. Smith0c2f9302019-05-29 11:46:58 -07002237 BYTEARRAY_HEX_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002238 {"index", (PyCFunction)bytearray_index, METH_VARARGS, _Py_index__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002239 BYTEARRAY_INSERT_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302240 {"isalnum", stringlib_isalnum, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002241 _Py_isalnum__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302242 {"isalpha", stringlib_isalpha, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002243 _Py_isalpha__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302244 {"isascii", stringlib_isascii, METH_NOARGS,
INADA Naokia49ac992018-01-27 14:06:21 +09002245 _Py_isascii__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302246 {"isdigit", stringlib_isdigit, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002247 _Py_isdigit__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302248 {"islower", stringlib_islower, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002249 _Py_islower__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302250 {"isspace", stringlib_isspace, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002251 _Py_isspace__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302252 {"istitle", stringlib_istitle, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002253 _Py_istitle__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302254 {"isupper", stringlib_isupper, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002255 _Py_isupper__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002256 BYTEARRAY_JOIN_METHODDEF
Tal Einatc929df32018-07-06 13:17:38 +03002257 STRINGLIB_LJUST_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302258 {"lower", stringlib_lower, METH_NOARGS, _Py_lower__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002259 BYTEARRAY_LSTRIP_METHODDEF
2260 BYTEARRAY_MAKETRANS_METHODDEF
2261 BYTEARRAY_PARTITION_METHODDEF
2262 BYTEARRAY_POP_METHODDEF
2263 BYTEARRAY_REMOVE_METHODDEF
2264 BYTEARRAY_REPLACE_METHODDEF
sweeneydea81849b2020-04-22 17:05:48 -04002265 BYTEARRAY_REMOVEPREFIX_METHODDEF
2266 BYTEARRAY_REMOVESUFFIX_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002267 BYTEARRAY_REVERSE_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002268 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__},
2269 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002270 STRINGLIB_RJUST_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002271 BYTEARRAY_RPARTITION_METHODDEF
2272 BYTEARRAY_RSPLIT_METHODDEF
2273 BYTEARRAY_RSTRIP_METHODDEF
2274 BYTEARRAY_SPLIT_METHODDEF
2275 BYTEARRAY_SPLITLINES_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002276 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002277 _Py_startswith__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002278 BYTEARRAY_STRIP_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302279 {"swapcase", stringlib_swapcase, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002280 _Py_swapcase__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302281 {"title", stringlib_title, METH_NOARGS, _Py_title__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002282 BYTEARRAY_TRANSLATE_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302283 {"upper", stringlib_upper, METH_NOARGS, _Py_upper__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002284 STRINGLIB_ZFILL_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002285 {NULL}
2286};
2287
Ethan Furmanb95b5612015-01-23 20:05:18 -08002288static PyObject *
2289bytearray_mod(PyObject *v, PyObject *w)
2290{
2291 if (!PyByteArray_Check(v))
2292 Py_RETURN_NOTIMPLEMENTED;
Berker Peksag43de36d2016-04-16 01:20:47 +03002293 return _PyBytes_FormatEx(PyByteArray_AS_STRING(v), PyByteArray_GET_SIZE(v), w, 1);
Ethan Furmanb95b5612015-01-23 20:05:18 -08002294}
2295
2296static PyNumberMethods bytearray_as_number = {
2297 0, /*nb_add*/
2298 0, /*nb_subtract*/
2299 0, /*nb_multiply*/
2300 bytearray_mod, /*nb_remainder*/
2301};
2302
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002303PyDoc_STRVAR(bytearray_doc,
Georg Brandl17cb8a82008-05-30 08:20:09 +00002304"bytearray(iterable_of_ints) -> bytearray\n\
2305bytearray(string, encoding[, errors]) -> bytearray\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002306bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\
2307bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\
2308bytearray() -> empty bytes array\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002309\n\
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03002310Construct a mutable bytearray object from:\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002311 - an iterable yielding integers in range(256)\n\
2312 - a text string encoded using the specified encoding\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002313 - a bytes or a buffer object\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002314 - any object implementing the buffer API.\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002315 - an integer");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002316
2317
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002318static PyObject *bytearray_iter(PyObject *seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002319
2320PyTypeObject PyByteArray_Type = {
2321 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2322 "bytearray",
2323 sizeof(PyByteArrayObject),
2324 0,
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002325 (destructor)bytearray_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002326 0, /* tp_vectorcall_offset */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002327 0, /* tp_getattr */
2328 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002329 0, /* tp_as_async */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002330 (reprfunc)bytearray_repr, /* tp_repr */
Ethan Furmanb95b5612015-01-23 20:05:18 -08002331 &bytearray_as_number, /* tp_as_number */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002332 &bytearray_as_sequence, /* tp_as_sequence */
2333 &bytearray_as_mapping, /* tp_as_mapping */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002334 0, /* tp_hash */
2335 0, /* tp_call */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002336 bytearray_str, /* tp_str */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002337 PyObject_GenericGetAttr, /* tp_getattro */
2338 0, /* tp_setattro */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002339 &bytearray_as_buffer, /* tp_as_buffer */
Brandt Bucher145bf262021-02-26 14:51:55 -08002340 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
2341 _Py_TPFLAGS_MATCH_SELF, /* tp_flags */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002342 bytearray_doc, /* tp_doc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002343 0, /* tp_traverse */
2344 0, /* tp_clear */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002345 (richcmpfunc)bytearray_richcompare, /* tp_richcompare */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002346 0, /* tp_weaklistoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002347 bytearray_iter, /* tp_iter */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002348 0, /* tp_iternext */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002349 bytearray_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002350 0, /* tp_members */
2351 0, /* tp_getset */
2352 0, /* tp_base */
2353 0, /* tp_dict */
2354 0, /* tp_descr_get */
2355 0, /* tp_descr_set */
2356 0, /* tp_dictoffset */
Serhiy Storchaka12f43342020-07-20 15:53:55 +03002357 (initproc)bytearray___init__, /* tp_init */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002358 PyType_GenericAlloc, /* tp_alloc */
2359 PyType_GenericNew, /* tp_new */
2360 PyObject_Del, /* tp_free */
2361};
2362
Serhiy Storchaka2ad93822020-12-03 12:46:16 +02002363/*********************** Bytearray Iterator ****************************/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002364
2365typedef struct {
2366 PyObject_HEAD
2367 Py_ssize_t it_index;
2368 PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
2369} bytesiterobject;
2370
2371static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002372bytearrayiter_dealloc(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002373{
2374 _PyObject_GC_UNTRACK(it);
2375 Py_XDECREF(it->it_seq);
2376 PyObject_GC_Del(it);
2377}
2378
2379static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002380bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002381{
2382 Py_VISIT(it->it_seq);
2383 return 0;
2384}
2385
2386static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002387bytearrayiter_next(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002388{
2389 PyByteArrayObject *seq;
2390 PyObject *item;
2391
2392 assert(it != NULL);
2393 seq = it->it_seq;
2394 if (seq == NULL)
2395 return NULL;
2396 assert(PyByteArray_Check(seq));
2397
2398 if (it->it_index < PyByteArray_GET_SIZE(seq)) {
2399 item = PyLong_FromLong(
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002400 (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002401 if (item != NULL)
2402 ++it->it_index;
2403 return item;
2404 }
2405
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002406 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03002407 Py_DECREF(seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002408 return NULL;
2409}
2410
2411static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302412bytearrayiter_length_hint(bytesiterobject *it, PyObject *Py_UNUSED(ignored))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002413{
2414 Py_ssize_t len = 0;
Serhiy Storchakaaf658722016-07-03 14:41:36 +03002415 if (it->it_seq) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002416 len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
Serhiy Storchakaaf658722016-07-03 14:41:36 +03002417 if (len < 0) {
2418 len = 0;
2419 }
2420 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002421 return PyLong_FromSsize_t(len);
2422}
2423
2424PyDoc_STRVAR(length_hint_doc,
2425 "Private method returning an estimate of len(list(it)).");
2426
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002427static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302428bytearrayiter_reduce(bytesiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002429{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002430 _Py_IDENTIFIER(iter);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002431 if (it->it_seq != NULL) {
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002432 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002433 it->it_seq, it->it_index);
2434 } else {
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002435 return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002436 }
2437}
2438
2439static PyObject *
2440bytearrayiter_setstate(bytesiterobject *it, PyObject *state)
2441{
2442 Py_ssize_t index = PyLong_AsSsize_t(state);
2443 if (index == -1 && PyErr_Occurred())
2444 return NULL;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00002445 if (it->it_seq != NULL) {
2446 if (index < 0)
2447 index = 0;
2448 else if (index > PyByteArray_GET_SIZE(it->it_seq))
2449 index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */
2450 it->it_index = index;
2451 }
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002452 Py_RETURN_NONE;
2453}
2454
2455PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
2456
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002457static PyMethodDef bytearrayiter_methods[] = {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002458 {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002459 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002460 {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002461 bytearray_reduce__doc__},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002462 {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
2463 setstate_doc},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002464 {NULL, NULL} /* sentinel */
2465};
2466
2467PyTypeObject PyByteArrayIter_Type = {
2468 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2469 "bytearray_iterator", /* tp_name */
2470 sizeof(bytesiterobject), /* tp_basicsize */
2471 0, /* tp_itemsize */
2472 /* methods */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002473 (destructor)bytearrayiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002474 0, /* tp_vectorcall_offset */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002475 0, /* tp_getattr */
2476 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002477 0, /* tp_as_async */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002478 0, /* tp_repr */
2479 0, /* tp_as_number */
2480 0, /* tp_as_sequence */
2481 0, /* tp_as_mapping */
2482 0, /* tp_hash */
2483 0, /* tp_call */
2484 0, /* tp_str */
2485 PyObject_GenericGetAttr, /* tp_getattro */
2486 0, /* tp_setattro */
2487 0, /* tp_as_buffer */
2488 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
2489 0, /* tp_doc */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002490 (traverseproc)bytearrayiter_traverse, /* tp_traverse */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002491 0, /* tp_clear */
2492 0, /* tp_richcompare */
2493 0, /* tp_weaklistoffset */
2494 PyObject_SelfIter, /* tp_iter */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002495 (iternextfunc)bytearrayiter_next, /* tp_iternext */
2496 bytearrayiter_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002497 0,
2498};
2499
2500static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002501bytearray_iter(PyObject *seq)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002502{
2503 bytesiterobject *it;
2504
2505 if (!PyByteArray_Check(seq)) {
2506 PyErr_BadInternalCall();
2507 return NULL;
2508 }
2509 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
2510 if (it == NULL)
2511 return NULL;
2512 it->it_index = 0;
2513 Py_INCREF(seq);
2514 it->it_seq = (PyByteArrayObject *)seq;
2515 _PyObject_GC_TRACK(it);
2516 return (PyObject *)it;
2517}