Coverage Report

Created: 2026-03-23 06:45

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