Coverage Report

Created: 2026-04-12 06:54

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