Coverage Report

Created: 2022-07-08 09:39

/home/mdboom/Work/builds/cpython/Objects/clinic/bytearrayobject.c.h
Line
Count
Source (jump to first uncovered line)
1
/*[clinic input]
2
preserve
3
[clinic start generated code]*/
4
5
static int
6
bytearray___init___impl(PyByteArrayObject *self, PyObject *arg,
7
                        const char *encoding, const char *errors);
8
9
static int
10
bytearray___init__(PyObject *self, PyObject *args, PyObject *kwargs)
11
{
12
    int return_value = -1;
13
    static const char * const _keywords[] = {"source", "encoding", "errors", NULL};
14
    static _PyArg_Parser _parser = {NULL, _keywords, "bytearray", 0};
15
    PyObject *argsbuf[3];
16
    PyObject * const *fastargs;
17
    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
18
    Py_ssize_t noptargs = nargs + (kwargs ? 
PyDict_GET_SIZE39
(kwargs) :
0731k
) - 0;
  Branch (18:36): [True: 39, False: 731k]
19
    PyObject *arg = NULL;
20
    const char *encoding = NULL;
21
    const char *errors = NULL;
22
23
    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 3, 0, argsbuf);
24
    if (!fastargs) {
  Branch (24:9): [True: 0, False: 731k]
25
        goto exit;
26
    }
27
    if (!noptargs) {
  Branch (27:9): [True: 626k, False: 104k]
28
        goto skip_optional_pos;
29
    }
30
    if (fastargs[0]) {
  Branch (30:9): [True: 104k, False: 2]
31
        arg = fastargs[0];
32
        if (!--noptargs) {
  Branch (32:13): [True: 104k, False: 33]
33
            goto skip_optional_pos;
34
        }
35
    }
36
    if (fastargs[1]) {
  Branch (36:9): [True: 31, False: 4]
37
        if (!PyUnicode_Check(fastargs[1])) {
  Branch (37:13): [True: 1, False: 30]
38
            _PyArg_BadArgument("bytearray", "argument 'encoding'", "str", fastargs[1]);
39
            goto exit;
40
        }
41
        Py_ssize_t encoding_length;
42
        encoding = PyUnicode_AsUTF8AndSize(fastargs[1], &encoding_length);
43
        if (encoding == NULL) {
  Branch (43:13): [True: 0, False: 30]
44
            goto exit;
45
        }
46
        if (strlen(encoding) != (size_t)encoding_length) {
  Branch (46:13): [True: 0, False: 30]
47
            PyErr_SetString(PyExc_ValueError, "embedded null character");
48
            goto exit;
49
        }
50
        if (!--noptargs) {
  Branch (50:13): [True: 27, False: 3]
51
            goto skip_optional_pos;
52
        }
53
    }
54
    if (!PyUnicode_Check(fastargs[2])) {
  Branch (54:9): [True: 1, False: 6]
55
        _PyArg_BadArgument("bytearray", "argument 'errors'", "str", fastargs[2]);
56
        goto exit;
57
    }
58
    Py_ssize_t errors_length;
59
    errors = PyUnicode_AsUTF8AndSize(fastargs[2], &errors_length);
60
    if (errors == NULL) {
  Branch (60:9): [True: 0, False: 6]
61
        goto exit;
62
    }
63
    if (strlen(errors) != (size_t)errors_length) {
  Branch (63:9): [True: 0, False: 6]
64
        PyErr_SetString(PyExc_ValueError, "embedded null character");
65
        goto exit;
66
    }
67
skip_optional_pos:
68
    return_value = bytearray___init___impl((PyByteArrayObject *)self, arg, encoding, errors);
69
70
exit:
71
    return return_value;
72
}
73
74
PyDoc_STRVAR(bytearray_clear__doc__,
75
"clear($self, /)\n"
76
"--\n"
77
"\n"
78
"Remove all items from the bytearray.");
79
80
#define BYTEARRAY_CLEAR_METHODDEF    \
81
    {"clear", (PyCFunction)bytearray_clear, METH_NOARGS, bytearray_clear__doc__},
82
83
static PyObject *
84
bytearray_clear_impl(PyByteArrayObject *self);
85
86
static PyObject *
87
bytearray_clear(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
88
{
89
    return bytearray_clear_impl(self);
90
}
91
92
PyDoc_STRVAR(bytearray_copy__doc__,
93
"copy($self, /)\n"
94
"--\n"
95
"\n"
96
"Return a copy of B.");
97
98
#define BYTEARRAY_COPY_METHODDEF    \
99
    {"copy", (PyCFunction)bytearray_copy, METH_NOARGS, bytearray_copy__doc__},
100
101
static PyObject *
102
bytearray_copy_impl(PyByteArrayObject *self);
103
104
static PyObject *
105
bytearray_copy(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
106
{
107
    return bytearray_copy_impl(self);
108
}
109
110
PyDoc_STRVAR(bytearray_removeprefix__doc__,
111
"removeprefix($self, prefix, /)\n"
112
"--\n"
113
"\n"
114
"Return a bytearray with the given prefix string removed if present.\n"
115
"\n"
116
"If the bytearray starts with the prefix string, return\n"
117
"bytearray[len(prefix):].  Otherwise, return a copy of the original\n"
118
"bytearray.");
119
120
#define BYTEARRAY_REMOVEPREFIX_METHODDEF    \
121
    {"removeprefix", (PyCFunction)bytearray_removeprefix, METH_O, bytearray_removeprefix__doc__},
122
123
static PyObject *
124
bytearray_removeprefix_impl(PyByteArrayObject *self, Py_buffer *prefix);
125
126
static PyObject *
127
bytearray_removeprefix(PyByteArrayObject *self, PyObject *arg)
128
{
129
    PyObject *return_value = NULL;
130
    Py_buffer prefix = {NULL, NULL};
131
132
    if (PyObject_GetBuffer(arg, &prefix, PyBUF_SIMPLE) != 0) {
  Branch (132:9): [True: 2, False: 9]
133
        goto exit;
134
    }
135
    if (!PyBuffer_IsContiguous(&prefix, 'C')) {
  Branch (135:9): [True: 0, False: 9]
136
        _PyArg_BadArgument("removeprefix", "argument", "contiguous buffer", arg);
137
        goto exit;
138
    }
139
    return_value = bytearray_removeprefix_impl(self, &prefix);
140
141
exit:
142
    /* Cleanup for prefix */
143
    if (prefix.obj) {
  Branch (143:9): [True: 9, False: 2]
144
       PyBuffer_Release(&prefix);
145
    }
146
147
    return return_value;
148
}
149
150
PyDoc_STRVAR(bytearray_removesuffix__doc__,
151
"removesuffix($self, suffix, /)\n"
152
"--\n"
153
"\n"
154
"Return a bytearray with the given suffix string removed if present.\n"
155
"\n"
156
"If the bytearray ends with the suffix string and that suffix is not\n"
157
"empty, return bytearray[:-len(suffix)].  Otherwise, return a copy of\n"
158
"the original bytearray.");
159
160
#define BYTEARRAY_REMOVESUFFIX_METHODDEF    \
161
    {"removesuffix", (PyCFunction)bytearray_removesuffix, METH_O, bytearray_removesuffix__doc__},
162
163
static PyObject *
164
bytearray_removesuffix_impl(PyByteArrayObject *self, Py_buffer *suffix);
165
166
static PyObject *
167
bytearray_removesuffix(PyByteArrayObject *self, PyObject *arg)
168
{
169
    PyObject *return_value = NULL;
170
    Py_buffer suffix = {NULL, NULL};
171
172
    if (PyObject_GetBuffer(arg, &suffix, PyBUF_SIMPLE) != 0) {
  Branch (172:9): [True: 2, False: 9]
173
        goto exit;
174
    }
175
    if (!PyBuffer_IsContiguous(&suffix, 'C')) {
  Branch (175:9): [True: 0, False: 9]
176
        _PyArg_BadArgument("removesuffix", "argument", "contiguous buffer", arg);
177
        goto exit;
178
    }
179
    return_value = bytearray_removesuffix_impl(self, &suffix);
180
181
exit:
182
    /* Cleanup for suffix */
183
    if (suffix.obj) {
  Branch (183:9): [True: 9, False: 2]
184
       PyBuffer_Release(&suffix);
185
    }
186
187
    return return_value;
188
}
189
190
PyDoc_STRVAR(bytearray_translate__doc__,
191
"translate($self, table, /, delete=b\'\')\n"
192
"--\n"
193
"\n"
194
"Return a copy with each character mapped by the given translation table.\n"
195
"\n"
196
"  table\n"
197
"    Translation table, which must be a bytes object of length 256.\n"
198
"\n"
199
"All characters occurring in the optional argument delete are removed.\n"
200
"The remaining characters are mapped through the given translation table.");
201
202
#define BYTEARRAY_TRANSLATE_METHODDEF    \
203
    {"translate", _PyCFunction_CAST(bytearray_translate), METH_FASTCALL|METH_KEYWORDS, bytearray_translate__doc__},
204
205
static PyObject *
206
bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
207
                         PyObject *deletechars);
208
209
static PyObject *
210
bytearray_translate(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
211
{
212
    PyObject *return_value = NULL;
213
    static const char * const _keywords[] = {"", "delete", NULL};
214
    static _PyArg_Parser _parser = {NULL, _keywords, "translate", 0};
215
    PyObject *argsbuf[2];
216
    Py_ssize_t noptargs = nargs + (kwnames ? 
PyTuple_GET_SIZE3
(kwnames) :
0684
) - 1;
  Branch (216:36): [True: 3, False: 684]
217
    PyObject *table;
218
    PyObject *deletechars = NULL;
219
220
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
221
    if (!args) {
  Branch (221:9): [True: 1, False: 686]
222
        goto exit;
223
    }
224
    table = args[0];
225
    if (!noptargs) {
  Branch (225:9): [True: 676, False: 10]
226
        goto skip_optional_pos;
227
    }
228
    deletechars = args[1];
229
skip_optional_pos:
230
    return_value = bytearray_translate_impl(self, table, deletechars);
231
232
exit:
233
    return return_value;
234
}
235
236
PyDoc_STRVAR(bytearray_maketrans__doc__,
237
"maketrans(frm, to, /)\n"
238
"--\n"
239
"\n"
240
"Return a translation table useable for the bytes or bytearray translate method.\n"
241
"\n"
242
"The returned table will be one where each byte in frm is mapped to the byte at\n"
243
"the same position in to.\n"
244
"\n"
245
"The bytes objects frm and to must be of the same length.");
246
247
#define BYTEARRAY_MAKETRANS_METHODDEF    \
248
    {"maketrans", _PyCFunction_CAST(bytearray_maketrans), METH_FASTCALL|METH_STATIC, bytearray_maketrans__doc__},
249
250
static PyObject *
251
bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to);
252
253
static PyObject *
254
bytearray_maketrans(void *null, PyObject *const *args, Py_ssize_t nargs)
255
{
256
    PyObject *return_value = NULL;
257
    Py_buffer frm = {NULL, NULL};
258
    Py_buffer to = {NULL, NULL};
259
260
    if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) {
261
        goto exit;
262
    }
263
    if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) {
  Branch (263:9): [True: 1, False: 75]
264
        goto exit;
265
    }
266
    if (!PyBuffer_IsContiguous(&frm, 'C')) {
  Branch (266:9): [True: 0, False: 75]
267
        _PyArg_BadArgument("maketrans", "argument 1", "contiguous buffer", args[0]);
268
        goto exit;
269
    }
270
    if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) {
  Branch (270:9): [True: 0, False: 75]
271
        goto exit;
272
    }
273
    if (!PyBuffer_IsContiguous(&to, 'C')) {
  Branch (273:9): [True: 0, False: 75]
274
        _PyArg_BadArgument("maketrans", "argument 2", "contiguous buffer", args[1]);
275
        goto exit;
276
    }
277
    return_value = bytearray_maketrans_impl(&frm, &to);
278
279
exit:
280
    /* Cleanup for frm */
281
    if (frm.obj) {
  Branch (281:9): [True: 75, False: 1]
282
       PyBuffer_Release(&frm);
283
    }
284
    /* Cleanup for to */
285
    if (to.obj) {
  Branch (285:9): [True: 75, False: 1]
286
       PyBuffer_Release(&to);
287
    }
288
289
    return return_value;
290
}
291
292
PyDoc_STRVAR(bytearray_replace__doc__,
293
"replace($self, old, new, count=-1, /)\n"
294
"--\n"
295
"\n"
296
"Return a copy with all occurrences of substring old replaced by new.\n"
297
"\n"
298
"  count\n"
299
"    Maximum number of occurrences to replace.\n"
300
"    -1 (the default value) means replace all occurrences.\n"
301
"\n"
302
"If the optional argument count is given, only the first count occurrences are\n"
303
"replaced.");
304
305
#define BYTEARRAY_REPLACE_METHODDEF    \
306
    {"replace", _PyCFunction_CAST(bytearray_replace), METH_FASTCALL, bytearray_replace__doc__},
307
308
static PyObject *
309
bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
310
                       Py_buffer *new, Py_ssize_t count);
311
312
static PyObject *
313
bytearray_replace(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
314
{
315
    PyObject *return_value = NULL;
316
    Py_buffer old = {NULL, NULL};
317
    Py_buffer new = {NULL, NULL};
318
    Py_ssize_t count = -1;
319
320
    if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
321
        goto exit;
322
    }
323
    if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) {
  Branch (323:9): [True: 2, False: 64.9k]
324
        goto exit;
325
    }
326
    if (!PyBuffer_IsContiguous(&old, 'C')) {
  Branch (326:9): [True: 0, False: 64.9k]
327
        _PyArg_BadArgument("replace", "argument 1", "contiguous buffer", args[0]);
328
        goto exit;
329
    }
330
    if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) {
  Branch (330:9): [True: 1, False: 64.9k]
331
        goto exit;
332
    }
333
    if (!PyBuffer_IsContiguous(&new, 'C')) {
  Branch (333:9): [True: 0, False: 64.9k]
334
        _PyArg_BadArgument("replace", "argument 2", "contiguous buffer", args[1]);
335
        goto exit;
336
    }
337
    if (nargs < 3) {
  Branch (337:9): [True: 64.8k, False: 68]
338
        goto skip_optional;
339
    }
340
    {
341
        Py_ssize_t ival = -1;
342
        PyObject *iobj = _PyNumber_Index(args[2]);
343
        if (iobj != NULL) {
  Branch (343:13): [True: 68, False: 0]
344
            ival = PyLong_AsSsize_t(iobj);
345
            Py_DECREF(iobj);
346
        }
347
        if (ival == -1 && 
PyErr_Occurred()8
) {
  Branch (347:13): [True: 8, False: 60]
  Branch (347:27): [True: 0, False: 8]
348
            goto exit;
349
        }
350
        count = ival;
351
    }
352
skip_optional:
353
    return_value = bytearray_replace_impl(self, &old, &new, count);
354
355
exit:
356
    /* Cleanup for old */
357
    if (old.obj) {
  Branch (357:9): [True: 64.9k, False: 4]
358
       PyBuffer_Release(&old);
359
    }
360
    /* Cleanup for new */
361
    if (new.obj) {
  Branch (361:9): [True: 64.9k, False: 5]
362
       PyBuffer_Release(&new);
363
    }
364
365
    return return_value;
366
}
367
368
PyDoc_STRVAR(bytearray_split__doc__,
369
"split($self, /, sep=None, maxsplit=-1)\n"
370
"--\n"
371
"\n"
372
"Return a list of the sections in the bytearray, using sep as the delimiter.\n"
373
"\n"
374
"  sep\n"
375
"    The delimiter according which to split the bytearray.\n"
376
"    None (the default value) means split on ASCII whitespace characters\n"
377
"    (space, tab, return, newline, formfeed, vertical tab).\n"
378
"  maxsplit\n"
379
"    Maximum number of splits to do.\n"
380
"    -1 (the default value) means no limit.");
381
382
#define BYTEARRAY_SPLIT_METHODDEF    \
383
    {"split", _PyCFunction_CAST(bytearray_split), METH_FASTCALL|METH_KEYWORDS, bytearray_split__doc__},
384
385
static PyObject *
386
bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
387
                     Py_ssize_t maxsplit);
388
389
static PyObject *
390
bytearray_split(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
391
{
392
    PyObject *return_value = NULL;
393
    static const char * const _keywords[] = {"sep", "maxsplit", NULL};
394
    static _PyArg_Parser _parser = {NULL, _keywords, "split", 0};
395
    PyObject *argsbuf[2];
396
    Py_ssize_t noptargs = nargs + (kwnames ? 
PyTuple_GET_SIZE5
(kwnames) :
02.18k
) - 0;
  Branch (396:36): [True: 5, False: 2.18k]
397
    PyObject *sep = Py_None;
398
    Py_ssize_t maxsplit = -1;
399
400
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
401
    if (!args) {
  Branch (401:9): [True: 1, False: 2.18k]
402
        goto exit;
403
    }
404
    if (!noptargs) {
  Branch (404:9): [True: 19, False: 2.16k]
405
        goto skip_optional_pos;
406
    }
407
    if (args[0]) {
  Branch (407:9): [True: 2.16k, False: 1]
408
        sep = args[0];
409
        if (!--noptargs) {
  Branch (409:13): [True: 30, False: 2.13k]
410
            goto skip_optional_pos;
411
        }
412
    }
413
    {
414
        Py_ssize_t ival = -1;
415
        PyObject *iobj = _PyNumber_Index(args[1]);
416
        if (iobj != NULL) {
  Branch (416:13): [True: 2.13k, False: 0]
417
            ival = PyLong_AsSsize_t(iobj);
418
            Py_DECREF(iobj);
419
        }
420
        if (ival == -1 && 
PyErr_Occurred()0
) {
  Branch (420:13): [True: 0, False: 2.13k]
  Branch (420:27): [True: 0, False: 0]
421
            goto exit;
422
        }
423
        maxsplit = ival;
424
    }
425
skip_optional_pos:
426
    return_value = bytearray_split_impl(self, sep, maxsplit);
427
428
exit:
429
    return return_value;
430
}
431
432
PyDoc_STRVAR(bytearray_partition__doc__,
433
"partition($self, sep, /)\n"
434
"--\n"
435
"\n"
436
"Partition the bytearray into three parts using the given separator.\n"
437
"\n"
438
"This will search for the separator sep in the bytearray. If the separator is\n"
439
"found, returns a 3-tuple containing the part before the separator, the\n"
440
"separator itself, and the part after it as new bytearray objects.\n"
441
"\n"
442
"If the separator is not found, returns a 3-tuple containing the copy of the\n"
443
"original bytearray object and two empty bytearray objects.");
444
445
#define BYTEARRAY_PARTITION_METHODDEF    \
446
    {"partition", (PyCFunction)bytearray_partition, METH_O, bytearray_partition__doc__},
447
448
PyDoc_STRVAR(bytearray_rpartition__doc__,
449
"rpartition($self, sep, /)\n"
450
"--\n"
451
"\n"
452
"Partition the bytearray into three parts using the given separator.\n"
453
"\n"
454
"This will search for the separator sep in the bytearray, starting at the end.\n"
455
"If the separator is found, returns a 3-tuple containing the part before the\n"
456
"separator, the separator itself, and the part after it as new bytearray\n"
457
"objects.\n"
458
"\n"
459
"If the separator is not found, returns a 3-tuple containing two empty bytearray\n"
460
"objects and the copy of the original bytearray object.");
461
462
#define BYTEARRAY_RPARTITION_METHODDEF    \
463
    {"rpartition", (PyCFunction)bytearray_rpartition, METH_O, bytearray_rpartition__doc__},
464
465
PyDoc_STRVAR(bytearray_rsplit__doc__,
466
"rsplit($self, /, sep=None, maxsplit=-1)\n"
467
"--\n"
468
"\n"
469
"Return a list of the sections in the bytearray, using sep as the delimiter.\n"
470
"\n"
471
"  sep\n"
472
"    The delimiter according which to split the bytearray.\n"
473
"    None (the default value) means split on ASCII whitespace characters\n"
474
"    (space, tab, return, newline, formfeed, vertical tab).\n"
475
"  maxsplit\n"
476
"    Maximum number of splits to do.\n"
477
"    -1 (the default value) means no limit.\n"
478
"\n"
479
"Splitting is done starting at the end of the bytearray and working to the front.");
480
481
#define BYTEARRAY_RSPLIT_METHODDEF    \
482
    {"rsplit", _PyCFunction_CAST(bytearray_rsplit), METH_FASTCALL|METH_KEYWORDS, bytearray_rsplit__doc__},
483
484
static PyObject *
485
bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
486
                      Py_ssize_t maxsplit);
487
488
static PyObject *
489
bytearray_rsplit(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
490
{
491
    PyObject *return_value = NULL;
492
    static const char * const _keywords[] = {"sep", "maxsplit", NULL};
493
    static _PyArg_Parser _parser = {NULL, _keywords, "rsplit", 0};
494
    PyObject *argsbuf[2];
495
    Py_ssize_t noptargs = nargs + (kwnames ? 
PyTuple_GET_SIZE5
(kwnames) :
083
) - 0;
  Branch (495:36): [True: 5, False: 83]
496
    PyObject *sep = Py_None;
497
    Py_ssize_t maxsplit = -1;
498
499
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
500
    if (!args) {
  Branch (500:9): [True: 1, False: 87]
501
        goto exit;
502
    }
503
    if (!noptargs) {
  Branch (503:9): [True: 13, False: 74]
504
        goto skip_optional_pos;
505
    }
506
    if (args[0]) {
  Branch (506:9): [True: 73, False: 1]
507
        sep = args[0];
508
        if (!--noptargs) {
  Branch (508:13): [True: 29, False: 44]
509
            goto skip_optional_pos;
510
        }
511
    }
512
    {
513
        Py_ssize_t ival = -1;
514
        PyObject *iobj = _PyNumber_Index(args[1]);
515
        if (iobj != NULL) {
  Branch (515:13): [True: 45, False: 0]
516
            ival = PyLong_AsSsize_t(iobj);
517
            Py_DECREF(iobj);
518
        }
519
        if (ival == -1 && 
PyErr_Occurred()0
) {
  Branch (519:13): [True: 0, False: 45]
  Branch (519:27): [True: 0, False: 0]
520
            goto exit;
521
        }
522
        maxsplit = ival;
523
    }
524
skip_optional_pos:
525
    return_value = bytearray_rsplit_impl(self, sep, maxsplit);
526
527
exit:
528
    return return_value;
529
}
530
531
PyDoc_STRVAR(bytearray_reverse__doc__,
532
"reverse($self, /)\n"
533
"--\n"
534
"\n"
535
"Reverse the order of the values in B in place.");
536
537
#define BYTEARRAY_REVERSE_METHODDEF    \
538
    {"reverse", (PyCFunction)bytearray_reverse, METH_NOARGS, bytearray_reverse__doc__},
539
540
static PyObject *
541
bytearray_reverse_impl(PyByteArrayObject *self);
542
543
static PyObject *
544
bytearray_reverse(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
545
{
546
    return bytearray_reverse_impl(self);
547
}
548
549
PyDoc_STRVAR(bytearray_insert__doc__,
550
"insert($self, index, item, /)\n"
551
"--\n"
552
"\n"
553
"Insert a single item into the bytearray before the given index.\n"
554
"\n"
555
"  index\n"
556
"    The index where the value is to be inserted.\n"
557
"  item\n"
558
"    The item to be inserted.");
559
560
#define BYTEARRAY_INSERT_METHODDEF    \
561
    {"insert", _PyCFunction_CAST(bytearray_insert), METH_FASTCALL, bytearray_insert__doc__},
562
563
static PyObject *
564
bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item);
565
566
static PyObject *
567
bytearray_insert(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
568
{
569
    PyObject *return_value = NULL;
570
    Py_ssize_t index;
571
    int item;
572
573
    if (!_PyArg_CheckPositional("insert", nargs, 2, 2)) {
574
        goto exit;
575
    }
576
    {
577
        Py_ssize_t ival = -1;
578
        PyObject *iobj = _PyNumber_Index(args[0]);
579
        if (iobj != NULL) {
  Branch (579:13): [True: 55, False: 0]
580
            ival = PyLong_AsSsize_t(iobj);
581
            Py_DECREF(iobj);
582
        }
583
        if (ival == -1 && 
PyErr_Occurred()0
) {
  Branch (583:13): [True: 0, False: 55]
  Branch (583:27): [True: 0, False: 0]
584
            goto exit;
585
        }
586
        index = ival;
587
    }
588
    if (!_getbytevalue(args[1], &item)) {
  Branch (588:9): [True: 1, False: 54]
589
        goto exit;
590
    }
591
    return_value = bytearray_insert_impl(self, index, item);
592
593
exit:
594
    return return_value;
595
}
596
597
PyDoc_STRVAR(bytearray_append__doc__,
598
"append($self, item, /)\n"
599
"--\n"
600
"\n"
601
"Append a single item to the end of the bytearray.\n"
602
"\n"
603
"  item\n"
604
"    The item to be appended.");
605
606
#define BYTEARRAY_APPEND_METHODDEF    \
607
    {"append", (PyCFunction)bytearray_append, METH_O, bytearray_append__doc__},
608
609
static PyObject *
610
bytearray_append_impl(PyByteArrayObject *self, int item);
611
612
static PyObject *
613
bytearray_append(PyByteArrayObject *self, PyObject *arg)
614
{
615
    PyObject *return_value = NULL;
616
    int item;
617
618
    if (!_getbytevalue(arg, &item)) {
  Branch (618:9): [True: 1, False: 814k]
619
        goto exit;
620
    }
621
    return_value = bytearray_append_impl(self, item);
622
623
exit:
624
    return return_value;
625
}
626
627
PyDoc_STRVAR(bytearray_extend__doc__,
628
"extend($self, iterable_of_ints, /)\n"
629
"--\n"
630
"\n"
631
"Append all the items from the iterator or sequence to the end of the bytearray.\n"
632
"\n"
633
"  iterable_of_ints\n"
634
"    The iterable of items to append.");
635
636
#define BYTEARRAY_EXTEND_METHODDEF    \
637
    {"extend", (PyCFunction)bytearray_extend, METH_O, bytearray_extend__doc__},
638
639
PyDoc_STRVAR(bytearray_pop__doc__,
640
"pop($self, index=-1, /)\n"
641
"--\n"
642
"\n"
643
"Remove and return a single item from B.\n"
644
"\n"
645
"  index\n"
646
"    The index from where to remove the item.\n"
647
"    -1 (the default value) means remove the last item.\n"
648
"\n"
649
"If no index argument is given, will pop the last item.");
650
651
#define BYTEARRAY_POP_METHODDEF    \
652
    {"pop", _PyCFunction_CAST(bytearray_pop), METH_FASTCALL, bytearray_pop__doc__},
653
654
static PyObject *
655
bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index);
656
657
static PyObject *
658
bytearray_pop(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
659
{
660
    PyObject *return_value = NULL;
661
    Py_ssize_t index = -1;
662
663
    if (!_PyArg_CheckPositional("pop", nargs, 0, 1)) {
664
        goto exit;
665
    }
666
    if (nargs < 1) {
  Branch (666:9): [True: 4, False: 4]
667
        goto skip_optional;
668
    }
669
    {
670
        Py_ssize_t ival = -1;
671
        PyObject *iobj = _PyNumber_Index(args[0]);
672
        if (iobj != NULL) {
  Branch (672:13): [True: 4, False: 0]
673
            ival = PyLong_AsSsize_t(iobj);
674
            Py_DECREF(iobj);
675
        }
676
        if (ival == -1 && 
PyErr_Occurred()0
) {
  Branch (676:13): [True: 0, False: 4]
  Branch (676:27): [True: 0, False: 0]
677
            goto exit;
678
        }
679
        index = ival;
680
    }
681
skip_optional:
682
    return_value = bytearray_pop_impl(self, index);
683
684
exit:
685
    return return_value;
686
}
687
688
PyDoc_STRVAR(bytearray_remove__doc__,
689
"remove($self, value, /)\n"
690
"--\n"
691
"\n"
692
"Remove the first occurrence of a value in the bytearray.\n"
693
"\n"
694
"  value\n"
695
"    The value to remove.");
696
697
#define BYTEARRAY_REMOVE_METHODDEF    \
698
    {"remove", (PyCFunction)bytearray_remove, METH_O, bytearray_remove__doc__},
699
700
static PyObject *
701
bytearray_remove_impl(PyByteArrayObject *self, int value);
702
703
static PyObject *
704
bytearray_remove(PyByteArrayObject *self, PyObject *arg)
705
{
706
    PyObject *return_value = NULL;
707
    int value;
708
709
    if (!_getbytevalue(arg, &value)) {
  Branch (709:9): [True: 3, False: 9]
710
        goto exit;
711
    }
712
    return_value = bytearray_remove_impl(self, value);
713
714
exit:
715
    return return_value;
716
}
717
718
PyDoc_STRVAR(bytearray_strip__doc__,
719
"strip($self, bytes=None, /)\n"
720
"--\n"
721
"\n"
722
"Strip leading and trailing bytes contained in the argument.\n"
723
"\n"
724
"If the argument is omitted or None, strip leading and trailing ASCII whitespace.");
725
726
#define BYTEARRAY_STRIP_METHODDEF    \
727
    {"strip", _PyCFunction_CAST(bytearray_strip), METH_FASTCALL, bytearray_strip__doc__},
728
729
static PyObject *
730
bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes);
731
732
static PyObject *
733
bytearray_strip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
734
{
735
    PyObject *return_value = NULL;
736
    PyObject *bytes = Py_None;
737
738
    if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
739
        goto exit;
740
    }
741
    if (nargs < 1) {
  Branch (741:9): [True: 13, False: 10]
742
        goto skip_optional;
743
    }
744
    bytes = args[0];
745
skip_optional:
746
    return_value = bytearray_strip_impl(self, bytes);
747
748
exit:
749
    return return_value;
750
}
751
752
PyDoc_STRVAR(bytearray_lstrip__doc__,
753
"lstrip($self, bytes=None, /)\n"
754
"--\n"
755
"\n"
756
"Strip leading bytes contained in the argument.\n"
757
"\n"
758
"If the argument is omitted or None, strip leading ASCII whitespace.");
759
760
#define BYTEARRAY_LSTRIP_METHODDEF    \
761
    {"lstrip", _PyCFunction_CAST(bytearray_lstrip), METH_FASTCALL, bytearray_lstrip__doc__},
762
763
static PyObject *
764
bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes);
765
766
static PyObject *
767
bytearray_lstrip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
768
{
769
    PyObject *return_value = NULL;
770
    PyObject *bytes = Py_None;
771
772
    if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
773
        goto exit;
774
    }
775
    if (nargs < 1) {
  Branch (775:9): [True: 4, False: 6]
776
        goto skip_optional;
777
    }
778
    bytes = args[0];
779
skip_optional:
780
    return_value = bytearray_lstrip_impl(self, bytes);
781
782
exit:
783
    return return_value;
784
}
785
786
PyDoc_STRVAR(bytearray_rstrip__doc__,
787
"rstrip($self, bytes=None, /)\n"
788
"--\n"
789
"\n"
790
"Strip trailing bytes contained in the argument.\n"
791
"\n"
792
"If the argument is omitted or None, strip trailing ASCII whitespace.");
793
794
#define BYTEARRAY_RSTRIP_METHODDEF    \
795
    {"rstrip", _PyCFunction_CAST(bytearray_rstrip), METH_FASTCALL, bytearray_rstrip__doc__},
796
797
static PyObject *
798
bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes);
799
800
static PyObject *
801
bytearray_rstrip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
802
{
803
    PyObject *return_value = NULL;
804
    PyObject *bytes = Py_None;
805
806
    if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
807
        goto exit;
808
    }
809
    if (nargs < 1) {
  Branch (809:9): [True: 4, False: 79]
810
        goto skip_optional;
811
    }
812
    bytes = args[0];
813
skip_optional:
814
    return_value = bytearray_rstrip_impl(self, bytes);
815
816
exit:
817
    return return_value;
818
}
819
820
PyDoc_STRVAR(bytearray_decode__doc__,
821
"decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
822
"--\n"
823
"\n"
824
"Decode the bytearray using the codec registered for encoding.\n"
825
"\n"
826
"  encoding\n"
827
"    The encoding with which to decode the bytearray.\n"
828
"  errors\n"
829
"    The error handling scheme to use for the handling of decoding errors.\n"
830
"    The default is \'strict\' meaning that decoding errors raise a\n"
831
"    UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n"
832
"    as well as any other name registered with codecs.register_error that\n"
833
"    can handle UnicodeDecodeErrors.");
834
835
#define BYTEARRAY_DECODE_METHODDEF    \
836
    {"decode", _PyCFunction_CAST(bytearray_decode), METH_FASTCALL|METH_KEYWORDS, bytearray_decode__doc__},
837
838
static PyObject *
839
bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
840
                      const char *errors);
841
842
static PyObject *
843
bytearray_decode(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
844
{
845
    PyObject *return_value = NULL;
846
    static const char * const _keywords[] = {"encoding", "errors", NULL};
847
    static _PyArg_Parser _parser = {NULL, _keywords, "decode", 0};
848
    PyObject *argsbuf[2];
849
    Py_ssize_t noptargs = nargs + (kwnames ? 
PyTuple_GET_SIZE1
(kwnames) :
0552k
) - 0;
  Branch (849:36): [True: 1, False: 552k]
850
    const char *encoding = NULL;
851
    const char *errors = NULL;
852
853
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
854
    if (!args) {
  Branch (854:9): [True: 0, False: 552k]
855
        goto exit;
856
    }
857
    if (!noptargs) {
  Branch (857:9): [True: 2.09k, False: 550k]
858
        goto skip_optional_pos;
859
    }
860
    if (args[0]) {
  Branch (860:9): [True: 550k, False: 0]
861
        if (!PyUnicode_Check(args[0])) {
  Branch (861:13): [True: 0, False: 550k]
862
            _PyArg_BadArgument("decode", "argument 'encoding'", "str", args[0]);
863
            goto exit;
864
        }
865
        Py_ssize_t encoding_length;
866
        encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length);
867
        if (encoding == NULL) {
  Branch (867:13): [True: 0, False: 550k]
868
            goto exit;
869
        }
870
        if (strlen(encoding) != (size_t)encoding_length) {
  Branch (870:13): [True: 0, False: 550k]
871
            PyErr_SetString(PyExc_ValueError, "embedded null character");
872
            goto exit;
873
        }
874
        if (!--noptargs) {
  Branch (874:13): [True: 550k, False: 2]
875
            goto skip_optional_pos;
876
        }
877
    }
878
    if (!PyUnicode_Check(args[1])) {
  Branch (878:9): [True: 0, False: 2]
879
        _PyArg_BadArgument("decode", "argument 'errors'", "str", args[1]);
880
        goto exit;
881
    }
882
    Py_ssize_t errors_length;
883
    errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
884
    if (errors == NULL) {
  Branch (884:9): [True: 0, False: 2]
885
        goto exit;
886
    }
887
    if (strlen(errors) != (size_t)errors_length) {
  Branch (887:9): [True: 0, False: 2]
888
        PyErr_SetString(PyExc_ValueError, "embedded null character");
889
        goto exit;
890
    }
891
skip_optional_pos:
892
    return_value = bytearray_decode_impl(self, encoding, errors);
893
894
exit:
895
    return return_value;
896
}
897
898
PyDoc_STRVAR(bytearray_join__doc__,
899
"join($self, iterable_of_bytes, /)\n"
900
"--\n"
901
"\n"
902
"Concatenate any number of bytes/bytearray objects.\n"
903
"\n"
904
"The bytearray whose method is called is inserted in between each pair.\n"
905
"\n"
906
"The result is returned as a new bytearray object.");
907
908
#define BYTEARRAY_JOIN_METHODDEF    \
909
    {"join", (PyCFunction)bytearray_join, METH_O, bytearray_join__doc__},
910
911
PyDoc_STRVAR(bytearray_splitlines__doc__,
912
"splitlines($self, /, keepends=False)\n"
913
"--\n"
914
"\n"
915
"Return a list of the lines in the bytearray, breaking at line boundaries.\n"
916
"\n"
917
"Line breaks are not included in the resulting list unless keepends is given and\n"
918
"true.");
919
920
#define BYTEARRAY_SPLITLINES_METHODDEF    \
921
    {"splitlines", _PyCFunction_CAST(bytearray_splitlines), METH_FASTCALL|METH_KEYWORDS, bytearray_splitlines__doc__},
922
923
static PyObject *
924
bytearray_splitlines_impl(PyByteArrayObject *self, int keepends);
925
926
static PyObject *
927
bytearray_splitlines(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
928
{
929
    PyObject *return_value = NULL;
930
    static const char * const _keywords[] = {"keepends", NULL};
931
    static _PyArg_Parser _parser = {NULL, _keywords, "splitlines", 0};
932
    PyObject *argsbuf[1];
933
    Py_ssize_t noptargs = nargs + (kwnames ? 
PyTuple_GET_SIZE2
(kwnames) :
019
) - 0;
  Branch (933:36): [True: 2, False: 19]
934
    int keepends = 0;
935
936
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
937
    if (!args) {
  Branch (937:9): [True: 1, False: 20]
938
        goto exit;
939
    }
940
    if (!noptargs) {
  Branch (940:9): [True: 16, False: 4]
941
        goto skip_optional_pos;
942
    }
943
    keepends = _PyLong_AsInt(args[0]);
944
    if (keepends == -1 && 
PyErr_Occurred()0
) {
  Branch (944:9): [True: 0, False: 4]
  Branch (944:27): [True: 0, False: 0]
945
        goto exit;
946
    }
947
skip_optional_pos:
948
    return_value = bytearray_splitlines_impl(self, keepends);
949
950
exit:
951
    return return_value;
952
}
953
954
PyDoc_STRVAR(bytearray_fromhex__doc__,
955
"fromhex($type, string, /)\n"
956
"--\n"
957
"\n"
958
"Create a bytearray object from a string of hexadecimal numbers.\n"
959
"\n"
960
"Spaces between two numbers are accepted.\n"
961
"Example: bytearray.fromhex(\'B9 01EF\') -> bytearray(b\'\\\\xb9\\\\x01\\\\xef\')");
962
963
#define BYTEARRAY_FROMHEX_METHODDEF    \
964
    {"fromhex", (PyCFunction)bytearray_fromhex, METH_O|METH_CLASS, bytearray_fromhex__doc__},
965
966
static PyObject *
967
bytearray_fromhex_impl(PyTypeObject *type, PyObject *string);
968
969
static PyObject *
970
bytearray_fromhex(PyTypeObject *type, PyObject *arg)
971
{
972
    PyObject *return_value = NULL;
973
    PyObject *string;
974
975
    if (!PyUnicode_Check(arg)) {
  Branch (975:9): [True: 2, False: 35]
976
        _PyArg_BadArgument("fromhex", "argument", "str", arg);
977
        goto exit;
978
    }
979
    if (PyUnicode_READY(arg) == -1) {
  Branch (979:9): [True: 0, False: 35]
980
        goto exit;
981
    }
982
    string = arg;
983
    return_value = bytearray_fromhex_impl(type, string);
984
985
exit:
986
    return return_value;
987
}
988
989
PyDoc_STRVAR(bytearray_hex__doc__,
990
"hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n"
991
"--\n"
992
"\n"
993
"Create a string of hexadecimal numbers from a bytearray object.\n"
994
"\n"
995
"  sep\n"
996
"    An optional single character or byte to separate hex bytes.\n"
997
"  bytes_per_sep\n"
998
"    How many bytes between separators.  Positive values count from the\n"
999
"    right, negative values count from the left.\n"
1000
"\n"
1001
"Example:\n"
1002
">>> value = bytearray([0xb9, 0x01, 0xef])\n"
1003
">>> value.hex()\n"
1004
"\'b901ef\'\n"
1005
">>> value.hex(\':\')\n"
1006
"\'b9:01:ef\'\n"
1007
">>> value.hex(\':\', 2)\n"
1008
"\'b9:01ef\'\n"
1009
">>> value.hex(\':\', -2)\n"
1010
"\'b901:ef\'");
1011
1012
#define BYTEARRAY_HEX_METHODDEF    \
1013
    {"hex", _PyCFunction_CAST(bytearray_hex), METH_FASTCALL|METH_KEYWORDS, bytearray_hex__doc__},
1014
1015
static PyObject *
1016
bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep);
1017
1018
static PyObject *
1019
bytearray_hex(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1020
{
1021
    PyObject *return_value = NULL;
1022
    static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL};
1023
    static _PyArg_Parser _parser = {NULL, _keywords, "hex", 0};
1024
    PyObject *argsbuf[2];
1025
    Py_ssize_t noptargs = nargs + (kwnames ? 
PyTuple_GET_SIZE0
(kwnames) : 0) - 0;
  Branch (1025:36): [True: 0, False: 46]
1026
    PyObject *sep = NULL;
1027
    int bytes_per_sep = 1;
1028
1029
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
1030
    if (!args) {
  Branch (1030:9): [True: 0, False: 46]
1031
        goto exit;
1032
    }
1033
    if (!noptargs) {
  Branch (1033:9): [True: 8, False: 38]
1034
        goto skip_optional_pos;
1035
    }
1036
    if (args[0]) {
  Branch (1036:9): [True: 38, False: 0]
1037
        sep = args[0];
1038
        if (!--noptargs) {
  Branch (1038:13): [True: 13, False: 25]
1039
            goto skip_optional_pos;
1040
        }
1041
    }
1042
    bytes_per_sep = _PyLong_AsInt(args[1]);
1043
    if (bytes_per_sep == -1 && 
PyErr_Occurred()1
) {
  Branch (1043:9): [True: 1, False: 24]
  Branch (1043:32): [True: 0, False: 1]
1044
        goto exit;
1045
    }
1046
skip_optional_pos:
1047
    return_value = bytearray_hex_impl(self, sep, bytes_per_sep);
1048
1049
exit:
1050
    return return_value;
1051
}
1052
1053
PyDoc_STRVAR(bytearray_reduce__doc__,
1054
"__reduce__($self, /)\n"
1055
"--\n"
1056
"\n"
1057
"Return state information for pickling.");
1058
1059
#define BYTEARRAY_REDUCE_METHODDEF    \
1060
    {"__reduce__", (PyCFunction)bytearray_reduce, METH_NOARGS, bytearray_reduce__doc__},
1061
1062
static PyObject *
1063
bytearray_reduce_impl(PyByteArrayObject *self);
1064
1065
static PyObject *
1066
bytearray_reduce(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
1067
{
1068
    return bytearray_reduce_impl(self);
1069
}
1070
1071
PyDoc_STRVAR(bytearray_reduce_ex__doc__,
1072
"__reduce_ex__($self, proto=0, /)\n"
1073
"--\n"
1074
"\n"
1075
"Return state information for pickling.");
1076
1077
#define BYTEARRAY_REDUCE_EX_METHODDEF    \
1078
    {"__reduce_ex__", _PyCFunction_CAST(bytearray_reduce_ex), METH_FASTCALL, bytearray_reduce_ex__doc__},
1079
1080
static PyObject *
1081
bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto);
1082
1083
static PyObject *
1084
bytearray_reduce_ex(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
1085
{
1086
    PyObject *return_value = NULL;
1087
    int proto = 0;
1088
1089
    if (!_PyArg_CheckPositional("__reduce_ex__", nargs, 0, 1)) {
1090
        goto exit;
1091
    }
1092
    if (nargs < 1) {
  Branch (1092:9): [True: 0, False: 30]
1093
        goto skip_optional;
1094
    }
1095
    proto = _PyLong_AsInt(args[0]);
1096
    if (proto == -1 && 
PyErr_Occurred()0
) {
  Branch (1096:9): [True: 0, False: 30]
  Branch (1096:24): [True: 0, False: 0]
1097
        goto exit;
1098
    }
1099
skip_optional:
1100
    return_value = bytearray_reduce_ex_impl(self, proto);
1101
1102
exit:
1103
    return return_value;
1104
}
1105
1106
PyDoc_STRVAR(bytearray_sizeof__doc__,
1107
"__sizeof__($self, /)\n"
1108
"--\n"
1109
"\n"
1110
"Returns the size of the bytearray object in memory, in bytes.");
1111
1112
#define BYTEARRAY_SIZEOF_METHODDEF    \
1113
    {"__sizeof__", (PyCFunction)bytearray_sizeof, METH_NOARGS, bytearray_sizeof__doc__},
1114
1115
static PyObject *
1116
bytearray_sizeof_impl(PyByteArrayObject *self);
1117
1118
static PyObject *
1119
bytearray_sizeof(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
1120
{
1121
    return bytearray_sizeof_impl(self);
1122
}
1123
/*[clinic end generated code: output=033e9eb5f2bb0139 input=a9049054013a1b77]*/