Bug Summary

File:Objects/bytearrayobject.c
Location:line 2464, column 65
Description:The right operand to '-' is always 0

Annotated Source Code

1/* PyByteArray (bytearray) implementation */
2
3#define PY_SSIZE_T_CLEAN
4#include "Python.h"
5#include "structmember.h"
6#include "bytes_methods.h"
7
8char _PyByteArray_empty_string[] = "";
9
10void
11PyByteArray_Fini(void)
12{
13}
14
15int
16PyByteArray_Init(void)
17{
18 return 1;
19}
20
21/* end nullbytes support */
22
23/* Helpers */
24
25static int
26_getbytevalue(PyObject* arg, int *value)
27{
28 long face_value;
29
30 if (PyLong_Check(arg)((((((PyObject*)(arg))->ob_type))->tp_flags & ((1L<<
24))) != 0)
) {
31 face_value = PyLong_AsLong(arg);
32 } else {
33 PyObject *index = PyNumber_Index(arg);
34 if (index == NULL((void*)0)) {
35 PyErr_Format(PyExc_TypeError, "an integer is required");
36 *value = -1;
37 return 0;
38 }
39 face_value = PyLong_AsLong(index);
40 Py_DECREF(index)do { if (_Py_RefTotal-- , --((PyObject*)(index))->ob_refcnt
!= 0) { if (((PyObject*)index)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/bytearrayobject.c", 40, (PyObject *)(index)); } else
_Py_Dealloc((PyObject *)(index)); } while (0)
;
41 }
42
43 if (face_value < 0 || face_value >= 256) {
44 /* this includes the OverflowError in case the long is too large */
45 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
46 *value = -1;
47 return 0;
48 }
49
50 *value = face_value;
51 return 1;
52}
53
54static int
55bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
56{
57 int ret;
58 void *ptr;
59 if (view == NULL((void*)0)) {
60 obj->ob_exports++;
61 return 0;
62 }
63 ptr = (void *) PyByteArray_AS_STRING(obj)((__builtin_expect(!(((((PyObject*)(obj))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(obj))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 63, "PyByteArray_Check(obj)") :
(void)0), (((PyVarObject*)(obj))->ob_size) ? ((PyByteArrayObject
*)(obj))->ob_bytes : _PyByteArray_empty_string)
;
64 ret = PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj)(((PyVarObject*)(obj))->ob_size), 0, flags);
65 if (ret >= 0) {
66 obj->ob_exports++;
67 }
68 return ret;
69}
70
71static void
72bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
73{
74 obj->ob_exports--;
75}
76
77static Py_ssize_t
78_getbuffer(PyObject *obj, Py_buffer *view)
79{
80 PyBufferProcs *buffer = Py_TYPE(obj)(((PyObject*)(obj))->ob_type)->tp_as_buffer;
81
82 if (buffer == NULL((void*)0) || buffer->bf_getbuffer == NULL((void*)0))
83 {
84 PyErr_Format(PyExc_TypeError,
85 "Type %.100s doesn't support the buffer API",
86 Py_TYPE(obj)(((PyObject*)(obj))->ob_type)->tp_name);
87 return -1;
88 }
89
90 if (buffer->bf_getbuffer(obj, view, PyBUF_SIMPLE0) < 0)
91 return -1;
92 return view->len;
93}
94
95static int
96_canresize(PyByteArrayObject *self)
97{
98 if (self->ob_exports > 0) {
99 PyErr_SetString(PyExc_BufferError,
100 "Existing exports of data: object cannot be re-sized");
101 return 0;
102 }
103 return 1;
104}
105
106/* Direct API functions */
107
108PyObject *
109PyByteArray_FromObject(PyObject *input)
110{
111 return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type,
112 input, NULL((void*)0));
113}
114
115PyObject *
116PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
117{
118 PyByteArrayObject *new;
119 Py_ssize_t alloc;
120
121 if (size < 0) {
122 PyErr_SetString(PyExc_SystemError,
123 "Negative size passed to PyByteArray_FromStringAndSize");
124 return NULL((void*)0);
125 }
126
127 /* Prevent buffer overflow when setting alloc to size+1. */
128 if (size == PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1))) {
129 return PyErr_NoMemory();
130 }
131
132 new = PyObject_New(PyByteArrayObject, &PyByteArray_Type)( (PyByteArrayObject *) _PyObject_New(&PyByteArray_Type) );
133 if (new == NULL((void*)0))
134 return NULL((void*)0);
135
136 if (size == 0) {
137 new->ob_bytes = NULL((void*)0);
138 alloc = 0;
139 }
140 else {
141 alloc = size + 1;
142 new->ob_bytes = PyMem_Malloc(alloc);
143 if (new->ob_bytes == NULL((void*)0)) {
144 Py_DECREF(new)do { if (_Py_RefTotal-- , --((PyObject*)(new))->ob_refcnt !=
0) { if (((PyObject*)new)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/bytearrayobject.c", 144, (PyObject *)(new)); } else
_Py_Dealloc((PyObject *)(new)); } while (0)
;
145 return PyErr_NoMemory();
146 }
147 if (bytes != NULL((void*)0) && size > 0)
148 memcpy(new->ob_bytes, bytes, size)((__builtin_object_size (new->ob_bytes, 0) != (size_t) -1)
? __builtin___memcpy_chk (new->ob_bytes, bytes, size, __builtin_object_size
(new->ob_bytes, 0)) : __inline_memcpy_chk (new->ob_bytes
, bytes, size))
;
149 new->ob_bytes[size] = '\0'; /* Trailing null byte */
150 }
151 Py_SIZE(new)(((PyVarObject*)(new))->ob_size) = size;
152 new->ob_alloc = alloc;
153 new->ob_exports = 0;
154
155 return (PyObject *)new;
156}
157
158Py_ssize_t
159PyByteArray_Size(PyObject *self)
160{
161 assert(self != NULL)(__builtin_expect(!(self != ((void*)0)), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 161, "self != NULL") : (void)0
)
;
162 assert(PyByteArray_Check(self))(__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 162, "PyByteArray_Check(self)"
) : (void)0)
;
163
164 return PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 164, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
;
165}
166
167char *
168PyByteArray_AsString(PyObject *self)
169{
170 assert(self != NULL)(__builtin_expect(!(self != ((void*)0)), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 170, "self != NULL") : (void)0
)
;
171 assert(PyByteArray_Check(self))(__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 171, "PyByteArray_Check(self)"
) : (void)0)
;
172
173 return PyByteArray_AS_STRING(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 173, "PyByteArray_Check(self)"
) : (void)0), (((PyVarObject*)(self))->ob_size) ? ((PyByteArrayObject
*)(self))->ob_bytes : _PyByteArray_empty_string)
;
174}
175
176int
177PyByteArray_Resize(PyObject *self, Py_ssize_t size)
178{
179 void *sval;
180 Py_ssize_t alloc = ((PyByteArrayObject *)self)->ob_alloc;
181
182 assert(self != NULL)(__builtin_expect(!(self != ((void*)0)), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 182, "self != NULL") : (void)0
)
;
183 assert(PyByteArray_Check(self))(__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 183, "PyByteArray_Check(self)"
) : (void)0)
;
184 assert(size >= 0)(__builtin_expect(!(size >= 0), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 184, "size >= 0") : (void)0
)
;
185
186 if (size == Py_SIZE(self)(((PyVarObject*)(self))->ob_size)) {
187 return 0;
188 }
189 if (!_canresize((PyByteArrayObject *)self)) {
190 return -1;
191 }
192
193 if (size < alloc / 2) {
194 /* Major downsize; resize down to exact size */
195 alloc = size + 1;
196 }
197 else if (size < alloc) {
198 /* Within allocated size; quick exit */
199 Py_SIZE(self)(((PyVarObject*)(self))->ob_size) = size;
200 ((PyByteArrayObject *)self)->ob_bytes[size] = '\0'; /* Trailing null */
201 return 0;
202 }
203 else if (size <= alloc * 1.125) {
204 /* Moderate upsize; overallocate similar to list_resize() */
205 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
206 }
207 else {
208 /* Major upsize; resize up to exact size */
209 alloc = size + 1;
210 }
211
212 sval = PyMem_Realloc(((PyByteArrayObject *)self)->ob_bytes, alloc);
213 if (sval == NULL((void*)0)) {
214 PyErr_NoMemory();
215 return -1;
216 }
217
218 ((PyByteArrayObject *)self)->ob_bytes = sval;
219 Py_SIZE(self)(((PyVarObject*)(self))->ob_size) = size;
220 ((PyByteArrayObject *)self)->ob_alloc = alloc;
221 ((PyByteArrayObject *)self)->ob_bytes[size] = '\0'; /* Trailing null byte */
222
223 return 0;
224}
225
226PyObject *
227PyByteArray_Concat(PyObject *a, PyObject *b)
228{
229 Py_ssize_t size;
230 Py_buffer va, vb;
231 PyByteArrayObject *result = NULL((void*)0);
232
233 va.len = -1;
234 vb.len = -1;
235 if (_getbuffer(a, &va) < 0 ||
236 _getbuffer(b, &vb) < 0) {
237 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
238 Py_TYPE(a)(((PyObject*)(a))->ob_type)->tp_name, Py_TYPE(b)(((PyObject*)(b))->ob_type)->tp_name);
239 goto done;
240 }
241
242 size = va.len + vb.len;
243 if (size < 0) {
244 PyErr_NoMemory();
245 goto done;
246 }
247
248 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL((void*)0), size);
249 if (result != NULL((void*)0)) {
250 memcpy(result->ob_bytes, va.buf, va.len)((__builtin_object_size (result->ob_bytes, 0) != (size_t) -
1) ? __builtin___memcpy_chk (result->ob_bytes, va.buf, va.
len, __builtin_object_size (result->ob_bytes, 0)) : __inline_memcpy_chk
(result->ob_bytes, va.buf, va.len))
;
251 memcpy(result->ob_bytes + va.len, vb.buf, vb.len)((__builtin_object_size (result->ob_bytes + va.len, 0) != (
size_t) -1) ? __builtin___memcpy_chk (result->ob_bytes + va
.len, vb.buf, vb.len, __builtin_object_size (result->ob_bytes
+ va.len, 0)) : __inline_memcpy_chk (result->ob_bytes + va
.len, vb.buf, vb.len))
;
252 }
253
254 done:
255 if (va.len != -1)
256 PyBuffer_Release(&va);
257 if (vb.len != -1)
258 PyBuffer_Release(&vb);
259 return (PyObject *)result;
260}
261
262/* Functions stuffed into the type object */
263
264static Py_ssize_t
265bytearray_length(PyByteArrayObject *self)
266{
267 return Py_SIZE(self)(((PyVarObject*)(self))->ob_size);
268}
269
270static PyObject *
271bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
272{
273 Py_ssize_t mysize;
274 Py_ssize_t size;
275 Py_buffer vo;
276
277 if (_getbuffer(other, &vo) < 0) {
278 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
279 Py_TYPE(other)(((PyObject*)(other))->ob_type)->tp_name, Py_TYPE(self)(((PyObject*)(self))->ob_type)->tp_name);
280 return NULL((void*)0);
281 }
282
283 mysize = Py_SIZE(self)(((PyVarObject*)(self))->ob_size);
284 size = mysize + vo.len;
285 if (size < 0) {
286 PyBuffer_Release(&vo);
287 return PyErr_NoMemory();
288 }
289 if (size < self->ob_alloc) {
290 Py_SIZE(self)(((PyVarObject*)(self))->ob_size) = size;
291 self->ob_bytes[Py_SIZE(self)(((PyVarObject*)(self))->ob_size)] = '\0'; /* Trailing null byte */
292 }
293 else if (PyByteArray_Resize((PyObject *)self, size) < 0) {
294 PyBuffer_Release(&vo);
295 return NULL((void*)0);
296 }
297 memcpy(self->ob_bytes + mysize, vo.buf, vo.len)((__builtin_object_size (self->ob_bytes + mysize, 0) != (size_t
) -1) ? __builtin___memcpy_chk (self->ob_bytes + mysize, vo
.buf, vo.len, __builtin_object_size (self->ob_bytes + mysize
, 0)) : __inline_memcpy_chk (self->ob_bytes + mysize, vo.buf
, vo.len))
;
298 PyBuffer_Release(&vo);
299 Py_INCREF(self)( _Py_RefTotal++ , ((PyObject*)(self))->ob_refcnt++);
300 return (PyObject *)self;
301}
302
303static PyObject *
304bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
305{
306 PyByteArrayObject *result;
307 Py_ssize_t mysize;
308 Py_ssize_t size;
309
310 if (count < 0)
311 count = 0;
312 mysize = Py_SIZE(self)(((PyVarObject*)(self))->ob_size);
313 if (count > 0 && mysize > PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1)) / count)
314 return PyErr_NoMemory();
315 size = mysize * count;
316 result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL((void*)0), size);
317 if (result != NULL((void*)0) && size != 0) {
318 if (mysize == 1)
319 memset(result->ob_bytes, self->ob_bytes[0], size)((__builtin_object_size (result->ob_bytes, 0) != (size_t) -
1) ? __builtin___memset_chk (result->ob_bytes, self->ob_bytes
[0], size, __builtin_object_size (result->ob_bytes, 0)) : __inline_memset_chk
(result->ob_bytes, self->ob_bytes[0], size))
;
320 else {
321 Py_ssize_t i;
322 for (i = 0; i < count; i++)
323 memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize)((__builtin_object_size (result->ob_bytes + i*mysize, 0) !=
(size_t) -1) ? __builtin___memcpy_chk (result->ob_bytes +
i*mysize, self->ob_bytes, mysize, __builtin_object_size (
result->ob_bytes + i*mysize, 0)) : __inline_memcpy_chk (result
->ob_bytes + i*mysize, self->ob_bytes, mysize))
;
324 }
325 }
326 return (PyObject *)result;
327}
328
329static PyObject *
330bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
331{
332 Py_ssize_t mysize;
333 Py_ssize_t size;
334
335 if (count < 0)
336 count = 0;
337 mysize = Py_SIZE(self)(((PyVarObject*)(self))->ob_size);
338 if (count > 0 && mysize > PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1)) / count)
339 return PyErr_NoMemory();
340 size = mysize * count;
341 if (size < self->ob_alloc) {
342 Py_SIZE(self)(((PyVarObject*)(self))->ob_size) = size;
343 self->ob_bytes[Py_SIZE(self)(((PyVarObject*)(self))->ob_size)] = '\0'; /* Trailing null byte */
344 }
345 else if (PyByteArray_Resize((PyObject *)self, size) < 0)
346 return NULL((void*)0);
347
348 if (mysize == 1)
349 memset(self->ob_bytes, self->ob_bytes[0], size)((__builtin_object_size (self->ob_bytes, 0) != (size_t) -1
) ? __builtin___memset_chk (self->ob_bytes, self->ob_bytes
[0], size, __builtin_object_size (self->ob_bytes, 0)) : __inline_memset_chk
(self->ob_bytes, self->ob_bytes[0], size))
;
350 else {
351 Py_ssize_t i;
352 for (i = 1; i < count; i++)
353 memcpy(self->ob_bytes + i*mysize, self->ob_bytes, mysize)((__builtin_object_size (self->ob_bytes + i*mysize, 0) != (
size_t) -1) ? __builtin___memcpy_chk (self->ob_bytes + i*mysize
, self->ob_bytes, mysize, __builtin_object_size (self->
ob_bytes + i*mysize, 0)) : __inline_memcpy_chk (self->ob_bytes
+ i*mysize, self->ob_bytes, mysize))
;
354 }
355
356 Py_INCREF(self)( _Py_RefTotal++ , ((PyObject*)(self))->ob_refcnt++);
357 return (PyObject *)self;
358}
359
360static PyObject *
361bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
362{
363 if (i < 0)
364 i += Py_SIZE(self)(((PyVarObject*)(self))->ob_size);
365 if (i < 0 || i >= Py_SIZE(self)(((PyVarObject*)(self))->ob_size)) {
366 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
367 return NULL((void*)0);
368 }
369 return PyLong_FromLong((unsigned char)(self->ob_bytes[i]));
370}
371
372static PyObject *
373bytearray_subscript(PyByteArrayObject *self, PyObject *index)
374{
375 if (PyIndex_Check(index)((index)->ob_type->tp_as_number != ((void*)0) &&
(index)->ob_type->tp_as_number->nb_index != ((void*
)0))
) {
376 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
377
378 if (i == -1 && PyErr_Occurred())
379 return NULL((void*)0);
380
381 if (i < 0)
382 i += PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 382, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
;
383
384 if (i < 0 || i >= Py_SIZE(self)(((PyVarObject*)(self))->ob_size)) {
385 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
386 return NULL((void*)0);
387 }
388 return PyLong_FromLong((unsigned char)(self->ob_bytes[i]));
389 }
390 else if (PySlice_Check(index)((((PyObject*)(index))->ob_type) == &PySlice_Type)) {
391 Py_ssize_t start, stop, step, slicelength, cur, i;
392 if (PySlice_GetIndicesEx(index,
393 PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 393, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
,
394 &start, &stop, &step, &slicelength) < 0) {
395 return NULL((void*)0);
396 }
397
398 if (slicelength <= 0)
399 return PyByteArray_FromStringAndSize("", 0);
400 else if (step == 1) {
401 return PyByteArray_FromStringAndSize(self->ob_bytes + start,
402 slicelength);
403 }
404 else {
405 char *source_buf = PyByteArray_AS_STRING(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 405, "PyByteArray_Check(self)"
) : (void)0), (((PyVarObject*)(self))->ob_size) ? ((PyByteArrayObject
*)(self))->ob_bytes : _PyByteArray_empty_string)
;
406 char *result_buf;
407 PyObject *result;
408
409 result = PyByteArray_FromStringAndSize(NULL((void*)0), slicelength);
410 if (result == NULL((void*)0))
411 return NULL((void*)0);
412
413 result_buf = PyByteArray_AS_STRING(result)((__builtin_expect(!(((((PyObject*)(result))->ob_type) == (
&PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(result
))->ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn
(__func__, "Objects/bytearrayobject.c", 413, "PyByteArray_Check(result)"
) : (void)0), (((PyVarObject*)(result))->ob_size) ? ((PyByteArrayObject
*)(result))->ob_bytes : _PyByteArray_empty_string)
;
414 for (cur = start, i = 0; i < slicelength;
415 cur += step, i++) {
416 result_buf[i] = source_buf[cur];
417 }
418 return result;
419 }
420 }
421 else {
422 PyErr_SetString(PyExc_TypeError, "bytearray indices must be integers");
423 return NULL((void*)0);
424 }
425}
426
427static int
428bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
429 PyObject *values)
430{
431 Py_ssize_t avail, needed;
432 void *bytes;
433 Py_buffer vbytes;
434 int res = 0;
435
436 vbytes.len = -1;
437 if (values == (PyObject *)self) {
438 /* Make a copy and call this function recursively */
439 int err;
440 values = PyByteArray_FromObject(values);
441 if (values == NULL((void*)0))
442 return -1;
443 err = bytearray_setslice(self, lo, hi, values);
444 Py_DECREF(values)do { if (_Py_RefTotal-- , --((PyObject*)(values))->ob_refcnt
!= 0) { if (((PyObject*)values)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/bytearrayobject.c", 444, (PyObject *)(values)); } else
_Py_Dealloc((PyObject *)(values)); } while (0)
;
445 return err;
446 }
447 if (values == NULL((void*)0)) {
448 /* del b[lo:hi] */
449 bytes = NULL((void*)0);
450 needed = 0;
451 }
452 else {
453 if (_getbuffer(values, &vbytes) < 0) {
454 PyErr_Format(PyExc_TypeError,
455 "can't set bytearray slice from %.100s",
456 Py_TYPE(values)(((PyObject*)(values))->ob_type)->tp_name);
457 return -1;
458 }
459 needed = vbytes.len;
460 bytes = vbytes.buf;
461 }
462
463 if (lo < 0)
464 lo = 0;
465 if (hi < lo)
466 hi = lo;
467 if (hi > Py_SIZE(self)(((PyVarObject*)(self))->ob_size))
468 hi = Py_SIZE(self)(((PyVarObject*)(self))->ob_size);
469
470 avail = hi - lo;
471 if (avail < 0)
472 lo = hi = avail = 0;
473
474 if (avail != needed) {
475 if (avail > needed) {
476 if (!_canresize(self)) {
477 res = -1;
478 goto finish;
479 }
480 /*
481 0 lo hi old_size
482 | |<----avail----->|<-----tomove------>|
483 | |<-needed->|<-----tomove------>|
484 0 lo new_hi new_size
485 */
486 memmove(self->ob_bytes + lo + needed, self->ob_bytes + hi,((__builtin_object_size (self->ob_bytes + lo + needed, 0) !=
(size_t) -1) ? __builtin___memmove_chk (self->ob_bytes + lo
+ needed, self->ob_bytes + hi, (((PyVarObject*)(self))->
ob_size) - hi, __builtin_object_size (self->ob_bytes + lo +
needed, 0)) : __inline_memmove_chk (self->ob_bytes + lo +
needed, self->ob_bytes + hi, (((PyVarObject*)(self))->
ob_size) - hi))
487 Py_SIZE(self) - hi)((__builtin_object_size (self->ob_bytes + lo + needed, 0) !=
(size_t) -1) ? __builtin___memmove_chk (self->ob_bytes + lo
+ needed, self->ob_bytes + hi, (((PyVarObject*)(self))->
ob_size) - hi, __builtin_object_size (self->ob_bytes + lo +
needed, 0)) : __inline_memmove_chk (self->ob_bytes + lo +
needed, self->ob_bytes + hi, (((PyVarObject*)(self))->
ob_size) - hi))
;
488 }
489 /* XXX(nnorwitz): need to verify this can't overflow! */
490 if (PyByteArray_Resize((PyObject *)self,
491 Py_SIZE(self)(((PyVarObject*)(self))->ob_size) + needed - avail) < 0) {
492 res = -1;
493 goto finish;
494 }
495 if (avail < needed) {
496 /*
497 0 lo hi old_size
498 | |<-avail->|<-----tomove------>|
499 | |<----needed---->|<-----tomove------>|
500 0 lo new_hi new_size
501 */
502 memmove(self->ob_bytes + lo + needed, self->ob_bytes + hi,((__builtin_object_size (self->ob_bytes + lo + needed, 0) !=
(size_t) -1) ? __builtin___memmove_chk (self->ob_bytes + lo
+ needed, self->ob_bytes + hi, (((PyVarObject*)(self))->
ob_size) - lo - needed, __builtin_object_size (self->ob_bytes
+ lo + needed, 0)) : __inline_memmove_chk (self->ob_bytes
+ lo + needed, self->ob_bytes + hi, (((PyVarObject*)(self
))->ob_size) - lo - needed))
503 Py_SIZE(self) - lo - needed)((__builtin_object_size (self->ob_bytes + lo + needed, 0) !=
(size_t) -1) ? __builtin___memmove_chk (self->ob_bytes + lo
+ needed, self->ob_bytes + hi, (((PyVarObject*)(self))->
ob_size) - lo - needed, __builtin_object_size (self->ob_bytes
+ lo + needed, 0)) : __inline_memmove_chk (self->ob_bytes
+ lo + needed, self->ob_bytes + hi, (((PyVarObject*)(self
))->ob_size) - lo - needed))
;
504 }
505 }
506
507 if (needed > 0)
508 memcpy(self->ob_bytes + lo, bytes, needed)((__builtin_object_size (self->ob_bytes + lo, 0) != (size_t
) -1) ? __builtin___memcpy_chk (self->ob_bytes + lo, bytes
, needed, __builtin_object_size (self->ob_bytes + lo, 0)) :
__inline_memcpy_chk (self->ob_bytes + lo, bytes, needed))
;
509
510
511 finish:
512 if (vbytes.len != -1)
513 PyBuffer_Release(&vbytes);
514 return res;
515}
516
517static int
518bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
519{
520 int ival;
521
522 if (i < 0)
523 i += Py_SIZE(self)(((PyVarObject*)(self))->ob_size);
524
525 if (i < 0 || i >= Py_SIZE(self)(((PyVarObject*)(self))->ob_size)) {
526 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
527 return -1;
528 }
529
530 if (value == NULL((void*)0))
531 return bytearray_setslice(self, i, i+1, NULL((void*)0));
532
533 if (!_getbytevalue(value, &ival))
534 return -1;
535
536 self->ob_bytes[i] = ival;
537 return 0;
538}
539
540static int
541bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)
542{
543 Py_ssize_t start, stop, step, slicelen, needed;
544 char *bytes;
545
546 if (PyIndex_Check(index)((index)->ob_type->tp_as_number != ((void*)0) &&
(index)->ob_type->tp_as_number->nb_index != ((void*
)0))
) {
547 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
548
549 if (i == -1 && PyErr_Occurred())
550 return -1;
551
552 if (i < 0)
553 i += PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 553, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
;
554
555 if (i < 0 || i >= Py_SIZE(self)(((PyVarObject*)(self))->ob_size)) {
556 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
557 return -1;
558 }
559
560 if (values == NULL((void*)0)) {
561 /* Fall through to slice assignment */
562 start = i;
563 stop = i + 1;
564 step = 1;
565 slicelen = 1;
566 }
567 else {
568 int ival;
569 if (!_getbytevalue(values, &ival))
570 return -1;
571 self->ob_bytes[i] = (char)ival;
572 return 0;
573 }
574 }
575 else if (PySlice_Check(index)((((PyObject*)(index))->ob_type) == &PySlice_Type)) {
576 if (PySlice_GetIndicesEx(index,
577 PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 577, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
,
578 &start, &stop, &step, &slicelen) < 0) {
579 return -1;
580 }
581 }
582 else {
583 PyErr_SetString(PyExc_TypeError, "bytearray indices must be integer");
584 return -1;
585 }
586
587 if (values == NULL((void*)0)) {
588 bytes = NULL((void*)0);
589 needed = 0;
590 }
591 else if (values == (PyObject *)self || !PyByteArray_Check(values)((((PyObject*)(values))->ob_type) == (&PyByteArray_Type
) || PyType_IsSubtype((((PyObject*)(values))->ob_type), (&
PyByteArray_Type)))
) {
592 /* Make a copy and call this function recursively */
593 int err;
594 values = PyByteArray_FromObject(values);
595 if (values == NULL((void*)0))
596 return -1;
597 err = bytearray_ass_subscript(self, index, values);
598 Py_DECREF(values)do { if (_Py_RefTotal-- , --((PyObject*)(values))->ob_refcnt
!= 0) { if (((PyObject*)values)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/bytearrayobject.c", 598, (PyObject *)(values)); } else
_Py_Dealloc((PyObject *)(values)); } while (0)
;
599 return err;
600 }
601 else {
602 assert(PyByteArray_Check(values))(__builtin_expect(!(((((PyObject*)(values))->ob_type) == (
&PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(values
))->ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn
(__func__, "Objects/bytearrayobject.c", 602, "PyByteArray_Check(values)"
) : (void)0)
;
603 bytes = ((PyByteArrayObject *)values)->ob_bytes;
604 needed = Py_SIZE(values)(((PyVarObject*)(values))->ob_size);
605 }
606 /* Make sure b[5:2] = ... inserts before 5, not before 2. */
607 if ((step < 0 && start < stop) ||
608 (step > 0 && start > stop))
609 stop = start;
610 if (step == 1) {
611 if (slicelen != needed) {
612 if (!_canresize(self))
613 return -1;
614 if (slicelen > needed) {
615 /*
616 0 start stop old_size
617 | |<---slicelen--->|<-----tomove------>|
618 | |<-needed->|<-----tomove------>|
619 0 lo new_hi new_size
620 */
621 memmove(self->ob_bytes + start + needed, self->ob_bytes + stop,((__builtin_object_size (self->ob_bytes + start + needed, 0
) != (size_t) -1) ? __builtin___memmove_chk (self->ob_bytes
+ start + needed, self->ob_bytes + stop, (((PyVarObject*)
(self))->ob_size) - stop, __builtin_object_size (self->
ob_bytes + start + needed, 0)) : __inline_memmove_chk (self->
ob_bytes + start + needed, self->ob_bytes + stop, (((PyVarObject
*)(self))->ob_size) - stop))
622 Py_SIZE(self) - stop)((__builtin_object_size (self->ob_bytes + start + needed, 0
) != (size_t) -1) ? __builtin___memmove_chk (self->ob_bytes
+ start + needed, self->ob_bytes + stop, (((PyVarObject*)
(self))->ob_size) - stop, __builtin_object_size (self->
ob_bytes + start + needed, 0)) : __inline_memmove_chk (self->
ob_bytes + start + needed, self->ob_bytes + stop, (((PyVarObject
*)(self))->ob_size) - stop))
;
623 }
624 if (PyByteArray_Resize((PyObject *)self,
625 Py_SIZE(self)(((PyVarObject*)(self))->ob_size) + needed - slicelen) < 0)
626 return -1;
627 if (slicelen < needed) {
628 /*
629 0 lo hi old_size
630 | |<-avail->|<-----tomove------>|
631 | |<----needed---->|<-----tomove------>|
632 0 lo new_hi new_size
633 */
634 memmove(self->ob_bytes + start + needed, self->ob_bytes + stop,((__builtin_object_size (self->ob_bytes + start + needed, 0
) != (size_t) -1) ? __builtin___memmove_chk (self->ob_bytes
+ start + needed, self->ob_bytes + stop, (((PyVarObject*)
(self))->ob_size) - start - needed, __builtin_object_size (
self->ob_bytes + start + needed, 0)) : __inline_memmove_chk
(self->ob_bytes + start + needed, self->ob_bytes + stop
, (((PyVarObject*)(self))->ob_size) - start - needed))
635 Py_SIZE(self) - start - needed)((__builtin_object_size (self->ob_bytes + start + needed, 0
) != (size_t) -1) ? __builtin___memmove_chk (self->ob_bytes
+ start + needed, self->ob_bytes + stop, (((PyVarObject*)
(self))->ob_size) - start - needed, __builtin_object_size (
self->ob_bytes + start + needed, 0)) : __inline_memmove_chk
(self->ob_bytes + start + needed, self->ob_bytes + stop
, (((PyVarObject*)(self))->ob_size) - start - needed))
;
636 }
637 }
638
639 if (needed > 0)
640 memcpy(self->ob_bytes + start, bytes, needed)((__builtin_object_size (self->ob_bytes + start, 0) != (size_t
) -1) ? __builtin___memcpy_chk (self->ob_bytes + start, bytes
, needed, __builtin_object_size (self->ob_bytes + start, 0
)) : __inline_memcpy_chk (self->ob_bytes + start, bytes, needed
))
;
641
642 return 0;
643 }
644 else {
645 if (needed == 0) {
646 /* Delete slice */
647 size_t cur;
648 Py_ssize_t i;
649
650 if (!_canresize(self))
651 return -1;
652
653 if (slicelen == 0)
654 /* Nothing to do here. */
655 return 0;
656
657 if (step < 0) {
658 stop = start + 1;
659 start = stop + step * (slicelen - 1) - 1;
660 step = -step;
661 }
662 for (cur = start, i = 0;
663 i < slicelen; cur += step, i++) {
664 Py_ssize_t lim = step - 1;
665
666 if (cur + step >= (size_t)PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 666, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
)
667 lim = PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 667, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
- cur - 1;
668
669 memmove(self->ob_bytes + cur - i,((__builtin_object_size (self->ob_bytes + cur - i, 0) != (
size_t) -1) ? __builtin___memmove_chk (self->ob_bytes + cur
- i, self->ob_bytes + cur + 1, lim, __builtin_object_size
(self->ob_bytes + cur - i, 0)) : __inline_memmove_chk (self
->ob_bytes + cur - i, self->ob_bytes + cur + 1, lim))
670 self->ob_bytes + cur + 1, lim)((__builtin_object_size (self->ob_bytes + cur - i, 0) != (
size_t) -1) ? __builtin___memmove_chk (self->ob_bytes + cur
- i, self->ob_bytes + cur + 1, lim, __builtin_object_size
(self->ob_bytes + cur - i, 0)) : __inline_memmove_chk (self
->ob_bytes + cur - i, self->ob_bytes + cur + 1, lim))
;
671 }
672 /* Move the tail of the bytes, in one chunk */
673 cur = start + (size_t)slicelen*step;
674 if (cur < (size_t)PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 674, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
) {
675 memmove(self->ob_bytes + cur - slicelen,((__builtin_object_size (self->ob_bytes + cur - slicelen, 0
) != (size_t) -1) ? __builtin___memmove_chk (self->ob_bytes
+ cur - slicelen, self->ob_bytes + cur, ((__builtin_expect
(!(((((PyObject*)(self))->ob_type) == (&PyByteArray_Type
) || PyType_IsSubtype((((PyObject*)(self))->ob_type), (&
PyByteArray_Type)))), 0) ? __assert_rtn(__func__, "Objects/bytearrayobject.c"
, 677, "PyByteArray_Check(self)") : (void)0),(((PyVarObject*)
(self))->ob_size)) - cur, __builtin_object_size (self->
ob_bytes + cur - slicelen, 0)) : __inline_memmove_chk (self->
ob_bytes + cur - slicelen, self->ob_bytes + cur, ((__builtin_expect
(!(((((PyObject*)(self))->ob_type) == (&PyByteArray_Type
) || PyType_IsSubtype((((PyObject*)(self))->ob_type), (&
PyByteArray_Type)))), 0) ? __assert_rtn(__func__, "Objects/bytearrayobject.c"
, 677, "PyByteArray_Check(self)") : (void)0),(((PyVarObject*)
(self))->ob_size)) - cur))
676 self->ob_bytes + cur,((__builtin_object_size (self->ob_bytes + cur - slicelen, 0
) != (size_t) -1) ? __builtin___memmove_chk (self->ob_bytes
+ cur - slicelen, self->ob_bytes + cur, ((__builtin_expect
(!(((((PyObject*)(self))->ob_type) == (&PyByteArray_Type
) || PyType_IsSubtype((((PyObject*)(self))->ob_type), (&
PyByteArray_Type)))), 0) ? __assert_rtn(__func__, "Objects/bytearrayobject.c"
, 677, "PyByteArray_Check(self)") : (void)0),(((PyVarObject*)
(self))->ob_size)) - cur, __builtin_object_size (self->
ob_bytes + cur - slicelen, 0)) : __inline_memmove_chk (self->
ob_bytes + cur - slicelen, self->ob_bytes + cur, ((__builtin_expect
(!(((((PyObject*)(self))->ob_type) == (&PyByteArray_Type
) || PyType_IsSubtype((((PyObject*)(self))->ob_type), (&
PyByteArray_Type)))), 0) ? __assert_rtn(__func__, "Objects/bytearrayobject.c"
, 677, "PyByteArray_Check(self)") : (void)0),(((PyVarObject*)
(self))->ob_size)) - cur))
677 PyByteArray_GET_SIZE(self) - cur)((__builtin_object_size (self->ob_bytes + cur - slicelen, 0
) != (size_t) -1) ? __builtin___memmove_chk (self->ob_bytes
+ cur - slicelen, self->ob_bytes + cur, ((__builtin_expect
(!(((((PyObject*)(self))->ob_type) == (&PyByteArray_Type
) || PyType_IsSubtype((((PyObject*)(self))->ob_type), (&
PyByteArray_Type)))), 0) ? __assert_rtn(__func__, "Objects/bytearrayobject.c"
, 677, "PyByteArray_Check(self)") : (void)0),(((PyVarObject*)
(self))->ob_size)) - cur, __builtin_object_size (self->
ob_bytes + cur - slicelen, 0)) : __inline_memmove_chk (self->
ob_bytes + cur - slicelen, self->ob_bytes + cur, ((__builtin_expect
(!(((((PyObject*)(self))->ob_type) == (&PyByteArray_Type
) || PyType_IsSubtype((((PyObject*)(self))->ob_type), (&
PyByteArray_Type)))), 0) ? __assert_rtn(__func__, "Objects/bytearrayobject.c"
, 677, "PyByteArray_Check(self)") : (void)0),(((PyVarObject*)
(self))->ob_size)) - cur))
;
678 }
679 if (PyByteArray_Resize((PyObject *)self,
680 PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 680, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
- slicelen) < 0)
681 return -1;
682
683 return 0;
684 }
685 else {
686 /* Assign slice */
687 Py_ssize_t i;
688 size_t cur;
689
690 if (needed != slicelen) {
691 PyErr_Format(PyExc_ValueError,
692 "attempt to assign bytes of size %zd "
693 "to extended slice of size %zd",
694 needed, slicelen);
695 return -1;
696 }
697 for (cur = start, i = 0; i < slicelen; cur += step, i++)
698 self->ob_bytes[cur] = bytes[i];
699 return 0;
700 }
701 }
702}
703
704static int
705bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
706{
707 static char *kwlist[] = {"source", "encoding", "errors", 0};
708 PyObject *arg = NULL((void*)0);
709 const char *encoding = NULL((void*)0);
710 const char *errors = NULL((void*)0);
711 Py_ssize_t count;
712 PyObject *it;
713 PyObject *(*iternext)(PyObject *);
714
715 if (Py_SIZE(self)(((PyVarObject*)(self))->ob_size) != 0) {
716 /* Empty previous contents (yes, do this first of all!) */
717 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
718 return -1;
719 }
720
721 /* Parse arguments */
722 if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kwds, "|Oss:bytearray", kwlist,
723 &arg, &encoding, &errors))
724 return -1;
725
726 /* Make a quick exit if no first argument */
727 if (arg == NULL((void*)0)) {
728 if (encoding != NULL((void*)0) || errors != NULL((void*)0)) {
729 PyErr_SetString(PyExc_TypeError,
730 "encoding or errors without sequence argument");
731 return -1;
732 }
733 return 0;
734 }
735
736 if (PyUnicode_Check(arg)((((((PyObject*)(arg))->ob_type))->tp_flags & ((1L<<
28))) != 0)
) {
737 /* Encode via the codec registry */
738 PyObject *encoded, *new;
739 if (encoding == NULL((void*)0)) {
740 PyErr_SetString(PyExc_TypeError,
741 "string argument without an encoding");
742 return -1;
743 }
744 encoded = PyUnicode_AsEncodedStringPyUnicodeUCS2_AsEncodedString(arg, encoding, errors);
745 if (encoded == NULL((void*)0))
746 return -1;
747 assert(PyBytes_Check(encoded))(__builtin_expect(!(((((((PyObject*)(encoded))->ob_type))->
tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 747, "PyBytes_Check(encoded)")
: (void)0)
;
748 new = bytearray_iconcat(self, encoded);
749 Py_DECREF(encoded)do { if (_Py_RefTotal-- , --((PyObject*)(encoded))->ob_refcnt
!= 0) { if (((PyObject*)encoded)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/bytearrayobject.c", 749, (PyObject *)(encoded)); } else
_Py_Dealloc((PyObject *)(encoded)); } while (0)
;
750 if (new == NULL((void*)0))
751 return -1;
752 Py_DECREF(new)do { if (_Py_RefTotal-- , --((PyObject*)(new))->ob_refcnt !=
0) { if (((PyObject*)new)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/bytearrayobject.c", 752, (PyObject *)(new)); } else
_Py_Dealloc((PyObject *)(new)); } while (0)
;
753 return 0;
754 }
755
756 /* If it's not unicode, there can't be encoding or errors */
757 if (encoding != NULL((void*)0) || errors != NULL((void*)0)) {
758 PyErr_SetString(PyExc_TypeError,
759 "encoding or errors without a string argument");
760 return -1;
761 }
762
763 /* Is it an int? */
764 count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
765 if (count == -1 && PyErr_Occurred()) {
766 if (PyErr_ExceptionMatches(PyExc_OverflowError))
767 return -1;
768 PyErr_Clear();
769 }
770 else if (count < 0) {
771 PyErr_SetString(PyExc_ValueError, "negative count");
772 return -1;
773 }
774 else {
775 if (count > 0) {
776 if (PyByteArray_Resize((PyObject *)self, count))
777 return -1;
778 memset(self->ob_bytes, 0, count)((__builtin_object_size (self->ob_bytes, 0) != (size_t) -1
) ? __builtin___memset_chk (self->ob_bytes, 0, count, __builtin_object_size
(self->ob_bytes, 0)) : __inline_memset_chk (self->ob_bytes
, 0, count))
;
779 }
780 return 0;
781 }
782
783 /* Use the buffer API */
784 if (PyObject_CheckBuffer(arg)(((arg)->ob_type->tp_as_buffer != ((void*)0)) &&
((arg)->ob_type->tp_as_buffer->bf_getbuffer != ((void
*)0)))
) {
785 Py_ssize_t size;
786 Py_buffer view;
787 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO((0x0100 | (0x0010 | 0x0008)) | 0x0004)) < 0)
788 return -1;
789 size = view.len;
790 if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
791 if (PyBuffer_ToContiguous(self->ob_bytes, &view, size, 'C') < 0)
792 goto fail;
793 PyBuffer_Release(&view);
794 return 0;
795 fail:
796 PyBuffer_Release(&view);
797 return -1;
798 }
799
800 /* XXX Optimize this if the arguments is a list, tuple */
801
802 /* Get the iterator */
803 it = PyObject_GetIter(arg);
804 if (it == NULL((void*)0))
805 return -1;
806 iternext = *Py_TYPE(it)(((PyObject*)(it))->ob_type)->tp_iternext;
807
808 /* Run the iterator to exhaustion */
809 for (;;) {
810 PyObject *item;
811 int rc, value;
812
813 /* Get the next item */
814 item = iternext(it);
815 if (item == NULL((void*)0)) {
816 if (PyErr_Occurred()) {
817 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
818 goto error;
819 PyErr_Clear();
820 }
821 break;
822 }
823
824 /* Interpret it as an int (__index__) */
825 rc = _getbytevalue(item, &value);
826 Py_DECREF(item)do { if (_Py_RefTotal-- , --((PyObject*)(item))->ob_refcnt
!= 0) { if (((PyObject*)item)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/bytearrayobject.c", 826, (PyObject *)(item)); } else
_Py_Dealloc((PyObject *)(item)); } while (0)
;
827 if (!rc)
828 goto error;
829
830 /* Append the byte */
831 if (Py_SIZE(self)(((PyVarObject*)(self))->ob_size) < self->ob_alloc)
832 Py_SIZE(self)(((PyVarObject*)(self))->ob_size)++;
833 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)(((PyVarObject*)(self))->ob_size)+1) < 0)
834 goto error;
835 self->ob_bytes[Py_SIZE(self)(((PyVarObject*)(self))->ob_size)-1] = value;
836 }
837
838 /* Clean up and return success */
839 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/bytearrayobject.c", 839, (PyObject *)(it)); } else _Py_Dealloc
((PyObject *)(it)); } while (0)
;
840 return 0;
841
842 error:
843 /* Error handling when it != NULL */
844 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/bytearrayobject.c", 844, (PyObject *)(it)); } else _Py_Dealloc
((PyObject *)(it)); } while (0)
;
845 return -1;
846}
847
848/* Mostly copied from string_repr, but without the
849 "smart quote" functionality. */
850static PyObject *
851bytearray_repr(PyByteArrayObject *self)
852{
853 static const char *hexdigits = "0123456789abcdef";
854 const char *quote_prefix = "bytearray(b";
855 const char *quote_postfix = ")";
856 Py_ssize_t length = Py_SIZE(self)(((PyVarObject*)(self))->ob_size);
857 /* 14 == strlen(quote_prefix) + 2 + strlen(quote_postfix) */
858 size_t newsize;
859 PyObject *v;
860 if (length > (PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1)) - 14) / 4) {
861 PyErr_SetString(PyExc_OverflowError,
862 "bytearray object is too large to make repr");
863 return NULL((void*)0);
864 }
865 newsize = 14 + 4 * length;
866 v = PyUnicode_FromUnicodePyUnicodeUCS2_FromUnicode(NULL((void*)0), newsize);
867 if (v == NULL((void*)0)) {
868 return NULL((void*)0);
869 }
870 else {
871 register Py_ssize_t i;
872 register Py_UNICODE c;
873 register Py_UNICODE *p;
874 int quote;
875
876 /* Figure out which quote to use; single is preferred */
877 quote = '\'';
878 {
879 char *test, *start;
880 start = PyByteArray_AS_STRING(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 880, "PyByteArray_Check(self)"
) : (void)0), (((PyVarObject*)(self))->ob_size) ? ((PyByteArrayObject
*)(self))->ob_bytes : _PyByteArray_empty_string)
;
881 for (test = start; test < start+length; ++test) {
882 if (*test == '"') {
883 quote = '\''; /* back to single */
884 goto decided;
885 }
886 else if (*test == '\'')
887 quote = '"';
888 }
889 decided:
890 ;
891 }
892
893 p = PyUnicode_AS_UNICODE(v)((__builtin_expect(!(((((((PyObject*)(v))->ob_type))->tp_flags
& ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__, "Objects/bytearrayobject.c"
, 893, "PyUnicode_Check(v)") : (void)0),(((PyUnicodeObject *)
(v))->str))
;
894 while (*quote_prefix)
895 *p++ = *quote_prefix++;
896 *p++ = quote;
897
898 for (i = 0; i < length; i++) {
899 /* There's at least enough room for a hex escape
900 and a closing quote. */
901 assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 5)(__builtin_expect(!(newsize - (p - ((__builtin_expect(!((((((
(PyObject*)(v))->ob_type))->tp_flags & ((1L<<
28))) != 0)), 0) ? __assert_rtn(__func__, "Objects/bytearrayobject.c"
, 901, "PyUnicode_Check(v)") : (void)0),(((PyUnicodeObject *)
(v))->str))) >= 5), 0) ? __assert_rtn(__func__, "Objects/bytearrayobject.c"
, 901, "newsize - (p - PyUnicode_AS_UNICODE(v)) >= 5") : (
void)0)
;
902 c = self->ob_bytes[i];
903 if (c == '\'' || c == '\\')
904 *p++ = '\\', *p++ = c;
905 else if (c == '\t')
906 *p++ = '\\', *p++ = 't';
907 else if (c == '\n')
908 *p++ = '\\', *p++ = 'n';
909 else if (c == '\r')
910 *p++ = '\\', *p++ = 'r';
911 else if (c == 0)
912 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
913 else if (c < ' ' || c >= 0x7f) {
914 *p++ = '\\';
915 *p++ = 'x';
916 *p++ = hexdigits[(c & 0xf0) >> 4];
917 *p++ = hexdigits[c & 0xf];
918 }
919 else
920 *p++ = c;
921 }
922 assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 1)(__builtin_expect(!(newsize - (p - ((__builtin_expect(!((((((
(PyObject*)(v))->ob_type))->tp_flags & ((1L<<
28))) != 0)), 0) ? __assert_rtn(__func__, "Objects/bytearrayobject.c"
, 922, "PyUnicode_Check(v)") : (void)0),(((PyUnicodeObject *)
(v))->str))) >= 1), 0) ? __assert_rtn(__func__, "Objects/bytearrayobject.c"
, 922, "newsize - (p - PyUnicode_AS_UNICODE(v)) >= 1") : (
void)0)
;
923 *p++ = quote;
924 while (*quote_postfix) {
925 *p++ = *quote_postfix++;
926 }
927 *p = '\0';
928 if (PyUnicode_ResizePyUnicodeUCS2_Resize(&v, (p - PyUnicode_AS_UNICODE(v)((__builtin_expect(!(((((((PyObject*)(v))->ob_type))->tp_flags
& ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__, "Objects/bytearrayobject.c"
, 928, "PyUnicode_Check(v)") : (void)0),(((PyUnicodeObject *)
(v))->str))
))) {
929 Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt !=
0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/bytearrayobject.c", 929, (PyObject *)(v)); } else _Py_Dealloc
((PyObject *)(v)); } while (0)
;
930 return NULL((void*)0);
931 }
932 return v;
933 }
934}
935
936static PyObject *
937bytearray_str(PyObject *op)
938{
939 if (Py_BytesWarningFlag) {
940 if (PyErr_WarnEx(PyExc_BytesWarning,
941 "str() on a bytearray instance", 1))
942 return NULL((void*)0);
943 }
944 return bytearray_repr((PyByteArrayObject*)op);
945}
946
947static PyObject *
948bytearray_richcompare(PyObject *self, PyObject *other, int op)
949{
950 Py_ssize_t self_size, other_size;
951 Py_buffer self_bytes, other_bytes;
952 PyObject *res;
953 Py_ssize_t minsize;
954 int cmp;
955
956 /* Bytes can be compared to anything that supports the (binary)
957 buffer API. Except that a comparison with Unicode is always an
958 error, even if the comparison is for equality. */
959 if (PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type) ||
960 PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type)) {
961 if (Py_BytesWarningFlag && (op == Py_EQ2 || op == Py_NE3)) {
962 if (PyErr_WarnEx(PyExc_BytesWarning,
963 "Comparison between bytearray and string", 1))
964 return NULL((void*)0);
965 }
966
967 Py_INCREF(Py_NotImplemented)( _Py_RefTotal++ , ((PyObject*)((&_Py_NotImplementedStruct
)))->ob_refcnt++)
;
968 return Py_NotImplemented(&_Py_NotImplementedStruct);
969 }
970
971 self_size = _getbuffer(self, &self_bytes);
972 if (self_size < 0) {
973 PyErr_Clear();
974 Py_INCREF(Py_NotImplemented)( _Py_RefTotal++ , ((PyObject*)((&_Py_NotImplementedStruct
)))->ob_refcnt++)
;
975 return Py_NotImplemented(&_Py_NotImplementedStruct);
976 }
977
978 other_size = _getbuffer(other, &other_bytes);
979 if (other_size < 0) {
980 PyErr_Clear();
981 PyBuffer_Release(&self_bytes);
982 Py_INCREF(Py_NotImplemented)( _Py_RefTotal++ , ((PyObject*)((&_Py_NotImplementedStruct
)))->ob_refcnt++)
;
983 return Py_NotImplemented(&_Py_NotImplementedStruct);
984 }
985
986 if (self_size != other_size && (op == Py_EQ2 || op == Py_NE3)) {
987 /* Shortcut: if the lengths differ, the objects differ */
988 cmp = (op == Py_NE3);
989 }
990 else {
991 minsize = self_size;
992 if (other_size < minsize)
993 minsize = other_size;
994
995 cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize);
996 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
997
998 if (cmp == 0) {
999 if (self_size < other_size)
1000 cmp = -1;
1001 else if (self_size > other_size)
1002 cmp = 1;
1003 }
1004
1005 switch (op) {
1006 case Py_LT0: cmp = cmp < 0; break;
1007 case Py_LE1: cmp = cmp <= 0; break;
1008 case Py_EQ2: cmp = cmp == 0; break;
1009 case Py_NE3: cmp = cmp != 0; break;
1010 case Py_GT4: cmp = cmp > 0; break;
1011 case Py_GE5: cmp = cmp >= 0; break;
1012 }
1013 }
1014
1015 res = cmp ? Py_True((PyObject *) &_Py_TrueStruct) : Py_False((PyObject *) &_Py_FalseStruct);
1016 PyBuffer_Release(&self_bytes);
1017 PyBuffer_Release(&other_bytes);
1018 Py_INCREF(res)( _Py_RefTotal++ , ((PyObject*)(res))->ob_refcnt++);
1019 return res;
1020}
1021
1022static void
1023bytearray_dealloc(PyByteArrayObject *self)
1024{
1025 if (self->ob_exports > 0) {
1026 PyErr_SetString(PyExc_SystemError,
1027 "deallocated bytearray object has exported buffers");
1028 PyErr_Print();
1029 }
1030 if (self->ob_bytes != 0) {
1031 PyMem_Free(self->ob_bytes);
1032 }
1033 Py_TYPE(self)(((PyObject*)(self))->ob_type)->tp_free((PyObject *)self);
1034}
1035
1036
1037/* -------------------------------------------------------------------- */
1038/* Methods */
1039
1040#define STRINGLIB_CHARchar char
1041#define STRINGLIB_LENPyByteArray_GET_SIZE PyByteArray_GET_SIZE
1042#define STRINGLIB_STRPyByteArray_AS_STRING PyByteArray_AS_STRING
1043#define STRINGLIB_NEWPyByteArray_FromStringAndSize PyByteArray_FromStringAndSize
1044#define STRINGLIB_ISSPACEPy_ISSPACE Py_ISSPACE
1045#define STRINGLIB_ISLINEBREAK(x)((x == '\n') || (x == '\r')) ((x == '\n') || (x == '\r'))
1046#define STRINGLIB_CHECK_EXACTPyByteArray_CheckExact PyByteArray_CheckExact
1047#define STRINGLIB_MUTABLE1 1
1048
1049#include "stringlib/fastsearch.h"
1050#include "stringlib/count.h"
1051#include "stringlib/find.h"
1052#include "stringlib/partition.h"
1053#include "stringlib/split.h"
1054#include "stringlib/ctype.h"
1055#include "stringlib/transmogrify.h"
1056
1057
1058/* The following Py_LOCAL_INLINE and Py_LOCAL functions
1059were copied from the old char* style string object. */
1060
1061/* helper macro to fixup start/end slice values */
1062#define ADJUST_INDICES(start, end, len)if (end > len) end = len; else if (end < 0) { end += len
; if (end < 0) end = 0; } if (start < 0) { start += len
; if (start < 0) start = 0; }
\
1063 if (end > len) \
1064 end = len; \
1065 else if (end < 0) { \
1066 end += len; \
1067 if (end < 0) \
1068 end = 0; \
1069 } \
1070 if (start < 0) { \
1071 start += len; \
1072 if (start < 0) \
1073 start = 0; \
1074 }
1075
1076Py_LOCAL_INLINE(Py_ssize_t)static inline Py_ssize_t
1077bytearray_find_internal(PyByteArrayObject *self, PyObject *args, int dir)
1078{
1079 PyObject *subobj;
1080 Py_buffer subbuf;
1081 Py_ssize_t start=0, end=PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1));
1082 Py_ssize_t res;
1083
1084 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O|O&O&:find/rfind/index/rindex", &subobj,
1085 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
1086 return -2;
1087 if (_getbuffer(subobj, &subbuf) < 0)
1088 return -2;
1089 if (dir > 0)
1090 res = stringlib_find_slice(
1091 PyByteArray_AS_STRING(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1091, "PyByteArray_Check(self)"
) : (void)0), (((PyVarObject*)(self))->ob_size) ? ((PyByteArrayObject
*)(self))->ob_bytes : _PyByteArray_empty_string)
, PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1091, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
,
1092 subbuf.buf, subbuf.len, start, end);
1093 else
1094 res = stringlib_rfind_slice(
1095 PyByteArray_AS_STRING(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1095, "PyByteArray_Check(self)"
) : (void)0), (((PyVarObject*)(self))->ob_size) ? ((PyByteArrayObject
*)(self))->ob_bytes : _PyByteArray_empty_string)
, PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1095, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
,
1096 subbuf.buf, subbuf.len, start, end);
1097 PyBuffer_Release(&subbuf);
1098 return res;
1099}
1100
1101PyDoc_STRVAR(find__doc__,static char find__doc__[] = "B.find(sub[, start[, end]]) -> int\n\nReturn the lowest index in B where subsection sub is found,\nsuch that sub is contained within s[start,end]. Optional\narguments start and end are interpreted as in slice notation.\n\nReturn -1 on failure."
1102"B.find(sub[, start[, end]]) -> int\n\static char find__doc__[] = "B.find(sub[, start[, end]]) -> int\n\nReturn the lowest index in B where subsection sub is found,\nsuch that sub is contained within s[start,end]. Optional\narguments start and end are interpreted as in slice notation.\n\nReturn -1 on failure."
1103\n\static char find__doc__[] = "B.find(sub[, start[, end]]) -> int\n\nReturn the lowest index in B where subsection sub is found,\nsuch that sub is contained within s[start,end]. Optional\narguments start and end are interpreted as in slice notation.\n\nReturn -1 on failure."
1104Return the lowest index in B where subsection sub is found,\n\static char find__doc__[] = "B.find(sub[, start[, end]]) -> int\n\nReturn the lowest index in B where subsection sub is found,\nsuch that sub is contained within s[start,end]. Optional\narguments start and end are interpreted as in slice notation.\n\nReturn -1 on failure."
1105such that sub is contained within s[start,end]. Optional\n\static char find__doc__[] = "B.find(sub[, start[, end]]) -> int\n\nReturn the lowest index in B where subsection sub is found,\nsuch that sub is contained within s[start,end]. Optional\narguments start and end are interpreted as in slice notation.\n\nReturn -1 on failure."
1106arguments start and end are interpreted as in slice notation.\n\static char find__doc__[] = "B.find(sub[, start[, end]]) -> int\n\nReturn the lowest index in B where subsection sub is found,\nsuch that sub is contained within s[start,end]. Optional\narguments start and end are interpreted as in slice notation.\n\nReturn -1 on failure."
1107\n\static char find__doc__[] = "B.find(sub[, start[, end]]) -> int\n\nReturn the lowest index in B where subsection sub is found,\nsuch that sub is contained within s[start,end]. Optional\narguments start and end are interpreted as in slice notation.\n\nReturn -1 on failure."
1108Return -1 on failure.")static char find__doc__[] = "B.find(sub[, start[, end]]) -> int\n\nReturn the lowest index in B where subsection sub is found,\nsuch that sub is contained within s[start,end]. Optional\narguments start and end are interpreted as in slice notation.\n\nReturn -1 on failure.";
1109
1110static PyObject *
1111bytearray_find(PyByteArrayObject *self, PyObject *args)
1112{
1113 Py_ssize_t result = bytearray_find_internal(self, args, +1);
1114 if (result == -2)
1115 return NULL((void*)0);
1116 return PyLong_FromSsize_t(result);
1117}
1118
1119PyDoc_STRVAR(count__doc__,static char count__doc__[] = "B.count(sub[, start[, end]]) -> int\n\nReturn the number of non-overlapping occurrences of subsection sub in\nbytes B[start:end]. Optional arguments start and end are interpreted\nas in slice notation."
1120"B.count(sub[, start[, end]]) -> int\n\static char count__doc__[] = "B.count(sub[, start[, end]]) -> int\n\nReturn the number of non-overlapping occurrences of subsection sub in\nbytes B[start:end]. Optional arguments start and end are interpreted\nas in slice notation."
1121\n\static char count__doc__[] = "B.count(sub[, start[, end]]) -> int\n\nReturn the number of non-overlapping occurrences of subsection sub in\nbytes B[start:end]. Optional arguments start and end are interpreted\nas in slice notation."
1122Return the number of non-overlapping occurrences of subsection sub in\n\static char count__doc__[] = "B.count(sub[, start[, end]]) -> int\n\nReturn the number of non-overlapping occurrences of subsection sub in\nbytes B[start:end]. Optional arguments start and end are interpreted\nas in slice notation."
1123bytes B[start:end]. Optional arguments start and end are interpreted\n\static char count__doc__[] = "B.count(sub[, start[, end]]) -> int\n\nReturn the number of non-overlapping occurrences of subsection sub in\nbytes B[start:end]. Optional arguments start and end are interpreted\nas in slice notation."
1124as in slice notation.")static char count__doc__[] = "B.count(sub[, start[, end]]) -> int\n\nReturn the number of non-overlapping occurrences of subsection sub in\nbytes B[start:end]. Optional arguments start and end are interpreted\nas in slice notation.";
1125
1126static PyObject *
1127bytearray_count(PyByteArrayObject *self, PyObject *args)
1128{
1129 PyObject *sub_obj;
1130 const char *str = PyByteArray_AS_STRING(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1130, "PyByteArray_Check(self)"
) : (void)0), (((PyVarObject*)(self))->ob_size) ? ((PyByteArrayObject
*)(self))->ob_bytes : _PyByteArray_empty_string)
;
1131 Py_ssize_t start = 0, end = PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1));
1132 Py_buffer vsub;
1133 PyObject *count_obj;
1134
1135 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O|O&O&:count", &sub_obj,
1136 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
1137 return NULL((void*)0);
1138
1139 if (_getbuffer(sub_obj, &vsub) < 0)
1140 return NULL((void*)0);
1141
1142 ADJUST_INDICES(start, end, PyByteArray_GET_SIZE(self))if (end > ((__builtin_expect(!(((((PyObject*)(self))->ob_type
) == (&PyByteArray_Type) || PyType_IsSubtype((((PyObject*
)(self))->ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn
(__func__, "Objects/bytearrayobject.c", 1142, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))) end = ((__builtin_expect
(!(((((PyObject*)(self))->ob_type) == (&PyByteArray_Type
) || PyType_IsSubtype((((PyObject*)(self))->ob_type), (&
PyByteArray_Type)))), 0) ? __assert_rtn(__func__, "Objects/bytearrayobject.c"
, 1142, "PyByteArray_Check(self)") : (void)0),(((PyVarObject*
)(self))->ob_size)); else if (end < 0) { end += ((__builtin_expect
(!(((((PyObject*)(self))->ob_type) == (&PyByteArray_Type
) || PyType_IsSubtype((((PyObject*)(self))->ob_type), (&
PyByteArray_Type)))), 0) ? __assert_rtn(__func__, "Objects/bytearrayobject.c"
, 1142, "PyByteArray_Check(self)") : (void)0),(((PyVarObject*
)(self))->ob_size)); if (end < 0) end = 0; } if (start <
0) { start += ((__builtin_expect(!(((((PyObject*)(self))->
ob_type) == (&PyByteArray_Type) || PyType_IsSubtype((((PyObject
*)(self))->ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn
(__func__, "Objects/bytearrayobject.c", 1142, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size)); if (start
< 0) start = 0; }
;
1143
1144 count_obj = PyLong_FromSsize_t(
1145 stringlib_count(str + start, end - start, vsub.buf, vsub.len, PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1)))
1146 );
1147 PyBuffer_Release(&vsub);
1148 return count_obj;
1149}
1150
1151
1152PyDoc_STRVAR(index__doc__,static char index__doc__[] = "B.index(sub[, start[, end]]) -> int\n\nLike B.find() but raise ValueError when the subsection is not found."
1153"B.index(sub[, start[, end]]) -> int\n\static char index__doc__[] = "B.index(sub[, start[, end]]) -> int\n\nLike B.find() but raise ValueError when the subsection is not found."
1154\n\static char index__doc__[] = "B.index(sub[, start[, end]]) -> int\n\nLike B.find() but raise ValueError when the subsection is not found."
1155Like B.find() but raise ValueError when the subsection is not found.")static char index__doc__[] = "B.index(sub[, start[, end]]) -> int\n\nLike B.find() but raise ValueError when the subsection is not found.";
1156
1157static PyObject *
1158bytearray_index(PyByteArrayObject *self, PyObject *args)
1159{
1160 Py_ssize_t result = bytearray_find_internal(self, args, +1);
1161 if (result == -2)
1162 return NULL((void*)0);
1163 if (result == -1) {
1164 PyErr_SetString(PyExc_ValueError,
1165 "subsection not found");
1166 return NULL((void*)0);
1167 }
1168 return PyLong_FromSsize_t(result);
1169}
1170
1171
1172PyDoc_STRVAR(rfind__doc__,static char rfind__doc__[] = "B.rfind(sub[, start[, end]]) -> int\n\nReturn the highest index in B where subsection sub is found,\nsuch that sub is contained within s[start,end]. Optional\narguments start and end are interpreted as in slice notation.\n\nReturn -1 on failure."
1173"B.rfind(sub[, start[, end]]) -> int\n\static char rfind__doc__[] = "B.rfind(sub[, start[, end]]) -> int\n\nReturn the highest index in B where subsection sub is found,\nsuch that sub is contained within s[start,end]. Optional\narguments start and end are interpreted as in slice notation.\n\nReturn -1 on failure."
1174\n\static char rfind__doc__[] = "B.rfind(sub[, start[, end]]) -> int\n\nReturn the highest index in B where subsection sub is found,\nsuch that sub is contained within s[start,end]. Optional\narguments start and end are interpreted as in slice notation.\n\nReturn -1 on failure."
1175Return the highest index in B where subsection sub is found,\n\static char rfind__doc__[] = "B.rfind(sub[, start[, end]]) -> int\n\nReturn the highest index in B where subsection sub is found,\nsuch that sub is contained within s[start,end]. Optional\narguments start and end are interpreted as in slice notation.\n\nReturn -1 on failure."
1176such that sub is contained within s[start,end]. Optional\n\static char rfind__doc__[] = "B.rfind(sub[, start[, end]]) -> int\n\nReturn the highest index in B where subsection sub is found,\nsuch that sub is contained within s[start,end]. Optional\narguments start and end are interpreted as in slice notation.\n\nReturn -1 on failure."
1177arguments start and end are interpreted as in slice notation.\n\static char rfind__doc__[] = "B.rfind(sub[, start[, end]]) -> int\n\nReturn the highest index in B where subsection sub is found,\nsuch that sub is contained within s[start,end]. Optional\narguments start and end are interpreted as in slice notation.\n\nReturn -1 on failure."
1178\n\static char rfind__doc__[] = "B.rfind(sub[, start[, end]]) -> int\n\nReturn the highest index in B where subsection sub is found,\nsuch that sub is contained within s[start,end]. Optional\narguments start and end are interpreted as in slice notation.\n\nReturn -1 on failure."
1179Return -1 on failure.")static char rfind__doc__[] = "B.rfind(sub[, start[, end]]) -> int\n\nReturn the highest index in B where subsection sub is found,\nsuch that sub is contained within s[start,end]. Optional\narguments start and end are interpreted as in slice notation.\n\nReturn -1 on failure.";
1180
1181static PyObject *
1182bytearray_rfind(PyByteArrayObject *self, PyObject *args)
1183{
1184 Py_ssize_t result = bytearray_find_internal(self, args, -1);
1185 if (result == -2)
1186 return NULL((void*)0);
1187 return PyLong_FromSsize_t(result);
1188}
1189
1190
1191PyDoc_STRVAR(rindex__doc__,static char rindex__doc__[] = "B.rindex(sub[, start[, end]]) -> int\n\nLike B.rfind() but raise ValueError when the subsection is not found."
1192"B.rindex(sub[, start[, end]]) -> int\n\static char rindex__doc__[] = "B.rindex(sub[, start[, end]]) -> int\n\nLike B.rfind() but raise ValueError when the subsection is not found."
1193\n\static char rindex__doc__[] = "B.rindex(sub[, start[, end]]) -> int\n\nLike B.rfind() but raise ValueError when the subsection is not found."
1194Like B.rfind() but raise ValueError when the subsection is not found.")static char rindex__doc__[] = "B.rindex(sub[, start[, end]]) -> int\n\nLike B.rfind() but raise ValueError when the subsection is not found.";
1195
1196static PyObject *
1197bytearray_rindex(PyByteArrayObject *self, PyObject *args)
1198{
1199 Py_ssize_t result = bytearray_find_internal(self, args, -1);
1200 if (result == -2)
1201 return NULL((void*)0);
1202 if (result == -1) {
1203 PyErr_SetString(PyExc_ValueError,
1204 "subsection not found");
1205 return NULL((void*)0);
1206 }
1207 return PyLong_FromSsize_t(result);
1208}
1209
1210
1211static int
1212bytearray_contains(PyObject *self, PyObject *arg)
1213{
1214 Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError);
1215 if (ival == -1 && PyErr_Occurred()) {
1216 Py_buffer varg;
1217 Py_ssize_t pos;
1218 PyErr_Clear();
1219 if (_getbuffer(arg, &varg) < 0)
1220 return -1;
1221 pos = stringlib_find(PyByteArray_AS_STRING(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1221, "PyByteArray_Check(self)"
) : (void)0), (((PyVarObject*)(self))->ob_size) ? ((PyByteArrayObject
*)(self))->ob_bytes : _PyByteArray_empty_string)
, Py_SIZE(self)(((PyVarObject*)(self))->ob_size),
1222 varg.buf, varg.len, 0);
1223 PyBuffer_Release(&varg);
1224 return pos >= 0;
1225 }
1226 if (ival < 0 || ival >= 256) {
1227 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
1228 return -1;
1229 }
1230
1231 return memchr(PyByteArray_AS_STRING(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1231, "PyByteArray_Check(self)"
) : (void)0), (((PyVarObject*)(self))->ob_size) ? ((PyByteArrayObject
*)(self))->ob_bytes : _PyByteArray_empty_string)
, (int) ival, Py_SIZE(self)(((PyVarObject*)(self))->ob_size)) != NULL((void*)0);
1232}
1233
1234
1235/* Matches the end (direction >= 0) or start (direction < 0) of self
1236 * against substr, using the start and end arguments. Returns
1237 * -1 on error, 0 if not found and 1 if found.
1238 */
1239Py_LOCAL(int)static int
1240_bytearray_tailmatch(PyByteArrayObject *self, PyObject *substr, Py_ssize_t start,
1241 Py_ssize_t end, int direction)
1242{
1243 Py_ssize_t len = PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1243, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
;
1244 const char* str;
1245 Py_buffer vsubstr;
1246 int rv = 0;
1247
1248 str = PyByteArray_AS_STRING(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1248, "PyByteArray_Check(self)"
) : (void)0), (((PyVarObject*)(self))->ob_size) ? ((PyByteArrayObject
*)(self))->ob_bytes : _PyByteArray_empty_string)
;
1249
1250 if (_getbuffer(substr, &vsubstr) < 0)
1251 return -1;
1252
1253 ADJUST_INDICES(start, end, len)if (end > len) end = len; else if (end < 0) { end += len
; if (end < 0) end = 0; } if (start < 0) { start += len
; if (start < 0) start = 0; }
;
1254
1255 if (direction < 0) {
1256 /* startswith */
1257 if (start+vsubstr.len > len) {
1258 goto done;
1259 }
1260 } else {
1261 /* endswith */
1262 if (end-start < vsubstr.len || start > len) {
1263 goto done;
1264 }
1265
1266 if (end-vsubstr.len > start)
1267 start = end - vsubstr.len;
1268 }
1269 if (end-start >= vsubstr.len)
1270 rv = ! memcmp(str+start, vsubstr.buf, vsubstr.len);
1271
1272done:
1273 PyBuffer_Release(&vsubstr);
1274 return rv;
1275}
1276
1277
1278PyDoc_STRVAR(startswith__doc__,static char startswith__doc__[] = "B.startswith(prefix[, start[, end]]) -> bool\n\nReturn True if B starts with the specified prefix, False otherwise.\nWith optional start, test B beginning at that position.\nWith optional end, stop comparing B at that position.\nprefix can also be a tuple of strings to try."
1279"B.startswith(prefix[, start[, end]]) -> bool\n\static char startswith__doc__[] = "B.startswith(prefix[, start[, end]]) -> bool\n\nReturn True if B starts with the specified prefix, False otherwise.\nWith optional start, test B beginning at that position.\nWith optional end, stop comparing B at that position.\nprefix can also be a tuple of strings to try."
1280\n\static char startswith__doc__[] = "B.startswith(prefix[, start[, end]]) -> bool\n\nReturn True if B starts with the specified prefix, False otherwise.\nWith optional start, test B beginning at that position.\nWith optional end, stop comparing B at that position.\nprefix can also be a tuple of strings to try."
1281Return True if B starts with the specified prefix, False otherwise.\n\static char startswith__doc__[] = "B.startswith(prefix[, start[, end]]) -> bool\n\nReturn True if B starts with the specified prefix, False otherwise.\nWith optional start, test B beginning at that position.\nWith optional end, stop comparing B at that position.\nprefix can also be a tuple of strings to try."
1282With optional start, test B beginning at that position.\n\static char startswith__doc__[] = "B.startswith(prefix[, start[, end]]) -> bool\n\nReturn True if B starts with the specified prefix, False otherwise.\nWith optional start, test B beginning at that position.\nWith optional end, stop comparing B at that position.\nprefix can also be a tuple of strings to try."
1283With optional end, stop comparing B at that position.\n\static char startswith__doc__[] = "B.startswith(prefix[, start[, end]]) -> bool\n\nReturn True if B starts with the specified prefix, False otherwise.\nWith optional start, test B beginning at that position.\nWith optional end, stop comparing B at that position.\nprefix can also be a tuple of strings to try."
1284prefix can also be a tuple of strings to try.")static char startswith__doc__[] = "B.startswith(prefix[, start[, end]]) -> bool\n\nReturn True if B starts with the specified prefix, False otherwise.\nWith optional start, test B beginning at that position.\nWith optional end, stop comparing B at that position.\nprefix can also be a tuple of strings to try.";
1285
1286static PyObject *
1287bytearray_startswith(PyByteArrayObject *self, PyObject *args)
1288{
1289 Py_ssize_t start = 0;
1290 Py_ssize_t end = PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1));
1291 PyObject *subobj;
1292 int result;
1293
1294 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O|O&O&:startswith", &subobj,
1295 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
1296 return NULL((void*)0);
1297 if (PyTuple_Check(subobj)((((((PyObject*)(subobj))->ob_type))->tp_flags & ((
1L<<26))) != 0)
) {
1298 Py_ssize_t i;
1299 for (i = 0; i < PyTuple_GET_SIZE(subobj)(((PyVarObject*)(subobj))->ob_size); i++) {
1300 result = _bytearray_tailmatch(self,
1301 PyTuple_GET_ITEM(subobj, i)(((PyTupleObject *)(subobj))->ob_item[i]),
1302 start, end, -1);
1303 if (result == -1)
1304 return NULL((void*)0);
1305 else if (result) {
1306 Py_RETURN_TRUEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_TrueStruct
)))->ob_refcnt++), ((PyObject *) &_Py_TrueStruct)
;
1307 }
1308 }
1309 Py_RETURN_FALSEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_FalseStruct
)))->ob_refcnt++), ((PyObject *) &_Py_FalseStruct)
;
1310 }
1311 result = _bytearray_tailmatch(self, subobj, start, end, -1);
1312 if (result == -1)
1313 return NULL((void*)0);
1314 else
1315 return PyBool_FromLong(result);
1316}
1317
1318PyDoc_STRVAR(endswith__doc__,static char endswith__doc__[] = "B.endswith(suffix[, start[, end]]) -> bool\n\nReturn True if B ends with the specified suffix, False otherwise.\nWith optional start, test B beginning at that position.\nWith optional end, stop comparing B at that position.\nsuffix can also be a tuple of strings to try."
1319"B.endswith(suffix[, start[, end]]) -> bool\n\static char endswith__doc__[] = "B.endswith(suffix[, start[, end]]) -> bool\n\nReturn True if B ends with the specified suffix, False otherwise.\nWith optional start, test B beginning at that position.\nWith optional end, stop comparing B at that position.\nsuffix can also be a tuple of strings to try."
1320\n\static char endswith__doc__[] = "B.endswith(suffix[, start[, end]]) -> bool\n\nReturn True if B ends with the specified suffix, False otherwise.\nWith optional start, test B beginning at that position.\nWith optional end, stop comparing B at that position.\nsuffix can also be a tuple of strings to try."
1321Return True if B ends with the specified suffix, False otherwise.\n\static char endswith__doc__[] = "B.endswith(suffix[, start[, end]]) -> bool\n\nReturn True if B ends with the specified suffix, False otherwise.\nWith optional start, test B beginning at that position.\nWith optional end, stop comparing B at that position.\nsuffix can also be a tuple of strings to try."
1322With optional start, test B beginning at that position.\n\static char endswith__doc__[] = "B.endswith(suffix[, start[, end]]) -> bool\n\nReturn True if B ends with the specified suffix, False otherwise.\nWith optional start, test B beginning at that position.\nWith optional end, stop comparing B at that position.\nsuffix can also be a tuple of strings to try."
1323With optional end, stop comparing B at that position.\n\static char endswith__doc__[] = "B.endswith(suffix[, start[, end]]) -> bool\n\nReturn True if B ends with the specified suffix, False otherwise.\nWith optional start, test B beginning at that position.\nWith optional end, stop comparing B at that position.\nsuffix can also be a tuple of strings to try."
1324suffix can also be a tuple of strings to try.")static char endswith__doc__[] = "B.endswith(suffix[, start[, end]]) -> bool\n\nReturn True if B ends with the specified suffix, False otherwise.\nWith optional start, test B beginning at that position.\nWith optional end, stop comparing B at that position.\nsuffix can also be a tuple of strings to try.";
1325
1326static PyObject *
1327bytearray_endswith(PyByteArrayObject *self, PyObject *args)
1328{
1329 Py_ssize_t start = 0;
1330 Py_ssize_t end = PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1));
1331 PyObject *subobj;
1332 int result;
1333
1334 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O|O&O&:endswith", &subobj,
1335 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
1336 return NULL((void*)0);
1337 if (PyTuple_Check(subobj)((((((PyObject*)(subobj))->ob_type))->tp_flags & ((
1L<<26))) != 0)
) {
1338 Py_ssize_t i;
1339 for (i = 0; i < PyTuple_GET_SIZE(subobj)(((PyVarObject*)(subobj))->ob_size); i++) {
1340 result = _bytearray_tailmatch(self,
1341 PyTuple_GET_ITEM(subobj, i)(((PyTupleObject *)(subobj))->ob_item[i]),
1342 start, end, +1);
1343 if (result == -1)
1344 return NULL((void*)0);
1345 else if (result) {
1346 Py_RETURN_TRUEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_TrueStruct
)))->ob_refcnt++), ((PyObject *) &_Py_TrueStruct)
;
1347 }
1348 }
1349 Py_RETURN_FALSEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_FalseStruct
)))->ob_refcnt++), ((PyObject *) &_Py_FalseStruct)
;
1350 }
1351 result = _bytearray_tailmatch(self, subobj, start, end, +1);
1352 if (result == -1)
1353 return NULL((void*)0);
1354 else
1355 return PyBool_FromLong(result);
1356}
1357
1358
1359PyDoc_STRVAR(translate__doc__,static char translate__doc__[] = "B.translate(table[, deletechars]) -> bytearray\n\nReturn a copy of B, where all characters occurring in the\noptional argument deletechars are removed, and the remaining\ncharacters have been mapped through the given translation\ntable, which must be a bytes object of length 256."
1360"B.translate(table[, deletechars]) -> bytearray\n\static char translate__doc__[] = "B.translate(table[, deletechars]) -> bytearray\n\nReturn a copy of B, where all characters occurring in the\noptional argument deletechars are removed, and the remaining\ncharacters have been mapped through the given translation\ntable, which must be a bytes object of length 256."
1361\n\static char translate__doc__[] = "B.translate(table[, deletechars]) -> bytearray\n\nReturn a copy of B, where all characters occurring in the\noptional argument deletechars are removed, and the remaining\ncharacters have been mapped through the given translation\ntable, which must be a bytes object of length 256."
1362Return a copy of B, where all characters occurring in the\n\static char translate__doc__[] = "B.translate(table[, deletechars]) -> bytearray\n\nReturn a copy of B, where all characters occurring in the\noptional argument deletechars are removed, and the remaining\ncharacters have been mapped through the given translation\ntable, which must be a bytes object of length 256."
1363optional argument deletechars are removed, and the remaining\n\static char translate__doc__[] = "B.translate(table[, deletechars]) -> bytearray\n\nReturn a copy of B, where all characters occurring in the\noptional argument deletechars are removed, and the remaining\ncharacters have been mapped through the given translation\ntable, which must be a bytes object of length 256."
1364characters have been mapped through the given translation\n\static char translate__doc__[] = "B.translate(table[, deletechars]) -> bytearray\n\nReturn a copy of B, where all characters occurring in the\noptional argument deletechars are removed, and the remaining\ncharacters have been mapped through the given translation\ntable, which must be a bytes object of length 256."
1365table, which must be a bytes object of length 256.")static char translate__doc__[] = "B.translate(table[, deletechars]) -> bytearray\n\nReturn a copy of B, where all characters occurring in the\noptional argument deletechars are removed, and the remaining\ncharacters have been mapped through the given translation\ntable, which must be a bytes object of length 256.";
1366
1367static PyObject *
1368bytearray_translate(PyByteArrayObject *self, PyObject *args)
1369{
1370 register char *input, *output;
1371 register const char *table;
1372 register Py_ssize_t i, c;
1373 PyObject *input_obj = (PyObject*)self;
1374 const char *output_start;
1375 Py_ssize_t inlen;
1376 PyObject *result = NULL((void*)0);
1377 int trans_table[256];
1378 PyObject *tableobj = NULL((void*)0), *delobj = NULL((void*)0);
1379 Py_buffer vtable, vdel;
1380
1381 if (!PyArg_UnpackTuple(args, "translate", 1, 2,
1382 &tableobj, &delobj))
1383 return NULL((void*)0);
1384
1385 if (tableobj == Py_None(&_Py_NoneStruct)) {
1386 table = NULL((void*)0);
1387 tableobj = NULL((void*)0);
1388 } else if (_getbuffer(tableobj, &vtable) < 0) {
1389 return NULL((void*)0);
1390 } else {
1391 if (vtable.len != 256) {
1392 PyErr_SetString(PyExc_ValueError,
1393 "translation table must be 256 characters long");
1394 PyBuffer_Release(&vtable);
1395 return NULL((void*)0);
1396 }
1397 table = (const char*)vtable.buf;
1398 }
1399
1400 if (delobj != NULL((void*)0)) {
1401 if (_getbuffer(delobj, &vdel) < 0) {
1402 if (tableobj != NULL((void*)0))
1403 PyBuffer_Release(&vtable);
1404 return NULL((void*)0);
1405 }
1406 }
1407 else {
1408 vdel.buf = NULL((void*)0);
1409 vdel.len = 0;
1410 }
1411
1412 inlen = PyByteArray_GET_SIZE(input_obj)((__builtin_expect(!(((((PyObject*)(input_obj))->ob_type) ==
(&PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(input_obj
))->ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn
(__func__, "Objects/bytearrayobject.c", 1412, "PyByteArray_Check(input_obj)"
) : (void)0),(((PyVarObject*)(input_obj))->ob_size))
;
1413 result = PyByteArray_FromStringAndSize((char *)NULL((void*)0), inlen);
1414 if (result == NULL((void*)0))
1415 goto done;
1416 output_start = output = PyByteArray_AsString(result);
1417 input = PyByteArray_AS_STRING(input_obj)((__builtin_expect(!(((((PyObject*)(input_obj))->ob_type) ==
(&PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(input_obj
))->ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn
(__func__, "Objects/bytearrayobject.c", 1417, "PyByteArray_Check(input_obj)"
) : (void)0), (((PyVarObject*)(input_obj))->ob_size) ? ((PyByteArrayObject
*)(input_obj))->ob_bytes : _PyByteArray_empty_string)
;
1418
1419 if (vdel.len == 0 && table != NULL((void*)0)) {
1420 /* If no deletions are required, use faster code */
1421 for (i = inlen; --i >= 0; ) {
1422 c = Py_CHARMASK(*input++)((unsigned char)((*input++) & 0xff));
1423 *output++ = table[c];
1424 }
1425 goto done;
1426 }
1427
1428 if (table == NULL((void*)0)) {
1429 for (i = 0; i < 256; i++)
1430 trans_table[i] = Py_CHARMASK(i)((unsigned char)((i) & 0xff));
1431 } else {
1432 for (i = 0; i < 256; i++)
1433 trans_table[i] = Py_CHARMASK(table[i])((unsigned char)((table[i]) & 0xff));
1434 }
1435
1436 for (i = 0; i < vdel.len; i++)
1437 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )((unsigned char)((((unsigned char*)vdel.buf)[i]) & 0xff))] = -1;
1438
1439 for (i = inlen; --i >= 0; ) {
1440 c = Py_CHARMASK(*input++)((unsigned char)((*input++) & 0xff));
1441 if (trans_table[c] != -1)
1442 if (Py_CHARMASK(*output++ = (char)trans_table[c])((unsigned char)((*output++ = (char)trans_table[c]) & 0xff
))
== c)
1443 continue;
1444 }
1445 /* Fix the size of the resulting string */
1446 if (inlen > 0)
1447 PyByteArray_Resize(result, output - output_start);
1448
1449done:
1450 if (tableobj != NULL((void*)0))
1451 PyBuffer_Release(&vtable);
1452 if (delobj != NULL((void*)0))
1453 PyBuffer_Release(&vdel);
1454 return result;
1455}
1456
1457
1458static PyObject *
1459bytearray_maketrans(PyObject *null, PyObject *args)
1460{
1461 return _Py_bytes_maketrans(args);
1462}
1463
1464
1465/* find and count characters and substrings */
1466
1467#define findchar(target, target_len, c)((char *)memchr((const void *)(target), c, target_len)) \
1468 ((char *)memchr((const void *)(target), c, target_len))
1469
1470
1471/* Bytes ops must return a string, create a copy */
1472Py_LOCAL(PyByteArrayObject *)static PyByteArrayObject *
1473return_self(PyByteArrayObject *self)
1474{
1475 /* always return a new bytearray */
1476 return (PyByteArrayObject *)PyByteArray_FromStringAndSize(
1477 PyByteArray_AS_STRING(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1477, "PyByteArray_Check(self)"
) : (void)0), (((PyVarObject*)(self))->ob_size) ? ((PyByteArrayObject
*)(self))->ob_bytes : _PyByteArray_empty_string)
,
1478 PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1478, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
);
1479}
1480
1481Py_LOCAL_INLINE(Py_ssize_t)static inline Py_ssize_t
1482countchar(const char *target, Py_ssize_t target_len, char c, Py_ssize_t maxcount)
1483{
1484 Py_ssize_t count=0;
1485 const char *start=target;
1486 const char *end=target+target_len;
1487
1488 while ( (start=findchar(start, end-start, c)((char *)memchr((const void *)(start), c, end-start))) != NULL((void*)0) ) {
1489 count++;
1490 if (count >= maxcount)
1491 break;
1492 start += 1;
1493 }
1494 return count;
1495}
1496
1497
1498/* Algorithms for different cases of string replacement */
1499
1500/* len(self)>=1, from="", len(to)>=1, maxcount>=1 */
1501Py_LOCAL(PyByteArrayObject *)static PyByteArrayObject *
1502replace_interleave(PyByteArrayObject *self,
1503 const char *to_s, Py_ssize_t to_len,
1504 Py_ssize_t maxcount)
1505{
1506 char *self_s, *result_s;
1507 Py_ssize_t self_len, result_len;
1508 Py_ssize_t count, i;
1509 PyByteArrayObject *result;
1510
1511 self_len = PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1511, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
;
1512
1513 /* 1 at the end plus 1 after every character;
1514 count = min(maxcount, self_len + 1) */
1515 if (maxcount <= self_len)
1516 count = maxcount;
1517 else
1518 /* Can't overflow: self_len + 1 <= maxcount <= PY_SSIZE_T_MAX. */
1519 count = self_len + 1;
1520
1521 /* Check for overflow */
1522 /* result_len = count * to_len + self_len; */
1523 assert(count > 0)(__builtin_expect(!(count > 0), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1523, "count > 0") : (void)
0)
;
1524 if (to_len > (PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1)) - self_len) / count) {
1525 PyErr_SetString(PyExc_OverflowError,
1526 "replace string is too long");
1527 return NULL((void*)0);
1528 }
1529 result_len = count * to_len + self_len;
1530
1531 if (! (result = (PyByteArrayObject *)
1532 PyByteArray_FromStringAndSize(NULL((void*)0), result_len)) )
1533 return NULL((void*)0);
1534
1535 self_s = PyByteArray_AS_STRING(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1535, "PyByteArray_Check(self)"
) : (void)0), (((PyVarObject*)(self))->ob_size) ? ((PyByteArrayObject
*)(self))->ob_bytes : _PyByteArray_empty_string)
;
1536 result_s = PyByteArray_AS_STRING(result)((__builtin_expect(!(((((PyObject*)(result))->ob_type) == (
&PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(result
))->ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn
(__func__, "Objects/bytearrayobject.c", 1536, "PyByteArray_Check(result)"
) : (void)0), (((PyVarObject*)(result))->ob_size) ? ((PyByteArrayObject
*)(result))->ob_bytes : _PyByteArray_empty_string)
;
1537
1538 /* TODO: special case single character, which doesn't need memcpy */
1539
1540 /* Lay the first one down (guaranteed this will occur) */
1541 Py_MEMCPY(result_s, to_s, to_len)((__builtin_object_size (result_s, 0) != (size_t) -1) ? __builtin___memcpy_chk
(result_s, to_s, to_len, __builtin_object_size (result_s, 0)
) : __inline_memcpy_chk (result_s, to_s, to_len))
;
1542 result_s += to_len;
1543 count -= 1;
1544
1545 for (i=0; i<count; i++) {
1546 *result_s++ = *self_s++;
1547 Py_MEMCPY(result_s, to_s, to_len)((__builtin_object_size (result_s, 0) != (size_t) -1) ? __builtin___memcpy_chk
(result_s, to_s, to_len, __builtin_object_size (result_s, 0)
) : __inline_memcpy_chk (result_s, to_s, to_len))
;
1548 result_s += to_len;
1549 }
1550
1551 /* Copy the rest of the original string */
1552 Py_MEMCPY(result_s, self_s, self_len-i)((__builtin_object_size (result_s, 0) != (size_t) -1) ? __builtin___memcpy_chk
(result_s, self_s, self_len-i, __builtin_object_size (result_s
, 0)) : __inline_memcpy_chk (result_s, self_s, self_len-i))
;
1553
1554 return result;
1555}
1556
1557/* Special case for deleting a single character */
1558/* len(self)>=1, len(from)==1, to="", maxcount>=1 */
1559Py_LOCAL(PyByteArrayObject *)static PyByteArrayObject *
1560replace_delete_single_character(PyByteArrayObject *self,
1561 char from_c, Py_ssize_t maxcount)
1562{
1563 char *self_s, *result_s;
1564 char *start, *next, *end;
1565 Py_ssize_t self_len, result_len;
1566 Py_ssize_t count;
1567 PyByteArrayObject *result;
1568
1569 self_len = PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1569, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
;
1570 self_s = PyByteArray_AS_STRING(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1570, "PyByteArray_Check(self)"
) : (void)0), (((PyVarObject*)(self))->ob_size) ? ((PyByteArrayObject
*)(self))->ob_bytes : _PyByteArray_empty_string)
;
1571
1572 count = countchar(self_s, self_len, from_c, maxcount);
1573 if (count == 0) {
1574 return return_self(self);
1575 }
1576
1577 result_len = self_len - count; /* from_len == 1 */
1578 assert(result_len>=0)(__builtin_expect(!(result_len>=0), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1578, "result_len>=0") : (void
)0)
;
1579
1580 if ( (result = (PyByteArrayObject *)
1581 PyByteArray_FromStringAndSize(NULL((void*)0), result_len)) == NULL((void*)0))
1582 return NULL((void*)0);
1583 result_s = PyByteArray_AS_STRING(result)((__builtin_expect(!(((((PyObject*)(result))->ob_type) == (
&PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(result
))->ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn
(__func__, "Objects/bytearrayobject.c", 1583, "PyByteArray_Check(result)"
) : (void)0), (((PyVarObject*)(result))->ob_size) ? ((PyByteArrayObject
*)(result))->ob_bytes : _PyByteArray_empty_string)
;
1584
1585 start = self_s;
1586 end = self_s + self_len;
1587 while (count-- > 0) {
1588 next = findchar(start, end-start, from_c)((char *)memchr((const void *)(start), from_c, end-start));
1589 if (next == NULL((void*)0))
1590 break;
1591 Py_MEMCPY(result_s, start, next-start)((__builtin_object_size (result_s, 0) != (size_t) -1) ? __builtin___memcpy_chk
(result_s, start, next-start, __builtin_object_size (result_s
, 0)) : __inline_memcpy_chk (result_s, start, next-start))
;
1592 result_s += (next-start);
1593 start = next+1;
1594 }
1595 Py_MEMCPY(result_s, start, end-start)((__builtin_object_size (result_s, 0) != (size_t) -1) ? __builtin___memcpy_chk
(result_s, start, end-start, __builtin_object_size (result_s
, 0)) : __inline_memcpy_chk (result_s, start, end-start))
;
1596
1597 return result;
1598}
1599
1600/* len(self)>=1, len(from)>=2, to="", maxcount>=1 */
1601
1602Py_LOCAL(PyByteArrayObject *)static PyByteArrayObject *
1603replace_delete_substring(PyByteArrayObject *self,
1604 const char *from_s, Py_ssize_t from_len,
1605 Py_ssize_t maxcount)
1606{
1607 char *self_s, *result_s;
1608 char *start, *next, *end;
1609 Py_ssize_t self_len, result_len;
1610 Py_ssize_t count, offset;
1611 PyByteArrayObject *result;
1612
1613 self_len = PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1613, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
;
1614 self_s = PyByteArray_AS_STRING(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1614, "PyByteArray_Check(self)"
) : (void)0), (((PyVarObject*)(self))->ob_size) ? ((PyByteArrayObject
*)(self))->ob_bytes : _PyByteArray_empty_string)
;
1615
1616 count = stringlib_count(self_s, self_len,
1617 from_s, from_len,
1618 maxcount);
1619
1620 if (count == 0) {
1621 /* no matches */
1622 return return_self(self);
1623 }
1624
1625 result_len = self_len - (count * from_len);
1626 assert (result_len>=0)(__builtin_expect(!(result_len>=0), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1626, "result_len>=0") : (void
)0)
;
1627
1628 if ( (result = (PyByteArrayObject *)
1629 PyByteArray_FromStringAndSize(NULL((void*)0), result_len)) == NULL((void*)0) )
1630 return NULL((void*)0);
1631
1632 result_s = PyByteArray_AS_STRING(result)((__builtin_expect(!(((((PyObject*)(result))->ob_type) == (
&PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(result
))->ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn
(__func__, "Objects/bytearrayobject.c", 1632, "PyByteArray_Check(result)"
) : (void)0), (((PyVarObject*)(result))->ob_size) ? ((PyByteArrayObject
*)(result))->ob_bytes : _PyByteArray_empty_string)
;
1633
1634 start = self_s;
1635 end = self_s + self_len;
1636 while (count-- > 0) {
1637 offset = stringlib_find(start, end-start,
1638 from_s, from_len,
1639 0);
1640 if (offset == -1)
1641 break;
1642 next = start + offset;
1643
1644 Py_MEMCPY(result_s, start, next-start)((__builtin_object_size (result_s, 0) != (size_t) -1) ? __builtin___memcpy_chk
(result_s, start, next-start, __builtin_object_size (result_s
, 0)) : __inline_memcpy_chk (result_s, start, next-start))
;
1645
1646 result_s += (next-start);
1647 start = next+from_len;
1648 }
1649 Py_MEMCPY(result_s, start, end-start)((__builtin_object_size (result_s, 0) != (size_t) -1) ? __builtin___memcpy_chk
(result_s, start, end-start, __builtin_object_size (result_s
, 0)) : __inline_memcpy_chk (result_s, start, end-start))
;
1650 return result;
1651}
1652
1653/* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */
1654Py_LOCAL(PyByteArrayObject *)static PyByteArrayObject *
1655replace_single_character_in_place(PyByteArrayObject *self,
1656 char from_c, char to_c,
1657 Py_ssize_t maxcount)
1658{
1659 char *self_s, *result_s, *start, *end, *next;
1660 Py_ssize_t self_len;
1661 PyByteArrayObject *result;
1662
1663 /* The result string will be the same size */
1664 self_s = PyByteArray_AS_STRING(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1664, "PyByteArray_Check(self)"
) : (void)0), (((PyVarObject*)(self))->ob_size) ? ((PyByteArrayObject
*)(self))->ob_bytes : _PyByteArray_empty_string)
;
1665 self_len = PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1665, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
;
1666
1667 next = findchar(self_s, self_len, from_c)((char *)memchr((const void *)(self_s), from_c, self_len));
1668
1669 if (next == NULL((void*)0)) {
1670 /* No matches; return the original bytes */
1671 return return_self(self);
1672 }
1673
1674 /* Need to make a new bytes */
1675 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL((void*)0), self_len);
1676 if (result == NULL((void*)0))
1677 return NULL((void*)0);
1678 result_s = PyByteArray_AS_STRING(result)((__builtin_expect(!(((((PyObject*)(result))->ob_type) == (
&PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(result
))->ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn
(__func__, "Objects/bytearrayobject.c", 1678, "PyByteArray_Check(result)"
) : (void)0), (((PyVarObject*)(result))->ob_size) ? ((PyByteArrayObject
*)(result))->ob_bytes : _PyByteArray_empty_string)
;
1679 Py_MEMCPY(result_s, self_s, self_len)((__builtin_object_size (result_s, 0) != (size_t) -1) ? __builtin___memcpy_chk
(result_s, self_s, self_len, __builtin_object_size (result_s
, 0)) : __inline_memcpy_chk (result_s, self_s, self_len))
;
1680
1681 /* change everything in-place, starting with this one */
1682 start = result_s + (next-self_s);
1683 *start = to_c;
1684 start++;
1685 end = result_s + self_len;
1686
1687 while (--maxcount > 0) {
1688 next = findchar(start, end-start, from_c)((char *)memchr((const void *)(start), from_c, end-start));
1689 if (next == NULL((void*)0))
1690 break;
1691 *next = to_c;
1692 start = next+1;
1693 }
1694
1695 return result;
1696}
1697
1698/* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */
1699Py_LOCAL(PyByteArrayObject *)static PyByteArrayObject *
1700replace_substring_in_place(PyByteArrayObject *self,
1701 const char *from_s, Py_ssize_t from_len,
1702 const char *to_s, Py_ssize_t to_len,
1703 Py_ssize_t maxcount)
1704{
1705 char *result_s, *start, *end;
1706 char *self_s;
1707 Py_ssize_t self_len, offset;
1708 PyByteArrayObject *result;
1709
1710 /* The result bytes will be the same size */
1711
1712 self_s = PyByteArray_AS_STRING(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1712, "PyByteArray_Check(self)"
) : (void)0), (((PyVarObject*)(self))->ob_size) ? ((PyByteArrayObject
*)(self))->ob_bytes : _PyByteArray_empty_string)
;
1713 self_len = PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1713, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
;
1714
1715 offset = stringlib_find(self_s, self_len,
1716 from_s, from_len,
1717 0);
1718 if (offset == -1) {
1719 /* No matches; return the original bytes */
1720 return return_self(self);
1721 }
1722
1723 /* Need to make a new bytes */
1724 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL((void*)0), self_len);
1725 if (result == NULL((void*)0))
1726 return NULL((void*)0);
1727 result_s = PyByteArray_AS_STRING(result)((__builtin_expect(!(((((PyObject*)(result))->ob_type) == (
&PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(result
))->ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn
(__func__, "Objects/bytearrayobject.c", 1727, "PyByteArray_Check(result)"
) : (void)0), (((PyVarObject*)(result))->ob_size) ? ((PyByteArrayObject
*)(result))->ob_bytes : _PyByteArray_empty_string)
;
1728 Py_MEMCPY(result_s, self_s, self_len)((__builtin_object_size (result_s, 0) != (size_t) -1) ? __builtin___memcpy_chk
(result_s, self_s, self_len, __builtin_object_size (result_s
, 0)) : __inline_memcpy_chk (result_s, self_s, self_len))
;
1729
1730 /* change everything in-place, starting with this one */
1731 start = result_s + offset;
1732 Py_MEMCPY(start, to_s, from_len)((__builtin_object_size (start, 0) != (size_t) -1) ? __builtin___memcpy_chk
(start, to_s, from_len, __builtin_object_size (start, 0)) : __inline_memcpy_chk
(start, to_s, from_len))
;
1733 start += from_len;
1734 end = result_s + self_len;
1735
1736 while ( --maxcount > 0) {
1737 offset = stringlib_find(start, end-start,
1738 from_s, from_len,
1739 0);
1740 if (offset==-1)
1741 break;
1742 Py_MEMCPY(start+offset, to_s, from_len)((__builtin_object_size (start+offset, 0) != (size_t) -1) ? __builtin___memcpy_chk
(start+offset, to_s, from_len, __builtin_object_size (start+
offset, 0)) : __inline_memcpy_chk (start+offset, to_s, from_len
))
;
1743 start += offset+from_len;
1744 }
1745
1746 return result;
1747}
1748
1749/* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */
1750Py_LOCAL(PyByteArrayObject *)static PyByteArrayObject *
1751replace_single_character(PyByteArrayObject *self,
1752 char from_c,
1753 const char *to_s, Py_ssize_t to_len,
1754 Py_ssize_t maxcount)
1755{
1756 char *self_s, *result_s;
1757 char *start, *next, *end;
1758 Py_ssize_t self_len, result_len;
1759 Py_ssize_t count;
1760 PyByteArrayObject *result;
1761
1762 self_s = PyByteArray_AS_STRING(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1762, "PyByteArray_Check(self)"
) : (void)0), (((PyVarObject*)(self))->ob_size) ? ((PyByteArrayObject
*)(self))->ob_bytes : _PyByteArray_empty_string)
;
1763 self_len = PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1763, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
;
1764
1765 count = countchar(self_s, self_len, from_c, maxcount);
1766 if (count == 0) {
1767 /* no matches, return unchanged */
1768 return return_self(self);
1769 }
1770
1771 /* use the difference between current and new, hence the "-1" */
1772 /* result_len = self_len + count * (to_len-1) */
1773 assert(count > 0)(__builtin_expect(!(count > 0), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1773, "count > 0") : (void)
0)
;
1774 if (to_len - 1 > (PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1)) - self_len) / count) {
1775 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
1776 return NULL((void*)0);
1777 }
1778 result_len = self_len + count * (to_len - 1);
1779
1780 if ( (result = (PyByteArrayObject *)
1781 PyByteArray_FromStringAndSize(NULL((void*)0), result_len)) == NULL((void*)0))
1782 return NULL((void*)0);
1783 result_s = PyByteArray_AS_STRING(result)((__builtin_expect(!(((((PyObject*)(result))->ob_type) == (
&PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(result
))->ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn
(__func__, "Objects/bytearrayobject.c", 1783, "PyByteArray_Check(result)"
) : (void)0), (((PyVarObject*)(result))->ob_size) ? ((PyByteArrayObject
*)(result))->ob_bytes : _PyByteArray_empty_string)
;
1784
1785 start = self_s;
1786 end = self_s + self_len;
1787 while (count-- > 0) {
1788 next = findchar(start, end-start, from_c)((char *)memchr((const void *)(start), from_c, end-start));
1789 if (next == NULL((void*)0))
1790 break;
1791
1792 if (next == start) {
1793 /* replace with the 'to' */
1794 Py_MEMCPY(result_s, to_s, to_len)((__builtin_object_size (result_s, 0) != (size_t) -1) ? __builtin___memcpy_chk
(result_s, to_s, to_len, __builtin_object_size (result_s, 0)
) : __inline_memcpy_chk (result_s, to_s, to_len))
;
1795 result_s += to_len;
1796 start += 1;
1797 } else {
1798 /* copy the unchanged old then the 'to' */
1799 Py_MEMCPY(result_s, start, next-start)((__builtin_object_size (result_s, 0) != (size_t) -1) ? __builtin___memcpy_chk
(result_s, start, next-start, __builtin_object_size (result_s
, 0)) : __inline_memcpy_chk (result_s, start, next-start))
;
1800 result_s += (next-start);
1801 Py_MEMCPY(result_s, to_s, to_len)((__builtin_object_size (result_s, 0) != (size_t) -1) ? __builtin___memcpy_chk
(result_s, to_s, to_len, __builtin_object_size (result_s, 0)
) : __inline_memcpy_chk (result_s, to_s, to_len))
;
1802 result_s += to_len;
1803 start = next+1;
1804 }
1805 }
1806 /* Copy the remainder of the remaining bytes */
1807 Py_MEMCPY(result_s, start, end-start)((__builtin_object_size (result_s, 0) != (size_t) -1) ? __builtin___memcpy_chk
(result_s, start, end-start, __builtin_object_size (result_s
, 0)) : __inline_memcpy_chk (result_s, start, end-start))
;
1808
1809 return result;
1810}
1811
1812/* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */
1813Py_LOCAL(PyByteArrayObject *)static PyByteArrayObject *
1814replace_substring(PyByteArrayObject *self,
1815 const char *from_s, Py_ssize_t from_len,
1816 const char *to_s, Py_ssize_t to_len,
1817 Py_ssize_t maxcount)
1818{
1819 char *self_s, *result_s;
1820 char *start, *next, *end;
1821 Py_ssize_t self_len, result_len;
1822 Py_ssize_t count, offset;
1823 PyByteArrayObject *result;
1824
1825 self_s = PyByteArray_AS_STRING(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1825, "PyByteArray_Check(self)"
) : (void)0), (((PyVarObject*)(self))->ob_size) ? ((PyByteArrayObject
*)(self))->ob_bytes : _PyByteArray_empty_string)
;
1826 self_len = PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1826, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
;
1827
1828 count = stringlib_count(self_s, self_len,
1829 from_s, from_len,
1830 maxcount);
1831
1832 if (count == 0) {
1833 /* no matches, return unchanged */
1834 return return_self(self);
1835 }
1836
1837 /* Check for overflow */
1838 /* result_len = self_len + count * (to_len-from_len) */
1839 assert(count > 0)(__builtin_expect(!(count > 0), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1839, "count > 0") : (void)
0)
;
1840 if (to_len - from_len > (PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1)) - self_len) / count) {
1841 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
1842 return NULL((void*)0);
1843 }
1844 result_len = self_len + count * (to_len - from_len);
1845
1846 if ( (result = (PyByteArrayObject *)
1847 PyByteArray_FromStringAndSize(NULL((void*)0), result_len)) == NULL((void*)0))
1848 return NULL((void*)0);
1849 result_s = PyByteArray_AS_STRING(result)((__builtin_expect(!(((((PyObject*)(result))->ob_type) == (
&PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(result
))->ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn
(__func__, "Objects/bytearrayobject.c", 1849, "PyByteArray_Check(result)"
) : (void)0), (((PyVarObject*)(result))->ob_size) ? ((PyByteArrayObject
*)(result))->ob_bytes : _PyByteArray_empty_string)
;
1850
1851 start = self_s;
1852 end = self_s + self_len;
1853 while (count-- > 0) {
1854 offset = stringlib_find(start, end-start,
1855 from_s, from_len,
1856 0);
1857 if (offset == -1)
1858 break;
1859 next = start+offset;
1860 if (next == start) {
1861 /* replace with the 'to' */
1862 Py_MEMCPY(result_s, to_s, to_len)((__builtin_object_size (result_s, 0) != (size_t) -1) ? __builtin___memcpy_chk
(result_s, to_s, to_len, __builtin_object_size (result_s, 0)
) : __inline_memcpy_chk (result_s, to_s, to_len))
;
1863 result_s += to_len;
1864 start += from_len;
1865 } else {
1866 /* copy the unchanged old then the 'to' */
1867 Py_MEMCPY(result_s, start, next-start)((__builtin_object_size (result_s, 0) != (size_t) -1) ? __builtin___memcpy_chk
(result_s, start, next-start, __builtin_object_size (result_s
, 0)) : __inline_memcpy_chk (result_s, start, next-start))
;
1868 result_s += (next-start);
1869 Py_MEMCPY(result_s, to_s, to_len)((__builtin_object_size (result_s, 0) != (size_t) -1) ? __builtin___memcpy_chk
(result_s, to_s, to_len, __builtin_object_size (result_s, 0)
) : __inline_memcpy_chk (result_s, to_s, to_len))
;
1870 result_s += to_len;
1871 start = next+from_len;
1872 }
1873 }
1874 /* Copy the remainder of the remaining bytes */
1875 Py_MEMCPY(result_s, start, end-start)((__builtin_object_size (result_s, 0) != (size_t) -1) ? __builtin___memcpy_chk
(result_s, start, end-start, __builtin_object_size (result_s
, 0)) : __inline_memcpy_chk (result_s, start, end-start))
;
1876
1877 return result;
1878}
1879
1880
1881Py_LOCAL(PyByteArrayObject *)static PyByteArrayObject *
1882replace(PyByteArrayObject *self,
1883 const char *from_s, Py_ssize_t from_len,
1884 const char *to_s, Py_ssize_t to_len,
1885 Py_ssize_t maxcount)
1886{
1887 if (maxcount < 0) {
1888 maxcount = PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1));
1889 } else if (maxcount == 0 || PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1889, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
== 0) {
1890 /* nothing to do; return the original bytes */
1891 return return_self(self);
1892 }
1893
1894 if (maxcount == 0 ||
1895 (from_len == 0 && to_len == 0)) {
1896 /* nothing to do; return the original bytes */
1897 return return_self(self);
1898 }
1899
1900 /* Handle zero-length special cases */
1901
1902 if (from_len == 0) {
1903 /* insert the 'to' bytes everywhere. */
1904 /* >>> "Python".replace("", ".") */
1905 /* '.P.y.t.h.o.n.' */
1906 return replace_interleave(self, to_s, to_len, maxcount);
1907 }
1908
1909 /* Except for "".replace("", "A") == "A" there is no way beyond this */
1910 /* point for an empty self bytes to generate a non-empty bytes */
1911 /* Special case so the remaining code always gets a non-empty bytes */
1912 if (PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1912, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
== 0) {
1913 return return_self(self);
1914 }
1915
1916 if (to_len == 0) {
1917 /* delete all occurrences of 'from' bytes */
1918 if (from_len == 1) {
1919 return replace_delete_single_character(
1920 self, from_s[0], maxcount);
1921 } else {
1922 return replace_delete_substring(self, from_s, from_len, maxcount);
1923 }
1924 }
1925
1926 /* Handle special case where both bytes have the same length */
1927
1928 if (from_len == to_len) {
1929 if (from_len == 1) {
1930 return replace_single_character_in_place(
1931 self,
1932 from_s[0],
1933 to_s[0],
1934 maxcount);
1935 } else {
1936 return replace_substring_in_place(
1937 self, from_s, from_len, to_s, to_len, maxcount);
1938 }
1939 }
1940
1941 /* Otherwise use the more generic algorithms */
1942 if (from_len == 1) {
1943 return replace_single_character(self, from_s[0],
1944 to_s, to_len, maxcount);
1945 } else {
1946 /* len('from')>=2, len('to')>=1 */
1947 return replace_substring(self, from_s, from_len, to_s, to_len, maxcount);
1948 }
1949}
1950
1951
1952PyDoc_STRVAR(replace__doc__,static char replace__doc__[] = "B.replace(old, new[, count]) -> bytearray\n\nReturn a copy of B with all occurrences of subsection\nold replaced by new. If the optional argument count is\ngiven, only the first count occurrences are replaced."
1953"B.replace(old, new[, count]) -> bytearray\n\static char replace__doc__[] = "B.replace(old, new[, count]) -> bytearray\n\nReturn a copy of B with all occurrences of subsection\nold replaced by new. If the optional argument count is\ngiven, only the first count occurrences are replaced."
1954\n\static char replace__doc__[] = "B.replace(old, new[, count]) -> bytearray\n\nReturn a copy of B with all occurrences of subsection\nold replaced by new. If the optional argument count is\ngiven, only the first count occurrences are replaced."
1955Return a copy of B with all occurrences of subsection\n\static char replace__doc__[] = "B.replace(old, new[, count]) -> bytearray\n\nReturn a copy of B with all occurrences of subsection\nold replaced by new. If the optional argument count is\ngiven, only the first count occurrences are replaced."
1956old replaced by new. If the optional argument count is\n\static char replace__doc__[] = "B.replace(old, new[, count]) -> bytearray\n\nReturn a copy of B with all occurrences of subsection\nold replaced by new. If the optional argument count is\ngiven, only the first count occurrences are replaced."
1957given, only the first count occurrences are replaced.")static char replace__doc__[] = "B.replace(old, new[, count]) -> bytearray\n\nReturn a copy of B with all occurrences of subsection\nold replaced by new. If the optional argument count is\ngiven, only the first count occurrences are replaced.";
1958
1959static PyObject *
1960bytearray_replace(PyByteArrayObject *self, PyObject *args)
1961{
1962 Py_ssize_t count = -1;
1963 PyObject *from, *to, *res;
1964 Py_buffer vfrom, vto;
1965
1966 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "OO|n:replace", &from, &to, &count))
1967 return NULL((void*)0);
1968
1969 if (_getbuffer(from, &vfrom) < 0)
1970 return NULL((void*)0);
1971 if (_getbuffer(to, &vto) < 0) {
1972 PyBuffer_Release(&vfrom);
1973 return NULL((void*)0);
1974 }
1975
1976 res = (PyObject *)replace((PyByteArrayObject *) self,
1977 vfrom.buf, vfrom.len,
1978 vto.buf, vto.len, count);
1979
1980 PyBuffer_Release(&vfrom);
1981 PyBuffer_Release(&vto);
1982 return res;
1983}
1984
1985PyDoc_STRVAR(split__doc__,static char split__doc__[] = "B.split([sep[, maxsplit]]) -> list of bytearrays\n\nReturn a list of the sections in B, using sep as the delimiter.\nIf sep is not given, B is split on ASCII whitespace characters\n(space, tab, return, newline, formfeed, vertical tab).\nIf maxsplit is given, at most maxsplit splits are done."
1986"B.split([sep[, maxsplit]]) -> list of bytearrays\n\static char split__doc__[] = "B.split([sep[, maxsplit]]) -> list of bytearrays\n\nReturn a list of the sections in B, using sep as the delimiter.\nIf sep is not given, B is split on ASCII whitespace characters\n(space, tab, return, newline, formfeed, vertical tab).\nIf maxsplit is given, at most maxsplit splits are done."
1987\n\static char split__doc__[] = "B.split([sep[, maxsplit]]) -> list of bytearrays\n\nReturn a list of the sections in B, using sep as the delimiter.\nIf sep is not given, B is split on ASCII whitespace characters\n(space, tab, return, newline, formfeed, vertical tab).\nIf maxsplit is given, at most maxsplit splits are done."
1988Return a list of the sections in B, using sep as the delimiter.\n\static char split__doc__[] = "B.split([sep[, maxsplit]]) -> list of bytearrays\n\nReturn a list of the sections in B, using sep as the delimiter.\nIf sep is not given, B is split on ASCII whitespace characters\n(space, tab, return, newline, formfeed, vertical tab).\nIf maxsplit is given, at most maxsplit splits are done."
1989If sep is not given, B is split on ASCII whitespace characters\n\static char split__doc__[] = "B.split([sep[, maxsplit]]) -> list of bytearrays\n\nReturn a list of the sections in B, using sep as the delimiter.\nIf sep is not given, B is split on ASCII whitespace characters\n(space, tab, return, newline, formfeed, vertical tab).\nIf maxsplit is given, at most maxsplit splits are done."
1990(space, tab, return, newline, formfeed, vertical tab).\n\static char split__doc__[] = "B.split([sep[, maxsplit]]) -> list of bytearrays\n\nReturn a list of the sections in B, using sep as the delimiter.\nIf sep is not given, B is split on ASCII whitespace characters\n(space, tab, return, newline, formfeed, vertical tab).\nIf maxsplit is given, at most maxsplit splits are done."
1991If maxsplit is given, at most maxsplit splits are done.")static char split__doc__[] = "B.split([sep[, maxsplit]]) -> list of bytearrays\n\nReturn a list of the sections in B, using sep as the delimiter.\nIf sep is not given, B is split on ASCII whitespace characters\n(space, tab, return, newline, formfeed, vertical tab).\nIf maxsplit is given, at most maxsplit splits are done.";
1992
1993static PyObject *
1994bytearray_split(PyByteArrayObject *self, PyObject *args)
1995{
1996 Py_ssize_t len = PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1996, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
, n;
1997 Py_ssize_t maxsplit = -1;
1998 const char *s = PyByteArray_AS_STRING(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 1998, "PyByteArray_Check(self)"
) : (void)0), (((PyVarObject*)(self))->ob_size) ? ((PyByteArrayObject
*)(self))->ob_bytes : _PyByteArray_empty_string)
, *sub;
1999 PyObject *list, *subobj = Py_None(&_Py_NoneStruct);
2000 Py_buffer vsub;
2001
2002 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "|On:split", &subobj, &maxsplit))
2003 return NULL((void*)0);
2004 if (maxsplit < 0)
2005 maxsplit = PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1));
2006
2007 if (subobj == Py_None(&_Py_NoneStruct))
2008 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
2009
2010 if (_getbuffer(subobj, &vsub) < 0)
2011 return NULL((void*)0);
2012 sub = vsub.buf;
2013 n = vsub.len;
2014
2015 list = stringlib_split(
2016 (PyObject*) self, s, len, sub, n, maxsplit
2017 );
2018 PyBuffer_Release(&vsub);
2019 return list;
2020}
2021
2022PyDoc_STRVAR(partition__doc__,static char partition__doc__[] = "B.partition(sep) -> (head, sep, tail)\n\nSearch for the separator sep in B, and return the part before it,\nthe separator itself, and the part after it. If the separator is not\nfound, returns B and two empty bytearray objects."
2023"B.partition(sep) -> (head, sep, tail)\n\static char partition__doc__[] = "B.partition(sep) -> (head, sep, tail)\n\nSearch for the separator sep in B, and return the part before it,\nthe separator itself, and the part after it. If the separator is not\nfound, returns B and two empty bytearray objects."
2024\n\static char partition__doc__[] = "B.partition(sep) -> (head, sep, tail)\n\nSearch for the separator sep in B, and return the part before it,\nthe separator itself, and the part after it. If the separator is not\nfound, returns B and two empty bytearray objects."
2025Search for the separator sep in B, and return the part before it,\n\static char partition__doc__[] = "B.partition(sep) -> (head, sep, tail)\n\nSearch for the separator sep in B, and return the part before it,\nthe separator itself, and the part after it. If the separator is not\nfound, returns B and two empty bytearray objects."
2026the separator itself, and the part after it. If the separator is not\n\static char partition__doc__[] = "B.partition(sep) -> (head, sep, tail)\n\nSearch for the separator sep in B, and return the part before it,\nthe separator itself, and the part after it. If the separator is not\nfound, returns B and two empty bytearray objects."
2027found, returns B and two empty bytearray objects.")static char partition__doc__[] = "B.partition(sep) -> (head, sep, tail)\n\nSearch for the separator sep in B, and return the part before it,\nthe separator itself, and the part after it. If the separator is not\nfound, returns B and two empty bytearray objects.";
2028
2029static PyObject *
2030bytearray_partition(PyByteArrayObject *self, PyObject *sep_obj)
2031{
2032 PyObject *bytesep, *result;
2033
2034 bytesep = PyByteArray_FromObject(sep_obj);
2035 if (! bytesep)
2036 return NULL((void*)0);
2037
2038 result = stringlib_partition(
2039 (PyObject*) self,
2040 PyByteArray_AS_STRING(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 2040, "PyByteArray_Check(self)"
) : (void)0), (((PyVarObject*)(self))->ob_size) ? ((PyByteArrayObject
*)(self))->ob_bytes : _PyByteArray_empty_string)
, PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 2040, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
,
2041 bytesep,
2042 PyByteArray_AS_STRING(bytesep)((__builtin_expect(!(((((PyObject*)(bytesep))->ob_type) ==
(&PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(bytesep
))->ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn
(__func__, "Objects/bytearrayobject.c", 2042, "PyByteArray_Check(bytesep)"
) : (void)0), (((PyVarObject*)(bytesep))->ob_size) ? ((PyByteArrayObject
*)(bytesep))->ob_bytes : _PyByteArray_empty_string)
, PyByteArray_GET_SIZE(bytesep)((__builtin_expect(!(((((PyObject*)(bytesep))->ob_type) ==
(&PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(bytesep
))->ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn
(__func__, "Objects/bytearrayobject.c", 2042, "PyByteArray_Check(bytesep)"
) : (void)0),(((PyVarObject*)(bytesep))->ob_size))
2043 );
2044
2045 Py_DECREF(bytesep)do { if (_Py_RefTotal-- , --((PyObject*)(bytesep))->ob_refcnt
!= 0) { if (((PyObject*)bytesep)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/bytearrayobject.c", 2045, (PyObject *)(bytesep)); }
else _Py_Dealloc((PyObject *)(bytesep)); } while (0)
;
2046 return result;
2047}
2048
2049PyDoc_STRVAR(rpartition__doc__,static char rpartition__doc__[] = "B.rpartition(sep) -> (head, sep, tail)\n\nSearch for the separator sep in B, starting at the end of B,\nand return the part before it, the separator itself, and the\npart after it. If the separator is not found, returns two empty\nbytearray objects and B."
2050"B.rpartition(sep) -> (head, sep, tail)\n\static char rpartition__doc__[] = "B.rpartition(sep) -> (head, sep, tail)\n\nSearch for the separator sep in B, starting at the end of B,\nand return the part before it, the separator itself, and the\npart after it. If the separator is not found, returns two empty\nbytearray objects and B."
2051\n\static char rpartition__doc__[] = "B.rpartition(sep) -> (head, sep, tail)\n\nSearch for the separator sep in B, starting at the end of B,\nand return the part before it, the separator itself, and the\npart after it. If the separator is not found, returns two empty\nbytearray objects and B."
2052Search for the separator sep in B, starting at the end of B,\n\static char rpartition__doc__[] = "B.rpartition(sep) -> (head, sep, tail)\n\nSearch for the separator sep in B, starting at the end of B,\nand return the part before it, the separator itself, and the\npart after it. If the separator is not found, returns two empty\nbytearray objects and B."
2053and return the part before it, the separator itself, and the\n\static char rpartition__doc__[] = "B.rpartition(sep) -> (head, sep, tail)\n\nSearch for the separator sep in B, starting at the end of B,\nand return the part before it, the separator itself, and the\npart after it. If the separator is not found, returns two empty\nbytearray objects and B."
2054part after it. If the separator is not found, returns two empty\n\static char rpartition__doc__[] = "B.rpartition(sep) -> (head, sep, tail)\n\nSearch for the separator sep in B, starting at the end of B,\nand return the part before it, the separator itself, and the\npart after it. If the separator is not found, returns two empty\nbytearray objects and B."
2055bytearray objects and B.")static char rpartition__doc__[] = "B.rpartition(sep) -> (head, sep, tail)\n\nSearch for the separator sep in B, starting at the end of B,\nand return the part before it, the separator itself, and the\npart after it. If the separator is not found, returns two empty\nbytearray objects and B.";
2056
2057static PyObject *
2058bytearray_rpartition(PyByteArrayObject *self, PyObject *sep_obj)
2059{
2060 PyObject *bytesep, *result;
2061
2062 bytesep = PyByteArray_FromObject(sep_obj);
2063 if (! bytesep)
2064 return NULL((void*)0);
2065
2066 result = stringlib_rpartition(
2067 (PyObject*) self,
2068 PyByteArray_AS_STRING(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 2068, "PyByteArray_Check(self)"
) : (void)0), (((PyVarObject*)(self))->ob_size) ? ((PyByteArrayObject
*)(self))->ob_bytes : _PyByteArray_empty_string)
, PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 2068, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
,
2069 bytesep,
2070 PyByteArray_AS_STRING(bytesep)((__builtin_expect(!(((((PyObject*)(bytesep))->ob_type) ==
(&PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(bytesep
))->ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn
(__func__, "Objects/bytearrayobject.c", 2070, "PyByteArray_Check(bytesep)"
) : (void)0), (((PyVarObject*)(bytesep))->ob_size) ? ((PyByteArrayObject
*)(bytesep))->ob_bytes : _PyByteArray_empty_string)
, PyByteArray_GET_SIZE(bytesep)((__builtin_expect(!(((((PyObject*)(bytesep))->ob_type) ==
(&PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(bytesep
))->ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn
(__func__, "Objects/bytearrayobject.c", 2070, "PyByteArray_Check(bytesep)"
) : (void)0),(((PyVarObject*)(bytesep))->ob_size))
2071 );
2072
2073 Py_DECREF(bytesep)do { if (_Py_RefTotal-- , --((PyObject*)(bytesep))->ob_refcnt
!= 0) { if (((PyObject*)bytesep)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/bytearrayobject.c", 2073, (PyObject *)(bytesep)); }
else _Py_Dealloc((PyObject *)(bytesep)); } while (0)
;
2074 return result;
2075}
2076
2077PyDoc_STRVAR(rsplit__doc__,static char rsplit__doc__[] = "B.rsplit(sep[, maxsplit]) -> list of bytearrays\n\nReturn a list of the sections in B, using sep as the delimiter,\nstarting at the end of B and working to the front.\nIf sep is not given, B is split on ASCII whitespace characters\n(space, tab, return, newline, formfeed, vertical tab).\nIf maxsplit is given, at most maxsplit splits are done."
2078"B.rsplit(sep[, maxsplit]) -> list of bytearrays\n\static char rsplit__doc__[] = "B.rsplit(sep[, maxsplit]) -> list of bytearrays\n\nReturn a list of the sections in B, using sep as the delimiter,\nstarting at the end of B and working to the front.\nIf sep is not given, B is split on ASCII whitespace characters\n(space, tab, return, newline, formfeed, vertical tab).\nIf maxsplit is given, at most maxsplit splits are done."
2079\n\static char rsplit__doc__[] = "B.rsplit(sep[, maxsplit]) -> list of bytearrays\n\nReturn a list of the sections in B, using sep as the delimiter,\nstarting at the end of B and working to the front.\nIf sep is not given, B is split on ASCII whitespace characters\n(space, tab, return, newline, formfeed, vertical tab).\nIf maxsplit is given, at most maxsplit splits are done."
2080Return a list of the sections in B, using sep as the delimiter,\n\static char rsplit__doc__[] = "B.rsplit(sep[, maxsplit]) -> list of bytearrays\n\nReturn a list of the sections in B, using sep as the delimiter,\nstarting at the end of B and working to the front.\nIf sep is not given, B is split on ASCII whitespace characters\n(space, tab, return, newline, formfeed, vertical tab).\nIf maxsplit is given, at most maxsplit splits are done."
2081starting at the end of B and working to the front.\n\static char rsplit__doc__[] = "B.rsplit(sep[, maxsplit]) -> list of bytearrays\n\nReturn a list of the sections in B, using sep as the delimiter,\nstarting at the end of B and working to the front.\nIf sep is not given, B is split on ASCII whitespace characters\n(space, tab, return, newline, formfeed, vertical tab).\nIf maxsplit is given, at most maxsplit splits are done."
2082If sep is not given, B is split on ASCII whitespace characters\n\static char rsplit__doc__[] = "B.rsplit(sep[, maxsplit]) -> list of bytearrays\n\nReturn a list of the sections in B, using sep as the delimiter,\nstarting at the end of B and working to the front.\nIf sep is not given, B is split on ASCII whitespace characters\n(space, tab, return, newline, formfeed, vertical tab).\nIf maxsplit is given, at most maxsplit splits are done."
2083(space, tab, return, newline, formfeed, vertical tab).\n\static char rsplit__doc__[] = "B.rsplit(sep[, maxsplit]) -> list of bytearrays\n\nReturn a list of the sections in B, using sep as the delimiter,\nstarting at the end of B and working to the front.\nIf sep is not given, B is split on ASCII whitespace characters\n(space, tab, return, newline, formfeed, vertical tab).\nIf maxsplit is given, at most maxsplit splits are done."
2084If maxsplit is given, at most maxsplit splits are done.")static char rsplit__doc__[] = "B.rsplit(sep[, maxsplit]) -> list of bytearrays\n\nReturn a list of the sections in B, using sep as the delimiter,\nstarting at the end of B and working to the front.\nIf sep is not given, B is split on ASCII whitespace characters\n(space, tab, return, newline, formfeed, vertical tab).\nIf maxsplit is given, at most maxsplit splits are done.";
2085
2086static PyObject *
2087bytearray_rsplit(PyByteArrayObject *self, PyObject *args)
2088{
2089 Py_ssize_t len = PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 2089, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
, n;
2090 Py_ssize_t maxsplit = -1;
2091 const char *s = PyByteArray_AS_STRING(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 2091, "PyByteArray_Check(self)"
) : (void)0), (((PyVarObject*)(self))->ob_size) ? ((PyByteArrayObject
*)(self))->ob_bytes : _PyByteArray_empty_string)
, *sub;
2092 PyObject *list, *subobj = Py_None(&_Py_NoneStruct);
2093 Py_buffer vsub;
2094
2095 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "|On:rsplit", &subobj, &maxsplit))
2096 return NULL((void*)0);
2097 if (maxsplit < 0)
2098 maxsplit = PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1));
2099
2100 if (subobj == Py_None(&_Py_NoneStruct))
2101 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
2102
2103 if (_getbuffer(subobj, &vsub) < 0)
2104 return NULL((void*)0);
2105 sub = vsub.buf;
2106 n = vsub.len;
2107
2108 list = stringlib_rsplit(
2109 (PyObject*) self, s, len, sub, n, maxsplit
2110 );
2111 PyBuffer_Release(&vsub);
2112 return list;
2113}
2114
2115PyDoc_STRVAR(reverse__doc__,static char reverse__doc__[] = "B.reverse() -> None\n\nReverse the order of the values in B in place."
2116"B.reverse() -> None\n\static char reverse__doc__[] = "B.reverse() -> None\n\nReverse the order of the values in B in place."
2117\n\static char reverse__doc__[] = "B.reverse() -> None\n\nReverse the order of the values in B in place."
2118Reverse the order of the values in B in place.")static char reverse__doc__[] = "B.reverse() -> None\n\nReverse the order of the values in B in place.";
2119static PyObject *
2120bytearray_reverse(PyByteArrayObject *self, PyObject *unused)
2121{
2122 char swap, *head, *tail;
2123 Py_ssize_t i, j, n = Py_SIZE(self)(((PyVarObject*)(self))->ob_size);
2124
2125 j = n / 2;
2126 head = self->ob_bytes;
2127 tail = head + n - 1;
2128 for (i = 0; i < j; i++) {
2129 swap = *head;
2130 *head++ = *tail;
2131 *tail-- = swap;
2132 }
2133
2134 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
2135}
2136
2137PyDoc_STRVAR(insert__doc__,static char insert__doc__[] = "B.insert(index, int) -> None\n\nInsert a single item into the bytearray before the given index."
2138"B.insert(index, int) -> None\n\static char insert__doc__[] = "B.insert(index, int) -> None\n\nInsert a single item into the bytearray before the given index."
2139\n\static char insert__doc__[] = "B.insert(index, int) -> None\n\nInsert a single item into the bytearray before the given index."
2140Insert a single item into the bytearray before the given index.")static char insert__doc__[] = "B.insert(index, int) -> None\n\nInsert a single item into the bytearray before the given index.";
2141static PyObject *
2142bytearray_insert(PyByteArrayObject *self, PyObject *args)
2143{
2144 PyObject *value;
2145 int ival;
2146 Py_ssize_t where, n = Py_SIZE(self)(((PyVarObject*)(self))->ob_size);
2147
2148 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "nO:insert", &where, &value))
2149 return NULL((void*)0);
2150
2151 if (n == PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1))) {
2152 PyErr_SetString(PyExc_OverflowError,
2153 "cannot add more objects to bytearray");
2154 return NULL((void*)0);
2155 }
2156 if (!_getbytevalue(value, &ival))
2157 return NULL((void*)0);
2158 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2159 return NULL((void*)0);
2160
2161 if (where < 0) {
2162 where += n;
2163 if (where < 0)
2164 where = 0;
2165 }
2166 if (where > n)
2167 where = n;
2168 memmove(self->ob_bytes + where + 1, self->ob_bytes + where, n - where)((__builtin_object_size (self->ob_bytes + where + 1, 0) !=
(size_t) -1) ? __builtin___memmove_chk (self->ob_bytes + where
+ 1, self->ob_bytes + where, n - where, __builtin_object_size
(self->ob_bytes + where + 1, 0)) : __inline_memmove_chk (
self->ob_bytes + where + 1, self->ob_bytes + where, n -
where))
;
2169 self->ob_bytes[where] = ival;
2170
2171 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
2172}
2173
2174PyDoc_STRVAR(append__doc__,static char append__doc__[] = "B.append(int) -> None\n\nAppend a single item to the end of B."
2175"B.append(int) -> None\n\static char append__doc__[] = "B.append(int) -> None\n\nAppend a single item to the end of B."
2176\n\static char append__doc__[] = "B.append(int) -> None\n\nAppend a single item to the end of B."
2177Append a single item to the end of B.")static char append__doc__[] = "B.append(int) -> None\n\nAppend a single item to the end of B.";
2178static PyObject *
2179bytearray_append(PyByteArrayObject *self, PyObject *arg)
2180{
2181 int value;
2182 Py_ssize_t n = Py_SIZE(self)(((PyVarObject*)(self))->ob_size);
2183
2184 if (! _getbytevalue(arg, &value))
2185 return NULL((void*)0);
2186 if (n == PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1))) {
2187 PyErr_SetString(PyExc_OverflowError,
2188 "cannot add more objects to bytearray");
2189 return NULL((void*)0);
2190 }
2191 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2192 return NULL((void*)0);
2193
2194 self->ob_bytes[n] = value;
2195
2196 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
2197}
2198
2199PyDoc_STRVAR(extend__doc__,static char extend__doc__[] = "B.extend(iterable_of_ints) -> None\n\nAppend all the elements from the iterator or sequence to the\nend of B."
2200"B.extend(iterable_of_ints) -> None\n\static char extend__doc__[] = "B.extend(iterable_of_ints) -> None\n\nAppend all the elements from the iterator or sequence to the\nend of B."
2201\n\static char extend__doc__[] = "B.extend(iterable_of_ints) -> None\n\nAppend all the elements from the iterator or sequence to the\nend of B."
2202Append all the elements from the iterator or sequence to the\n\static char extend__doc__[] = "B.extend(iterable_of_ints) -> None\n\nAppend all the elements from the iterator or sequence to the\nend of B."
2203end of B.")static char extend__doc__[] = "B.extend(iterable_of_ints) -> None\n\nAppend all the elements from the iterator or sequence to the\nend of B.";
2204static PyObject *
2205bytearray_extend(PyByteArrayObject *self, PyObject *arg)
2206{
2207 PyObject *it, *item, *bytearray_obj;
2208 Py_ssize_t buf_size = 0, len = 0;
2209 int value;
2210 char *buf;
2211
2212 /* bytearray_setslice code only accepts something supporting PEP 3118. */
2213 if (PyObject_CheckBuffer(arg)(((arg)->ob_type->tp_as_buffer != ((void*)0)) &&
((arg)->ob_type->tp_as_buffer->bf_getbuffer != ((void
*)0)))
) {
2214 if (bytearray_setslice(self, Py_SIZE(self)(((PyVarObject*)(self))->ob_size), Py_SIZE(self)(((PyVarObject*)(self))->ob_size), arg) == -1)
2215 return NULL((void*)0);
2216
2217 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
2218 }
2219
2220 it = PyObject_GetIter(arg);
2221 if (it == NULL((void*)0))
2222 return NULL((void*)0);
2223
2224 /* Try to determine the length of the argument. 32 is abitrary. */
2225 buf_size = _PyObject_LengthHint(arg, 32);
2226 if (buf_size == -1) {
2227 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/bytearrayobject.c", 2227, (PyObject *)(it)); } else
_Py_Dealloc((PyObject *)(it)); } while (0)
;
2228 return NULL((void*)0);
2229 }
2230
2231 bytearray_obj = PyByteArray_FromStringAndSize(NULL((void*)0), buf_size);
2232 if (bytearray_obj == NULL((void*)0))
2233 return NULL((void*)0);
2234 buf = PyByteArray_AS_STRING(bytearray_obj)((__builtin_expect(!(((((PyObject*)(bytearray_obj))->ob_type
) == (&PyByteArray_Type) || PyType_IsSubtype((((PyObject*
)(bytearray_obj))->ob_type), (&PyByteArray_Type)))), 0
) ? __assert_rtn(__func__, "Objects/bytearrayobject.c", 2234,
"PyByteArray_Check(bytearray_obj)") : (void)0), (((PyVarObject
*)(bytearray_obj))->ob_size) ? ((PyByteArrayObject *)(bytearray_obj
))->ob_bytes : _PyByteArray_empty_string)
;
2235
2236 while ((item = PyIter_Next(it)) != NULL((void*)0)) {
2237 if (! _getbytevalue(item, &value)) {
2238 Py_DECREF(item)do { if (_Py_RefTotal-- , --((PyObject*)(item))->ob_refcnt
!= 0) { if (((PyObject*)item)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/bytearrayobject.c", 2238, (PyObject *)(item)); } else
_Py_Dealloc((PyObject *)(item)); } while (0)
;
2239 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/bytearrayobject.c", 2239, (PyObject *)(it)); } else
_Py_Dealloc((PyObject *)(it)); } while (0)
;
2240 Py_DECREF(bytearray_obj)do { if (_Py_RefTotal-- , --((PyObject*)(bytearray_obj))->
ob_refcnt != 0) { if (((PyObject*)bytearray_obj)->ob_refcnt
< 0) _Py_NegativeRefcount("Objects/bytearrayobject.c", 2240
, (PyObject *)(bytearray_obj)); } else _Py_Dealloc((PyObject *
)(bytearray_obj)); } while (0)
;
2241 return NULL((void*)0);
2242 }
2243 buf[len++] = value;
2244 Py_DECREF(item)do { if (_Py_RefTotal-- , --((PyObject*)(item))->ob_refcnt
!= 0) { if (((PyObject*)item)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/bytearrayobject.c", 2244, (PyObject *)(item)); } else
_Py_Dealloc((PyObject *)(item)); } while (0)
;
2245
2246 if (len >= buf_size) {
2247 buf_size = len + (len >> 1) + 1;
2248 if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
2249 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/bytearrayobject.c", 2249, (PyObject *)(it)); } else
_Py_Dealloc((PyObject *)(it)); } while (0)
;
2250 Py_DECREF(bytearray_obj)do { if (_Py_RefTotal-- , --((PyObject*)(bytearray_obj))->
ob_refcnt != 0) { if (((PyObject*)bytearray_obj)->ob_refcnt
< 0) _Py_NegativeRefcount("Objects/bytearrayobject.c", 2250
, (PyObject *)(bytearray_obj)); } else _Py_Dealloc((PyObject *
)(bytearray_obj)); } while (0)
;
2251 return NULL((void*)0);
2252 }
2253 /* Recompute the `buf' pointer, since the resizing operation may
2254 have invalidated it. */
2255 buf = PyByteArray_AS_STRING(bytearray_obj)((__builtin_expect(!(((((PyObject*)(bytearray_obj))->ob_type
) == (&PyByteArray_Type) || PyType_IsSubtype((((PyObject*
)(bytearray_obj))->ob_type), (&PyByteArray_Type)))), 0
) ? __assert_rtn(__func__, "Objects/bytearrayobject.c", 2255,
"PyByteArray_Check(bytearray_obj)") : (void)0), (((PyVarObject
*)(bytearray_obj))->ob_size) ? ((PyByteArrayObject *)(bytearray_obj
))->ob_bytes : _PyByteArray_empty_string)
;
2256 }
2257 }
2258 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/bytearrayobject.c", 2258, (PyObject *)(it)); } else
_Py_Dealloc((PyObject *)(it)); } while (0)
;
2259
2260 /* Resize down to exact size. */
2261 if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) {
2262 Py_DECREF(bytearray_obj)do { if (_Py_RefTotal-- , --((PyObject*)(bytearray_obj))->
ob_refcnt != 0) { if (((PyObject*)bytearray_obj)->ob_refcnt
< 0) _Py_NegativeRefcount("Objects/bytearrayobject.c", 2262
, (PyObject *)(bytearray_obj)); } else _Py_Dealloc((PyObject *
)(bytearray_obj)); } while (0)
;
2263 return NULL((void*)0);
2264 }
2265
2266 if (bytearray_setslice(self, Py_SIZE(self)(((PyVarObject*)(self))->ob_size), Py_SIZE(self)(((PyVarObject*)(self))->ob_size), bytearray_obj) == -1)
2267 return NULL((void*)0);
2268 Py_DECREF(bytearray_obj)do { if (_Py_RefTotal-- , --((PyObject*)(bytearray_obj))->
ob_refcnt != 0) { if (((PyObject*)bytearray_obj)->ob_refcnt
< 0) _Py_NegativeRefcount("Objects/bytearrayobject.c", 2268
, (PyObject *)(bytearray_obj)); } else _Py_Dealloc((PyObject *
)(bytearray_obj)); } while (0)
;
2269
2270 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
2271}
2272
2273PyDoc_STRVAR(pop__doc__,static char pop__doc__[] = "B.pop([index]) -> int\n\nRemove and return a single item from B. If no index\nargument is given, will pop the last value."
2274"B.pop([index]) -> int\n\static char pop__doc__[] = "B.pop([index]) -> int\n\nRemove and return a single item from B. If no index\nargument is given, will pop the last value."
2275\n\static char pop__doc__[] = "B.pop([index]) -> int\n\nRemove and return a single item from B. If no index\nargument is given, will pop the last value."
2276Remove and return a single item from B. If no index\n\static char pop__doc__[] = "B.pop([index]) -> int\n\nRemove and return a single item from B. If no index\nargument is given, will pop the last value."
2277argument is given, will pop the last value.")static char pop__doc__[] = "B.pop([index]) -> int\n\nRemove and return a single item from B. If no index\nargument is given, will pop the last value.";
2278static PyObject *
2279bytearray_pop(PyByteArrayObject *self, PyObject *args)
2280{
2281 int value;
2282 Py_ssize_t where = -1, n = Py_SIZE(self)(((PyVarObject*)(self))->ob_size);
2283
2284 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "|n:pop", &where))
2285 return NULL((void*)0);
2286
2287 if (n == 0) {
2288 PyErr_SetString(PyExc_OverflowError,
2289 "cannot pop an empty bytearray");
2290 return NULL((void*)0);
2291 }
2292 if (where < 0)
2293 where += Py_SIZE(self)(((PyVarObject*)(self))->ob_size);
2294 if (where < 0 || where >= Py_SIZE(self)(((PyVarObject*)(self))->ob_size)) {
2295 PyErr_SetString(PyExc_IndexError, "pop index out of range");
2296 return NULL((void*)0);
2297 }
2298 if (!_canresize(self))
2299 return NULL((void*)0);
2300
2301 value = self->ob_bytes[where];
2302 memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where)((__builtin_object_size (self->ob_bytes + where, 0) != (size_t
) -1) ? __builtin___memmove_chk (self->ob_bytes + where, self
->ob_bytes + where + 1, n - where, __builtin_object_size (
self->ob_bytes + where, 0)) : __inline_memmove_chk (self->
ob_bytes + where, self->ob_bytes + where + 1, n - where))
;
2303 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2304 return NULL((void*)0);
2305
2306 return PyLong_FromLong((unsigned char)value);
2307}
2308
2309PyDoc_STRVAR(remove__doc__,static char remove__doc__[] = "B.remove(int) -> None\n\nRemove the first occurrence of a value in B."
2310"B.remove(int) -> None\n\static char remove__doc__[] = "B.remove(int) -> None\n\nRemove the first occurrence of a value in B."
2311\n\static char remove__doc__[] = "B.remove(int) -> None\n\nRemove the first occurrence of a value in B."
2312Remove the first occurrence of a value in B.")static char remove__doc__[] = "B.remove(int) -> None\n\nRemove the first occurrence of a value in B.";
2313static PyObject *
2314bytearray_remove(PyByteArrayObject *self, PyObject *arg)
2315{
2316 int value;
2317 Py_ssize_t where, n = Py_SIZE(self)(((PyVarObject*)(self))->ob_size);
2318
2319 if (! _getbytevalue(arg, &value))
2320 return NULL((void*)0);
2321
2322 for (where = 0; where < n; where++) {
2323 if (self->ob_bytes[where] == value)
2324 break;
2325 }
2326 if (where == n) {
2327 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
2328 return NULL((void*)0);
2329 }
2330 if (!_canresize(self))
2331 return NULL((void*)0);
2332
2333 memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where)((__builtin_object_size (self->ob_bytes + where, 0) != (size_t
) -1) ? __builtin___memmove_chk (self->ob_bytes + where, self
->ob_bytes + where + 1, n - where, __builtin_object_size (
self->ob_bytes + where, 0)) : __inline_memmove_chk (self->
ob_bytes + where, self->ob_bytes + where + 1, n - where))
;
2334 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2335 return NULL((void*)0);
2336
2337 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
2338}
2339
2340/* XXX These two helpers could be optimized if argsize == 1 */
2341
2342static Py_ssize_t
2343lstrip_helper(unsigned char *myptr, Py_ssize_t mysize,
2344 void *argptr, Py_ssize_t argsize)
2345{
2346 Py_ssize_t i = 0;
2347 while (i < mysize && memchr(argptr, myptr[i], argsize))
2348 i++;
2349 return i;
2350}
2351
2352static Py_ssize_t
2353rstrip_helper(unsigned char *myptr, Py_ssize_t mysize,
2354 void *argptr, Py_ssize_t argsize)
2355{
2356 Py_ssize_t i = mysize - 1;
2357 while (i >= 0 && memchr(argptr, myptr[i], argsize))
2358 i--;
2359 return i + 1;
2360}
2361
2362PyDoc_STRVAR(strip__doc__,static char strip__doc__[] = "B.strip([bytes]) -> bytearray\n\nStrip leading and trailing bytes contained in the argument\nand return the result as a new bytearray.\nIf the argument is omitted, strip ASCII whitespace."
2363"B.strip([bytes]) -> bytearray\n\static char strip__doc__[] = "B.strip([bytes]) -> bytearray\n\nStrip leading and trailing bytes contained in the argument\nand return the result as a new bytearray.\nIf the argument is omitted, strip ASCII whitespace."
2364\n\static char strip__doc__[] = "B.strip([bytes]) -> bytearray\n\nStrip leading and trailing bytes contained in the argument\nand return the result as a new bytearray.\nIf the argument is omitted, strip ASCII whitespace."
2365Strip leading and trailing bytes contained in the argument\n\static char strip__doc__[] = "B.strip([bytes]) -> bytearray\n\nStrip leading and trailing bytes contained in the argument\nand return the result as a new bytearray.\nIf the argument is omitted, strip ASCII whitespace."
2366and return the result as a new bytearray.\n\static char strip__doc__[] = "B.strip([bytes]) -> bytearray\n\nStrip leading and trailing bytes contained in the argument\nand return the result as a new bytearray.\nIf the argument is omitted, strip ASCII whitespace."
2367If the argument is omitted, strip ASCII whitespace.")static char strip__doc__[] = "B.strip([bytes]) -> bytearray\n\nStrip leading and trailing bytes contained in the argument\nand return the result as a new bytearray.\nIf the argument is omitted, strip ASCII whitespace.";
2368static PyObject *
2369bytearray_strip(PyByteArrayObject *self, PyObject *args)
2370{
2371 Py_ssize_t left, right, mysize, argsize;
2372 void *myptr, *argptr;
2373 PyObject *arg = Py_None(&_Py_NoneStruct);
2374 Py_buffer varg;
2375 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "|O:strip", &arg))
2376 return NULL((void*)0);
2377 if (arg == Py_None(&_Py_NoneStruct)) {
2378 argptr = "\t\n\r\f\v ";
2379 argsize = 6;
2380 }
2381 else {
2382 if (_getbuffer(arg, &varg) < 0)
2383 return NULL((void*)0);
2384 argptr = varg.buf;
2385 argsize = varg.len;
2386 }
2387 myptr = self->ob_bytes;
2388 mysize = Py_SIZE(self)(((PyVarObject*)(self))->ob_size);
2389 left = lstrip_helper(myptr, mysize, argptr, argsize);
2390 if (left == mysize)
2391 right = left;
2392 else
2393 right = rstrip_helper(myptr, mysize, argptr, argsize);
2394 if (arg != Py_None(&_Py_NoneStruct))
2395 PyBuffer_Release(&varg);
2396 return PyByteArray_FromStringAndSize(self->ob_bytes + left, right - left);
2397}
2398
2399PyDoc_STRVAR(lstrip__doc__,static char lstrip__doc__[] = "B.lstrip([bytes]) -> bytearray\n\nStrip leading bytes contained in the argument\nand return the result as a new bytearray.\nIf the argument is omitted, strip leading ASCII whitespace."
2400"B.lstrip([bytes]) -> bytearray\n\static char lstrip__doc__[] = "B.lstrip([bytes]) -> bytearray\n\nStrip leading bytes contained in the argument\nand return the result as a new bytearray.\nIf the argument is omitted, strip leading ASCII whitespace."
2401\n\static char lstrip__doc__[] = "B.lstrip([bytes]) -> bytearray\n\nStrip leading bytes contained in the argument\nand return the result as a new bytearray.\nIf the argument is omitted, strip leading ASCII whitespace."
2402Strip leading bytes contained in the argument\n\static char lstrip__doc__[] = "B.lstrip([bytes]) -> bytearray\n\nStrip leading bytes contained in the argument\nand return the result as a new bytearray.\nIf the argument is omitted, strip leading ASCII whitespace."
2403and return the result as a new bytearray.\n\static char lstrip__doc__[] = "B.lstrip([bytes]) -> bytearray\n\nStrip leading bytes contained in the argument\nand return the result as a new bytearray.\nIf the argument is omitted, strip leading ASCII whitespace."
2404If the argument is omitted, strip leading ASCII whitespace.")static char lstrip__doc__[] = "B.lstrip([bytes]) -> bytearray\n\nStrip leading bytes contained in the argument\nand return the result as a new bytearray.\nIf the argument is omitted, strip leading ASCII whitespace.";
2405static PyObject *
2406bytearray_lstrip(PyByteArrayObject *self, PyObject *args)
2407{
2408 Py_ssize_t left, right, mysize, argsize;
2409 void *myptr, *argptr;
2410 PyObject *arg = Py_None(&_Py_NoneStruct);
2411 Py_buffer varg;
2412 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "|O:lstrip", &arg))
2413 return NULL((void*)0);
2414 if (arg == Py_None(&_Py_NoneStruct)) {
2415 argptr = "\t\n\r\f\v ";
2416 argsize = 6;
2417 }
2418 else {
2419 if (_getbuffer(arg, &varg) < 0)
2420 return NULL((void*)0);
2421 argptr = varg.buf;
2422 argsize = varg.len;
2423 }
2424 myptr = self->ob_bytes;
2425 mysize = Py_SIZE(self)(((PyVarObject*)(self))->ob_size);
2426 left = lstrip_helper(myptr, mysize, argptr, argsize);
2427 right = mysize;
2428 if (arg != Py_None(&_Py_NoneStruct))
2429 PyBuffer_Release(&varg);
2430 return PyByteArray_FromStringAndSize(self->ob_bytes + left, right - left);
2431}
2432
2433PyDoc_STRVAR(rstrip__doc__,static char rstrip__doc__[] = "B.rstrip([bytes]) -> bytearray\n\nStrip trailing bytes contained in the argument\nand return the result as a new bytearray.\nIf the argument is omitted, strip trailing ASCII whitespace."
2434"B.rstrip([bytes]) -> bytearray\n\static char rstrip__doc__[] = "B.rstrip([bytes]) -> bytearray\n\nStrip trailing bytes contained in the argument\nand return the result as a new bytearray.\nIf the argument is omitted, strip trailing ASCII whitespace."
2435\n\static char rstrip__doc__[] = "B.rstrip([bytes]) -> bytearray\n\nStrip trailing bytes contained in the argument\nand return the result as a new bytearray.\nIf the argument is omitted, strip trailing ASCII whitespace."
2436Strip trailing bytes contained in the argument\n\static char rstrip__doc__[] = "B.rstrip([bytes]) -> bytearray\n\nStrip trailing bytes contained in the argument\nand return the result as a new bytearray.\nIf the argument is omitted, strip trailing ASCII whitespace."
2437and return the result as a new bytearray.\n\static char rstrip__doc__[] = "B.rstrip([bytes]) -> bytearray\n\nStrip trailing bytes contained in the argument\nand return the result as a new bytearray.\nIf the argument is omitted, strip trailing ASCII whitespace."
2438If the argument is omitted, strip trailing ASCII whitespace.")static char rstrip__doc__[] = "B.rstrip([bytes]) -> bytearray\n\nStrip trailing bytes contained in the argument\nand return the result as a new bytearray.\nIf the argument is omitted, strip trailing ASCII whitespace.";
2439static PyObject *
2440bytearray_rstrip(PyByteArrayObject *self, PyObject *args)
2441{
2442 Py_ssize_t left, right, mysize, argsize;
2443 void *myptr, *argptr;
2444 PyObject *arg = Py_None(&_Py_NoneStruct);
2445 Py_buffer varg;
2446 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "|O:rstrip", &arg))
1
Taking false branch
2447 return NULL((void*)0);
2448 if (arg == Py_None(&_Py_NoneStruct)) {
2
Taking true branch
2449 argptr = "\t\n\r\f\v ";
2450 argsize = 6;
2451 }
2452 else {
2453 if (_getbuffer(arg, &varg) < 0)
2454 return NULL((void*)0);
2455 argptr = varg.buf;
2456 argsize = varg.len;
2457 }
2458 myptr = self->ob_bytes;
2459 mysize = Py_SIZE(self)(((PyVarObject*)(self))->ob_size);
2460 left = 0;
3
The value 0 is assigned to 'left'
2461 right = rstrip_helper(myptr, mysize, argptr, argsize);
2462 if (arg != Py_None(&_Py_NoneStruct))
4
Taking false branch
2463 PyBuffer_Release(&varg);
2464 return PyByteArray_FromStringAndSize(self->ob_bytes + left, right - left);
5
The right operand to '-' is always 0
2465}
2466
2467PyDoc_STRVAR(decode_doc,static char decode_doc[] = "B.decode(encoding='utf-8', errors='strict') -> str\n\nDecode B using the codec registered for encoding. Default encoding\nis 'utf-8'. errors may be given to set a different error\nhandling scheme. Default is 'strict' meaning that encoding errors raise\na UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\nas well as any other name registered with codecs.register_error that is\nable to handle UnicodeDecodeErrors."
2468"B.decode(encoding='utf-8', errors='strict') -> str\n\static char decode_doc[] = "B.decode(encoding='utf-8', errors='strict') -> str\n\nDecode B using the codec registered for encoding. Default encoding\nis 'utf-8'. errors may be given to set a different error\nhandling scheme. Default is 'strict' meaning that encoding errors raise\na UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\nas well as any other name registered with codecs.register_error that is\nable to handle UnicodeDecodeErrors."
2469\n\static char decode_doc[] = "B.decode(encoding='utf-8', errors='strict') -> str\n\nDecode B using the codec registered for encoding. Default encoding\nis 'utf-8'. errors may be given to set a different error\nhandling scheme. Default is 'strict' meaning that encoding errors raise\na UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\nas well as any other name registered with codecs.register_error that is\nable to handle UnicodeDecodeErrors."
2470Decode B using the codec registered for encoding. Default encoding\n\static char decode_doc[] = "B.decode(encoding='utf-8', errors='strict') -> str\n\nDecode B using the codec registered for encoding. Default encoding\nis 'utf-8'. errors may be given to set a different error\nhandling scheme. Default is 'strict' meaning that encoding errors raise\na UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\nas well as any other name registered with codecs.register_error that is\nable to handle UnicodeDecodeErrors."
2471is 'utf-8'. errors may be given to set a different error\n\static char decode_doc[] = "B.decode(encoding='utf-8', errors='strict') -> str\n\nDecode B using the codec registered for encoding. Default encoding\nis 'utf-8'. errors may be given to set a different error\nhandling scheme. Default is 'strict' meaning that encoding errors raise\na UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\nas well as any other name registered with codecs.register_error that is\nable to handle UnicodeDecodeErrors."
2472handling scheme. Default is 'strict' meaning that encoding errors raise\n\static char decode_doc[] = "B.decode(encoding='utf-8', errors='strict') -> str\n\nDecode B using the codec registered for encoding. Default encoding\nis 'utf-8'. errors may be given to set a different error\nhandling scheme. Default is 'strict' meaning that encoding errors raise\na UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\nas well as any other name registered with codecs.register_error that is\nable to handle UnicodeDecodeErrors."
2473a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\static char decode_doc[] = "B.decode(encoding='utf-8', errors='strict') -> str\n\nDecode B using the codec registered for encoding. Default encoding\nis 'utf-8'. errors may be given to set a different error\nhandling scheme. Default is 'strict' meaning that encoding errors raise\na UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\nas well as any other name registered with codecs.register_error that is\nable to handle UnicodeDecodeErrors."
2474as well as any other name registered with codecs.register_error that is\n\static char decode_doc[] = "B.decode(encoding='utf-8', errors='strict') -> str\n\nDecode B using the codec registered for encoding. Default encoding\nis 'utf-8'. errors may be given to set a different error\nhandling scheme. Default is 'strict' meaning that encoding errors raise\na UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\nas well as any other name registered with codecs.register_error that is\nable to handle UnicodeDecodeErrors."
2475able to handle UnicodeDecodeErrors.")static char decode_doc[] = "B.decode(encoding='utf-8', errors='strict') -> str\n\nDecode B using the codec registered for encoding. Default encoding\nis 'utf-8'. errors may be given to set a different error\nhandling scheme. Default is 'strict' meaning that encoding errors raise\na UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\nas well as any other name registered with codecs.register_error that is\nable to handle UnicodeDecodeErrors.";
2476
2477static PyObject *
2478bytearray_decode(PyObject *self, PyObject *args, PyObject *kwargs)
2479{
2480 const char *encoding = NULL((void*)0);
2481 const char *errors = NULL((void*)0);
2482 static char *kwlist[] = {"encoding", "errors", 0};
2483
2484 if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kwargs, "|ss:decode", kwlist, &encoding, &errors))
2485 return NULL((void*)0);
2486 if (encoding == NULL((void*)0))
2487 encoding = PyUnicode_GetDefaultEncodingPyUnicodeUCS2_GetDefaultEncoding();
2488 return PyUnicode_FromEncodedObjectPyUnicodeUCS2_FromEncodedObject(self, encoding, errors);
2489}
2490
2491PyDoc_STRVAR(alloc_doc,static char alloc_doc[] = "B.__alloc__() -> int\n\nReturn the number of bytes actually allocated."
2492"B.__alloc__() -> int\n\static char alloc_doc[] = "B.__alloc__() -> int\n\nReturn the number of bytes actually allocated."
2493\n\static char alloc_doc[] = "B.__alloc__() -> int\n\nReturn the number of bytes actually allocated."
2494Return the number of bytes actually allocated.")static char alloc_doc[] = "B.__alloc__() -> int\n\nReturn the number of bytes actually allocated.";
2495
2496static PyObject *
2497bytearray_alloc(PyByteArrayObject *self)
2498{
2499 return PyLong_FromSsize_t(self->ob_alloc);
2500}
2501
2502PyDoc_STRVAR(join_doc,static char join_doc[] = "B.join(iterable_of_bytes) -> bytearray\n\nConcatenate any number of bytes/bytearray objects, with B\nin between each pair, and return the result as a new bytearray."
2503"B.join(iterable_of_bytes) -> bytearray\n\static char join_doc[] = "B.join(iterable_of_bytes) -> bytearray\n\nConcatenate any number of bytes/bytearray objects, with B\nin between each pair, and return the result as a new bytearray."
2504\n\static char join_doc[] = "B.join(iterable_of_bytes) -> bytearray\n\nConcatenate any number of bytes/bytearray objects, with B\nin between each pair, and return the result as a new bytearray."
2505Concatenate any number of bytes/bytearray objects, with B\n\static char join_doc[] = "B.join(iterable_of_bytes) -> bytearray\n\nConcatenate any number of bytes/bytearray objects, with B\nin between each pair, and return the result as a new bytearray."
2506in between each pair, and return the result as a new bytearray.")static char join_doc[] = "B.join(iterable_of_bytes) -> bytearray\n\nConcatenate any number of bytes/bytearray objects, with B\nin between each pair, and return the result as a new bytearray.";
2507
2508static PyObject *
2509bytearray_join(PyByteArrayObject *self, PyObject *it)
2510{
2511 PyObject *seq;
2512 Py_ssize_t mysize = Py_SIZE(self)(((PyVarObject*)(self))->ob_size);
2513 Py_ssize_t i;
2514 Py_ssize_t n;
2515 PyObject **items;
2516 Py_ssize_t totalsize = 0;
2517 PyObject *result;
2518 char *dest;
2519
2520 seq = PySequence_Fast(it, "can only join an iterable");
2521 if (seq == NULL((void*)0))
2522 return NULL((void*)0);
2523 n = PySequence_Fast_GET_SIZE(seq)(((((((PyObject*)(seq))->ob_type))->tp_flags & ((1L
<<25))) != 0) ? (((PyVarObject*)(seq))->ob_size) : (
((PyVarObject*)(seq))->ob_size))
;
2524 items = PySequence_Fast_ITEMS(seq)(((((((PyObject*)(seq))->ob_type))->tp_flags & ((1L
<<25))) != 0) ? ((PyListObject *)(seq))->ob_item : (
(PyTupleObject *)(seq))->ob_item)
;
2525
2526 /* Compute the total size, and check that they are all bytes */
2527 /* XXX Shouldn't we use _getbuffer() on these items instead? */
2528 for (i = 0; i < n; i++) {
2529 PyObject *obj = items[i];
2530 if (!PyByteArray_Check(obj)((((PyObject*)(obj))->ob_type) == (&PyByteArray_Type) ||
PyType_IsSubtype((((PyObject*)(obj))->ob_type), (&PyByteArray_Type
)))
&& !PyBytes_Check(obj)((((((PyObject*)(obj))->ob_type))->tp_flags & ((1L<<
27))) != 0)
) {
2531 PyErr_Format(PyExc_TypeError,
2532 "can only join an iterable of bytes "
2533 "(item %ld has type '%.100s')",
2534 /* XXX %ld isn't right on Win64 */
2535 (long)i, Py_TYPE(obj)(((PyObject*)(obj))->ob_type)->tp_name);
2536 goto error;
2537 }
2538 if (i > 0)
2539 totalsize += mysize;
2540 totalsize += Py_SIZE(obj)(((PyVarObject*)(obj))->ob_size);
2541 if (totalsize < 0) {
2542 PyErr_NoMemory();
2543 goto error;
2544 }
2545 }
2546
2547 /* Allocate the result, and copy the bytes */
2548 result = PyByteArray_FromStringAndSize(NULL((void*)0), totalsize);
2549 if (result == NULL((void*)0))
2550 goto error;
2551 dest = PyByteArray_AS_STRING(result)((__builtin_expect(!(((((PyObject*)(result))->ob_type) == (
&PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(result
))->ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn
(__func__, "Objects/bytearrayobject.c", 2551, "PyByteArray_Check(result)"
) : (void)0), (((PyVarObject*)(result))->ob_size) ? ((PyByteArrayObject
*)(result))->ob_bytes : _PyByteArray_empty_string)
;
2552 for (i = 0; i < n; i++) {
2553 PyObject *obj = items[i];
2554 Py_ssize_t size = Py_SIZE(obj)(((PyVarObject*)(obj))->ob_size);
2555 char *buf;
2556 if (PyByteArray_Check(obj)((((PyObject*)(obj))->ob_type) == (&PyByteArray_Type) ||
PyType_IsSubtype((((PyObject*)(obj))->ob_type), (&PyByteArray_Type
)))
)
2557 buf = PyByteArray_AS_STRING(obj)((__builtin_expect(!(((((PyObject*)(obj))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(obj))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 2557, "PyByteArray_Check(obj)"
) : (void)0), (((PyVarObject*)(obj))->ob_size) ? ((PyByteArrayObject
*)(obj))->ob_bytes : _PyByteArray_empty_string)
;
2558 else
2559 buf = PyBytes_AS_STRING(obj)((__builtin_expect(!(((((((PyObject*)(obj))->ob_type))->
tp_flags & ((1L<<27))) != 0)), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 2559, "PyBytes_Check(obj)") : (
void)0), (((PyBytesObject *)(obj))->ob_sval))
;
2560 if (i) {
2561 memcpy(dest, self->ob_bytes, mysize)((__builtin_object_size (dest, 0) != (size_t) -1) ? __builtin___memcpy_chk
(dest, self->ob_bytes, mysize, __builtin_object_size (dest
, 0)) : __inline_memcpy_chk (dest, self->ob_bytes, mysize)
)
;
2562 dest += mysize;
2563 }
2564 memcpy(dest, buf, size)((__builtin_object_size (dest, 0) != (size_t) -1) ? __builtin___memcpy_chk
(dest, buf, size, __builtin_object_size (dest, 0)) : __inline_memcpy_chk
(dest, buf, size))
;
2565 dest += size;
2566 }
2567
2568 /* Done */
2569 Py_DECREF(seq)do { if (_Py_RefTotal-- , --((PyObject*)(seq))->ob_refcnt !=
0) { if (((PyObject*)seq)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/bytearrayobject.c", 2569, (PyObject *)(seq)); } else
_Py_Dealloc((PyObject *)(seq)); } while (0)
;
2570 return result;
2571
2572 /* Error handling */
2573 error:
2574 Py_DECREF(seq)do { if (_Py_RefTotal-- , --((PyObject*)(seq))->ob_refcnt !=
0) { if (((PyObject*)seq)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/bytearrayobject.c", 2574, (PyObject *)(seq)); } else
_Py_Dealloc((PyObject *)(seq)); } while (0)
;
2575 return NULL((void*)0);
2576}
2577
2578PyDoc_STRVAR(splitlines__doc__,static char splitlines__doc__[] = "B.splitlines([keepends]) -> list of lines\n\nReturn a list of the lines in B, breaking at line boundaries.\nLine breaks are not included in the resulting list unless keepends\nis given and true."
2579"B.splitlines([keepends]) -> list of lines\n\static char splitlines__doc__[] = "B.splitlines([keepends]) -> list of lines\n\nReturn a list of the lines in B, breaking at line boundaries.\nLine breaks are not included in the resulting list unless keepends\nis given and true."
2580\n\static char splitlines__doc__[] = "B.splitlines([keepends]) -> list of lines\n\nReturn a list of the lines in B, breaking at line boundaries.\nLine breaks are not included in the resulting list unless keepends\nis given and true."
2581Return a list of the lines in B, breaking at line boundaries.\n\static char splitlines__doc__[] = "B.splitlines([keepends]) -> list of lines\n\nReturn a list of the lines in B, breaking at line boundaries.\nLine breaks are not included in the resulting list unless keepends\nis given and true."
2582Line breaks are not included in the resulting list unless keepends\n\static char splitlines__doc__[] = "B.splitlines([keepends]) -> list of lines\n\nReturn a list of the lines in B, breaking at line boundaries.\nLine breaks are not included in the resulting list unless keepends\nis given and true."
2583is given and true.")static char splitlines__doc__[] = "B.splitlines([keepends]) -> list of lines\n\nReturn a list of the lines in B, breaking at line boundaries.\nLine breaks are not included in the resulting list unless keepends\nis given and true.";
2584
2585static PyObject*
2586bytearray_splitlines(PyObject *self, PyObject *args)
2587{
2588 int keepends = 0;
2589
2590 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "|i:splitlines", &keepends))
2591 return NULL((void*)0);
2592
2593 return stringlib_splitlines(
2594 (PyObject*) self, PyByteArray_AS_STRING(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 2594, "PyByteArray_Check(self)"
) : (void)0), (((PyVarObject*)(self))->ob_size) ? ((PyByteArrayObject
*)(self))->ob_bytes : _PyByteArray_empty_string)
,
2595 PyByteArray_GET_SIZE(self)((__builtin_expect(!(((((PyObject*)(self))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(self))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 2595, "PyByteArray_Check(self)"
) : (void)0),(((PyVarObject*)(self))->ob_size))
, keepends
2596 );
2597}
2598
2599PyDoc_STRVAR(fromhex_doc,static char fromhex_doc[] = "bytearray.fromhex(string) -> bytearray (static method)\n\nCreate a bytearray object from a string of hexadecimal numbers.\nSpaces between two numbers are accepted.\nExample: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')."
2600"bytearray.fromhex(string) -> bytearray (static method)\n\static char fromhex_doc[] = "bytearray.fromhex(string) -> bytearray (static method)\n\nCreate a bytearray object from a string of hexadecimal numbers.\nSpaces between two numbers are accepted.\nExample: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')."
2601\n\static char fromhex_doc[] = "bytearray.fromhex(string) -> bytearray (static method)\n\nCreate a bytearray object from a string of hexadecimal numbers.\nSpaces between two numbers are accepted.\nExample: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')."
2602Create a bytearray object from a string of hexadecimal numbers.\n\static char fromhex_doc[] = "bytearray.fromhex(string) -> bytearray (static method)\n\nCreate a bytearray object from a string of hexadecimal numbers.\nSpaces between two numbers are accepted.\nExample: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')."
2603Spaces between two numbers are accepted.\n\static char fromhex_doc[] = "bytearray.fromhex(string) -> bytearray (static method)\n\nCreate a bytearray object from a string of hexadecimal numbers.\nSpaces between two numbers are accepted.\nExample: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')."
2604Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef').")static char fromhex_doc[] = "bytearray.fromhex(string) -> bytearray (static method)\n\nCreate a bytearray object from a string of hexadecimal numbers.\nSpaces between two numbers are accepted.\nExample: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef').";
2605
2606static int
2607hex_digit_to_int(Py_UNICODE c)
2608{
2609 if (c >= 128)
2610 return -1;
2611 if (Py_ISDIGIT(c)(_Py_ctype_table[((unsigned char)((c) & 0xff))] & 0x04
)
)
2612 return c - '0';
2613 else {
2614 if (Py_ISUPPER(c)(_Py_ctype_table[((unsigned char)((c) & 0xff))] & 0x02
)
)
2615 c = Py_TOLOWER(c)(_Py_ctype_tolower[((unsigned char)((c) & 0xff))]);
2616 if (c >= 'a' && c <= 'f')
2617 return c - 'a' + 10;
2618 }
2619 return -1;
2620}
2621
2622static PyObject *
2623bytearray_fromhex(PyObject *cls, PyObject *args)
2624{
2625 PyObject *newbytes, *hexobj;
2626 char *buf;
2627 Py_UNICODE *hex;
2628 Py_ssize_t hexlen, byteslen, i, j;
2629 int top, bot;
2630
2631 if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "U:fromhex", &hexobj))
2632 return NULL((void*)0);
2633 assert(PyUnicode_Check(hexobj))(__builtin_expect(!(((((((PyObject*)(hexobj))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 2633, "PyUnicode_Check(hexobj)"
) : (void)0)
;
2634 hexlen = PyUnicode_GET_SIZE(hexobj)((__builtin_expect(!(((((((PyObject*)(hexobj))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 2634, "PyUnicode_Check(hexobj)"
) : (void)0),(((PyUnicodeObject *)(hexobj))->length))
;
2635 hex = PyUnicode_AS_UNICODE(hexobj)((__builtin_expect(!(((((((PyObject*)(hexobj))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 2635, "PyUnicode_Check(hexobj)"
) : (void)0),(((PyUnicodeObject *)(hexobj))->str))
;
2636 byteslen = hexlen/2; /* This overestimates if there are spaces */
2637 newbytes = PyByteArray_FromStringAndSize(NULL((void*)0), byteslen);
2638 if (!newbytes)
2639 return NULL((void*)0);
2640 buf = PyByteArray_AS_STRING(newbytes)((__builtin_expect(!(((((PyObject*)(newbytes))->ob_type) ==
(&PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(newbytes
))->ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn
(__func__, "Objects/bytearrayobject.c", 2640, "PyByteArray_Check(newbytes)"
) : (void)0), (((PyVarObject*)(newbytes))->ob_size) ? ((PyByteArrayObject
*)(newbytes))->ob_bytes : _PyByteArray_empty_string)
;
2641 for (i = j = 0; i < hexlen; i += 2) {
2642 /* skip over spaces in the input */
2643 while (hex[i] == ' ')
2644 i++;
2645 if (i >= hexlen)
2646 break;
2647 top = hex_digit_to_int(hex[i]);
2648 bot = hex_digit_to_int(hex[i+1]);
2649 if (top == -1 || bot == -1) {
2650 PyErr_Format(PyExc_ValueError,
2651 "non-hexadecimal number found in "
2652 "fromhex() arg at position %zd", i);
2653 goto error;
2654 }
2655 buf[j++] = (top << 4) + bot;
2656 }
2657 if (PyByteArray_Resize(newbytes, j) < 0)
2658 goto error;
2659 return newbytes;
2660
2661 error:
2662 Py_DECREF(newbytes)do { if (_Py_RefTotal-- , --((PyObject*)(newbytes))->ob_refcnt
!= 0) { if (((PyObject*)newbytes)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/bytearrayobject.c", 2662, (PyObject *)(newbytes)); }
else _Py_Dealloc((PyObject *)(newbytes)); } while (0)
;
2663 return NULL((void*)0);
2664}
2665
2666PyDoc_STRVAR(reduce_doc, "Return state information for pickling.")static char reduce_doc[] = "Return state information for pickling.";
2667
2668static PyObject *
2669bytearray_reduce(PyByteArrayObject *self)
2670{
2671 PyObject *latin1, *dict;
2672 if (self->ob_bytes)
2673 latin1 = PyUnicode_DecodeLatin1PyUnicodeUCS2_DecodeLatin1(self->ob_bytes,
2674 Py_SIZE(self)(((PyVarObject*)(self))->ob_size), NULL((void*)0));
2675 else
2676 latin1 = PyUnicode_FromStringPyUnicodeUCS2_FromString("");
2677
2678 dict = PyObject_GetAttrString((PyObject *)self, "__dict__");
2679 if (dict == NULL((void*)0)) {
2680 PyErr_Clear();
2681 dict = Py_None(&_Py_NoneStruct);
2682 Py_INCREF(dict)( _Py_RefTotal++ , ((PyObject*)(dict))->ob_refcnt++);
2683 }
2684
2685 return Py_BuildValue_Py_BuildValue_SizeT("(O(Ns)N)", Py_TYPE(self)(((PyObject*)(self))->ob_type), latin1, "latin-1", dict);
2686}
2687
2688PyDoc_STRVAR(sizeof_doc,static char sizeof_doc[] = "B.__sizeof__() -> int\n \nReturns the size of B in memory, in bytes"
2689"B.__sizeof__() -> int\n\static char sizeof_doc[] = "B.__sizeof__() -> int\n \nReturns the size of B in memory, in bytes"
2690 \n\static char sizeof_doc[] = "B.__sizeof__() -> int\n \nReturns the size of B in memory, in bytes"
2691Returns the size of B in memory, in bytes")static char sizeof_doc[] = "B.__sizeof__() -> int\n \nReturns the size of B in memory, in bytes";
2692static PyObject *
2693bytearray_sizeof(PyByteArrayObject *self)
2694{
2695 Py_ssize_t res;
2696
2697 res = sizeof(PyByteArrayObject) + self->ob_alloc * sizeof(char);
2698 return PyLong_FromSsize_t(res);
2699}
2700
2701static PySequenceMethods bytearray_as_sequence = {
2702 (lenfunc)bytearray_length, /* sq_length */
2703 (binaryfunc)PyByteArray_Concat, /* sq_concat */
2704 (ssizeargfunc)bytearray_repeat, /* sq_repeat */
2705 (ssizeargfunc)bytearray_getitem, /* sq_item */
2706 0, /* sq_slice */
2707 (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */
2708 0, /* sq_ass_slice */
2709 (objobjproc)bytearray_contains, /* sq_contains */
2710 (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */
2711 (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */
2712};
2713
2714static PyMappingMethods bytearray_as_mapping = {
2715 (lenfunc)bytearray_length,
2716 (binaryfunc)bytearray_subscript,
2717 (objobjargproc)bytearray_ass_subscript,
2718};
2719
2720static PyBufferProcs bytearray_as_buffer = {
2721 (getbufferproc)bytearray_getbuffer,
2722 (releasebufferproc)bytearray_releasebuffer,
2723};
2724
2725static PyMethodDef
2726bytearray_methods[] = {
2727 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS0x0004, alloc_doc},
2728 {"__reduce__", (PyCFunction)bytearray_reduce, METH_NOARGS0x0004, reduce_doc},
2729 {"__sizeof__", (PyCFunction)bytearray_sizeof, METH_NOARGS0x0004, sizeof_doc},
2730 {"append", (PyCFunction)bytearray_append, METH_O0x0008, append__doc__},
2731 {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS0x0004,
2732 _Py_capitalize__doc__},
2733 {"center", (PyCFunction)stringlib_center, METH_VARARGS0x0001, center__doc__},
2734 {"count", (PyCFunction)bytearray_count, METH_VARARGS0x0001, count__doc__},
2735 {"decode", (PyCFunction)bytearray_decode, METH_VARARGS0x0001 | METH_KEYWORDS0x0002, decode_doc},
2736 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS0x0001, endswith__doc__},
2737 {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS0x0001,
2738 expandtabs__doc__},
2739 {"extend", (PyCFunction)bytearray_extend, METH_O0x0008, extend__doc__},
2740 {"find", (PyCFunction)bytearray_find, METH_VARARGS0x0001, find__doc__},
2741 {"fromhex", (PyCFunction)bytearray_fromhex, METH_VARARGS0x0001|METH_CLASS0x0010,
2742 fromhex_doc},
2743 {"index", (PyCFunction)bytearray_index, METH_VARARGS0x0001, index__doc__},
2744 {"insert", (PyCFunction)bytearray_insert, METH_VARARGS0x0001, insert__doc__},
2745 {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS0x0004,
2746 _Py_isalnum__doc__},
2747 {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS0x0004,
2748 _Py_isalpha__doc__},
2749 {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS0x0004,
2750 _Py_isdigit__doc__},
2751 {"islower", (PyCFunction)stringlib_islower, METH_NOARGS0x0004,
2752 _Py_islower__doc__},
2753 {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS0x0004,
2754 _Py_isspace__doc__},
2755 {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS0x0004,
2756 _Py_istitle__doc__},
2757 {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS0x0004,
2758 _Py_isupper__doc__},
2759 {"join", (PyCFunction)bytearray_join, METH_O0x0008, join_doc},
2760 {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS0x0001, ljust__doc__},
2761 {"lower", (PyCFunction)stringlib_lower, METH_NOARGS0x0004, _Py_lower__doc__},
2762 {"lstrip", (PyCFunction)bytearray_lstrip, METH_VARARGS0x0001, lstrip__doc__},
2763 {"maketrans", (PyCFunction)bytearray_maketrans, METH_VARARGS0x0001|METH_STATIC0x0020,
2764 _Py_maketrans__doc__},
2765 {"partition", (PyCFunction)bytearray_partition, METH_O0x0008, partition__doc__},
2766 {"pop", (PyCFunction)bytearray_pop, METH_VARARGS0x0001, pop__doc__},
2767 {"remove", (PyCFunction)bytearray_remove, METH_O0x0008, remove__doc__},
2768 {"replace", (PyCFunction)bytearray_replace, METH_VARARGS0x0001, replace__doc__},
2769 {"reverse", (PyCFunction)bytearray_reverse, METH_NOARGS0x0004, reverse__doc__},
2770 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS0x0001, rfind__doc__},
2771 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS0x0001, rindex__doc__},
2772 {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS0x0001, rjust__doc__},
2773 {"rpartition", (PyCFunction)bytearray_rpartition, METH_O0x0008, rpartition__doc__},
2774 {"rsplit", (PyCFunction)bytearray_rsplit, METH_VARARGS0x0001, rsplit__doc__},
2775 {"rstrip", (PyCFunction)bytearray_rstrip, METH_VARARGS0x0001, rstrip__doc__},
2776 {"split", (PyCFunction)bytearray_split, METH_VARARGS0x0001, split__doc__},
2777 {"splitlines", (PyCFunction)bytearray_splitlines, METH_VARARGS0x0001,
2778 splitlines__doc__},
2779 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS0x0001 ,
2780 startswith__doc__},
2781 {"strip", (PyCFunction)bytearray_strip, METH_VARARGS0x0001, strip__doc__},
2782 {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS0x0004,
2783 _Py_swapcase__doc__},
2784 {"title", (PyCFunction)stringlib_title, METH_NOARGS0x0004, _Py_title__doc__},
2785 {"translate", (PyCFunction)bytearray_translate, METH_VARARGS0x0001,
2786 translate__doc__},
2787 {"upper", (PyCFunction)stringlib_upper, METH_NOARGS0x0004, _Py_upper__doc__},
2788 {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS0x0001, zfill__doc__},
2789 {NULL((void*)0)}
2790};
2791
2792PyDoc_STRVAR(bytearray_doc,static char bytearray_doc[] = "bytearray(iterable_of_ints) -> bytearray\nbytearray(string, encoding[, errors]) -> bytearray\nbytearray(bytes_or_bytearray) -> mutable copy of bytes_or_bytearray\nbytearray(memory_view) -> bytearray\n\nConstruct an mutable bytearray object from:\n - an iterable yielding integers in range(256)\n - a text string encoded using the specified encoding\n - a bytes or a bytearray object\n - any object implementing the buffer API.\n\nbytearray(int) -> bytearray\n\nConstruct a zero-initialized bytearray of the given length."
2793"bytearray(iterable_of_ints) -> bytearray\n\static char bytearray_doc[] = "bytearray(iterable_of_ints) -> bytearray\nbytearray(string, encoding[, errors]) -> bytearray\nbytearray(bytes_or_bytearray) -> mutable copy of bytes_or_bytearray\nbytearray(memory_view) -> bytearray\n\nConstruct an mutable bytearray object from:\n - an iterable yielding integers in range(256)\n - a text string encoded using the specified encoding\n - a bytes or a bytearray object\n - any object implementing the buffer API.\n\nbytearray(int) -> bytearray\n\nConstruct a zero-initialized bytearray of the given length."
2794bytearray(string, encoding[, errors]) -> bytearray\n\static char bytearray_doc[] = "bytearray(iterable_of_ints) -> bytearray\nbytearray(string, encoding[, errors]) -> bytearray\nbytearray(bytes_or_bytearray) -> mutable copy of bytes_or_bytearray\nbytearray(memory_view) -> bytearray\n\nConstruct an mutable bytearray object from:\n - an iterable yielding integers in range(256)\n - a text string encoded using the specified encoding\n - a bytes or a bytearray object\n - any object implementing the buffer API.\n\nbytearray(int) -> bytearray\n\nConstruct a zero-initialized bytearray of the given length."
2795bytearray(bytes_or_bytearray) -> mutable copy of bytes_or_bytearray\n\static char bytearray_doc[] = "bytearray(iterable_of_ints) -> bytearray\nbytearray(string, encoding[, errors]) -> bytearray\nbytearray(bytes_or_bytearray) -> mutable copy of bytes_or_bytearray\nbytearray(memory_view) -> bytearray\n\nConstruct an mutable bytearray object from:\n - an iterable yielding integers in range(256)\n - a text string encoded using the specified encoding\n - a bytes or a bytearray object\n - any object implementing the buffer API.\n\nbytearray(int) -> bytearray\n\nConstruct a zero-initialized bytearray of the given length."
2796bytearray(memory_view) -> bytearray\n\static char bytearray_doc[] = "bytearray(iterable_of_ints) -> bytearray\nbytearray(string, encoding[, errors]) -> bytearray\nbytearray(bytes_or_bytearray) -> mutable copy of bytes_or_bytearray\nbytearray(memory_view) -> bytearray\n\nConstruct an mutable bytearray object from:\n - an iterable yielding integers in range(256)\n - a text string encoded using the specified encoding\n - a bytes or a bytearray object\n - any object implementing the buffer API.\n\nbytearray(int) -> bytearray\n\nConstruct a zero-initialized bytearray of the given length."
2797\n\static char bytearray_doc[] = "bytearray(iterable_of_ints) -> bytearray\nbytearray(string, encoding[, errors]) -> bytearray\nbytearray(bytes_or_bytearray) -> mutable copy of bytes_or_bytearray\nbytearray(memory_view) -> bytearray\n\nConstruct an mutable bytearray object from:\n - an iterable yielding integers in range(256)\n - a text string encoded using the specified encoding\n - a bytes or a bytearray object\n - any object implementing the buffer API.\n\nbytearray(int) -> bytearray\n\nConstruct a zero-initialized bytearray of the given length."
2798Construct an mutable bytearray object from:\n\static char bytearray_doc[] = "bytearray(iterable_of_ints) -> bytearray\nbytearray(string, encoding[, errors]) -> bytearray\nbytearray(bytes_or_bytearray) -> mutable copy of bytes_or_bytearray\nbytearray(memory_view) -> bytearray\n\nConstruct an mutable bytearray object from:\n - an iterable yielding integers in range(256)\n - a text string encoded using the specified encoding\n - a bytes or a bytearray object\n - any object implementing the buffer API.\n\nbytearray(int) -> bytearray\n\nConstruct a zero-initialized bytearray of the given length."
2799 - an iterable yielding integers in range(256)\n\static char bytearray_doc[] = "bytearray(iterable_of_ints) -> bytearray\nbytearray(string, encoding[, errors]) -> bytearray\nbytearray(bytes_or_bytearray) -> mutable copy of bytes_or_bytearray\nbytearray(memory_view) -> bytearray\n\nConstruct an mutable bytearray object from:\n - an iterable yielding integers in range(256)\n - a text string encoded using the specified encoding\n - a bytes or a bytearray object\n - any object implementing the buffer API.\n\nbytearray(int) -> bytearray\n\nConstruct a zero-initialized bytearray of the given length."
2800 - a text string encoded using the specified encoding\n\static char bytearray_doc[] = "bytearray(iterable_of_ints) -> bytearray\nbytearray(string, encoding[, errors]) -> bytearray\nbytearray(bytes_or_bytearray) -> mutable copy of bytes_or_bytearray\nbytearray(memory_view) -> bytearray\n\nConstruct an mutable bytearray object from:\n - an iterable yielding integers in range(256)\n - a text string encoded using the specified encoding\n - a bytes or a bytearray object\n - any object implementing the buffer API.\n\nbytearray(int) -> bytearray\n\nConstruct a zero-initialized bytearray of the given length."
2801 - a bytes or a bytearray object\n\static char bytearray_doc[] = "bytearray(iterable_of_ints) -> bytearray\nbytearray(string, encoding[, errors]) -> bytearray\nbytearray(bytes_or_bytearray) -> mutable copy of bytes_or_bytearray\nbytearray(memory_view) -> bytearray\n\nConstruct an mutable bytearray object from:\n - an iterable yielding integers in range(256)\n - a text string encoded using the specified encoding\n - a bytes or a bytearray object\n - any object implementing the buffer API.\n\nbytearray(int) -> bytearray\n\nConstruct a zero-initialized bytearray of the given length."
2802 - any object implementing the buffer API.\n\static char bytearray_doc[] = "bytearray(iterable_of_ints) -> bytearray\nbytearray(string, encoding[, errors]) -> bytearray\nbytearray(bytes_or_bytearray) -> mutable copy of bytes_or_bytearray\nbytearray(memory_view) -> bytearray\n\nConstruct an mutable bytearray object from:\n - an iterable yielding integers in range(256)\n - a text string encoded using the specified encoding\n - a bytes or a bytearray object\n - any object implementing the buffer API.\n\nbytearray(int) -> bytearray\n\nConstruct a zero-initialized bytearray of the given length."
2803\n\static char bytearray_doc[] = "bytearray(iterable_of_ints) -> bytearray\nbytearray(string, encoding[, errors]) -> bytearray\nbytearray(bytes_or_bytearray) -> mutable copy of bytes_or_bytearray\nbytearray(memory_view) -> bytearray\n\nConstruct an mutable bytearray object from:\n - an iterable yielding integers in range(256)\n - a text string encoded using the specified encoding\n - a bytes or a bytearray object\n - any object implementing the buffer API.\n\nbytearray(int) -> bytearray\n\nConstruct a zero-initialized bytearray of the given length."
2804bytearray(int) -> bytearray\n\static char bytearray_doc[] = "bytearray(iterable_of_ints) -> bytearray\nbytearray(string, encoding[, errors]) -> bytearray\nbytearray(bytes_or_bytearray) -> mutable copy of bytes_or_bytearray\nbytearray(memory_view) -> bytearray\n\nConstruct an mutable bytearray object from:\n - an iterable yielding integers in range(256)\n - a text string encoded using the specified encoding\n - a bytes or a bytearray object\n - any object implementing the buffer API.\n\nbytearray(int) -> bytearray\n\nConstruct a zero-initialized bytearray of the given length."
2805\n\static char bytearray_doc[] = "bytearray(iterable_of_ints) -> bytearray\nbytearray(string, encoding[, errors]) -> bytearray\nbytearray(bytes_or_bytearray) -> mutable copy of bytes_or_bytearray\nbytearray(memory_view) -> bytearray\n\nConstruct an mutable bytearray object from:\n - an iterable yielding integers in range(256)\n - a text string encoded using the specified encoding\n - a bytes or a bytearray object\n - any object implementing the buffer API.\n\nbytearray(int) -> bytearray\n\nConstruct a zero-initialized bytearray of the given length."
2806Construct a zero-initialized bytearray of the given length.")static char bytearray_doc[] = "bytearray(iterable_of_ints) -> bytearray\nbytearray(string, encoding[, errors]) -> bytearray\nbytearray(bytes_or_bytearray) -> mutable copy of bytes_or_bytearray\nbytearray(memory_view) -> bytearray\n\nConstruct an mutable bytearray object from:\n - an iterable yielding integers in range(256)\n - a text string encoded using the specified encoding\n - a bytes or a bytearray object\n - any object implementing the buffer API.\n\nbytearray(int) -> bytearray\n\nConstruct a zero-initialized bytearray of the given length.";
2807
2808
2809static PyObject *bytearray_iter(PyObject *seq);
2810
2811PyTypeObject PyByteArray_Type = {
2812 PyVarObject_HEAD_INIT(&PyType_Type, 0){ { 0, 0, 1, &PyType_Type }, 0 },
2813 "bytearray",
2814 sizeof(PyByteArrayObject),
2815 0,
2816 (destructor)bytearray_dealloc, /* tp_dealloc */
2817 0, /* tp_print */
2818 0, /* tp_getattr */
2819 0, /* tp_setattr */
2820 0, /* tp_reserved */
2821 (reprfunc)bytearray_repr, /* tp_repr */
2822 0, /* tp_as_number */
2823 &bytearray_as_sequence, /* tp_as_sequence */
2824 &bytearray_as_mapping, /* tp_as_mapping */
2825 0, /* tp_hash */
2826 0, /* tp_call */
2827 bytearray_str, /* tp_str */
2828 PyObject_GenericGetAttr, /* tp_getattro */
2829 0, /* tp_setattro */
2830 &bytearray_as_buffer, /* tp_as_buffer */
2831 Py_TPFLAGS_DEFAULT( 0 | (1L<<18) | 0) | Py_TPFLAGS_BASETYPE(1L<<10), /* tp_flags */
2832 bytearray_doc, /* tp_doc */
2833 0, /* tp_traverse */
2834 0, /* tp_clear */
2835 (richcmpfunc)bytearray_richcompare, /* tp_richcompare */
2836 0, /* tp_weaklistoffset */
2837 bytearray_iter, /* tp_iter */
2838 0, /* tp_iternext */
2839 bytearray_methods, /* tp_methods */
2840 0, /* tp_members */
2841 0, /* tp_getset */
2842 0, /* tp_base */
2843 0, /* tp_dict */
2844 0, /* tp_descr_get */
2845 0, /* tp_descr_set */
2846 0, /* tp_dictoffset */
2847 (initproc)bytearray_init, /* tp_init */
2848 PyType_GenericAlloc, /* tp_alloc */
2849 PyType_GenericNew, /* tp_new */
2850 PyObject_Del_PyObject_DebugFree, /* tp_free */
2851};
2852
2853/*********************** Bytes Iterator ****************************/
2854
2855typedef struct {
2856 PyObject_HEADPyObject ob_base;
2857 Py_ssize_t it_index;
2858 PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
2859} bytesiterobject;
2860
2861static void
2862bytearrayiter_dealloc(bytesiterobject *it)
2863{
2864 _PyObject_GC_UNTRACK(it)do { PyGC_Head *g = ((PyGC_Head *)(it)-1); (__builtin_expect(
!(g->gc.gc_refs != (-2)), 0) ? __assert_rtn(__func__, "Objects/bytearrayobject.c"
, 2864, "g->gc.gc_refs != _PyGC_REFS_UNTRACKED") : (void)0
); g->gc.gc_refs = (-2); g->gc.gc_prev->gc.gc_next =
g->gc.gc_next; g->gc.gc_next->gc.gc_prev = g->gc
.gc_prev; g->gc.gc_next = ((void*)0); } while (0);
;
2865 Py_XDECREF(it->it_seq)do { if ((it->it_seq) == ((void*)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(it->it_seq))->ob_refcnt != 0) { if (
((PyObject*)it->it_seq)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/bytearrayobject.c", 2865, (PyObject *)(it->it_seq
)); } else _Py_Dealloc((PyObject *)(it->it_seq)); } while (
0); } while (0)
;
2866 PyObject_GC_Del(it);
2867}
2868
2869static int
2870bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
2871{
2872 Py_VISIT(it->it_seq)do { if (it->it_seq) { int vret = visit((PyObject *)(it->
it_seq), arg); if (vret) return vret; } } while (0)
;
2873 return 0;
2874}
2875
2876static PyObject *
2877bytearrayiter_next(bytesiterobject *it)
2878{
2879 PyByteArrayObject *seq;
2880 PyObject *item;
2881
2882 assert(it != NULL)(__builtin_expect(!(it != ((void*)0)), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 2882, "it != NULL") : (void)0)
;
2883 seq = it->it_seq;
2884 if (seq == NULL((void*)0))
2885 return NULL((void*)0);
2886 assert(PyByteArray_Check(seq))(__builtin_expect(!(((((PyObject*)(seq))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(seq))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 2886, "PyByteArray_Check(seq)"
) : (void)0)
;
2887
2888 if (it->it_index < PyByteArray_GET_SIZE(seq)((__builtin_expect(!(((((PyObject*)(seq))->ob_type) == (&
PyByteArray_Type) || PyType_IsSubtype((((PyObject*)(seq))->
ob_type), (&PyByteArray_Type)))), 0) ? __assert_rtn(__func__
, "Objects/bytearrayobject.c", 2888, "PyByteArray_Check(seq)"
) : (void)0),(((PyVarObject*)(seq))->ob_size))
) {
2889 item = PyLong_FromLong(
2890 (unsigned char)seq->ob_bytes[it->it_index]);
2891 if (item != NULL((void*)0))
2892 ++it->it_index;
2893 return item;
2894 }
2895
2896 Py_DECREF(seq)do { if (_Py_RefTotal-- , --((PyObject*)(seq))->ob_refcnt !=
0) { if (((PyObject*)seq)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/bytearrayobject.c", 2896, (PyObject *)(seq)); } else
_Py_Dealloc((PyObject *)(seq)); } while (0)
;
2897 it->it_seq = NULL((void*)0);
2898 return NULL((void*)0);
2899}
2900
2901static PyObject *
2902bytesarrayiter_length_hint(bytesiterobject *it)
2903{
2904 Py_ssize_t len = 0;
2905 if (it->it_seq)
2906 len = PyByteArray_GET_SIZE(it->it_seq)((__builtin_expect(!(((((PyObject*)(it->it_seq))->ob_type
) == (&PyByteArray_Type) || PyType_IsSubtype((((PyObject*
)(it->it_seq))->ob_type), (&PyByteArray_Type)))), 0
) ? __assert_rtn(__func__, "Objects/bytearrayobject.c", 2906,
"PyByteArray_Check(it->it_seq)") : (void)0),(((PyVarObject
*)(it->it_seq))->ob_size))
- it->it_index;
2907 return PyLong_FromSsize_t(len);
2908}
2909
2910PyDoc_STRVAR(length_hint_doc,static char length_hint_doc[] = "Private method returning an estimate of len(list(it))."
2911 "Private method returning an estimate of len(list(it)).")static char length_hint_doc[] = "Private method returning an estimate of len(list(it)).";
2912
2913static PyMethodDef bytearrayiter_methods[] = {
2914 {"__length_hint__", (PyCFunction)bytesarrayiter_length_hint, METH_NOARGS0x0004,
2915 length_hint_doc},
2916 {NULL((void*)0), NULL((void*)0)} /* sentinel */
2917};
2918
2919PyTypeObject PyByteArrayIter_Type = {
2920 PyVarObject_HEAD_INIT(&PyType_Type, 0){ { 0, 0, 1, &PyType_Type }, 0 },
2921 "bytearray_iterator", /* tp_name */
2922 sizeof(bytesiterobject), /* tp_basicsize */
2923 0, /* tp_itemsize */
2924 /* methods */
2925 (destructor)bytearrayiter_dealloc, /* tp_dealloc */
2926 0, /* tp_print */
2927 0, /* tp_getattr */
2928 0, /* tp_setattr */
2929 0, /* tp_reserved */
2930 0, /* tp_repr */
2931 0, /* tp_as_number */
2932 0, /* tp_as_sequence */
2933 0, /* tp_as_mapping */
2934 0, /* tp_hash */
2935 0, /* tp_call */
2936 0, /* tp_str */
2937 PyObject_GenericGetAttr, /* tp_getattro */
2938 0, /* tp_setattro */
2939 0, /* tp_as_buffer */
2940 Py_TPFLAGS_DEFAULT( 0 | (1L<<18) | 0) | Py_TPFLAGS_HAVE_GC(1L<<14), /* tp_flags */
2941 0, /* tp_doc */
2942 (traverseproc)bytearrayiter_traverse, /* tp_traverse */
2943 0, /* tp_clear */
2944 0, /* tp_richcompare */
2945 0, /* tp_weaklistoffset */
2946 PyObject_SelfIter, /* tp_iter */
2947 (iternextfunc)bytearrayiter_next, /* tp_iternext */
2948 bytearrayiter_methods, /* tp_methods */
2949 0,
2950};
2951
2952static PyObject *
2953bytearray_iter(PyObject *seq)
2954{
2955 bytesiterobject *it;
2956
2957 if (!PyByteArray_Check(seq)((((PyObject*)(seq))->ob_type) == (&PyByteArray_Type) ||
PyType_IsSubtype((((PyObject*)(seq))->ob_type), (&PyByteArray_Type
)))
) {
2958 PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/bytearrayobject.c", 2958);
2959 return NULL((void*)0);
2960 }
2961 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type)( (bytesiterobject *) _PyObject_GC_New(&PyByteArrayIter_Type
) )
;
2962 if (it == NULL((void*)0))
2963 return NULL((void*)0);
2964 it->it_index = 0;
2965 Py_INCREF(seq)( _Py_RefTotal++ , ((PyObject*)(seq))->ob_refcnt++);
2966 it->it_seq = (PyByteArrayObject *)seq;
2967 _PyObject_GC_TRACK(it)do { PyGC_Head *g = ((PyGC_Head *)(it)-1); if (g->gc.gc_refs
!= (-2)) Py_FatalError("GC object already tracked"); g->gc
.gc_refs = (-3); g->gc.gc_next = _PyGC_generation0; g->
gc.gc_prev = _PyGC_generation0->gc.gc_prev; g->gc.gc_prev
->gc.gc_next = g; _PyGC_generation0->gc.gc_prev = g; } while
(0);
;
2968 return (PyObject *)it;
2969}