Coverage Report

Created: 2026-12-01 06:09

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