Coverage Report

Created: 2026-10-31 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Objects/clinic/bytearrayobject.c.h
Line
Count
Source
1
/*[clinic input]
2
preserve
3
[clinic start generated code]*/
4
5
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6
#  include "pycore_gc.h"          // PyGC_Head
7
#endif
8
#include "pycore_abstract.h"      // _PyNumber_Index()
9
#include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION()
10
#include "pycore_modsupport.h"    // _PyArg_UnpackKeywords()
11
#include "pycore_runtime.h"       // _Py_SINGLETON()
12
#include "pycore_unicodeobject.h" // _PyUnicode_AsUTF8NoNUL()
13
14
static PyObject *
15
bytearray_iconcat_impl(PyByteArrayObject *self, PyObject *other);
16
17
static PyObject *
18
bytearray_iconcat(PyObject *self, PyObject *other)
19
14.7M
{
20
14.7M
    PyObject *return_value = NULL;
21
22
14.7M
    Py_BEGIN_CRITICAL_SECTION(self);
23
14.7M
    return_value = bytearray_iconcat_impl((PyByteArrayObject *)self, other);
24
14.7M
    Py_END_CRITICAL_SECTION();
25
26
14.7M
    return return_value;
27
14.7M
}
28
29
static PyObject *
30
bytearray_repeat_impl(PyByteArrayObject *self, Py_ssize_t count);
31
32
static PyObject *
33
bytearray_repeat(PyObject *self, Py_ssize_t count)
34
0
{
35
0
    PyObject *return_value = NULL;
36
37
0
    Py_BEGIN_CRITICAL_SECTION(self);
38
0
    return_value = bytearray_repeat_impl((PyByteArrayObject *)self, count);
39
0
    Py_END_CRITICAL_SECTION();
40
41
0
    return return_value;
42
0
}
43
44
static PyObject *
45
bytearray_irepeat_impl(PyByteArrayObject *self, Py_ssize_t count);
46
47
static PyObject *
48
bytearray_irepeat(PyObject *self, Py_ssize_t count)
49
0
{
50
0
    PyObject *return_value = NULL;
51
52
0
    Py_BEGIN_CRITICAL_SECTION(self);
53
0
    return_value = bytearray_irepeat_impl((PyByteArrayObject *)self, count);
54
0
    Py_END_CRITICAL_SECTION();
55
56
0
    return return_value;
57
0
}
58
59
static PyObject *
60
bytearray_getitem_impl(PyByteArrayObject *self, Py_ssize_t i);
61
62
static PyObject *
63
bytearray_getitem(PyObject *self, Py_ssize_t i)
64
0
{
65
0
    PyObject *return_value = NULL;
66
67
0
    Py_BEGIN_CRITICAL_SECTION(self);
68
0
    return_value = bytearray_getitem_impl((PyByteArrayObject *)self, i);
69
0
    Py_END_CRITICAL_SECTION();
70
71
0
    return return_value;
72
0
}
73
74
static int
75
bytearray_setitem_impl(PyByteArrayObject *self, Py_ssize_t i,
76
                       PyObject *value);
77
78
static int
79
bytearray_setitem(PyObject *self, Py_ssize_t i, PyObject *value)
80
0
{
81
0
    int return_value = -1;
82
83
0
    Py_BEGIN_CRITICAL_SECTION(self);
84
0
    return_value = bytearray_setitem_impl((PyByteArrayObject *)self, i, value);
85
0
    Py_END_CRITICAL_SECTION();
86
87
0
    return return_value;
88
0
}
89
90
static int
91
bytearray___init___impl(PyByteArrayObject *self, PyObject *arg,
92
                        const char *encoding, const char *errors);
93
94
static int
95
bytearray___init___helper(PyObject *self, PyObject *const *args,
96
    Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames)
97
0
{
98
0
    int return_value = -1;
99
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
100
101
0
    #define NUM_KEYWORDS 3
102
0
    static struct {
103
0
        PyGC_Head _this_is_not_used;
104
0
        PyObject_VAR_HEAD
105
0
        Py_hash_t ob_hash;
106
0
        PyObject *ob_item[NUM_KEYWORDS];
107
0
    } _kwtuple = {
108
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
109
0
        .ob_hash = -1,
110
0
        .ob_item = { &_Py_ID(source), &_Py_ID(encoding), &_Py_ID(errors), },
111
0
    };
112
0
    #undef NUM_KEYWORDS
113
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
114
115
    #else  // !Py_BUILD_CORE
116
    #  define KWTUPLE NULL
117
    #endif  // !Py_BUILD_CORE
118
119
0
    static const char * const _keywords[] = {"source", "encoding", "errors", NULL};
120
0
    static _PyArg_Parser _parser = {
121
0
        .keywords = _keywords,
122
0
        .fname = "bytearray",
123
0
        .kwtuple = KWTUPLE,
124
0
    };
125
0
    #undef KWTUPLE
126
0
    PyObject *argsbuf[3];
127
0
    PyObject * const *fastargs;
128
0
    Py_ssize_t noptargs = nargs + nkw - 0;
129
0
    PyObject *arg = NULL;
130
0
    const char *encoding = NULL;
131
0
    const char *errors = NULL;
132
133
0
    fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser,
134
0
            /*minpos*/ 0, /*maxpos*/ 3, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
135
0
    if (!fastargs) {
136
0
        goto exit;
137
0
    }
138
0
    if (!noptargs) {
139
0
        goto skip_optional_pos;
140
0
    }
141
0
    if (fastargs[0]) {
142
0
        arg = fastargs[0];
143
0
        if (!--noptargs) {
144
0
            goto skip_optional_pos;
145
0
        }
146
0
    }
147
0
    if (fastargs[1]) {
148
0
        if (!PyUnicode_Check(fastargs[1])) {
149
0
            _PyArg_BadArgument("bytearray", "argument 'encoding'", "str", fastargs[1]);
150
0
            goto exit;
151
0
        }
152
0
        encoding = _PyUnicode_AsUTF8NoNUL(fastargs[1]);
153
0
        if (encoding == NULL) {
154
0
            goto exit;
155
0
        }
156
0
        if (!--noptargs) {
157
0
            goto skip_optional_pos;
158
0
        }
159
0
    }
160
0
    if (!PyUnicode_Check(fastargs[2])) {
161
0
        _PyArg_BadArgument("bytearray", "argument 'errors'", "str", fastargs[2]);
162
0
        goto exit;
163
0
    }
164
0
    errors = _PyUnicode_AsUTF8NoNUL(fastargs[2]);
165
0
    if (errors == NULL) {
166
0
        goto exit;
167
0
    }
168
0
skip_optional_pos:
169
0
    return_value = bytearray___init___impl((PyByteArrayObject *)self, arg, encoding, errors);
170
171
0
exit:
172
0
    return return_value;
173
0
}
174
175
static int
176
bytearray___init__(PyObject *self, PyObject *args, PyObject *kwargs)
177
0
{
178
0
    return bytearray___init___helper(self, _PyTuple_CAST(args)->ob_item,
179
0
        PyTuple_GET_SIZE(args),
180
0
        kwargs ? PyDict_GET_SIZE(kwargs) : 0,
181
0
        kwargs, NULL);
182
0
}
183
184
static PyObject *
185
bytearray_vectorcall(PyObject *type, PyObject *const *args,
186
    size_t nargsf, PyObject *kwnames)
187
22.1M
{
188
22.1M
    PyObject *return_value = NULL;
189
22.1M
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
190
22.1M
    PyObject *self;
191
22.1M
    int _result;
192
22.1M
    PyObject *arg = NULL;
193
22.1M
    const char *encoding = NULL;
194
22.1M
    const char *errors = NULL;
195
196
22.1M
    assert(_PyType_CAST(type)->tp_init == bytearray___init__);
197
    /* Make sure the type object is immutable: the generated
198
     * vectorcall doesn't deal e.g. with users reassigning __init__. */
199
22.1M
    assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
200
22.1M
    if (kwnames != NULL || nargs > 3) {
201
0
        self = _PyType_CAST(type)->tp_new(_PyType_CAST(type),
202
0
            (PyObject *)&_Py_SINGLETON(tuple_empty), NULL);
203
0
        if (self == NULL) {
204
0
            return NULL;
205
0
        }
206
0
        _result = bytearray___init___helper(self, args, nargs,
207
0
            kwnames ? PyTuple_GET_SIZE(kwnames) : 0,
208
0
            NULL, kwnames);
209
0
        if (_result != 0) {
210
0
            Py_DECREF(self);
211
0
            return NULL;
212
0
        }
213
0
        return self;
214
0
    }
215
22.1M
    if (nargs < 1) {
216
6.88M
        goto skip_optional;
217
6.88M
    }
218
15.2M
    arg = args[0];
219
15.2M
    if (nargs < 2) {
220
15.2M
        goto skip_optional;
221
15.2M
    }
222
0
    if (!PyUnicode_Check(args[1])) {
223
0
        _PyArg_BadArgument("bytearray", "argument 'encoding'", "str", args[1]);
224
0
        goto exit;
225
0
    }
226
0
    encoding = _PyUnicode_AsUTF8NoNUL(args[1]);
227
0
    if (encoding == NULL) {
228
0
        goto exit;
229
0
    }
230
0
    if (nargs < 3) {
231
0
        goto skip_optional;
232
0
    }
233
0
    if (!PyUnicode_Check(args[2])) {
234
0
        _PyArg_BadArgument("bytearray", "argument 'errors'", "str", args[2]);
235
0
        goto exit;
236
0
    }
237
0
    errors = _PyUnicode_AsUTF8NoNUL(args[2]);
238
0
    if (errors == NULL) {
239
0
        goto exit;
240
0
    }
241
22.1M
skip_optional:
242
22.1M
    self = _PyType_CAST(type)->tp_new(_PyType_CAST(type),
243
22.1M
        (PyObject *)&_Py_SINGLETON(tuple_empty), NULL);
244
22.1M
    if (self == NULL) {
245
0
        goto exit;
246
0
    }
247
22.1M
    _result = bytearray___init___impl((PyByteArrayObject *)self, arg, encoding, errors);
248
22.1M
    if (_result != 0) {
249
0
        Py_DECREF(self);
250
0
        goto exit;
251
0
    }
252
22.1M
    return_value = self;
253
254
22.1M
exit:
255
22.1M
    return return_value;
256
22.1M
}
257
258
static PyObject *
259
bytearray_repr_impl(PyObject *op);
260
261
static PyObject *
262
bytearray_repr(PyObject *op)
263
0
{
264
0
    PyObject *return_value = NULL;
265
266
0
    Py_BEGIN_CRITICAL_SECTION(op);
267
0
    return_value = bytearray_repr_impl(op);
268
0
    Py_END_CRITICAL_SECTION();
269
270
0
    return return_value;
271
0
}
272
273
PyDoc_STRVAR(bytearray_find__doc__,
274
"find($self, sub[, start[, end]], /)\n"
275
"--\n"
276
"\n"
277
"Return the lowest index in B where subsection \'sub\' is found, such that \'sub\' is contained within B[start:end].\n"
278
"\n"
279
"  start\n"
280
"    Optional start position. Default: start of the bytes.\n"
281
"  end\n"
282
"    Optional stop position. Default: end of the bytes.\n"
283
"\n"
284
"Return -1 on failure.");
285
286
#define BYTEARRAY_FIND_METHODDEF    \
287
    {"find", _PyCFunction_CAST(bytearray_find), METH_FASTCALL, bytearray_find__doc__},
288
289
static PyObject *
290
bytearray_find_impl(PyByteArrayObject *self, PyObject *sub, Py_ssize_t start,
291
                    Py_ssize_t end);
292
293
static PyObject *
294
bytearray_find(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
295
358k
{
296
358k
    PyObject *return_value = NULL;
297
358k
    PyObject *sub;
298
358k
    Py_ssize_t start = 0;
299
358k
    Py_ssize_t end = PY_SSIZE_T_MAX;
300
301
358k
    if (!_PyArg_CheckPositional("find", nargs, 1, 3)) {
302
0
        goto exit;
303
0
    }
304
358k
    sub = args[0];
305
358k
    if (nargs < 2) {
306
0
        goto skip_optional;
307
0
    }
308
358k
    if (!_PyEval_SliceIndex(args[1], &start)) {
309
0
        goto exit;
310
0
    }
311
358k
    if (nargs < 3) {
312
358k
        goto skip_optional;
313
358k
    }
314
0
    if (!_PyEval_SliceIndex(args[2], &end)) {
315
0
        goto exit;
316
0
    }
317
358k
skip_optional:
318
358k
    Py_BEGIN_CRITICAL_SECTION(self);
319
358k
    return_value = bytearray_find_impl((PyByteArrayObject *)self, sub, start, end);
320
358k
    Py_END_CRITICAL_SECTION();
321
322
358k
exit:
323
358k
    return return_value;
324
358k
}
325
326
PyDoc_STRVAR(bytearray_count__doc__,
327
"count($self, sub[, start[, end]], /)\n"
328
"--\n"
329
"\n"
330
"Return the number of non-overlapping occurrences of subsection \'sub\' in bytes B[start:end].\n"
331
"\n"
332
"  start\n"
333
"    Optional start position. Default: start of the bytes.\n"
334
"  end\n"
335
"    Optional stop position. Default: end of the bytes.");
336
337
#define BYTEARRAY_COUNT_METHODDEF    \
338
    {"count", _PyCFunction_CAST(bytearray_count), METH_FASTCALL, bytearray_count__doc__},
339
340
static PyObject *
341
bytearray_count_impl(PyByteArrayObject *self, PyObject *sub,
342
                     Py_ssize_t start, Py_ssize_t end);
343
344
static PyObject *
345
bytearray_count(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
346
0
{
347
0
    PyObject *return_value = NULL;
348
0
    PyObject *sub;
349
0
    Py_ssize_t start = 0;
350
0
    Py_ssize_t end = PY_SSIZE_T_MAX;
351
352
0
    if (!_PyArg_CheckPositional("count", nargs, 1, 3)) {
353
0
        goto exit;
354
0
    }
355
0
    sub = args[0];
356
0
    if (nargs < 2) {
357
0
        goto skip_optional;
358
0
    }
359
0
    if (!_PyEval_SliceIndex(args[1], &start)) {
360
0
        goto exit;
361
0
    }
362
0
    if (nargs < 3) {
363
0
        goto skip_optional;
364
0
    }
365
0
    if (!_PyEval_SliceIndex(args[2], &end)) {
366
0
        goto exit;
367
0
    }
368
0
skip_optional:
369
0
    Py_BEGIN_CRITICAL_SECTION(self);
370
0
    return_value = bytearray_count_impl((PyByteArrayObject *)self, sub, start, end);
371
0
    Py_END_CRITICAL_SECTION();
372
373
0
exit:
374
0
    return return_value;
375
0
}
376
377
PyDoc_STRVAR(bytearray_clear__doc__,
378
"clear($self, /)\n"
379
"--\n"
380
"\n"
381
"Remove all items from the bytearray.");
382
383
#define BYTEARRAY_CLEAR_METHODDEF    \
384
    {"clear", (PyCFunction)bytearray_clear, METH_NOARGS, bytearray_clear__doc__},
385
386
static PyObject *
387
bytearray_clear_impl(PyByteArrayObject *self);
388
389
static PyObject *
390
bytearray_clear(PyObject *self, PyObject *Py_UNUSED(ignored))
391
0
{
392
0
    return bytearray_clear_impl((PyByteArrayObject *)self);
393
0
}
394
395
PyDoc_STRVAR(bytearray_copy__doc__,
396
"copy($self, /)\n"
397
"--\n"
398
"\n"
399
"Return a copy of B.");
400
401
#define BYTEARRAY_COPY_METHODDEF    \
402
    {"copy", (PyCFunction)bytearray_copy, METH_NOARGS, bytearray_copy__doc__},
403
404
static PyObject *
405
bytearray_copy_impl(PyByteArrayObject *self);
406
407
static PyObject *
408
bytearray_copy(PyObject *self, PyObject *Py_UNUSED(ignored))
409
0
{
410
0
    PyObject *return_value = NULL;
411
412
0
    Py_BEGIN_CRITICAL_SECTION(self);
413
0
    return_value = bytearray_copy_impl((PyByteArrayObject *)self);
414
0
    Py_END_CRITICAL_SECTION();
415
416
0
    return return_value;
417
0
}
418
419
PyDoc_STRVAR(bytearray_index__doc__,
420
"index($self, sub[, start[, end]], /)\n"
421
"--\n"
422
"\n"
423
"Return the lowest index in B where subsection \'sub\' is found, such that \'sub\' is contained within B[start:end].\n"
424
"\n"
425
"  start\n"
426
"    Optional start position. Default: start of the bytes.\n"
427
"  end\n"
428
"    Optional stop position. Default: end of the bytes.\n"
429
"\n"
430
"Raise ValueError if the subsection is not found.");
431
432
#define BYTEARRAY_INDEX_METHODDEF    \
433
    {"index", _PyCFunction_CAST(bytearray_index), METH_FASTCALL, bytearray_index__doc__},
434
435
static PyObject *
436
bytearray_index_impl(PyByteArrayObject *self, PyObject *sub,
437
                     Py_ssize_t start, Py_ssize_t end);
438
439
static PyObject *
440
bytearray_index(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
441
0
{
442
0
    PyObject *return_value = NULL;
443
0
    PyObject *sub;
444
0
    Py_ssize_t start = 0;
445
0
    Py_ssize_t end = PY_SSIZE_T_MAX;
446
447
0
    if (!_PyArg_CheckPositional("index", nargs, 1, 3)) {
448
0
        goto exit;
449
0
    }
450
0
    sub = args[0];
451
0
    if (nargs < 2) {
452
0
        goto skip_optional;
453
0
    }
454
0
    if (!_PyEval_SliceIndex(args[1], &start)) {
455
0
        goto exit;
456
0
    }
457
0
    if (nargs < 3) {
458
0
        goto skip_optional;
459
0
    }
460
0
    if (!_PyEval_SliceIndex(args[2], &end)) {
461
0
        goto exit;
462
0
    }
463
0
skip_optional:
464
0
    Py_BEGIN_CRITICAL_SECTION(self);
465
0
    return_value = bytearray_index_impl((PyByteArrayObject *)self, sub, start, end);
466
0
    Py_END_CRITICAL_SECTION();
467
468
0
exit:
469
0
    return return_value;
470
0
}
471
472
PyDoc_STRVAR(bytearray_rfind__doc__,
473
"rfind($self, sub[, start[, end]], /)\n"
474
"--\n"
475
"\n"
476
"Return the highest index in B where subsection \'sub\' is found, such that \'sub\' is contained within B[start:end].\n"
477
"\n"
478
"  start\n"
479
"    Optional start position. Default: start of the bytes.\n"
480
"  end\n"
481
"    Optional stop position. Default: end of the bytes.\n"
482
"\n"
483
"Return -1 on failure.");
484
485
#define BYTEARRAY_RFIND_METHODDEF    \
486
    {"rfind", _PyCFunction_CAST(bytearray_rfind), METH_FASTCALL, bytearray_rfind__doc__},
487
488
static PyObject *
489
bytearray_rfind_impl(PyByteArrayObject *self, PyObject *sub,
490
                     Py_ssize_t start, Py_ssize_t end);
491
492
static PyObject *
493
bytearray_rfind(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
494
0
{
495
0
    PyObject *return_value = NULL;
496
0
    PyObject *sub;
497
0
    Py_ssize_t start = 0;
498
0
    Py_ssize_t end = PY_SSIZE_T_MAX;
499
500
0
    if (!_PyArg_CheckPositional("rfind", nargs, 1, 3)) {
501
0
        goto exit;
502
0
    }
503
0
    sub = args[0];
504
0
    if (nargs < 2) {
505
0
        goto skip_optional;
506
0
    }
507
0
    if (!_PyEval_SliceIndex(args[1], &start)) {
508
0
        goto exit;
509
0
    }
510
0
    if (nargs < 3) {
511
0
        goto skip_optional;
512
0
    }
513
0
    if (!_PyEval_SliceIndex(args[2], &end)) {
514
0
        goto exit;
515
0
    }
516
0
skip_optional:
517
0
    Py_BEGIN_CRITICAL_SECTION(self);
518
0
    return_value = bytearray_rfind_impl((PyByteArrayObject *)self, sub, start, end);
519
0
    Py_END_CRITICAL_SECTION();
520
521
0
exit:
522
0
    return return_value;
523
0
}
524
525
PyDoc_STRVAR(bytearray_rindex__doc__,
526
"rindex($self, sub[, start[, end]], /)\n"
527
"--\n"
528
"\n"
529
"Return the highest index in B where subsection \'sub\' is found, such that \'sub\' is contained within B[start:end].\n"
530
"\n"
531
"  start\n"
532
"    Optional start position. Default: start of the bytes.\n"
533
"  end\n"
534
"    Optional stop position. Default: end of the bytes.\n"
535
"\n"
536
"Raise ValueError if the subsection is not found.");
537
538
#define BYTEARRAY_RINDEX_METHODDEF    \
539
    {"rindex", _PyCFunction_CAST(bytearray_rindex), METH_FASTCALL, bytearray_rindex__doc__},
540
541
static PyObject *
542
bytearray_rindex_impl(PyByteArrayObject *self, PyObject *sub,
543
                      Py_ssize_t start, Py_ssize_t end);
544
545
static PyObject *
546
bytearray_rindex(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
547
0
{
548
0
    PyObject *return_value = NULL;
549
0
    PyObject *sub;
550
0
    Py_ssize_t start = 0;
551
0
    Py_ssize_t end = PY_SSIZE_T_MAX;
552
553
0
    if (!_PyArg_CheckPositional("rindex", nargs, 1, 3)) {
554
0
        goto exit;
555
0
    }
556
0
    sub = args[0];
557
0
    if (nargs < 2) {
558
0
        goto skip_optional;
559
0
    }
560
0
    if (!_PyEval_SliceIndex(args[1], &start)) {
561
0
        goto exit;
562
0
    }
563
0
    if (nargs < 3) {
564
0
        goto skip_optional;
565
0
    }
566
0
    if (!_PyEval_SliceIndex(args[2], &end)) {
567
0
        goto exit;
568
0
    }
569
0
skip_optional:
570
0
    Py_BEGIN_CRITICAL_SECTION(self);
571
0
    return_value = bytearray_rindex_impl((PyByteArrayObject *)self, sub, start, end);
572
0
    Py_END_CRITICAL_SECTION();
573
574
0
exit:
575
0
    return return_value;
576
0
}
577
578
PyDoc_STRVAR(bytearray_startswith__doc__,
579
"startswith($self, prefix[, start[, end]], /)\n"
580
"--\n"
581
"\n"
582
"Return True if the bytearray starts with the specified prefix, False otherwise.\n"
583
"\n"
584
"  prefix\n"
585
"    A bytes or a tuple of bytes to try.\n"
586
"  start\n"
587
"    Optional start position. Default: start of the bytearray.\n"
588
"  end\n"
589
"    Optional stop position. Default: end of the bytearray.");
590
591
#define BYTEARRAY_STARTSWITH_METHODDEF    \
592
    {"startswith", _PyCFunction_CAST(bytearray_startswith), METH_FASTCALL, bytearray_startswith__doc__},
593
594
static PyObject *
595
bytearray_startswith_impl(PyByteArrayObject *self, PyObject *subobj,
596
                          Py_ssize_t start, Py_ssize_t end);
597
598
static PyObject *
599
bytearray_startswith(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
600
0
{
601
0
    PyObject *return_value = NULL;
602
0
    PyObject *subobj;
603
0
    Py_ssize_t start = 0;
604
0
    Py_ssize_t end = PY_SSIZE_T_MAX;
605
606
0
    if (!_PyArg_CheckPositional("startswith", nargs, 1, 3)) {
607
0
        goto exit;
608
0
    }
609
0
    subobj = args[0];
610
0
    if (nargs < 2) {
611
0
        goto skip_optional;
612
0
    }
613
0
    if (!_PyEval_SliceIndex(args[1], &start)) {
614
0
        goto exit;
615
0
    }
616
0
    if (nargs < 3) {
617
0
        goto skip_optional;
618
0
    }
619
0
    if (!_PyEval_SliceIndex(args[2], &end)) {
620
0
        goto exit;
621
0
    }
622
0
skip_optional:
623
0
    Py_BEGIN_CRITICAL_SECTION(self);
624
0
    return_value = bytearray_startswith_impl((PyByteArrayObject *)self, subobj, start, end);
625
0
    Py_END_CRITICAL_SECTION();
626
627
0
exit:
628
0
    return return_value;
629
0
}
630
631
PyDoc_STRVAR(bytearray_endswith__doc__,
632
"endswith($self, suffix[, start[, end]], /)\n"
633
"--\n"
634
"\n"
635
"Return True if the bytearray ends with the specified suffix, False otherwise.\n"
636
"\n"
637
"  suffix\n"
638
"    A bytes or a tuple of bytes to try.\n"
639
"  start\n"
640
"    Optional start position. Default: start of the bytearray.\n"
641
"  end\n"
642
"    Optional stop position. Default: end of the bytearray.");
643
644
#define BYTEARRAY_ENDSWITH_METHODDEF    \
645
    {"endswith", _PyCFunction_CAST(bytearray_endswith), METH_FASTCALL, bytearray_endswith__doc__},
646
647
static PyObject *
648
bytearray_endswith_impl(PyByteArrayObject *self, PyObject *subobj,
649
                        Py_ssize_t start, Py_ssize_t end);
650
651
static PyObject *
652
bytearray_endswith(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
653
0
{
654
0
    PyObject *return_value = NULL;
655
0
    PyObject *subobj;
656
0
    Py_ssize_t start = 0;
657
0
    Py_ssize_t end = PY_SSIZE_T_MAX;
658
659
0
    if (!_PyArg_CheckPositional("endswith", nargs, 1, 3)) {
660
0
        goto exit;
661
0
    }
662
0
    subobj = args[0];
663
0
    if (nargs < 2) {
664
0
        goto skip_optional;
665
0
    }
666
0
    if (!_PyEval_SliceIndex(args[1], &start)) {
667
0
        goto exit;
668
0
    }
669
0
    if (nargs < 3) {
670
0
        goto skip_optional;
671
0
    }
672
0
    if (!_PyEval_SliceIndex(args[2], &end)) {
673
0
        goto exit;
674
0
    }
675
0
skip_optional:
676
0
    Py_BEGIN_CRITICAL_SECTION(self);
677
0
    return_value = bytearray_endswith_impl((PyByteArrayObject *)self, subobj, start, end);
678
0
    Py_END_CRITICAL_SECTION();
679
680
0
exit:
681
0
    return return_value;
682
0
}
683
684
PyDoc_STRVAR(bytearray_removeprefix__doc__,
685
"removeprefix($self, prefix, /)\n"
686
"--\n"
687
"\n"
688
"Return a bytearray with the given prefix string removed if present.\n"
689
"\n"
690
"If the bytearray starts with the prefix string, return\n"
691
"bytearray[len(prefix):].  Otherwise, return a copy of the original\n"
692
"bytearray.");
693
694
#define BYTEARRAY_REMOVEPREFIX_METHODDEF    \
695
    {"removeprefix", (PyCFunction)bytearray_removeprefix, METH_O, bytearray_removeprefix__doc__},
696
697
static PyObject *
698
bytearray_removeprefix_impl(PyByteArrayObject *self, Py_buffer *prefix);
699
700
static PyObject *
701
bytearray_removeprefix(PyObject *self, PyObject *arg)
702
0
{
703
0
    PyObject *return_value = NULL;
704
0
    Py_buffer prefix = {NULL, NULL};
705
706
0
    if (PyObject_GetBuffer(arg, &prefix, PyBUF_SIMPLE) != 0) {
707
0
        goto exit;
708
0
    }
709
0
    Py_BEGIN_CRITICAL_SECTION(self);
710
0
    return_value = bytearray_removeprefix_impl((PyByteArrayObject *)self, &prefix);
711
0
    Py_END_CRITICAL_SECTION();
712
713
0
exit:
714
    /* Cleanup for prefix */
715
0
    if (prefix.obj) {
716
0
       PyBuffer_Release(&prefix);
717
0
    }
718
719
0
    return return_value;
720
0
}
721
722
PyDoc_STRVAR(bytearray_removesuffix__doc__,
723
"removesuffix($self, suffix, /)\n"
724
"--\n"
725
"\n"
726
"Return a bytearray with the given suffix string removed if present.\n"
727
"\n"
728
"If the bytearray ends with the suffix string and that suffix is not\n"
729
"empty, return bytearray[:-len(suffix)].  Otherwise, return a copy of\n"
730
"the original bytearray.");
731
732
#define BYTEARRAY_REMOVESUFFIX_METHODDEF    \
733
    {"removesuffix", (PyCFunction)bytearray_removesuffix, METH_O, bytearray_removesuffix__doc__},
734
735
static PyObject *
736
bytearray_removesuffix_impl(PyByteArrayObject *self, Py_buffer *suffix);
737
738
static PyObject *
739
bytearray_removesuffix(PyObject *self, PyObject *arg)
740
0
{
741
0
    PyObject *return_value = NULL;
742
0
    Py_buffer suffix = {NULL, NULL};
743
744
0
    if (PyObject_GetBuffer(arg, &suffix, PyBUF_SIMPLE) != 0) {
745
0
        goto exit;
746
0
    }
747
0
    Py_BEGIN_CRITICAL_SECTION(self);
748
0
    return_value = bytearray_removesuffix_impl((PyByteArrayObject *)self, &suffix);
749
0
    Py_END_CRITICAL_SECTION();
750
751
0
exit:
752
    /* Cleanup for suffix */
753
0
    if (suffix.obj) {
754
0
       PyBuffer_Release(&suffix);
755
0
    }
756
757
0
    return return_value;
758
0
}
759
760
PyDoc_STRVAR(bytearray_resize__doc__,
761
"resize($self, size, /)\n"
762
"--\n"
763
"\n"
764
"Resize the internal buffer of bytearray to len.\n"
765
"\n"
766
"  size\n"
767
"    New size to resize to.");
768
769
#define BYTEARRAY_RESIZE_METHODDEF    \
770
    {"resize", (PyCFunction)bytearray_resize, METH_O, bytearray_resize__doc__},
771
772
static PyObject *
773
bytearray_resize_impl(PyByteArrayObject *self, Py_ssize_t size);
774
775
static PyObject *
776
bytearray_resize(PyObject *self, PyObject *arg)
777
0
{
778
0
    PyObject *return_value = NULL;
779
0
    Py_ssize_t size;
780
781
0
    {
782
0
        Py_ssize_t ival = -1;
783
0
        PyObject *iobj = _PyNumber_Index(arg);
784
0
        if (iobj != NULL) {
785
0
            ival = PyLong_AsSsize_t(iobj);
786
0
            Py_DECREF(iobj);
787
0
        }
788
0
        if (ival == -1 && PyErr_Occurred()) {
789
0
            goto exit;
790
0
        }
791
0
        size = ival;
792
0
    }
793
0
    Py_BEGIN_CRITICAL_SECTION(self);
794
0
    return_value = bytearray_resize_impl((PyByteArrayObject *)self, size);
795
0
    Py_END_CRITICAL_SECTION();
796
797
0
exit:
798
0
    return return_value;
799
0
}
800
801
PyDoc_STRVAR(bytearray_take_bytes__doc__,
802
"take_bytes($self, n=None, /)\n"
803
"--\n"
804
"\n"
805
"Take *n* bytes from the bytearray and return them as a bytes object.\n"
806
"\n"
807
"  n\n"
808
"    Bytes to take, negative indexes from end. None indicates all bytes.");
809
810
#define BYTEARRAY_TAKE_BYTES_METHODDEF    \
811
    {"take_bytes", _PyCFunction_CAST(bytearray_take_bytes), METH_FASTCALL, bytearray_take_bytes__doc__},
812
813
static PyObject *
814
bytearray_take_bytes_impl(PyByteArrayObject *self, PyObject *n);
815
816
static PyObject *
817
bytearray_take_bytes(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
818
6.88M
{
819
6.88M
    PyObject *return_value = NULL;
820
6.88M
    PyObject *n = Py_None;
821
822
6.88M
    if (!_PyArg_CheckPositional("take_bytes", nargs, 0, 1)) {
823
0
        goto exit;
824
0
    }
825
6.88M
    if (nargs < 1) {
826
6.88M
        goto skip_optional;
827
6.88M
    }
828
0
    n = args[0];
829
6.88M
skip_optional:
830
6.88M
    Py_BEGIN_CRITICAL_SECTION(self);
831
6.88M
    return_value = bytearray_take_bytes_impl((PyByteArrayObject *)self, n);
832
6.88M
    Py_END_CRITICAL_SECTION();
833
834
6.88M
exit:
835
6.88M
    return return_value;
836
6.88M
}
837
838
PyDoc_STRVAR(bytearray_translate__doc__,
839
"translate($self, table, /, delete=b\'\')\n"
840
"--\n"
841
"\n"
842
"Return a copy with each character mapped by the given translation table.\n"
843
"\n"
844
"  table\n"
845
"    Translation table, which must be a bytes object of length 256.\n"
846
"\n"
847
"All characters occurring in the optional argument delete are\n"
848
"removed.  The remaining characters are mapped through the given\n"
849
"translation table.");
850
851
#define BYTEARRAY_TRANSLATE_METHODDEF    \
852
    {"translate", _PyCFunction_CAST(bytearray_translate), METH_FASTCALL|METH_KEYWORDS, bytearray_translate__doc__},
853
854
static PyObject *
855
bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
856
                         PyObject *deletechars);
857
858
static PyObject *
859
bytearray_translate(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
860
63.3k
{
861
63.3k
    PyObject *return_value = NULL;
862
63.3k
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
863
864
63.3k
    #define NUM_KEYWORDS 1
865
63.3k
    static struct {
866
63.3k
        PyGC_Head _this_is_not_used;
867
63.3k
        PyObject_VAR_HEAD
868
63.3k
        Py_hash_t ob_hash;
869
63.3k
        PyObject *ob_item[NUM_KEYWORDS];
870
63.3k
    } _kwtuple = {
871
63.3k
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
872
63.3k
        .ob_hash = -1,
873
63.3k
        .ob_item = { &_Py_ID(delete), },
874
63.3k
    };
875
63.3k
    #undef NUM_KEYWORDS
876
63.3k
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
877
878
    #else  // !Py_BUILD_CORE
879
    #  define KWTUPLE NULL
880
    #endif  // !Py_BUILD_CORE
881
882
63.3k
    static const char * const _keywords[] = {"", "delete", NULL};
883
63.3k
    static _PyArg_Parser _parser = {
884
63.3k
        .keywords = _keywords,
885
63.3k
        .fname = "translate",
886
63.3k
        .kwtuple = KWTUPLE,
887
63.3k
    };
888
63.3k
    #undef KWTUPLE
889
63.3k
    PyObject *argsbuf[2];
890
63.3k
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
891
63.3k
    PyObject *table;
892
63.3k
    PyObject *deletechars = NULL;
893
894
63.3k
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
895
63.3k
            /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
896
63.3k
    if (!args) {
897
0
        goto exit;
898
0
    }
899
63.3k
    table = args[0];
900
63.3k
    if (!noptargs) {
901
63.3k
        goto skip_optional_pos;
902
63.3k
    }
903
0
    deletechars = args[1];
904
63.3k
skip_optional_pos:
905
63.3k
    Py_BEGIN_CRITICAL_SECTION(self);
906
63.3k
    return_value = bytearray_translate_impl((PyByteArrayObject *)self, table, deletechars);
907
63.3k
    Py_END_CRITICAL_SECTION();
908
909
63.3k
exit:
910
63.3k
    return return_value;
911
63.3k
}
912
913
PyDoc_STRVAR(bytearray_maketrans__doc__,
914
"maketrans(frm, to, /)\n"
915
"--\n"
916
"\n"
917
"Return a translation table usable for the bytes or bytearray translate method.\n"
918
"\n"
919
"The returned table will be one where each byte in frm is mapped to\n"
920
"the byte at the same position in to.\n"
921
"\n"
922
"The bytes objects frm and to must be of the same length.");
923
924
#define BYTEARRAY_MAKETRANS_METHODDEF    \
925
    {"maketrans", _PyCFunction_CAST(bytearray_maketrans), METH_FASTCALL|METH_STATIC, bytearray_maketrans__doc__},
926
927
static PyObject *
928
bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to);
929
930
static PyObject *
931
bytearray_maketrans(PyObject *null, PyObject *const *args, Py_ssize_t nargs)
932
0
{
933
0
    PyObject *return_value = NULL;
934
0
    Py_buffer frm = {NULL, NULL};
935
0
    Py_buffer to = {NULL, NULL};
936
937
0
    if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) {
938
0
        goto exit;
939
0
    }
940
0
    if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) {
941
0
        goto exit;
942
0
    }
943
0
    if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) {
944
0
        goto exit;
945
0
    }
946
0
    return_value = bytearray_maketrans_impl(&frm, &to);
947
948
0
exit:
949
    /* Cleanup for frm */
950
0
    if (frm.obj) {
951
0
       PyBuffer_Release(&frm);
952
0
    }
953
    /* Cleanup for to */
954
0
    if (to.obj) {
955
0
       PyBuffer_Release(&to);
956
0
    }
957
958
0
    return return_value;
959
0
}
960
961
PyDoc_STRVAR(bytearray_replace__doc__,
962
"replace($self, old, new, /, count=-1)\n"
963
"--\n"
964
"\n"
965
"Return a copy with all occurrences of substring old replaced by new.\n"
966
"\n"
967
"  count\n"
968
"    Maximum number of occurrences to replace.\n"
969
"    -1 (the default value) means replace all occurrences.\n"
970
"\n"
971
"If count is given, only the first count occurrences are replaced.\n"
972
"If count is not specified or -1, then all occurrences are replaced.");
973
974
#define BYTEARRAY_REPLACE_METHODDEF    \
975
    {"replace", _PyCFunction_CAST(bytearray_replace), METH_FASTCALL|METH_KEYWORDS, bytearray_replace__doc__},
976
977
static PyObject *
978
bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
979
                       Py_buffer *new, Py_ssize_t count);
980
981
static PyObject *
982
bytearray_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
983
0
{
984
0
    PyObject *return_value = NULL;
985
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
986
987
0
    #define NUM_KEYWORDS 1
988
0
    static struct {
989
0
        PyGC_Head _this_is_not_used;
990
0
        PyObject_VAR_HEAD
991
0
        Py_hash_t ob_hash;
992
0
        PyObject *ob_item[NUM_KEYWORDS];
993
0
    } _kwtuple = {
994
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
995
0
        .ob_hash = -1,
996
0
        .ob_item = { &_Py_ID(count), },
997
0
    };
998
0
    #undef NUM_KEYWORDS
999
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1000
1001
    #else  // !Py_BUILD_CORE
1002
    #  define KWTUPLE NULL
1003
    #endif  // !Py_BUILD_CORE
1004
1005
0
    static const char * const _keywords[] = {"", "", "count", NULL};
1006
0
    static _PyArg_Parser _parser = {
1007
0
        .keywords = _keywords,
1008
0
        .fname = "replace",
1009
0
        .kwtuple = KWTUPLE,
1010
0
    };
1011
0
    #undef KWTUPLE
1012
0
    PyObject *argsbuf[3];
1013
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
1014
0
    Py_buffer old = {NULL, NULL};
1015
0
    Py_buffer new = {NULL, NULL};
1016
0
    Py_ssize_t count = -1;
1017
1018
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1019
0
            /*minpos*/ 2, /*maxpos*/ 3, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1020
0
    if (!args) {
1021
0
        goto exit;
1022
0
    }
1023
0
    if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) {
1024
0
        goto exit;
1025
0
    }
1026
0
    if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) {
1027
0
        goto exit;
1028
0
    }
1029
0
    if (!noptargs) {
1030
0
        goto skip_optional_pos;
1031
0
    }
1032
0
    {
1033
0
        Py_ssize_t ival = -1;
1034
0
        PyObject *iobj = _PyNumber_Index(args[2]);
1035
0
        if (iobj != NULL) {
1036
0
            ival = PyLong_AsSsize_t(iobj);
1037
0
            Py_DECREF(iobj);
1038
0
        }
1039
0
        if (ival == -1 && PyErr_Occurred()) {
1040
0
            goto exit;
1041
0
        }
1042
0
        count = ival;
1043
0
    }
1044
0
skip_optional_pos:
1045
0
    Py_BEGIN_CRITICAL_SECTION(self);
1046
0
    return_value = bytearray_replace_impl((PyByteArrayObject *)self, &old, &new, count);
1047
0
    Py_END_CRITICAL_SECTION();
1048
1049
0
exit:
1050
    /* Cleanup for old */
1051
0
    if (old.obj) {
1052
0
       PyBuffer_Release(&old);
1053
0
    }
1054
    /* Cleanup for new */
1055
0
    if (new.obj) {
1056
0
       PyBuffer_Release(&new);
1057
0
    }
1058
1059
0
    return return_value;
1060
0
}
1061
1062
PyDoc_STRVAR(bytearray_split__doc__,
1063
"split($self, /, sep=None, maxsplit=-1)\n"
1064
"--\n"
1065
"\n"
1066
"Return a list of the sections in the bytearray, using sep as the delimiter.\n"
1067
"\n"
1068
"  sep\n"
1069
"    The delimiter according which to split the bytearray.\n"
1070
"    None (the default value) means split on ASCII whitespace\n"
1071
"    characters (space, tab, return, newline, formfeed, vertical tab).\n"
1072
"  maxsplit\n"
1073
"    Maximum number of splits to do.\n"
1074
"    -1 (the default value) means no limit.");
1075
1076
#define BYTEARRAY_SPLIT_METHODDEF    \
1077
    {"split", _PyCFunction_CAST(bytearray_split), METH_FASTCALL|METH_KEYWORDS, bytearray_split__doc__},
1078
1079
static PyObject *
1080
bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
1081
                     Py_ssize_t maxsplit);
1082
1083
static PyObject *
1084
bytearray_split(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1085
0
{
1086
0
    PyObject *return_value = NULL;
1087
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1088
1089
0
    #define NUM_KEYWORDS 2
1090
0
    static struct {
1091
0
        PyGC_Head _this_is_not_used;
1092
0
        PyObject_VAR_HEAD
1093
0
        Py_hash_t ob_hash;
1094
0
        PyObject *ob_item[NUM_KEYWORDS];
1095
0
    } _kwtuple = {
1096
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1097
0
        .ob_hash = -1,
1098
0
        .ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), },
1099
0
    };
1100
0
    #undef NUM_KEYWORDS
1101
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1102
1103
    #else  // !Py_BUILD_CORE
1104
    #  define KWTUPLE NULL
1105
    #endif  // !Py_BUILD_CORE
1106
1107
0
    static const char * const _keywords[] = {"sep", "maxsplit", NULL};
1108
0
    static _PyArg_Parser _parser = {
1109
0
        .keywords = _keywords,
1110
0
        .fname = "split",
1111
0
        .kwtuple = KWTUPLE,
1112
0
    };
1113
0
    #undef KWTUPLE
1114
0
    PyObject *argsbuf[2];
1115
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1116
0
    PyObject *sep = Py_None;
1117
0
    Py_ssize_t maxsplit = -1;
1118
1119
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1120
0
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1121
0
    if (!args) {
1122
0
        goto exit;
1123
0
    }
1124
0
    if (!noptargs) {
1125
0
        goto skip_optional_pos;
1126
0
    }
1127
0
    if (args[0]) {
1128
0
        sep = args[0];
1129
0
        if (!--noptargs) {
1130
0
            goto skip_optional_pos;
1131
0
        }
1132
0
    }
1133
0
    {
1134
0
        Py_ssize_t ival = -1;
1135
0
        PyObject *iobj = _PyNumber_Index(args[1]);
1136
0
        if (iobj != NULL) {
1137
0
            ival = PyLong_AsSsize_t(iobj);
1138
0
            Py_DECREF(iobj);
1139
0
        }
1140
0
        if (ival == -1 && PyErr_Occurred()) {
1141
0
            goto exit;
1142
0
        }
1143
0
        maxsplit = ival;
1144
0
    }
1145
0
skip_optional_pos:
1146
0
    Py_BEGIN_CRITICAL_SECTION(self);
1147
0
    return_value = bytearray_split_impl((PyByteArrayObject *)self, sep, maxsplit);
1148
0
    Py_END_CRITICAL_SECTION();
1149
1150
0
exit:
1151
0
    return return_value;
1152
0
}
1153
1154
PyDoc_STRVAR(bytearray_partition__doc__,
1155
"partition($self, sep, /)\n"
1156
"--\n"
1157
"\n"
1158
"Partition the bytearray into three parts using the given separator.\n"
1159
"\n"
1160
"This will search for the separator sep in the bytearray.  If the\n"
1161
"separator is found, returns a 3-tuple containing the part before the\n"
1162
"separator, the separator itself, and the part after it as new\n"
1163
"bytearray objects.\n"
1164
"\n"
1165
"If the separator is not found, returns a 3-tuple containing the copy\n"
1166
"of the original bytearray object and two empty bytearray objects.");
1167
1168
#define BYTEARRAY_PARTITION_METHODDEF    \
1169
    {"partition", (PyCFunction)bytearray_partition, METH_O, bytearray_partition__doc__},
1170
1171
static PyObject *
1172
bytearray_partition_impl(PyByteArrayObject *self, PyObject *sep);
1173
1174
static PyObject *
1175
bytearray_partition(PyObject *self, PyObject *sep)
1176
0
{
1177
0
    PyObject *return_value = NULL;
1178
1179
0
    Py_BEGIN_CRITICAL_SECTION(self);
1180
0
    return_value = bytearray_partition_impl((PyByteArrayObject *)self, sep);
1181
0
    Py_END_CRITICAL_SECTION();
1182
1183
0
    return return_value;
1184
0
}
1185
1186
PyDoc_STRVAR(bytearray_rpartition__doc__,
1187
"rpartition($self, sep, /)\n"
1188
"--\n"
1189
"\n"
1190
"Partition the bytearray into three parts using the given separator.\n"
1191
"\n"
1192
"This will search for the separator sep in the bytearray, starting at\n"
1193
"the end.  If the separator is found, returns a 3-tuple containing\n"
1194
"the part before the separator, the separator itself, and the part\n"
1195
"after it as new bytearray objects.\n"
1196
"\n"
1197
"If the separator is not found, returns a 3-tuple containing two\n"
1198
"empty bytearray objects and the copy of the original bytearray\n"
1199
"object.");
1200
1201
#define BYTEARRAY_RPARTITION_METHODDEF    \
1202
    {"rpartition", (PyCFunction)bytearray_rpartition, METH_O, bytearray_rpartition__doc__},
1203
1204
static PyObject *
1205
bytearray_rpartition_impl(PyByteArrayObject *self, PyObject *sep);
1206
1207
static PyObject *
1208
bytearray_rpartition(PyObject *self, PyObject *sep)
1209
0
{
1210
0
    PyObject *return_value = NULL;
1211
1212
0
    Py_BEGIN_CRITICAL_SECTION(self);
1213
0
    return_value = bytearray_rpartition_impl((PyByteArrayObject *)self, sep);
1214
0
    Py_END_CRITICAL_SECTION();
1215
1216
0
    return return_value;
1217
0
}
1218
1219
PyDoc_STRVAR(bytearray_rsplit__doc__,
1220
"rsplit($self, /, sep=None, maxsplit=-1)\n"
1221
"--\n"
1222
"\n"
1223
"Return a list of the sections in the bytearray, using sep as the delimiter.\n"
1224
"\n"
1225
"  sep\n"
1226
"    The delimiter according which to split the bytearray.\n"
1227
"    None (the default value) means split on ASCII whitespace\n"
1228
"    characters (space, tab, return, newline, formfeed, vertical tab).\n"
1229
"  maxsplit\n"
1230
"    Maximum number of splits to do.\n"
1231
"    -1 (the default value) means no limit.\n"
1232
"\n"
1233
"Splitting is done starting at the end of the bytearray and working\n"
1234
"to the front.");
1235
1236
#define BYTEARRAY_RSPLIT_METHODDEF    \
1237
    {"rsplit", _PyCFunction_CAST(bytearray_rsplit), METH_FASTCALL|METH_KEYWORDS, bytearray_rsplit__doc__},
1238
1239
static PyObject *
1240
bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
1241
                      Py_ssize_t maxsplit);
1242
1243
static PyObject *
1244
bytearray_rsplit(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1245
0
{
1246
0
    PyObject *return_value = NULL;
1247
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1248
1249
0
    #define NUM_KEYWORDS 2
1250
0
    static struct {
1251
0
        PyGC_Head _this_is_not_used;
1252
0
        PyObject_VAR_HEAD
1253
0
        Py_hash_t ob_hash;
1254
0
        PyObject *ob_item[NUM_KEYWORDS];
1255
0
    } _kwtuple = {
1256
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1257
0
        .ob_hash = -1,
1258
0
        .ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), },
1259
0
    };
1260
0
    #undef NUM_KEYWORDS
1261
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1262
1263
    #else  // !Py_BUILD_CORE
1264
    #  define KWTUPLE NULL
1265
    #endif  // !Py_BUILD_CORE
1266
1267
0
    static const char * const _keywords[] = {"sep", "maxsplit", NULL};
1268
0
    static _PyArg_Parser _parser = {
1269
0
        .keywords = _keywords,
1270
0
        .fname = "rsplit",
1271
0
        .kwtuple = KWTUPLE,
1272
0
    };
1273
0
    #undef KWTUPLE
1274
0
    PyObject *argsbuf[2];
1275
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1276
0
    PyObject *sep = Py_None;
1277
0
    Py_ssize_t maxsplit = -1;
1278
1279
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1280
0
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1281
0
    if (!args) {
1282
0
        goto exit;
1283
0
    }
1284
0
    if (!noptargs) {
1285
0
        goto skip_optional_pos;
1286
0
    }
1287
0
    if (args[0]) {
1288
0
        sep = args[0];
1289
0
        if (!--noptargs) {
1290
0
            goto skip_optional_pos;
1291
0
        }
1292
0
    }
1293
0
    {
1294
0
        Py_ssize_t ival = -1;
1295
0
        PyObject *iobj = _PyNumber_Index(args[1]);
1296
0
        if (iobj != NULL) {
1297
0
            ival = PyLong_AsSsize_t(iobj);
1298
0
            Py_DECREF(iobj);
1299
0
        }
1300
0
        if (ival == -1 && PyErr_Occurred()) {
1301
0
            goto exit;
1302
0
        }
1303
0
        maxsplit = ival;
1304
0
    }
1305
0
skip_optional_pos:
1306
0
    Py_BEGIN_CRITICAL_SECTION(self);
1307
0
    return_value = bytearray_rsplit_impl((PyByteArrayObject *)self, sep, maxsplit);
1308
0
    Py_END_CRITICAL_SECTION();
1309
1310
0
exit:
1311
0
    return return_value;
1312
0
}
1313
1314
PyDoc_STRVAR(bytearray_reverse__doc__,
1315
"reverse($self, /)\n"
1316
"--\n"
1317
"\n"
1318
"Reverse the order of the values in B in place.");
1319
1320
#define BYTEARRAY_REVERSE_METHODDEF    \
1321
    {"reverse", (PyCFunction)bytearray_reverse, METH_NOARGS, bytearray_reverse__doc__},
1322
1323
static PyObject *
1324
bytearray_reverse_impl(PyByteArrayObject *self);
1325
1326
static PyObject *
1327
bytearray_reverse(PyObject *self, PyObject *Py_UNUSED(ignored))
1328
0
{
1329
0
    PyObject *return_value = NULL;
1330
1331
0
    Py_BEGIN_CRITICAL_SECTION(self);
1332
0
    return_value = bytearray_reverse_impl((PyByteArrayObject *)self);
1333
0
    Py_END_CRITICAL_SECTION();
1334
1335
0
    return return_value;
1336
0
}
1337
1338
PyDoc_STRVAR(bytearray_insert__doc__,
1339
"insert($self, index, item, /)\n"
1340
"--\n"
1341
"\n"
1342
"Insert a single item into the bytearray before the given index.\n"
1343
"\n"
1344
"  index\n"
1345
"    The index where the value is to be inserted.\n"
1346
"  item\n"
1347
"    The item to be inserted.");
1348
1349
#define BYTEARRAY_INSERT_METHODDEF    \
1350
    {"insert", _PyCFunction_CAST(bytearray_insert), METH_FASTCALL, bytearray_insert__doc__},
1351
1352
static PyObject *
1353
bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item);
1354
1355
static PyObject *
1356
bytearray_insert(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1357
17.2k
{
1358
17.2k
    PyObject *return_value = NULL;
1359
17.2k
    Py_ssize_t index;
1360
17.2k
    int item;
1361
1362
17.2k
    if (!_PyArg_CheckPositional("insert", nargs, 2, 2)) {
1363
0
        goto exit;
1364
0
    }
1365
17.2k
    {
1366
17.2k
        Py_ssize_t ival = -1;
1367
17.2k
        PyObject *iobj = _PyNumber_Index(args[0]);
1368
17.2k
        if (iobj != NULL) {
1369
17.2k
            ival = PyLong_AsSsize_t(iobj);
1370
17.2k
            Py_DECREF(iobj);
1371
17.2k
        }
1372
17.2k
        if (ival == -1 && PyErr_Occurred()) {
1373
0
            goto exit;
1374
0
        }
1375
17.2k
        index = ival;
1376
17.2k
    }
1377
17.2k
    if (!_getbytevalue(args[1], &item)) {
1378
0
        goto exit;
1379
0
    }
1380
17.2k
    Py_BEGIN_CRITICAL_SECTION(self);
1381
17.2k
    return_value = bytearray_insert_impl((PyByteArrayObject *)self, index, item);
1382
17.2k
    Py_END_CRITICAL_SECTION();
1383
1384
17.2k
exit:
1385
17.2k
    return return_value;
1386
17.2k
}
1387
1388
PyDoc_STRVAR(bytearray_append__doc__,
1389
"append($self, item, /)\n"
1390
"--\n"
1391
"\n"
1392
"Append a single item to the end of the bytearray.\n"
1393
"\n"
1394
"  item\n"
1395
"    The item to be appended.");
1396
1397
#define BYTEARRAY_APPEND_METHODDEF    \
1398
    {"append", (PyCFunction)bytearray_append, METH_O, bytearray_append__doc__},
1399
1400
static PyObject *
1401
bytearray_append_impl(PyByteArrayObject *self, int item);
1402
1403
static PyObject *
1404
bytearray_append(PyObject *self, PyObject *arg)
1405
9.93M
{
1406
9.93M
    PyObject *return_value = NULL;
1407
9.93M
    int item;
1408
1409
9.93M
    if (!_getbytevalue(arg, &item)) {
1410
0
        goto exit;
1411
0
    }
1412
9.93M
    Py_BEGIN_CRITICAL_SECTION(self);
1413
9.93M
    return_value = bytearray_append_impl((PyByteArrayObject *)self, item);
1414
9.93M
    Py_END_CRITICAL_SECTION();
1415
1416
9.93M
exit:
1417
9.93M
    return return_value;
1418
9.93M
}
1419
1420
PyDoc_STRVAR(bytearray_extend__doc__,
1421
"extend($self, iterable_of_ints, /)\n"
1422
"--\n"
1423
"\n"
1424
"Append all the items from the iterator or sequence to the end of the bytearray.\n"
1425
"\n"
1426
"  iterable_of_ints\n"
1427
"    The iterable of items to append.");
1428
1429
#define BYTEARRAY_EXTEND_METHODDEF    \
1430
    {"extend", (PyCFunction)bytearray_extend, METH_O, bytearray_extend__doc__},
1431
1432
static PyObject *
1433
bytearray_extend_impl(PyByteArrayObject *self, PyObject *iterable_of_ints);
1434
1435
static PyObject *
1436
bytearray_extend(PyObject *self, PyObject *iterable_of_ints)
1437
11.3M
{
1438
11.3M
    PyObject *return_value = NULL;
1439
1440
11.3M
    Py_BEGIN_CRITICAL_SECTION(self);
1441
11.3M
    return_value = bytearray_extend_impl((PyByteArrayObject *)self, iterable_of_ints);
1442
11.3M
    Py_END_CRITICAL_SECTION();
1443
1444
11.3M
    return return_value;
1445
11.3M
}
1446
1447
PyDoc_STRVAR(bytearray_pop__doc__,
1448
"pop($self, index=-1, /)\n"
1449
"--\n"
1450
"\n"
1451
"Remove and return a single item from B.\n"
1452
"\n"
1453
"  index\n"
1454
"    The index from where to remove the item.\n"
1455
"    -1 (the default value) means remove the last item.\n"
1456
"\n"
1457
"If no index argument is given, will pop the last item.");
1458
1459
#define BYTEARRAY_POP_METHODDEF    \
1460
    {"pop", _PyCFunction_CAST(bytearray_pop), METH_FASTCALL, bytearray_pop__doc__},
1461
1462
static PyObject *
1463
bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index);
1464
1465
static PyObject *
1466
bytearray_pop(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1467
0
{
1468
0
    PyObject *return_value = NULL;
1469
0
    Py_ssize_t index = -1;
1470
1471
0
    if (!_PyArg_CheckPositional("pop", nargs, 0, 1)) {
1472
0
        goto exit;
1473
0
    }
1474
0
    if (nargs < 1) {
1475
0
        goto skip_optional;
1476
0
    }
1477
0
    {
1478
0
        Py_ssize_t ival = -1;
1479
0
        PyObject *iobj = _PyNumber_Index(args[0]);
1480
0
        if (iobj != NULL) {
1481
0
            ival = PyLong_AsSsize_t(iobj);
1482
0
            Py_DECREF(iobj);
1483
0
        }
1484
0
        if (ival == -1 && PyErr_Occurred()) {
1485
0
            goto exit;
1486
0
        }
1487
0
        index = ival;
1488
0
    }
1489
0
skip_optional:
1490
0
    Py_BEGIN_CRITICAL_SECTION(self);
1491
0
    return_value = bytearray_pop_impl((PyByteArrayObject *)self, index);
1492
0
    Py_END_CRITICAL_SECTION();
1493
1494
0
exit:
1495
0
    return return_value;
1496
0
}
1497
1498
PyDoc_STRVAR(bytearray_remove__doc__,
1499
"remove($self, value, /)\n"
1500
"--\n"
1501
"\n"
1502
"Remove the first occurrence of a value in the bytearray.\n"
1503
"\n"
1504
"  value\n"
1505
"    The value to remove.");
1506
1507
#define BYTEARRAY_REMOVE_METHODDEF    \
1508
    {"remove", (PyCFunction)bytearray_remove, METH_O, bytearray_remove__doc__},
1509
1510
static PyObject *
1511
bytearray_remove_impl(PyByteArrayObject *self, int value);
1512
1513
static PyObject *
1514
bytearray_remove(PyObject *self, PyObject *arg)
1515
0
{
1516
0
    PyObject *return_value = NULL;
1517
0
    int value;
1518
1519
0
    if (!_getbytevalue(arg, &value)) {
1520
0
        goto exit;
1521
0
    }
1522
0
    Py_BEGIN_CRITICAL_SECTION(self);
1523
0
    return_value = bytearray_remove_impl((PyByteArrayObject *)self, value);
1524
0
    Py_END_CRITICAL_SECTION();
1525
1526
0
exit:
1527
0
    return return_value;
1528
0
}
1529
1530
PyDoc_STRVAR(bytearray_strip__doc__,
1531
"strip($self, bytes=None, /)\n"
1532
"--\n"
1533
"\n"
1534
"Strip leading and trailing bytes contained in the argument.\n"
1535
"\n"
1536
"If the argument is omitted or None, strip leading and trailing ASCII\n"
1537
"whitespace.");
1538
1539
#define BYTEARRAY_STRIP_METHODDEF    \
1540
    {"strip", _PyCFunction_CAST(bytearray_strip), METH_FASTCALL, bytearray_strip__doc__},
1541
1542
static PyObject *
1543
bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes);
1544
1545
static PyObject *
1546
bytearray_strip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1547
0
{
1548
0
    PyObject *return_value = NULL;
1549
0
    PyObject *bytes = Py_None;
1550
1551
0
    if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
1552
0
        goto exit;
1553
0
    }
1554
0
    if (nargs < 1) {
1555
0
        goto skip_optional;
1556
0
    }
1557
0
    bytes = args[0];
1558
0
skip_optional:
1559
0
    Py_BEGIN_CRITICAL_SECTION(self);
1560
0
    return_value = bytearray_strip_impl((PyByteArrayObject *)self, bytes);
1561
0
    Py_END_CRITICAL_SECTION();
1562
1563
0
exit:
1564
0
    return return_value;
1565
0
}
1566
1567
PyDoc_STRVAR(bytearray_lstrip__doc__,
1568
"lstrip($self, bytes=None, /)\n"
1569
"--\n"
1570
"\n"
1571
"Strip leading bytes contained in the argument.\n"
1572
"\n"
1573
"If the argument is omitted or None, strip leading ASCII whitespace.");
1574
1575
#define BYTEARRAY_LSTRIP_METHODDEF    \
1576
    {"lstrip", _PyCFunction_CAST(bytearray_lstrip), METH_FASTCALL, bytearray_lstrip__doc__},
1577
1578
static PyObject *
1579
bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes);
1580
1581
static PyObject *
1582
bytearray_lstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1583
0
{
1584
0
    PyObject *return_value = NULL;
1585
0
    PyObject *bytes = Py_None;
1586
1587
0
    if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
1588
0
        goto exit;
1589
0
    }
1590
0
    if (nargs < 1) {
1591
0
        goto skip_optional;
1592
0
    }
1593
0
    bytes = args[0];
1594
0
skip_optional:
1595
0
    Py_BEGIN_CRITICAL_SECTION(self);
1596
0
    return_value = bytearray_lstrip_impl((PyByteArrayObject *)self, bytes);
1597
0
    Py_END_CRITICAL_SECTION();
1598
1599
0
exit:
1600
0
    return return_value;
1601
0
}
1602
1603
PyDoc_STRVAR(bytearray_rstrip__doc__,
1604
"rstrip($self, bytes=None, /)\n"
1605
"--\n"
1606
"\n"
1607
"Strip trailing bytes contained in the argument.\n"
1608
"\n"
1609
"If the argument is omitted or None, strip trailing ASCII whitespace.");
1610
1611
#define BYTEARRAY_RSTRIP_METHODDEF    \
1612
    {"rstrip", _PyCFunction_CAST(bytearray_rstrip), METH_FASTCALL, bytearray_rstrip__doc__},
1613
1614
static PyObject *
1615
bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes);
1616
1617
static PyObject *
1618
bytearray_rstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1619
0
{
1620
0
    PyObject *return_value = NULL;
1621
0
    PyObject *bytes = Py_None;
1622
1623
0
    if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
1624
0
        goto exit;
1625
0
    }
1626
0
    if (nargs < 1) {
1627
0
        goto skip_optional;
1628
0
    }
1629
0
    bytes = args[0];
1630
0
skip_optional:
1631
0
    Py_BEGIN_CRITICAL_SECTION(self);
1632
0
    return_value = bytearray_rstrip_impl((PyByteArrayObject *)self, bytes);
1633
0
    Py_END_CRITICAL_SECTION();
1634
1635
0
exit:
1636
0
    return return_value;
1637
0
}
1638
1639
PyDoc_STRVAR(bytearray_decode__doc__,
1640
"decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
1641
"--\n"
1642
"\n"
1643
"Decode the bytearray using the codec registered for encoding.\n"
1644
"\n"
1645
"  encoding\n"
1646
"    The encoding with which to decode the bytearray.\n"
1647
"  errors\n"
1648
"    The error handling scheme to use for the handling of decoding\n"
1649
"    errors.  The default is \'strict\' meaning that decoding errors\n"
1650
"    raise a UnicodeDecodeError.  Other possible values are \'ignore\'\n"
1651
"    and \'replace\' as well as any other name registered with\n"
1652
"    codecs.register_error that can handle UnicodeDecodeErrors.");
1653
1654
#define BYTEARRAY_DECODE_METHODDEF    \
1655
    {"decode", _PyCFunction_CAST(bytearray_decode), METH_FASTCALL|METH_KEYWORDS, bytearray_decode__doc__},
1656
1657
static PyObject *
1658
bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
1659
                      const char *errors);
1660
1661
static PyObject *
1662
bytearray_decode(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1663
548k
{
1664
548k
    PyObject *return_value = NULL;
1665
548k
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1666
1667
548k
    #define NUM_KEYWORDS 2
1668
548k
    static struct {
1669
548k
        PyGC_Head _this_is_not_used;
1670
548k
        PyObject_VAR_HEAD
1671
548k
        Py_hash_t ob_hash;
1672
548k
        PyObject *ob_item[NUM_KEYWORDS];
1673
548k
    } _kwtuple = {
1674
548k
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1675
548k
        .ob_hash = -1,
1676
548k
        .ob_item = { &_Py_ID(encoding), &_Py_ID(errors), },
1677
548k
    };
1678
548k
    #undef NUM_KEYWORDS
1679
548k
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1680
1681
    #else  // !Py_BUILD_CORE
1682
    #  define KWTUPLE NULL
1683
    #endif  // !Py_BUILD_CORE
1684
1685
548k
    static const char * const _keywords[] = {"encoding", "errors", NULL};
1686
548k
    static _PyArg_Parser _parser = {
1687
548k
        .keywords = _keywords,
1688
548k
        .fname = "decode",
1689
548k
        .kwtuple = KWTUPLE,
1690
548k
    };
1691
548k
    #undef KWTUPLE
1692
548k
    PyObject *argsbuf[2];
1693
548k
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1694
548k
    const char *encoding = NULL;
1695
548k
    const char *errors = NULL;
1696
1697
548k
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1698
548k
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1699
548k
    if (!args) {
1700
0
        goto exit;
1701
0
    }
1702
548k
    if (!noptargs) {
1703
0
        goto skip_optional_pos;
1704
0
    }
1705
548k
    if (args[0]) {
1706
548k
        if (!PyUnicode_Check(args[0])) {
1707
0
            _PyArg_BadArgument("decode", "argument 'encoding'", "str", args[0]);
1708
0
            goto exit;
1709
0
        }
1710
548k
        encoding = _PyUnicode_AsUTF8NoNUL(args[0]);
1711
548k
        if (encoding == NULL) {
1712
0
            goto exit;
1713
0
        }
1714
548k
        if (!--noptargs) {
1715
0
            goto skip_optional_pos;
1716
0
        }
1717
548k
    }
1718
548k
    if (!PyUnicode_Check(args[1])) {
1719
0
        _PyArg_BadArgument("decode", "argument 'errors'", "str", args[1]);
1720
0
        goto exit;
1721
0
    }
1722
548k
    errors = _PyUnicode_AsUTF8NoNUL(args[1]);
1723
548k
    if (errors == NULL) {
1724
0
        goto exit;
1725
0
    }
1726
548k
skip_optional_pos:
1727
548k
    Py_BEGIN_CRITICAL_SECTION(self);
1728
548k
    return_value = bytearray_decode_impl((PyByteArrayObject *)self, encoding, errors);
1729
548k
    Py_END_CRITICAL_SECTION();
1730
1731
548k
exit:
1732
548k
    return return_value;
1733
548k
}
1734
1735
PyDoc_STRVAR(bytearray_join__doc__,
1736
"join($self, iterable_of_bytes, /)\n"
1737
"--\n"
1738
"\n"
1739
"Concatenate any number of bytes/bytearray objects.\n"
1740
"\n"
1741
"The bytearray whose method is called is inserted in between each\n"
1742
"pair.\n"
1743
"\n"
1744
"The result is returned as a new bytearray object.");
1745
1746
#define BYTEARRAY_JOIN_METHODDEF    \
1747
    {"join", (PyCFunction)bytearray_join, METH_O, bytearray_join__doc__},
1748
1749
static PyObject *
1750
bytearray_join_impl(PyByteArrayObject *self, PyObject *iterable_of_bytes);
1751
1752
static PyObject *
1753
bytearray_join(PyObject *self, PyObject *iterable_of_bytes)
1754
0
{
1755
0
    PyObject *return_value = NULL;
1756
1757
0
    Py_BEGIN_CRITICAL_SECTION(self);
1758
0
    return_value = bytearray_join_impl((PyByteArrayObject *)self, iterable_of_bytes);
1759
0
    Py_END_CRITICAL_SECTION();
1760
1761
0
    return return_value;
1762
0
}
1763
1764
PyDoc_STRVAR(bytearray_splitlines__doc__,
1765
"splitlines($self, /, keepends=False)\n"
1766
"--\n"
1767
"\n"
1768
"Return a list of the lines in the bytearray, breaking at line boundaries.\n"
1769
"\n"
1770
"Line breaks are not included in the resulting list unless keepends\n"
1771
"is given and true.");
1772
1773
#define BYTEARRAY_SPLITLINES_METHODDEF    \
1774
    {"splitlines", _PyCFunction_CAST(bytearray_splitlines), METH_FASTCALL|METH_KEYWORDS, bytearray_splitlines__doc__},
1775
1776
static PyObject *
1777
bytearray_splitlines_impl(PyByteArrayObject *self, int keepends);
1778
1779
static PyObject *
1780
bytearray_splitlines(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1781
0
{
1782
0
    PyObject *return_value = NULL;
1783
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1784
1785
0
    #define NUM_KEYWORDS 1
1786
0
    static struct {
1787
0
        PyGC_Head _this_is_not_used;
1788
0
        PyObject_VAR_HEAD
1789
0
        Py_hash_t ob_hash;
1790
0
        PyObject *ob_item[NUM_KEYWORDS];
1791
0
    } _kwtuple = {
1792
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1793
0
        .ob_hash = -1,
1794
0
        .ob_item = { &_Py_ID(keepends), },
1795
0
    };
1796
0
    #undef NUM_KEYWORDS
1797
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1798
1799
    #else  // !Py_BUILD_CORE
1800
    #  define KWTUPLE NULL
1801
    #endif  // !Py_BUILD_CORE
1802
1803
0
    static const char * const _keywords[] = {"keepends", NULL};
1804
0
    static _PyArg_Parser _parser = {
1805
0
        .keywords = _keywords,
1806
0
        .fname = "splitlines",
1807
0
        .kwtuple = KWTUPLE,
1808
0
    };
1809
0
    #undef KWTUPLE
1810
0
    PyObject *argsbuf[1];
1811
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1812
0
    int keepends = 0;
1813
1814
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1815
0
            /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1816
0
    if (!args) {
1817
0
        goto exit;
1818
0
    }
1819
0
    if (!noptargs) {
1820
0
        goto skip_optional_pos;
1821
0
    }
1822
0
    keepends = PyObject_IsTrue(args[0]);
1823
0
    if (keepends < 0) {
1824
0
        goto exit;
1825
0
    }
1826
0
skip_optional_pos:
1827
0
    Py_BEGIN_CRITICAL_SECTION(self);
1828
0
    return_value = bytearray_splitlines_impl((PyByteArrayObject *)self, keepends);
1829
0
    Py_END_CRITICAL_SECTION();
1830
1831
0
exit:
1832
0
    return return_value;
1833
0
}
1834
1835
PyDoc_STRVAR(bytearray_fromhex__doc__,
1836
"fromhex($type, string, /)\n"
1837
"--\n"
1838
"\n"
1839
"Create a bytearray object from a string of hexadecimal numbers.\n"
1840
"\n"
1841
"Spaces between two numbers are accepted.\n"
1842
"Example:\n"
1843
"    bytearray.fromhex(\'B9 01EF\') -> bytearray(b\'\\\\xb9\\\\x01\\\\xef\')");
1844
1845
#define BYTEARRAY_FROMHEX_METHODDEF    \
1846
    {"fromhex", (PyCFunction)bytearray_fromhex, METH_O|METH_CLASS, bytearray_fromhex__doc__},
1847
1848
static PyObject *
1849
bytearray_fromhex_impl(PyTypeObject *type, PyObject *string);
1850
1851
static PyObject *
1852
bytearray_fromhex(PyObject *type, PyObject *string)
1853
0
{
1854
0
    PyObject *return_value = NULL;
1855
1856
0
    return_value = bytearray_fromhex_impl((PyTypeObject *)type, string);
1857
1858
0
    return return_value;
1859
0
}
1860
1861
PyDoc_STRVAR(bytearray_hex__doc__,
1862
"hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n"
1863
"--\n"
1864
"\n"
1865
"Create a string of hexadecimal numbers from a bytearray object.\n"
1866
"\n"
1867
"  sep\n"
1868
"    An optional single character or byte to separate hex bytes.\n"
1869
"  bytes_per_sep\n"
1870
"    How many bytes between separators.  Positive values count from\n"
1871
"    the right, negative values count from the left.\n"
1872
"\n"
1873
"Example:\n"
1874
">>> value = bytearray([0xb9, 0x01, 0xef])\n"
1875
">>> value.hex()\n"
1876
"\'b901ef\'\n"
1877
">>> value.hex(\':\')\n"
1878
"\'b9:01:ef\'\n"
1879
">>> value.hex(\':\', 2)\n"
1880
"\'b9:01ef\'\n"
1881
">>> value.hex(\':\', -2)\n"
1882
"\'b901:ef\'");
1883
1884
#define BYTEARRAY_HEX_METHODDEF    \
1885
    {"hex", _PyCFunction_CAST(bytearray_hex), METH_FASTCALL|METH_KEYWORDS, bytearray_hex__doc__},
1886
1887
static PyObject *
1888
bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep,
1889
                   Py_ssize_t bytes_per_sep);
1890
1891
static PyObject *
1892
bytearray_hex(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1893
0
{
1894
0
    PyObject *return_value = NULL;
1895
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1896
1897
0
    #define NUM_KEYWORDS 2
1898
0
    static struct {
1899
0
        PyGC_Head _this_is_not_used;
1900
0
        PyObject_VAR_HEAD
1901
0
        Py_hash_t ob_hash;
1902
0
        PyObject *ob_item[NUM_KEYWORDS];
1903
0
    } _kwtuple = {
1904
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1905
0
        .ob_hash = -1,
1906
0
        .ob_item = { &_Py_ID(sep), &_Py_ID(bytes_per_sep), },
1907
0
    };
1908
0
    #undef NUM_KEYWORDS
1909
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1910
1911
    #else  // !Py_BUILD_CORE
1912
    #  define KWTUPLE NULL
1913
    #endif  // !Py_BUILD_CORE
1914
1915
0
    static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL};
1916
0
    static _PyArg_Parser _parser = {
1917
0
        .keywords = _keywords,
1918
0
        .fname = "hex",
1919
0
        .kwtuple = KWTUPLE,
1920
0
    };
1921
0
    #undef KWTUPLE
1922
0
    PyObject *argsbuf[2];
1923
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1924
0
    PyObject *sep = NULL;
1925
0
    Py_ssize_t bytes_per_sep = 1;
1926
1927
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1928
0
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1929
0
    if (!args) {
1930
0
        goto exit;
1931
0
    }
1932
0
    if (!noptargs) {
1933
0
        goto skip_optional_pos;
1934
0
    }
1935
0
    if (args[0]) {
1936
0
        sep = args[0];
1937
0
        if (!--noptargs) {
1938
0
            goto skip_optional_pos;
1939
0
        }
1940
0
    }
1941
0
    {
1942
0
        Py_ssize_t ival = -1;
1943
0
        PyObject *iobj = _PyNumber_Index(args[1]);
1944
0
        if (iobj != NULL) {
1945
0
            ival = PyLong_AsSsize_t(iobj);
1946
0
            Py_DECREF(iobj);
1947
0
        }
1948
0
        if (ival == -1 && PyErr_Occurred()) {
1949
0
            goto exit;
1950
0
        }
1951
0
        bytes_per_sep = ival;
1952
0
    }
1953
0
skip_optional_pos:
1954
0
    Py_BEGIN_CRITICAL_SECTION(self);
1955
0
    return_value = bytearray_hex_impl((PyByteArrayObject *)self, sep, bytes_per_sep);
1956
0
    Py_END_CRITICAL_SECTION();
1957
1958
0
exit:
1959
0
    return return_value;
1960
0
}
1961
1962
PyDoc_STRVAR(bytearray_reduce__doc__,
1963
"__reduce__($self, /)\n"
1964
"--\n"
1965
"\n"
1966
"Return state information for pickling.");
1967
1968
#define BYTEARRAY_REDUCE_METHODDEF    \
1969
    {"__reduce__", (PyCFunction)bytearray_reduce, METH_NOARGS, bytearray_reduce__doc__},
1970
1971
static PyObject *
1972
bytearray_reduce_impl(PyByteArrayObject *self);
1973
1974
static PyObject *
1975
bytearray_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
1976
0
{
1977
0
    PyObject *return_value = NULL;
1978
1979
0
    Py_BEGIN_CRITICAL_SECTION(self);
1980
0
    return_value = bytearray_reduce_impl((PyByteArrayObject *)self);
1981
0
    Py_END_CRITICAL_SECTION();
1982
1983
0
    return return_value;
1984
0
}
1985
1986
PyDoc_STRVAR(bytearray_reduce_ex__doc__,
1987
"__reduce_ex__($self, proto=0, /)\n"
1988
"--\n"
1989
"\n"
1990
"Return state information for pickling.");
1991
1992
#define BYTEARRAY_REDUCE_EX_METHODDEF    \
1993
    {"__reduce_ex__", _PyCFunction_CAST(bytearray_reduce_ex), METH_FASTCALL, bytearray_reduce_ex__doc__},
1994
1995
static PyObject *
1996
bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto);
1997
1998
static PyObject *
1999
bytearray_reduce_ex(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
2000
0
{
2001
0
    PyObject *return_value = NULL;
2002
0
    int proto = 0;
2003
2004
0
    if (!_PyArg_CheckPositional("__reduce_ex__", nargs, 0, 1)) {
2005
0
        goto exit;
2006
0
    }
2007
0
    if (nargs < 1) {
2008
0
        goto skip_optional;
2009
0
    }
2010
0
    proto = PyLong_AsInt(args[0]);
2011
0
    if (proto == -1 && PyErr_Occurred()) {
2012
0
        goto exit;
2013
0
    }
2014
0
skip_optional:
2015
0
    Py_BEGIN_CRITICAL_SECTION(self);
2016
0
    return_value = bytearray_reduce_ex_impl((PyByteArrayObject *)self, proto);
2017
0
    Py_END_CRITICAL_SECTION();
2018
2019
0
exit:
2020
0
    return return_value;
2021
0
}
2022
2023
PyDoc_STRVAR(bytearray_sizeof__doc__,
2024
"__sizeof__($self, /)\n"
2025
"--\n"
2026
"\n"
2027
"Returns the size of the bytearray object in memory, in bytes.");
2028
2029
#define BYTEARRAY_SIZEOF_METHODDEF    \
2030
    {"__sizeof__", (PyCFunction)bytearray_sizeof, METH_NOARGS, bytearray_sizeof__doc__},
2031
2032
static PyObject *
2033
bytearray_sizeof_impl(PyByteArrayObject *self);
2034
2035
static PyObject *
2036
bytearray_sizeof(PyObject *self, PyObject *Py_UNUSED(ignored))
2037
0
{
2038
0
    return bytearray_sizeof_impl((PyByteArrayObject *)self);
2039
0
}
2040
/*[clinic end generated code: output=b8e659fe4b2f770e input=a9049054013a1b77]*/