Coverage Report

Created: 2025-11-24 06:11

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
1.51M
{
20
1.51M
    int return_value = -1;
21
1.51M
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
22
23
1.51M
    #define NUM_KEYWORDS 3
24
1.51M
    static struct {
25
1.51M
        PyGC_Head _this_is_not_used;
26
1.51M
        PyObject_VAR_HEAD
27
1.51M
        Py_hash_t ob_hash;
28
1.51M
        PyObject *ob_item[NUM_KEYWORDS];
29
1.51M
    } _kwtuple = {
30
1.51M
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
31
1.51M
        .ob_hash = -1,
32
1.51M
        .ob_item = { &_Py_ID(source), &_Py_ID(encoding), &_Py_ID(errors), },
33
1.51M
    };
34
1.51M
    #undef NUM_KEYWORDS
35
1.51M
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
36
37
    #else  // !Py_BUILD_CORE
38
    #  define KWTUPLE NULL
39
    #endif  // !Py_BUILD_CORE
40
41
1.51M
    static const char * const _keywords[] = {"source", "encoding", "errors", NULL};
42
1.51M
    static _PyArg_Parser _parser = {
43
1.51M
        .keywords = _keywords,
44
1.51M
        .fname = "bytearray",
45
1.51M
        .kwtuple = KWTUPLE,
46
1.51M
    };
47
1.51M
    #undef KWTUPLE
48
1.51M
    PyObject *argsbuf[3];
49
1.51M
    PyObject * const *fastargs;
50
1.51M
    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
51
1.51M
    Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
52
1.51M
    PyObject *arg = NULL;
53
1.51M
    const char *encoding = NULL;
54
1.51M
    const char *errors = NULL;
55
56
1.51M
    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
57
1.51M
            /*minpos*/ 0, /*maxpos*/ 3, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
58
1.51M
    if (!fastargs) {
59
0
        goto exit;
60
0
    }
61
1.51M
    if (!noptargs) {
62
433k
        goto skip_optional_pos;
63
433k
    }
64
1.08M
    if (fastargs[0]) {
65
1.08M
        arg = fastargs[0];
66
1.08M
        if (!--noptargs) {
67
1.08M
            goto skip_optional_pos;
68
1.08M
        }
69
1.08M
    }
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
1.51M
skip_optional_pos:
102
1.51M
    return_value = bytearray___init___impl((PyByteArrayObject *)self, arg, encoding, errors);
103
104
1.51M
exit:
105
1.51M
    return return_value;
106
1.51M
}
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
2.08M
{
131
2.08M
    PyObject *return_value = NULL;
132
2.08M
    PyObject *sub;
133
2.08M
    Py_ssize_t start = 0;
134
2.08M
    Py_ssize_t end = PY_SSIZE_T_MAX;
135
136
2.08M
    if (!_PyArg_CheckPositional("find", nargs, 1, 3)) {
137
0
        goto exit;
138
0
    }
139
2.08M
    sub = args[0];
140
2.08M
    if (nargs < 2) {
141
0
        goto skip_optional;
142
0
    }
143
2.08M
    if (!_PyEval_SliceIndex(args[1], &start)) {
144
0
        goto exit;
145
0
    }
146
2.08M
    if (nargs < 3) {
147
2.08M
        goto skip_optional;
148
2.08M
    }
149
0
    if (!_PyEval_SliceIndex(args[2], &end)) {
150
0
        goto exit;
151
0
    }
152
2.08M
skip_optional:
153
2.08M
    Py_BEGIN_CRITICAL_SECTION(self);
154
2.08M
    return_value = bytearray_find_impl((PyByteArrayObject *)self, sub, start, end);
155
2.08M
    Py_END_CRITICAL_SECTION();
156
157
2.08M
exit:
158
2.08M
    return return_value;
159
2.08M
}
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
    return_value = bytearray_resize_impl((PyByteArrayObject *)self, size);
629
630
0
exit:
631
0
    return return_value;
632
0
}
633
634
PyDoc_STRVAR(bytearray_take_bytes__doc__,
635
"take_bytes($self, n=None, /)\n"
636
"--\n"
637
"\n"
638
"Take *n* bytes from the bytearray and return them as a bytes object.\n"
639
"\n"
640
"  n\n"
641
"    Bytes to take, negative indexes from end. None indicates all bytes.");
642
643
#define BYTEARRAY_TAKE_BYTES_METHODDEF    \
644
    {"take_bytes", _PyCFunction_CAST(bytearray_take_bytes), METH_FASTCALL, bytearray_take_bytes__doc__},
645
646
static PyObject *
647
bytearray_take_bytes_impl(PyByteArrayObject *self, PyObject *n);
648
649
static PyObject *
650
bytearray_take_bytes(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
651
0
{
652
0
    PyObject *return_value = NULL;
653
0
    PyObject *n = Py_None;
654
655
0
    if (!_PyArg_CheckPositional("take_bytes", nargs, 0, 1)) {
656
0
        goto exit;
657
0
    }
658
0
    if (nargs < 1) {
659
0
        goto skip_optional;
660
0
    }
661
0
    n = args[0];
662
0
skip_optional:
663
0
    Py_BEGIN_CRITICAL_SECTION(self);
664
0
    return_value = bytearray_take_bytes_impl((PyByteArrayObject *)self, n);
665
0
    Py_END_CRITICAL_SECTION();
666
667
0
exit:
668
0
    return return_value;
669
0
}
670
671
PyDoc_STRVAR(bytearray_translate__doc__,
672
"translate($self, table, /, delete=b\'\')\n"
673
"--\n"
674
"\n"
675
"Return a copy with each character mapped by the given translation table.\n"
676
"\n"
677
"  table\n"
678
"    Translation table, which must be a bytes object of length 256.\n"
679
"\n"
680
"All characters occurring in the optional argument delete are removed.\n"
681
"The remaining characters are mapped through the given translation table.");
682
683
#define BYTEARRAY_TRANSLATE_METHODDEF    \
684
    {"translate", _PyCFunction_CAST(bytearray_translate), METH_FASTCALL|METH_KEYWORDS, bytearray_translate__doc__},
685
686
static PyObject *
687
bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
688
                         PyObject *deletechars);
689
690
static PyObject *
691
bytearray_translate(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
692
54.5k
{
693
54.5k
    PyObject *return_value = NULL;
694
54.5k
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
695
696
54.5k
    #define NUM_KEYWORDS 1
697
54.5k
    static struct {
698
54.5k
        PyGC_Head _this_is_not_used;
699
54.5k
        PyObject_VAR_HEAD
700
54.5k
        Py_hash_t ob_hash;
701
54.5k
        PyObject *ob_item[NUM_KEYWORDS];
702
54.5k
    } _kwtuple = {
703
54.5k
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
704
54.5k
        .ob_hash = -1,
705
54.5k
        .ob_item = { &_Py_ID(delete), },
706
54.5k
    };
707
54.5k
    #undef NUM_KEYWORDS
708
54.5k
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
709
710
    #else  // !Py_BUILD_CORE
711
    #  define KWTUPLE NULL
712
    #endif  // !Py_BUILD_CORE
713
714
54.5k
    static const char * const _keywords[] = {"", "delete", NULL};
715
54.5k
    static _PyArg_Parser _parser = {
716
54.5k
        .keywords = _keywords,
717
54.5k
        .fname = "translate",
718
54.5k
        .kwtuple = KWTUPLE,
719
54.5k
    };
720
54.5k
    #undef KWTUPLE
721
54.5k
    PyObject *argsbuf[2];
722
54.5k
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
723
54.5k
    PyObject *table;
724
54.5k
    PyObject *deletechars = NULL;
725
726
54.5k
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
727
54.5k
            /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
728
54.5k
    if (!args) {
729
0
        goto exit;
730
0
    }
731
54.5k
    table = args[0];
732
54.5k
    if (!noptargs) {
733
54.5k
        goto skip_optional_pos;
734
54.5k
    }
735
0
    deletechars = args[1];
736
54.5k
skip_optional_pos:
737
54.5k
    Py_BEGIN_CRITICAL_SECTION(self);
738
54.5k
    return_value = bytearray_translate_impl((PyByteArrayObject *)self, table, deletechars);
739
54.5k
    Py_END_CRITICAL_SECTION();
740
741
54.5k
exit:
742
54.5k
    return return_value;
743
54.5k
}
744
745
PyDoc_STRVAR(bytearray_maketrans__doc__,
746
"maketrans(frm, to, /)\n"
747
"--\n"
748
"\n"
749
"Return a translation table usable for the bytes or bytearray translate method.\n"
750
"\n"
751
"The returned table will be one where each byte in frm is mapped to the byte at\n"
752
"the same position in to.\n"
753
"\n"
754
"The bytes objects frm and to must be of the same length.");
755
756
#define BYTEARRAY_MAKETRANS_METHODDEF    \
757
    {"maketrans", _PyCFunction_CAST(bytearray_maketrans), METH_FASTCALL|METH_STATIC, bytearray_maketrans__doc__},
758
759
static PyObject *
760
bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to);
761
762
static PyObject *
763
bytearray_maketrans(PyObject *null, PyObject *const *args, Py_ssize_t nargs)
764
0
{
765
0
    PyObject *return_value = NULL;
766
0
    Py_buffer frm = {NULL, NULL};
767
0
    Py_buffer to = {NULL, NULL};
768
769
0
    if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) {
770
0
        goto exit;
771
0
    }
772
0
    if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) {
773
0
        goto exit;
774
0
    }
775
0
    if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) {
776
0
        goto exit;
777
0
    }
778
0
    return_value = bytearray_maketrans_impl(&frm, &to);
779
780
0
exit:
781
    /* Cleanup for frm */
782
0
    if (frm.obj) {
783
0
       PyBuffer_Release(&frm);
784
0
    }
785
    /* Cleanup for to */
786
0
    if (to.obj) {
787
0
       PyBuffer_Release(&to);
788
0
    }
789
790
0
    return return_value;
791
0
}
792
793
PyDoc_STRVAR(bytearray_replace__doc__,
794
"replace($self, old, new, count=-1, /)\n"
795
"--\n"
796
"\n"
797
"Return a copy with all occurrences of substring old replaced by new.\n"
798
"\n"
799
"  count\n"
800
"    Maximum number of occurrences to replace.\n"
801
"    -1 (the default value) means replace all occurrences.\n"
802
"\n"
803
"If the optional argument count is given, only the first count occurrences are\n"
804
"replaced.");
805
806
#define BYTEARRAY_REPLACE_METHODDEF    \
807
    {"replace", _PyCFunction_CAST(bytearray_replace), METH_FASTCALL, bytearray_replace__doc__},
808
809
static PyObject *
810
bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
811
                       Py_buffer *new, Py_ssize_t count);
812
813
static PyObject *
814
bytearray_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
815
0
{
816
0
    PyObject *return_value = NULL;
817
0
    Py_buffer old = {NULL, NULL};
818
0
    Py_buffer new = {NULL, NULL};
819
0
    Py_ssize_t count = -1;
820
821
0
    if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
822
0
        goto exit;
823
0
    }
824
0
    if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) {
825
0
        goto exit;
826
0
    }
827
0
    if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) {
828
0
        goto exit;
829
0
    }
830
0
    if (nargs < 3) {
831
0
        goto skip_optional;
832
0
    }
833
0
    {
834
0
        Py_ssize_t ival = -1;
835
0
        PyObject *iobj = _PyNumber_Index(args[2]);
836
0
        if (iobj != NULL) {
837
0
            ival = PyLong_AsSsize_t(iobj);
838
0
            Py_DECREF(iobj);
839
0
        }
840
0
        if (ival == -1 && PyErr_Occurred()) {
841
0
            goto exit;
842
0
        }
843
0
        count = ival;
844
0
    }
845
0
skip_optional:
846
0
    Py_BEGIN_CRITICAL_SECTION(self);
847
0
    return_value = bytearray_replace_impl((PyByteArrayObject *)self, &old, &new, count);
848
0
    Py_END_CRITICAL_SECTION();
849
850
0
exit:
851
    /* Cleanup for old */
852
0
    if (old.obj) {
853
0
       PyBuffer_Release(&old);
854
0
    }
855
    /* Cleanup for new */
856
0
    if (new.obj) {
857
0
       PyBuffer_Release(&new);
858
0
    }
859
860
0
    return return_value;
861
0
}
862
863
PyDoc_STRVAR(bytearray_split__doc__,
864
"split($self, /, sep=None, maxsplit=-1)\n"
865
"--\n"
866
"\n"
867
"Return a list of the sections in the bytearray, using sep as the delimiter.\n"
868
"\n"
869
"  sep\n"
870
"    The delimiter according which to split the bytearray.\n"
871
"    None (the default value) means split on ASCII whitespace characters\n"
872
"    (space, tab, return, newline, formfeed, vertical tab).\n"
873
"  maxsplit\n"
874
"    Maximum number of splits to do.\n"
875
"    -1 (the default value) means no limit.");
876
877
#define BYTEARRAY_SPLIT_METHODDEF    \
878
    {"split", _PyCFunction_CAST(bytearray_split), METH_FASTCALL|METH_KEYWORDS, bytearray_split__doc__},
879
880
static PyObject *
881
bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
882
                     Py_ssize_t maxsplit);
883
884
static PyObject *
885
bytearray_split(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
886
0
{
887
0
    PyObject *return_value = NULL;
888
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
889
890
0
    #define NUM_KEYWORDS 2
891
0
    static struct {
892
0
        PyGC_Head _this_is_not_used;
893
0
        PyObject_VAR_HEAD
894
0
        Py_hash_t ob_hash;
895
0
        PyObject *ob_item[NUM_KEYWORDS];
896
0
    } _kwtuple = {
897
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
898
0
        .ob_hash = -1,
899
0
        .ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), },
900
0
    };
901
0
    #undef NUM_KEYWORDS
902
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
903
904
    #else  // !Py_BUILD_CORE
905
    #  define KWTUPLE NULL
906
    #endif  // !Py_BUILD_CORE
907
908
0
    static const char * const _keywords[] = {"sep", "maxsplit", NULL};
909
0
    static _PyArg_Parser _parser = {
910
0
        .keywords = _keywords,
911
0
        .fname = "split",
912
0
        .kwtuple = KWTUPLE,
913
0
    };
914
0
    #undef KWTUPLE
915
0
    PyObject *argsbuf[2];
916
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
917
0
    PyObject *sep = Py_None;
918
0
    Py_ssize_t maxsplit = -1;
919
920
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
921
0
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
922
0
    if (!args) {
923
0
        goto exit;
924
0
    }
925
0
    if (!noptargs) {
926
0
        goto skip_optional_pos;
927
0
    }
928
0
    if (args[0]) {
929
0
        sep = args[0];
930
0
        if (!--noptargs) {
931
0
            goto skip_optional_pos;
932
0
        }
933
0
    }
934
0
    {
935
0
        Py_ssize_t ival = -1;
936
0
        PyObject *iobj = _PyNumber_Index(args[1]);
937
0
        if (iobj != NULL) {
938
0
            ival = PyLong_AsSsize_t(iobj);
939
0
            Py_DECREF(iobj);
940
0
        }
941
0
        if (ival == -1 && PyErr_Occurred()) {
942
0
            goto exit;
943
0
        }
944
0
        maxsplit = ival;
945
0
    }
946
0
skip_optional_pos:
947
0
    Py_BEGIN_CRITICAL_SECTION(self);
948
0
    return_value = bytearray_split_impl((PyByteArrayObject *)self, sep, maxsplit);
949
0
    Py_END_CRITICAL_SECTION();
950
951
0
exit:
952
0
    return return_value;
953
0
}
954
955
PyDoc_STRVAR(bytearray_partition__doc__,
956
"partition($self, sep, /)\n"
957
"--\n"
958
"\n"
959
"Partition the bytearray into three parts using the given separator.\n"
960
"\n"
961
"This will search for the separator sep in the bytearray. If the separator is\n"
962
"found, returns a 3-tuple containing the part before the separator, the\n"
963
"separator itself, and the part after it as new bytearray objects.\n"
964
"\n"
965
"If the separator is not found, returns a 3-tuple containing the copy of the\n"
966
"original bytearray object and two empty bytearray objects.");
967
968
#define BYTEARRAY_PARTITION_METHODDEF    \
969
    {"partition", (PyCFunction)bytearray_partition, METH_O, bytearray_partition__doc__},
970
971
static PyObject *
972
bytearray_partition_impl(PyByteArrayObject *self, PyObject *sep);
973
974
static PyObject *
975
bytearray_partition(PyObject *self, PyObject *sep)
976
0
{
977
0
    PyObject *return_value = NULL;
978
979
0
    Py_BEGIN_CRITICAL_SECTION(self);
980
0
    return_value = bytearray_partition_impl((PyByteArrayObject *)self, sep);
981
0
    Py_END_CRITICAL_SECTION();
982
983
0
    return return_value;
984
0
}
985
986
PyDoc_STRVAR(bytearray_rpartition__doc__,
987
"rpartition($self, sep, /)\n"
988
"--\n"
989
"\n"
990
"Partition the bytearray into three parts using the given separator.\n"
991
"\n"
992
"This will search for the separator sep in the bytearray, starting at the end.\n"
993
"If the separator is found, returns a 3-tuple containing the part before the\n"
994
"separator, the separator itself, and the part after it as new bytearray\n"
995
"objects.\n"
996
"\n"
997
"If the separator is not found, returns a 3-tuple containing two empty bytearray\n"
998
"objects and the copy of the original bytearray object.");
999
1000
#define BYTEARRAY_RPARTITION_METHODDEF    \
1001
    {"rpartition", (PyCFunction)bytearray_rpartition, METH_O, bytearray_rpartition__doc__},
1002
1003
static PyObject *
1004
bytearray_rpartition_impl(PyByteArrayObject *self, PyObject *sep);
1005
1006
static PyObject *
1007
bytearray_rpartition(PyObject *self, PyObject *sep)
1008
0
{
1009
0
    PyObject *return_value = NULL;
1010
1011
0
    Py_BEGIN_CRITICAL_SECTION(self);
1012
0
    return_value = bytearray_rpartition_impl((PyByteArrayObject *)self, sep);
1013
0
    Py_END_CRITICAL_SECTION();
1014
1015
0
    return return_value;
1016
0
}
1017
1018
PyDoc_STRVAR(bytearray_rsplit__doc__,
1019
"rsplit($self, /, sep=None, maxsplit=-1)\n"
1020
"--\n"
1021
"\n"
1022
"Return a list of the sections in the bytearray, using sep as the delimiter.\n"
1023
"\n"
1024
"  sep\n"
1025
"    The delimiter according which to split the bytearray.\n"
1026
"    None (the default value) means split on ASCII whitespace characters\n"
1027
"    (space, tab, return, newline, formfeed, vertical tab).\n"
1028
"  maxsplit\n"
1029
"    Maximum number of splits to do.\n"
1030
"    -1 (the default value) means no limit.\n"
1031
"\n"
1032
"Splitting is done starting at the end of the bytearray and working to the front.");
1033
1034
#define BYTEARRAY_RSPLIT_METHODDEF    \
1035
    {"rsplit", _PyCFunction_CAST(bytearray_rsplit), METH_FASTCALL|METH_KEYWORDS, bytearray_rsplit__doc__},
1036
1037
static PyObject *
1038
bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
1039
                      Py_ssize_t maxsplit);
1040
1041
static PyObject *
1042
bytearray_rsplit(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1043
0
{
1044
0
    PyObject *return_value = NULL;
1045
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1046
1047
0
    #define NUM_KEYWORDS 2
1048
0
    static struct {
1049
0
        PyGC_Head _this_is_not_used;
1050
0
        PyObject_VAR_HEAD
1051
0
        Py_hash_t ob_hash;
1052
0
        PyObject *ob_item[NUM_KEYWORDS];
1053
0
    } _kwtuple = {
1054
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1055
0
        .ob_hash = -1,
1056
0
        .ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), },
1057
0
    };
1058
0
    #undef NUM_KEYWORDS
1059
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1060
1061
    #else  // !Py_BUILD_CORE
1062
    #  define KWTUPLE NULL
1063
    #endif  // !Py_BUILD_CORE
1064
1065
0
    static const char * const _keywords[] = {"sep", "maxsplit", NULL};
1066
0
    static _PyArg_Parser _parser = {
1067
0
        .keywords = _keywords,
1068
0
        .fname = "rsplit",
1069
0
        .kwtuple = KWTUPLE,
1070
0
    };
1071
0
    #undef KWTUPLE
1072
0
    PyObject *argsbuf[2];
1073
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1074
0
    PyObject *sep = Py_None;
1075
0
    Py_ssize_t maxsplit = -1;
1076
1077
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1078
0
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1079
0
    if (!args) {
1080
0
        goto exit;
1081
0
    }
1082
0
    if (!noptargs) {
1083
0
        goto skip_optional_pos;
1084
0
    }
1085
0
    if (args[0]) {
1086
0
        sep = args[0];
1087
0
        if (!--noptargs) {
1088
0
            goto skip_optional_pos;
1089
0
        }
1090
0
    }
1091
0
    {
1092
0
        Py_ssize_t ival = -1;
1093
0
        PyObject *iobj = _PyNumber_Index(args[1]);
1094
0
        if (iobj != NULL) {
1095
0
            ival = PyLong_AsSsize_t(iobj);
1096
0
            Py_DECREF(iobj);
1097
0
        }
1098
0
        if (ival == -1 && PyErr_Occurred()) {
1099
0
            goto exit;
1100
0
        }
1101
0
        maxsplit = ival;
1102
0
    }
1103
0
skip_optional_pos:
1104
0
    Py_BEGIN_CRITICAL_SECTION(self);
1105
0
    return_value = bytearray_rsplit_impl((PyByteArrayObject *)self, sep, maxsplit);
1106
0
    Py_END_CRITICAL_SECTION();
1107
1108
0
exit:
1109
0
    return return_value;
1110
0
}
1111
1112
PyDoc_STRVAR(bytearray_reverse__doc__,
1113
"reverse($self, /)\n"
1114
"--\n"
1115
"\n"
1116
"Reverse the order of the values in B in place.");
1117
1118
#define BYTEARRAY_REVERSE_METHODDEF    \
1119
    {"reverse", (PyCFunction)bytearray_reverse, METH_NOARGS, bytearray_reverse__doc__},
1120
1121
static PyObject *
1122
bytearray_reverse_impl(PyByteArrayObject *self);
1123
1124
static PyObject *
1125
bytearray_reverse(PyObject *self, PyObject *Py_UNUSED(ignored))
1126
0
{
1127
0
    PyObject *return_value = NULL;
1128
1129
0
    Py_BEGIN_CRITICAL_SECTION(self);
1130
0
    return_value = bytearray_reverse_impl((PyByteArrayObject *)self);
1131
0
    Py_END_CRITICAL_SECTION();
1132
1133
0
    return return_value;
1134
0
}
1135
1136
PyDoc_STRVAR(bytearray_insert__doc__,
1137
"insert($self, index, item, /)\n"
1138
"--\n"
1139
"\n"
1140
"Insert a single item into the bytearray before the given index.\n"
1141
"\n"
1142
"  index\n"
1143
"    The index where the value is to be inserted.\n"
1144
"  item\n"
1145
"    The item to be inserted.");
1146
1147
#define BYTEARRAY_INSERT_METHODDEF    \
1148
    {"insert", _PyCFunction_CAST(bytearray_insert), METH_FASTCALL, bytearray_insert__doc__},
1149
1150
static PyObject *
1151
bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item);
1152
1153
static PyObject *
1154
bytearray_insert(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1155
0
{
1156
0
    PyObject *return_value = NULL;
1157
0
    Py_ssize_t index;
1158
0
    int item;
1159
1160
0
    if (!_PyArg_CheckPositional("insert", nargs, 2, 2)) {
1161
0
        goto exit;
1162
0
    }
1163
0
    {
1164
0
        Py_ssize_t ival = -1;
1165
0
        PyObject *iobj = _PyNumber_Index(args[0]);
1166
0
        if (iobj != NULL) {
1167
0
            ival = PyLong_AsSsize_t(iobj);
1168
0
            Py_DECREF(iobj);
1169
0
        }
1170
0
        if (ival == -1 && PyErr_Occurred()) {
1171
0
            goto exit;
1172
0
        }
1173
0
        index = ival;
1174
0
    }
1175
0
    if (!_getbytevalue(args[1], &item)) {
1176
0
        goto exit;
1177
0
    }
1178
0
    Py_BEGIN_CRITICAL_SECTION(self);
1179
0
    return_value = bytearray_insert_impl((PyByteArrayObject *)self, index, item);
1180
0
    Py_END_CRITICAL_SECTION();
1181
1182
0
exit:
1183
0
    return return_value;
1184
0
}
1185
1186
PyDoc_STRVAR(bytearray_append__doc__,
1187
"append($self, item, /)\n"
1188
"--\n"
1189
"\n"
1190
"Append a single item to the end of the bytearray.\n"
1191
"\n"
1192
"  item\n"
1193
"    The item to be appended.");
1194
1195
#define BYTEARRAY_APPEND_METHODDEF    \
1196
    {"append", (PyCFunction)bytearray_append, METH_O, bytearray_append__doc__},
1197
1198
static PyObject *
1199
bytearray_append_impl(PyByteArrayObject *self, int item);
1200
1201
static PyObject *
1202
bytearray_append(PyObject *self, PyObject *arg)
1203
511k
{
1204
511k
    PyObject *return_value = NULL;
1205
511k
    int item;
1206
1207
511k
    if (!_getbytevalue(arg, &item)) {
1208
0
        goto exit;
1209
0
    }
1210
511k
    Py_BEGIN_CRITICAL_SECTION(self);
1211
511k
    return_value = bytearray_append_impl((PyByteArrayObject *)self, item);
1212
511k
    Py_END_CRITICAL_SECTION();
1213
1214
511k
exit:
1215
511k
    return return_value;
1216
511k
}
1217
1218
PyDoc_STRVAR(bytearray_extend__doc__,
1219
"extend($self, iterable_of_ints, /)\n"
1220
"--\n"
1221
"\n"
1222
"Append all the items from the iterator or sequence to the end of the bytearray.\n"
1223
"\n"
1224
"  iterable_of_ints\n"
1225
"    The iterable of items to append.");
1226
1227
#define BYTEARRAY_EXTEND_METHODDEF    \
1228
    {"extend", (PyCFunction)bytearray_extend, METH_O, bytearray_extend__doc__},
1229
1230
static PyObject *
1231
bytearray_extend_impl(PyByteArrayObject *self, PyObject *iterable_of_ints);
1232
1233
static PyObject *
1234
bytearray_extend(PyObject *self, PyObject *iterable_of_ints)
1235
8.92M
{
1236
8.92M
    PyObject *return_value = NULL;
1237
1238
8.92M
    Py_BEGIN_CRITICAL_SECTION(self);
1239
8.92M
    return_value = bytearray_extend_impl((PyByteArrayObject *)self, iterable_of_ints);
1240
8.92M
    Py_END_CRITICAL_SECTION();
1241
1242
8.92M
    return return_value;
1243
8.92M
}
1244
1245
PyDoc_STRVAR(bytearray_pop__doc__,
1246
"pop($self, index=-1, /)\n"
1247
"--\n"
1248
"\n"
1249
"Remove and return a single item from B.\n"
1250
"\n"
1251
"  index\n"
1252
"    The index from where to remove the item.\n"
1253
"    -1 (the default value) means remove the last item.\n"
1254
"\n"
1255
"If no index argument is given, will pop the last item.");
1256
1257
#define BYTEARRAY_POP_METHODDEF    \
1258
    {"pop", _PyCFunction_CAST(bytearray_pop), METH_FASTCALL, bytearray_pop__doc__},
1259
1260
static PyObject *
1261
bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index);
1262
1263
static PyObject *
1264
bytearray_pop(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1265
0
{
1266
0
    PyObject *return_value = NULL;
1267
0
    Py_ssize_t index = -1;
1268
1269
0
    if (!_PyArg_CheckPositional("pop", nargs, 0, 1)) {
1270
0
        goto exit;
1271
0
    }
1272
0
    if (nargs < 1) {
1273
0
        goto skip_optional;
1274
0
    }
1275
0
    {
1276
0
        Py_ssize_t ival = -1;
1277
0
        PyObject *iobj = _PyNumber_Index(args[0]);
1278
0
        if (iobj != NULL) {
1279
0
            ival = PyLong_AsSsize_t(iobj);
1280
0
            Py_DECREF(iobj);
1281
0
        }
1282
0
        if (ival == -1 && PyErr_Occurred()) {
1283
0
            goto exit;
1284
0
        }
1285
0
        index = ival;
1286
0
    }
1287
0
skip_optional:
1288
0
    Py_BEGIN_CRITICAL_SECTION(self);
1289
0
    return_value = bytearray_pop_impl((PyByteArrayObject *)self, index);
1290
0
    Py_END_CRITICAL_SECTION();
1291
1292
0
exit:
1293
0
    return return_value;
1294
0
}
1295
1296
PyDoc_STRVAR(bytearray_remove__doc__,
1297
"remove($self, value, /)\n"
1298
"--\n"
1299
"\n"
1300
"Remove the first occurrence of a value in the bytearray.\n"
1301
"\n"
1302
"  value\n"
1303
"    The value to remove.");
1304
1305
#define BYTEARRAY_REMOVE_METHODDEF    \
1306
    {"remove", (PyCFunction)bytearray_remove, METH_O, bytearray_remove__doc__},
1307
1308
static PyObject *
1309
bytearray_remove_impl(PyByteArrayObject *self, int value);
1310
1311
static PyObject *
1312
bytearray_remove(PyObject *self, PyObject *arg)
1313
0
{
1314
0
    PyObject *return_value = NULL;
1315
0
    int value;
1316
1317
0
    if (!_getbytevalue(arg, &value)) {
1318
0
        goto exit;
1319
0
    }
1320
0
    Py_BEGIN_CRITICAL_SECTION(self);
1321
0
    return_value = bytearray_remove_impl((PyByteArrayObject *)self, value);
1322
0
    Py_END_CRITICAL_SECTION();
1323
1324
0
exit:
1325
0
    return return_value;
1326
0
}
1327
1328
PyDoc_STRVAR(bytearray_strip__doc__,
1329
"strip($self, bytes=None, /)\n"
1330
"--\n"
1331
"\n"
1332
"Strip leading and trailing bytes contained in the argument.\n"
1333
"\n"
1334
"If the argument is omitted or None, strip leading and trailing ASCII whitespace.");
1335
1336
#define BYTEARRAY_STRIP_METHODDEF    \
1337
    {"strip", _PyCFunction_CAST(bytearray_strip), METH_FASTCALL, bytearray_strip__doc__},
1338
1339
static PyObject *
1340
bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes);
1341
1342
static PyObject *
1343
bytearray_strip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1344
0
{
1345
0
    PyObject *return_value = NULL;
1346
0
    PyObject *bytes = Py_None;
1347
1348
0
    if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
1349
0
        goto exit;
1350
0
    }
1351
0
    if (nargs < 1) {
1352
0
        goto skip_optional;
1353
0
    }
1354
0
    bytes = args[0];
1355
0
skip_optional:
1356
0
    Py_BEGIN_CRITICAL_SECTION(self);
1357
0
    return_value = bytearray_strip_impl((PyByteArrayObject *)self, bytes);
1358
0
    Py_END_CRITICAL_SECTION();
1359
1360
0
exit:
1361
0
    return return_value;
1362
0
}
1363
1364
PyDoc_STRVAR(bytearray_lstrip__doc__,
1365
"lstrip($self, bytes=None, /)\n"
1366
"--\n"
1367
"\n"
1368
"Strip leading bytes contained in the argument.\n"
1369
"\n"
1370
"If the argument is omitted or None, strip leading ASCII whitespace.");
1371
1372
#define BYTEARRAY_LSTRIP_METHODDEF    \
1373
    {"lstrip", _PyCFunction_CAST(bytearray_lstrip), METH_FASTCALL, bytearray_lstrip__doc__},
1374
1375
static PyObject *
1376
bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes);
1377
1378
static PyObject *
1379
bytearray_lstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1380
0
{
1381
0
    PyObject *return_value = NULL;
1382
0
    PyObject *bytes = Py_None;
1383
1384
0
    if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
1385
0
        goto exit;
1386
0
    }
1387
0
    if (nargs < 1) {
1388
0
        goto skip_optional;
1389
0
    }
1390
0
    bytes = args[0];
1391
0
skip_optional:
1392
0
    Py_BEGIN_CRITICAL_SECTION(self);
1393
0
    return_value = bytearray_lstrip_impl((PyByteArrayObject *)self, bytes);
1394
0
    Py_END_CRITICAL_SECTION();
1395
1396
0
exit:
1397
0
    return return_value;
1398
0
}
1399
1400
PyDoc_STRVAR(bytearray_rstrip__doc__,
1401
"rstrip($self, bytes=None, /)\n"
1402
"--\n"
1403
"\n"
1404
"Strip trailing bytes contained in the argument.\n"
1405
"\n"
1406
"If the argument is omitted or None, strip trailing ASCII whitespace.");
1407
1408
#define BYTEARRAY_RSTRIP_METHODDEF    \
1409
    {"rstrip", _PyCFunction_CAST(bytearray_rstrip), METH_FASTCALL, bytearray_rstrip__doc__},
1410
1411
static PyObject *
1412
bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes);
1413
1414
static PyObject *
1415
bytearray_rstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1416
0
{
1417
0
    PyObject *return_value = NULL;
1418
0
    PyObject *bytes = Py_None;
1419
1420
0
    if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
1421
0
        goto exit;
1422
0
    }
1423
0
    if (nargs < 1) {
1424
0
        goto skip_optional;
1425
0
    }
1426
0
    bytes = args[0];
1427
0
skip_optional:
1428
0
    Py_BEGIN_CRITICAL_SECTION(self);
1429
0
    return_value = bytearray_rstrip_impl((PyByteArrayObject *)self, bytes);
1430
0
    Py_END_CRITICAL_SECTION();
1431
1432
0
exit:
1433
0
    return return_value;
1434
0
}
1435
1436
PyDoc_STRVAR(bytearray_decode__doc__,
1437
"decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
1438
"--\n"
1439
"\n"
1440
"Decode the bytearray using the codec registered for encoding.\n"
1441
"\n"
1442
"  encoding\n"
1443
"    The encoding with which to decode the bytearray.\n"
1444
"  errors\n"
1445
"    The error handling scheme to use for the handling of decoding errors.\n"
1446
"    The default is \'strict\' meaning that decoding errors raise a\n"
1447
"    UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n"
1448
"    as well as any other name registered with codecs.register_error that\n"
1449
"    can handle UnicodeDecodeErrors.");
1450
1451
#define BYTEARRAY_DECODE_METHODDEF    \
1452
    {"decode", _PyCFunction_CAST(bytearray_decode), METH_FASTCALL|METH_KEYWORDS, bytearray_decode__doc__},
1453
1454
static PyObject *
1455
bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
1456
                      const char *errors);
1457
1458
static PyObject *
1459
bytearray_decode(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1460
575k
{
1461
575k
    PyObject *return_value = NULL;
1462
575k
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1463
1464
575k
    #define NUM_KEYWORDS 2
1465
575k
    static struct {
1466
575k
        PyGC_Head _this_is_not_used;
1467
575k
        PyObject_VAR_HEAD
1468
575k
        Py_hash_t ob_hash;
1469
575k
        PyObject *ob_item[NUM_KEYWORDS];
1470
575k
    } _kwtuple = {
1471
575k
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1472
575k
        .ob_hash = -1,
1473
575k
        .ob_item = { &_Py_ID(encoding), &_Py_ID(errors), },
1474
575k
    };
1475
575k
    #undef NUM_KEYWORDS
1476
575k
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1477
1478
    #else  // !Py_BUILD_CORE
1479
    #  define KWTUPLE NULL
1480
    #endif  // !Py_BUILD_CORE
1481
1482
575k
    static const char * const _keywords[] = {"encoding", "errors", NULL};
1483
575k
    static _PyArg_Parser _parser = {
1484
575k
        .keywords = _keywords,
1485
575k
        .fname = "decode",
1486
575k
        .kwtuple = KWTUPLE,
1487
575k
    };
1488
575k
    #undef KWTUPLE
1489
575k
    PyObject *argsbuf[2];
1490
575k
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1491
575k
    const char *encoding = NULL;
1492
575k
    const char *errors = NULL;
1493
1494
575k
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1495
575k
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1496
575k
    if (!args) {
1497
0
        goto exit;
1498
0
    }
1499
575k
    if (!noptargs) {
1500
0
        goto skip_optional_pos;
1501
0
    }
1502
575k
    if (args[0]) {
1503
575k
        if (!PyUnicode_Check(args[0])) {
1504
0
            _PyArg_BadArgument("decode", "argument 'encoding'", "str", args[0]);
1505
0
            goto exit;
1506
0
        }
1507
575k
        Py_ssize_t encoding_length;
1508
575k
        encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length);
1509
575k
        if (encoding == NULL) {
1510
0
            goto exit;
1511
0
        }
1512
575k
        if (strlen(encoding) != (size_t)encoding_length) {
1513
0
            PyErr_SetString(PyExc_ValueError, "embedded null character");
1514
0
            goto exit;
1515
0
        }
1516
575k
        if (!--noptargs) {
1517
0
            goto skip_optional_pos;
1518
0
        }
1519
575k
    }
1520
575k
    if (!PyUnicode_Check(args[1])) {
1521
0
        _PyArg_BadArgument("decode", "argument 'errors'", "str", args[1]);
1522
0
        goto exit;
1523
0
    }
1524
575k
    Py_ssize_t errors_length;
1525
575k
    errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
1526
575k
    if (errors == NULL) {
1527
0
        goto exit;
1528
0
    }
1529
575k
    if (strlen(errors) != (size_t)errors_length) {
1530
0
        PyErr_SetString(PyExc_ValueError, "embedded null character");
1531
0
        goto exit;
1532
0
    }
1533
575k
skip_optional_pos:
1534
575k
    Py_BEGIN_CRITICAL_SECTION(self);
1535
575k
    return_value = bytearray_decode_impl((PyByteArrayObject *)self, encoding, errors);
1536
575k
    Py_END_CRITICAL_SECTION();
1537
1538
575k
exit:
1539
575k
    return return_value;
1540
575k
}
1541
1542
PyDoc_STRVAR(bytearray_join__doc__,
1543
"join($self, iterable_of_bytes, /)\n"
1544
"--\n"
1545
"\n"
1546
"Concatenate any number of bytes/bytearray objects.\n"
1547
"\n"
1548
"The bytearray whose method is called is inserted in between each pair.\n"
1549
"\n"
1550
"The result is returned as a new bytearray object.");
1551
1552
#define BYTEARRAY_JOIN_METHODDEF    \
1553
    {"join", (PyCFunction)bytearray_join, METH_O, bytearray_join__doc__},
1554
1555
static PyObject *
1556
bytearray_join_impl(PyByteArrayObject *self, PyObject *iterable_of_bytes);
1557
1558
static PyObject *
1559
bytearray_join(PyObject *self, PyObject *iterable_of_bytes)
1560
0
{
1561
0
    PyObject *return_value = NULL;
1562
1563
0
    Py_BEGIN_CRITICAL_SECTION(self);
1564
0
    return_value = bytearray_join_impl((PyByteArrayObject *)self, iterable_of_bytes);
1565
0
    Py_END_CRITICAL_SECTION();
1566
1567
0
    return return_value;
1568
0
}
1569
1570
PyDoc_STRVAR(bytearray_splitlines__doc__,
1571
"splitlines($self, /, keepends=False)\n"
1572
"--\n"
1573
"\n"
1574
"Return a list of the lines in the bytearray, breaking at line boundaries.\n"
1575
"\n"
1576
"Line breaks are not included in the resulting list unless keepends is given and\n"
1577
"true.");
1578
1579
#define BYTEARRAY_SPLITLINES_METHODDEF    \
1580
    {"splitlines", _PyCFunction_CAST(bytearray_splitlines), METH_FASTCALL|METH_KEYWORDS, bytearray_splitlines__doc__},
1581
1582
static PyObject *
1583
bytearray_splitlines_impl(PyByteArrayObject *self, int keepends);
1584
1585
static PyObject *
1586
bytearray_splitlines(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1587
0
{
1588
0
    PyObject *return_value = NULL;
1589
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1590
1591
0
    #define NUM_KEYWORDS 1
1592
0
    static struct {
1593
0
        PyGC_Head _this_is_not_used;
1594
0
        PyObject_VAR_HEAD
1595
0
        Py_hash_t ob_hash;
1596
0
        PyObject *ob_item[NUM_KEYWORDS];
1597
0
    } _kwtuple = {
1598
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1599
0
        .ob_hash = -1,
1600
0
        .ob_item = { &_Py_ID(keepends), },
1601
0
    };
1602
0
    #undef NUM_KEYWORDS
1603
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1604
1605
    #else  // !Py_BUILD_CORE
1606
    #  define KWTUPLE NULL
1607
    #endif  // !Py_BUILD_CORE
1608
1609
0
    static const char * const _keywords[] = {"keepends", NULL};
1610
0
    static _PyArg_Parser _parser = {
1611
0
        .keywords = _keywords,
1612
0
        .fname = "splitlines",
1613
0
        .kwtuple = KWTUPLE,
1614
0
    };
1615
0
    #undef KWTUPLE
1616
0
    PyObject *argsbuf[1];
1617
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1618
0
    int keepends = 0;
1619
1620
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1621
0
            /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1622
0
    if (!args) {
1623
0
        goto exit;
1624
0
    }
1625
0
    if (!noptargs) {
1626
0
        goto skip_optional_pos;
1627
0
    }
1628
0
    keepends = PyObject_IsTrue(args[0]);
1629
0
    if (keepends < 0) {
1630
0
        goto exit;
1631
0
    }
1632
0
skip_optional_pos:
1633
0
    Py_BEGIN_CRITICAL_SECTION(self);
1634
0
    return_value = bytearray_splitlines_impl((PyByteArrayObject *)self, keepends);
1635
0
    Py_END_CRITICAL_SECTION();
1636
1637
0
exit:
1638
0
    return return_value;
1639
0
}
1640
1641
PyDoc_STRVAR(bytearray_fromhex__doc__,
1642
"fromhex($type, string, /)\n"
1643
"--\n"
1644
"\n"
1645
"Create a bytearray object from a string of hexadecimal numbers.\n"
1646
"\n"
1647
"Spaces between two numbers are accepted.\n"
1648
"Example: bytearray.fromhex(\'B9 01EF\') -> bytearray(b\'\\\\xb9\\\\x01\\\\xef\')");
1649
1650
#define BYTEARRAY_FROMHEX_METHODDEF    \
1651
    {"fromhex", (PyCFunction)bytearray_fromhex, METH_O|METH_CLASS, bytearray_fromhex__doc__},
1652
1653
static PyObject *
1654
bytearray_fromhex_impl(PyTypeObject *type, PyObject *string);
1655
1656
static PyObject *
1657
bytearray_fromhex(PyObject *type, PyObject *string)
1658
0
{
1659
0
    PyObject *return_value = NULL;
1660
1661
0
    return_value = bytearray_fromhex_impl((PyTypeObject *)type, string);
1662
1663
0
    return return_value;
1664
0
}
1665
1666
PyDoc_STRVAR(bytearray_hex__doc__,
1667
"hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n"
1668
"--\n"
1669
"\n"
1670
"Create a string of hexadecimal numbers from a bytearray object.\n"
1671
"\n"
1672
"  sep\n"
1673
"    An optional single character or byte to separate hex bytes.\n"
1674
"  bytes_per_sep\n"
1675
"    How many bytes between separators.  Positive values count from the\n"
1676
"    right, negative values count from the left.\n"
1677
"\n"
1678
"Example:\n"
1679
">>> value = bytearray([0xb9, 0x01, 0xef])\n"
1680
">>> value.hex()\n"
1681
"\'b901ef\'\n"
1682
">>> value.hex(\':\')\n"
1683
"\'b9:01:ef\'\n"
1684
">>> value.hex(\':\', 2)\n"
1685
"\'b9:01ef\'\n"
1686
">>> value.hex(\':\', -2)\n"
1687
"\'b901:ef\'");
1688
1689
#define BYTEARRAY_HEX_METHODDEF    \
1690
    {"hex", _PyCFunction_CAST(bytearray_hex), METH_FASTCALL|METH_KEYWORDS, bytearray_hex__doc__},
1691
1692
static PyObject *
1693
bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep);
1694
1695
static PyObject *
1696
bytearray_hex(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1697
0
{
1698
0
    PyObject *return_value = NULL;
1699
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1700
1701
0
    #define NUM_KEYWORDS 2
1702
0
    static struct {
1703
0
        PyGC_Head _this_is_not_used;
1704
0
        PyObject_VAR_HEAD
1705
0
        Py_hash_t ob_hash;
1706
0
        PyObject *ob_item[NUM_KEYWORDS];
1707
0
    } _kwtuple = {
1708
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1709
0
        .ob_hash = -1,
1710
0
        .ob_item = { &_Py_ID(sep), &_Py_ID(bytes_per_sep), },
1711
0
    };
1712
0
    #undef NUM_KEYWORDS
1713
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1714
1715
    #else  // !Py_BUILD_CORE
1716
    #  define KWTUPLE NULL
1717
    #endif  // !Py_BUILD_CORE
1718
1719
0
    static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL};
1720
0
    static _PyArg_Parser _parser = {
1721
0
        .keywords = _keywords,
1722
0
        .fname = "hex",
1723
0
        .kwtuple = KWTUPLE,
1724
0
    };
1725
0
    #undef KWTUPLE
1726
0
    PyObject *argsbuf[2];
1727
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1728
0
    PyObject *sep = NULL;
1729
0
    int bytes_per_sep = 1;
1730
1731
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1732
0
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1733
0
    if (!args) {
1734
0
        goto exit;
1735
0
    }
1736
0
    if (!noptargs) {
1737
0
        goto skip_optional_pos;
1738
0
    }
1739
0
    if (args[0]) {
1740
0
        sep = args[0];
1741
0
        if (!--noptargs) {
1742
0
            goto skip_optional_pos;
1743
0
        }
1744
0
    }
1745
0
    bytes_per_sep = PyLong_AsInt(args[1]);
1746
0
    if (bytes_per_sep == -1 && PyErr_Occurred()) {
1747
0
        goto exit;
1748
0
    }
1749
0
skip_optional_pos:
1750
0
    Py_BEGIN_CRITICAL_SECTION(self);
1751
0
    return_value = bytearray_hex_impl((PyByteArrayObject *)self, sep, bytes_per_sep);
1752
0
    Py_END_CRITICAL_SECTION();
1753
1754
0
exit:
1755
0
    return return_value;
1756
0
}
1757
1758
PyDoc_STRVAR(bytearray_reduce__doc__,
1759
"__reduce__($self, /)\n"
1760
"--\n"
1761
"\n"
1762
"Return state information for pickling.");
1763
1764
#define BYTEARRAY_REDUCE_METHODDEF    \
1765
    {"__reduce__", (PyCFunction)bytearray_reduce, METH_NOARGS, bytearray_reduce__doc__},
1766
1767
static PyObject *
1768
bytearray_reduce_impl(PyByteArrayObject *self);
1769
1770
static PyObject *
1771
bytearray_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
1772
0
{
1773
0
    PyObject *return_value = NULL;
1774
1775
0
    Py_BEGIN_CRITICAL_SECTION(self);
1776
0
    return_value = bytearray_reduce_impl((PyByteArrayObject *)self);
1777
0
    Py_END_CRITICAL_SECTION();
1778
1779
0
    return return_value;
1780
0
}
1781
1782
PyDoc_STRVAR(bytearray_reduce_ex__doc__,
1783
"__reduce_ex__($self, proto=0, /)\n"
1784
"--\n"
1785
"\n"
1786
"Return state information for pickling.");
1787
1788
#define BYTEARRAY_REDUCE_EX_METHODDEF    \
1789
    {"__reduce_ex__", _PyCFunction_CAST(bytearray_reduce_ex), METH_FASTCALL, bytearray_reduce_ex__doc__},
1790
1791
static PyObject *
1792
bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto);
1793
1794
static PyObject *
1795
bytearray_reduce_ex(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1796
0
{
1797
0
    PyObject *return_value = NULL;
1798
0
    int proto = 0;
1799
1800
0
    if (!_PyArg_CheckPositional("__reduce_ex__", nargs, 0, 1)) {
1801
0
        goto exit;
1802
0
    }
1803
0
    if (nargs < 1) {
1804
0
        goto skip_optional;
1805
0
    }
1806
0
    proto = PyLong_AsInt(args[0]);
1807
0
    if (proto == -1 && PyErr_Occurred()) {
1808
0
        goto exit;
1809
0
    }
1810
0
skip_optional:
1811
0
    Py_BEGIN_CRITICAL_SECTION(self);
1812
0
    return_value = bytearray_reduce_ex_impl((PyByteArrayObject *)self, proto);
1813
0
    Py_END_CRITICAL_SECTION();
1814
1815
0
exit:
1816
0
    return return_value;
1817
0
}
1818
1819
PyDoc_STRVAR(bytearray_sizeof__doc__,
1820
"__sizeof__($self, /)\n"
1821
"--\n"
1822
"\n"
1823
"Returns the size of the bytearray object in memory, in bytes.");
1824
1825
#define BYTEARRAY_SIZEOF_METHODDEF    \
1826
    {"__sizeof__", (PyCFunction)bytearray_sizeof, METH_NOARGS, bytearray_sizeof__doc__},
1827
1828
static PyObject *
1829
bytearray_sizeof_impl(PyByteArrayObject *self);
1830
1831
static PyObject *
1832
bytearray_sizeof(PyObject *self, PyObject *Py_UNUSED(ignored))
1833
0
{
1834
0
    return bytearray_sizeof_impl((PyByteArrayObject *)self);
1835
0
}
1836
/*[clinic end generated code: output=5eddefde2a001ceb input=a9049054013a1b77]*/