blob: d3964358bc59de23e376b95a2eb5851762c8e472 [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 Stinner45876a92020-02-12 22:32:34 +01005#include "pycore_bytes_methods.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +01006#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01007#include "pycore_pymem.h"
8#include "pycore_pystate.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00009#include "structmember.h"
Ethan Furmanb95b5612015-01-23 20:05:18 -080010#include "bytesobject.h"
Gregory P. Smith8cb65692015-04-25 23:22:26 +000011#include "pystrhex.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +000012
Martin v. Löwis7252a6e2014-07-27 16:25:09 +020013/*[clinic input]
14class bytearray "PyByteArrayObject *" "&PyByteArray_Type"
15[clinic start generated code]*/
16/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/
17
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000018char _PyByteArray_empty_string[] = "";
Christian Heimes2c9c7a52008-05-26 13:42:13 +000019
Christian Heimes2c9c7a52008-05-26 13:42:13 +000020/* end nullbytes support */
21
22/* Helpers */
23
24static int
25_getbytevalue(PyObject* arg, int *value)
26{
27 long face_value;
28
29 if (PyLong_Check(arg)) {
30 face_value = PyLong_AsLong(arg);
Georg Brandl9a54d7c2008-07-16 23:15:30 +000031 } else {
32 PyObject *index = PyNumber_Index(arg);
33 if (index == NULL) {
Mark Dickinson10de93a2010-07-09 19:25:48 +000034 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000035 return 0;
36 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +000037 face_value = PyLong_AsLong(index);
38 Py_DECREF(index);
39 }
40
41 if (face_value < 0 || face_value >= 256) {
42 /* this includes the OverflowError in case the long is too large */
43 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
Mark Dickinson10de93a2010-07-09 19:25:48 +000044 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000045 return 0;
46 }
47
48 *value = face_value;
49 return 1;
50}
51
52static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +000053bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000054{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000055 void *ptr;
56 if (view == NULL) {
Stefan Krah5178d912015-02-03 16:57:21 +010057 PyErr_SetString(PyExc_BufferError,
58 "bytearray_getbuffer: view==NULL argument is obsolete");
59 return -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000060 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000061 ptr = (void *) PyByteArray_AS_STRING(obj);
Stefan Krah5178d912015-02-03 16:57:21 +010062 /* cannot fail if view != NULL and readonly == 0 */
63 (void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
64 obj->ob_exports++;
65 return 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000066}
67
68static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +000069bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000070{
71 obj->ob_exports--;
72}
73
Antoine Pitrou5504e892008-12-06 21:27:53 +000074static int
75_canresize(PyByteArrayObject *self)
76{
77 if (self->ob_exports > 0) {
78 PyErr_SetString(PyExc_BufferError,
79 "Existing exports of data: object cannot be re-sized");
80 return 0;
81 }
82 return 1;
83}
84
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030085#include "clinic/bytearrayobject.c.h"
86
Christian Heimes2c9c7a52008-05-26 13:42:13 +000087/* Direct API functions */
88
89PyObject *
90PyByteArray_FromObject(PyObject *input)
91{
Petr Viktorinffd97532020-02-11 17:46:57 +010092 return PyObject_CallOneArg((PyObject *)&PyByteArray_Type, input);
Christian Heimes2c9c7a52008-05-26 13:42:13 +000093}
94
Serhiy Storchakaa2314282017-10-29 02:11:54 +030095static PyObject *
96_PyByteArray_FromBufferObject(PyObject *obj)
97{
98 PyObject *result;
99 Py_buffer view;
100
101 if (PyObject_GetBuffer(obj, &view, PyBUF_FULL_RO) < 0) {
102 return NULL;
103 }
104 result = PyByteArray_FromStringAndSize(NULL, view.len);
105 if (result != NULL &&
106 PyBuffer_ToContiguous(PyByteArray_AS_STRING(result),
107 &view, view.len, 'C') < 0)
108 {
109 Py_CLEAR(result);
110 }
111 PyBuffer_Release(&view);
112 return result;
113}
114
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000115PyObject *
116PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
117{
118 PyByteArrayObject *new;
119 Py_ssize_t alloc;
120
121 if (size < 0) {
122 PyErr_SetString(PyExc_SystemError,
123 "Negative size passed to PyByteArray_FromStringAndSize");
124 return NULL;
125 }
126
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000127 /* Prevent buffer overflow when setting alloc to size+1. */
128 if (size == PY_SSIZE_T_MAX) {
129 return PyErr_NoMemory();
130 }
131
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000132 new = PyObject_New(PyByteArrayObject, &PyByteArray_Type);
133 if (new == NULL)
134 return NULL;
135
136 if (size == 0) {
137 new->ob_bytes = NULL;
138 alloc = 0;
139 }
140 else {
141 alloc = size + 1;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100142 new->ob_bytes = PyObject_Malloc(alloc);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000143 if (new->ob_bytes == NULL) {
144 Py_DECREF(new);
145 return PyErr_NoMemory();
146 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +0000147 if (bytes != NULL && size > 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000148 memcpy(new->ob_bytes, bytes, size);
149 new->ob_bytes[size] = '\0'; /* Trailing null byte */
150 }
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100151 Py_SET_SIZE(new, size);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000152 new->ob_alloc = alloc;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200153 new->ob_start = new->ob_bytes;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000154 new->ob_exports = 0;
155
156 return (PyObject *)new;
157}
158
159Py_ssize_t
160PyByteArray_Size(PyObject *self)
161{
162 assert(self != NULL);
163 assert(PyByteArray_Check(self));
164
165 return PyByteArray_GET_SIZE(self);
166}
167
168char *
169PyByteArray_AsString(PyObject *self)
170{
171 assert(self != NULL);
172 assert(PyByteArray_Check(self));
173
174 return PyByteArray_AS_STRING(self);
175}
176
177int
Antoine Pitroucc231542014-11-02 18:40:09 +0100178PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000179{
180 void *sval;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200181 PyByteArrayObject *obj = ((PyByteArrayObject *)self);
Antoine Pitroucc231542014-11-02 18:40:09 +0100182 /* All computations are done unsigned to avoid integer overflows
183 (see issue #22335). */
184 size_t alloc = (size_t) obj->ob_alloc;
185 size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes);
186 size_t size = (size_t) requested_size;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000187
188 assert(self != NULL);
189 assert(PyByteArray_Check(self));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200190 assert(logical_offset <= alloc);
Antoine Pitroucc231542014-11-02 18:40:09 +0100191 assert(requested_size >= 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000192
Antoine Pitroucc231542014-11-02 18:40:09 +0100193 if (requested_size == Py_SIZE(self)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000194 return 0;
195 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200196 if (!_canresize(obj)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000197 return -1;
198 }
199
Antoine Pitrou25454112015-05-19 20:52:27 +0200200 if (size + logical_offset + 1 <= alloc) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200201 /* Current buffer is large enough to host the requested size,
202 decide on a strategy. */
203 if (size < alloc / 2) {
204 /* Major downsize; resize down to exact size */
205 alloc = size + 1;
206 }
207 else {
208 /* Minor downsize; quick exit */
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100209 Py_SET_SIZE(self, size);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200210 PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */
211 return 0;
212 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000213 }
214 else {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200215 /* Need growing, decide on a strategy */
216 if (size <= alloc * 1.125) {
217 /* Moderate upsize; overallocate similar to list_resize() */
218 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
219 }
220 else {
221 /* Major upsize; resize up to exact size */
222 alloc = size + 1;
223 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000224 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100225 if (alloc > PY_SSIZE_T_MAX) {
226 PyErr_NoMemory();
227 return -1;
228 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000229
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200230 if (logical_offset > 0) {
231 sval = PyObject_Malloc(alloc);
232 if (sval == NULL) {
233 PyErr_NoMemory();
234 return -1;
235 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100236 memcpy(sval, PyByteArray_AS_STRING(self),
Stefan Krahdce65022017-08-25 20:12:05 +0200237 Py_MIN((size_t)requested_size, (size_t)Py_SIZE(self)));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200238 PyObject_Free(obj->ob_bytes);
239 }
240 else {
241 sval = PyObject_Realloc(obj->ob_bytes, alloc);
242 if (sval == NULL) {
243 PyErr_NoMemory();
244 return -1;
245 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000246 }
247
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200248 obj->ob_bytes = obj->ob_start = sval;
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100249 Py_SET_SIZE(self, size);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200250 obj->ob_alloc = alloc;
251 obj->ob_bytes[size] = '\0'; /* Trailing null byte */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000252
253 return 0;
254}
255
256PyObject *
257PyByteArray_Concat(PyObject *a, PyObject *b)
258{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000259 Py_buffer va, vb;
260 PyByteArrayObject *result = NULL;
261
262 va.len = -1;
263 vb.len = -1;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200264 if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 ||
265 PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000266 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
Serhiy Storchaka6b5a9ec2017-03-19 19:47:02 +0200267 Py_TYPE(b)->tp_name, Py_TYPE(a)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000268 goto done;
269 }
270
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300271 if (va.len > PY_SSIZE_T_MAX - vb.len) {
272 PyErr_NoMemory();
273 goto done;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000274 }
275
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300276 result = (PyByteArrayObject *) \
277 PyByteArray_FromStringAndSize(NULL, va.len + vb.len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000278 if (result != NULL) {
279 memcpy(result->ob_bytes, va.buf, va.len);
280 memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
281 }
282
283 done:
284 if (va.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000285 PyBuffer_Release(&va);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000286 if (vb.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000287 PyBuffer_Release(&vb);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000288 return (PyObject *)result;
289}
290
291/* Functions stuffed into the type object */
292
293static Py_ssize_t
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000294bytearray_length(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000295{
296 return Py_SIZE(self);
297}
298
299static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000300bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000301{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000302 Py_ssize_t size;
303 Py_buffer vo;
304
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200305 if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000306 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
307 Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name);
308 return NULL;
309 }
310
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300311 size = Py_SIZE(self);
312 if (size > PY_SSIZE_T_MAX - vo.len) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000313 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000314 return PyErr_NoMemory();
315 }
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300316 if (PyByteArray_Resize((PyObject *)self, size + vo.len) < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000317 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000318 return NULL;
319 }
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300320 memcpy(PyByteArray_AS_STRING(self) + size, vo.buf, vo.len);
Martin v. Löwis423be952008-08-13 15:53:07 +0000321 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000322 Py_INCREF(self);
323 return (PyObject *)self;
324}
325
326static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000327bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000328{
329 PyByteArrayObject *result;
330 Py_ssize_t mysize;
331 Py_ssize_t size;
332
333 if (count < 0)
334 count = 0;
335 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000336 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000337 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000338 size = mysize * count;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000339 result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size);
340 if (result != NULL && size != 0) {
341 if (mysize == 1)
342 memset(result->ob_bytes, self->ob_bytes[0], size);
343 else {
344 Py_ssize_t i;
345 for (i = 0; i < count; i++)
346 memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize);
347 }
348 }
349 return (PyObject *)result;
350}
351
352static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000353bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000354{
355 Py_ssize_t mysize;
356 Py_ssize_t size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200357 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000358
359 if (count < 0)
360 count = 0;
361 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000362 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000363 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000364 size = mysize * count;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200365 if (PyByteArray_Resize((PyObject *)self, size) < 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000366 return NULL;
367
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200368 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000369 if (mysize == 1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200370 memset(buf, buf[0], size);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000371 else {
372 Py_ssize_t i;
373 for (i = 1; i < count; i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200374 memcpy(buf + i*mysize, buf, mysize);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000375 }
376
377 Py_INCREF(self);
378 return (PyObject *)self;
379}
380
381static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000382bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000383{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000384 if (i < 0 || i >= Py_SIZE(self)) {
385 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
386 return NULL;
387 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200388 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000389}
390
391static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000392bytearray_subscript(PyByteArrayObject *self, PyObject *index)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000393{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000394 if (PyIndex_Check(index)) {
395 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000396
397 if (i == -1 && PyErr_Occurred())
398 return NULL;
399
400 if (i < 0)
401 i += PyByteArray_GET_SIZE(self);
402
403 if (i < 0 || i >= Py_SIZE(self)) {
404 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
405 return NULL;
406 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200407 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000408 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000409 else if (PySlice_Check(index)) {
Zackery Spytz14514d92019-05-17 01:13:03 -0600410 Py_ssize_t start, stop, step, slicelength, i;
411 size_t cur;
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300412 if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000413 return NULL;
414 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300415 slicelength = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self),
416 &start, &stop, step);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000417
418 if (slicelength <= 0)
419 return PyByteArray_FromStringAndSize("", 0);
420 else if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200421 return PyByteArray_FromStringAndSize(
422 PyByteArray_AS_STRING(self) + start, slicelength);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000423 }
424 else {
425 char *source_buf = PyByteArray_AS_STRING(self);
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000426 char *result_buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000427 PyObject *result;
428
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000429 result = PyByteArray_FromStringAndSize(NULL, slicelength);
430 if (result == NULL)
431 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000432
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000433 result_buf = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000434 for (cur = start, i = 0; i < slicelength;
435 cur += step, i++) {
436 result_buf[i] = source_buf[cur];
437 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000438 return result;
439 }
440 }
441 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400442 PyErr_Format(PyExc_TypeError,
443 "bytearray indices must be integers or slices, not %.200s",
444 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000445 return NULL;
446 }
447}
448
449static int
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200450bytearray_setslice_linear(PyByteArrayObject *self,
451 Py_ssize_t lo, Py_ssize_t hi,
452 char *bytes, Py_ssize_t bytes_len)
453{
454 Py_ssize_t avail = hi - lo;
455 char *buf = PyByteArray_AS_STRING(self);
456 Py_ssize_t growth = bytes_len - avail;
Victor Stinner84557232013-11-21 12:29:51 +0100457 int res = 0;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200458 assert(avail >= 0);
459
Victor Stinner84557232013-11-21 12:29:51 +0100460 if (growth < 0) {
461 if (!_canresize(self))
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200462 return -1;
Victor Stinner84557232013-11-21 12:29:51 +0100463
464 if (lo == 0) {
465 /* Shrink the buffer by advancing its logical start */
466 self->ob_start -= growth;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200467 /*
Victor Stinner84557232013-11-21 12:29:51 +0100468 0 lo hi old_size
469 | |<----avail----->|<-----tail------>|
470 | |<-bytes_len->|<-----tail------>|
471 0 new_lo new_hi new_size
472 */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200473 }
Victor Stinner84557232013-11-21 12:29:51 +0100474 else {
475 /*
476 0 lo hi old_size
477 | |<----avail----->|<-----tomove------>|
478 | |<-bytes_len->|<-----tomove------>|
479 0 lo new_hi new_size
480 */
481 memmove(buf + lo + bytes_len, buf + hi,
482 Py_SIZE(self) - hi);
483 }
484 if (PyByteArray_Resize((PyObject *)self,
485 Py_SIZE(self) + growth) < 0) {
486 /* Issue #19578: Handling the memory allocation failure here is
487 tricky here because the bytearray object has already been
488 modified. Depending on growth and lo, the behaviour is
489 different.
490
491 If growth < 0 and lo != 0, the operation is completed, but a
492 MemoryError is still raised and the memory block is not
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700493 shrunk. Otherwise, the bytearray is restored in its previous
Victor Stinner84557232013-11-21 12:29:51 +0100494 state and a MemoryError is raised. */
495 if (lo == 0) {
496 self->ob_start += growth;
497 return -1;
498 }
499 /* memmove() removed bytes, the bytearray object cannot be
500 restored in its previous state. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100501 Py_SET_SIZE(self, Py_SIZE(self) + growth);
Victor Stinner84557232013-11-21 12:29:51 +0100502 res = -1;
503 }
504 buf = PyByteArray_AS_STRING(self);
505 }
506 else if (growth > 0) {
507 if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) {
508 PyErr_NoMemory();
509 return -1;
510 }
511
512 if (PyByteArray_Resize((PyObject *)self,
513 Py_SIZE(self) + growth) < 0) {
514 return -1;
515 }
516 buf = PyByteArray_AS_STRING(self);
517 /* Make the place for the additional bytes */
518 /*
519 0 lo hi old_size
520 | |<-avail->|<-----tomove------>|
521 | |<---bytes_len-->|<-----tomove------>|
522 0 lo new_hi new_size
523 */
524 memmove(buf + lo + bytes_len, buf + hi,
525 Py_SIZE(self) - lo - bytes_len);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200526 }
527
528 if (bytes_len > 0)
529 memcpy(buf + lo, bytes, bytes_len);
Victor Stinner84557232013-11-21 12:29:51 +0100530 return res;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200531}
532
533static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000534bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000535 PyObject *values)
536{
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200537 Py_ssize_t needed;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000538 void *bytes;
539 Py_buffer vbytes;
540 int res = 0;
541
542 vbytes.len = -1;
543 if (values == (PyObject *)self) {
544 /* Make a copy and call this function recursively */
545 int err;
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300546 values = PyByteArray_FromStringAndSize(PyByteArray_AS_STRING(values),
547 PyByteArray_GET_SIZE(values));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000548 if (values == NULL)
549 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000550 err = bytearray_setslice(self, lo, hi, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000551 Py_DECREF(values);
552 return err;
553 }
554 if (values == NULL) {
555 /* del b[lo:hi] */
556 bytes = NULL;
557 needed = 0;
558 }
559 else {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200560 if (PyObject_GetBuffer(values, &vbytes, PyBUF_SIMPLE) != 0) {
561 PyErr_Format(PyExc_TypeError,
562 "can't set bytearray slice from %.100s",
563 Py_TYPE(values)->tp_name);
564 return -1;
565 }
566 needed = vbytes.len;
567 bytes = vbytes.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000568 }
569
570 if (lo < 0)
571 lo = 0;
572 if (hi < lo)
573 hi = lo;
574 if (hi > Py_SIZE(self))
575 hi = Py_SIZE(self);
576
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200577 res = bytearray_setslice_linear(self, lo, hi, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000578 if (vbytes.len != -1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200579 PyBuffer_Release(&vbytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000580 return res;
581}
582
583static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000584bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000585{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000586 int ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000587
588 if (i < 0)
589 i += Py_SIZE(self);
590
591 if (i < 0 || i >= Py_SIZE(self)) {
592 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
593 return -1;
594 }
595
596 if (value == NULL)
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000597 return bytearray_setslice(self, i, i+1, NULL);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000598
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000599 if (!_getbytevalue(value, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000600 return -1;
601
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200602 PyByteArray_AS_STRING(self)[i] = ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000603 return 0;
604}
605
606static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000607bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000608{
609 Py_ssize_t start, stop, step, slicelen, needed;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200610 char *buf, *bytes;
611 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000612
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000613 if (PyIndex_Check(index)) {
614 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000615
616 if (i == -1 && PyErr_Occurred())
617 return -1;
618
619 if (i < 0)
620 i += PyByteArray_GET_SIZE(self);
621
622 if (i < 0 || i >= Py_SIZE(self)) {
623 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
624 return -1;
625 }
626
627 if (values == NULL) {
628 /* Fall through to slice assignment */
629 start = i;
630 stop = i + 1;
631 step = 1;
632 slicelen = 1;
633 }
634 else {
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000635 int ival;
636 if (!_getbytevalue(values, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000637 return -1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200638 buf[i] = (char)ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000639 return 0;
640 }
641 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000642 else if (PySlice_Check(index)) {
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300643 if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000644 return -1;
645 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300646 slicelen = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self), &start,
647 &stop, step);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000648 }
649 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400650 PyErr_Format(PyExc_TypeError,
651 "bytearray indices must be integers or slices, not %.200s",
652 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000653 return -1;
654 }
655
656 if (values == NULL) {
657 bytes = NULL;
658 needed = 0;
659 }
660 else if (values == (PyObject *)self || !PyByteArray_Check(values)) {
Christian Heimes6d26ade2012-11-03 23:07:59 +0100661 int err;
Ezio Melottic64bcbe2012-11-03 21:19:06 +0200662 if (PyNumber_Check(values) || PyUnicode_Check(values)) {
663 PyErr_SetString(PyExc_TypeError,
664 "can assign only bytes, buffers, or iterables "
665 "of ints in range(0, 256)");
666 return -1;
667 }
Georg Brandlf3fa5682010-12-04 17:09:30 +0000668 /* Make a copy and call this function recursively */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000669 values = PyByteArray_FromObject(values);
670 if (values == NULL)
671 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000672 err = bytearray_ass_subscript(self, index, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000673 Py_DECREF(values);
674 return err;
675 }
676 else {
677 assert(PyByteArray_Check(values));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200678 bytes = PyByteArray_AS_STRING(values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000679 needed = Py_SIZE(values);
680 }
681 /* Make sure b[5:2] = ... inserts before 5, not before 2. */
682 if ((step < 0 && start < stop) ||
683 (step > 0 && start > stop))
684 stop = start;
685 if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200686 return bytearray_setslice_linear(self, start, stop, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000687 }
688 else {
689 if (needed == 0) {
690 /* Delete slice */
Mark Dickinsonbc099642010-01-29 17:27:24 +0000691 size_t cur;
692 Py_ssize_t i;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000693
Antoine Pitrou5504e892008-12-06 21:27:53 +0000694 if (!_canresize(self))
695 return -1;
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000696
697 if (slicelen == 0)
698 /* Nothing to do here. */
699 return 0;
700
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000701 if (step < 0) {
702 stop = start + 1;
703 start = stop + step * (slicelen - 1) - 1;
704 step = -step;
705 }
706 for (cur = start, i = 0;
707 i < slicelen; cur += step, i++) {
708 Py_ssize_t lim = step - 1;
709
Mark Dickinson66f575b2010-02-14 12:53:32 +0000710 if (cur + step >= (size_t)PyByteArray_GET_SIZE(self))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000711 lim = PyByteArray_GET_SIZE(self) - cur - 1;
712
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200713 memmove(buf + cur - i,
714 buf + cur + 1, lim);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000715 }
716 /* Move the tail of the bytes, in one chunk */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000717 cur = start + (size_t)slicelen*step;
Mark Dickinson66f575b2010-02-14 12:53:32 +0000718 if (cur < (size_t)PyByteArray_GET_SIZE(self)) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200719 memmove(buf + cur - slicelen,
720 buf + cur,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000721 PyByteArray_GET_SIZE(self) - cur);
722 }
723 if (PyByteArray_Resize((PyObject *)self,
724 PyByteArray_GET_SIZE(self) - slicelen) < 0)
725 return -1;
726
727 return 0;
728 }
729 else {
730 /* Assign slice */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000731 Py_ssize_t i;
732 size_t cur;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000733
734 if (needed != slicelen) {
735 PyErr_Format(PyExc_ValueError,
736 "attempt to assign bytes of size %zd "
737 "to extended slice of size %zd",
738 needed, slicelen);
739 return -1;
740 }
741 for (cur = start, i = 0; i < slicelen; cur += step, i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200742 buf[cur] = bytes[i];
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000743 return 0;
744 }
745 }
746}
747
748static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000749bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000750{
751 static char *kwlist[] = {"source", "encoding", "errors", 0};
752 PyObject *arg = NULL;
753 const char *encoding = NULL;
754 const char *errors = NULL;
755 Py_ssize_t count;
756 PyObject *it;
757 PyObject *(*iternext)(PyObject *);
758
759 if (Py_SIZE(self) != 0) {
760 /* Empty previous contents (yes, do this first of all!) */
761 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
762 return -1;
763 }
764
765 /* Parse arguments */
Georg Brandl3dbca812008-07-23 16:10:53 +0000766 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000767 &arg, &encoding, &errors))
768 return -1;
769
770 /* Make a quick exit if no first argument */
771 if (arg == NULL) {
772 if (encoding != NULL || errors != NULL) {
773 PyErr_SetString(PyExc_TypeError,
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300774 encoding != NULL ?
775 "encoding without a string argument" :
776 "errors without a string argument");
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000777 return -1;
778 }
779 return 0;
780 }
781
782 if (PyUnicode_Check(arg)) {
783 /* Encode via the codec registry */
784 PyObject *encoded, *new;
785 if (encoding == NULL) {
786 PyErr_SetString(PyExc_TypeError,
787 "string argument without an encoding");
788 return -1;
789 }
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000790 encoded = PyUnicode_AsEncodedString(arg, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000791 if (encoded == NULL)
792 return -1;
793 assert(PyBytes_Check(encoded));
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000794 new = bytearray_iconcat(self, encoded);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000795 Py_DECREF(encoded);
796 if (new == NULL)
797 return -1;
798 Py_DECREF(new);
799 return 0;
800 }
801
802 /* If it's not unicode, there can't be encoding or errors */
803 if (encoding != NULL || errors != NULL) {
804 PyErr_SetString(PyExc_TypeError,
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300805 encoding != NULL ?
806 "encoding without a string argument" :
807 "errors without a string argument");
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000808 return -1;
809 }
810
811 /* Is it an int? */
Serhiy Storchakaeb249882016-08-15 09:46:07 +0300812 if (PyIndex_Check(arg)) {
813 count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
814 if (count == -1 && PyErr_Occurred()) {
Serhiy Storchakae8904212018-10-15 00:02:57 +0300815 if (!PyErr_ExceptionMatches(PyExc_TypeError))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000816 return -1;
INADA Naokia634e232017-01-06 17:32:01 +0900817 PyErr_Clear(); /* fall through */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000818 }
INADA Naokia634e232017-01-06 17:32:01 +0900819 else {
820 if (count < 0) {
821 PyErr_SetString(PyExc_ValueError, "negative count");
822 return -1;
823 }
824 if (count > 0) {
825 if (PyByteArray_Resize((PyObject *)self, count))
826 return -1;
827 memset(PyByteArray_AS_STRING(self), 0, count);
828 }
829 return 0;
830 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000831 }
832
833 /* Use the buffer API */
834 if (PyObject_CheckBuffer(arg)) {
835 Py_ssize_t size;
836 Py_buffer view;
837 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
838 return -1;
839 size = view.len;
840 if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200841 if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self),
842 &view, size, 'C') < 0)
Stefan Krah7d12d9d2012-07-28 12:25:55 +0200843 goto fail;
Martin v. Löwis423be952008-08-13 15:53:07 +0000844 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000845 return 0;
846 fail:
Martin v. Löwis423be952008-08-13 15:53:07 +0000847 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000848 return -1;
849 }
850
851 /* XXX Optimize this if the arguments is a list, tuple */
852
853 /* Get the iterator */
854 it = PyObject_GetIter(arg);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300855 if (it == NULL) {
856 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
857 PyErr_Format(PyExc_TypeError,
858 "cannot convert '%.200s' object to bytearray",
Victor Stinner58ac7002020-02-07 03:04:21 +0100859 Py_TYPE(arg)->tp_name);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300860 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000861 return -1;
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +0300862 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000863 iternext = *Py_TYPE(it)->tp_iternext;
864
865 /* Run the iterator to exhaustion */
866 for (;;) {
867 PyObject *item;
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000868 int rc, value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000869
870 /* Get the next item */
871 item = iternext(it);
872 if (item == NULL) {
873 if (PyErr_Occurred()) {
874 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
875 goto error;
876 PyErr_Clear();
877 }
878 break;
879 }
880
881 /* Interpret it as an int (__index__) */
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000882 rc = _getbytevalue(item, &value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000883 Py_DECREF(item);
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000884 if (!rc)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000885 goto error;
886
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000887 /* Append the byte */
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300888 if (Py_SIZE(self) + 1 < self->ob_alloc) {
Victor Stinner60ac6ed2020-02-07 23:18:08 +0100889 Py_SET_SIZE(self, Py_SIZE(self) + 1);
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300890 PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0';
891 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000892 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
893 goto error;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200894 PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000895 }
896
897 /* Clean up and return success */
898 Py_DECREF(it);
899 return 0;
900
901 error:
902 /* Error handling when it != NULL */
903 Py_DECREF(it);
904 return -1;
905}
906
907/* Mostly copied from string_repr, but without the
908 "smart quote" functionality. */
909static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000910bytearray_repr(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000911{
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300912 const char *className = _PyType_Name(Py_TYPE(self));
913 const char *quote_prefix = "(b";
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000914 const char *quote_postfix = ")";
915 Py_ssize_t length = Py_SIZE(self);
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300916 /* 6 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
917 Py_ssize_t newsize;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000918 PyObject *v;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200919 Py_ssize_t i;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200920 char *bytes;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200921 char c;
922 char *p;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200923 int quote;
924 char *test, *start;
925 char *buffer;
926
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300927 newsize = strlen(className);
928 if (length > (PY_SSIZE_T_MAX - 6 - newsize) / 4) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000929 PyErr_SetString(PyExc_OverflowError,
930 "bytearray object is too large to make repr");
931 return NULL;
932 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200933
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300934 newsize += 6 + length * 4;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100935 buffer = PyObject_Malloc(newsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200936 if (buffer == NULL) {
937 PyErr_NoMemory();
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000938 return NULL;
939 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000940
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200941 /* Figure out which quote to use; single is preferred */
942 quote = '\'';
943 start = PyByteArray_AS_STRING(self);
944 for (test = start; test < start+length; ++test) {
945 if (*test == '"') {
946 quote = '\''; /* back to single */
947 break;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000948 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200949 else if (*test == '\'')
950 quote = '"';
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000951 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200952
953 p = buffer;
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300954 while (*className)
955 *p++ = *className++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200956 while (*quote_prefix)
957 *p++ = *quote_prefix++;
958 *p++ = quote;
959
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200960 bytes = PyByteArray_AS_STRING(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200961 for (i = 0; i < length; i++) {
962 /* There's at least enough room for a hex escape
963 and a closing quote. */
964 assert(newsize - (p - buffer) >= 5);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200965 c = bytes[i];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200966 if (c == '\'' || c == '\\')
967 *p++ = '\\', *p++ = c;
968 else if (c == '\t')
969 *p++ = '\\', *p++ = 't';
970 else if (c == '\n')
971 *p++ = '\\', *p++ = 'n';
972 else if (c == '\r')
973 *p++ = '\\', *p++ = 'r';
974 else if (c == 0)
975 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
976 else if (c < ' ' || c >= 0x7f) {
977 *p++ = '\\';
978 *p++ = 'x';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200979 *p++ = Py_hexdigits[(c & 0xf0) >> 4];
980 *p++ = Py_hexdigits[c & 0xf];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200981 }
982 else
983 *p++ = c;
984 }
985 assert(newsize - (p - buffer) >= 1);
986 *p++ = quote;
987 while (*quote_postfix) {
988 *p++ = *quote_postfix++;
989 }
990
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300991 v = PyUnicode_FromStringAndSize(buffer, p - buffer);
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100992 PyObject_Free(buffer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200993 return v;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000994}
995
996static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000997bytearray_str(PyObject *op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000998{
Victor Stinner331a6a52019-05-27 16:39:22 +0200999 PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;
Victor Stinnerc96be812019-05-14 17:34:56 +02001000 if (config->bytes_warning) {
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001001 if (PyErr_WarnEx(PyExc_BytesWarning,
1002 "str() on a bytearray instance", 1)) {
1003 return NULL;
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001004 }
Victor Stinner53b7d4e2018-07-25 01:37:05 +02001005 }
1006 return bytearray_repr((PyByteArrayObject*)op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001007}
1008
1009static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001010bytearray_richcompare(PyObject *self, PyObject *other, int op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001011{
1012 Py_ssize_t self_size, other_size;
1013 Py_buffer self_bytes, other_bytes;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001014 int cmp, rc;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001015
1016 /* Bytes can be compared to anything that supports the (binary)
1017 buffer API. Except that a comparison with Unicode is always an
1018 error, even if the comparison is for equality. */
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001019 rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type);
1020 if (!rc)
1021 rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type);
1022 if (rc < 0)
1023 return NULL;
1024 if (rc) {
Victor Stinner331a6a52019-05-27 16:39:22 +02001025 PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;
Victor Stinnerc96be812019-05-14 17:34:56 +02001026 if (config->bytes_warning && (op == Py_EQ || op == Py_NE)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001027 if (PyErr_WarnEx(PyExc_BytesWarning,
Georg Brandle5d68ac2008-06-04 11:30:26 +00001028 "Comparison between bytearray and string", 1))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001029 return NULL;
1030 }
1031
Brian Curtindfc80e32011-08-10 20:28:54 -05001032 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001033 }
1034
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001035 if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001036 PyErr_Clear();
Brian Curtindfc80e32011-08-10 20:28:54 -05001037 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001038 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001039 self_size = self_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001040
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001041 if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001042 PyErr_Clear();
Martin v. Löwis423be952008-08-13 15:53:07 +00001043 PyBuffer_Release(&self_bytes);
Brian Curtindfc80e32011-08-10 20:28:54 -05001044 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001045 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001046 other_size = other_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001047
1048 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
1049 /* Shortcut: if the lengths differ, the objects differ */
stratakise8b19652017-11-02 11:32:54 +01001050 PyBuffer_Release(&self_bytes);
1051 PyBuffer_Release(&other_bytes);
1052 return PyBool_FromLong((op == Py_NE));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001053 }
1054 else {
stratakise8b19652017-11-02 11:32:54 +01001055 cmp = memcmp(self_bytes.buf, other_bytes.buf,
1056 Py_MIN(self_size, other_size));
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001057 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
1058
stratakise8b19652017-11-02 11:32:54 +01001059 PyBuffer_Release(&self_bytes);
1060 PyBuffer_Release(&other_bytes);
1061
1062 if (cmp != 0) {
1063 Py_RETURN_RICHCOMPARE(cmp, 0, op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001064 }
1065
stratakise8b19652017-11-02 11:32:54 +01001066 Py_RETURN_RICHCOMPARE(self_size, other_size, op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001067 }
1068
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001069}
1070
1071static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001072bytearray_dealloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001073{
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001074 if (self->ob_exports > 0) {
1075 PyErr_SetString(PyExc_SystemError,
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001076 "deallocated bytearray object has exported buffers");
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001077 PyErr_Print();
1078 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001079 if (self->ob_bytes != 0) {
Antoine Pitrou39aba4f2011-11-12 21:15:28 +01001080 PyObject_Free(self->ob_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001081 }
1082 Py_TYPE(self)->tp_free((PyObject *)self);
1083}
1084
1085
1086/* -------------------------------------------------------------------- */
1087/* Methods */
1088
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001089#define FASTSEARCH fastsearch
1090#define STRINGLIB(F) stringlib_##F
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001091#define STRINGLIB_CHAR char
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001092#define STRINGLIB_SIZEOF_CHAR 1
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001093#define STRINGLIB_LEN PyByteArray_GET_SIZE
1094#define STRINGLIB_STR PyByteArray_AS_STRING
1095#define STRINGLIB_NEW PyByteArray_FromStringAndSize
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001096#define STRINGLIB_ISSPACE Py_ISSPACE
1097#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001098#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1099#define STRINGLIB_MUTABLE 1
1100
1101#include "stringlib/fastsearch.h"
1102#include "stringlib/count.h"
1103#include "stringlib/find.h"
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001104#include "stringlib/join.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001105#include "stringlib/partition.h"
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001106#include "stringlib/split.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001107#include "stringlib/ctype.h"
1108#include "stringlib/transmogrify.h"
1109
1110
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001111static PyObject *
1112bytearray_find(PyByteArrayObject *self, PyObject *args)
1113{
1114 return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1115}
1116
1117static PyObject *
1118bytearray_count(PyByteArrayObject *self, PyObject *args)
1119{
1120 return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1121}
1122
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001123/*[clinic input]
1124bytearray.clear
1125
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001126Remove all items from the bytearray.
1127[clinic start generated code]*/
1128
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001129static PyObject *
1130bytearray_clear_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001131/*[clinic end generated code: output=85c2fe6aede0956c input=ed6edae9de447ac4]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001132{
1133 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1134 return NULL;
1135 Py_RETURN_NONE;
1136}
1137
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001138/*[clinic input]
1139bytearray.copy
1140
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001141Return a copy of B.
1142[clinic start generated code]*/
1143
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001144static PyObject *
1145bytearray_copy_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001146/*[clinic end generated code: output=68cfbcfed484c132 input=6597b0c01bccaa9e]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001147{
1148 return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self),
1149 PyByteArray_GET_SIZE(self));
1150}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001151
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001152static PyObject *
1153bytearray_index(PyByteArrayObject *self, PyObject *args)
1154{
1155 return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1156}
1157
1158static PyObject *
1159bytearray_rfind(PyByteArrayObject *self, PyObject *args)
1160{
1161 return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1162}
1163
1164static PyObject *
1165bytearray_rindex(PyByteArrayObject *self, PyObject *args)
1166{
1167 return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1168}
1169
1170static int
1171bytearray_contains(PyObject *self, PyObject *arg)
1172{
1173 return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg);
1174}
1175
1176static PyObject *
1177bytearray_startswith(PyByteArrayObject *self, PyObject *args)
1178{
1179 return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1180}
1181
1182static PyObject *
1183bytearray_endswith(PyByteArrayObject *self, PyObject *args)
1184{
1185 return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1186}
1187
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001188
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001189/*[clinic input]
1190bytearray.translate
1191
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001192 table: object
1193 Translation table, which must be a bytes object of length 256.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001194 /
Martin Panter1b6c6da2016-08-27 08:35:02 +00001195 delete as deletechars: object(c_default="NULL") = b''
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001196
1197Return a copy with each character mapped by the given translation table.
1198
Martin Panter1b6c6da2016-08-27 08:35:02 +00001199All characters occurring in the optional argument delete are removed.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001200The remaining characters are mapped through the given translation table.
1201[clinic start generated code]*/
1202
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001203static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001204bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
Martin Panter1b6c6da2016-08-27 08:35:02 +00001205 PyObject *deletechars)
1206/*[clinic end generated code: output=b6a8f01c2a74e446 input=cfff956d4d127a9b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001207{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001208 char *input, *output;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001209 const char *table_chars;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001210 Py_ssize_t i, c;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001211 PyObject *input_obj = (PyObject*)self;
1212 const char *output_start;
1213 Py_ssize_t inlen;
Georg Brandlccc47b62008-12-28 11:44:14 +00001214 PyObject *result = NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001215 int trans_table[256];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001216 Py_buffer vtable, vdel;
1217
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001218 if (table == Py_None) {
1219 table_chars = NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001220 table = NULL;
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001221 } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001222 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001223 } else {
1224 if (vtable.len != 256) {
1225 PyErr_SetString(PyExc_ValueError,
1226 "translation table must be 256 characters long");
Georg Brandl953152f2009-07-22 12:03:59 +00001227 PyBuffer_Release(&vtable);
1228 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001229 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001230 table_chars = (const char*)vtable.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001231 }
1232
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001233 if (deletechars != NULL) {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001234 if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) {
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001235 if (table != NULL)
Georg Brandl953152f2009-07-22 12:03:59 +00001236 PyBuffer_Release(&vtable);
1237 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001238 }
1239 }
1240 else {
1241 vdel.buf = NULL;
1242 vdel.len = 0;
1243 }
1244
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001245 inlen = PyByteArray_GET_SIZE(input_obj);
1246 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1247 if (result == NULL)
1248 goto done;
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03001249 output_start = output = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001250 input = PyByteArray_AS_STRING(input_obj);
1251
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001252 if (vdel.len == 0 && table_chars != NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001253 /* If no deletions are required, use faster code */
1254 for (i = inlen; --i >= 0; ) {
1255 c = Py_CHARMASK(*input++);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001256 *output++ = table_chars[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001257 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001258 goto done;
1259 }
Georg Brandlccc47b62008-12-28 11:44:14 +00001260
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001261 if (table_chars == NULL) {
Georg Brandlccc47b62008-12-28 11:44:14 +00001262 for (i = 0; i < 256; i++)
1263 trans_table[i] = Py_CHARMASK(i);
1264 } else {
1265 for (i = 0; i < 256; i++)
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001266 trans_table[i] = Py_CHARMASK(table_chars[i]);
Georg Brandlccc47b62008-12-28 11:44:14 +00001267 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001268
1269 for (i = 0; i < vdel.len; i++)
1270 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1271
1272 for (i = inlen; --i >= 0; ) {
1273 c = Py_CHARMASK(*input++);
1274 if (trans_table[c] != -1)
Martin Panter1b6c6da2016-08-27 08:35:02 +00001275 *output++ = (char)trans_table[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001276 }
1277 /* Fix the size of the resulting string */
1278 if (inlen > 0)
Christian Heimesc731bbe2013-07-21 02:04:35 +02001279 if (PyByteArray_Resize(result, output - output_start) < 0) {
1280 Py_CLEAR(result);
1281 goto done;
1282 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001283
1284done:
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001285 if (table != NULL)
Georg Brandlccc47b62008-12-28 11:44:14 +00001286 PyBuffer_Release(&vtable);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001287 if (deletechars != NULL)
Martin v. Löwis423be952008-08-13 15:53:07 +00001288 PyBuffer_Release(&vdel);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001289 return result;
1290}
1291
1292
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001293/*[clinic input]
1294
1295@staticmethod
1296bytearray.maketrans
1297
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001298 frm: Py_buffer
1299 to: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001300 /
1301
1302Return a translation table useable for the bytes or bytearray translate method.
1303
1304The returned table will be one where each byte in frm is mapped to the byte at
1305the same position in to.
1306
1307The bytes objects frm and to must be of the same length.
1308[clinic start generated code]*/
1309
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001310static PyObject *
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001311bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001312/*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001313{
1314 return _Py_bytes_maketrans(frm, to);
Georg Brandlabc38772009-04-12 15:51:51 +00001315}
1316
1317
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001318/*[clinic input]
1319bytearray.replace
1320
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001321 old: Py_buffer
1322 new: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001323 count: Py_ssize_t = -1
1324 Maximum number of occurrences to replace.
1325 -1 (the default value) means replace all occurrences.
1326 /
1327
1328Return a copy with all occurrences of substring old replaced by new.
1329
1330If the optional argument count is given, only the first count occurrences are
1331replaced.
1332[clinic start generated code]*/
1333
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001334static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001335bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
1336 Py_buffer *new, Py_ssize_t count)
1337/*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001338{
Serhiy Storchakafb81d3c2016-05-05 09:26:07 +03001339 return stringlib_replace((PyObject *)self,
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001340 (const char *)old->buf, old->len,
1341 (const char *)new->buf, new->len, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001342}
1343
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001344/*[clinic input]
1345bytearray.split
1346
1347 sep: object = None
1348 The delimiter according which to split the bytearray.
1349 None (the default value) means split on ASCII whitespace characters
1350 (space, tab, return, newline, formfeed, vertical tab).
1351 maxsplit: Py_ssize_t = -1
1352 Maximum number of splits to do.
1353 -1 (the default value) means no limit.
1354
1355Return a list of the sections in the bytearray, using sep as the delimiter.
1356[clinic start generated code]*/
1357
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001358static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001359bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
1360 Py_ssize_t maxsplit)
1361/*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001362{
1363 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001364 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001365 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001366 Py_buffer vsub;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001367
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001368 if (maxsplit < 0)
1369 maxsplit = PY_SSIZE_T_MAX;
1370
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001371 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001372 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001373
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001374 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001375 return NULL;
1376 sub = vsub.buf;
1377 n = vsub.len;
1378
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001379 list = stringlib_split(
1380 (PyObject*) self, s, len, sub, n, maxsplit
1381 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001382 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001383 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001384}
1385
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001386/*[clinic input]
1387bytearray.partition
1388
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001389 sep: object
1390 /
1391
1392Partition the bytearray into three parts using the given separator.
1393
1394This will search for the separator sep in the bytearray. If the separator is
1395found, returns a 3-tuple containing the part before the separator, the
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001396separator itself, and the part after it as new bytearray objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001397
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001398If the separator is not found, returns a 3-tuple containing the copy of the
1399original bytearray object and two empty bytearray objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001400[clinic start generated code]*/
1401
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001402static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001403bytearray_partition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001404/*[clinic end generated code: output=45d2525ddd35f957 input=8f644749ee4fc83a]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001405{
1406 PyObject *bytesep, *result;
1407
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001408 bytesep = _PyByteArray_FromBufferObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001409 if (! bytesep)
1410 return NULL;
1411
1412 result = stringlib_partition(
1413 (PyObject*) self,
1414 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1415 bytesep,
1416 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1417 );
1418
1419 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001420 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001421}
1422
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001423/*[clinic input]
1424bytearray.rpartition
1425
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001426 sep: object
1427 /
1428
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001429Partition the bytearray into three parts using the given separator.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001430
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001431This will search for the separator sep in the bytearray, starting at the end.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001432If the separator is found, returns a 3-tuple containing the part before the
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001433separator, the separator itself, and the part after it as new bytearray
1434objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001435
1436If the separator is not found, returns a 3-tuple containing two empty bytearray
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001437objects and the copy of the original bytearray object.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001438[clinic start generated code]*/
1439
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001440static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001441bytearray_rpartition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001442/*[clinic end generated code: output=440de3c9426115e8 input=7e3df3e6cb8fa0ac]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001443{
1444 PyObject *bytesep, *result;
1445
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001446 bytesep = _PyByteArray_FromBufferObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001447 if (! bytesep)
1448 return NULL;
1449
1450 result = stringlib_rpartition(
1451 (PyObject*) self,
1452 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1453 bytesep,
1454 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1455 );
1456
1457 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001458 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001459}
1460
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001461/*[clinic input]
1462bytearray.rsplit = bytearray.split
1463
1464Return a list of the sections in the bytearray, using sep as the delimiter.
1465
1466Splitting is done starting at the end of the bytearray and working to the front.
1467[clinic start generated code]*/
1468
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001469static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001470bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
1471 Py_ssize_t maxsplit)
1472/*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001473{
1474 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001475 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001476 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001477 Py_buffer vsub;
1478
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001479 if (maxsplit < 0)
1480 maxsplit = PY_SSIZE_T_MAX;
1481
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001482 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001483 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001484
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001485 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001486 return NULL;
1487 sub = vsub.buf;
1488 n = vsub.len;
1489
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001490 list = stringlib_rsplit(
1491 (PyObject*) self, s, len, sub, n, maxsplit
1492 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001493 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001494 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001495}
1496
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001497/*[clinic input]
1498bytearray.reverse
1499
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001500Reverse the order of the values in B in place.
1501[clinic start generated code]*/
1502
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001503static PyObject *
1504bytearray_reverse_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001505/*[clinic end generated code: output=9f7616f29ab309d3 input=543356319fc78557]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001506{
1507 char swap, *head, *tail;
1508 Py_ssize_t i, j, n = Py_SIZE(self);
1509
1510 j = n / 2;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001511 head = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001512 tail = head + n - 1;
1513 for (i = 0; i < j; i++) {
1514 swap = *head;
1515 *head++ = *tail;
1516 *tail-- = swap;
1517 }
1518
1519 Py_RETURN_NONE;
1520}
1521
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001522
1523/*[python input]
1524class bytesvalue_converter(CConverter):
1525 type = 'int'
1526 converter = '_getbytevalue'
1527[python start generated code]*/
1528/*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/
1529
1530
1531/*[clinic input]
1532bytearray.insert
1533
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001534 index: Py_ssize_t
1535 The index where the value is to be inserted.
1536 item: bytesvalue
1537 The item to be inserted.
1538 /
1539
1540Insert a single item into the bytearray before the given index.
1541[clinic start generated code]*/
1542
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001543static PyObject *
1544bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001545/*[clinic end generated code: output=76c775a70e7b07b7 input=b2b5d07e9de6c070]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001546{
1547 Py_ssize_t n = Py_SIZE(self);
1548 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001549
1550 if (n == PY_SSIZE_T_MAX) {
1551 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001552 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001553 return NULL;
1554 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001555 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1556 return NULL;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001557 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001558
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001559 if (index < 0) {
1560 index += n;
1561 if (index < 0)
1562 index = 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001563 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001564 if (index > n)
1565 index = n;
1566 memmove(buf + index + 1, buf + index, n - index);
1567 buf[index] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001568
1569 Py_RETURN_NONE;
1570}
1571
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001572/*[clinic input]
1573bytearray.append
1574
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001575 item: bytesvalue
1576 The item to be appended.
1577 /
1578
1579Append a single item to the end of the bytearray.
1580[clinic start generated code]*/
1581
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001582static PyObject *
1583bytearray_append_impl(PyByteArrayObject *self, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001584/*[clinic end generated code: output=a154e19ed1886cb6 input=20d6bec3d1340593]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001585{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001586 Py_ssize_t n = Py_SIZE(self);
1587
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001588 if (n == PY_SSIZE_T_MAX) {
1589 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001590 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001591 return NULL;
1592 }
1593 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1594 return NULL;
1595
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001596 PyByteArray_AS_STRING(self)[n] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001597
1598 Py_RETURN_NONE;
1599}
1600
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001601/*[clinic input]
1602bytearray.extend
1603
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001604 iterable_of_ints: object
1605 The iterable of items to append.
1606 /
1607
1608Append all the items from the iterator or sequence to the end of the bytearray.
1609[clinic start generated code]*/
1610
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001611static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001612bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001613/*[clinic end generated code: output=98155dbe249170b1 input=c617b3a93249ba28]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001614{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001615 PyObject *it, *item, *bytearray_obj;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001616 Py_ssize_t buf_size = 0, len = 0;
1617 int value;
1618 char *buf;
1619
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001620 /* bytearray_setslice code only accepts something supporting PEP 3118. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001621 if (PyObject_CheckBuffer(iterable_of_ints)) {
1622 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_ints) == -1)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001623 return NULL;
1624
1625 Py_RETURN_NONE;
1626 }
1627
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001628 it = PyObject_GetIter(iterable_of_ints);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +03001629 if (it == NULL) {
1630 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
1631 PyErr_Format(PyExc_TypeError,
1632 "can't extend bytearray with %.100s",
Victor Stinner58ac7002020-02-07 03:04:21 +01001633 Py_TYPE(iterable_of_ints)->tp_name);
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +03001634 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001635 return NULL;
Serhiy Storchaka2c2044e2018-10-21 15:29:12 +03001636 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001637
Ezio Melotti42da6632011-03-15 05:18:48 +02001638 /* Try to determine the length of the argument. 32 is arbitrary. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001639 buf_size = PyObject_LengthHint(iterable_of_ints, 32);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001640 if (buf_size == -1) {
1641 Py_DECREF(it);
1642 return NULL;
1643 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001644
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001645 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001646 if (bytearray_obj == NULL) {
1647 Py_DECREF(it);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001648 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001649 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001650 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001651
1652 while ((item = PyIter_Next(it)) != NULL) {
1653 if (! _getbytevalue(item, &value)) {
1654 Py_DECREF(item);
1655 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001656 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001657 return NULL;
1658 }
1659 buf[len++] = value;
1660 Py_DECREF(item);
1661
1662 if (len >= buf_size) {
Martin Panter371731e2016-07-18 07:53:13 +00001663 Py_ssize_t addition;
1664 if (len == PY_SSIZE_T_MAX) {
1665 Py_DECREF(it);
1666 Py_DECREF(bytearray_obj);
1667 return PyErr_NoMemory();
1668 }
1669 addition = len >> 1;
1670 if (addition > PY_SSIZE_T_MAX - len - 1)
1671 buf_size = PY_SSIZE_T_MAX;
1672 else
1673 buf_size = len + addition + 1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001674 if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001675 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001676 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001677 return NULL;
1678 }
1679 /* Recompute the `buf' pointer, since the resizing operation may
1680 have invalidated it. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001681 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001682 }
1683 }
1684 Py_DECREF(it);
1685
1686 /* Resize down to exact size. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001687 if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) {
1688 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001689 return NULL;
1690 }
1691
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001692 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) {
1693 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001694 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001695 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001696 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001697
Brandt Bucher2a7d5962019-06-26 12:06:18 -07001698 if (PyErr_Occurred()) {
1699 return NULL;
1700 }
1701
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001702 Py_RETURN_NONE;
1703}
1704
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001705/*[clinic input]
1706bytearray.pop
1707
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001708 index: Py_ssize_t = -1
1709 The index from where to remove the item.
1710 -1 (the default value) means remove the last item.
1711 /
1712
1713Remove and return a single item from B.
1714
1715If no index argument is given, will pop the last item.
1716[clinic start generated code]*/
1717
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001718static PyObject *
1719bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001720/*[clinic end generated code: output=e0ccd401f8021da8 input=3591df2d06c0d237]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001721{
1722 int value;
1723 Py_ssize_t n = Py_SIZE(self);
1724 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001725
1726 if (n == 0) {
Eli Bendersky1bc4f192011-03-04 04:55:25 +00001727 PyErr_SetString(PyExc_IndexError,
1728 "pop from empty bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001729 return NULL;
1730 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001731 if (index < 0)
1732 index += Py_SIZE(self);
1733 if (index < 0 || index >= Py_SIZE(self)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001734 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1735 return NULL;
1736 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001737 if (!_canresize(self))
1738 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001739
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001740 buf = PyByteArray_AS_STRING(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001741 value = buf[index];
1742 memmove(buf + index, buf + index + 1, n - index);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001743 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1744 return NULL;
1745
Mark Dickinson54a3db92009-09-06 10:19:23 +00001746 return PyLong_FromLong((unsigned char)value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001747}
1748
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001749/*[clinic input]
1750bytearray.remove
1751
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001752 value: bytesvalue
1753 The value to remove.
1754 /
1755
1756Remove the first occurrence of a value in the bytearray.
1757[clinic start generated code]*/
1758
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001759static PyObject *
1760bytearray_remove_impl(PyByteArrayObject *self, int value)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001761/*[clinic end generated code: output=d659e37866709c13 input=121831240cd51ddf]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001762{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001763 Py_ssize_t where, n = Py_SIZE(self);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001764 char *buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001765
Serhiy Storchaka4b234942016-05-16 22:24:03 +03001766 where = stringlib_find_char(buf, n, value);
1767 if (where < 0) {
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001768 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001769 return NULL;
1770 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001771 if (!_canresize(self))
1772 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001773
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001774 memmove(buf + where, buf + where + 1, n - where);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001775 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1776 return NULL;
1777
1778 Py_RETURN_NONE;
1779}
1780
1781/* XXX These two helpers could be optimized if argsize == 1 */
1782
1783static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001784lstrip_helper(const char *myptr, Py_ssize_t mysize,
1785 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001786{
1787 Py_ssize_t i = 0;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001788 while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001789 i++;
1790 return i;
1791}
1792
1793static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001794rstrip_helper(const char *myptr, Py_ssize_t mysize,
1795 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001796{
1797 Py_ssize_t i = mysize - 1;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001798 while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001799 i--;
1800 return i + 1;
1801}
1802
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001803/*[clinic input]
1804bytearray.strip
1805
1806 bytes: object = None
1807 /
1808
1809Strip leading and trailing bytes contained in the argument.
1810
1811If the argument is omitted or None, strip leading and trailing ASCII whitespace.
1812[clinic start generated code]*/
1813
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001814static PyObject *
1815bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001816/*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001817{
1818 Py_ssize_t left, right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001819 char *myptr;
1820 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001821 Py_buffer vbytes;
1822
1823 if (bytes == Py_None) {
1824 bytesptr = "\t\n\r\f\v ";
1825 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001826 }
1827 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001828 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001829 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001830 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001831 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001832 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001833 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001834 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001835 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001836 if (left == mysize)
1837 right = left;
1838 else
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001839 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1840 if (bytes != Py_None)
1841 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001842 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001843}
1844
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001845/*[clinic input]
1846bytearray.lstrip
1847
1848 bytes: object = None
1849 /
1850
1851Strip leading bytes contained in the argument.
1852
1853If the argument is omitted or None, strip leading ASCII whitespace.
1854[clinic start generated code]*/
1855
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001856static PyObject *
1857bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001858/*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001859{
1860 Py_ssize_t left, right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001861 char *myptr;
1862 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001863 Py_buffer vbytes;
1864
1865 if (bytes == Py_None) {
1866 bytesptr = "\t\n\r\f\v ";
1867 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001868 }
1869 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001870 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001871 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001872 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001873 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001874 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001875 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001876 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001877 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001878 right = mysize;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001879 if (bytes != Py_None)
1880 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001881 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001882}
1883
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001884/*[clinic input]
1885bytearray.rstrip
1886
1887 bytes: object = None
1888 /
1889
1890Strip trailing bytes contained in the argument.
1891
1892If the argument is omitted or None, strip trailing ASCII whitespace.
1893[clinic start generated code]*/
1894
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001895static PyObject *
1896bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001897/*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001898{
1899 Py_ssize_t right, mysize, byteslen;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001900 char *myptr;
1901 const char *bytesptr;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001902 Py_buffer vbytes;
1903
1904 if (bytes == Py_None) {
1905 bytesptr = "\t\n\r\f\v ";
1906 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001907 }
1908 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001909 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001910 return NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001911 bytesptr = (const char *) vbytes.buf;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001912 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001913 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001914 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001915 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001916 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1917 if (bytes != Py_None)
1918 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001919 return PyByteArray_FromStringAndSize(myptr, right);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001920}
1921
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001922/*[clinic input]
1923bytearray.decode
1924
1925 encoding: str(c_default="NULL") = 'utf-8'
1926 The encoding with which to decode the bytearray.
1927 errors: str(c_default="NULL") = 'strict'
1928 The error handling scheme to use for the handling of decoding errors.
1929 The default is 'strict' meaning that decoding errors raise a
1930 UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
1931 as well as any other name registered with codecs.register_error that
1932 can handle UnicodeDecodeErrors.
1933
1934Decode the bytearray using the codec registered for encoding.
1935[clinic start generated code]*/
1936
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001937static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001938bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
1939 const char *errors)
1940/*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001941{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001942 if (encoding == NULL)
1943 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis0efea322014-07-27 17:29:17 +02001944 return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001945}
1946
1947PyDoc_STRVAR(alloc_doc,
1948"B.__alloc__() -> int\n\
1949\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00001950Return the number of bytes actually allocated.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001951
1952static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301953bytearray_alloc(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001954{
1955 return PyLong_FromSsize_t(self->ob_alloc);
1956}
1957
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001958/*[clinic input]
1959bytearray.join
1960
1961 iterable_of_bytes: object
1962 /
1963
1964Concatenate any number of bytes/bytearray objects.
1965
1966The bytearray whose method is called is inserted in between each pair.
1967
1968The result is returned as a new bytearray object.
1969[clinic start generated code]*/
1970
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001971static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001972bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001973/*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001974{
Martin v. Löwis0efea322014-07-27 17:29:17 +02001975 return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001976}
1977
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001978/*[clinic input]
1979bytearray.splitlines
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001980
Serhiy Storchaka202fda52017-03-12 10:10:47 +02001981 keepends: bool(accept={int}) = False
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001982
1983Return a list of the lines in the bytearray, breaking at line boundaries.
1984
1985Line breaks are not included in the resulting list unless keepends is given and
1986true.
1987[clinic start generated code]*/
1988
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001989static PyObject *
1990bytearray_splitlines_impl(PyByteArrayObject *self, int keepends)
Serhiy Storchaka202fda52017-03-12 10:10:47 +02001991/*[clinic end generated code: output=4223c94b895f6ad9 input=99a27ad959b9cf6b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001992{
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001993 return stringlib_splitlines(
1994 (PyObject*) self, PyByteArray_AS_STRING(self),
1995 PyByteArray_GET_SIZE(self), keepends
1996 );
1997}
1998
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001999/*[clinic input]
2000@classmethod
2001bytearray.fromhex
2002
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002003 string: unicode
2004 /
2005
2006Create a bytearray object from a string of hexadecimal numbers.
2007
2008Spaces between two numbers are accepted.
2009Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')
2010[clinic start generated code]*/
2011
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002012static PyObject *
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002013bytearray_fromhex_impl(PyTypeObject *type, PyObject *string)
2014/*[clinic end generated code: output=8f0f0b6d30fb3ba0 input=f033a16d1fb21f48]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002015{
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002016 PyObject *result = _PyBytes_FromHex(string, type == &PyByteArray_Type);
2017 if (type != &PyByteArray_Type && result != NULL) {
Petr Viktorinffd97532020-02-11 17:46:57 +01002018 Py_SETREF(result, PyObject_CallOneArg((PyObject *)type, result));
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002019 }
2020 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002021}
2022
Gregory P. Smith0c2f9302019-05-29 11:46:58 -07002023/*[clinic input]
2024bytearray.hex
2025
2026 sep: object = NULL
2027 An optional single character or byte to separate hex bytes.
2028 bytes_per_sep: int = 1
2029 How many bytes between separators. Positive values count from the
2030 right, negative values count from the left.
2031
2032Create a str of hexadecimal numbers from a bytearray object.
2033
2034Example:
2035>>> value = bytearray([0xb9, 0x01, 0xef])
2036>>> value.hex()
2037'b901ef'
2038>>> value.hex(':')
2039'b9:01:ef'
2040>>> value.hex(':', 2)
2041'b9:01ef'
2042>>> value.hex(':', -2)
2043'b901:ef'
2044[clinic start generated code]*/
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002045
2046static PyObject *
Gregory P. Smith0c2f9302019-05-29 11:46:58 -07002047bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep)
2048/*[clinic end generated code: output=29c4e5ef72c565a0 input=814c15830ac8c4b5]*/
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002049{
2050 char* argbuf = PyByteArray_AS_STRING(self);
2051 Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
Gregory P. Smith0c2f9302019-05-29 11:46:58 -07002052 return _Py_strhex_with_sep(argbuf, arglen, sep, bytes_per_sep);
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002053}
2054
2055static PyObject *
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002056_common_reduce(PyByteArrayObject *self, int proto)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002057{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002058 PyObject *dict;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002059 _Py_IDENTIFIER(__dict__);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002060 char *buf;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002061
Serhiy Storchaka41c57b32019-09-01 12:03:39 +03002062 if (_PyObject_LookupAttrId((PyObject *)self, &PyId___dict__, &dict) < 0) {
2063 return NULL;
2064 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002065 if (dict == NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002066 dict = Py_None;
2067 Py_INCREF(dict);
2068 }
2069
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002070 buf = PyByteArray_AS_STRING(self);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002071 if (proto < 3) {
2072 /* use str based reduction for backwards compatibility with Python 2.x */
2073 PyObject *latin1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002074 if (Py_SIZE(self))
2075 latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002076 else
2077 latin1 = PyUnicode_FromString("");
2078 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
2079 }
2080 else {
2081 /* use more efficient byte based reduction */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002082 if (Py_SIZE(self)) {
2083 return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002084 }
2085 else {
2086 return Py_BuildValue("(O()N)", Py_TYPE(self), dict);
2087 }
2088 }
2089}
2090
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002091/*[clinic input]
2092bytearray.__reduce__ as bytearray_reduce
2093
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002094Return state information for pickling.
2095[clinic start generated code]*/
2096
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002097static PyObject *
2098bytearray_reduce_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002099/*[clinic end generated code: output=52bf304086464cab input=44b5737ada62dd3f]*/
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002100{
2101 return _common_reduce(self, 2);
2102}
2103
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002104/*[clinic input]
2105bytearray.__reduce_ex__ as bytearray_reduce_ex
2106
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002107 proto: int = 0
2108 /
2109
2110Return state information for pickling.
2111[clinic start generated code]*/
2112
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002113static PyObject *
2114bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002115/*[clinic end generated code: output=52eac33377197520 input=f129bc1a1aa151ee]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002116{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002117 return _common_reduce(self, proto);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002118}
2119
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002120/*[clinic input]
2121bytearray.__sizeof__ as bytearray_sizeof
2122
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002123Returns the size of the bytearray object in memory, in bytes.
2124[clinic start generated code]*/
2125
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002126static PyObject *
2127bytearray_sizeof_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002128/*[clinic end generated code: output=738abdd17951c427 input=e27320fd98a4bc5a]*/
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002129{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002130 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002131
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002132 res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002133 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002134}
2135
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002136static PySequenceMethods bytearray_as_sequence = {
2137 (lenfunc)bytearray_length, /* sq_length */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002138 (binaryfunc)PyByteArray_Concat, /* sq_concat */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002139 (ssizeargfunc)bytearray_repeat, /* sq_repeat */
2140 (ssizeargfunc)bytearray_getitem, /* sq_item */
2141 0, /* sq_slice */
2142 (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */
2143 0, /* sq_ass_slice */
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002144 (objobjproc)bytearray_contains, /* sq_contains */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002145 (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */
2146 (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002147};
2148
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002149static PyMappingMethods bytearray_as_mapping = {
2150 (lenfunc)bytearray_length,
2151 (binaryfunc)bytearray_subscript,
2152 (objobjargproc)bytearray_ass_subscript,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002153};
2154
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002155static PyBufferProcs bytearray_as_buffer = {
2156 (getbufferproc)bytearray_getbuffer,
2157 (releasebufferproc)bytearray_releasebuffer,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002158};
2159
2160static PyMethodDef
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002161bytearray_methods[] = {
2162 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002163 BYTEARRAY_REDUCE_METHODDEF
2164 BYTEARRAY_REDUCE_EX_METHODDEF
2165 BYTEARRAY_SIZEOF_METHODDEF
2166 BYTEARRAY_APPEND_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302167 {"capitalize", stringlib_capitalize, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002168 _Py_capitalize__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002169 STRINGLIB_CENTER_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002170 BYTEARRAY_CLEAR_METHODDEF
2171 BYTEARRAY_COPY_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002172 {"count", (PyCFunction)bytearray_count, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002173 _Py_count__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002174 BYTEARRAY_DECODE_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002175 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002176 _Py_endswith__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002177 STRINGLIB_EXPANDTABS_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002178 BYTEARRAY_EXTEND_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002179 {"find", (PyCFunction)bytearray_find, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002180 _Py_find__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002181 BYTEARRAY_FROMHEX_METHODDEF
Gregory P. Smith0c2f9302019-05-29 11:46:58 -07002182 BYTEARRAY_HEX_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002183 {"index", (PyCFunction)bytearray_index, METH_VARARGS, _Py_index__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002184 BYTEARRAY_INSERT_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302185 {"isalnum", stringlib_isalnum, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002186 _Py_isalnum__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302187 {"isalpha", stringlib_isalpha, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002188 _Py_isalpha__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302189 {"isascii", stringlib_isascii, METH_NOARGS,
INADA Naokia49ac992018-01-27 14:06:21 +09002190 _Py_isascii__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302191 {"isdigit", stringlib_isdigit, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002192 _Py_isdigit__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302193 {"islower", stringlib_islower, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002194 _Py_islower__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302195 {"isspace", stringlib_isspace, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002196 _Py_isspace__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302197 {"istitle", stringlib_istitle, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002198 _Py_istitle__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302199 {"isupper", stringlib_isupper, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002200 _Py_isupper__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002201 BYTEARRAY_JOIN_METHODDEF
Tal Einatc929df32018-07-06 13:17:38 +03002202 STRINGLIB_LJUST_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302203 {"lower", stringlib_lower, METH_NOARGS, _Py_lower__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002204 BYTEARRAY_LSTRIP_METHODDEF
2205 BYTEARRAY_MAKETRANS_METHODDEF
2206 BYTEARRAY_PARTITION_METHODDEF
2207 BYTEARRAY_POP_METHODDEF
2208 BYTEARRAY_REMOVE_METHODDEF
2209 BYTEARRAY_REPLACE_METHODDEF
2210 BYTEARRAY_REVERSE_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002211 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__},
2212 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002213 STRINGLIB_RJUST_METHODDEF
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002214 BYTEARRAY_RPARTITION_METHODDEF
2215 BYTEARRAY_RSPLIT_METHODDEF
2216 BYTEARRAY_RSTRIP_METHODDEF
2217 BYTEARRAY_SPLIT_METHODDEF
2218 BYTEARRAY_SPLITLINES_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002219 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002220 _Py_startswith__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002221 BYTEARRAY_STRIP_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302222 {"swapcase", stringlib_swapcase, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002223 _Py_swapcase__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302224 {"title", stringlib_title, METH_NOARGS, _Py_title__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002225 BYTEARRAY_TRANSLATE_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302226 {"upper", stringlib_upper, METH_NOARGS, _Py_upper__doc__},
Tal Einatc929df32018-07-06 13:17:38 +03002227 STRINGLIB_ZFILL_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002228 {NULL}
2229};
2230
Ethan Furmanb95b5612015-01-23 20:05:18 -08002231static PyObject *
2232bytearray_mod(PyObject *v, PyObject *w)
2233{
2234 if (!PyByteArray_Check(v))
2235 Py_RETURN_NOTIMPLEMENTED;
Berker Peksag43de36d2016-04-16 01:20:47 +03002236 return _PyBytes_FormatEx(PyByteArray_AS_STRING(v), PyByteArray_GET_SIZE(v), w, 1);
Ethan Furmanb95b5612015-01-23 20:05:18 -08002237}
2238
2239static PyNumberMethods bytearray_as_number = {
2240 0, /*nb_add*/
2241 0, /*nb_subtract*/
2242 0, /*nb_multiply*/
2243 bytearray_mod, /*nb_remainder*/
2244};
2245
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002246PyDoc_STRVAR(bytearray_doc,
Georg Brandl17cb8a82008-05-30 08:20:09 +00002247"bytearray(iterable_of_ints) -> bytearray\n\
2248bytearray(string, encoding[, errors]) -> bytearray\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002249bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\
2250bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\
2251bytearray() -> empty bytes array\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002252\n\
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03002253Construct a mutable bytearray object from:\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002254 - an iterable yielding integers in range(256)\n\
2255 - a text string encoded using the specified encoding\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002256 - a bytes or a buffer object\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002257 - any object implementing the buffer API.\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002258 - an integer");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002259
2260
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002261static PyObject *bytearray_iter(PyObject *seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002262
2263PyTypeObject PyByteArray_Type = {
2264 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2265 "bytearray",
2266 sizeof(PyByteArrayObject),
2267 0,
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002268 (destructor)bytearray_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002269 0, /* tp_vectorcall_offset */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002270 0, /* tp_getattr */
2271 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002272 0, /* tp_as_async */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002273 (reprfunc)bytearray_repr, /* tp_repr */
Ethan Furmanb95b5612015-01-23 20:05:18 -08002274 &bytearray_as_number, /* tp_as_number */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002275 &bytearray_as_sequence, /* tp_as_sequence */
2276 &bytearray_as_mapping, /* tp_as_mapping */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002277 0, /* tp_hash */
2278 0, /* tp_call */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002279 bytearray_str, /* tp_str */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002280 PyObject_GenericGetAttr, /* tp_getattro */
2281 0, /* tp_setattro */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002282 &bytearray_as_buffer, /* tp_as_buffer */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002283 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002284 bytearray_doc, /* tp_doc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002285 0, /* tp_traverse */
2286 0, /* tp_clear */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002287 (richcmpfunc)bytearray_richcompare, /* tp_richcompare */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002288 0, /* tp_weaklistoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002289 bytearray_iter, /* tp_iter */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002290 0, /* tp_iternext */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002291 bytearray_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002292 0, /* tp_members */
2293 0, /* tp_getset */
2294 0, /* tp_base */
2295 0, /* tp_dict */
2296 0, /* tp_descr_get */
2297 0, /* tp_descr_set */
2298 0, /* tp_dictoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002299 (initproc)bytearray_init, /* tp_init */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002300 PyType_GenericAlloc, /* tp_alloc */
2301 PyType_GenericNew, /* tp_new */
2302 PyObject_Del, /* tp_free */
2303};
2304
2305/*********************** Bytes Iterator ****************************/
2306
2307typedef struct {
2308 PyObject_HEAD
2309 Py_ssize_t it_index;
2310 PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
2311} bytesiterobject;
2312
2313static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002314bytearrayiter_dealloc(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002315{
2316 _PyObject_GC_UNTRACK(it);
2317 Py_XDECREF(it->it_seq);
2318 PyObject_GC_Del(it);
2319}
2320
2321static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002322bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002323{
2324 Py_VISIT(it->it_seq);
2325 return 0;
2326}
2327
2328static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002329bytearrayiter_next(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002330{
2331 PyByteArrayObject *seq;
2332 PyObject *item;
2333
2334 assert(it != NULL);
2335 seq = it->it_seq;
2336 if (seq == NULL)
2337 return NULL;
2338 assert(PyByteArray_Check(seq));
2339
2340 if (it->it_index < PyByteArray_GET_SIZE(seq)) {
2341 item = PyLong_FromLong(
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002342 (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002343 if (item != NULL)
2344 ++it->it_index;
2345 return item;
2346 }
2347
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002348 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03002349 Py_DECREF(seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002350 return NULL;
2351}
2352
2353static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302354bytearrayiter_length_hint(bytesiterobject *it, PyObject *Py_UNUSED(ignored))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002355{
2356 Py_ssize_t len = 0;
Serhiy Storchakaaf658722016-07-03 14:41:36 +03002357 if (it->it_seq) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002358 len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
Serhiy Storchakaaf658722016-07-03 14:41:36 +03002359 if (len < 0) {
2360 len = 0;
2361 }
2362 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002363 return PyLong_FromSsize_t(len);
2364}
2365
2366PyDoc_STRVAR(length_hint_doc,
2367 "Private method returning an estimate of len(list(it)).");
2368
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002369static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302370bytearrayiter_reduce(bytesiterobject *it, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002371{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002372 _Py_IDENTIFIER(iter);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002373 if (it->it_seq != NULL) {
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002374 return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002375 it->it_seq, it->it_index);
2376 } else {
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02002377 return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002378 }
2379}
2380
2381static PyObject *
2382bytearrayiter_setstate(bytesiterobject *it, PyObject *state)
2383{
2384 Py_ssize_t index = PyLong_AsSsize_t(state);
2385 if (index == -1 && PyErr_Occurred())
2386 return NULL;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00002387 if (it->it_seq != NULL) {
2388 if (index < 0)
2389 index = 0;
2390 else if (index > PyByteArray_GET_SIZE(it->it_seq))
2391 index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */
2392 it->it_index = index;
2393 }
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002394 Py_RETURN_NONE;
2395}
2396
2397PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
2398
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002399static PyMethodDef bytearrayiter_methods[] = {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002400 {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002401 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002402 {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002403 bytearray_reduce__doc__},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002404 {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
2405 setstate_doc},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002406 {NULL, NULL} /* sentinel */
2407};
2408
2409PyTypeObject PyByteArrayIter_Type = {
2410 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2411 "bytearray_iterator", /* tp_name */
2412 sizeof(bytesiterobject), /* tp_basicsize */
2413 0, /* tp_itemsize */
2414 /* methods */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002415 (destructor)bytearrayiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002416 0, /* tp_vectorcall_offset */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002417 0, /* tp_getattr */
2418 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002419 0, /* tp_as_async */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002420 0, /* tp_repr */
2421 0, /* tp_as_number */
2422 0, /* tp_as_sequence */
2423 0, /* tp_as_mapping */
2424 0, /* tp_hash */
2425 0, /* tp_call */
2426 0, /* tp_str */
2427 PyObject_GenericGetAttr, /* tp_getattro */
2428 0, /* tp_setattro */
2429 0, /* tp_as_buffer */
2430 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
2431 0, /* tp_doc */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002432 (traverseproc)bytearrayiter_traverse, /* tp_traverse */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002433 0, /* tp_clear */
2434 0, /* tp_richcompare */
2435 0, /* tp_weaklistoffset */
2436 PyObject_SelfIter, /* tp_iter */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002437 (iternextfunc)bytearrayiter_next, /* tp_iternext */
2438 bytearrayiter_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002439 0,
2440};
2441
2442static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002443bytearray_iter(PyObject *seq)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002444{
2445 bytesiterobject *it;
2446
2447 if (!PyByteArray_Check(seq)) {
2448 PyErr_BadInternalCall();
2449 return NULL;
2450 }
2451 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
2452 if (it == NULL)
2453 return NULL;
2454 it->it_index = 0;
2455 Py_INCREF(seq);
2456 it->it_seq = (PyByteArrayObject *)seq;
2457 _PyObject_GC_TRACK(it);
2458 return (PyObject *)it;
2459}