Coverage Report

Created: 2026-10-17 06:15

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