Coverage Report

Created: 2026-05-16 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Objects/clinic/bytesobject.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_modsupport.h"    // _PyArg_UnpackKeywords()
11
12
PyDoc_STRVAR(bytes___bytes____doc__,
13
"__bytes__($self, /)\n"
14
"--\n"
15
"\n"
16
"Convert this value to exact type bytes.");
17
18
#define BYTES___BYTES___METHODDEF    \
19
    {"__bytes__", (PyCFunction)bytes___bytes__, METH_NOARGS, bytes___bytes____doc__},
20
21
static PyObject *
22
bytes___bytes___impl(PyBytesObject *self);
23
24
static PyObject *
25
bytes___bytes__(PyObject *self, PyObject *Py_UNUSED(ignored))
26
42.8k
{
27
42.8k
    return bytes___bytes___impl((PyBytesObject *)self);
28
42.8k
}
29
30
PyDoc_STRVAR(bytes_split__doc__,
31
"split($self, /, sep=None, maxsplit=-1)\n"
32
"--\n"
33
"\n"
34
"Return a list of the sections in the bytes, using sep as the delimiter.\n"
35
"\n"
36
"  sep\n"
37
"    The delimiter according which to split the bytes.\n"
38
"    None (the default value) means split on ASCII whitespace characters\n"
39
"    (space, tab, return, newline, formfeed, vertical tab).\n"
40
"  maxsplit\n"
41
"    Maximum number of splits to do.\n"
42
"    -1 (the default value) means no limit.");
43
44
#define BYTES_SPLIT_METHODDEF    \
45
    {"split", _PyCFunction_CAST(bytes_split), METH_FASTCALL|METH_KEYWORDS, bytes_split__doc__},
46
47
static PyObject *
48
bytes_split_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit);
49
50
static PyObject *
51
bytes_split(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
52
2.93M
{
53
2.93M
    PyObject *return_value = NULL;
54
2.93M
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
55
56
2.93M
    #define NUM_KEYWORDS 2
57
2.93M
    static struct {
58
2.93M
        PyGC_Head _this_is_not_used;
59
2.93M
        PyObject_VAR_HEAD
60
2.93M
        Py_hash_t ob_hash;
61
2.93M
        PyObject *ob_item[NUM_KEYWORDS];
62
2.93M
    } _kwtuple = {
63
2.93M
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
64
2.93M
        .ob_hash = -1,
65
2.93M
        .ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), },
66
2.93M
    };
67
2.93M
    #undef NUM_KEYWORDS
68
2.93M
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
69
70
    #else  // !Py_BUILD_CORE
71
    #  define KWTUPLE NULL
72
    #endif  // !Py_BUILD_CORE
73
74
2.93M
    static const char * const _keywords[] = {"sep", "maxsplit", NULL};
75
2.93M
    static _PyArg_Parser _parser = {
76
2.93M
        .keywords = _keywords,
77
2.93M
        .fname = "split",
78
2.93M
        .kwtuple = KWTUPLE,
79
2.93M
    };
80
2.93M
    #undef KWTUPLE
81
2.93M
    PyObject *argsbuf[2];
82
2.93M
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
83
2.93M
    PyObject *sep = Py_None;
84
2.93M
    Py_ssize_t maxsplit = -1;
85
86
2.93M
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
87
2.93M
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
88
2.93M
    if (!args) {
89
0
        goto exit;
90
0
    }
91
2.93M
    if (!noptargs) {
92
0
        goto skip_optional_pos;
93
0
    }
94
2.93M
    if (args[0]) {
95
2.93M
        sep = args[0];
96
2.93M
        if (!--noptargs) {
97
2.93M
            goto skip_optional_pos;
98
2.93M
        }
99
2.93M
    }
100
0
    {
101
0
        Py_ssize_t ival = -1;
102
0
        PyObject *iobj = _PyNumber_Index(args[1]);
103
0
        if (iobj != NULL) {
104
0
            ival = PyLong_AsSsize_t(iobj);
105
0
            Py_DECREF(iobj);
106
0
        }
107
0
        if (ival == -1 && PyErr_Occurred()) {
108
0
            goto exit;
109
0
        }
110
0
        maxsplit = ival;
111
0
    }
112
2.93M
skip_optional_pos:
113
2.93M
    return_value = bytes_split_impl((PyBytesObject *)self, sep, maxsplit);
114
115
2.93M
exit:
116
2.93M
    return return_value;
117
2.93M
}
118
119
PyDoc_STRVAR(bytes_partition__doc__,
120
"partition($self, sep, /)\n"
121
"--\n"
122
"\n"
123
"Partition the bytes into three parts using the given separator.\n"
124
"\n"
125
"This will search for the separator sep in the bytes. If the separator is found,\n"
126
"returns a 3-tuple containing the part before the separator, the separator\n"
127
"itself, and the part after it.\n"
128
"\n"
129
"If the separator is not found, returns a 3-tuple containing the original bytes\n"
130
"object and two empty bytes objects.");
131
132
#define BYTES_PARTITION_METHODDEF    \
133
    {"partition", (PyCFunction)bytes_partition, METH_O, bytes_partition__doc__},
134
135
static PyObject *
136
bytes_partition_impl(PyBytesObject *self, Py_buffer *sep);
137
138
static PyObject *
139
bytes_partition(PyObject *self, PyObject *arg)
140
361k
{
141
361k
    PyObject *return_value = NULL;
142
361k
    Py_buffer sep = {NULL, NULL};
143
144
361k
    if (PyObject_GetBuffer(arg, &sep, PyBUF_SIMPLE) != 0) {
145
0
        goto exit;
146
0
    }
147
361k
    return_value = bytes_partition_impl((PyBytesObject *)self, &sep);
148
149
361k
exit:
150
    /* Cleanup for sep */
151
361k
    if (sep.obj) {
152
361k
       PyBuffer_Release(&sep);
153
361k
    }
154
155
361k
    return return_value;
156
361k
}
157
158
PyDoc_STRVAR(bytes_rpartition__doc__,
159
"rpartition($self, sep, /)\n"
160
"--\n"
161
"\n"
162
"Partition the bytes into three parts using the given separator.\n"
163
"\n"
164
"This will search for the separator sep in the bytes, starting at the end. If\n"
165
"the separator is found, returns a 3-tuple containing the part before the\n"
166
"separator, the separator itself, and the part after it.\n"
167
"\n"
168
"If the separator is not found, returns a 3-tuple containing two empty bytes\n"
169
"objects and the original bytes object.");
170
171
#define BYTES_RPARTITION_METHODDEF    \
172
    {"rpartition", (PyCFunction)bytes_rpartition, METH_O, bytes_rpartition__doc__},
173
174
static PyObject *
175
bytes_rpartition_impl(PyBytesObject *self, Py_buffer *sep);
176
177
static PyObject *
178
bytes_rpartition(PyObject *self, PyObject *arg)
179
0
{
180
0
    PyObject *return_value = NULL;
181
0
    Py_buffer sep = {NULL, NULL};
182
183
0
    if (PyObject_GetBuffer(arg, &sep, PyBUF_SIMPLE) != 0) {
184
0
        goto exit;
185
0
    }
186
0
    return_value = bytes_rpartition_impl((PyBytesObject *)self, &sep);
187
188
0
exit:
189
    /* Cleanup for sep */
190
0
    if (sep.obj) {
191
0
       PyBuffer_Release(&sep);
192
0
    }
193
194
0
    return return_value;
195
0
}
196
197
PyDoc_STRVAR(bytes_rsplit__doc__,
198
"rsplit($self, /, sep=None, maxsplit=-1)\n"
199
"--\n"
200
"\n"
201
"Return a list of the sections in the bytes, using sep as the delimiter.\n"
202
"\n"
203
"  sep\n"
204
"    The delimiter according which to split the bytes.\n"
205
"    None (the default value) means split on ASCII whitespace characters\n"
206
"    (space, tab, return, newline, formfeed, vertical tab).\n"
207
"  maxsplit\n"
208
"    Maximum number of splits to do.\n"
209
"    -1 (the default value) means no limit.\n"
210
"\n"
211
"Splitting is done starting at the end of the bytes and working to the front.");
212
213
#define BYTES_RSPLIT_METHODDEF    \
214
    {"rsplit", _PyCFunction_CAST(bytes_rsplit), METH_FASTCALL|METH_KEYWORDS, bytes_rsplit__doc__},
215
216
static PyObject *
217
bytes_rsplit_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit);
218
219
static PyObject *
220
bytes_rsplit(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
221
0
{
222
0
    PyObject *return_value = NULL;
223
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
224
225
0
    #define NUM_KEYWORDS 2
226
0
    static struct {
227
0
        PyGC_Head _this_is_not_used;
228
0
        PyObject_VAR_HEAD
229
0
        Py_hash_t ob_hash;
230
0
        PyObject *ob_item[NUM_KEYWORDS];
231
0
    } _kwtuple = {
232
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
233
0
        .ob_hash = -1,
234
0
        .ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), },
235
0
    };
236
0
    #undef NUM_KEYWORDS
237
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
238
239
    #else  // !Py_BUILD_CORE
240
    #  define KWTUPLE NULL
241
    #endif  // !Py_BUILD_CORE
242
243
0
    static const char * const _keywords[] = {"sep", "maxsplit", NULL};
244
0
    static _PyArg_Parser _parser = {
245
0
        .keywords = _keywords,
246
0
        .fname = "rsplit",
247
0
        .kwtuple = KWTUPLE,
248
0
    };
249
0
    #undef KWTUPLE
250
0
    PyObject *argsbuf[2];
251
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
252
0
    PyObject *sep = Py_None;
253
0
    Py_ssize_t maxsplit = -1;
254
255
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
256
0
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
257
0
    if (!args) {
258
0
        goto exit;
259
0
    }
260
0
    if (!noptargs) {
261
0
        goto skip_optional_pos;
262
0
    }
263
0
    if (args[0]) {
264
0
        sep = args[0];
265
0
        if (!--noptargs) {
266
0
            goto skip_optional_pos;
267
0
        }
268
0
    }
269
0
    {
270
0
        Py_ssize_t ival = -1;
271
0
        PyObject *iobj = _PyNumber_Index(args[1]);
272
0
        if (iobj != NULL) {
273
0
            ival = PyLong_AsSsize_t(iobj);
274
0
            Py_DECREF(iobj);
275
0
        }
276
0
        if (ival == -1 && PyErr_Occurred()) {
277
0
            goto exit;
278
0
        }
279
0
        maxsplit = ival;
280
0
    }
281
0
skip_optional_pos:
282
0
    return_value = bytes_rsplit_impl((PyBytesObject *)self, sep, maxsplit);
283
284
0
exit:
285
0
    return return_value;
286
0
}
287
288
PyDoc_STRVAR(bytes_join__doc__,
289
"join($self, iterable_of_bytes, /)\n"
290
"--\n"
291
"\n"
292
"Concatenate any number of bytes objects.\n"
293
"\n"
294
"The bytes whose method is called is inserted in between each pair.\n"
295
"\n"
296
"The result is returned as a new bytes object.\n"
297
"\n"
298
"Example: b\'.\'.join([b\'ab\', b\'pq\', b\'rs\']) -> b\'ab.pq.rs\'.");
299
300
#define BYTES_JOIN_METHODDEF    \
301
    {"join", (PyCFunction)bytes_join, METH_O, bytes_join__doc__},
302
303
static PyObject *
304
bytes_join_impl(PyBytesObject *self, PyObject *iterable_of_bytes);
305
306
static PyObject *
307
bytes_join(PyObject *self, PyObject *iterable_of_bytes)
308
269k
{
309
269k
    PyObject *return_value = NULL;
310
311
269k
    return_value = bytes_join_impl((PyBytesObject *)self, iterable_of_bytes);
312
313
269k
    return return_value;
314
269k
}
315
316
PyDoc_STRVAR(bytes_find__doc__,
317
"find($self, sub[, start[, end]], /)\n"
318
"--\n"
319
"\n"
320
"Return the lowest index in B where subsection \'sub\' is found, such that \'sub\' is contained within B[start,end].\n"
321
"\n"
322
"  start\n"
323
"    Optional start position. Default: start of the bytes.\n"
324
"  end\n"
325
"    Optional stop position. Default: end of the bytes.\n"
326
"\n"
327
"Return -1 on failure.");
328
329
#define BYTES_FIND_METHODDEF    \
330
    {"find", _PyCFunction_CAST(bytes_find), METH_FASTCALL, bytes_find__doc__},
331
332
static PyObject *
333
bytes_find_impl(PyBytesObject *self, PyObject *sub, Py_ssize_t start,
334
                Py_ssize_t end);
335
336
static PyObject *
337
bytes_find(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
338
12.9M
{
339
12.9M
    PyObject *return_value = NULL;
340
12.9M
    PyObject *sub;
341
12.9M
    Py_ssize_t start = 0;
342
12.9M
    Py_ssize_t end = PY_SSIZE_T_MAX;
343
344
12.9M
    if (!_PyArg_CheckPositional("find", nargs, 1, 3)) {
345
0
        goto exit;
346
0
    }
347
12.9M
    sub = args[0];
348
12.9M
    if (nargs < 2) {
349
12.9M
        goto skip_optional;
350
12.9M
    }
351
32.1k
    if (!_PyEval_SliceIndex(args[1], &start)) {
352
0
        goto exit;
353
0
    }
354
32.1k
    if (nargs < 3) {
355
32.1k
        goto skip_optional;
356
32.1k
    }
357
0
    if (!_PyEval_SliceIndex(args[2], &end)) {
358
0
        goto exit;
359
0
    }
360
12.9M
skip_optional:
361
12.9M
    return_value = bytes_find_impl((PyBytesObject *)self, sub, start, end);
362
363
12.9M
exit:
364
12.9M
    return return_value;
365
12.9M
}
366
367
PyDoc_STRVAR(bytes_index__doc__,
368
"index($self, sub[, start[, end]], /)\n"
369
"--\n"
370
"\n"
371
"Return the lowest index in B where subsection \'sub\' is found, such that \'sub\' is contained within B[start,end].\n"
372
"\n"
373
"  start\n"
374
"    Optional start position. Default: start of the bytes.\n"
375
"  end\n"
376
"    Optional stop position. Default: end of the bytes.\n"
377
"\n"
378
"Raise ValueError if the subsection is not found.");
379
380
#define BYTES_INDEX_METHODDEF    \
381
    {"index", _PyCFunction_CAST(bytes_index), METH_FASTCALL, bytes_index__doc__},
382
383
static PyObject *
384
bytes_index_impl(PyBytesObject *self, PyObject *sub, Py_ssize_t start,
385
                 Py_ssize_t end);
386
387
static PyObject *
388
bytes_index(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
389
0
{
390
0
    PyObject *return_value = NULL;
391
0
    PyObject *sub;
392
0
    Py_ssize_t start = 0;
393
0
    Py_ssize_t end = PY_SSIZE_T_MAX;
394
395
0
    if (!_PyArg_CheckPositional("index", nargs, 1, 3)) {
396
0
        goto exit;
397
0
    }
398
0
    sub = args[0];
399
0
    if (nargs < 2) {
400
0
        goto skip_optional;
401
0
    }
402
0
    if (!_PyEval_SliceIndex(args[1], &start)) {
403
0
        goto exit;
404
0
    }
405
0
    if (nargs < 3) {
406
0
        goto skip_optional;
407
0
    }
408
0
    if (!_PyEval_SliceIndex(args[2], &end)) {
409
0
        goto exit;
410
0
    }
411
0
skip_optional:
412
0
    return_value = bytes_index_impl((PyBytesObject *)self, sub, start, end);
413
414
0
exit:
415
0
    return return_value;
416
0
}
417
418
PyDoc_STRVAR(bytes_rfind__doc__,
419
"rfind($self, sub[, start[, end]], /)\n"
420
"--\n"
421
"\n"
422
"Return the highest index in B where subsection \'sub\' is found, such that \'sub\' is contained within B[start,end].\n"
423
"\n"
424
"  start\n"
425
"    Optional start position. Default: start of the bytes.\n"
426
"  end\n"
427
"    Optional stop position. Default: end of the bytes.\n"
428
"\n"
429
"Return -1 on failure.");
430
431
#define BYTES_RFIND_METHODDEF    \
432
    {"rfind", _PyCFunction_CAST(bytes_rfind), METH_FASTCALL, bytes_rfind__doc__},
433
434
static PyObject *
435
bytes_rfind_impl(PyBytesObject *self, PyObject *sub, Py_ssize_t start,
436
                 Py_ssize_t end);
437
438
static PyObject *
439
bytes_rfind(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
440
279k
{
441
279k
    PyObject *return_value = NULL;
442
279k
    PyObject *sub;
443
279k
    Py_ssize_t start = 0;
444
279k
    Py_ssize_t end = PY_SSIZE_T_MAX;
445
446
279k
    if (!_PyArg_CheckPositional("rfind", nargs, 1, 3)) {
447
0
        goto exit;
448
0
    }
449
279k
    sub = args[0];
450
279k
    if (nargs < 2) {
451
279k
        goto skip_optional;
452
279k
    }
453
0
    if (!_PyEval_SliceIndex(args[1], &start)) {
454
0
        goto exit;
455
0
    }
456
0
    if (nargs < 3) {
457
0
        goto skip_optional;
458
0
    }
459
0
    if (!_PyEval_SliceIndex(args[2], &end)) {
460
0
        goto exit;
461
0
    }
462
279k
skip_optional:
463
279k
    return_value = bytes_rfind_impl((PyBytesObject *)self, sub, start, end);
464
465
279k
exit:
466
279k
    return return_value;
467
279k
}
468
469
PyDoc_STRVAR(bytes_rindex__doc__,
470
"rindex($self, sub[, start[, end]], /)\n"
471
"--\n"
472
"\n"
473
"Return the highest index in B where subsection \'sub\' is found, such that \'sub\' is contained within B[start,end].\n"
474
"\n"
475
"  start\n"
476
"    Optional start position. Default: start of the bytes.\n"
477
"  end\n"
478
"    Optional stop position. Default: end of the bytes.\n"
479
"\n"
480
"Raise ValueError if the subsection is not found.");
481
482
#define BYTES_RINDEX_METHODDEF    \
483
    {"rindex", _PyCFunction_CAST(bytes_rindex), METH_FASTCALL, bytes_rindex__doc__},
484
485
static PyObject *
486
bytes_rindex_impl(PyBytesObject *self, PyObject *sub, Py_ssize_t start,
487
                  Py_ssize_t end);
488
489
static PyObject *
490
bytes_rindex(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
491
0
{
492
0
    PyObject *return_value = NULL;
493
0
    PyObject *sub;
494
0
    Py_ssize_t start = 0;
495
0
    Py_ssize_t end = PY_SSIZE_T_MAX;
496
497
0
    if (!_PyArg_CheckPositional("rindex", nargs, 1, 3)) {
498
0
        goto exit;
499
0
    }
500
0
    sub = args[0];
501
0
    if (nargs < 2) {
502
0
        goto skip_optional;
503
0
    }
504
0
    if (!_PyEval_SliceIndex(args[1], &start)) {
505
0
        goto exit;
506
0
    }
507
0
    if (nargs < 3) {
508
0
        goto skip_optional;
509
0
    }
510
0
    if (!_PyEval_SliceIndex(args[2], &end)) {
511
0
        goto exit;
512
0
    }
513
0
skip_optional:
514
0
    return_value = bytes_rindex_impl((PyBytesObject *)self, sub, start, end);
515
516
0
exit:
517
0
    return return_value;
518
0
}
519
520
PyDoc_STRVAR(bytes_strip__doc__,
521
"strip($self, bytes=None, /)\n"
522
"--\n"
523
"\n"
524
"Strip leading and trailing bytes contained in the argument.\n"
525
"\n"
526
"If the argument is omitted or None, strip leading and trailing ASCII whitespace.");
527
528
#define BYTES_STRIP_METHODDEF    \
529
    {"strip", _PyCFunction_CAST(bytes_strip), METH_FASTCALL, bytes_strip__doc__},
530
531
static PyObject *
532
bytes_strip_impl(PyBytesObject *self, PyObject *bytes);
533
534
static PyObject *
535
bytes_strip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
536
0
{
537
0
    PyObject *return_value = NULL;
538
0
    PyObject *bytes = Py_None;
539
540
0
    if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
541
0
        goto exit;
542
0
    }
543
0
    if (nargs < 1) {
544
0
        goto skip_optional;
545
0
    }
546
0
    bytes = args[0];
547
0
skip_optional:
548
0
    return_value = bytes_strip_impl((PyBytesObject *)self, bytes);
549
550
0
exit:
551
0
    return return_value;
552
0
}
553
554
PyDoc_STRVAR(bytes_lstrip__doc__,
555
"lstrip($self, bytes=None, /)\n"
556
"--\n"
557
"\n"
558
"Strip leading bytes contained in the argument.\n"
559
"\n"
560
"If the argument is omitted or None, strip leading  ASCII whitespace.");
561
562
#define BYTES_LSTRIP_METHODDEF    \
563
    {"lstrip", _PyCFunction_CAST(bytes_lstrip), METH_FASTCALL, bytes_lstrip__doc__},
564
565
static PyObject *
566
bytes_lstrip_impl(PyBytesObject *self, PyObject *bytes);
567
568
static PyObject *
569
bytes_lstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
570
0
{
571
0
    PyObject *return_value = NULL;
572
0
    PyObject *bytes = Py_None;
573
574
0
    if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
575
0
        goto exit;
576
0
    }
577
0
    if (nargs < 1) {
578
0
        goto skip_optional;
579
0
    }
580
0
    bytes = args[0];
581
0
skip_optional:
582
0
    return_value = bytes_lstrip_impl((PyBytesObject *)self, bytes);
583
584
0
exit:
585
0
    return return_value;
586
0
}
587
588
PyDoc_STRVAR(bytes_rstrip__doc__,
589
"rstrip($self, bytes=None, /)\n"
590
"--\n"
591
"\n"
592
"Strip trailing bytes contained in the argument.\n"
593
"\n"
594
"If the argument is omitted or None, strip trailing ASCII whitespace.");
595
596
#define BYTES_RSTRIP_METHODDEF    \
597
    {"rstrip", _PyCFunction_CAST(bytes_rstrip), METH_FASTCALL, bytes_rstrip__doc__},
598
599
static PyObject *
600
bytes_rstrip_impl(PyBytesObject *self, PyObject *bytes);
601
602
static PyObject *
603
bytes_rstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
604
294
{
605
294
    PyObject *return_value = NULL;
606
294
    PyObject *bytes = Py_None;
607
608
294
    if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
609
0
        goto exit;
610
0
    }
611
294
    if (nargs < 1) {
612
0
        goto skip_optional;
613
0
    }
614
294
    bytes = args[0];
615
294
skip_optional:
616
294
    return_value = bytes_rstrip_impl((PyBytesObject *)self, bytes);
617
618
294
exit:
619
294
    return return_value;
620
294
}
621
622
PyDoc_STRVAR(bytes_count__doc__,
623
"count($self, sub[, start[, end]], /)\n"
624
"--\n"
625
"\n"
626
"Return the number of non-overlapping occurrences of subsection \'sub\' in bytes B[start:end].\n"
627
"\n"
628
"  start\n"
629
"    Optional start position. Default: start of the bytes.\n"
630
"  end\n"
631
"    Optional stop position. Default: end of the bytes.");
632
633
#define BYTES_COUNT_METHODDEF    \
634
    {"count", _PyCFunction_CAST(bytes_count), METH_FASTCALL, bytes_count__doc__},
635
636
static PyObject *
637
bytes_count_impl(PyBytesObject *self, PyObject *sub, Py_ssize_t start,
638
                 Py_ssize_t end);
639
640
static PyObject *
641
bytes_count(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
642
5.69M
{
643
5.69M
    PyObject *return_value = NULL;
644
5.69M
    PyObject *sub;
645
5.69M
    Py_ssize_t start = 0;
646
5.69M
    Py_ssize_t end = PY_SSIZE_T_MAX;
647
648
5.69M
    if (!_PyArg_CheckPositional("count", nargs, 1, 3)) {
649
0
        goto exit;
650
0
    }
651
5.69M
    sub = args[0];
652
5.69M
    if (nargs < 2) {
653
5.69M
        goto skip_optional;
654
5.69M
    }
655
0
    if (!_PyEval_SliceIndex(args[1], &start)) {
656
0
        goto exit;
657
0
    }
658
0
    if (nargs < 3) {
659
0
        goto skip_optional;
660
0
    }
661
0
    if (!_PyEval_SliceIndex(args[2], &end)) {
662
0
        goto exit;
663
0
    }
664
5.69M
skip_optional:
665
5.69M
    return_value = bytes_count_impl((PyBytesObject *)self, sub, start, end);
666
667
5.69M
exit:
668
5.69M
    return return_value;
669
5.69M
}
670
671
PyDoc_STRVAR(bytes_translate__doc__,
672
"translate($self, table, /, delete=b\'\')\n"
673
"--\n"
674
"\n"
675
"Return a copy with each character mapped by the given translation table.\n"
676
"\n"
677
"  table\n"
678
"    Translation table, which must be a bytes object of length 256.\n"
679
"\n"
680
"All characters occurring in the optional argument delete are removed.\n"
681
"The remaining characters are mapped through the given translation table.");
682
683
#define BYTES_TRANSLATE_METHODDEF    \
684
    {"translate", _PyCFunction_CAST(bytes_translate), METH_FASTCALL|METH_KEYWORDS, bytes_translate__doc__},
685
686
static PyObject *
687
bytes_translate_impl(PyBytesObject *self, PyObject *table,
688
                     PyObject *deletechars);
689
690
static PyObject *
691
bytes_translate(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
692
0
{
693
0
    PyObject *return_value = NULL;
694
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
695
696
0
    #define NUM_KEYWORDS 1
697
0
    static struct {
698
0
        PyGC_Head _this_is_not_used;
699
0
        PyObject_VAR_HEAD
700
0
        Py_hash_t ob_hash;
701
0
        PyObject *ob_item[NUM_KEYWORDS];
702
0
    } _kwtuple = {
703
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
704
0
        .ob_hash = -1,
705
0
        .ob_item = { &_Py_ID(delete), },
706
0
    };
707
0
    #undef NUM_KEYWORDS
708
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
709
710
    #else  // !Py_BUILD_CORE
711
    #  define KWTUPLE NULL
712
    #endif  // !Py_BUILD_CORE
713
714
0
    static const char * const _keywords[] = {"", "delete", NULL};
715
0
    static _PyArg_Parser _parser = {
716
0
        .keywords = _keywords,
717
0
        .fname = "translate",
718
0
        .kwtuple = KWTUPLE,
719
0
    };
720
0
    #undef KWTUPLE
721
0
    PyObject *argsbuf[2];
722
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
723
0
    PyObject *table;
724
0
    PyObject *deletechars = NULL;
725
726
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
727
0
            /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
728
0
    if (!args) {
729
0
        goto exit;
730
0
    }
731
0
    table = args[0];
732
0
    if (!noptargs) {
733
0
        goto skip_optional_pos;
734
0
    }
735
0
    deletechars = args[1];
736
0
skip_optional_pos:
737
0
    return_value = bytes_translate_impl((PyBytesObject *)self, table, deletechars);
738
739
0
exit:
740
0
    return return_value;
741
0
}
742
743
PyDoc_STRVAR(bytes_maketrans__doc__,
744
"maketrans(frm, to, /)\n"
745
"--\n"
746
"\n"
747
"Return a translation table usable for the bytes or bytearray translate method.\n"
748
"\n"
749
"The returned table will be one where each byte in frm is mapped to the byte at\n"
750
"the same position in to.\n"
751
"\n"
752
"The bytes objects frm and to must be of the same length.");
753
754
#define BYTES_MAKETRANS_METHODDEF    \
755
    {"maketrans", _PyCFunction_CAST(bytes_maketrans), METH_FASTCALL|METH_STATIC, bytes_maketrans__doc__},
756
757
static PyObject *
758
bytes_maketrans_impl(Py_buffer *frm, Py_buffer *to);
759
760
static PyObject *
761
bytes_maketrans(PyObject *null, PyObject *const *args, Py_ssize_t nargs)
762
9
{
763
9
    PyObject *return_value = NULL;
764
9
    Py_buffer frm = {NULL, NULL};
765
9
    Py_buffer to = {NULL, NULL};
766
767
9
    if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) {
768
0
        goto exit;
769
0
    }
770
9
    if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) {
771
0
        goto exit;
772
0
    }
773
9
    if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) {
774
0
        goto exit;
775
0
    }
776
9
    return_value = bytes_maketrans_impl(&frm, &to);
777
778
9
exit:
779
    /* Cleanup for frm */
780
9
    if (frm.obj) {
781
9
       PyBuffer_Release(&frm);
782
9
    }
783
    /* Cleanup for to */
784
9
    if (to.obj) {
785
9
       PyBuffer_Release(&to);
786
9
    }
787
788
9
    return return_value;
789
9
}
790
791
PyDoc_STRVAR(bytes_replace__doc__,
792
"replace($self, old, new, /, count=-1)\n"
793
"--\n"
794
"\n"
795
"Return a copy with all occurrences of substring old replaced by new.\n"
796
"\n"
797
"  count\n"
798
"    Maximum number of occurrences to replace.\n"
799
"    -1 (the default value) means replace all occurrences.\n"
800
"\n"
801
"If count is given, only the first count occurrences are replaced.\n"
802
"If count is not specified or -1, then all occurrences are replaced.");
803
804
#define BYTES_REPLACE_METHODDEF    \
805
    {"replace", _PyCFunction_CAST(bytes_replace), METH_FASTCALL|METH_KEYWORDS, bytes_replace__doc__},
806
807
static PyObject *
808
bytes_replace_impl(PyBytesObject *self, Py_buffer *old, Py_buffer *new,
809
                   Py_ssize_t count);
810
811
static PyObject *
812
bytes_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
813
34.0k
{
814
34.0k
    PyObject *return_value = NULL;
815
34.0k
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
816
817
34.0k
    #define NUM_KEYWORDS 1
818
34.0k
    static struct {
819
34.0k
        PyGC_Head _this_is_not_used;
820
34.0k
        PyObject_VAR_HEAD
821
34.0k
        Py_hash_t ob_hash;
822
34.0k
        PyObject *ob_item[NUM_KEYWORDS];
823
34.0k
    } _kwtuple = {
824
34.0k
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
825
34.0k
        .ob_hash = -1,
826
34.0k
        .ob_item = { &_Py_ID(count), },
827
34.0k
    };
828
34.0k
    #undef NUM_KEYWORDS
829
34.0k
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
830
831
    #else  // !Py_BUILD_CORE
832
    #  define KWTUPLE NULL
833
    #endif  // !Py_BUILD_CORE
834
835
34.0k
    static const char * const _keywords[] = {"", "", "count", NULL};
836
34.0k
    static _PyArg_Parser _parser = {
837
34.0k
        .keywords = _keywords,
838
34.0k
        .fname = "replace",
839
34.0k
        .kwtuple = KWTUPLE,
840
34.0k
    };
841
34.0k
    #undef KWTUPLE
842
34.0k
    PyObject *argsbuf[3];
843
34.0k
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
844
34.0k
    Py_buffer old = {NULL, NULL};
845
34.0k
    Py_buffer new = {NULL, NULL};
846
34.0k
    Py_ssize_t count = -1;
847
848
34.0k
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
849
34.0k
            /*minpos*/ 2, /*maxpos*/ 3, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
850
34.0k
    if (!args) {
851
0
        goto exit;
852
0
    }
853
34.0k
    if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) {
854
0
        goto exit;
855
0
    }
856
34.0k
    if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) {
857
0
        goto exit;
858
0
    }
859
34.0k
    if (!noptargs) {
860
34.0k
        goto skip_optional_pos;
861
34.0k
    }
862
0
    {
863
0
        Py_ssize_t ival = -1;
864
0
        PyObject *iobj = _PyNumber_Index(args[2]);
865
0
        if (iobj != NULL) {
866
0
            ival = PyLong_AsSsize_t(iobj);
867
0
            Py_DECREF(iobj);
868
0
        }
869
0
        if (ival == -1 && PyErr_Occurred()) {
870
0
            goto exit;
871
0
        }
872
0
        count = ival;
873
0
    }
874
34.0k
skip_optional_pos:
875
34.0k
    return_value = bytes_replace_impl((PyBytesObject *)self, &old, &new, count);
876
877
34.0k
exit:
878
    /* Cleanup for old */
879
34.0k
    if (old.obj) {
880
34.0k
       PyBuffer_Release(&old);
881
34.0k
    }
882
    /* Cleanup for new */
883
34.0k
    if (new.obj) {
884
34.0k
       PyBuffer_Release(&new);
885
34.0k
    }
886
887
34.0k
    return return_value;
888
34.0k
}
889
890
PyDoc_STRVAR(bytes_removeprefix__doc__,
891
"removeprefix($self, prefix, /)\n"
892
"--\n"
893
"\n"
894
"Return a bytes object with the given prefix string removed if present.\n"
895
"\n"
896
"If the bytes starts with the prefix string, return bytes[len(prefix):].\n"
897
"Otherwise, return a copy of the original bytes.");
898
899
#define BYTES_REMOVEPREFIX_METHODDEF    \
900
    {"removeprefix", (PyCFunction)bytes_removeprefix, METH_O, bytes_removeprefix__doc__},
901
902
static PyObject *
903
bytes_removeprefix_impl(PyBytesObject *self, Py_buffer *prefix);
904
905
static PyObject *
906
bytes_removeprefix(PyObject *self, PyObject *arg)
907
0
{
908
0
    PyObject *return_value = NULL;
909
0
    Py_buffer prefix = {NULL, NULL};
910
911
0
    if (PyObject_GetBuffer(arg, &prefix, PyBUF_SIMPLE) != 0) {
912
0
        goto exit;
913
0
    }
914
0
    return_value = bytes_removeprefix_impl((PyBytesObject *)self, &prefix);
915
916
0
exit:
917
    /* Cleanup for prefix */
918
0
    if (prefix.obj) {
919
0
       PyBuffer_Release(&prefix);
920
0
    }
921
922
0
    return return_value;
923
0
}
924
925
PyDoc_STRVAR(bytes_removesuffix__doc__,
926
"removesuffix($self, suffix, /)\n"
927
"--\n"
928
"\n"
929
"Return a bytes object with the given suffix string removed if present.\n"
930
"\n"
931
"If the bytes ends with the suffix string and that suffix is not empty,\n"
932
"return bytes[:-len(prefix)].  Otherwise, return a copy of the original\n"
933
"bytes.");
934
935
#define BYTES_REMOVESUFFIX_METHODDEF    \
936
    {"removesuffix", (PyCFunction)bytes_removesuffix, METH_O, bytes_removesuffix__doc__},
937
938
static PyObject *
939
bytes_removesuffix_impl(PyBytesObject *self, Py_buffer *suffix);
940
941
static PyObject *
942
bytes_removesuffix(PyObject *self, PyObject *arg)
943
0
{
944
0
    PyObject *return_value = NULL;
945
0
    Py_buffer suffix = {NULL, NULL};
946
947
0
    if (PyObject_GetBuffer(arg, &suffix, PyBUF_SIMPLE) != 0) {
948
0
        goto exit;
949
0
    }
950
0
    return_value = bytes_removesuffix_impl((PyBytesObject *)self, &suffix);
951
952
0
exit:
953
    /* Cleanup for suffix */
954
0
    if (suffix.obj) {
955
0
       PyBuffer_Release(&suffix);
956
0
    }
957
958
0
    return return_value;
959
0
}
960
961
PyDoc_STRVAR(bytes_startswith__doc__,
962
"startswith($self, prefix[, start[, end]], /)\n"
963
"--\n"
964
"\n"
965
"Return True if the bytes starts with the specified prefix, False otherwise.\n"
966
"\n"
967
"  prefix\n"
968
"    A bytes or a tuple of bytes to try.\n"
969
"  start\n"
970
"    Optional start position. Default: start of the bytes.\n"
971
"  end\n"
972
"    Optional stop position. Default: end of the bytes.");
973
974
#define BYTES_STARTSWITH_METHODDEF    \
975
    {"startswith", _PyCFunction_CAST(bytes_startswith), METH_FASTCALL, bytes_startswith__doc__},
976
977
static PyObject *
978
bytes_startswith_impl(PyBytesObject *self, PyObject *subobj,
979
                      Py_ssize_t start, Py_ssize_t end);
980
981
static PyObject *
982
bytes_startswith(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
983
1.93M
{
984
1.93M
    PyObject *return_value = NULL;
985
1.93M
    PyObject *subobj;
986
1.93M
    Py_ssize_t start = 0;
987
1.93M
    Py_ssize_t end = PY_SSIZE_T_MAX;
988
989
1.93M
    if (!_PyArg_CheckPositional("startswith", nargs, 1, 3)) {
990
0
        goto exit;
991
0
    }
992
1.93M
    subobj = args[0];
993
1.93M
    if (nargs < 2) {
994
1.93M
        goto skip_optional;
995
1.93M
    }
996
0
    if (!_PyEval_SliceIndex(args[1], &start)) {
997
0
        goto exit;
998
0
    }
999
0
    if (nargs < 3) {
1000
0
        goto skip_optional;
1001
0
    }
1002
0
    if (!_PyEval_SliceIndex(args[2], &end)) {
1003
0
        goto exit;
1004
0
    }
1005
1.93M
skip_optional:
1006
1.93M
    return_value = bytes_startswith_impl((PyBytesObject *)self, subobj, start, end);
1007
1008
1.93M
exit:
1009
1.93M
    return return_value;
1010
1.93M
}
1011
1012
PyDoc_STRVAR(bytes_endswith__doc__,
1013
"endswith($self, suffix[, start[, end]], /)\n"
1014
"--\n"
1015
"\n"
1016
"Return True if the bytes ends with the specified suffix, False otherwise.\n"
1017
"\n"
1018
"  suffix\n"
1019
"    A bytes or a tuple of bytes to try.\n"
1020
"  start\n"
1021
"    Optional start position. Default: start of the bytes.\n"
1022
"  end\n"
1023
"    Optional stop position. Default: end of the bytes.");
1024
1025
#define BYTES_ENDSWITH_METHODDEF    \
1026
    {"endswith", _PyCFunction_CAST(bytes_endswith), METH_FASTCALL, bytes_endswith__doc__},
1027
1028
static PyObject *
1029
bytes_endswith_impl(PyBytesObject *self, PyObject *subobj, Py_ssize_t start,
1030
                    Py_ssize_t end);
1031
1032
static PyObject *
1033
bytes_endswith(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1034
315
{
1035
315
    PyObject *return_value = NULL;
1036
315
    PyObject *subobj;
1037
315
    Py_ssize_t start = 0;
1038
315
    Py_ssize_t end = PY_SSIZE_T_MAX;
1039
1040
315
    if (!_PyArg_CheckPositional("endswith", nargs, 1, 3)) {
1041
0
        goto exit;
1042
0
    }
1043
315
    subobj = args[0];
1044
315
    if (nargs < 2) {
1045
315
        goto skip_optional;
1046
315
    }
1047
0
    if (!_PyEval_SliceIndex(args[1], &start)) {
1048
0
        goto exit;
1049
0
    }
1050
0
    if (nargs < 3) {
1051
0
        goto skip_optional;
1052
0
    }
1053
0
    if (!_PyEval_SliceIndex(args[2], &end)) {
1054
0
        goto exit;
1055
0
    }
1056
315
skip_optional:
1057
315
    return_value = bytes_endswith_impl((PyBytesObject *)self, subobj, start, end);
1058
1059
315
exit:
1060
315
    return return_value;
1061
315
}
1062
1063
PyDoc_STRVAR(bytes_decode__doc__,
1064
"decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
1065
"--\n"
1066
"\n"
1067
"Decode the bytes using the codec registered for encoding.\n"
1068
"\n"
1069
"  encoding\n"
1070
"    The encoding with which to decode the bytes.\n"
1071
"  errors\n"
1072
"    The error handling scheme to use for the handling of decoding errors.\n"
1073
"    The default is \'strict\' meaning that decoding errors raise a\n"
1074
"    UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n"
1075
"    as well as any other name registered with codecs.register_error that\n"
1076
"    can handle UnicodeDecodeErrors.");
1077
1078
#define BYTES_DECODE_METHODDEF    \
1079
    {"decode", _PyCFunction_CAST(bytes_decode), METH_FASTCALL|METH_KEYWORDS, bytes_decode__doc__},
1080
1081
static PyObject *
1082
bytes_decode_impl(PyBytesObject *self, const char *encoding,
1083
                  const char *errors);
1084
1085
static PyObject *
1086
bytes_decode(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1087
18.4M
{
1088
18.4M
    PyObject *return_value = NULL;
1089
18.4M
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1090
1091
18.4M
    #define NUM_KEYWORDS 2
1092
18.4M
    static struct {
1093
18.4M
        PyGC_Head _this_is_not_used;
1094
18.4M
        PyObject_VAR_HEAD
1095
18.4M
        Py_hash_t ob_hash;
1096
18.4M
        PyObject *ob_item[NUM_KEYWORDS];
1097
18.4M
    } _kwtuple = {
1098
18.4M
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1099
18.4M
        .ob_hash = -1,
1100
18.4M
        .ob_item = { &_Py_ID(encoding), &_Py_ID(errors), },
1101
18.4M
    };
1102
18.4M
    #undef NUM_KEYWORDS
1103
18.4M
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1104
1105
    #else  // !Py_BUILD_CORE
1106
    #  define KWTUPLE NULL
1107
    #endif  // !Py_BUILD_CORE
1108
1109
18.4M
    static const char * const _keywords[] = {"encoding", "errors", NULL};
1110
18.4M
    static _PyArg_Parser _parser = {
1111
18.4M
        .keywords = _keywords,
1112
18.4M
        .fname = "decode",
1113
18.4M
        .kwtuple = KWTUPLE,
1114
18.4M
    };
1115
18.4M
    #undef KWTUPLE
1116
18.4M
    PyObject *argsbuf[2];
1117
18.4M
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1118
18.4M
    const char *encoding = NULL;
1119
18.4M
    const char *errors = NULL;
1120
1121
18.4M
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1122
18.4M
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1123
18.4M
    if (!args) {
1124
0
        goto exit;
1125
0
    }
1126
18.4M
    if (!noptargs) {
1127
64.5k
        goto skip_optional_pos;
1128
64.5k
    }
1129
18.3M
    if (args[0]) {
1130
18.3M
        if (!PyUnicode_Check(args[0])) {
1131
0
            _PyArg_BadArgument("decode", "argument 'encoding'", "str", args[0]);
1132
0
            goto exit;
1133
0
        }
1134
18.3M
        Py_ssize_t encoding_length;
1135
18.3M
        encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length);
1136
18.3M
        if (encoding == NULL) {
1137
0
            goto exit;
1138
0
        }
1139
18.3M
        if (strlen(encoding) != (size_t)encoding_length) {
1140
1.12k
            PyErr_SetString(PyExc_ValueError, "embedded null character");
1141
1.12k
            goto exit;
1142
1.12k
        }
1143
18.3M
        if (!--noptargs) {
1144
1.39M
            goto skip_optional_pos;
1145
1.39M
        }
1146
18.3M
    }
1147
16.9M
    if (!PyUnicode_Check(args[1])) {
1148
0
        _PyArg_BadArgument("decode", "argument 'errors'", "str", args[1]);
1149
0
        goto exit;
1150
0
    }
1151
16.9M
    Py_ssize_t errors_length;
1152
16.9M
    errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
1153
16.9M
    if (errors == NULL) {
1154
0
        goto exit;
1155
0
    }
1156
16.9M
    if (strlen(errors) != (size_t)errors_length) {
1157
0
        PyErr_SetString(PyExc_ValueError, "embedded null character");
1158
0
        goto exit;
1159
0
    }
1160
18.4M
skip_optional_pos:
1161
18.4M
    return_value = bytes_decode_impl((PyBytesObject *)self, encoding, errors);
1162
1163
18.4M
exit:
1164
18.4M
    return return_value;
1165
18.4M
}
1166
1167
PyDoc_STRVAR(bytes_splitlines__doc__,
1168
"splitlines($self, /, keepends=False)\n"
1169
"--\n"
1170
"\n"
1171
"Return a list of the lines in the bytes, breaking at line boundaries.\n"
1172
"\n"
1173
"Line breaks are not included in the resulting list unless keepends is given and\n"
1174
"true.");
1175
1176
#define BYTES_SPLITLINES_METHODDEF    \
1177
    {"splitlines", _PyCFunction_CAST(bytes_splitlines), METH_FASTCALL|METH_KEYWORDS, bytes_splitlines__doc__},
1178
1179
static PyObject *
1180
bytes_splitlines_impl(PyBytesObject *self, int keepends);
1181
1182
static PyObject *
1183
bytes_splitlines(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1184
0
{
1185
0
    PyObject *return_value = NULL;
1186
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1187
1188
0
    #define NUM_KEYWORDS 1
1189
0
    static struct {
1190
0
        PyGC_Head _this_is_not_used;
1191
0
        PyObject_VAR_HEAD
1192
0
        Py_hash_t ob_hash;
1193
0
        PyObject *ob_item[NUM_KEYWORDS];
1194
0
    } _kwtuple = {
1195
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1196
0
        .ob_hash = -1,
1197
0
        .ob_item = { &_Py_ID(keepends), },
1198
0
    };
1199
0
    #undef NUM_KEYWORDS
1200
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1201
1202
    #else  // !Py_BUILD_CORE
1203
    #  define KWTUPLE NULL
1204
    #endif  // !Py_BUILD_CORE
1205
1206
0
    static const char * const _keywords[] = {"keepends", NULL};
1207
0
    static _PyArg_Parser _parser = {
1208
0
        .keywords = _keywords,
1209
0
        .fname = "splitlines",
1210
0
        .kwtuple = KWTUPLE,
1211
0
    };
1212
0
    #undef KWTUPLE
1213
0
    PyObject *argsbuf[1];
1214
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1215
0
    int keepends = 0;
1216
1217
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1218
0
            /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1219
0
    if (!args) {
1220
0
        goto exit;
1221
0
    }
1222
0
    if (!noptargs) {
1223
0
        goto skip_optional_pos;
1224
0
    }
1225
0
    keepends = PyObject_IsTrue(args[0]);
1226
0
    if (keepends < 0) {
1227
0
        goto exit;
1228
0
    }
1229
0
skip_optional_pos:
1230
0
    return_value = bytes_splitlines_impl((PyBytesObject *)self, keepends);
1231
1232
0
exit:
1233
0
    return return_value;
1234
0
}
1235
1236
PyDoc_STRVAR(bytes_fromhex__doc__,
1237
"fromhex($type, string, /)\n"
1238
"--\n"
1239
"\n"
1240
"Create a bytes object from a string of hexadecimal numbers.\n"
1241
"\n"
1242
"Spaces between two numbers are accepted.\n"
1243
"Example: bytes.fromhex(\'B9 01EF\') -> b\'\\\\xb9\\\\x01\\\\xef\'.");
1244
1245
#define BYTES_FROMHEX_METHODDEF    \
1246
    {"fromhex", (PyCFunction)bytes_fromhex, METH_O|METH_CLASS, bytes_fromhex__doc__},
1247
1248
static PyObject *
1249
bytes_fromhex_impl(PyTypeObject *type, PyObject *string);
1250
1251
static PyObject *
1252
bytes_fromhex(PyObject *type, PyObject *string)
1253
33.2k
{
1254
33.2k
    PyObject *return_value = NULL;
1255
1256
33.2k
    return_value = bytes_fromhex_impl((PyTypeObject *)type, string);
1257
1258
33.2k
    return return_value;
1259
33.2k
}
1260
1261
PyDoc_STRVAR(bytes_hex__doc__,
1262
"hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n"
1263
"--\n"
1264
"\n"
1265
"Create a string of hexadecimal numbers from a bytes object.\n"
1266
"\n"
1267
"  sep\n"
1268
"    An optional single character or byte to separate hex bytes.\n"
1269
"  bytes_per_sep\n"
1270
"    How many bytes between separators.  Positive values count from the\n"
1271
"    right, negative values count from the left.\n"
1272
"\n"
1273
"Example:\n"
1274
">>> value = b\'\\xb9\\x01\\xef\'\n"
1275
">>> value.hex()\n"
1276
"\'b901ef\'\n"
1277
">>> value.hex(\':\')\n"
1278
"\'b9:01:ef\'\n"
1279
">>> value.hex(\':\', 2)\n"
1280
"\'b9:01ef\'\n"
1281
">>> value.hex(\':\', -2)\n"
1282
"\'b901:ef\'");
1283
1284
#define BYTES_HEX_METHODDEF    \
1285
    {"hex", _PyCFunction_CAST(bytes_hex), METH_FASTCALL|METH_KEYWORDS, bytes_hex__doc__},
1286
1287
static PyObject *
1288
bytes_hex_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t bytes_per_sep);
1289
1290
static PyObject *
1291
bytes_hex(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1292
0
{
1293
0
    PyObject *return_value = NULL;
1294
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1295
1296
0
    #define NUM_KEYWORDS 2
1297
0
    static struct {
1298
0
        PyGC_Head _this_is_not_used;
1299
0
        PyObject_VAR_HEAD
1300
0
        Py_hash_t ob_hash;
1301
0
        PyObject *ob_item[NUM_KEYWORDS];
1302
0
    } _kwtuple = {
1303
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1304
0
        .ob_hash = -1,
1305
0
        .ob_item = { &_Py_ID(sep), &_Py_ID(bytes_per_sep), },
1306
0
    };
1307
0
    #undef NUM_KEYWORDS
1308
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1309
1310
    #else  // !Py_BUILD_CORE
1311
    #  define KWTUPLE NULL
1312
    #endif  // !Py_BUILD_CORE
1313
1314
0
    static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL};
1315
0
    static _PyArg_Parser _parser = {
1316
0
        .keywords = _keywords,
1317
0
        .fname = "hex",
1318
0
        .kwtuple = KWTUPLE,
1319
0
    };
1320
0
    #undef KWTUPLE
1321
0
    PyObject *argsbuf[2];
1322
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1323
0
    PyObject *sep = NULL;
1324
0
    Py_ssize_t bytes_per_sep = 1;
1325
1326
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1327
0
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1328
0
    if (!args) {
1329
0
        goto exit;
1330
0
    }
1331
0
    if (!noptargs) {
1332
0
        goto skip_optional_pos;
1333
0
    }
1334
0
    if (args[0]) {
1335
0
        sep = args[0];
1336
0
        if (!--noptargs) {
1337
0
            goto skip_optional_pos;
1338
0
        }
1339
0
    }
1340
0
    {
1341
0
        Py_ssize_t ival = -1;
1342
0
        PyObject *iobj = _PyNumber_Index(args[1]);
1343
0
        if (iobj != NULL) {
1344
0
            ival = PyLong_AsSsize_t(iobj);
1345
0
            Py_DECREF(iobj);
1346
0
        }
1347
0
        if (ival == -1 && PyErr_Occurred()) {
1348
0
            goto exit;
1349
0
        }
1350
0
        bytes_per_sep = ival;
1351
0
    }
1352
0
skip_optional_pos:
1353
0
    return_value = bytes_hex_impl((PyBytesObject *)self, sep, bytes_per_sep);
1354
1355
0
exit:
1356
0
    return return_value;
1357
0
}
1358
1359
static PyObject *
1360
bytes_new_impl(PyTypeObject *type, PyObject *x, const char *encoding,
1361
               const char *errors);
1362
1363
static PyObject *
1364
bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1365
16.3M
{
1366
16.3M
    PyObject *return_value = NULL;
1367
16.3M
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1368
1369
16.3M
    #define NUM_KEYWORDS 3
1370
16.3M
    static struct {
1371
16.3M
        PyGC_Head _this_is_not_used;
1372
16.3M
        PyObject_VAR_HEAD
1373
16.3M
        Py_hash_t ob_hash;
1374
16.3M
        PyObject *ob_item[NUM_KEYWORDS];
1375
16.3M
    } _kwtuple = {
1376
16.3M
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1377
16.3M
        .ob_hash = -1,
1378
16.3M
        .ob_item = { &_Py_ID(source), &_Py_ID(encoding), &_Py_ID(errors), },
1379
16.3M
    };
1380
16.3M
    #undef NUM_KEYWORDS
1381
16.3M
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1382
1383
    #else  // !Py_BUILD_CORE
1384
    #  define KWTUPLE NULL
1385
    #endif  // !Py_BUILD_CORE
1386
1387
16.3M
    static const char * const _keywords[] = {"source", "encoding", "errors", NULL};
1388
16.3M
    static _PyArg_Parser _parser = {
1389
16.3M
        .keywords = _keywords,
1390
16.3M
        .fname = "bytes",
1391
16.3M
        .kwtuple = KWTUPLE,
1392
16.3M
    };
1393
16.3M
    #undef KWTUPLE
1394
16.3M
    PyObject *argsbuf[3];
1395
16.3M
    PyObject * const *fastargs;
1396
16.3M
    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
1397
16.3M
    Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
1398
16.3M
    PyObject *x = NULL;
1399
16.3M
    const char *encoding = NULL;
1400
16.3M
    const char *errors = NULL;
1401
1402
16.3M
    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
1403
16.3M
            /*minpos*/ 0, /*maxpos*/ 3, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1404
16.3M
    if (!fastargs) {
1405
0
        goto exit;
1406
0
    }
1407
16.3M
    if (!noptargs) {
1408
0
        goto skip_optional_pos;
1409
0
    }
1410
16.3M
    if (fastargs[0]) {
1411
16.3M
        x = fastargs[0];
1412
16.3M
        if (!--noptargs) {
1413
16.0M
            goto skip_optional_pos;
1414
16.0M
        }
1415
16.3M
    }
1416
323k
    if (fastargs[1]) {
1417
323k
        if (!PyUnicode_Check(fastargs[1])) {
1418
0
            _PyArg_BadArgument("bytes", "argument 'encoding'", "str", fastargs[1]);
1419
0
            goto exit;
1420
0
        }
1421
323k
        Py_ssize_t encoding_length;
1422
323k
        encoding = PyUnicode_AsUTF8AndSize(fastargs[1], &encoding_length);
1423
323k
        if (encoding == NULL) {
1424
0
            goto exit;
1425
0
        }
1426
323k
        if (strlen(encoding) != (size_t)encoding_length) {
1427
0
            PyErr_SetString(PyExc_ValueError, "embedded null character");
1428
0
            goto exit;
1429
0
        }
1430
323k
        if (!--noptargs) {
1431
323k
            goto skip_optional_pos;
1432
323k
        }
1433
323k
    }
1434
0
    if (!PyUnicode_Check(fastargs[2])) {
1435
0
        _PyArg_BadArgument("bytes", "argument 'errors'", "str", fastargs[2]);
1436
0
        goto exit;
1437
0
    }
1438
0
    Py_ssize_t errors_length;
1439
0
    errors = PyUnicode_AsUTF8AndSize(fastargs[2], &errors_length);
1440
0
    if (errors == NULL) {
1441
0
        goto exit;
1442
0
    }
1443
0
    if (strlen(errors) != (size_t)errors_length) {
1444
0
        PyErr_SetString(PyExc_ValueError, "embedded null character");
1445
0
        goto exit;
1446
0
    }
1447
16.3M
skip_optional_pos:
1448
16.3M
    return_value = bytes_new_impl(type, x, encoding, errors);
1449
1450
16.3M
exit:
1451
16.3M
    return return_value;
1452
16.3M
}
1453
/*[clinic end generated code: output=b252801ff04a89b3 input=a9049054013a1b77]*/