Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/Objects/bytearrayobject.c
12 views
1
/* PyByteArray (bytearray) implementation */
2
3
#include "Python.h"
4
#include "pycore_abstract.h" // _PyIndex_Check()
5
#include "pycore_bytes_methods.h"
6
#include "pycore_bytesobject.h"
7
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
8
#include "pycore_strhex.h" // _Py_strhex_with_sep()
9
#include "pycore_long.h" // _PyLong_FromUnsignedChar()
10
#include "bytesobject.h"
11
12
/*[clinic input]
13
class bytearray "PyByteArrayObject *" "&PyByteArray_Type"
14
[clinic start generated code]*/
15
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/
16
17
/* For PyByteArray_AS_STRING(). */
18
char _PyByteArray_empty_string[] = "";
19
20
/* Helpers */
21
22
static int
23
_getbytevalue(PyObject* arg, int *value)
24
{
25
int overflow;
26
long face_value = PyLong_AsLongAndOverflow(arg, &overflow);
27
28
if (face_value == -1 && PyErr_Occurred()) {
29
*value = -1;
30
return 0;
31
}
32
if (face_value < 0 || face_value >= 256) {
33
/* this includes an overflow in converting to C long */
34
PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
35
*value = -1;
36
return 0;
37
}
38
39
*value = face_value;
40
return 1;
41
}
42
43
static int
44
bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
45
{
46
void *ptr;
47
if (view == NULL) {
48
PyErr_SetString(PyExc_BufferError,
49
"bytearray_getbuffer: view==NULL argument is obsolete");
50
return -1;
51
}
52
ptr = (void *) PyByteArray_AS_STRING(obj);
53
/* cannot fail if view != NULL and readonly == 0 */
54
(void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
55
obj->ob_exports++;
56
return 0;
57
}
58
59
static void
60
bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
61
{
62
obj->ob_exports--;
63
assert(obj->ob_exports >= 0);
64
}
65
66
static int
67
_canresize(PyByteArrayObject *self)
68
{
69
if (self->ob_exports > 0) {
70
PyErr_SetString(PyExc_BufferError,
71
"Existing exports of data: object cannot be re-sized");
72
return 0;
73
}
74
return 1;
75
}
76
77
#include "clinic/bytearrayobject.c.h"
78
79
/* Direct API functions */
80
81
PyObject *
82
PyByteArray_FromObject(PyObject *input)
83
{
84
return PyObject_CallOneArg((PyObject *)&PyByteArray_Type, input);
85
}
86
87
static PyObject *
88
_PyByteArray_FromBufferObject(PyObject *obj)
89
{
90
PyObject *result;
91
Py_buffer view;
92
93
if (PyObject_GetBuffer(obj, &view, PyBUF_FULL_RO) < 0) {
94
return NULL;
95
}
96
result = PyByteArray_FromStringAndSize(NULL, view.len);
97
if (result != NULL &&
98
PyBuffer_ToContiguous(PyByteArray_AS_STRING(result),
99
&view, view.len, 'C') < 0)
100
{
101
Py_CLEAR(result);
102
}
103
PyBuffer_Release(&view);
104
return result;
105
}
106
107
PyObject *
108
PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
109
{
110
PyByteArrayObject *new;
111
Py_ssize_t alloc;
112
113
if (size < 0) {
114
PyErr_SetString(PyExc_SystemError,
115
"Negative size passed to PyByteArray_FromStringAndSize");
116
return NULL;
117
}
118
119
/* Prevent buffer overflow when setting alloc to size+1. */
120
if (size == PY_SSIZE_T_MAX) {
121
return PyErr_NoMemory();
122
}
123
124
new = PyObject_New(PyByteArrayObject, &PyByteArray_Type);
125
if (new == NULL)
126
return NULL;
127
128
if (size == 0) {
129
new->ob_bytes = NULL;
130
alloc = 0;
131
}
132
else {
133
alloc = size + 1;
134
new->ob_bytes = PyObject_Malloc(alloc);
135
if (new->ob_bytes == NULL) {
136
Py_DECREF(new);
137
return PyErr_NoMemory();
138
}
139
if (bytes != NULL && size > 0)
140
memcpy(new->ob_bytes, bytes, size);
141
new->ob_bytes[size] = '\0'; /* Trailing null byte */
142
}
143
Py_SET_SIZE(new, size);
144
new->ob_alloc = alloc;
145
new->ob_start = new->ob_bytes;
146
new->ob_exports = 0;
147
148
return (PyObject *)new;
149
}
150
151
Py_ssize_t
152
PyByteArray_Size(PyObject *self)
153
{
154
assert(self != NULL);
155
assert(PyByteArray_Check(self));
156
157
return PyByteArray_GET_SIZE(self);
158
}
159
160
char *
161
PyByteArray_AsString(PyObject *self)
162
{
163
assert(self != NULL);
164
assert(PyByteArray_Check(self));
165
166
return PyByteArray_AS_STRING(self);
167
}
168
169
int
170
PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
171
{
172
void *sval;
173
PyByteArrayObject *obj = ((PyByteArrayObject *)self);
174
/* All computations are done unsigned to avoid integer overflows
175
(see issue #22335). */
176
size_t alloc = (size_t) obj->ob_alloc;
177
size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes);
178
size_t size = (size_t) requested_size;
179
180
assert(self != NULL);
181
assert(PyByteArray_Check(self));
182
assert(logical_offset <= alloc);
183
assert(requested_size >= 0);
184
185
if (requested_size == Py_SIZE(self)) {
186
return 0;
187
}
188
if (!_canresize(obj)) {
189
return -1;
190
}
191
192
if (size + logical_offset + 1 <= alloc) {
193
/* Current buffer is large enough to host the requested size,
194
decide on a strategy. */
195
if (size < alloc / 2) {
196
/* Major downsize; resize down to exact size */
197
alloc = size + 1;
198
}
199
else {
200
/* Minor downsize; quick exit */
201
Py_SET_SIZE(self, size);
202
PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */
203
return 0;
204
}
205
}
206
else {
207
/* Need growing, decide on a strategy */
208
if (size <= alloc * 1.125) {
209
/* Moderate upsize; overallocate similar to list_resize() */
210
alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
211
}
212
else {
213
/* Major upsize; resize up to exact size */
214
alloc = size + 1;
215
}
216
}
217
if (alloc > PY_SSIZE_T_MAX) {
218
PyErr_NoMemory();
219
return -1;
220
}
221
222
if (logical_offset > 0) {
223
sval = PyObject_Malloc(alloc);
224
if (sval == NULL) {
225
PyErr_NoMemory();
226
return -1;
227
}
228
memcpy(sval, PyByteArray_AS_STRING(self),
229
Py_MIN((size_t)requested_size, (size_t)Py_SIZE(self)));
230
PyObject_Free(obj->ob_bytes);
231
}
232
else {
233
sval = PyObject_Realloc(obj->ob_bytes, alloc);
234
if (sval == NULL) {
235
PyErr_NoMemory();
236
return -1;
237
}
238
}
239
240
obj->ob_bytes = obj->ob_start = sval;
241
Py_SET_SIZE(self, size);
242
obj->ob_alloc = alloc;
243
obj->ob_bytes[size] = '\0'; /* Trailing null byte */
244
245
return 0;
246
}
247
248
PyObject *
249
PyByteArray_Concat(PyObject *a, PyObject *b)
250
{
251
Py_buffer va, vb;
252
PyByteArrayObject *result = NULL;
253
254
va.len = -1;
255
vb.len = -1;
256
if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 ||
257
PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) {
258
PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
259
Py_TYPE(b)->tp_name, Py_TYPE(a)->tp_name);
260
goto done;
261
}
262
263
if (va.len > PY_SSIZE_T_MAX - vb.len) {
264
PyErr_NoMemory();
265
goto done;
266
}
267
268
result = (PyByteArrayObject *) \
269
PyByteArray_FromStringAndSize(NULL, va.len + vb.len);
270
// result->ob_bytes is NULL if result is an empty bytearray:
271
// if va.len + vb.len equals zero.
272
if (result != NULL && result->ob_bytes != NULL) {
273
memcpy(result->ob_bytes, va.buf, va.len);
274
memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
275
}
276
277
done:
278
if (va.len != -1)
279
PyBuffer_Release(&va);
280
if (vb.len != -1)
281
PyBuffer_Release(&vb);
282
return (PyObject *)result;
283
}
284
285
/* Functions stuffed into the type object */
286
287
static Py_ssize_t
288
bytearray_length(PyByteArrayObject *self)
289
{
290
return Py_SIZE(self);
291
}
292
293
static PyObject *
294
bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
295
{
296
Py_ssize_t size;
297
Py_buffer vo;
298
299
if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) {
300
PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
301
Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name);
302
return NULL;
303
}
304
305
size = Py_SIZE(self);
306
if (size > PY_SSIZE_T_MAX - vo.len) {
307
PyBuffer_Release(&vo);
308
return PyErr_NoMemory();
309
}
310
if (PyByteArray_Resize((PyObject *)self, size + vo.len) < 0) {
311
PyBuffer_Release(&vo);
312
return NULL;
313
}
314
memcpy(PyByteArray_AS_STRING(self) + size, vo.buf, vo.len);
315
PyBuffer_Release(&vo);
316
return Py_NewRef(self);
317
}
318
319
static PyObject *
320
bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
321
{
322
if (count < 0)
323
count = 0;
324
const Py_ssize_t mysize = Py_SIZE(self);
325
if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
326
return PyErr_NoMemory();
327
Py_ssize_t size = mysize * count;
328
PyByteArrayObject* result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size);
329
const char* buf = PyByteArray_AS_STRING(self);
330
if (result != NULL && size != 0) {
331
_PyBytes_Repeat(result->ob_bytes, size, buf, mysize);
332
}
333
return (PyObject *)result;
334
}
335
336
static PyObject *
337
bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
338
{
339
if (count < 0)
340
count = 0;
341
else if (count == 1) {
342
return Py_NewRef(self);
343
}
344
345
const Py_ssize_t mysize = Py_SIZE(self);
346
if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
347
return PyErr_NoMemory();
348
const Py_ssize_t size = mysize * count;
349
if (PyByteArray_Resize((PyObject *)self, size) < 0)
350
return NULL;
351
352
char* buf = PyByteArray_AS_STRING(self);
353
_PyBytes_Repeat(buf, size, buf, mysize);
354
355
return Py_NewRef(self);
356
}
357
358
static PyObject *
359
bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
360
{
361
if (i < 0 || i >= Py_SIZE(self)) {
362
PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
363
return NULL;
364
}
365
return _PyLong_FromUnsignedChar((unsigned char)(self->ob_start[i]));
366
}
367
368
static PyObject *
369
bytearray_subscript(PyByteArrayObject *self, PyObject *index)
370
{
371
if (_PyIndex_Check(index)) {
372
Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
373
374
if (i == -1 && PyErr_Occurred())
375
return NULL;
376
377
if (i < 0)
378
i += PyByteArray_GET_SIZE(self);
379
380
if (i < 0 || i >= Py_SIZE(self)) {
381
PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
382
return NULL;
383
}
384
return _PyLong_FromUnsignedChar((unsigned char)(self->ob_start[i]));
385
}
386
else if (PySlice_Check(index)) {
387
Py_ssize_t start, stop, step, slicelength, i;
388
size_t cur;
389
if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
390
return NULL;
391
}
392
slicelength = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self),
393
&start, &stop, step);
394
395
if (slicelength <= 0)
396
return PyByteArray_FromStringAndSize("", 0);
397
else if (step == 1) {
398
return PyByteArray_FromStringAndSize(
399
PyByteArray_AS_STRING(self) + start, slicelength);
400
}
401
else {
402
char *source_buf = PyByteArray_AS_STRING(self);
403
char *result_buf;
404
PyObject *result;
405
406
result = PyByteArray_FromStringAndSize(NULL, slicelength);
407
if (result == NULL)
408
return NULL;
409
410
result_buf = PyByteArray_AS_STRING(result);
411
for (cur = start, i = 0; i < slicelength;
412
cur += step, i++) {
413
result_buf[i] = source_buf[cur];
414
}
415
return result;
416
}
417
}
418
else {
419
PyErr_Format(PyExc_TypeError,
420
"bytearray indices must be integers or slices, not %.200s",
421
Py_TYPE(index)->tp_name);
422
return NULL;
423
}
424
}
425
426
static int
427
bytearray_setslice_linear(PyByteArrayObject *self,
428
Py_ssize_t lo, Py_ssize_t hi,
429
char *bytes, Py_ssize_t bytes_len)
430
{
431
Py_ssize_t avail = hi - lo;
432
char *buf = PyByteArray_AS_STRING(self);
433
Py_ssize_t growth = bytes_len - avail;
434
int res = 0;
435
assert(avail >= 0);
436
437
if (growth < 0) {
438
if (!_canresize(self))
439
return -1;
440
441
if (lo == 0) {
442
/* Shrink the buffer by advancing its logical start */
443
self->ob_start -= growth;
444
/*
445
0 lo hi old_size
446
| |<----avail----->|<-----tail------>|
447
| |<-bytes_len->|<-----tail------>|
448
0 new_lo new_hi new_size
449
*/
450
}
451
else {
452
/*
453
0 lo hi old_size
454
| |<----avail----->|<-----tomove------>|
455
| |<-bytes_len->|<-----tomove------>|
456
0 lo new_hi new_size
457
*/
458
memmove(buf + lo + bytes_len, buf + hi,
459
Py_SIZE(self) - hi);
460
}
461
if (PyByteArray_Resize((PyObject *)self,
462
Py_SIZE(self) + growth) < 0) {
463
/* Issue #19578: Handling the memory allocation failure here is
464
tricky here because the bytearray object has already been
465
modified. Depending on growth and lo, the behaviour is
466
different.
467
468
If growth < 0 and lo != 0, the operation is completed, but a
469
MemoryError is still raised and the memory block is not
470
shrunk. Otherwise, the bytearray is restored in its previous
471
state and a MemoryError is raised. */
472
if (lo == 0) {
473
self->ob_start += growth;
474
return -1;
475
}
476
/* memmove() removed bytes, the bytearray object cannot be
477
restored in its previous state. */
478
Py_SET_SIZE(self, Py_SIZE(self) + growth);
479
res = -1;
480
}
481
buf = PyByteArray_AS_STRING(self);
482
}
483
else if (growth > 0) {
484
if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) {
485
PyErr_NoMemory();
486
return -1;
487
}
488
489
if (PyByteArray_Resize((PyObject *)self,
490
Py_SIZE(self) + growth) < 0) {
491
return -1;
492
}
493
buf = PyByteArray_AS_STRING(self);
494
/* Make the place for the additional bytes */
495
/*
496
0 lo hi old_size
497
| |<-avail->|<-----tomove------>|
498
| |<---bytes_len-->|<-----tomove------>|
499
0 lo new_hi new_size
500
*/
501
memmove(buf + lo + bytes_len, buf + hi,
502
Py_SIZE(self) - lo - bytes_len);
503
}
504
505
if (bytes_len > 0)
506
memcpy(buf + lo, bytes, bytes_len);
507
return res;
508
}
509
510
static int
511
bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
512
PyObject *values)
513
{
514
Py_ssize_t needed;
515
void *bytes;
516
Py_buffer vbytes;
517
int res = 0;
518
519
vbytes.len = -1;
520
if (values == (PyObject *)self) {
521
/* Make a copy and call this function recursively */
522
int err;
523
values = PyByteArray_FromStringAndSize(PyByteArray_AS_STRING(values),
524
PyByteArray_GET_SIZE(values));
525
if (values == NULL)
526
return -1;
527
err = bytearray_setslice(self, lo, hi, values);
528
Py_DECREF(values);
529
return err;
530
}
531
if (values == NULL) {
532
/* del b[lo:hi] */
533
bytes = NULL;
534
needed = 0;
535
}
536
else {
537
if (PyObject_GetBuffer(values, &vbytes, PyBUF_SIMPLE) != 0) {
538
PyErr_Format(PyExc_TypeError,
539
"can't set bytearray slice from %.100s",
540
Py_TYPE(values)->tp_name);
541
return -1;
542
}
543
needed = vbytes.len;
544
bytes = vbytes.buf;
545
}
546
547
if (lo < 0)
548
lo = 0;
549
if (hi < lo)
550
hi = lo;
551
if (hi > Py_SIZE(self))
552
hi = Py_SIZE(self);
553
554
res = bytearray_setslice_linear(self, lo, hi, bytes, needed);
555
if (vbytes.len != -1)
556
PyBuffer_Release(&vbytes);
557
return res;
558
}
559
560
static int
561
bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
562
{
563
int ival = -1;
564
565
// GH-91153: We need to do this *before* the size check, in case value has a
566
// nasty __index__ method that changes the size of the bytearray:
567
if (value && !_getbytevalue(value, &ival)) {
568
return -1;
569
}
570
571
if (i < 0) {
572
i += Py_SIZE(self);
573
}
574
575
if (i < 0 || i >= Py_SIZE(self)) {
576
PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
577
return -1;
578
}
579
580
if (value == NULL) {
581
return bytearray_setslice(self, i, i+1, NULL);
582
}
583
584
assert(0 <= ival && ival < 256);
585
PyByteArray_AS_STRING(self)[i] = ival;
586
return 0;
587
}
588
589
static int
590
bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)
591
{
592
Py_ssize_t start, stop, step, slicelen, needed;
593
char *buf, *bytes;
594
buf = PyByteArray_AS_STRING(self);
595
596
if (_PyIndex_Check(index)) {
597
Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
598
599
if (i == -1 && PyErr_Occurred()) {
600
return -1;
601
}
602
603
int ival = -1;
604
605
// GH-91153: We need to do this *before* the size check, in case values
606
// has a nasty __index__ method that changes the size of the bytearray:
607
if (values && !_getbytevalue(values, &ival)) {
608
return -1;
609
}
610
611
if (i < 0) {
612
i += PyByteArray_GET_SIZE(self);
613
}
614
615
if (i < 0 || i >= Py_SIZE(self)) {
616
PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
617
return -1;
618
}
619
620
if (values == NULL) {
621
/* Fall through to slice assignment */
622
start = i;
623
stop = i + 1;
624
step = 1;
625
slicelen = 1;
626
}
627
else {
628
assert(0 <= ival && ival < 256);
629
buf[i] = (char)ival;
630
return 0;
631
}
632
}
633
else if (PySlice_Check(index)) {
634
if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
635
return -1;
636
}
637
slicelen = PySlice_AdjustIndices(PyByteArray_GET_SIZE(self), &start,
638
&stop, step);
639
}
640
else {
641
PyErr_Format(PyExc_TypeError,
642
"bytearray indices must be integers or slices, not %.200s",
643
Py_TYPE(index)->tp_name);
644
return -1;
645
}
646
647
if (values == NULL) {
648
bytes = NULL;
649
needed = 0;
650
}
651
else if (values == (PyObject *)self || !PyByteArray_Check(values)) {
652
int err;
653
if (PyNumber_Check(values) || PyUnicode_Check(values)) {
654
PyErr_SetString(PyExc_TypeError,
655
"can assign only bytes, buffers, or iterables "
656
"of ints in range(0, 256)");
657
return -1;
658
}
659
/* Make a copy and call this function recursively */
660
values = PyByteArray_FromObject(values);
661
if (values == NULL)
662
return -1;
663
err = bytearray_ass_subscript(self, index, values);
664
Py_DECREF(values);
665
return err;
666
}
667
else {
668
assert(PyByteArray_Check(values));
669
bytes = PyByteArray_AS_STRING(values);
670
needed = Py_SIZE(values);
671
}
672
/* Make sure b[5:2] = ... inserts before 5, not before 2. */
673
if ((step < 0 && start < stop) ||
674
(step > 0 && start > stop))
675
stop = start;
676
if (step == 1) {
677
return bytearray_setslice_linear(self, start, stop, bytes, needed);
678
}
679
else {
680
if (needed == 0) {
681
/* Delete slice */
682
size_t cur;
683
Py_ssize_t i;
684
685
if (!_canresize(self))
686
return -1;
687
688
if (slicelen == 0)
689
/* Nothing to do here. */
690
return 0;
691
692
if (step < 0) {
693
stop = start + 1;
694
start = stop + step * (slicelen - 1) - 1;
695
step = -step;
696
}
697
for (cur = start, i = 0;
698
i < slicelen; cur += step, i++) {
699
Py_ssize_t lim = step - 1;
700
701
if (cur + step >= (size_t)PyByteArray_GET_SIZE(self))
702
lim = PyByteArray_GET_SIZE(self) - cur - 1;
703
704
memmove(buf + cur - i,
705
buf + cur + 1, lim);
706
}
707
/* Move the tail of the bytes, in one chunk */
708
cur = start + (size_t)slicelen*step;
709
if (cur < (size_t)PyByteArray_GET_SIZE(self)) {
710
memmove(buf + cur - slicelen,
711
buf + cur,
712
PyByteArray_GET_SIZE(self) - cur);
713
}
714
if (PyByteArray_Resize((PyObject *)self,
715
PyByteArray_GET_SIZE(self) - slicelen) < 0)
716
return -1;
717
718
return 0;
719
}
720
else {
721
/* Assign slice */
722
Py_ssize_t i;
723
size_t cur;
724
725
if (needed != slicelen) {
726
PyErr_Format(PyExc_ValueError,
727
"attempt to assign bytes of size %zd "
728
"to extended slice of size %zd",
729
needed, slicelen);
730
return -1;
731
}
732
for (cur = start, i = 0; i < slicelen; cur += step, i++)
733
buf[cur] = bytes[i];
734
return 0;
735
}
736
}
737
}
738
739
/*[clinic input]
740
bytearray.__init__
741
742
source as arg: object = NULL
743
encoding: str = NULL
744
errors: str = NULL
745
746
[clinic start generated code]*/
747
748
static int
749
bytearray___init___impl(PyByteArrayObject *self, PyObject *arg,
750
const char *encoding, const char *errors)
751
/*[clinic end generated code: output=4ce1304649c2f8b3 input=1141a7122eefd7b9]*/
752
{
753
Py_ssize_t count;
754
PyObject *it;
755
PyObject *(*iternext)(PyObject *);
756
757
if (Py_SIZE(self) != 0) {
758
/* Empty previous contents (yes, do this first of all!) */
759
if (PyByteArray_Resize((PyObject *)self, 0) < 0)
760
return -1;
761
}
762
763
/* Make a quick exit if no first argument */
764
if (arg == NULL) {
765
if (encoding != NULL || errors != NULL) {
766
PyErr_SetString(PyExc_TypeError,
767
encoding != NULL ?
768
"encoding without a string argument" :
769
"errors without a string argument");
770
return -1;
771
}
772
return 0;
773
}
774
775
if (PyUnicode_Check(arg)) {
776
/* Encode via the codec registry */
777
PyObject *encoded, *new;
778
if (encoding == NULL) {
779
PyErr_SetString(PyExc_TypeError,
780
"string argument without an encoding");
781
return -1;
782
}
783
encoded = PyUnicode_AsEncodedString(arg, encoding, errors);
784
if (encoded == NULL)
785
return -1;
786
assert(PyBytes_Check(encoded));
787
new = bytearray_iconcat(self, encoded);
788
Py_DECREF(encoded);
789
if (new == NULL)
790
return -1;
791
Py_DECREF(new);
792
return 0;
793
}
794
795
/* If it's not unicode, there can't be encoding or errors */
796
if (encoding != NULL || errors != NULL) {
797
PyErr_SetString(PyExc_TypeError,
798
encoding != NULL ?
799
"encoding without a string argument" :
800
"errors without a string argument");
801
return -1;
802
}
803
804
/* Is it an int? */
805
if (_PyIndex_Check(arg)) {
806
count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
807
if (count == -1 && PyErr_Occurred()) {
808
if (!PyErr_ExceptionMatches(PyExc_TypeError))
809
return -1;
810
PyErr_Clear(); /* fall through */
811
}
812
else {
813
if (count < 0) {
814
PyErr_SetString(PyExc_ValueError, "negative count");
815
return -1;
816
}
817
if (count > 0) {
818
if (PyByteArray_Resize((PyObject *)self, count))
819
return -1;
820
memset(PyByteArray_AS_STRING(self), 0, count);
821
}
822
return 0;
823
}
824
}
825
826
/* Use the buffer API */
827
if (PyObject_CheckBuffer(arg)) {
828
Py_ssize_t size;
829
Py_buffer view;
830
if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
831
return -1;
832
size = view.len;
833
if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
834
if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self),
835
&view, size, 'C') < 0)
836
goto fail;
837
PyBuffer_Release(&view);
838
return 0;
839
fail:
840
PyBuffer_Release(&view);
841
return -1;
842
}
843
844
if (PyList_CheckExact(arg) || PyTuple_CheckExact(arg)) {
845
Py_ssize_t size = PySequence_Fast_GET_SIZE(arg);
846
if (PyByteArray_Resize((PyObject *)self, size) < 0) {
847
return -1;
848
}
849
PyObject **items = PySequence_Fast_ITEMS(arg);
850
char *s = PyByteArray_AS_STRING(self);
851
for (Py_ssize_t i = 0; i < size; i++) {
852
int value;
853
if (!PyLong_CheckExact(items[i])) {
854
/* Resize to 0 and go through slowpath */
855
if (Py_SIZE(self) != 0) {
856
if (PyByteArray_Resize((PyObject *)self, 0) < 0) {
857
return -1;
858
}
859
}
860
goto slowpath;
861
}
862
int rc = _getbytevalue(items[i], &value);
863
if (!rc) {
864
return -1;
865
}
866
s[i] = value;
867
}
868
return 0;
869
}
870
slowpath:
871
/* Get the iterator */
872
it = PyObject_GetIter(arg);
873
if (it == NULL) {
874
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
875
PyErr_Format(PyExc_TypeError,
876
"cannot convert '%.200s' object to bytearray",
877
Py_TYPE(arg)->tp_name);
878
}
879
return -1;
880
}
881
iternext = *Py_TYPE(it)->tp_iternext;
882
883
/* Run the iterator to exhaustion */
884
for (;;) {
885
PyObject *item;
886
int rc, value;
887
888
/* Get the next item */
889
item = iternext(it);
890
if (item == NULL) {
891
if (PyErr_Occurred()) {
892
if (!PyErr_ExceptionMatches(PyExc_StopIteration))
893
goto error;
894
PyErr_Clear();
895
}
896
break;
897
}
898
899
/* Interpret it as an int (__index__) */
900
rc = _getbytevalue(item, &value);
901
Py_DECREF(item);
902
if (!rc)
903
goto error;
904
905
/* Append the byte */
906
if (Py_SIZE(self) + 1 < self->ob_alloc) {
907
Py_SET_SIZE(self, Py_SIZE(self) + 1);
908
PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0';
909
}
910
else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
911
goto error;
912
PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value;
913
}
914
915
/* Clean up and return success */
916
Py_DECREF(it);
917
return 0;
918
919
error:
920
/* Error handling when it != NULL */
921
Py_DECREF(it);
922
return -1;
923
}
924
925
/* Mostly copied from string_repr, but without the
926
"smart quote" functionality. */
927
static PyObject *
928
bytearray_repr(PyByteArrayObject *self)
929
{
930
const char *className = _PyType_Name(Py_TYPE(self));
931
const char *quote_prefix = "(b";
932
const char *quote_postfix = ")";
933
Py_ssize_t length = Py_SIZE(self);
934
/* 6 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
935
Py_ssize_t newsize;
936
PyObject *v;
937
Py_ssize_t i;
938
char *bytes;
939
char c;
940
char *p;
941
int quote;
942
char *test, *start;
943
char *buffer;
944
945
newsize = strlen(className);
946
if (length > (PY_SSIZE_T_MAX - 6 - newsize) / 4) {
947
PyErr_SetString(PyExc_OverflowError,
948
"bytearray object is too large to make repr");
949
return NULL;
950
}
951
952
newsize += 6 + length * 4;
953
buffer = PyObject_Malloc(newsize);
954
if (buffer == NULL) {
955
PyErr_NoMemory();
956
return NULL;
957
}
958
959
/* Figure out which quote to use; single is preferred */
960
quote = '\'';
961
start = PyByteArray_AS_STRING(self);
962
for (test = start; test < start+length; ++test) {
963
if (*test == '"') {
964
quote = '\''; /* back to single */
965
break;
966
}
967
else if (*test == '\'')
968
quote = '"';
969
}
970
971
p = buffer;
972
while (*className)
973
*p++ = *className++;
974
while (*quote_prefix)
975
*p++ = *quote_prefix++;
976
*p++ = quote;
977
978
bytes = PyByteArray_AS_STRING(self);
979
for (i = 0; i < length; i++) {
980
/* There's at least enough room for a hex escape
981
and a closing quote. */
982
assert(newsize - (p - buffer) >= 5);
983
c = bytes[i];
984
if (c == '\'' || c == '\\')
985
*p++ = '\\', *p++ = c;
986
else if (c == '\t')
987
*p++ = '\\', *p++ = 't';
988
else if (c == '\n')
989
*p++ = '\\', *p++ = 'n';
990
else if (c == '\r')
991
*p++ = '\\', *p++ = 'r';
992
else if (c == 0)
993
*p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
994
else if (c < ' ' || c >= 0x7f) {
995
*p++ = '\\';
996
*p++ = 'x';
997
*p++ = Py_hexdigits[(c & 0xf0) >> 4];
998
*p++ = Py_hexdigits[c & 0xf];
999
}
1000
else
1001
*p++ = c;
1002
}
1003
assert(newsize - (p - buffer) >= 1);
1004
*p++ = quote;
1005
while (*quote_postfix) {
1006
*p++ = *quote_postfix++;
1007
}
1008
1009
v = PyUnicode_FromStringAndSize(buffer, p - buffer);
1010
PyObject_Free(buffer);
1011
return v;
1012
}
1013
1014
static PyObject *
1015
bytearray_str(PyObject *op)
1016
{
1017
if (_Py_GetConfig()->bytes_warning) {
1018
if (PyErr_WarnEx(PyExc_BytesWarning,
1019
"str() on a bytearray instance", 1)) {
1020
return NULL;
1021
}
1022
}
1023
return bytearray_repr((PyByteArrayObject*)op);
1024
}
1025
1026
static PyObject *
1027
bytearray_richcompare(PyObject *self, PyObject *other, int op)
1028
{
1029
Py_ssize_t self_size, other_size;
1030
Py_buffer self_bytes, other_bytes;
1031
int cmp;
1032
1033
if (!PyObject_CheckBuffer(self) || !PyObject_CheckBuffer(other)) {
1034
if (PyUnicode_Check(self) || PyUnicode_Check(other)) {
1035
if (_Py_GetConfig()->bytes_warning && (op == Py_EQ || op == Py_NE)) {
1036
if (PyErr_WarnEx(PyExc_BytesWarning,
1037
"Comparison between bytearray and string", 1))
1038
return NULL;
1039
}
1040
}
1041
Py_RETURN_NOTIMPLEMENTED;
1042
}
1043
1044
/* Bytearrays can be compared to anything that supports the buffer API. */
1045
if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
1046
PyErr_Clear();
1047
Py_RETURN_NOTIMPLEMENTED;
1048
}
1049
self_size = self_bytes.len;
1050
1051
if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) {
1052
PyErr_Clear();
1053
PyBuffer_Release(&self_bytes);
1054
Py_RETURN_NOTIMPLEMENTED;
1055
}
1056
other_size = other_bytes.len;
1057
1058
if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
1059
/* Shortcut: if the lengths differ, the objects differ */
1060
PyBuffer_Release(&self_bytes);
1061
PyBuffer_Release(&other_bytes);
1062
return PyBool_FromLong((op == Py_NE));
1063
}
1064
else {
1065
cmp = memcmp(self_bytes.buf, other_bytes.buf,
1066
Py_MIN(self_size, other_size));
1067
/* In ISO C, memcmp() guarantees to use unsigned bytes! */
1068
1069
PyBuffer_Release(&self_bytes);
1070
PyBuffer_Release(&other_bytes);
1071
1072
if (cmp != 0) {
1073
Py_RETURN_RICHCOMPARE(cmp, 0, op);
1074
}
1075
1076
Py_RETURN_RICHCOMPARE(self_size, other_size, op);
1077
}
1078
1079
}
1080
1081
static void
1082
bytearray_dealloc(PyByteArrayObject *self)
1083
{
1084
if (self->ob_exports > 0) {
1085
PyErr_SetString(PyExc_SystemError,
1086
"deallocated bytearray object has exported buffers");
1087
PyErr_Print();
1088
}
1089
if (self->ob_bytes != 0) {
1090
PyObject_Free(self->ob_bytes);
1091
}
1092
Py_TYPE(self)->tp_free((PyObject *)self);
1093
}
1094
1095
1096
/* -------------------------------------------------------------------- */
1097
/* Methods */
1098
1099
#define STRINGLIB_IS_UNICODE 0
1100
#define FASTSEARCH fastsearch
1101
#define STRINGLIB(F) stringlib_##F
1102
#define STRINGLIB_CHAR char
1103
#define STRINGLIB_SIZEOF_CHAR 1
1104
#define STRINGLIB_LEN PyByteArray_GET_SIZE
1105
#define STRINGLIB_STR PyByteArray_AS_STRING
1106
#define STRINGLIB_NEW PyByteArray_FromStringAndSize
1107
#define STRINGLIB_ISSPACE Py_ISSPACE
1108
#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
1109
#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1110
#define STRINGLIB_FAST_MEMCHR memchr
1111
#define STRINGLIB_MUTABLE 1
1112
1113
#include "stringlib/fastsearch.h"
1114
#include "stringlib/count.h"
1115
#include "stringlib/find.h"
1116
#include "stringlib/join.h"
1117
#include "stringlib/partition.h"
1118
#include "stringlib/split.h"
1119
#include "stringlib/ctype.h"
1120
#include "stringlib/transmogrify.h"
1121
1122
1123
static PyObject *
1124
bytearray_find(PyByteArrayObject *self, PyObject *args)
1125
{
1126
return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1127
}
1128
1129
static PyObject *
1130
bytearray_count(PyByteArrayObject *self, PyObject *args)
1131
{
1132
return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1133
}
1134
1135
/*[clinic input]
1136
bytearray.clear
1137
1138
Remove all items from the bytearray.
1139
[clinic start generated code]*/
1140
1141
static PyObject *
1142
bytearray_clear_impl(PyByteArrayObject *self)
1143
/*[clinic end generated code: output=85c2fe6aede0956c input=ed6edae9de447ac4]*/
1144
{
1145
if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1146
return NULL;
1147
Py_RETURN_NONE;
1148
}
1149
1150
/*[clinic input]
1151
bytearray.copy
1152
1153
Return a copy of B.
1154
[clinic start generated code]*/
1155
1156
static PyObject *
1157
bytearray_copy_impl(PyByteArrayObject *self)
1158
/*[clinic end generated code: output=68cfbcfed484c132 input=6597b0c01bccaa9e]*/
1159
{
1160
return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self),
1161
PyByteArray_GET_SIZE(self));
1162
}
1163
1164
static PyObject *
1165
bytearray_index(PyByteArrayObject *self, PyObject *args)
1166
{
1167
return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1168
}
1169
1170
static PyObject *
1171
bytearray_rfind(PyByteArrayObject *self, PyObject *args)
1172
{
1173
return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1174
}
1175
1176
static PyObject *
1177
bytearray_rindex(PyByteArrayObject *self, PyObject *args)
1178
{
1179
return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1180
}
1181
1182
static int
1183
bytearray_contains(PyObject *self, PyObject *arg)
1184
{
1185
return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg);
1186
}
1187
1188
static PyObject *
1189
bytearray_startswith(PyByteArrayObject *self, PyObject *args)
1190
{
1191
return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1192
}
1193
1194
static PyObject *
1195
bytearray_endswith(PyByteArrayObject *self, PyObject *args)
1196
{
1197
return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1198
}
1199
1200
/*[clinic input]
1201
bytearray.removeprefix as bytearray_removeprefix
1202
1203
prefix: Py_buffer
1204
/
1205
1206
Return a bytearray with the given prefix string removed if present.
1207
1208
If the bytearray starts with the prefix string, return
1209
bytearray[len(prefix):]. Otherwise, return a copy of the original
1210
bytearray.
1211
[clinic start generated code]*/
1212
1213
static PyObject *
1214
bytearray_removeprefix_impl(PyByteArrayObject *self, Py_buffer *prefix)
1215
/*[clinic end generated code: output=6cabc585e7f502e0 input=968aada38aedd262]*/
1216
{
1217
const char *self_start = PyByteArray_AS_STRING(self);
1218
Py_ssize_t self_len = PyByteArray_GET_SIZE(self);
1219
const char *prefix_start = prefix->buf;
1220
Py_ssize_t prefix_len = prefix->len;
1221
1222
if (self_len >= prefix_len
1223
&& memcmp(self_start, prefix_start, prefix_len) == 0)
1224
{
1225
return PyByteArray_FromStringAndSize(self_start + prefix_len,
1226
self_len - prefix_len);
1227
}
1228
1229
return PyByteArray_FromStringAndSize(self_start, self_len);
1230
}
1231
1232
/*[clinic input]
1233
bytearray.removesuffix as bytearray_removesuffix
1234
1235
suffix: Py_buffer
1236
/
1237
1238
Return a bytearray with the given suffix string removed if present.
1239
1240
If the bytearray ends with the suffix string and that suffix is not
1241
empty, return bytearray[:-len(suffix)]. Otherwise, return a copy of
1242
the original bytearray.
1243
[clinic start generated code]*/
1244
1245
static PyObject *
1246
bytearray_removesuffix_impl(PyByteArrayObject *self, Py_buffer *suffix)
1247
/*[clinic end generated code: output=2bc8cfb79de793d3 input=c1827e810b2f6b99]*/
1248
{
1249
const char *self_start = PyByteArray_AS_STRING(self);
1250
Py_ssize_t self_len = PyByteArray_GET_SIZE(self);
1251
const char *suffix_start = suffix->buf;
1252
Py_ssize_t suffix_len = suffix->len;
1253
1254
if (self_len >= suffix_len
1255
&& memcmp(self_start + self_len - suffix_len,
1256
suffix_start, suffix_len) == 0)
1257
{
1258
return PyByteArray_FromStringAndSize(self_start,
1259
self_len - suffix_len);
1260
}
1261
1262
return PyByteArray_FromStringAndSize(self_start, self_len);
1263
}
1264
1265
1266
/*[clinic input]
1267
bytearray.translate
1268
1269
table: object
1270
Translation table, which must be a bytes object of length 256.
1271
/
1272
delete as deletechars: object(c_default="NULL") = b''
1273
1274
Return a copy with each character mapped by the given translation table.
1275
1276
All characters occurring in the optional argument delete are removed.
1277
The remaining characters are mapped through the given translation table.
1278
[clinic start generated code]*/
1279
1280
static PyObject *
1281
bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
1282
PyObject *deletechars)
1283
/*[clinic end generated code: output=b6a8f01c2a74e446 input=cfff956d4d127a9b]*/
1284
{
1285
char *input, *output;
1286
const char *table_chars;
1287
Py_ssize_t i, c;
1288
PyObject *input_obj = (PyObject*)self;
1289
const char *output_start;
1290
Py_ssize_t inlen;
1291
PyObject *result = NULL;
1292
int trans_table[256];
1293
Py_buffer vtable, vdel;
1294
1295
if (table == Py_None) {
1296
table_chars = NULL;
1297
table = NULL;
1298
} else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) {
1299
return NULL;
1300
} else {
1301
if (vtable.len != 256) {
1302
PyErr_SetString(PyExc_ValueError,
1303
"translation table must be 256 characters long");
1304
PyBuffer_Release(&vtable);
1305
return NULL;
1306
}
1307
table_chars = (const char*)vtable.buf;
1308
}
1309
1310
if (deletechars != NULL) {
1311
if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) {
1312
if (table != NULL)
1313
PyBuffer_Release(&vtable);
1314
return NULL;
1315
}
1316
}
1317
else {
1318
vdel.buf = NULL;
1319
vdel.len = 0;
1320
}
1321
1322
inlen = PyByteArray_GET_SIZE(input_obj);
1323
result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1324
if (result == NULL)
1325
goto done;
1326
output_start = output = PyByteArray_AS_STRING(result);
1327
input = PyByteArray_AS_STRING(input_obj);
1328
1329
if (vdel.len == 0 && table_chars != NULL) {
1330
/* If no deletions are required, use faster code */
1331
for (i = inlen; --i >= 0; ) {
1332
c = Py_CHARMASK(*input++);
1333
*output++ = table_chars[c];
1334
}
1335
goto done;
1336
}
1337
1338
if (table_chars == NULL) {
1339
for (i = 0; i < 256; i++)
1340
trans_table[i] = Py_CHARMASK(i);
1341
} else {
1342
for (i = 0; i < 256; i++)
1343
trans_table[i] = Py_CHARMASK(table_chars[i]);
1344
}
1345
1346
for (i = 0; i < vdel.len; i++)
1347
trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1348
1349
for (i = inlen; --i >= 0; ) {
1350
c = Py_CHARMASK(*input++);
1351
if (trans_table[c] != -1)
1352
*output++ = (char)trans_table[c];
1353
}
1354
/* Fix the size of the resulting bytearray */
1355
if (inlen > 0)
1356
if (PyByteArray_Resize(result, output - output_start) < 0) {
1357
Py_CLEAR(result);
1358
goto done;
1359
}
1360
1361
done:
1362
if (table != NULL)
1363
PyBuffer_Release(&vtable);
1364
if (deletechars != NULL)
1365
PyBuffer_Release(&vdel);
1366
return result;
1367
}
1368
1369
1370
/*[clinic input]
1371
1372
@staticmethod
1373
bytearray.maketrans
1374
1375
frm: Py_buffer
1376
to: Py_buffer
1377
/
1378
1379
Return a translation table usable for the bytes or bytearray translate method.
1380
1381
The returned table will be one where each byte in frm is mapped to the byte at
1382
the same position in to.
1383
1384
The bytes objects frm and to must be of the same length.
1385
[clinic start generated code]*/
1386
1387
static PyObject *
1388
bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to)
1389
/*[clinic end generated code: output=1df267d99f56b15e input=b10de38c85950a63]*/
1390
{
1391
return _Py_bytes_maketrans(frm, to);
1392
}
1393
1394
1395
/*[clinic input]
1396
bytearray.replace
1397
1398
old: Py_buffer
1399
new: Py_buffer
1400
count: Py_ssize_t = -1
1401
Maximum number of occurrences to replace.
1402
-1 (the default value) means replace all occurrences.
1403
/
1404
1405
Return a copy with all occurrences of substring old replaced by new.
1406
1407
If the optional argument count is given, only the first count occurrences are
1408
replaced.
1409
[clinic start generated code]*/
1410
1411
static PyObject *
1412
bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
1413
Py_buffer *new, Py_ssize_t count)
1414
/*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/
1415
{
1416
return stringlib_replace((PyObject *)self,
1417
(const char *)old->buf, old->len,
1418
(const char *)new->buf, new->len, count);
1419
}
1420
1421
/*[clinic input]
1422
bytearray.split
1423
1424
sep: object = None
1425
The delimiter according which to split the bytearray.
1426
None (the default value) means split on ASCII whitespace characters
1427
(space, tab, return, newline, formfeed, vertical tab).
1428
maxsplit: Py_ssize_t = -1
1429
Maximum number of splits to do.
1430
-1 (the default value) means no limit.
1431
1432
Return a list of the sections in the bytearray, using sep as the delimiter.
1433
[clinic start generated code]*/
1434
1435
static PyObject *
1436
bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
1437
Py_ssize_t maxsplit)
1438
/*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/
1439
{
1440
Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
1441
const char *s = PyByteArray_AS_STRING(self), *sub;
1442
PyObject *list;
1443
Py_buffer vsub;
1444
1445
if (maxsplit < 0)
1446
maxsplit = PY_SSIZE_T_MAX;
1447
1448
if (sep == Py_None)
1449
return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
1450
1451
if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
1452
return NULL;
1453
sub = vsub.buf;
1454
n = vsub.len;
1455
1456
list = stringlib_split(
1457
(PyObject*) self, s, len, sub, n, maxsplit
1458
);
1459
PyBuffer_Release(&vsub);
1460
return list;
1461
}
1462
1463
/*[clinic input]
1464
bytearray.partition
1465
1466
sep: object
1467
/
1468
1469
Partition the bytearray into three parts using the given separator.
1470
1471
This will search for the separator sep in the bytearray. If the separator is
1472
found, returns a 3-tuple containing the part before the separator, the
1473
separator itself, and the part after it as new bytearray objects.
1474
1475
If the separator is not found, returns a 3-tuple containing the copy of the
1476
original bytearray object and two empty bytearray objects.
1477
[clinic start generated code]*/
1478
1479
static PyObject *
1480
bytearray_partition(PyByteArrayObject *self, PyObject *sep)
1481
/*[clinic end generated code: output=45d2525ddd35f957 input=8f644749ee4fc83a]*/
1482
{
1483
PyObject *bytesep, *result;
1484
1485
bytesep = _PyByteArray_FromBufferObject(sep);
1486
if (! bytesep)
1487
return NULL;
1488
1489
result = stringlib_partition(
1490
(PyObject*) self,
1491
PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1492
bytesep,
1493
PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1494
);
1495
1496
Py_DECREF(bytesep);
1497
return result;
1498
}
1499
1500
/*[clinic input]
1501
bytearray.rpartition
1502
1503
sep: object
1504
/
1505
1506
Partition the bytearray into three parts using the given separator.
1507
1508
This will search for the separator sep in the bytearray, starting at the end.
1509
If the separator is found, returns a 3-tuple containing the part before the
1510
separator, the separator itself, and the part after it as new bytearray
1511
objects.
1512
1513
If the separator is not found, returns a 3-tuple containing two empty bytearray
1514
objects and the copy of the original bytearray object.
1515
[clinic start generated code]*/
1516
1517
static PyObject *
1518
bytearray_rpartition(PyByteArrayObject *self, PyObject *sep)
1519
/*[clinic end generated code: output=440de3c9426115e8 input=7e3df3e6cb8fa0ac]*/
1520
{
1521
PyObject *bytesep, *result;
1522
1523
bytesep = _PyByteArray_FromBufferObject(sep);
1524
if (! bytesep)
1525
return NULL;
1526
1527
result = stringlib_rpartition(
1528
(PyObject*) self,
1529
PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1530
bytesep,
1531
PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1532
);
1533
1534
Py_DECREF(bytesep);
1535
return result;
1536
}
1537
1538
/*[clinic input]
1539
bytearray.rsplit = bytearray.split
1540
1541
Return a list of the sections in the bytearray, using sep as the delimiter.
1542
1543
Splitting is done starting at the end of the bytearray and working to the front.
1544
[clinic start generated code]*/
1545
1546
static PyObject *
1547
bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
1548
Py_ssize_t maxsplit)
1549
/*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/
1550
{
1551
Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
1552
const char *s = PyByteArray_AS_STRING(self), *sub;
1553
PyObject *list;
1554
Py_buffer vsub;
1555
1556
if (maxsplit < 0)
1557
maxsplit = PY_SSIZE_T_MAX;
1558
1559
if (sep == Py_None)
1560
return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
1561
1562
if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
1563
return NULL;
1564
sub = vsub.buf;
1565
n = vsub.len;
1566
1567
list = stringlib_rsplit(
1568
(PyObject*) self, s, len, sub, n, maxsplit
1569
);
1570
PyBuffer_Release(&vsub);
1571
return list;
1572
}
1573
1574
/*[clinic input]
1575
bytearray.reverse
1576
1577
Reverse the order of the values in B in place.
1578
[clinic start generated code]*/
1579
1580
static PyObject *
1581
bytearray_reverse_impl(PyByteArrayObject *self)
1582
/*[clinic end generated code: output=9f7616f29ab309d3 input=543356319fc78557]*/
1583
{
1584
char swap, *head, *tail;
1585
Py_ssize_t i, j, n = Py_SIZE(self);
1586
1587
j = n / 2;
1588
head = PyByteArray_AS_STRING(self);
1589
tail = head + n - 1;
1590
for (i = 0; i < j; i++) {
1591
swap = *head;
1592
*head++ = *tail;
1593
*tail-- = swap;
1594
}
1595
1596
Py_RETURN_NONE;
1597
}
1598
1599
1600
/*[python input]
1601
class bytesvalue_converter(CConverter):
1602
type = 'int'
1603
converter = '_getbytevalue'
1604
[python start generated code]*/
1605
/*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/
1606
1607
1608
/*[clinic input]
1609
bytearray.insert
1610
1611
index: Py_ssize_t
1612
The index where the value is to be inserted.
1613
item: bytesvalue
1614
The item to be inserted.
1615
/
1616
1617
Insert a single item into the bytearray before the given index.
1618
[clinic start generated code]*/
1619
1620
static PyObject *
1621
bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item)
1622
/*[clinic end generated code: output=76c775a70e7b07b7 input=b2b5d07e9de6c070]*/
1623
{
1624
Py_ssize_t n = Py_SIZE(self);
1625
char *buf;
1626
1627
if (n == PY_SSIZE_T_MAX) {
1628
PyErr_SetString(PyExc_OverflowError,
1629
"cannot add more objects to bytearray");
1630
return NULL;
1631
}
1632
if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1633
return NULL;
1634
buf = PyByteArray_AS_STRING(self);
1635
1636
if (index < 0) {
1637
index += n;
1638
if (index < 0)
1639
index = 0;
1640
}
1641
if (index > n)
1642
index = n;
1643
memmove(buf + index + 1, buf + index, n - index);
1644
buf[index] = item;
1645
1646
Py_RETURN_NONE;
1647
}
1648
1649
/*[clinic input]
1650
bytearray.append
1651
1652
item: bytesvalue
1653
The item to be appended.
1654
/
1655
1656
Append a single item to the end of the bytearray.
1657
[clinic start generated code]*/
1658
1659
static PyObject *
1660
bytearray_append_impl(PyByteArrayObject *self, int item)
1661
/*[clinic end generated code: output=a154e19ed1886cb6 input=20d6bec3d1340593]*/
1662
{
1663
Py_ssize_t n = Py_SIZE(self);
1664
1665
if (n == PY_SSIZE_T_MAX) {
1666
PyErr_SetString(PyExc_OverflowError,
1667
"cannot add more objects to bytearray");
1668
return NULL;
1669
}
1670
if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1671
return NULL;
1672
1673
PyByteArray_AS_STRING(self)[n] = item;
1674
1675
Py_RETURN_NONE;
1676
}
1677
1678
/*[clinic input]
1679
bytearray.extend
1680
1681
iterable_of_ints: object
1682
The iterable of items to append.
1683
/
1684
1685
Append all the items from the iterator or sequence to the end of the bytearray.
1686
[clinic start generated code]*/
1687
1688
static PyObject *
1689
bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
1690
/*[clinic end generated code: output=98155dbe249170b1 input=c617b3a93249ba28]*/
1691
{
1692
PyObject *it, *item, *bytearray_obj;
1693
Py_ssize_t buf_size = 0, len = 0;
1694
int value;
1695
char *buf;
1696
1697
/* bytearray_setslice code only accepts something supporting PEP 3118. */
1698
if (PyObject_CheckBuffer(iterable_of_ints)) {
1699
if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_ints) == -1)
1700
return NULL;
1701
1702
Py_RETURN_NONE;
1703
}
1704
1705
it = PyObject_GetIter(iterable_of_ints);
1706
if (it == NULL) {
1707
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
1708
PyErr_Format(PyExc_TypeError,
1709
"can't extend bytearray with %.100s",
1710
Py_TYPE(iterable_of_ints)->tp_name);
1711
}
1712
return NULL;
1713
}
1714
1715
/* Try to determine the length of the argument. 32 is arbitrary. */
1716
buf_size = PyObject_LengthHint(iterable_of_ints, 32);
1717
if (buf_size == -1) {
1718
Py_DECREF(it);
1719
return NULL;
1720
}
1721
1722
bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
1723
if (bytearray_obj == NULL) {
1724
Py_DECREF(it);
1725
return NULL;
1726
}
1727
buf = PyByteArray_AS_STRING(bytearray_obj);
1728
1729
while ((item = PyIter_Next(it)) != NULL) {
1730
if (! _getbytevalue(item, &value)) {
1731
Py_DECREF(item);
1732
Py_DECREF(it);
1733
Py_DECREF(bytearray_obj);
1734
return NULL;
1735
}
1736
buf[len++] = value;
1737
Py_DECREF(item);
1738
1739
if (len >= buf_size) {
1740
Py_ssize_t addition;
1741
if (len == PY_SSIZE_T_MAX) {
1742
Py_DECREF(it);
1743
Py_DECREF(bytearray_obj);
1744
return PyErr_NoMemory();
1745
}
1746
addition = len >> 1;
1747
if (addition > PY_SSIZE_T_MAX - len - 1)
1748
buf_size = PY_SSIZE_T_MAX;
1749
else
1750
buf_size = len + addition + 1;
1751
if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
1752
Py_DECREF(it);
1753
Py_DECREF(bytearray_obj);
1754
return NULL;
1755
}
1756
/* Recompute the `buf' pointer, since the resizing operation may
1757
have invalidated it. */
1758
buf = PyByteArray_AS_STRING(bytearray_obj);
1759
}
1760
}
1761
Py_DECREF(it);
1762
1763
if (PyErr_Occurred()) {
1764
Py_DECREF(bytearray_obj);
1765
return NULL;
1766
}
1767
1768
/* Resize down to exact size. */
1769
if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) {
1770
Py_DECREF(bytearray_obj);
1771
return NULL;
1772
}
1773
1774
if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) {
1775
Py_DECREF(bytearray_obj);
1776
return NULL;
1777
}
1778
Py_DECREF(bytearray_obj);
1779
1780
assert(!PyErr_Occurred());
1781
Py_RETURN_NONE;
1782
}
1783
1784
/*[clinic input]
1785
bytearray.pop
1786
1787
index: Py_ssize_t = -1
1788
The index from where to remove the item.
1789
-1 (the default value) means remove the last item.
1790
/
1791
1792
Remove and return a single item from B.
1793
1794
If no index argument is given, will pop the last item.
1795
[clinic start generated code]*/
1796
1797
static PyObject *
1798
bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index)
1799
/*[clinic end generated code: output=e0ccd401f8021da8 input=3591df2d06c0d237]*/
1800
{
1801
int value;
1802
Py_ssize_t n = Py_SIZE(self);
1803
char *buf;
1804
1805
if (n == 0) {
1806
PyErr_SetString(PyExc_IndexError,
1807
"pop from empty bytearray");
1808
return NULL;
1809
}
1810
if (index < 0)
1811
index += Py_SIZE(self);
1812
if (index < 0 || index >= Py_SIZE(self)) {
1813
PyErr_SetString(PyExc_IndexError, "pop index out of range");
1814
return NULL;
1815
}
1816
if (!_canresize(self))
1817
return NULL;
1818
1819
buf = PyByteArray_AS_STRING(self);
1820
value = buf[index];
1821
memmove(buf + index, buf + index + 1, n - index);
1822
if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1823
return NULL;
1824
1825
return _PyLong_FromUnsignedChar((unsigned char)value);
1826
}
1827
1828
/*[clinic input]
1829
bytearray.remove
1830
1831
value: bytesvalue
1832
The value to remove.
1833
/
1834
1835
Remove the first occurrence of a value in the bytearray.
1836
[clinic start generated code]*/
1837
1838
static PyObject *
1839
bytearray_remove_impl(PyByteArrayObject *self, int value)
1840
/*[clinic end generated code: output=d659e37866709c13 input=121831240cd51ddf]*/
1841
{
1842
Py_ssize_t where, n = Py_SIZE(self);
1843
char *buf = PyByteArray_AS_STRING(self);
1844
1845
where = stringlib_find_char(buf, n, value);
1846
if (where < 0) {
1847
PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
1848
return NULL;
1849
}
1850
if (!_canresize(self))
1851
return NULL;
1852
1853
memmove(buf + where, buf + where + 1, n - where);
1854
if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1855
return NULL;
1856
1857
Py_RETURN_NONE;
1858
}
1859
1860
#define LEFTSTRIP 0
1861
#define RIGHTSTRIP 1
1862
#define BOTHSTRIP 2
1863
1864
static PyObject*
1865
bytearray_strip_impl_helper(PyByteArrayObject* self, PyObject* bytes, int striptype)
1866
{
1867
Py_ssize_t mysize, byteslen;
1868
const char* myptr;
1869
const char* bytesptr;
1870
Py_buffer vbytes;
1871
1872
if (bytes == Py_None) {
1873
bytesptr = "\t\n\r\f\v ";
1874
byteslen = 6;
1875
}
1876
else {
1877
if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
1878
return NULL;
1879
bytesptr = (const char*)vbytes.buf;
1880
byteslen = vbytes.len;
1881
}
1882
myptr = PyByteArray_AS_STRING(self);
1883
mysize = Py_SIZE(self);
1884
1885
Py_ssize_t left = 0;
1886
if (striptype != RIGHTSTRIP) {
1887
while (left < mysize && memchr(bytesptr, (unsigned char)myptr[left], byteslen))
1888
left++;
1889
}
1890
Py_ssize_t right = mysize;
1891
if (striptype != LEFTSTRIP) {
1892
do {
1893
right--;
1894
} while (right >= left && memchr(bytesptr, (unsigned char)myptr[right], byteslen));
1895
right++;
1896
}
1897
if (bytes != Py_None)
1898
PyBuffer_Release(&vbytes);
1899
return PyByteArray_FromStringAndSize(myptr + left, right - left);
1900
}
1901
1902
/*[clinic input]
1903
bytearray.strip
1904
1905
bytes: object = None
1906
/
1907
1908
Strip leading and trailing bytes contained in the argument.
1909
1910
If the argument is omitted or None, strip leading and trailing ASCII whitespace.
1911
[clinic start generated code]*/
1912
1913
static PyObject *
1914
bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
1915
/*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
1916
{
1917
return bytearray_strip_impl_helper(self, bytes, BOTHSTRIP);
1918
}
1919
1920
/*[clinic input]
1921
bytearray.lstrip
1922
1923
bytes: object = None
1924
/
1925
1926
Strip leading bytes contained in the argument.
1927
1928
If the argument is omitted or None, strip leading ASCII whitespace.
1929
[clinic start generated code]*/
1930
1931
static PyObject *
1932
bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
1933
/*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
1934
{
1935
return bytearray_strip_impl_helper(self, bytes, LEFTSTRIP);
1936
}
1937
1938
/*[clinic input]
1939
bytearray.rstrip
1940
1941
bytes: object = None
1942
/
1943
1944
Strip trailing bytes contained in the argument.
1945
1946
If the argument is omitted or None, strip trailing ASCII whitespace.
1947
[clinic start generated code]*/
1948
1949
static PyObject *
1950
bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
1951
/*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
1952
{
1953
return bytearray_strip_impl_helper(self, bytes, RIGHTSTRIP);
1954
}
1955
1956
/*[clinic input]
1957
bytearray.decode
1958
1959
encoding: str(c_default="NULL") = 'utf-8'
1960
The encoding with which to decode the bytearray.
1961
errors: str(c_default="NULL") = 'strict'
1962
The error handling scheme to use for the handling of decoding errors.
1963
The default is 'strict' meaning that decoding errors raise a
1964
UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
1965
as well as any other name registered with codecs.register_error that
1966
can handle UnicodeDecodeErrors.
1967
1968
Decode the bytearray using the codec registered for encoding.
1969
[clinic start generated code]*/
1970
1971
static PyObject *
1972
bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
1973
const char *errors)
1974
/*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/
1975
{
1976
if (encoding == NULL)
1977
encoding = PyUnicode_GetDefaultEncoding();
1978
return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
1979
}
1980
1981
PyDoc_STRVAR(alloc_doc,
1982
"B.__alloc__() -> int\n\
1983
\n\
1984
Return the number of bytes actually allocated.");
1985
1986
static PyObject *
1987
bytearray_alloc(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
1988
{
1989
return PyLong_FromSsize_t(self->ob_alloc);
1990
}
1991
1992
/*[clinic input]
1993
bytearray.join
1994
1995
iterable_of_bytes: object
1996
/
1997
1998
Concatenate any number of bytes/bytearray objects.
1999
2000
The bytearray whose method is called is inserted in between each pair.
2001
2002
The result is returned as a new bytearray object.
2003
[clinic start generated code]*/
2004
2005
static PyObject *
2006
bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes)
2007
/*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/
2008
{
2009
return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
2010
}
2011
2012
/*[clinic input]
2013
bytearray.splitlines
2014
2015
keepends: bool = False
2016
2017
Return a list of the lines in the bytearray, breaking at line boundaries.
2018
2019
Line breaks are not included in the resulting list unless keepends is given and
2020
true.
2021
[clinic start generated code]*/
2022
2023
static PyObject *
2024
bytearray_splitlines_impl(PyByteArrayObject *self, int keepends)
2025
/*[clinic end generated code: output=4223c94b895f6ad9 input=66b2dcdea8d093bf]*/
2026
{
2027
return stringlib_splitlines(
2028
(PyObject*) self, PyByteArray_AS_STRING(self),
2029
PyByteArray_GET_SIZE(self), keepends
2030
);
2031
}
2032
2033
/*[clinic input]
2034
@classmethod
2035
bytearray.fromhex
2036
2037
string: unicode
2038
/
2039
2040
Create a bytearray object from a string of hexadecimal numbers.
2041
2042
Spaces between two numbers are accepted.
2043
Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')
2044
[clinic start generated code]*/
2045
2046
static PyObject *
2047
bytearray_fromhex_impl(PyTypeObject *type, PyObject *string)
2048
/*[clinic end generated code: output=8f0f0b6d30fb3ba0 input=f033a16d1fb21f48]*/
2049
{
2050
PyObject *result = _PyBytes_FromHex(string, type == &PyByteArray_Type);
2051
if (type != &PyByteArray_Type && result != NULL) {
2052
Py_SETREF(result, PyObject_CallOneArg((PyObject *)type, result));
2053
}
2054
return result;
2055
}
2056
2057
/*[clinic input]
2058
bytearray.hex
2059
2060
sep: object = NULL
2061
An optional single character or byte to separate hex bytes.
2062
bytes_per_sep: int = 1
2063
How many bytes between separators. Positive values count from the
2064
right, negative values count from the left.
2065
2066
Create a string of hexadecimal numbers from a bytearray object.
2067
2068
Example:
2069
>>> value = bytearray([0xb9, 0x01, 0xef])
2070
>>> value.hex()
2071
'b901ef'
2072
>>> value.hex(':')
2073
'b9:01:ef'
2074
>>> value.hex(':', 2)
2075
'b9:01ef'
2076
>>> value.hex(':', -2)
2077
'b901:ef'
2078
[clinic start generated code]*/
2079
2080
static PyObject *
2081
bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep)
2082
/*[clinic end generated code: output=29c4e5ef72c565a0 input=808667e49bcccb54]*/
2083
{
2084
char* argbuf = PyByteArray_AS_STRING(self);
2085
Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
2086
return _Py_strhex_with_sep(argbuf, arglen, sep, bytes_per_sep);
2087
}
2088
2089
static PyObject *
2090
_common_reduce(PyByteArrayObject *self, int proto)
2091
{
2092
PyObject *state;
2093
const char *buf;
2094
2095
state = _PyObject_GetState((PyObject *)self);
2096
if (state == NULL) {
2097
return NULL;
2098
}
2099
2100
if (!Py_SIZE(self)) {
2101
return Py_BuildValue("(O()N)", Py_TYPE(self), state);
2102
}
2103
buf = PyByteArray_AS_STRING(self);
2104
if (proto < 3) {
2105
/* use str based reduction for backwards compatibility with Python 2.x */
2106
PyObject *latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
2107
return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", state);
2108
}
2109
else {
2110
/* use more efficient byte based reduction */
2111
return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), state);
2112
}
2113
}
2114
2115
/*[clinic input]
2116
bytearray.__reduce__ as bytearray_reduce
2117
2118
Return state information for pickling.
2119
[clinic start generated code]*/
2120
2121
static PyObject *
2122
bytearray_reduce_impl(PyByteArrayObject *self)
2123
/*[clinic end generated code: output=52bf304086464cab input=44b5737ada62dd3f]*/
2124
{
2125
return _common_reduce(self, 2);
2126
}
2127
2128
/*[clinic input]
2129
bytearray.__reduce_ex__ as bytearray_reduce_ex
2130
2131
proto: int = 0
2132
/
2133
2134
Return state information for pickling.
2135
[clinic start generated code]*/
2136
2137
static PyObject *
2138
bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto)
2139
/*[clinic end generated code: output=52eac33377197520 input=f129bc1a1aa151ee]*/
2140
{
2141
return _common_reduce(self, proto);
2142
}
2143
2144
/*[clinic input]
2145
bytearray.__sizeof__ as bytearray_sizeof
2146
2147
Returns the size of the bytearray object in memory, in bytes.
2148
[clinic start generated code]*/
2149
2150
static PyObject *
2151
bytearray_sizeof_impl(PyByteArrayObject *self)
2152
/*[clinic end generated code: output=738abdd17951c427 input=e27320fd98a4bc5a]*/
2153
{
2154
size_t res = _PyObject_SIZE(Py_TYPE(self));
2155
res += (size_t)self->ob_alloc * sizeof(char);
2156
return PyLong_FromSize_t(res);
2157
}
2158
2159
static PySequenceMethods bytearray_as_sequence = {
2160
(lenfunc)bytearray_length, /* sq_length */
2161
(binaryfunc)PyByteArray_Concat, /* sq_concat */
2162
(ssizeargfunc)bytearray_repeat, /* sq_repeat */
2163
(ssizeargfunc)bytearray_getitem, /* sq_item */
2164
0, /* sq_slice */
2165
(ssizeobjargproc)bytearray_setitem, /* sq_ass_item */
2166
0, /* sq_ass_slice */
2167
(objobjproc)bytearray_contains, /* sq_contains */
2168
(binaryfunc)bytearray_iconcat, /* sq_inplace_concat */
2169
(ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */
2170
};
2171
2172
static PyMappingMethods bytearray_as_mapping = {
2173
(lenfunc)bytearray_length,
2174
(binaryfunc)bytearray_subscript,
2175
(objobjargproc)bytearray_ass_subscript,
2176
};
2177
2178
static PyBufferProcs bytearray_as_buffer = {
2179
(getbufferproc)bytearray_getbuffer,
2180
(releasebufferproc)bytearray_releasebuffer,
2181
};
2182
2183
static PyMethodDef
2184
bytearray_methods[] = {
2185
{"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
2186
BYTEARRAY_REDUCE_METHODDEF
2187
BYTEARRAY_REDUCE_EX_METHODDEF
2188
BYTEARRAY_SIZEOF_METHODDEF
2189
BYTEARRAY_APPEND_METHODDEF
2190
{"capitalize", stringlib_capitalize, METH_NOARGS,
2191
_Py_capitalize__doc__},
2192
STRINGLIB_CENTER_METHODDEF
2193
BYTEARRAY_CLEAR_METHODDEF
2194
BYTEARRAY_COPY_METHODDEF
2195
{"count", (PyCFunction)bytearray_count, METH_VARARGS,
2196
_Py_count__doc__},
2197
BYTEARRAY_DECODE_METHODDEF
2198
{"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS,
2199
_Py_endswith__doc__},
2200
STRINGLIB_EXPANDTABS_METHODDEF
2201
BYTEARRAY_EXTEND_METHODDEF
2202
{"find", (PyCFunction)bytearray_find, METH_VARARGS,
2203
_Py_find__doc__},
2204
BYTEARRAY_FROMHEX_METHODDEF
2205
BYTEARRAY_HEX_METHODDEF
2206
{"index", (PyCFunction)bytearray_index, METH_VARARGS, _Py_index__doc__},
2207
BYTEARRAY_INSERT_METHODDEF
2208
{"isalnum", stringlib_isalnum, METH_NOARGS,
2209
_Py_isalnum__doc__},
2210
{"isalpha", stringlib_isalpha, METH_NOARGS,
2211
_Py_isalpha__doc__},
2212
{"isascii", stringlib_isascii, METH_NOARGS,
2213
_Py_isascii__doc__},
2214
{"isdigit", stringlib_isdigit, METH_NOARGS,
2215
_Py_isdigit__doc__},
2216
{"islower", stringlib_islower, METH_NOARGS,
2217
_Py_islower__doc__},
2218
{"isspace", stringlib_isspace, METH_NOARGS,
2219
_Py_isspace__doc__},
2220
{"istitle", stringlib_istitle, METH_NOARGS,
2221
_Py_istitle__doc__},
2222
{"isupper", stringlib_isupper, METH_NOARGS,
2223
_Py_isupper__doc__},
2224
BYTEARRAY_JOIN_METHODDEF
2225
STRINGLIB_LJUST_METHODDEF
2226
{"lower", stringlib_lower, METH_NOARGS, _Py_lower__doc__},
2227
BYTEARRAY_LSTRIP_METHODDEF
2228
BYTEARRAY_MAKETRANS_METHODDEF
2229
BYTEARRAY_PARTITION_METHODDEF
2230
BYTEARRAY_POP_METHODDEF
2231
BYTEARRAY_REMOVE_METHODDEF
2232
BYTEARRAY_REPLACE_METHODDEF
2233
BYTEARRAY_REMOVEPREFIX_METHODDEF
2234
BYTEARRAY_REMOVESUFFIX_METHODDEF
2235
BYTEARRAY_REVERSE_METHODDEF
2236
{"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__},
2237
{"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__},
2238
STRINGLIB_RJUST_METHODDEF
2239
BYTEARRAY_RPARTITION_METHODDEF
2240
BYTEARRAY_RSPLIT_METHODDEF
2241
BYTEARRAY_RSTRIP_METHODDEF
2242
BYTEARRAY_SPLIT_METHODDEF
2243
BYTEARRAY_SPLITLINES_METHODDEF
2244
{"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
2245
_Py_startswith__doc__},
2246
BYTEARRAY_STRIP_METHODDEF
2247
{"swapcase", stringlib_swapcase, METH_NOARGS,
2248
_Py_swapcase__doc__},
2249
{"title", stringlib_title, METH_NOARGS, _Py_title__doc__},
2250
BYTEARRAY_TRANSLATE_METHODDEF
2251
{"upper", stringlib_upper, METH_NOARGS, _Py_upper__doc__},
2252
STRINGLIB_ZFILL_METHODDEF
2253
{NULL}
2254
};
2255
2256
static PyObject *
2257
bytearray_mod(PyObject *v, PyObject *w)
2258
{
2259
if (!PyByteArray_Check(v))
2260
Py_RETURN_NOTIMPLEMENTED;
2261
return _PyBytes_FormatEx(PyByteArray_AS_STRING(v), PyByteArray_GET_SIZE(v), w, 1);
2262
}
2263
2264
static PyNumberMethods bytearray_as_number = {
2265
0, /*nb_add*/
2266
0, /*nb_subtract*/
2267
0, /*nb_multiply*/
2268
bytearray_mod, /*nb_remainder*/
2269
};
2270
2271
PyDoc_STRVAR(bytearray_doc,
2272
"bytearray(iterable_of_ints) -> bytearray\n\
2273
bytearray(string, encoding[, errors]) -> bytearray\n\
2274
bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\
2275
bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\
2276
bytearray() -> empty bytes array\n\
2277
\n\
2278
Construct a mutable bytearray object from:\n\
2279
- an iterable yielding integers in range(256)\n\
2280
- a text string encoded using the specified encoding\n\
2281
- a bytes or a buffer object\n\
2282
- any object implementing the buffer API.\n\
2283
- an integer");
2284
2285
2286
static PyObject *bytearray_iter(PyObject *seq);
2287
2288
PyTypeObject PyByteArray_Type = {
2289
PyVarObject_HEAD_INIT(&PyType_Type, 0)
2290
"bytearray",
2291
sizeof(PyByteArrayObject),
2292
0,
2293
(destructor)bytearray_dealloc, /* tp_dealloc */
2294
0, /* tp_vectorcall_offset */
2295
0, /* tp_getattr */
2296
0, /* tp_setattr */
2297
0, /* tp_as_async */
2298
(reprfunc)bytearray_repr, /* tp_repr */
2299
&bytearray_as_number, /* tp_as_number */
2300
&bytearray_as_sequence, /* tp_as_sequence */
2301
&bytearray_as_mapping, /* tp_as_mapping */
2302
0, /* tp_hash */
2303
0, /* tp_call */
2304
bytearray_str, /* tp_str */
2305
PyObject_GenericGetAttr, /* tp_getattro */
2306
0, /* tp_setattro */
2307
&bytearray_as_buffer, /* tp_as_buffer */
2308
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
2309
_Py_TPFLAGS_MATCH_SELF, /* tp_flags */
2310
bytearray_doc, /* tp_doc */
2311
0, /* tp_traverse */
2312
0, /* tp_clear */
2313
(richcmpfunc)bytearray_richcompare, /* tp_richcompare */
2314
0, /* tp_weaklistoffset */
2315
bytearray_iter, /* tp_iter */
2316
0, /* tp_iternext */
2317
bytearray_methods, /* tp_methods */
2318
0, /* tp_members */
2319
0, /* tp_getset */
2320
0, /* tp_base */
2321
0, /* tp_dict */
2322
0, /* tp_descr_get */
2323
0, /* tp_descr_set */
2324
0, /* tp_dictoffset */
2325
(initproc)bytearray___init__, /* tp_init */
2326
PyType_GenericAlloc, /* tp_alloc */
2327
PyType_GenericNew, /* tp_new */
2328
PyObject_Del, /* tp_free */
2329
};
2330
2331
/*********************** Bytearray Iterator ****************************/
2332
2333
typedef struct {
2334
PyObject_HEAD
2335
Py_ssize_t it_index;
2336
PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
2337
} bytesiterobject;
2338
2339
static void
2340
bytearrayiter_dealloc(bytesiterobject *it)
2341
{
2342
_PyObject_GC_UNTRACK(it);
2343
Py_XDECREF(it->it_seq);
2344
PyObject_GC_Del(it);
2345
}
2346
2347
static int
2348
bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
2349
{
2350
Py_VISIT(it->it_seq);
2351
return 0;
2352
}
2353
2354
static PyObject *
2355
bytearrayiter_next(bytesiterobject *it)
2356
{
2357
PyByteArrayObject *seq;
2358
2359
assert(it != NULL);
2360
seq = it->it_seq;
2361
if (seq == NULL)
2362
return NULL;
2363
assert(PyByteArray_Check(seq));
2364
2365
if (it->it_index < PyByteArray_GET_SIZE(seq)) {
2366
return _PyLong_FromUnsignedChar(
2367
(unsigned char)PyByteArray_AS_STRING(seq)[it->it_index++]);
2368
}
2369
2370
it->it_seq = NULL;
2371
Py_DECREF(seq);
2372
return NULL;
2373
}
2374
2375
static PyObject *
2376
bytearrayiter_length_hint(bytesiterobject *it, PyObject *Py_UNUSED(ignored))
2377
{
2378
Py_ssize_t len = 0;
2379
if (it->it_seq) {
2380
len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
2381
if (len < 0) {
2382
len = 0;
2383
}
2384
}
2385
return PyLong_FromSsize_t(len);
2386
}
2387
2388
PyDoc_STRVAR(length_hint_doc,
2389
"Private method returning an estimate of len(list(it)).");
2390
2391
static PyObject *
2392
bytearrayiter_reduce(bytesiterobject *it, PyObject *Py_UNUSED(ignored))
2393
{
2394
PyObject *iter = _PyEval_GetBuiltin(&_Py_ID(iter));
2395
2396
/* _PyEval_GetBuiltin can invoke arbitrary code,
2397
* call must be before access of iterator pointers.
2398
* see issue #101765 */
2399
2400
if (it->it_seq != NULL) {
2401
return Py_BuildValue("N(O)n", iter, it->it_seq, it->it_index);
2402
} else {
2403
return Py_BuildValue("N(())", iter);
2404
}
2405
}
2406
2407
static PyObject *
2408
bytearrayiter_setstate(bytesiterobject *it, PyObject *state)
2409
{
2410
Py_ssize_t index = PyLong_AsSsize_t(state);
2411
if (index == -1 && PyErr_Occurred())
2412
return NULL;
2413
if (it->it_seq != NULL) {
2414
if (index < 0)
2415
index = 0;
2416
else if (index > PyByteArray_GET_SIZE(it->it_seq))
2417
index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */
2418
it->it_index = index;
2419
}
2420
Py_RETURN_NONE;
2421
}
2422
2423
PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
2424
2425
static PyMethodDef bytearrayiter_methods[] = {
2426
{"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
2427
length_hint_doc},
2428
{"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
2429
bytearray_reduce__doc__},
2430
{"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
2431
setstate_doc},
2432
{NULL, NULL} /* sentinel */
2433
};
2434
2435
PyTypeObject PyByteArrayIter_Type = {
2436
PyVarObject_HEAD_INIT(&PyType_Type, 0)
2437
"bytearray_iterator", /* tp_name */
2438
sizeof(bytesiterobject), /* tp_basicsize */
2439
0, /* tp_itemsize */
2440
/* methods */
2441
(destructor)bytearrayiter_dealloc, /* tp_dealloc */
2442
0, /* tp_vectorcall_offset */
2443
0, /* tp_getattr */
2444
0, /* tp_setattr */
2445
0, /* tp_as_async */
2446
0, /* tp_repr */
2447
0, /* tp_as_number */
2448
0, /* tp_as_sequence */
2449
0, /* tp_as_mapping */
2450
0, /* tp_hash */
2451
0, /* tp_call */
2452
0, /* tp_str */
2453
PyObject_GenericGetAttr, /* tp_getattro */
2454
0, /* tp_setattro */
2455
0, /* tp_as_buffer */
2456
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
2457
0, /* tp_doc */
2458
(traverseproc)bytearrayiter_traverse, /* tp_traverse */
2459
0, /* tp_clear */
2460
0, /* tp_richcompare */
2461
0, /* tp_weaklistoffset */
2462
PyObject_SelfIter, /* tp_iter */
2463
(iternextfunc)bytearrayiter_next, /* tp_iternext */
2464
bytearrayiter_methods, /* tp_methods */
2465
0,
2466
};
2467
2468
static PyObject *
2469
bytearray_iter(PyObject *seq)
2470
{
2471
bytesiterobject *it;
2472
2473
if (!PyByteArray_Check(seq)) {
2474
PyErr_BadInternalCall();
2475
return NULL;
2476
}
2477
it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
2478
if (it == NULL)
2479
return NULL;
2480
it->it_index = 0;
2481
it->it_seq = (PyByteArrayObject *)Py_NewRef(seq);
2482
_PyObject_GC_TRACK(it);
2483
return (PyObject *)it;
2484
}
2485
2486