blob: c92cfc01fd1860784fe51b4e195b35fa47d9b02d [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"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06005#include "internal/mem.h"
6#include "internal/pystate.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00007#include "structmember.h"
8#include "bytes_methods.h"
Ethan Furmanb95b5612015-01-23 20:05:18 -08009#include "bytesobject.h"
Gregory P. Smith8cb65692015-04-25 23:22:26 +000010#include "pystrhex.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +000011
Martin v. Löwis7252a6e2014-07-27 16:25:09 +020012/*[clinic input]
13class bytearray "PyByteArrayObject *" "&PyByteArray_Type"
14[clinic start generated code]*/
15/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/
16
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000017char _PyByteArray_empty_string[] = "";
Christian Heimes2c9c7a52008-05-26 13:42:13 +000018
19void
20PyByteArray_Fini(void)
21{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000022}
23
24int
25PyByteArray_Init(void)
26{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000027 return 1;
28}
29
30/* end nullbytes support */
31
32/* Helpers */
33
34static int
35_getbytevalue(PyObject* arg, int *value)
36{
37 long face_value;
38
39 if (PyLong_Check(arg)) {
40 face_value = PyLong_AsLong(arg);
Georg Brandl9a54d7c2008-07-16 23:15:30 +000041 } else {
42 PyObject *index = PyNumber_Index(arg);
43 if (index == NULL) {
44 PyErr_Format(PyExc_TypeError, "an integer is required");
Mark Dickinson10de93a2010-07-09 19:25:48 +000045 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000046 return 0;
47 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +000048 face_value = PyLong_AsLong(index);
49 Py_DECREF(index);
50 }
51
52 if (face_value < 0 || face_value >= 256) {
53 /* this includes the OverflowError in case the long is too large */
54 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
Mark Dickinson10de93a2010-07-09 19:25:48 +000055 *value = -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000056 return 0;
57 }
58
59 *value = face_value;
60 return 1;
61}
62
63static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +000064bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000065{
Christian Heimes2c9c7a52008-05-26 13:42:13 +000066 void *ptr;
67 if (view == NULL) {
Stefan Krah5178d912015-02-03 16:57:21 +010068 PyErr_SetString(PyExc_BufferError,
69 "bytearray_getbuffer: view==NULL argument is obsolete");
70 return -1;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000071 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000072 ptr = (void *) PyByteArray_AS_STRING(obj);
Stefan Krah5178d912015-02-03 16:57:21 +010073 /* cannot fail if view != NULL and readonly == 0 */
74 (void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
75 obj->ob_exports++;
76 return 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000077}
78
79static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +000080bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000081{
82 obj->ob_exports--;
83}
84
Antoine Pitrou5504e892008-12-06 21:27:53 +000085static int
86_canresize(PyByteArrayObject *self)
87{
88 if (self->ob_exports > 0) {
89 PyErr_SetString(PyExc_BufferError,
90 "Existing exports of data: object cannot be re-sized");
91 return 0;
92 }
93 return 1;
94}
95
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030096#include "clinic/bytearrayobject.c.h"
97
Christian Heimes2c9c7a52008-05-26 13:42:13 +000098/* Direct API functions */
99
100PyObject *
101PyByteArray_FromObject(PyObject *input)
102{
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100103 return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type,
104 input, NULL);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000105}
106
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300107static PyObject *
108_PyByteArray_FromBufferObject(PyObject *obj)
109{
110 PyObject *result;
111 Py_buffer view;
112
113 if (PyObject_GetBuffer(obj, &view, PyBUF_FULL_RO) < 0) {
114 return NULL;
115 }
116 result = PyByteArray_FromStringAndSize(NULL, view.len);
117 if (result != NULL &&
118 PyBuffer_ToContiguous(PyByteArray_AS_STRING(result),
119 &view, view.len, 'C') < 0)
120 {
121 Py_CLEAR(result);
122 }
123 PyBuffer_Release(&view);
124 return result;
125}
126
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000127PyObject *
128PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
129{
130 PyByteArrayObject *new;
131 Py_ssize_t alloc;
132
133 if (size < 0) {
134 PyErr_SetString(PyExc_SystemError,
135 "Negative size passed to PyByteArray_FromStringAndSize");
136 return NULL;
137 }
138
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000139 /* Prevent buffer overflow when setting alloc to size+1. */
140 if (size == PY_SSIZE_T_MAX) {
141 return PyErr_NoMemory();
142 }
143
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000144 new = PyObject_New(PyByteArrayObject, &PyByteArray_Type);
145 if (new == NULL)
146 return NULL;
147
148 if (size == 0) {
149 new->ob_bytes = NULL;
150 alloc = 0;
151 }
152 else {
153 alloc = size + 1;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100154 new->ob_bytes = PyObject_Malloc(alloc);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000155 if (new->ob_bytes == NULL) {
156 Py_DECREF(new);
157 return PyErr_NoMemory();
158 }
Antoine Pitroufc8d6f42010-01-17 12:38:54 +0000159 if (bytes != NULL && size > 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000160 memcpy(new->ob_bytes, bytes, size);
161 new->ob_bytes[size] = '\0'; /* Trailing null byte */
162 }
163 Py_SIZE(new) = size;
164 new->ob_alloc = alloc;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200165 new->ob_start = new->ob_bytes;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000166 new->ob_exports = 0;
167
168 return (PyObject *)new;
169}
170
171Py_ssize_t
172PyByteArray_Size(PyObject *self)
173{
174 assert(self != NULL);
175 assert(PyByteArray_Check(self));
176
177 return PyByteArray_GET_SIZE(self);
178}
179
180char *
181PyByteArray_AsString(PyObject *self)
182{
183 assert(self != NULL);
184 assert(PyByteArray_Check(self));
185
186 return PyByteArray_AS_STRING(self);
187}
188
189int
Antoine Pitroucc231542014-11-02 18:40:09 +0100190PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000191{
192 void *sval;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200193 PyByteArrayObject *obj = ((PyByteArrayObject *)self);
Antoine Pitroucc231542014-11-02 18:40:09 +0100194 /* All computations are done unsigned to avoid integer overflows
195 (see issue #22335). */
196 size_t alloc = (size_t) obj->ob_alloc;
197 size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes);
198 size_t size = (size_t) requested_size;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000199
200 assert(self != NULL);
201 assert(PyByteArray_Check(self));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200202 assert(logical_offset <= alloc);
Antoine Pitroucc231542014-11-02 18:40:09 +0100203 assert(requested_size >= 0);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000204
Antoine Pitroucc231542014-11-02 18:40:09 +0100205 if (requested_size == Py_SIZE(self)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000206 return 0;
207 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200208 if (!_canresize(obj)) {
Antoine Pitrou5504e892008-12-06 21:27:53 +0000209 return -1;
210 }
211
Antoine Pitrou25454112015-05-19 20:52:27 +0200212 if (size + logical_offset + 1 <= alloc) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200213 /* Current buffer is large enough to host the requested size,
214 decide on a strategy. */
215 if (size < alloc / 2) {
216 /* Major downsize; resize down to exact size */
217 alloc = size + 1;
218 }
219 else {
220 /* Minor downsize; quick exit */
221 Py_SIZE(self) = size;
222 PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */
223 return 0;
224 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000225 }
226 else {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200227 /* Need growing, decide on a strategy */
228 if (size <= alloc * 1.125) {
229 /* Moderate upsize; overallocate similar to list_resize() */
230 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
231 }
232 else {
233 /* Major upsize; resize up to exact size */
234 alloc = size + 1;
235 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000236 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100237 if (alloc > PY_SSIZE_T_MAX) {
238 PyErr_NoMemory();
239 return -1;
240 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000241
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200242 if (logical_offset > 0) {
243 sval = PyObject_Malloc(alloc);
244 if (sval == NULL) {
245 PyErr_NoMemory();
246 return -1;
247 }
Antoine Pitroucc231542014-11-02 18:40:09 +0100248 memcpy(sval, PyByteArray_AS_STRING(self),
Stefan Krahdce65022017-08-25 20:12:05 +0200249 Py_MIN((size_t)requested_size, (size_t)Py_SIZE(self)));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200250 PyObject_Free(obj->ob_bytes);
251 }
252 else {
253 sval = PyObject_Realloc(obj->ob_bytes, alloc);
254 if (sval == NULL) {
255 PyErr_NoMemory();
256 return -1;
257 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000258 }
259
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200260 obj->ob_bytes = obj->ob_start = sval;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000261 Py_SIZE(self) = size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200262 obj->ob_alloc = alloc;
263 obj->ob_bytes[size] = '\0'; /* Trailing null byte */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000264
265 return 0;
266}
267
268PyObject *
269PyByteArray_Concat(PyObject *a, PyObject *b)
270{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000271 Py_buffer va, vb;
272 PyByteArrayObject *result = NULL;
273
274 va.len = -1;
275 vb.len = -1;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200276 if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 ||
277 PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000278 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
Serhiy Storchaka6b5a9ec2017-03-19 19:47:02 +0200279 Py_TYPE(b)->tp_name, Py_TYPE(a)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000280 goto done;
281 }
282
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300283 if (va.len > PY_SSIZE_T_MAX - vb.len) {
284 PyErr_NoMemory();
285 goto done;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000286 }
287
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300288 result = (PyByteArrayObject *) \
289 PyByteArray_FromStringAndSize(NULL, va.len + vb.len);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000290 if (result != NULL) {
291 memcpy(result->ob_bytes, va.buf, va.len);
292 memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
293 }
294
295 done:
296 if (va.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000297 PyBuffer_Release(&va);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000298 if (vb.len != -1)
Martin v. Löwis423be952008-08-13 15:53:07 +0000299 PyBuffer_Release(&vb);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000300 return (PyObject *)result;
301}
302
303/* Functions stuffed into the type object */
304
305static Py_ssize_t
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000306bytearray_length(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000307{
308 return Py_SIZE(self);
309}
310
311static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000312bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000313{
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000314 Py_ssize_t size;
315 Py_buffer vo;
316
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200317 if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000318 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
319 Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name);
320 return NULL;
321 }
322
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300323 size = Py_SIZE(self);
324 if (size > PY_SSIZE_T_MAX - vo.len) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000325 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000326 return PyErr_NoMemory();
327 }
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300328 if (PyByteArray_Resize((PyObject *)self, size + vo.len) < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000329 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000330 return NULL;
331 }
Serhiy Storchaka06cfb0c2016-07-10 20:48:43 +0300332 memcpy(PyByteArray_AS_STRING(self) + size, vo.buf, vo.len);
Martin v. Löwis423be952008-08-13 15:53:07 +0000333 PyBuffer_Release(&vo);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000334 Py_INCREF(self);
335 return (PyObject *)self;
336}
337
338static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000339bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000340{
341 PyByteArrayObject *result;
342 Py_ssize_t mysize;
343 Py_ssize_t size;
344
345 if (count < 0)
346 count = 0;
347 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000348 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000349 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000350 size = mysize * count;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000351 result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size);
352 if (result != NULL && size != 0) {
353 if (mysize == 1)
354 memset(result->ob_bytes, self->ob_bytes[0], size);
355 else {
356 Py_ssize_t i;
357 for (i = 0; i < count; i++)
358 memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize);
359 }
360 }
361 return (PyObject *)result;
362}
363
364static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000365bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000366{
367 Py_ssize_t mysize;
368 Py_ssize_t size;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200369 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000370
371 if (count < 0)
372 count = 0;
373 mysize = Py_SIZE(self);
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000374 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000375 return PyErr_NoMemory();
Mark Dickinsoncf940c72010-08-10 18:35:01 +0000376 size = mysize * count;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200377 if (PyByteArray_Resize((PyObject *)self, size) < 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000378 return NULL;
379
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200380 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000381 if (mysize == 1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200382 memset(buf, buf[0], size);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000383 else {
384 Py_ssize_t i;
385 for (i = 1; i < count; i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200386 memcpy(buf + i*mysize, buf, mysize);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000387 }
388
389 Py_INCREF(self);
390 return (PyObject *)self;
391}
392
393static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000394bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000395{
396 if (i < 0)
397 i += Py_SIZE(self);
398 if (i < 0 || i >= Py_SIZE(self)) {
399 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
400 return NULL;
401 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200402 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000403}
404
405static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000406bytearray_subscript(PyByteArrayObject *self, PyObject *index)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000407{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000408 if (PyIndex_Check(index)) {
409 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000410
411 if (i == -1 && PyErr_Occurred())
412 return NULL;
413
414 if (i < 0)
415 i += PyByteArray_GET_SIZE(self);
416
417 if (i < 0 || i >= Py_SIZE(self)) {
418 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
419 return NULL;
420 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200421 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000422 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000423 else if (PySlice_Check(index)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000424 Py_ssize_t start, stop, step, slicelength, cur, i;
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300425 if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000426 return NULL;
427 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300428 slicelength = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self),
429 &start, &stop, step);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000430
431 if (slicelength <= 0)
432 return PyByteArray_FromStringAndSize("", 0);
433 else if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200434 return PyByteArray_FromStringAndSize(
435 PyByteArray_AS_STRING(self) + start, slicelength);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000436 }
437 else {
438 char *source_buf = PyByteArray_AS_STRING(self);
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000439 char *result_buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000440 PyObject *result;
441
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000442 result = PyByteArray_FromStringAndSize(NULL, slicelength);
443 if (result == NULL)
444 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000445
Alexandre Vassalottie2641f42009-04-03 06:38:02 +0000446 result_buf = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000447 for (cur = start, i = 0; i < slicelength;
448 cur += step, i++) {
449 result_buf[i] = source_buf[cur];
450 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000451 return result;
452 }
453 }
454 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400455 PyErr_Format(PyExc_TypeError,
456 "bytearray indices must be integers or slices, not %.200s",
457 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000458 return NULL;
459 }
460}
461
462static int
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200463bytearray_setslice_linear(PyByteArrayObject *self,
464 Py_ssize_t lo, Py_ssize_t hi,
465 char *bytes, Py_ssize_t bytes_len)
466{
467 Py_ssize_t avail = hi - lo;
468 char *buf = PyByteArray_AS_STRING(self);
469 Py_ssize_t growth = bytes_len - avail;
Victor Stinner84557232013-11-21 12:29:51 +0100470 int res = 0;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200471 assert(avail >= 0);
472
Victor Stinner84557232013-11-21 12:29:51 +0100473 if (growth < 0) {
474 if (!_canresize(self))
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200475 return -1;
Victor Stinner84557232013-11-21 12:29:51 +0100476
477 if (lo == 0) {
478 /* Shrink the buffer by advancing its logical start */
479 self->ob_start -= growth;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200480 /*
Victor Stinner84557232013-11-21 12:29:51 +0100481 0 lo hi old_size
482 | |<----avail----->|<-----tail------>|
483 | |<-bytes_len->|<-----tail------>|
484 0 new_lo new_hi new_size
485 */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200486 }
Victor Stinner84557232013-11-21 12:29:51 +0100487 else {
488 /*
489 0 lo hi old_size
490 | |<----avail----->|<-----tomove------>|
491 | |<-bytes_len->|<-----tomove------>|
492 0 lo new_hi new_size
493 */
494 memmove(buf + lo + bytes_len, buf + hi,
495 Py_SIZE(self) - hi);
496 }
497 if (PyByteArray_Resize((PyObject *)self,
498 Py_SIZE(self) + growth) < 0) {
499 /* Issue #19578: Handling the memory allocation failure here is
500 tricky here because the bytearray object has already been
501 modified. Depending on growth and lo, the behaviour is
502 different.
503
504 If growth < 0 and lo != 0, the operation is completed, but a
505 MemoryError is still raised and the memory block is not
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700506 shrunk. Otherwise, the bytearray is restored in its previous
Victor Stinner84557232013-11-21 12:29:51 +0100507 state and a MemoryError is raised. */
508 if (lo == 0) {
509 self->ob_start += growth;
510 return -1;
511 }
512 /* memmove() removed bytes, the bytearray object cannot be
513 restored in its previous state. */
514 Py_SIZE(self) += growth;
515 res = -1;
516 }
517 buf = PyByteArray_AS_STRING(self);
518 }
519 else if (growth > 0) {
520 if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) {
521 PyErr_NoMemory();
522 return -1;
523 }
524
525 if (PyByteArray_Resize((PyObject *)self,
526 Py_SIZE(self) + growth) < 0) {
527 return -1;
528 }
529 buf = PyByteArray_AS_STRING(self);
530 /* Make the place for the additional bytes */
531 /*
532 0 lo hi old_size
533 | |<-avail->|<-----tomove------>|
534 | |<---bytes_len-->|<-----tomove------>|
535 0 lo new_hi new_size
536 */
537 memmove(buf + lo + bytes_len, buf + hi,
538 Py_SIZE(self) - lo - bytes_len);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200539 }
540
541 if (bytes_len > 0)
542 memcpy(buf + lo, bytes, bytes_len);
Victor Stinner84557232013-11-21 12:29:51 +0100543 return res;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200544}
545
546static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000547bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000548 PyObject *values)
549{
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200550 Py_ssize_t needed;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000551 void *bytes;
552 Py_buffer vbytes;
553 int res = 0;
554
555 vbytes.len = -1;
556 if (values == (PyObject *)self) {
557 /* Make a copy and call this function recursively */
558 int err;
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300559 values = PyByteArray_FromStringAndSize(PyByteArray_AS_STRING(values),
560 PyByteArray_GET_SIZE(values));
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000561 if (values == NULL)
562 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000563 err = bytearray_setslice(self, lo, hi, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000564 Py_DECREF(values);
565 return err;
566 }
567 if (values == NULL) {
568 /* del b[lo:hi] */
569 bytes = NULL;
570 needed = 0;
571 }
572 else {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200573 if (PyObject_GetBuffer(values, &vbytes, PyBUF_SIMPLE) != 0) {
574 PyErr_Format(PyExc_TypeError,
575 "can't set bytearray slice from %.100s",
576 Py_TYPE(values)->tp_name);
577 return -1;
578 }
579 needed = vbytes.len;
580 bytes = vbytes.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000581 }
582
583 if (lo < 0)
584 lo = 0;
585 if (hi < lo)
586 hi = lo;
587 if (hi > Py_SIZE(self))
588 hi = Py_SIZE(self);
589
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200590 res = bytearray_setslice_linear(self, lo, hi, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000591 if (vbytes.len != -1)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200592 PyBuffer_Release(&vbytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000593 return res;
594}
595
596static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000597bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000598{
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000599 int ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000600
601 if (i < 0)
602 i += Py_SIZE(self);
603
604 if (i < 0 || i >= Py_SIZE(self)) {
605 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
606 return -1;
607 }
608
609 if (value == NULL)
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000610 return bytearray_setslice(self, i, i+1, NULL);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000611
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000612 if (!_getbytevalue(value, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000613 return -1;
614
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200615 PyByteArray_AS_STRING(self)[i] = ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000616 return 0;
617}
618
619static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000620bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000621{
622 Py_ssize_t start, stop, step, slicelen, needed;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200623 char *buf, *bytes;
624 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000625
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000626 if (PyIndex_Check(index)) {
627 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000628
629 if (i == -1 && PyErr_Occurred())
630 return -1;
631
632 if (i < 0)
633 i += PyByteArray_GET_SIZE(self);
634
635 if (i < 0 || i >= Py_SIZE(self)) {
636 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
637 return -1;
638 }
639
640 if (values == NULL) {
641 /* Fall through to slice assignment */
642 start = i;
643 stop = i + 1;
644 step = 1;
645 slicelen = 1;
646 }
647 else {
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000648 int ival;
649 if (!_getbytevalue(values, &ival))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000650 return -1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200651 buf[i] = (char)ival;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000652 return 0;
653 }
654 }
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000655 else if (PySlice_Check(index)) {
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300656 if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000657 return -1;
658 }
Serhiy Storchakab879fe82017-04-08 09:53:51 +0300659 slicelen = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self), &start,
660 &stop, step);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000661 }
662 else {
Terry Jan Reedyffff1442014-08-02 01:30:37 -0400663 PyErr_Format(PyExc_TypeError,
664 "bytearray indices must be integers or slices, not %.200s",
665 Py_TYPE(index)->tp_name);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000666 return -1;
667 }
668
669 if (values == NULL) {
670 bytes = NULL;
671 needed = 0;
672 }
673 else if (values == (PyObject *)self || !PyByteArray_Check(values)) {
Christian Heimes6d26ade2012-11-03 23:07:59 +0100674 int err;
Ezio Melottic64bcbe2012-11-03 21:19:06 +0200675 if (PyNumber_Check(values) || PyUnicode_Check(values)) {
676 PyErr_SetString(PyExc_TypeError,
677 "can assign only bytes, buffers, or iterables "
678 "of ints in range(0, 256)");
679 return -1;
680 }
Georg Brandlf3fa5682010-12-04 17:09:30 +0000681 /* Make a copy and call this function recursively */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000682 values = PyByteArray_FromObject(values);
683 if (values == NULL)
684 return -1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000685 err = bytearray_ass_subscript(self, index, values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000686 Py_DECREF(values);
687 return err;
688 }
689 else {
690 assert(PyByteArray_Check(values));
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200691 bytes = PyByteArray_AS_STRING(values);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000692 needed = Py_SIZE(values);
693 }
694 /* Make sure b[5:2] = ... inserts before 5, not before 2. */
695 if ((step < 0 && start < stop) ||
696 (step > 0 && start > stop))
697 stop = start;
698 if (step == 1) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200699 return bytearray_setslice_linear(self, start, stop, bytes, needed);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000700 }
701 else {
702 if (needed == 0) {
703 /* Delete slice */
Mark Dickinsonbc099642010-01-29 17:27:24 +0000704 size_t cur;
705 Py_ssize_t i;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000706
Antoine Pitrou5504e892008-12-06 21:27:53 +0000707 if (!_canresize(self))
708 return -1;
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000709
710 if (slicelen == 0)
711 /* Nothing to do here. */
712 return 0;
713
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000714 if (step < 0) {
715 stop = start + 1;
716 start = stop + step * (slicelen - 1) - 1;
717 step = -step;
718 }
719 for (cur = start, i = 0;
720 i < slicelen; cur += step, i++) {
721 Py_ssize_t lim = step - 1;
722
Mark Dickinson66f575b2010-02-14 12:53:32 +0000723 if (cur + step >= (size_t)PyByteArray_GET_SIZE(self))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000724 lim = PyByteArray_GET_SIZE(self) - cur - 1;
725
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200726 memmove(buf + cur - i,
727 buf + cur + 1, lim);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000728 }
729 /* Move the tail of the bytes, in one chunk */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000730 cur = start + (size_t)slicelen*step;
Mark Dickinson66f575b2010-02-14 12:53:32 +0000731 if (cur < (size_t)PyByteArray_GET_SIZE(self)) {
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200732 memmove(buf + cur - slicelen,
733 buf + cur,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000734 PyByteArray_GET_SIZE(self) - cur);
735 }
736 if (PyByteArray_Resize((PyObject *)self,
737 PyByteArray_GET_SIZE(self) - slicelen) < 0)
738 return -1;
739
740 return 0;
741 }
742 else {
743 /* Assign slice */
Mark Dickinson7e3b9482010-08-06 21:33:18 +0000744 Py_ssize_t i;
745 size_t cur;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000746
747 if (needed != slicelen) {
748 PyErr_Format(PyExc_ValueError,
749 "attempt to assign bytes of size %zd "
750 "to extended slice of size %zd",
751 needed, slicelen);
752 return -1;
753 }
754 for (cur = start, i = 0; i < slicelen; cur += step, i++)
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200755 buf[cur] = bytes[i];
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000756 return 0;
757 }
758 }
759}
760
761static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000762bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000763{
764 static char *kwlist[] = {"source", "encoding", "errors", 0};
765 PyObject *arg = NULL;
766 const char *encoding = NULL;
767 const char *errors = NULL;
768 Py_ssize_t count;
769 PyObject *it;
770 PyObject *(*iternext)(PyObject *);
771
772 if (Py_SIZE(self) != 0) {
773 /* Empty previous contents (yes, do this first of all!) */
774 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
775 return -1;
776 }
777
778 /* Parse arguments */
Georg Brandl3dbca812008-07-23 16:10:53 +0000779 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist,
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000780 &arg, &encoding, &errors))
781 return -1;
782
783 /* Make a quick exit if no first argument */
784 if (arg == NULL) {
785 if (encoding != NULL || errors != NULL) {
786 PyErr_SetString(PyExc_TypeError,
787 "encoding or errors without sequence argument");
788 return -1;
789 }
790 return 0;
791 }
792
793 if (PyUnicode_Check(arg)) {
794 /* Encode via the codec registry */
795 PyObject *encoded, *new;
796 if (encoding == NULL) {
797 PyErr_SetString(PyExc_TypeError,
798 "string argument without an encoding");
799 return -1;
800 }
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000801 encoded = PyUnicode_AsEncodedString(arg, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000802 if (encoded == NULL)
803 return -1;
804 assert(PyBytes_Check(encoded));
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000805 new = bytearray_iconcat(self, encoded);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000806 Py_DECREF(encoded);
807 if (new == NULL)
808 return -1;
809 Py_DECREF(new);
810 return 0;
811 }
812
813 /* If it's not unicode, there can't be encoding or errors */
814 if (encoding != NULL || errors != NULL) {
815 PyErr_SetString(PyExc_TypeError,
816 "encoding or errors without a string argument");
817 return -1;
818 }
819
820 /* Is it an int? */
Serhiy Storchakaeb249882016-08-15 09:46:07 +0300821 if (PyIndex_Check(arg)) {
822 count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
823 if (count == -1 && PyErr_Occurred()) {
INADA Naokia634e232017-01-06 17:32:01 +0900824 if (PyErr_ExceptionMatches(PyExc_OverflowError))
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000825 return -1;
INADA Naokia634e232017-01-06 17:32:01 +0900826 PyErr_Clear(); /* fall through */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000827 }
INADA Naokia634e232017-01-06 17:32:01 +0900828 else {
829 if (count < 0) {
830 PyErr_SetString(PyExc_ValueError, "negative count");
831 return -1;
832 }
833 if (count > 0) {
834 if (PyByteArray_Resize((PyObject *)self, count))
835 return -1;
836 memset(PyByteArray_AS_STRING(self), 0, count);
837 }
838 return 0;
839 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000840 }
841
842 /* Use the buffer API */
843 if (PyObject_CheckBuffer(arg)) {
844 Py_ssize_t size;
845 Py_buffer view;
846 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
847 return -1;
848 size = view.len;
849 if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200850 if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self),
851 &view, size, 'C') < 0)
Stefan Krah7d12d9d2012-07-28 12:25:55 +0200852 goto fail;
Martin v. Löwis423be952008-08-13 15:53:07 +0000853 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000854 return 0;
855 fail:
Martin v. Löwis423be952008-08-13 15:53:07 +0000856 PyBuffer_Release(&view);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000857 return -1;
858 }
859
860 /* XXX Optimize this if the arguments is a list, tuple */
861
862 /* Get the iterator */
863 it = PyObject_GetIter(arg);
864 if (it == NULL)
865 return -1;
866 iternext = *Py_TYPE(it)->tp_iternext;
867
868 /* Run the iterator to exhaustion */
869 for (;;) {
870 PyObject *item;
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000871 int rc, value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000872
873 /* Get the next item */
874 item = iternext(it);
875 if (item == NULL) {
876 if (PyErr_Occurred()) {
877 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
878 goto error;
879 PyErr_Clear();
880 }
881 break;
882 }
883
884 /* Interpret it as an int (__index__) */
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000885 rc = _getbytevalue(item, &value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000886 Py_DECREF(item);
Georg Brandl9a54d7c2008-07-16 23:15:30 +0000887 if (!rc)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000888 goto error;
889
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000890 /* Append the byte */
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300891 if (Py_SIZE(self) + 1 < self->ob_alloc) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000892 Py_SIZE(self)++;
Serhiy Storchaka7b6e3b92015-06-29 21:14:06 +0300893 PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0';
894 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000895 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
896 goto error;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200897 PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000898 }
899
900 /* Clean up and return success */
901 Py_DECREF(it);
902 return 0;
903
904 error:
905 /* Error handling when it != NULL */
906 Py_DECREF(it);
907 return -1;
908}
909
910/* Mostly copied from string_repr, but without the
911 "smart quote" functionality. */
912static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +0000913bytearray_repr(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000914{
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300915 const char *className = _PyType_Name(Py_TYPE(self));
916 const char *quote_prefix = "(b";
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000917 const char *quote_postfix = ")";
918 Py_ssize_t length = Py_SIZE(self);
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300919 /* 6 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
920 Py_ssize_t newsize;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000921 PyObject *v;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200922 Py_ssize_t i;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200923 char *bytes;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200924 char c;
925 char *p;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200926 int quote;
927 char *test, *start;
928 char *buffer;
929
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300930 newsize = strlen(className);
931 if (length > (PY_SSIZE_T_MAX - 6 - newsize) / 4) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000932 PyErr_SetString(PyExc_OverflowError,
933 "bytearray object is too large to make repr");
934 return NULL;
935 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200936
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300937 newsize += 6 + length * 4;
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100938 buffer = PyObject_Malloc(newsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200939 if (buffer == NULL) {
940 PyErr_NoMemory();
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000941 return NULL;
942 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000943
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200944 /* Figure out which quote to use; single is preferred */
945 quote = '\'';
946 start = PyByteArray_AS_STRING(self);
947 for (test = start; test < start+length; ++test) {
948 if (*test == '"') {
949 quote = '\''; /* back to single */
950 break;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000951 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200952 else if (*test == '\'')
953 quote = '"';
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000954 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200955
956 p = buffer;
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300957 while (*className)
958 *p++ = *className++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200959 while (*quote_prefix)
960 *p++ = *quote_prefix++;
961 *p++ = quote;
962
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200963 bytes = PyByteArray_AS_STRING(self);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200964 for (i = 0; i < length; i++) {
965 /* There's at least enough room for a hex escape
966 and a closing quote. */
967 assert(newsize - (p - buffer) >= 5);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +0200968 c = bytes[i];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200969 if (c == '\'' || c == '\\')
970 *p++ = '\\', *p++ = c;
971 else if (c == '\t')
972 *p++ = '\\', *p++ = 't';
973 else if (c == '\n')
974 *p++ = '\\', *p++ = 'n';
975 else if (c == '\r')
976 *p++ = '\\', *p++ = 'r';
977 else if (c == 0)
978 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
979 else if (c < ' ' || c >= 0x7f) {
980 *p++ = '\\';
981 *p++ = 'x';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200982 *p++ = Py_hexdigits[(c & 0xf0) >> 4];
983 *p++ = Py_hexdigits[c & 0xf];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200984 }
985 else
986 *p++ = c;
987 }
988 assert(newsize - (p - buffer) >= 1);
989 *p++ = quote;
990 while (*quote_postfix) {
991 *p++ = *quote_postfix++;
992 }
993
Serhiy Storchakab3a77962017-09-21 14:24:13 +0300994 v = PyUnicode_FromStringAndSize(buffer, p - buffer);
Antoine Pitrou39aba4f2011-11-12 21:15:28 +0100995 PyObject_Free(buffer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200996 return v;
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000997}
998
999static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001000bytearray_str(PyObject *op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001001{
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001002 if (Py_BytesWarningFlag) {
1003 if (PyErr_WarnEx(PyExc_BytesWarning,
1004 "str() on a bytearray instance", 1))
1005 return NULL;
1006 }
1007 return bytearray_repr((PyByteArrayObject*)op);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001008}
1009
1010static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001011bytearray_richcompare(PyObject *self, PyObject *other, int op)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001012{
1013 Py_ssize_t self_size, other_size;
1014 Py_buffer self_bytes, other_bytes;
1015 PyObject *res;
1016 Py_ssize_t minsize;
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001017 int cmp, rc;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001018
1019 /* Bytes can be compared to anything that supports the (binary)
1020 buffer API. Except that a comparison with Unicode is always an
1021 error, even if the comparison is for equality. */
Serhiy Storchakafa494fd2015-05-30 17:45:22 +03001022 rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type);
1023 if (!rc)
1024 rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type);
1025 if (rc < 0)
1026 return NULL;
1027 if (rc) {
Barry Warsaw9e9dcd62008-10-17 01:50:37 +00001028 if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001029 if (PyErr_WarnEx(PyExc_BytesWarning,
Georg Brandle5d68ac2008-06-04 11:30:26 +00001030 "Comparison between bytearray and string", 1))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001031 return NULL;
1032 }
1033
Brian Curtindfc80e32011-08-10 20:28:54 -05001034 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001035 }
1036
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001037 if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001038 PyErr_Clear();
Brian Curtindfc80e32011-08-10 20:28:54 -05001039 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001040 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001041 self_size = self_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001042
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001043 if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001044 PyErr_Clear();
Martin v. Löwis423be952008-08-13 15:53:07 +00001045 PyBuffer_Release(&self_bytes);
Brian Curtindfc80e32011-08-10 20:28:54 -05001046 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001047 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001048 other_size = other_bytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001049
1050 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
1051 /* Shortcut: if the lengths differ, the objects differ */
1052 cmp = (op == Py_NE);
1053 }
1054 else {
1055 minsize = self_size;
1056 if (other_size < minsize)
1057 minsize = other_size;
1058
1059 cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize);
1060 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
1061
1062 if (cmp == 0) {
1063 if (self_size < other_size)
1064 cmp = -1;
1065 else if (self_size > other_size)
1066 cmp = 1;
1067 }
1068
1069 switch (op) {
1070 case Py_LT: cmp = cmp < 0; break;
1071 case Py_LE: cmp = cmp <= 0; break;
1072 case Py_EQ: cmp = cmp == 0; break;
1073 case Py_NE: cmp = cmp != 0; break;
1074 case Py_GT: cmp = cmp > 0; break;
1075 case Py_GE: cmp = cmp >= 0; break;
1076 }
1077 }
1078
1079 res = cmp ? Py_True : Py_False;
Martin v. Löwis423be952008-08-13 15:53:07 +00001080 PyBuffer_Release(&self_bytes);
1081 PyBuffer_Release(&other_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001082 Py_INCREF(res);
1083 return res;
1084}
1085
1086static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001087bytearray_dealloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001088{
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001089 if (self->ob_exports > 0) {
1090 PyErr_SetString(PyExc_SystemError,
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001091 "deallocated bytearray object has exported buffers");
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001092 PyErr_Print();
1093 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001094 if (self->ob_bytes != 0) {
Antoine Pitrou39aba4f2011-11-12 21:15:28 +01001095 PyObject_Free(self->ob_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001096 }
1097 Py_TYPE(self)->tp_free((PyObject *)self);
1098}
1099
1100
1101/* -------------------------------------------------------------------- */
1102/* Methods */
1103
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001104#define FASTSEARCH fastsearch
1105#define STRINGLIB(F) stringlib_##F
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001106#define STRINGLIB_CHAR char
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001107#define STRINGLIB_SIZEOF_CHAR 1
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001108#define STRINGLIB_LEN PyByteArray_GET_SIZE
1109#define STRINGLIB_STR PyByteArray_AS_STRING
1110#define STRINGLIB_NEW PyByteArray_FromStringAndSize
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001111#define STRINGLIB_ISSPACE Py_ISSPACE
1112#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001113#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1114#define STRINGLIB_MUTABLE 1
1115
1116#include "stringlib/fastsearch.h"
1117#include "stringlib/count.h"
1118#include "stringlib/find.h"
Antoine Pitroucfc22b42012-10-16 21:07:23 +02001119#include "stringlib/join.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001120#include "stringlib/partition.h"
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001121#include "stringlib/split.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001122#include "stringlib/ctype.h"
1123#include "stringlib/transmogrify.h"
1124
1125
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001126static PyObject *
1127bytearray_find(PyByteArrayObject *self, PyObject *args)
1128{
1129 return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1130}
1131
1132static PyObject *
1133bytearray_count(PyByteArrayObject *self, PyObject *args)
1134{
1135 return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1136}
1137
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001138/*[clinic input]
1139bytearray.clear
1140
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001141Remove all items from the bytearray.
1142[clinic start generated code]*/
1143
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001144static PyObject *
1145bytearray_clear_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001146/*[clinic end generated code: output=85c2fe6aede0956c input=ed6edae9de447ac4]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001147{
1148 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1149 return NULL;
1150 Py_RETURN_NONE;
1151}
1152
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001153/*[clinic input]
1154bytearray.copy
1155
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001156Return a copy of B.
1157[clinic start generated code]*/
1158
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001159static PyObject *
1160bytearray_copy_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001161/*[clinic end generated code: output=68cfbcfed484c132 input=6597b0c01bccaa9e]*/
Eli Bendersky4db28d32011-03-03 18:21:02 +00001162{
1163 return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self),
1164 PyByteArray_GET_SIZE(self));
1165}
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001166
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001167static PyObject *
1168bytearray_index(PyByteArrayObject *self, PyObject *args)
1169{
1170 return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1171}
1172
1173static PyObject *
1174bytearray_rfind(PyByteArrayObject *self, PyObject *args)
1175{
1176 return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1177}
1178
1179static PyObject *
1180bytearray_rindex(PyByteArrayObject *self, PyObject *args)
1181{
1182 return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1183}
1184
1185static int
1186bytearray_contains(PyObject *self, PyObject *arg)
1187{
1188 return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg);
1189}
1190
1191static PyObject *
1192bytearray_startswith(PyByteArrayObject *self, PyObject *args)
1193{
1194 return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1195}
1196
1197static PyObject *
1198bytearray_endswith(PyByteArrayObject *self, PyObject *args)
1199{
1200 return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1201}
1202
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001203
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001204/*[clinic input]
1205bytearray.translate
1206
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001207 table: object
1208 Translation table, which must be a bytes object of length 256.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001209 /
Martin Panter1b6c6da2016-08-27 08:35:02 +00001210 delete as deletechars: object(c_default="NULL") = b''
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001211
1212Return a copy with each character mapped by the given translation table.
1213
Martin Panter1b6c6da2016-08-27 08:35:02 +00001214All characters occurring in the optional argument delete are removed.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001215The remaining characters are mapped through the given translation table.
1216[clinic start generated code]*/
1217
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001218static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001219bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
Martin Panter1b6c6da2016-08-27 08:35:02 +00001220 PyObject *deletechars)
1221/*[clinic end generated code: output=b6a8f01c2a74e446 input=cfff956d4d127a9b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001222{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001223 char *input, *output;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001224 const char *table_chars;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001225 Py_ssize_t i, c;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001226 PyObject *input_obj = (PyObject*)self;
1227 const char *output_start;
1228 Py_ssize_t inlen;
Georg Brandlccc47b62008-12-28 11:44:14 +00001229 PyObject *result = NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001230 int trans_table[256];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001231 Py_buffer vtable, vdel;
1232
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001233 if (table == Py_None) {
1234 table_chars = NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001235 table = NULL;
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001236 } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001237 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001238 } else {
1239 if (vtable.len != 256) {
1240 PyErr_SetString(PyExc_ValueError,
1241 "translation table must be 256 characters long");
Georg Brandl953152f2009-07-22 12:03:59 +00001242 PyBuffer_Release(&vtable);
1243 return NULL;
Georg Brandlccc47b62008-12-28 11:44:14 +00001244 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001245 table_chars = (const char*)vtable.buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001246 }
1247
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001248 if (deletechars != NULL) {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001249 if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) {
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001250 if (table != NULL)
Georg Brandl953152f2009-07-22 12:03:59 +00001251 PyBuffer_Release(&vtable);
1252 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001253 }
1254 }
1255 else {
1256 vdel.buf = NULL;
1257 vdel.len = 0;
1258 }
1259
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001260 inlen = PyByteArray_GET_SIZE(input_obj);
1261 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1262 if (result == NULL)
1263 goto done;
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03001264 output_start = output = PyByteArray_AS_STRING(result);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001265 input = PyByteArray_AS_STRING(input_obj);
1266
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001267 if (vdel.len == 0 && table_chars != NULL) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001268 /* If no deletions are required, use faster code */
1269 for (i = inlen; --i >= 0; ) {
1270 c = Py_CHARMASK(*input++);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001271 *output++ = table_chars[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001272 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001273 goto done;
1274 }
Georg Brandlccc47b62008-12-28 11:44:14 +00001275
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001276 if (table_chars == NULL) {
Georg Brandlccc47b62008-12-28 11:44:14 +00001277 for (i = 0; i < 256; i++)
1278 trans_table[i] = Py_CHARMASK(i);
1279 } else {
1280 for (i = 0; i < 256; i++)
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001281 trans_table[i] = Py_CHARMASK(table_chars[i]);
Georg Brandlccc47b62008-12-28 11:44:14 +00001282 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001283
1284 for (i = 0; i < vdel.len; i++)
1285 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1286
1287 for (i = inlen; --i >= 0; ) {
1288 c = Py_CHARMASK(*input++);
1289 if (trans_table[c] != -1)
Martin Panter1b6c6da2016-08-27 08:35:02 +00001290 *output++ = (char)trans_table[c];
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001291 }
1292 /* Fix the size of the resulting string */
1293 if (inlen > 0)
Christian Heimesc731bbe2013-07-21 02:04:35 +02001294 if (PyByteArray_Resize(result, output - output_start) < 0) {
1295 Py_CLEAR(result);
1296 goto done;
1297 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001298
1299done:
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001300 if (table != NULL)
Georg Brandlccc47b62008-12-28 11:44:14 +00001301 PyBuffer_Release(&vtable);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001302 if (deletechars != NULL)
Martin v. Löwis423be952008-08-13 15:53:07 +00001303 PyBuffer_Release(&vdel);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001304 return result;
1305}
1306
1307
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001308/*[clinic input]
1309
1310@staticmethod
1311bytearray.maketrans
1312
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001313 frm: Py_buffer
1314 to: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001315 /
1316
1317Return a translation table useable for the bytes or bytearray translate method.
1318
1319The returned table will be one where each byte in frm is mapped to the byte at
1320the same position in to.
1321
1322The bytes objects frm and to must be of the same length.
1323[clinic start generated code]*/
1324
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001325static PyObject *
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001326bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001327/*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001328{
1329 return _Py_bytes_maketrans(frm, to);
Georg Brandlabc38772009-04-12 15:51:51 +00001330}
1331
1332
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001333/*[clinic input]
1334bytearray.replace
1335
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001336 old: Py_buffer
1337 new: Py_buffer
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001338 count: Py_ssize_t = -1
1339 Maximum number of occurrences to replace.
1340 -1 (the default value) means replace all occurrences.
1341 /
1342
1343Return a copy with all occurrences of substring old replaced by new.
1344
1345If the optional argument count is given, only the first count occurrences are
1346replaced.
1347[clinic start generated code]*/
1348
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001349static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001350bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
1351 Py_buffer *new, Py_ssize_t count)
1352/*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001353{
Serhiy Storchakafb81d3c2016-05-05 09:26:07 +03001354 return stringlib_replace((PyObject *)self,
Serhiy Storchakae09132f2016-07-03 13:57:48 +03001355 (const char *)old->buf, old->len,
1356 (const char *)new->buf, new->len, count);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001357}
1358
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001359/*[clinic input]
1360bytearray.split
1361
1362 sep: object = None
1363 The delimiter according which to split the bytearray.
1364 None (the default value) means split on ASCII whitespace characters
1365 (space, tab, return, newline, formfeed, vertical tab).
1366 maxsplit: Py_ssize_t = -1
1367 Maximum number of splits to do.
1368 -1 (the default value) means no limit.
1369
1370Return a list of the sections in the bytearray, using sep as the delimiter.
1371[clinic start generated code]*/
1372
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001373static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001374bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
1375 Py_ssize_t maxsplit)
1376/*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001377{
1378 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001379 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001380 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001381 Py_buffer vsub;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001382
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001383 if (maxsplit < 0)
1384 maxsplit = PY_SSIZE_T_MAX;
1385
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001386 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001387 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001388
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001389 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001390 return NULL;
1391 sub = vsub.buf;
1392 n = vsub.len;
1393
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001394 list = stringlib_split(
1395 (PyObject*) self, s, len, sub, n, maxsplit
1396 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001397 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001398 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001399}
1400
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001401/*[clinic input]
1402bytearray.partition
1403
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001404 sep: object
1405 /
1406
1407Partition the bytearray into three parts using the given separator.
1408
1409This will search for the separator sep in the bytearray. If the separator is
1410found, returns a 3-tuple containing the part before the separator, the
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001411separator itself, and the part after it as new bytearray objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001412
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001413If the separator is not found, returns a 3-tuple containing the copy of the
1414original bytearray object and two empty bytearray objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001415[clinic start generated code]*/
1416
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001417static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001418bytearray_partition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001419/*[clinic end generated code: output=45d2525ddd35f957 input=8f644749ee4fc83a]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001420{
1421 PyObject *bytesep, *result;
1422
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001423 bytesep = _PyByteArray_FromBufferObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001424 if (! bytesep)
1425 return NULL;
1426
1427 result = stringlib_partition(
1428 (PyObject*) self,
1429 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1430 bytesep,
1431 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1432 );
1433
1434 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001435 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001436}
1437
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001438/*[clinic input]
1439bytearray.rpartition
1440
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001441 sep: object
1442 /
1443
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001444Partition the bytearray into three parts using the given separator.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001445
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001446This will search for the separator sep in the bytearray, starting at the end.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001447If the separator is found, returns a 3-tuple containing the part before the
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001448separator, the separator itself, and the part after it as new bytearray
1449objects.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001450
1451If the separator is not found, returns a 3-tuple containing two empty bytearray
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001452objects and the copy of the original bytearray object.
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001453[clinic start generated code]*/
1454
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001455static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001456bytearray_rpartition(PyByteArrayObject *self, PyObject *sep)
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001457/*[clinic end generated code: output=440de3c9426115e8 input=7e3df3e6cb8fa0ac]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001458{
1459 PyObject *bytesep, *result;
1460
Serhiy Storchakaa2314282017-10-29 02:11:54 +03001461 bytesep = _PyByteArray_FromBufferObject(sep);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001462 if (! bytesep)
1463 return NULL;
1464
1465 result = stringlib_rpartition(
1466 (PyObject*) self,
1467 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1468 bytesep,
1469 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1470 );
1471
1472 Py_DECREF(bytesep);
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001473 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001474}
1475
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001476/*[clinic input]
1477bytearray.rsplit = bytearray.split
1478
1479Return a list of the sections in the bytearray, using sep as the delimiter.
1480
1481Splitting is done starting at the end of the bytearray and working to the front.
1482[clinic start generated code]*/
1483
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001484static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001485bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
1486 Py_ssize_t maxsplit)
1487/*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001488{
1489 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001490 const char *s = PyByteArray_AS_STRING(self), *sub;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001491 PyObject *list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001492 Py_buffer vsub;
1493
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001494 if (maxsplit < 0)
1495 maxsplit = PY_SSIZE_T_MAX;
1496
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001497 if (sep == Py_None)
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001498 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001499
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001500 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001501 return NULL;
1502 sub = vsub.buf;
1503 n = vsub.len;
1504
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001505 list = stringlib_rsplit(
1506 (PyObject*) self, s, len, sub, n, maxsplit
1507 );
Martin v. Löwis423be952008-08-13 15:53:07 +00001508 PyBuffer_Release(&vsub);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001509 return list;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001510}
1511
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001512/*[clinic input]
1513bytearray.reverse
1514
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001515Reverse the order of the values in B in place.
1516[clinic start generated code]*/
1517
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001518static PyObject *
1519bytearray_reverse_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001520/*[clinic end generated code: output=9f7616f29ab309d3 input=543356319fc78557]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001521{
1522 char swap, *head, *tail;
1523 Py_ssize_t i, j, n = Py_SIZE(self);
1524
1525 j = n / 2;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001526 head = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001527 tail = head + n - 1;
1528 for (i = 0; i < j; i++) {
1529 swap = *head;
1530 *head++ = *tail;
1531 *tail-- = swap;
1532 }
1533
1534 Py_RETURN_NONE;
1535}
1536
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001537
1538/*[python input]
1539class bytesvalue_converter(CConverter):
1540 type = 'int'
1541 converter = '_getbytevalue'
1542[python start generated code]*/
1543/*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/
1544
1545
1546/*[clinic input]
1547bytearray.insert
1548
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001549 index: Py_ssize_t
1550 The index where the value is to be inserted.
1551 item: bytesvalue
1552 The item to be inserted.
1553 /
1554
1555Insert a single item into the bytearray before the given index.
1556[clinic start generated code]*/
1557
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001558static PyObject *
1559bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001560/*[clinic end generated code: output=76c775a70e7b07b7 input=b2b5d07e9de6c070]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001561{
1562 Py_ssize_t n = Py_SIZE(self);
1563 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001564
1565 if (n == PY_SSIZE_T_MAX) {
1566 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001567 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001568 return NULL;
1569 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001570 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1571 return NULL;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001572 buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001573
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001574 if (index < 0) {
1575 index += n;
1576 if (index < 0)
1577 index = 0;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001578 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001579 if (index > n)
1580 index = n;
1581 memmove(buf + index + 1, buf + index, n - index);
1582 buf[index] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001583
1584 Py_RETURN_NONE;
1585}
1586
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001587/*[clinic input]
1588bytearray.append
1589
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001590 item: bytesvalue
1591 The item to be appended.
1592 /
1593
1594Append a single item to the end of the bytearray.
1595[clinic start generated code]*/
1596
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001597static PyObject *
1598bytearray_append_impl(PyByteArrayObject *self, int item)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001599/*[clinic end generated code: output=a154e19ed1886cb6 input=20d6bec3d1340593]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001600{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001601 Py_ssize_t n = Py_SIZE(self);
1602
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001603 if (n == PY_SSIZE_T_MAX) {
1604 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001605 "cannot add more objects to bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001606 return NULL;
1607 }
1608 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1609 return NULL;
1610
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001611 PyByteArray_AS_STRING(self)[n] = item;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001612
1613 Py_RETURN_NONE;
1614}
1615
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001616/*[clinic input]
1617bytearray.extend
1618
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001619 iterable_of_ints: object
1620 The iterable of items to append.
1621 /
1622
1623Append all the items from the iterator or sequence to the end of the bytearray.
1624[clinic start generated code]*/
1625
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001626static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001627bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001628/*[clinic end generated code: output=98155dbe249170b1 input=c617b3a93249ba28]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001629{
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001630 PyObject *it, *item, *bytearray_obj;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001631 Py_ssize_t buf_size = 0, len = 0;
1632 int value;
1633 char *buf;
1634
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001635 /* bytearray_setslice code only accepts something supporting PEP 3118. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001636 if (PyObject_CheckBuffer(iterable_of_ints)) {
1637 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_ints) == -1)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001638 return NULL;
1639
1640 Py_RETURN_NONE;
1641 }
1642
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001643 it = PyObject_GetIter(iterable_of_ints);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001644 if (it == NULL)
1645 return NULL;
1646
Ezio Melotti42da6632011-03-15 05:18:48 +02001647 /* Try to determine the length of the argument. 32 is arbitrary. */
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001648 buf_size = PyObject_LengthHint(iterable_of_ints, 32);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001649 if (buf_size == -1) {
1650 Py_DECREF(it);
1651 return NULL;
1652 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001653
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001654 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001655 if (bytearray_obj == NULL) {
1656 Py_DECREF(it);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001657 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001658 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001659 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001660
1661 while ((item = PyIter_Next(it)) != NULL) {
1662 if (! _getbytevalue(item, &value)) {
1663 Py_DECREF(item);
1664 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001665 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001666 return NULL;
1667 }
1668 buf[len++] = value;
1669 Py_DECREF(item);
1670
1671 if (len >= buf_size) {
Martin Panter371731e2016-07-18 07:53:13 +00001672 Py_ssize_t addition;
1673 if (len == PY_SSIZE_T_MAX) {
1674 Py_DECREF(it);
1675 Py_DECREF(bytearray_obj);
1676 return PyErr_NoMemory();
1677 }
1678 addition = len >> 1;
1679 if (addition > PY_SSIZE_T_MAX - len - 1)
1680 buf_size = PY_SSIZE_T_MAX;
1681 else
1682 buf_size = len + addition + 1;
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001683 if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001684 Py_DECREF(it);
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001685 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001686 return NULL;
1687 }
1688 /* Recompute the `buf' pointer, since the resizing operation may
1689 have invalidated it. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001690 buf = PyByteArray_AS_STRING(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001691 }
1692 }
1693 Py_DECREF(it);
1694
1695 /* Resize down to exact size. */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001696 if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) {
1697 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001698 return NULL;
1699 }
1700
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001701 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) {
1702 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001703 return NULL;
Antoine Pitrou58bb82e2012-04-01 16:05:46 +02001704 }
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001705 Py_DECREF(bytearray_obj);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001706
1707 Py_RETURN_NONE;
1708}
1709
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001710/*[clinic input]
1711bytearray.pop
1712
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001713 index: Py_ssize_t = -1
1714 The index from where to remove the item.
1715 -1 (the default value) means remove the last item.
1716 /
1717
1718Remove and return a single item from B.
1719
1720If no index argument is given, will pop the last item.
1721[clinic start generated code]*/
1722
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001723static PyObject *
1724bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001725/*[clinic end generated code: output=e0ccd401f8021da8 input=3591df2d06c0d237]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001726{
1727 int value;
1728 Py_ssize_t n = Py_SIZE(self);
1729 char *buf;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001730
1731 if (n == 0) {
Eli Bendersky1bc4f192011-03-04 04:55:25 +00001732 PyErr_SetString(PyExc_IndexError,
1733 "pop from empty bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001734 return NULL;
1735 }
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001736 if (index < 0)
1737 index += Py_SIZE(self);
1738 if (index < 0 || index >= Py_SIZE(self)) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001739 PyErr_SetString(PyExc_IndexError, "pop index out of range");
1740 return NULL;
1741 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001742 if (!_canresize(self))
1743 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001744
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001745 buf = PyByteArray_AS_STRING(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001746 value = buf[index];
1747 memmove(buf + index, buf + index + 1, n - index);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001748 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1749 return NULL;
1750
Mark Dickinson54a3db92009-09-06 10:19:23 +00001751 return PyLong_FromLong((unsigned char)value);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001752}
1753
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001754/*[clinic input]
1755bytearray.remove
1756
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001757 value: bytesvalue
1758 The value to remove.
1759 /
1760
1761Remove the first occurrence of a value in the bytearray.
1762[clinic start generated code]*/
1763
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001764static PyObject *
1765bytearray_remove_impl(PyByteArrayObject *self, int value)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001766/*[clinic end generated code: output=d659e37866709c13 input=121831240cd51ddf]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001767{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001768 Py_ssize_t where, n = Py_SIZE(self);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001769 char *buf = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001770
Serhiy Storchaka4b234942016-05-16 22:24:03 +03001771 where = stringlib_find_char(buf, n, value);
1772 if (where < 0) {
Mark Dickinson2b6705f2009-09-06 10:34:47 +00001773 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001774 return NULL;
1775 }
Antoine Pitrou5504e892008-12-06 21:27:53 +00001776 if (!_canresize(self))
1777 return NULL;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001778
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001779 memmove(buf + where, buf + where + 1, n - where);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001780 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1781 return NULL;
1782
1783 Py_RETURN_NONE;
1784}
1785
1786/* XXX These two helpers could be optimized if argsize == 1 */
1787
1788static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001789lstrip_helper(const char *myptr, Py_ssize_t mysize,
1790 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001791{
1792 Py_ssize_t i = 0;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001793 while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001794 i++;
1795 return i;
1796}
1797
1798static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001799rstrip_helper(const char *myptr, Py_ssize_t mysize,
1800 const void *argptr, Py_ssize_t argsize)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001801{
1802 Py_ssize_t i = mysize - 1;
Antoine Pitrou5b720752013-10-05 21:24:10 +02001803 while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001804 i--;
1805 return i + 1;
1806}
1807
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001808/*[clinic input]
1809bytearray.strip
1810
1811 bytes: object = None
1812 /
1813
1814Strip leading and trailing bytes contained in the argument.
1815
1816If the argument is omitted or None, strip leading and trailing ASCII whitespace.
1817[clinic start generated code]*/
1818
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001819static PyObject *
1820bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001821/*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001822{
1823 Py_ssize_t left, right, mysize, byteslen;
1824 char *myptr, *bytesptr;
1825 Py_buffer vbytes;
1826
1827 if (bytes == Py_None) {
1828 bytesptr = "\t\n\r\f\v ";
1829 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001830 }
1831 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001832 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001833 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001834 bytesptr = (char *) vbytes.buf;
1835 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001836 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001837 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001838 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001839 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001840 if (left == mysize)
1841 right = left;
1842 else
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001843 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1844 if (bytes != Py_None)
1845 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001846 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001847}
1848
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001849/*[clinic input]
1850bytearray.lstrip
1851
1852 bytes: object = None
1853 /
1854
1855Strip leading bytes contained in the argument.
1856
1857If the argument is omitted or None, strip leading ASCII whitespace.
1858[clinic start generated code]*/
1859
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001860static PyObject *
1861bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001862/*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001863{
1864 Py_ssize_t left, right, mysize, byteslen;
1865 char *myptr, *bytesptr;
1866 Py_buffer vbytes;
1867
1868 if (bytes == Py_None) {
1869 bytesptr = "\t\n\r\f\v ";
1870 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001871 }
1872 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001873 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001874 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001875 bytesptr = (char *) vbytes.buf;
1876 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001877 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001878 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001879 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001880 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001881 right = mysize;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001882 if (bytes != Py_None)
1883 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001884 return PyByteArray_FromStringAndSize(myptr + left, right - left);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001885}
1886
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001887/*[clinic input]
1888bytearray.rstrip
1889
1890 bytes: object = None
1891 /
1892
1893Strip trailing bytes contained in the argument.
1894
1895If the argument is omitted or None, strip trailing ASCII whitespace.
1896[clinic start generated code]*/
1897
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001898static PyObject *
1899bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001900/*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001901{
1902 Py_ssize_t right, mysize, byteslen;
1903 char *myptr, *bytesptr;
1904 Py_buffer vbytes;
1905
1906 if (bytes == Py_None) {
1907 bytesptr = "\t\n\r\f\v ";
1908 byteslen = 6;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001909 }
1910 else {
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +02001911 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001912 return NULL;
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001913 bytesptr = (char *) vbytes.buf;
1914 byteslen = vbytes.len;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001915 }
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001916 myptr = PyByteArray_AS_STRING(self);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001917 mysize = Py_SIZE(self);
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001918 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1919 if (bytes != Py_None)
1920 PyBuffer_Release(&vbytes);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02001921 return PyByteArray_FromStringAndSize(myptr, right);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001922}
1923
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001924/*[clinic input]
1925bytearray.decode
1926
1927 encoding: str(c_default="NULL") = 'utf-8'
1928 The encoding with which to decode the bytearray.
1929 errors: str(c_default="NULL") = 'strict'
1930 The error handling scheme to use for the handling of decoding errors.
1931 The default is 'strict' meaning that decoding errors raise a
1932 UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
1933 as well as any other name registered with codecs.register_error that
1934 can handle UnicodeDecodeErrors.
1935
1936Decode the bytearray using the codec registered for encoding.
1937[clinic start generated code]*/
1938
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001939static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001940bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
1941 const char *errors)
1942/*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001943{
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001944 if (encoding == NULL)
1945 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis0efea322014-07-27 17:29:17 +02001946 return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001947}
1948
1949PyDoc_STRVAR(alloc_doc,
1950"B.__alloc__() -> int\n\
1951\n\
Georg Brandl17cb8a82008-05-30 08:20:09 +00001952Return the number of bytes actually allocated.");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001953
1954static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00001955bytearray_alloc(PyByteArrayObject *self)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001956{
1957 return PyLong_FromSsize_t(self->ob_alloc);
1958}
1959
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001960/*[clinic input]
1961bytearray.join
1962
1963 iterable_of_bytes: object
1964 /
1965
1966Concatenate any number of bytes/bytearray objects.
1967
1968The bytearray whose method is called is inserted in between each pair.
1969
1970The result is returned as a new bytearray object.
1971[clinic start generated code]*/
1972
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001973static PyObject *
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001974bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001975/*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001976{
Martin v. Löwis0efea322014-07-27 17:29:17 +02001977 return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00001978}
1979
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001980/*[clinic input]
1981bytearray.splitlines
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001982
Serhiy Storchaka202fda52017-03-12 10:10:47 +02001983 keepends: bool(accept={int}) = False
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001984
1985Return a list of the lines in the bytearray, breaking at line boundaries.
1986
1987Line breaks are not included in the resulting list unless keepends is given and
1988true.
1989[clinic start generated code]*/
1990
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001991static PyObject *
1992bytearray_splitlines_impl(PyByteArrayObject *self, int keepends)
Serhiy Storchaka202fda52017-03-12 10:10:47 +02001993/*[clinic end generated code: output=4223c94b895f6ad9 input=99a27ad959b9cf6b]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02001994{
Antoine Pitrouf2c54842010-01-13 08:07:53 +00001995 return stringlib_splitlines(
1996 (PyObject*) self, PyByteArray_AS_STRING(self),
1997 PyByteArray_GET_SIZE(self), keepends
1998 );
1999}
2000
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002001/*[clinic input]
2002@classmethod
2003bytearray.fromhex
2004
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002005 string: unicode
2006 /
2007
2008Create a bytearray object from a string of hexadecimal numbers.
2009
2010Spaces between two numbers are accepted.
2011Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')
2012[clinic start generated code]*/
2013
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002014static PyObject *
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002015bytearray_fromhex_impl(PyTypeObject *type, PyObject *string)
2016/*[clinic end generated code: output=8f0f0b6d30fb3ba0 input=f033a16d1fb21f48]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002017{
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002018 PyObject *result = _PyBytes_FromHex(string, type == &PyByteArray_Type);
2019 if (type != &PyByteArray_Type && result != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002020 Py_SETREF(result, PyObject_CallFunctionObjArgs((PyObject *)type,
2021 result, NULL));
Serhiy Storchaka0855e702016-07-01 17:22:31 +03002022 }
2023 return result;
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002024}
2025
Gregory P. Smith8cb65692015-04-25 23:22:26 +00002026PyDoc_STRVAR(hex__doc__,
2027"B.hex() -> string\n\
2028\n\
2029Create a string of hexadecimal numbers from a bytearray object.\n\
2030Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'.");
2031
2032static PyObject *
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002033bytearray_hex(PyBytesObject *self)
2034{
2035 char* argbuf = PyByteArray_AS_STRING(self);
2036 Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
2037 return _Py_strhex(argbuf, arglen);
2038}
2039
2040static PyObject *
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002041_common_reduce(PyByteArrayObject *self, int proto)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002042{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002043 PyObject *dict;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002044 _Py_IDENTIFIER(__dict__);
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002045 char *buf;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002046
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002047 dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002048 if (dict == NULL) {
2049 PyErr_Clear();
2050 dict = Py_None;
2051 Py_INCREF(dict);
2052 }
2053
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002054 buf = PyByteArray_AS_STRING(self);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002055 if (proto < 3) {
2056 /* use str based reduction for backwards compatibility with Python 2.x */
2057 PyObject *latin1;
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002058 if (Py_SIZE(self))
2059 latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002060 else
2061 latin1 = PyUnicode_FromString("");
2062 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
2063 }
2064 else {
2065 /* use more efficient byte based reduction */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002066 if (Py_SIZE(self)) {
2067 return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict);
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002068 }
2069 else {
2070 return Py_BuildValue("(O()N)", Py_TYPE(self), dict);
2071 }
2072 }
2073}
2074
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002075/*[clinic input]
2076bytearray.__reduce__ as bytearray_reduce
2077
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002078Return state information for pickling.
2079[clinic start generated code]*/
2080
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002081static PyObject *
2082bytearray_reduce_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002083/*[clinic end generated code: output=52bf304086464cab input=44b5737ada62dd3f]*/
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002084{
2085 return _common_reduce(self, 2);
2086}
2087
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002088/*[clinic input]
2089bytearray.__reduce_ex__ as bytearray_reduce_ex
2090
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002091 proto: int = 0
2092 /
2093
2094Return state information for pickling.
2095[clinic start generated code]*/
2096
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002097static PyObject *
2098bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002099/*[clinic end generated code: output=52eac33377197520 input=f129bc1a1aa151ee]*/
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002100{
Antoine Pitroub0e1f8b2011-12-05 20:40:08 +01002101 return _common_reduce(self, proto);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002102}
2103
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002104/*[clinic input]
2105bytearray.__sizeof__ as bytearray_sizeof
2106
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002107Returns the size of the bytearray object in memory, in bytes.
2108[clinic start generated code]*/
2109
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002110static PyObject *
2111bytearray_sizeof_impl(PyByteArrayObject *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03002112/*[clinic end generated code: output=738abdd17951c427 input=e27320fd98a4bc5a]*/
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002113{
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002114 Py_ssize_t res;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002115
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002116 res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char);
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00002117 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +00002118}
2119
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002120static PySequenceMethods bytearray_as_sequence = {
2121 (lenfunc)bytearray_length, /* sq_length */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002122 (binaryfunc)PyByteArray_Concat, /* sq_concat */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002123 (ssizeargfunc)bytearray_repeat, /* sq_repeat */
2124 (ssizeargfunc)bytearray_getitem, /* sq_item */
2125 0, /* sq_slice */
2126 (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */
2127 0, /* sq_ass_slice */
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002128 (objobjproc)bytearray_contains, /* sq_contains */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002129 (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */
2130 (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002131};
2132
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002133static PyMappingMethods bytearray_as_mapping = {
2134 (lenfunc)bytearray_length,
2135 (binaryfunc)bytearray_subscript,
2136 (objobjargproc)bytearray_ass_subscript,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002137};
2138
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002139static PyBufferProcs bytearray_as_buffer = {
2140 (getbufferproc)bytearray_getbuffer,
2141 (releasebufferproc)bytearray_releasebuffer,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002142};
2143
2144static PyMethodDef
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002145bytearray_methods[] = {
2146 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002147 BYTEARRAY_REDUCE_METHODDEF
2148 BYTEARRAY_REDUCE_EX_METHODDEF
2149 BYTEARRAY_SIZEOF_METHODDEF
2150 BYTEARRAY_APPEND_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002151 {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS,
2152 _Py_capitalize__doc__},
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002153 {"center", (PyCFunction)stringlib_center, METH_VARARGS, _Py_center__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002154 BYTEARRAY_CLEAR_METHODDEF
2155 BYTEARRAY_COPY_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002156 {"count", (PyCFunction)bytearray_count, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002157 _Py_count__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002158 BYTEARRAY_DECODE_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002159 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002160 _Py_endswith__doc__},
Ezio Melotti745d54d2013-11-16 19:10:57 +02002161 {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002162 _Py_expandtabs__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002163 BYTEARRAY_EXTEND_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002164 {"find", (PyCFunction)bytearray_find, METH_VARARGS,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002165 _Py_find__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002166 BYTEARRAY_FROMHEX_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002167 {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__},
2168 {"index", (PyCFunction)bytearray_index, METH_VARARGS, _Py_index__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002169 BYTEARRAY_INSERT_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002170 {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS,
2171 _Py_isalnum__doc__},
2172 {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS,
2173 _Py_isalpha__doc__},
2174 {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS,
2175 _Py_isdigit__doc__},
2176 {"islower", (PyCFunction)stringlib_islower, METH_NOARGS,
2177 _Py_islower__doc__},
2178 {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS,
2179 _Py_isspace__doc__},
2180 {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS,
2181 _Py_istitle__doc__},
2182 {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS,
2183 _Py_isupper__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002184 BYTEARRAY_JOIN_METHODDEF
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002185 {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, _Py_ljust__doc__},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002186 {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002187 BYTEARRAY_LSTRIP_METHODDEF
2188 BYTEARRAY_MAKETRANS_METHODDEF
2189 BYTEARRAY_PARTITION_METHODDEF
2190 BYTEARRAY_POP_METHODDEF
2191 BYTEARRAY_REMOVE_METHODDEF
2192 BYTEARRAY_REPLACE_METHODDEF
2193 BYTEARRAY_REVERSE_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002194 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__},
2195 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__},
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002196 {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, _Py_rjust__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002197 BYTEARRAY_RPARTITION_METHODDEF
2198 BYTEARRAY_RSPLIT_METHODDEF
2199 BYTEARRAY_RSTRIP_METHODDEF
2200 BYTEARRAY_SPLIT_METHODDEF
2201 BYTEARRAY_SPLITLINES_METHODDEF
Serhiy Storchakae09132f2016-07-03 13:57:48 +03002202 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002203 _Py_startswith__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002204 BYTEARRAY_STRIP_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002205 {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS,
2206 _Py_swapcase__doc__},
2207 {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__},
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002208 BYTEARRAY_TRANSLATE_METHODDEF
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002209 {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__},
Serhiy Storchakadd40fc32016-05-04 22:23:26 +03002210 {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, _Py_zfill__doc__},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002211 {NULL}
2212};
2213
Ethan Furmanb95b5612015-01-23 20:05:18 -08002214static PyObject *
2215bytearray_mod(PyObject *v, PyObject *w)
2216{
2217 if (!PyByteArray_Check(v))
2218 Py_RETURN_NOTIMPLEMENTED;
Berker Peksag43de36d2016-04-16 01:20:47 +03002219 return _PyBytes_FormatEx(PyByteArray_AS_STRING(v), PyByteArray_GET_SIZE(v), w, 1);
Ethan Furmanb95b5612015-01-23 20:05:18 -08002220}
2221
2222static PyNumberMethods bytearray_as_number = {
2223 0, /*nb_add*/
2224 0, /*nb_subtract*/
2225 0, /*nb_multiply*/
2226 bytearray_mod, /*nb_remainder*/
2227};
2228
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002229PyDoc_STRVAR(bytearray_doc,
Georg Brandl17cb8a82008-05-30 08:20:09 +00002230"bytearray(iterable_of_ints) -> bytearray\n\
2231bytearray(string, encoding[, errors]) -> bytearray\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002232bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\
2233bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\
2234bytearray() -> empty bytes array\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002235\n\
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03002236Construct a mutable bytearray object from:\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002237 - an iterable yielding integers in range(256)\n\
2238 - a text string encoded using the specified encoding\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002239 - a bytes or a buffer object\n\
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002240 - any object implementing the buffer API.\n\
Victor Stinnerbb2e9c42011-12-17 23:18:07 +01002241 - an integer");
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002242
2243
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002244static PyObject *bytearray_iter(PyObject *seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002245
2246PyTypeObject PyByteArray_Type = {
2247 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2248 "bytearray",
2249 sizeof(PyByteArrayObject),
2250 0,
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002251 (destructor)bytearray_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002252 0, /* tp_print */
2253 0, /* tp_getattr */
2254 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002255 0, /* tp_reserved */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002256 (reprfunc)bytearray_repr, /* tp_repr */
Ethan Furmanb95b5612015-01-23 20:05:18 -08002257 &bytearray_as_number, /* tp_as_number */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002258 &bytearray_as_sequence, /* tp_as_sequence */
2259 &bytearray_as_mapping, /* tp_as_mapping */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002260 0, /* tp_hash */
2261 0, /* tp_call */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002262 bytearray_str, /* tp_str */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002263 PyObject_GenericGetAttr, /* tp_getattro */
2264 0, /* tp_setattro */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002265 &bytearray_as_buffer, /* tp_as_buffer */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002266 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002267 bytearray_doc, /* tp_doc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002268 0, /* tp_traverse */
2269 0, /* tp_clear */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002270 (richcmpfunc)bytearray_richcompare, /* tp_richcompare */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002271 0, /* tp_weaklistoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002272 bytearray_iter, /* tp_iter */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002273 0, /* tp_iternext */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002274 bytearray_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002275 0, /* tp_members */
2276 0, /* tp_getset */
2277 0, /* tp_base */
2278 0, /* tp_dict */
2279 0, /* tp_descr_get */
2280 0, /* tp_descr_set */
2281 0, /* tp_dictoffset */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002282 (initproc)bytearray_init, /* tp_init */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002283 PyType_GenericAlloc, /* tp_alloc */
2284 PyType_GenericNew, /* tp_new */
2285 PyObject_Del, /* tp_free */
2286};
2287
2288/*********************** Bytes Iterator ****************************/
2289
2290typedef struct {
2291 PyObject_HEAD
2292 Py_ssize_t it_index;
2293 PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
2294} bytesiterobject;
2295
2296static void
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002297bytearrayiter_dealloc(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002298{
2299 _PyObject_GC_UNTRACK(it);
2300 Py_XDECREF(it->it_seq);
2301 PyObject_GC_Del(it);
2302}
2303
2304static int
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002305bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002306{
2307 Py_VISIT(it->it_seq);
2308 return 0;
2309}
2310
2311static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002312bytearrayiter_next(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002313{
2314 PyByteArrayObject *seq;
2315 PyObject *item;
2316
2317 assert(it != NULL);
2318 seq = it->it_seq;
2319 if (seq == NULL)
2320 return NULL;
2321 assert(PyByteArray_Check(seq));
2322
2323 if (it->it_index < PyByteArray_GET_SIZE(seq)) {
2324 item = PyLong_FromLong(
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +02002325 (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002326 if (item != NULL)
2327 ++it->it_index;
2328 return item;
2329 }
2330
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002331 it->it_seq = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03002332 Py_DECREF(seq);
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002333 return NULL;
2334}
2335
2336static PyObject *
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002337bytearrayiter_length_hint(bytesiterobject *it)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002338{
2339 Py_ssize_t len = 0;
Serhiy Storchakaaf658722016-07-03 14:41:36 +03002340 if (it->it_seq) {
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002341 len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
Serhiy Storchakaaf658722016-07-03 14:41:36 +03002342 if (len < 0) {
2343 len = 0;
2344 }
2345 }
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002346 return PyLong_FromSsize_t(len);
2347}
2348
2349PyDoc_STRVAR(length_hint_doc,
2350 "Private method returning an estimate of len(list(it)).");
2351
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002352static PyObject *
2353bytearrayiter_reduce(bytesiterobject *it)
2354{
2355 if (it->it_seq != NULL) {
Antoine Pitroua7013882012-04-05 00:04:20 +02002356 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002357 it->it_seq, it->it_index);
2358 } else {
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02002359 return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002360 }
2361}
2362
2363static PyObject *
2364bytearrayiter_setstate(bytesiterobject *it, PyObject *state)
2365{
2366 Py_ssize_t index = PyLong_AsSsize_t(state);
2367 if (index == -1 && PyErr_Occurred())
2368 return NULL;
Kristján Valur Jónsson25dded02014-03-05 13:47:57 +00002369 if (it->it_seq != NULL) {
2370 if (index < 0)
2371 index = 0;
2372 else if (index > PyByteArray_GET_SIZE(it->it_seq))
2373 index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */
2374 it->it_index = index;
2375 }
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002376 Py_RETURN_NONE;
2377}
2378
2379PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
2380
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002381static PyMethodDef bytearrayiter_methods[] = {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002382 {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002383 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002384 {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
Martin v. Löwis7252a6e2014-07-27 16:25:09 +02002385 bytearray_reduce__doc__},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002386 {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
2387 setstate_doc},
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002388 {NULL, NULL} /* sentinel */
2389};
2390
2391PyTypeObject PyByteArrayIter_Type = {
2392 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2393 "bytearray_iterator", /* tp_name */
2394 sizeof(bytesiterobject), /* tp_basicsize */
2395 0, /* tp_itemsize */
2396 /* methods */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002397 (destructor)bytearrayiter_dealloc, /* tp_dealloc */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002398 0, /* tp_print */
2399 0, /* tp_getattr */
2400 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002401 0, /* tp_reserved */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002402 0, /* tp_repr */
2403 0, /* tp_as_number */
2404 0, /* tp_as_sequence */
2405 0, /* tp_as_mapping */
2406 0, /* tp_hash */
2407 0, /* tp_call */
2408 0, /* tp_str */
2409 PyObject_GenericGetAttr, /* tp_getattro */
2410 0, /* tp_setattro */
2411 0, /* tp_as_buffer */
2412 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
2413 0, /* tp_doc */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002414 (traverseproc)bytearrayiter_traverse, /* tp_traverse */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002415 0, /* tp_clear */
2416 0, /* tp_richcompare */
2417 0, /* tp_weaklistoffset */
2418 PyObject_SelfIter, /* tp_iter */
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002419 (iternextfunc)bytearrayiter_next, /* tp_iternext */
2420 bytearrayiter_methods, /* tp_methods */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002421 0,
2422};
2423
2424static PyObject *
Benjamin Peterson153c70f2009-04-18 15:42:12 +00002425bytearray_iter(PyObject *seq)
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002426{
2427 bytesiterobject *it;
2428
2429 if (!PyByteArray_Check(seq)) {
2430 PyErr_BadInternalCall();
2431 return NULL;
2432 }
2433 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
2434 if (it == NULL)
2435 return NULL;
2436 it->it_index = 0;
2437 Py_INCREF(seq);
2438 it->it_seq = (PyByteArrayObject *)seq;
2439 _PyObject_GC_TRACK(it);
2440 return (PyObject *)it;
2441}