Coverage Report

Created: 2026-05-30 06:18

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